1212#include "internal/cryptlib.h"
1313#include <openssl/x509.h>
1414#include <openssl/evp.h>
15+ #include <openssl/core_names.h>
16+ #include <openssl/kdf.h>
17+
18+ #define PKCS5_PBES1_OUTPUT_LENGTH 16
19+ #define PKCS5_PBES1_KEY_IV_LENGTH 8
1520
1621/*
1722 * Doesn't do anything now: Builtin PBE algorithms in static table.
@@ -25,15 +30,16 @@ int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
2530 ASN1_TYPE * param , const EVP_CIPHER * cipher ,
2631 const EVP_MD * md , int en_de )
2732{
28- EVP_MD_CTX * ctx ;
29- unsigned char md_tmp [EVP_MAX_MD_SIZE ];
30- unsigned char key [EVP_MAX_KEY_LENGTH ], iv [EVP_MAX_IV_LENGTH ];
31- int i , ivl , kl ;
32- PBEPARAM * pbe ;
33+ unsigned char out [PKCS5_PBES1_OUTPUT_LENGTH ];
34+ int ivl , kl ;
35+ PBEPARAM * pbe = NULL ;
3336 int saltlen , iter ;
3437 unsigned char * salt ;
35- int mdsize ;
3638 int rv = 0 ;
39+ EVP_KDF * kdf ;
40+ EVP_KDF_CTX * kctx = NULL ;
41+ OSSL_PARAM params [5 ], * p = params ;
42+ const char * mdname = EVP_MD_name (md );
3743
3844 /* Extract useful info from parameter */
3945 if (param == NULL || param -> type != V_ASN1_SEQUENCE ||
@@ -49,16 +55,14 @@ int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
4955 }
5056
5157 ivl = EVP_CIPHER_iv_length (cipher );
52- if (ivl < 0 || ivl > 16 ) {
58+ if (ivl != PKCS5_PBES1_KEY_IV_LENGTH ) {
5359 ERR_raise (ERR_LIB_EVP , EVP_R_INVALID_IV_LENGTH );
54- PBEPARAM_free (pbe );
55- return 0 ;
60+ goto err ;
5661 }
5762 kl = EVP_CIPHER_key_length (cipher );
58- if (kl < 0 || kl > ( int ) sizeof ( md_tmp ) ) {
63+ if (kl != PKCS5_PBES1_KEY_IV_LENGTH ) {
5964 ERR_raise (ERR_LIB_EVP , EVP_R_INVALID_KEY_LENGTH );
60- PBEPARAM_free (pbe );
61- return 0 ;
65+ goto err ;
6266 }
6367
6468 if (pbe -> iter == NULL )
@@ -73,43 +77,29 @@ int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
7377 else if (passlen == -1 )
7478 passlen = strlen (pass );
7579
76- ctx = EVP_MD_CTX_new ();
77- if (ctx == NULL ) {
78- ERR_raise (ERR_LIB_EVP , ERR_R_MALLOC_FAILURE );
79- goto err ;
80- }
81-
82- if (!EVP_DigestInit_ex (ctx , md , NULL ))
83- goto err ;
84- if (!EVP_DigestUpdate (ctx , pass , passlen ))
80+ kdf = EVP_KDF_fetch (NULL , OSSL_KDF_NAME_PBKDF1 , NULL );
81+ kctx = EVP_KDF_CTX_new (kdf );
82+ EVP_KDF_free (kdf );
83+ if (kctx == NULL )
8584 goto err ;
86- if (!EVP_DigestUpdate (ctx , salt , saltlen ))
87- goto err ;
88- PBEPARAM_free (pbe );
89- pbe = NULL ;
90- if (!EVP_DigestFinal_ex (ctx , md_tmp , NULL ))
85+ * p ++ = OSSL_PARAM_construct_octet_string (OSSL_KDF_PARAM_PASSWORD ,
86+ (char * )pass , (size_t )passlen );
87+ * p ++ = OSSL_PARAM_construct_octet_string (OSSL_KDF_PARAM_SALT ,
88+ salt , saltlen );
89+ * p ++ = OSSL_PARAM_construct_int (OSSL_KDF_PARAM_ITER , & iter );
90+ * p ++ = OSSL_PARAM_construct_utf8_string (OSSL_KDF_PARAM_DIGEST ,
91+ (char * )mdname , 0 );
92+ * p = OSSL_PARAM_construct_end ();
93+ if (EVP_KDF_derive (kctx , out , PKCS5_PBES1_OUTPUT_LENGTH , params ) != 1 )
9194 goto err ;
92- mdsize = EVP_MD_size (md );
93- if (mdsize < 0 )
94- goto err ;
95- for (i = 1 ; i < iter ; i ++ ) {
96- if (!EVP_DigestInit_ex (ctx , md , NULL ))
97- goto err ;
98- if (!EVP_DigestUpdate (ctx , md_tmp , mdsize ))
99- goto err ;
100- if (!EVP_DigestFinal_ex (ctx , md_tmp , NULL ))
101- goto err ;
102- }
103- memcpy (key , md_tmp , kl );
104- memcpy (iv , md_tmp + (16 - ivl ), ivl );
105- if (!EVP_CipherInit_ex (cctx , cipher , NULL , key , iv , en_de ))
95+
96+ if (!EVP_CipherInit_ex (cctx , cipher , NULL , out ,
97+ out + PKCS5_PBES1_KEY_IV_LENGTH , en_de ))
10698 goto err ;
107- OPENSSL_cleanse (md_tmp , EVP_MAX_MD_SIZE );
108- OPENSSL_cleanse (key , EVP_MAX_KEY_LENGTH );
109- OPENSSL_cleanse (iv , EVP_MAX_IV_LENGTH );
99+ OPENSSL_cleanse (out , PKCS5_PBES1_OUTPUT_LENGTH );
110100 rv = 1 ;
111101 err :
102+ EVP_KDF_CTX_free (kctx );
112103 PBEPARAM_free (pbe );
113- EVP_MD_CTX_free (ctx );
114104 return rv ;
115105}
0 commit comments