master
Stefan Krulj 14 years ago
parent 26437de3e3
commit decce4d4d8
  1. 12
      sha1.c
  2. 14
      test.c

@ -1,3 +1,10 @@
/**
* SHA1 - Secure Hash Algorithm
* This code is a direct implementation of the algorithm presented in rfc3174.
* No optimizations has been used, although I tried to keep it small in order
* to fit on an ATTiny45 (together with VUSB).
* It is written in assumption that the target architecture is little endian.
*/
#include "sha1.h"
#define H1 0x67452301
@ -22,6 +29,7 @@ void sha1_addbyte(uint8_t c){
uint8_t* p=(uint8_t*)chunk;
uint8_t i=(chunk_cnt & 0x03);
/* chunk_cnt-i)+3-i deals with the endianess */
p[(chunk_cnt-i)+3-i]=c;
chunk_cnt++;
if(chunk_cnt==64){
@ -76,9 +84,9 @@ void sha1_fill(int32_t len){
/*"append 0-bits to the message to obtain a length of len%512=448"*/
/*"append length of message in bits as 64-bit integer"*/
uint8_t j;
uint8_t end=(uint8_t)((120-(len-(len/64)*64+1))%64+1);
uint8_t end=(uint8_t)((120-(len-(len/64)*64+1))%64);
sha1_addbyte(0x80);
for(j=1;j<end;j++){
for(j=0;j<end;j++){
sha1_addbyte(0x00);
}
{

@ -8,7 +8,7 @@
char sha1sum[41];
#define SHA1STR() { \
#define SHA1STR() { \
p=sha1sum; \
for(i=19; i>=0; --i){ \
sprintf(p,"%02x", ((unsigned char*)(sha1_digest))[i] ); \
@ -20,6 +20,9 @@ char sha1sum[41];
#define SECRET1 {0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}
#define MSG1 "Hi There"
#define SECRET2 {'J', 'e', 'f', 'e'};
#define MSG2 ""
int main(){
/*SHA1STR makro needs those two*/
int i;
@ -49,10 +52,17 @@ int main(){
printf("SHA1: %s\n",sha1sum);
assert(strcmp("4d71043a68fa28b9592d1aa5748e5cd4291c0512",sha1sum)==0);
char sec[20]=SECRET1;
char sec1[20]=SECRET1;
hmac(MSG1,8,sec,20);
SHA1STR();
printf("HMAC: %s\n",sha1sum);
assert(strcmp("b617318655057264e28bc0b6fb378c8ef146be00",sha1sum)==0);
char sec1[20]=SECRET2;
hmac(MSG2,8,sec,20);
SHA1STR();
printf("HMAC: %s\n",sha1sum);
//assert(strcmp("b617318655057264e28bc0b6fb378c8ef146be00",sha1sum)==0);
}

Loading…
Cancel
Save