/src/openssl30/providers/implementations/kdfs/scrypt.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2017-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdlib.h> |
11 | | #include <stdarg.h> |
12 | | #include <string.h> |
13 | | #include <openssl/evp.h> |
14 | | #include <openssl/kdf.h> |
15 | | #include <openssl/err.h> |
16 | | #include <openssl/core_names.h> |
17 | | #include <openssl/proverr.h> |
18 | | #include "crypto/evp.h" |
19 | | #include "internal/numbers.h" |
20 | | #include "prov/implementations.h" |
21 | | #include "prov/provider_ctx.h" |
22 | | #include "prov/providercommon.h" |
23 | | #include "prov/implementations.h" |
24 | | |
25 | | #ifndef OPENSSL_NO_SCRYPT |
26 | | |
27 | | static OSSL_FUNC_kdf_newctx_fn kdf_scrypt_new; |
28 | | static OSSL_FUNC_kdf_freectx_fn kdf_scrypt_free; |
29 | | static OSSL_FUNC_kdf_reset_fn kdf_scrypt_reset; |
30 | | static OSSL_FUNC_kdf_derive_fn kdf_scrypt_derive; |
31 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_scrypt_settable_ctx_params; |
32 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_scrypt_set_ctx_params; |
33 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_scrypt_gettable_ctx_params; |
34 | | static OSSL_FUNC_kdf_get_ctx_params_fn kdf_scrypt_get_ctx_params; |
35 | | |
36 | | static int scrypt_alg(const char *pass, size_t passlen, |
37 | | const unsigned char *salt, size_t saltlen, |
38 | | uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem, |
39 | | unsigned char *key, size_t keylen, EVP_MD *sha256, |
40 | | OSSL_LIB_CTX *libctx, const char *propq); |
41 | | |
42 | | typedef struct { |
43 | | OSSL_LIB_CTX *libctx; |
44 | | char *propq; |
45 | | unsigned char *pass; |
46 | | size_t pass_len; |
47 | | unsigned char *salt; |
48 | | size_t salt_len; |
49 | | uint64_t N; |
50 | | uint64_t r, p; |
51 | | uint64_t maxmem_bytes; |
52 | | EVP_MD *sha256; |
53 | | } KDF_SCRYPT; |
54 | | |
55 | | static void kdf_scrypt_init(KDF_SCRYPT *ctx); |
56 | | |
57 | | static void *kdf_scrypt_new(void *provctx) |
58 | 0 | { |
59 | 0 | KDF_SCRYPT *ctx; |
60 | |
|
61 | 0 | if (!ossl_prov_is_running()) |
62 | 0 | return NULL; |
63 | | |
64 | 0 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
65 | 0 | if (ctx == NULL) { |
66 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); |
67 | 0 | return NULL; |
68 | 0 | } |
69 | 0 | ctx->libctx = PROV_LIBCTX_OF(provctx); |
70 | 0 | kdf_scrypt_init(ctx); |
71 | 0 | return ctx; |
72 | 0 | } |
73 | | |
74 | | static void kdf_scrypt_free(void *vctx) |
75 | 30 | { |
76 | 30 | KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx; |
77 | | |
78 | 30 | if (ctx != NULL) { |
79 | 30 | OPENSSL_free(ctx->propq); |
80 | 30 | EVP_MD_free(ctx->sha256); |
81 | 30 | kdf_scrypt_reset(ctx); |
82 | 30 | OPENSSL_free(ctx); |
83 | 30 | } |
84 | 30 | } |
85 | | |
86 | | static void kdf_scrypt_reset(void *vctx) |
87 | 30 | { |
88 | 30 | KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx; |
89 | | |
90 | 30 | OPENSSL_free(ctx->salt); |
91 | 30 | ctx->salt = NULL; |
92 | 30 | OPENSSL_clear_free(ctx->pass, ctx->pass_len); |
93 | 30 | ctx->pass = NULL; |
94 | 30 | kdf_scrypt_init(ctx); |
95 | 30 | } |
96 | | |
97 | | static void kdf_scrypt_init(KDF_SCRYPT *ctx) |
98 | 60 | { |
99 | | /* Default values are the most conservative recommendation given in the |
100 | | * original paper of C. Percival. Derivation uses roughly 1 GiB of memory |
101 | | * for this parameter choice (approx. 128 * r * N * p bytes). |
102 | | */ |
103 | 60 | ctx->N = 1 << 20; |
104 | 60 | ctx->r = 8; |
105 | 60 | ctx->p = 1; |
106 | 60 | ctx->maxmem_bytes = 1025 * 1024 * 1024; |
107 | 60 | } |
108 | | |
109 | | static int scrypt_set_membuf(unsigned char **buffer, size_t *buflen, |
110 | | const OSSL_PARAM *p) |
111 | 60 | { |
112 | 60 | OPENSSL_clear_free(*buffer, *buflen); |
113 | 60 | *buffer = NULL; |
114 | 60 | *buflen = 0; |
115 | | |
116 | 60 | if (p->data_size == 0) { |
117 | 13 | if ((*buffer = OPENSSL_malloc(1)) == NULL) { |
118 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); |
119 | 0 | return 0; |
120 | 0 | } |
121 | 47 | } else if (p->data != NULL) { |
122 | 47 | if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen)) |
123 | 0 | return 0; |
124 | 47 | } |
125 | 60 | return 1; |
126 | 60 | } |
127 | | |
128 | | static int set_digest(KDF_SCRYPT *ctx) |
129 | 0 | { |
130 | 0 | EVP_MD_free(ctx->sha256); |
131 | 0 | ctx->sha256 = EVP_MD_fetch(ctx->libctx, "sha256", ctx->propq); |
132 | 0 | if (ctx->sha256 == NULL) { |
133 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOAD_SHA256); |
134 | 0 | return 0; |
135 | 0 | } |
136 | 0 | return 1; |
137 | 0 | } |
138 | | |
139 | | static int set_property_query(KDF_SCRYPT *ctx, const char *propq) |
140 | 0 | { |
141 | 0 | OPENSSL_free(ctx->propq); |
142 | 0 | ctx->propq = NULL; |
143 | 0 | if (propq != NULL) { |
144 | 0 | ctx->propq = OPENSSL_strdup(propq); |
145 | 0 | if (ctx->propq == NULL) { |
146 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); |
147 | 0 | return 0; |
148 | 0 | } |
149 | 0 | } |
150 | 0 | return 1; |
151 | 0 | } |
152 | | |
153 | | static int kdf_scrypt_derive(void *vctx, unsigned char *key, size_t keylen, |
154 | | const OSSL_PARAM params[]) |
155 | 0 | { |
156 | 0 | KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx; |
157 | |
|
158 | 0 | if (!ossl_prov_is_running() || !kdf_scrypt_set_ctx_params(ctx, params)) |
159 | 0 | return 0; |
160 | | |
161 | 0 | if (ctx->pass == NULL) { |
162 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); |
163 | 0 | return 0; |
164 | 0 | } |
165 | | |
166 | 0 | if (ctx->salt == NULL) { |
167 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT); |
168 | 0 | return 0; |
169 | 0 | } |
170 | | |
171 | 0 | if (ctx->sha256 == NULL && !set_digest(ctx)) |
172 | 0 | return 0; |
173 | | |
174 | 0 | return scrypt_alg((char *)ctx->pass, ctx->pass_len, ctx->salt, |
175 | 0 | ctx->salt_len, ctx->N, ctx->r, ctx->p, |
176 | 0 | ctx->maxmem_bytes, key, keylen, ctx->sha256, |
177 | 0 | ctx->libctx, ctx->propq); |
178 | 0 | } |
179 | | |
180 | | static int is_power_of_two(uint64_t value) |
181 | 0 | { |
182 | 0 | return (value != 0) && ((value & (value - 1)) == 0); |
183 | 0 | } |
184 | | |
185 | | static int kdf_scrypt_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
186 | | { |
187 | | const OSSL_PARAM *p; |
188 | | KDF_SCRYPT *ctx = vctx; |
189 | | uint64_t u64_value; |
190 | | |
191 | | if (params == NULL) |
192 | | return 1; |
193 | | |
194 | | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL) |
195 | | if (!scrypt_set_membuf(&ctx->pass, &ctx->pass_len, p)) |
196 | | return 0; |
197 | | |
198 | | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) |
199 | | if (!scrypt_set_membuf(&ctx->salt, &ctx->salt_len, p)) |
200 | | return 0; |
201 | | |
202 | | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_N)) |
203 | | != NULL) { |
204 | | if (!OSSL_PARAM_get_uint64(p, &u64_value) |
205 | | || u64_value <= 1 |
206 | | || !is_power_of_two(u64_value)) |
207 | | return 0; |
208 | | ctx->N = u64_value; |
209 | | } |
210 | | |
211 | | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_R)) |
212 | | != NULL) { |
213 | | if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1) |
214 | | return 0; |
215 | | ctx->r = u64_value; |
216 | | } |
217 | | |
218 | | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_P)) |
219 | | != NULL) { |
220 | | if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1) |
221 | | return 0; |
222 | | ctx->p = u64_value; |
223 | | } |
224 | | |
225 | | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_MAXMEM)) |
226 | | != NULL) { |
227 | | if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1) |
228 | | return 0; |
229 | | ctx->maxmem_bytes = u64_value; |
230 | | } |
231 | | |
232 | | p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PROPERTIES); |
233 | | if (p != NULL) { |
234 | | if (p->data_type != OSSL_PARAM_UTF8_STRING |
235 | | || !set_property_query(ctx, p->data) |
236 | | || !set_digest(ctx)) |
237 | | return 0; |
238 | | } |
239 | | return 1; |
240 | | } |
241 | | |
242 | | static const OSSL_PARAM *kdf_scrypt_settable_ctx_params(ossl_unused void *ctx, |
243 | | ossl_unused void *p_ctx) |
244 | 34 | { |
245 | 34 | static const OSSL_PARAM known_settable_ctx_params[] = { |
246 | 34 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0), |
247 | 34 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0), |
248 | 34 | OSSL_PARAM_uint64(OSSL_KDF_PARAM_SCRYPT_N, NULL), |
249 | 34 | OSSL_PARAM_uint32(OSSL_KDF_PARAM_SCRYPT_R, NULL), |
250 | 34 | OSSL_PARAM_uint32(OSSL_KDF_PARAM_SCRYPT_P, NULL), |
251 | 34 | OSSL_PARAM_uint64(OSSL_KDF_PARAM_SCRYPT_MAXMEM, NULL), |
252 | 34 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), |
253 | 34 | OSSL_PARAM_END |
254 | 34 | }; |
255 | 34 | return known_settable_ctx_params; |
256 | 34 | } |
257 | | |
258 | | static int kdf_scrypt_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
259 | 0 | { |
260 | 0 | OSSL_PARAM *p; |
261 | |
|
262 | 0 | if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) |
263 | 0 | return OSSL_PARAM_set_size_t(p, SIZE_MAX); |
264 | 0 | return -2; |
265 | 0 | } |
266 | | |
267 | | static const OSSL_PARAM *kdf_scrypt_gettable_ctx_params(ossl_unused void *ctx, |
268 | | ossl_unused void *p_ctx) |
269 | 0 | { |
270 | 0 | static const OSSL_PARAM known_gettable_ctx_params[] = { |
271 | 0 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
272 | 0 | OSSL_PARAM_END |
273 | 0 | }; |
274 | 0 | return known_gettable_ctx_params; |
275 | 0 | } |
276 | | |
277 | | const OSSL_DISPATCH ossl_kdf_scrypt_functions[] = { |
278 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_scrypt_new }, |
279 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_scrypt_free }, |
280 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_scrypt_reset }, |
281 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_scrypt_derive }, |
282 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
283 | | (void(*)(void))kdf_scrypt_settable_ctx_params }, |
284 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_scrypt_set_ctx_params }, |
285 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
286 | | (void(*)(void))kdf_scrypt_gettable_ctx_params }, |
287 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_scrypt_get_ctx_params }, |
288 | | { 0, NULL } |
289 | | }; |
290 | | |
291 | 0 | #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b)))) |
292 | | static void salsa208_word_specification(uint32_t inout[16]) |
293 | 0 | { |
294 | 0 | int i; |
295 | 0 | uint32_t x[16]; |
296 | |
|
297 | 0 | memcpy(x, inout, sizeof(x)); |
298 | 0 | for (i = 8; i > 0; i -= 2) { |
299 | 0 | x[4] ^= R(x[0] + x[12], 7); |
300 | 0 | x[8] ^= R(x[4] + x[0], 9); |
301 | 0 | x[12] ^= R(x[8] + x[4], 13); |
302 | 0 | x[0] ^= R(x[12] + x[8], 18); |
303 | 0 | x[9] ^= R(x[5] + x[1], 7); |
304 | 0 | x[13] ^= R(x[9] + x[5], 9); |
305 | 0 | x[1] ^= R(x[13] + x[9], 13); |
306 | 0 | x[5] ^= R(x[1] + x[13], 18); |
307 | 0 | x[14] ^= R(x[10] + x[6], 7); |
308 | 0 | x[2] ^= R(x[14] + x[10], 9); |
309 | 0 | x[6] ^= R(x[2] + x[14], 13); |
310 | 0 | x[10] ^= R(x[6] + x[2], 18); |
311 | 0 | x[3] ^= R(x[15] + x[11], 7); |
312 | 0 | x[7] ^= R(x[3] + x[15], 9); |
313 | 0 | x[11] ^= R(x[7] + x[3], 13); |
314 | 0 | x[15] ^= R(x[11] + x[7], 18); |
315 | 0 | x[1] ^= R(x[0] + x[3], 7); |
316 | 0 | x[2] ^= R(x[1] + x[0], 9); |
317 | 0 | x[3] ^= R(x[2] + x[1], 13); |
318 | 0 | x[0] ^= R(x[3] + x[2], 18); |
319 | 0 | x[6] ^= R(x[5] + x[4], 7); |
320 | 0 | x[7] ^= R(x[6] + x[5], 9); |
321 | 0 | x[4] ^= R(x[7] + x[6], 13); |
322 | 0 | x[5] ^= R(x[4] + x[7], 18); |
323 | 0 | x[11] ^= R(x[10] + x[9], 7); |
324 | 0 | x[8] ^= R(x[11] + x[10], 9); |
325 | 0 | x[9] ^= R(x[8] + x[11], 13); |
326 | 0 | x[10] ^= R(x[9] + x[8], 18); |
327 | 0 | x[12] ^= R(x[15] + x[14], 7); |
328 | 0 | x[13] ^= R(x[12] + x[15], 9); |
329 | 0 | x[14] ^= R(x[13] + x[12], 13); |
330 | 0 | x[15] ^= R(x[14] + x[13], 18); |
331 | 0 | } |
332 | 0 | for (i = 0; i < 16; ++i) |
333 | 0 | inout[i] += x[i]; |
334 | 0 | OPENSSL_cleanse(x, sizeof(x)); |
335 | 0 | } |
336 | | |
337 | | static void scryptBlockMix(uint32_t *B_, uint32_t *B, uint64_t r) |
338 | 0 | { |
339 | 0 | uint64_t i, j; |
340 | 0 | uint32_t X[16], *pB; |
341 | |
|
342 | 0 | memcpy(X, B + (r * 2 - 1) * 16, sizeof(X)); |
343 | 0 | pB = B; |
344 | 0 | for (i = 0; i < r * 2; i++) { |
345 | 0 | for (j = 0; j < 16; j++) |
346 | 0 | X[j] ^= *pB++; |
347 | 0 | salsa208_word_specification(X); |
348 | 0 | memcpy(B_ + (i / 2 + (i & 1) * r) * 16, X, sizeof(X)); |
349 | 0 | } |
350 | 0 | OPENSSL_cleanse(X, sizeof(X)); |
351 | 0 | } |
352 | | |
353 | | static void scryptROMix(unsigned char *B, uint64_t r, uint64_t N, |
354 | | uint32_t *X, uint32_t *T, uint32_t *V) |
355 | 0 | { |
356 | 0 | unsigned char *pB; |
357 | 0 | uint32_t *pV; |
358 | 0 | uint64_t i, k; |
359 | | |
360 | | /* Convert from little endian input */ |
361 | 0 | for (pV = V, i = 0, pB = B; i < 32 * r; i++, pV++) { |
362 | 0 | *pV = *pB++; |
363 | 0 | *pV |= *pB++ << 8; |
364 | 0 | *pV |= *pB++ << 16; |
365 | 0 | *pV |= (uint32_t)*pB++ << 24; |
366 | 0 | } |
367 | |
|
368 | 0 | for (i = 1; i < N; i++, pV += 32 * r) |
369 | 0 | scryptBlockMix(pV, pV - 32 * r, r); |
370 | |
|
371 | 0 | scryptBlockMix(X, V + (N - 1) * 32 * r, r); |
372 | |
|
373 | 0 | for (i = 0; i < N; i++) { |
374 | 0 | uint32_t j; |
375 | 0 | j = X[16 * (2 * r - 1)] % N; |
376 | 0 | pV = V + 32 * r * j; |
377 | 0 | for (k = 0; k < 32 * r; k++) |
378 | 0 | T[k] = X[k] ^ *pV++; |
379 | 0 | scryptBlockMix(X, T, r); |
380 | 0 | } |
381 | | /* Convert output to little endian */ |
382 | 0 | for (i = 0, pB = B; i < 32 * r; i++) { |
383 | 0 | uint32_t xtmp = X[i]; |
384 | 0 | *pB++ = xtmp & 0xff; |
385 | 0 | *pB++ = (xtmp >> 8) & 0xff; |
386 | 0 | *pB++ = (xtmp >> 16) & 0xff; |
387 | 0 | *pB++ = (xtmp >> 24) & 0xff; |
388 | 0 | } |
389 | 0 | } |
390 | | |
391 | | #ifndef SIZE_MAX |
392 | | # define SIZE_MAX ((size_t)-1) |
393 | | #endif |
394 | | |
395 | | /* |
396 | | * Maximum power of two that will fit in uint64_t: this should work on |
397 | | * most (all?) platforms. |
398 | | */ |
399 | | |
400 | 0 | #define LOG2_UINT64_MAX (sizeof(uint64_t) * 8 - 1) |
401 | | |
402 | | /* |
403 | | * Maximum value of p * r: |
404 | | * p <= ((2^32-1) * hLen) / MFLen => |
405 | | * p <= ((2^32-1) * 32) / (128 * r) => |
406 | | * p * r <= (2^30-1) |
407 | | */ |
408 | | |
409 | 0 | #define SCRYPT_PR_MAX ((1 << 30) - 1) |
410 | | |
411 | | static int scrypt_alg(const char *pass, size_t passlen, |
412 | | const unsigned char *salt, size_t saltlen, |
413 | | uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem, |
414 | | unsigned char *key, size_t keylen, EVP_MD *sha256, |
415 | | OSSL_LIB_CTX *libctx, const char *propq) |
416 | 0 | { |
417 | 0 | int rv = 0; |
418 | 0 | unsigned char *B; |
419 | 0 | uint32_t *X, *V, *T; |
420 | 0 | uint64_t i, Blen, Vlen; |
421 | | |
422 | | /* Sanity check parameters */ |
423 | | /* initial check, r,p must be non zero, N >= 2 and a power of 2 */ |
424 | 0 | if (r == 0 || p == 0 || N < 2 || (N & (N - 1))) |
425 | 0 | return 0; |
426 | | /* Check p * r < SCRYPT_PR_MAX avoiding overflow */ |
427 | 0 | if (p > SCRYPT_PR_MAX / r) { |
428 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED); |
429 | 0 | return 0; |
430 | 0 | } |
431 | | |
432 | | /* |
433 | | * Need to check N: if 2^(128 * r / 8) overflows limit this is |
434 | | * automatically satisfied since N <= UINT64_MAX. |
435 | | */ |
436 | | |
437 | 0 | if (16 * r <= LOG2_UINT64_MAX) { |
438 | 0 | if (N >= (((uint64_t)1) << (16 * r))) { |
439 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED); |
440 | 0 | return 0; |
441 | 0 | } |
442 | 0 | } |
443 | | |
444 | | /* Memory checks: check total allocated buffer size fits in uint64_t */ |
445 | | |
446 | | /* |
447 | | * B size in section 5 step 1.S |
448 | | * Note: we know p * 128 * r < UINT64_MAX because we already checked |
449 | | * p * r < SCRYPT_PR_MAX |
450 | | */ |
451 | 0 | Blen = p * 128 * r; |
452 | | /* |
453 | | * Yet we pass it as integer to PKCS5_PBKDF2_HMAC... [This would |
454 | | * have to be revised when/if PKCS5_PBKDF2_HMAC accepts size_t.] |
455 | | */ |
456 | 0 | if (Blen > INT_MAX) { |
457 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED); |
458 | 0 | return 0; |
459 | 0 | } |
460 | | |
461 | | /* |
462 | | * Check 32 * r * (N + 2) * sizeof(uint32_t) fits in uint64_t |
463 | | * This is combined size V, X and T (section 4) |
464 | | */ |
465 | 0 | i = UINT64_MAX / (32 * sizeof(uint32_t)); |
466 | 0 | if (N + 2 > i / r) { |
467 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED); |
468 | 0 | return 0; |
469 | 0 | } |
470 | 0 | Vlen = 32 * r * (N + 2) * sizeof(uint32_t); |
471 | | |
472 | | /* check total allocated size fits in uint64_t */ |
473 | 0 | if (Blen > UINT64_MAX - Vlen) { |
474 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED); |
475 | 0 | return 0; |
476 | 0 | } |
477 | | |
478 | | /* Check that the maximum memory doesn't exceed a size_t limits */ |
479 | 0 | if (maxmem > SIZE_MAX) |
480 | 0 | maxmem = SIZE_MAX; |
481 | |
|
482 | 0 | if (Blen + Vlen > maxmem) { |
483 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED); |
484 | 0 | return 0; |
485 | 0 | } |
486 | | |
487 | | /* If no key return to indicate parameters are OK */ |
488 | 0 | if (key == NULL) |
489 | 0 | return 1; |
490 | | |
491 | 0 | B = OPENSSL_malloc((size_t)(Blen + Vlen)); |
492 | 0 | if (B == NULL) { |
493 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
494 | 0 | return 0; |
495 | 0 | } |
496 | 0 | X = (uint32_t *)(B + Blen); |
497 | 0 | T = X + 32 * r; |
498 | 0 | V = T + 32 * r; |
499 | 0 | if (ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, 1, sha256, |
500 | 0 | (int)Blen, B, libctx, propq) == 0) |
501 | 0 | goto err; |
502 | | |
503 | 0 | for (i = 0; i < p; i++) |
504 | 0 | scryptROMix(B + 128 * r * i, r, N, X, T, V); |
505 | |
|
506 | 0 | if (ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, B, (int)Blen, 1, sha256, |
507 | 0 | keylen, key, libctx, propq) == 0) |
508 | 0 | goto err; |
509 | 0 | rv = 1; |
510 | 0 | err: |
511 | 0 | if (rv == 0) |
512 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_PBKDF2_ERROR); |
513 | |
|
514 | 0 | OPENSSL_clear_free(B, (size_t)(Blen + Vlen)); |
515 | 0 | return rv; |
516 | 0 | } |
517 | | |
518 | | #endif |