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