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