01e32b6ee1dd89bbe07d08b9570a7ec3b42c3241
[openssl.git] / crypto / asn1 / p5_scrypt.c
1 /*
2  * Copyright 2015-2020 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
10 #include <stdio.h>
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>
17
18 #ifndef OPENSSL_NO_SCRYPT
19 /* PKCS#5 scrypt password based encryption structures */
20
21 ASN1_SEQUENCE(SCRYPT_PARAMS) = {
22         ASN1_SIMPLE(SCRYPT_PARAMS, salt, ASN1_OCTET_STRING),
23         ASN1_SIMPLE(SCRYPT_PARAMS, costParameter, ASN1_INTEGER),
24         ASN1_SIMPLE(SCRYPT_PARAMS, blockSize, ASN1_INTEGER),
25         ASN1_SIMPLE(SCRYPT_PARAMS, parallelizationParameter, ASN1_INTEGER),
26         ASN1_OPT(SCRYPT_PARAMS, keyLength, ASN1_INTEGER),
27 } ASN1_SEQUENCE_END(SCRYPT_PARAMS)
28
29 IMPLEMENT_ASN1_FUNCTIONS(SCRYPT_PARAMS)
30
31 static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen,
32                                     size_t keylen, uint64_t N, uint64_t r,
33                                     uint64_t p);
34
35 /*
36  * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm using scrypt
37  */
38
39 X509_ALGOR *PKCS5_pbe2_set_scrypt(const EVP_CIPHER *cipher,
40                                   const unsigned char *salt, int saltlen,
41                                   unsigned char *aiv, uint64_t N, uint64_t r,
42                                   uint64_t p)
43 {
44     X509_ALGOR *scheme = NULL, *ret = NULL;
45     int alg_nid;
46     size_t keylen = 0;
47     EVP_CIPHER_CTX *ctx = NULL;
48     unsigned char iv[EVP_MAX_IV_LENGTH];
49     PBE2PARAM *pbe2 = NULL;
50
51     if (!cipher) {
52         ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
53         goto err;
54     }
55
56     if (EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
57         ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_SCRYPT_PARAMETERS);
58         goto err;
59     }
60
61     alg_nid = EVP_CIPHER_type(cipher);
62     if (alg_nid == NID_undef) {
63         ERR_raise(ERR_LIB_ASN1, ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
64         goto err;
65     }
66
67     pbe2 = PBE2PARAM_new();
68     if (pbe2 == NULL)
69         goto merr;
70
71     /* Setup the AlgorithmIdentifier for the encryption scheme */
72     scheme = pbe2->encryption;
73
74     scheme->algorithm = OBJ_nid2obj(alg_nid);
75     scheme->parameter = ASN1_TYPE_new();
76     if (scheme->parameter == NULL)
77         goto merr;
78
79     /* Create random IV */
80     if (EVP_CIPHER_iv_length(cipher)) {
81         if (aiv)
82             memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
83         else if (RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)) <= 0)
84             goto err;
85     }
86
87     ctx = EVP_CIPHER_CTX_new();
88     if (ctx == NULL)
89         goto merr;
90
91     /* Dummy cipherinit to just setup the IV */
92     if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0) == 0)
93         goto err;
94     if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) <= 0) {
95         ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
96         goto err;
97     }
98     EVP_CIPHER_CTX_free(ctx);
99     ctx = NULL;
100
101     /* If its RC2 then we'd better setup the key length */
102
103     if (alg_nid == NID_rc2_cbc)
104         keylen = EVP_CIPHER_key_length(cipher);
105
106     /* Setup keyfunc */
107
108     X509_ALGOR_free(pbe2->keyfunc);
109
110     pbe2->keyfunc = pkcs5_scrypt_set(salt, saltlen, keylen, N, r, p);
111
112     if (pbe2->keyfunc == NULL)
113         goto merr;
114
115     /* Now set up top level AlgorithmIdentifier */
116
117     ret = X509_ALGOR_new();
118     if (ret == NULL)
119         goto merr;
120
121     ret->algorithm = OBJ_nid2obj(NID_pbes2);
122
123     /* Encode PBE2PARAM into parameter */
124
125     if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
126                                 &ret->parameter) == NULL)
127         goto merr;
128
129     PBE2PARAM_free(pbe2);
130     pbe2 = NULL;
131
132     return ret;
133
134  merr:
135     ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
136
137  err:
138     PBE2PARAM_free(pbe2);
139     X509_ALGOR_free(ret);
140     EVP_CIPHER_CTX_free(ctx);
141
142     return NULL;
143 }
144
145 static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen,
146                                     size_t keylen, uint64_t N, uint64_t r,
147                                     uint64_t p)
148 {
149     X509_ALGOR *keyfunc = NULL;
150     SCRYPT_PARAMS *sparam = SCRYPT_PARAMS_new();
151
152     if (sparam == NULL)
153         goto merr;
154
155     if (!saltlen)
156         saltlen = PKCS5_SALT_LEN;
157
158     /* This will either copy salt or grow the buffer */
159     if (ASN1_STRING_set(sparam->salt, salt, saltlen) == 0)
160         goto merr;
161
162     if (salt == NULL && RAND_bytes(sparam->salt->data, saltlen) <= 0)
163         goto err;
164
165     if (ASN1_INTEGER_set_uint64(sparam->costParameter, N) == 0)
166         goto merr;
167
168     if (ASN1_INTEGER_set_uint64(sparam->blockSize, r) == 0)
169         goto merr;
170
171     if (ASN1_INTEGER_set_uint64(sparam->parallelizationParameter, p) == 0)
172         goto merr;
173
174     /* If have a key len set it up */
175
176     if (keylen > 0) {
177         sparam->keyLength = ASN1_INTEGER_new();
178         if (sparam->keyLength == NULL)
179             goto merr;
180         if (ASN1_INTEGER_set_int64(sparam->keyLength, keylen) == 0)
181             goto merr;
182     }
183
184     /* Finally setup the keyfunc structure */
185
186     keyfunc = X509_ALGOR_new();
187     if (keyfunc == NULL)
188         goto merr;
189
190     keyfunc->algorithm = OBJ_nid2obj(NID_id_scrypt);
191
192     /* Encode SCRYPT_PARAMS into parameter of pbe2 */
193
194     if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), sparam,
195                                 &keyfunc->parameter) == NULL)
196         goto merr;
197
198     SCRYPT_PARAMS_free(sparam);
199     return keyfunc;
200
201  merr:
202     ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
203  err:
204     SCRYPT_PARAMS_free(sparam);
205     X509_ALGOR_free(keyfunc);
206     return NULL;
207 }
208
209 int PKCS5_v2_scrypt_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
210                              int passlen, ASN1_TYPE *param,
211                              const EVP_CIPHER *c, const EVP_MD *md, int en_de)
212 {
213     unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
214     uint64_t p, r, N;
215     size_t saltlen;
216     size_t keylen = 0;
217     int t, rv = 0;
218     SCRYPT_PARAMS *sparam = NULL;
219
220     if (EVP_CIPHER_CTX_get0_cipher(ctx) == NULL) {
221         ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
222         goto err;
223     }
224
225     /* Decode parameter */
226
227     sparam = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), param);
228
229     if (sparam == NULL) {
230         ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
231         goto err;
232     }
233
234     t = EVP_CIPHER_CTX_key_length(ctx);
235     if (t < 0) {
236         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
237         goto err;
238     }
239     keylen = t;
240
241     /* Now check the parameters of sparam */
242
243     if (sparam->keyLength) {
244         uint64_t spkeylen;
245         if ((ASN1_INTEGER_get_uint64(&spkeylen, sparam->keyLength) == 0)
246             || (spkeylen != keylen)) {
247             ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEYLENGTH);
248             goto err;
249         }
250     }
251     /* Check all parameters fit in uint64_t and are acceptable to scrypt */
252     if (ASN1_INTEGER_get_uint64(&N, sparam->costParameter) == 0
253         || ASN1_INTEGER_get_uint64(&r, sparam->blockSize) == 0
254         || ASN1_INTEGER_get_uint64(&p, sparam->parallelizationParameter) == 0
255         || EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
256         ERR_raise(ERR_LIB_EVP, EVP_R_ILLEGAL_SCRYPT_PARAMETERS);
257         goto err;
258     }
259
260     /* it seems that its all OK */
261
262     salt = sparam->salt->data;
263     saltlen = sparam->salt->length;
264     if (EVP_PBE_scrypt(pass, passlen, salt, saltlen, N, r, p, 0, key, keylen)
265         == 0)
266         goto err;
267     rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
268  err:
269     if (keylen)
270         OPENSSL_cleanse(key, keylen);
271     SCRYPT_PARAMS_free(sparam);
272     return rv;
273 }
274 #endif /* OPENSSL_NO_SCRYPT */