c097210bd48d317ad26081cbb0ab4c7a23aa382c
[openssl.git] / crypto / evp / p5_crpt2.c
1 /*
2  * Copyright 1999-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 <stdlib.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/x509.h>
14 #include <openssl/evp.h>
15 #include <openssl/kdf.h>
16 #include <openssl/hmac.h>
17 #include <openssl/trace.h>
18 #include <openssl/core_names.h>
19 #include "crypto/evp.h"
20 #include "evp_local.h"
21
22 int pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen,
23                          const unsigned char *salt, int saltlen, int iter,
24                          const EVP_MD *digest, int keylen, unsigned char *out,
25                          OSSL_LIB_CTX *libctx, const char *propq)
26 {
27     const char *empty = "";
28     int rv = 1, mode = 1;
29     EVP_KDF *kdf;
30     EVP_KDF_CTX *kctx;
31     const char *mdname = EVP_MD_name(digest);
32     OSSL_PARAM params[6], *p = params;
33
34     /* Keep documented behaviour. */
35     if (pass == NULL) {
36         pass = empty;
37         passlen = 0;
38     } else if (passlen == -1) {
39         passlen = strlen(pass);
40     }
41     if (salt == NULL && saltlen == 0)
42         salt = (unsigned char *)empty;
43
44     kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, propq);
45     kctx = EVP_KDF_CTX_new(kdf);
46     EVP_KDF_free(kdf);
47     if (kctx == NULL)
48         return 0;
49     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
50                                              (char *)pass, (size_t)passlen);
51     *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
52     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
53                                              (unsigned char *)salt, saltlen);
54     *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
55     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
56                                             (char *)mdname, 0);
57     *p = OSSL_PARAM_construct_end();
58     if (EVP_KDF_CTX_set_params(kctx, params) != 1
59             || EVP_KDF_derive(kctx, out, keylen) != 1)
60         rv = 0;
61
62     EVP_KDF_CTX_free(kctx);
63
64     OSSL_TRACE_BEGIN(PKCS5V2) {
65         BIO_printf(trc_out, "Password:\n");
66         BIO_hex_string(trc_out,
67                        0, passlen, pass, passlen);
68         BIO_printf(trc_out, "\n");
69         BIO_printf(trc_out, "Salt:\n");
70         BIO_hex_string(trc_out,
71                        0, saltlen, salt, saltlen);
72         BIO_printf(trc_out, "\n");
73         BIO_printf(trc_out, "Iteration count %d\n", iter);
74         BIO_printf(trc_out, "Key:\n");
75         BIO_hex_string(trc_out,
76                        0, keylen, out, keylen);
77         BIO_printf(trc_out, "\n");
78     } OSSL_TRACE_END(PKCS5V2);
79     return rv;
80 }
81
82 int PKCS5_PBKDF2_HMAC(const char *pass, int passlen, const unsigned char *salt,
83                       int saltlen, int iter, const EVP_MD *digest, int keylen,
84                       unsigned char *out)
85 {
86     return pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, digest,
87                                 keylen, out, NULL, NULL);
88 }
89
90
91 int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
92                            const unsigned char *salt, int saltlen, int iter,
93                            int keylen, unsigned char *out)
94 {
95     return PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, EVP_sha1(),
96                              keylen, out);
97 }
98
99 /*
100  * Now the key derivation function itself. This is a bit evil because it has
101  * to check the ASN1 parameters are valid: and there are quite a few of
102  * them...
103  */
104
105 int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
106                           ASN1_TYPE *param, const EVP_CIPHER *c,
107                           const EVP_MD *md, int en_de)
108 {
109     PBE2PARAM *pbe2 = NULL;
110     const EVP_CIPHER *cipher;
111     EVP_PBE_KEYGEN *kdf;
112
113     int rv = 0;
114
115     pbe2 = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBE2PARAM), param);
116     if (pbe2 == NULL) {
117         ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
118         goto err;
119     }
120
121     /* See if we recognise the key derivation function */
122     if (!EVP_PBE_find(EVP_PBE_TYPE_KDF, OBJ_obj2nid(pbe2->keyfunc->algorithm),
123                         NULL, NULL, &kdf)) {
124         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
125         goto err;
126     }
127
128     /*
129      * lets see if we recognise the encryption algorithm.
130      */
131
132     cipher = EVP_get_cipherbyobj(pbe2->encryption->algorithm);
133
134     if (!cipher) {
135         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
136         goto err;
137     }
138
139     /* Fixup cipher based on AlgorithmIdentifier */
140     if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
141         goto err;
142     if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) < 0) {
143         ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
144         goto err;
145     }
146     rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de);
147  err:
148     PBE2PARAM_free(pbe2);
149     return rv;
150 }
151
152 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
153                              int passlen, ASN1_TYPE *param,
154                              const EVP_CIPHER *c, const EVP_MD *md, int en_de)
155 {
156     unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
157     int saltlen, iter, t;
158     int rv = 0;
159     unsigned int keylen = 0;
160     int prf_nid, hmac_md_nid;
161     PBKDF2PARAM *kdf = NULL;
162     const EVP_MD *prfmd;
163
164     if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
165         ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
166         goto err;
167     }
168     keylen = EVP_CIPHER_CTX_key_length(ctx);
169     OPENSSL_assert(keylen <= sizeof(key));
170
171     /* Decode parameter */
172
173     kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param);
174
175     if (kdf == NULL) {
176         ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
177         goto err;
178     }
179
180     t = EVP_CIPHER_CTX_key_length(ctx);
181     if (t < 0) {
182         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
183         goto err;
184     }
185     keylen = t;
186
187     /* Now check the parameters of the kdf */
188
189     if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) {
190         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEYLENGTH);
191         goto err;
192     }
193
194     if (kdf->prf)
195         prf_nid = OBJ_obj2nid(kdf->prf->algorithm);
196     else
197         prf_nid = NID_hmacWithSHA1;
198
199     if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) {
200         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF);
201         goto err;
202     }
203
204     prfmd = EVP_get_digestbynid(hmac_md_nid);
205     if (prfmd == NULL) {
206         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF);
207         goto err;
208     }
209
210     if (kdf->salt->type != V_ASN1_OCTET_STRING) {
211         ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_SALT_TYPE);
212         goto err;
213     }
214
215     /* it seems that its all OK */
216     salt = kdf->salt->value.octet_string->data;
217     saltlen = kdf->salt->value.octet_string->length;
218     iter = ASN1_INTEGER_get(kdf->iter);
219     if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, prfmd,
220                            keylen, key))
221         goto err;
222     rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
223  err:
224     OPENSSL_cleanse(key, keylen);
225     PBKDF2PARAM_free(kdf);
226     return rv;
227 }