Create BIO_read_ex() which handles size_t arguments
[openssl.git] / crypto / evp / p5_crpt2.c
1 /*
2  * Copyright 1999-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 <stdlib.h>
12 #include "internal/cryptlib.h"
13 # include <openssl/x509.h>
14 # include <openssl/evp.h>
15 # include <openssl/hmac.h>
16 # include "evp_locl.h"
17
18 /* set this to print out info about the keygen algorithm */
19 /* #define OPENSSL_DEBUG_PKCS5V2 */
20
21 # ifdef OPENSSL_DEBUG_PKCS5V2
22 static void h__dump(const unsigned char *p, int len);
23 # endif
24
25 /*
26  * This is an implementation of PKCS#5 v2.0 password based encryption key
27  * derivation function PBKDF2. SHA1 version verified against test vectors
28  * posted by Peter Gutmann <pgut001@cs.auckland.ac.nz> to the PKCS-TNG
29  * <pkcs-tng@rsa.com> mailing list.
30  */
31
32 int PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
33                       const unsigned char *salt, int saltlen, int iter,
34                       const EVP_MD *digest, int keylen, unsigned char *out)
35 {
36     unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
37     int cplen, j, k, tkeylen, mdlen;
38     unsigned long i = 1;
39     HMAC_CTX *hctx_tpl = NULL, *hctx = NULL;
40
41     mdlen = EVP_MD_size(digest);
42     if (mdlen < 0)
43         return 0;
44
45     hctx_tpl = HMAC_CTX_new();
46     if (hctx_tpl == NULL)
47         return 0;
48     p = out;
49     tkeylen = keylen;
50     if (!pass)
51         passlen = 0;
52     else if (passlen == -1)
53         passlen = strlen(pass);
54     if (!HMAC_Init_ex(hctx_tpl, pass, passlen, digest, NULL)) {
55         HMAC_CTX_free(hctx_tpl);
56         return 0;
57     }
58     hctx = HMAC_CTX_new();
59     if (hctx == NULL) {
60         HMAC_CTX_free(hctx_tpl);
61         return 0;
62     }
63     while (tkeylen) {
64         if (tkeylen > mdlen)
65             cplen = mdlen;
66         else
67             cplen = tkeylen;
68         /*
69          * We are unlikely to ever use more than 256 blocks (5120 bits!) but
70          * just in case...
71          */
72         itmp[0] = (unsigned char)((i >> 24) & 0xff);
73         itmp[1] = (unsigned char)((i >> 16) & 0xff);
74         itmp[2] = (unsigned char)((i >> 8) & 0xff);
75         itmp[3] = (unsigned char)(i & 0xff);
76         if (!HMAC_CTX_copy(hctx, hctx_tpl)) {
77             HMAC_CTX_free(hctx);
78             HMAC_CTX_free(hctx_tpl);
79             return 0;
80         }
81         if (!HMAC_Update(hctx, salt, saltlen)
82             || !HMAC_Update(hctx, itmp, 4)
83             || !HMAC_Final(hctx, digtmp, NULL)) {
84             HMAC_CTX_free(hctx);
85             HMAC_CTX_free(hctx_tpl);
86             return 0;
87         }
88         HMAC_CTX_reset(hctx);
89         memcpy(p, digtmp, cplen);
90         for (j = 1; j < iter; j++) {
91             if (!HMAC_CTX_copy(hctx, hctx_tpl)) {
92                 HMAC_CTX_free(hctx);
93                 HMAC_CTX_free(hctx_tpl);
94                 return 0;
95             }
96             if (!HMAC_Update(hctx, digtmp, mdlen)
97                 || !HMAC_Final(hctx, digtmp, NULL)) {
98                 HMAC_CTX_free(hctx);
99                 HMAC_CTX_free(hctx_tpl);
100                 return 0;
101             }
102             HMAC_CTX_reset(hctx);
103             for (k = 0; k < cplen; k++)
104                 p[k] ^= digtmp[k];
105         }
106         tkeylen -= cplen;
107         i++;
108         p += cplen;
109     }
110     HMAC_CTX_free(hctx);
111     HMAC_CTX_free(hctx_tpl);
112 # ifdef OPENSSL_DEBUG_PKCS5V2
113     fprintf(stderr, "Password:\n");
114     h__dump(pass, passlen);
115     fprintf(stderr, "Salt:\n");
116     h__dump(salt, saltlen);
117     fprintf(stderr, "Iteration count %d\n", iter);
118     fprintf(stderr, "Key:\n");
119     h__dump(out, keylen);
120 # endif
121     return 1;
122 }
123
124 int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
125                            const unsigned char *salt, int saltlen, int iter,
126                            int keylen, unsigned char *out)
127 {
128     return PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, EVP_sha1(),
129                              keylen, out);
130 }
131
132 # ifdef DO_TEST
133 main()
134 {
135     unsigned char out[4];
136     unsigned char salt[] = { 0x12, 0x34, 0x56, 0x78 };
137     PKCS5_PBKDF2_HMAC_SHA1("password", -1, salt, 4, 5, 4, out);
138     fprintf(stderr, "Out %02X %02X %02X %02X\n",
139             out[0], out[1], out[2], out[3]);
140 }
141
142 # endif
143
144 /*
145  * Now the key derivation function itself. This is a bit evil because it has
146  * to check the ASN1 parameters are valid: and there are quite a few of
147  * them...
148  */
149
150 int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
151                           ASN1_TYPE *param, const EVP_CIPHER *c,
152                           const EVP_MD *md, int en_de)
153 {
154     PBE2PARAM *pbe2 = NULL;
155     const EVP_CIPHER *cipher;
156     EVP_PBE_KEYGEN *kdf;
157
158     int rv = 0;
159
160     pbe2 = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBE2PARAM), param);
161     if (pbe2 == NULL) {
162         EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_DECODE_ERROR);
163         goto err;
164     }
165
166     /* See if we recognise the key derivation function */
167     if (!EVP_PBE_find(EVP_PBE_TYPE_KDF, OBJ_obj2nid(pbe2->keyfunc->algorithm),
168                         NULL, NULL, &kdf)) {
169         EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN,
170                EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
171         goto err;
172     }
173
174     /*
175      * lets see if we recognise the encryption algorithm.
176      */
177
178     cipher = EVP_get_cipherbyobj(pbe2->encryption->algorithm);
179
180     if (!cipher) {
181         EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_UNSUPPORTED_CIPHER);
182         goto err;
183     }
184
185     /* Fixup cipher based on AlgorithmIdentifier */
186     if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
187         goto err;
188     if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) < 0) {
189         EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_CIPHER_PARAMETER_ERROR);
190         goto err;
191     }
192     rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de);
193  err:
194     PBE2PARAM_free(pbe2);
195     return rv;
196 }
197
198 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
199                              int passlen, ASN1_TYPE *param,
200                              const EVP_CIPHER *c, const EVP_MD *md, int en_de)
201 {
202     unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
203     int saltlen, iter;
204     int rv = 0;
205     unsigned int keylen = 0;
206     int prf_nid, hmac_md_nid;
207     PBKDF2PARAM *kdf = NULL;
208     const EVP_MD *prfmd;
209
210     if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
211         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_NO_CIPHER_SET);
212         goto err;
213     }
214     keylen = EVP_CIPHER_CTX_key_length(ctx);
215     OPENSSL_assert(keylen <= sizeof key);
216
217     /* Decode parameter */
218
219     kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param);
220
221     if (kdf == NULL) {
222         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_DECODE_ERROR);
223         goto err;
224     }
225
226     keylen = EVP_CIPHER_CTX_key_length(ctx);
227
228     /* Now check the parameters of the kdf */
229
230     if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) {
231         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_KEYLENGTH);
232         goto err;
233     }
234
235     if (kdf->prf)
236         prf_nid = OBJ_obj2nid(kdf->prf->algorithm);
237     else
238         prf_nid = NID_hmacWithSHA1;
239
240     if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) {
241         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_PRF);
242         goto err;
243     }
244
245     prfmd = EVP_get_digestbynid(hmac_md_nid);
246     if (prfmd == NULL) {
247         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_PRF);
248         goto err;
249     }
250
251     if (kdf->salt->type != V_ASN1_OCTET_STRING) {
252         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_SALT_TYPE);
253         goto err;
254     }
255
256     /* it seems that its all OK */
257     salt = kdf->salt->value.octet_string->data;
258     saltlen = kdf->salt->value.octet_string->length;
259     iter = ASN1_INTEGER_get(kdf->iter);
260     if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, prfmd,
261                            keylen, key))
262         goto err;
263     rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
264  err:
265     OPENSSL_cleanse(key, keylen);
266     PBKDF2PARAM_free(kdf);
267     return rv;
268 }
269
270 # ifdef OPENSSL_DEBUG_PKCS5V2
271 static void h__dump(const unsigned char *p, int len)
272 {
273     for (; len--; p++)
274         fprintf(stderr, "%02X ", *p);
275     fprintf(stderr, "\n");
276 }
277 # endif