Merge pull request #3065 from cesanta/reduino

Rename conflictive macros, add and refactor crypto tests
This commit is contained in:
Sergio R. Caprile authored and GitHub committed 2025-03-04 12:11:45 -03:00
commit 7304e2171d
7 files changed
+104 -41

No files matched your search

+18 -19
View File
@@ -7836,15 +7836,11 @@ void mg_hmac_sha256(uint8_t dst[32], uint8_t *key, size_t keysz, uint8_t *data,
mg_sha256_final(dst, &ctx); mg_sha256_final(dst, &ctx);
} }
//===================================== #define rotr64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
// TODO: rename macros #define ep064(x) (rotr64(x, 28) ^ rotr64(x, 34) ^ rotr64(x, 39))
#define ROTR64(x, n) (((x) >> (n)) | ((x) << (64 - (n)))) #define ep164(x) (rotr64(x, 14) ^ rotr64(x, 18) ^ rotr64(x, 41))
#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) #define sig064(x) (rotr64(x, 1) ^ rotr64(x, 8) ^ ((x) >> 7))
#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) #define sig164(x) (rotr64(x, 19) ^ rotr64(x, 61) ^ ((x) >> 6))
#define EP0(x) (ROTR64(x, 28) ^ ROTR64(x, 34) ^ ROTR64(x, 39))
#define EP1(x) (ROTR64(x, 14) ^ ROTR64(x, 18) ^ ROTR64(x, 41))
#define SIG0(x) (ROTR64(x, 1) ^ ROTR64(x, 8) ^ ((x) >> 7))
#define SIG1(x) (ROTR64(x, 19) ^ ROTR64(x, 61) ^ ((x) >> 6))
static const uint64_t mg_sha256_k2[80] = { static const uint64_t mg_sha256_k2[80] = {
0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f,
@@ -7886,7 +7882,7 @@ static void mg_sha384_transform(mg_sha384_ctx *ctx, const uint8_t data[]) {
((uint64_t) data[j + 4] << 24) | ((uint64_t) data[j + 5] << 16) | ((uint64_t) data[j + 4] << 24) | ((uint64_t) data[j + 5] << 16) |
((uint64_t) data[j + 6] << 8) | ((uint64_t) data[j + 7]); ((uint64_t) data[j + 6] << 8) | ((uint64_t) data[j + 7]);
for (; i < 80; ++i) for (; i < 80; ++i)
m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; m[i] = sig164(m[i - 2]) + m[i - 7] + sig064(m[i - 15]) + m[i - 16];
a = ctx->state[0]; a = ctx->state[0];
b = ctx->state[1]; b = ctx->state[1];
@@ -7898,8 +7894,8 @@ static void mg_sha384_transform(mg_sha384_ctx *ctx, const uint8_t data[]) {
h = ctx->state[7]; h = ctx->state[7];
for (i = 0; i < 80; ++i) { for (i = 0; i < 80; ++i) {
uint64_t t1 = h + EP1(e) + CH(e, f, g) + mg_sha256_k2[i] + m[i]; uint64_t t1 = h + ep164(e) + ch(e, f, g) + mg_sha256_k2[i] + m[i];
uint64_t t2 = EP0(a) + MAJ(a, b, c); uint64_t t2 = ep064(a) + maj(a, b, c);
h = g; h = g;
g = f; g = f;
f = e; f = e;
@@ -9356,9 +9352,6 @@ static void gcm_zero_ctx(gcm_context *ctx);
* *
*******************************************************************************/ *******************************************************************************/
static int aes_tables_inited = 0; // run-once flag for performing key static int aes_tables_inited = 0; // run-once flag for performing key
// expasion table generation (see below) // expasion table generation (see below)
/* /*
@@ -13937,7 +13930,8 @@ void mg_tls_ctx_free(struct mg_mgr *mgr) {
#endif #endif
#if defined(MG_TLS) && MG_TLS == MG_TLS_BUILTIN
#if MG_TLS == MG_TLS_BUILTIN
#define NS_INTERNAL static #define NS_INTERNAL static
typedef struct _bigint bigint; /**< An alias for _bigint */ typedef struct _bigint bigint; /**< An alias for _bigint */
@@ -15583,7 +15577,7 @@ int mg_rsa_mod_pow(const uint8_t *mod, size_t modsz, const uint8_t *exp, size_t
#define DEC_31 30 #define DEC_31 30
#define DEC_32 31 #define DEC_32 31
#define DEC(N) MG_UECC_CONCAT(DEC_, N) #define DEC_(N) MG_UECC_CONCAT(DEC_, N)
#define SECOND_ARG(_, val, ...) val #define SECOND_ARG(_, val, ...) val
#define SOME_CHECK_0 ~, 0 #define SOME_CHECK_0 ~, 0
@@ -15597,14 +15591,14 @@ int mg_rsa_mod_pow(const uint8_t *mod, size_t modsz, const uint8_t *exp, size_t
#define REPEAT_NAME_SOME() REPEAT_SOME #define REPEAT_NAME_SOME() REPEAT_SOME
#define REPEAT_0(...) #define REPEAT_0(...)
#define REPEAT_SOME(N, stuff) \ #define REPEAT_SOME(N, stuff) \
DEFER(MG_UECC_CONCAT(REPEAT_NAME_, SOME_OR_0(DEC(N))))()(DEC(N), stuff) stuff DEFER(MG_UECC_CONCAT(REPEAT_NAME_, SOME_OR_0(DEC_(N))))()(DEC_(N), stuff) stuff
#define REPEAT(N, stuff) EVAL(REPEAT_SOME(N, stuff)) #define REPEAT(N, stuff) EVAL(REPEAT_SOME(N, stuff))
#define REPEATM_NAME_0() REPEATM_0 #define REPEATM_NAME_0() REPEATM_0
#define REPEATM_NAME_SOME() REPEATM_SOME #define REPEATM_NAME_SOME() REPEATM_SOME
#define REPEATM_0(...) #define REPEATM_0(...)
#define REPEATM_SOME(N, macro) \ #define REPEATM_SOME(N, macro) \
macro(N) DEFER(MG_UECC_CONCAT(REPEATM_NAME_, SOME_OR_0(DEC(N))))()(DEC(N), macro) macro(N) DEFER(MG_UECC_CONCAT(REPEATM_NAME_, SOME_OR_0(DEC_(N))))()(DEC_(N), macro)
#define REPEATM(N, macro) EVAL(REPEATM_SOME(N, macro)) #define REPEATM(N, macro) EVAL(REPEATM_SOME(N, macro))
#endif #endif
@@ -18756,6 +18750,9 @@ void mg_uecc_point_mult(mg_uecc_word_t *result, const mg_uecc_word_t *point,
#if MG_TLS == MG_TLS_BUILTIN
const uint8_t X25519_BASE_POINT[X25519_BYTES] = {9}; const uint8_t X25519_BASE_POINT[X25519_BYTES] = {9};
#define X25519_WBITS 32 #define X25519_WBITS 32
@@ -19005,6 +19002,8 @@ int mg_tls_x25519(uint8_t out[X25519_BYTES], const uint8_t scalar[X25519_BYTES],
return ret; return ret;
} }
#endif
#ifdef MG_ENABLE_LINES #ifdef MG_ENABLE_LINES
#line 1 "src/url.c" #line 1 "src/url.c"
#endif #endif
+8 -12
View File
@@ -173,15 +173,11 @@ void mg_hmac_sha256(uint8_t dst[32], uint8_t *key, size_t keysz, uint8_t *data,
mg_sha256_final(dst, &ctx); mg_sha256_final(dst, &ctx);
} }
//===================================== #define rotr64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
// TODO: rename macros #define ep064(x) (rotr64(x, 28) ^ rotr64(x, 34) ^ rotr64(x, 39))
#define ROTR64(x, n) (((x) >> (n)) | ((x) << (64 - (n)))) #define ep164(x) (rotr64(x, 14) ^ rotr64(x, 18) ^ rotr64(x, 41))
#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) #define sig064(x) (rotr64(x, 1) ^ rotr64(x, 8) ^ ((x) >> 7))
#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) #define sig164(x) (rotr64(x, 19) ^ rotr64(x, 61) ^ ((x) >> 6))
#define EP0(x) (ROTR64(x, 28) ^ ROTR64(x, 34) ^ ROTR64(x, 39))
#define EP1(x) (ROTR64(x, 14) ^ ROTR64(x, 18) ^ ROTR64(x, 41))
#define SIG0(x) (ROTR64(x, 1) ^ ROTR64(x, 8) ^ ((x) >> 7))
#define SIG1(x) (ROTR64(x, 19) ^ ROTR64(x, 61) ^ ((x) >> 6))
static const uint64_t mg_sha256_k2[80] = { static const uint64_t mg_sha256_k2[80] = {
0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f,
@@ -223,7 +219,7 @@ static void mg_sha384_transform(mg_sha384_ctx *ctx, const uint8_t data[]) {
((uint64_t) data[j + 4] << 24) | ((uint64_t) data[j + 5] << 16) | ((uint64_t) data[j + 4] << 24) | ((uint64_t) data[j + 5] << 16) |
((uint64_t) data[j + 6] << 8) | ((uint64_t) data[j + 7]); ((uint64_t) data[j + 6] << 8) | ((uint64_t) data[j + 7]);
for (; i < 80; ++i) for (; i < 80; ++i)
m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; m[i] = sig164(m[i - 2]) + m[i - 7] + sig064(m[i - 15]) + m[i - 16];
a = ctx->state[0]; a = ctx->state[0];
b = ctx->state[1]; b = ctx->state[1];
@@ -235,8 +231,8 @@ static void mg_sha384_transform(mg_sha384_ctx *ctx, const uint8_t data[]) {
h = ctx->state[7]; h = ctx->state[7];
for (i = 0; i < 80; ++i) { for (i = 0; i < 80; ++i) {
uint64_t t1 = h + EP1(e) + CH(e, f, g) + mg_sha256_k2[i] + m[i]; uint64_t t1 = h + ep164(e) + ch(e, f, g) + mg_sha256_k2[i] + m[i];
uint64_t t2 = EP0(a) + MAJ(a, b, c); uint64_t t2 = ep064(a) + maj(a, b, c);
h = g; h = g;
g = f; g = f;
f = e; f = e;
-3
View File
@@ -188,9 +188,6 @@ static void gcm_zero_ctx(gcm_context *ctx);
* *
*******************************************************************************/ *******************************************************************************/
#include "tls.h"
#include "tls_aes128.h"
static int aes_tables_inited = 0; // run-once flag for performing key static int aes_tables_inited = 0; // run-once flag for performing key
// expasion table generation (see below) // expasion table generation (see below)
/* /*
+2 -1
View File
@@ -1,6 +1,7 @@
#include "tls.h"
#include "tls_rsa.h" #include "tls_rsa.h"
#if defined(MG_TLS) && MG_TLS == MG_TLS_BUILTIN #if MG_TLS == MG_TLS_BUILTIN
#define NS_INTERNAL static #define NS_INTERNAL static
typedef struct _bigint bigint; /**< An alias for _bigint */ typedef struct _bigint bigint; /**< An alias for _bigint */
+3 -3
View File
@@ -63,7 +63,7 @@
#define DEC_31 30 #define DEC_31 30
#define DEC_32 31 #define DEC_32 31
#define DEC(N) MG_UECC_CONCAT(DEC_, N) #define DEC_(N) MG_UECC_CONCAT(DEC_, N)
#define SECOND_ARG(_, val, ...) val #define SECOND_ARG(_, val, ...) val
#define SOME_CHECK_0 ~, 0 #define SOME_CHECK_0 ~, 0
@@ -77,14 +77,14 @@
#define REPEAT_NAME_SOME() REPEAT_SOME #define REPEAT_NAME_SOME() REPEAT_SOME
#define REPEAT_0(...) #define REPEAT_0(...)
#define REPEAT_SOME(N, stuff) \ #define REPEAT_SOME(N, stuff) \
DEFER(MG_UECC_CONCAT(REPEAT_NAME_, SOME_OR_0(DEC(N))))()(DEC(N), stuff) stuff DEFER(MG_UECC_CONCAT(REPEAT_NAME_, SOME_OR_0(DEC_(N))))()(DEC_(N), stuff) stuff
#define REPEAT(N, stuff) EVAL(REPEAT_SOME(N, stuff)) #define REPEAT(N, stuff) EVAL(REPEAT_SOME(N, stuff))
#define REPEATM_NAME_0() REPEATM_0 #define REPEATM_NAME_0() REPEATM_0
#define REPEATM_NAME_SOME() REPEATM_SOME #define REPEATM_NAME_SOME() REPEATM_SOME
#define REPEATM_0(...) #define REPEATM_0(...)
#define REPEATM_SOME(N, macro) \ #define REPEATM_SOME(N, macro) \
macro(N) DEFER(MG_UECC_CONCAT(REPEATM_NAME_, SOME_OR_0(DEC(N))))()(DEC(N), macro) macro(N) DEFER(MG_UECC_CONCAT(REPEATM_NAME_, SOME_OR_0(DEC_(N))))()(DEC_(N), macro)
#define REPEATM(N, macro) EVAL(REPEATM_SOME(N, macro)) #define REPEATM(N, macro) EVAL(REPEATM_SOME(N, macro))
#endif #endif
+5
View File
@@ -4,9 +4,12 @@
* Author: Mike Hamburg * Author: Mike Hamburg
* License: MIT License * License: MIT License
*/ */
#include "tls.h"
#include "tls_x25519.h" #include "tls_x25519.h"
#include "util.h" #include "util.h"
#if MG_TLS == MG_TLS_BUILTIN
const uint8_t X25519_BASE_POINT[X25519_BYTES] = {9}; const uint8_t X25519_BASE_POINT[X25519_BYTES] = {9};
#define X25519_WBITS 32 #define X25519_WBITS 32
@@ -255,3 +258,5 @@ int mg_tls_x25519(uint8_t out[X25519_BYTES], const uint8_t scalar[X25519_BYTES],
} }
return ret; return ret;
} }
#endif
+68 -3
View File
@@ -3587,6 +3587,63 @@ static void test_sha1(void) {
test_sha1_str(")_)+_)!&^*%$#>>>{}}}{{{][[[[]]]", expected_hash_3); test_sha1_str(")_)+_)!&^*%$#>>>{}}}{{{][[[[]]]", expected_hash_3);
} }
static void test_sha256_str(const char *string,
const unsigned char *expected_hash) {
unsigned char digest[32];
mg_sha256(digest, (unsigned char *) string, strlen(string));
ASSERT((memcmp(digest, expected_hash, 32) == 0));
}
static void test_sha256(void) {
const unsigned char expected_hash_1[] = {
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4,
0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b,
0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55};
const unsigned char expected_hash_2[] = {
0xbc, 0x07, 0x32, 0x21, 0x17, 0x8e, 0x81, 0xbd, 0x2f, 0x67, 0x13,
0x3a, 0xca, 0xb4, 0x07, 0xad, 0x5b, 0x61, 0x8b, 0x33, 0xd2, 0x95,
0x9e, 0x94, 0x45, 0x45, 0xdc, 0x24, 0x99, 0x0a, 0xff, 0x92};
const unsigned char expected_hash_3[] = {
0x1b, 0x65, 0x3e, 0xda, 0x9a, 0x2a, 0x24, 0x55, 0xa3, 0x56, 0x38,
0x08, 0xf4, 0xf7, 0xc5, 0xa6, 0xc5, 0x2d, 0x2c, 0xb1, 0x71, 0xe5,
0x90, 0x4c, 0x83, 0x9c, 0x77, 0x92, 0x51, 0xa2, 0x84, 0x4a};
test_sha256_str("", expected_hash_1);
test_sha256_str(
"#&*%$DHFH(0x12345)^&*(^!@$%^^&&*1298**&^%DHKSHFLS)(*)&^^%$#!!!!",
expected_hash_2);
test_sha256_str(")_)+_)!&^*%$#>>>{}}}{{{][[[[]]]", expected_hash_3);
}
static void test_sha384_str(const char *string,
const unsigned char *expected_hash) {
unsigned char digest[48];
mg_sha384(digest, (unsigned char *) string, strlen(string));
ASSERT((memcmp(digest, expected_hash, 48) == 0));
}
static void test_sha384(void) {
const unsigned char expected_hash_1[] = {
0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e,
0xb1, 0xb1, 0xe3, 0x6a, 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43,
0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda, 0x27, 0x4e, 0xde, 0xbf,
0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b};
const unsigned char expected_hash_2[] = {
0x77, 0xe7, 0x0a, 0x31, 0xe5, 0xcd, 0x68, 0xa4, 0xc5, 0xb3, 0x70, 0x55,
0x38, 0xd0, 0x90, 0xb0, 0xcd, 0xb6, 0xf4, 0x1c, 0x2e, 0xe6, 0xf4, 0xdd,
0xf6, 0xb4, 0xfc, 0x97, 0x01, 0x79, 0x3c, 0x89, 0x82, 0x3b, 0x13, 0xa2,
0x48, 0xa7, 0xfe, 0xd2, 0xd0, 0xc4, 0xbf, 0xed, 0x85, 0xb6, 0x20, 0xc7};
const unsigned char expected_hash_3[] = {
0x45, 0xa1, 0xc6, 0x4d, 0x99, 0x29, 0x42, 0x87, 0x49, 0x46, 0x73, 0x3c,
0x3b, 0xc8, 0xbc, 0x9c, 0x43, 0x10, 0x75, 0x23, 0x89, 0x22, 0x04, 0x41,
0xcd, 0xa3, 0x34, 0xeb, 0x97, 0x9f, 0x2a, 0xbf, 0x17, 0x94, 0x38, 0x72,
0x6b, 0xd8, 0x8e, 0xcc, 0xb5, 0x50, 0xc6, 0x5b, 0x35, 0x1f, 0x91, 0x90};
test_sha384_str("", expected_hash_1);
test_sha384_str(
"#&*%$DHFH(0x12345)^&*(^!@$%^^&&*1298**&^%DHKSHFLS)(*)&^^%$#!!!!",
expected_hash_2);
test_sha384_str(")_)+_)!&^*%$#>>>{}}}{{{][[[[]]]", expected_hash_3);
}
static void test_split(void) { static void test_split(void) {
struct mg_str a, b, s; struct mg_str a, b, s;
@@ -3638,7 +3695,8 @@ static void test_split(void) {
ASSERT(mg_strcmp(b, mg_str("")) == 0); ASSERT(mg_strcmp(b, mg_str("")) == 0);
} }
static void test_crypto(void) { static void test_x25519(void) {
#if MG_TLS == MG_TLS_BUILTIN
uint8_t key[X25519_BYTES]; uint8_t key[X25519_BYTES];
uint8_t buf[X25519_BYTES]; uint8_t buf[X25519_BYTES];
char tmp[100]; char tmp[100];
@@ -3649,6 +3707,15 @@ static void test_crypto(void) {
mg_snprintf(tmp, sizeof(tmp), "%M", mg_print_hex, sizeof(buf), buf); mg_snprintf(tmp, sizeof(tmp), "%M", mg_print_hex, sizeof(buf), buf);
MG_INFO(("%s", tmp)); MG_INFO(("%s", tmp));
ASSERT(mg_strcmp(mg_str("8f40c5adb6"), mg_str_n(tmp, 10)) == 0); ASSERT(mg_strcmp(mg_str("8f40c5adb6"), mg_str_n(tmp, 10)) == 0);
#endif
}
static void test_crypto(void) {
test_md5();
test_sha1();
test_sha256();
test_sha384();
test_x25519();
} }
int main(void) { int main(void) {
@@ -3698,8 +3765,6 @@ int main(void) {
(void) test_sntp, (void) test_mqtt, (void) test_http_client; (void) test_sntp, (void) test_mqtt, (void) test_http_client;
#endif #endif
test_poll(); test_poll();
test_md5();
test_sha1();
printf("SUCCESS. Total tests: %d\n", s_num_tests); printf("SUCCESS. Total tests: %d\n", s_num_tests);
return EXIT_SUCCESS; return EXIT_SUCCESS;