CHACHA20 -> MG_ENABLE_CHACHA20

This commit is contained in:
Sergey Lyubka committed 2025-08-12 08:42:20 +01:00
1 parent 76b7356873
commit 9f76ea8030
4 files changed
+24 -22

No files matched your search

+8 -11
View File
@@ -10903,10 +10903,6 @@ int mg_aes_gcm_decrypt(unsigned char *output, const unsigned char *input,
#if MG_TLS == MG_TLS_BUILTIN #if MG_TLS == MG_TLS_BUILTIN
#ifndef CHACHA20
#define CHACHA20 1
#endif
/* TLS 1.3 Record Content Type (RFC8446 B.1) */ /* TLS 1.3 Record Content Type (RFC8446 B.1) */
#define MG_TLS_CHANGE_CIPHER 20 #define MG_TLS_CHANGE_CIPHER 20
#define MG_TLS_ALERT 21 #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 hello_hash[32];
uint8_t server_hs_secret[32]; uint8_t server_hs_secret[32];
uint8_t client_hs_secret[32]; uint8_t client_hs_secret[32];
#if CHACHA20 #if MG_ENABLE_CHACHA20
const size_t keysz = 32; const size_t keysz = 32;
#else #else
const size_t keysz = 16; 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 master_secret[32];
uint8_t server_secret[32]; uint8_t server_secret[32];
uint8_t client_secret[32]; uint8_t client_secret[32];
#if CHACHA20 #if MG_ENABLE_CHACHA20
const size_t keysz = 32; const size_t keysz = 32;
#else #else
const size_t keysz = 16; 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 = uint8_t *iv =
c->is_client ? tls->enc.client_write_iv : tls->enc.server_write_iv; c->is_client ? tls->enc.client_write_iv : tls->enc.server_write_iv;
#if !CHACHA20 #if MG_ENABLE_CHACHA20
#else
mg_gcm_initialize(); mg_gcm_initialize();
#endif #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; tag = wio->buf + wio->len + msgsz + 1;
memmove(outmsg, msg, msgsz); memmove(outmsg, msg, msgsz);
outmsg[msgsz] = msgtype; outmsg[msgsz] = msgtype;
#if CHACHA20 #if MG_ENABLE_CHACHA20
(void) tag; // tag is only used in aes gcm (void) tag; // tag is only used in aes gcm
{ {
size_t maxlen = MG_IO_SIZE > 16384 ? 16384 : MG_IO_SIZE; 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[9] ^= (uint8_t) ((seq >> 16) & 255U);
nonce[10] ^= (uint8_t) ((seq >> 8) & 255U); nonce[10] ^= (uint8_t) ((seq >> 8) & 255U);
nonce[11] ^= (uint8_t) ((seq) &255U); nonce[11] ^= (uint8_t) ((seq) &255U);
#if CHACHA20 #if MG_ENABLE_CHACHA20
{ {
uint8_t *dec = (uint8_t *) mg_calloc(1, msgsz); uint8_t *dec = (uint8_t *) mg_calloc(1, msgsz);
size_t n; size_t n;
@@ -11532,7 +11529,7 @@ static void mg_tls_server_send_hello(struct mg_connection *c) {
PLACEHOLDER_32B, PLACEHOLDER_32B,
// session ID length + session ID (32 bytes) // session ID length + session ID (32 bytes)
0x20, PLACEHOLDER_32B, 0x20, PLACEHOLDER_32B,
#if defined(CHACHA20) && CHACHA20 #if MG_ENABLE_CHACHA20
// TLS_CHACHA20_POLY1305_SHA256 + no compression // TLS_CHACHA20_POLY1305_SHA256 + no compression
0x13, 0x03, 0x00, 0x13, 0x03, 0x00,
#else #else
@@ -11731,7 +11728,7 @@ static void mg_tls_client_send_hello(struct mg_connection *c) {
// session ID length + session ID (32 bytes) // session ID length + session ID (32 bytes)
0x20, PLACEHOLDER_32B, 0x00, 0x20, PLACEHOLDER_32B, 0x00,
0x02, // size = 2 bytes 0x02, // size = 2 bytes
#if defined(CHACHA20) && CHACHA20 #if MG_ENABLE_CHACHA20
// TLS_CHACHA20_POLY1305_SHA256 // TLS_CHACHA20_POLY1305_SHA256
0x13, 0x03, 0x13, 0x03,
#else #else
+4
View File
@@ -1059,6 +1059,10 @@ struct timeval {
#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0 #define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0
#endif #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. // Macros to record timestamped events that happens with a connection.
+4
View File
@@ -186,3 +186,7 @@
#ifndef MG_ENABLE_TCPIP_PRINT_DEBUG_STATS #ifndef MG_ENABLE_TCPIP_PRINT_DEBUG_STATS
#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0 #define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0
#endif #endif
#ifndef MG_ENABLE_CHACHA20
#define MG_ENABLE_CHACHA20 1 // When set to 0, GCM is used. For MG_TLS_BUILTIN
#endif
+8 -11
View File
@@ -12,10 +12,6 @@
#if MG_TLS == MG_TLS_BUILTIN #if MG_TLS == MG_TLS_BUILTIN
#ifndef CHACHA20
#define CHACHA20 1
#endif
/* TLS 1.3 Record Content Type (RFC8446 B.1) */ /* TLS 1.3 Record Content Type (RFC8446 B.1) */
#define MG_TLS_CHANGE_CIPHER 20 #define MG_TLS_CHANGE_CIPHER 20
#define MG_TLS_ALERT 21 #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 hello_hash[32];
uint8_t server_hs_secret[32]; uint8_t server_hs_secret[32];
uint8_t client_hs_secret[32]; uint8_t client_hs_secret[32];
#if CHACHA20 #if MG_ENABLE_CHACHA20
const size_t keysz = 32; const size_t keysz = 32;
#else #else
const size_t keysz = 16; 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 master_secret[32];
uint8_t server_secret[32]; uint8_t server_secret[32];
uint8_t client_secret[32]; uint8_t client_secret[32];
#if CHACHA20 #if MG_ENABLE_CHACHA20
const size_t keysz = 32; const size_t keysz = 32;
#else #else
const size_t keysz = 16; 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 = uint8_t *iv =
c->is_client ? tls->enc.client_write_iv : tls->enc.server_write_iv; c->is_client ? tls->enc.client_write_iv : tls->enc.server_write_iv;
#if !CHACHA20 #if MG_ENABLE_CHACHA20
#else
mg_gcm_initialize(); mg_gcm_initialize();
#endif #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; tag = wio->buf + wio->len + msgsz + 1;
memmove(outmsg, msg, msgsz); memmove(outmsg, msg, msgsz);
outmsg[msgsz] = msgtype; outmsg[msgsz] = msgtype;
#if CHACHA20 #if MG_ENABLE_CHACHA20
(void) tag; // tag is only used in aes gcm (void) tag; // tag is only used in aes gcm
{ {
size_t maxlen = MG_IO_SIZE > 16384 ? 16384 : MG_IO_SIZE; 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[9] ^= (uint8_t) ((seq >> 16) & 255U);
nonce[10] ^= (uint8_t) ((seq >> 8) & 255U); nonce[10] ^= (uint8_t) ((seq >> 8) & 255U);
nonce[11] ^= (uint8_t) ((seq) &255U); nonce[11] ^= (uint8_t) ((seq) &255U);
#if CHACHA20 #if MG_ENABLE_CHACHA20
{ {
uint8_t *dec = (uint8_t *) mg_calloc(1, msgsz); uint8_t *dec = (uint8_t *) mg_calloc(1, msgsz);
size_t n; size_t n;
@@ -641,7 +638,7 @@ static void mg_tls_server_send_hello(struct mg_connection *c) {
PLACEHOLDER_32B, PLACEHOLDER_32B,
// session ID length + session ID (32 bytes) // session ID length + session ID (32 bytes)
0x20, PLACEHOLDER_32B, 0x20, PLACEHOLDER_32B,
#if defined(CHACHA20) && CHACHA20 #if MG_ENABLE_CHACHA20
// TLS_CHACHA20_POLY1305_SHA256 + no compression // TLS_CHACHA20_POLY1305_SHA256 + no compression
0x13, 0x03, 0x00, 0x13, 0x03, 0x00,
#else #else
@@ -840,7 +837,7 @@ static void mg_tls_client_send_hello(struct mg_connection *c) {
// session ID length + session ID (32 bytes) // session ID length + session ID (32 bytes)
0x20, PLACEHOLDER_32B, 0x00, 0x20, PLACEHOLDER_32B, 0x00,
0x02, // size = 2 bytes 0x02, // size = 2 bytes
#if defined(CHACHA20) && CHACHA20 #if MG_ENABLE_CHACHA20
// TLS_CHACHA20_POLY1305_SHA256 // TLS_CHACHA20_POLY1305_SHA256
0x13, 0x03, 0x13, 0x03,
#else #else