|
| 1 | +/* Copyright (c) Dirk-Willem van Gulik, All rights reserved. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +#ifndef _TOTP_RFC6238_H |
| 19 | +#define _TOTP_RFC6238_H |
| 20 | + |
| 21 | +// Needed for the SHA1 |
| 22 | +// |
| 23 | +#include <mbedtls/md.h> |
| 24 | + |
| 25 | +// Needed for base32 decode - origin |
| 26 | +// https://github.com/dirkx/Arduino-Base32-Decode/releases |
| 27 | +// |
| 28 | +#include <Base32-Decode.h> |
| 29 | + |
| 30 | +class TOTP { |
| 31 | + public: |
| 32 | + |
| 33 | + // Defaults from RFC 6238 |
| 34 | + // Seed assumed in base64 format; and to be a multiple of 8 bits. |
| 35 | + // once decoded. |
| 36 | + static const time_t RFC6238_DEFAULT_interval = 30; // seconds (default) |
| 37 | + static const time_t RFC6238_DEFAULT_epoch = 0; // epoch relative to the unix epoch (jan 1970 is the default) |
| 38 | + static const int RFC6238_DEFAULT_digits = 6; // length (default is 6) |
| 39 | + |
| 40 | + static String * currentOTP(String seed, |
| 41 | + time_t interval = RFC6238_DEFAULT_interval, |
| 42 | + int digits = RFC6238_DEFAULT_digits, |
| 43 | + time_t epoch = RFC6238_DEFAULT_epoch |
| 44 | + ) |
| 45 | + { |
| 46 | + return currentOTP(seed, time(NULL), interval, digits, epoch); |
| 47 | + } |
| 48 | + |
| 49 | + static String * currentOTP(String seed, |
| 50 | + time_t t, |
| 51 | + time_t interval = RFC6238_DEFAULT_interval, |
| 52 | + int digits = RFC6238_DEFAULT_digits, |
| 53 | + time_t epoch = RFC6238_DEFAULT_epoch |
| 54 | + ) |
| 55 | + { |
| 56 | + uint64_t v = t; |
| 57 | + v = (v - epoch) / interval; |
| 58 | + |
| 59 | + // HMAC is calculated in big-endian (network) order. |
| 60 | + // v = htonll(v); |
| 61 | + |
| 62 | + // Unfortunately htonll is not exposed |
| 63 | + uint32_t endianness = 0xdeadbeef; |
| 64 | + if ((*(const uint8_t *)&endianness) == 0xef) { |
| 65 | + v = ((v & 0x00000000ffffffff) << 32) | ((v & 0xffffffff00000000) >> 32); |
| 66 | + v = ((v & 0x0000ffff0000ffff) << 16) | ((v & 0xffff0000ffff0000) >> 16); |
| 67 | + v = ((v & 0x00ff00ff00ff00ff) << 8) | ((v & 0xff00ff00ff00ff00) >> 8); |
| 68 | + }; |
| 69 | + |
| 70 | + unsigned char buff[ seed.length() ]; |
| 71 | + bzero(buff, sizeof(buff)); |
| 72 | + int n = base32decode(seed.c_str(), buff, sizeof(buff)); |
| 73 | + if (n < 0) { |
| 74 | + Serial.println("Could not decode base32 seed"); |
| 75 | + return NULL; |
| 76 | + } |
| 77 | + |
| 78 | +#ifdef RFC6238_DEBUG |
| 79 | + Serial.print("Key: "); |
| 80 | + Serial.print(seed); |
| 81 | + Serial.print(" --> "); |
| 82 | + for (int i = 0; i < n; i++) { |
| 83 | + Serial.printf("%02x", buff[i]); |
| 84 | + } |
| 85 | + Serial.printf(" -- bits=%d -- check this against https://cryptotools.net/otp\n",n * 8); |
| 86 | +#endif |
| 87 | + |
| 88 | + unsigned char digest[20]; |
| 89 | + if (mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), |
| 90 | + buff, n, // key |
| 91 | + (unsigned char*) &v, sizeof(v), // input |
| 92 | + digest)) return NULL; |
| 93 | + |
| 94 | + uint8_t offst = digest[19] & 0x0f; |
| 95 | + uint32_t bin_code = (digest[offst + 0] & 0x7f) << 24 |
| 96 | + | (digest[offst + 1] & 0xff) << 16 |
| 97 | + | (digest[offst + 2] & 0xff) << 8 |
| 98 | + | (digest[offst + 3] & 0xff); |
| 99 | + int power = pow(10, digits); |
| 100 | + |
| 101 | +#if RFC6238_DEBUG |
| 102 | + // To check against https://cryptotools.net/otp |
| 103 | + // |
| 104 | + for (int i = 0; i < 20; i++) { |
| 105 | + if (offst == i) Serial.print("|"); |
| 106 | + Serial.printf("%02x", digest[i]); |
| 107 | + if (offst == i) Serial.print("|"); |
| 108 | + } |
| 109 | + Serial.println(); |
| 110 | +#endif |
| 111 | + |
| 112 | + // prefix with zero's - as needed & cut off to right number of digits. |
| 113 | + // |
| 114 | + char outbuff[32]; |
| 115 | + snprintf(outbuff, sizeof(outbuff), "%06u", bin_code % power); |
| 116 | + String * otp = new String(outbuff); |
| 117 | + |
| 118 | + return (otp); |
| 119 | + } |
| 120 | +}; |
| 121 | +#endif |
0 commit comments