diff --git a/common/inc/config.h b/common/inc/config.h index 60bc39ac16360d395365c51099953d4e522ab8a1..1d1574eeacf6abc5bb149c25ae60b7c79c939187 100644 --- a/common/inc/config.h +++ b/common/inc/config.h @@ -18,6 +18,8 @@ #define HCF_MAX_STR_LEN 1024 // input string parameter max length limit, include \0 #define HCF_MAX_ALGO_NAME_LEN 128 // input algoName parameter max length limit, include \0 +#define HCF_MAX_MD_NAME_LEN 128 // input mdName parameter max length limit, include \0 +#define HCF_MAX_CIPHER_NAME_LEN 128 // input cipherName parameter max length limit, include \0 #define LOG_PRINT_MAX_LEN 256 // log max length limit #define HCF_MAX_BUFFER_LEN 8192 #define SERIAL_NUMBER_HEDER_SIZE 2 diff --git a/frameworks/cj/include/crypto_ffi.h b/frameworks/cj/include/crypto_ffi.h index 2aefc5673feb97c0ac59a1a0cbd0f1b8ce73f921..c1ff81ed48247d30d8142b2e9e27b41ba28a38d7 100644 --- a/frameworks/cj/include/crypto_ffi.h +++ b/frameworks/cj/include/crypto_ffi.h @@ -42,6 +42,7 @@ #include "sym_key_generator_impl.h" #include "sym_key_impl.h" #include "verify_impl.h" +#include "detailed_hmac_params.h" extern "C" { typedef struct { diff --git a/frameworks/cj/src/crypto_ffi.cpp b/frameworks/cj/src/crypto_ffi.cpp index 3ecbf2cb6262c30f38b5e4973feacb953a81d604..101a27f525a4592e7b02a056c3e82f00c7b21018 100644 --- a/frameworks/cj/src/crypto_ffi.cpp +++ b/frameworks/cj/src/crypto_ffi.cpp @@ -601,7 +601,10 @@ namespace OHOS { { LOGD("[Mac] CreateMac start"); HcfMac *macObj = nullptr; - HcfResult res = HcfMacCreate(algName, &macObj); + HcfHmacParamsSpec paramsSpec = {}; + paramsSpec.base.algName = "HMAC"; + paramsSpec.mdName = algName; + HcfResult res = HcfMacCreate(reinterpret_cast(¶msSpec), &macObj); *errCode = static_cast(res); if (res != HCF_SUCCESS) { LOGE("create c macObj failed."); diff --git a/frameworks/crypto_operation/mac.c b/frameworks/crypto_operation/mac.c index 245d5e8e076b155d3a5989710668b65867046d34..948f7b52cceb7bca7d0d3c811d57ebaead656d61 100644 --- a/frameworks/crypto_operation/mac.c +++ b/frameworks/crypto_operation/mac.c @@ -19,13 +19,15 @@ #include "mac_spi.h" #include "mac_openssl.h" +#include "detailed_hmac_params.h" +#include "detailed_cmac_params.h" #include "log.h" #include "config.h" #include "memory.h" #include "utils.h" -typedef HcfResult (*HcfMacSpiCreateFunc)(const char *, HcfMacSpi **); +typedef HcfResult (*HcfMacSpiCreateFunc)(HcfMacParamsSpec *, HcfMacSpi **); typedef struct { HcfMac base; @@ -36,19 +38,19 @@ typedef struct { } HcfMacImpl; typedef struct { - char *algoName; + char *mdName; HcfMacSpiCreateFunc createSpiFunc; -} HcfMacAbility; - -static const HcfMacAbility MAC_ABILITY_SET[] = { - { "SHA1", OpensslMacSpiCreate }, - { "SHA224", OpensslMacSpiCreate }, - { "SHA256", OpensslMacSpiCreate }, - { "SHA384", OpensslMacSpiCreate }, - { "SHA512", OpensslMacSpiCreate }, - { "SM3", OpensslMacSpiCreate }, - { "MD5", OpensslMacSpiCreate }, +} HcfHmacAbility; + +static const HcfHmacAbility HMAC_ABILITY_SET[] = { + { "SHA1", OpensslHmacSpiCreate }, + { "SHA224", OpensslHmacSpiCreate }, + { "SHA256", OpensslHmacSpiCreate }, + { "SHA384", OpensslHmacSpiCreate }, + { "SHA512", OpensslHmacSpiCreate }, + { "SM3", OpensslHmacSpiCreate }, + { "MD5", OpensslHmacSpiCreate }, }; static const char *GetMacClass(void) @@ -56,14 +58,14 @@ static const char *GetMacClass(void) return "HMAC"; } -static HcfMacSpiCreateFunc FindAbility(const char *algoName) +static HcfMacSpiCreateFunc FindAbility(const char *mdName) { - for (uint32_t i = 0; i < (sizeof(MAC_ABILITY_SET) / sizeof(MAC_ABILITY_SET[0])); i++) { - if (strcmp(MAC_ABILITY_SET[i].algoName, algoName) == 0) { - return MAC_ABILITY_SET[i].createSpiFunc; + for (uint32_t i = 0; i < (sizeof(HMAC_ABILITY_SET) / sizeof(HMAC_ABILITY_SET[0])); i++) { + if (strcmp(HMAC_ABILITY_SET[i].mdName, mdName) == 0) { + return HMAC_ABILITY_SET[i].createSpiFunc; } } - LOGE("Algo not support! [Algo]: %s", algoName); + LOGE("Algo not support! [Algo]: %s", mdName); return NULL; } @@ -151,29 +153,78 @@ static void MacDestroy(HcfObjectBase *self) HcfFree(impl); } -HcfResult HcfMacCreate(const char *algoName, HcfMac **mac) +static HcfResult SetMacAlgoName(HcfMacImpl *macImpl, const char *algoName) { - if (!HcfIsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (mac == NULL)) { - LOGE("Invalid input params while creating mac!"); + if (strcpy_s(macImpl->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) { + LOGE("Failed to copy algoName!"); return HCF_INVALID_PARAMS; } - HcfMacSpiCreateFunc createSpiFunc = FindAbility(algoName); - if (createSpiFunc == NULL) { - LOGE("Algo name is error!"); + return HCF_SUCCESS; +} + +static HcfResult HandleCmacAlgo(HcfMacImpl *macImpl, const HcfMacParamsSpec *paramsSpec, + HcfMacSpiCreateFunc *createSpiFunc) +{ + const char *cipherName = ((HcfCmacParamsSpec *)paramsSpec)->cipherName; + if (cipherName == NULL) { + LOGE("Invalid cipher name: null pointer."); return HCF_INVALID_PARAMS; } + + if ((strcmp(cipherName, "AES128") != 0) && (strcmp(cipherName, "AES256") != 0)) { + LOGE("Unsupported cipher name: %s, only support AES128 and AES256.", cipherName); + return HCF_INVALID_PARAMS; + } + *createSpiFunc = OpensslCmacSpiCreate; + return SetMacAlgoName(macImpl, cipherName); +} + +static HcfResult HandleHmacAlgo(HcfMacImpl *macImpl, const HcfMacParamsSpec *paramsSpec, + HcfMacSpiCreateFunc *createSpiFunc) +{ + const char *mdName = ((HcfHmacParamsSpec *)paramsSpec)->mdName; + *createSpiFunc = FindAbility(mdName); + if (*createSpiFunc == NULL) { + LOGE("Unsupported HMAC algorithm: %s", mdName); + return HCF_INVALID_PARAMS; + } + return SetMacAlgoName(macImpl, mdName); +} + +HcfResult HcfMacCreate(HcfMacParamsSpec *paramsSpec, HcfMac **mac) +{ + if (paramsSpec == NULL || !HcfIsStrValid(paramsSpec->algName, HCF_MAX_ALGO_NAME_LEN) || (mac == NULL)) { + LOGE("Invalid input params while creating mac!"); + return HCF_INVALID_PARAMS; + } + HcfMacSpiCreateFunc createSpiFunc = NULL; HcfMacImpl *returnMacApi = (HcfMacImpl *)HcfMalloc(sizeof(HcfMacImpl), 0); if (returnMacApi == NULL) { LOGE("Failed to allocate Mac Obj memory!"); return HCF_ERR_MALLOC; } - if (strcpy_s(returnMacApi->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) { - LOGE("Failed to copy algoName!"); + + HcfResult res = HCF_INVALID_PARAMS; + if (strcmp(paramsSpec->algName, "CMAC") == 0) { + res = HandleCmacAlgo(returnMacApi, paramsSpec, &createSpiFunc); + } else if (strcmp(paramsSpec->algName, "HMAC") == 0) { + res = HandleHmacAlgo(returnMacApi, paramsSpec, &createSpiFunc); + } else { + LOGE("Unsupported algorithm: %s", paramsSpec->algName); + HcfFree(returnMacApi); + return HCF_INVALID_PARAMS; + } + + if (res != HCF_SUCCESS) { HcfFree(returnMacApi); + return res; + } + if (createSpiFunc == NULL) { + LOGE("Algo name is error!"); return HCF_INVALID_PARAMS; } HcfMacSpi *spiObj = NULL; - HcfResult res = createSpiFunc(algoName, &spiObj); + res = createSpiFunc(paramsSpec, &spiObj); if (res != HCF_SUCCESS) { LOGE("Failed to create spi object!"); HcfFree(returnMacApi); diff --git a/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h b/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h index 0974a4eaa290100f28c74ed07253234a8beaa03f..923d1e2165ad76416e51f9b068d825abdca22d83 100644 --- a/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h +++ b/frameworks/js/napi/crypto/inc/napi_crypto_framework_defines.h @@ -100,6 +100,9 @@ const std::string HKDF_ALG_NAME = "HKDF"; const std::string HKDF_PARAMS_KEY = "key"; const std::string HKDF_PARAMS_INFO = "info"; +const std::string MD_NAME = "mdName"; +const std::string CIPHER_NAME = "cipherName"; + } // namespace CryptoFramework } // namespace OHOS diff --git a/frameworks/js/napi/crypto/src/napi_mac.cpp b/frameworks/js/napi/crypto/src/napi_mac.cpp index 1d91c44e30defb7e41f16ffeab053f5981fb6b9b..54accc7bd8a00e05b75c1fded55b35bdc11806ee 100644 --- a/frameworks/js/napi/crypto/src/napi_mac.cpp +++ b/frameworks/js/napi/crypto/src/napi_mac.cpp @@ -19,6 +19,9 @@ #include "log.h" #include "memory.h" +#include "mac_params.h" +#include "detailed_hmac_params.h" +#include "detailed_cmac_params.h" #include "napi_sym_key.h" #include "napi_utils.h" #include "napi_crypto_framework_defines.h" @@ -89,6 +92,16 @@ static void FreeCryptoFwkCtx(napi_env env, MacCtx *context) HcfFree(context); } +static void FreeMacParams(HcfMacParamsSpec *params) +{ + if (strcmp(params->algName, "HMAC") == 0) { + HcfFree(static_cast(const_cast(((HcfHmacParamsSpec *)params)->mdName))); + } else if (strcmp(params->algName, "CMAC") == 0) { + HcfFree(static_cast(const_cast(((HcfCmacParamsSpec *)params)->cipherName))); + } + HcfFree(params); +} + static void ReturnCallbackResult(napi_env env, MacCtx *context, napi_value result) { napi_value businessError = nullptr; @@ -647,6 +660,177 @@ static napi_value NapiWrapMac(napi_env env, napi_value instance, NapiMac *macNap return instance; } +static bool GetHmacParamsSpec(napi_env env, napi_value arg, const char *algName, HcfMacParamsSpec **paramsSpec) +{ + napi_value data = nullptr; + napi_valuetype valueType = napi_undefined; + if ((env == nullptr) || (arg == nullptr) || (paramsSpec == nullptr)) { + LOGE("Invalid params!"); + return false; + } + + napi_status status = napi_get_named_property(env, arg, MD_NAME.c_str(), &data); + napi_typeof(env, data, &valueType); + if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) { + LOGE("failed to get valid algo name!"); + return false; + } + std::string mdName; + if (!GetStringFromJSParams(env, data, mdName)) { + LOGE("GetStringFromJSParams failed!"); + return false; + } + HcfHmacParamsSpec *tmp = static_cast(HcfMalloc(sizeof(HcfHmacParamsSpec), 0)); + if (tmp == nullptr) { + LOGE("malloc hmac spec failed!"); + return false; + } + char* mdNameCopy = static_cast(HcfMalloc(mdName.length() + 1, 0)); + if (mdNameCopy == nullptr) { + LOGE("malloc mdName failed!"); + HcfFree(tmp); + return false; + } + if (memcpy_s(mdNameCopy, mdName.length() + 1, mdName.c_str(), mdName.length() + 1) != EOK) { + LOGE("copy mdName failed!"); + HcfFree(mdNameCopy); + HcfFree(tmp); + return false; + } + tmp->base.algName = algName; + tmp->mdName = mdNameCopy; + *paramsSpec = reinterpret_cast(tmp); + return true; +} + +static bool GetCmacParamsSpec(napi_env env, napi_value arg, const char *algName, HcfMacParamsSpec **paramsSpec) +{ + napi_value data = nullptr; + napi_valuetype valueType = napi_undefined; + if ((env == nullptr) || (arg == nullptr) || (paramsSpec == nullptr)) { + LOGE("Invalid params!"); + return false; + } + + napi_status status = napi_get_named_property(env, arg, CIPHER_NAME.c_str(), &data); + napi_typeof(env, data, &valueType); + if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) { + LOGE("failed to get valid algo name!"); + return false; + } + std::string cipherName; + if (!GetStringFromJSParams(env, data, cipherName)) { + LOGE("GetStringFromJSParams failed!"); + return false; + } + HcfCmacParamsSpec *tmp = nullptr; + tmp = static_cast(HcfMalloc(sizeof(HcfCmacParamsSpec), 0)); + if (tmp == nullptr) { + LOGE("malloc hmac spec failed!"); + return false; + } + char* cipherNameCopy = static_cast(HcfMalloc(cipherName.length() + 1, 0)); + if (cipherNameCopy == nullptr) { + LOGE("malloc cipherName failed!"); + HcfFree(tmp); + return false; + } + if (memcpy_s(cipherNameCopy, cipherName.length() + 1, cipherName.c_str(), cipherName.length() + 1) != EOK) { + LOGE("copy cipherName failed!"); + HcfFree(cipherNameCopy); + HcfFree(tmp); + return false; + } + tmp->base.algName = algName; + tmp->cipherName = cipherNameCopy; + *paramsSpec = reinterpret_cast(tmp); + return true; +} + +static bool GetMacSpecFromJSParams(napi_env env, napi_value arg, HcfMacParamsSpec **params) +{ + napi_value data = nullptr; + napi_valuetype valueType = napi_undefined; + if ((env == nullptr) || (arg == nullptr) || (params == nullptr)) { + LOGE("Invalid params!"); + return false; + } + + napi_status status = napi_get_named_property(env, arg, ALGO_PARAMS.c_str(), &data); + napi_typeof(env, data, &valueType); + if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) { + LOGE("failed to get valid algo name!"); + return false; + } + std::string algoName; + if (!GetStringFromJSParams(env, data, algoName)) { + LOGE("GetStringFromJSParams failed!"); + return false; + } + if (algoName.compare("HMAC") == 0) { + return GetHmacParamsSpec(env, arg, algoName.c_str(), params); + } else if (algoName.compare("CMAC") == 0) { + return GetCmacParamsSpec(env, arg, algoName.c_str(), params); + } else { + LOGE("Not support that alg"); + return false; + } + return true; +} + +static bool GetStringMacParams(napi_env env, napi_value *argv, HcfMacParamsSpec **paramsSpec) +{ + if ((env == nullptr) || (paramsSpec == nullptr)) { + LOGE("Invalid params!"); + return false; + } + std::string algoName; + if (!GetStringFromJSParams(env, argv[PARAM0], algoName)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Failed to get algorithm.")); + LOGE("Failed to get algorithm."); + return false; + } + *paramsSpec = reinterpret_cast(HcfMalloc(sizeof(HcfHmacParamsSpec), 0)); + if (*paramsSpec == nullptr) { + napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "Failed to allocate memory.")); + LOGE("Failed to allocate memory."); + return false; + } + char* mdNameCopy = static_cast(HcfMalloc(algoName.length() + 1, 0)); + if (mdNameCopy == nullptr) { + LOGE("malloc mdName failed!"); + return false; + } + if (memcpy_s(mdNameCopy, algoName.length() + 1, algoName.c_str(), algoName.length() + 1) != EOK) { + LOGE("copy mdName failed!"); + HcfFree(mdNameCopy); + return false; + } + (reinterpret_cast(*paramsSpec))->base.algName = "HMAC"; + (reinterpret_cast(*paramsSpec))->mdName = mdNameCopy; + return true; +} + +static bool SetparamsSpec(napi_env env, napi_value *argv, HcfMacParamsSpec **paramsSpec) +{ + napi_valuetype valueType; + napi_typeof(env, argv[PARAM0], &valueType); + if (valueType == napi_string) { + if (!GetStringMacParams(env, argv, paramsSpec)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Failed to get mac params.")); + LOGE("Failed to get mac params."); + return false; + } + } else { + if (!GetMacSpecFromJSParams(env, argv[PARAM0], paramsSpec)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Failed to get mac params.")); + LOGE("Failed to get mac params."); + return false; + } + } + return true; +} + napi_value NapiMac::CreateMac(napi_env env, napi_callback_info info) { size_t expectedArgc = ARGS_SIZE_ONE; @@ -658,21 +842,25 @@ napi_value NapiMac::CreateMac(napi_env env, napi_callback_info info) LOGE("The input args num is invalid."); return nullptr; } - std::string algoName; - if (!GetStringFromJSParams(env, argv[PARAM0], algoName)) { - napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Failed to get algorithm.")); - LOGE("Failed to get algorithm."); + + HcfMacParamsSpec *paramsSpec = nullptr; + if (!SetparamsSpec(env, argv, ¶msSpec)) { + napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Failed to set mac params.")); + LOGE("Failed to set mac params."); return nullptr; } + HcfMac *macObj = nullptr; - HcfResult res = HcfMacCreate(algoName.c_str(), &macObj); + HcfResult res = HcfMacCreate(paramsSpec, &macObj); if (res != HCF_SUCCESS) { napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.")); LOGE("create c macObj failed."); + FreeMacParams(paramsSpec); return nullptr; } + FreeMacParams(paramsSpec); napi_value napiAlgName = nullptr; - napi_create_string_utf8(env, algoName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName); + napi_create_string_utf8(env, paramsSpec->algName, NAPI_AUTO_LENGTH, &napiAlgName); napi_value instance = nullptr; napi_value constructor = nullptr; napi_get_reference_value(env, classRef_, &constructor); diff --git a/interfaces/inner_api/algorithm_parameter/detailed_cmac_params.h b/interfaces/inner_api/algorithm_parameter/detailed_cmac_params.h new file mode 100644 index 0000000000000000000000000000000000000000..52228030a5c5382affb2a010c80b6765f66464df --- /dev/null +++ b/interfaces/inner_api/algorithm_parameter/detailed_cmac_params.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2025 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_DETAILED_CMAC_PARAMS_H +#define HCF_DETAILED_CMAC_PARAMS_H + +#include "mac_params.h" + +typedef struct HcfCmacParamsSpec HcfCmacParamsSpec; + +struct HcfCmacParamsSpec { + HcfMacParamsSpec base; + const char *cipherName; +}; + +#endif // HCF_DETAILED_CMAC_PARAMS_H diff --git a/interfaces/inner_api/algorithm_parameter/detailed_hmac_params.h b/interfaces/inner_api/algorithm_parameter/detailed_hmac_params.h new file mode 100644 index 0000000000000000000000000000000000000000..c9fb55deebc7fb14592d7c84c19634a3de89e091 --- /dev/null +++ b/interfaces/inner_api/algorithm_parameter/detailed_hmac_params.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_DETAILED_HMAC_PARAMS_H +#define HCF_DETAILED_HMAC_PARAMS_H + +#include "mac_params.h" + +typedef struct HcfHmacParamsSpec HcfHmacParamsSpec; + +struct HcfHmacParamsSpec { + HcfMacParamsSpec base; + const char *mdName; +}; + +#endif // HCF_DETAILED_HMAC_PARAMS_H diff --git a/interfaces/inner_api/algorithm_parameter/mac_params.h b/interfaces/inner_api/algorithm_parameter/mac_params.h new file mode 100644 index 0000000000000000000000000000000000000000..379fd40eae665113436ebf38ee604d4debf5e6c0 --- /dev/null +++ b/interfaces/inner_api/algorithm_parameter/mac_params.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HCF_MAC_PARAMS_H +#define HCF_MAC_PARAMS_H + +typedef struct HcfMacParamsSpec HcfMacParamsSpec; + +struct HcfMacParamsSpec { + const char *algName; +}; + +#endif // HCF_MAC_PARAMS_H diff --git a/interfaces/inner_api/crypto_operation/mac.h b/interfaces/inner_api/crypto_operation/mac.h index f3ecfddcd817c02dd6de9695ed60e38e9d6f0b7f..cd7811aa7de196ab826047c4d50f5f1ff9635ff8 100644 --- a/interfaces/inner_api/crypto_operation/mac.h +++ b/interfaces/inner_api/crypto_operation/mac.h @@ -21,6 +21,7 @@ #include "result.h" #include "sym_key.h" #include "object_base.h" +#include "mac_params.h" typedef struct HcfMac HcfMac; @@ -42,7 +43,7 @@ struct HcfMac { extern "C" { #endif -HcfResult HcfMacCreate(const char *algoName, HcfMac **mac); +HcfResult HcfMacCreate(HcfMacParamsSpec *paramsSpec, HcfMac **mac); #ifdef __cplusplus } diff --git a/plugin/openssl_plugin/common/inc/openssl_adapter.h b/plugin/openssl_plugin/common/inc/openssl_adapter.h index d624f9c8b52f44b1b19a245b223c56c4e343202a..e804d0829ac60b57532ad661dd3eaeeda8b1aa1f 100644 --- a/plugin/openssl_plugin/common/inc/openssl_adapter.h +++ b/plugin/openssl_plugin/common/inc/openssl_adapter.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -248,6 +249,14 @@ size_t OpensslHmacSize(const HMAC_CTX *ctx); void OpensslHmacCtxFree(HMAC_CTX *ctx); HMAC_CTX *OpensslHmacCtxNew(void); +int OpensslCmacInit(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen, const OSSL_PARAM params[]); +int OpensslCmacUpdate(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen); +int OpensslCmacFinal(EVP_MAC_CTX *ctx, unsigned char *out, size_t *outl, size_t outsize); +size_t OpensslCmacSize(EVP_MAC_CTX *ctx); +void OpensslCmacCtxFree(EVP_MAC_CTX *ctx); +EVP_MAC_CTX *OpensslCmacCtxNew(EVP_MAC *mac); +void OpensslMacFree(EVP_MAC *mac); + void OpensslEvpCipherCtxFree(EVP_CIPHER_CTX *ctx); const EVP_CIPHER *OpensslEvpAes128Ecb(void); const EVP_CIPHER *OpensslEvpAes192Ecb(void); diff --git a/plugin/openssl_plugin/common/src/openssl_adapter.c b/plugin/openssl_plugin/common/src/openssl_adapter.c index fb3b1e0d571fc416d96746e517a9c79abaaeda98..ffac3335824bf03dd676ae8eb96dc5104218c98c 100644 --- a/plugin/openssl_plugin/common/src/openssl_adapter.c +++ b/plugin/openssl_plugin/common/src/openssl_adapter.c @@ -934,6 +934,41 @@ HMAC_CTX *OpensslHmacCtxNew(void) return HMAC_CTX_new(); } +int OpensslCmacInit(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen, const OSSL_PARAM params[]) +{ + return EVP_MAC_init(ctx, key, keylen, params); +} + +int OpensslCmacUpdate(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen) +{ + return EVP_MAC_update(ctx, data, datalen); +} + +int OpensslCmacFinal(EVP_MAC_CTX *ctx, unsigned char *out, size_t *outl, size_t outsize) +{ + return EVP_MAC_final(ctx, out, outl, outsize); +} + +size_t OpensslCmacSize(EVP_MAC_CTX *ctx) +{ + return EVP_MAC_CTX_get_mac_size(ctx); +} + +void OpensslCmacCtxFree(EVP_MAC_CTX *ctx) +{ + EVP_MAC_CTX_free(ctx); +} + +void OpensslMacFree(EVP_MAC *mac) +{ + EVP_MAC_free(mac); +} + +EVP_MAC_CTX *OpensslCmacCtxNew(EVP_MAC *mac) +{ + return EVP_MAC_CTX_new(mac); +} + void OpensslEvpCipherCtxFree(EVP_CIPHER_CTX *ctx) { EVP_CIPHER_CTX_free(ctx); diff --git a/plugin/openssl_plugin/crypto_operation/hmac/inc/mac_openssl.h b/plugin/openssl_plugin/crypto_operation/hmac/inc/mac_openssl.h index 419dc306ea4a9ef80ec03ffb33e15884638117bb..a272e91cb19233590ebbf5ba59529b3688fb7066 100644 --- a/plugin/openssl_plugin/crypto_operation/hmac/inc/mac_openssl.h +++ b/plugin/openssl_plugin/crypto_operation/hmac/inc/mac_openssl.h @@ -16,6 +16,7 @@ #define HCF_HMAC_OPENSSL_H #include "mac_spi.h" +#include "mac_params.h" #define HCF_OPENSSL_INVALID_MAC_LEN 0 @@ -23,7 +24,8 @@ extern "C" { #endif -HcfResult OpensslMacSpiCreate(const char *opensslAlgoName, HcfMacSpi **spiObj); +HcfResult OpensslHmacSpiCreate(HcfMacParamsSpec *paramsSpec, HcfMacSpi **spiObj); +HcfResult OpensslCmacSpiCreate(HcfMacParamsSpec *paramsSpec, HcfMacSpi **spiObj); #ifdef __cplusplus } 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 4e3d9baea9ca4799715d6be81afa466fe8d22f24..4e88ec0291cb8debe46c40ccf58d300c3dcbcefd 100644 --- a/plugin/openssl_plugin/crypto_operation/hmac/src/mac_openssl.c +++ b/plugin/openssl_plugin/crypto_operation/hmac/src/mac_openssl.c @@ -23,30 +23,54 @@ #include "memory.h" #include "config.h" #include "utils.h" +#include "detailed_hmac_params.h" +#include "detailed_cmac_params.h" typedef struct { HcfMacSpi base; HMAC_CTX *ctx; - char opensslAlgoName[HCF_MAX_ALGO_NAME_LEN]; -} HcfMacSpiImpl; + char opensslMdName[HCF_MAX_MD_NAME_LEN]; +} HcfHmacSpiImpl; -static const char *OpensslGetMacClass(void) +typedef struct { + HcfMacSpi base; + + EVP_MAC_CTX *ctx; + + char opensslCipherName[HCF_MAX_CIPHER_NAME_LEN]; +} HcfCmacSpiImpl; + +static const char *OpensslGetHmacClass(void) +{ + return "OpensslHmac"; +} + +static const char *OpensslGetCmacClass(void) +{ + return "OpensslCmac"; +} + +static HMAC_CTX *OpensslGetHmacCtx(HcfMacSpi *self) { - return "OpensslMac"; + if (!HcfIsClassMatch((HcfObjectBase *)self, OpensslGetHmacClass())) { + LOGE("Class is not match."); + return NULL; + } + return ((HcfHmacSpiImpl *)self)->ctx; } -static HMAC_CTX *OpensslGetMacCtx(HcfMacSpi *self) +static EVP_MAC_CTX *OpensslGetCmacCtx(HcfMacSpi *self) { - if (!HcfIsClassMatch((HcfObjectBase *)self, OpensslGetMacClass())) { + if (!HcfIsClassMatch((HcfObjectBase *)self, OpensslGetCmacClass())) { LOGE("Class is not match."); return NULL; } - return ((HcfMacSpiImpl *)self)->ctx; + return ((HcfCmacSpiImpl *)self)->ctx; } -static const EVP_MD *OpensslGetMacAlgoFromString(const char *mdName) +static const EVP_MD *OpensslGetHmacAlgoFromString(const char *mdName) { if (strcmp(mdName, "SHA1") == 0) { return OpensslEvpSha1(); @@ -66,9 +90,9 @@ static const EVP_MD *OpensslGetMacAlgoFromString(const char *mdName) return NULL; } -static HcfResult OpensslEngineInitMac(HcfMacSpi *self, const HcfSymKey *key) +static HcfResult OpensslEngineInitHmac(HcfMacSpi *self, const HcfSymKey *key) { - if (OpensslGetMacCtx(self) == NULL) { + if (OpensslGetHmacCtx(self) == NULL) { LOGD("[error] The CTX is NULL!"); return HCF_ERR_CRYPTO_OPERATION; } @@ -76,7 +100,7 @@ static HcfResult OpensslEngineInitMac(HcfMacSpi *self, const HcfSymKey *key) LOGE("Class is not match."); return HCF_INVALID_PARAMS; } - if (!HcfIsClassMatch((HcfObjectBase *)self, OpensslGetMacClass())) { + if (!HcfIsClassMatch((HcfObjectBase *)self, OpensslGetHmacClass())) { LOGE("Class is not match."); return HCF_INVALID_PARAMS; } @@ -85,8 +109,8 @@ static HcfResult OpensslEngineInitMac(HcfMacSpi *self, const HcfSymKey *key) LOGE("Invalid keyMaterial"); return HCF_INVALID_PARAMS; } - const EVP_MD *mdfunc = OpensslGetMacAlgoFromString(((HcfMacSpiImpl *)self)->opensslAlgoName); - int32_t ret = OpensslHmacInitEx(OpensslGetMacCtx(self), keyBlob.data, keyBlob.len, mdfunc, NULL); + const EVP_MD *mdfunc = OpensslGetHmacAlgoFromString(((HcfHmacSpiImpl *)self)->opensslMdName); + int32_t ret = OpensslHmacInitEx(OpensslGetHmacCtx(self), keyBlob.data, keyBlob.len, mdfunc, NULL); if (ret != HCF_OPENSSL_SUCCESS) { LOGD("[error] HMAC_Init_ex return error!"); HcfPrintOpensslError(); @@ -95,13 +119,13 @@ static HcfResult OpensslEngineInitMac(HcfMacSpi *self, const HcfSymKey *key) return HCF_SUCCESS; } -static HcfResult OpensslEngineUpdateMac(HcfMacSpi *self, HcfBlob *input) +static HcfResult OpensslEngineUpdateHmac(HcfMacSpi *self, HcfBlob *input) { - if (OpensslGetMacCtx(self) == NULL) { + if (OpensslGetHmacCtx(self) == NULL) { LOGD("[error] The CTX is NULL!"); return HCF_ERR_CRYPTO_OPERATION; } - if (HMAC_Update(OpensslGetMacCtx(self), input->data, input->len) != HCF_OPENSSL_SUCCESS) { + if (HMAC_Update(OpensslGetHmacCtx(self), input->data, input->len) != HCF_OPENSSL_SUCCESS) { LOGD("[error] HMAC_Update return error!"); HcfPrintOpensslError(); return HCF_ERR_CRYPTO_OPERATION; @@ -109,15 +133,15 @@ static HcfResult OpensslEngineUpdateMac(HcfMacSpi *self, HcfBlob *input) return HCF_SUCCESS; } -static HcfResult OpensslEngineDoFinalMac(HcfMacSpi *self, HcfBlob *output) +static HcfResult OpensslEngineDoFinalHmac(HcfMacSpi *self, HcfBlob *output) { - if (OpensslGetMacCtx(self) == NULL) { + if (OpensslGetHmacCtx(self) == NULL) { LOGD("[error] The CTX is NULL!"); return HCF_ERR_CRYPTO_OPERATION; } unsigned char outputBuf[EVP_MAX_MD_SIZE]; uint32_t outputLen; - int32_t ret = OpensslHmacFinal(OpensslGetMacCtx(self), outputBuf, &outputLen); + int32_t ret = OpensslHmacFinal(OpensslGetHmacCtx(self), outputBuf, &outputLen); if (ret != HCF_OPENSSL_SUCCESS) { LOGD("[error] HMAC_Final return error!"); HcfPrintOpensslError(); @@ -133,43 +157,43 @@ static HcfResult OpensslEngineDoFinalMac(HcfMacSpi *self, HcfBlob *output) return HCF_SUCCESS; } -static uint32_t OpensslEngineGetMacLength(HcfMacSpi *self) +static uint32_t OpensslEngineGetHmacLength(HcfMacSpi *self) { - if (OpensslGetMacCtx(self) == NULL) { + if (OpensslGetHmacCtx(self) == NULL) { LOGD("[error] The CTX is NULL!"); return HCF_OPENSSL_INVALID_MAC_LEN; } - return OpensslHmacSize(OpensslGetMacCtx(self)); + return OpensslHmacSize(OpensslGetHmacCtx(self)); } -static void OpensslDestroyMac(HcfObjectBase *self) +static void OpensslDestroyHmac(HcfObjectBase *self) { if (self == NULL) { LOGE("Self ptr is NULL"); return; } - if (!HcfIsClassMatch(self, OpensslGetMacClass())) { + if (!HcfIsClassMatch(self, OpensslGetHmacClass())) { LOGE("Class is not match."); return; } - if (OpensslGetMacCtx((HcfMacSpi *)self) != NULL) { - OpensslHmacCtxFree(OpensslGetMacCtx((HcfMacSpi *)self)); + if (OpensslGetHmacCtx((HcfMacSpi *)self) != NULL) { + OpensslHmacCtxFree(OpensslGetHmacCtx((HcfMacSpi *)self)); } HcfFree(self); } -HcfResult OpensslMacSpiCreate(const char *opensslAlgoName, HcfMacSpi **spiObj) +HcfResult OpensslHmacSpiCreate(HcfMacParamsSpec *paramsSpec, HcfMacSpi **spiObj) { - if (spiObj == NULL) { + if (paramsSpec == NULL || spiObj == NULL) { LOGE("Invalid input parameter."); return HCF_INVALID_PARAMS; } - HcfMacSpiImpl *returnSpiImpl = (HcfMacSpiImpl *)HcfMalloc(sizeof(HcfMacSpiImpl), 0); + HcfHmacSpiImpl *returnSpiImpl = (HcfHmacSpiImpl *)HcfMalloc(sizeof(HcfHmacSpiImpl), 0); if (returnSpiImpl == NULL) { LOGE("Failed to allocate returnImpl memory!"); return HCF_ERR_MALLOC; } - if (strcpy_s(returnSpiImpl->opensslAlgoName, HCF_MAX_ALGO_NAME_LEN, opensslAlgoName) != EOK) { + if (strcpy_s(returnSpiImpl->opensslMdName, HCF_MAX_MD_NAME_LEN, ((HcfHmacParamsSpec *)paramsSpec)->mdName) != EOK) { LOGE("Failed to copy algoName!"); HcfFree(returnSpiImpl); return HCF_INVALID_PARAMS; @@ -180,12 +204,154 @@ HcfResult OpensslMacSpiCreate(const char *opensslAlgoName, HcfMacSpi **spiObj) HcfFree(returnSpiImpl); return HCF_ERR_CRYPTO_OPERATION; } - returnSpiImpl->base.base.getClass = OpensslGetMacClass; - returnSpiImpl->base.base.destroy = OpensslDestroyMac; - returnSpiImpl->base.engineInitMac = OpensslEngineInitMac; - returnSpiImpl->base.engineUpdateMac = OpensslEngineUpdateMac; - returnSpiImpl->base.engineDoFinalMac = OpensslEngineDoFinalMac; - returnSpiImpl->base.engineGetMacLength = OpensslEngineGetMacLength; + returnSpiImpl->base.base.getClass = OpensslGetHmacClass; + returnSpiImpl->base.base.destroy = OpensslDestroyHmac; + returnSpiImpl->base.engineInitMac = OpensslEngineInitHmac; + returnSpiImpl->base.engineUpdateMac = OpensslEngineUpdateHmac; + returnSpiImpl->base.engineDoFinalMac = OpensslEngineDoFinalHmac; + returnSpiImpl->base.engineGetMacLength = OpensslEngineGetHmacLength; + *spiObj = (HcfMacSpi *)returnSpiImpl; + return HCF_SUCCESS; +} + +static HcfResult OpensslEngineInitCmac(HcfMacSpi *self, const HcfSymKey *key) +{ + OSSL_PARAM params[4] = {}; + OSSL_PARAM *p = params; + if (OpensslGetCmacCtx(self) == NULL) { + LOGD("[error] The CTX is NULL!"); + return HCF_ERR_CRYPTO_OPERATION; + } + if (!HcfIsClassMatch((const HcfObjectBase *)key, OPENSSL_SYM_KEY_CLASS)) { + LOGE("Class is not match."); + return HCF_INVALID_PARAMS; + } + if (!HcfIsClassMatch((HcfObjectBase *)self, OpensslGetCmacClass())) { + LOGE("Class is not match."); + return HCF_INVALID_PARAMS; + } + HcfBlob keyBlob = ((SymKeyImpl *)key)->keyMaterial; + if (!HcfIsBlobValid(&keyBlob)) { + LOGE("Invalid keyMaterial"); + return HCF_INVALID_PARAMS; + } + *p++ = OpensslOsslParamConstructUtf8String("cipher", ((HcfCmacSpiImpl *)self)->opensslCipherName, + strlen(((HcfCmacSpiImpl *)self)->opensslCipherName)); + *p++ = OpensslOsslParamConstructEnd(); + int32_t ret = OpensslCmacInit(OpensslGetCmacCtx(self), keyBlob.data, keyBlob.len, params); + if (ret != HCF_OPENSSL_SUCCESS) { + LOGE("CMAC_Init return error!"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + return HCF_SUCCESS; +} + +static HcfResult OpensslEngineUpdateCmac(HcfMacSpi *self, HcfBlob *input) +{ + if (OpensslGetCmacCtx(self) == NULL) { + LOGE("The CTX is NULL!"); + return HCF_ERR_CRYPTO_OPERATION; + } + if (OpensslCmacUpdate(OpensslGetCmacCtx(self), input->data, input->len) != HCF_OPENSSL_SUCCESS) { + LOGE("CMAC_Update return error!"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + return HCF_SUCCESS; +} + +static HcfResult OpensslEngineDoFinalCmac(HcfMacSpi *self, HcfBlob *output) +{ + if (OpensslGetCmacCtx(self) == NULL) { + LOGE("The CTX is NULL!"); + return HCF_ERR_CRYPTO_OPERATION; + } + size_t outputLen = 0; + unsigned char outputBuf[EVP_MAX_MD_SIZE]; + int32_t ret = OpensslCmacFinal(OpensslGetCmacCtx(self), NULL, &outputLen, 0); + if (ret != HCF_OPENSSL_SUCCESS) { + LOGE("CMAC_Final return error!"); + HcfPrintOpensslError(); + return HCF_ERR_CRYPTO_OPERATION; + } + ret = OpensslCmacFinal(OpensslGetCmacCtx(self), outputBuf, &outputLen, outputLen); + if (ret != HCF_OPENSSL_SUCCESS) { + LOGE("CMAC_Final return error!"); + HcfPrintOpensslError(); + HcfFree(output->data); + return HCF_ERR_CRYPTO_OPERATION; + } + output->data = (uint8_t *)HcfMalloc(outputLen, 0); + if (output->data == NULL) { + LOGE("Failed to allocate output->data memory!"); + return HCF_ERR_MALLOC; + } + (void)memcpy_s(output->data, outputLen, outputBuf, outputLen); + output->len = outputLen; + return HCF_SUCCESS; +} + +static uint32_t OpensslEngineGetCmacLength(HcfMacSpi *self) +{ + if (OpensslGetCmacCtx(self) == NULL) { + LOGE("The CTX is NULL!"); + return HCF_OPENSSL_INVALID_MAC_LEN; + } + return OpensslCmacSize(OpensslGetCmacCtx(self)); +} + +static void OpensslDestroyCmac(HcfObjectBase *self) +{ + if (self == NULL) { + LOGE("Self ptr is NULL"); + return; + } + if (!HcfIsClassMatch(self, OpensslGetCmacClass())) { + LOGE("Class is not match."); + return; + } + if (OpensslGetCmacCtx((HcfMacSpi *)self) != NULL) { + OpensslCmacCtxFree(OpensslGetCmacCtx((HcfMacSpi *)self)); + } + HcfFree(self); +} + +HcfResult OpensslCmacSpiCreate(HcfMacParamsSpec *paramsSpec, HcfMacSpi **spiObj) +{ + if (paramsSpec == NULL || spiObj == NULL) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + HcfCmacSpiImpl *returnSpiImpl = (HcfCmacSpiImpl *)HcfMalloc(sizeof(HcfCmacSpiImpl), 0); + if (returnSpiImpl == NULL) { + LOGE("Failed to allocate returnImpl memory!"); + return HCF_ERR_MALLOC; + } + if (strcpy_s(returnSpiImpl->opensslCipherName, HCF_MAX_CIPHER_NAME_LEN, + ((HcfCmacParamsSpec *)paramsSpec)->cipherName) != EOK) { + LOGE("Failed to copy algoName!"); + HcfFree(returnSpiImpl); + return HCF_INVALID_PARAMS; + } + EVP_MAC *mac = EVP_MAC_fetch(NULL, "CMAC", NULL); + if (mac == NULL) { + LOGD("fetch failed"); + return HCF_ERR_CRYPTO_OPERATION; + } + returnSpiImpl->ctx = EVP_MAC_CTX_new(mac); + if (returnSpiImpl->ctx == NULL) { + LOGD("[error] Failed to create ctx!"); + HcfFree(returnSpiImpl); + return HCF_ERR_CRYPTO_OPERATION; + } + OpensslMacFree(mac); + returnSpiImpl->base.base.getClass = OpensslGetCmacClass; + returnSpiImpl->base.base.destroy = OpensslDestroyCmac; + returnSpiImpl->base.engineInitMac = OpensslEngineInitCmac; + returnSpiImpl->base.engineUpdateMac = OpensslEngineUpdateCmac; + returnSpiImpl->base.engineDoFinalMac = OpensslEngineDoFinalCmac; + returnSpiImpl->base.engineGetMacLength = OpensslEngineGetCmacLength; *spiObj = (HcfMacSpi *)returnSpiImpl; return HCF_SUCCESS; } \ No newline at end of file diff --git a/test/fuzztest/crypto_operation/hcfmaccreate_fuzzer/hcfmaccreate_fuzzer.cpp b/test/fuzztest/crypto_operation/hcfmaccreate_fuzzer/hcfmaccreate_fuzzer.cpp index 35be61e778715dd85daf27707424c01a398e0839..8ecdc70547c0acf27f3457c8453e9b78325cbf26 100755 --- a/test/fuzztest/crypto_operation/hcfmaccreate_fuzzer/hcfmaccreate_fuzzer.cpp +++ b/test/fuzztest/crypto_operation/hcfmaccreate_fuzzer/hcfmaccreate_fuzzer.cpp @@ -23,14 +23,18 @@ #include "mac.h" #include "result.h" #include "sym_key_generator.h" +#include "detailed_hmac_params.h" namespace OHOS { static const int KEY_LEN = 16; static void TestMacConvertSymKey(const uint8_t* data, size_t size) { + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; HcfMac *macObj = nullptr; - HcfResult res = HcfMacCreate("SHA1", &macObj); + HcfResult res = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); if (res != HCF_SUCCESS) { return; } @@ -47,8 +51,11 @@ namespace OHOS { static void TestMac(const uint8_t* data, size_t size) { + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; HcfMac *macObj = nullptr; - HcfResult res = HcfMacCreate("SHA1", &macObj); + HcfResult res = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); if (res != HCF_SUCCESS) { return; } @@ -75,11 +82,14 @@ namespace OHOS { bool HcfMacCreateFuzzTest(const uint8_t* data, size_t size) { + HcfHmacParamsSpec params = {}; + params.mdName = "SHA1"; std::string alg(reinterpret_cast(data), size); + params.base.algName = alg.c_str(); TestMacConvertSymKey(data, size); TestMac(data, size); HcfMac *macObj = nullptr; - HcfResult res = HcfMacCreate(alg.c_str(), &macObj); + HcfResult res = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); if (res != HCF_SUCCESS) { return false; } diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 869b1d140df317fbfe476c3d7d5bfe9fd5b85d83..ed5f29b7087ea107a442dd7fda2cffbc7b433f1d 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -49,6 +49,7 @@ ohos_unittest("crypto_framework_test") { "src/crypto_brainpool_key_agreement_test.cpp", "src/crypto_brainpool_no_length_sign_test.cpp", "src/crypto_brainpool_no_length_verify_test.cpp", + "src/crypto_cmac_test.cpp", "src/crypto_common_cov_test.cpp", "src/crypto_dh_asy_key_generator_by_spec_test.cpp", "src/crypto_dh_asy_key_generator_test.cpp", diff --git a/test/unittest/src/crypto_cmac_test.cpp b/test/unittest/src/crypto_cmac_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4df5204bb1ab198b4dd3af3d3a1ccc307acd97d1 --- /dev/null +++ b/test/unittest/src/crypto_cmac_test.cpp @@ -0,0 +1,572 @@ +/* + * Copyright (C) 2025 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "securec.h" + +#include "mac.h" +#include "sym_key_generator.h" +#include "mac_openssl.h" +#include "mac_params.h" +#include "detailed_cmac_params.h" + +#include "log.h" +#include "memory.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class CryptoCmacTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +constexpr uint32_t MAX_MAC_BLOB_LEN = 5000; +constexpr uint32_t INVALID_LEN = 0; +constexpr uint32_t AES128_LEN = 16; + +static char g_testBigData[] = "VqRH5dzdeeturr5zN5vE77DtqjV7kNKbDJqk4mNqyYRTXymhjR\r\n" +"Yz8c2pNvJiCxyFwvLvWfPh2Y2eDAuxbdm2Dt4UzKJtNqEpNYKVZLiyH4a4MhR4BpFhvhJVHy2ALbYq2rW\r\n" +"LqJfNzA6v8kHNaFypNDMgX35kifyCiYkq85XUKDJCdewUzCZ2N8twC8Z9kL37K67bkL35VYFZSXyrNTdV\r\n" +"pB6kqPjwZYrjx5tXzMMgJW8ePqmAhZUVjtPGXTLVt8BUnaVRuWjD97xjS3VH9EwFeyrqJ46B92rkuGexY\r\n" +"cjXuvhHTnQNPbYfake7KMEWG2wgGLmZmjnakULhFgjt6TQhvCWMJAHYn8Zgczd3C3HkPrQgUXJgAiwf3r\r\n" +"jJbgbBpQkkbcfMBZZ3SSLe2J9jw6MkdEf3eBQX9rFVQSgBQgZ9KEW8XLb5kCTcyhRZPBbiHD4qQRyrwKT\r\n" +"mnGZqP5Aru6GDkhFk78jfjtk35HyB7AY7UZXkczRfVYAxa5Mk256MhAHkE3uAvPZTyY7N3qk9U7cLTrce\r\n" +"wJLH6wrymrMvQWgpvrBevMghnURZUcZAWUznDn56WnwGAzYAWmJqdXqAfcvgZwCFTjxdiaEZGpEyUrcS8\r\n" +"nr48ZeXS5aytz5Y7RnU5SxcHbgF8PerWVdftxmghPAvGkQ6f3dcXr9w9bbGqg5KJHyQCxabp8bjZpyFdb\r\n" +"VTq8DpQ6AJjxdjn8cuLTf9giGFxDjtQncicUdqP7YvVDr5AFgWc83cddyryVLZEBGAFfqbbKWF9KnPjRZ\r\n" +"AbuZ7SqrkxhQHu87Hxh3xHUHB8Lb3DGZ4vhnqaLnJBxFK8Ve4F2FfbgfHfQtALFDUWp6dSz8Hvdpj4CGw\r\n" +"FaSb8b5hTemaQRguYAqaUwJVvZ7G2AwkFnV9PHUngmybAFxg8HMAT3K7yAiQJWWqPxdGq8jXPAqZFNkGu\r\n" +"2mnJ5xfnY3z63PFk6TXU9Ga2YmHvtycXxwqMBEctQRa3zVWGVSrh3NF6jXa\r\n"; + +void CryptoCmacTest::SetUpTestCase() {} +void CryptoCmacTest::TearDownTestCase() {} + +void CryptoCmacTest::SetUp() // add init here, this will be called before test. +{ +} + +void CryptoCmacTest::TearDown() // add destroy here, this will be called when test case done. +{ +} + +static void PrintfBlobInHex(uint8_t *data, size_t dataLen) +{ + for (size_t i = 0; i < dataLen; i++) { + printf("%02hhX", data[i]); + } + printf("\n"); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest001, TestSize.Level0) +{ + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, nullptr); + EXPECT_NE(ret, HCF_SUCCESS); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest002, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + ASSERT_EQ(ret, HCF_SUCCESS); + EXPECT_NE(macObj, nullptr); + HcfObjDestroy(macObj); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest003, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES256"; + + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + EXPECT_EQ(ret, HCF_SUCCESS); + EXPECT_NE(macObj, nullptr); + HcfObjDestroy(macObj); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest004, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfResult ret = HcfMacCreate(nullptr, &macObj); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); + EXPECT_EQ(macObj, nullptr); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest005, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + ASSERT_EQ(ret, HCF_SUCCESS); + EXPECT_NE(macObj, nullptr); + // test api functions + const char *cipherName = macObj->getAlgoName(macObj); + int32_t cmpRes = strcmp(cipherName, "AES128"); + EXPECT_EQ(cmpRes, HCF_SUCCESS); + HcfObjDestroy(macObj); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest006, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + ASSERT_EQ(ret, HCF_SUCCESS); + // set a nullptr key + HcfSymKey *key = nullptr; + // test api functions + ret = macObj->init(macObj, key); + EXPECT_NE(ret, HCF_SUCCESS); + HcfObjDestroy(macObj); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest007, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + ASSERT_EQ(ret, HCF_SUCCESS); + // create a symKey generator + HcfSymKeyGenerator *generator = nullptr; + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + // get sym key from preset keyBlob + uint8_t testKey[] = "abcdefghijklmnop"; + uint32_t testKeyLen = 16; + HcfSymKey *key = nullptr; + HcfBlob keyMaterialBlob = {.data = reinterpret_cast(testKey), .len = testKeyLen}; + generator->convertSymKey(generator, &keyMaterialBlob, &key); + // test api functions + ret = macObj->init(macObj, key); + EXPECT_EQ(ret, HCF_SUCCESS); + HcfObjDestroy(macObj); + HcfObjDestroy(key); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest008, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + ASSERT_EQ(ret, HCF_SUCCESS); + // set input and output buf + uint8_t testData[] = "My test data"; + // define input and output data in blob form + HcfBlob inBlob = {.data = reinterpret_cast(testData), .len = sizeof(testData)}; + // test api functions + ret = macObj->update(macObj, &inBlob); + EXPECT_NE(ret, HCF_SUCCESS); + HcfObjDestroy(macObj); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest009, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + ASSERT_EQ(ret, HCF_SUCCESS); + // cteate key generator and set key text + HcfSymKeyGenerator *generator = nullptr; + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + // get sym key from preset keyBlob + uint8_t testKey[] = "abcdefghijklmnop"; + uint32_t testKeyLen = 16; + HcfSymKey *key = nullptr; + HcfBlob keyMaterialBlob = {.data = reinterpret_cast(testKey), .len = testKeyLen}; + generator->convertSymKey(generator, &keyMaterialBlob, &key); + // define input and output data in blob form + HcfBlob *inBlob = nullptr; + // test api functions + ret = macObj->init(macObj, key); + EXPECT_EQ(ret, HCF_SUCCESS); + ret = macObj->update(macObj, inBlob); + EXPECT_NE(ret, HCF_SUCCESS); + HcfObjDestroy(macObj); + HcfObjDestroy(key); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest010, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + ASSERT_EQ(ret, HCF_SUCCESS); + // cteate key generator and set key text + HcfSymKeyGenerator *generator = nullptr; + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + // get sym key from preset keyBlob + uint8_t testKey[] = "abcdefghijklmnop"; + uint32_t testKeyLen = 16; + HcfSymKey *key = nullptr; + HcfBlob keyMaterialBlob = {.data = reinterpret_cast(testKey), .len = testKeyLen}; + generator->convertSymKey(generator, &keyMaterialBlob, &key); + // set input and output buf + uint8_t testData[] = "My test data"; + // define input and output data in blob form + HcfBlob inBlob = {.data = reinterpret_cast(testData), .len = sizeof(testData)}; + // test api functions + ret = macObj->init(macObj, key); + EXPECT_EQ(ret, HCF_SUCCESS); + ret = macObj->update(macObj, &inBlob); + EXPECT_EQ(ret, HCF_SUCCESS); + HcfObjDestroy(macObj); + HcfObjDestroy(key); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest012, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + ASSERT_EQ(ret, HCF_SUCCESS); + // cteate key generator and set key text + HcfSymKeyGenerator *generator = nullptr; + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + // get sym key from preset keyBlob + uint8_t testKey[] = "abcdefghijklmnop"; + uint32_t testKeyLen = 16; + HcfSymKey *key = nullptr; + HcfBlob keyMaterialBlob = {.data = reinterpret_cast(testKey), .len = testKeyLen}; + generator->convertSymKey(generator, &keyMaterialBlob, &key); + printf("get symkey finish"); + // set input and output buf + HcfBlob outBlob = { .data = nullptr, .len = 0 }; + // test api functions + ret = macObj->init(macObj, key); + EXPECT_EQ(ret, HCF_SUCCESS); + printf("test init finish"); + ret = macObj->doFinal(macObj, &outBlob); + EXPECT_EQ(ret, HCF_SUCCESS); + printf("test dofinal finish"); + PrintfBlobInHex(outBlob.data, outBlob.len); + // destroy the API obj and blob data + HcfBlobDataClearAndFree(&outBlob); + printf("HcfBlobDataClearAndFree finish"); + HcfObjDestroy(macObj); + printf("HcfObjDestroy macObj finish"); + HcfObjDestroy(key); + printf("HcfObjDestroy key finish"); + HcfObjDestroy(generator); + printf("test finish"); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest013, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + ASSERT_EQ(ret, HCF_SUCCESS); + // cteate key generator + HcfSymKeyGenerator *generator = nullptr; + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + // get sym key from preset keyBlob + uint8_t testKey[] = "abcdefghijklmnop"; + uint32_t testKeyLen = 16; + HcfSymKey *key = nullptr; + HcfBlob keyMaterialBlob = {.data = reinterpret_cast(testKey), .len = testKeyLen}; + generator->convertSymKey(generator, &keyMaterialBlob, &key); + // set input and output buf + uint8_t testData[] = "My test data"; + // define input and output data in blob form + HcfBlob inBlob = {.data = reinterpret_cast(testData), .len = sizeof(testData)}; + HcfBlob outBlob = { .data = nullptr, .len = 0 }; + // test api functions + 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); + // destroy the API obj and blob data + HcfBlobDataClearAndFree(&outBlob); + HcfObjDestroy(macObj); + HcfObjDestroy(key); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest014, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + ASSERT_EQ(ret, HCF_SUCCESS); + // cteate key generator + HcfSymKeyGenerator *generator = nullptr; + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + // get sym key from preset keyBlob + uint8_t testKey[] = "abcdefghijklmnop"; + uint32_t testKeyLen = 16; + HcfSymKey *key = nullptr; + HcfBlob keyMaterialBlob = {.data = reinterpret_cast(testKey), .len = testKeyLen}; + generator->convertSymKey(generator, &keyMaterialBlob, &key); + // define input and output data in blob form + HcfBlob inBlob = {0}; + inBlob.data = reinterpret_cast(g_testBigData); + inBlob.len = strnlen(g_testBigData, MAX_MAC_BLOB_LEN); + HcfBlob outBlob = { .data = nullptr, .len = 0 }; + // test api functions + 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); + PrintfBlobInHex(outBlob.data, outBlob.len); + // destroy the API obj and blob data + HcfBlobDataClearAndFree(&outBlob); + HcfObjDestroy(macObj); + HcfObjDestroy(key); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest016, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + ASSERT_EQ(ret, HCF_SUCCESS); + // cteate key generator + HcfSymKeyGenerator *generator = nullptr; + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + // get sym key from preset keyBlob + uint8_t testKey[] = "abcdefghijklmnop"; + uint32_t testKeyLen = 16; + HcfSymKey *key = nullptr; + HcfBlob keyMaterialBlob = {.data = reinterpret_cast(testKey), .len = testKeyLen}; + generator->convertSymKey(generator, &keyMaterialBlob, &key); + // test api functions + ret = macObj->init(macObj, key); + EXPECT_EQ(ret, HCF_SUCCESS); + uint32_t len = macObj->getMacLength(macObj); + EXPECT_EQ(len, AES128_LEN); + HcfObjDestroy(macObj); + HcfObjDestroy(key); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest017, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + EXPECT_EQ(ret, HCF_SUCCESS); + // create a symKey generator + HcfSymKeyGenerator *generator = nullptr; + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + EXPECT_EQ(ret, HCF_SUCCESS); + // set key data and convert it to key obj + uint8_t testKey[] = "abcdefghijklmnop"; + uint32_t testKeyLen = 16; + HcfSymKey *key = nullptr; + HcfBlob keyMaterialBlob = {.data = reinterpret_cast(testKey), .len = testKeyLen}; + generator->convertSymKey(generator, &keyMaterialBlob, &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, AES128_LEN); + // destroy the API obj and blob data + HcfBlobDataClearAndFree(&outBlob); + HcfObjDestroy(macObj); + HcfObjDestroy(key); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest018, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + EXPECT_EQ(ret, HCF_SUCCESS); + // create a symKey generator + HcfSymKeyGenerator *generator = nullptr; + ret = HcfSymKeyGeneratorCreate("AES128", &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, AES128_LEN); + // destroy the API obj and blob data + HcfBlobDataClearAndFree(&outBlob); + HcfObjDestroy(macObj); + HcfObjDestroy(key); + HcfObjDestroy(generator); +} + +static const char *GetInvalidMacClass(void) +{ + return "INVALID_MAC_CLASS"; +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest019, TestSize.Level0) +{ + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "SHA256"; + HcfResult ret = OpensslHmacSpiCreate((HcfMacParamsSpec *)¶ms, nullptr); + EXPECT_NE(ret, HCF_SUCCESS); + HcfMacSpi *spiObj = nullptr; + ret = OpensslHmacSpiCreate(nullptr, &spiObj); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest021, TestSize.Level0) +{ + HcfMac *macObj = nullptr; + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + ASSERT_EQ(ret, HCF_SUCCESS); + HcfMac invalidMacObj = {{0}}; + invalidMacObj.base.getClass = GetInvalidMacClass; + HcfSymKeyGenerator *generator = nullptr; + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + uint8_t testKey[] = "abcdefghijklmnop"; + uint32_t testKeyLen = sizeof(testKey) / sizeof(testKey[0]); + HcfSymKey *key = nullptr; + HcfBlob keyMaterialBlob = {.data = reinterpret_cast(testKey), .len = testKeyLen}; + generator->convertSymKey(generator, &keyMaterialBlob, &key); + uint8_t testData[] = "My test data"; + HcfBlob inBlob = {.data = reinterpret_cast(testData), .len = sizeof(testData)}; + HcfBlob outBlob = { .data = nullptr, .len = 0 }; + ret = macObj->init(&invalidMacObj, key); + EXPECT_NE(ret, HCF_SUCCESS); + ret = macObj->update(&invalidMacObj, &inBlob); + EXPECT_NE(ret, HCF_SUCCESS); + ret = macObj->doFinal(&invalidMacObj, &outBlob); + EXPECT_NE(ret, HCF_SUCCESS); + uint32_t len = macObj->getMacLength(&invalidMacObj); + EXPECT_EQ(len, HCF_OPENSSL_INVALID_MAC_LEN); + const char *algoName = macObj->getAlgoName(&invalidMacObj); + EXPECT_EQ(algoName, nullptr); + HcfBlobDataClearAndFree(&outBlob); + macObj->base.destroy(&(invalidMacObj.base)); + HcfObjDestroy(macObj); + HcfObjDestroy(key); + HcfObjDestroy(generator); +} + +HWTEST_F(CryptoCmacTest, CryptoCmacTest022, TestSize.Level0) +{ + HcfMacSpi *spiObj = nullptr; + HcfMacSpi invalidSpi = {{0}}; + invalidSpi.base.getClass = GetInvalidMacClass; + // create a symKey generator + HcfSymKeyGenerator *generator = nullptr; + HcfResult ret = HcfSymKeyGeneratorCreate("AES128", &generator); + ASSERT_EQ(ret, HCF_SUCCESS); + // set key data and convert it to key obj + uint8_t testKey[] = "abcdefghijklmnop"; + uint32_t testKeyLen = 16; + HcfSymKey *key = nullptr; + HcfBlob keyMaterialBlob = {.data = reinterpret_cast(testKey), .len = testKeyLen}; + generator->convertSymKey(generator, &keyMaterialBlob, &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 }; + + HcfCmacParamsSpec params = {}; + params.base.algName = "CMAC"; + params.cipherName = "AES128"; + ret = OpensslHmacSpiCreate((HcfMacParamsSpec *)¶ms, &spiObj); + EXPECT_EQ(ret, HCF_SUCCESS); + ASSERT_NE(spiObj, nullptr); + (void)spiObj->base.destroy(nullptr); + (void)spiObj->base.destroy(&(invalidSpi.base)); + ret = spiObj->engineInitMac(&invalidSpi, key); + EXPECT_NE(ret, HCF_SUCCESS); + ret = spiObj->engineUpdateMac(&invalidSpi, &inBlob); + EXPECT_NE(ret, HCF_SUCCESS); + ret = spiObj->engineDoFinalMac(&invalidSpi, &outBlob); + EXPECT_NE(ret, HCF_SUCCESS); + uint32_t len = spiObj->engineGetMacLength(&invalidSpi); + EXPECT_EQ(len, INVALID_LEN); + HcfObjDestroy(spiObj); + HcfObjDestroy(key); + HcfObjDestroy(generator); +} + +} \ No newline at end of file diff --git a/test/unittest/src/crypto_mac_test.cpp b/test/unittest/src/crypto_mac_test.cpp index e9b961d0d19bff72a5ea8a22672924d60ae1efcf..0f6c439c776d1e213f51d6c396cc66527c4ebfa1 100644 --- a/test/unittest/src/crypto_mac_test.cpp +++ b/test/unittest/src/crypto_mac_test.cpp @@ -20,6 +20,8 @@ #include "mac.h" #include "sym_key_generator.h" #include "mac_openssl.h" +#include "mac_params.h" +#include "detailed_hmac_params.h" #include "log.h" #include "memory.h" @@ -80,7 +82,11 @@ static void PrintfBlobInHex(uint8_t *data, size_t dataLen) HWTEST_F(CryptoMacTest, CryptoFrameworkHmacCreateTest002, TestSize.Level0) { - HcfResult ret = HcfMacCreate("SHA1", nullptr); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, nullptr); EXPECT_NE(ret, HCF_SUCCESS); } @@ -88,7 +94,11 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoSuppTest001, TestSize.Level0) { // create a SHA obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); EXPECT_NE(macObj, nullptr); HcfObjDestroy(macObj); @@ -98,7 +108,11 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoSuppTest002, TestSize.Level0) { // create a SHA obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA3"; + + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); EXPECT_NE(ret, HCF_SUCCESS); EXPECT_EQ(macObj, nullptr); } @@ -108,7 +122,7 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoSuppTest003, TestSize.Level0) // create a SHA obj HcfMac *macObj = nullptr; HcfResult ret = HcfMacCreate(nullptr, &macObj); - EXPECT_NE(ret, HCF_SUCCESS); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); EXPECT_EQ(macObj, nullptr); } @@ -116,11 +130,15 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoNameTest001, TestSize.Level0) { // create a SHA obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); EXPECT_NE(macObj, nullptr); // test api functions - const char *algoName = macObj->getAlgoName(macObj); + const char *algoName = macObj->getAlgoName(macObj); int32_t cmpRes = strcmp(algoName, "SHA1"); EXPECT_EQ(cmpRes, HCF_SUCCESS); HcfObjDestroy(macObj); @@ -130,7 +148,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacInitTest001, TestSize.Level0) { // create a SHA obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // set a nullptr key HcfSymKey *key = nullptr; @@ -144,7 +165,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacInitTest002, TestSize.Level0) { // create a SHA obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -168,7 +192,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacUpdateTest001, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // set input and output buf uint8_t testData[] = "My test data"; @@ -184,7 +211,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacUpdateTest002, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // cteate key generator and set key text HcfSymKeyGenerator *generator = nullptr; @@ -212,7 +242,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacUpdateTest003, TestSize.Level0) { // create a API obj with SHA1 HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // cteate key generator and set key text HcfSymKeyGenerator *generator = nullptr; @@ -242,7 +275,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacDoFinalTest001, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // set input and output buf HcfBlob outBlob = { .data = nullptr, .len = 0 }; @@ -257,7 +293,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacDoFinalTest002, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // cteate key generator and set key text HcfSymKeyGenerator *generator = nullptr; @@ -295,7 +334,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacDoFinalTest003, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // cteate key generator HcfSymKeyGenerator *generator = nullptr; @@ -330,7 +372,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacDoFinalTest004, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA256", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA256"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // cteate key generator HcfSymKeyGenerator *generator = nullptr; @@ -366,7 +411,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacLenTest001, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // test api functions uint32_t len = macObj->getMacLength(macObj); @@ -378,7 +426,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacLenTest002, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // cteate key generator HcfSymKeyGenerator *generator = nullptr; @@ -404,7 +455,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest001, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -440,7 +494,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest002, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA224", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA224"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -476,7 +533,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest003, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA256", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA256"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -512,7 +572,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest004, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA384", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA384"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -548,7 +611,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest005, TestSize.Level0) { // create a SHA1 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA512", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA512"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -583,7 +649,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest005, TestSize.Level0) HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest006, TestSize.Level0) { HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA256", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA256"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); EXPECT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -618,7 +687,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest006, TestSize.Level0) HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest007, TestSize.Level0) { HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA1", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA1"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); EXPECT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -649,7 +721,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest007, TestSize.Level0) HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest008, TestSize.Level0) { HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA224", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA224"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); EXPECT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -680,7 +755,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest008, TestSize.Level0) HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest009, TestSize.Level0) { HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA256", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA256"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); EXPECT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -711,7 +789,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest009, TestSize.Level0) HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest010, TestSize.Level0) { HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA384", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA384"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); EXPECT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -742,7 +823,10 @@ HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest010, TestSize.Level0) HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest011, TestSize.Level0) { HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA512", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA512"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); EXPECT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -814,17 +898,23 @@ static const char *GetInvalidMacClass(void) HWTEST_F(CryptoMacTest, InvalidInputMacTest001, TestSize.Level0) { - HcfResult ret = OpensslMacSpiCreate("SHA256", nullptr); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA256"; + HcfResult ret = OpensslHmacSpiCreate((HcfMacParamsSpec *)¶ms, nullptr); EXPECT_NE(ret, HCF_SUCCESS); HcfMacSpi *spiObj = nullptr; - ret = OpensslMacSpiCreate(nullptr, &spiObj); - EXPECT_NE(ret, HCF_SUCCESS); + ret = OpensslHmacSpiCreate(nullptr, &spiObj); + EXPECT_EQ(ret, HCF_INVALID_PARAMS); } HWTEST_F(CryptoMacTest, NullParamMacTest001, TestSize.Level0) { HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA256", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA256"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); ret = macObj->init(nullptr, nullptr); EXPECT_NE(ret, HCF_SUCCESS); @@ -843,7 +933,10 @@ HWTEST_F(CryptoMacTest, NullParamMacTest001, TestSize.Level0) HWTEST_F(CryptoMacTest, InvalidFrameworkClassMacTest001, TestSize.Level0) { HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA256", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA256"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); HcfMac invalidMacObj = {{0}}; invalidMacObj.base.getClass = GetInvalidMacClass; @@ -894,7 +987,11 @@ HWTEST_F(CryptoMacTest, InvalidSpiClassMacTest001, TestSize.Level0) uint8_t testData[] = "My test data"; HcfBlob inBlob = {.data = reinterpret_cast(testData), .len = sizeof(testData)}; HcfBlob outBlob = { .data = nullptr, .len = 0 }; - ret = OpensslMacSpiCreate("SHA256", &spiObj); + + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SHA256"; + ret = OpensslHmacSpiCreate((HcfMacParamsSpec *)¶ms, &spiObj); EXPECT_EQ(ret, HCF_SUCCESS); ASSERT_NE(spiObj, nullptr); (void)spiObj->base.destroy(nullptr); @@ -915,7 +1012,10 @@ HWTEST_F(CryptoMacTest, InvalidSpiClassMacTest001, TestSize.Level0) HWTEST_F(CryptoMacTest, CryptoFrameworkHmacAlgoTest012, TestSize.Level0) { HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("MD5", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "MD5"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); EXPECT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; diff --git a/test/unittest/src/crypto_md_sm3_test.cpp b/test/unittest/src/crypto_md_sm3_test.cpp index 791ee35d5cb7d2084c96dd6ff44cfc6e56bc920e..3fd324362a1c874e8cd143378f4501c8e7f59c21 100644 --- a/test/unittest/src/crypto_md_sm3_test.cpp +++ b/test/unittest/src/crypto_md_sm3_test.cpp @@ -108,7 +108,6 @@ HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3UpdateTest002, TestSize.Level0) HcfObjDestroy(mdObj); } - HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3DoFinalTest001, TestSize.Level0) { // create a SM3 obj @@ -195,4 +194,4 @@ HWTEST_F(CryptoMdSM3Test, CryptoFrameworkMdSM3AlgoTest004, TestSize.Level0) HcfResult ret = OpensslMdSpiCreate("SM2", &spiObj); EXPECT_NE(ret, HCF_SUCCESS); } -} \ No newline at end of file +} diff --git a/test/unittest/src/crypto_sm3_mac_test.cpp b/test/unittest/src/crypto_sm3_mac_test.cpp index eb92c830d21a419fb71f15aacf1dcda85127b642..522be4349cedae31c44d5c5fdcdcdac8f78c3712 100644 --- a/test/unittest/src/crypto_sm3_mac_test.cpp +++ b/test/unittest/src/crypto_sm3_mac_test.cpp @@ -20,6 +20,8 @@ #include "mac.h" #include "sym_key_generator.h" #include "mac_openssl.h" +#include "mac_params.h" +#include "detailed_hmac_params.h" #include "log.h" #include "memory.h" @@ -75,7 +77,10 @@ static void PrintfBlobInHex(uint8_t *data, size_t dataLen) HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3CreateTest001, TestSize.Level0) { - HcfResult ret = HcfMacCreate("SM3", nullptr); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, nullptr); EXPECT_NE(ret, HCF_SUCCESS); } @@ -83,7 +88,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoSuppTest001, TestSize.Level { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); EXPECT_NE(macObj, nullptr); HcfObjDestroy(macObj); @@ -93,9 +101,13 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoSuppTest002, TestSize.Level { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SHA3", &macObj); - EXPECT_NE(ret, HCF_SUCCESS); - EXPECT_EQ(macObj, nullptr); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); + EXPECT_EQ(ret, HCF_SUCCESS); + EXPECT_NE(macObj, nullptr); + HcfObjDestroy(macObj); } HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoSuppTest003, TestSize.Level0) @@ -111,7 +123,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoNameTest001, TestSize.Level { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); EXPECT_NE(macObj, nullptr); // test api functions @@ -125,7 +140,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3InitTest001, TestSize.Level0) { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // set a nullptr key HcfSymKey *key = nullptr; @@ -139,7 +157,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3InitTest002, TestSize.Level0) { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -163,7 +184,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3UpdateTest001, TestSize.Level0) { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // set input and output buf uint8_t testData[] = "My test data"; @@ -179,7 +203,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3UpdateTest002, TestSize.Level0) { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // cteate key generator and set key text HcfSymKeyGenerator *generator = nullptr; @@ -207,7 +234,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3UpdateTest003, TestSize.Level0) { // create a API obj with SM3 HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // cteate key generator and set key text HcfSymKeyGenerator *generator = nullptr; @@ -237,7 +267,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3DoFinalTest001, TestSize.Level0 { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // set input and output buf HcfBlob outBlob = { .data = nullptr, .len = 0 }; @@ -252,7 +285,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3DoFinalTest002, TestSize.Level0 { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // cteate key generator and set key text HcfSymKeyGenerator *generator = nullptr; @@ -290,7 +326,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3DoFinalTest003, TestSize.Level0 { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // cteate key generator HcfSymKeyGenerator *generator = nullptr; @@ -325,7 +364,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3DoFinalTest004, TestSize.Level0 { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // cteate key generator HcfSymKeyGenerator *generator = nullptr; @@ -361,7 +403,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3LenTest001, TestSize.Level0) { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // test api functions uint32_t len = macObj->getMacLength(macObj); @@ -373,7 +418,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3LenTest002, TestSize.Level0) { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // cteate key generator HcfSymKeyGenerator *generator = nullptr; @@ -399,7 +447,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoTest001, TestSize.Level0) { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -435,7 +486,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoTest002, TestSize.Level0) { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -468,7 +522,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoTest003, TestSize.Level0) { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -501,7 +558,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoTest004, TestSize.Level0) { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -534,7 +594,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoTest005, TestSize.Level0) { // create a SM3 obj HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -566,7 +629,10 @@ HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoTest005, TestSize.Level0) HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoTest006, TestSize.Level0) { HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); // create a symKey generator HcfSymKeyGenerator *generator = nullptr; @@ -602,17 +668,23 @@ static const char *GetInvalidMacSm3Class(void) HWTEST_F(CryptoSM3MacTest, InvalidInputMacSM3Test001, TestSize.Level0) { - HcfResult ret = OpensslMacSpiCreate("SM3", nullptr); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = OpensslHmacSpiCreate((HcfMacParamsSpec *)¶ms, nullptr); EXPECT_NE(ret, HCF_SUCCESS); HcfMacSpi *spiObj = nullptr; - ret = OpensslMacSpiCreate(nullptr, &spiObj); + ret = OpensslHmacSpiCreate(nullptr, &spiObj); EXPECT_NE(ret, HCF_SUCCESS); } HWTEST_F(CryptoSM3MacTest, NullParamMacSM3Test001, TestSize.Level0) { HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); ret = macObj->init(nullptr, nullptr); EXPECT_NE(ret, HCF_SUCCESS); @@ -631,7 +703,10 @@ HWTEST_F(CryptoSM3MacTest, NullParamMacSM3Test001, TestSize.Level0) HWTEST_F(CryptoSM3MacTest, InvalidFrameworkClassMacSM3Test001, TestSize.Level0) { HcfMac *macObj = nullptr; - HcfResult ret = HcfMacCreate("SM3", &macObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + HcfResult ret = HcfMacCreate((HcfMacParamsSpec *)¶ms, &macObj); ASSERT_EQ(ret, HCF_SUCCESS); HcfMac invalidMacObj = {{0}}; invalidMacObj.base.getClass = GetInvalidMacSm3Class; @@ -682,7 +757,10 @@ HWTEST_F(CryptoSM3MacTest, InvalidSpiClassMacSM3Test001, TestSize.Level0) uint8_t testData[] = "My test data"; HcfBlob inBlob = {.data = reinterpret_cast(testData), .len = sizeof(testData)}; HcfBlob outBlob = { .data = nullptr, .len = 0 }; - ret = OpensslMacSpiCreate("SM3", &spiObj); + HcfHmacParamsSpec params = {}; + params.base.algName = "HMAC"; + params.mdName = "SM3"; + ret = OpensslHmacSpiCreate((HcfMacParamsSpec *)¶ms, &spiObj); EXPECT_EQ(ret, HCF_SUCCESS); ASSERT_NE(spiObj, nullptr); (void)spiObj->base.destroy(nullptr); @@ -699,4 +777,4 @@ HWTEST_F(CryptoSM3MacTest, InvalidSpiClassMacSM3Test001, TestSize.Level0) HcfObjDestroy(key); HcfObjDestroy(generator); } -} \ No newline at end of file +}