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