From 683f65f8ac06148eb9fa283309840e16ec3f421d Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Sat, 31 Aug 2024 12:21:12 +0100 Subject: [PATCH] Make mg_random() fallible. Abort TLS on weak RNG --- examples/infineon/infineon-xmc7200/hal.c | 3 +- .../same54-xpro/device-dashboard/main.c | 3 +- .../microchip/same54-xpro/mqtt-client/main.c | 3 +- .../frdm-mcxn947-make-baremetal-builtin/hal.c | 3 +- .../frdm-mcxn947-make-freertos-builtin/hal.c | 3 +- .../source/hal.c | 3 +- .../source/hal.c | 3 +- .../rt1020-evk-make-baremetal-builtin/main.c | 3 +- .../rt1020-evk-make-freertos-builtin/main.c | 3 +- .../rt1060-evk-make-baremetal-builtin/main.c | 4 +- .../rt1060-evk-make-freertos-builtin/main.c | 3 +- .../source/main.c | 3 +- .../ek-ra6m4-make-baremetal-builtin/hal.c | 3 +- examples/rp2040/pico-w5500/main.c | 3 +- .../Core/Src/main.c | 3 +- .../Core/Src/main.c | 3 +- .../Core/Src/main.c | 3 +- .../stm32/nucleo-f429zi-keil-baremetal/main.c | 3 +- .../stm32/nucleo-f429zi-keil-freertos/main.c | 3 +- .../main.c | 5 +- .../main.c | 3 +- .../main.c | 3 +- .../Core/Src/main.c | 3 +- .../Core/Src/main.c | 3 +- .../Core/Src/main.c | 3 +- .../stm32/nucleo-f746zg-keil-baremetal/main.c | 3 +- .../nucleo-f746zg-keil-freertos-lwip/main.c | 9 ++-- .../nucleo-f746zg-keil-freertos-tcp/main.c | 9 ++-- .../stm32/nucleo-f746zg-keil-freertos/main.c | 3 +- .../main.c | 12 ++--- .../nucleo-f746zg-keil-freertos_cmsis2/main.c | 3 +- .../stm32/nucleo-f746zg-keil-rtx-mdk/main.c | 9 ++-- examples/stm32/nucleo-f746zg-keil-rtx/main.c | 3 +- .../stm32/nucleo-f746zg-keil-rtx5-lwip/main.c | 12 ++--- .../stm32/nucleo-f746zg-keil-rtx5-mdk/main.c | 10 ++-- examples/stm32/nucleo-f746zg-keil-rtx5/main.c | 3 +- .../main.c | 3 +- .../main.c | 5 +- .../main.c | 3 +- .../main.c | 3 +- .../nucleo-f746zg-make-freertos-tcp/main.c | 3 +- .../nucleo-g031-make-baremetal-builtin/main.c | 3 +- .../main.c | 3 +- .../main.c | 3 +- .../main.c | 3 +- .../main.c | 3 +- .../Core/Src/main.c | 3 +- .../Core/Src/main.c | 3 +- .../main.c | 3 +- .../main.c | 3 +- .../main.c | 3 +- .../main.c | 3 +- .../main.c | 3 +- .../wch/ch32v307-make-baremetal-builtin/hal.c | 3 +- mongoose.c | 47 +++++++++++++++---- mongoose.h | 20 ++++++-- src/arch_win32.h | 18 ++++++- src/tls_builtin.c | 8 ++-- src/util.c | 39 +++++++++++++-- src/util.h | 2 +- test/Makefile | 13 ++--- tutorials/http/http-server/Makefile | 7 ++- 62 files changed, 253 insertions(+), 111 deletions(-) diff --git a/examples/infineon/infineon-xmc7200/hal.c b/examples/infineon/infineon-xmc7200/hal.c index d0240e6a..2755b460 100644 --- a/examples/infineon/infineon-xmc7200/hal.c +++ b/examples/infineon/infineon-xmc7200/hal.c @@ -8,11 +8,12 @@ void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms s_ticks++; } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } uint64_t mg_millis(void) { // Let Mongoose use our uptime function diff --git a/examples/microchip/same54-xpro/device-dashboard/main.c b/examples/microchip/same54-xpro/device-dashboard/main.c index 14e54b49..c56150bb 100644 --- a/examples/microchip/same54-xpro/device-dashboard/main.c +++ b/examples/microchip/same54-xpro/device-dashboard/main.c @@ -22,11 +22,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/microchip/same54-xpro/mqtt-client/main.c b/examples/microchip/same54-xpro/mqtt-client/main.c index 1f86bc32..037c5d7a 100644 --- a/examples/microchip/same54-xpro/mqtt-client/main.c +++ b/examples/microchip/same54-xpro/mqtt-client/main.c @@ -24,11 +24,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void fn(struct mg_connection *c, int ev, void *ev_data) { diff --git a/examples/nxp/frdm-mcxn947-make-baremetal-builtin/hal.c b/examples/nxp/frdm-mcxn947-make-baremetal-builtin/hal.c index aa465716..45560b09 100644 --- a/examples/nxp/frdm-mcxn947-make-baremetal-builtin/hal.c +++ b/examples/nxp/frdm-mcxn947-make-baremetal-builtin/hal.c @@ -9,11 +9,12 @@ void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms } #if 0 -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } #endif diff --git a/examples/nxp/frdm-mcxn947-make-freertos-builtin/hal.c b/examples/nxp/frdm-mcxn947-make-freertos-builtin/hal.c index 3c32b0a3..a1ff1914 100644 --- a/examples/nxp/frdm-mcxn947-make-freertos-builtin/hal.c +++ b/examples/nxp/frdm-mcxn947-make-freertos-builtin/hal.c @@ -9,11 +9,12 @@ void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms s_ticks++; } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } uint64_t mg_millis(void) { // Let Mongoose use our uptime function diff --git a/examples/nxp/frdm-mcxn947-xpresso-baremetal-builtin/source/hal.c b/examples/nxp/frdm-mcxn947-xpresso-baremetal-builtin/source/hal.c index aa465716..45560b09 100644 --- a/examples/nxp/frdm-mcxn947-xpresso-baremetal-builtin/source/hal.c +++ b/examples/nxp/frdm-mcxn947-xpresso-baremetal-builtin/source/hal.c @@ -9,11 +9,12 @@ void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms } #if 0 -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } #endif diff --git a/examples/nxp/frdm-mcxn947-xpresso-freertos-builtin/source/hal.c b/examples/nxp/frdm-mcxn947-xpresso-freertos-builtin/source/hal.c index 316721b2..9e6903b8 100644 --- a/examples/nxp/frdm-mcxn947-xpresso-freertos-builtin/source/hal.c +++ b/examples/nxp/frdm-mcxn947-xpresso-freertos-builtin/source/hal.c @@ -9,11 +9,12 @@ void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms s_ticks++; } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } uint64_t mg_millis(void) { // Let Mongoose use our uptime function diff --git a/examples/nxp/rt1020-evk-make-baremetal-builtin/main.c b/examples/nxp/rt1020-evk-make-baremetal-builtin/main.c index 3fd164e5..1fca6513 100644 --- a/examples/nxp/rt1020-evk-make-baremetal-builtin/main.c +++ b/examples/nxp/rt1020-evk-make-baremetal-builtin/main.c @@ -16,11 +16,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { gpio_toggle(LED); // Blink LED diff --git a/examples/nxp/rt1020-evk-make-freertos-builtin/main.c b/examples/nxp/rt1020-evk-make-freertos-builtin/main.c index 2ef15ff7..9a497361 100644 --- a/examples/nxp/rt1020-evk-make-freertos-builtin/main.c +++ b/examples/nxp/rt1020-evk-make-freertos-builtin/main.c @@ -7,11 +7,12 @@ #define BLINK_PERIOD_MS 1000 // LED blinking period in millis -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/nxp/rt1060-evk-make-baremetal-builtin/main.c b/examples/nxp/rt1060-evk-make-baremetal-builtin/main.c index 3fd164e5..7a66465a 100644 --- a/examples/nxp/rt1060-evk-make-baremetal-builtin/main.c +++ b/examples/nxp/rt1060-evk-make-baremetal-builtin/main.c @@ -16,12 +16,14 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } + static void timer_fn(void *arg) { gpio_toggle(LED); // Blink LED struct mg_tcpip_if *ifp = arg; // And show diff --git a/examples/nxp/rt1060-evk-make-freertos-builtin/main.c b/examples/nxp/rt1060-evk-make-freertos-builtin/main.c index 497f6fbe..96be22d6 100644 --- a/examples/nxp/rt1060-evk-make-freertos-builtin/main.c +++ b/examples/nxp/rt1060-evk-make-freertos-builtin/main.c @@ -15,11 +15,12 @@ void SysTick_Handler(void) { xPortSysTickHandler(); } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/nxp/rt1060-evk-xpresso-baremetal-builtin/source/main.c b/examples/nxp/rt1060-evk-xpresso-baremetal-builtin/source/main.c index c83c335f..ba76eaca 100644 --- a/examples/nxp/rt1060-evk-xpresso-baremetal-builtin/source/main.c +++ b/examples/nxp/rt1060-evk-xpresso-baremetal-builtin/source/main.c @@ -33,8 +33,9 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG TRNG_GetRandomData(TRNG, buf, len); // Init by BOARD_InitBootPeripherals() + return true; } static void timer_fn(void *arg) { diff --git a/examples/renesas/ek-ra6m4-make-baremetal-builtin/hal.c b/examples/renesas/ek-ra6m4-make-baremetal-builtin/hal.c index a326f37f..ac8d6b0b 100644 --- a/examples/renesas/ek-ra6m4-make-baremetal-builtin/hal.c +++ b/examples/renesas/ek-ra6m4-make-baremetal-builtin/hal.c @@ -47,11 +47,12 @@ void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms } #if 0 -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } #endif diff --git a/examples/rp2040/pico-w5500/main.c b/examples/rp2040/pico-w5500/main.c index 3c79e67f..81586803 100644 --- a/examples/rp2040/pico-w5500/main.c +++ b/examples/rp2040/pico-w5500/main.c @@ -26,11 +26,12 @@ static uint8_t spi_txn(void *spi, uint8_t byte) { return result; } -void mg_random(void *buf, size_t len) { +bool mg_random(void *buf, size_t len) { for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = get_rand_32(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f429zi-cube-baremetal-builtin/Core/Src/main.c b/examples/stm32/nucleo-f429zi-cube-baremetal-builtin/Core/Src/main.c index 3bab391a..b44edf0a 100644 --- a/examples/stm32/nucleo-f429zi-cube-baremetal-builtin/Core/Src/main.c +++ b/examples/stm32/nucleo-f429zi-cube-baremetal-builtin/Core/Src/main.c @@ -76,13 +76,14 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return (uint64_t)HAL_GetTick(); // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f429zi-cube-freertos-builtin/Core/Src/main.c b/examples/stm32/nucleo-f429zi-cube-freertos-builtin/Core/Src/main.c index 335b2cc0..86a95492 100644 --- a/examples/stm32/nucleo-f429zi-cube-freertos-builtin/Core/Src/main.c +++ b/examples/stm32/nucleo-f429zi-cube-freertos-builtin/Core/Src/main.c @@ -90,13 +90,14 @@ void server(void *argument); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f429zi-cube-freertos-lwip/Core/Src/main.c b/examples/stm32/nucleo-f429zi-cube-freertos-lwip/Core/Src/main.c index 22cf18a9..0d20e994 100644 --- a/examples/stm32/nucleo-f429zi-cube-freertos-lwip/Core/Src/main.c +++ b/examples/stm32/nucleo-f429zi-cube-freertos-lwip/Core/Src/main.c @@ -70,13 +70,14 @@ void blinker(void const * argument); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } /* USER CODE END 0 */ diff --git a/examples/stm32/nucleo-f429zi-keil-baremetal/main.c b/examples/stm32/nucleo-f429zi-keil-baremetal/main.c index 8ae1eb69..cd0538b0 100644 --- a/examples/stm32/nucleo-f429zi-keil-baremetal/main.c +++ b/examples/stm32/nucleo-f429zi-keil-baremetal/main.c @@ -11,13 +11,14 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return (uint64_t) HAL_GetTick(); // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f429zi-keil-freertos/main.c b/examples/stm32/nucleo-f429zi-keil-freertos/main.c index 9b368493..6cf2dbbc 100644 --- a/examples/stm32/nucleo-f429zi-keil-freertos/main.c +++ b/examples/stm32/nucleo-f429zi-keil-freertos/main.c @@ -17,13 +17,14 @@ void SysTick_Handler(void) { xPortSysTickHandler(); } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f429zi-make-baremetal-builtin-rndis/main.c b/examples/stm32/nucleo-f429zi-make-baremetal-builtin-rndis/main.c index 4c7f689c..74cadc70 100644 --- a/examples/stm32/nucleo-f429zi-make-baremetal-builtin-rndis/main.c +++ b/examples/stm32/nucleo-f429zi-make-baremetal-builtin-rndis/main.c @@ -22,11 +22,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } bool tud_network_recv_cb(const uint8_t *buf, uint16_t len) { @@ -80,7 +81,7 @@ int main(void) { gpio_output(LED); // Setup blue LED uart_init(UART_DEBUG, 115200); // Initialise debug printf - struct mg_mgr mgr; // Initialise + struct mg_mgr mgr; // Initialise mg_mgr_init(&mgr); // Mongoose event manager mg_log_set(MG_LL_DEBUG); // Set log level diff --git a/examples/stm32/nucleo-f429zi-make-baremetal-builtin/main.c b/examples/stm32/nucleo-f429zi-make-baremetal-builtin/main.c index 19ad4a75..0dca9f1c 100644 --- a/examples/stm32/nucleo-f429zi-make-baremetal-builtin/main.c +++ b/examples/stm32/nucleo-f429zi-make-baremetal-builtin/main.c @@ -16,11 +16,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f429zi-make-freertos-builtin/main.c b/examples/stm32/nucleo-f429zi-make-freertos-builtin/main.c index 75798cef..58382cba 100644 --- a/examples/stm32/nucleo-f429zi-make-freertos-builtin/main.c +++ b/examples/stm32/nucleo-f429zi-make-freertos-builtin/main.c @@ -7,11 +7,12 @@ #define BLINK_PERIOD_MS 1000 // LED blinking period in millis -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f746zg-cube-baremetal-builtin/Core/Src/main.c b/examples/stm32/nucleo-f746zg-cube-baremetal-builtin/Core/Src/main.c index f086b785..4c48e5b6 100644 --- a/examples/stm32/nucleo-f746zg-cube-baremetal-builtin/Core/Src/main.c +++ b/examples/stm32/nucleo-f746zg-cube-baremetal-builtin/Core/Src/main.c @@ -90,13 +90,14 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return (uint64_t)HAL_GetTick(); // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f746zg-cube-freertos-builtin/Core/Src/main.c b/examples/stm32/nucleo-f746zg-cube-freertos-builtin/Core/Src/main.c index 4a538d50..edc9028e 100644 --- a/examples/stm32/nucleo-f746zg-cube-freertos-builtin/Core/Src/main.c +++ b/examples/stm32/nucleo-f746zg-cube-freertos-builtin/Core/Src/main.c @@ -104,13 +104,14 @@ void server(void *argument); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f746zg-cube-freertos-lwip/Core/Src/main.c b/examples/stm32/nucleo-f746zg-cube-freertos-lwip/Core/Src/main.c index 45878ddf..112d66cf 100644 --- a/examples/stm32/nucleo-f746zg-cube-freertos-lwip/Core/Src/main.c +++ b/examples/stm32/nucleo-f746zg-cube-freertos-lwip/Core/Src/main.c @@ -71,13 +71,14 @@ void blinker(void const * argument); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } /* USER CODE END 0 */ diff --git a/examples/stm32/nucleo-f746zg-keil-baremetal/main.c b/examples/stm32/nucleo-f746zg-keil-baremetal/main.c index b4cc5b30..612fbf6d 100644 --- a/examples/stm32/nucleo-f746zg-keil-baremetal/main.c +++ b/examples/stm32/nucleo-f746zg-keil-baremetal/main.c @@ -12,13 +12,14 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return (uint64_t) HAL_GetTick(); // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f746zg-keil-freertos-lwip/main.c b/examples/stm32/nucleo-f746zg-keil-freertos-lwip/main.c index c1351648..fcaf0b61 100644 --- a/examples/stm32/nucleo-f746zg-keil-freertos-lwip/main.c +++ b/examples/stm32/nucleo-f746zg-keil-freertos-lwip/main.c @@ -16,18 +16,19 @@ extern void xPortSysTickHandler(void); void SysTick_Handler (void) { HAL_IncTick(); - // xPortSysTickHandler() must be called after vTaskStartScheduler() and mx_init() takes longer than 1ms + // xPortSysTickHandler() must be called after vTaskStartScheduler() and mx_init() takes longer than 1ms if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) xPortSysTickHandler(); } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void server(void *args) { @@ -74,14 +75,14 @@ static void netw_init (struct netif *netif) { static struct netif s_netif; -static void app_main (void *args) { +static void app_main (void *args) { netw_init(&s_netif); xTaskCreate(netw, "netw", 128, &s_netif, configMAX_PRIORITIES - 1, NULL); // Create the Ethernet link/rx thread MG_INFO(("Waiting for IP...")); while(ip4_addr_isany_val(*netif_ip4_addr(&s_netif))) vTaskDelay(pdMS_TO_TICKS(200)); MG_INFO(("READY, IP: %s", ip4addr_ntoa(netif_ip4_addr(&s_netif)))); - + xTaskCreate(server, "server", 2048, 0, configMAX_PRIORITIES - 1, NULL); vTaskDelete(NULL); (void)args; diff --git a/examples/stm32/nucleo-f746zg-keil-freertos-tcp/main.c b/examples/stm32/nucleo-f746zg-keil-freertos-tcp/main.c index e54522ce..130acb07 100644 --- a/examples/stm32/nucleo-f746zg-keil-freertos-tcp/main.c +++ b/examples/stm32/nucleo-f746zg-keil-freertos-tcp/main.c @@ -13,17 +13,18 @@ extern RNG_HandleTypeDef hrng; extern void xPortSysTickHandler(void); void SysTick_Handler (void) { HAL_IncTick(); - // xPortSysTickHandler() must be called after vTaskStartScheduler() and mx_init() takes longer than 1ms + // xPortSysTickHandler() must be called after vTaskStartScheduler() and mx_init() takes longer than 1ms if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) xPortSysTickHandler(); } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void server(void *args) { @@ -70,7 +71,7 @@ int main(void) { uint8_t macaddr[6] = GENERATE_LOCALLY_ADMINISTERED_MAC(); // required for fallback if DHCP fails - static const uint8_t ipaddr[4] = {192, 168, 0, 77}; + static const uint8_t ipaddr[4] = {192, 168, 0, 77}; static const uint8_t netmask[4] = {255, 255, 255, 0}; static const uint8_t dnsaddr[4] = {8, 8, 8, 8}; static const uint8_t gwaddr[4] = {192, 168, 0, 1}; @@ -98,7 +99,7 @@ void HAL_ETH_MspInit(ETH_HandleTypeDef *heth) { uint32_t ulApplicationGetNextSequenceNumber(uint32_t a, uint16_t b, uint32_t c, uint16_t d) { (void) a, (void) b, (void) c, (void) d; - uint32_t x; + uint32_t x; HAL_RNG_GenerateRandomNumber(&hrng, &x); return x; } diff --git a/examples/stm32/nucleo-f746zg-keil-freertos/main.c b/examples/stm32/nucleo-f746zg-keil-freertos/main.c index e6617060..fb9e7443 100644 --- a/examples/stm32/nucleo-f746zg-keil-freertos/main.c +++ b/examples/stm32/nucleo-f746zg-keil-freertos/main.c @@ -17,13 +17,14 @@ void SysTick_Handler(void) { xPortSysTickHandler(); } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f746zg-keil-freertos_cmsis2-lwip/main.c b/examples/stm32/nucleo-f746zg-keil-freertos_cmsis2-lwip/main.c index 980c6c01..40199ee4 100644 --- a/examples/stm32/nucleo-f746zg-keil-freertos_cmsis2-lwip/main.c +++ b/examples/stm32/nucleo-f746zg-keil-freertos_cmsis2-lwip/main.c @@ -5,7 +5,7 @@ #include "mongoose.h" #include "main.h" #include "net.h" -#include "cmsis_os2.h" +#include "cmsis_os2.h" #include "ethernetif.h" #include "lwip/dhcp.h" #include "lwip/netif.h" @@ -14,13 +14,14 @@ #define BLINK_PERIOD_MS 1000 // LED blinking period in millis -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void server(void *args) { @@ -67,14 +68,14 @@ static void netw_init (struct netif *netif) { static struct netif s_netif; -static void app_main (void *args) { +static void app_main (void *args) { netw_init(&s_netif); osThreadNew(netw, &s_netif, NULL); // Create the Ethernet link/rx thread with a default stack size MG_INFO(("Waiting for IP...")); while(ip4_addr_isany_val(*netif_ip4_addr(&s_netif))) osDelay((osKernelGetTickFreq() * 200U) / 1000U); MG_INFO(("READY, IP: %s", ip4addr_ntoa(netif_ip4_addr(&s_netif)))); - + const osThreadAttr_t server_attr = { .stack_size = 8192 // Create the server thread with a stack size of 8KB }; @@ -94,7 +95,6 @@ int main(void) { osKernelInitialize(); // Initialize CMSIS-RTOS osThreadNew(blinker, NULL, NULL); // Create the blinker thread with a default stack size osThreadNew(app_main, NULL, NULL); // Create the thread that will start networking, use a default stack size - osKernelStart(); // This blocks + osKernelStart(); // This blocks return 0; } - diff --git a/examples/stm32/nucleo-f746zg-keil-freertos_cmsis2/main.c b/examples/stm32/nucleo-f746zg-keil-freertos_cmsis2/main.c index 9fc7e73f..148386e3 100644 --- a/examples/stm32/nucleo-f746zg-keil-freertos_cmsis2/main.c +++ b/examples/stm32/nucleo-f746zg-keil-freertos_cmsis2/main.c @@ -9,13 +9,14 @@ #define BLINK_PERIOD_MS 1000 // LED blinking period in millis -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f746zg-keil-rtx-mdk/main.c b/examples/stm32/nucleo-f746zg-keil-rtx-mdk/main.c index 7f0bf6e0..e50435fb 100644 --- a/examples/stm32/nucleo-f746zg-keil-rtx-mdk/main.c +++ b/examples/stm32/nucleo-f746zg-keil-rtx-mdk/main.c @@ -5,18 +5,19 @@ #include "mongoose.h" #include "main.h" #include "net.h" -#include "cmsis_os.h" +#include "cmsis_os.h" #define BLINK_PERIOD_MS 1000 // LED blinking period in millis -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void server(const void *args) { @@ -50,7 +51,7 @@ void netDHCP_Notify (uint32_t if_num, uint8_t option, const uint8_t *val, uint32 (void)if_num, (void)val, (void)len; } -static void app_main (const void *args) { +static void app_main (const void *args) { uint8_t ipaddr[NET_ADDR_IP4_LEN]; char ip[40]; netInitialize(); @@ -70,7 +71,7 @@ osThreadDef(blinker, osPriorityNormal, 1, 0); // Create the blinker thread with osThreadDef(app_main, osPriorityNormal, 1, 1024); // Create the thread that will start networking with a stack size of 1KB extern void mx_init(void); - + int main(void) { // this is not actually baremetal main() but the "main" thread osKernelInitialize(); // Stop kernel mx_init(); // Setup clock and all peripherals configured in CubeMX diff --git a/examples/stm32/nucleo-f746zg-keil-rtx/main.c b/examples/stm32/nucleo-f746zg-keil-rtx/main.c index 43edfa83..79a97d34 100644 --- a/examples/stm32/nucleo-f746zg-keil-rtx/main.c +++ b/examples/stm32/nucleo-f746zg-keil-rtx/main.c @@ -9,13 +9,14 @@ #define BLINK_PERIOD_MS 1000 // LED blinking period in millis -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f746zg-keil-rtx5-lwip/main.c b/examples/stm32/nucleo-f746zg-keil-rtx5-lwip/main.c index 62103e58..5177dcb0 100644 --- a/examples/stm32/nucleo-f746zg-keil-rtx5-lwip/main.c +++ b/examples/stm32/nucleo-f746zg-keil-rtx5-lwip/main.c @@ -5,7 +5,7 @@ #include "mongoose.h" #include "main.h" #include "net.h" -#include "cmsis_os2.h" +#include "cmsis_os2.h" #include "ethernetif.h" #include "lwip/dhcp.h" #include "lwip/netif.h" @@ -14,13 +14,14 @@ #define BLINK_PERIOD_MS 1000 // LED blinking period in millis -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void server(void *args) { @@ -67,14 +68,14 @@ static void netw_init (struct netif *netif) { static struct netif s_netif; -__NO_RETURN static void app_main (void *args) { +__NO_RETURN static void app_main (void *args) { netw_init(&s_netif); osThreadNew(netw, &s_netif, NULL); // Create the Ethernet link/rx thread with a default stack size MG_INFO(("Waiting for IP...")); while(ip4_addr_isany_val(*netif_ip4_addr(&s_netif))) osDelay((osKernelGetTickFreq() * 200U) / 1000U); MG_INFO(("READY, IP: %s", ip4addr_ntoa(netif_ip4_addr(&s_netif)))); - + const osThreadAttr_t server_attr = { .stack_size = 8192 // Create the server thread with a stack size of 8KB }; @@ -95,7 +96,6 @@ int main(void) { osKernelInitialize(); // Initialize CMSIS-RTOS osThreadNew(blinker, NULL, NULL); // Create the blinker thread with a default stack size osThreadNew(app_main, NULL, NULL); // Create the thread that will start networking, use a default stack size - osKernelStart(); // This blocks + osKernelStart(); // This blocks return 0; } - diff --git a/examples/stm32/nucleo-f746zg-keil-rtx5-mdk/main.c b/examples/stm32/nucleo-f746zg-keil-rtx5-mdk/main.c index 47a79360..bd6826d8 100644 --- a/examples/stm32/nucleo-f746zg-keil-rtx5-mdk/main.c +++ b/examples/stm32/nucleo-f746zg-keil-rtx5-mdk/main.c @@ -5,18 +5,19 @@ #include "mongoose.h" #include "main.h" #include "net.h" -#include "cmsis_os2.h" +#include "cmsis_os2.h" #define BLINK_PERIOD_MS 1000 // LED blinking period in millis -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void server(void *args) { @@ -48,7 +49,7 @@ void netDHCP_Notify (uint32_t if_num, uint8_t option, const uint8_t *val, uint32 (void)if_num, (void)val, (void)len; } -__NO_RETURN static void app_main (void *args) { +__NO_RETURN static void app_main (void *args) { uint8_t ipaddr[NET_ADDR_IP4_LEN]; char ip[40]; @@ -80,7 +81,6 @@ int main(void) { osKernelInitialize(); // Initialize CMSIS-RTOS osThreadNew(blinker, NULL, NULL); // Create the blinker thread with a default stack size s_am = osThreadNew(app_main, NULL, NULL); // Create the thread that will start networking, use a default stack size - osKernelStart(); // This blocks + osKernelStart(); // This blocks return 0; } - diff --git a/examples/stm32/nucleo-f746zg-keil-rtx5/main.c b/examples/stm32/nucleo-f746zg-keil-rtx5/main.c index 81f0fe1c..cd2b2095 100644 --- a/examples/stm32/nucleo-f746zg-keil-rtx5/main.c +++ b/examples/stm32/nucleo-f746zg-keil-rtx5/main.c @@ -9,13 +9,14 @@ #define BLINK_PERIOD_MS 1000 // LED blinking period in millis -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f746zg-make-baremetal-builtin-cmsis_driver/main.c b/examples/stm32/nucleo-f746zg-make-baremetal-builtin-cmsis_driver/main.c index 2c8e5fcb..55b65d7c 100644 --- a/examples/stm32/nucleo-f746zg-make-baremetal-builtin-cmsis_driver/main.c +++ b/examples/stm32/nucleo-f746zg-make-baremetal-builtin-cmsis_driver/main.c @@ -16,11 +16,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f746zg-make-baremetal-builtin-rndis/main.c b/examples/stm32/nucleo-f746zg-make-baremetal-builtin-rndis/main.c index 4c7f689c..74cadc70 100644 --- a/examples/stm32/nucleo-f746zg-make-baremetal-builtin-rndis/main.c +++ b/examples/stm32/nucleo-f746zg-make-baremetal-builtin-rndis/main.c @@ -22,11 +22,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } bool tud_network_recv_cb(const uint8_t *buf, uint16_t len) { @@ -80,7 +81,7 @@ int main(void) { gpio_output(LED); // Setup blue LED uart_init(UART_DEBUG, 115200); // Initialise debug printf - struct mg_mgr mgr; // Initialise + struct mg_mgr mgr; // Initialise mg_mgr_init(&mgr); // Mongoose event manager mg_log_set(MG_LL_DEBUG); // Set log level diff --git a/examples/stm32/nucleo-f746zg-make-baremetal-builtin/main.c b/examples/stm32/nucleo-f746zg-make-baremetal-builtin/main.c index 25a2a76e..dc4fa7a4 100644 --- a/examples/stm32/nucleo-f746zg-make-baremetal-builtin/main.c +++ b/examples/stm32/nucleo-f746zg-make-baremetal-builtin/main.c @@ -16,11 +16,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f746zg-make-freertos-builtin/main.c b/examples/stm32/nucleo-f746zg-make-freertos-builtin/main.c index 75798cef..58382cba 100644 --- a/examples/stm32/nucleo-f746zg-make-freertos-builtin/main.c +++ b/examples/stm32/nucleo-f746zg-make-freertos-builtin/main.c @@ -7,11 +7,12 @@ #define BLINK_PERIOD_MS 1000 // LED blinking period in millis -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-f746zg-make-freertos-tcp/main.c b/examples/stm32/nucleo-f746zg-make-freertos-tcp/main.c index b0e36be0..0ac40691 100644 --- a/examples/stm32/nucleo-f746zg-make-freertos-tcp/main.c +++ b/examples/stm32/nucleo-f746zg-make-freertos-tcp/main.c @@ -23,11 +23,12 @@ void SysTick_Handler(void) { xPortSysTickHandler(); } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void ethernet_init(void) { diff --git a/examples/stm32/nucleo-g031-make-baremetal-builtin/main.c b/examples/stm32/nucleo-g031-make-baremetal-builtin/main.c index ec4ba68b..08d4810e 100644 --- a/examples/stm32/nucleo-g031-make-baremetal-builtin/main.c +++ b/examples/stm32/nucleo-g031-make-baremetal-builtin/main.c @@ -23,11 +23,12 @@ void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms s_ticks++; } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } uint64_t mg_millis(void) { // Let Mongoose use our uptime function diff --git a/examples/stm32/nucleo-h563zi-make-baremetal-builtin/main.c b/examples/stm32/nucleo-h563zi-make-baremetal-builtin/main.c index b60d1ce9..803f448c 100644 --- a/examples/stm32/nucleo-h563zi-make-baremetal-builtin/main.c +++ b/examples/stm32/nucleo-h563zi-make-baremetal-builtin/main.c @@ -16,11 +16,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-h563zi-make-freertos-builtin/main.c b/examples/stm32/nucleo-h563zi-make-freertos-builtin/main.c index 272030a9..a5c217bb 100644 --- a/examples/stm32/nucleo-h563zi-make-freertos-builtin/main.c +++ b/examples/stm32/nucleo-h563zi-make-freertos-builtin/main.c @@ -7,11 +7,12 @@ #define BLINK_PERIOD_MS 1000 // LED blinking period in millis -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-h723zg-make-baremetal-builtin/main.c b/examples/stm32/nucleo-h723zg-make-baremetal-builtin/main.c index a0b00c3a..8ce71edc 100644 --- a/examples/stm32/nucleo-h723zg-make-baremetal-builtin/main.c +++ b/examples/stm32/nucleo-h723zg-make-baremetal-builtin/main.c @@ -16,11 +16,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } #ifdef MQTT_DASHBOARD diff --git a/examples/stm32/nucleo-h723zg-make-freertos-builtin/main.c b/examples/stm32/nucleo-h723zg-make-freertos-builtin/main.c index 7c261536..e363229f 100644 --- a/examples/stm32/nucleo-h723zg-make-freertos-builtin/main.c +++ b/examples/stm32/nucleo-h723zg-make-freertos-builtin/main.c @@ -15,11 +15,12 @@ void SysTick_Handler(void) { xPortSysTickHandler(); } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-h743zi-cube-baremetal-builtin/Core/Src/main.c b/examples/stm32/nucleo-h743zi-cube-baremetal-builtin/Core/Src/main.c index 94f5b235..33e15a85 100644 --- a/examples/stm32/nucleo-h743zi-cube-baremetal-builtin/Core/Src/main.c +++ b/examples/stm32/nucleo-h743zi-cube-baremetal-builtin/Core/Src/main.c @@ -90,13 +90,14 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return (uint64_t)HAL_GetTick(); // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-h743zi-cube-freertos-builtin/Core/Src/main.c b/examples/stm32/nucleo-h743zi-cube-freertos-builtin/Core/Src/main.c index e63defcc..1fa028eb 100644 --- a/examples/stm32/nucleo-h743zi-cube-freertos-builtin/Core/Src/main.c +++ b/examples/stm32/nucleo-h743zi-cube-freertos-builtin/Core/Src/main.c @@ -104,13 +104,14 @@ void server(void *argument); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG extern RNG_HandleTypeDef hrng; for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r; HAL_RNG_GenerateRandomNumber(&hrng, &r); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-h743zi-make-baremetal-builtin/main.c b/examples/stm32/nucleo-h743zi-make-baremetal-builtin/main.c index cd6a30a3..f9cb57db 100644 --- a/examples/stm32/nucleo-h743zi-make-baremetal-builtin/main.c +++ b/examples/stm32/nucleo-h743zi-make-baremetal-builtin/main.c @@ -16,11 +16,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/nucleo-h743zi-make-freertos-builtin/main.c b/examples/stm32/nucleo-h743zi-make-freertos-builtin/main.c index 9706f30a..f01a1cb7 100644 --- a/examples/stm32/nucleo-h743zi-make-freertos-builtin/main.c +++ b/examples/stm32/nucleo-h743zi-make-freertos-builtin/main.c @@ -14,11 +14,12 @@ void SysTick_Handler(void) { xPortSysTickHandler(); } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/stm32h573i-dk-make-baremetal-builtin/main.c b/examples/stm32/stm32h573i-dk-make-baremetal-builtin/main.c index b60d1ce9..803f448c 100644 --- a/examples/stm32/stm32h573i-dk-make-baremetal-builtin/main.c +++ b/examples/stm32/stm32h573i-dk-make-baremetal-builtin/main.c @@ -16,11 +16,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/stm32h573i-dk-make-freertos-builtin/main.c b/examples/stm32/stm32h573i-dk-make-freertos-builtin/main.c index 272030a9..a5c217bb 100644 --- a/examples/stm32/stm32h573i-dk-make-freertos-builtin/main.c +++ b/examples/stm32/stm32h573i-dk-make-freertos-builtin/main.c @@ -7,11 +7,12 @@ #define BLINK_PERIOD_MS 1000 // LED blinking period in millis -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/stm32/stm32h747i-disco-make-baremetal-builtin/main.c b/examples/stm32/stm32h747i-disco-make-baremetal-builtin/main.c index 2f008f4c..4b208805 100644 --- a/examples/stm32/stm32h747i-disco-make-baremetal-builtin/main.c +++ b/examples/stm32/stm32h747i-disco-make-baremetal-builtin/main.c @@ -16,11 +16,12 @@ uint64_t mg_millis(void) { // Let Mongoose use our uptime function return s_ticks; // Return number of milliseconds since boot } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } static void timer_fn(void *arg) { diff --git a/examples/wch/ch32v307-make-baremetal-builtin/hal.c b/examples/wch/ch32v307-make-baremetal-builtin/hal.c index fa4e227a..1b82ab72 100644 --- a/examples/wch/ch32v307-make-baremetal-builtin/hal.c +++ b/examples/wch/ch32v307-make-baremetal-builtin/hal.c @@ -26,11 +26,12 @@ __attribute__((interrupt())) void SysTick_Handler(void) { SysTick->SR = 0; } -void mg_random(void *buf, size_t len) { // Use on-board RNG +bool mg_random(void *buf, size_t len) { // Use on-board RNG for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } + return true; } uint64_t mg_millis(void) { // Let Mongoose use our uptime function diff --git a/mongoose.c b/mongoose.c index cf0eb4be..de50ea3d 100644 --- a/mongoose.c +++ b/mongoose.c @@ -10066,7 +10066,7 @@ static void mg_tls_server_send_hello(struct mg_connection *c) { // calculate keyshare uint8_t x25519_pub[X25519_BYTES]; uint8_t x25519_prv[X25519_BYTES]; - mg_random(x25519_prv, sizeof(x25519_prv)); + if (!mg_random(x25519_prv, sizeof(x25519_prv))) mg_error(c, "RNG"); mg_tls_x25519(x25519_pub, x25519_prv, X25519_BASE_POINT, 1); mg_tls_x25519(tls->x25519_sec, x25519_prv, tls->x25519_cli, 1); mg_tls_hexdump("s x25519 sec", tls->x25519_sec, sizeof(tls->x25519_sec)); @@ -10292,12 +10292,12 @@ static void mg_tls_client_send_hello(struct mg_connection *c) { } // calculate keyshare - mg_random(tls->x25519_cli, sizeof(tls->x25519_cli)); + if (!mg_random(tls->x25519_cli, sizeof(tls->x25519_cli))) mg_error(c, "RNG"); mg_tls_x25519(x25519_pub, tls->x25519_cli, X25519_BASE_POINT, 1); // fill in the gaps: random + session ID + keyshare - mg_random(tls->session_id, sizeof(tls->session_id)); - mg_random(tls->random, sizeof(tls->random)); + if (!mg_random(tls->session_id, sizeof(tls->session_id))) mg_error(c, "RNG"); + if (!mg_random(tls->random, sizeof(tls->random))) mg_error(c, "RNG"); memmove(msg_client_hello + 11, tls->random, sizeof(tls->random)); memmove(msg_client_hello + 44, tls->session_id, sizeof(tls->session_id)); memmove(msg_client_hello + 94, x25519_pub, sizeof(x25519_pub)); @@ -16337,6 +16337,7 @@ struct mg_str mg_url_pass(const char *url) { #endif + // Not using memset for zeroing memory, cause it can be dropped by compiler // See https://github.com/cesanta/mongoose/pull/1265 void mg_bzero(volatile unsigned char *buf, size_t len) { @@ -16347,22 +16348,50 @@ void mg_bzero(volatile unsigned char *buf, size_t len) { #if MG_ENABLE_CUSTOM_RANDOM #else -void mg_random(void *buf, size_t len) { - bool done = false; +bool mg_random(void *buf, size_t len) { + bool success = false; unsigned char *p = (unsigned char *) buf; #if MG_ARCH == MG_ARCH_ESP32 while (len--) *p++ = (unsigned char) (esp_random() & 255); - done = true; + success = true; #elif MG_ARCH == MG_ARCH_WIN32 + static bool initialised = false; +#if defined(_MSC_VER) && _MSC_VER < 1700 + static HCRYPTPROV hProv; + // CryptGenRandom() implementation earlier than 2008 is weak, see + // https://en.wikipedia.org/wiki/CryptGenRandom + if (initialised == false) { + initialised = CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT); + } + if (initialised == true) { + success = CryptGenRandom(hProv, len, p); + } +#else + // BCrypt is a "new generation" strong crypto API, so try it first + static BCRYPT_ALG_HANDLE hProv; + if (initialised == false && + BCryptOpenAlgorithmProvider(&hProv, BCRYPT_RNG_ALGORITHM, NULL, 0) == 0) { + initialised = true; + } + if (initialised == true) { + success = BCryptGenRandom(hProv, p, (ULONG) len, 0) == 0; + } +#endif + #elif MG_ARCH == MG_ARCH_UNIX FILE *fp = fopen("/dev/urandom", "rb"); if (fp != NULL) { - if (fread(buf, 1, len, fp) == len) done = true; + if (fread(buf, 1, len, fp) == len) success = true; fclose(fp); } #endif // If everything above did not work, fallback to a pseudo random generator - while (!done && len--) *p++ = (unsigned char) (rand() & 255); + if (success == false) { + MG_ERROR(("Weak RNG: using rand()")); + while (len--) *p++ = (unsigned char) (rand() & 255); + } + return success; } #endif diff --git a/mongoose.h b/mongoose.h index 1cc3d837..3489fe49 100644 --- a/mongoose.h +++ b/mongoose.h @@ -443,6 +443,20 @@ typedef enum { false = 0, true = 1 } bool; #include #include +// For mg_random() +#if defined(_MSC_VER) && _MSC_VER < 1700 +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x400 // Let vc98 pick up wincrypt.h +#endif +#include +#pragma comment(lib, "advapi32.lib") +#else +#include +#if defined(_MSC_VER) +#pragma comment(lib, "bcrypt.lib") +#endif +#endif + // Protect from calls like std::snprintf in app code // See https://github.com/cesanta/mongoose/issues/1047 #ifndef __cplusplus @@ -487,12 +501,12 @@ typedef int socklen_t; (((errcode) < 0) && (WSAGetLastError() == WSAECONNRESET)) #define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX) -#define sleep(x) Sleep((x) *1000) +#define sleep(x) Sleep((x) * 1000) #define mkdir(a, b) _mkdir(a) #define timegm(x) _mkgmtime(x) #ifndef S_ISDIR -#define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR) +#define S_ISDIR(x) (((x) & _S_IFMT) == _S_IFDIR) #endif #ifndef MG_ENABLE_DIRLIST @@ -1056,7 +1070,7 @@ struct mg_str mg_unpacked(const char *path); // Packed file as mg_str #endif void mg_bzero(volatile unsigned char *buf, size_t len); -void mg_random(void *buf, size_t len); +bool mg_random(void *buf, size_t len); char *mg_random_str(char *buf, size_t len); uint16_t mg_ntohs(uint16_t net); uint32_t mg_ntohl(uint32_t net); diff --git a/src/arch_win32.h b/src/arch_win32.h index b0ab1b69..c31310f3 100644 --- a/src/arch_win32.h +++ b/src/arch_win32.h @@ -50,6 +50,20 @@ typedef enum { false = 0, true = 1 } bool; #include #include +// For mg_random() +#if defined(_MSC_VER) && _MSC_VER < 1700 +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x400 // Let vc98 pick up wincrypt.h +#endif +#include +#pragma comment(lib, "advapi32.lib") +#else +#include +#if defined(_MSC_VER) +#pragma comment(lib, "bcrypt.lib") +#endif +#endif + // Protect from calls like std::snprintf in app code // See https://github.com/cesanta/mongoose/issues/1047 #ifndef __cplusplus @@ -94,12 +108,12 @@ typedef int socklen_t; (((errcode) < 0) && (WSAGetLastError() == WSAECONNRESET)) #define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX) -#define sleep(x) Sleep((x) *1000) +#define sleep(x) Sleep((x) * 1000) #define mkdir(a, b) _mkdir(a) #define timegm(x) _mkgmtime(x) #ifndef S_ISDIR -#define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR) +#define S_ISDIR(x) (((x) & _S_IFMT) == _S_IFDIR) #endif #ifndef MG_ENABLE_DIRLIST diff --git a/src/tls_builtin.c b/src/tls_builtin.c index d96d4b39..9c4c585d 100644 --- a/src/tls_builtin.c +++ b/src/tls_builtin.c @@ -623,7 +623,7 @@ static void mg_tls_server_send_hello(struct mg_connection *c) { // calculate keyshare uint8_t x25519_pub[X25519_BYTES]; uint8_t x25519_prv[X25519_BYTES]; - mg_random(x25519_prv, sizeof(x25519_prv)); + if (!mg_random(x25519_prv, sizeof(x25519_prv))) mg_error(c, "RNG"); mg_tls_x25519(x25519_pub, x25519_prv, X25519_BASE_POINT, 1); mg_tls_x25519(tls->x25519_sec, x25519_prv, tls->x25519_cli, 1); mg_tls_hexdump("s x25519 sec", tls->x25519_sec, sizeof(tls->x25519_sec)); @@ -849,12 +849,12 @@ static void mg_tls_client_send_hello(struct mg_connection *c) { } // calculate keyshare - mg_random(tls->x25519_cli, sizeof(tls->x25519_cli)); + if (!mg_random(tls->x25519_cli, sizeof(tls->x25519_cli))) mg_error(c, "RNG"); mg_tls_x25519(x25519_pub, tls->x25519_cli, X25519_BASE_POINT, 1); // fill in the gaps: random + session ID + keyshare - mg_random(tls->session_id, sizeof(tls->session_id)); - mg_random(tls->random, sizeof(tls->random)); + if (!mg_random(tls->session_id, sizeof(tls->session_id))) mg_error(c, "RNG"); + if (!mg_random(tls->random, sizeof(tls->random))) mg_error(c, "RNG"); memmove(msg_client_hello + 11, tls->random, sizeof(tls->random)); memmove(msg_client_hello + 44, tls->session_id, sizeof(tls->session_id)); memmove(msg_client_hello + 94, x25519_pub, sizeof(x25519_pub)); diff --git a/src/util.c b/src/util.c index 332ce93b..345fa0f5 100644 --- a/src/util.c +++ b/src/util.c @@ -1,3 +1,4 @@ +#include "log.h" #include "util.h" // Not using memset for zeroing memory, cause it can be dropped by compiler @@ -10,22 +11,50 @@ void mg_bzero(volatile unsigned char *buf, size_t len) { #if MG_ENABLE_CUSTOM_RANDOM #else -void mg_random(void *buf, size_t len) { - bool done = false; +bool mg_random(void *buf, size_t len) { + bool success = false; unsigned char *p = (unsigned char *) buf; #if MG_ARCH == MG_ARCH_ESP32 while (len--) *p++ = (unsigned char) (esp_random() & 255); - done = true; + success = true; #elif MG_ARCH == MG_ARCH_WIN32 + static bool initialised = false; +#if defined(_MSC_VER) && _MSC_VER < 1700 + static HCRYPTPROV hProv; + // CryptGenRandom() implementation earlier than 2008 is weak, see + // https://en.wikipedia.org/wiki/CryptGenRandom + if (initialised == false) { + initialised = CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT); + } + if (initialised == true) { + success = CryptGenRandom(hProv, len, p); + } +#else + // BCrypt is a "new generation" strong crypto API, so try it first + static BCRYPT_ALG_HANDLE hProv; + if (initialised == false && + BCryptOpenAlgorithmProvider(&hProv, BCRYPT_RNG_ALGORITHM, NULL, 0) == 0) { + initialised = true; + } + if (initialised == true) { + success = BCryptGenRandom(hProv, p, (ULONG) len, 0) == 0; + } +#endif + #elif MG_ARCH == MG_ARCH_UNIX FILE *fp = fopen("/dev/urandom", "rb"); if (fp != NULL) { - if (fread(buf, 1, len, fp) == len) done = true; + if (fread(buf, 1, len, fp) == len) success = true; fclose(fp); } #endif // If everything above did not work, fallback to a pseudo random generator - while (!done && len--) *p++ = (unsigned char) (rand() & 255); + if (success == false) { + MG_ERROR(("Weak RNG: using rand()")); + while (len--) *p++ = (unsigned char) (rand() & 255); + } + return success; } #endif diff --git a/src/util.h b/src/util.h index 58f3b5ab..78405946 100644 --- a/src/util.h +++ b/src/util.h @@ -12,7 +12,7 @@ #endif void mg_bzero(volatile unsigned char *buf, size_t len); -void mg_random(void *buf, size_t len); +bool mg_random(void *buf, size_t len); char *mg_random_str(char *buf, size_t len); uint16_t mg_ntohs(uint16_t net); uint32_t mg_ntohl(uint32_t net); diff --git a/test/Makefile b/test/Makefile index 4926da7f..def1a243 100644 --- a/test/Makefile +++ b/test/Makefile @@ -6,9 +6,10 @@ OPTS ?= -O3 -g3 INCS ?= -Isrc -I. SSL ?= CWD ?= $(realpath $(CURDIR)) +ROOT_DIR = $(realpath $(CWD)/..) ENV ?= -e Tmp=. -e WINEDEBUG=-all DOCKER_BIN ?= docker -DOCKER ?= $(DOCKER_BIN) run --platform linux/amd64 --rm $(ENV) -v $(CWD)/..:$(CWD)/.. -w $(CWD) +DOCKER ?= $(DOCKER_BIN) run --platform linux/amd64 --rm $(ENV) -v $(ROOT_DIR):$(ROOT_DIR) -w $(CWD) VCFLAGS = /nologo /W3 /O2 /MD /I. $(DEFS) $(TFLAGS) IPV6 ?= 1 ASAN ?= -fsanitize=address,undefined,alignment -fno-sanitize-recover=all -fno-omit-frame-pointer -fno-common @@ -166,23 +167,23 @@ riscv: mongoose.h $(SRCS) $(DOCKER) mdashnet/riscv riscv-none-elf-gcc -march=rv32imc -mabi=ilp32 $(SRCS) $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -o unit_test vc98: Makefile mongoose.h $(SRCS) - $(DOCKER) mdashnet/vc98 wine cl $(SRCS) $(VCFLAGS) ws2_32.lib /Fe$@.exe + $(DOCKER) mdashnet/vc98 wine cl $(SRCS) $(VCFLAGS) /Fe$@.exe $(DOCKER) mdashnet/vc98 wine $@.exe vc17: Makefile mongoose.h $(SRCS) - $(DOCKER) mdashnet/vc17 wine64 cl $(SRCS) $(VCFLAGS) ws2_32.lib /Fe$@.exe + $(DOCKER) mdashnet/vc17 wine64 cl $(SRCS) $(VCFLAGS) /Fe$@.exe $(DOCKER) mdashnet/vc17 wine64 $@.exe vc22: Makefile mongoose.h $(SRCS) - $(DOCKER) mdashnet/vc22 wine64 cl $(SRCS) $(VCFLAGS) ws2_32.lib /Fe$@.exe + $(DOCKER) mdashnet/vc22 wine64 cl $(SRCS) $(VCFLAGS) /Fe$@.exe $(DOCKER) mdashnet/vc22 wine64 $@.exe mingw: Makefile mongoose.h $(SRCS) - $(DOCKER) mdashnet/mingw x86_64-w64-mingw32-gcc $(SRCS) -W -Wall -Werror -I. $(DEFS) -lwsock32 -o $@.exe + $(DOCKER) mdashnet/mingw x86_64-w64-mingw32-gcc $(SRCS) -W -Wall -Werror -I. $(DEFS) -lwsock32 -lbcrypt -o $@.exe $(DOCKER) mdashnet/mingw wine64 $@.exe mingw++: Makefile mongoose.h $(SRCS) - $(DOCKER) mdashnet/mingw x86_64-w64-mingw32-g++ $(SRCS) -W -Wall -Werror -I. $(DEFS) -lwsock32 -o $@.exe + $(DOCKER) mdashnet/mingw x86_64-w64-mingw32-g++ $(SRCS) -W -Wall -Werror -I. $(DEFS) -lwsock32 -lbcrypt -o $@.exe linux-libs: CFLAGS += -fPIC linux-libs: LDFLAGS += -Wl,-soname,libmongoose.so.$(VERSION) diff --git a/tutorials/http/http-server/Makefile b/tutorials/http/http-server/Makefile index 43098952..b0a6b340 100644 --- a/tutorials/http/http-server/Makefile +++ b/tutorials/http/http-server/Makefile @@ -3,7 +3,7 @@ DELETE = rm -rf # Command to remove files OUT ?= -o $(PROG) # Compiler argument for output file SOURCES = main.c mongoose.c # Source code files CFLAGS = -W -Wall -Wextra -g -I. # Build options -CFLAGS += -fsanitize=address,undefined,alignment +#CFLAGS += -fsanitize=address,undefined,alignment # Mongoose build options. See https://mongoose.ws/documentation/#build-options CFLAGS_MONGOOSE += -DMG_HTTP_DIRLIST_TIME_FMT="%Y/%m/%d %H:%M:%S" @@ -13,7 +13,7 @@ CFLAGS_MONGOOSE += -DMG_TLS=MG_TLS_BUILTIN ifeq ($(OS),Windows_NT) # Windows settings. Assume MinGW compiler. To use VC: make CC=cl CFLAGS=/MD OUT=/Feprog.exe PROG ?= example.exe # Use .exe suffix for the binary CC = gcc # Use MinGW gcc compiler - CFLAGS += -lws2_32 # Link against Winsock library + CFLAGS += -lws2_32 -lbcrypt # Link against Winsock library DELETE = cmd /C del /Q /F /S # Command prompt command to delete files OUT ?= -o $(PROG) # Build output endif @@ -24,5 +24,8 @@ all: $(PROG) # Default target. Build and run program $(PROG): $(SOURCES) # Build program from sources $(CC) $(SOURCES) $(CFLAGS) $(CFLAGS_MONGOOSE) $(CFLAGS_EXTRA) $(OUT) +vc98: + cl $(SOURCES) -DMG_ENABLE_SSI=1 -DMG_TLS=MG_TLS_BUILTIN + clean: # Cleanup. Delete built program and all build artifacts $(DELETE) $(PROG) *.o *.obj *.exe *.dSYM