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