3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2006-2013 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <hmac-sha1.h>
27 uint32_t dtrunc(uint8_t* buffer) {
34 idx = buffer[19] & 0x0f;
35 r.w8[3] = buffer[idx++] & 0x7f;
36 r.w8[2] = buffer[idx++];
37 r.w8[1] = buffer[idx++];
38 r.w8[0] = buffer[idx];
43 void reverse_string(char *str) {
45 end = str + strlen(str) - 1;
56 void to_digits(char *buffer, uint32_t value, uint8_t digits) {
61 while (value && digits--) {
64 *buffer++ = t.rem + '0';
69 void hotp(char *buffer, const void* secret, uint16_t secret_length_b, uint32_t counter, uint8_t digits) {
72 uint8_t ctr_buffer[8];
75 d.ctr_buffer[7] = counter & 0xff;
77 d.ctr_buffer[6] = counter & 0xff;
79 d.ctr_buffer[5] = counter & 0xff;
81 d.ctr_buffer[4] = counter & 0xff;
89 hmac_sha1(d.mac, secret, secret_length_b, d.ctr_buffer, 64);
91 to_digits(buffer, s, digits);
92 reverse_string(buffer);