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
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);
}
@ -41,10 +42,12 @@ main(int argc, char *argv[])
uint64_t counter;
int32_t r;
int ch, do_hotp;
enum otp_hmac hmac;
counter = 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) {
case 'H':
counter = mystrtonum(optarg, 0, LLONG_MAX, &errstr);
@ -52,6 +55,16 @@ main(int argc, char *argv[])
errx(1, "counter is %s: %s", errstr, optarg);
do_hotp = 1;
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':
counter = mystrtonum(optarg, 0, LLONG_MAX, &errstr);
if (errstr != NULL)
@ -69,13 +82,12 @@ main(int argc, char *argv[])
usage();
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)
errx(1, "couldn't calculate HOTP");
printf("%0*" PRId32 "\n", 6, r);
} else {
r = totp(OTP_HMAC_SHA1, argv[0], strlen(argv[0]), counter,
30, 8);
r = totp(hmac, argv[0], strlen(argv[0]), counter, 30, 8);
if (r == -1)
errx(1, "couldn't calculate TOTP");
printf("%0*" PRId32 "\n", 8, r);