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/>.
23 #include <hmac-sha1.h>
26 uint32_t dtrunc(uint8_t* buffer) {
33 idx = buffer[19] & 0x0f;
34 r.w8[3] = buffer[idx++] & 0x7f;
35 r.w8[2] = buffer[idx++];
36 r.w8[1] = buffer[idx++];
37 r.w8[0] = buffer[idx];
42 void to_digits(char *buffer, uint32_t value, uint8_t digits) {
47 while (value && digits--) {
50 *buffer++ = t.rem + '0';
55 void hotp(char *buffer, void* secret, uint16_t secret_length_b, uint32_t counter, uint8_t digits) {
58 uint8_t ctr_buffer[8];
65 d.ctr_buffer[3] = counter & 0xff;
67 d.ctr_buffer[2] = counter & 0xff;
69 d.ctr_buffer[1] = counter & 0xff;
71 d.ctr_buffer[0] = counter & 0xff;
72 hmac_sha1(d.mac, secret, secret_length_b, d.ctr_buffer, 64);
74 to_digits(buffer, s, digits);