2 * Copyright 1999-2018 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
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 "internal/evp_int.h"
21 int PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
22 const unsigned char *salt, int saltlen, int iter,
23 const EVP_MD *digest, int keylen, unsigned char *out)
25 const char *empty = "";
29 /* Keep documented behaviour. */
33 } else if (passlen == -1) {
34 passlen = strlen(pass);
36 if (salt == NULL && saltlen == 0)
37 salt = (unsigned char *)empty;
39 kctx = EVP_KDF_CTX_new_id(EVP_KDF_PBKDF2);
42 if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PASS, pass, (size_t)passlen) != 1
43 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT,
44 salt, (size_t)saltlen) != 1
45 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_ITER, iter) != 1
46 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, digest) != 1
47 || EVP_KDF_derive(kctx, out, keylen) != 1)
50 EVP_KDF_CTX_free(kctx);
52 OSSL_TRACE_BEGIN(PKCS5V2) {
53 BIO_printf(trc_out, "Password:\n");
54 BIO_hex_string(trc_out,
55 0, passlen, pass, passlen);
56 BIO_printf(trc_out, "\n");
57 BIO_printf(trc_out, "Salt:\n");
58 BIO_hex_string(trc_out,
59 0, saltlen, salt, saltlen);
60 BIO_printf(trc_out, "\n");
61 BIO_printf(trc_out, "Iteration count %d\n", iter);
62 BIO_printf(trc_out, "Key:\n");
63 BIO_hex_string(trc_out,
64 0, keylen, out, keylen);
65 BIO_printf(trc_out, "\n");
66 } OSSL_TRACE_END(PKCS5V2);
70 int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
71 const unsigned char *salt, int saltlen, int iter,
72 int keylen, unsigned char *out)
74 return PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, EVP_sha1(),
79 * Now the key derivation function itself. This is a bit evil because it has
80 * to check the ASN1 parameters are valid: and there are quite a few of
84 int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
85 ASN1_TYPE *param, const EVP_CIPHER *c,
86 const EVP_MD *md, int en_de)
88 PBE2PARAM *pbe2 = NULL;
89 const EVP_CIPHER *cipher;
94 pbe2 = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBE2PARAM), param);
96 EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_DECODE_ERROR);
100 /* See if we recognise the key derivation function */
101 if (!EVP_PBE_find(EVP_PBE_TYPE_KDF, OBJ_obj2nid(pbe2->keyfunc->algorithm),
103 EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN,
104 EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
109 * lets see if we recognise the encryption algorithm.
112 cipher = EVP_get_cipherbyobj(pbe2->encryption->algorithm);
115 EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_UNSUPPORTED_CIPHER);
119 /* Fixup cipher based on AlgorithmIdentifier */
120 if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
122 if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) < 0) {
123 EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_CIPHER_PARAMETER_ERROR);
126 rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de);
128 PBE2PARAM_free(pbe2);
132 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
133 int passlen, ASN1_TYPE *param,
134 const EVP_CIPHER *c, const EVP_MD *md, int en_de)
136 unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
139 unsigned int keylen = 0;
140 int prf_nid, hmac_md_nid;
141 PBKDF2PARAM *kdf = NULL;
144 if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
145 EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_NO_CIPHER_SET);
148 keylen = EVP_CIPHER_CTX_key_length(ctx);
149 OPENSSL_assert(keylen <= sizeof(key));
151 /* Decode parameter */
153 kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param);
156 EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_DECODE_ERROR);
160 keylen = EVP_CIPHER_CTX_key_length(ctx);
162 /* Now check the parameters of the kdf */
164 if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) {
165 EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_KEYLENGTH);
170 prf_nid = OBJ_obj2nid(kdf->prf->algorithm);
172 prf_nid = NID_hmacWithSHA1;
174 if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) {
175 EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_PRF);
179 prfmd = EVP_get_digestbynid(hmac_md_nid);
181 EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_PRF);
185 if (kdf->salt->type != V_ASN1_OCTET_STRING) {
186 EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_SALT_TYPE);
190 /* it seems that its all OK */
191 salt = kdf->salt->value.octet_string->data;
192 saltlen = kdf->salt->value.octet_string->length;
193 iter = ASN1_INTEGER_get(kdf->iter);
194 if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, prfmd,
197 rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
199 OPENSSL_cleanse(key, keylen);
200 PBKDF2PARAM_free(kdf);