2 * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include "crypto/asn1.h"
13 #include <openssl/asn1t.h>
14 #include <openssl/core.h>
15 #include <openssl/core_names.h>
16 #include <openssl/x509.h>
17 #include <openssl/rand.h>
19 /* PKCS#5 v2.0 password based encryption structures */
21 ASN1_SEQUENCE(PBE2PARAM) = {
22 ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR),
23 ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR)
24 } ASN1_SEQUENCE_END(PBE2PARAM)
26 IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM)
28 ASN1_SEQUENCE(PBKDF2PARAM) = {
29 ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY),
30 ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER),
31 ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER),
32 ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR)
33 } ASN1_SEQUENCE_END(PBKDF2PARAM)
35 IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM)
38 * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm: yes I know
39 * this is horrible! Extended version to allow application supplied PRF NID
43 X509_ALGOR *PKCS5_pbe2_set_iv_ex(const EVP_CIPHER *cipher, int iter,
44 unsigned char *salt, int saltlen,
45 unsigned char *aiv, int prf_nid,
48 X509_ALGOR *scheme = NULL, *ret = NULL;
49 int alg_nid, keylen, ivlen;
50 EVP_CIPHER_CTX *ctx = NULL;
51 unsigned char iv[EVP_MAX_IV_LENGTH];
52 PBE2PARAM *pbe2 = NULL;
54 alg_nid = EVP_CIPHER_get_type(cipher);
55 if (alg_nid == NID_undef) {
56 ERR_raise(ERR_LIB_ASN1, ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
60 if ((pbe2 = PBE2PARAM_new()) == NULL)
63 /* Setup the AlgorithmIdentifier for the encryption scheme */
64 scheme = pbe2->encryption;
65 scheme->algorithm = OBJ_nid2obj(alg_nid);
66 if ((scheme->parameter = ASN1_TYPE_new()) == NULL)
69 /* Create random IV */
70 ivlen = EVP_CIPHER_get_iv_length(cipher);
73 memcpy(iv, aiv, ivlen);
74 else if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
78 ctx = EVP_CIPHER_CTX_new();
82 /* Dummy cipherinit to just setup the IV, and PRF */
83 if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0))
85 if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) <= 0) {
86 ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
90 * If prf NID unspecified see if cipher has a preference. An error is OK
91 * here: just means use default PRF.
94 if ((prf_nid == -1) &&
95 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0) {
96 prf_nid = NID_hmacWithSHA256;
99 EVP_CIPHER_CTX_free(ctx);
102 /* If its RC2 then we'd better setup the key length */
104 if (alg_nid == NID_rc2_cbc)
105 keylen = EVP_CIPHER_get_key_length(cipher);
111 X509_ALGOR_free(pbe2->keyfunc);
113 pbe2->keyfunc = PKCS5_pbkdf2_set_ex(iter, salt, saltlen, prf_nid, keylen,
116 if (pbe2->keyfunc == NULL)
119 /* Now set up top level AlgorithmIdentifier */
121 if ((ret = X509_ALGOR_new()) == NULL)
124 ret->algorithm = OBJ_nid2obj(NID_pbes2);
126 /* Encode PBE2PARAM into parameter */
128 if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
132 PBE2PARAM_free(pbe2);
138 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
141 EVP_CIPHER_CTX_free(ctx);
142 PBE2PARAM_free(pbe2);
143 /* Note 'scheme' is freed as part of pbe2 */
144 X509_ALGOR_free(ret);
149 X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
150 unsigned char *salt, int saltlen,
151 unsigned char *aiv, int prf_nid)
153 return PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, aiv, prf_nid,
157 X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
158 unsigned char *salt, int saltlen)
160 return PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL, -1,
165 X509_ALGOR *PKCS5_pbkdf2_set_ex(int iter, unsigned char *salt, int saltlen,
166 int prf_nid, int keylen,
167 OSSL_LIB_CTX *libctx)
169 X509_ALGOR *keyfunc = NULL;
170 PBKDF2PARAM *kdf = NULL;
171 ASN1_OCTET_STRING *osalt = NULL;
173 if ((kdf = PBKDF2PARAM_new()) == NULL)
175 if ((osalt = ASN1_OCTET_STRING_new()) == NULL)
178 kdf->salt->value.octet_string = osalt;
179 kdf->salt->type = V_ASN1_OCTET_STRING;
184 saltlen = PKCS5_SALT_LEN;
185 if ((osalt->data = OPENSSL_malloc(saltlen)) == NULL)
188 osalt->length = saltlen;
191 memcpy(osalt->data, salt, saltlen);
192 else if (RAND_bytes_ex(libctx, osalt->data, saltlen, 0) <= 0)
196 iter = PKCS5_DEFAULT_ITER;
198 if (!ASN1_INTEGER_set(kdf->iter, iter))
201 /* If have a key len set it up */
204 if ((kdf->keylength = ASN1_INTEGER_new()) == NULL)
206 if (!ASN1_INTEGER_set(kdf->keylength, keylen))
210 /* prf can stay NULL if we are using hmacWithSHA1 */
211 if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1) {
212 kdf->prf = ossl_X509_ALGOR_from_nid(prf_nid, V_ASN1_NULL, NULL);
213 if (kdf->prf == NULL)
217 /* Finally setup the keyfunc structure */
219 keyfunc = X509_ALGOR_new();
223 keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2);
225 /* Encode PBKDF2PARAM into parameter of pbe2 */
227 if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), kdf,
228 &keyfunc->parameter))
231 PBKDF2PARAM_free(kdf);
235 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
236 PBKDF2PARAM_free(kdf);
237 X509_ALGOR_free(keyfunc);
241 X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
242 int prf_nid, int keylen)
244 return PKCS5_pbkdf2_set_ex(iter, salt, saltlen, prf_nid, keylen, NULL);