]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/kdfs/scrypt.c
scrypt: implement ctx dup operation
[thirdparty/openssl.git] / providers / implementations / kdfs / scrypt.c
1 /*
2 * Copyright 2017-2021 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/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"
25
26 #ifndef OPENSSL_NO_SCRYPT
27
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;
37
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);
43
44 typedef struct {
45 OSSL_LIB_CTX *libctx;
46 char *propq;
47 unsigned char *pass;
48 size_t pass_len;
49 unsigned char *salt;
50 size_t salt_len;
51 uint64_t N;
52 uint64_t r, p;
53 uint64_t maxmem_bytes;
54 EVP_MD *sha256;
55 } KDF_SCRYPT;
56
57 static void kdf_scrypt_init(KDF_SCRYPT *ctx);
58
59 static void *kdf_scrypt_new(void *provctx)
60 {
61 KDF_SCRYPT *ctx;
62
63 if (!ossl_prov_is_running())
64 return NULL;
65
66 ctx = OPENSSL_zalloc(sizeof(*ctx));
67 if (ctx == NULL) {
68 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
69 return NULL;
70 }
71 ctx->libctx = PROV_LIBCTX_OF(provctx);
72 kdf_scrypt_init(ctx);
73 return ctx;
74 }
75
76 static void kdf_scrypt_free(void *vctx)
77 {
78 KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx;
79
80 if (ctx != NULL) {
81 OPENSSL_free(ctx->propq);
82 EVP_MD_free(ctx->sha256);
83 kdf_scrypt_reset(ctx);
84 OPENSSL_free(ctx);
85 }
86 }
87
88 static void kdf_scrypt_reset(void *vctx)
89 {
90 KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx;
91
92 OPENSSL_free(ctx->salt);
93 OPENSSL_clear_free(ctx->pass, ctx->pass_len);
94 kdf_scrypt_init(ctx);
95 }
96
97 static void *kdf_scrypt_dup(void *vctx)
98 {
99 const KDF_SCRYPT *src = (const KDF_SCRYPT *)vctx;
100 KDF_SCRYPT *dest;
101
102 dest = kdf_scrypt_new(src->libctx);
103 if (dest != NULL) {
104 if (src->sha256 != NULL && !EVP_MD_up_ref(src->sha256))
105 goto err;
106 if (src->propq != NULL) {
107 dest->propq = OPENSSL_strdup(src->propq);
108 if (dest->propq == NULL)
109 goto err;
110 }
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))
115 goto err;
116 dest->N = src->N;
117 dest->r = src->r;
118 dest->p = src->p;
119 dest->maxmem_bytes = src->maxmem_bytes;
120 dest->sha256 = src->sha256;
121 }
122 return dest;
123
124 err:
125 kdf_scrypt_free(dest);
126 return NULL;
127 }
128
129 static void kdf_scrypt_init(KDF_SCRYPT *ctx)
130 {
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).
134 */
135 ctx->N = 1 << 20;
136 ctx->r = 8;
137 ctx->p = 1;
138 ctx->maxmem_bytes = 1025 * 1024 * 1024;
139 }
140
141 static int scrypt_set_membuf(unsigned char **buffer, size_t *buflen,
142 const OSSL_PARAM *p)
143 {
144 OPENSSL_clear_free(*buffer, *buflen);
145 *buffer = NULL;
146 *buflen = 0;
147
148 if (p->data_size == 0) {
149 if ((*buffer = OPENSSL_malloc(1)) == NULL) {
150 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
151 return 0;
152 }
153 } else if (p->data != NULL) {
154 if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
155 return 0;
156 }
157 return 1;
158 }
159
160 static int set_digest(KDF_SCRYPT *ctx)
161 {
162 EVP_MD_free(ctx->sha256);
163 ctx->sha256 = EVP_MD_fetch(ctx->libctx, "sha256", ctx->propq);
164 if (ctx->sha256 == NULL) {
165 OPENSSL_free(ctx);
166 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOAD_SHA256);
167 return 0;
168 }
169 return 1;
170 }
171
172 static int set_property_query(KDF_SCRYPT *ctx, const char *propq)
173 {
174 OPENSSL_free(ctx->propq);
175 ctx->propq = NULL;
176 if (propq != NULL) {
177 ctx->propq = OPENSSL_strdup(propq);
178 if (ctx->propq == NULL) {
179 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
180 return 0;
181 }
182 }
183 return 1;
184 }
185
186 static int kdf_scrypt_derive(void *vctx, unsigned char *key, size_t keylen,
187 const OSSL_PARAM params[])
188 {
189 KDF_SCRYPT *ctx = (KDF_SCRYPT *)vctx;
190
191 if (!ossl_prov_is_running() || !kdf_scrypt_set_ctx_params(ctx, params))
192 return 0;
193
194 if (ctx->pass == NULL) {
195 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
196 return 0;
197 }
198
199 if (ctx->salt == NULL) {
200 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
201 return 0;
202 }
203
204 if (ctx->sha256 == NULL && !set_digest(ctx))
205 return 0;
206
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);
211 }
212
213 static int is_power_of_two(uint64_t value)
214 {
215 return (value != 0) && ((value & (value - 1)) == 0);
216 }
217
218 static int kdf_scrypt_set_ctx_params(void *vctx, const OSSL_PARAM params[])
219 {
220 const OSSL_PARAM *p;
221 KDF_SCRYPT *ctx = vctx;
222 uint64_t u64_value;
223
224 if (params == NULL)
225 return 1;
226
227 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
228 if (!scrypt_set_membuf(&ctx->pass, &ctx->pass_len, p))
229 return 0;
230
231 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
232 if (!scrypt_set_membuf(&ctx->salt, &ctx->salt_len, p))
233 return 0;
234
235 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_N))
236 != NULL) {
237 if (!OSSL_PARAM_get_uint64(p, &u64_value)
238 || u64_value <= 1
239 || !is_power_of_two(u64_value))
240 return 0;
241 ctx->N = u64_value;
242 }
243
244 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_R))
245 != NULL) {
246 if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1)
247 return 0;
248 ctx->r = u64_value;
249 }
250
251 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_P))
252 != NULL) {
253 if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1)
254 return 0;
255 ctx->p = u64_value;
256 }
257
258 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SCRYPT_MAXMEM))
259 != NULL) {
260 if (!OSSL_PARAM_get_uint64(p, &u64_value) || u64_value < 1)
261 return 0;
262 ctx->maxmem_bytes = u64_value;
263 }
264
265 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PROPERTIES);
266 if (p != NULL) {
267 if (p->data_type != OSSL_PARAM_UTF8_STRING
268 || !set_property_query(ctx, p->data)
269 || !set_digest(ctx))
270 return 0;
271 }
272 return 1;
273 }
274
275 static const OSSL_PARAM *kdf_scrypt_settable_ctx_params(ossl_unused void *ctx,
276 ossl_unused void *p_ctx)
277 {
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),
286 OSSL_PARAM_END
287 };
288 return known_settable_ctx_params;
289 }
290
291 static int kdf_scrypt_get_ctx_params(void *vctx, OSSL_PARAM params[])
292 {
293 OSSL_PARAM *p;
294
295 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
296 return OSSL_PARAM_set_size_t(p, SIZE_MAX);
297 return -2;
298 }
299
300 static const OSSL_PARAM *kdf_scrypt_gettable_ctx_params(ossl_unused void *ctx,
301 ossl_unused void *p_ctx)
302 {
303 static const OSSL_PARAM known_gettable_ctx_params[] = {
304 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
305 OSSL_PARAM_END
306 };
307 return known_gettable_ctx_params;
308 }
309
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 },
322 { 0, NULL }
323 };
324
325 #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
326 static void salsa208_word_specification(uint32_t inout[16])
327 {
328 int i;
329 uint32_t x[16];
330
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);
365 }
366 for (i = 0; i < 16; ++i)
367 inout[i] += x[i];
368 OPENSSL_cleanse(x, sizeof(x));
369 }
370
371 static void scryptBlockMix(uint32_t *B_, uint32_t *B, uint64_t r)
372 {
373 uint64_t i, j;
374 uint32_t X[16], *pB;
375
376 memcpy(X, B + (r * 2 - 1) * 16, sizeof(X));
377 pB = B;
378 for (i = 0; i < r * 2; i++) {
379 for (j = 0; j < 16; j++)
380 X[j] ^= *pB++;
381 salsa208_word_specification(X);
382 memcpy(B_ + (i / 2 + (i & 1) * r) * 16, X, sizeof(X));
383 }
384 OPENSSL_cleanse(X, sizeof(X));
385 }
386
387 static void scryptROMix(unsigned char *B, uint64_t r, uint64_t N,
388 uint32_t *X, uint32_t *T, uint32_t *V)
389 {
390 unsigned char *pB;
391 uint32_t *pV;
392 uint64_t i, k;
393
394 /* Convert from little endian input */
395 for (pV = V, i = 0, pB = B; i < 32 * r; i++, pV++) {
396 *pV = *pB++;
397 *pV |= *pB++ << 8;
398 *pV |= *pB++ << 16;
399 *pV |= (uint32_t)*pB++ << 24;
400 }
401
402 for (i = 1; i < N; i++, pV += 32 * r)
403 scryptBlockMix(pV, pV - 32 * r, r);
404
405 scryptBlockMix(X, V + (N - 1) * 32 * r, r);
406
407 for (i = 0; i < N; i++) {
408 uint32_t j;
409 j = X[16 * (2 * r - 1)] % N;
410 pV = V + 32 * r * j;
411 for (k = 0; k < 32 * r; k++)
412 T[k] = X[k] ^ *pV++;
413 scryptBlockMix(X, T, r);
414 }
415 /* Convert output to little endian */
416 for (i = 0, pB = B; i < 32 * r; i++) {
417 uint32_t xtmp = X[i];
418 *pB++ = xtmp & 0xff;
419 *pB++ = (xtmp >> 8) & 0xff;
420 *pB++ = (xtmp >> 16) & 0xff;
421 *pB++ = (xtmp >> 24) & 0xff;
422 }
423 }
424
425 #ifndef SIZE_MAX
426 # define SIZE_MAX ((size_t)-1)
427 #endif
428
429 /*
430 * Maximum power of two that will fit in uint64_t: this should work on
431 * most (all?) platforms.
432 */
433
434 #define LOG2_UINT64_MAX (sizeof(uint64_t) * 8 - 1)
435
436 /*
437 * Maximum value of p * r:
438 * p <= ((2^32-1) * hLen) / MFLen =>
439 * p <= ((2^32-1) * 32) / (128 * r) =>
440 * p * r <= (2^30-1)
441 */
442
443 #define SCRYPT_PR_MAX ((1 << 30) - 1)
444
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)
450 {
451 int rv = 0;
452 unsigned char *B;
453 uint32_t *X, *V, *T;
454 uint64_t i, Blen, Vlen;
455
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)))
459 return 0;
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);
463 return 0;
464 }
465
466 /*
467 * Need to check N: if 2^(128 * r / 8) overflows limit this is
468 * automatically satisfied since N <= UINT64_MAX.
469 */
470
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);
474 return 0;
475 }
476 }
477
478 /* Memory checks: check total allocated buffer size fits in uint64_t */
479
480 /*
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
484 */
485 Blen = p * 128 * r;
486 /*
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.]
489 */
490 if (Blen > INT_MAX) {
491 ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
492 return 0;
493 }
494
495 /*
496 * Check 32 * r * (N + 2) * sizeof(uint32_t) fits in uint64_t
497 * This is combined size V, X and T (section 4)
498 */
499 i = UINT64_MAX / (32 * sizeof(uint32_t));
500 if (N + 2 > i / r) {
501 ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
502 return 0;
503 }
504 Vlen = 32 * r * (N + 2) * sizeof(uint32_t);
505
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);
509 return 0;
510 }
511
512 /* Check that the maximum memory doesn't exceed a size_t limits */
513 if (maxmem > SIZE_MAX)
514 maxmem = SIZE_MAX;
515
516 if (Blen + Vlen > maxmem) {
517 ERR_raise(ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED);
518 return 0;
519 }
520
521 /* If no key return to indicate parameters are OK */
522 if (key == NULL)
523 return 1;
524
525 B = OPENSSL_malloc((size_t)(Blen + Vlen));
526 if (B == NULL) {
527 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
528 return 0;
529 }
530 X = (uint32_t *)(B + Blen);
531 T = X + 32 * r;
532 V = T + 32 * r;
533 if (ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, 1, sha256,
534 (int)Blen, B, libctx, propq) == 0)
535 goto err;
536
537 for (i = 0; i < p; i++)
538 scryptROMix(B + 128 * r * i, r, N, X, T, V);
539
540 if (ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, B, (int)Blen, 1, sha256,
541 keylen, key, libctx, propq) == 0)
542 goto err;
543 rv = 1;
544 err:
545 if (rv == 0)
546 ERR_raise(ERR_LIB_EVP, EVP_R_PBKDF2_ERROR);
547
548 OPENSSL_clear_free(B, (size_t)(Blen + Vlen));
549 return rv;
550 }
551
552 #endif