| 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 |
42 |
| static SV *pwd_b64(pTHX_ const unsigned char *in, size_t len) { |
| 41 |
42 |
| SV *out = newSVpvs(""); |
| 42 |
|
| size_t i; |
| 43 |
357 |
▶ | for (i = 0; i + 2 < len; i += 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 2 < len; i += 3) { |
315 |
42 |
357 |
|
| 44 |
315 |
| U32 v = ((U32)in[i] << 16) | ((U32)in[i+1] << 8) | in[i+2]; |
| 45 |
315 |
| char c[4] = { pwd_b64_alpha[(v >> 18) & 63], pwd_b64_alpha[(v >> 12) & 63], |
| 46 |
315 |
| pwd_b64_alpha[(v >> 6) & 63], pwd_b64_alpha[v & 63] }; |
| 47 |
315 |
| sv_catpvn(out, c, 4); |
| 48 |
|
| } |
| 49 |
42 |
▶ | if (len - i == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len - i == 1) { |
21 |
21 |
42 |
|
| 50 |
21 |
| U32 v = (U32)in[i] << 16; |
| 51 |
21 |
| char c[4] = { pwd_b64_alpha[(v >> 18) & 63], pwd_b64_alpha[(v >> 12) & 63], |
| 52 |
|
| '=', '=' }; |
| 53 |
21 |
| sv_catpvn(out, c, 4); |
| 54 |
|
| } |
| 55 |
|
| /* |
| 56 |
|
| # uncoverable branch false |
| 57 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 58 |
21 |
▶ | else if (len - i == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (len - i == 2) { |
21 |
0 |
21 |
|
| 59 |
21 |
| U32 v = ((U32)in[i] << 16) | ((U32)in[i+1] << 8); |
| 60 |
21 |
| char c[4] = { pwd_b64_alpha[(v >> 18) & 63], pwd_b64_alpha[(v >> 12) & 63], |
| 61 |
21 |
| pwd_b64_alpha[(v >> 6) & 63], '=' }; |
| 62 |
21 |
| sv_catpvn(out, c, 4); |
| 63 |
|
| } |
| 64 |
42 |
| return out; |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
870 |
| static int pwd_b64_val(unsigned char c) { |
| 68 |
870 |
▶ | if (c >= 'A' && c <= 'Z') return c - 'A'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'A' && c <= 'Z') return c - 'A'; |
691 |
179 |
870 |
| if (c >= 'A' && c <= 'Z') return c - 'A'; |
326 |
365 |
691 |
|
| 69 |
544 |
▶ | if (c >= 'a' && c <= 'z') return c - 'a' + 26; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'a' && c <= 'z') return c - 'a' + 26; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
365 |
179 |
544 |
| if (c >= 'a' && c <= 'z') return c - 'a' + 26; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
365 |
0 |
365 |
|
| 70 |
179 |
▶ | if (c >= '0' && c <= '9') return c - '0' + 52; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= '0' && c <= '9') return c - '0' + 52; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
146 |
33 |
179 |
| if (c >= '0' && c <= '9') return c - '0' + 52; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
146 |
0 |
146 |
|
| 71 |
33 |
▶ | if (c == '+') return 62; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '+') return 62; |
22 |
11 |
33 |
|
| 72 |
11 |
▶ | if (c == '/') return 63; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '/') return 63; |
10 |
1 |
11 |
|
| 73 |
1 |
| return -1; |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
|
| /* Decode into out (caps outmax); returns the byte count or -1 on any |
| 77 |
|
| * malformation. Padding optional - both padded and stripped forms decode. */ |
| 78 |
29 |
| static SSize_t pwd_b64_decode(const char *in, size_t len, |
| 79 |
|
| unsigned char *out, size_t outmax) { |
| 80 |
29 |
| U32 acc = 0; |
| 81 |
29 |
| int bits = 0; |
| 82 |
29 |
| size_t i, n = 0; |
| 83 |
72 |
▶ | while (len && in[len - 1] == '=') len--; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| while (len && in[len - 1] == '=') len--; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
72 |
0 |
72 |
| while (len && in[len - 1] == '=') len--; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
43 |
29 |
72 |
|
| 84 |
898 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
870 |
28 |
898 |
|
| 85 |
870 |
| int v = pwd_b64_val((unsigned char)in[i]); |
| 86 |
870 |
▶ | if (v < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v < 0) return -1; |
1 |
869 |
870 |
|
| 87 |
869 |
| acc = (acc << 6) | (U32)v; |
| 88 |
869 |
| bits += 6; |
| 89 |
869 |
▶ | if (bits >= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bits >= 8) { |
641 |
228 |
869 |
|
| 90 |
641 |
| bits -= 8; |
| 91 |
|
| /* |
| 92 |
|
| # uncoverable branch true |
| 93 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 94 |
641 |
▶ | if (n >= outmax) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n >= outmax) return -1; |
0 |
641 |
641 |
|
| 95 |
641 |
| out[n++] = (unsigned char)((acc >> bits) & 0xff); |
| 96 |
|
| } |
| 97 |
|
| } |
| 98 |
28 |
| return (SSize_t)n; |
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
|
| /* ---- PBKDF2-HMAC-SHA256 --------------------------------------------------- */ |
| 102 |
|
| |
| 103 |
|
| /* The full block loop, although 32-byte keys make it a single block today - |
| 104 |
|
| * the day the key length changes must not be the day the loop appears. */ |
| 105 |
31 |
| static void pwd_pbkdf2(const unsigned char *pass, size_t plen, |
| 106 |
|
| const unsigned char *salt, size_t slen, |
| 107 |
|
| U32 iterations, unsigned char *key, size_t keylen) { |
| 108 |
|
| /* 64 matches the parse buffers below - a stored salt can never be |
| 109 |
|
| * longer, and the default is PWD_SALT_LEN */ |
| 110 |
|
| unsigned char block[64 + 4]; |
| 111 |
|
| unsigned char u[32], t[32]; |
| 112 |
31 |
| size_t off = 0; |
| 113 |
31 |
| U32 b = 1; |
| 114 |
|
| /* |
| 115 |
|
| # uncoverable branch true |
| 116 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 117 |
31 |
▶ | if (slen > 64) { memset(key, 0, keylen); return; } /* unreachable via |
Branch: 1 branch
| Branch | True | False | Total |
| if (slen > 64) { memset(key, 0, keylen); return; } /* unreachable via |
0 |
31 |
31 |
|
| 118 |
|
| parse; fail closed */ |
| 119 |
62 |
▶ | while (off < keylen) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (off < keylen) { |
31 |
31 |
62 |
|
| 120 |
31 |
| size_t take = keylen - off < 32 ? keylen - off : 32; |
| 121 |
|
| U32 i; |
| 122 |
31 |
| memcpy(block, salt, slen); |
| 123 |
31 |
| block[slen] = (unsigned char)((b >> 24) & 0xff); |
| 124 |
31 |
| block[slen + 1] = (unsigned char)((b >> 16) & 0xff); |
| 125 |
31 |
| block[slen + 2] = (unsigned char)((b >> 8) & 0xff); |
| 126 |
31 |
| block[slen + 3] = (unsigned char)(b & 0xff); |
| 127 |
31 |
| pk_hmac_sha256(pass, plen, block, slen + 4, u); |
| 128 |
31 |
| memcpy(t, u, 32); |
| 129 |
33000 |
▶ | for (i = 1; i < iterations; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < iterations; i++) { |
32969 |
31 |
33000 |
|
| 130 |
|
| unsigned char u2[32]; |
| 131 |
|
| int j; |
| 132 |
32969 |
| pk_hmac_sha256(pass, plen, u, 32, u2); |
| 133 |
32969 |
| memcpy(u, u2, 32); |
| 134 |
1087977 |
▶ | for (j = 0; j < 32; j++) t[j] ^= u[j]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < 32; j++) t[j] ^= u[j]; |
1055008 |
32969 |
1087977 |
|
| 135 |
|
| } |
| 136 |
31 |
| memcpy(key + off, t, take); |
| 137 |
31 |
| off += take; |
| 138 |
31 |
| b++; |
| 139 |
|
| } |
| 140 |
|
| } |
| 141 |
|
| |
| 142 |
|
| /* ---- the stored string ---------------------------------------------------- */ |
| 143 |
|
| |
| 144 |
23 |
| static SV *pwd_hash(pTHX_ const unsigned char *pass, STRLEN plen, |
| 145 |
|
| U32 iterations) { |
| 146 |
|
| unsigned char salt[PWD_SALT_LEN], key[PWD_KEY_LEN]; |
| 147 |
|
| SV *out, *b64; |
| 148 |
|
| /* |
| 149 |
|
| # uncoverable branch true |
| 150 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 151 |
23 |
▶ | if (!iterations) iterations = PWD_DEFAULT_ITERATIONS; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!iterations) iterations = PWD_DEFAULT_ITERATIONS; |
0 |
23 |
23 |
|
| 152 |
|
| /* refused here, because verify would refuse the string it made */ |
| 153 |
23 |
▶ | if (iterations < PWD_MIN_ITERATIONS || iterations > PWD_MAX_ITERATIONS) |
Branch: 2 branches
| Branch | True | False | Total |
| if (iterations < PWD_MIN_ITERATIONS || iterations > PWD_MAX_ITERATIONS) |
22 |
1 |
23 |
| if (iterations < PWD_MIN_ITERATIONS || iterations > PWD_MAX_ITERATIONS) |
1 |
21 |
22 |
|
| 154 |
2 |
| croak("Punk::Auth::Password: iterations must be between %d and %d", |
| 155 |
|
| PWD_MIN_ITERATIONS, PWD_MAX_ITERATIONS); |
| 156 |
21 |
| pk_random_bytes(aTHX_ salt, sizeof salt); |
| 157 |
21 |
| pwd_pbkdf2(pass, plen, salt, sizeof salt, iterations, key, sizeof key); |
| 158 |
21 |
| out = newSVpvf(PWD_SCHEME "$%u$", (unsigned)iterations); |
| 159 |
21 |
| b64 = pwd_b64(aTHX_ salt, sizeof salt); |
| 160 |
21 |
| sv_catsv(out, b64); SvREFCNT_dec(b64); |
| 161 |
21 |
| sv_catpvs(out, "$"); |
| 162 |
21 |
| b64 = pwd_b64(aTHX_ key, sizeof key); |
| 163 |
21 |
| sv_catsv(out, b64); SvREFCNT_dec(b64); |
| 164 |
21 |
| return out; |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
|
| /* Parse a stored string into its parts. Returns 1 and fills the outputs, or |
| 168 |
|
| * 0 for anything that does not parse - which verify treats as a mismatch and |
| 169 |
|
| * needs_rehash treats as "rehash". */ |
| 170 |
22 |
| static int pwd_parse(const char *s, STRLEN len, U32 *iterations, |
| 171 |
|
| unsigned char *salt, size_t saltmax, size_t *saltlen, |
| 172 |
|
| unsigned char *key, size_t keymax, size_t *keylen) { |
| 173 |
22 |
| const char *end = s + len, *p, *q; |
| 174 |
22 |
| U32 it = 0; |
| 175 |
|
| SSize_t n; |
| 176 |
22 |
▶ | if (len <= PWD_SCHEME_LEN + 1) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len <= PWD_SCHEME_LEN + 1) return 0; |
3 |
19 |
22 |
|
| 177 |
|
| /* |
| 178 |
|
| # uncoverable branch true |
| 179 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 180 |
19 |
▶ | if (memcmp(s, PWD_SCHEME "$", PWD_SCHEME_LEN + 1) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(s, PWD_SCHEME "$", PWD_SCHEME_LEN + 1) != 0) return 0; |
0 |
19 |
19 |
|
| 181 |
19 |
| p = s + PWD_SCHEME_LEN + 1; |
| 182 |
19 |
| q = p; |
| 183 |
|
| /* |
| 184 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 185 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 186 |
96 |
▶ | while (q < end && *q >= '0' && *q <= '9') { |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 187 |
77 |
| U32 nit = it * 10 + (U32)(*q - '0'); |
| 188 |
|
| /* |
| 189 |
|
| # uncoverable branch true |
| 190 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 191 |
77 |
▶ | if (nit < it) return 0; /* overflow */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (nit < it) return 0; /* overflow */ |
0 |
77 |
77 |
|
| 192 |
77 |
| it = nit; q++; |
| 193 |
|
| } |
| 194 |
|
| /* |
| 195 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 196 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 197 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 198 |
19 |
▶ | if (q == p || !it || q >= end || *q != '$') return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 199 |
18 |
▶ | if (it < PWD_MIN_ITERATIONS || it > PWD_MAX_ITERATIONS) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 200 |
15 |
| p = q + 1; |
| 201 |
15 |
| q = (const char *)memchr(p, '$', (size_t)(end - p)); |
| 202 |
|
| /* |
| 203 |
|
| # uncoverable branch true |
| 204 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 205 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 206 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 207 |
15 |
▶ | if (!q || q == p || q + 1 >= end) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 208 |
15 |
| n = pwd_b64_decode(p, (size_t)(q - p), salt, saltmax); |
| 209 |
15 |
▶ | if (n <= 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) return 0; |
1 |
14 |
15 |
|
| 210 |
14 |
| *saltlen = (size_t)n; |
| 211 |
14 |
| n = pwd_b64_decode(q + 1, (size_t)(end - q - 1), key, keymax); |
| 212 |
14 |
▶ | if (n != PWD_KEY_LEN) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n != PWD_KEY_LEN) return 0; |
1 |
13 |
14 |
|
| 213 |
13 |
| *keylen = (size_t)n; |
| 214 |
13 |
| *iterations = it; |
| 215 |
13 |
| return 1; |
| 216 |
|
| } |
| 217 |
|
| |
| 218 |
|
| /* Constant-time over the derived key; the length check leaks only the length, |
| 219 |
|
| * which the format fixes anyway. */ |
| 220 |
16 |
| static int pwd_verify(pTHX_ const unsigned char *pass, STRLEN plen, |
| 221 |
|
| const char *stored, STRLEN slen) { |
| 222 |
|
| unsigned char salt[64], want[64], got[64]; |
| 223 |
|
| size_t saltlen, keylen, i; |
| 224 |
|
| U32 iterations; |
| 225 |
16 |
| unsigned char diff = 0; |
| 226 |
16 |
▶ | if (!pwd_parse(stored, slen, &iterations, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pwd_parse(stored, slen, &iterations, |
7 |
9 |
16 |
|
| 227 |
|
| salt, sizeof salt, &saltlen, |
| 228 |
|
| want, sizeof want, &keylen)) |
| 229 |
7 |
| return 0; |
| 230 |
9 |
| pwd_pbkdf2(pass, plen, salt, saltlen, iterations, got, keylen); |
| 231 |
297 |
▶ | for (i = 0; i < keylen; i++) diff |= (unsigned char)(want[i] ^ got[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < keylen; i++) diff |= (unsigned char)(want[i] ^ got[i]); |
288 |
9 |
297 |
|
| 232 |
9 |
| return diff == 0; |
| 233 |
|
| } |
| 234 |
|
| |
| 235 |
|
| /* Burn the same work as a real verify without a stored hash to compare - the |
| 236 |
|
| * unknown-email path of a login must cost what the wrong-password path costs, |
| 237 |
|
| * or response time says which emails exist. */ |
| 238 |
1 |
| static void pwd_dummy_verify(pTHX_ const unsigned char *pass, STRLEN plen, |
| 239 |
|
| U32 iterations) { |
| 240 |
|
| static const unsigned char salt[PWD_SALT_LEN] = "punk.dummy.salt!"; |
| 241 |
|
| unsigned char key[PWD_KEY_LEN]; |
| 242 |
|
| /* |
| 243 |
|
| # uncoverable branch true |
| 244 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 245 |
1 |
▶ | if (!iterations) iterations = PWD_DEFAULT_ITERATIONS; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!iterations) iterations = PWD_DEFAULT_ITERATIONS; |
0 |
1 |
1 |
|
| 246 |
|
| /* |
| 247 |
|
| # uncoverable branch true |
| 248 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 249 |
1 |
▶ | if (iterations < PWD_MIN_ITERATIONS) iterations = PWD_MIN_ITERATIONS; |
Branch: 1 branch
| Branch | True | False | Total |
| if (iterations < PWD_MIN_ITERATIONS) iterations = PWD_MIN_ITERATIONS; |
0 |
1 |
1 |
|
| 250 |
|
| /* |
| 251 |
|
| # uncoverable branch true |
| 252 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 253 |
1 |
▶ | if (iterations > PWD_MAX_ITERATIONS) iterations = PWD_MAX_ITERATIONS; |
Branch: 1 branch
| Branch | True | False | Total |
| if (iterations > PWD_MAX_ITERATIONS) iterations = PWD_MAX_ITERATIONS; |
0 |
1 |
1 |
|
| 254 |
1 |
| pwd_pbkdf2(pass, plen, salt, sizeof salt, iterations, key, sizeof key); |
| 255 |
1 |
| } |
| 256 |
|
| |
| 257 |
6 |
| static int pwd_needs_rehash(pTHX_ const char *stored, STRLEN slen, |
| 258 |
|
| U32 current) { |
| 259 |
|
| unsigned char salt[64], key[64]; |
| 260 |
|
| size_t saltlen, keylen; |
| 261 |
|
| U32 iterations; |
| 262 |
|
| /* |
| 263 |
|
| # uncoverable branch true |
| 264 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 265 |
6 |
▶ | if (!current) current = PWD_DEFAULT_ITERATIONS; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!current) current = PWD_DEFAULT_ITERATIONS; |
0 |
6 |
6 |
|
| 266 |
6 |
▶ | if (!pwd_parse(stored, slen, &iterations, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pwd_parse(stored, slen, &iterations, |
2 |
4 |
6 |
|
| 267 |
|
| salt, sizeof salt, &saltlen, |
| 268 |
|
| key, sizeof key, &keylen)) |
| 269 |
2 |
| return 1; /* fail toward rehash */ |
| 270 |
4 |
| return iterations < current; |
| 271 |
|
| } |
| 272 |
|
| |
| 273 |
|
| /* ---- single-use token material -------------------------------------------- */ |
| 274 |
|
| |
| 275 |
|
| /* 32 random bytes, base64url unpadded - 43 characters, cookie- and URL-safe. */ |
| 276 |
11 |
| static SV *pwd_token(pTHX) { |
| 277 |
|
| unsigned char raw[32]; |
| 278 |
11 |
| pk_random_bytes(aTHX_ raw, sizeof raw); |
| 279 |
11 |
| return pk_b64url(aTHX_ raw, sizeof raw); |
| 280 |
|
| } |
| 281 |
|
| |
| 282 |
|
| /* Lowercase sha256 hex of the token - the only form storage ever sees. */ |
| 283 |
23 |
| static SV *pwd_token_digest(pTHX_ const unsigned char *tok, STRLEN len) { |
| 284 |
|
| static const char hex[] = "0123456789abcdef"; |
| 285 |
|
| unsigned char d[32]; |
| 286 |
|
| char out[64]; |
| 287 |
|
| int i; |
| 288 |
23 |
| pk_sha256(tok, len, d); |
| 289 |
759 |
▶ | for (i = 0; i < 32; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 32; i++) { |
736 |
23 |
759 |
|
| 290 |
736 |
| out[i * 2] = hex[d[i] >> 4]; |
| 291 |
736 |
| out[i * 2 + 1] = hex[d[i] & 15]; |
| 292 |
|
| } |
| 293 |
23 |
| return newSVpvn(out, 64); |
| 294 |
|
| } |
| 295 |
|
| |
| 296 |
|
| #endif /* PUNK_PASSWORD_H */ |