diff --git a/Makefile b/Makefile index 871732f..b681f06 100644 --- a/Makefile +++ b/Makefile @@ -121,6 +121,6 @@ clean: rm -f *.hex *.elf *.map *.list *.o *~ *.lst rm -f usbdrv/*.o usbdrv/*.map usbdrv/*.list usbdrv/*~ usbdrv/*.lst -test: test.c sha1.c - gcc test.c sha1.c -o test +test: test.c sha1.c hmac.c + gcc test.c sha1.c hmac.c -o test diff --git a/debug.py b/debug.py index 41d6570..d05e6a5 100755 --- a/debug.py +++ b/debug.py @@ -26,7 +26,10 @@ class SHA1 (object): w.append(lrot(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1) & 0xffffffff) - #print(w) + for i in w: + tmp=hex(i)[2:] + print("0"*(8-len(tmp)),end="") + print(tmp,end=" ") a = self._h0 b = self._h1 @@ -82,5 +85,6 @@ print("inner:",SHA1( ipad+text ).hexdigest() ) print("result:",outer) + lrot = lambda x, n: (x << n) | (x >> (32 - n)) diff --git a/hmac.c b/hmac.c index 392204c..ec4d0b0 100644 --- a/hmac.c +++ b/hmac.c @@ -1,11 +1,9 @@ #include "hmac.h" -#define KEYLEN 20 -static const uint8_t secret[KEYLEN]={0x11}; -void hmac(uint8_t* msg, uint8_t msglen){ - uint8_t i; +void hmac(uint8_t* msg, uint8_t msglen, uint8_t* secret, uint8_t sec_len){ + int8_t i; /* ipad = the byte 0x36 repeated B times opad = the byte 0x5C repeated B times. @@ -20,47 +18,36 @@ void hmac(uint8_t* msg, uint8_t msglen){ /* inner Hash */ sha1_init(); /*k xor ipad*/ - for(i=0;i=0;--i){ sha1_addbyte(hmac_result[i]); } sha1_fill(SHA1_B+SHA1_SIZE); /* save the result */ memcpy(hmac_result,sha1_digest,SHA1_SIZE); - #ifdef TO_LITTLE_ENDIAN32 - for(i=0; i #include +#include #include "sha1.h" char sha1sum[41]; +#define SHA1STR() { \ + p=sha1sum; \ + for(i=19; i>=0; --i){ \ + sprintf(p,"%02x", ((unsigned char*)(sha1_digest))[i] ); \ + p+=2; \ + }} + +#define SOMEDATA "fassadenpassformpassendedachkonstuktionen_lagerraumroboterverwaltungssoftwarejungelnesehtorptsurb_fahrradpumpenschlauch_mcguiver_mit_pommes" + +#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" + int main(){ + /*SHA1STR makro needs those two*/ int i; char* p; + sha1_init(); sha1_fill(0); - p=sha1sum; - for(i=0; i<20; ++i){ - sprintf(p,"%02x", ((char*)(sha1_digest))[i] ); - p+=2; + SHA1STR(); + printf("SHA1: %s\n",sha1sum); + assert(strcmp("da39a3ee5e6b4b0d3255bfef95601890afd80709",sha1sum)==0); + + sha1_init(); + sha1_addbyte('a'); + sha1_addbyte('b'); + sha1_addbyte('c'); + sha1_fill(3); + SHA1STR(); + printf("SHA1: %s\n",sha1sum); + assert(strcmp("a9993e364706816aba3e25717850c26c9cd0d89d",sha1sum)==0); + + sha1_init(); + for(i=0;i