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