RAM usage is to high, will try lib crypto

master
Stefan Krulj 14 years ago
parent b11f2152bd
commit c7cf8448b8
  1. 36
      Makefile
  2. 50
      main.c
  3. 20
      sha1.c
  4. 2
      test.c
  5. 26
      trunc.c
  6. 2
      trunc.h

@ -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

@ -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<<RXCIE)|*/(1<<RXEN0)|(1<<TXEN0);
/*odd parity, 1 stop bit, 8bit words*/
UCSR0C |= (1<<UPM01)|(1<<UPM00)|(1<<UCSZ01)|(1<<UCSZ00);
}
void tx( char data ){
while ( !( UCSR0A & (1<<UDRE0)) );
UDR0 = data;
@ -39,15 +51,51 @@ void sendunum(uint32_t num){
sendstr( text+leadingzero );
}
void tx_hex(uint8_t c){
if( ((c&0xF0)>>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<<RXC0)) );
return UDR0;
}
#endif //DEBUG
#define SECRET {'J', 'e', 'f', 'e'};
#define SECLEN 4
#define MSG "what do ya want for nothing?"
int main(void) __attribute__((noreturn));
int __attribute__((OS_main)) main(void) {
sendstr("Hello");
int8_t i;
uint8_t sec[SECLEN]=SECRET;
initUSART();
//sendstr("Hello\n");
tx('+');
hmac(MSG,28,sec,4);
//get_otp_from_cnt(777, 6, sec, SECLEN); //get a 6 digit one-time-password with counter 777
for(i=0; i<20; i++){
tx_hex( ((uint8_t*)sha1_digest)[i] );
}
tx('+');
tx('\n');
for(;;){
}

@ -1,8 +1,7 @@
/**
* 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).
* No optimizations has been used, although I tried to keep it small in flashsize
* It is written in assumption that the target architecture is little endian.
*/
#include "sha1.h"
@ -19,12 +18,10 @@
#define K4 0xCA62C1D6
#define _LROT32(x,n) (( (uint32_t)(x) << n ) | ( (uint32_t)(x) >>(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);

@ -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));
}

@ -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);
}

@ -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

Loading…
Cancel
Save