diff --git a/cli.c b/cli.c index bc264cd..b44a482 100644 --- a/cli.c +++ b/cli.c @@ -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; } diff --git a/otp.c b/otp.c index 472cb0a..aa7f1ff 100644 --- a/otp.c +++ b/otp.c @@ -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); } diff --git a/run-tests.sh b/run-tests.sh index 81561e7..d6515da 100644 --- a/run-tests.sh +++ b/run-tests.sh @@ -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