diff --git a/frameworks/crypto_operation/mac.c b/frameworks/crypto_operation/mac.c index 06d60cb6564437b8f9ef08d90a978f4e9b451bc1..2068525747b982adef2a22fe9f3c938b36156b25 100644 --- a/frameworks/crypto_operation/mac.c +++ b/frameworks/crypto_operation/mac.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -48,6 +48,7 @@ static const HcfMacAbility MAC_ABILITY_SET[] = { { "SHA384", OpensslMacSpiCreate }, { "SHA512", OpensslMacSpiCreate }, { "SM3", OpensslMacSpiCreate }, + { "MD5", OpensslMacSpiCreate }, }; static const char *GetMacClass(void) diff --git a/frameworks/js/napi/crypto/inc/napi_pri_key.h b/frameworks/js/napi/crypto/inc/napi_pri_key.h index 8046cffc142ba902850445baa93ae111e4243b7f..8ac93c00374e2c310e657281aec749b0acac716f 100644 --- a/frameworks/js/napi/crypto/inc/napi_pri_key.h +++ b/frameworks/js/napi/crypto/inc/napi_pri_key.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -37,6 +37,8 @@ public: static napi_value PriKeyConstructor(napi_env env, napi_callback_info info); static napi_value JsGetEncoded(napi_env env, napi_callback_info info); + static napi_value JsGetEncodedDer(napi_env env, napi_callback_info info); + static napi_value JsClearMem(napi_env env, napi_callback_info info); static napi_value JsGetAsyKeySpec(napi_env env, napi_callback_info info); diff --git a/frameworks/js/napi/crypto/src/napi_pri_key.cpp b/frameworks/js/napi/crypto/src/napi_pri_key.cpp index baa1966b8f4c94a2a2b66b9e14e8445589e42ad2..1292c02f7d907a5f70a885370865483ad8e1a3a9 100644 --- a/frameworks/js/napi/crypto/src/napi_pri_key.cpp +++ b/frameworks/js/napi/crypto/src/napi_pri_key.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -211,10 +211,53 @@ napi_value NapiPriKey::JsGetAsyKeySpec(napi_env env, napi_callback_info info) } } +napi_value NapiPriKey::JsGetEncodedDer(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + NapiPriKey *napiPriKey = nullptr; + size_t argc = ARGS_SIZE_ONE; + napi_value argv[ARGS_SIZE_ONE] = { nullptr }; + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + if (argc != ARGS_SIZE_ONE) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "wrong argument num.")); + LOGE("wrong argument num. require 1 arguments. [Argc]: %zu!", argc); + return nullptr; + } + std::string format; + if (!GetStringFromJSParams(env, argv[0], format)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get format.")); + LOGE("get format fail."); + return nullptr; + } + napi_status status = napi_unwrap(env, thisVar, reinterpret_cast(&napiPriKey)); + if (status != napi_ok || napiPriKey == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to unwrap private key obj!")); + LOGE("failed to unwrap private key obj!"); + return nullptr; + } + HcfPriKey *priKey = napiPriKey->GetPriKey(); + if (priKey == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get private key obj!")); + LOGE("failed to get private key obj!"); + return nullptr; + } + HcfBlob returnBlob = { .data = nullptr, .len = 0 }; + HcfResult res = priKey->getEncodedDer(priKey, format.c_str(), &returnBlob); + if (res != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "ecc get private key encodedDer fail.")); + LOGE("ecc get private key encodeDer fail."); + return nullptr; + } + napi_value instance = ConvertBlobToNapiValue(env, &returnBlob); + HcfBlobDataFree(&returnBlob); + return instance; +} + void NapiPriKey::DefinePriKeyJSClass(napi_env env) { napi_property_descriptor classDesc[] = { DECLARE_NAPI_FUNCTION("getEncoded", NapiPriKey::JsGetEncoded), + DECLARE_NAPI_FUNCTION("getEncodedDer", NapiPriKey::JsGetEncodedDer), DECLARE_NAPI_FUNCTION("clearMem", NapiPriKey::JsClearMem), DECLARE_NAPI_FUNCTION("getAsyKeySpec", NapiPriKey::JsGetAsyKeySpec), }; diff --git a/frameworks/key/sym_key_generator.c b/frameworks/key/sym_key_generator.c index f374b7f3d00911626f1837cdd875c0db2034ebb6..ada99b95ac62b7ae28d6d0c0679c5199f3b8788f 100644 --- a/frameworks/key/sym_key_generator.c +++ b/frameworks/key/sym_key_generator.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -36,6 +36,7 @@ #define HMAC_KEY_SIZE_SHA384 384 #define HMAC_KEY_SIZE_SHA512 512 #define HMAC_KEY_SIZE_SM3 256 +#define HMAC_KEY_SIZE_MD5 128 typedef HcfResult (*SymKeyGeneratorSpiCreateFunc)(SymKeyAttr *, HcfSymKeyGeneratorSpi **); @@ -137,9 +138,12 @@ static void SetKeyLenByDigest(HcfAlgParaValue value, void *attr) case HCF_OPENSSL_DIGEST_SM3: keyAttr->keySize = HMAC_KEY_SIZE_SM3; break; + case HCF_OPENSSL_DIGEST_MD5: + keyAttr->keySize = HMAC_KEY_SIZE_MD5; + break; default: - // We will ignore the 'MD5' and 'NoHash' inputs - LOGE("Invalid digest input: MD5 or NoHash"); + // We will ignore the and 'NoHash' inputs + LOGE("Invalid digest input: NoHash"); break; } } diff --git a/interfaces/innerkits/key/pri_key.h b/interfaces/innerkits/key/pri_key.h index 72fd9ef9ac1221cc57b35cbfd8bbd98c21fce24b..8d42bd15dc7b3a8c9a896b277497234a1d1d4bd8 100644 --- a/interfaces/innerkits/key/pri_key.h +++ b/interfaces/innerkits/key/pri_key.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -31,6 +31,8 @@ struct HcfPriKey { HcfResult (*getAsyKeySpecInt)(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt); + HcfResult (*getEncodedDer)(const HcfPriKey *self, const char *format, HcfBlob *returnBlob); + void (*clearMem)(HcfPriKey *self); }; diff --git a/plugin/openssl_plugin/common/inc/openssl_adapter.h b/plugin/openssl_plugin/common/inc/openssl_adapter.h index feeeb5831706dde5f1df46943129e0a093d2e4e8..23dc64dbf7c82b508fa35070f93f5f1bfdf5b55a 100644 --- a/plugin/openssl_plugin/common/inc/openssl_adapter.h +++ b/plugin/openssl_plugin/common/inc/openssl_adapter.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -118,6 +118,7 @@ EVP_PKEY *Openssl_EVP_PKEY_new_raw_private_key(int type, ENGINE *e, const unsign int Openssl_EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub, size_t *len); int Openssl_EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv, size_t *len); int Openssl_EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key); +int Openssl_EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key); void Openssl_EVP_PKEY_free(EVP_PKEY *pkey); EVP_PKEY_CTX *Openssl_EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey, const char *propquery); diff --git a/plugin/openssl_plugin/common/src/openssl_adapter.c b/plugin/openssl_plugin/common/src/openssl_adapter.c index b48c03e03c12b4cc5a2a009a7aaa0a3fef725c95..a7e647d099db69245da7b338abbf2898bf498785 100644 --- a/plugin/openssl_plugin/common/src/openssl_adapter.c +++ b/plugin/openssl_plugin/common/src/openssl_adapter.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -420,6 +420,11 @@ int Openssl_EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) return EVP_PKEY_assign_EC_KEY(pkey, key); } +int Openssl_EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) +{ + return EVP_PKEY_set1_EC_KEY(pkey, key); +} + void Openssl_EVP_PKEY_free(EVP_PKEY *pkey) { if (pkey != NULL) { diff --git a/plugin/openssl_plugin/crypto_operation/hmac/src/mac_openssl.c b/plugin/openssl_plugin/crypto_operation/hmac/src/mac_openssl.c index fcf10b5f87787da2777a5c50b055df85c05baa8b..09fac6f1cc57ebff0220dc9846465d2cc671a19d 100644 --- a/plugin/openssl_plugin/crypto_operation/hmac/src/mac_openssl.c +++ b/plugin/openssl_plugin/crypto_operation/hmac/src/mac_openssl.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -60,6 +60,8 @@ static const EVP_MD *OpensslGetMacAlgoFromString(const char *mdName) return Openssl_EVP_sha512(); } else if (strcmp(mdName, "SM3") == 0) { return Openssl_EVP_sm3(); + } else if (strcmp(mdName, "MD5") == 0) { + return Openssl_EVP_md5(); } return NULL; } diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c index 3738cb8f1b6e47282460dcdd3ae350d3f6e935fc..13934f9d50d1cbdc4aeab928c0541afcc6c44202 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/alg_25519_asy_key_generator_openssl.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -409,6 +409,14 @@ static HcfResult GetStrSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKe return HCF_NOT_SUPPORT; } +static HcfResult GetAlg25519PriKeyEncodedDer(const HcfPriKey *self, const char *format, HcfBlob *returnBlob) +{ + (void)self; + (void)format; + (void)returnBlob; + return HCF_INVALID_PARAMS; +} + static void ClearAlg25519PriKeyMem(HcfPriKey *self) { if (self == NULL) { @@ -474,6 +482,7 @@ static void FillOpensslAlg25519PriKeyFunc(HcfOpensslAlg25519PriKey *sk) sk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromAlg25519PriKey; sk->base.getAsyKeySpecInt = GetIntSpecFromAlg25519PriKey; sk->base.getAsyKeySpecString = GetStrSpecFromAlg25519PriKey; + sk->base.getEncodedDer = GetAlg25519PriKeyEncodedDer; sk->base.clearMem = ClearAlg25519PriKeyMem; } diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c index 3efea48078b66fef79e7cff34be0f404fbefa6e2..45f505d29d17ae11b39c5ce7b6e0dfebf6a20385 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/dh_asy_key_generator_openssl.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -384,6 +384,14 @@ static HcfResult GetStrSpecFromDhPriKey(const HcfPriKey *self, const AsyKeySpecI return HCF_NOT_SUPPORT; } +static HcfResult GetDhPriKeyEncodedDer(const HcfPriKey *self, const char *format, HcfBlob *returnBlob) +{ + (void)self; + (void)format; + (void)returnBlob; + return HCF_INVALID_PARAMS; +} + static void ClearDhPriKeyMem(HcfPriKey *self) { if (self == NULL) { @@ -495,6 +503,7 @@ static void FillOpensslDhPriKeyFunc(HcfOpensslDhPriKey *sk) sk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromDhPriKey; sk->base.getAsyKeySpecInt = GetIntSpecFromDhPriKey; sk->base.getAsyKeySpecString = GetStrSpecFromDhPriKey; + sk->base.getEncodedDer = GetDhPriKeyEncodedDer; sk->base.clearMem = ClearDhPriKeyMem; } diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c index d04ce18f5d8cb65807361271af22ece0e33a8101..47b59becf6ea9375d147d120ccd71909806b12b1 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/dsa_asy_key_generator_openssl.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -348,6 +348,14 @@ static void ClearDsaPriKeyMem(HcfPriKey *self) impl->sk = NULL; } +static HcfResult GetDsaPriKeyEncodedDer(const HcfPriKey *self, const char *format, HcfBlob *returnBlob) +{ + (void)self; + (void)format; + (void)returnBlob; + return HCF_INVALID_PARAMS; +} + static HcfResult GenerateDsaEvpKey(int32_t keyLen, EVP_PKEY **ppkey) { EVP_PKEY_CTX *paramsCtx = NULL; @@ -420,6 +428,7 @@ static void FillOpensslDsaPriKeyFunc(HcfOpensslDsaPriKey *sk) sk->base.getAsyKeySpecInt = GetIntSpecFromDsaPriKey; sk->base.getAsyKeySpecString = GetStrSpecFromDsaPriKey; sk->base.clearMem = ClearDsaPriKeyMem; + sk->base.getEncodedDer = GetDsaPriKeyEncodedDer; } static HcfResult CreateDsaPubKey(DSA *pk, HcfOpensslDsaPubKey **returnPubKey) diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c index 603a7bf97d16aeeab8b9b661e34655b576df9744..da008e1627f231110ae648ae54e576d1b39a0448 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/ecc_asy_key_generator_openssl.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -1085,6 +1085,98 @@ static HcfResult GetEccPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob) return HCF_SUCCESS; } +static HcfResult ParamCheck(const HcfPriKey *self, const char *format, HcfBlob *returnBlob) +{ + if ((self == NULL) || (format == NULL) || (returnBlob == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_ECC_PRI_KEY_CLASS)) { + LOGE("Invalid ecc params."); + return HCF_INVALID_PARAMS; + } + if (strcmp(format, "PKCS8") != 0) { + LOGE("Invalid point format."); + return HCF_INVALID_PARAMS; + } + return HCF_SUCCESS; +} + +static HcfResult CopyMemFromBIO(BIO *bio, HcfBlob *returnBlob) +{ + int len = BIO_pending(bio); + if (len <= 0) { + LOGE("Bio len less than 0."); + return HCF_INVALID_PARAMS; + } + HcfBlob tmpBlob; + tmpBlob.len = len; + tmpBlob.data = (uint8_t *)HcfMalloc(sizeof(uint8_t) * len, 0); + if (tmpBlob.data == NULL) { + LOGE("Malloc mem for blob fail."); + return HCF_ERR_MALLOC; + } + if (Openssl_BIO_read(bio, tmpBlob.data, tmpBlob.len) <= 0) { + LOGE("Bio read fail"); + HcfPrintOpensslError(); + HcfFree(tmpBlob.data); + return HCF_ERR_CRYPTO_OPERATION; + } + returnBlob->len = tmpBlob.len; + returnBlob->data = tmpBlob.data; + return HCF_SUCCESS; +} + +static HcfResult GetECPriKeyEncodedDer(const HcfPriKey *self, const char *format, HcfBlob *returnBlob) +{ + HcfResult ret = ParamCheck(self, format, returnBlob); + if (ret != HCF_SUCCESS) { + return ret; + } + HcfOpensslEccPriKey *impl = (HcfOpensslEccPriKey *)self; + if (impl->curveId != 0) { + Openssl_EC_KEY_set_asn1_flag(impl->ecKey, OPENSSL_EC_NAMED_CURVE); + } else { + Openssl_EC_KEY_set_asn1_flag(impl->ecKey, OPENSSL_EC_EXPLICIT_CURVE); + } + // keep consistence of 3.2 + Openssl_EC_KEY_set_enc_flags(impl->ecKey, EC_PKEY_NO_PUBKEY); + EVP_PKEY *pkey = Openssl_EVP_PKEY_new(); + if (pkey == NULL) { + HcfPrintOpensslError(); + LOGE("New pKey failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + if (Openssl_EVP_PKEY_set1_EC_KEY(pkey, impl->ecKey) != HCF_OPENSSL_SUCCESS) { + Openssl_EVP_PKEY_free(pkey); + HcfPrintOpensslError(); + LOGE("set ec key failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + BIO *bio = Openssl_BIO_new(Openssl_BIO_s_mem()); + if (bio == NULL) { + LOGE("BIO new fail."); + HcfPrintOpensslError(); + ret = HCF_ERR_CRYPTO_OPERATION; + goto ERR2; + } + if (Openssl_i2d_PKCS8PrivateKey_bio(bio, pkey, NULL, NULL, 0, NULL, NULL) != HCF_OPENSSL_SUCCESS) { + LOGE("i2d privateKey bio fail."); + HcfPrintOpensslError(); + ret = HCF_ERR_CRYPTO_OPERATION; + goto ERR1; + } + ret = CopyMemFromBIO(bio, returnBlob); + if (ret != HCF_SUCCESS) { + LOGE("Copy mem from BIO fail."); + } +ERR1: + Openssl_BIO_free_all(bio); +ERR2: + Openssl_EVP_PKEY_free(pkey); + return ret; +} + static void EccPriKeyClearMem(HcfPriKey *self) { if (self == NULL) { @@ -1362,6 +1454,7 @@ static HcfResult PackEccPriKey(int32_t curveId, EC_KEY *ecKey, const char *field returnPriKey->base.getAsyKeySpecBigInteger = GetECPriKeySpecBigInteger; returnPriKey->base.getAsyKeySpecString = GetECPriKeySpecString; returnPriKey->base.getAsyKeySpecInt = GetECPriKeySpecInt; + returnPriKey->base.getEncodedDer = GetECPriKeyEncodedDer; returnPriKey->curveId = curveId; returnPriKey->ecKey = ecKey; returnPriKey->fieldType = tmpFieldType; @@ -1392,31 +1485,55 @@ static HcfResult ConvertEcPubKey(int32_t curveId, HcfBlob *pubKeyBlob, HcfOpenss const unsigned char *tmpData = (const unsigned char *)(pubKeyBlob->data); EC_KEY *ecKey = Openssl_d2i_EC_PUBKEY(NULL, &tmpData, pubKeyBlob->len); if (ecKey == NULL) { - LOGD("[error] d2i_EC_PUBKEY fail."); + LOGE("d2i_EC_PUBKEY fail."); HcfPrintOpensslError(); return HCF_ERR_CRYPTO_OPERATION; } HcfResult res = PackEccPubKey(curveId, ecKey, g_eccGenerateFieldType, returnPubKey); if (res != HCF_SUCCESS) { - LOGD("[error] PackEccPubKey failed."); + LOGE("PackEccPubKey failed."); Openssl_EC_KEY_free(ecKey); return res; + } + return HCF_SUCCESS; +} + +static HcfResult ConvertPriFromEncoded(EC_KEY **eckey, HcfBlob *priKeyBlob) +{ + const unsigned char *tmpData = (const unsigned char *)(priKeyBlob->data); + EVP_PKEY *pkey = Openssl_d2i_PrivateKey(EVP_PKEY_EC, NULL, &tmpData, priKeyBlob->len); + if (pkey == NULL) { + HcfPrintOpensslError(); + LOGE("d2i pri key failed."); + return HCF_ERR_CRYPTO_OPERATION; + } + *eckey = EVP_PKEY_get1_EC_KEY(pkey); + Openssl_EVP_PKEY_free(pkey); + if (*eckey == NULL) { + LOGE("Get eckey failed"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; } return HCF_SUCCESS; } static HcfResult ConvertEcPriKey(int32_t curveId, HcfBlob *priKeyBlob, HcfOpensslEccPriKey **returnPriKey) { - const unsigned char *tmpData = (const unsigned char *)(priKeyBlob->data); - EC_KEY *ecKey = Openssl_d2i_ECPrivateKey(NULL, &tmpData, priKeyBlob->len); + EC_KEY *ecKey = NULL; + HcfResult res = ConvertPriFromEncoded(&ecKey, priKeyBlob); + if (res != HCF_SUCCESS) { + LOGE("i2d for private key failed"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } if (ecKey == NULL) { - LOGD("[error] d2i_ECPrivateKey fail"); + LOGE("d2i ec private key fail"); HcfPrintOpensslError(); return HCF_ERR_CRYPTO_OPERATION; } - HcfResult res = PackEccPriKey(curveId, ecKey, g_eccGenerateFieldType, returnPriKey); + res = PackEccPriKey(curveId, ecKey, g_eccGenerateFieldType, returnPriKey); if (res != HCF_SUCCESS) { - LOGD("[error] PackEccPriKey failed."); + LOGE("Pack ec pri key failed."); Openssl_EC_KEY_free(ecKey); return res; } diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c index 762bb6e73706d56bce132eeeebbcb80528d7b64e..bddb3849902dc2a9690d8192e9ed8a9a46774f5c 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/rsa_asy_key_generator_openssl.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -153,6 +153,13 @@ static HcfResult GetRsaPriKeySpecInt(const HcfPriKey *self, const AsyKeySpecItem LOGE("Rsa has no integer attribute"); return HCF_NOT_SUPPORT; } +static HcfResult GetRsaPriKeyEncodedDer(const HcfPriKey *self, const char *format, HcfBlob *returnBlob) +{ + (void)self; + (void)format; + (void)returnBlob; + return HCF_INVALID_PARAMS; +} static HcfResult GetRsaPriKeySpecBigInteger(const HcfPriKey *self, const AsyKeySpecItem item, HcfBigInteger *returnBigInteger) @@ -552,6 +559,7 @@ static HcfResult PackPriKey(RSA *rsaPriKey, HcfOpensslRsaPriKey **retPriKey) (*retPriKey)->base.getAsyKeySpecBigInteger = GetRsaPriKeySpecBigInteger; (*retPriKey)->base.getAsyKeySpecString = GetRsaPriKeySpecString; (*retPriKey)->base.getAsyKeySpecInt = GetRsaPriKeySpecInt; + (*retPriKey)->base.getEncodedDer = GetRsaPriKeyEncodedDer; return HCF_SUCCESS; } diff --git a/plugin/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c b/plugin/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c index 4b052c476a1adfd52fbdf98c3ab032bd1264c5a2..4bbb00d7a100bed779623ca7c333a6d351e4e0e7 100644 --- a/plugin/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c +++ b/plugin/openssl_plugin/key/asy_key_generator/src/sm2_asy_key_generator_openssl.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -761,6 +761,14 @@ static HcfResult PackSm2PubKey(int32_t curveId, EC_KEY *ecKey, const char *field return HCF_SUCCESS; } +static HcfResult GetSm2PriKeyEncodedDer(const HcfPriKey *self, const char *format, HcfBlob *returnBlob) +{ + (void)self; + (void)format; + (void)returnBlob; + return HCF_INVALID_PARAMS; +} + static HcfResult PackSm2PriKey(int32_t curveId, EC_KEY *ecKey, const char *fieldType, HcfOpensslSm2PriKey **returnObj) { @@ -795,6 +803,7 @@ static HcfResult PackSm2PriKey(int32_t curveId, EC_KEY *ecKey, const char *field returnPriKey->base.getAsyKeySpecString = GetSm2PriKeySpecString; returnPriKey->base.getAsyKeySpecInt = GetSm2PriKeySpecInt; returnPriKey->base.clearMem = Sm2PriKeyClearMem; + returnPriKey->base.getEncodedDer = GetSm2PriKeyEncodedDer; returnPriKey->curveId = curveId; returnPriKey->ecKey = ecKey; returnPriKey->fieldType = tmpFieldType; diff --git a/test/unittest/src/crypto_ecc_asy_key_generator_test.cpp b/test/unittest/src/crypto_ecc_asy_key_generator_test.cpp index 48a7b78712bb3c1e821c1f030c0b58ec7507ad7f..0590d7f81b91e29fccd1f964f30687d74e878f37 100644 --- a/test/unittest/src/crypto_ecc_asy_key_generator_test.cpp +++ b/test/unittest/src/crypto_ecc_asy_key_generator_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -2343,4 +2343,52 @@ HWTEST_F(CryptoEccAsyKeyGeneratorTest, CryptoEccAsyKeyGeneratorTest702, TestSize EndRecordOpensslCallNum(); } + +HWTEST_F(CryptoEccAsyKeyGeneratorTest, CryptoEccPrvKeyDerConvertTest801, TestSize.Level0) +{ + HcfAsyKeyGenerator *generator = nullptr; + int32_t res = HcfAsyKeyGeneratorCreate("ECC224", &generator); + const char *wrongFormat = "PKCS7"; + const char *format = "PKCS8"; + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(keyPair, nullptr); + + HcfBlob pubKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->pubKey->base.getEncoded(&(keyPair->pubKey->base), &pubKeyBlob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(pubKeyBlob.data, nullptr); + ASSERT_NE(pubKeyBlob.len, 0); + + HcfBlob priKeyBlob = { .data = nullptr, .len = 0 }; + res = keyPair->priKey->getEncodedDer(keyPair->priKey, wrongFormat, &priKeyBlob); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(priKeyBlob.data, nullptr); + ASSERT_EQ(priKeyBlob.len, 0); + + res = keyPair->priKey->getEncodedDer(nullptr, format, nullptr); + ASSERT_EQ(res, HCF_INVALID_PARAMS); + ASSERT_EQ(priKeyBlob.data, nullptr); + ASSERT_EQ(priKeyBlob.len, 0); + + res = keyPair->priKey->getEncodedDer(keyPair->priKey, format, &priKeyBlob); + ASSERT_EQ(res, HCF_SUCCESS); + ASSERT_NE(priKeyBlob.data, nullptr); + ASSERT_NE(priKeyBlob.len, 0); + + HcfKeyPair *outKeyPair = nullptr; + res = generator->convertKey(generator, nullptr, &pubKeyBlob, &priKeyBlob, &outKeyPair); + ASSERT_EQ(res, HCF_SUCCESS); + + HcfFree(pubKeyBlob.data); + HcfFree(priKeyBlob.data); + HcfObjDestroy(outKeyPair); + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); + + EndRecordMallocNum(); +} + } diff --git a/test/unittest/src/crypto_mac_test.cpp b/test/unittest/src/crypto_mac_test.cpp index c64b59960ceb678c648524ce438ab16021df3b96..53e5c40ddcc597424100fe98038554b31282fa24 100644 --- a/test/unittest/src/crypto_mac_test.cpp +++ b/test/unittest/src/crypto_mac_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Huawei Device Co., Ltd. + * Copyright (C) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -43,6 +43,7 @@ constexpr uint32_t SHA224_LEN = 28; constexpr uint32_t SHA256_LEN = 32; constexpr uint32_t SHA384_LEN = 48; constexpr uint32_t SHA512_LEN = 64; +constexpr uint32_t MD5_LEN = 16; static char g_testBigData[] = "VqRH5dzdeeturr5zN5vE77DtqjV7kNKbDJqk4mNqyYRTXymhjR\r\n" "Yz8c2pNvJiCxyFwvLvWfPh2Y2eDAuxbdm2Dt4UzKJtNqEpNYKVZLiyH4a4MhR4BpFhvhJVHy2ALbYq2rW\r\n" @@ -910,4 +911,36 @@ HWTEST_F(CryptoMacTest, InvalidSpiClassMacTest001, TestSize.Level0) HcfObjDestroy(key); HcfObjDestroy(generator); } + +HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest012, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfResult ret = HcfMacCreate("MD5", &macObj); + EXPECT_EQ(ret, HCF_SUCCESS); + // create a symKey generator + HcfSymKeyGenerator *generator = nullptr; + ret = HcfSymKeyGeneratorCreate("HMAC|MD5", &generator); + EXPECT_EQ(ret, HCF_SUCCESS); + HcfSymKey *key = nullptr; + generator->generateSymKey(generator, &key); + // set input and output blob + uint8_t testData[] = "My test data"; + HcfBlob inBlob = {.data = reinterpret_cast(testData), .len = sizeof(testData)}; + HcfBlob outBlob = { .data = nullptr, .len = 0 }; + // test api funcitons + ret = macObj->init(macObj, key); + EXPECT_EQ(ret, HCF_SUCCESS); + ret = macObj->update(macObj, &inBlob); + EXPECT_EQ(ret, HCF_SUCCESS); + ret = macObj->doFinal(macObj, &outBlob); + EXPECT_EQ(ret, HCF_SUCCESS); + uint32_t len = macObj->getMacLength(macObj); + EXPECT_EQ(len, MD5_LEN); + // destroy the API obj and blob data + HcfBlobDataClearAndFree(&outBlob); + HcfObjDestroy(macObj); + HcfObjDestroy(key); + HcfObjDestroy(generator); +} + } \ No newline at end of file