11cb0ef41Sopenharmony_ci/* 21cb0ef41Sopenharmony_ci * Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved. 31cb0ef41Sopenharmony_ci * 41cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 51cb0ef41Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 61cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at 71cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html 81cb0ef41Sopenharmony_ci */ 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci#include <stdio.h> 111cb0ef41Sopenharmony_ci#include <stdlib.h> 121cb0ef41Sopenharmony_ci#include "internal/cryptlib.h" 131cb0ef41Sopenharmony_ci#include <openssl/x509.h> 141cb0ef41Sopenharmony_ci#include <openssl/evp.h> 151cb0ef41Sopenharmony_ci#include <openssl/kdf.h> 161cb0ef41Sopenharmony_ci#include <openssl/hmac.h> 171cb0ef41Sopenharmony_ci#include <openssl/trace.h> 181cb0ef41Sopenharmony_ci#include <openssl/core_names.h> 191cb0ef41Sopenharmony_ci#include "crypto/evp.h" 201cb0ef41Sopenharmony_ci#include "evp_local.h" 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciint ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen, 231cb0ef41Sopenharmony_ci const unsigned char *salt, int saltlen, int iter, 241cb0ef41Sopenharmony_ci const EVP_MD *digest, int keylen, unsigned char *out, 251cb0ef41Sopenharmony_ci OSSL_LIB_CTX *libctx, const char *propq) 261cb0ef41Sopenharmony_ci{ 271cb0ef41Sopenharmony_ci const char *empty = ""; 281cb0ef41Sopenharmony_ci int rv = 1, mode = 1; 291cb0ef41Sopenharmony_ci EVP_KDF *kdf; 301cb0ef41Sopenharmony_ci EVP_KDF_CTX *kctx; 311cb0ef41Sopenharmony_ci const char *mdname = EVP_MD_get0_name(digest); 321cb0ef41Sopenharmony_ci OSSL_PARAM params[6], *p = params; 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci /* Keep documented behaviour. */ 351cb0ef41Sopenharmony_ci if (pass == NULL) { 361cb0ef41Sopenharmony_ci pass = empty; 371cb0ef41Sopenharmony_ci passlen = 0; 381cb0ef41Sopenharmony_ci } else if (passlen == -1) { 391cb0ef41Sopenharmony_ci passlen = strlen(pass); 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci if (salt == NULL && saltlen == 0) 421cb0ef41Sopenharmony_ci salt = (unsigned char *)empty; 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, propq); 451cb0ef41Sopenharmony_ci if (kdf == NULL) 461cb0ef41Sopenharmony_ci return 0; 471cb0ef41Sopenharmony_ci kctx = EVP_KDF_CTX_new(kdf); 481cb0ef41Sopenharmony_ci EVP_KDF_free(kdf); 491cb0ef41Sopenharmony_ci if (kctx == NULL) 501cb0ef41Sopenharmony_ci return 0; 511cb0ef41Sopenharmony_ci *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, 521cb0ef41Sopenharmony_ci (char *)pass, (size_t)passlen); 531cb0ef41Sopenharmony_ci *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode); 541cb0ef41Sopenharmony_ci *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, 551cb0ef41Sopenharmony_ci (unsigned char *)salt, saltlen); 561cb0ef41Sopenharmony_ci *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter); 571cb0ef41Sopenharmony_ci *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, 581cb0ef41Sopenharmony_ci (char *)mdname, 0); 591cb0ef41Sopenharmony_ci *p = OSSL_PARAM_construct_end(); 601cb0ef41Sopenharmony_ci if (EVP_KDF_derive(kctx, out, keylen, params) != 1) 611cb0ef41Sopenharmony_ci rv = 0; 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci EVP_KDF_CTX_free(kctx); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci OSSL_TRACE_BEGIN(PKCS5V2) { 661cb0ef41Sopenharmony_ci BIO_printf(trc_out, "Password:\n"); 671cb0ef41Sopenharmony_ci BIO_hex_string(trc_out, 681cb0ef41Sopenharmony_ci 0, passlen, pass, passlen); 691cb0ef41Sopenharmony_ci BIO_printf(trc_out, "\n"); 701cb0ef41Sopenharmony_ci BIO_printf(trc_out, "Salt:\n"); 711cb0ef41Sopenharmony_ci BIO_hex_string(trc_out, 721cb0ef41Sopenharmony_ci 0, saltlen, salt, saltlen); 731cb0ef41Sopenharmony_ci BIO_printf(trc_out, "\n"); 741cb0ef41Sopenharmony_ci BIO_printf(trc_out, "Iteration count %d\n", iter); 751cb0ef41Sopenharmony_ci BIO_printf(trc_out, "Key:\n"); 761cb0ef41Sopenharmony_ci BIO_hex_string(trc_out, 771cb0ef41Sopenharmony_ci 0, keylen, out, keylen); 781cb0ef41Sopenharmony_ci BIO_printf(trc_out, "\n"); 791cb0ef41Sopenharmony_ci } OSSL_TRACE_END(PKCS5V2); 801cb0ef41Sopenharmony_ci return rv; 811cb0ef41Sopenharmony_ci} 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ciint PKCS5_PBKDF2_HMAC(const char *pass, int passlen, const unsigned char *salt, 841cb0ef41Sopenharmony_ci int saltlen, int iter, const EVP_MD *digest, int keylen, 851cb0ef41Sopenharmony_ci unsigned char *out) 861cb0ef41Sopenharmony_ci{ 871cb0ef41Sopenharmony_ci return ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, digest, 881cb0ef41Sopenharmony_ci keylen, out, NULL, NULL); 891cb0ef41Sopenharmony_ci} 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ciint PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen, 931cb0ef41Sopenharmony_ci const unsigned char *salt, int saltlen, int iter, 941cb0ef41Sopenharmony_ci int keylen, unsigned char *out) 951cb0ef41Sopenharmony_ci{ 961cb0ef41Sopenharmony_ci EVP_MD *digest; 971cb0ef41Sopenharmony_ci int r = 0; 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci if ((digest = EVP_MD_fetch(NULL, SN_sha1, NULL)) != NULL) 1001cb0ef41Sopenharmony_ci r = ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, 1011cb0ef41Sopenharmony_ci digest, keylen, out, NULL, NULL); 1021cb0ef41Sopenharmony_ci EVP_MD_free(digest); 1031cb0ef41Sopenharmony_ci return r; 1041cb0ef41Sopenharmony_ci} 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci/* 1071cb0ef41Sopenharmony_ci * Now the key derivation function itself. This is a bit evil because it has 1081cb0ef41Sopenharmony_ci * to check the ASN1 parameters are valid: and there are quite a few of 1091cb0ef41Sopenharmony_ci * them... 1101cb0ef41Sopenharmony_ci */ 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ciint PKCS5_v2_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, 1131cb0ef41Sopenharmony_ci ASN1_TYPE *param, const EVP_CIPHER *c, 1141cb0ef41Sopenharmony_ci const EVP_MD *md, int en_de, 1151cb0ef41Sopenharmony_ci OSSL_LIB_CTX *libctx, const char *propq) 1161cb0ef41Sopenharmony_ci{ 1171cb0ef41Sopenharmony_ci PBE2PARAM *pbe2 = NULL; 1181cb0ef41Sopenharmony_ci char ciph_name[80]; 1191cb0ef41Sopenharmony_ci const EVP_CIPHER *cipher = NULL; 1201cb0ef41Sopenharmony_ci EVP_CIPHER *cipher_fetch = NULL; 1211cb0ef41Sopenharmony_ci EVP_PBE_KEYGEN_EX *kdf; 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci int rv = 0; 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci pbe2 = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBE2PARAM), param); 1261cb0ef41Sopenharmony_ci if (pbe2 == NULL) { 1271cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR); 1281cb0ef41Sopenharmony_ci goto err; 1291cb0ef41Sopenharmony_ci } 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci /* See if we recognise the key derivation function */ 1321cb0ef41Sopenharmony_ci if (!EVP_PBE_find_ex(EVP_PBE_TYPE_KDF, OBJ_obj2nid(pbe2->keyfunc->algorithm), 1331cb0ef41Sopenharmony_ci NULL, NULL, NULL, &kdf)) { 1341cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION); 1351cb0ef41Sopenharmony_ci goto err; 1361cb0ef41Sopenharmony_ci } 1371cb0ef41Sopenharmony_ci 1381cb0ef41Sopenharmony_ci /* 1391cb0ef41Sopenharmony_ci * lets see if we recognise the encryption algorithm. 1401cb0ef41Sopenharmony_ci */ 1411cb0ef41Sopenharmony_ci if (OBJ_obj2txt(ciph_name, sizeof(ciph_name), pbe2->encryption->algorithm, 0) <= 0) { 1421cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER); 1431cb0ef41Sopenharmony_ci goto err; 1441cb0ef41Sopenharmony_ci } 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci (void)ERR_set_mark(); 1471cb0ef41Sopenharmony_ci cipher = cipher_fetch = EVP_CIPHER_fetch(libctx, ciph_name, propq); 1481cb0ef41Sopenharmony_ci /* Fallback to legacy method */ 1491cb0ef41Sopenharmony_ci if (cipher == NULL) 1501cb0ef41Sopenharmony_ci cipher = EVP_get_cipherbyname(ciph_name); 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci if (cipher == NULL) { 1531cb0ef41Sopenharmony_ci (void)ERR_clear_last_mark(); 1541cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER); 1551cb0ef41Sopenharmony_ci goto err; 1561cb0ef41Sopenharmony_ci } 1571cb0ef41Sopenharmony_ci (void)ERR_pop_to_mark(); 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_ci /* Fixup cipher based on AlgorithmIdentifier */ 1601cb0ef41Sopenharmony_ci if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de)) 1611cb0ef41Sopenharmony_ci goto err; 1621cb0ef41Sopenharmony_ci if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) <= 0) { 1631cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR); 1641cb0ef41Sopenharmony_ci goto err; 1651cb0ef41Sopenharmony_ci } 1661cb0ef41Sopenharmony_ci rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de, libctx, propq); 1671cb0ef41Sopenharmony_ci err: 1681cb0ef41Sopenharmony_ci EVP_CIPHER_free(cipher_fetch); 1691cb0ef41Sopenharmony_ci PBE2PARAM_free(pbe2); 1701cb0ef41Sopenharmony_ci return rv; 1711cb0ef41Sopenharmony_ci} 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ciint PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, 1741cb0ef41Sopenharmony_ci ASN1_TYPE *param, const EVP_CIPHER *c, 1751cb0ef41Sopenharmony_ci const EVP_MD *md, int en_de) 1761cb0ef41Sopenharmony_ci{ 1771cb0ef41Sopenharmony_ci return PKCS5_v2_PBE_keyivgen_ex(ctx, pass, passlen, param, c, md, en_de, NULL, NULL); 1781cb0ef41Sopenharmony_ci} 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ciint PKCS5_v2_PBKDF2_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, 1811cb0ef41Sopenharmony_ci int passlen, ASN1_TYPE *param, 1821cb0ef41Sopenharmony_ci const EVP_CIPHER *c, const EVP_MD *md, int en_de, 1831cb0ef41Sopenharmony_ci OSSL_LIB_CTX *libctx, const char *propq) 1841cb0ef41Sopenharmony_ci{ 1851cb0ef41Sopenharmony_ci unsigned char *salt, key[EVP_MAX_KEY_LENGTH]; 1861cb0ef41Sopenharmony_ci int saltlen, iter, t; 1871cb0ef41Sopenharmony_ci int rv = 0; 1881cb0ef41Sopenharmony_ci unsigned int keylen = 0; 1891cb0ef41Sopenharmony_ci int prf_nid, hmac_md_nid; 1901cb0ef41Sopenharmony_ci PBKDF2PARAM *kdf = NULL; 1911cb0ef41Sopenharmony_ci const EVP_MD *prfmd = NULL; 1921cb0ef41Sopenharmony_ci EVP_MD *prfmd_fetch = NULL; 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ci if (EVP_CIPHER_CTX_get0_cipher(ctx) == NULL) { 1951cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET); 1961cb0ef41Sopenharmony_ci goto err; 1971cb0ef41Sopenharmony_ci } 1981cb0ef41Sopenharmony_ci keylen = EVP_CIPHER_CTX_get_key_length(ctx); 1991cb0ef41Sopenharmony_ci OPENSSL_assert(keylen <= sizeof(key)); 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci /* Decode parameter */ 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ci kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param); 2041cb0ef41Sopenharmony_ci 2051cb0ef41Sopenharmony_ci if (kdf == NULL) { 2061cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR); 2071cb0ef41Sopenharmony_ci goto err; 2081cb0ef41Sopenharmony_ci } 2091cb0ef41Sopenharmony_ci 2101cb0ef41Sopenharmony_ci t = EVP_CIPHER_CTX_get_key_length(ctx); 2111cb0ef41Sopenharmony_ci if (t < 0) { 2121cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH); 2131cb0ef41Sopenharmony_ci goto err; 2141cb0ef41Sopenharmony_ci } 2151cb0ef41Sopenharmony_ci keylen = t; 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci /* Now check the parameters of the kdf */ 2181cb0ef41Sopenharmony_ci 2191cb0ef41Sopenharmony_ci if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) { 2201cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEYLENGTH); 2211cb0ef41Sopenharmony_ci goto err; 2221cb0ef41Sopenharmony_ci } 2231cb0ef41Sopenharmony_ci 2241cb0ef41Sopenharmony_ci if (kdf->prf) 2251cb0ef41Sopenharmony_ci prf_nid = OBJ_obj2nid(kdf->prf->algorithm); 2261cb0ef41Sopenharmony_ci else 2271cb0ef41Sopenharmony_ci prf_nid = NID_hmacWithSHA1; 2281cb0ef41Sopenharmony_ci 2291cb0ef41Sopenharmony_ci if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) { 2301cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF); 2311cb0ef41Sopenharmony_ci goto err; 2321cb0ef41Sopenharmony_ci } 2331cb0ef41Sopenharmony_ci 2341cb0ef41Sopenharmony_ci (void)ERR_set_mark(); 2351cb0ef41Sopenharmony_ci prfmd = prfmd_fetch = EVP_MD_fetch(libctx, OBJ_nid2sn(hmac_md_nid), propq); 2361cb0ef41Sopenharmony_ci if (prfmd == NULL) 2371cb0ef41Sopenharmony_ci prfmd = EVP_get_digestbynid(hmac_md_nid); 2381cb0ef41Sopenharmony_ci if (prfmd == NULL) { 2391cb0ef41Sopenharmony_ci (void)ERR_clear_last_mark(); 2401cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF); 2411cb0ef41Sopenharmony_ci goto err; 2421cb0ef41Sopenharmony_ci } 2431cb0ef41Sopenharmony_ci (void)ERR_pop_to_mark(); 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ci if (kdf->salt->type != V_ASN1_OCTET_STRING) { 2461cb0ef41Sopenharmony_ci ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_SALT_TYPE); 2471cb0ef41Sopenharmony_ci goto err; 2481cb0ef41Sopenharmony_ci } 2491cb0ef41Sopenharmony_ci 2501cb0ef41Sopenharmony_ci /* it seems that its all OK */ 2511cb0ef41Sopenharmony_ci salt = kdf->salt->value.octet_string->data; 2521cb0ef41Sopenharmony_ci saltlen = kdf->salt->value.octet_string->length; 2531cb0ef41Sopenharmony_ci iter = ASN1_INTEGER_get(kdf->iter); 2541cb0ef41Sopenharmony_ci if (!ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, prfmd, 2551cb0ef41Sopenharmony_ci keylen, key, libctx, propq)) 2561cb0ef41Sopenharmony_ci goto err; 2571cb0ef41Sopenharmony_ci rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de); 2581cb0ef41Sopenharmony_ci err: 2591cb0ef41Sopenharmony_ci OPENSSL_cleanse(key, keylen); 2601cb0ef41Sopenharmony_ci PBKDF2PARAM_free(kdf); 2611cb0ef41Sopenharmony_ci EVP_MD_free(prfmd_fetch); 2621cb0ef41Sopenharmony_ci return rv; 2631cb0ef41Sopenharmony_ci} 2641cb0ef41Sopenharmony_ci 2651cb0ef41Sopenharmony_ciint PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, 2661cb0ef41Sopenharmony_ci int passlen, ASN1_TYPE *param, 2671cb0ef41Sopenharmony_ci const EVP_CIPHER *c, const EVP_MD *md, int en_de) 2681cb0ef41Sopenharmony_ci{ 2691cb0ef41Sopenharmony_ci return PKCS5_v2_PBKDF2_keyivgen_ex(ctx, pass, passlen, param, c, md, en_de, 2701cb0ef41Sopenharmony_ci NULL, NULL); 2711cb0ef41Sopenharmony_ci} 272