include/ptotp/ptotp_otp.h

filter: Statementstmt 91.3% 53 / 58 Branchbran 70.6% 41 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.0% 94 / 116 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/plugin.xs ↑ Summary include/ptotp/ptotp_rand.h »
1 #ifndef PTOTP_OTP_H
2 #define PTOTP_OTP_H
3  
4 #include <stddef.h>
5 #include <string.h>
6  
7 #include "frh_abi.h"
8  
9 /* ptotp_otp.h - HOTP (RFC 4226) and TOTP (RFC 6238) over the HMAC a
10 * frh_abi table provides. Pure C, no perl types: the table pointer
11 * comes in as an argument, resolved once by the XS at boot.
12 *
13 * The counter is uint64-shaped end to end and never passes through an
14 * IV or a %d on its way anywhere: the 32-bit IV smokers truncate
15 * 64-bit casts, and a truncated counter is a code that verifies on
16 * the developer's machine and nowhere else.
17 */
18  
19 typedef unsigned long long ptotp_u64;
20  
21 #define PTOTP_MAX_DIGEST 64
22  
23 /* HOTP: counter as eight big-endian bytes, HMAC, dynamic truncation,
24 * modulo 10^digits. Returns the code, or (unsigned long)-1 on an HMAC
25 * failure or digits outside 6..8. */
26 82 static unsigned long ptotp_hotp(const frh_abi *H, int alg_id,
27 const unsigned char *secret, size_t slen,
28 ptotp_u64 counter, int digits)
29 {
30 unsigned char msg[8], mac[PTOTP_MAX_DIGEST];
31 const frh_algo_t *a;
32 unsigned long code, mod;
33 size_t off;
34 int i;
35  
36 82 ▶ if (!H || digits < 6 || digits > 8)
Branch: 3 branches
BranchTrueFalseTotal
if (!H || digits < 6 || digits > 8) 82 0 82
if (!H || digits < 6 || digits > 8) 82 0 82
if (!H || digits < 6 || digits > 8) 0 82 82
37 0 return (unsigned long)-1;
38 82 a = H->algo_by_id(alg_id);
39 82 ▶ if (!a || !a->hmac_able || a->digest_size < 20 ||
Branch: 3 branches
BranchTrueFalseTotal
if (!a || !a->hmac_able || a->digest_size < 20 || 82 0 82
if (!a || !a->hmac_able || a->digest_size < 20 || 82 0 82
if (!a || !a->hmac_able || a->digest_size < 20 || 82 0 82
40 82 ▶ a->digest_size > PTOTP_MAX_DIGEST)
Branch: 1 branch
BranchTrueFalseTotal
a->digest_size > PTOTP_MAX_DIGEST) 0 82 82
41 0 return (unsigned long)-1;
42  
43 738 ▶ for (i = 7; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 7; i >= 0; i--) { 656 82 738
44 656 msg[i] = (unsigned char)(counter & 0xff);
45 656 counter >>= 8;
46 }
47 82 ▶ if (H->hmac(alg_id, secret, slen, msg, 8, mac) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (H->hmac(alg_id, secret, slen, msg, 8, mac) != 0) 0 82 82
48 0 return (unsigned long)-1;
49  
50 /* dynamic truncation: the low nibble of the last byte indexes four
51 * bytes; mask the top bit */
52 82 off = mac[a->digest_size - 1] & 0x0f;
53 82 code = ((unsigned long)(mac[off] & 0x7f) << 24)
54 82 | ((unsigned long) mac[off + 1] << 16)
55 82 | ((unsigned long) mac[off + 2] << 8)
56 82 | (unsigned long) mac[off + 3];
57  
58 82 mod = 1;
59 612 ▶ for (i = 0; i < digits; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < digits; i++) 530 82 612
60 530 mod *= 10;
61 82 return code % mod;
62 }
63  
64 /* TOTP at a moment: HOTP at time/period. */
65 22 static unsigned long ptotp_totp_at(const frh_abi *H, int alg_id,
66 const unsigned char *secret, size_t slen,
67 ptotp_u64 unixtime, unsigned period,
68 int digits)
69 {
70 22 ▶ if (!period)
Branch: 1 branch
BranchTrueFalseTotal
if (!period) 0 22 22
71 0 return (unsigned long)-1;
72 22 return ptotp_hotp(H, alg_id, secret, slen, unixtime / period, digits);
73 }
74  
75 /* Constant-time comparison of two digit strings of equal length. */
76 46 static int ptotp_ct_eq(const char *a, const char *b, size_t n)
77 {
78 46 unsigned char acc = 0;
79 size_t i;
80  
81 322 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 276 46 322
82 276 acc |= (unsigned char)(a[i] ^ b[i]);
83 46 return acc == 0;
84 }
85  
86 /* Render a code as exactly `digits` ASCII digits. */
87 82 static void ptotp_render(unsigned long code, int digits, char *out)
88 {
89 int i;
90  
91 612 ▶ for (i = digits - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = digits - 1; i >= 0; i--) { 530 82 612
92 530 out[i] = (char)('0' + code % 10);
93 530 code /= 10;
94 }
95 82 out[digits] = '\0';
96 82 }
97  
98 /* Verify a submitted code against the window around `now`.
99 *
100 * Checks counters now/period - skew .. + skew. Two properties most
101 * implementations skip, both deliberate:
102 *
103 * - The window loop NEVER breaks early on a match, and each
104 * comparison is constant-time, so the time taken does not report
105 * which counter hit or whether one did.
106 *
107 * - `last_counter` makes the verifier replay-safe by construction:
108 * any code whose counter does not exceed it is refused even when
109 * it would otherwise verify, and on success *matched receives the
110 * counter for the caller to store. A verifier returning a bare
111 * boolean cannot be made replay-safe from outside. Pass
112 * (ptotp_u64)-1 as last_counter for "no floor" - 0 would wrongly
113 * refuse the counter-zero code, which RFC 4226's own test vectors
114 * exercise.
115 *
116 * The submitted code is refused before any HMAC runs when its length
117 * is not `digits` or it contains a non-digit.
118 *
119 * Returns 1 on acceptance, 0 otherwise. */
120 20 static int ptotp_verify(const frh_abi *H, int alg_id,
121 const unsigned char *secret, size_t slen,
122 ptotp_u64 now, unsigned period, int digits,
123 const char *code, size_t clen,
124 unsigned skew, ptotp_u64 last_counter,
125 ptotp_u64 *matched)
126 {
127 ptotp_u64 centre, c, lo, hi;
128 20 int ok = 0;
129 size_t i;
130  
131 20 ▶ if (!H || !period || digits < 6 || digits > 8 || clen != (size_t)digits)
Branch: 5 branches
BranchTrueFalseTotal
if (!H || !period || digits < 6 || digits > 8 || clen != (size_t)digits) 20 0 20
if (!H || !period || digits < 6 || digits > 8 || clen != (size_t)digits) 20 0 20
if (!H || !period || digits < 6 || digits > 8 || clen != (size_t)digits) 20 0 20
if (!H || !period || digits < 6 || digits > 8 || clen != (size_t)digits) 20 0 20
if (!H || !period || digits < 6 || digits > 8 || clen != (size_t)digits) 4 16 20
132 4 return 0;
133 111 ▶ for (i = 0; i < clen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < clen; i++) 96 15 111
134 96 ▶ if (code[i] < '0' || code[i] > '9')
Branch: 2 branches
BranchTrueFalseTotal
if (code[i] < '0' || code[i] > '9') 96 0 96
if (code[i] < '0' || code[i] > '9') 1 95 96
135 1 return 0;
136  
137 15 centre = now / period;
138 15 ▶ lo = centre > skew ? centre - skew : 0;
Branch: 1 branch
BranchTrueFalseTotal
lo = centre > skew ? centre - skew : 0; 14 1 15
139 15 hi = centre + skew;
140  
141 61 ▶ for (c = lo; c <= hi; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = lo; c <= hi; c++) { 46 15 61
142 46 unsigned long want = ptotp_hotp(H, alg_id, secret, slen, c, digits);
143 char buf[9];
144  
145 46 ▶ if (want == (unsigned long)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (want == (unsigned long)-1) 0 46 46
146 0 continue;
147 46 ptotp_render(want, digits, buf);
148 46 ▶ if (ptotp_ct_eq(code, buf, clen)
Branch: 1 branch
BranchTrueFalseTotal
if (ptotp_ct_eq(code, buf, clen) 12 34 46
149 12 ▶ && (last_counter == (ptotp_u64)-1 || c > last_counter)
Branch: 2 branches
BranchTrueFalseTotal
&& (last_counter == (ptotp_u64)-1 || c > last_counter) 5 7 12
&& (last_counter == (ptotp_u64)-1 || c > last_counter) 2 3 5
150 9 ▶ && !ok) {
Branch: 1 branch
BranchTrueFalseTotal
&& !ok) { 9 0 9
151 9 ok = 1;
152 9 ▶ if (matched)
Branch: 1 branch
BranchTrueFalseTotal
if (matched) 9 0 9
153 9 *matched = c;
154 }
155 /* no break: every counter in the window is always checked */
156 }
157 15 return ok;
158 }
159  
160 #endif /* PTOTP_OTP_H */
« xs/plugin.xs ↑ Summary include/ptotp/ptotp_rand.h »