#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(uint64_t cnt, uint8_t digits){ cnt=TO_LITTLE_ENDIAN64(cnt); hmac((uint8_t*)(&cnt),8); return get_otp(hmac_result,digits); }