2 * Copyright (c) 2022, 2023 Omar Polo <op@openbsd.org>
3 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include <openssl/evp.h>
29 #include <openssl/hmac.h>
32 # define pledge(a, b) (0)
36 # define __dead __attribute__((noreturn))
39 #if defined(__FreeBSD__) || defined(__NetBSD__)
40 # include <sys/endian.h>
41 #elif defined(__APPLE__)
42 # include <machine/endian.h>
43 # include <libkern/OSByteOrder.h>
44 # define htobe64(x) OSSwapHostToBigInt64(x)
45 # define be32toh(x) OSSwapBigToHostInt32(x)
51 usage(const char *argv0)
55 if ((me = strrchr(argv0, '/')) != NULL)
60 fprintf(stderr, "usage: %s\n", me);
67 if (c >= 'A' && c <= 'Z')
69 if (c >= '2' && c <= '7')
70 return (c - '2' + 26);
76 b32decode(const char *s, char *q, size_t qlen)
82 memset(val, 0, sizeof(val));
83 for (i = 0; i < 8; ++i) {
86 if ((val[i] = b32c(*s)) == -1)
97 *q++ = (val[0] << 3) | (val[1] >> 2);
98 *q++ = ((val[1] & 0x03) << 6) | (val[2] << 1) | (val[3] >> 4);
99 *q++ = ((val[3] & 0x0F) << 4) | (val[4] >> 1);
100 *q++ = ((val[4] & 0x01) << 7) | (val[5] << 2) | (val[6] >> 3);
101 *q++ = ((val[6] & 0x07) << 5) | val[7];
107 /* adapted from httpd(8) */
109 url_decode(char *url, char *dst)
125 if (!isxdigit((unsigned char)p[1]) ||
126 !isxdigit((unsigned char)p[2]))
133 * We don't have to validate "hex" because it is
134 * guaranteed to include two hex chars followed by nul.
136 x = strtoul(hex, NULL, 16);
145 uri2secret(char *s, int *digits, const EVP_MD **alg, int *period)
147 char *q, *t, *f, *ep, *secret = NULL;
150 if ((q = strchr(s, '?')) == NULL)
154 while ((f = strsep(&t, "&")) != NULL) {
155 if (!strncmp(f, "secret=", 7))
157 else if (!strncmp(f, "digits=", 7)) {
161 else if (!strcmp(f, "7"))
163 else if (!strcmp(f, "8"))
166 warnx("invalid number of digits; using 6");
167 } else if (!strncmp(f, "algorithm=", 10)) {
169 if (!strcmp(f, "SHA1"))
171 else if (!strcmp(f, "SHA256"))
173 else if (!strcmp(f, "SHA512"))
176 warnx("unknown algorithm; using SHA1");
177 } else if (!strncmp(f, "period=", 7)) {
180 l = strtol(f, &ep, 10);
181 if (f[0] == '\0' || *ep != '\0')
182 err(1, "period is not a number: %s", f);
183 if (errno == ERANGE && (l == LONG_MAX || l == LONG_MIN))
184 err(1, "period is way out of range: %s", f);
185 if (l < 1 || l > 120)
186 err(1, "period is out of range: %s", f);
193 if (url_decode(secret, s) == NULL)
194 errx(1, "failed to percent-decode the secret");
199 main(int argc, char **argv)
204 unsigned char md[EVP_MAX_MD_SIZE];
207 char *s, *q, *line = NULL;
213 int ch, digits = 6, period = 30;
215 if (pledge("stdio", NULL) == -1)
218 if ((argv0 = argv[0]) == NULL)
221 while ((ch = getopt(argc, argv, "")) != -1) {
235 linelen = getline(&line, &linesize, stdin);
239 errx(1, "no secret provided");
241 for (s = q = line; *s != '\0'; ++s) {
242 if (isspace((unsigned char)*s)) {
250 errx(1, "no secret provided");
252 if (!strncmp(line, "otpauth://", 10) &&
253 uri2secret(line, &digits, &alg, &period) == -1)
254 errx(1, "failed to decode otpauth URI");
256 if ((buflen = b32decode(line, buf, sizeof(buf))) == 0)
257 err(1, "can't base32 decode the secret");
259 ct = htobe64(time(NULL) / period);
261 HMAC(alg, buf, buflen, (unsigned char *)&ct, sizeof(ct), md, &mdlen);
263 off = md[mdlen - 1] & 0x0F;
265 memcpy(&hash, md + off, sizeof(hash));
266 hash = be32toh(hash);
270 printf("%06d\n", (hash & 0x7FFFFFFF) % 1000000);
273 printf("%07d\n", (hash & 0x7FFFFFFF) % 10000000);
276 printf("%08d\n", (hash & 0x7FFFFFFF) % 100000000);