diff --git a/dummyFile b/dummyFile index 557db03..f2ba8f8 100644 --- a/dummyFile +++ b/dummyFile @@ -1 +1 @@ -Hello World +abc \ No newline at end of file diff --git a/example.c b/example.c index dd6466d..0ced164 100644 --- a/example.c +++ b/example.c @@ -1,15 +1,20 @@ #include "sha1.h" #include +#include int main(){ SHA1_Result r = sha1file("dummyFile"); char buffer[41]; + + // final 160 bit hash value is : + // (h0 << 128) | (h1 << 96) | (h2 << 64) | (h3 << 32) | h4 + // but since c does not support ints bigger than 64 bit, we will just print the values of all h(i)'s sprintf(buffer, "%x%x%x%x%x", r.h0, r.h1, r.h2, r.h3, r.h4); int i = 0; while(buffer[i] != '\0') printf("%c", buffer[i++]); printf("\n"); - // Expected : 1d135491d20198c382098d477009a910d7e9ca22 + return 0; } diff --git a/sha1.h b/sha1.h index 1c9f95a..8660022 100644 --- a/sha1.h +++ b/sha1.h @@ -1,10 +1,12 @@ -#ifndef _SHA1_H -#define _SHA1_H +#ifndef INCLUDED_SHA1_H +#define INCLUDED_SHA1_H #include #include #include +#define leftRotate32(N,D) (N<>(32-D)) + typedef struct SHA1_Result{ uint32_t h0; uint32_t h1; @@ -13,13 +15,6 @@ typedef struct SHA1_Result{ uint32_t h4; } SHA1_Result; -SHA1_Result sha1file(char *filename); - -uint32_t leftRotate(uint32_t n, unsigned int d) -{ - return (n << d) | (n >> (32 - d)); -} - SHA1_Result sha1file(char *filename){ FILE* file; @@ -28,11 +23,11 @@ SHA1_Result sha1file(char *filename){ uint32_t size_in_bytes = 0; uint64_t size_in_bits = 0; - uint32_t h0 = 1742612993; - uint32_t h1 = 3137593996; - uint32_t h2 = 18805212; - uint32_t h3 = 2459167202; - uint32_t h4 = 3455001513; + uint32_t h0 = 0x67452301; + uint32_t h1 = 0xEFCDAB89; + uint32_t h2 = 0x98BADCFE; + uint32_t h3 = 0x10325476; + uint32_t h4 = 0xC3D2E1F0; file = fopen(filename,"r"); if(errno != 0){ @@ -42,14 +37,17 @@ SHA1_Result sha1file(char *filename){ } size_t bytes_read; - char buffer[64]; - - while(1){ + uint8_t buffer[64]; + + int lastRound = 0; + while(lastRound != 1){ bytes_read = fread(buffer, 1, 64, file); number_of_chuncks++; + + printf("We read %d bytes\n", bytes_read); + if(bytes_read != 64){ - // * We need to convert to 512 bit * // - // printf("\nWe read %d bytes", bytes_read); + // * We need to convert to 512 bit * // size_in_bytes += bytes_read; // the final size is done here size_in_bits = size_in_bytes * 8; @@ -58,122 +56,92 @@ SHA1_Result sha1file(char *filename){ bytes_read += 1; for(int i = bytes_read; i <= 55; i++) - buffer[i] = 0x00; + buffer[i] = 0; //adding size to end for(int i = 63; i >= 56; i--){ buffer[i] = size_in_bits % 256; size_in_bits /= 256; } - - /////////////////////// ****************************************** //////////////////////////////////////////// - //continue how we do for 512 bit chunck - uint32_t w[80]; - for(int i = 0; i < 16; i++) - w[i] = buffer[i] + buffer[i+1] * 256 + buffer[i+2] * 65536 + buffer[i+3] * 16777216; - for(int i = 16; i < 80; i++) - w[i] = leftRotate( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i - 16] , 1 ); - - uint32_t a = h0; - uint32_t b = h1; - uint32_t c = h2; - uint32_t d = h3; - uint32_t e = h4; - - uint32_t f; - uint32_t k; - // The 80 iterations - for(int i = 0; i < 80; i++){ - if(i >= 0 && i <= 19){ - f = (b & c) ^ (( ~ b ) & d); - k = 1518500249; - }else if ( i >= 20 && i <= 39){ - f = b ^ c ^ d; - k = 1859775393; - }else if (i >= 40 && i <= 59){ - f = (b & c) ^ (b & d) ^ (c & d); - k = 2400959708; - }else if (i >= 60 && i <= 79){ - f = b ^ c ^ d; - k = 3395469782; - } - - uint32_t temp = leftRotate(a,5) + f + e + k + w[i]; - e = d; - d = c; - c = leftRotate(b, 30); - b = a; - a = temp; - } - - // Add this chunk's hash result - h0 += a; - h1 += b; - h2 += c; - h3 += d; - h4 += e; - /////////////////////// ****************************************** //////////////////////////////////////////// - break; + lastRound = 1; // This is the last round } - else{ - // * We got a 512 bit chunk * // - // printf("\nWe read 64 bytes"); - size_in_bytes += 64; - - /////////////////////// ****************************************** //////////////////////////////////////////// - uint32_t w[80]; - for(int i = 0; i < 16; i++) - w[i] = buffer[i] + buffer[i+1] * 256 + buffer[i+2] * 65536 + buffer[i+3] * 16777216; - for(int i = 16; i < 80; i++) - w[i] = leftRotate( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i - 16] , 1 ); - - uint32_t a = h0; - uint32_t b = h1; - uint32_t c = h2; - uint32_t d = h3; - uint32_t e = h4; - - uint32_t f; - uint32_t k; + + + /** + // Printing each byte of the chunk + printf("------------------------------------------\n"); + for(int i = 0; i < 64; i++) + printf("%x i=%d\n", buffer[i], i); + printf("------------------------------------------\n"); + **/ + + // * We usually get a 512 bit chunk except the last chunck which we need to pad (which we do in the if above) * // + size_in_bytes += 64; + + /////////////////////// ****************************************** //////////////////////////////////////////// + uint32_t w[80]; + for(int i = 0; i < 16; i++) + w[i] = buffer[4*i] * 16777216 + buffer[(4*i)+1] * 65536 + buffer[(4*i)+2] * 256 + buffer[(4*i)+3]; + + for(int i = 16; i < 80; i++) + w[i] = leftRotate32( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i - 16] , 1 ); + + + /** Printing each w for the chunck **/ + printf("------------------------------------------\n"); + for(int i = 0; i < 80; i++) + printf("%x i=%d\n", w[i], i); + printf("------------------------------------------\n"); + /** Printing each w **/ + + uint32_t a = h0; + uint32_t b = h1; + uint32_t c = h2; + uint32_t d = h3; + uint32_t e = h4; + + uint32_t f; + uint32_t k; - // The 80 iterations - for(int i = 0; i < 80; i++){ - if(i >= 0 && i <= 19){ - f = (b & c) ^ (( ~ b ) & d); - k = 1518500249; - }else if ( i >= 20 && i <= 39){ - f = b ^ c ^ d; - k = 1859775393; - }else if (i >= 40 && i <= 59){ - f = (b & c) ^ (b & d) ^ (c & d); - k = 2400959708; - }else if (i >= 60 && i <= 79){ - f = b ^ c ^ d; - k = 3395469782; - } - - uint32_t temp = leftRotate(a,5) + f + e + k + w[i]; - e = d; - d = c; - c = leftRotate(b, 30); - b = a; - a = temp; + // The 80 iterations + for(int i = 0; i < 80; i++){ + if(i >= 0 && i <= 19){ + f = d ^ ( b & ( c ^ d )); + k = 0x5A827999; + }else if ( i >= 20 && i <= 39){ + f = b ^ c ^ d; + k = 0x6ED9EBA1; + }else if (i >= 40 && i <= 59){ + f = (b & c) ^ (b & d) ^ (c & d); + k = 0x8F1BBCDC; + }else if (i >= 60 && i <= 79){ + f = b ^ c ^ d; + k = 0xCA62C1D6; } - // Add this chunk's hash result - h0 += a; - h1 += b; - h2 += c; - h3 += d; - h4 += e; - /////////////////////// ****************************************** //////////////////////////////////////////// + uint32_t temp = leftRotate32(a,5) + f + e + k + w[i]; + e = d; + d = c; + c = leftRotate32(b, 30); + b = a; + a = temp; } + + // Add this chunk's hash result + h0 += a; + h1 += b; + h2 += c; + h3 += d; + h4 += e; + + /////////////////////// ****************************************** //////////////////////////////////////////// } fclose(file); + + printf("0x%x 0x%x 0x%x 0x%x 0x%x\n", h0, h1, h2, h3, h4); - // printf("\nFinal result %x%x%x%x%x", h0, h1, h2, h3, h4); r.h0 = h0; r.h1 = h1; r.h2 = h2;