1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "private-lib-core.h"
26
27 lws_usec_t
lws_now_usecs(void)28 lws_now_usecs(void)
29 {
30 struct timeval tv;
31 gettimeofday(&tv, NULL);
32 return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
33 }
34
35 size_t
lws_get_random(struct lws_context * context,void * buf,size_t len)36 lws_get_random(struct lws_context *context, void *buf, size_t len)
37 {
38 #if defined(LWS_WITH_ESP32)
39 uint8_t *pb = buf;
40
41 while (len) {
42 uint32_t r = esp_random();
43 uint8_t *p = (uint8_t *)&r;
44 int b = 4;
45
46 if (len < b)
47 b = len;
48
49 len -= b;
50
51 while (b--)
52 *pb++ = p[b];
53 }
54
55 return pb - (uint8_t *)buf;
56 #else
57 int n;
58
59 n = mbedtls_ctr_drbg_random(&context->mcdc, buf, len);
60 if (!n)
61 return len;
62
63 /* failed */
64
65 lwsl_err("%s: mbedtls_ctr_drbg_random returned 0x%x\n", __func__, n);
66
67 return 0;
68 #endif
69 }
70
71
lwsl_emit_syslog(int level,const char * line)72 void lwsl_emit_syslog(int level, const char *line)
73 {
74 lwsl_emit_stderr(level, line);
75 }
76
77 int
lws_plat_drop_app_privileges(struct lws_context * context,int actually_init)78 lws_plat_drop_app_privileges(struct lws_context *context, int actually_init)
79 {
80 return 0;
81 }
82
83 int
lws_plat_recommended_rsa_bits(void)84 lws_plat_recommended_rsa_bits(void)
85 {
86 /*
87 * 2048-bit key generation takes up to a minute on ESP32, 4096
88 * is like 15 minutes +
89 */
90 return 2048;
91 }
92