refactor: hide debug info

This commit is contained in:
cscherr 2025-07-17 18:16:55 +02:00
parent a3f77b4f45
commit 7ef66ead56
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7
3 changed files with 41 additions and 50 deletions

View file

@ -5,6 +5,25 @@
#include <stdio.h> #include <stdio.h>
#include <threads.h> #include <threads.h>
#ifdef TEST
// #define SHOW_INTERNALS
#endif // TEST
#ifdef SHOW_INTERNALS
#define sha2_256_show_internal_state(context) \
TRACE("Internal State:\n\tcorrupted: %d\n\tintermediate_hash: ", \
context->corrupted); \
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); \
TRACE("\tcorrupted: %d\n\tcomputed: %d\n", context->corrupted, \
context->computed);
#else
#define sha2_256_show_internal_state(context)
#endif // SHOW_INTERNALS
#define err_handler(RES) \ #define err_handler(RES) \
if (RES) { \ if (RES) { \
return RES; \ return RES; \
@ -42,16 +61,6 @@ static uint32_t addTemp = 0;
#define SHA_Ch(x, y, z) (((x) & ((y) ^ (z))) ^ (z)) #define SHA_Ch(x, y, z) (((x) & ((y) ^ (z))) ^ (z))
#define SHA_Maj(x, y, z) (((x) & ((y) | (z))) | ((y) & (z))) #define SHA_Maj(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
#define sha2_256_show_internal_state(context) \
TRACE("Internal State:\n\tcorrupted: %d\n\tintermediate_hash: ", \
context->corrupted); \
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); \
TRACE("\tcorrupted: %d\n\tcomputed: %d\n", context->corrupted, \
context->computed);
static const uint32_t H[SHA2_256_HashParts] = { static const uint32_t H[SHA2_256_HashParts] = {
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
@ -96,10 +105,10 @@ static void sha2_256_process_message_block(SHA2Context *context) {
TRACE("\tW[%d]: %032b\n", o, W[o]) \ TRACE("\tW[%d]: %032b\n", o, W[o]) \
} }
#ifdef TEST #ifdef SHOW_INTERNALS
TRACELN("before process message block"); TRACELN("before process message block");
sha2_256_show_internal_state(context); sha2_256_show_internal_state(context);
#endif // TEST #endif // SHOW_INTERNALS
uint8_t t, t4; /* Loop counter */ uint8_t t, t4; /* Loop counter */
uint32_t temp1, temp2; /* Temporary word value */ uint32_t temp1, temp2; /* Temporary word value */
@ -116,18 +125,18 @@ static void sha2_256_process_message_block(SHA2Context *context) {
(((uint32_t)context->message_block[t4 + 3])); (((uint32_t)context->message_block[t4 + 3]));
} }
#ifdef TEST #ifdef SHOW_INTERNALS
dump_w; dump_w;
#endif // TEST #endif // SHOW_INTERNALS
for (t = 16; t < SHA2_256_BlockSize; t++) { 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] = SHA256_sigma1(W[t - 2]) + W[t - 7] + SHA256_sigma0(W[t - 15]) +
W[t - 16]; W[t - 16];
} }
#ifdef TEST #ifdef SHOW_INTERNALS
dump_w; dump_w;
#endif // TEST #endif // SHOW_INTERNALS
A = context->intermediate_hash[0]; A = context->intermediate_hash[0];
B = context->intermediate_hash[1]; B = context->intermediate_hash[1];
@ -139,10 +148,10 @@ static void sha2_256_process_message_block(SHA2Context *context) {
H = context->intermediate_hash[7]; H = context->intermediate_hash[7];
for (t = 0; t < SHA2_256_BlockSize; t++) { for (t = 0; t < SHA2_256_BlockSize; t++) {
#ifdef TEST #ifdef SHOW_INTERNALS
TRACE("Iter: %d\t", t); TRACE("Iter: %d\t", t);
dump_alphabet; dump_alphabet;
#endif // TEST #endif // SHOW_INTERNALS
temp1 = H + SHA256_SIGMA1(E) + SHA_Ch(E, F, G) + K[t] + W[t]; temp1 = H + SHA256_SIGMA1(E) + SHA_Ch(E, F, G) + K[t] + W[t];
temp2 = SHA256_SIGMA0(A) + SHA_Maj(A, B, C); temp2 = SHA256_SIGMA0(A) + SHA_Maj(A, B, C);
@ -156,17 +165,17 @@ static void sha2_256_process_message_block(SHA2Context *context) {
A = temp1 + temp2; A = temp1 + temp2;
} }
#ifdef TEST #ifdef SHOW_INTERNALS
TRACE("DONE:\t") TRACE("DONE:\t")
dump_alphabet; dump_alphabet;
#endif // TEST #endif // SHOW_INTERNALS
#ifdef TEST #ifdef SHOW_INTERNALS
TRACE("Intermediate hash before we add the working variables:\n"); TRACE("Intermediate hash before we add the working variables:\n");
for (int o = 0; o < SHA2_256_HashParts; o++) { for (int o = 0; o < SHA2_256_HashParts; o++) {
TRACE("\t%d:\t%032b\n", o, context->intermediate_hash[o]); TRACE("\t%d:\t%032b\n", o, context->intermediate_hash[o]);
} }
#endif // TEST #endif // SHOW_INTERNALS
context->intermediate_hash[0] += A; context->intermediate_hash[0] += A;
context->intermediate_hash[1] += B; context->intermediate_hash[1] += B;
@ -179,12 +188,12 @@ static void sha2_256_process_message_block(SHA2Context *context) {
context->message_block_index = 0; context->message_block_index = 0;
#ifdef TEST #ifdef SHOW_INTERNALS
TRACE("Hash data if we finish now: "); TRACE("Hash data if we finish now: ");
dump_hash(context); dump_hash(context);
TRACELN("\nafter process message block"); TRACELN("\nafter process message block");
sha2_256_show_internal_state(context); sha2_256_show_internal_state(context);
#endif // TEST #endif // SHOW_INTERNALS
} }
/* /*
@ -210,7 +219,6 @@ static void sha2_256_process_message_block(SHA2Context *context) {
* Nothing. * Nothing.
*/ */
static void sha2_256_pad_message(SHA2Context *context, uint8_t pad_byte) { static void sha2_256_pad_message(SHA2Context *context, uint8_t pad_byte) {
TRACELN("pad message");
/* /*
* Check to see if the current message block is too small to hold * Check to see if the current message block is too small to hold
* the initial padding bits and length. If so, we will pad the * the initial padding bits and length. If so, we will pad the
@ -242,7 +250,6 @@ static void sha2_256_pad_message(SHA2Context *context, uint8_t pad_byte) {
sha2_256_process_message_block(context); sha2_256_process_message_block(context);
TRACELN("After pad message:");
sha2_256_show_internal_state(context); sha2_256_show_internal_state(context);
} }
@ -318,9 +325,6 @@ SHA2Result sha2_256_reset(SHA2Context *context) {
*/ */
SHA2Result sha2_256_input(SHA2Context *context, const uint8_t data[], SHA2Result sha2_256_input(SHA2Context *context, const uint8_t data[],
size_t len) { size_t len) {
TRACE("Processing input: ");
dump_data(data, len);
sha2_256_show_internal_state(context); sha2_256_show_internal_state(context);
if (!context) { if (!context) {
@ -349,8 +353,6 @@ SHA2Result sha2_256_input(SHA2Context *context, const uint8_t data[],
(context->message_block_index == SHA2_256_BlockSize)) { (context->message_block_index == SHA2_256_BlockSize)) {
TRACELN("processing the message block after inputting") TRACELN("processing the message block after inputting")
sha2_256_process_message_block(context); sha2_256_process_message_block(context);
} else {
TRACELN("not processing the message block");
} }
data++; data++;
@ -404,21 +406,17 @@ SHA2Result sha2_256_oneshot(const uint8_t *data, const size_t len,
SHA2Context context; SHA2Context context;
SHA2Result res; SHA2Result res;
TRACELN("SHA2 reset");
res = sha2_256_reset(&context); res = sha2_256_reset(&context);
err_handler(res); err_handler(res);
sha2_256_show_internal_state((&context)); sha2_256_show_internal_state((&context));
TRACELN("SHA2 input");
res = sha2_256_input(&context, data, len); res = sha2_256_input(&context, data, len);
err_handler(res); err_handler(res);
sha2_256_show_internal_state((&context)); sha2_256_show_internal_state((&context));
TRACELN("SHA2 result");
res = sha2_256_result(&context, digest); res = sha2_256_result(&context, digest);
err_handler(res); err_handler(res);
sha2_256_show_internal_state((&context)); sha2_256_show_internal_state((&context));
TRACELN("SHA2 return");
return res; return res;
} }

View file

@ -10,6 +10,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#define HASH(b0, b1, b2, b3, b4, b5, b6, b7) {b0, b1, b2, b3, b4, b5, b6, b7}
#define SHA2_256_HashBits 256 #define SHA2_256_HashBits 256
#define SHA2_256_HashBytes SHA2_256_HashBits / 8 #define SHA2_256_HashBytes SHA2_256_HashBits / 8
#define SHA2_256_HashParts SHA2_256_HashBytes / 4 #define SHA2_256_HashParts SHA2_256_HashBytes / 4

View file

@ -10,22 +10,15 @@ void setUp(void) {}
void tearDown(void) {} void tearDown(void) {}
#define HEXARR(...) \
(uint8_t[SHA2_256_HashBytes]) { __VA_ARGS__ }
// BUG: may drop the null at end of strings // BUG: may drop the null at end of strings
#define SAMPLE(INPUT, ...) {(uint8_t[]){INPUT "\0"}, HEXARR(__VA_ARGS__)} #define SAMPLE(INPUT, HASH) \
{(uint8_t[]){INPUT "\0"}, (uint8_t *)(uint32_t[])HASH}
#define TEST_VALUES_LEN 3 #define TEST_VALUES_LEN 1
uint8_t *test_values[TEST_VALUES_LEN][2] = { uint8_t *test_values[TEST_VALUES_LEN][2] = {
SAMPLE("AAAA", 0x63, 0xc1, 0xdd, 0x95, 0x1f, 0xfe, 0xdf, 0x6f, 0x7f, 0xd9, SAMPLE("AAAA", HASH(0x63c1dd95, 0x1ffedf6f, 0x7fd968ad, 0x4efa39b8,
0x68, 0xad, 0x4e, 0xfa, 0x39, 0xb8, 0xed, 0x58, 0x4f, 0x16, 0x2f, 0xed584f16, 0x2f46e715, 0x114ee184, 0xf8de9201)),
0x46, 0xe7, 0x15, 0x11, 0x4e, 0xe1, 0x84, 0xf8, 0xde, 0x92, 0x01, ), };
SAMPLE("", 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, ),
SAMPLE("BAAA", 0x49, 0xe3, 0xcd, 0x45, 0x27, 0xc9, 0x6c, 0xdc, 0x01, 0x01,
0x60, 0xff, 0x08, 0x52, 0x0e, 0x0c, 0xb6, 0x3c, 0x6e, 0xf8, 0xc4,
0xe7, 0xd4, 0x86, 0x08, 0x99, 0x53, 0x43, 0x7f, 0x83, 0xa1, 0x59, )};
void test_sha2_check(void) { void test_sha2_check(void) {
SHA2Digest digest; SHA2Digest digest;
@ -40,7 +33,7 @@ void test_sha2_check(void) {
TRACELN("Input:"); TRACELN("Input:");
dump_data(input, len); dump_data(input, len);
TRACELN("Hash oneshot"); TRACELN("Call oneshot");
res = sha2_256_oneshot(input, len, digest); res = sha2_256_oneshot(input, len, digest);
TEST_ASSERT_EQUAL_MESSAGE( TEST_ASSERT_EQUAL_MESSAGE(
shaSuccess, res, shaSuccess, res,
@ -56,5 +49,4 @@ void test_sha2_check(void) {
TEST_ASSERT_EQUAL_MEMORY(test_values[i][1], digest, SHA2_256_HashBytes); TEST_ASSERT_EQUAL_MEMORY(test_values[i][1], digest, SHA2_256_HashBytes);
} }
TRACELN("ALL TESTS DONE")
} }