2015-05-19 19:43:12 +08:00
|
|
|
/*
|
2018-04-03 20:57:12 +08:00
|
|
|
* Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2015-05-19 19:43:12 +08:00
|
|
|
*
|
2018-12-06 20:40:06 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:24:46 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2015-05-19 19:43:12 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/evp.h>
|
2015-05-21 08:16:31 +08:00
|
|
|
#include <openssl/err.h>
|
2018-06-22 05:16:18 +08:00
|
|
|
#include <openssl/kdf.h>
|
2019-02-27 16:53:17 +08:00
|
|
|
#include "internal/numbers.h"
|
2015-05-19 19:43:12 +08:00
|
|
|
|
2015-09-05 01:07:10 +08:00
|
|
|
#ifndef OPENSSL_NO_SCRYPT
|
|
|
|
|
2015-05-19 19:43:12 +08:00
|
|
|
/*
|
|
|
|
* Maximum permitted memory allow this to be overridden with Configuration
|
|
|
|
* option: e.g. -DSCRYPT_MAX_MEM=0 for maximum possible.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef SCRYPT_MAX_MEM
|
|
|
|
# if SCRYPT_MAX_MEM == 0
|
|
|
|
# undef SCRYPT_MAX_MEM
|
|
|
|
/*
|
|
|
|
* Although we could theoretically allocate SIZE_MAX memory that would leave
|
|
|
|
* no memory available for anything else so set limit as half that.
|
|
|
|
*/
|
|
|
|
# define SCRYPT_MAX_MEM (SIZE_MAX/2)
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
/* Default memory limit: 32 MB */
|
|
|
|
# define SCRYPT_MAX_MEM (1024 * 1024 * 32)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int EVP_PBE_scrypt(const char *pass, size_t passlen,
|
|
|
|
const unsigned char *salt, size_t saltlen,
|
|
|
|
uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem,
|
|
|
|
unsigned char *key, size_t keylen)
|
|
|
|
{
|
2018-06-22 05:16:18 +08:00
|
|
|
const char *empty = "";
|
|
|
|
int rv = 1;
|
|
|
|
EVP_KDF_CTX *kctx;
|
|
|
|
|
2019-02-18 00:13:08 +08:00
|
|
|
if (r > UINT32_MAX || p > UINT32_MAX) {
|
|
|
|
EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_PARAMETER_TOO_LARGE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
/* Maintain existing behaviour. */
|
|
|
|
if (pass == NULL) {
|
|
|
|
pass = empty;
|
|
|
|
passlen = 0;
|
2015-05-19 19:43:12 +08:00
|
|
|
}
|
2019-03-14 22:23:04 +08:00
|
|
|
if (salt == NULL) {
|
|
|
|
salt = (const unsigned char *)empty;
|
|
|
|
saltlen = 0;
|
|
|
|
}
|
2015-05-19 19:43:12 +08:00
|
|
|
if (maxmem == 0)
|
|
|
|
maxmem = SCRYPT_MAX_MEM;
|
2017-09-15 06:01:42 +08:00
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
kctx = EVP_KDF_CTX_new_id(EVP_KDF_SCRYPT);
|
|
|
|
if (kctx == NULL)
|
2015-05-19 19:43:12 +08:00
|
|
|
return 0;
|
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PASS, pass, (size_t)passlen) != 1
|
|
|
|
|| EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT,
|
|
|
|
salt, (size_t)saltlen) != 1
|
|
|
|
|| EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SCRYPT_N, N) != 1
|
|
|
|
|| EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SCRYPT_R, (uint32_t)r) != 1
|
|
|
|
|| EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SCRYPT_P, (uint32_t)p) != 1
|
|
|
|
|| EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAXMEM_BYTES, maxmem) != 1
|
|
|
|
|| EVP_KDF_derive(kctx, key, keylen) != 1)
|
|
|
|
rv = 0;
|
|
|
|
|
|
|
|
EVP_KDF_CTX_free(kctx);
|
2015-05-19 19:43:12 +08:00
|
|
|
return rv;
|
|
|
|
}
|
2018-06-22 05:16:18 +08:00
|
|
|
|
2015-09-05 01:07:10 +08:00
|
|
|
#endif
|