2 * Copyright 2015-2016 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
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/err.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509.h>
16 #include <openssl/rand.h>
18 #ifndef OPENSSL_NO_SCRYPT
19 /* PKCS#5 scrypt password based encryption structures */
22 ASN1_OCTET_STRING *salt;
23 ASN1_INTEGER *costParameter;
24 ASN1_INTEGER *blockSize;
25 ASN1_INTEGER *parallelizationParameter;
26 ASN1_INTEGER *keyLength;
29 ASN1_SEQUENCE(SCRYPT_PARAMS) = {
30 ASN1_SIMPLE(SCRYPT_PARAMS, salt, ASN1_OCTET_STRING),
31 ASN1_SIMPLE(SCRYPT_PARAMS, costParameter, ASN1_INTEGER),
32 ASN1_SIMPLE(SCRYPT_PARAMS, blockSize, ASN1_INTEGER),
33 ASN1_SIMPLE(SCRYPT_PARAMS, parallelizationParameter, ASN1_INTEGER),
34 ASN1_OPT(SCRYPT_PARAMS, keyLength, ASN1_INTEGER),
35 } static_ASN1_SEQUENCE_END(SCRYPT_PARAMS)
37 DECLARE_ASN1_ALLOC_FUNCTIONS(SCRYPT_PARAMS)
38 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(SCRYPT_PARAMS)
40 static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen,
41 size_t keylen, uint64_t N, uint64_t r,
45 * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm using scrypt
48 X509_ALGOR *PKCS5_pbe2_set_scrypt(const EVP_CIPHER *cipher,
49 const unsigned char *salt, int saltlen,
50 unsigned char *aiv, uint64_t N, uint64_t r,
53 X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL;
56 EVP_CIPHER_CTX *ctx = NULL;
57 unsigned char iv[EVP_MAX_IV_LENGTH];
58 PBE2PARAM *pbe2 = NULL;
62 ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT, ERR_R_PASSED_NULL_PARAMETER);
66 if (EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
67 ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
68 ASN1_R_INVALID_SCRYPT_PARAMETERS);
72 alg_nid = EVP_CIPHER_type(cipher);
73 if (alg_nid == NID_undef) {
74 ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
75 ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
78 obj = OBJ_nid2obj(alg_nid);
79 pbe2 = PBE2PARAM_new();
83 /* Setup the AlgorithmIdentifier for the encryption scheme */
84 scheme = pbe2->encryption;
86 scheme->algorithm = obj;
87 scheme->parameter = ASN1_TYPE_new();
88 if (scheme->parameter == NULL)
91 /* Create random IV */
92 if (EVP_CIPHER_iv_length(cipher)) {
94 memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
95 else if (RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0)
99 ctx = EVP_CIPHER_CTX_new();
103 /* Dummy cipherinit to just setup the IV */
104 if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0) == 0)
106 if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) < 0) {
107 ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
108 ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
111 EVP_CIPHER_CTX_free(ctx);
114 /* If its RC2 then we'd better setup the key length */
116 if (alg_nid == NID_rc2_cbc)
117 keylen = EVP_CIPHER_key_length(cipher);
121 X509_ALGOR_free(pbe2->keyfunc);
123 pbe2->keyfunc = pkcs5_scrypt_set(salt, saltlen, keylen, N, r, p);
125 if (pbe2->keyfunc == NULL)
128 /* Now set up top level AlgorithmIdentifier */
130 ret = X509_ALGOR_new();
134 ret->algorithm = OBJ_nid2obj(NID_pbes2);
136 /* Encode PBE2PARAM into parameter */
138 if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
139 &ret->parameter) == NULL)
142 PBE2PARAM_free(pbe2);
148 ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT, ERR_R_MALLOC_FAILURE);
151 PBE2PARAM_free(pbe2);
152 X509_ALGOR_free(kalg);
153 X509_ALGOR_free(ret);
154 EVP_CIPHER_CTX_free(ctx);
160 static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen,
161 size_t keylen, uint64_t N, uint64_t r,
164 X509_ALGOR *keyfunc = NULL;
165 SCRYPT_PARAMS *sparam = NULL;
167 sparam = SCRYPT_PARAMS_new();
172 saltlen = PKCS5_SALT_LEN;
174 /* This will either copy salt or grow the buffer */
175 if (ASN1_STRING_set(sparam->salt, salt, saltlen) == 0)
178 if (salt == NULL && RAND_bytes(sparam->salt->data, saltlen) <= 0)
181 if (ASN1_INTEGER_set_uint64(sparam->costParameter, N) == 0)
184 if (ASN1_INTEGER_set_uint64(sparam->blockSize, r) == 0)
187 if (ASN1_INTEGER_set_uint64(sparam->parallelizationParameter, p) == 0)
190 /* If have a key len set it up */
193 sparam->keyLength = ASN1_INTEGER_new();
194 if (sparam->keyLength == NULL)
196 if (ASN1_INTEGER_set_int64(sparam->keyLength, keylen) == 0)
200 /* Finally setup the keyfunc structure */
202 keyfunc = X509_ALGOR_new();
206 keyfunc->algorithm = OBJ_nid2obj(NID_id_scrypt);
208 /* Encode SCRYPT_PARAMS into parameter of pbe2 */
210 if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), sparam,
211 &keyfunc->parameter) == NULL)
214 SCRYPT_PARAMS_free(sparam);
218 ASN1err(ASN1_F_PKCS5_SCRYPT_SET, ERR_R_MALLOC_FAILURE);
220 SCRYPT_PARAMS_free(sparam);
221 X509_ALGOR_free(keyfunc);
225 int PKCS5_v2_scrypt_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
226 int passlen, ASN1_TYPE *param,
227 const EVP_CIPHER *c, const EVP_MD *md, int en_de)
229 unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
234 SCRYPT_PARAMS *sparam = NULL;
236 if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
237 EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, EVP_R_NO_CIPHER_SET);
241 /* Decode parameter */
243 sparam = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), param);
245 if (sparam == NULL) {
246 EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, EVP_R_DECODE_ERROR);
250 keylen = EVP_CIPHER_CTX_key_length(ctx);
252 /* Now check the parameters of sparam */
254 if (sparam->keyLength) {
256 if ((ASN1_INTEGER_get_uint64(&spkeylen, sparam->keyLength) == 0)
257 || (spkeylen != keylen)) {
258 EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN,
259 EVP_R_UNSUPPORTED_KEYLENGTH);
263 /* Check all parameters fit in uint64_t and are acceptable to scrypt */
264 if (ASN1_INTEGER_get_uint64(&N, sparam->costParameter) == 0
265 || ASN1_INTEGER_get_uint64(&r, sparam->blockSize) == 0
266 || ASN1_INTEGER_get_uint64(&p, sparam->parallelizationParameter) == 0
267 || EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
268 EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN,
269 EVP_R_ILLEGAL_SCRYPT_PARAMETERS);
273 /* it seems that its all OK */
275 salt = sparam->salt->data;
276 saltlen = sparam->salt->length;
277 if (EVP_PBE_scrypt(pass, passlen, salt, saltlen, N, r, p, 0, key, keylen)
280 rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
283 OPENSSL_cleanse(key, keylen);
284 SCRYPT_PARAMS_free(sparam);
287 #endif /* OPENSSL_NO_SCRYPT */