yescrypt-common.c

filter: Statementstmt 37.5% 150 / 400 Branchbran 32.2% 91 / 282 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 35.3% 241 / 682 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Crypt/Passwd/XS.pm ↑ Summary yescrypt-opt.c »
1 /*-
2 * Copyright 2013-2018 Alexander Peslyak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
9 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
11 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
12 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
13 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
14 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
16 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
18 * SUCH DAMAGE.
19 */
20  
21 /* cpx: rename external symbols (see cpx_yescrypt_names.h) */
22 #include "cpx_yescrypt_names.h"
23  
24 #include <stdint.h>
25 #include <string.h>
26  
27 #include "insecure_memzero.h"
28 #include "sha256.h"
29  
30 #define YESCRYPT_INTERNAL
31 #include "yescrypt.h"
32  
33 #define BYTES2CHARS(bytes) ((((bytes) * 8) + 5) / 6)
34  
35 #define HASH_SIZE sizeof(yescrypt_binary_t) /* bytes */
36 #define HASH_LEN BYTES2CHARS(HASH_SIZE) /* base-64 chars */
37  
38 /*
39 * "$y$", up to 8 params of up to 6 chars each, '$', salt
40 * Alternatively, but that's smaller:
41 * "$7$", 3 params encoded as 1+5+5 chars, salt
42 */
43 #define PREFIX_LEN (3 + 8 * 6 + 1 + BYTES2CHARS(32))
44  
45 static const char * const itoa64 =
46 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
47  
48 static const uint8_t atoi64_partial[77] = {
49 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
50 64, 64, 64, 64, 64, 64, 64,
51 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
52 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
53 64, 64, 64, 64, 64, 64,
54 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
55 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
56 };
57  
58 0 static uint8_t *encode64_uint32(uint8_t *dst, size_t dstlen,
59 uint32_t src, uint32_t min)
60 {
61 0 uint32_t start = 0, end = 47, chars = 1, bits = 0;
62  
63 0 ▶ if (src < min)
Branch: 1 branch
BranchTrueFalseTotal
if (src < min) 0 0 0
64 0 return NULL;
65 0 src -= min;
66  
67 0 do {
68 0 uint32_t count = (end + 1 - start) << bits;
69 0 ▶ if (src < count)
Branch: 1 branch
BranchTrueFalseTotal
if (src < count) 0 0 0
70 0 break;
71 0 ▶ if (start >= 63)
Branch: 1 branch
BranchTrueFalseTotal
if (start >= 63) 0 0 0
72 0 return NULL;
73 0 start = end + 1;
74 0 end = start + (62 - end) / 2;
75 0 src -= count;
76 0 chars++;
77 0 bits += 6;
78 } while (1);
79  
80 0 ▶ if (dstlen <= chars) /* require room for a NUL terminator */
Branch: 1 branch
BranchTrueFalseTotal
if (dstlen <= chars) /* require room for a NUL terminator */ 0 0 0
81 0 return NULL;
82  
83 0 *dst++ = itoa64[start + (src >> bits)];
84  
85 0 ▶ while (--chars) {
Branch: 1 branch
BranchTrueFalseTotal
while (--chars) { 0 0 0
86 0 bits -= 6;
87 0 *dst++ = itoa64[(src >> bits) & 0x3f];
88 }
89  
90 0 *dst = 0; /* NUL terminate just in case */
91  
92 0 return dst;
93 }
94  
95 399 static inline uint32_t atoi64(uint8_t src)
96 {
97 399 ▶ if (src >= '.' && src <= 'z')
Branch: 2 branches
BranchTrueFalseTotal
if (src >= '.' && src <= 'z') 389 10 399
if (src >= '.' && src <= 'z') 389 0 389
98 389 return atoi64_partial[src - '.'];
99  
100 10 return 64;
101 }
102  
103 118 static const uint8_t *decode64_uint32(uint32_t *dst,
104 const uint8_t *src, uint32_t min)
105 {
106 118 uint32_t start = 0, end = 47, chars = 1, bits = 0;
107 uint32_t c;
108  
109 118 c = atoi64(*src++);
110 118 ▶ if (c > 63)
Branch: 1 branch
BranchTrueFalseTotal
if (c > 63) 5 113 118
111 5 goto fail;
112  
113 113 *dst = min;
114 121 ▶ while (c > end) {
Branch: 1 branch
BranchTrueFalseTotal
while (c > end) { 8 113 121
115 8 *dst += (end + 1 - start) << bits;
116 8 start = end + 1;
117 8 end = start + (62 - end) / 2;
118 8 chars++;
119 8 bits += 6;
120 }
121  
122 113 *dst += (c - start) << bits;
123  
124 121 ▶ while (--chars) {
Branch: 1 branch
BranchTrueFalseTotal
while (--chars) { 8 113 121
125 8 c = atoi64(*src++);
126 8 ▶ if (c > 63)
Branch: 1 branch
BranchTrueFalseTotal
if (c > 63) 0 8 8
127 0 goto fail;
128 8 bits -= 6;
129 8 *dst += c << bits;
130 }
131  
132 113 return src;
133  
134 5 fail:
135 5 *dst = 0;
136 5 return NULL;
137 }
138  
139 220 static uint8_t *encode64_uint32_fixed(uint8_t *dst, size_t dstlen,
140 uint32_t src, uint32_t srcbits)
141 {
142 uint32_t bits;
143  
144 1080 ▶ for (bits = 0; bits < srcbits; bits += 6) {
Branch: 1 branch
BranchTrueFalseTotal
for (bits = 0; bits < srcbits; bits += 6) { 860 220 1080
145 860 ▶ if (dstlen < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (dstlen < 2) 0 860 860
146 0 return NULL;
147 860 *dst++ = itoa64[src & 0x3f];
148 860 dstlen--;
149 860 src >>= 6;
150 }
151  
152 220 ▶ if (src || dstlen < 1)
Branch: 2 branches
BranchTrueFalseTotal
if (src || dstlen < 1) 220 0 220
if (src || dstlen < 1) 0 220 220
153 0 return NULL;
154  
155 220 *dst = 0; /* NUL terminate just in case */
156  
157 220 return dst;
158 }
159  
160 20 static uint8_t *encode64(uint8_t *dst, size_t dstlen,
161 const uint8_t *src, size_t srclen)
162 {
163 size_t i;
164  
165 240 ▶ for (i = 0; i < srclen; ) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < srclen; ) { 220 20 240
166 uint8_t *dnext;
167 220 uint32_t value = 0, bits = 0;
168 do {
169 640 value |= (uint32_t)src[i++] << bits;
170 640 bits += 8;
171 640 ▶ } while (bits < 24 && i < srclen);
Branch: 2 branches
BranchTrueFalseTotal
} while (bits < 24 && i < srclen); 440 200 640
} while (bits < 24 && i < srclen); 420 20 440
172 220 dnext = encode64_uint32_fixed(dst, dstlen, value, bits);
173 220 ▶ if (!dnext)
Branch: 1 branch
BranchTrueFalseTotal
if (!dnext) 0 220 220
174 0 return NULL;
175 220 dstlen -= dnext - dst;
176 220 dst = dnext;
177 }
178  
179 20 ▶ if (dstlen < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (dstlen < 1) 0 20 20
180 0 return NULL;
181  
182 20 *dst = 0; /* NUL terminate just in case */
183  
184 20 return dst;
185 }
186  
187 0 static const uint8_t *decode64_uint32_fixed(uint32_t *dst, uint32_t dstbits,
188 const uint8_t *src)
189 {
190 uint32_t bits;
191  
192 0 *dst = 0;
193 0 ▶ for (bits = 0; bits < dstbits; bits += 6) {
Branch: 1 branch
BranchTrueFalseTotal
for (bits = 0; bits < dstbits; bits += 6) { 0 0 0
194 0 uint32_t c = atoi64(*src++);
195 0 ▶ if (c > 63) {
Branch: 1 branch
BranchTrueFalseTotal
if (c > 63) { 0 0 0
196 0 *dst = 0;
197 0 return NULL;
198 }
199 0 *dst |= c << bits;
200 }
201  
202 0 return src;
203 }
204  
205 31 static const uint8_t *decode64(uint8_t *dst, size_t *dstlen,
206 const uint8_t *src, size_t srclen)
207 {
208 31 size_t dstpos = 0;
209  
210 99 ▶ while (dstpos <= *dstlen && srclen) {
Branch: 2 branches
BranchTrueFalseTotal
while (dstpos <= *dstlen && srclen) { 99 0 99
while (dstpos <= *dstlen && srclen) { 73 26 99
211 73 uint32_t value = 0, bits = 0;
212 276 ▶ while (srclen--) {
Branch: 1 branch
BranchTrueFalseTotal
while (srclen--) { 273 3 276
213 273 uint32_t c = atoi64(*src);
214 273 ▶ if (c > 63) {
Branch: 1 branch
BranchTrueFalseTotal
if (c > 63) { 5 268 273
215 5 srclen = 0;
216 5 break;
217 }
218 268 src++;
219 268 value |= c << bits;
220 268 bits += 6;
221 268 ▶ if (bits >= 24)
Branch: 1 branch
BranchTrueFalseTotal
if (bits >= 24) 65 203 268
222 65 break;
223 }
224 73 ▶ if (!bits)
Branch: 1 branch
BranchTrueFalseTotal
if (!bits) 4 69 73
225 4 break;
226 69 ▶ if (bits < 12) /* must have at least one full byte */
Branch: 1 branch
BranchTrueFalseTotal
if (bits < 12) /* must have at least one full byte */ 0 69 69
227 0 goto fail;
228 199 ▶ while (dstpos++ < *dstlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (dstpos++ < *dstlen) { 199 0 199
229 199 *dst++ = value;
230 199 value >>= 8;
231 199 bits -= 8;
232 199 ▶ if (bits < 8) { /* 2 or 4 */
Branch: 1 branch
BranchTrueFalseTotal
if (bits < 8) { /* 2 or 4 */ 69 130 199
233 69 ▶ if (value) /* must be 0 */
Branch: 1 branch
BranchTrueFalseTotal
if (value) /* must be 0 */ 1 68 69
234 1 goto fail;
235 68 bits = 0;
236 68 break;
237 }
238 }
239 68 ▶ if (bits)
Branch: 1 branch
BranchTrueFalseTotal
if (bits) 0 68 68
240 0 goto fail;
241 }
242  
243 30 ▶ if (!srclen && dstpos <= *dstlen) {
Branch: 2 branches
BranchTrueFalseTotal
if (!srclen && dstpos <= *dstlen) { 30 0 30
if (!srclen && dstpos <= *dstlen) { 0 30 30
244 30 *dstlen = dstpos;
245 30 return src;
246 }
247  
248 0 fail:
249 1 *dstlen = 0;
250 1 return NULL;
251 }
252  
253 typedef enum { ENC = 1, DEC = -1 } encrypt_dir_t;
254  
255 0 static void memxor(unsigned char *dst, unsigned char *src, size_t size)
256 {
257 0 ▶ while (size--)
Branch: 1 branch
BranchTrueFalseTotal
while (size--) 0 0 0
258 0 *dst++ ^= *src++;
259 0 }
260  
261 0 static void encrypt(unsigned char *data, size_t datalen,
262 const yescrypt_binary_t *key, encrypt_dir_t dir)
263 {
264 SHA256_CTX ctx;
265 unsigned char f[32 + 4];
266 size_t halflen, which;
267 unsigned char mask, round, target;
268  
269 0 ▶ if (!datalen)
Branch: 1 branch
BranchTrueFalseTotal
if (!datalen) 0 0 0
270 0 return;
271 0 ▶ if (datalen > 64)
Branch: 1 branch
BranchTrueFalseTotal
if (datalen > 64) 0 0 0
272 0 datalen = 64;
273  
274 0 halflen = datalen >> 1;
275  
276 0 which = 0; /* offset to half we are working on (0 or halflen) */
277 0 mask = 0x0f; /* current half's extra nibble mask if datalen is odd */
278  
279 0 round = 0;
280 0 target = 5; /* 6 rounds due to Jacques Patarin's CRYPTO 2004 paper */
281  
282 0 ▶ if (dir == DEC) {
Branch: 1 branch
BranchTrueFalseTotal
if (dir == DEC) { 0 0 0
283 0 which = halflen; /* even round count, so swap the halves */
284 0 mask ^= 0xff;
285  
286 0 round = target;
287 0 target = 0;
288 }
289  
290 0 f[32] = 0;
291 0 f[33] = sizeof(*key);
292 0 f[34] = datalen;
293  
294 do {
295 0 SHA256_Init(&ctx);
296 0 f[35] = round;
297 0 SHA256_Update(&ctx, &f[32], 4);
298 0 SHA256_Update(&ctx, key, sizeof(*key));
299 0 SHA256_Update(&ctx, &data[which], halflen);
300 0 ▶ if (datalen & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (datalen & 1) { 0 0 0
301 0 f[0] = data[datalen - 1] & mask;
302 0 SHA256_Update(&ctx, f, 1);
303 }
304 0 SHA256_Final(f, &ctx);
305 0 which ^= halflen;
306 0 memxor(&data[which], f, halflen);
307 0 ▶ if (datalen & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (datalen & 1) { 0 0 0
308 0 mask ^= 0xff;
309 0 data[datalen - 1] ^= f[halflen] & mask;
310 }
311 0 ▶ if (round == target)
Branch: 1 branch
BranchTrueFalseTotal
if (round == target) 0 0 0
312 0 break;
313 0 round += dir;
314 } while (1);
315  
316 /* ctx is presumably zeroized by SHA256_Final() */
317 0 insecure_memzero(f, sizeof(f));
318 }
319  
320 36 uint8_t *yescrypt_r(const yescrypt_shared_t *shared, yescrypt_local_t *local,
321 const uint8_t *passwd, size_t passwdlen,
322 const uint8_t *setting,
323 const yescrypt_binary_t *key,
324 uint8_t *buf, size_t buflen)
325 {
326 unsigned char saltbin[64], hashbin[32];
327 const uint8_t *src, *saltstr, *salt;
328 uint8_t *dst;
329 size_t need, prefixlen, saltstrlen, saltlen;
330 36 yescrypt_params_t params = { .p = 1 };
331  
332 36 ▶ if (setting[0] != '$' ||
Branch: 1 branch
BranchTrueFalseTotal
if (setting[0] != '$' || 36 0 36
333 36 ▶ (setting[1] != '7' && setting[1] != 'y') ||
Branch: 2 branches
BranchTrueFalseTotal
(setting[1] != '7' && setting[1] != 'y') || 36 0 36
(setting[1] != '7' && setting[1] != 'y') || 36 0 36
334 36 ▶ setting[2] != '$')
Branch: 1 branch
BranchTrueFalseTotal
setting[2] != '$') 0 36 36
335 0 return NULL;
336 36 src = setting + 3;
337  
338 36 ▶ if (setting[1] == '7') {
Branch: 1 branch
BranchTrueFalseTotal
if (setting[1] == '7') { 0 36 36
339 0 uint32_t N_log2 = atoi64(*src++);
340 0 ▶ if (N_log2 < 1 || N_log2 > 63)
Branch: 2 branches
BranchTrueFalseTotal
if (N_log2 < 1 || N_log2 > 63) 0 0 0
if (N_log2 < 1 || N_log2 > 63) 0 0 0
341 0 return NULL;
342 0 params.N = (uint64_t)1 << N_log2;
343  
344 0 src = decode64_uint32_fixed(&params.r, 30, src);
345 0 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 0 0 0
346 0 return NULL;
347  
348 0 src = decode64_uint32_fixed(&params.p, 30, src);
349 0 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 0 0 0
350 0 return NULL;
351  
352 0 ▶ if (key)
Branch: 1 branch
BranchTrueFalseTotal
if (key) 0 0 0
353 0 return NULL;
354 } else {
355 uint32_t flavor, N_log2;
356  
357 36 src = decode64_uint32(&flavor, src, 0);
358 36 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 3 33 36
359 5 return NULL;
360  
361 33 ▶ if (flavor < YESCRYPT_RW) {
Branch: 1 branch
BranchTrueFalseTotal
if (flavor < YESCRYPT_RW) { 2 31 33
362 2 params.flags = flavor;
363 31 ▶ } else if (flavor <= YESCRYPT_RW + (YESCRYPT_RW_FLAVOR_MASK >> 2)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (flavor <= YESCRYPT_RW + (YESCRYPT_RW_FLAVOR_MASK >> 2)) { 31 0 31
364 31 params.flags = YESCRYPT_RW + ((flavor - YESCRYPT_RW) << 2);
365 } else {
366 0 return NULL;
367 }
368  
369 33 src = decode64_uint32(&N_log2, src, 1);
370 33 ▶ if (!src || N_log2 > 63)
Branch: 2 branches
BranchTrueFalseTotal
if (!src || N_log2 > 63) 32 1 33
if (!src || N_log2 > 63) 0 32 32
371 1 return NULL;
372 32 params.N = (uint64_t)1 << N_log2;
373  
374 32 src = decode64_uint32(&params.r, src, 1);
375 32 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 0 32 32
376 0 return NULL;
377  
378 32 ▶ if (*src != '$') {
Branch: 1 branch
BranchTrueFalseTotal
if (*src != '$') { 9 23 32
379 uint32_t have;
380  
381 9 src = decode64_uint32(&have, src, 1);
382 9 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 1 8 9
383 1 return NULL;
384  
385 8 ▶ if (have & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (have & 1) { 1 7 8
386 1 src = decode64_uint32(&params.p, src, 2);
387 1 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 0 1 1
388 0 return NULL;
389 }
390  
391 8 ▶ if (have & 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (have & 2) { 5 3 8
392 5 src = decode64_uint32(&params.t, src, 1);
393 5 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 0 5 5
394 0 return NULL;
395 }
396  
397 8 ▶ if (have & 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (have & 4) { 1 7 8
398 1 src = decode64_uint32(&params.g, src, 1);
399 1 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 0 1 1
400 0 return NULL;
401 }
402  
403 8 ▶ if (have & 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (have & 8) { 1 7 8
404 uint32_t NROM_log2;
405 1 src = decode64_uint32(&NROM_log2, src, 1);
406 1 ▶ if (!src || NROM_log2 > 63)
Branch: 2 branches
BranchTrueFalseTotal
if (!src || NROM_log2 > 63) 1 0 1
if (!src || NROM_log2 > 63) 0 1 1
407 0 return NULL;
408 1 params.NROM = (uint64_t)1 << NROM_log2;
409 }
410 }
411  
412 31 ▶ if (*src++ != '$')
Branch: 1 branch
BranchTrueFalseTotal
if (*src++ != '$') 0 31 31
413 0 return NULL;
414 }
415  
416 31 prefixlen = src - setting;
417  
418 31 saltstr = src;
419 31 src = (uint8_t *)strrchr((char *)saltstr, '$');
420 31 ▶ if (src)
Branch: 1 branch
BranchTrueFalseTotal
if (src) 4 27 31
421 4 saltstrlen = src - saltstr;
422 else
423 27 saltstrlen = strlen((char *)saltstr);
424  
425 31 ▶ if (setting[1] == '7') {
Branch: 1 branch
BranchTrueFalseTotal
if (setting[1] == '7') { 0 31 31
426 0 salt = saltstr;
427 0 saltlen = saltstrlen;
428 } else {
429 const uint8_t *saltend;
430  
431 31 saltlen = sizeof(saltbin);
432 31 saltend = decode64(saltbin, &saltlen, saltstr, saltstrlen);
433  
434 31 ▶ if (!saltend || (size_t)(saltend - saltstr) != saltstrlen)
Branch: 2 branches
BranchTrueFalseTotal
if (!saltend || (size_t)(saltend - saltstr) != saltstrlen) 30 1 31
if (!saltend || (size_t)(saltend - saltstr) != saltstrlen) 1 29 30
435 2 goto fail;
436  
437 29 salt = saltbin;
438  
439 29 ▶ if (key)
Branch: 1 branch
BranchTrueFalseTotal
if (key) 0 29 29
440 0 encrypt(saltbin, saltlen, key, ENC);
441 }
442  
443 29 need = prefixlen + saltstrlen + 1 + HASH_LEN + 1;
444 29 ▶ if (need > buflen || need < saltstrlen)
Branch: 2 branches
BranchTrueFalseTotal
if (need > buflen || need < saltstrlen) 29 0 29
if (need > buflen || need < saltstrlen) 0 29 29
445 0 goto fail;
446  
447 /* cpx: refuse settings whose cost we do not support, before allocating */
448 29 ▶ if (cpx_yescrypt_check_params(&params))
Branch: 1 branch
BranchTrueFalseTotal
if (cpx_yescrypt_check_params(&params)) 9 20 29
449 9 goto fail;
450  
451 20 ▶ if (yescrypt_kdf(shared, local, passwd, passwdlen, salt, saltlen,
Branch: 1 branch
BranchTrueFalseTotal
if (yescrypt_kdf(shared, local, passwd, passwdlen, salt, saltlen, 0 20 20
452 &params, hashbin, sizeof(hashbin)))
453 0 goto fail;
454  
455 20 ▶ if (key) {
Branch: 1 branch
BranchTrueFalseTotal
if (key) { 0 20 20
456 0 insecure_memzero(saltbin, sizeof(saltbin));
457 0 encrypt(hashbin, sizeof(hashbin), key, ENC);
458 }
459  
460 20 dst = buf;
461 20 memcpy(dst, setting, prefixlen + saltstrlen);
462 20 dst += prefixlen + saltstrlen;
463 20 *dst++ = '$';
464  
465 20 dst = encode64(dst, buflen - (dst - buf), hashbin, sizeof(hashbin));
466 20 insecure_memzero(hashbin, sizeof(hashbin));
467 20 ▶ if (!dst || dst >= buf + buflen)
Branch: 2 branches
BranchTrueFalseTotal
if (!dst || dst >= buf + buflen) 20 0 20
if (!dst || dst >= buf + buflen) 0 20 20
468 0 return NULL;
469  
470 20 *dst = 0; /* NUL termination */
471  
472 20 return buf;
473  
474 11 fail:
475 11 insecure_memzero(saltbin, sizeof(saltbin));
476 11 insecure_memzero(hashbin, sizeof(hashbin));
477 11 return NULL;
478 }
479  
480 0 uint8_t *yescrypt(const uint8_t *passwd, const uint8_t *setting)
481 {
482 /* prefix, '$', hash, NUL */
483 static uint8_t buf[PREFIX_LEN + 1 + HASH_LEN + 1];
484 yescrypt_local_t local;
485 uint8_t *retval;
486  
487 0 ▶ if (yescrypt_init_local(&local))
Branch: 1 branch
BranchTrueFalseTotal
if (yescrypt_init_local(&local)) 0 0 0
488 0 return NULL;
489 0 retval = yescrypt_r(NULL, &local,
490 passwd, strlen((char *)passwd), setting, NULL, buf, sizeof(buf));
491 0 ▶ if (yescrypt_free_local(&local))
Branch: 1 branch
BranchTrueFalseTotal
if (yescrypt_free_local(&local)) 0 0 0
492 0 return NULL;
493 0 return retval;
494 }
495  
496 0 uint8_t *yescrypt_reencrypt(uint8_t *hash,
497 const yescrypt_binary_t *from_key,
498 const yescrypt_binary_t *to_key)
499 {
500 0 uint8_t *retval = NULL, *saltstart, *hashstart;
501 const uint8_t *hashend;
502 unsigned char saltbin[64], hashbin[32];
503 size_t saltstrlen, saltlen, hashlen;
504  
505 0 ▶ if (strncmp((char *)hash, "$y$", 3))
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp((char *)hash, "$y$", 3)) 0 0 0
506 0 return NULL;
507  
508 0 saltstart = NULL;
509 0 hashstart = (uint8_t *)strrchr((char *)hash, '$');
510 0 ▶ if (hashstart) {
Branch: 1 branch
BranchTrueFalseTotal
if (hashstart) { 0 0 0
511 0 ▶ if (hashstart > (uint8_t *)hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (hashstart > (uint8_t *)hash) { 0 0 0
512 0 saltstart = hashstart - 1;
513 0 ▶ while (*saltstart != '$' && saltstart > hash)
Branch: 2 branches
BranchTrueFalseTotal
while (*saltstart != '$' && saltstart > hash) 0 0 0
while (*saltstart != '$' && saltstart > hash) 0 0 0
514 0 saltstart--;
515 0 ▶ if (*saltstart == '$')
Branch: 1 branch
BranchTrueFalseTotal
if (*saltstart == '$') 0 0 0
516 0 saltstart++;
517 }
518 0 hashstart++;
519 } else {
520 0 hashstart = hash;
521 }
522 0 ▶ saltstrlen = saltstart ? (hashstart - 1 - saltstart) : 0;
Branch: 1 branch
BranchTrueFalseTotal
saltstrlen = saltstart ? (hashstart - 1 - saltstart) : 0; 0 0 0
523 0 ▶ if (saltstrlen > BYTES2CHARS(64) ||
Branch: 1 branch
BranchTrueFalseTotal
if (saltstrlen > BYTES2CHARS(64) || 0 0 0
524 0 ▶ strlen((char *)hashstart) != HASH_LEN)
Branch: 1 branch
BranchTrueFalseTotal
strlen((char *)hashstart) != HASH_LEN) 0 0 0
525 0 return NULL;
526  
527 0 ▶ if (saltstrlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (saltstrlen) { 0 0 0
528 const uint8_t *saltend;
529 0 saltlen = sizeof(saltbin);
530 0 saltend = decode64(saltbin, &saltlen, saltstart, saltstrlen);
531 0 ▶ if (!saltend || *saltend != '$' || saltlen < 1 || saltlen > 64)
Branch: 4 branches
BranchTrueFalseTotal
if (!saltend || *saltend != '$' || saltlen < 1 || saltlen > 64) 0 0 0
if (!saltend || *saltend != '$' || saltlen < 1 || saltlen > 64) 0 0 0
if (!saltend || *saltend != '$' || saltlen < 1 || saltlen > 64) 0 0 0
if (!saltend || *saltend != '$' || saltlen < 1 || saltlen > 64) 0 0 0
532 0 goto out;
533  
534 0 ▶ if (from_key)
Branch: 1 branch
BranchTrueFalseTotal
if (from_key) 0 0 0
535 0 encrypt(saltbin, saltlen, from_key, ENC);
536 0 ▶ if (to_key)
Branch: 1 branch
BranchTrueFalseTotal
if (to_key) 0 0 0
537 0 encrypt(saltbin, saltlen, to_key, DEC);
538 }
539  
540 0 hashlen = sizeof(hashbin);
541 0 hashend = decode64(hashbin, &hashlen, hashstart, HASH_LEN);
542 0 ▶ if (!hashend || *hashend || hashlen != sizeof(hashbin))
Branch: 3 branches
BranchTrueFalseTotal
if (!hashend || *hashend || hashlen != sizeof(hashbin)) 0 0 0
if (!hashend || *hashend || hashlen != sizeof(hashbin)) 0 0 0
if (!hashend || *hashend || hashlen != sizeof(hashbin)) 0 0 0
543 0 goto out;
544  
545 0 ▶ if (from_key)
Branch: 1 branch
BranchTrueFalseTotal
if (from_key) 0 0 0
546 0 encrypt(hashbin, hashlen, from_key, DEC);
547 0 ▶ if (to_key)
Branch: 1 branch
BranchTrueFalseTotal
if (to_key) 0 0 0
548 0 encrypt(hashbin, hashlen, to_key, ENC);
549  
550 0 ▶ if (saltstrlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (saltstrlen) { 0 0 0
551 0 ▶ if (!encode64(saltstart, saltstrlen + 1, saltbin, saltlen))
Branch: 1 branch
BranchTrueFalseTotal
if (!encode64(saltstart, saltstrlen + 1, saltbin, saltlen)) 0 0 0
552 0 goto out; /* can't happen */
553 0 *(saltstart + saltstrlen) = '$';
554 }
555  
556 0 ▶ if (!encode64(hashstart, HASH_LEN + 1, hashbin, hashlen))
Branch: 1 branch
BranchTrueFalseTotal
if (!encode64(hashstart, HASH_LEN + 1, hashbin, hashlen)) 0 0 0
557 0 goto out; /* can't happen */
558  
559 0 retval = hash;
560  
561 0 out:
562 0 insecure_memzero(saltbin, sizeof(saltbin));
563 0 insecure_memzero(hashbin, sizeof(hashbin));
564  
565 0 return retval;
566 }
567  
568 0 static uint32_t N2log2(uint64_t N)
569 {
570 uint32_t N_log2;
571  
572 0 ▶ if (N < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (N < 2) 0 0 0
573 0 return 0;
574  
575 0 N_log2 = 2;
576 0 ▶ while (N >> N_log2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
while (N >> N_log2 != 0) 0 0 0
577 0 N_log2++;
578 0 N_log2--;
579  
580 0 ▶ if (N >> N_log2 != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (N >> N_log2 != 1) 0 0 0
581 0 return 0;
582  
583 0 return N_log2;
584 }
585  
586 0 uint8_t *yescrypt_encode_params_r(const yescrypt_params_t *params,
587 const uint8_t *src, size_t srclen,
588 uint8_t *buf, size_t buflen)
589 {
590 uint32_t flavor, N_log2, NROM_log2, have;
591 uint8_t *dst;
592  
593 0 ▶ if (srclen > SIZE_MAX / 16)
Branch: 1 branch
BranchTrueFalseTotal
if (srclen > SIZE_MAX / 16) 0 0 0
594 0 return NULL;
595  
596 0 ▶ if (params->flags < YESCRYPT_RW) {
Branch: 1 branch
BranchTrueFalseTotal
if (params->flags < YESCRYPT_RW) { 0 0 0
597 0 flavor = params->flags;
598 0 ▶ } else if ((params->flags & YESCRYPT_MODE_MASK) == YESCRYPT_RW &&
Branch: 1 branch
BranchTrueFalseTotal
} else if ((params->flags & YESCRYPT_MODE_MASK) == YESCRYPT_RW && 0 0 0
599 0 ▶ params->flags <= (YESCRYPT_RW | YESCRYPT_RW_FLAVOR_MASK)) {
Branch: 1 branch
BranchTrueFalseTotal
params->flags <= (YESCRYPT_RW | YESCRYPT_RW_FLAVOR_MASK)) { 0 0 0
600 0 flavor = YESCRYPT_RW + (params->flags >> 2);
601 } else {
602 0 return NULL;
603 }
604  
605 0 N_log2 = N2log2(params->N);
606 0 ▶ if (!N_log2)
Branch: 1 branch
BranchTrueFalseTotal
if (!N_log2) 0 0 0
607 0 return NULL;
608  
609 0 NROM_log2 = N2log2(params->NROM);
610 0 ▶ if (params->NROM && !NROM_log2)
Branch: 2 branches
BranchTrueFalseTotal
if (params->NROM && !NROM_log2) 0 0 0
if (params->NROM && !NROM_log2) 0 0 0
611 0 return NULL;
612  
613 0 ▶ if ((uint64_t)params->r * (uint64_t)params->p >= (1U << 30))
Branch: 1 branch
BranchTrueFalseTotal
if ((uint64_t)params->r * (uint64_t)params->p >= (1U << 30)) 0 0 0
614 0 return NULL;
615  
616 0 dst = buf;
617 0 *dst++ = '$';
618 0 *dst++ = 'y';
619 0 *dst++ = '$';
620  
621 0 dst = encode64_uint32(dst, buflen - (dst - buf), flavor, 0);
622 0 ▶ if (!dst)
Branch: 1 branch
BranchTrueFalseTotal
if (!dst) 0 0 0
623 0 return NULL;
624  
625 0 dst = encode64_uint32(dst, buflen - (dst - buf), N_log2, 1);
626 0 ▶ if (!dst)
Branch: 1 branch
BranchTrueFalseTotal
if (!dst) 0 0 0
627 0 return NULL;
628  
629 0 dst = encode64_uint32(dst, buflen - (dst - buf), params->r, 1);
630 0 ▶ if (!dst)
Branch: 1 branch
BranchTrueFalseTotal
if (!dst) 0 0 0
631 0 return NULL;
632  
633 0 have = 0;
634 0 ▶ if (params->p != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (params->p != 1) 0 0 0
635 0 have |= 1;
636 0 ▶ if (params->t)
Branch: 1 branch
BranchTrueFalseTotal
if (params->t) 0 0 0
637 0 have |= 2;
638 0 ▶ if (params->g)
Branch: 1 branch
BranchTrueFalseTotal
if (params->g) 0 0 0
639 0 have |= 4;
640 0 ▶ if (NROM_log2)
Branch: 1 branch
BranchTrueFalseTotal
if (NROM_log2) 0 0 0
641 0 have |= 8;
642  
643 0 ▶ if (have) {
Branch: 1 branch
BranchTrueFalseTotal
if (have) { 0 0 0
644 0 dst = encode64_uint32(dst, buflen - (dst - buf), have, 1);
645 0 ▶ if (!dst)
Branch: 1 branch
BranchTrueFalseTotal
if (!dst) 0 0 0
646 0 return NULL;
647 }
648  
649 0 ▶ if (params->p != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (params->p != 1) { 0 0 0
650 0 dst = encode64_uint32(dst, buflen - (dst - buf), params->p, 2);
651 0 ▶ if (!dst)
Branch: 1 branch
BranchTrueFalseTotal
if (!dst) 0 0 0
652 0 return NULL;
653 }
654  
655 0 ▶ if (params->t) {
Branch: 1 branch
BranchTrueFalseTotal
if (params->t) { 0 0 0
656 0 dst = encode64_uint32(dst, buflen - (dst - buf), params->t, 1);
657 0 ▶ if (!dst)
Branch: 1 branch
BranchTrueFalseTotal
if (!dst) 0 0 0
658 0 return NULL;
659 }
660  
661 0 ▶ if (params->g) {
Branch: 1 branch
BranchTrueFalseTotal
if (params->g) { 0 0 0
662 0 dst = encode64_uint32(dst, buflen - (dst - buf), params->g, 1);
663 0 ▶ if (!dst)
Branch: 1 branch
BranchTrueFalseTotal
if (!dst) 0 0 0
664 0 return NULL;
665 }
666  
667 0 ▶ if (NROM_log2) {
Branch: 1 branch
BranchTrueFalseTotal
if (NROM_log2) { 0 0 0
668 0 dst = encode64_uint32(dst, buflen - (dst - buf), NROM_log2, 1);
669 0 ▶ if (!dst)
Branch: 1 branch
BranchTrueFalseTotal
if (!dst) 0 0 0
670 0 return NULL;
671 }
672  
673 0 ▶ if (dst >= buf + buflen)
Branch: 1 branch
BranchTrueFalseTotal
if (dst >= buf + buflen) 0 0 0
674 0 return NULL;
675  
676 0 *dst++ = '$';
677  
678 0 dst = encode64(dst, buflen - (dst - buf), src, srclen);
679 0 ▶ if (!dst || dst >= buf + buflen)
Branch: 2 branches
BranchTrueFalseTotal
if (!dst || dst >= buf + buflen) 0 0 0
if (!dst || dst >= buf + buflen) 0 0 0
680 0 return NULL;
681  
682 0 *dst = 0; /* NUL termination */
683  
684 0 return buf;
685 }
686  
687 0 uint8_t *yescrypt_encode_params(const yescrypt_params_t *params,
688 const uint8_t *src, size_t srclen)
689 {
690 /* prefix, NUL */
691 static uint8_t buf[PREFIX_LEN + 1];
692 0 return yescrypt_encode_params_r(params, src, srclen, buf, sizeof(buf));
693 }
694  
695 0 int crypto_scrypt(const uint8_t *passwd, size_t passwdlen,
696 const uint8_t *salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
697 uint8_t *buf, size_t buflen)
698 {
699 yescrypt_local_t local;
700 0 yescrypt_params_t params = { .flags = 0, .N = N, .r = r, .p = p };
701 int retval;
702  
703 0 ▶ if (yescrypt_init_local(&local))
Branch: 1 branch
BranchTrueFalseTotal
if (yescrypt_init_local(&local)) 0 0 0
704 0 return -1;
705 0 retval = yescrypt_kdf(NULL, &local,
706 passwd, passwdlen, salt, saltlen, &params, buf, buflen);
707 0 ▶ if (yescrypt_free_local(&local))
Branch: 1 branch
BranchTrueFalseTotal
if (yescrypt_free_local(&local)) 0 0 0
708 0 return -1;
709 0 return retval;
710 }
« blib/lib/Crypt/Passwd/XS.pm ↑ Summary yescrypt-opt.c »