Adapt all EVP_CIPHER_CTX users for it becoming opaque
[openssl.git] / crypto / asn1 / p5_scrypt.c
1 /* p5_scrypt.c */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4  * 2015.
5  */
6 /* ====================================================================
7  * Copyright (c) 2015 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59
60 #include <stdio.h>
61 #include "internal/cryptlib.h"
62 #include <openssl/asn1t.h>
63 #include <openssl/err.h>
64 #include <openssl/evp.h>
65 #include <openssl/x509.h>
66 #include <openssl/rand.h>
67
68 #ifndef OPENSSL_NO_SCRYPT
69 /* PKCS#5 scrypt password based encryption structures */
70
71 typedef struct {
72     ASN1_OCTET_STRING *salt;
73     ASN1_INTEGER *costParameter;
74     ASN1_INTEGER *blockSize;
75     ASN1_INTEGER *parallelizationParameter;
76     ASN1_INTEGER *keyLength;
77 } SCRYPT_PARAMS;
78
79 ASN1_SEQUENCE(SCRYPT_PARAMS) = {
80         ASN1_SIMPLE(SCRYPT_PARAMS, salt, ASN1_OCTET_STRING),
81         ASN1_SIMPLE(SCRYPT_PARAMS, costParameter, ASN1_INTEGER),
82         ASN1_SIMPLE(SCRYPT_PARAMS, blockSize, ASN1_INTEGER),
83         ASN1_SIMPLE(SCRYPT_PARAMS, parallelizationParameter, ASN1_INTEGER),
84         ASN1_OPT(SCRYPT_PARAMS, keyLength, ASN1_INTEGER),
85 } static_ASN1_SEQUENCE_END(SCRYPT_PARAMS)
86
87 DECLARE_ASN1_ALLOC_FUNCTIONS(SCRYPT_PARAMS)
88 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(SCRYPT_PARAMS)
89
90 static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen,
91                                     size_t keylen, uint64_t N, uint64_t r,
92                                     uint64_t p);
93
94 /*
95  * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm using scrypt
96  */
97
98 X509_ALGOR *PKCS5_pbe2_set_scrypt(const EVP_CIPHER *cipher,
99                                   const unsigned char *salt, int saltlen,
100                                   unsigned char *aiv, uint64_t N, uint64_t r,
101                                   uint64_t p)
102 {
103     X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL;
104     int alg_nid;
105     size_t keylen = 0;
106     EVP_CIPHER_CTX *ctx = NULL;
107     unsigned char iv[EVP_MAX_IV_LENGTH];
108     PBE2PARAM *pbe2 = NULL;
109     ASN1_OBJECT *obj;
110
111     if (!cipher) {
112         ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT, ERR_R_PASSED_NULL_PARAMETER);
113         goto err;
114     }
115
116     if (EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
117         ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
118                 ASN1_R_INVALID_SCRYPT_PARAMETERS);
119         goto err;
120     }
121
122     alg_nid = EVP_CIPHER_type(cipher);
123     if (alg_nid == NID_undef) {
124         ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
125                 ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
126         goto err;
127     }
128     obj = OBJ_nid2obj(alg_nid);
129     pbe2 = PBE2PARAM_new();
130     if (pbe2 == NULL)
131         goto merr;
132
133     /* Setup the AlgorithmIdentifier for the encryption scheme */
134     scheme = pbe2->encryption;
135
136     scheme->algorithm = obj;
137     scheme->parameter = ASN1_TYPE_new();
138     if (scheme->parameter == NULL)
139         goto merr;
140
141     /* Create random IV */
142     if (EVP_CIPHER_iv_length(cipher)) {
143         if (aiv)
144             memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
145         else if (RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0)
146             goto err;
147     }
148
149     ctx = EVP_CIPHER_CTX_new();
150     if (ctx == NULL)
151         goto merr;
152
153     /* Dummy cipherinit to just setup the IV */
154     if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0) == 0)
155         goto err;
156     if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) < 0) {
157         ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
158                 ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
159         goto err;
160     }
161     EVP_CIPHER_CTX_free(ctx);
162     ctx = NULL;
163
164     /* If its RC2 then we'd better setup the key length */
165
166     if (alg_nid == NID_rc2_cbc)
167         keylen = EVP_CIPHER_key_length(cipher);
168
169     /* Setup keyfunc */
170
171     X509_ALGOR_free(pbe2->keyfunc);
172
173     pbe2->keyfunc = pkcs5_scrypt_set(salt, saltlen, keylen, N, r, p);
174
175     if (pbe2->keyfunc == NULL)
176         goto merr;
177
178     /* Now set up top level AlgorithmIdentifier */
179
180     ret = X509_ALGOR_new();
181     if (ret == NULL)
182         goto merr;
183
184     ret->algorithm = OBJ_nid2obj(NID_pbes2);
185
186     /* Encode PBE2PARAM into parameter */
187
188     if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
189                                 &ret->parameter) == NULL)
190         goto merr;
191
192     PBE2PARAM_free(pbe2);
193     pbe2 = NULL;
194
195     return ret;
196
197  merr:
198     ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT, ERR_R_MALLOC_FAILURE);
199
200  err:
201     PBE2PARAM_free(pbe2);
202     X509_ALGOR_free(kalg);
203     X509_ALGOR_free(ret);
204     EVP_CIPHER_CTX_free(ctx);
205
206     return NULL;
207
208 }
209
210 static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen,
211                                     size_t keylen, uint64_t N, uint64_t r,
212                                     uint64_t p)
213 {
214     X509_ALGOR *keyfunc = NULL;
215     SCRYPT_PARAMS *sparam = NULL;
216
217     sparam = SCRYPT_PARAMS_new();
218     if (sparam == NULL)
219         goto merr;
220
221     if (!saltlen)
222         saltlen = PKCS5_SALT_LEN;
223
224     /* This will either copy salt or grow the buffer */
225     if (ASN1_STRING_set(sparam->salt, salt, saltlen) == 0)
226         goto merr;
227
228     if (salt == NULL && RAND_bytes(sparam->salt->data, saltlen) <= 0)
229         goto err;
230
231     if (ASN1_INTEGER_set_uint64(sparam->costParameter, N) == 0)
232         goto merr;
233
234     if (ASN1_INTEGER_set_uint64(sparam->blockSize, r) == 0)
235         goto merr;
236
237     if (ASN1_INTEGER_set_uint64(sparam->parallelizationParameter, p) == 0)
238         goto merr;
239
240     /* If have a key len set it up */
241
242     if (keylen > 0) {
243         sparam->keyLength = ASN1_INTEGER_new();
244         if (sparam->keyLength == NULL)
245             goto merr;
246         if (ASN1_INTEGER_set_int64(sparam->keyLength, keylen) == 0)
247             goto merr;
248     }
249
250     /* Finally setup the keyfunc structure */
251
252     keyfunc = X509_ALGOR_new();
253     if (keyfunc == NULL)
254         goto merr;
255
256     keyfunc->algorithm = OBJ_nid2obj(NID_id_scrypt);
257
258     /* Encode SCRYPT_PARAMS into parameter of pbe2 */
259
260     if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), sparam,
261                                 &keyfunc->parameter) == NULL)
262         goto merr;
263
264     SCRYPT_PARAMS_free(sparam);
265     return keyfunc;
266
267  merr:
268     ASN1err(ASN1_F_PKCS5_SCRYPT_SET, ERR_R_MALLOC_FAILURE);
269  err:
270     SCRYPT_PARAMS_free(sparam);
271     X509_ALGOR_free(keyfunc);
272     return NULL;
273 }
274
275 int PKCS5_v2_scrypt_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
276                              int passlen, ASN1_TYPE *param,
277                              const EVP_CIPHER *c, const EVP_MD *md, int en_de)
278 {
279     unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
280     uint64_t p, r, N;
281     size_t saltlen;
282     size_t keylen = 0;
283     int rv = 0;
284     SCRYPT_PARAMS *sparam = NULL;
285
286     if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
287         EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, EVP_R_NO_CIPHER_SET);
288         goto err;
289     }
290
291     /* Decode parameter */
292
293     sparam = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), param);
294
295     if (sparam == NULL) {
296         EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, EVP_R_DECODE_ERROR);
297         goto err;
298     }
299
300     keylen = EVP_CIPHER_CTX_key_length(ctx);
301
302     /* Now check the parameters of sparam */
303
304     if (sparam->keyLength) {
305         uint64_t spkeylen;
306         if ((ASN1_INTEGER_get_uint64(&spkeylen, sparam->keyLength) == 0)
307             || (spkeylen != keylen)) {
308             EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN,
309                    EVP_R_UNSUPPORTED_KEYLENGTH);
310             goto err;
311         }
312     }
313     /* Check all parameters fit in uint64_t and are acceptable to scrypt */
314     if (ASN1_INTEGER_get_uint64(&N, sparam->costParameter) == 0
315         || ASN1_INTEGER_get_uint64(&r, sparam->blockSize) == 0
316         || ASN1_INTEGER_get_uint64(&p, sparam->parallelizationParameter) == 0
317         || EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
318         EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN,
319                EVP_R_ILLEGAL_SCRYPT_PARAMETERS);
320         goto err;
321     }
322
323     /* it seems that its all OK */
324
325     salt = sparam->salt->data;
326     saltlen = sparam->salt->length;
327     if (EVP_PBE_scrypt(pass, passlen, salt, saltlen, N, r, p, 0, key, keylen)
328         == 0)
329         goto err;
330     rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
331  err:
332     if (keylen)
333         OPENSSL_cleanse(key, keylen);
334     SCRYPT_PARAMS_free(sparam);
335     return rv;
336 }
337 #endif /* OPENSSL_NO_SCRYPT */