totp

Simple cli tool for storing TOTP secrets and generating tokens
git clone https://git.inz.fi/totp/
Log | Files | Refs | Submodules

commit 493bc6f598be675bef1950351a64013e2d0d3834
parent 4c3b80f9966566ed49e36da09cfd2862e6ceac6c
Author: Santtu Lakkala <inz@inz.fi>
Date:   Fri,  2 Jul 2021 09:26:09 +0300

Add test.c, fixes and improvements

Diffstat:
Mmain.c | 69++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Atest.c | 430+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 488 insertions(+), 11 deletions(-)

diff --git a/main.c b/main.c @@ -8,6 +8,7 @@ #include <fcntl.h> #include <fnmatch.h> #include <inttypes.h> +#include <termios.h> #include <time.h> #include <unistd.h> @@ -399,6 +400,7 @@ void write_filter_key(uint8_t digest, } enum cmd { + CMD_NONE, CMD_TOK, CMD_LIST, CMD_ADD, @@ -452,12 +454,24 @@ static size_t uridecode(char *buf, size_t len, bool getarg) return w - buf; } +static void setecho(bool echo) +{ + struct termios tio; + if (tcgetattr(STDIN_FILENO, &tio)) + return; + if (echo) + tio.c_lflag |= ECHO; + else + tio.c_lflag &= ~ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, &tio); +} + int main(int argc, char *argv[]) { int fd; int r; struct sha1 d; - enum cmd cmd; + enum cmd cmd = CMD_NONE; char *totpuri; const char *key = NULL; const char *keyfile = NULL; @@ -505,21 +519,51 @@ int main(int argc, char *argv[]) } ARGEND + if (cmd == CMD_NONE && !argc) + usage(); + sha1_init(&d); if (key) { sha1_update(&d, key, strlen(key)); - } else if (keyfile) { - fd = open(keyfile, O_RDONLY); - if (fd < 0) - exit(1); - while ((r = read(fd, d.buffer + (d.len & 63), - sizeof(d.buffer) - (d.len & 63))) > 0) { - d.len += r; - if (!(d.len & 63)) - sha1_update(&d, d.buffer, sizeof(d.buffer)); + } else { + size_t l = 0; + if (keyfile && strcmp(keyfile, "-")) { + fd = open(keyfile, O_RDONLY); + } else { + fd = STDIN_FILENO; + + if (!keyfile) { + fprintf(stderr, "Enter passphrase: "); + setecho(false); + } + } + + while ((r = read(fd, d.buffer + l, + sizeof(d.buffer) - l)) > 0) { + size_t ll = strncspn((const char *)d.buffer + l, r, "\r\n"); + + if (ll < (size_t)r) { + l += ll; + break; + } + + l += r; + if (l < sizeof(d.buffer)) + continue; + sha1_update(&d, d.buffer, sizeof(d.buffer)); + l = 0; + } + + if (l) + sha1_update(&d, d.buffer, l); + + if (!keyfile) { + fprintf(stderr, "\n"); + setecho(true); + } else if (strcmp(keyfile, "-")) { + close(fd); } - close(fd); } sha1_finish(&d); @@ -643,6 +687,9 @@ int main(int argc, char *argv[]) break; } + case CMD_NONE: + keyquery = argv[0]; + /* fall-through */ case CMD_TOK: free(newsecretfile); fd = open(secretfile, O_RDONLY); diff --git a/test.c b/test.c @@ -0,0 +1,430 @@ +#include <stdio.h> +#include <string.h> +#include <stdint.h> +#include <inttypes.h> + +#include "util.h" +#include "sha1.h" +#include "sha256.h" +#include "sha512.h" + +void hexdump(FILE *f, const void *data, size_t len) +{ + const uint8_t *d; + + for (d = data; len--; d++) + fprintf(f, "%02x", *d); +} + +void test_sha1(void) +{ + const char *test_datas[] = { + "abc", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + }; + + const uint8_t test_hashes[][SHA1_HASHSIZE] = { + { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E, + 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D, }, + { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, + 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1, }, + }; + + struct sha1 s; + size_t i; + + for (i = 0; i < sizeof(test_datas) / sizeof(*test_datas); i++) { + sha1_init(&s); + sha1_update(&s, test_datas[i], strlen(test_datas[i])); + sha1_finish(&s); + + if (memcmp(s.h, test_hashes[i], sizeof(s.h))) { + fprintf(stderr, "%s: hash %zu mismatch, got:\n\t", + __FUNCTION__, i); + hexdump(stderr, s.h, sizeof(s.h)); + fprintf(stderr, "\n, expected:\n\t"); + hexdump(stderr, + test_hashes[i], sizeof(test_hashes[i])); + fprintf(stderr, "\n"); + } + } +} + +void test_hmac_sha1(void) +{ + const char *test_datas[] = { + "Sample message for keylen=blocklen", + "Sample message for keylen<blocklen", + "Sample message for keylen=blocklen", + "Sample message for keylen<blocklen, with truncated tag" + }; + + uint8_t keybuf[128]; + const size_t keylens[] = { + 64, 20, 100, 49 + }; + + const size_t taglens[] = { + 20, 20, 20, 12 + }; + + const uint8_t test_tags[][SHA1_HASHSIZE] = { + { 0x5F, 0xD5, 0x96, 0xEE, 0x78, 0xD5, 0x55, 0x3C, 0x8F, 0xF4, + 0xE7, 0x2D, 0x26, 0x6D, 0xFD, 0x19, 0x23, 0x66, 0xDA, 0x29, }, + { 0x4C, 0x99, 0xFF, 0x0C, 0xB1, 0xB3, 0x1B, 0xD3, 0x3F, 0x84, + 0x31, 0xDB, 0xAF, 0x4D, 0x17, 0xFC, 0xD3, 0x56, 0xA8, 0x07, }, + { 0x2D, 0x51, 0xB2, 0xF7, 0x75, 0x0E, 0x41, 0x05, 0x84, 0x66, + 0x2E, 0x38, 0xF1, 0x33, 0x43, 0x5F, 0x4C, 0x4F, 0xD4, 0x2A, }, + { 0xFE, 0x35, 0x29, 0x56, 0x5C, 0xD8, 0xE2, 0x8C, 0x5F, 0xA7, + 0x9E, 0xAC, }, + }; + + size_t i; + + for (i = 0; i < sizeof(keybuf); i++) + keybuf[i] = i; + + for (i = 0; i < sizeof(test_datas) / sizeof(*test_datas); i++) { + uint8_t hmacbuf[SHA1_HASHSIZE]; + sha1_hmac(keybuf, keylens[i], test_datas[i], strlen(test_datas[i]), hmacbuf); + + if (memcmp(hmacbuf, test_tags[i], taglens[i])) { + fprintf(stderr, "%s: HMAC %zu mismatch, got:\n\t", __FUNCTION__, i); + hexdump(stderr, hmacbuf, taglens[i]); + fprintf(stderr, "\n, expected:\n\t"); + hexdump(stderr, test_tags[i], taglens[i]); + fprintf(stderr, "\n"); + } + } +} + +void test_sha224(void) +{ + const char *test_datas[] = { + "abc", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + }; + + const uint8_t test_hashes[][SHA224_HASHSIZE] = { + { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22, 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, + 0x55, 0xB3, 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7, 0xE3, 0x6C, 0x9D, 0xA7, }, + { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC, 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, + 0x01, 0x50, 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19, 0x52, 0x52, 0x25, 0x25, }, + }; + + struct sha224 s; + size_t i; + + for (i = 0; i < sizeof(test_datas) / sizeof(*test_datas); i++) { + sha224_init(&s); + sha224_update(&s, test_datas[i], strlen(test_datas[i])); + sha224_finish(&s); + + if (memcmp(s.h, test_hashes[i], sizeof(s.h))) { + fprintf(stderr, "%s: hash %zu mismatch, got:\n\t", __FUNCTION__, i); + hexdump(stderr, s.h, sizeof(s.h)); + fprintf(stderr, "\n, expected:\n\t"); + hexdump(stderr, test_hashes[i], sizeof(test_hashes[i])); + fprintf(stderr, "\n"); + } + } +} + +void test_sha256(void) +{ + const char *test_datas[] = { + "abc", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + }; + + const uint8_t test_hashes[][SHA256_HASHSIZE] = { + { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA, 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23, + 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C, 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD, }, + { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8, 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39, + 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67, 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1, }, + }; + + struct sha256 s; + size_t i; + + for (i = 0; i < sizeof(test_datas) / sizeof(*test_datas); i++) { + sha256_init(&s); + sha256_update(&s, test_datas[i], strlen(test_datas[i])); + sha256_finish(&s); + + if (memcmp(s.h, test_hashes[i], sizeof(s.h))) + fprintf(stderr, "%s: hash %zu mismatch\n", __FUNCTION__, i); + } +} + +void test_hmac_sha256(void) +{ + const char *test_datas[] = { + "Sample message for keylen=blocklen", + "Sample message for keylen<blocklen", + "Sample message for keylen=blocklen", + "Sample message for keylen<blocklen, with truncated tag" + }; + + uint8_t keybuf[128]; + const size_t keylens[] = { + 64, 32, 100, 49 + }; + + const size_t taglens[] = { + 32, 32, 32, 16 + }; + + const uint8_t test_tags[][SHA256_HASHSIZE] = { + { 0x8B, 0xB9, 0xA1, 0xDB, 0x98, 0x06, 0xF2, 0x0D, 0xF7, 0xF7, 0x7B, 0x82, 0x13, 0x8C, 0x79, 0x14, 0xD1, 0x74, 0xD5, 0x9E, 0x13, 0xDC, 0x4D, 0x01, 0x69, 0xC9, 0x05, 0x7B, 0x13, 0x3E, 0x1D, 0x62, }, + { 0xA2, 0x8C, 0xF4, 0x31, 0x30, 0xEE, 0x69, 0x6A, 0x98, 0xF1, 0x4A, 0x37, 0x67, 0x8B, 0x56, 0xBC, 0xFC, 0xBD, 0xD9, 0xE5, 0xCF, 0x69, 0x71, 0x7F, 0xEC, 0xF5, 0x48, 0x0F, 0x0E, 0xBD, 0xF7, 0x90, }, + { 0xBD, 0xCC, 0xB6, 0xC7, 0x2D, 0xDE, 0xAD, 0xB5, 0x00, 0xAE, 0x76, 0x83, 0x86, 0xCB, 0x38, 0xCC, 0x41, 0xC6, 0x3D, 0xBB, 0x08, 0x78, 0xDD, 0xB9, 0xC7, 0xA3, 0x8A, 0x43, 0x1B, 0x78, 0x37, 0x8D, }, + { 0x27, 0xA8, 0xB1, 0x57, 0x83, 0x9E, 0xFE, 0xAC, 0x98, 0xDF, 0x07, 0x0B, 0x33, 0x1D, 0x59, 0x36, }, + }; + + size_t i; + + for (i = 0; i < sizeof(keybuf); i++) + keybuf[i] = i; + + for (i = 0; i < sizeof(test_datas) / sizeof(*test_datas); i++) { + uint8_t hmacbuf[SHA256_HASHSIZE]; + sha256_hmac(keybuf, keylens[i], test_datas[i], strlen(test_datas[i]), hmacbuf); + + if (memcmp(hmacbuf, test_tags[i], taglens[i])) { + fprintf(stderr, "%s: HMAC %zu mismatch, got:\n\t", __FUNCTION__, i); + hexdump(stderr, hmacbuf, taglens[i]); + fprintf(stderr, "\n, expected:\n\t"); + hexdump(stderr, test_tags[i], taglens[i]); + fprintf(stderr, "\n"); + } + } +} + +void test_sha384(void) +{ + const char *test_datas[] = { + "abc", + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + }; + + const uint8_t test_hashes[][SHA384_HASHSIZE] = { + { 0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B, 0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07, + 0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63, 0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED, + 0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23, 0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7, }, + { 0x09, 0x33, 0x0C, 0x33, 0xF7, 0x11, 0x47, 0xE8, 0x3D, 0x19, 0x2F, 0xC7, 0x82, 0xCD, 0x1B, 0x47, + 0x53, 0x11, 0x1B, 0x17, 0x3B, 0x3B, 0x05, 0xD2, 0x2F, 0xA0, 0x80, 0x86, 0xE3, 0xB0, 0xF7, 0x12, + 0xFC, 0xC7, 0xC7, 0x1A, 0x55, 0x7E, 0x2D, 0xB9, 0x66, 0xC3, 0xE9, 0xFA, 0x91, 0x74, 0x60, 0x39, }, + }; + + struct sha384 s; + size_t i; + + for (i = 0; i < sizeof(test_datas) / sizeof(*test_datas); i++) { + sha384_init(&s); + sha384_update(&s, test_datas[i], strlen(test_datas[i])); + sha384_finish(&s); + + if (memcmp(s.h, test_hashes[i], sizeof(s.h))) + fprintf(stderr, "%s: hash %zu mismatch\n", __FUNCTION__, i); + } +} + +void test_sha512(void) +{ + const char *test_datas[] = { + "abc", + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + }; + + const uint8_t test_hashes[][SHA512_HASHSIZE] = { + { 0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA, 0xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31, + 0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2, 0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A, + 0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8, 0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD, + 0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E, 0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F, }, + { 0x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA, 0x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F, + 0x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1, 0x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18, + 0x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4, 0x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A, + 0xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54, 0x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09, }, + }; + + struct sha512 s; + size_t i; + + for (i = 0; i < sizeof(test_datas) / sizeof(*test_datas); i++) { + sha512_init(&s); + sha512_update(&s, test_datas[i], strlen(test_datas[i])); + sha512_finish(&s); + + if (memcmp(s.h, test_hashes[i], sizeof(s.h))) + fprintf(stderr, "%s: hash %zu mismatch\n", __FUNCTION__, i); + } +} + +void test_hmac_sha512(void) +{ + const char *test_datas[] = { + "Sample message for keylen=blocklen", + "Sample message for keylen<blocklen", + "Sample message for keylen=blocklen", + "Sample message for keylen<blocklen, with truncated tag" + }; + + uint8_t keybuf[256]; + const size_t keylens[] = { + 128, 64, 200, 49 + }; + + const size_t taglens[] = { + 64, 64, 64, 32 + }; + + const uint8_t test_tags[][SHA512_HASHSIZE] = { + { 0xFC, 0x25, 0xE2, 0x40, 0x65, 0x8C, 0xA7, 0x85, 0xB7, 0xA8, 0x11, 0xA8, 0xD3, 0xF7, 0xB4, 0xCA, 0x48, 0xCF, 0xA2, 0x6A, 0x8A, 0x36, 0x6B, 0xF2, 0xCD, 0x1F, 0x83, 0x6B, 0x05, 0xFC, 0xB0, 0x24, 0xBD, 0x36, 0x85, 0x30, 0x81, 0x81, 0x1D, 0x6C, 0xEA, 0x42, 0x16, 0xEB, 0xAD, 0x79, 0xDA, 0x1C, 0xFC, 0xB9, 0x5E, 0xA4, 0x58, 0x6B, 0x8A, 0x0C, 0xE3, 0x56, 0x59, 0x6A, 0x55, 0xFB, 0x13, 0x47, }, + { 0xFD, 0x44, 0xC1, 0x8B, 0xDA, 0x0B, 0xB0, 0xA6, 0xCE, 0x0E, 0x82, 0xB0, 0x31, 0xBF, 0x28, 0x18, 0xF6, 0x53, 0x9B, 0xD5, 0x6E, 0xC0, 0x0B, 0xDC, 0x10, 0xA8, 0xA2, 0xD7, 0x30, 0xB3, 0x63, 0x4D, 0xE2, 0x54, 0x5D, 0x63, 0x9B, 0x0F, 0x2C, 0xF7, 0x10, 0xD0, 0x69, 0x2C, 0x72, 0xA1, 0x89, 0x6F, 0x1F, 0x21, 0x1C, 0x2B, 0x92, 0x2D, 0x1A, 0x96, 0xC3, 0x92, 0xE0, 0x7E, 0x7E, 0xA9, 0xFE, 0xDC, }, + { 0xD9, 0x3E, 0xC8, 0xD2, 0xDE, 0x1A, 0xD2, 0xA9, 0x95, 0x7C, 0xB9, 0xB8, 0x3F, 0x14, 0xE7, 0x6A, 0xD6, 0xB5, 0xE0, 0xCC, 0xE2, 0x85, 0x07, 0x9A, 0x12, 0x7D, 0x3B, 0x14, 0xBC, 0xCB, 0x7A, 0xA7, 0x28, 0x6D, 0x4A, 0xC0, 0xD4, 0xCE, 0x64, 0x21, 0x5F, 0x2B, 0xC9, 0xE6, 0x87, 0x0B, 0x33, 0xD9, 0x74, 0x38, 0xBE, 0x4A, 0xAA, 0x20, 0xCD, 0xA5, 0xC5, 0xA9, 0x12, 0xB4, 0x8B, 0x8E, 0x27, 0xF3, }, + { 0x00, 0xF3, 0xE9, 0xA7, 0x7B, 0xB0, 0xF0, 0x6D, 0xE1, 0x5F, 0x16, 0x06, 0x03, 0xE4, 0x2B, 0x50, 0x28, 0x75, 0x88, 0x08, 0x59, 0x66, 0x64, 0xC0, 0x3E, 0x1A, 0xB8, 0xFB, 0x2B, 0x07, 0x67, 0x78, }, + }; + + size_t i; + + for (i = 0; i < sizeof(keybuf); i++) + keybuf[i] = i; + + for (i = 0; i < sizeof(test_datas) / sizeof(*test_datas); i++) { + uint8_t hmacbuf[SHA512_HASHSIZE]; + sha512_hmac(keybuf, keylens[i], test_datas[i], strlen(test_datas[i]), hmacbuf); + + if (memcmp(hmacbuf, test_tags[i], taglens[i])) { + fprintf(stderr, "%s: HMAC %zu mismatch, got:\n\t", __FUNCTION__, i); + hexdump(stderr, hmacbuf, taglens[i]); + fprintf(stderr, "\n, expected:\n\t"); + hexdump(stderr, test_tags[i], taglens[i]); + fprintf(stderr, "\n"); + } + } +} + +void test_totp_sha1(void) +{ + /* Test vectors from RFC 6238 appendix B */ + const char *key = "12345678901234567890"; + const uint8_t period = 30; + const time_t t0 = 0; + + const time_t times[] = { + 59, 1111111109, 1111111111, 1234567890, 2000000000, 20000000000 + }; + const uint32_t totps[] = { + 94287082, /*0*/7081804, 14050471, 89005924, 69279037, 65353130 + }; + const uint32_t modulo = 100000000; + size_t i; + + for (i = 0; i < sizeof(times) / sizeof(*times); i++) { + uint32_t token = totp(key, strlen(key), times[i], period, t0, sha1_hmac, SHA1_HASHSIZE); + + if (token % modulo != totps[i]) + fprintf(stderr, "%s: token %zu mismatch, got %08" PRIu32 ", expected %08" PRIu32 "\n", + __FUNCTION__, i, token % modulo, totps[i]); + } +} + +void test_totp_sha256(void) +{ + /* Test vectors from RFC 6238 appendix B but key/seed from appendix A */ + const char *key = "12345678901234567890123456789012"; + const uint8_t period = 30; + const time_t t0 = 0; + + const time_t times[] = { + 59, 1111111109, 1111111111, 1234567890, 2000000000, 20000000000 + }; + const uint32_t totps[] = { + 46119246, 68084774, 67062674, 91819424, 90698825, 77737706 + }; + const uint32_t modulo = 100000000; + size_t i; + + for (i = 0; i < sizeof(times) / sizeof(*times); i++) { + uint32_t token = totp(key, strlen(key), times[i], period, t0, sha256_hmac, SHA256_HASHSIZE); + + if (token % modulo != totps[i]) + fprintf(stderr, "%s: token %zu mismatch, got %08" PRIu32 ", expected %08" PRIu32 "\n", + __FUNCTION__, i, token % modulo, totps[i]); + } +} + +void test_totp_sha512(void) +{ + /* Test vectors from RFC 6238 appendix B but key/seed from appendix A */ + const char *key = "1234567890123456789012345678901234567890123456789012345678901234"; + const uint8_t period = 30; + const time_t t0 = 0; + + const time_t times[] = { + 59, 1111111109, 1111111111, 1234567890, 2000000000, 20000000000 + }; + const uint32_t totps[] = { + 90693936, 25091201, 99943326, 93441116, 38618901, 47863826 + }; + const uint32_t modulo = 100000000; + size_t i; + + for (i = 0; i < sizeof(times) / sizeof(*times); i++) { + uint32_t token = totp(key, strlen(key), times[i], period, t0, sha512_hmac, SHA512_HASHSIZE); + + if (token % modulo != totps[i]) + fprintf(stderr, "%s: token %zu mismatch, got %08" PRIu32 ", expected %08" PRIu32 "\n", + __FUNCTION__, i, token % modulo, totps[i]); + } +} + +void test_debase32(void) +{ + const char *base32s[] = { + "MFRGG", + "MFRGGZDFMZTWQ2LKNM", + "MFRGGZDF", + }; + + const char *plaintext[] = { + "abc", + "abcdefghijk", + "abcde" + }; + + size_t i; + + for (i = 0; i < sizeof(base32s) / sizeof(*base32s); i++) { + char buffer[64]; + int len = sprintf(buffer, "%s", base32s[i]); + buffer[debase32(buffer, len)] = '\0'; + + if (strcmp(buffer, plaintext[i])) + fprintf(stderr, "%s: plaintext mismatch, got %s, expected %s\n", + __FUNCTION__, buffer, plaintext[i]); + } +} + +int main(int argc, char **argv) +{ + (void)argc; + (void)argv; + + test_sha1(); + test_sha224(); + test_sha256(); + test_sha384(); + test_sha512(); + + test_hmac_sha1(); + test_hmac_sha256(); + test_hmac_sha512(); + + test_totp_sha1(); + test_totp_sha256(); + test_totp_sha512(); + + test_debase32(); +}