From a3f77b4f45f476e3796fc5f9cd3091effadcc984 Mon Sep 17 00:00:00 2001 From: cscherr Date: Thu, 17 Jul 2025 18:00:53 +0200 Subject: [PATCH] fix: sha2 works --- .../algorithms/algorithms-c/src/hash/sha2.c | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/crates/algorithms/algorithms-c/src/hash/sha2.c b/crates/algorithms/algorithms-c/src/hash/sha2.c index fe753f6..62659dc 100644 --- a/crates/algorithms/algorithms-c/src/hash/sha2.c +++ b/crates/algorithms/algorithms-c/src/hash/sha2.c @@ -2,6 +2,7 @@ #include "trace.h" #include #include +#include #include #define err_handler(RES) \ @@ -17,6 +18,12 @@ static uint32_t addTemp = 0; ? shaInputTooLong \ : (context)->corrupted) +#define dump_hash(context) \ + for (int o = 0; o < SHA2_256_HashParts; o++) { \ + TRACE("%08x", context->intermediate_hash[o]); \ + } \ + TRACELN("") + /* Define the SHA shift, rotate left, and rotate right macros */ #define SHA256_SHR(bits, word) ((word) >> (bits)) #define SHA256_ROTL(bits, word) (((word) << (bits)) | ((word) >> (32 - (bits)))) @@ -38,7 +45,7 @@ static uint32_t addTemp = 0; #define sha2_256_show_internal_state(context) \ TRACE("Internal State:\n\tcorrupted: %d\n\tintermediate_hash: ", \ context->corrupted); \ - dump_data((uint8_t *)context->intermediate_hash, SHA2_256_HashBytes); \ + dump_hash(context); \ TRACE("\tmessage_block_index: %d\n\tmessage_block: ", \ context->message_block_index); \ dump_data((uint8_t *)context->message_block, SHA2_256_BlockSize); \ @@ -79,13 +86,20 @@ static const uint32_t K[SHA2_256_BlockSize] = { * names used in the Secure Hash Standard. */ static void sha2_256_process_message_block(SHA2Context *context) { -#define dump_letter(X) TRACE("%s: %08x\n", STR(X), X) -#define dump_alphabet(A, B, C, D, E, F, G, H) \ +#define dump_letter(X) TRACE("%s: %08x ", STR(X), X) +#define dump_alphabet \ dump_letter(A) dump_letter(B) dump_letter(C) dump_letter(D) dump_letter(E) \ - dump_letter(F) dump_letter(G) dump_letter(H) + dump_letter(F) dump_letter(G) dump_letter(H) TRACELN("") +#define dump_w \ + TRACELN("Dumping W:"); \ + for (int o = 0; o < SHA2_256_BlockSize; o++) { \ + TRACE("\tW[%d]: %032b\n", o, W[o]) \ + } +#ifdef TEST TRACELN("before process message block"); sha2_256_show_internal_state(context); +#endif // TEST uint8_t t, t4; /* Loop counter */ uint32_t temp1, temp2; /* Temporary word value */ @@ -102,11 +116,19 @@ static void sha2_256_process_message_block(SHA2Context *context) { (((uint32_t)context->message_block[t4 + 3])); } +#ifdef TEST + dump_w; +#endif // TEST + for (t = 16; t < SHA2_256_BlockSize; t++) { W[t] = SHA256_sigma1(W[t - 2]) + W[t - 7] + SHA256_sigma0(W[t - 15]) + W[t - 16]; } +#ifdef TEST + dump_w; +#endif // TEST + A = context->intermediate_hash[0]; B = context->intermediate_hash[1]; C = context->intermediate_hash[2]; @@ -117,6 +139,11 @@ static void sha2_256_process_message_block(SHA2Context *context) { H = context->intermediate_hash[7]; for (t = 0; t < SHA2_256_BlockSize; t++) { +#ifdef TEST + TRACE("Iter: %d\t", t); + dump_alphabet; +#endif // TEST + temp1 = H + SHA256_SIGMA1(E) + SHA_Ch(E, F, G) + K[t] + W[t]; temp2 = SHA256_SIGMA0(A) + SHA_Maj(A, B, C); H = G; @@ -129,6 +156,18 @@ static void sha2_256_process_message_block(SHA2Context *context) { A = temp1 + temp2; } +#ifdef TEST + TRACE("DONE:\t") + dump_alphabet; +#endif // TEST + +#ifdef TEST + TRACE("Intermediate hash before we add the working variables:\n"); + for (int o = 0; o < SHA2_256_HashParts; o++) { + TRACE("\t%d:\t%032b\n", o, context->intermediate_hash[o]); + } +#endif // TEST + context->intermediate_hash[0] += A; context->intermediate_hash[1] += B; context->intermediate_hash[2] += C; @@ -140,8 +179,12 @@ static void sha2_256_process_message_block(SHA2Context *context) { context->message_block_index = 0; - TRACELN("after process message block"); +#ifdef TEST + TRACE("Hash data if we finish now: "); + dump_hash(context); + TRACELN("\nafter process message block"); sha2_256_show_internal_state(context); +#endif // TEST } /*