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