VUSB-Firmware for Attiny85 implementing HMAC one time password authentication
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.
 
 
 
 
hmac-otp/hmac.c

66 lines
1.7 KiB

#include "hmac.h"
#define KEYLEN 20
static const uint8_t secret[KEYLEN]={0x11};
void hmac(uint8_t* msg, uint8_t msglen){
uint8_t i;
/*
ipad = the byte 0x36 repeated B times
opad = the byte 0x5C repeated B times.
To compute HMAC over the data `text' we perform
H(K XOR opad, H(K XOR ipad, text))
B is length of hash in bytes, K is the secret and "," ist the string concatination operator
*/
/* inner Hash */
sha1_init();
/*k xor ipad*/
for(i=0;i<KEYLEN;i++){
sha1_addbyte(secret[i]^IPAD);
}
/*if key is shorter than blocksize, treat the rest as 0es*/
for(i=KEYLEN;i<SHA1_B;i++){
sha1_addbyte(IPAD);
}
/* text */
for(i=0;i<msglen;i++){
sha1_addbyte(msg[i]);
}
sha1_fill(SHA1_B+msglen);
/* save the result */
memcpy(hmac_result,sha1_digest,SHA1_SIZE);
#ifdef TO_LITTLE_ENDIAN32
for(i=0; i<SHA1_SIZE/4; i++){
((uint32_t*)hmac_result)[i]=TO_LITTLE_ENDIAN32(((uint32_t*)hmac_result)[i]);
}
#endif //TO_LITTLE_ENDIAN32
/* outer hash */
sha1_init();
/*k xor opad*/
for(i=0;i<KEYLEN;i++){
sha1_addbyte(secret[i]^OPAD);
}
/*if key is shorter than blocksize, treat the rest as 0es*/
for(i=KEYLEN;i<SHA1_B;i++){
sha1_addbyte(OPAD);
}
for(i=0;i<SHA1_SIZE;i++){
sha1_addbyte(hmac_result[i]);
}
sha1_fill(SHA1_B+SHA1_SIZE);
/* save the result */
memcpy(hmac_result,sha1_digest,SHA1_SIZE);
#ifdef TO_LITTLE_ENDIAN32
for(i=0; i<SHA1_SIZE/4; i++){
((uint32_t*)hmac_result)[i]=TO_LITTLE_ENDIAN32(((uint32_t*)hmac_result)[i]);
}
#endif //TO_LITTLE_ENDIAN32
}