]> foleosoft.com Git - CryptoFoleo.git/commitdiff
Sat Aug 19 02:01:06 PM EDT 2023
authormiha-q <>
Sat, 19 Aug 2023 18:01:06 +0000 (14:01 -0400)
committermiha-q <>
Sat, 19 Aug 2023 18:01:06 +0000 (14:01 -0400)
src/sha256.c

index cb077602fd85e0cd09e56010148e390fa6412c8f..e7269d0a434afbcf248cc618ba28a7c9dc4ffdd1 100644 (file)
@@ -45,7 +45,7 @@ static uint32_t foleo_sha256_k[] =
    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
 };
 
-static void foleo_sha256_process_block(uint32_t *p)
+static void foleo_sha256_process_block(uint32_t* h, uint32_t *p)
 {
 
     uint8_t i;
@@ -64,14 +64,14 @@ static void foleo_sha256_process_block(uint32_t *p)
 
     i = 0;
     
-    A = foleo_sha256_h[0];
-    B = foleo_sha256_h[1];
-    C = foleo_sha256_h[2];
-    D = foleo_sha256_h[3];
-    E = foleo_sha256_h[4];
-    F = foleo_sha256_h[5];
-    G = foleo_sha256_h[6];
-    H = foleo_sha256_h[7];
+    A = h[0];
+    B = h[1];
+    C = h[2];
+    D = h[3];
+    E = h[4];
+    F = h[5];
+    G = h[6];
+    H = h[7];
 
     for (i = 0; i < 64; i++)
     {
@@ -92,14 +92,14 @@ static void foleo_sha256_process_block(uint32_t *p)
         A = t1 + t2;
     }
 
-    foleo_sha256_h[0] += A;
-    foleo_sha256_h[1] += B;
-    foleo_sha256_h[2] += C;
-    foleo_sha256_h[3] += D;
-    foleo_sha256_h[4] += E;
-    foleo_sha256_h[5] += F;
-    foleo_sha256_h[6] += G;
-    foleo_sha256_h[7] += H;
+    h[0] += A;
+    h[1] += B;
+    h[2] += C;
+    h[3] += D;
+    h[4] += E;
+    h[5] += F;
+    h[6] += G;
+    h[7] += H;
 
 }
 
@@ -152,9 +152,10 @@ static uint32_t* foleo_sha256_pad(uint8_t* msg, uint32_t size, uint32_t* newsize
 uint8_t* foleo_sha256(uint8_t* msg, uint32_t size)
 {
     //set initial state
-    for (uint8_t i = 0; i < sizeof(foleo_sha256_h) / sizeof(uint32_t); i++)
+    uint32_t* h = malloc(sizeof(foleo_sha256_init));
+    for (uint8_t i = 0; i < sizeof(foleo_sha256_init) / sizeof(uint32_t); i++)
     {
-        foleo_sha256_h[i] = foleo_sha256_init[i];
+        h[i] = foleo_sha256_init[i];
     }
 
     //pad the message
@@ -164,21 +165,22 @@ uint8_t* foleo_sha256(uint8_t* msg, uint32_t size)
     //run the algorithm
     for (uint32_t i = 0; i < newsize / 16; i++)
     {
-        foleo_sha256_process_block(padded + (i * 16));
+        foleo_sha256_process_block(h, padded + (i * 16));
     }
     
     //done
     free(padded);
 
     //breakout
-    uint8_t* out = malloc(sizeof(foleo_sha256_h) * sizeof(uint32_t));
-    for (uint32_t i = 0; i < sizeof(foleo_sha256_h); i++)
+    uint8_t* out = malloc(sizeof(foleo_sha256_init));
+    for (uint32_t i = 0; i < sizeof(foleo_sha256_init) / sizeof(uint32_t); i++)
     {
-        out[i * sizeof(uint32_t) + 0] = foleo_sha256_h[i] >> 24;
-        out[i * sizeof(uint32_t) + 1] = foleo_sha256_h[i] >> 16;
-        out[i * sizeof(uint32_t) + 2] = foleo_sha256_h[i] >>  8;
-        out[i * sizeof(uint32_t) + 3] = foleo_sha256_h[i];
+        out[i * sizeof(uint32_t) + 0] = h[i] >> 24;
+        out[i * sizeof(uint32_t) + 1] = h[i] >> 16;
+        out[i * sizeof(uint32_t) + 2] = h[i] >>  8;
+        out[i * sizeof(uint32_t) + 3] = h[i];
     }
+    free(h);
     return out;
 }