From 01fd86e194c8116ab182264d1981891805150f74 Mon Sep 17 00:00:00 2001 From: guoxin <756090608@qq.com> Date: Sat, 17 Aug 2024 16:18:27 +0800 Subject: [PATCH] =?UTF-8?q?mini=E5=B9=B3=E5=8F=B0=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoxin <756090608@qq.com> --- BUILD.gn | 2 + bundle.json | 3 +- frameworks/BUILD.gn | 61 +++++++++-------- frameworks/js/liteapi/BUILD.gn | 32 +++++++++ frameworks/js/liteapi/inc/lite_api.h | 47 +++++++++++++ frameworks/js/liteapi/inc/lite_api_common.h | 32 +++++++++ frameworks/js/liteapi/inc/lite_api_errcode.h | 29 ++++++++ frameworks/js/liteapi/src/lite_api.cpp | 38 +++++++++++ frameworks/js/liteapi/src/lite_api_common.cpp | 35 ++++++++++ .../js/liteapi/src/lite_api_errcode.cpp | 31 +++++++++ .../js/liteapi/src/lite_mbedtls_md_item.cpp | 67 +++++++++++++++++++ .../js/liteapi/src/lite_mbedtls_rand_item.cpp | 53 +++++++++++++++ 12 files changed, 401 insertions(+), 29 deletions(-) create mode 100644 frameworks/js/liteapi/BUILD.gn create mode 100644 frameworks/js/liteapi/inc/lite_api.h create mode 100644 frameworks/js/liteapi/inc/lite_api_common.h create mode 100644 frameworks/js/liteapi/inc/lite_api_errcode.h create mode 100644 frameworks/js/liteapi/src/lite_api.cpp create mode 100644 frameworks/js/liteapi/src/lite_api_common.cpp create mode 100644 frameworks/js/liteapi/src/lite_api_errcode.cpp create mode 100644 frameworks/js/liteapi/src/lite_mbedtls_md_item.cpp create mode 100644 frameworks/js/liteapi/src/lite_mbedtls_rand_item.cpp diff --git a/BUILD.gn b/BUILD.gn index c7ff8f3..43ac77a 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -24,6 +24,8 @@ group("crypto_framework_component") { "frameworks/native:ohcrypto", "plugin:crypto_openssl_plugin_lib", ] + } else if (os_level == "mini") { + deps = [ "frameworks/js/liteapi:cryptoframework_liteapi" ] } } diff --git a/bundle.json b/bundle.json index 3fe7bf3..e0f37ed 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/frameworks/BUILD.gn b/frameworks/BUILD.gn index 11b713a..42dde7b 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -24,36 +24,41 @@ config("framework_config") { ] } -ohos_shared_library("crypto_framework_lib") { - 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") { + 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", - ] + cflags = [ + "-DHILOG_ENABLE", + "-fPIC", + "-Wall", + ] - deps = [ - "../common:crypto_plugin_common", - "../plugin:crypto_openssl_plugin_lib", - ] + deps = [ + "../common:crypto_plugin_common", + "../plugin:crypto_openssl_plugin_lib", + ] - external_deps = [ - "c_utils:utils", - "hilog:libhilog", - ] + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] + } +} else if (os_level == "mini") { + ohos_static_library("crypto_framework_lib") { + } } diff --git a/frameworks/js/liteapi/BUILD.gn b/frameworks/js/liteapi/BUILD.gn new file mode 100644 index 0000000..e069d95 --- /dev/null +++ b/frameworks/js/liteapi/BUILD.gn @@ -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. + +import("//build/lite/config/component/lite_component.gni") +import("//build/ohos.gni") + +ohos_static_library("cryptoframework_liteapi") { + subsystem_name = "security" + part_name = "crypto_framework" + include_dirs = [ + "inc", + "//base/security/crypto_framework/interfaces/innerkits/common/", + ] + sources = [ + "src/lite_api.cpp", + "src/lite_api_common.cpp", + "src/lite_api_errcode.cpp", + "src/lite_mbedtls_md_item.cpp", + "src/lite_mbedtls_rand_item.cpp", + ] + configs = [ "${product_path}:product_public_configs" ] +} diff --git a/frameworks/js/liteapi/inc/lite_api.h b/frameworks/js/liteapi/inc/lite_api.h new file mode 100644 index 0000000..8396e9f --- /dev/null +++ b/frameworks/js/liteapi/inc/lite_api.h @@ -0,0 +1,47 @@ +/* + * 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 LITE_API_H +#define LITE_API_H + +#include "jsi/jsi.h" +#include "jsi/jsi_types.h" + +namespace OHOS { +namespace ACELite { +class CryptoFrameworkLiteModule final : public MemoryHeap { +public: + CryptoFrameworkLiteModule() {} + ~CryptoFrameworkLiteModule() {} + + // Md + static JSIValue createMd(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + 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 createRandom(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum); + 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); +}; + +void InitCryptoFrameworkModule(JSIValue exports); + +} // namespace ACELite +} // namespace OHOS +#endif // LITE_API_H diff --git a/frameworks/js/liteapi/inc/lite_api_common.h b/frameworks/js/liteapi/inc/lite_api_common.h new file mode 100644 index 0000000..40207df --- /dev/null +++ b/frameworks/js/liteapi/inc/lite_api_common.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 LITE_API_COMMON_H +#define LITE_API_COMMON_H + +#include "jsi.h" +#include "jsi/jsi_types.h" + +namespace OHOS { +namespace ACELite { + +static const char *RESULT_ERROR_PROPERTY_CODE = "code"; +static const char *RESULT_ERROR_PROPERTY_MESSAGE = "message"; +void CallbackResultFailure(const JSIValue thisVal, const JSIValue callback, int32_t error, const char *errorMsg); +JSIValue GetErrorCodeInfo(int32_t errorCode); + +} // namespace ACELite +} // namespace OHOS +#endif // LITE_API_COMMON_H diff --git a/frameworks/js/liteapi/inc/lite_api_errcode.h b/frameworks/js/liteapi/inc/lite_api_errcode.h new file mode 100644 index 0000000..b29fb75 --- /dev/null +++ b/frameworks/js/liteapi/inc/lite_api_errcode.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 LITE_API_ERRCODE_H +#define LITE_API_ERRCODE_H + +#include "jsi/jsi.h" +#include "jsi/jsi_types.h" + +namespace OHOS { +namespace ACELite { + +JSIValue GetErrorCodeResultMsg(int32_t errorCode, const char *errorMsg); + +} // namespace ACELite +} // namespace OHOS +#endif // LITE_API_ERRCODE_H diff --git a/frameworks/js/liteapi/src/lite_api.cpp b/frameworks/js/liteapi/src/lite_api.cpp new file mode 100644 index 0000000..59940f9 --- /dev/null +++ b/frameworks/js/liteapi/src/lite_api.cpp @@ -0,0 +1,38 @@ +/* + * 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 "lite_api.h" +#include "jsi.h" + +namespace OHOS { +namespace ACELite { + +void InitCryptoFrameworkModule(JSIValue exports) +{ + JSI::SetModuleAPI(exports, "createMd", CryptoFrameworkLiteModule::createMd); + JSI::SetModuleAPI(exports, "update", CryptoFrameworkLiteModule::update); + JSI::SetModuleAPI(exports, "updateSync", CryptoFrameworkLiteModule::updateSync); + JSI::SetModuleAPI(exports, "digest", CryptoFrameworkLiteModule::digest); + JSI::SetModuleAPI(exports, "digestSync", CryptoFrameworkLiteModule::digestSync); + JSI::SetModuleAPI(exports, "getMdLength", CryptoFrameworkLiteModule::getMdLength); + + JSI::SetModuleAPI(exports, "createRandom", CryptoFrameworkLiteModule::createRandom); + JSI::SetModuleAPI(exports, "generateRandom", CryptoFrameworkLiteModule::generateRandom); + JSI::SetModuleAPI(exports, "generateRandomSync", CryptoFrameworkLiteModule::generateRandomSync); + JSI::SetModuleAPI(exports, "setSeed", CryptoFrameworkLiteModule::setSeed); +} + +} // ACELite +} // OHOS diff --git a/frameworks/js/liteapi/src/lite_api_common.cpp b/frameworks/js/liteapi/src/lite_api_common.cpp new file mode 100644 index 0000000..6f0da01 --- /dev/null +++ b/frameworks/js/liteapi/src/lite_api_common.cpp @@ -0,0 +1,35 @@ +/* + * 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 "lite_api_common.h" +#include "lite_api_errcode.h" + +namespace OHOS { +namespace ACELite { + +#define ARRAY_SIZE 2 +#define ARRAY_INDEX_ZERO 0 +#define ARRAY_INDEX_ONE 1 + +void CallbackResultFailure(const JSIValue thisVal, const JSIValue callback, int32_t error, const char *errorMsg) +{ + JSIValue params[ARRAY_SIZE] = {GetErrorCodeResultMsg(error, errorMsg), JSI::CreateNull()}; + JSI::CallFunction(callback, thisVal, params, (sizeof(params) / sizeof((params)[0]))); + JSI::ReleaseValue(params[ARRAY_INDEX_ZERO]); + JSI::ReleaseValue(params[ARRAY_INDEX_ONE]); +} + +} // ACELite +} // OHOS diff --git a/frameworks/js/liteapi/src/lite_api_errcode.cpp b/frameworks/js/liteapi/src/lite_api_errcode.cpp new file mode 100644 index 0000000..8bbfb0b --- /dev/null +++ b/frameworks/js/liteapi/src/lite_api_errcode.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 "lite_api_errcode.h" +#include "lite_api_common.h" + +namespace OHOS { +namespace ACELite { + +JSIValue GetErrorCodeResultMsg(int32_t errorCode, const char *errorMsg) +{ + JSIValue res = JSI::CreateObject(); + JSI::SetNumberProperty(res, RESULT_ERROR_PROPERTY_CODE, errorCode); + JSI::SetStringProperty(res, RESULT_ERROR_PROPERTY_MESSAGE, errorMsg); + return res; +} + +} // ACELite +} // OHOS diff --git a/frameworks/js/liteapi/src/lite_mbedtls_md_item.cpp b/frameworks/js/liteapi/src/lite_mbedtls_md_item.cpp new file mode 100644 index 0000000..c5e66b5 --- /dev/null +++ b/frameworks/js/liteapi/src/lite_mbedtls_md_item.cpp @@ -0,0 +1,67 @@ +/* + * 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 "lite_api.h" +#include "jsi.h" +#include "jsi_types.h" +#include "log.h" +#include "lite_api_common.h" + +namespace OHOS { +namespace ACELite { + +JSIValue CryptoFrameworkLiteModule::createMd(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + JSIValue error = JSI::CreateErrorWithCode(JSI_ERR_CODE_NOT_SUPPORTED, "interface not support"); + return error; +} + +JSIValue CryptoFrameworkLiteModule::update(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + JSIValue undefValue = JSI::CreateUndefined(); + CallbackResultFailure(thisVal, args[1], JSI_ERR_CODE_NOT_SUPPORTED, "interface not support"); + return undefValue; +} + +JSIValue CryptoFrameworkLiteModule::updateSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + JSIValue undefValue = JSI::CreateUndefined(); + CallbackResultFailure(thisVal, args[1], JSI_ERR_CODE_NOT_SUPPORTED, "interface not support"); + return undefValue; +} + +JSIValue CryptoFrameworkLiteModule::digest(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + JSIValue undefValue = JSI::CreateUndefined(); + CallbackResultFailure(thisVal, args[1], JSI_ERR_CODE_NOT_SUPPORTED, "interface not support"); + return undefValue; +} + +JSIValue CryptoFrameworkLiteModule::digestSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + JSIValue undefValue = JSI::CreateUndefined(); + CallbackResultFailure(thisVal, args[1], JSI_ERR_CODE_NOT_SUPPORTED, "interface not support"); + return undefValue; +} + +JSIValue CryptoFrameworkLiteModule::getMdLength(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + JSIValue undefValue = JSI::CreateUndefined(); + CallbackResultFailure(thisVal, args[1], JSI_ERR_CODE_NOT_SUPPORTED, "interface not support"); + return undefValue; +} + +} // ACELite +} // OHOS diff --git a/frameworks/js/liteapi/src/lite_mbedtls_rand_item.cpp b/frameworks/js/liteapi/src/lite_mbedtls_rand_item.cpp new file mode 100644 index 0000000..7718b06 --- /dev/null +++ b/frameworks/js/liteapi/src/lite_mbedtls_rand_item.cpp @@ -0,0 +1,53 @@ +/* + * 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 "lite_api.h" +#include "jsi.h" +#include "jsi_types.h" +#include "log.h" +#include "lite_api_common.h" + +namespace OHOS { +namespace ACELite { + +JSIValue CryptoFrameworkLiteModule::createRandom(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + JSIValue error = JSI::CreateErrorWithCode(JSI_ERR_CODE_NOT_SUPPORTED, "interface not support"); + return error; +} + +JSIValue CryptoFrameworkLiteModule::generateRandom(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + JSIValue undefValue = JSI::CreateUndefined(); + CallbackResultFailure(thisVal, args[1], JSI_ERR_CODE_NOT_SUPPORTED, "interface not support"); + return undefValue; +} + +JSIValue CryptoFrameworkLiteModule::generateRandomSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + JSIValue undefValue = JSI::CreateUndefined(); + CallbackResultFailure(thisVal, args[1], JSI_ERR_CODE_NOT_SUPPORTED, "interface not support"); + return undefValue; +} + +JSIValue CryptoFrameworkLiteModule::setSeed(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum) +{ + JSIValue undefValue = JSI::CreateUndefined(); + CallbackResultFailure(thisVal, args[1], JSI_ERR_CODE_NOT_SUPPORTED, "interface not support"); + return undefValue; +} + +} // ACELite +} // OHOS -- Gitee