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

53 lines
1.4 KiB

#include "hmac.h"
void hmac(uint8_t* msg, uint8_t msglen, uint8_t* secret, uint8_t sec_len){
int8_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<sec_len;++i){
sha1_addbyte(secret[i]^IPAD);
}
/*if key is shorter than blocksize, treat the rest as 0es*/
for(i=sec_len;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);
/* outer hash */
sha1_init();
/*k xor opad*/
for(i=0;i<sec_len;++i){
sha1_addbyte(secret[i]^OPAD);
}
/*if key is shorter than blocksize, treat the rest as 0es*/
for(i=sec_len;i<SHA1_B;++i){
sha1_addbyte(OPAD);
}
for(i=SHA1_SIZE-1;i>=0;--i){
sha1_addbyte(hmac_result[i]);
}
sha1_fill(SHA1_B+SHA1_SIZE);
/* save the result */
memcpy(hmac_result,sha1_digest,SHA1_SIZE);
}