]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/p5_crpt2.c
rand: remove unimplemented librandom stub code
[thirdparty/openssl.git] / crypto / evp / p5_crpt2.c
CommitLineData
0f113f3e 1/*
da1c088f 2 * Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved.
97e4a932 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
97e4a932 8 */
62867571 9
97e4a932
DSH
10#include <stdio.h>
11#include <stdlib.h>
b39fc560 12#include "internal/cryptlib.h"
5a285add
DM
13#include <openssl/x509.h>
14#include <openssl/evp.h>
15#include <openssl/kdf.h>
16#include <openssl/hmac.h>
3a9b3d2d 17#include <openssl/trace.h>
7707526b 18#include <openssl/core_names.h>
25f2138b 19#include "crypto/evp.h"
706457b7 20#include "evp_local.h"
97e4a932 21
4e17fb00
SL
22int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen,
23 const unsigned char *salt, int saltlen, int iter,
b536880c 24 const EVP_MD *digest, int keylen, unsigned char *out,
4e17fb00 25 OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e 26{
fa013b65 27 const char *empty = "";
7707526b
P
28 int rv = 1, mode = 1;
29 EVP_KDF *kdf;
5a285add 30 EVP_KDF_CTX *kctx;
ed576acd 31 const char *mdname = EVP_MD_get0_name(digest);
7707526b 32 OSSL_PARAM params[6], *p = params;
0f113f3e 33
5a285add 34 /* Keep documented behaviour. */
fa013b65
PB
35 if (pass == NULL) {
36 pass = empty;
0f113f3e 37 passlen = 0;
fa013b65 38 } else if (passlen == -1) {
0f113f3e 39 passlen = strlen(pass);
fa013b65 40 }
5a285add
DM
41 if (salt == NULL && saltlen == 0)
42 salt = (unsigned char *)empty;
43
5ccada09 44 kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, propq);
5f1424c6 45 if (kdf == NULL)
46 return 0;
660c5344 47 kctx = EVP_KDF_CTX_new(kdf);
7707526b 48 EVP_KDF_free(kdf);
5a285add 49 if (kctx == NULL)
0f113f3e 50 return 0;
7707526b
P
51 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
52 (char *)pass, (size_t)passlen);
53 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
54 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
55 (unsigned char *)salt, saltlen);
56 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
57 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
8b6ffd40 58 (char *)mdname, 0);
7707526b 59 *p = OSSL_PARAM_construct_end();
36fae6e8 60 if (EVP_KDF_derive(kctx, out, keylen, params) != 1)
5a285add
DM
61 rv = 0;
62
660c5344 63 EVP_KDF_CTX_free(kctx);
5a285add 64
3a9b3d2d
RL
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);
5a285add 80 return rv;
0f113f3e 81}
856640b5 82
5ccada09
SL
83int 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{
4e17fb00
SL
87 return ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, digest,
88 keylen, out, NULL, NULL);
5ccada09
SL
89}
90
91
856640b5 92int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
0f113f3e
MC
93 const unsigned char *salt, int saltlen, int iter,
94 int keylen, unsigned char *out)
95{
2ec64916
P
96 EVP_MD *digest;
97 int r = 0;
98
99 if ((digest = EVP_MD_fetch(NULL, SN_sha1, NULL)) != NULL)
100 r = ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter,
101 digest, keylen, out, NULL, NULL);
102 EVP_MD_free(digest);
103 return r;
0f113f3e
MC
104}
105
0f113f3e
MC
106/*
107 * Now the key derivation function itself. This is a bit evil because it has
108 * to check the ASN1 parameters are valid: and there are quite a few of
109 * them...
97e4a932
DSH
110 */
111
b536880c
JS
112int PKCS5_v2_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
113 ASN1_TYPE *param, const EVP_CIPHER *c,
114 const EVP_MD *md, int en_de,
115 OSSL_LIB_CTX *libctx, const char *propq)
97e4a932 116{
0f113f3e 117 PBE2PARAM *pbe2 = NULL;
b536880c
JS
118 char ciph_name[80];
119 const EVP_CIPHER *cipher = NULL;
120 EVP_CIPHER *cipher_fetch = NULL;
121 EVP_PBE_KEYGEN_EX *kdf;
0f113f3e
MC
122
123 int rv = 0;
124
e93c8748
DSH
125 pbe2 = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBE2PARAM), param);
126 if (pbe2 == NULL) {
9311d0c4 127 ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
0f113f3e
MC
128 goto err;
129 }
130
131 /* See if we recognise the key derivation function */
b536880c
JS
132 if (!EVP_PBE_find_ex(EVP_PBE_TYPE_KDF, OBJ_obj2nid(pbe2->keyfunc->algorithm),
133 NULL, NULL, NULL, &kdf)) {
9311d0c4 134 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
0f113f3e
MC
135 goto err;
136 }
137
138 /*
139 * lets see if we recognise the encryption algorithm.
140 */
b536880c
JS
141 if (OBJ_obj2txt(ciph_name, sizeof(ciph_name), pbe2->encryption->algorithm, 0) <= 0) {
142 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
143 goto err;
144 }
0f113f3e 145
3c15d677 146 (void)ERR_set_mark();
b536880c
JS
147 cipher = cipher_fetch = EVP_CIPHER_fetch(libctx, ciph_name, propq);
148 /* Fallback to legacy method */
149 if (cipher == NULL)
150 cipher = EVP_get_cipherbyname(ciph_name);
0f113f3e 151
b536880c 152 if (cipher == NULL) {
3c15d677 153 (void)ERR_clear_last_mark();
9311d0c4 154 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
0f113f3e
MC
155 goto err;
156 }
3c15d677 157 (void)ERR_pop_to_mark();
0f113f3e
MC
158
159 /* Fixup cipher based on AlgorithmIdentifier */
160 if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
161 goto err;
114d99b4 162 if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) <= 0) {
9311d0c4 163 ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
0f113f3e
MC
164 goto err;
165 }
b536880c 166 rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de, libctx, propq);
0f113f3e 167 err:
b536880c 168 EVP_CIPHER_free(cipher_fetch);
0f113f3e
MC
169 PBE2PARAM_free(pbe2);
170 return rv;
3d63b396
DSH
171}
172
b536880c
JS
173int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
174 ASN1_TYPE *param, const EVP_CIPHER *c,
175 const EVP_MD *md, int en_de)
176{
177 return PKCS5_v2_PBE_keyivgen_ex(ctx, pass, passlen, param, c, md, en_de, NULL, NULL);
178}
179
180int PKCS5_v2_PBKDF2_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass,
181 int passlen, ASN1_TYPE *param,
182 const EVP_CIPHER *c, const EVP_MD *md, int en_de,
183 OSSL_LIB_CTX *libctx, const char *propq)
3d63b396 184{
0f113f3e 185 unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
ea053ec9 186 int saltlen, iter, t;
0f113f3e
MC
187 int rv = 0;
188 unsigned int keylen = 0;
189 int prf_nid, hmac_md_nid;
190 PBKDF2PARAM *kdf = NULL;
b536880c
JS
191 const EVP_MD *prfmd = NULL;
192 EVP_MD *prfmd_fetch = NULL;
0f113f3e 193
f6c95e46 194 if (EVP_CIPHER_CTX_get0_cipher(ctx) == NULL) {
9311d0c4 195 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
0f113f3e
MC
196 goto err;
197 }
ed576acd 198 keylen = EVP_CIPHER_CTX_get_key_length(ctx);
cbe29648 199 OPENSSL_assert(keylen <= sizeof(key));
0f113f3e
MC
200
201 /* Decode parameter */
202
e93c8748 203 kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param);
0f113f3e 204
e93c8748 205 if (kdf == NULL) {
9311d0c4 206 ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
0f113f3e
MC
207 goto err;
208 }
209
ed576acd 210 t = EVP_CIPHER_CTX_get_key_length(ctx);
ea053ec9 211 if (t < 0) {
9311d0c4 212 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
ea053ec9
P
213 goto err;
214 }
215 keylen = t;
0f113f3e
MC
216
217 /* Now check the parameters of the kdf */
218
219 if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) {
9311d0c4 220 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEYLENGTH);
0f113f3e
MC
221 goto err;
222 }
223
224 if (kdf->prf)
225 prf_nid = OBJ_obj2nid(kdf->prf->algorithm);
226 else
227 prf_nid = NID_hmacWithSHA1;
228
229 if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) {
9311d0c4 230 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF);
0f113f3e
MC
231 goto err;
232 }
233
dc4ccc70 234 (void)ERR_set_mark();
b536880c
JS
235 prfmd = prfmd_fetch = EVP_MD_fetch(libctx, OBJ_nid2sn(hmac_md_nid), propq);
236 if (prfmd == NULL)
237 prfmd = EVP_get_digestbynid(hmac_md_nid);
0f113f3e 238 if (prfmd == NULL) {
dc4ccc70 239 (void)ERR_clear_last_mark();
9311d0c4 240 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF);
0f113f3e
MC
241 goto err;
242 }
dc4ccc70 243 (void)ERR_pop_to_mark();
0f113f3e
MC
244
245 if (kdf->salt->type != V_ASN1_OCTET_STRING) {
9311d0c4 246 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_SALT_TYPE);
0f113f3e
MC
247 goto err;
248 }
249
250 /* it seems that its all OK */
251 salt = kdf->salt->value.octet_string->data;
252 saltlen = kdf->salt->value.octet_string->length;
253 iter = ASN1_INTEGER_get(kdf->iter);
b536880c
JS
254 if (!ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, prfmd,
255 keylen, key, libctx, propq))
0f113f3e
MC
256 goto err;
257 rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
258 err:
259 OPENSSL_cleanse(key, keylen);
260 PBKDF2PARAM_free(kdf);
b536880c 261 EVP_MD_free(prfmd_fetch);
0f113f3e 262 return rv;
97e4a932 263}
b536880c
JS
264
265int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
266 int passlen, ASN1_TYPE *param,
267 const EVP_CIPHER *c, const EVP_MD *md, int en_de)
268{
269 return PKCS5_v2_PBKDF2_keyivgen_ex(ctx, pass, passlen, param, c, md, en_de,
270 NULL, NULL);
271}