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