830a97dde25c28a8d78470cf36190f0e4dcce0d0
[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         EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, 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         EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN,
125                EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
126         goto err;
127     }
128
129     /*
130      * lets see if we recognise the encryption algorithm.
131      */
132
133     cipher = EVP_get_cipherbyobj(pbe2->encryption->algorithm);
134
135     if (!cipher) {
136         EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_UNSUPPORTED_CIPHER);
137         goto err;
138     }
139
140     /* Fixup cipher based on AlgorithmIdentifier */
141     if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
142         goto err;
143     if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) < 0) {
144         EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_CIPHER_PARAMETER_ERROR);
145         goto err;
146     }
147     rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de);
148  err:
149     PBE2PARAM_free(pbe2);
150     return rv;
151 }
152
153 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
154                              int passlen, ASN1_TYPE *param,
155                              const EVP_CIPHER *c, const EVP_MD *md, int en_de)
156 {
157     unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
158     int saltlen, iter, t;
159     int rv = 0;
160     unsigned int keylen = 0;
161     int prf_nid, hmac_md_nid;
162     PBKDF2PARAM *kdf = NULL;
163     const EVP_MD *prfmd;
164
165     if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
166         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_NO_CIPHER_SET);
167         goto err;
168     }
169     keylen = EVP_CIPHER_CTX_key_length(ctx);
170     OPENSSL_assert(keylen <= sizeof(key));
171
172     /* Decode parameter */
173
174     kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param);
175
176     if (kdf == NULL) {
177         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_DECODE_ERROR);
178         goto err;
179     }
180
181     t = EVP_CIPHER_CTX_key_length(ctx);
182     if (t < 0) {
183         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_INVALID_KEY_LENGTH);
184         goto err;
185     }
186     keylen = t;
187
188     /* Now check the parameters of the kdf */
189
190     if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) {
191         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_KEYLENGTH);
192         goto err;
193     }
194
195     if (kdf->prf)
196         prf_nid = OBJ_obj2nid(kdf->prf->algorithm);
197     else
198         prf_nid = NID_hmacWithSHA1;
199
200     if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) {
201         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_PRF);
202         goto err;
203     }
204
205     prfmd = EVP_get_digestbynid(hmac_md_nid);
206     if (prfmd == NULL) {
207         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_PRF);
208         goto err;
209     }
210
211     if (kdf->salt->type != V_ASN1_OCTET_STRING) {
212         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_SALT_TYPE);
213         goto err;
214     }
215
216     /* it seems that its all OK */
217     salt = kdf->salt->value.octet_string->data;
218     saltlen = kdf->salt->value.octet_string->length;
219     iter = ASN1_INTEGER_get(kdf->iter);
220     if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, prfmd,
221                            keylen, key))
222         goto err;
223     rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
224  err:
225     OPENSSL_cleanse(key, keylen);
226     PBKDF2PARAM_free(kdf);
227     return rv;
228 }