2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
12 #include <openssl/hmac.h>
13 #include <openssl/kdf.h>
14 #include <openssl/evp.h>
15 #include "internal/cryptlib.h"
16 #include "internal/evp_int.h"
18 #ifndef OPENSSL_NO_SCRYPT
20 static int atou64(const char *nptr, uint64_t *result);
28 uint64_t maxmem_bytes;
31 /* Custom uint64_t parser since we do not have strtoull */
32 static int atou64(const char *nptr, uint64_t *result)
40 if ((*nptr < '0') || (*nptr > '9')) {
43 digit = (unsigned int)(*nptr - '0');
44 new_value = (value * 10) + digit;
45 if ((new_value < digit) || ((new_value - digit) / 10 != value)) {
56 static int pkey_scrypt_init(EVP_PKEY_CTX *ctx)
58 SCRYPT_PKEY_CTX *kctx;
60 kctx = OPENSSL_zalloc(sizeof(*kctx));
64 /* Default values are the most conservative recommendation given in the
65 * original paper of C. Percival. Derivation uses roughly 1 GiB of memory
66 * for this parameter choice (approx. 128 * r * (N + p) bytes).
71 kctx->maxmem_bytes = 1025 * 1024 * 1024;
78 static void pkey_scrypt_cleanup(EVP_PKEY_CTX *ctx)
80 SCRYPT_PKEY_CTX *kctx = ctx->data;
82 OPENSSL_clear_free(kctx->salt, kctx->salt_len);
83 OPENSSL_clear_free(kctx->pass, kctx->pass_len);
87 static int pkey_scrypt_set_membuf(unsigned char **buffer, size_t *buflen,
88 const unsigned char *new_buffer,
91 if (new_buffer == NULL)
98 OPENSSL_clear_free(*buffer, *buflen);
100 if (new_buflen > 0) {
101 *buffer = OPENSSL_memdup(new_buffer, new_buflen);
103 *buffer = OPENSSL_malloc(1);
108 *buflen = new_buflen;
112 static int is_power_of_two(uint64_t value)
114 return (value != 0) && ((value & (value - 1)) == 0);
117 static int pkey_scrypt_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
119 SCRYPT_PKEY_CTX *kctx = ctx->data;
123 case EVP_PKEY_CTRL_PASS:
124 return pkey_scrypt_set_membuf(&kctx->pass, &kctx->pass_len, p2, p1);
126 case EVP_PKEY_CTRL_SCRYPT_SALT:
127 return pkey_scrypt_set_membuf(&kctx->salt, &kctx->salt_len, p2, p1);
129 case EVP_PKEY_CTRL_SCRYPT_N:
130 u64_value = *((uint64_t *)p2);
131 if ((u64_value <= 1) || !is_power_of_two(u64_value))
136 case EVP_PKEY_CTRL_SCRYPT_R:
137 u64_value = *((uint64_t *)p2);
143 case EVP_PKEY_CTRL_SCRYPT_P:
144 u64_value = *((uint64_t *)p2);
150 case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES:
151 u64_value = *((uint64_t *)p2);
154 kctx->maxmem_bytes = u64_value;
163 static int pkey_scrypt_ctrl_uint64(EVP_PKEY_CTX *ctx, int type,
168 if (!atou64(value, &int_value)) {
169 KDFerr(KDF_F_PKEY_SCRYPT_CTRL_UINT64, KDF_R_VALUE_ERROR);
172 return pkey_scrypt_ctrl(ctx, type, 0, &int_value);
175 static int pkey_scrypt_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
179 KDFerr(KDF_F_PKEY_SCRYPT_CTRL_STR, KDF_R_VALUE_MISSING);
183 if (strcmp(type, "pass") == 0)
184 return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_PASS, value);
186 if (strcmp(type, "hexpass") == 0)
187 return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_PASS, value);
189 if (strcmp(type, "salt") == 0)
190 return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SCRYPT_SALT, value);
192 if (strcmp(type, "hexsalt") == 0)
193 return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SCRYPT_SALT, value);
195 if (strcmp(type, "N") == 0)
196 return pkey_scrypt_ctrl_uint64(ctx, EVP_PKEY_CTRL_SCRYPT_N, value);
198 if (strcmp(type, "r") == 0)
199 return pkey_scrypt_ctrl_uint64(ctx, EVP_PKEY_CTRL_SCRYPT_R, value);
201 if (strcmp(type, "p") == 0)
202 return pkey_scrypt_ctrl_uint64(ctx, EVP_PKEY_CTRL_SCRYPT_P, value);
204 if (strcmp(type, "maxmem_bytes") == 0)
205 return pkey_scrypt_ctrl_uint64(ctx, EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES,
208 KDFerr(KDF_F_PKEY_SCRYPT_CTRL_STR, KDF_R_UNKNOWN_PARAMETER_TYPE);
212 static int pkey_scrypt_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
215 SCRYPT_PKEY_CTX *kctx = ctx->data;
217 if (kctx->pass == NULL) {
218 KDFerr(KDF_F_PKEY_SCRYPT_DERIVE, KDF_R_MISSING_PASS);
222 if (kctx->salt == NULL) {
223 KDFerr(KDF_F_PKEY_SCRYPT_DERIVE, KDF_R_MISSING_SALT);
227 return EVP_PBE_scrypt((char *)kctx->pass, kctx->pass_len, kctx->salt,
228 kctx->salt_len, kctx->N, kctx->r, kctx->p,
229 kctx->maxmem_bytes, key, *keylen);
232 const EVP_PKEY_METHOD scrypt_pkey_meth = {