diff --git a/BUILD.gn b/BUILD.gn index c7ff8f3d16aad3249e7d61d87f1ef7d0b0438d8d..d8af064088fb7a142e104ec8dd2ae124b3dd94e6 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -24,6 +24,12 @@ group("crypto_framework_component") { "frameworks/native:ohcrypto", "plugin:crypto_openssl_plugin_lib", ] + } else if (os_level == "mini") { + deps = [ + "frameworks:crypto_framework_lib", + "frameworks/js/jsi:cryptoframework_jsi", + "plugin:crypto_mbedtls_plugin_lib", + ] } } diff --git a/README_zh.md b/README_zh.md index 7cad669c1c0cf04775ae0c473812160b9adc5126..37a292dbcdd19de5e643215fc692e9912482e7d7 100644 --- a/README_zh.md +++ b/README_zh.md @@ -21,14 +21,17 @@ base/security/crypto_framwork ├── test # unitest ├── common # 内部依赖的公共方法 ├── plugin # 算法适配的插件实现 +│ ├── mbedtls_plugin # mbedtls 插件 │ └── openssl_plugin # openssl 插件 ├── frameworks # 框架实现层 │ ├── spi # SPI的接口 │ ├── js +│ ├── jsi # 通过jsi封装的JS接口代码实现 │ └── napi # 通过napi封装的JS接口代码实现 │ ├── algorithm_parameter # 算法参数 │ ├── crypto_operation # 算法操作,包括mac、md、加解密、签名验签、秘钥协商 │ ├── key # 秘钥材料 +│ ├── native # 对外提供算法库C接口 │ └── rand # 随机数 ``` diff --git a/bundle.json b/bundle.json index 3fe7bf3939af95563abae350df41d4fbbb1101e5..e0f37ed55fd9cc2b94d2636a0f6d14cdb653454a 100644 --- a/bundle.json +++ b/bundle.json @@ -29,7 +29,8 @@ ], "features": [ "crypto_framework_enabled" ], "adapted_system_type": [ - "standard" + "standard", + "mini" ], "rom": "2048KB", "ram": "", diff --git a/common/BUILD.gn b/common/BUILD.gn index 965bd6cdded8e7b655777364b4f3833ad213267e..98419d94ded2e09e7d221b19a88683a9c3aeb0c1 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -14,31 +14,46 @@ import("//base/security/crypto_framework/common/common.gni") import("//build/ohos.gni") -ohos_static_library("crypto_plugin_common") { - branch_protector_ret = "pac_ret" - subsystem_name = "security" - part_name = "crypto_framework" - include_dirs = crypto_framwork_common_inc_path - - sources = crypto_framwork_common_files - - if (os_level == "standard") { - sanitize = { - cfi = true - cfi_cross_dso = true - debug = false +if (os_level == "standard") { + ohos_static_library("crypto_plugin_common") { + branch_protector_ret = "pac_ret" + subsystem_name = "security" + part_name = "crypto_framework" + include_dirs = crypto_framwork_common_inc_path + + sources = crypto_framwork_common_files + + if (os_level == "standard") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } } + defines = [ "HILOG_ENABLE" ] + cflags = [ + "-DHILOG_ENABLE", + "-fPIC", + "-Wall", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] } +} else if (os_level == "mini") { + ohos_static_library("crypto_common_lite") { + subsystem_name = "security" + part_name = "crypto_framework" + include_dirs = crypto_framwork_common_inc_path + include_dirs += + [ "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite" ] - defines = [ "HILOG_ENABLE" ] - cflags = [ - "-DHILOG_ENABLE", - "-fPIC", - "-Wall", - ] - - external_deps = [ - "c_utils:utils", - "hilog:libhilog", - ] + sources = crypto_framwork_common_files_lite + + defines = [ "MINI_HILOG_ENABLE" ] + + configs = [ "${product_path}:product_public_configs" ] + } } diff --git a/common/common.gni b/common/common.gni index 5507276b51ab50a285b2a36ca91db3babe3e360d..b6c1865b30e679d88f3e79b03a32ed876bf5ea73 100644 --- a/common/common.gni +++ b/common/common.gni @@ -30,3 +30,11 @@ framework_common_util_files = [ ] crypto_framwork_common_files = framework_common_util_files + +crypto_framwork_common_files_lite = [ + "//base/security/crypto_framework/common/src/blob.c", + "//base/security/crypto_framework/common/src/utils.c", + "//base/security/crypto_framework/common/src/log.c", + "//base/security/crypto_framework/common/src/memory.c", + "//base/security/crypto_framework/common/src/object_base.c", +] diff --git a/common/inc/log.h b/common/inc/log.h index 0a18450c811b3a843642860575c280f3da09a312..ded862155449711d8d984ad87c33d3f60b32c27c 100644 --- a/common/inc/log.h +++ b/common/inc/log.h @@ -19,7 +19,16 @@ #include #include -#ifdef HILOG_ENABLE +#if defined(MINI_HILOG_ENABLE) + +#include "hiview_log.h" + +#define LOGD(fmt, ...) HILOG_DEBUG(HILOG_MODULE_SCY, fmt, ##__VA_ARGS__) +#define LOGI(fmt, ...) HILOG_INFO(HILOG_MODULE_SCY, fmt, ##__VA_ARGS__) +#define LOGW(fmt, ...) HILOG_WARN(HILOG_MODULE_SCY, fmt, ##__VA_ARGS__) +#define LOGE(fmt, ...) HILOG_ERROR(HILOG_MODULE_SCY, fmt, ##__VA_ARGS__) + +#elif defined(HILOG_ENABLE) enum HcfLogLevel { HCF_LOG_LEVEL_I, diff --git a/figures/zh-cn_crypto_framework_architecture.png b/figures/zh-cn_crypto_framework_architecture.png index a35cc52b27ea1b2dfd68eb762a1d0dd102bdd68d..7c73e2ebd609c95d76c3f075b31ff45062190de6 100755 Binary files a/figures/zh-cn_crypto_framework_architecture.png and b/figures/zh-cn_crypto_framework_architecture.png differ diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index c8614512d8fdbac8f3573c011b2c456ba73cfc18..9d864090615f1b31e6914b37e66e2406b91b7b84 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -24,37 +24,67 @@ config("framework_config") { ] } -ohos_shared_library("crypto_framework_lib") { - branch_protector_ret = "pac_ret" - subsystem_name = "security" - innerapi_tags = [ "platformsdk" ] - part_name = "crypto_framework" - public_configs = [ ":framework_config" ] - include_dirs = framework_inc_path + crypto_framwork_common_inc_path - - sources = framework_files - - if (os_level == "standard") { - sanitize = { - cfi = true - cfi_cross_dso = true - debug = false +if (os_level == "standard") { + ohos_shared_library("crypto_framework_lib") { + branch_protector_ret = "pac_ret" + subsystem_name = "security" + innerapi_tags = [ "platformsdk" ] + part_name = "crypto_framework" + public_configs = [ ":framework_config" ] + include_dirs = framework_inc_path + crypto_framwork_common_inc_path + + sources = framework_files + + if (os_level == "standard") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } } + + cflags = [ + "-DHILOG_ENABLE", + "-fPIC", + "-Wall", + ] + + deps = [ + "../common:crypto_plugin_common", + "../plugin:crypto_openssl_plugin_lib", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] } +} else if (os_level == "mini") { + ohos_static_library("crypto_framework_lib") { + subsystem_name = "security" + part_name = "crypto_framework" + public_configs = [ ":framework_config" ] + include_dirs = framework_inc_lite_path + crypto_framwork_common_inc_path + include_dirs += + [ "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite" ] - cflags = [ - "-DHILOG_ENABLE", - "-fPIC", - "-Wall", - ] + sources = framework_lite_files - deps = [ - "../common:crypto_plugin_common", - "../plugin:crypto_openssl_plugin_lib", - ] + defines = [ + "CRYPTO_MBEDTLS", + "MINI_HILOG_ENABLE", + ] - external_deps = [ - "c_utils:utils", - "hilog:libhilog", - ] + deps = [ + "../common:crypto_common_lite", + "../plugin:crypto_mbedtls_plugin_lib", + ] + + configs = [ "${product_path}:product_public_configs" ] + + cflags = [ + "--diag_suppress", + "Pe188,Pe186", + ] + } } diff --git a/frameworks/crypto_operation/md.c b/frameworks/crypto_operation/md.c index 0a921fd8395acd803c1352e3a6212850b074542e..888f3bebaf0eef31ab88b9de75068b8eb21c790f 100644 --- a/frameworks/crypto_operation/md.c +++ b/frameworks/crypto_operation/md.c @@ -19,7 +19,11 @@ #include "sym_key.h" #include "md_spi.h" +#ifdef CRYPTO_MBEDTLS +#include "mbedtls_md.h" +#else #include "md_openssl.h" +#endif #include "log.h" #include "config.h" @@ -43,6 +47,12 @@ typedef struct { } HcfMdAbility; static const HcfMdAbility MD_ABILITY_SET[] = { +#ifdef CRYPTO_MBEDTLS + { "SHA1", MbedtlsMdSpiCreate }, + { "SHA256", MbedtlsMdSpiCreate }, + { "SHA512", MbedtlsMdSpiCreate }, + { "MD5", MbedtlsMdSpiCreate }, +#else { "SHA1", OpensslMdSpiCreate }, { "SHA224", OpensslMdSpiCreate }, { "SHA256", OpensslMdSpiCreate }, @@ -50,6 +60,7 @@ static const HcfMdAbility MD_ABILITY_SET[] = { { "SHA512", OpensslMdSpiCreate }, { "MD5", OpensslMdSpiCreate }, { "SM3", OpensslMdSpiCreate }, +#endif }; static const char *GetMdClass(void) diff --git a/frameworks/crypto_operation/rand.c b/frameworks/crypto_operation/rand.c index 99018add125280c94ea72148656f3d801a3e2811..0f4227d8fa857204ce3396263d2d8a6ec4dec8c2 100644 --- a/frameworks/crypto_operation/rand.c +++ b/frameworks/crypto_operation/rand.c @@ -18,7 +18,11 @@ #include #include #include "rand_spi.h" +#ifdef CRYPTO_MBEDTLS +#include "mbedtls_rand.h" +#else #include "rand_openssl.h" +#endif #include "log.h" #include "config.h" #include "memory.h" @@ -40,15 +44,19 @@ typedef struct { HcfRandSpiCreateFunc createSpiFunc; } HcfRandAbility; -static const HcfRandAbility RAND_ABILITY_SET[] = { - { "OpensslRand", HcfRandSpiCreate } -}; - static const char *GetRandClass(void) { return "Rand"; } +static const HcfRandAbility RAND_ABILITY_SET[] = { +#ifdef CRYPTO_MBEDTLS + { "MbedtlsRand", MbedtlsRandSpiCreate } +#else + { "OpensslRand", HcfRandSpiCreate } +#endif +}; + static HcfRandSpiCreateFunc FindAbility(const char *algoName) { for (uint32_t i = 0; i < (sizeof(RAND_ABILITY_SET) / sizeof(RAND_ABILITY_SET[0])); i++) { @@ -127,7 +135,11 @@ HcfResult HcfRandCreate(HcfRand **random) LOGE("Invalid input params while creating rand!"); return HCF_INVALID_PARAMS; } +#ifdef CRYPTO_MBEDTLS + HcfRandSpiCreateFunc createSpiFunc = FindAbility("MbedtlsRand"); +#else HcfRandSpiCreateFunc createSpiFunc = FindAbility("OpensslRand"); +#endif if (createSpiFunc == NULL) { LOGE("Algo not supported!"); return HCF_NOT_SUPPORT; diff --git a/frameworks/frameworks.gni b/frameworks/frameworks.gni index 623a379253235fdbd20ddcdcd699a8cd4b2e789b..957383a1ad10b002edc1b20ed1e668daa7490ea7 100644 --- a/frameworks/frameworks.gni +++ b/frameworks/frameworks.gni @@ -65,3 +65,17 @@ framework_files = framework_cipher_files + framework_key_files + framework_mac_files + framework_rand_files + framework_md_files + framework_kdf_files + framework_sm2_crypto_util_files + +framework_inc_lite_path = [ + "${base_path}/interfaces/innerkits/algorithm_parameter", + "${base_path}/interfaces/innerkits/common", + "${base_path}/interfaces/innerkits/crypto_operation", + "${base_path}/interfaces/innerkits/key", + "${base_path}/common/inc", + "${plugin_path}/mbedtls_plugin/common", + "${plugin_path}/mbedtls_plugin/md/inc", + "${plugin_path}/mbedtls_plugin/rand/inc", + "${framework_path}/spi", +] + +framework_lite_files = framework_rand_files + framework_md_files diff --git a/frameworks/js/jsi/BUILD.gn b/frameworks/js/jsi/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..6ac9a4ce9bf84ed4fa63482e2e6e0b8a94028aa2 --- /dev/null +++ b/frameworks/js/jsi/BUILD.gn @@ -0,0 +1,45 @@ +# 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. + +import("//base/security/crypto_framework/common/common.gni") +import("//base/security/crypto_framework/frameworks/frameworks.gni") +import("//build/lite/config/component/lite_component.gni") +import("//build/ohos.gni") + +ohos_static_library("cryptoframework_jsi") { + subsystem_name = "security" + part_name = "crypto_framework" + include_dirs = [ "inc" ] + include_dirs += framework_inc_path + include_dirs += [ + "../../../../../hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite", + ] + + sources = [ + "src/jsi_api.cpp", + "src/jsi_api_common.cpp", + "src/jsi_api_errcode.cpp", + "src/jsi_list.cpp", + "src/jsi_md.cpp", + "src/jsi_rand.cpp", + "src/jsi_utils.cpp", + ] + defines = [ "MINI_HILOG_ENABLE" ] + + deps = [ + "../../../common:crypto_common_lite", + "../../../frameworks:crypto_framework_lib", + ] + + configs = [ "${product_path}:product_public_configs" ] +} diff --git a/frameworks/js/jsi/inc/jsi_api.h b/frameworks/js/jsi/inc/jsi_api.h new file mode 100644 index 0000000000000000000000000000000000000000..723492524fde0ddf9bcb7828bc034479f64ad000 --- /dev/null +++ b/frameworks/js/jsi/inc/jsi_api.h @@ -0,0 +1,54 @@ +/* + * 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 JSI_API_H +#define JSI_API_H + +#include "jsi/jsi.h" +#include "jsi/jsi_types.h" + +namespace OHOS { +namespace ACELite { +class CryptoFrameworkLiteModule final : public MemoryHeap { +public: + CryptoFrameworkLiteModule() {} + ~CryptoFrameworkLiteModule() {}; + + static JSIValue CreateMd(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + static JSIValue CreateRandom(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + static void OnDestroy(void); + +private: + // Md + static JSIValue Update(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + static JSIValue UpdateSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + static JSIValue Digest(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + static JSIValue DigestSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + static JSIValue GetMdLength(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + + // Random + static JSIValue GenerateRandom(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + static JSIValue GenerateRandomSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + static JSIValue SetSeed(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + + static void MdDestroy(void); + static void RandomDestroy(void); +}; + +void InitCryptoFrameworkModule(JSIValue exports); + +} // namespace ACELite +} // namespace OHOS +#endif // JSI_API_H diff --git a/frameworks/js/jsi/inc/jsi_api_common.h b/frameworks/js/jsi/inc/jsi_api_common.h new file mode 100644 index 0000000000000000000000000000000000000000..e80dffb5e1845d371c33c539a45969be50a52379 --- /dev/null +++ b/frameworks/js/jsi/inc/jsi_api_common.h @@ -0,0 +1,43 @@ +/* + * 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 JSI_API_COMMON_H +#define JSI_API_COMMON_H + +#include "jsi.h" +#include "jsi/jsi_types.h" + +#include "md.h" +#include "rand.h" +#include "object_base.h" + +namespace OHOS { +namespace ACELite { + +typedef enum { + JSI_ALG_MD = 1, + JSI_ALG_RAND = 2, + JSI_ALG_MAX +} LiteAlgType; + +#define ARRAY_MAX_SIZE 2 +#define ARRAY_INDEX_ZERO 0 +#define ARRAY_INDEX_ONE 1 + +void JsiAsyncCallback(const JSIValue thisVal, JSIValue args, const JSIValue *params, uint8_t paramsNum); + +} // namespace ACELite +} // namespace OHOS +#endif // JSI_API_COMMON_H diff --git a/frameworks/js/jsi/inc/jsi_api_errcode.h b/frameworks/js/jsi/inc/jsi_api_errcode.h new file mode 100644 index 0000000000000000000000000000000000000000..6563a42968b6f1c429899e4850e209b3635d2e4a --- /dev/null +++ b/frameworks/js/jsi/inc/jsi_api_errcode.h @@ -0,0 +1,30 @@ +/* + * 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 JSI_API_ERRCODE_H +#define JSI_API_ERRCODE_H + +#include "jsi/jsi.h" +#include "jsi/jsi_types.h" + +namespace OHOS { +namespace ACELite { + +void CallbackErrorCodeOrDataResult(const JSIValue thisVal, const JSIValue args, int32_t errCode, const JSIValue data); +JSIValue ThrowErrorCodeResult(int32_t errorCode); + +} // namespace ACELite +} // namespace OHOS +#endif // JSI_API_ERRCODE_H diff --git a/frameworks/js/jsi/inc/jsi_list.h b/frameworks/js/jsi/inc/jsi_list.h new file mode 100644 index 0000000000000000000000000000000000000000..06d25374fafbd9042c09d4c1d46aded76c514228 --- /dev/null +++ b/frameworks/js/jsi/inc/jsi_list.h @@ -0,0 +1,43 @@ +/* + * 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 JSI_LIST_H +#define JSI_LIST_H + +#include "los_list.h" +#include "jsi_api_common.h" + +namespace OHOS { +namespace ACELite { + +typedef struct { + LiteAlgType type; + LOS_DL_LIST *objListHeader; +} ListInfo; + +typedef struct { + LOS_DL_LIST listNode; + uint32_t objAddr; +} ObjList; + +void ListObjInit(LiteAlgType type); +HcfResult ListAddObjNode(LiteAlgType type, uint32_t addAddr); +void ListDeleteObjNode(LiteAlgType type, uint32_t deleteAddr); +void ListDestroy(LiteAlgType type); + +} // namespace ACELite +} // namespace OHOS + +#endif // JSI_LIST_H diff --git a/frameworks/js/jsi/inc/jsi_utils.h b/frameworks/js/jsi/inc/jsi_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..e3ee876b6a115cd896b72a9985261e0b415f5ef0 --- /dev/null +++ b/frameworks/js/jsi/inc/jsi_utils.h @@ -0,0 +1,32 @@ +/* + * 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 JSI_UTILS_H +#define JSI_UTILS_H + +#include + +#include "blob.h" +#include "jsi_api_common.h" + +namespace OHOS { +namespace ACELite { + +HcfResult ParseUint8ArrayToBlob(JSIValue value, HcfBlob *blob); +JSIValue ConstructJSIReturnResult(const HcfBlob *blob); + +} // namespace ACELite +} // namespace OHOS +#endif // JSI_UTILS_H diff --git a/frameworks/js/jsi/src/jsi_api.cpp b/frameworks/js/jsi/src/jsi_api.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3312a0b3e603f2a279282bd269c2f304c37d24f5 --- /dev/null +++ b/frameworks/js/jsi/src/jsi_api.cpp @@ -0,0 +1,39 @@ +/* + * 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. + */ + +#include "jsi_api.h" +#include "jsi_list.h" +#include "jsi.h" + +namespace OHOS { +namespace ACELite { + +void InitCryptoFrameworkModule(JSIValue exports) +{ + JSI::SetModuleAPI(exports, "createMd", CryptoFrameworkLiteModule::CreateMd); + JSI::SetModuleAPI(exports, "createRandom", CryptoFrameworkLiteModule::CreateRandom); + JSI::SetOnDestroy(exports, CryptoFrameworkLiteModule::OnDestroy); + ListObjInit(JSI_ALG_MD); + ListObjInit(JSI_ALG_RAND); +} + +void CryptoFrameworkLiteModule::OnDestroy(void) +{ + RandomDestroy(); + MdDestroy(); +} + +} // ACELite +} // OHOS diff --git a/frameworks/js/jsi/src/jsi_api_common.cpp b/frameworks/js/jsi/src/jsi_api_common.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36db62386968ed51f5c3e67b7880eb9757772d5e --- /dev/null +++ b/frameworks/js/jsi/src/jsi_api_common.cpp @@ -0,0 +1,31 @@ +/* + * 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. + */ + +#include "jsi_api_common.h" +#include "jsi_api_errcode.h" + +namespace OHOS { +namespace ACELite { + +void JsiAsyncCallback(const JSIValue thisVal, const JSIValue args, const JSIValue *params, uint8_t paramsNum) +{ + JSIValue para[ARRAY_MAX_SIZE] = { params[ARRAY_INDEX_ZERO], params[ARRAY_INDEX_ONE] }; + JSI::CallFunction(args, thisVal, para, paramsNum); + JSI::ReleaseValue(para[ARRAY_INDEX_ZERO]); + JSI::ReleaseValue(para[ARRAY_INDEX_ONE]); +} + +} // namespace ACELite +} // namespace OHOS diff --git a/frameworks/js/jsi/src/jsi_api_errcode.cpp b/frameworks/js/jsi/src/jsi_api_errcode.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9707bbc8fcc5a12841f6f753001349d45e2ac59 --- /dev/null +++ b/frameworks/js/jsi/src/jsi_api_errcode.cpp @@ -0,0 +1,88 @@ +/* + * 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. + */ + +#include "jsi_api_errcode.h" +#include "jsi_api_common.h" + +namespace OHOS { +namespace ACELite { + +constexpr uint32_t JSI_ERR_CODE_DEFAULT_ERR = 0; +constexpr uint32_t JSI_ERR_CODE_OUT_OF_MEMORY = 17620001; +constexpr uint32_t JSI_ERR_CODE_RUNTIME_ERROR = 17620002; +constexpr uint32_t JSI_ERR_CODE_CRYPTO_OPERATION = 17630001; + +typedef struct { + uint32_t errorCode; + const char *errorMsg; +} JsiErrMsg; + +static JsiErrMsg g_errMsg[] = { + { JSI_ERR_CODE_PARAM_CHECK_FAILED, "Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;\ + 2. Incorrect parameter types; 3. Parameter verification failed." }, + { JSI_ERR_CODE_NOT_SUPPORTED, "Capability not supported. Failed to call the API due to limited device\ + capabilities." }, + { JSI_ERR_CODE_OUT_OF_MEMORY, "memory error." }, + { JSI_ERR_CODE_RUNTIME_ERROR, "runtime error." }, + { JSI_ERR_CODE_CRYPTO_OPERATION, "crypto operation error." }, +}; + +static uint32_t GetJsiErrValueByErrCode(HcfResult errCode) +{ + switch (errCode) { + case HCF_INVALID_PARAMS: + return JSI_ERR_CODE_PARAM_CHECK_FAILED; + case HCF_NOT_SUPPORT: + return JSI_ERR_CODE_NOT_SUPPORTED; + case HCF_ERR_MALLOC: + return JSI_ERR_CODE_OUT_OF_MEMORY; + case HCF_ERR_NAPI: + return JSI_ERR_CODE_RUNTIME_ERROR; + case HCF_ERR_CRYPTO_OPERATION: + return JSI_ERR_CODE_CRYPTO_OPERATION; + default: + return JSI_ERR_CODE_DEFAULT_ERR; + } +} + +JSIValue ThrowErrorCodeResult(int32_t errCode) +{ + for (uint32_t index = 0; index < sizeof(g_errMsg) / sizeof(g_errMsg[0]); index++) { + if (g_errMsg[index].errorCode == GetJsiErrValueByErrCode((HcfResult)errCode)) { + return JSI::CreateErrorWithCode(g_errMsg[index].errorCode, g_errMsg[index].errorMsg); + } + } + + return JSI::CreateUndefined(); +} + +void CallbackErrorCodeOrDataResult(const JSIValue thisVal, const JSIValue args, int32_t errCode, const JSIValue data) +{ + for (uint32_t index = 0; index < sizeof(g_errMsg) /sizeof(g_errMsg[0]); index++) { + if (g_errMsg[index].errorCode == GetJsiErrValueByErrCode((HcfResult)errCode)) { + JSIValue errObj = JSI::CreateObject(); + JSI::SetNumberProperty(errObj, "code", g_errMsg[index].errorCode); + JSI::SetStringProperty(errObj, "message", g_errMsg[index].errorMsg); + JSIValue params[ARRAY_MAX_SIZE] = { errObj, data }; + JsiAsyncCallback(thisVal, args, params, ARRAY_MAX_SIZE); + return; + } + } + JSIValue params[ARRAY_MAX_SIZE] = { JSI::CreateUndefined(), data }; + JsiAsyncCallback(thisVal, args, params, ARRAY_MAX_SIZE); +} + +} // ACELite +} // OHOS diff --git a/frameworks/js/jsi/src/jsi_list.cpp b/frameworks/js/jsi/src/jsi_list.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14343b082ebc1c879e84087bed44df61af5d687c --- /dev/null +++ b/frameworks/js/jsi/src/jsi_list.cpp @@ -0,0 +1,97 @@ +/* + * 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. + */ + +#include "jsi_list.h" +#include "memory.h" + +static LOS_DL_LIST g_mdObjListHeader = { 0 }; +static LOS_DL_LIST g_randObjListHeader = { 0 }; + +namespace OHOS { +namespace ACELite { + +ListInfo g_listMap[] = { + { JSI_ALG_MD, &g_mdObjListHeader }, + { JSI_ALG_RAND, &g_randObjListHeader } +}; + +LOS_DL_LIST *GetListHeader(LiteAlgType type) +{ + for (uint32_t index = 0; index < sizeof(g_listMap) / sizeof(g_listMap[0]); index++) { + if (type == g_listMap[index].type) { + return g_listMap[index].objListHeader; + } + } + + return nullptr; +} + +void ListObjInit(LiteAlgType type) +{ + LOS_ListInit(GetListHeader(type)); +} + +HcfResult ListAddObjNode(LiteAlgType type, uint32_t addAddr) +{ + ObjList *obj = (ObjList *)HcfMalloc(sizeof(ObjList), 0); + if (obj == nullptr) { + return HCF_ERR_MALLOC; + } + obj->objAddr = addAddr; + + if (GetListHeader(type)->pstNext == nullptr) { + LOS_ListInit(GetListHeader(type)); + } + LOS_ListAdd(GetListHeader(type), &(obj->listNode)); + + return HCF_SUCCESS; +} + +void ListDeleteObjNode(LiteAlgType type, uint32_t deleteAddr) +{ + ObjList *obj = nullptr; + ObjList *objNext = nullptr; + LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(obj, objNext, GetListHeader(type), ObjList, listNode) { + if (obj == nullptr) { + return; + } + if ((obj->objAddr != 0) && (obj->objAddr == deleteAddr)) { + LOS_ListDelete(&(obj->listNode)); + HcfObjDestroy((void *)deleteAddr); + obj->objAddr = 0; + HcfFree(obj); + obj = nullptr; + } + } +} + +void ListDestroy(LiteAlgType type) +{ + ObjList *obj = nullptr; + ObjList *objNext = nullptr; + uint32_t i = 0; + LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(obj, objNext, GetListHeader(type), ObjList, listNode) { + if (obj == nullptr) { + return; + } + LOS_ListDelete(&(obj->listNode)); + HcfObjDestroy((void *)(obj->objAddr)); + HcfFree(obj); + obj = nullptr; + } +} + +} // ACELite +} // OHOS diff --git a/frameworks/js/jsi/src/jsi_md.cpp b/frameworks/js/jsi/src/jsi_md.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d0d361d74478d62bfd10ac52fcce2cef4d8dfe4 --- /dev/null +++ b/frameworks/js/jsi/src/jsi_md.cpp @@ -0,0 +1,200 @@ +/* + * 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. + */ + +#include "jsi_api.h" +#include "jsi_api_common.h" +#include "jsi_api_errcode.h" +#include "jsi_utils.h" +#include "jsi_list.h" +#include "securec.h" +#include "log.h" + +namespace OHOS { +namespace ACELite { + +JSIValue CryptoFrameworkLiteModule::CreateMd(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + if ((args == nullptr) || (argsNum != ARRAY_INDEX_ONE)) { + LOGE("CreateMd args is err!"); + return ThrowErrorCodeResult(HCF_INVALID_PARAMS); + } + char *alg = JSI::ValueToString(args[0]); + if (alg == nullptr) { + LOGE("Update alg is null!"); + return ThrowErrorCodeResult(HCF_INVALID_PARAMS); + } + + HcfMd *mdObj = nullptr; + HcfResult res = HcfMdCreate(reinterpret_cast(alg), &mdObj); + if (res != HCF_SUCCESS) { + LOGE("CreateMd is mdObj err res %d!", res); + return ThrowErrorCodeResult(res); + } + res = ListAddObjNode(JSI_ALG_MD, (uint32_t)mdObj); + if (res != HCF_SUCCESS) { + LOGE("md add node is %d err!", res); + HcfObjDestroy((void *)mdObj); + return ThrowErrorCodeResult(res); + } + + JSIValue serviceObj = JSI::CreateObject(); + JSIValue update = JSI::CreateFunction(Update); + JSIValue updateSync = JSI::CreateFunction(UpdateSync); + JSIValue digest = JSI::CreateFunction(Digest); + JSIValue digestSync = JSI::CreateFunction(DigestSync); + JSIValue getMdLength = JSI::CreateFunction(GetMdLength); + JSI::SetNamedProperty(serviceObj, "update", update); + JSI::SetNamedProperty(serviceObj, "updateSync", updateSync); + JSI::SetNamedProperty(serviceObj, "digest", digest); + JSI::SetNamedProperty(serviceObj, "digestSync", digestSync); + JSI::SetNamedProperty(serviceObj, "getMdLength", getMdLength); + JSI::SetNumberProperty(serviceObj, "mdObj", (double)(uint32_t)mdObj); + JSI::ReleaseValueList(update, updateSync, digest, digestSync, getMdLength, ARGS_END); + + return serviceObj; +} + +JSIValue CryptoFrameworkLiteModule::Update(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + if ((args == nullptr) || (argsNum != ARRAY_MAX_SIZE)) { + LOGE("Update args is null!"); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], HCF_INVALID_PARAMS, JSI::CreateNull()); + return JSI::CreateUndefined(); + } + HcfMd *mdObj = (HcfMd *)(uint32_t)JSI::GetNumberProperty(thisVal, "mdObj"); + if (mdObj == nullptr) { + LOGE("Update mdObj is null!!"); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], HCF_INVALID_PARAMS, JSI::CreateNull()); + return JSI::CreateUndefined(); + } + + JSIValue inVlaue = JSI::GetNamedProperty(args[ARRAY_INDEX_ZERO], "data"); + HcfBlob inBlob = { .data = nullptr, .len = 0 }; + HcfResult errCode = ParseUint8ArrayToBlob(inVlaue, &inBlob); + if (errCode != HCF_SUCCESS) { + LOGE("Update inBlob is null!"); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], HCF_INVALID_PARAMS, JSI::CreateNull()); + return JSI::CreateUndefined(); + } + + errCode = mdObj->update(mdObj, &inBlob); + HcfBlobDataClearAndFree(&inBlob); + if (errCode != HCF_SUCCESS) { + LOGE("Update errCode not is success!"); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], errCode, JSI::CreateNull()); + return JSI::CreateUndefined(); + } + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], HCF_SUCCESS, JSI::CreateNull()); + + return JSI::CreateUndefined(); +} + +JSIValue CryptoFrameworkLiteModule::UpdateSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + if ((args == nullptr) || (argsNum != ARRAY_INDEX_ONE)) { + LOGE("UpdateSync args is null!"); + return ThrowErrorCodeResult(HCF_INVALID_PARAMS); + } + HcfMd *mdObj = (HcfMd *)(uint32_t)JSI::GetNumberProperty(thisVal, "mdObj"); + if (mdObj == nullptr) { + LOGE("UpdateSync mdObj is null!!"); + return ThrowErrorCodeResult(HCF_INVALID_PARAMS); + } + JSIValue inVlaue = JSI::GetNamedProperty(args[ARRAY_INDEX_ZERO], "data"); + HcfBlob inBlob = { .data = nullptr, .len = 0 }; + HcfResult errCode = ParseUint8ArrayToBlob(inVlaue, &inBlob); + if (errCode != HCF_SUCCESS) { + LOGE("UpdateSync inBlob is null!"); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], HCF_INVALID_PARAMS, JSI::CreateNull()); + return JSI::CreateUndefined(); + } + + errCode = mdObj->update(mdObj, &inBlob); + HcfBlobDataClearAndFree(&inBlob); + if (errCode != HCF_SUCCESS) { + LOGE("UpdateSync update ret is error!"); + } + + return ThrowErrorCodeResult(errCode); +} + +JSIValue CryptoFrameworkLiteModule::Digest(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + if ((args == nullptr) || (argsNum != ARRAY_INDEX_ONE)) { + LOGE("Digest args is err or mdObj nullptr!"); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], HCF_INVALID_PARAMS, JSI::CreateUndefined()); + return JSI::CreateUndefined(); + } + HcfMd *mdObj = (HcfMd *)(uint32_t)JSI::GetNumberProperty(thisVal, "mdObj"); + if (mdObj == nullptr) { + LOGE("Digest mdObj is null!!"); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], HCF_INVALID_PARAMS, JSI::CreateUndefined()); + return JSI::CreateUndefined(); + } + HcfBlob outBlob = { .data = nullptr, .len = 0 }; + HcfResult errCode = mdObj->doFinal(mdObj, &outBlob); + if (errCode != HCF_SUCCESS) { + LOGE("Digest errCode not is success!"); + HcfBlobDataClearAndFree(&outBlob); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], errCode, JSI::CreateUndefined()); + return JSI::CreateUndefined(); + } + JSIValue outVlaue = ConstructJSIReturnResult(&outBlob); + CallbackErrorCodeOrDataResult(thisVal, args[0], errCode, outVlaue); + HcfBlobDataClearAndFree(&outBlob); + + return JSI::CreateUndefined(); +} + +JSIValue CryptoFrameworkLiteModule::DigestSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + HcfMd *mdObj = (HcfMd *)(uint32_t)JSI::GetNumberProperty(thisVal, "mdObj"); + if (mdObj == nullptr) { + LOGE("DigestSync mdObj is null!!"); + return ThrowErrorCodeResult(HCF_INVALID_PARAMS); + } + + HcfBlob outBlob = { .data = nullptr, .len = 0 }; + HcfResult errCode = mdObj->doFinal(mdObj, &outBlob); + if (errCode != HCF_SUCCESS) { + LOGE("DigestSync errCode not is success!"); + HcfBlobDataClearAndFree(&outBlob); + return ThrowErrorCodeResult(errCode); + } + + JSIValue mdSyncData = ConstructJSIReturnResult(&outBlob); + HcfBlobDataClearAndFree(&outBlob); + + return mdSyncData; +} + +JSIValue CryptoFrameworkLiteModule::GetMdLength(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + HcfMd *mdObj = (HcfMd *)(uint32_t)JSI::GetNumberProperty(thisVal, "mdObj"); + if (mdObj == nullptr) { + LOGE("GetMdLength mdObj is null!"); + return ThrowErrorCodeResult(HCF_INVALID_PARAMS); + } + + return JSI::CreateNumber(mdObj->getMdLength(mdObj)); +} + +void CryptoFrameworkLiteModule::MdDestroy(void) +{ + ListDestroy(JSI_ALG_MD); +} + +} // namespace ACELite +} // namespace OHOS diff --git a/frameworks/js/jsi/src/jsi_rand.cpp b/frameworks/js/jsi/src/jsi_rand.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1059e09b003e2ddf0f914fdb487112d6f44f1ef --- /dev/null +++ b/frameworks/js/jsi/src/jsi_rand.cpp @@ -0,0 +1,160 @@ +/* + * 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. + */ + +#include "jsi_api.h" +#include "jsi_api_common.h" +#include "jsi_api_errcode.h" +#include "jsi_utils.h" +#include "jsi_list.h" +#include "securec.h" +#include "jsi.h" +#include "jsi_types.h" +#include "log.h" + +namespace OHOS { +namespace ACELite { + +JSIValue CryptoFrameworkLiteModule::CreateRandom(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + HcfRand *randObj = nullptr; + HcfResult res = HcfRandCreate(&randObj); + if (res != HCF_SUCCESS) { + LOGE("CreateRandom is randObj err %d!", res); + return ThrowErrorCodeResult(res); + } + + res = ListAddObjNode(JSI_ALG_RAND, (uint32_t)randObj); + if (res != HCF_SUCCESS) { + LOGE("rand add node is %d err!", res); + HcfObjDestroy((void *)randObj); + return ThrowErrorCodeResult(res); + } + + JSIValue serviceObj = JSI::CreateObject(); + JSIValue generateRandom = JSI::CreateFunction(GenerateRandom); + JSIValue generateRandomSync = JSI::CreateFunction(GenerateRandomSync); + JSIValue setSeed = JSI::CreateFunction(SetSeed); + + JSI::SetNamedProperty(serviceObj, "generateRandom", generateRandom); + JSI::SetNamedProperty(serviceObj, "generateRandomSync", generateRandomSync); + JSI::SetNamedProperty(serviceObj, "setSeed", setSeed); + JSI::SetNumberProperty(serviceObj, "randObj", (double)(uint32_t)randObj); + JSI::ReleaseValueList(generateRandom, generateRandomSync, setSeed, ARGS_END); + + return serviceObj; +} + +JSIValue CryptoFrameworkLiteModule::GenerateRandom(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + HcfRand *randObj = (HcfRand *)(uint32_t)JSI::GetNumberProperty(thisVal, "randObj"); + if (randObj == nullptr) { + LOGE("GenerateRandom randObj is null!"); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], HCF_INVALID_PARAMS, JSI::CreateUndefined()); + return JSI::CreateUndefined(); + } + if ((args == nullptr) || (argsNum != ARRAY_MAX_SIZE) || (args[ARRAY_INDEX_ONE] == nullptr)) { + LOGE("GenerateRandom params is err!"); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], HCF_INVALID_PARAMS, JSI::CreateUndefined()); + return JSI::CreateUndefined(); + } + + int32_t numBytes = (int32_t)JSI::ValueToNumber(args[0]); + if (numBytes <= 0) { + LOGE("GenerateRandom numBytes too small!"); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], HCF_INVALID_PARAMS, JSI::CreateUndefined()); + return JSI::CreateUndefined(); + } + HcfBlob randBlob = { .data = nullptr, .len = 0 }; + HcfResult res = randObj->generateRandom(randObj, numBytes, &randBlob); + if (res != HCF_SUCCESS) { + LOGE("GenerateRandom randObj not is success!"); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], res, JSI::CreateUndefined()); + return JSI::CreateUndefined(); + } + + JSIValue outVlaue = ConstructJSIReturnResult(&randBlob); + CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], res, outVlaue); + HcfBlobDataClearAndFree(&randBlob); + + return JSI::CreateUndefined(); +} + +JSIValue CryptoFrameworkLiteModule::GenerateRandomSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + HcfRand *randObj = (HcfRand *)(uint32_t)JSI::GetNumberProperty(thisVal, "randObj"); + if (randObj == nullptr) { + LOGE("GenerateRandom randObj is null!!"); + return ThrowErrorCodeResult(HCF_INVALID_PARAMS); + } + + if ((args == nullptr) || (argsNum != ARRAY_INDEX_ONE)) { + LOGE("GenerateRandomSync params is err"); + return ThrowErrorCodeResult(HCF_INVALID_PARAMS); + } + + int32_t numBytes = (int32_t)JSI::ValueToNumber(args[0]); + if (numBytes <= 0) { + LOGE("GenerateRandomSync numBytes too small!"); + return ThrowErrorCodeResult(HCF_INVALID_PARAMS); + } + HcfBlob randBlob = { .data = nullptr, .len = 0 }; + HcfResult res = randObj->generateRandom(randObj, numBytes, &randBlob); + if (res != HCF_SUCCESS) { + LOGE("GenerateRandomSync randObj not is success!"); + HcfBlobDataClearAndFree(&randBlob); + return ThrowErrorCodeResult(res); + } + JSIValue randomSyncData = ConstructJSIReturnResult(&randBlob); + HcfBlobDataClearAndFree(&randBlob); + + return randomSyncData; +} + +JSIValue CryptoFrameworkLiteModule::SetSeed(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + HcfRand *randObj = (HcfRand *)(uint32_t)JSI::GetNumberProperty(thisVal, "randObj"); + if (randObj == nullptr) { + LOGE("SetSeed randObj is null!!"); + return ThrowErrorCodeResult(HCF_INVALID_PARAMS); + } + if ((args == nullptr) || (argsNum != ARRAY_INDEX_ONE)) { + LOGE("SetSeed params is null"); + return ThrowErrorCodeResult(HCF_INVALID_PARAMS); + } + JSIValue inVlaue = JSI::GetNamedProperty(args[ARRAY_INDEX_ZERO], "data"); + HcfBlob seedBlob = { .data = nullptr, .len = 0 }; + HcfResult errCode = ParseUint8ArrayToBlob(inVlaue, &seedBlob); + if (errCode != HCF_SUCCESS) { + LOGE("SetSeed seedBlob is null!"); + return ThrowErrorCodeResult(HCF_ERR_MALLOC); + } + + HcfResult res = randObj->setSeed(randObj, &seedBlob); + HcfBlobDataClearAndFree(&seedBlob); + if (res != HCF_SUCCESS) { + LOGE("setSeed randObj not is success!"); + return ThrowErrorCodeResult(res); + } + + return ThrowErrorCodeResult(HCF_SUCCESS); +} + +void CryptoFrameworkLiteModule::RandomDestroy(void) +{ + ListDestroy(JSI_ALG_RAND); +} + +} // namespace ACELite +} // namespace OHOS diff --git a/frameworks/js/jsi/src/jsi_utils.cpp b/frameworks/js/jsi/src/jsi_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cbbb46d56297463c69658cb9d735ad728eadf51 --- /dev/null +++ b/frameworks/js/jsi/src/jsi_utils.cpp @@ -0,0 +1,91 @@ +/* + * 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. + */ + +#include "jsi_utils.h" +#include "jsi.h" +#include "jsi_types.h" +#include "memory.h" +#include "securec.h" +#include "utils.h" +#include "log.h" + +namespace OHOS { +namespace ACELite { + +HcfResult ParseUint8ArrayToBlob(JSIValue value, HcfBlob *blob) +{ + if (!JSI::ValueIsTypedArray(value) || (blob == nullptr)) { + LOGE("value is not a typed array!"); + return HCF_INVALID_PARAMS; + } + TypedArrayType arrayType; + size_t arraySize = 0; + size_t byteOffset = 0; + JSIValue arrayBuffer = nullptr; + uint8_t *dataArray; + HcfResult ret = HCF_SUCCESS; + do { + dataArray = JSI::GetTypedArrayInfo(value, arrayType, arraySize, arrayBuffer, byteOffset); + if (dataArray == nullptr) { + ret = HCF_ERR_CRYPTO_OPERATION; + break; + } + if (arrayType != TypedArrayType::JSI_UINT8_ARRAY) { + LOGE("value is not a uint8 array"); + ret = HCF_INVALID_PARAMS; + break; + } + blob->data = (uint8_t *)HcfMalloc(arraySize, 0); + if (blob->data == nullptr) { + ret = HCF_ERR_MALLOC; + break; + } + memcpy_s(blob->data, arraySize, dataArray + byteOffset, arraySize); + blob->len = arraySize; + } while (0); + if (arrayBuffer != nullptr) { + JSI::ReleaseValue(arrayBuffer); + arrayBuffer = nullptr; + } + return ret; +} + +JSIValue ConstructJSIReturnResult(const HcfBlob *blob) +{ + JSIValue res; + do { + res = JSI::CreateObject(); + if (res == nullptr) { + break; + } + if (blob->data != nullptr) { + uint8_t *arrayBuffer = nullptr; + JSIValue buffer = JSI::CreateArrayBuffer(blob->len, arrayBuffer); + if (arrayBuffer == nullptr) { + LOGE("create jsi array buffer failed"); + JSI::ReleaseValue(buffer); + return res; + } + (void)memcpy_s(arrayBuffer, blob->len, blob->data, blob->len); + JSIValue typedArray = JSI::CreateTypedArray(TypedArrayType::JSI_UINT8_ARRAY, blob->len, buffer, 0); + JSI::ReleaseValue(buffer); + JSI::SetNamedProperty(res, "data", typedArray); + } + } while (0); + return res; +} + +} // namespace ACELite +} // namespace OHOS diff --git a/frameworks/spi/rand_spi.h b/frameworks/spi/rand_spi.h index 8b554c672eecbb6eb2e5338dc18c53e4917cb7ff..166950f6b876dc85ecc864567f38f9a26513b94e 100644 --- a/frameworks/spi/rand_spi.h +++ b/frameworks/spi/rand_spi.h @@ -22,6 +22,7 @@ #include "object_base.h" #define OPENSSL_RAND_ALGORITHM "CTR_DRBG" +#define MBEDTLS_RAND_ALGORITHM "CTR_DRBG_MBEDTLS" typedef struct HcfRandSpi HcfRandSpi; diff --git a/plugin/BUILD.gn b/plugin/BUILD.gn index 3c3e7659c8f4f7d2668b03aaf230d4583b6a5f48..08b689cbf7b6c9ec1bbd7686abca993ed8fde260 100644 --- a/plugin/BUILD.gn +++ b/plugin/BUILD.gn @@ -25,36 +25,55 @@ config("plugin_config") { ] } -ohos_shared_library("crypto_openssl_plugin_lib") { - branch_protector_ret = "pac_ret" - subsystem_name = "security" - innerapi_tags = [ "platformsdk_indirect" ] - part_name = "crypto_framework" - public_configs = [ ":plugin_config" ] - include_dirs = plugin_inc_path + crypto_framwork_common_inc_path - - sources = plugin_files - - if (os_level == "standard") { - sanitize = { - cfi = true - cfi_cross_dso = true - debug = false +if (os_level == "standard") { + ohos_shared_library("crypto_openssl_plugin_lib") { + branch_protector_ret = "pac_ret" + subsystem_name = "security" + innerapi_tags = [ "platformsdk_indirect" ] + part_name = "crypto_framework" + public_configs = [ ":plugin_config" ] + include_dirs = plugin_inc_path + crypto_framwork_common_inc_path + + sources = plugin_files + + if (os_level == "standard") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } } + + cflags = [ + "-DHILOG_ENABLE", + "-fPIC", + "-Wall", + ] + + deps = [ "//base/security/crypto_framework/common:crypto_plugin_common" ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "openssl:libcrypto_shared", + ] + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } +} else if (os_level == "mini") { + ohos_static_library("crypto_mbedtls_plugin_lib") { + subsystem_name = "security" + part_name = "crypto_framework" + public_configs = [ ":plugin_config" ] + include_dirs = crypto_framwork_common_inc_path + mbedtls_plugin_inc_path + include_dirs += + [ "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite" ] - cflags = [ - "-DHILOG_ENABLE", - "-fPIC", - "-Wall", - ] + sources = mbedtls_plugin_files - deps = [ "//base/security/crypto_framework/common:crypto_plugin_common" ] + defines = [ "MINI_HILOG_ENABLE" ] - external_deps = [ - "c_utils:utils", - "hilog:libhilog", - "openssl:libcrypto_shared", - ] - defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] + deps = [ "//base/security/crypto_framework/common:crypto_common_lite" ] + + configs = [ "${product_path}:product_public_configs" ] + } } diff --git a/plugin/mbedtls_plugin/common/mbedtls_common.h b/plugin/mbedtls_plugin/common/mbedtls_common.h new file mode 100644 index 0000000000000000000000000000000000000000..702a39e58af488ee296f9fd9715f8f22e462789e --- /dev/null +++ b/plugin/mbedtls_plugin/common/mbedtls_common.h @@ -0,0 +1,30 @@ +/* + * 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 MBEDTLS_COMMON_H +#define MBEDTLS_COMMON_H + +#include +#include + +#include "result.h" +#include "utils.h" + +#define HCF_MBEDTLS_SUCCESS 0 +#define HCF_MBEDTLS_FAILURE (-1) +#define HCF_BITS_PER_BYTE 8 +#define HCF_EVP_MAX_MD_SIZE 64 + +#endif diff --git a/plugin/mbedtls_plugin/md/inc/mbedtls_md.h b/plugin/mbedtls_plugin/md/inc/mbedtls_md.h new file mode 100644 index 0000000000000000000000000000000000000000..cbd66dac9a2b86858045dbfab383d48dd4cc7dd1 --- /dev/null +++ b/plugin/mbedtls_plugin/md/inc/mbedtls_md.h @@ -0,0 +1,31 @@ +/* + * 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_MBEDTLS_MD_H +#define HCF_MBEDTLS_MD_H + +#include "md_spi.h" + +#define HCF_MBEDTLS_INVALID_MD_LEN 0 + +#ifdef __cplusplus +extern "C" { +#endif + +HcfResult MbedtlsMdSpiCreate(const char *mbedtlsAlgoName, HcfMdSpi **spiObj); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/plugin/mbedtls_plugin/md/src/mbedtls_md.c b/plugin/mbedtls_plugin/md/src/mbedtls_md.c new file mode 100644 index 0000000000000000000000000000000000000000..73a5fd680191873de98caade4e9317713fa67bae --- /dev/null +++ b/plugin/mbedtls_plugin/md/src/mbedtls_md.c @@ -0,0 +1,197 @@ +/* + * 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. + */ + +#include "mbedtls_md.h" + +#include "mbedtls_common.h" +#include "mbedtls/md.h" +#include "securec.h" +#include "log.h" +#include "memory.h" +#include "config.h" +#include "utils.h" + +typedef struct { + HcfMdSpi base; + mbedtls_md_context_t *ctx; + char mbedtlsAlgoName[HCF_MAX_ALGO_NAME_LEN]; +} MbedtlsMdSpiImpl; + +mbedtls_md_context_t *MbedtlsEvpMdCtxNew(void) +{ + return (mbedtls_md_context_t *)HcfMalloc(sizeof(mbedtls_md_context_t), 0); +} + +void MbedtlsEvpMdCtxFree(mbedtls_md_context_t *ctx) +{ + HcfFree(ctx); +} + +static const char *MbedtlsGetMdClass(void) +{ + return "MbedtlsMd"; +} + +static mbedtls_md_context_t *MbedtlsGetMdCtx(HcfMdSpi *self) +{ + if (!HcfIsClassMatch((HcfObjectBase *)self, MbedtlsGetMdClass())) { + LOGE("Class is not match."); + return NULL; + } + + return ((MbedtlsMdSpiImpl *)self)->ctx; +} + +static HcfResult MbedtlsEngineUpdateMd(HcfMdSpi *self, HcfBlob *input) +{ + mbedtls_md_context_t *ctx = MbedtlsGetMdCtx(self); + if (ctx == NULL) { + LOGD("The CTX is NULL!"); + return HCF_INVALID_PARAMS; + } + int32_t ret = mbedtls_md_update(ctx, (const unsigned char *)input->data, input->len); + if (ret != HCF_MBEDTLS_SUCCESS) { + LOGD("EVP_DigestUpdate return error %d!", ret); + return HCF_ERR_CRYPTO_OPERATION; + } + + return HCF_SUCCESS; +} + +static HcfResult MbedtlsEngineDoFinalMd(HcfMdSpi *self, HcfBlob *output) +{ + if ((self == NULL) || (output == NULL)) { + LOGE("The input self ptr is NULL!"); + return HCF_INVALID_PARAMS; + } + mbedtls_md_context_t *ctx = MbedtlsGetMdCtx(self); + if (ctx == NULL) { + LOGE("The CTX is NULL!"); + return HCF_INVALID_PARAMS; + } + unsigned char outputBuf[HCF_EVP_MAX_MD_SIZE] = { 0 }; + uint8_t outputLen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(ctx)); + if (outputLen == 0) { + LOGD("Failed to md get size is 0!"); + return HCF_ERR_CRYPTO_OPERATION; + } + int32_t ret = mbedtls_md_finish(ctx, outputBuf); + if (ret != HCF_MBEDTLS_SUCCESS) { + LOGD("Failed to md finish return error is %d!", ret); + 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 MbedtlsEngineGetMdLength(HcfMdSpi *self) +{ + mbedtls_md_context_t *ctx = MbedtlsGetMdCtx(self); + if (ctx == NULL) { + LOGD("The CTX is NULL!"); + return HCF_MBEDTLS_INVALID_MD_LEN; + } + uint8_t outputLen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(ctx)); + if ((outputLen == 0) || (outputLen > HCF_EVP_MAX_MD_SIZE)) { + LOGD("Get the overflow path length is %d in mbedtls!", outputLen); + return HCF_MBEDTLS_INVALID_MD_LEN; + } + + return outputLen; +} + +static void MbedtlsDestroyMd(HcfObjectBase *self) +{ + if (self == NULL) { + LOGE("The input self ptr is NULL!"); + return; + } + if (!HcfIsClassMatch(self, MbedtlsGetMdClass())) { + LOGE("Class is not match."); + return; + } + if (MbedtlsGetMdCtx((HcfMdSpi *)self) != NULL) { + mbedtls_md_free(MbedtlsGetMdCtx((HcfMdSpi *)self)); + MbedtlsEvpMdCtxFree(MbedtlsGetMdCtx((HcfMdSpi *)self)); + } + HcfFree(self); +} + +typedef struct { + char *mdAlg; + mbedtls_md_type_t mdType; +} MdAlgTypeMap; + +static MdAlgTypeMap g_mdAlgMap[] = { + { "MD5", MBEDTLS_MD_MD5 }, + { "SHA1", MBEDTLS_MD_SHA1 }, + { "SHA256", MBEDTLS_MD_SHA256 }, + { "SHA512", MBEDTLS_MD_SHA512 }, +}; + +int MbedtlsEvpDigestInitEx(mbedtls_md_context_t *ctx, const char *mbedtlsAlgoName) +{ + for (uint32_t index = 0; index < sizeof(g_mdAlgMap) / sizeof(g_mdAlgMap[0]); index++) { + if (strcmp(g_mdAlgMap[index].mdAlg, mbedtlsAlgoName) == 0) { + mbedtls_md_init(ctx); + mbedtls_md_setup(ctx, mbedtls_md_info_from_type(g_mdAlgMap[index].mdType), 0); + mbedtls_md_starts(ctx); + return HCF_MBEDTLS_SUCCESS; + } + } + + return HCF_MBEDTLS_FAILURE; +} + +HcfResult MbedtlsMdSpiCreate(const char *mbedtlsAlgoName, HcfMdSpi **spiObj) +{ + if (spiObj == NULL) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + MbedtlsMdSpiImpl *returnSpiImpl = (MbedtlsMdSpiImpl *)HcfMalloc(sizeof(MbedtlsMdSpiImpl), 0); + if (returnSpiImpl == NULL) { + LOGE("Failed to allocate returnImpl memory!"); + return HCF_ERR_MALLOC; + } + returnSpiImpl->ctx = MbedtlsEvpMdCtxNew(); + if (returnSpiImpl->ctx == NULL) { + LOGE("Failed to create ctx!"); + HcfFree(returnSpiImpl); + return HCF_ERR_MALLOC; + } + int32_t ret = MbedtlsEvpDigestInitEx(returnSpiImpl->ctx, mbedtlsAlgoName); + if (ret != HCF_MBEDTLS_SUCCESS) { + LOGD("Failed to init MD ret is %d!", ret); + MbedtlsEvpMdCtxFree(returnSpiImpl->ctx); + HcfFree(returnSpiImpl); + return HCF_ERR_CRYPTO_OPERATION; + } + returnSpiImpl->base.base.getClass = MbedtlsGetMdClass; + returnSpiImpl->base.base.destroy = MbedtlsDestroyMd; + returnSpiImpl->base.engineUpdateMd = MbedtlsEngineUpdateMd; + returnSpiImpl->base.engineDoFinalMd = MbedtlsEngineDoFinalMd; + returnSpiImpl->base.engineGetMdLength = MbedtlsEngineGetMdLength; + *spiObj = (HcfMdSpi *)returnSpiImpl; + + return HCF_SUCCESS; +} diff --git a/plugin/mbedtls_plugin/rand/inc/mbedtls_rand.h b/plugin/mbedtls_plugin/rand/inc/mbedtls_rand.h new file mode 100644 index 0000000000000000000000000000000000000000..3ce39205dacecaeedfee446e193cf70ed3bedbd7 --- /dev/null +++ b/plugin/mbedtls_plugin/rand/inc/mbedtls_rand.h @@ -0,0 +1,29 @@ +/* + * 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_MBEDTLS_RAND_H +#define HCF_MBEDTLS_RAND_H + +#include "rand_spi.h" + +#ifdef __cplusplus +extern "C" { +#endif + +HcfResult MbedtlsRandSpiCreate(HcfRandSpi **spiObj); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/plugin/mbedtls_plugin/rand/src/mbedtls_rand.c b/plugin/mbedtls_plugin/rand/src/mbedtls_rand.c new file mode 100644 index 0000000000000000000000000000000000000000..247eb691e6207982f92aa4b20a013de07497b1ef --- /dev/null +++ b/plugin/mbedtls_plugin/rand/src/mbedtls_rand.c @@ -0,0 +1,208 @@ +/* + * 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. + */ + +#include "mbedtls_rand.h" + +#include "mbedtls/entropy.h" +#include "mbedtls/ctr_drbg.h" +#include "securec.h" +#include "log.h" +#include "memory.h" +#include "utils.h" + +typedef struct { + HcfRandSpi base; + mbedtls_entropy_context *entropy; + mbedtls_ctr_drbg_context *ctrDrbg; +} HcfRandSpiImpl; + +static const char *GetMbedtlsRandClass(void) +{ + return "RandMbedtls"; +} + +static mbedtls_entropy_context *MbedtlsGetMdEntropy(HcfRandSpi *self) +{ + if (!HcfIsClassMatch((HcfObjectBase *)self, GetMbedtlsRandClass())) { + LOGE("Class is not match."); + return NULL; + } + + return ((HcfRandSpiImpl *)self)->entropy; +} + +static mbedtls_ctr_drbg_context *MbedtlsGetMdCtrDrbg(HcfRandSpi *self) +{ + if (!HcfIsClassMatch((HcfObjectBase *)self, GetMbedtlsRandClass())) { + LOGE("Class is not match."); + return NULL; + } + return ((HcfRandSpiImpl *)self)->ctrDrbg; +} + +static HcfResult MbedtlsGenerateRandom(HcfRandSpi *self, int32_t numBytes, HcfBlob *random) +{ + if ((self == NULL) || (random == NULL)) { + LOGE("Invalid params!"); + return HCF_INVALID_PARAMS; + } + if (numBytes <= 0) { + LOGE("Invalid numBytes!"); + return HCF_INVALID_PARAMS; + } + mbedtls_ctr_drbg_context *ctrDrbg = MbedtlsGetMdCtrDrbg(self); + if (ctrDrbg == NULL) { + LOGE("Invalid ctrDrbg null!"); + return HCF_INVALID_PARAMS; + } + random->data = (uint8_t *)HcfMalloc(numBytes, 0); + if (random->data == NULL) { + LOGE("Failed to allocate random->data memory!"); + return HCF_ERR_MALLOC; + } + int32_t ret = mbedtls_ctr_drbg_random(ctrDrbg, random->data, numBytes); + if (ret != 0) { + LOGE("RAND_bytes return is %d error!", ret); + HcfFree(random->data); + random->data = NULL; + return HCF_ERR_CRYPTO_OPERATION; + } + random->len = numBytes; + + return HCF_SUCCESS; +} + +static const char *MbedtlsGetRandAlgoName(HcfRandSpi *self) +{ + if (self == NULL) { + LOGE("Invalid input parameter."); + return NULL; + } + if (!HcfIsClassMatch((HcfObjectBase *)self, GetMbedtlsRandClass())) { + LOGE("Class is not match."); + return NULL; + } + + return MBEDTLS_RAND_ALGORITHM; +} + +static void MbedtlsSetSeed(HcfRandSpi *self, HcfBlob *seed) +{ + if ((self == NULL) || (seed == NULL)) { + LOGE("Invalid params!"); + return; + } + if ((seed->data == NULL) || (seed->len == 0)) { + LOGE("Invalid numBytes!"); + return; + } + mbedtls_ctr_drbg_context *ctrDrbg = MbedtlsGetMdCtrDrbg(self); + if (ctrDrbg == NULL) { + LOGE("Invalid ctrDrbg params!"); + return; + } + mbedtls_entropy_context *entropy = MbedtlsGetMdEntropy(self); + if (entropy == NULL) { + LOGE("Invalid entropy params!"); + return; + } + int32_t ret = mbedtls_ctr_drbg_seed(ctrDrbg, mbedtls_entropy_func, entropy, + (const unsigned char *)seed->data, seed->len); + if (ret != 0) { + LOGE("seed return is %d error!", ret); + return; + } +} + +static void DestroyMbedtlsRand(HcfObjectBase *self) +{ + if (self == NULL) { + LOGE("Self ptr is NULL!"); + return; + } + if (!HcfIsClassMatch(self, GetMbedtlsRandClass())) { + LOGE("Class is not match."); + return; + } + mbedtls_ctr_drbg_context *ctrDrbg = MbedtlsGetMdCtrDrbg((HcfRandSpi *)self); + if (ctrDrbg != NULL) { + mbedtls_ctr_drbg_free(ctrDrbg); + HcfFree(ctrDrbg); + } + mbedtls_entropy_context *entropy = MbedtlsGetMdEntropy((HcfRandSpi *)self); + if (entropy != NULL) { + mbedtls_entropy_free(entropy); + HcfFree(entropy); + } + HcfFree(self); +} + +static int32_t MbedtlsRandInitEx(mbedtls_entropy_context **entropy, mbedtls_ctr_drbg_context **ctrDrbg) +{ + if ((entropy == NULL) || (ctrDrbg == NULL)) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + *entropy = (mbedtls_entropy_context *)HcfMalloc(sizeof(mbedtls_entropy_context), 0); + if (*entropy == NULL) { + LOGE("Failed to allocate *entropy memory!"); + return HCF_ERR_MALLOC; + } + *ctrDrbg = (mbedtls_ctr_drbg_context *)HcfMalloc(sizeof(mbedtls_ctr_drbg_context), 0); + if (*ctrDrbg == NULL) { + HcfFree(*entropy); + LOGE("Failed to allocate *ctrDrbg memory!"); + return HCF_ERR_MALLOC; + } + mbedtls_entropy_init(*entropy); + mbedtls_ctr_drbg_init(*ctrDrbg); + int32_t ret = mbedtls_ctr_drbg_seed(*ctrDrbg, mbedtls_entropy_func, *entropy, NULL, 0); + if (ret != 0) { + LOGE("Failed seed ret is %d!", ret); + mbedtls_entropy_free(*entropy); + mbedtls_ctr_drbg_free(*ctrDrbg); + HcfFree(*entropy); + HcfFree(*ctrDrbg); + return HCF_ERR_CRYPTO_OPERATION; + } + + return HCF_SUCCESS; +} + +HcfResult MbedtlsRandSpiCreate(HcfRandSpi **spiObj) +{ + if (spiObj == NULL) { + LOGE("Invalid input parameter."); + return HCF_INVALID_PARAMS; + } + HcfRandSpiImpl *returnSpiImpl = (HcfRandSpiImpl *)HcfMalloc(sizeof(HcfRandSpiImpl), 0); + if (returnSpiImpl == NULL) { + LOGE("Failed to allocate *returnSpiImpl memory!"); + return HCF_ERR_MALLOC; + } + int32_t ret = MbedtlsRandInitEx(&(returnSpiImpl->entropy), &(returnSpiImpl->ctrDrbg)); + if (ret != HCF_SUCCESS) { + LOGE("Failed to allocate entropy ctrDrbg memory!"); + return HCF_ERR_MALLOC; + } + returnSpiImpl->base.base.getClass = GetMbedtlsRandClass; + returnSpiImpl->base.base.destroy = DestroyMbedtlsRand; + returnSpiImpl->base.engineGenerateRandom = MbedtlsGenerateRandom; + returnSpiImpl->base.engineSetSeed = MbedtlsSetSeed; + returnSpiImpl->base.engineGetAlgoName = MbedtlsGetRandAlgoName; + *spiObj = (HcfRandSpi *)returnSpiImpl; + + return HCF_SUCCESS; +} diff --git a/plugin/plugin.gni b/plugin/plugin.gni index 8a18e7fd4724e4e49bcaff0b76e6d8eb1333c900..2d7f0d17c7455f74f1af557df4211b3592dee626 100644 --- a/plugin/plugin.gni +++ b/plugin/plugin.gni @@ -96,3 +96,17 @@ plugin_files = plugin_asy_key_generator_files + plugin_key_agreement_files + plugin_sym_key_files + plugin_cipher_files + plugin_hmac_files + plugin_rand_files + plugin_md_files + plugin_signature_files + plugin_common_files + plugin_kdf_files + +mbedtls_plugin_inc_path = [ + "${base_path}/interfaces/innerkits/common", + "${plugin_path}/mbedtls_plugin/common", + "${plugin_path}/mbedtls_plugin/md/inc", + "${plugin_path}/mbedtls_plugin/rand/inc", + "//base/security/crypto_framework/frameworks/spi", + "//base/security/crypto_framework/common/inc", +] + +mbedtls_plugin_files = [ + "${plugin_path}/mbedtls_plugin/md/src/mbedtls_md.c", + "${plugin_path}/mbedtls_plugin/rand/src/mbedtls_rand.c", +] diff --git a/test/unittest/src/crypto_dsa_asy_key_generator_by_spec_test.cpp b/test/unittest/src/crypto_dsa_asy_key_generator_by_spec_test.cpp index 59b725f82bd8d3bbba65b53d50fe3487d30c3b39..a85af2f7a5a2e0ef0b89b85e18fdbed6cfd850b0 100644 --- a/test/unittest/src/crypto_dsa_asy_key_generator_by_spec_test.cpp +++ b/test/unittest/src/crypto_dsa_asy_key_generator_by_spec_test.cpp @@ -308,7 +308,7 @@ HWTEST_F(CryptoDsaAsyKeyGeneratorBySpecTest, CryptoDsaAsyKeyGeneratorBySpecTest1 const char *className = generator->base.getClass(); HcfObjDestroy(generator); - ASSERT_EQ(strcmp(className, g_asyKeyGeneratorBySpecClass), 0); + ASSERT_EQ(&className, &g_asyKeyGeneratorBySpecClass); } HWTEST_F(CryptoDsaAsyKeyGeneratorBySpecTest, CryptoDsaAsyKeyGeneratorBySpecTest102, TestSize.Level0) diff --git a/test/unittest/src/ecc/crypto_ecc_verify_sub_test.cpp b/test/unittest/src/ecc/crypto_ecc_verify_sub_test.cpp index 15e189c6f19c800cd04f64efcec4a088b2620825..689d510f8f842f53e29030fd7280fd6774ee61d4 100644 --- a/test/unittest/src/ecc/crypto_ecc_verify_sub_test.cpp +++ b/test/unittest/src/ecc/crypto_ecc_verify_sub_test.cpp @@ -1902,7 +1902,7 @@ HWTEST_F(CryptoEccVerifySubTest, CryptoEccVerifySubTest601, TestSize.Level0) ASSERT_EQ(flag, true); HcfObjDestroy(verify); - HcfBlobDataFree((HcfBlob *)&out); + OH_Crypto_FreeDataBlob(&out); uint32_t mallocCount = GetMallocNum(); MemoryMockTestFunc(mallocCount, &out); @@ -1961,7 +1961,7 @@ HWTEST_F(CryptoEccVerifySubTest, CryptoEccVerifySubTest602, TestSize.Level0) uint32_t mallocCount = GetOpensslCallNum(); OpensslMockTestFunc(mallocCount, &out); - HcfBlobDataFree((HcfBlob *)&out); + OH_Crypto_FreeDataBlob(&out); EndRecordOpensslCallNum(); } } diff --git a/test/unittest/src/sm2/crypto_sm2_asy_key_generator_by_spec_sub_test.cpp b/test/unittest/src/sm2/crypto_sm2_asy_key_generator_by_spec_sub_test.cpp index 9d9e456d2b812f371eff81ae7469f89b460c5284..2a726e276605ccff1e29913a4dd2d3ec6e6f4895 100644 --- a/test/unittest/src/sm2/crypto_sm2_asy_key_generator_by_spec_sub_test.cpp +++ b/test/unittest/src/sm2/crypto_sm2_asy_key_generator_by_spec_sub_test.cpp @@ -123,6 +123,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfFree(blob.data); HcfObjDestroy(pubKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPubKeySpec(reinterpret_cast(paramSpec)); } @@ -150,6 +151,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(pubKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPubKeySpec(reinterpret_cast(paramSpec)); } @@ -174,6 +176,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -198,6 +201,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -219,6 +223,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe ASSERT_NE(res, HCF_SUCCESS); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -240,6 +245,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe ASSERT_NE(res, HCF_SUCCESS); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -267,6 +273,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -291,6 +298,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe priKey->base.base.destroy((HcfObjectBase *)(&(priKey->base.base))); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -317,6 +325,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); + HcfFree(g_eccCommSpec); } HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTest075, TestSize.Level0) @@ -341,6 +350,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe priKey->base.base.destroy(&g_obj); HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -368,6 +378,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -393,6 +404,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe priKey->clearMem(priKey); HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -418,6 +430,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe priKey->clearMem(nullptr); HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -445,6 +458,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -472,6 +486,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -499,6 +514,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -526,6 +542,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -553,6 +570,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -581,6 +599,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); + HcfFree(g_eccCommSpec); } HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTest085, TestSize.Level0) @@ -610,6 +629,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfFree(blob.data); HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -642,6 +662,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfFree(blob.data); HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -673,6 +694,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfFree(blob.data); HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -700,6 +722,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); } @@ -753,6 +776,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(outKeyPair); HcfObjDestroy(generator); HcfObjDestroy(generatorBySpec); + HcfFree(g_eccCommSpec); DestroyEccKeyPairSpec(reinterpret_cast(paramSpec)); } @@ -802,6 +826,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(keyPair); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccKeyPairSpec(reinterpret_cast(paramSpec)); } @@ -861,6 +886,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(keyPair); HcfObjDestroy(generator); + HcfFree(g_eccCommSpec); DestroyEccKeyPairSpec(reinterpret_cast(paramSpec)); } @@ -889,7 +915,9 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe ASSERT_EQ(res, HCF_SUCCESS); HcfObjDestroy(spiObj); + HcfObjDestroy(keyPair); DestroyEccKeyPairSpec(reinterpret_cast(paramSpec)); + HcfFree(g_eccCommSpec); } HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTest093, TestSize.Level0) @@ -917,7 +945,9 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe ASSERT_EQ(res, HCF_SUCCESS); HcfObjDestroy(spiObj); + HcfObjDestroy(pubKey); DestroyEccKeyPairSpec(reinterpret_cast(paramSpec)); + HcfFree(g_eccCommSpec); } HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTest094, TestSize.Level0) @@ -945,7 +975,9 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe ASSERT_EQ(res, HCF_SUCCESS); HcfObjDestroy(spiObj); + HcfObjDestroy(priKey); DestroyEccKeyPairSpec(reinterpret_cast(paramSpec)); + HcfFree(g_eccCommSpec); } HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTest095, TestSize.Level0) @@ -972,7 +1004,9 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe ASSERT_EQ(res, HCF_SUCCESS); HcfObjDestroy(spiObj); + HcfObjDestroy(keyPair); FreeEccCommParamsSpec(reinterpret_cast(paramSpec)); + HcfFree(paramSpec); } HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTest096, TestSize.Level0) @@ -1005,6 +1039,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(priKey); HcfObjDestroy(generator); DestroyEccKeyPairSpec(reinterpret_cast(paramSpec)); + HcfFree(g_eccCommSpec); } HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTest097, TestSize.Level0) @@ -1034,6 +1069,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(pubKey); HcfObjDestroy(generator); DestroyEccKeyPairSpec(reinterpret_cast(paramSpec)); + HcfFree(g_eccCommSpec); } HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTest098, TestSize.Level0) @@ -1056,6 +1092,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe res = keyPair->priKey->getAsyKeySpecString(keyPair->priKey, item, &retStr); ASSERT_EQ(res, HCF_SUCCESS); ASSERT_NE(retStr, nullptr); + HcfFree(retStr); retStr = nullptr; res = keyPair->pubKey->getAsyKeySpecString(keyPair->pubKey, item, &retStr); ASSERT_EQ(res, HCF_SUCCESS); @@ -1064,6 +1101,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(keyPair); HcfObjDestroy(generator); FreeEccCommParamsSpec(reinterpret_cast(paramSpec)); + HcfFree(paramSpec); } HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTest099, TestSize.Level0) @@ -1088,6 +1126,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(keyPair); HcfObjDestroy(generator); FreeEccCommParamsSpec(reinterpret_cast(paramSpec)); + HcfFree(paramSpec); } HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTest100, TestSize.Level0) @@ -1122,6 +1161,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(keyPair); HcfObjDestroy(generator); DestroyEccKeyPairSpec(reinterpret_cast(paramSpec)); + HcfFree(g_eccCommSpec); } HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTest101, TestSize.Level0) @@ -1155,6 +1195,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(keyPair); HcfObjDestroy(generator); DestroyEccKeyPairSpec(reinterpret_cast(paramSpec)); + HcfFree(g_eccCommSpec); } static HcfResult ConstructSm2256KeyPairParamsSpecByGet(HcfEccKeyPairParamsSpec *eccKeyPairSpec, @@ -1273,6 +1314,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe HcfObjDestroy(generator); HcfObjDestroy(generatorSpec); FreeEccCommParamsSpec(reinterpret_cast(g_eccCommSpec)); + HcfFree(g_eccCommSpec); } static void OpensslMockTestFunc(uint32_t mallocCount, HcfAsyKeyParamsSpec *paramSpec) @@ -1359,6 +1401,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe uint32_t mallocCount = GetOpensslCallNum(); OpensslMockTestFunc(mallocCount, paramSpec); DestroyEccKeyPairSpec(reinterpret_cast(paramSpec)); + HcfFree(g_eccCommSpec); EndRecordOpensslCallNum(); } @@ -1410,6 +1453,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe uint32_t mallocCount = GetOpensslCallNum(); OpensslMockTestFunc1(mallocCount, paramSpec); FreeEccCommParamsSpec(reinterpret_cast(paramSpec)); + HcfFree(paramSpec); EndRecordOpensslCallNum(); } @@ -1462,6 +1506,7 @@ HWTEST_F(CryptoSm2AsyKeyGeneratorBySpecSubTest, CryptoSm2AsyKeyGeneratorBySpecTe uint32_t mallocCount = GetOpensslCallNum(); OpensslMockTestFunc2(mallocCount, paramSpec); DestroyEccPriKeySpec(reinterpret_cast(paramSpec)); + HcfFree(g_eccCommSpec); EndRecordOpensslCallNum(); }