Have to debug

Currently the result is wrong for sha1
main
lomna-dev 1 year ago
parent fc7e9350cc
commit 419be2bb94

@ -1 +1 @@
Hello World abc

@ -1,15 +1,20 @@
#include "sha1.h" #include "sha1.h"
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
int main(){ int main(){
SHA1_Result r = sha1file("dummyFile"); SHA1_Result r = sha1file("dummyFile");
char buffer[41]; 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); sprintf(buffer, "%x%x%x%x%x", r.h0, r.h1, r.h2, r.h3, r.h4);
int i = 0; int i = 0;
while(buffer[i] != '\0') while(buffer[i] != '\0')
printf("%c", buffer[i++]); printf("%c", buffer[i++]);
printf("\n"); printf("\n");
// Expected : 1d135491d20198c382098d477009a910d7e9ca22
return 0; return 0;
} }

126
sha1.h

@ -1,10 +1,12 @@
#ifndef _SHA1_H #ifndef INCLUDED_SHA1_H
#define _SHA1_H #define INCLUDED_SHA1_H
#include <stdint.h> #include <stdint.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#define leftRotate32(N,D) (N<<D)|(N>>(32-D))
typedef struct SHA1_Result{ typedef struct SHA1_Result{
uint32_t h0; uint32_t h0;
uint32_t h1; uint32_t h1;
@ -13,13 +15,6 @@ typedef struct SHA1_Result{
uint32_t h4; uint32_t h4;
} SHA1_Result; } 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){ SHA1_Result sha1file(char *filename){
FILE* file; FILE* file;
@ -28,11 +23,11 @@ SHA1_Result sha1file(char *filename){
uint32_t size_in_bytes = 0; uint32_t size_in_bytes = 0;
uint64_t size_in_bits = 0; uint64_t size_in_bits = 0;
uint32_t h0 = 1742612993; uint32_t h0 = 0x67452301;
uint32_t h1 = 3137593996; uint32_t h1 = 0xEFCDAB89;
uint32_t h2 = 18805212; uint32_t h2 = 0x98BADCFE;
uint32_t h3 = 2459167202; uint32_t h3 = 0x10325476;
uint32_t h4 = 3455001513; uint32_t h4 = 0xC3D2E1F0;
file = fopen(filename,"r"); file = fopen(filename,"r");
if(errno != 0){ if(errno != 0){
@ -42,14 +37,17 @@ SHA1_Result sha1file(char *filename){
} }
size_t bytes_read; size_t bytes_read;
char buffer[64]; uint8_t buffer[64];
while(1){ int lastRound = 0;
while(lastRound != 1){
bytes_read = fread(buffer, 1, 64, file); bytes_read = fread(buffer, 1, 64, file);
number_of_chuncks++; number_of_chuncks++;
printf("We read %d bytes\n", bytes_read);
if(bytes_read != 64){ if(bytes_read != 64){
// * We need to convert to 512 bit * // // * We need to convert to 512 bit * //
// printf("\nWe read %d bytes", bytes_read);
size_in_bytes += bytes_read; // the final size is done here size_in_bytes += bytes_read; // the final size is done here
size_in_bits = size_in_bytes * 8; size_in_bits = size_in_bytes * 8;
@ -58,7 +56,7 @@ SHA1_Result sha1file(char *filename){
bytes_read += 1; bytes_read += 1;
for(int i = bytes_read; i <= 55; i++) for(int i = bytes_read; i <= 55; i++)
buffer[i] = 0x00; buffer[i] = 0;
//adding size to end //adding size to end
for(int i = 63; i >= 56; i--){ for(int i = 63; i >= 56; i--){
@ -66,67 +64,36 @@ SHA1_Result sha1file(char *filename){
size_in_bits /= 256; size_in_bits /= 256;
} }
/////////////////////// ****************************************** //////////////////////////////////////////// lastRound = 1; // This is the last round
//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; // Printing each byte of the chunk
h1 += b; printf("------------------------------------------\n");
h2 += c; for(int i = 0; i < 64; i++)
h3 += d; printf("%x i=%d\n", buffer[i], i);
h4 += e; printf("------------------------------------------\n");
/////////////////////// ****************************************** //////////////////////////////////////////// **/
break;
} // * We usually get a 512 bit chunk except the last chunck which we need to pad (which we do in the if above) * //
else{
// * We got a 512 bit chunk * //
// printf("\nWe read 64 bytes");
size_in_bytes += 64; size_in_bytes += 64;
/////////////////////// ****************************************** //////////////////////////////////////////// /////////////////////// ****************************************** ////////////////////////////////////////////
uint32_t w[80]; uint32_t w[80];
for(int i = 0; i < 16; i++) for(int i = 0; i < 16; i++)
w[i] = buffer[i] + buffer[i+1] * 256 + buffer[i+2] * 65536 + buffer[i+3] * 16777216; 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++) for(int i = 16; i < 80; i++)
w[i] = leftRotate( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i - 16] , 1 ); 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 a = h0;
uint32_t b = h1; uint32_t b = h1;
@ -140,23 +107,23 @@ SHA1_Result sha1file(char *filename){
// The 80 iterations // The 80 iterations
for(int i = 0; i < 80; i++){ for(int i = 0; i < 80; i++){
if(i >= 0 && i <= 19){ if(i >= 0 && i <= 19){
f = (b & c) ^ (( ~ b ) & d); f = d ^ ( b & ( c ^ d ));
k = 1518500249; k = 0x5A827999;
}else if ( i >= 20 && i <= 39){ }else if ( i >= 20 && i <= 39){
f = b ^ c ^ d; f = b ^ c ^ d;
k = 1859775393; k = 0x6ED9EBA1;
}else if (i >= 40 && i <= 59){ }else if (i >= 40 && i <= 59){
f = (b & c) ^ (b & d) ^ (c & d); f = (b & c) ^ (b & d) ^ (c & d);
k = 2400959708; k = 0x8F1BBCDC;
}else if (i >= 60 && i <= 79){ }else if (i >= 60 && i <= 79){
f = b ^ c ^ d; f = b ^ c ^ d;
k = 3395469782; k = 0xCA62C1D6;
} }
uint32_t temp = leftRotate(a,5) + f + e + k + w[i]; uint32_t temp = leftRotate32(a,5) + f + e + k + w[i];
e = d; e = d;
d = c; d = c;
c = leftRotate(b, 30); c = leftRotate32(b, 30);
b = a; b = a;
a = temp; a = temp;
} }
@ -167,13 +134,14 @@ SHA1_Result sha1file(char *filename){
h2 += c; h2 += c;
h3 += d; h3 += d;
h4 += e; h4 += e;
/////////////////////// ****************************************** //////////////////////////////////////////// /////////////////////// ****************************************** ////////////////////////////////////////////
} }
}
fclose(file); fclose(file);
// printf("\nFinal result %x%x%x%x%x", h0, h1, h2, h3, h4); printf("0x%x 0x%x 0x%x 0x%x 0x%x\n", h0, h1, h2, h3, h4);
r.h0 = h0; r.h0 = h0;
r.h1 = h1; r.h1 = h1;
r.h2 = h2; r.h2 = h2;

Loading…
Cancel
Save