X-Git-Url: https://git.cryptolib.org/?p=labortage2013badge.git;a=blobdiff_plain;f=firmware%2Fhotp.c;fp=firmware%2Fhotp.c;h=f0e7c3c155ea32ad1a5a2543d5010207626608fb;hp=0000000000000000000000000000000000000000;hb=10cef9c333ab2ece1ae499ace212c3df212a4bfc;hpb=2b9eb2b94bce002cac2301745f93dc81407d9d34
diff --git a/firmware/hotp.c b/firmware/hotp.c
new file mode 100644
index 0000000..f0e7c3c
--- /dev/null
+++ b/firmware/hotp.c
@@ -0,0 +1,75 @@
+/* htop.c */
+/*
+ This file is part of the AVR-Crypto-Lib.
+ Copyright (C) 2006-2013 Daniel Otte (daniel.otte@rub.de)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+*/
+
+
+#include
+#include
+#include
+
+static
+uint32_t dtrunc(uint8_t* buffer) {
+ uint8_t idx;
+ union {
+ uint8_t w8[4];
+ uint32_t w32;
+ } r;
+
+ idx = buffer[19] & 0x0f;
+ r.w8[3] = buffer[idx++] & 0x7f;
+ r.w8[2] = buffer[idx++];
+ r.w8[1] = buffer[idx++];
+ r.w8[0] = buffer[idx];
+ return r.w32;
+}
+
+static
+void to_digits(char *buffer, uint32_t value, uint8_t digits) {
+ ldiv_t t;
+ if (value == 0) {
+ *buffer++ = '0';
+ }
+ while (value && digits--) {
+ t = ldiv(value, 10);
+ value = t.quot;
+ *buffer++ = t.rem + '0';
+ }
+ *buffer = '\0';
+}
+
+void hotp(char *buffer, void* secret, uint16_t secret_length_b, uint32_t counter, uint8_t digits) {
+ union {
+ uint8_t mac[20];
+ uint8_t ctr_buffer[8];
+ } d;
+ uint32_t s;
+ d.ctr_buffer[7] = 0;
+ d.ctr_buffer[6] = 0;
+ d.ctr_buffer[5] = 0;
+ d.ctr_buffer[4] = 0;
+ d.ctr_buffer[3] = counter & 0xff;
+ counter >>= 8;
+ d.ctr_buffer[2] = counter & 0xff;
+ counter >>= 8;
+ d.ctr_buffer[1] = counter & 0xff;
+ counter >>= 8;
+ d.ctr_buffer[0] = counter & 0xff;
+ hmac_sha1(d.mac, secret, secret_length_b, d.ctr_buffer, 64);
+ s = dtrunc(d.mac);
+ to_digits(buffer, s, digits);
+}