added printing of values

main
Jonas Arnold 3 years ago
parent 2e0bdf5487
commit ae5ccf9fa8
  1. 34
      ASYD_Cryptograhpy/SW05-SHA1/main.c

@ -22,7 +22,7 @@
#define VALUE_LENGTH 32 #define VALUE_LENGTH 32
#define MESSAGE_DIGEST_LENGTH 160 #define MESSAGE_DIGEST_LENGTH 160
#define BLOCK_LENGTH (512/8) #define BLOCK_LENGTH (512/8)
#define INPUT_MESSAGE ("Testnachricht 123") #define INPUT_MESSAGE ("abc")
#pragma region SHA1-Constants #pragma region SHA1-Constants
// initial hash values // initial hash values
@ -112,31 +112,41 @@ void main(void) {
printf("\n------ Stages ------\n\n"); printf("\n------ Stages ------\n\n");
#pragma region Stages calculations #pragma region Stages calculations
uint32_t (*f_ptr)(uint32_t, uint32_t, uint32_t) = &SHA1_f1; // use: (*f_ptr)(b,c,d); uint32_t (*f_ptr)(uint32_t, uint32_t, uint32_t) = &SHA1_f1; // use: (*f_ptr)(b,c,d);
uint8_t t = 1; uint8_t stage = 1;
// print header row
printf("\tA\t\tB\t\tC\t\tD\t\tE\n");
// stage 1...4 // stage 1...4
for (uint32_t i = 0; i < 80; i++) { for (uint32_t t = 0; t < 80; t++) {
uint32_t T = leftRotate(a, 5) + ((*f_ptr)(b, c, d)) + e + k[t] + WordArray[i]; uint32_t T = leftRotate(a, 5) + ((*f_ptr)(b, c, d)) + e + k[stage] + WordArray[t];
e = d; e = d;
d = c; d = c;
c = leftRotate(b, 30); c = leftRotate(b, 30);
b = a; b = a;
a = T; a = T;
// print row
printf("t= %02d:\t%08X\t%08X\t%08X\t%08X\t%08X\t\n", t, a, b, c, d, e);
// switch stages // switch stages
if (i == 19) { f_ptr = SHA1_f2; t = 2; } if (t == 19) { f_ptr = SHA1_f2; stage = 2; printf("Switched to stage 2.\n"); }
else if (i == 39) { f_ptr = SHA1_f3; t = 3; } else if (t == 39) { f_ptr = SHA1_f3; stage = 3; printf("Switched to stage 3.\n"); }
else if (i == 59) { f_ptr = SHA1_f4; t = 4; } else if (t == 59) { f_ptr = SHA1_f4; stage = 4; printf("Switched to stage 4.\n"); }
} }
#pragma endregion #pragma endregion
printf("\n------ Intermediate hash calculation ------\n\n"); printf("\n------ Intermediate hash calculation ------\n\n");
#pragma region intermediate hash calculation #pragma region intermediate hash calculation
h[0] = a + h[0]; uint32_t summand[5] = { a, b, c, d, e };
h[1] = b + h[1]; for (uint8_t i = 0; i < 5; i++) {
h[2] = c + h[2]; // calculate
h[3] = d + h[3]; uint32_t h_new = summand[i] + h[i];
h[4] = e + h[4]; // print
printf("H[%i] = %08X + %08X = %08X\n", i, summand[i], h[i], h_new);
// set value
h[i] = h_new;
}
#pragma endregion #pragma endregion
#pragma endregion #pragma endregion

Loading…
Cancel
Save