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