Add -h flag for using a HMAC other than SHA1

This commit is contained in:
Lucas 2020-06-14 17:57:42 +00:00
parent 88ec5427c7
commit 4e95ab2b5d
1 changed files with 17 additions and 5 deletions

22
cli.c
View File

@ -30,7 +30,8 @@ extern const char *__progname;
static void static void
usage(void) usage(void)
{ {
fprintf(stderr, "Usage: %s [-H counter] SECRET\n", __progname); fprintf(stderr, "Usage:\n"
" %s [-h HMAC] [-H counter | -T counter] SECRET\n", __progname);
exit(1); exit(1);
} }
@ -41,10 +42,12 @@ main(int argc, char *argv[])
uint64_t counter; uint64_t counter;
int32_t r; int32_t r;
int ch, do_hotp; int ch, do_hotp;
enum otp_hmac hmac;
counter = 0; counter = 0;
do_hotp = 0; do_hotp = 0;
while ((ch = getopt(argc, argv, "H:T:")) != -1) { hmac = OTP_HMAC_SHA1;
while ((ch = getopt(argc, argv, "H:h:T:")) != -1) {
switch (ch) { switch (ch) {
case 'H': case 'H':
counter = mystrtonum(optarg, 0, LLONG_MAX, &errstr); counter = mystrtonum(optarg, 0, LLONG_MAX, &errstr);
@ -52,6 +55,16 @@ main(int argc, char *argv[])
errx(1, "counter is %s: %s", errstr, optarg); errx(1, "counter is %s: %s", errstr, optarg);
do_hotp = 1; do_hotp = 1;
break; break;
case 'h':
if (strcasecmp(optarg, "sha1") == 0)
hmac = OTP_HMAC_SHA1;
else if (strcasecmp(optarg, "sha256") == 0)
hmac = OTP_HMAC_SHA256;
else if (strcasecmp(optarg, "sha512") == 0)
hmac = OTP_HMAC_SHA512;
else
usage();
break;
case 'T': case 'T':
counter = mystrtonum(optarg, 0, LLONG_MAX, &errstr); counter = mystrtonum(optarg, 0, LLONG_MAX, &errstr);
if (errstr != NULL) if (errstr != NULL)
@ -69,13 +82,12 @@ main(int argc, char *argv[])
usage(); usage();
if (do_hotp) { if (do_hotp) {
r = hotp(OTP_HMAC_SHA1, argv[0], strlen(argv[0]), counter, 6); r = hotp(hmac, argv[0], strlen(argv[0]), counter, 6);
if (r == -1) if (r == -1)
errx(1, "couldn't calculate HOTP"); errx(1, "couldn't calculate HOTP");
printf("%0*" PRId32 "\n", 6, r); printf("%0*" PRId32 "\n", 6, r);
} else { } else {
r = totp(OTP_HMAC_SHA1, argv[0], strlen(argv[0]), counter, r = totp(hmac, argv[0], strlen(argv[0]), counter, 30, 8);
30, 8);
if (r == -1) if (r == -1)
errx(1, "couldn't calculate TOTP"); errx(1, "couldn't calculate TOTP");
printf("%0*" PRId32 "\n", 8, r); printf("%0*" PRId32 "\n", 8, r);