diff --git a/frameworks/js/ani/BUILD.gn b/frameworks/js/ani/BUILD.gn index a203f23e977a0c6e9f10a5d4796946e21ef878c2..623d825dabc969409ddb3ce536d07602a53bbce2 100644 --- a/frameworks/js/ani/BUILD.gn +++ b/frameworks/js/ani/BUILD.gn @@ -59,6 +59,7 @@ taihe_shared_library("crypto_framework_ani") { "${framework_path}/js/ani/src/ani_pub_key.cpp", "${framework_path}/js/ani/src/ani_rand.cpp", "${framework_path}/js/ani/src/ani_sign.cpp", + "${framework_path}/js/ani/src/ani_signature_utils.cpp", "${framework_path}/js/ani/src/ani_sm2_crypto_util.cpp", "${framework_path}/js/ani/src/ani_sym_key.cpp", "${framework_path}/js/ani/src/ani_sym_key_generator.cpp", diff --git a/frameworks/js/ani/dts/cryptoFramework.d.ts b/frameworks/js/ani/dts/cryptoFramework.d.ts index 0750e32c96b2f8c1f6874011bd9cddca9f076222..f5efa385e12de4a4c9ca75fff3da3735572e1e4f 100644 --- a/frameworks/js/ani/dts/cryptoFramework.d.ts +++ b/frameworks/js/ani/dts/cryptoFramework.d.ts @@ -13,7 +13,6 @@ * limitations under the License. */ -import { int, long } from './@ohos.base'; import type { AsyncCallback } from './@ohos.base'; declare namespace cryptoFramework { @@ -22,6 +21,7 @@ declare namespace cryptoFramework { NOT_SUPPORT = 801, ERR_OUT_OF_MEMORY = 17620001, ERR_RUNTIME_ERROR = 17620002, + ERR_PARAMETER_CHECK_FAILED = 17620003, ERR_CRYPTO_OPERATION = 17630001 } @@ -474,6 +474,16 @@ declare namespace cryptoFramework { static genCipherTextBySpec(spec: SM2CipherTextSpec, mode?: string): DataBlob; static getCipherTextSpec(cipherText: DataBlob, mode?: string): SM2CipherTextSpec; } + + interface EccSignatureSpec { + r: bigint; + s: bigint; + } + + class SignatureUtils { + static genEccSignatureSpec(data: Uint8Array): EccSignatureSpec; + static genEccSignature(spec: EccSignatureSpec): Uint8Array; + } } export default cryptoFramework; diff --git a/frameworks/js/ani/idl/ohos.security.cryptoFramework.cryptoFramework.taihe b/frameworks/js/ani/idl/ohos.security.cryptoFramework.cryptoFramework.taihe index 21790fab8ebe092f6b8111732168ee002364d8f8..8ae1bbed4239b693f715ffba7d8987baa6264c09 100644 --- a/frameworks/js/ani/idl/ohos.security.cryptoFramework.cryptoFramework.taihe +++ b/frameworks/js/ani/idl/ohos.security.cryptoFramework.cryptoFramework.taihe @@ -24,6 +24,7 @@ enum Result: i32 { NOT_SUPPORT = 801, ERR_OUT_OF_MEMORY = 17620001, ERR_RUNTIME_ERROR = 17620002, + ERR_PARAMETER_CHECK_FAILED = 17620003, ERR_CRYPTO_OPERATION = 17630001 } @@ -596,14 +597,12 @@ interface KeyAgreement { function CreateKeyAgreement(algName: String): KeyAgreement; @class -interface DHKeyUtil { -} +interface DHKeyUtil {} @static("DHKeyUtil") function GenDHCommonParamsSpec(pLen: i32, skLen: Optional): DHCommonParamsSpec; @class -interface ECCKeyUtil { -} +interface ECCKeyUtil {} @static("ECCKeyUtil") function GenECCCommonParamsSpec(curveName: String): ECCCommonParamsSpec; @static("ECCKeyUtil") @@ -619,9 +618,20 @@ struct SM2CipherTextSpec { } @class -interface SM2CryptoUtil { -} +interface SM2CryptoUtil {} @static("SM2CryptoUtil") function GenCipherTextBySpec(spec: SM2CipherTextSpec, mode: Optional): DataBlob; @static("SM2CryptoUtil") function GetCipherTextSpec(cipherText: DataBlob, mode: Optional): SM2CipherTextSpec; + +struct EccSignatureSpec { + r: @bigint Array; + s: @bigint Array; +} + +@class +interface SignatureUtils {} +@static("SignatureUtils") +function GenEccSignatureSpec(data: @typedarray Array): EccSignatureSpec; +@static("SignatureUtils") +function GenEccSignature(spec: EccSignatureSpec): @typedarray Array; diff --git a/frameworks/js/ani/inc/ani_signature_utils.h b/frameworks/js/ani/inc/ani_signature_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..5a8d4098f9d067ffe06c6b6e119f6c6b7ddd62a5 --- /dev/null +++ b/frameworks/js/ani/inc/ani_signature_utils.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2025-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANI_SIGNATURE_UTILS_H +#define ANI_SIGNATURE_UTILS_H + +#include "ani_common.h" + +namespace ANI::CryptoFramework { +class SignatureUtilsImpl { +public: + SignatureUtilsImpl() = delete; + ~SignatureUtilsImpl() = delete; +}; +} // namespace ANI::CryptoFramework + +#endif // ANI_SIGNATURE_UTILS_H diff --git a/frameworks/js/ani/inc/ani_sym_key.h b/frameworks/js/ani/inc/ani_sym_key.h index e1a6210bbe4eeddfdfa877657a92aae210c58fbd..b54667521049ffcb0a5041994bf68d8b09d6c671 100644 --- a/frameworks/js/ani/inc/ani_sym_key.h +++ b/frameworks/js/ani/inc/ani_sym_key.h @@ -26,9 +26,9 @@ public: explicit SymKeyImpl(HcfSymKey *symKey); ~SymKeyImpl(); - int64_t GetKeyObj(); int64_t GetSymKeyObj(); void ClearMem(); + int64_t GetKeyObj(); DataBlob GetEncoded(); string GetFormat(); string GetAlgName(); diff --git a/frameworks/js/ani/src/ani_common.cpp b/frameworks/js/ani/src/ani_common.cpp index ccdff8afe571a30d2f250d9adc104b6d727819f7..a2babd0e1e579f25c0b70d90bea3a494a2d675ff 100644 --- a/frameworks/js/ani/src/ani_common.cpp +++ b/frameworks/js/ani/src/ani_common.cpp @@ -25,6 +25,7 @@ enum ResultCode { NOT_SUPPORT = 801, ERR_OUT_OF_MEMORY = 17620001, ERR_RUNTIME_ERROR = 17620002, + ERR_PARAMETER_CHECK_FAILED = 17620003, ERR_CRYPTO_OPERATION = 17630001, }; @@ -33,6 +34,7 @@ static const std::unordered_map RESULT_CODE = { { HCF_INVALID_PARAMS, INVALID_PARAMS }, { HCF_NOT_SUPPORT, NOT_SUPPORT }, { HCF_ERR_MALLOC, ERR_OUT_OF_MEMORY }, + { HCF_ERR_PARAMETER_CHECK_FAILED, ERR_PARAMETER_CHECK_FAILED }, { HCF_ERR_CRYPTO_OPERATION, ERR_CRYPTO_OPERATION }, }; diff --git a/frameworks/js/ani/src/ani_signature_utils.cpp b/frameworks/js/ani/src/ani_signature_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec7b9370286d496e315059f593d5d41d2b23ee0f --- /dev/null +++ b/frameworks/js/ani/src/ani_signature_utils.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2025-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ani_signature_utils.h" + +namespace ANI::CryptoFramework { +EccSignatureSpec GenEccSignatureSpec(array_view data) +{ + TH_THROW(std::runtime_error, "GenEccSignatureSpec not implemented"); +} + +array GenEccSignature(EccSignatureSpec const& spec) +{ + TH_THROW(std::runtime_error, "GenEccSignature not implemented"); +} +} // namespace ANI::CryptoFramework + +// Since these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_GenEccSignatureSpec(ANI::CryptoFramework::GenEccSignatureSpec); +TH_EXPORT_CPP_API_GenEccSignature(ANI::CryptoFramework::GenEccSignature); +// NOLINTEND diff --git a/frameworks/js/ani/src/ani_sym_key.cpp b/frameworks/js/ani/src/ani_sym_key.cpp index 3cfe429cbb360c3ef29ac023cc2ae188b31be9c7..71b2cdacb094e6e02d131f4077f99763c1158764 100644 --- a/frameworks/js/ani/src/ani_sym_key.cpp +++ b/frameworks/js/ani/src/ani_sym_key.cpp @@ -26,11 +26,6 @@ SymKeyImpl::~SymKeyImpl() this->symKey_ = nullptr; } -int64_t SymKeyImpl::GetKeyObj() -{ - return reinterpret_cast(&this->symKey_->key); -} - int64_t SymKeyImpl::GetSymKeyObj() { return reinterpret_cast(this->symKey_); @@ -45,6 +40,11 @@ void SymKeyImpl::ClearMem() this->symKey_->clearMem(this->symKey_); } +int64_t SymKeyImpl::GetKeyObj() +{ + return reinterpret_cast(&this->symKey_->key); +} + DataBlob SymKeyImpl::GetEncoded() { if (this->symKey_ == nullptr) { diff --git a/frameworks/js/ani/src/impl/ohos.security.cryptoFramework.cryptoFramework.impl.cpp b/frameworks/js/ani/src/impl/ohos.security.cryptoFramework.cryptoFramework.impl.cpp index c1205ea1dc2df0b454ef5c8e89cc612296a147fc..375b8f6be3dbc32bedb08b480c4fbe14fd407593 100644 --- a/frameworks/js/ani/src/impl/ohos.security.cryptoFramework.cryptoFramework.impl.cpp +++ b/frameworks/js/ani/src/impl/ohos.security.cryptoFramework.cryptoFramework.impl.cpp @@ -487,6 +487,13 @@ public: } }; +class SignatureUtilsImpl { +public: + SignatureUtilsImpl() { + // Don't forget to implement the constructor. + } +}; + Md CreateMd(string_view algName) { // The parameters in the make_holder function should be of the same type // as the parameters in the constructor of the actual implementation class. @@ -582,6 +589,14 @@ DataBlob GenCipherTextBySpec(SM2CipherTextSpec const& spec, optional_view mode) { TH_THROW(std::runtime_error, "GetCipherTextSpec not implemented"); } + +EccSignatureSpec GenEccSignatureSpec(array_view data) { + TH_THROW(std::runtime_error, "GenEccSignatureSpec not implemented"); +} + +array GenEccSignature(EccSignatureSpec const& spec) { + TH_THROW(std::runtime_error, "GenEccSignature not implemented"); +} } // namespace // Since these macros are auto-generate, lint will cause false positive. @@ -604,4 +619,6 @@ TH_EXPORT_CPP_API_ConvertPoint(ConvertPoint); TH_EXPORT_CPP_API_GetEncodedPoint(GetEncodedPoint); TH_EXPORT_CPP_API_GenCipherTextBySpec(GenCipherTextBySpec); TH_EXPORT_CPP_API_GetCipherTextSpec(GetCipherTextSpec); +TH_EXPORT_CPP_API_GenEccSignatureSpec(GenEccSignatureSpec); +TH_EXPORT_CPP_API_GenEccSignature(GenEccSignature); // NOLINTEND diff --git a/interfaces/inner_api/common/result.h b/interfaces/inner_api/common/result.h index 24088e20afeb9f8c77d64831db600191453c79cb..a0e72a9bc54555137b676be9a0164ab4539c17c0 100644 --- a/interfaces/inner_api/common/result.h +++ b/interfaces/inner_api/common/result.h @@ -28,6 +28,8 @@ typedef enum HcfResult { HCF_ERR_MALLOC = -20001, /** Indicates that call napi api fails. */ HCF_ERR_NAPI = -20002, + /** Indicates that parameter check failed. */ + HCF_ERR_PARAMETER_CHECK_FAILED = -20003, /** Indicates that third part has something wrong. */ HCF_ERR_CRYPTO_OPERATION = -30001,