You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
998 B
29 lines
998 B
|
|
#include "trunc.h"
|
|
|
|
int32_t get_otp(uint8_t* hmac, uint8_t digits){
|
|
uint8_t n;
|
|
uint32_t dt;
|
|
|
|
/*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]);
|
|
/*Step 3 return dt mod 10^digits*/
|
|
switch(digits){
|
|
case 6: return dt % 1000000;
|
|
case 7: return dt % 10000000;
|
|
default: case 8: return dt % 100000000;
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|