From 9f76ea8030ef146b2ce37146ad13d721fec7e6f1 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 12 Aug 2025 08:40:07 +0100 Subject: [PATCH] CHACHA20 -> MG_ENABLE_CHACHA20 --- mongoose.c | 19 ++++++++----------- mongoose.h | 4 ++++ src/config.h | 4 ++++ src/tls_builtin.c | 19 ++++++++----------- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/mongoose.c b/mongoose.c index 190079d1..99354e89 100644 --- a/mongoose.c +++ b/mongoose.c @@ -10903,10 +10903,6 @@ int mg_aes_gcm_decrypt(unsigned char *output, const unsigned char *input, #if MG_TLS == MG_TLS_BUILTIN -#ifndef CHACHA20 -#define CHACHA20 1 -#endif - /* TLS 1.3 Record Content Type (RFC8446 B.1) */ #define MG_TLS_CHANGE_CIPHER 20 #define MG_TLS_ALERT 21 @@ -11182,7 +11178,7 @@ static void mg_tls_generate_handshake_keys(struct mg_connection *c) { uint8_t hello_hash[32]; uint8_t server_hs_secret[32]; uint8_t client_hs_secret[32]; -#if CHACHA20 +#if MG_ENABLE_CHACHA20 const size_t keysz = 32; #else const size_t keysz = 16; @@ -11246,7 +11242,7 @@ static void mg_tls_generate_application_keys(struct mg_connection *c) { uint8_t master_secret[32]; uint8_t server_secret[32]; uint8_t client_secret[32]; -#if CHACHA20 +#if MG_ENABLE_CHACHA20 const size_t keysz = 32; #else const size_t keysz = 16; @@ -11310,7 +11306,8 @@ static void mg_tls_encrypt(struct mg_connection *c, const uint8_t *msg, uint8_t *iv = c->is_client ? tls->enc.client_write_iv : tls->enc.server_write_iv; -#if !CHACHA20 +#if MG_ENABLE_CHACHA20 +#else mg_gcm_initialize(); #endif @@ -11326,7 +11323,7 @@ static void mg_tls_encrypt(struct mg_connection *c, const uint8_t *msg, tag = wio->buf + wio->len + msgsz + 1; memmove(outmsg, msg, msgsz); outmsg[msgsz] = msgtype; -#if CHACHA20 +#if MG_ENABLE_CHACHA20 (void) tag; // tag is only used in aes gcm { size_t maxlen = MG_IO_SIZE > 16384 ? 16384 : MG_IO_SIZE; @@ -11398,7 +11395,7 @@ static int mg_tls_recv_record(struct mg_connection *c) { nonce[9] ^= (uint8_t) ((seq >> 16) & 255U); nonce[10] ^= (uint8_t) ((seq >> 8) & 255U); nonce[11] ^= (uint8_t) ((seq) &255U); -#if CHACHA20 +#if MG_ENABLE_CHACHA20 { uint8_t *dec = (uint8_t *) mg_calloc(1, msgsz); size_t n; @@ -11532,7 +11529,7 @@ static void mg_tls_server_send_hello(struct mg_connection *c) { PLACEHOLDER_32B, // session ID length + session ID (32 bytes) 0x20, PLACEHOLDER_32B, -#if defined(CHACHA20) && CHACHA20 +#if MG_ENABLE_CHACHA20 // TLS_CHACHA20_POLY1305_SHA256 + no compression 0x13, 0x03, 0x00, #else @@ -11731,7 +11728,7 @@ static void mg_tls_client_send_hello(struct mg_connection *c) { // session ID length + session ID (32 bytes) 0x20, PLACEHOLDER_32B, 0x00, 0x02, // size = 2 bytes -#if defined(CHACHA20) && CHACHA20 +#if MG_ENABLE_CHACHA20 // TLS_CHACHA20_POLY1305_SHA256 0x13, 0x03, #else diff --git a/mongoose.h b/mongoose.h index 9957bb8f..3b50b26d 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1059,6 +1059,10 @@ struct timeval { #define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0 #endif +#ifndef MG_ENABLE_CHACHA20 +#define MG_ENABLE_CHACHA20 1 // When set to 0, GCM is used. For MG_TLS_BUILTIN +#endif + // Macros to record timestamped events that happens with a connection. diff --git a/src/config.h b/src/config.h index c9610b5b..23be2982 100644 --- a/src/config.h +++ b/src/config.h @@ -186,3 +186,7 @@ #ifndef MG_ENABLE_TCPIP_PRINT_DEBUG_STATS #define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0 #endif + +#ifndef MG_ENABLE_CHACHA20 +#define MG_ENABLE_CHACHA20 1 // When set to 0, GCM is used. For MG_TLS_BUILTIN +#endif diff --git a/src/tls_builtin.c b/src/tls_builtin.c index 3dbccdae..7b184b1f 100644 --- a/src/tls_builtin.c +++ b/src/tls_builtin.c @@ -12,10 +12,6 @@ #if MG_TLS == MG_TLS_BUILTIN -#ifndef CHACHA20 -#define CHACHA20 1 -#endif - /* TLS 1.3 Record Content Type (RFC8446 B.1) */ #define MG_TLS_CHANGE_CIPHER 20 #define MG_TLS_ALERT 21 @@ -291,7 +287,7 @@ static void mg_tls_generate_handshake_keys(struct mg_connection *c) { uint8_t hello_hash[32]; uint8_t server_hs_secret[32]; uint8_t client_hs_secret[32]; -#if CHACHA20 +#if MG_ENABLE_CHACHA20 const size_t keysz = 32; #else const size_t keysz = 16; @@ -355,7 +351,7 @@ static void mg_tls_generate_application_keys(struct mg_connection *c) { uint8_t master_secret[32]; uint8_t server_secret[32]; uint8_t client_secret[32]; -#if CHACHA20 +#if MG_ENABLE_CHACHA20 const size_t keysz = 32; #else const size_t keysz = 16; @@ -419,7 +415,8 @@ static void mg_tls_encrypt(struct mg_connection *c, const uint8_t *msg, uint8_t *iv = c->is_client ? tls->enc.client_write_iv : tls->enc.server_write_iv; -#if !CHACHA20 +#if MG_ENABLE_CHACHA20 +#else mg_gcm_initialize(); #endif @@ -435,7 +432,7 @@ static void mg_tls_encrypt(struct mg_connection *c, const uint8_t *msg, tag = wio->buf + wio->len + msgsz + 1; memmove(outmsg, msg, msgsz); outmsg[msgsz] = msgtype; -#if CHACHA20 +#if MG_ENABLE_CHACHA20 (void) tag; // tag is only used in aes gcm { size_t maxlen = MG_IO_SIZE > 16384 ? 16384 : MG_IO_SIZE; @@ -507,7 +504,7 @@ static int mg_tls_recv_record(struct mg_connection *c) { nonce[9] ^= (uint8_t) ((seq >> 16) & 255U); nonce[10] ^= (uint8_t) ((seq >> 8) & 255U); nonce[11] ^= (uint8_t) ((seq) &255U); -#if CHACHA20 +#if MG_ENABLE_CHACHA20 { uint8_t *dec = (uint8_t *) mg_calloc(1, msgsz); size_t n; @@ -641,7 +638,7 @@ static void mg_tls_server_send_hello(struct mg_connection *c) { PLACEHOLDER_32B, // session ID length + session ID (32 bytes) 0x20, PLACEHOLDER_32B, -#if defined(CHACHA20) && CHACHA20 +#if MG_ENABLE_CHACHA20 // TLS_CHACHA20_POLY1305_SHA256 + no compression 0x13, 0x03, 0x00, #else @@ -840,7 +837,7 @@ static void mg_tls_client_send_hello(struct mg_connection *c) { // session ID length + session ID (32 bytes) 0x20, PLACEHOLDER_32B, 0x00, 0x02, // size = 2 bytes -#if defined(CHACHA20) && CHACHA20 +#if MG_ENABLE_CHACHA20 // TLS_CHACHA20_POLY1305_SHA256 0x13, 0x03, #else