|
|
|
@ -1,6 +1,13 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include "trunc.h" |
|
|
|
#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){ |
|
|
|
int32_t get_otp(uint8_t* hmac, uint8_t digits){ |
|
|
|
uint8_t n; |
|
|
|
uint8_t n; |
|
|
|
uint32_t dt; |
|
|
|
uint32_t dt; |
|
|
|
@ -8,12 +15,13 @@ int32_t get_otp(uint8_t* hmac, uint8_t digits){ |
|
|
|
/*Step 1 generate HMAC-SHA1*/ |
|
|
|
/*Step 1 generate HMAC-SHA1*/ |
|
|
|
/*Step 2 get DT(HMAC) */ |
|
|
|
/*Step 2 get DT(HMAC) */ |
|
|
|
/* get the lower 4-bits and treat them as a number n*/ |
|
|
|
/* 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]*/ |
|
|
|
/* DT is the 31bit integer at HMAC[n]+HMAC[n+1]+HMAC[n+2]+HMAC[n+3]*/ |
|
|
|
n=hmac[SHA1_SIZE-1] & 0x0F; |
|
|
|
/* the MSB is ignored */ |
|
|
|
dt= (uint32_t)(hmac[n]&0x7F)<<(uint32_t)24 | |
|
|
|
n=hmac[0] & 0x0F; |
|
|
|
(uint32_t)(hmac[n+1])<<(uint32_t)16 |
|
|
|
|
dt= (uint32_t)(hmac[SHA1_SIZE-1-n]&0x7F)<<(uint32_t)24 | |
|
|
|
(uint32_t)(hmac[n+2])<<(uint32_t)8 |
|
|
|
|
(uint32_t)(hmac[SHA1_SIZE-2-n])<<(uint32_t)16 |
|
|
|
|
(uint32_t)(hmac[n+3]); |
|
|
|
(uint32_t)(hmac[SHA1_SIZE-3-n])<<(uint32_t)8 |
|
|
|
|
|
|
|
|
(uint32_t)(hmac[SHA1_SIZE-4-n]); |
|
|
|
/*Step 3 return dt mod 10^digits*/ |
|
|
|
/*Step 3 return dt mod 10^digits*/ |
|
|
|
switch(digits){ |
|
|
|
switch(digits){ |
|
|
|
case 6: return dt % 1000000; |
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|