| 1 |
|
| /*- |
| 2 |
|
| * Copyright (c) 2026 cPanel, Inc. |
| 3 |
|
| * All rights reserved. |
| 4 |
|
| * |
| 5 |
|
| * This library is free software; you can redistribute it and/or modify |
| 6 |
|
| * it under the same terms as Perl itself, either Perl version 5.10.1 or, |
| 7 |
|
| * at your option, any later version of Perl 5 you may have available. |
| 8 |
|
| * |
| 9 |
|
| * crypt(3)-style glue around the vendored Openwall yescrypt sources. |
| 10 |
|
| */ |
| 11 |
|
| |
| 12 |
|
| #include "cpx_yescrypt_names.h" |
| 13 |
|
| |
| 14 |
|
| #include <string.h> |
| 15 |
|
| |
| 16 |
|
| #include "yescrypt.h" |
| 17 |
|
| #include "yescrypt_crypt.h" |
| 18 |
|
| |
| 19 |
|
| /* |
| 20 |
|
| * Budget for one hash: the V array (128 * r * N bytes) times the number of |
| 21 |
|
| * passes over it (t + 1). 1 GiB fits the costliest setting libxcrypt |
| 22 |
|
| * generates, $y$jFT$ (a 1 GiB V, t = 0), and stops a hostile setting such as |
| 23 |
|
| * $y$jLT$ (64 GiB) or a huge t before any work is done. |
| 24 |
|
| */ |
| 25 |
|
| #define CPX_YESCRYPT_MAX_MEM ((uint64_t)1 << 30) |
| 26 |
|
| |
| 27 |
|
| /* |
| 28 |
|
| * The other buffers (B, XY, S-boxes) add 384 * r bytes plus 12 KiB on top of |
| 29 |
|
| * V, and the work outside the V passes also grows with r, so the budget alone |
| 30 |
|
| * does not bound a small-N, huge-r setting such as $y$j/y3vrD$ (N = 4, |
| 31 |
|
| * r = 2^21). libxcrypt never generates r above 32. |
| 32 |
|
| */ |
| 33 |
|
| #define CPX_YESCRYPT_MAX_R 32 |
| 34 |
|
| |
| 35 |
|
| /* |
| 36 |
|
| * Policy check called from yescrypt_r() once the setting is parsed and before |
| 37 |
|
| * any memory is allocated. Returns 0 if the parameters are acceptable. |
| 38 |
|
| */ |
| 39 |
29 |
| int cpx_yescrypt_check_params (const yescrypt_params_t *params) |
| 40 |
|
| { |
| 41 |
|
| /* No ROM, no hash upgrades, no multi-lane mode. */ |
| 42 |
29 |
▶ | if (params->NROM != 0 || params->g != 0 || params->p != 1) |
Branch: 3 branches
| Branch | True | False | Total |
| if (params->NROM != 0 || params->g != 0 || params->p != 1) |
28 |
1 |
29 |
| if (params->NROM != 0 || params->g != 0 || params->p != 1) |
27 |
1 |
28 |
| if (params->NROM != 0 || params->g != 0 || params->p != 1) |
1 |
26 |
27 |
|
| 43 |
3 |
| return -1; |
| 44 |
26 |
▶ | if (params->r == 0 || params->r > CPX_YESCRYPT_MAX_R || params->N < 2) |
Branch: 3 branches
| Branch | True | False | Total |
| if (params->r == 0 || params->r > CPX_YESCRYPT_MAX_R || params->N < 2) |
26 |
0 |
26 |
| if (params->r == 0 || params->r > CPX_YESCRYPT_MAX_R || params->N < 2) |
24 |
2 |
26 |
| if (params->r == 0 || params->r > CPX_YESCRYPT_MAX_R || params->N < 2) |
0 |
24 |
24 |
|
| 45 |
2 |
| return -1; |
| 46 |
|
| |
| 47 |
|
| /* N * 128 * r * (t + 1) <= MAX_MEM, divided out so nothing overflows. */ |
| 48 |
24 |
▶ | if (params->N > CPX_YESCRYPT_MAX_MEM / 128 / params->r / ((uint64_t)params->t + 1)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (params->N > CPX_YESCRYPT_MAX_MEM / 128 / params->r / ((uint64_t)params->t + 1)) |
4 |
20 |
24 |
|
| 49 |
4 |
| return -1; |
| 50 |
|
| |
| 51 |
20 |
| return 0; |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
39 |
| char *cpx_yescrypt_crypt (const char *key, const char *salt, char *buf, size_t buflen) |
| 55 |
|
| { |
| 56 |
|
| yescrypt_local_t local; |
| 57 |
|
| uint8_t *ret; |
| 58 |
|
| |
| 59 |
39 |
▶ | if (buflen == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (buflen == 0) |
0 |
39 |
39 |
|
| 60 |
0 |
| return NULL; |
| 61 |
|
| |
| 62 |
39 |
| buf[0] = '\0'; |
| 63 |
|
| |
| 64 |
|
| /* yescrypt_r() also accepts "$7$" (scrypt); that scheme is not supported. */ |
| 65 |
39 |
▶ | if (strncmp(salt, "$y$", 3) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(salt, "$y$", 3) != 0) |
3 |
36 |
39 |
|
| 66 |
3 |
| return buf; |
| 67 |
|
| |
| 68 |
36 |
▶ | if (yescrypt_init_local(&local)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (yescrypt_init_local(&local)) |
0 |
36 |
36 |
|
| 69 |
0 |
| return buf; |
| 70 |
|
| |
| 71 |
36 |
| ret = yescrypt_r(NULL, &local, |
| 72 |
|
| (const uint8_t *)key, strlen(key), |
| 73 |
|
| (const uint8_t *)salt, NULL, |
| 74 |
|
| (uint8_t *)buf, buflen); |
| 75 |
|
| |
| 76 |
|
| /* The region is freed on every call. */ |
| 77 |
36 |
▶ | if (yescrypt_free_local(&local) || ret == NULL) |
Branch: 2 branches
| Branch | True | False | Total |
| if (yescrypt_free_local(&local) || ret == NULL) |
36 |
0 |
36 |
| if (yescrypt_free_local(&local) || ret == NULL) |
16 |
20 |
36 |
|
| 78 |
16 |
| buf[0] = '\0'; |
| 79 |
|
| |
| 80 |
36 |
| return buf; |
| 81 |
|
| } |