Skip to content

Commit

Permalink
Rename conflictive macros
Browse files Browse the repository at this point in the history
  • Loading branch information
scaprile committed Mar 3, 2025
1 parent 55bc610 commit 75f6d33
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 38 deletions.
31 changes: 12 additions & 19 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -7667,15 +7667,11 @@ void mg_hmac_sha256(uint8_t dst[32], uint8_t *key, size_t keysz, uint8_t *data,
mg_sha256_final(dst, &ctx);
}

//=====================================
// TODO: rename macros
#define ROTR64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#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))
#define rotr64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
#define ep064(x) (rotr64(x, 28) ^ rotr64(x, 34) ^ rotr64(x, 39))
#define ep164(x) (rotr64(x, 14) ^ rotr64(x, 18) ^ rotr64(x, 41))
#define sig064(x) (rotr64(x, 1) ^ rotr64(x, 8) ^ ((x) >> 7))
#define SIG164(x) (rotr64(x, 19) ^ rotr64(x, 61) ^ ((x) >> 6))

static const uint64_t mg_sha256_k2[80] = {
0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f,
Expand Down Expand Up @@ -7717,7 +7713,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 + 6] << 8) | ((uint64_t) data[j + 7]);
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];
b = ctx->state[1];
Expand All @@ -7729,8 +7725,8 @@ static void mg_sha384_transform(mg_sha384_ctx *ctx, const uint8_t data[]) {
h = ctx->state[7];

for (i = 0; i < 80; ++i) {
uint64_t t1 = h + EP1(e) + CH(e, f, g) + mg_sha256_k2[i] + m[i];
uint64_t t2 = EP0(a) + MAJ(a, b, c);
uint64_t t1 = h + ep164(e) + ch(e, f, g) + mg_sha256_k2[i] + m[i];
uint64_t t2 = ep064(a) + maj(a, b, c);
h = g;
g = f;
f = e;
Expand Down Expand Up @@ -9187,9 +9183,6 @@ static void gcm_zero_ctx(gcm_context *ctx);
*
*******************************************************************************/




static int aes_tables_inited = 0; // run-once flag for performing key
// expasion table generation (see below)
/*
Expand Down Expand Up @@ -13768,7 +13761,7 @@ void mg_tls_ctx_free(struct mg_mgr *mgr) {
#endif


#if defined(MG_TLS) && MG_TLS == MG_TLS_BUILTIN
#if MG_TLS == MG_TLS_BUILTIN

#define NS_INTERNAL static
typedef struct _bigint bigint; /**< An alias for _bigint */
Expand Down Expand Up @@ -15414,7 +15407,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_32 31

#define DEC(N) MG_UECC_CONCAT(DEC_, N)
#define DEC_(N) MG_UECC_CONCAT(DEC_, N)

#define SECOND_ARG(_, val, ...) val
#define SOME_CHECK_0 ~, 0
Expand All @@ -15428,14 +15421,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_0(...)
#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 REPEATM_NAME_0() REPEATM_0
#define REPEATM_NAME_SOME() REPEATM_SOME
#define REPEATM_0(...)
#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))
#endif

Expand Down
20 changes: 8 additions & 12 deletions src/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

//=====================================
// TODO: rename macros
#define ROTR64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#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))
#define rotr64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
#define ep064(x) (rotr64(x, 28) ^ rotr64(x, 34) ^ rotr64(x, 39))
#define ep164(x) (rotr64(x, 14) ^ rotr64(x, 18) ^ rotr64(x, 41))
#define sig064(x) (rotr64(x, 1) ^ rotr64(x, 8) ^ ((x) >> 7))
#define SIG164(x) (rotr64(x, 19) ^ rotr64(x, 61) ^ ((x) >> 6))

static const uint64_t mg_sha256_k2[80] = {
0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f,
Expand Down Expand Up @@ -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 + 6] << 8) | ((uint64_t) data[j + 7]);
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];
b = ctx->state[1];
Expand All @@ -235,8 +231,8 @@ static void mg_sha384_transform(mg_sha384_ctx *ctx, const uint8_t data[]) {
h = ctx->state[7];

for (i = 0; i < 80; ++i) {
uint64_t t1 = h + EP1(e) + CH(e, f, g) + mg_sha256_k2[i] + m[i];
uint64_t t2 = EP0(a) + MAJ(a, b, c);
uint64_t t1 = h + ep164(e) + ch(e, f, g) + mg_sha256_k2[i] + m[i];
uint64_t t2 = ep064(a) + maj(a, b, c);
h = g;
g = f;
f = e;
Expand Down
3 changes: 0 additions & 3 deletions src/tls_aes128.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
// expasion table generation (see below)
/*
Expand Down
2 changes: 1 addition & 1 deletion src/tls_rsa.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "tls_rsa.h"

#if defined(MG_TLS) && MG_TLS == MG_TLS_BUILTIN
#if MG_TLS == MG_TLS_BUILTIN

#define NS_INTERNAL static
typedef struct _bigint bigint; /**< An alias for _bigint */
Expand Down
6 changes: 3 additions & 3 deletions src/tls_uecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
#define DEC_31 30
#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 SOME_CHECK_0 ~, 0
Expand All @@ -77,14 +77,14 @@
#define REPEAT_NAME_SOME() REPEAT_SOME
#define REPEAT_0(...)
#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 REPEATM_NAME_0() REPEATM_0
#define REPEATM_NAME_SOME() REPEATM_SOME
#define REPEATM_0(...)
#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))
#endif

Expand Down

0 comments on commit 75f6d33

Please sign in to comment.