master
Stefan Krulj 14 years ago
parent 502924ac8a
commit 6ea60084a8
  1. 4
      Makefile
  2. 6
      debug.py
  3. 29
      hmac.c
  4. 2
      hmac.h
  5. 38
      sha1.c
  6. 47
      test.c
  7. 6
      trunc.c
  8. 2
      trunc.h

@ -121,6 +121,6 @@ clean:
rm -f *.hex *.elf *.map *.list *.o *~ *.lst rm -f *.hex *.elf *.map *.list *.o *~ *.lst
rm -f usbdrv/*.o usbdrv/*.map usbdrv/*.list usbdrv/*~ usbdrv/*.lst rm -f usbdrv/*.o usbdrv/*.map usbdrv/*.list usbdrv/*~ usbdrv/*.lst
test: test.c sha1.c test: test.c sha1.c hmac.c
gcc test.c sha1.c -o test gcc test.c sha1.c hmac.c -o test

@ -26,7 +26,10 @@ class SHA1 (object):
w.append(lrot(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1) w.append(lrot(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1)
& 0xffffffff) & 0xffffffff)
#print(w) for i in w:
tmp=hex(i)[2:]
print("0"*(8-len(tmp)),end="")
print(tmp,end=" ")
a = self._h0 a = self._h0
b = self._h1 b = self._h1
@ -82,5 +85,6 @@ print("inner:",SHA1( ipad+text ).hexdigest() )
print("result:",outer) print("result:",outer)
lrot = lambda x, n: (x << n) | (x >> (32 - n)) lrot = lambda x, n: (x << n) | (x >> (32 - n))

@ -1,11 +1,9 @@
#include "hmac.h" #include "hmac.h"
#define KEYLEN 20
static const uint8_t secret[KEYLEN]={0x11};
void hmac(uint8_t* msg, uint8_t msglen){ void hmac(uint8_t* msg, uint8_t msglen, uint8_t* secret, uint8_t sec_len){
uint8_t i; int8_t i;
/* /*
ipad = the byte 0x36 repeated B times ipad = the byte 0x36 repeated B times
opad = the byte 0x5C repeated B times. opad = the byte 0x5C repeated B times.
@ -20,47 +18,36 @@ void hmac(uint8_t* msg, uint8_t msglen){
/* inner Hash */ /* inner Hash */
sha1_init(); sha1_init();
/*k xor ipad*/ /*k xor ipad*/
for(i=0;i<KEYLEN;i++){ for(i=0;i<sec_len;++i){
sha1_addbyte(secret[i]^IPAD); sha1_addbyte(secret[i]^IPAD);
} }
/*if key is shorter than blocksize, treat the rest as 0es*/ /*if key is shorter than blocksize, treat the rest as 0es*/
for(i=KEYLEN;i<SHA1_B;i++){ for(i=sec_len;i<SHA1_B;++i){
sha1_addbyte(IPAD); sha1_addbyte(IPAD);
} }
/* text */ /* text */
for(i=0;i<msglen;i++){ for(i=0;i<msglen;++i){
sha1_addbyte(msg[i]); sha1_addbyte(msg[i]);
} }
sha1_fill(SHA1_B+msglen); sha1_fill(SHA1_B+msglen);
/* save the result */ /* save the result */
memcpy(hmac_result,sha1_digest,SHA1_SIZE); memcpy(hmac_result,sha1_digest,SHA1_SIZE);
#ifdef TO_LITTLE_ENDIAN32
for(i=0; i<SHA1_SIZE/4; i++){
((uint32_t*)hmac_result)[i]=TO_LITTLE_ENDIAN32(((uint32_t*)hmac_result)[i]);
}
#endif //TO_LITTLE_ENDIAN32
/* outer hash */ /* outer hash */
sha1_init(); sha1_init();
/*k xor opad*/ /*k xor opad*/
for(i=0;i<KEYLEN;i++){ for(i=0;i<sec_len;++i){
sha1_addbyte(secret[i]^OPAD); sha1_addbyte(secret[i]^OPAD);
} }
/*if key is shorter than blocksize, treat the rest as 0es*/ /*if key is shorter than blocksize, treat the rest as 0es*/
for(i=KEYLEN;i<SHA1_B;i++){ for(i=sec_len;i<SHA1_B;++i){
sha1_addbyte(OPAD); sha1_addbyte(OPAD);
} }
for(i=0;i<SHA1_SIZE;i++){ for(i=SHA1_SIZE-1;i>=0;--i){
sha1_addbyte(hmac_result[i]); sha1_addbyte(hmac_result[i]);
} }
sha1_fill(SHA1_B+SHA1_SIZE); sha1_fill(SHA1_B+SHA1_SIZE);
/* save the result */ /* save the result */
memcpy(hmac_result,sha1_digest,SHA1_SIZE); memcpy(hmac_result,sha1_digest,SHA1_SIZE);
#ifdef TO_LITTLE_ENDIAN32
for(i=0; i<SHA1_SIZE/4; i++){
((uint32_t*)hmac_result)[i]=TO_LITTLE_ENDIAN32(((uint32_t*)hmac_result)[i]);
}
#endif //TO_LITTLE_ENDIAN32
} }

@ -14,6 +14,6 @@
uint8_t hmac_result[SHA1_SIZE]; uint8_t hmac_result[SHA1_SIZE];
void hmac(uint8_t* msg,uint8_t msglen); void hmac(uint8_t* msg, uint8_t msglen, uint8_t* secret, uint8_t sec_len);
#endif //_HMAC_H_ #endif //_HMAC_H_

