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