include/punk/punk_password.h

filter: Statementstmt 100% 123 / 123 Branchbran 78.5% 77 / 98 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.5% 200 / 221 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_compat.h ↑ Summary xs/csrf.xs »
1 /* punk_password.h - password hashing, in C, over the dist's own crypto.
2 *
3 * PBKDF2-HMAC-SHA256 on the SHA-256 already bundled for sessions
4 * (punk_session.h), salted from the entropy source the build probes
5 * (pk_random_bytes, punk_csrf.h). No library dependency.
6 *
7 * The stored format is MaatSite::Password's, byte for byte, so rows hashed in
8 * production verify unchanged:
9 *
10 * pbkdf2-sha256$<iterations>$<salt base64>$<key base64>
11 *
12 * Standard padded base64 (that is what MIME::Base64 wrote), 16-byte salt,
13 * 32-byte key. verify() takes the iteration count from the stored string, so
14 * cost upgrades need no flag day: needs_rehash() says when, and the caller
15 * re-hashes on login with the plaintext in hand.
16 *
17 * Include after punk_session.h (pk_sha256 / pk_hmac_sha256 / pk_b64url) and
18 * punk_csrf.h (pk_random_bytes).
19 */
20  
21 #ifndef PUNK_PASSWORD_H
22 #define PUNK_PASSWORD_H
23  
24 #define PWD_SCHEME "pbkdf2-sha256"
25 #define PWD_SCHEME_LEN (sizeof(PWD_SCHEME) - 1)
26 #define PWD_SALT_LEN 16
27 #define PWD_KEY_LEN 32
28 #define PWD_DEFAULT_ITERATIONS 60000
29 /* What a cost may be, minted or stored. The stored string is not trusted to
30 * choose its own: below the floor it is a downgrade somebody wrote into the
31 * column, above the ceiling it is a login that never returns. */
32 #define PWD_MIN_ITERATIONS 1000
33 #define PWD_MAX_ITERATIONS 10000000
34  
35 /* ---- standard base64 (padded - the MIME::Base64 wire form) ---------------- */
36  
37 static const char pwd_b64_alpha[] =
38 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
39  
40 34 static SV *pwd_b64(pTHX_ const unsigned char *in, size_t len) {
41 34 SV *out = newSVpvs("");
42 size_t i;
43 289 ▶ for (i = 0; i + 2 < len; i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 2 < len; i += 3) { 255 34 289
44 255 U32 v = ((U32)in[i] << 16) | ((U32)in[i+1] << 8) | in[i+2];
45 255 char c[4] = { pwd_b64_alpha[(v >> 18) & 63], pwd_b64_alpha[(v >> 12) & 63],
46 255 pwd_b64_alpha[(v >> 6) & 63], pwd_b64_alpha[v & 63] };
47 255 sv_catpvn(out, c, 4);
48 }
49 34 ▶ if (len - i == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (len - i == 1) { 17 17 34
50 17 U32 v = (U32)in[i] << 16;
51 17 char c[4] = { pwd_b64_alpha[(v >> 18) & 63], pwd_b64_alpha[(v >> 12) & 63],
52 '=', '=' };
53 17 sv_catpvn(out, c, 4);
54 }
55 17 ▶ else if (len - i == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (len - i == 2) { 17 0 17
56 17 U32 v = ((U32)in[i] << 16) | ((U32)in[i+1] << 8);
57 17 char c[4] = { pwd_b64_alpha[(v >> 18) & 63], pwd_b64_alpha[(v >> 12) & 63],
58 17 pwd_b64_alpha[(v >> 6) & 63], '=' };
59 17 sv_catpvn(out, c, 4);
60 }
61 34 return out;
62 }
63  
64 870 static int pwd_b64_val(unsigned char c) {
65 870 ▶ if (c >= 'A' && c <= 'Z') return c - 'A';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'Z') return c - 'A'; 740 130 870
if (c >= 'A' && c <= 'Z') return c - 'A'; 370 370 740
66 500 ▶ if (c >= 'a' && c <= 'z') return c - 'a' + 26;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'z') return c - 'a' + 26; 370 130 500
if (c >= 'a' && c <= 'z') return c - 'a' + 26; 370 0 370
67 130 ▶ if (c >= '0' && c <= '9') return c - '0' + 52;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0' + 52; 102 28 130
if (c >= '0' && c <= '9') return c - '0' + 52; 102 0 102
68 28 ▶ if (c == '+') return 62;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '+') return 62; 12 16 28
69 16 ▶ if (c == '/') return 63;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '/') return 63; 15 1 16
70 1 return -1;
71 }
72  
73 /* Decode into out (caps outmax); returns the byte count or -1 on any
74 * malformation. Padding optional - both padded and stripped forms decode. */
75 29 static SSize_t pwd_b64_decode(const char *in, size_t len,
76 unsigned char *out, size_t outmax) {
77 29 U32 acc = 0;
78 29 int bits = 0;
79 29 size_t i, n = 0;
80 72 ▶ while (len && in[len - 1] == '=') len--;
Branch: 2 branches
BranchTrueFalseTotal
while (len && in[len - 1] == '=') len--; 72 0 72
while (len && in[len - 1] == '=') len--; 43 29 72
81 898 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 870 28 898
82 870 int v = pwd_b64_val((unsigned char)in[i]);
83 870 ▶ if (v < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) return -1; 1 869 870
84 869 acc = (acc << 6) | (U32)v;
85 869 bits += 6;
86 869 ▶ if (bits >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits >= 8) { 641 228 869
87 641 bits -= 8;
88 641 ▶ if (n >= outmax) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= outmax) return -1; 0 641 641
89 641 out[n++] = (unsigned char)((acc >> bits) & 0xff);
90 }
91 }
92 28 return (SSize_t)n;
93 }
94  
95 /* ---- PBKDF2-HMAC-SHA256 --------------------------------------------------- */
96  
97 /* The full block loop, although 32-byte keys make it a single block today -
98 * the day the key length changes must not be the day the loop appears. */
99 27 static void pwd_pbkdf2(const unsigned char *pass, size_t plen,
100 const unsigned char *salt, size_t slen,
101 U32 iterations, unsigned char *key, size_t keylen) {
102 /* 64 matches the parse buffers below - a stored salt can never be
103 * longer, and the default is PWD_SALT_LEN */
104 unsigned char block[64 + 4];
105 unsigned char u[32], t[32];
106 27 size_t off = 0;
107 27 U32 b = 1;
108 27 ▶ if (slen > 64) { memset(key, 0, keylen); return; } /* unreachable via
Branch: 1 branch
BranchTrueFalseTotal
if (slen > 64) { memset(key, 0, keylen); return; } /* unreachable via 0 27 27
109 parse; fail closed */
110 54 ▶ while (off < keylen) {
Branch: 1 branch
BranchTrueFalseTotal
while (off < keylen) { 27 27 54
111 27 size_t take = keylen - off < 32 ? keylen - off : 32;
112 U32 i;
113 27 memcpy(block, salt, slen);
114 27 block[slen] = (unsigned char)((b >> 24) & 0xff);
115 27 block[slen + 1] = (unsigned char)((b >> 16) & 0xff);
116 27 block[slen + 2] = (unsigned char)((b >> 8) & 0xff);
117 27 block[slen + 3] = (unsigned char)(b & 0xff);
118 27 pk_hmac_sha256(pass, plen, block, slen + 4, u);
119 27 memcpy(t, u, 32);
120 29000 ▶ for (i = 1; i < iterations; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < iterations; i++) { 28973 27 29000
121 unsigned char u2[32];
122 int j;
123 28973 pk_hmac_sha256(pass, plen, u, 32, u2);
124 28973 memcpy(u, u2, 32);
125 956109 ▶ for (j = 0; j < 32; j++) t[j] ^= u[j];
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 32; j++) t[j] ^= u[j]; 927136 28973 956109
126 }
127 27 memcpy(key + off, t, take);
128 27 off += take;
129 27 b++;
130 }
131 }
132  
133 /* ---- the stored string ---------------------------------------------------- */
134  
135 19 static SV *pwd_hash(pTHX_ const unsigned char *pass, STRLEN plen,
136 U32 iterations) {
137 unsigned char salt[PWD_SALT_LEN], key[PWD_KEY_LEN];
138 SV *out, *b64;
139 19 ▶ if (!iterations) iterations = PWD_DEFAULT_ITERATIONS;
Branch: 1 branch
BranchTrueFalseTotal
if (!iterations) iterations = PWD_DEFAULT_ITERATIONS; 0 19 19
140 /* refused here, because verify would refuse the string it made */
141 19 ▶ if (iterations < PWD_MIN_ITERATIONS || iterations > PWD_MAX_ITERATIONS)
Branch: 2 branches
BranchTrueFalseTotal
if (iterations < PWD_MIN_ITERATIONS || iterations > PWD_MAX_ITERATIONS) 18 1 19
if (iterations < PWD_MIN_ITERATIONS || iterations > PWD_MAX_ITERATIONS) 1 17 18
142 2 croak("Punk::Auth::Password: iterations must be between %d and %d",
143 PWD_MIN_ITERATIONS, PWD_MAX_ITERATIONS);
144 17 pk_random_bytes(aTHX_ salt, sizeof salt);
145 17 pwd_pbkdf2(pass, plen, salt, sizeof salt, iterations, key, sizeof key);
146 17 out = newSVpvf(PWD_SCHEME "$%u$", (unsigned)iterations);
147 17 b64 = pwd_b64(aTHX_ salt, sizeof salt);
148 17 sv_catsv(out, b64); SvREFCNT_dec(b64);
149 17 sv_catpvs(out, "$");
150 17 b64 = pwd_b64(aTHX_ key, sizeof key);
151 17 sv_catsv(out, b64); SvREFCNT_dec(b64);
152 17 return out;
153 }
154  
155 /* Parse a stored string into its parts. Returns 1 and fills the outputs, or
156 * 0 for anything that does not parse - which verify treats as a mismatch and
157 * needs_rehash treats as "rehash". */
158 22 static int pwd_parse(const char *s, STRLEN len, U32 *iterations,
159 unsigned char *salt, size_t saltmax, size_t *saltlen,
160 unsigned char *key, size_t keymax, size_t *keylen) {
161 22 const char *end = s + len, *p, *q;
162 22 U32 it = 0;
163 SSize_t n;
164 22 ▶ if (len <= PWD_SCHEME_LEN + 1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len <= PWD_SCHEME_LEN + 1) return 0; 3 19 22
165 19 ▶ if (memcmp(s, PWD_SCHEME "$", PWD_SCHEME_LEN + 1) != 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(s, PWD_SCHEME "$", PWD_SCHEME_LEN + 1) != 0) return 0; 0 19 19
166 19 p = s + PWD_SCHEME_LEN + 1;
167 19 q = p;
168 96 ▶ while (q < end && *q >= '0' && *q <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (q < end && *q >= '0' && *q <= '9') { 96 0 96
while (q < end && *q >= '0' && *q <= '9') { 77 19 96
while (q < end && *q >= '0' && *q <= '9') { 77 0 77
169 77 U32 nit = it * 10 + (U32)(*q - '0');
170 77 ▶ if (nit < it) return 0; /* overflow */
Branch: 1 branch
BranchTrueFalseTotal
if (nit < it) return 0; /* overflow */ 0 77 77
171 77 it = nit; q++;
172 }
173 19 ▶ if (q == p || !it || q >= end || *q != '$') return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (q == p || !it || q >= end || *q != '$') return 0; 19 0 19
if (q == p || !it || q >= end || *q != '$') return 0; 18 1 19
if (q == p || !it || q >= end || *q != '$') return 0; 18 0 18
if (q == p || !it || q >= end || *q != '$') return 0; 0 18 18
174 18 ▶ if (it < PWD_MIN_ITERATIONS || it > PWD_MAX_ITERATIONS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (it < PWD_MIN_ITERATIONS || it > PWD_MAX_ITERATIONS) return 0; 16 2 18
if (it < PWD_MIN_ITERATIONS || it > PWD_MAX_ITERATIONS) return 0; 1 15 16
175 15 p = q + 1;
176 15 q = (const char *)memchr(p, '$', (size_t)(end - p));
177 15 ▶ if (!q || q == p || q + 1 >= end) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!q || q == p || q + 1 >= end) return 0; 15 0 15
if (!q || q == p || q + 1 >= end) return 0; 15 0 15
if (!q || q == p || q + 1 >= end) return 0; 0 15 15
178 15 n = pwd_b64_decode(p, (size_t)(q - p), salt, saltmax);
179 15 ▶ if (n <= 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) return 0; 1 14 15
180 14 *saltlen = (size_t)n;
181 14 n = pwd_b64_decode(q + 1, (size_t)(end - q - 1), key, keymax);
182 14 ▶ if (n != PWD_KEY_LEN) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n != PWD_KEY_LEN) return 0; 1 13 14
183 13 *keylen = (size_t)n;
184 13 *iterations = it;
185 13 return 1;
186 }
187  
188 /* Constant-time over the derived key; the length check leaks only the length,
189 * which the format fixes anyway. */
190 16 static int pwd_verify(pTHX_ const unsigned char *pass, STRLEN plen,
191 const char *stored, STRLEN slen) {
192 unsigned char salt[64], want[64], got[64];
193 size_t saltlen, keylen, i;
194 U32 iterations;
195 16 unsigned char diff = 0;
196 16 ▶ if (!pwd_parse(stored, slen, &iterations,
Branch: 1 branch
BranchTrueFalseTotal
if (!pwd_parse(stored, slen, &iterations, 7 9 16
197 salt, sizeof salt, &saltlen,
198 want, sizeof want, &keylen))
199 7 return 0;
200 9 pwd_pbkdf2(pass, plen, salt, saltlen, iterations, got, keylen);
201 297 ▶ for (i = 0; i < keylen; i++) diff |= (unsigned char)(want[i] ^ got[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < keylen; i++) diff |= (unsigned char)(want[i] ^ got[i]); 288 9 297
202 9 return diff == 0;
203 }
204  
205 /* Burn the same work as a real verify without a stored hash to compare - the
206 * unknown-email path of a login must cost what the wrong-password path costs,
207 * or response time says which emails exist. */
208 1 static void pwd_dummy_verify(pTHX_ const unsigned char *pass, STRLEN plen,
209 U32 iterations) {
210 static const unsigned char salt[PWD_SALT_LEN] = "punk.dummy.salt!";
211 unsigned char key[PWD_KEY_LEN];
212 1 ▶ if (!iterations) iterations = PWD_DEFAULT_ITERATIONS;
Branch: 1 branch
BranchTrueFalseTotal
if (!iterations) iterations = PWD_DEFAULT_ITERATIONS; 0 1 1
213 1 ▶ if (iterations < PWD_MIN_ITERATIONS) iterations = PWD_MIN_ITERATIONS;
Branch: 1 branch
BranchTrueFalseTotal
if (iterations < PWD_MIN_ITERATIONS) iterations = PWD_MIN_ITERATIONS; 0 1 1
214 1 ▶ if (iterations > PWD_MAX_ITERATIONS) iterations = PWD_MAX_ITERATIONS;
Branch: 1 branch
BranchTrueFalseTotal
if (iterations > PWD_MAX_ITERATIONS) iterations = PWD_MAX_ITERATIONS; 0 1 1
215 1 pwd_pbkdf2(pass, plen, salt, sizeof salt, iterations, key, sizeof key);
216 1 }
217  
218 6 static int pwd_needs_rehash(pTHX_ const char *stored, STRLEN slen,
219 U32 current) {
220 unsigned char salt[64], key[64];
221 size_t saltlen, keylen;
222 U32 iterations;
223 6 ▶ if (!current) current = PWD_DEFAULT_ITERATIONS;
Branch: 1 branch
BranchTrueFalseTotal
if (!current) current = PWD_DEFAULT_ITERATIONS; 0 6 6
224 6 ▶ if (!pwd_parse(stored, slen, &iterations,
Branch: 1 branch
BranchTrueFalseTotal
if (!pwd_parse(stored, slen, &iterations, 2 4 6
225 salt, sizeof salt, &saltlen,
226 key, sizeof key, &keylen))
227 2 return 1; /* fail toward rehash */
228 4 return iterations < current;
229 }
230  
231 /* ---- single-use token material -------------------------------------------- */
232  
233 /* 32 random bytes, base64url unpadded - 43 characters, cookie- and URL-safe. */
234 11 static SV *pwd_token(pTHX) {
235 unsigned char raw[32];
236 11 pk_random_bytes(aTHX_ raw, sizeof raw);
237 11 return pk_b64url(aTHX_ raw, sizeof raw);
238 }
239  
240 /* Lowercase sha256 hex of the token - the only form storage ever sees. */
241 23 static SV *pwd_token_digest(pTHX_ const unsigned char *tok, STRLEN len) {
242 static const char hex[] = "0123456789abcdef";
243 unsigned char d[32];
244 char out[64];
245 int i;
246 23 pk_sha256(tok, len, d);
247 759 ▶ for (i = 0; i < 32; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 32; i++) { 736 23 759
248 736 out[i * 2] = hex[d[i] >> 4];
249 736 out[i * 2 + 1] = hex[d[i] & 15];
250 }
251 23 return newSVpvn(out, 64);
252 }
253  
254 #endif /* PUNK_PASSWORD_H */
« include/punk/punk_compat.h ↑ Summary xs/csrf.xs »