@ -1,4 +1,5 @@
#include "sha1.h" #include "sha1.h"
#define H1 0x67452301 #define H1 0x67452301
#define H2 0xEFCDAB89 #define H2 0xEFCDAB89
#define H3 0x98BADCFE #define H3 0x98BADCFE
@ -26,24 +27,15 @@ void sha1_addbyte(uint8_t c){
if(chunk_cnt==64){ if(chunk_cnt==64){
/*chunk extension*/ /*chunk extension*/
uint32_t a,b,c,d,e,chunk_e[64]; uint32_t a,b,c,d,e,chunk_e[64];
#ifdef TO_LITTLE_ENDIAN32asdas
for(i=0; i<16; i++){
chunk[i]=TO_LITTLE_ENDIAN32(chunk[i]);
//printf("%08x",chunk[i]);
}
//printf("\n");
#endif //TO_LITTLE_ENDIAN32
for(i=0; i<64; i++){ for(i=0; i<64; i++){
chunk_e[i]= _LROT32( _W(i+16-3) ^ _W(i+16-8) ^ _W(i+16-14) ^ _W(i+16-16), 1 ); chunk_e[i]= _LROT32( _W(i+16-3) ^ _W(i+16-8) ^ _W(i+16-14) ^ _W(i+16-16), 1 );
} }
a=sha1_digest[0]; a=sha1_digest[4];
b=sha1_digest[1]; b=sha1_digest[3];
c=sha1_digest[2]; c=sha1_digest[2];
d=sha1_digest[3]; d=sha1_digest[1];
e=sha1_digest[4]; e=sha1_digest[0];
/*main loop*/ /*main loop*/
for(i=0;i<80;i++){ for(i=0;i<80;i++){
@ -70,11 +62,11 @@ void sha1_addbyte(uint8_t c){
a = temp; a = temp;
} }
sha1_digest[0]+=a; sha1_digest[4]+=a;
sha1_digest[1]+=b; sha1_digest[3]+=b;
sha1_digest[2]+=c; sha1_digest[2]+=c;
sha1_digest[3]+=d; sha1_digest[1]+=d;
sha1_digest[4]+=e; sha1_digest[0]+=e;
chunk_cnt=0; chunk_cnt=0;
} }
@ -107,7 +99,7 @@ void sha1_fill(int32_t len){
} }
} }
void sha1(char* msg, int32_t len){ /*void sha1(char* msg, int32_t len){
int32_t i; int32_t i;
uint8_t j; uint8_t j;
sha1_init(); sha1_init();
@ -124,14 +116,14 @@ void sha1(char* msg, int32_t len){
sha1_addbyte(msg[i+j]); sha1_addbyte(msg[i+j]);
} }
} }
} }*/
void sha1_init(void){ void sha1_init(void){
chunk_cnt=0; chunk_cnt=0;
sha1_digest[0]=H1; sha1_digest[4]=H1;
sha1_digest[1]=H2; sha1_digest[3]=H2;
sha1_digest[2]=H3; sha1_digest[2]=H3;
sha1_digest[3]=H4; sha1_digest[1]=H4;
sha1_digest[4]=H5; sha1_digest[0]=H5;
} }

@ -1,22 +1,57 @@
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
#include <string.h>
#include "sha1.h" #include "sha1.h"
char sha1sum[41]; 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(){ int main(){
/*SHA1STR makro needs those two*/
int i; int i;
char* p; char* p;
sha1_init(); sha1_init();
sha1_fill(0); sha1_fill(0);
p=sha1sum; SHA1STR();
for(i=0; i<20; ++i){ printf("SHA1: %s\n",sha1sum);
sprintf(p,"%02x", ((char*)(sha1_digest))[i] ); assert(strcmp("da39a3ee5e6b4b0d3255bfef95601890afd80709",sha1sum)==0);
p+=2;
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<strlen(SOMEDATA);i++){
sha1_addbyte(SOMEDATA[i]);
} }
sprintf(sha1sum,"%x%x%x%x%x",sha1_digest[0],sha1_digest[1],sha1_digest[2],sha1_digest[3],sha1_digest[4]); sha1_fill( strlen(SOMEDATA) );
SHA1STR();
printf("SHA1: %s\n",sha1sum); printf("SHA1: %s\n",sha1sum);
// assert(strncmp("da39a3ee5e6b4b0d3255bfef95601890afd80709",sha1sum)==0); assert(strcmp("4d71043a68fa28b9592d1aa5748e5cd4291c0512",sha1sum)==0);
char sec[20]=SECRET1;
hmac(MSG1,8,sec,20);
SHA1STR();
printf("HMAC: %s\n",sha1sum);
assert(strcmp("b617318655057264e28bc0b6fb378c8ef146be00",sha1sum)==0);
} }

@ -22,8 +22,8 @@ int32_t get_otp(uint8_t* hmac, uint8_t digits){
} }
} }
int32_t get_otp_from_cnt(uint64_t cnt, uint8_t digits){ int32_t get_otp_from_cnt(uint32_t cnt, uint8_t digits, uint8_t* secret, uint8_t sec_len){
cnt=TO_LITTLE_ENDIAN64(cnt); cnt=TO_LITTLE_ENDIAN32(cnt);
hmac((uint8_t*)(&cnt),8); hmac((uint8_t*)(&cnt),8,secret,sec_len);
return get_otp(hmac_result,digits); return get_otp(hmac_result,digits);
} }

@ -13,6 +13,6 @@
(x & 0x00000000000000FFul)<<(7*8) (x & 0x00000000000000FFul)<<(7*8)
int32_t get_otp(uint8_t* hmac, uint8_t digits); int32_t get_otp(uint8_t* hmac, uint8_t digits);
int32_t get_otp_from_cnt(uint64_t cnt, uint8_t digits); int32_t get_otp_from_cnt(uint32_t cnt, uint8_t digits, uint8_t* secret, uint8_t sec_len);
#endif #endif

Loading…
Cancel
Save