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