From c7cf8448b83e184fc279e8242aa9af85112743bc Mon Sep 17 00:00:00 2001 From: Stefan Krulj Date: Thu, 28 Jun 2012 18:52:40 +0200 Subject: [PATCH] RAM usage is to high, will try lib crypto --- Makefile | 36 ++++++++++++++++++------------------ main.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- sha1.c | 20 ++++++++++---------- test.c | 2 ++ trunc.c | 26 +++++++++++++++----------- trunc.h | 2 +- 6 files changed, 95 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index e8d0dd6..2e0010c 100644 --- a/Makefile +++ b/Makefile @@ -61,22 +61,22 @@ OPTLEVEL=s # Flags CFLAGS=-I. $(INC) -g -mmcu=$(MCU) -O$(OPTLEVEL) \ -DF_CPU=16500000 \ - -mcall-prologues \ - -mtiny-stack \ - -fno-jump-tables \ - -fno-inline-functions \ - -fno-inline-small-functions \ - -fno-split-wide-types \ - -fpack-struct \ - -fshort-enums \ - -funsigned-bitfields \ - -funsigned-char \ - -Wall -Wstrict-prototypes \ - -Wl,--gc-sections \ - -Wl,--print-gc-sections \ - -ffunction-sections -fdata-sections \ - -Wa,-ahlms=$(firstword \ - $(filter %.lst, $(<:.c=.lst))) +# -mcall-prologues \ +# -mtiny-stack \ +# -fno-jump-tables \ +# -fno-inline-functions \ +# -fno-inline-small-functions \ +# -fno-split-wide-types \ +# -fpack-struct \ +# -fshort-enums \ +# -funsigned-bitfields \ +# -funsigned-char \ +# -Wall -Wstrict-prototypes \ +# -Wl,--gc-sections \ +# -Wl,--print-gc-sections \ +# -ffunction-sections -fdata-sections \ +# -Wa,-ahlms=$(firstword \ +# $(filter %.lst, $(<:.c=.lst))) ASMFLAGS =-I. $(INC) -mmcu=$(MCU) \ -x assembler-with-cpp \ -DF_CPU=16500000 \ @@ -133,6 +133,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 hmac.c - gcc test.c sha1.c hmac.c -o test +test: test.c sha1.c hmac.c trunc.c + gcc test.c sha1.c hmac.c trunc.c -o test diff --git a/main.c b/main.c index 29cd6b0..9a01a22 100644 --- a/main.c +++ b/main.c @@ -4,6 +4,18 @@ #define DEBUG #ifdef DEBUG + +#define MYUBRR 8 /*BAUD=115200*/ + +void initUSART(){ + UBRR0 = (unsigned char) MYUBRR; + + /*enable USART write and read*/ + UCSR0B |= /*(1<>4) <0x0A ){ + tx('0'+((c&0xF0)>>4) ); + }else{ + tx('A'-0x0A+((c&0xF0)>>4)); + } + + if( (c&0x0F)<0x0A ){ + tx('0'+(c&0x0F)); + }else{ + tx('A'-0x0A+(c&0x0F)); + } +} + char rx( void ){ while ( !(UCSR0A & (1<>(32-n) )) -#define _W(n) (n>=16 ? _sha1_chunk_e[n-16] : _sha1_chunk[n]) - -uint32_t _sha1_chunk[16]; -static uint8_t chunk_cnt; -uint32_t _sha1_chunk_e[64]; /*putting this here saves a lot of code space*/ +/*putting this here saves a lot of code space*/ +static uint32_t _sha1_chunk[80] __attribute__ ((section (".noinit"))); +static uint8_t chunk_cnt __attribute__ ((section (".noinit"))); void sha1_addbyte(uint8_t c){ uint8_t* p=(uint8_t*)_sha1_chunk; @@ -36,8 +33,11 @@ void sha1_addbyte(uint8_t c){ if(chunk_cnt==64){ /*chunk extension*/ uint32_t a,b,c,d,e; - for(i=0; i<64; ++i){ - _sha1_chunk_e[i]= _LROT32( _W(i+16-3) ^ _W(i+16-8) ^ _W(i+16-14) ^ _W(i+16-16), 1 ); + for(i=16; i<80; ++i){ + _sha1_chunk[i]= _LROT32( _sha1_chunk[i- 3] ^ + _sha1_chunk[i- 8] ^ + _sha1_chunk[i-14] ^ + _sha1_chunk[i-16], 1 ); } a=sha1_digest[4]; b=sha1_digest[3]; @@ -62,7 +62,7 @@ void sha1_addbyte(uint8_t c){ k=K4; f=b ^ c ^ d; } - temp = e + k + f+_LROT32(a,5)+_W(i); + temp = e + k + f+_LROT32(a,5)+_sha1_chunk[i]; e = d; d = c; c = _LROT32(b,30); diff --git a/test.c b/test.c index 9606423..a1a7c32 100644 --- a/test.c +++ b/test.c @@ -64,5 +64,7 @@ int main(){ printf("HMAC: %s\n",sha1sum); assert(strcmp("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",sha1sum)==0); + printf("OTP: %d\n",get_otp_from_cnt(777, 6, sec2, 4)); + } diff --git a/trunc.c b/trunc.c index ad78b73..8a55804 100644 --- a/trunc.c +++ b/trunc.c @@ -1,6 +1,13 @@ #include "trunc.h" +int32_t get_otp_from_cnt(uint64_t cnt, uint8_t digits, uint8_t* secret, uint8_t sec_len){ + uint8_t tmp[8]={((uint8_t*)&cnt)[7],((uint8_t*)&cnt)[6],((uint8_t*)&cnt)[5],((uint8_t*)&cnt)[4],((uint8_t*)&cnt)[3],((uint8_t*)&cnt)[2],((uint8_t*)&cnt)[1],((uint8_t*)&cnt)[0]}; + /*Step 1 generate HMAC-SHA1*/ + hmac(tmp,8,secret,sec_len); + return get_otp(hmac_result,digits); +} + int32_t get_otp(uint8_t* hmac, uint8_t digits){ uint8_t n; uint32_t dt; @@ -8,12 +15,13 @@ int32_t get_otp(uint8_t* hmac, uint8_t digits){ /*Step 1 generate HMAC-SHA1*/ /*Step 2 get DT(HMAC) */ /* get the lower 4-bits and treat them as a number n*/ - /* DT is the 32bit integer at HMAC[n]+HMAC[n+1]+HMAC[n+2]+HMAC[n+3]*/ - n=hmac[SHA1_SIZE-1] & 0x0F; - dt= (uint32_t)(hmac[n]&0x7F)<<(uint32_t)24 | - (uint32_t)(hmac[n+1])<<(uint32_t)16 | - (uint32_t)(hmac[n+2])<<(uint32_t)8 | - (uint32_t)(hmac[n+3]); + /* DT is the 31bit integer at HMAC[n]+HMAC[n+1]+HMAC[n+2]+HMAC[n+3]*/ + /* the MSB is ignored */ + n=hmac[0] & 0x0F; + dt= (uint32_t)(hmac[SHA1_SIZE-1-n]&0x7F)<<(uint32_t)24 | + (uint32_t)(hmac[SHA1_SIZE-2-n])<<(uint32_t)16 | + (uint32_t)(hmac[SHA1_SIZE-3-n])<<(uint32_t)8 | + (uint32_t)(hmac[SHA1_SIZE-4-n]); /*Step 3 return dt mod 10^digits*/ switch(digits){ case 6: return dt % 1000000; @@ -22,8 +30,4 @@ int32_t get_otp(uint8_t* hmac, 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_ENDIAN32(cnt); - hmac((uint8_t*)(&cnt),8,secret,sec_len); - return get_otp(hmac_result,digits); -} + diff --git a/trunc.h b/trunc.h index 315e6aa..4dfab11 100644 --- a/trunc.h +++ b/trunc.h @@ -13,6 +13,6 @@ (x & 0x00000000000000FFul)<<(7*8) int32_t get_otp(uint8_t* hmac, uint8_t digits); -int32_t get_otp_from_cnt(uint32_t cnt, uint8_t digits, uint8_t* secret, uint8_t sec_len); +int32_t get_otp_from_cnt(uint64_t cnt, uint8_t digits, uint8_t* secret, uint8_t sec_len); #endif