Add TOTP support

Add all the test vectors from RFC 6238, but comment the non-SHA1 ones
for the time being.
This commit is contained in:
Lucas 2020-06-14 17:29:45 +00:00
parent 22333c5d6e
commit 41ca24d151
3 changed files with 34 additions and 4 deletions

17
cli.c
View File

@ -44,7 +44,7 @@ main(int argc, char *argv[])
counter = 0;
do_hotp = 0;
while ((ch = getopt(argc, argv, "H:")) != -1) {
while ((ch = getopt(argc, argv, "H:T:")) != -1) {
switch (ch) {
case 'H':
counter = mystrtonum(optarg, 0, LLONG_MAX, &errstr);
@ -52,6 +52,12 @@ main(int argc, char *argv[])
errx(1, "counter is %s: %s", errstr, optarg);
do_hotp = 1;
break;
case 'T':
counter = mystrtonum(optarg, 0, LLONG_MAX, &errstr);
if (errstr != NULL)
errx(1, "counter is %s: %s", errstr, optarg);
do_hotp = 0;
break;
default:
usage();
}
@ -67,8 +73,13 @@ main(int argc, char *argv[])
if (r == -1)
errx(1, "couldn't calculate HOTP");
printf("%" PRId32 "\n", r);
} else
errx(1, "TOTP unimplemented");
} else {
r = totp(OTP_HMAC_SHA1, argv[0], strlen(argv[0]), counter,
30, 8);
if (r == -1)
errx(1, "couldn't calculate TOTP");
printf("%" PRId32 "\n", r);
}
return 0;
}

2
otp.c
View File

@ -78,5 +78,5 @@ int32_t
totp(enum otp_hmac hmac, const void *key, size_t key_len, uint64_t t,
unsigned int step, unsigned int digits)
{
return -1;
return hotp(hmac, key, key_len, t / step, digits);
}

View File

@ -44,6 +44,25 @@ case_eq 162583 ./otpcli -H 7 "$HOTP_SECRET"
case_eq 399871 ./otpcli -H 8 "$HOTP_SECRET"
case_eq 520489 ./otpcli -H 9 "$HOTP_SECRET"
case_eq 94287082 ./otpcli -T 59 "$HOTP_SECRET"
#case_eq 46119246 ./otpcli -h sha256 -T 59 "$HOTP_SECRET"
#case_eq 90693936 ./otpcli -h sha512 -T 59 "$HOTP_SECRET"
case_eq 07081804 ./otpcli -T 1111111109 "$HOTP_SECRET"
#case_eq 68084774 ./otpcli -h sha256 -T 1111111109 "$HOTP_SECRET"
#case_eq 25091201 ./otpcli -h sha512 -T 1111111109 "$HOTP_SECRET"
case_eq 14050471 ./otpcli -T 1111111111 "$HOTP_SECRET"
#case_eq 67062674 ./otpcli -h sha256 -T 1111111111 "$HOTP_SECRET"
#case_eq 99943326 ./otpcli -h sha512 -T 1111111111 "$HOTP_SECRET"
case_eq 89005924 ./otpcli -T 1234567890 "$HOTP_SECRET"
#case_eq 91819424 ./otpcli -h sha256 -T 1234567890 "$HOTP_SECRET"
#case_eq 93441116 ./otpcli -h sha512 -T 1234567890 "$HOTP_SECRET"
case_eq 69279037 ./otpcli -T 2000000000 "$HOTP_SECRET"
#case_eq 90698825 ./otpcli -h sha256 -T 2000000000 "$HOTP_SECRET"
#case_eq 38618901 ./otpcli -h sha512 -T 2000000000 "$HOTP_SECRET"
case_eq 65353130 ./otpcli -T 20000000000 "$HOTP_SECRET"
#case_eq 77737706 ./otpcli -h sha256 -T 20000000000 "$HOTP_SECRET"
#case_eq 47863826 ./otpcli -h sha512 -T 20000000000 "$HOTP_SECRET"
if [ $_test_rc -eq 0 ]; then
printf "All %u tests completed successfully!\n" "$_test_nr" >&2
else