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