81 lines
1.5 KiB
C
81 lines
1.5 KiB
C
/*
|
|
* otpcli - CLI utility for generating OTPs
|
|
*
|
|
* Written in 2020 by Lucas
|
|
*
|
|
* To the extent possible under law, the author(s) have dedicated all
|
|
* copyright and related and neighboring rights to this software to the
|
|
* public domain worldwide. This software is distributed without any
|
|
* warranty.
|
|
*
|
|
* You should have received a copy of the CC0 Public Domain Dedication
|
|
* along with this software. If not, see
|
|
* <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
*/
|
|
|
|
#include <errno.h>
|
|
#include <inttypes.h>
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "err.h"
|
|
#include "otp.h"
|
|
|
|
extern const char *__progname;
|
|
|
|
static void
|
|
usage(void)
|
|
{
|
|
fprintf(stderr, "Usage: %s [-H counter] SECRET\n", __progname);
|
|
exit(1);
|
|
}
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
char *end;
|
|
uintmax_t n;
|
|
uint64_t counter;
|
|
int32_t r;
|
|
int ch, do_hotp;
|
|
|
|
counter = 0;
|
|
do_hotp = 0;
|
|
while ((ch = getopt(argc, argv, "H:")) != -1) {
|
|
switch (ch) {
|
|
case 'H':
|
|
errno = 0;
|
|
n = strtoumax(optarg, &end, 0);
|
|
if (*optarg == '\0' || *end != '\0')
|
|
errx(1, "-H: not a number");
|
|
if (errno != 0)
|
|
err(1, "-H");
|
|
else if (n > UINT64_MAX)
|
|
errx(1, "-H: out of range");
|
|
counter = n;
|
|
do_hotp = 1;
|
|
break;
|
|
default:
|
|
usage();
|
|
}
|
|
}
|
|
argc -= optind;
|
|
argv += optind;
|
|
|
|
if (argc != 1)
|
|
usage();
|
|
|
|
if (do_hotp) {
|
|
r = hotp(OTP_HMAC_SHA1, argv[0], strlen(argv[0]), counter, 6);
|
|
if (r == -1)
|
|
errx(1, "couldn't calculate HOTP");
|
|
printf("%" PRId32 "\n", r);
|
|
} else
|
|
errx(1, "TOTP unimplemented");
|
|
|
|
return 0;
|
|
}
|