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