]>
Commit | Line | Data |
---|---|---|
a95fb9e3 | 1 | /* |
c4d3c19b | 2 | * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved. |
a95fb9e3 | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
62867571 RS |
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 | |
a95fb9e3 DSH |
8 | */ |
9 | ||
a95fb9e3 | 10 | #include <openssl/evp.h> |
fef034f8 | 11 | #include <openssl/err.h> |
5a285add | 12 | #include <openssl/kdf.h> |
a95fb9e3 | 13 | |
b0809bc8 RS |
14 | #ifndef OPENSSL_NO_SCRYPT |
15 | ||
a95fb9e3 DSH |
16 | /* |
17 | * Maximum permitted memory allow this to be overridden with Configuration | |
18 | * option: e.g. -DSCRYPT_MAX_MEM=0 for maximum possible. | |
19 | */ | |
20 | ||
21 | #ifdef SCRYPT_MAX_MEM | |
22 | # if SCRYPT_MAX_MEM == 0 | |
23 | # undef SCRYPT_MAX_MEM | |
24 | /* | |
25 | * Although we could theoretically allocate SIZE_MAX memory that would leave | |
26 | * no memory available for anything else so set limit as half that. | |
27 | */ | |
28 | # define SCRYPT_MAX_MEM (SIZE_MAX/2) | |
29 | # endif | |
30 | #else | |
31 | /* Default memory limit: 32 MB */ | |
32 | # define SCRYPT_MAX_MEM (1024 * 1024 * 32) | |
33 | #endif | |
34 | ||
35 | int EVP_PBE_scrypt(const char *pass, size_t passlen, | |
36 | const unsigned char *salt, size_t saltlen, | |
37 | uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem, | |
38 | unsigned char *key, size_t keylen) | |
39 | { | |
5a285add DM |
40 | const char *empty = ""; |
41 | int rv = 1; | |
42 | EVP_KDF_CTX *kctx; | |
43 | ||
4c3941c2 MC |
44 | if (r > UINT32_MAX || p > UINT32_MAX) { |
45 | EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_PARAMETER_TOO_LARGE); | |
46 | return 0; | |
47 | } | |
48 | ||
5a285add DM |
49 | /* Maintain existing behaviour. */ |
50 | if (pass == NULL) { | |
51 | pass = empty; | |
52 | passlen = 0; | |
a95fb9e3 | 53 | } |
a95fb9e3 DSH |
54 | if (maxmem == 0) |
55 | maxmem = SCRYPT_MAX_MEM; | |
44589b5d | 56 | |
5a285add DM |
57 | kctx = EVP_KDF_CTX_new_id(EVP_KDF_SCRYPT); |
58 | if (kctx == NULL) | |
a95fb9e3 | 59 | return 0; |
a95fb9e3 | 60 | |
5a285add DM |
61 | if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PASS, pass, (size_t)passlen) != 1 |
62 | || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT, | |
63 | salt, (size_t)saltlen) != 1 | |
64 | || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SCRYPT_N, N) != 1 | |
65 | || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SCRYPT_R, (uint32_t)r) != 1 | |
66 | || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SCRYPT_P, (uint32_t)p) != 1 | |
67 | || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAXMEM_BYTES, maxmem) != 1 | |
68 | || EVP_KDF_derive(kctx, key, keylen) != 1) | |
69 | rv = 0; | |
70 | ||
71 | EVP_KDF_CTX_free(kctx); | |
a95fb9e3 DSH |
72 | return rv; |
73 | } | |
5a285add | 74 | |
b0809bc8 | 75 | #endif |