2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include <openssl/evp.h>
14 #include <openssl/kdf.h>
15 #include <openssl/err.h>
16 #include <openssl/core_names.h>
17 #include <openssl/proverr.h>
18 #include "crypto/evp.h"
19 #include "internal/numbers.h"
20 #include "prov/implementations.h"
21 #include "prov/provider_ctx.h"
22 #include "prov/providercommon.h"
23 #include "prov/implementations.h"
24 #include "prov/provider_util.h"
26 #ifndef OPENSSL_NO_SCRYPT
28 static OSSL_FUNC_kdf_newctx_fn kdf_scrypt_new
;
29 static OSSL_FUNC_kdf_dupctx_fn kdf_scrypt_dup
;
30 static OSSL_FUNC_kdf_freectx_fn kdf_scrypt_free
;
31 static OSSL_FUNC_kdf_reset_fn kdf_scrypt_reset
;
32 static OSSL_FUNC_kdf_derive_fn kdf_scrypt_derive
;
33 static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_scrypt_settable_ctx_params
;
34 static OSSL_FUNC_kdf_set_ctx_params_fn kdf_scrypt_set_ctx_params
;
35 static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_scrypt_gettable_ctx_params
;
36 static OSSL_FUNC_kdf_get_ctx_params_fn kdf_scrypt_get_ctx_params
;
38 static int scrypt_alg(const char *pass
, size_t passlen
,
39 const unsigned char *salt
, size_t saltlen
,
40 uint64_t N
, uint64_t r
, uint64_t p
, uint64_t maxmem
,
41 unsigned char *key
, size_t keylen
, EVP_MD
*sha256
,
42 OSSL_LIB_CTX
*libctx
, const char *propq
);
53 uint64_t maxmem_bytes
;
57 static void kdf_scrypt_init(KDF_SCRYPT
*ctx
);
59 static void *kdf_scrypt_new(void *provctx
)
63 if (!ossl_prov_is_running())
66 ctx
= OPENSSL_zalloc(sizeof(*ctx
));
68 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
71 ctx
->libctx
= PROV_LIBCTX_OF(provctx
);
76 static void kdf_scrypt_free(void *vctx
)
78 KDF_SCRYPT
*ctx
= (KDF_SCRYPT
*)vctx
;
81 OPENSSL_free(ctx
->propq
);
82 EVP_MD_free(ctx
->sha256
);
83 kdf_scrypt_reset(ctx
);
88 static void kdf_scrypt_reset(void *vctx
)
90 KDF_SCRYPT
*ctx
= (KDF_SCRYPT
*)vctx
;
92 OPENSSL_free(ctx
->salt
);
93 OPENSSL_clear_free(ctx
->pass
, ctx
->pass_len
);
97 static void *kdf_scrypt_dup(void *vctx
)
99 const KDF_SCRYPT
*src
= (const KDF_SCRYPT
*)vctx
;
102 dest
= kdf_scrypt_new(src
->libctx
);
104 if (src
->sha256
!= NULL
&& !EVP_MD_up_ref(src
->sha256
))
106 if (src
->propq
!= NULL
) {
107 dest
->propq
= OPENSSL_strdup(src
->propq
);
108 if (dest
->propq
== NULL
)
111 if (!ossl_prov_memdup(src
->salt
, src
->salt_len
,
112 &dest
->salt
, &dest
->salt_len
)
113 || !ossl_prov_memdup(src
->pass
, src
->pass_len
,
114 &dest
->pass
, &dest
->pass_len
))
119 dest
->maxmem_bytes
= src
->maxmem_bytes
;
120 dest
->sha256
= src
->sha256
;
125 kdf_scrypt_free(dest
);
129 static void kdf_scrypt_init(KDF_SCRYPT
*ctx
)
131 /* Default values are the most conservative recommendation given in the
132 * original paper of C. Percival. Derivation uses roughly 1 GiB of memory
133 * for this parameter choice (approx. 128 * r * N * p bytes).
138 ctx
->maxmem_bytes
= 1025 * 1024 * 1024;
141 static int scrypt_set_membuf(unsigned char **buffer
, size_t *buflen
,
144 OPENSSL_clear_free(*buffer
, *buflen
);
148 if (p
->data_size
== 0) {
149 if ((*buffer
= OPENSSL_malloc(1)) == NULL
) {
150 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
153 } else if (p
->data
!= NULL
) {
154 if (!OSSL_PARAM_get_octet_string(p
, (void **)buffer
, 0, buflen
))
160 static int set_digest(KDF_SCRYPT
*ctx
)
162 EVP_MD_free(ctx
->sha256
);
163 ctx
->sha256
= EVP_MD_fetch(ctx
->libctx
, "sha256", ctx
->propq
);
164 if (ctx
->sha256
== NULL
) {
166 ERR_raise(ERR_LIB_PROV
, PROV_R_UNABLE_TO_LOAD_SHA256
);
172 static int set_property_query(KDF_SCRYPT
*ctx
, const char *propq
)
174 OPENSSL_free(ctx
->propq
);
177 ctx
->propq
= OPENSSL_strdup(propq
);
178 if (ctx
->propq
== NULL
) {
179 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
186 static int kdf_scrypt_derive(void *vctx
, unsigned char *key
, size_t keylen
,
187 const OSSL_PARAM params
[])
189 KDF_SCRYPT
*ctx
= (KDF_SCRYPT
*)vctx
;
191 if (!ossl_prov_is_running() || !kdf_scrypt_set_ctx_params(ctx
, params
))
194 if (ctx
->pass
== NULL
) {
195 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_PASS
);
199 if (ctx
->salt
== NULL
) {
200 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_SALT
);
204 if (ctx
->sha256
== NULL
&& !set_digest(ctx
))
207 return scrypt_alg((char *)ctx
->pass
, ctx
->pass_len
, ctx
->salt
,
208 ctx
->salt_len
, ctx
->N
, ctx
->r
, ctx
->p
,
209 ctx
->maxmem_bytes
, key
, keylen
, ctx
->sha256
,
210 ctx
->libctx
, ctx
->propq
);
213 static int is_power_of_two(uint64_t value
)
215 return (value
!= 0) && ((value
& (value
- 1)) == 0);
218 static int kdf_scrypt_set_ctx_params(void *vctx
, const OSSL_PARAM params
[])
221 KDF_SCRYPT
*ctx
= vctx
;
227 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_PASSWORD
)) != NULL
)
228 if (!scrypt_set_membuf(&ctx
->pass
, &ctx
->pass_len
, p
))
231 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_SALT
)) != NULL
)
232 if (!scrypt_set_membuf(&ctx
->salt
, &ctx
->salt_len
, p
))
235 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_SCRYPT_N
))
237 if (!OSSL_PARAM_get_uint64(p
, &u64_value
)
239 || !is_power_of_two(u64_value
))
244 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_SCRYPT_R
))
246 if (!OSSL_PARAM_get_uint64(p
, &u64_value
) || u64_value
< 1)
251 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_SCRYPT_P
))
253 if (!OSSL_PARAM_get_uint64(p
, &u64_value
) || u64_value
< 1)
258 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_SCRYPT_MAXMEM
))
260 if (!OSSL_PARAM_get_uint64(p
, &u64_value
) || u64_value
< 1)
262 ctx
->maxmem_bytes
= u64_value
;
265 p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_PROPERTIES
);
267 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
268 || !set_property_query(ctx
, p
->data
)
275 static const OSSL_PARAM
*kdf_scrypt_settable_ctx_params(ossl_unused
void *ctx
,
276 ossl_unused
void *p_ctx
)
278 static const OSSL_PARAM known_settable_ctx_params
[] = {
279 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD
, NULL
, 0),
280 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT
, NULL
, 0),
281 OSSL_PARAM_uint64(OSSL_KDF_PARAM_SCRYPT_N
, NULL
),
282 OSSL_PARAM_uint32(OSSL_KDF_PARAM_SCRYPT_R
, NULL
),
283 OSSL_PARAM_uint32(OSSL_KDF_PARAM_SCRYPT_P
, NULL
),
284 OSSL_PARAM_uint64(OSSL_KDF_PARAM_SCRYPT_MAXMEM
, NULL
),
285 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES
, NULL
, 0),
288 return known_settable_ctx_params
;
291 static int kdf_scrypt_get_ctx_params(void *vctx
, OSSL_PARAM params
[])
295 if ((p
= OSSL_PARAM_locate(params
, OSSL_KDF_PARAM_SIZE
)) != NULL
)
296 return OSSL_PARAM_set_size_t(p
, SIZE_MAX
);
300 static const OSSL_PARAM
*kdf_scrypt_gettable_ctx_params(ossl_unused
void *ctx
,
301 ossl_unused
void *p_ctx
)
303 static const OSSL_PARAM known_gettable_ctx_params
[] = {
304 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE
, NULL
),
307 return known_gettable_ctx_params
;
310 const OSSL_DISPATCH ossl_kdf_scrypt_functions
[] = {
311 { OSSL_FUNC_KDF_NEWCTX
, (void(*)(void))kdf_scrypt_new
},
312 { OSSL_FUNC_KDF_DUPCTX
, (void(*)(void))kdf_scrypt_dup
},
313 { OSSL_FUNC_KDF_FREECTX
, (void(*)(void))kdf_scrypt_free
},
314 { OSSL_FUNC_KDF_RESET
, (void(*)(void))kdf_scrypt_reset
},
315 { OSSL_FUNC_KDF_DERIVE
, (void(*)(void))kdf_scrypt_derive
},
316 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS
,
317 (void(*)(void))kdf_scrypt_settable_ctx_params
},
318 { OSSL_FUNC_KDF_SET_CTX_PARAMS
, (void(*)(void))kdf_scrypt_set_ctx_params
},
319 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS
,
320 (void(*)(void))kdf_scrypt_gettable_ctx_params
},
321 { OSSL_FUNC_KDF_GET_CTX_PARAMS
, (void(*)(void))kdf_scrypt_get_ctx_params
},
325 #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
326 static void salsa208_word_specification(uint32_t inout
[16])
331 memcpy(x
, inout
, sizeof(x
));
332 for (i
= 8; i
> 0; i
-= 2) {
333 x
[4] ^= R(x
[0] + x
[12], 7);
334 x
[8] ^= R(x
[4] + x
[0], 9);
335 x
[12] ^= R(x
[8] + x
[4], 13);
336 x
[0] ^= R(x
[12] + x
[8], 18);
337 x
[9] ^= R(x
[5] + x
[1], 7);
338 x
[13] ^= R(x
[9] + x
[5], 9);
339 x
[1] ^= R(x
[13] + x
[9], 13);
340 x
[5] ^= R(x
[1] + x
[13], 18);
341 x
[14] ^= R(x
[10] + x
[6], 7);
342 x
[2] ^= R(x
[14] + x
[10], 9);
343 x
[6] ^= R(x
[2] + x
[14], 13);
344 x
[10] ^= R(x
[6] + x
[2], 18);
345 x
[3] ^= R(x
[15] + x
[11], 7);
346 x
[7] ^= R(x
[3] + x
[15], 9);
347 x
[11] ^= R(x
[7] + x
[3], 13);
348 x
[15] ^= R(x
[11] + x
[7], 18);
349 x
[1] ^= R(x
[0] + x
[3], 7);
350 x
[2] ^= R(x
[1] + x
[0], 9);
351 x
[3] ^= R(x
[2] + x
[1], 13);
352 x
[0] ^= R(x
[3] + x
[2], 18);
353 x
[6] ^= R(x
[5] + x
[4], 7);
354 x
[7] ^= R(x
[6] + x
[5], 9);
355 x
[4] ^= R(x
[7] + x
[6], 13);
356 x
[5] ^= R(x
[4] + x
[7], 18);
357 x
[11] ^= R(x
[10] + x
[9], 7);
358 x
[8] ^= R(x
[11] + x
[10], 9);
359 x
[9] ^= R(x
[8] + x
[11], 13);
360 x
[10] ^= R(x
[9] + x
[8], 18);
361 x
[12] ^= R(x
[15] + x
[14], 7);
362 x
[13] ^= R(x
[12] + x
[15], 9);
363 x
[14] ^= R(x
[13] + x
[12], 13);
364 x
[15] ^= R(x
[14] + x
[13], 18);
366 for (i
= 0; i
< 16; ++i
)
368 OPENSSL_cleanse(x
, sizeof(x
));
371 static void scryptBlockMix(uint32_t *B_
, uint32_t *B
, uint64_t r
)
376 memcpy(X
, B
+ (r
* 2 - 1) * 16, sizeof(X
));
378 for (i
= 0; i
< r
* 2; i
++) {
379 for (j
= 0; j
< 16; j
++)
381 salsa208_word_specification(X
);
382 memcpy(B_
+ (i
/ 2 + (i
& 1) * r
) * 16, X
, sizeof(X
));
384 OPENSSL_cleanse(X
, sizeof(X
));
387 static void scryptROMix(unsigned char *B
, uint64_t r
, uint64_t N
,
388 uint32_t *X
, uint32_t *T
, uint32_t *V
)
394 /* Convert from little endian input */
395 for (pV
= V
, i
= 0, pB
= B
; i
< 32 * r
; i
++, pV
++) {
399 *pV
|= (uint32_t)*pB
++ << 24;
402 for (i
= 1; i
< N
; i
++, pV
+= 32 * r
)
403 scryptBlockMix(pV
, pV
- 32 * r
, r
);
405 scryptBlockMix(X
, V
+ (N
- 1) * 32 * r
, r
);
407 for (i
= 0; i
< N
; i
++) {
409 j
= X
[16 * (2 * r
- 1)] % N
;
411 for (k
= 0; k
< 32 * r
; k
++)
413 scryptBlockMix(X
, T
, r
);
415 /* Convert output to little endian */
416 for (i
= 0, pB
= B
; i
< 32 * r
; i
++) {
417 uint32_t xtmp
= X
[i
];
419 *pB
++ = (xtmp
>> 8) & 0xff;
420 *pB
++ = (xtmp
>> 16) & 0xff;
421 *pB
++ = (xtmp
>> 24) & 0xff;
426 # define SIZE_MAX ((size_t)-1)
430 * Maximum power of two that will fit in uint64_t: this should work on
431 * most (all?) platforms.
434 #define LOG2_UINT64_MAX (sizeof(uint64_t) * 8 - 1)
437 * Maximum value of p * r:
438 * p <= ((2^32-1) * hLen) / MFLen =>
439 * p <= ((2^32-1) * 32) / (128 * r) =>
443 #define SCRYPT_PR_MAX ((1 << 30) - 1)
445 static int scrypt_alg(const char *pass
, size_t passlen
,
446 const unsigned char *salt
, size_t saltlen
,
447 uint64_t N
, uint64_t r
, uint64_t p
, uint64_t maxmem
,
448 unsigned char *key
, size_t keylen
, EVP_MD
*sha256
,
449 OSSL_LIB_CTX
*libctx
, const char *propq
)
454 uint64_t i
, Blen
, Vlen
;
456 /* Sanity check parameters */
457 /* initial check, r,p must be non zero, N >= 2 and a power of 2 */
458 if (r
== 0 || p
== 0 || N
< 2 || (N
& (N
- 1)))
460 /* Check p * r < SCRYPT_PR_MAX avoiding overflow */
461 if (p
> SCRYPT_PR_MAX
/ r
) {
462 ERR_raise(ERR_LIB_EVP
, EVP_R_MEMORY_LIMIT_EXCEEDED
);
467 * Need to check N: if 2^(128 * r / 8) overflows limit this is
468 * automatically satisfied since N <= UINT64_MAX.
471 if (16 * r
<= LOG2_UINT64_MAX
) {
472 if (N
>= (((uint64_t)1) << (16 * r
))) {
473 ERR_raise(ERR_LIB_EVP
, EVP_R_MEMORY_LIMIT_EXCEEDED
);
478 /* Memory checks: check total allocated buffer size fits in uint64_t */
481 * B size in section 5 step 1.S
482 * Note: we know p * 128 * r < UINT64_MAX because we already checked
483 * p * r < SCRYPT_PR_MAX
487 * Yet we pass it as integer to PKCS5_PBKDF2_HMAC... [This would
488 * have to be revised when/if PKCS5_PBKDF2_HMAC accepts size_t.]
490 if (Blen
> INT_MAX
) {
491 ERR_raise(ERR_LIB_EVP
, EVP_R_MEMORY_LIMIT_EXCEEDED
);
496 * Check 32 * r * (N + 2) * sizeof(uint32_t) fits in uint64_t
497 * This is combined size V, X and T (section 4)
499 i
= UINT64_MAX
/ (32 * sizeof(uint32_t));
501 ERR_raise(ERR_LIB_EVP
, EVP_R_MEMORY_LIMIT_EXCEEDED
);
504 Vlen
= 32 * r
* (N
+ 2) * sizeof(uint32_t);
506 /* check total allocated size fits in uint64_t */
507 if (Blen
> UINT64_MAX
- Vlen
) {
508 ERR_raise(ERR_LIB_EVP
, EVP_R_MEMORY_LIMIT_EXCEEDED
);
512 /* Check that the maximum memory doesn't exceed a size_t limits */
513 if (maxmem
> SIZE_MAX
)
516 if (Blen
+ Vlen
> maxmem
) {
517 ERR_raise(ERR_LIB_EVP
, EVP_R_MEMORY_LIMIT_EXCEEDED
);
521 /* If no key return to indicate parameters are OK */
525 B
= OPENSSL_malloc((size_t)(Blen
+ Vlen
));
527 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
530 X
= (uint32_t *)(B
+ Blen
);
533 if (ossl_pkcs5_pbkdf2_hmac_ex(pass
, passlen
, salt
, saltlen
, 1, sha256
,
534 (int)Blen
, B
, libctx
, propq
) == 0)
537 for (i
= 0; i
< p
; i
++)
538 scryptROMix(B
+ 128 * r
* i
, r
, N
, X
, T
, V
);
540 if (ossl_pkcs5_pbkdf2_hmac_ex(pass
, passlen
, B
, (int)Blen
, 1, sha256
,
541 keylen
, key
, libctx
, propq
) == 0)
546 ERR_raise(ERR_LIB_EVP
, EVP_R_PBKDF2_ERROR
);
548 OPENSSL_clear_free(B
, (size_t)(Blen
+ Vlen
));