|
|
@ -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,122 +56,92 @@ 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--){
|
|
|
|
buffer[i] = size_in_bits % 256;
|
|
|
|
buffer[i] = size_in_bits % 256;
|
|
|
|
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
|
|
|
|
lastRound = 1; // This is the last round
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
|
|
|
|
// * We got a 512 bit chunk * //
|
|
|
|
|
|
|
|
// printf("\nWe read 64 bytes");
|
|
|
|
/**
|
|
|
|
size_in_bytes += 64;
|
|
|
|
// Printing each byte of the chunk
|
|
|
|
|
|
|
|
printf("------------------------------------------\n");
|
|
|
|
/////////////////////// ****************************************** ////////////////////////////////////////////
|
|
|
|
for(int i = 0; i < 64; i++)
|
|
|
|
uint32_t w[80];
|
|
|
|
printf("%x i=%d\n", buffer[i], i);
|
|
|
|
for(int i = 0; i < 16; i++)
|
|
|
|
printf("------------------------------------------\n");
|
|
|
|
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 );
|
|
|
|
// * 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 a = h0;
|
|
|
|
|
|
|
|
uint32_t b = h1;
|
|
|
|
/////////////////////// ****************************************** ////////////////////////////////////////////
|
|
|
|
uint32_t c = h2;
|
|
|
|
uint32_t w[80];
|
|
|
|
uint32_t d = h3;
|
|
|
|
for(int i = 0; i < 16; i++)
|
|
|
|
uint32_t e = h4;
|
|
|
|
w[i] = buffer[4*i] * 16777216 + buffer[(4*i)+1] * 65536 + buffer[(4*i)+2] * 256 + buffer[(4*i)+3];
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t f;
|
|
|
|
for(int i = 16; i < 80; i++)
|
|
|
|
uint32_t k;
|
|
|
|
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
|
|
|
|
// 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];
|
|
|
|
|
|
|
|
e = d;
|
|
|
|
|
|
|
|
d = c;
|
|
|
|
|
|
|
|
c = leftRotate(b, 30);
|
|
|
|
|
|
|
|
b = a;
|
|
|
|
|
|
|
|
a = temp;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add this chunk's hash result
|
|
|
|
uint32_t temp = leftRotate32(a,5) + f + e + k + w[i];
|
|
|
|
h0 += a;
|
|
|
|
e = d;
|
|
|
|
h1 += b;
|
|
|
|
d = c;
|
|
|
|
h2 += c;
|
|
|
|
c = leftRotate32(b, 30);
|
|
|
|
h3 += d;
|
|
|
|
b = a;
|
|
|
|
h4 += e;
|
|
|
|
a = temp;
|
|
|
|
/////////////////////// ****************************************** ////////////////////////////////////////////
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add this chunk's hash result
|
|
|
|
|
|
|
|
h0 += a;
|
|
|
|
|
|
|
|
h1 += b;
|
|
|
|
|
|
|
|
h2 += c;
|
|
|
|
|
|
|
|
h3 += d;
|
|
|
|
|
|
|
|
h4 += e;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////// ****************************************** ////////////////////////////////////////////
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
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.h0 = h0;
|
|
|
|
r.h1 = h1;
|
|
|
|
r.h1 = h1;
|
|
|
|
r.h2 = h2;
|
|
|
|
r.h2 = h2;
|
|
|
|