From e78f432e44f3603ee7337e8ec93825bbb7946567 Mon Sep 17 00:00:00 2001 From: kang1024 Date: Fri, 25 Jul 2025 16:43:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A50702=E5=88=86=E6=94=AF?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8Darkts=E9=9D=99=E6=80=81=E5=8C=96?= =?UTF-8?q?=E5=B7=B2=E7=9F=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: kang1024 --- frameworks/js/ani/BUILD.gn | 1 + frameworks/js/ani/dts/cryptoFramework.d.ts | 13 +- ...rity.cryptoFramework.cryptoFramework.taihe | 22 +- frameworks/js/ani/inc/ani_common.h | 12 +- frameworks/js/ani/inc/ani_signature_utils.h | 29 ++ frameworks/js/ani/inc/ani_sym_key.h | 2 +- .../ani/src/ani_asy_key_generator_by_spec.cpp | 301 +++++++++--------- frameworks/js/ani/src/ani_common.cpp | 56 ++-- frameworks/js/ani/src/ani_dh_key_util.cpp | 6 +- frameworks/js/ani/src/ani_ecc_key_util.cpp | 25 +- frameworks/js/ani/src/ani_md.cpp | 2 +- frameworks/js/ani/src/ani_pri_key.cpp | 4 +- frameworks/js/ani/src/ani_pub_key.cpp | 4 +- frameworks/js/ani/src/ani_signature_utils.cpp | 36 +++ frameworks/js/ani/src/ani_sm2_crypto_util.cpp | 13 +- frameworks/js/ani/src/ani_sym_key.cpp | 10 +- ...y.cryptoFramework.cryptoFramework.impl.cpp | 17 + .../js/ani/test/test_asy_key_generator.ets | 106 +++--- .../test/test_asy_key_generator_by_spec.ets | 132 ++++---- frameworks/js/ani/test/test_cipher.ets | 26 +- frameworks/js/ani/test/test_dh_key_util.ets | 16 +- frameworks/js/ani/test/test_ecc_key_util.ets | 44 +-- frameworks/js/ani/test/test_kdf.ets | 24 +- frameworks/js/ani/test/test_key_agreement.ets | 12 +- frameworks/js/ani/test/test_mac.ets | 16 +- frameworks/js/ani/test/test_main.ets | 4 + frameworks/js/ani/test/test_md.ets | 18 +- frameworks/js/ani/test/test_rand.ets | 42 +-- frameworks/js/ani/test/test_sign.ets | 24 +- .../js/ani/test/test_sm2_crypto_util.ets | 22 +- .../js/ani/test/test_sym_key_generator.ets | 24 +- frameworks/js/ani/test/test_utils.ets | 8 +- frameworks/js/ani/test/test_verify.ets | 28 +- interfaces/inner_api/common/result.h | 2 + 34 files changed, 606 insertions(+), 495 deletions(-) create mode 100644 frameworks/js/ani/inc/ani_signature_utils.h create mode 100644 frameworks/js/ani/src/ani_signature_utils.cpp diff --git a/frameworks/js/ani/BUILD.gn b/frameworks/js/ani/BUILD.gn index a203f23..623d825 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 0750e32..8dfe765 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 } @@ -59,7 +59,6 @@ declare namespace cryptoFramework { cipherName: string; } - interface Key { getEncoded(): DataBlob; readonly format: string; @@ -474,6 +473,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 21790fa..8ae1bbe 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_common.h b/frameworks/js/ani/inc/ani_common.h index 8598be9..87abbc6 100644 --- a/frameworks/js/ani/inc/ani_common.h +++ b/frameworks/js/ani/inc/ani_common.h @@ -51,22 +51,24 @@ constexpr int SPEC_ITEM_TYPE_UINT8ARR = 4; #define ANI_LOGE_THROW(code, msg) \ do { \ - LOGE(msg); \ - set_business_error(ConvertResultCode(code), msg); \ + int rc = ConvertResultCode(code); \ + LOGE("%{public}s, code: %{public}d", msg, rc); \ + set_business_error(rc, msg); \ } while (0) int ConvertResultCode(HcfResult res); template -void ArrayU8ToDataBlob(const array &arr, T &blob); +void ArrayU8ToDataBlob(const T &arr, HcfBlob &blob); +void DataBlobToArrayU8(const HcfBlob &blob, array &arr); template -void DataBlobToArrayU8(const T &blob, array &arr); +bool ArrayU8ToBigInteger(const T &arr, HcfBigInteger &bigint); +void BigIntegerToArrayU8(const HcfBigInteger &bigint, array &arr); void StringToDataBlob(const string &str, HcfBlob &blob); int GetAsyKeySpecType(HcfAsyKeySpecItem item); - int GetSignSpecType(HcfSignSpecItem item); } // namespace ANI::CryptoFramework 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 0000000..5a8d409 --- /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 e1a6210..b546675 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_asy_key_generator_by_spec.cpp b/frameworks/js/ani/src/ani_asy_key_generator_by_spec.cpp index f551a92..0d9626b 100644 --- a/frameworks/js/ani/src/ani_asy_key_generator_by_spec.cpp +++ b/frameworks/js/ani/src/ani_asy_key_generator_by_spec.cpp @@ -26,6 +26,28 @@ namespace { using namespace ANI::CryptoFramework; +union AsyKeySpecUnion { + HcfDsaKeyPairParamsSpec dsaKeyPairSpec; + HcfDsaPubKeyParamsSpec dsaPubKeySpec; + HcfDsaCommParamsSpec dsaCommonParamsSpec; + HcfEccKeyPairParamsSpec eccKeyPairSpec; + HcfEccPubKeyParamsSpec eccPubKeySpec; + HcfEccPriKeyParamsSpec eccPriKeySpec; + HcfEccCommParamsSpec eccCommonParamsSpec; + HcfRsaKeyPairParamsSpec rsaKeyPairSpec; + HcfRsaPubKeyParamsSpec rsaPubKeySpec; + HcfAlg25519KeyPairParamsSpec ed25519KeyPairSpec; + HcfAlg25519PubKeyParamsSpec ed25519PubKeySpec; + HcfAlg25519PriKeyParamsSpec ed25519PriKeySpec; + HcfAlg25519KeyPairParamsSpec x25519KeyPairSpec; + HcfAlg25519PubKeyParamsSpec x25519PubKeySpec; + HcfAlg25519PriKeyParamsSpec x25519PriKeySpec; + HcfDhKeyPairParamsSpec dhKeyPairSpec; + HcfDhPubKeyParamsSpec dhPubKeySpec; + HcfDhPriKeyParamsSpec dhPriKeySpec; + HcfDhCommParamsSpec dhCommonParamsSpec; +}; + const std::string DSA_ALG_NAME = "DSA"; const std::string ECC_ALG_NAME = "ECC"; const std::string RSA_ALG_NAME = "RSA"; @@ -38,30 +60,30 @@ void SetDSAKeyPairParamsSpecAttribute(DSAKeyPairSpec const& dsaParams, HcfDsaKey { dsaKeyPairSpec.base.base.specType = static_cast(dsaParams.base.specType.get_value()); dsaKeyPairSpec.base.base.algName = const_cast(dsaParams.base.algName.c_str()); - ArrayU8ToDataBlob(dsaParams.params.p, dsaKeyPairSpec.base.p); - ArrayU8ToDataBlob(dsaParams.params.q, dsaKeyPairSpec.base.q); - ArrayU8ToDataBlob(dsaParams.params.g, dsaKeyPairSpec.base.g); - ArrayU8ToDataBlob(dsaParams.pk, dsaKeyPairSpec.pk); - ArrayU8ToDataBlob(dsaParams.sk, dsaKeyPairSpec.sk); + ArrayU8ToBigInteger(dsaParams.params.p, dsaKeyPairSpec.base.p); + ArrayU8ToBigInteger(dsaParams.params.q, dsaKeyPairSpec.base.q); + ArrayU8ToBigInteger(dsaParams.params.g, dsaKeyPairSpec.base.g); + ArrayU8ToBigInteger(dsaParams.pk, dsaKeyPairSpec.pk); + ArrayU8ToBigInteger(dsaParams.sk, dsaKeyPairSpec.sk); } void SetDSAPubKeyParamsSpecAttribute(DSAPubKeySpec const& dsaParams, HcfDsaPubKeyParamsSpec &dsaPubKeySpec) { dsaPubKeySpec.base.base.specType = static_cast(dsaParams.base.specType.get_value()); dsaPubKeySpec.base.base.algName = const_cast(dsaParams.base.algName.c_str()); - ArrayU8ToDataBlob(dsaParams.params.p, dsaPubKeySpec.base.p); - ArrayU8ToDataBlob(dsaParams.params.q, dsaPubKeySpec.base.q); - ArrayU8ToDataBlob(dsaParams.params.g, dsaPubKeySpec.base.g); - ArrayU8ToDataBlob(dsaParams.pk, dsaPubKeySpec.pk); + ArrayU8ToBigInteger(dsaParams.params.p, dsaPubKeySpec.base.p); + ArrayU8ToBigInteger(dsaParams.params.q, dsaPubKeySpec.base.q); + ArrayU8ToBigInteger(dsaParams.params.g, dsaPubKeySpec.base.g); + ArrayU8ToBigInteger(dsaParams.pk, dsaPubKeySpec.pk); } void SetDSACommonParamsSpecAttribute(DSACommonParamsSpec const& dsaParams, HcfDsaCommParamsSpec &dsaCommonParamsSpec) { dsaCommonParamsSpec.base.specType = static_cast(dsaParams.base.specType.get_value()); dsaCommonParamsSpec.base.algName = const_cast(dsaParams.base.algName.c_str()); - ArrayU8ToDataBlob(dsaParams.p, dsaCommonParamsSpec.p); - ArrayU8ToDataBlob(dsaParams.q, dsaCommonParamsSpec.q); - ArrayU8ToDataBlob(dsaParams.g, dsaCommonParamsSpec.g); + ArrayU8ToBigInteger(dsaParams.p, dsaCommonParamsSpec.p); + ArrayU8ToBigInteger(dsaParams.q, dsaCommonParamsSpec.q); + ArrayU8ToBigInteger(dsaParams.g, dsaCommonParamsSpec.g); } void SetECCKeyPairParamsSpecAttribute(ECCKeyPairSpec const& eccParams, HcfEccKeyPairParamsSpec &eccKeyPairSpec) @@ -75,17 +97,17 @@ void SetECCKeyPairParamsSpecAttribute(ECCKeyPairSpec const& eccParams, HcfEccKey HcfECFieldFp* fieldFp = reinterpret_cast(eccKeyPairSpec.base.field); fieldFp->base.fieldType = const_cast(eccParams.params.field.get_ECFIELDFP_ref().base.fieldType.c_str()); - ArrayU8ToDataBlob(eccParams.params.field.get_ECFIELDFP_ref().p, fieldFp->p); + ArrayU8ToBigInteger(eccParams.params.field.get_ECFIELDFP_ref().p, fieldFp->p); } eccKeyPairSpec.base.h = eccParams.params.h; - ArrayU8ToDataBlob(eccParams.params.a, eccKeyPairSpec.base.a); - ArrayU8ToDataBlob(eccParams.params.b, eccKeyPairSpec.base.b); - ArrayU8ToDataBlob(eccParams.params.g.x, eccKeyPairSpec.base.g.x); - ArrayU8ToDataBlob(eccParams.params.g.y, eccKeyPairSpec.base.g.y); - ArrayU8ToDataBlob(eccParams.params.n, eccKeyPairSpec.base.n); - ArrayU8ToDataBlob(eccParams.pk.x, eccKeyPairSpec.pk.x); - ArrayU8ToDataBlob(eccParams.pk.y, eccKeyPairSpec.pk.y); - ArrayU8ToDataBlob(eccParams.sk, eccKeyPairSpec.sk); + ArrayU8ToBigInteger(eccParams.params.a, eccKeyPairSpec.base.a); + ArrayU8ToBigInteger(eccParams.params.b, eccKeyPairSpec.base.b); + ArrayU8ToBigInteger(eccParams.params.g.x, eccKeyPairSpec.base.g.x); + ArrayU8ToBigInteger(eccParams.params.g.y, eccKeyPairSpec.base.g.y); + ArrayU8ToBigInteger(eccParams.params.n, eccKeyPairSpec.base.n); + ArrayU8ToBigInteger(eccParams.pk.x, eccKeyPairSpec.pk.x); + ArrayU8ToBigInteger(eccParams.pk.y, eccKeyPairSpec.pk.y); + ArrayU8ToBigInteger(eccParams.sk, eccKeyPairSpec.sk); } void SetECCPubKeyParamsSpecAttribute(ECCPubKeySpec const& eccParams, HcfEccPubKeyParamsSpec &eccPubKeySpec) @@ -99,16 +121,16 @@ void SetECCPubKeyParamsSpecAttribute(ECCPubKeySpec const& eccParams, HcfEccPubKe HcfECFieldFp* fieldFp = reinterpret_cast(eccPubKeySpec.base.field); fieldFp->base.fieldType = const_cast(eccParams.params.field.get_ECFIELDFP_ref().base.fieldType.c_str()); - ArrayU8ToDataBlob(eccParams.params.field.get_ECFIELDFP_ref().p, fieldFp->p); + ArrayU8ToBigInteger(eccParams.params.field.get_ECFIELDFP_ref().p, fieldFp->p); } eccPubKeySpec.base.h = eccParams.params.h; - ArrayU8ToDataBlob(eccParams.params.a, eccPubKeySpec.base.a); - ArrayU8ToDataBlob(eccParams.params.b, eccPubKeySpec.base.b); - ArrayU8ToDataBlob(eccParams.params.g.x, eccPubKeySpec.base.g.x); - ArrayU8ToDataBlob(eccParams.params.g.y, eccPubKeySpec.base.g.y); - ArrayU8ToDataBlob(eccParams.params.n, eccPubKeySpec.base.n); - ArrayU8ToDataBlob(eccParams.pk.x, eccPubKeySpec.pk.x); - ArrayU8ToDataBlob(eccParams.pk.y, eccPubKeySpec.pk.y); + ArrayU8ToBigInteger(eccParams.params.a, eccPubKeySpec.base.a); + ArrayU8ToBigInteger(eccParams.params.b, eccPubKeySpec.base.b); + ArrayU8ToBigInteger(eccParams.params.g.x, eccPubKeySpec.base.g.x); + ArrayU8ToBigInteger(eccParams.params.g.y, eccPubKeySpec.base.g.y); + ArrayU8ToBigInteger(eccParams.params.n, eccPubKeySpec.base.n); + ArrayU8ToBigInteger(eccParams.pk.x, eccPubKeySpec.pk.x); + ArrayU8ToBigInteger(eccParams.pk.y, eccPubKeySpec.pk.y); } void SetECCPriKeyParamsSpecAttribute(ECCPriKeySpec const& eccParams, HcfEccPriKeyParamsSpec &eccPriKeySpec) @@ -122,15 +144,15 @@ void SetECCPriKeyParamsSpecAttribute(ECCPriKeySpec const& eccParams, HcfEccPriKe HcfECFieldFp* fieldFp = reinterpret_cast(eccPriKeySpec.base.field); fieldFp->base.fieldType = const_cast(eccParams.params.field.get_ECFIELDFP_ref().base.fieldType.c_str()); - ArrayU8ToDataBlob(eccParams.params.field.get_ECFIELDFP_ref().p, fieldFp->p); + ArrayU8ToBigInteger(eccParams.params.field.get_ECFIELDFP_ref().p, fieldFp->p); } eccPriKeySpec.base.h = eccParams.params.h; - ArrayU8ToDataBlob(eccParams.params.a, eccPriKeySpec.base.a); - ArrayU8ToDataBlob(eccParams.params.b, eccPriKeySpec.base.b); - ArrayU8ToDataBlob(eccParams.params.g.x, eccPriKeySpec.base.g.x); - ArrayU8ToDataBlob(eccParams.params.g.y, eccPriKeySpec.base.g.y); - ArrayU8ToDataBlob(eccParams.params.n, eccPriKeySpec.base.n); - ArrayU8ToDataBlob(eccParams.sk, eccPriKeySpec.sk); + ArrayU8ToBigInteger(eccParams.params.a, eccPriKeySpec.base.a); + ArrayU8ToBigInteger(eccParams.params.b, eccPriKeySpec.base.b); + ArrayU8ToBigInteger(eccParams.params.g.x, eccPriKeySpec.base.g.x); + ArrayU8ToBigInteger(eccParams.params.g.y, eccPriKeySpec.base.g.y); + ArrayU8ToBigInteger(eccParams.params.n, eccPriKeySpec.base.n); + ArrayU8ToBigInteger(eccParams.sk, eccPriKeySpec.sk); } void SetECCCommonParamsSpecAttribute(ECCCommonParamsSpec const& eccParams, HcfEccCommParamsSpec &eccCommonParamsSpec) @@ -142,31 +164,31 @@ void SetECCCommonParamsSpecAttribute(ECCCommonParamsSpec const& eccParams, HcfEc } else if (eccParams.field.get_tag() == OptECField::tag_t::ECFIELDFP) { HcfECFieldFp* fieldFp = reinterpret_cast(eccCommonParamsSpec.field); fieldFp->base.fieldType = const_cast(eccParams.field.get_ECFIELDFP_ref().base.fieldType.c_str()); - ArrayU8ToDataBlob(eccParams.field.get_ECFIELDFP_ref().p, fieldFp->p); + ArrayU8ToBigInteger(eccParams.field.get_ECFIELDFP_ref().p, fieldFp->p); } eccCommonParamsSpec.h = eccParams.h; - ArrayU8ToDataBlob(eccParams.a, eccCommonParamsSpec.a); - ArrayU8ToDataBlob(eccParams.b, eccCommonParamsSpec.b); - ArrayU8ToDataBlob(eccParams.g.x, eccCommonParamsSpec.g.x); - ArrayU8ToDataBlob(eccParams.g.y, eccCommonParamsSpec.g.y); - ArrayU8ToDataBlob(eccParams.n, eccCommonParamsSpec.n); + ArrayU8ToBigInteger(eccParams.a, eccCommonParamsSpec.a); + ArrayU8ToBigInteger(eccParams.b, eccCommonParamsSpec.b); + ArrayU8ToBigInteger(eccParams.g.x, eccCommonParamsSpec.g.x); + ArrayU8ToBigInteger(eccParams.g.y, eccCommonParamsSpec.g.y); + ArrayU8ToBigInteger(eccParams.n, eccCommonParamsSpec.n); } void SetRSAKeyPairParamsSpecAttribute(RSAKeyPairSpec const& rsaParams, HcfRsaKeyPairParamsSpec &rsaKeyPairSpec) { rsaKeyPairSpec.base.base.specType = static_cast(rsaParams.base.specType.get_value()); rsaKeyPairSpec.base.base.algName = const_cast(rsaParams.base.algName.c_str()); - ArrayU8ToDataBlob(rsaParams.params.n, rsaKeyPairSpec.base.n); - ArrayU8ToDataBlob(rsaParams.pk, rsaKeyPairSpec.pk); - ArrayU8ToDataBlob(rsaParams.sk, rsaKeyPairSpec.sk); + ArrayU8ToBigInteger(rsaParams.params.n, rsaKeyPairSpec.base.n); + ArrayU8ToBigInteger(rsaParams.pk, rsaKeyPairSpec.pk); + ArrayU8ToBigInteger(rsaParams.sk, rsaKeyPairSpec.sk); } void SetRSAPubKeyParamsSpecAttribute(RSAPubKeySpec const& rsaParams, HcfRsaPubKeyParamsSpec &rsaPubKeySpec) { rsaPubKeySpec.base.base.specType = static_cast(rsaParams.base.specType.get_value()); rsaPubKeySpec.base.base.algName = const_cast(rsaParams.base.algName.c_str()); - ArrayU8ToDataBlob(rsaParams.params.n, rsaPubKeySpec.base.n); - ArrayU8ToDataBlob(rsaParams.pk, rsaPubKeySpec.pk); + ArrayU8ToBigInteger(rsaParams.params.n, rsaPubKeySpec.base.n); + ArrayU8ToBigInteger(rsaParams.pk, rsaPubKeySpec.pk); } void SetEd25519KeyPairParamsSpecAttribute(ED25519KeyPairSpec const& ed25519Params, @@ -174,8 +196,8 @@ void SetEd25519KeyPairParamsSpecAttribute(ED25519KeyPairSpec const& ed25519Param { ed25519KeyPairSpec.base.specType = static_cast(ed25519Params.base.specType.get_value()); ed25519KeyPairSpec.base.algName = const_cast(ed25519Params.base.algName.c_str()); - ArrayU8ToDataBlob(ed25519Params.pk, ed25519KeyPairSpec.pk); - ArrayU8ToDataBlob(ed25519Params.sk, ed25519KeyPairSpec.sk); + ArrayU8ToBigInteger(ed25519Params.pk, ed25519KeyPairSpec.pk); + ArrayU8ToBigInteger(ed25519Params.sk, ed25519KeyPairSpec.sk); } void SetEd25519PubKeyParamsSpecAttribute(ED25519PubKeySpec const& ed25519Params, @@ -183,7 +205,7 @@ void SetEd25519PubKeyParamsSpecAttribute(ED25519PubKeySpec const& ed25519Params, { ed25519PubKeySpec.base.specType = static_cast(ed25519Params.base.specType.get_value()); ed25519PubKeySpec.base.algName = const_cast(ed25519Params.base.algName.c_str()); - ArrayU8ToDataBlob(ed25519Params.pk, ed25519PubKeySpec.pk); + ArrayU8ToBigInteger(ed25519Params.pk, ed25519PubKeySpec.pk); } void SetEd25519PriKeyParamsSpecAttribute(ED25519PriKeySpec const& ed25519Params, @@ -191,7 +213,7 @@ void SetEd25519PriKeyParamsSpecAttribute(ED25519PriKeySpec const& ed25519Params, { ed25519PriKeySpec.base.specType = static_cast(ed25519Params.base.specType.get_value()); ed25519PriKeySpec.base.algName = const_cast(ed25519Params.base.algName.c_str()); - ArrayU8ToDataBlob(ed25519Params.sk, ed25519PriKeySpec.sk); + ArrayU8ToBigInteger(ed25519Params.sk, ed25519PriKeySpec.sk); } void SetX25519KeyPairParamsSpecAttribute(X25519KeyPairSpec const& x25519Params, @@ -199,8 +221,8 @@ void SetX25519KeyPairParamsSpecAttribute(X25519KeyPairSpec const& x25519Params, { x25519KeyPairSpec.base.specType = static_cast(x25519Params.base.specType.get_value()); x25519KeyPairSpec.base.algName = const_cast(x25519Params.base.algName.c_str()); - ArrayU8ToDataBlob(x25519Params.pk, x25519KeyPairSpec.pk); - ArrayU8ToDataBlob(x25519Params.sk, x25519KeyPairSpec.sk); + ArrayU8ToBigInteger(x25519Params.pk, x25519KeyPairSpec.pk); + ArrayU8ToBigInteger(x25519Params.sk, x25519KeyPairSpec.sk); } void SetX25519PubKeyParamsSpecAttribute(X25519PubKeySpec const& x25519Params, @@ -208,7 +230,7 @@ void SetX25519PubKeyParamsSpecAttribute(X25519PubKeySpec const& x25519Params, { x25519PubKeySpec.base.specType = static_cast(x25519Params.base.specType.get_value()); x25519PubKeySpec.base.algName = const_cast(x25519Params.base.algName.c_str()); - ArrayU8ToDataBlob(x25519Params.pk, x25519PubKeySpec.pk); + ArrayU8ToBigInteger(x25519Params.pk, x25519PubKeySpec.pk); } void SetX25519PriKeyParamsSpecAttribute(X25519PriKeySpec const& x25519Params, @@ -216,7 +238,7 @@ void SetX25519PriKeyParamsSpecAttribute(X25519PriKeySpec const& x25519Params, { x25519PriKeySpec.base.specType = static_cast(x25519Params.base.specType.get_value()); x25519PriKeySpec.base.algName = const_cast(x25519Params.base.algName.c_str()); - ArrayU8ToDataBlob(x25519Params.sk, x25519PriKeySpec.sk); + ArrayU8ToBigInteger(x25519Params.sk, x25519PriKeySpec.sk); } void SetDhKeyPairParamsSpecAttribute(DHKeyPairSpec const& dhParams, HcfDhKeyPairParamsSpec &dhKeyPairSpec) @@ -224,10 +246,10 @@ void SetDhKeyPairParamsSpecAttribute(DHKeyPairSpec const& dhParams, HcfDhKeyPair dhKeyPairSpec.base.base.specType = static_cast(dhParams.base.specType.get_value()); dhKeyPairSpec.base.base.algName = const_cast(dhParams.base.algName.c_str()); dhKeyPairSpec.base.length = dhParams.params.l; - ArrayU8ToDataBlob(dhParams.params.p, dhKeyPairSpec.base.p); - ArrayU8ToDataBlob(dhParams.params.g, dhKeyPairSpec.base.g); - ArrayU8ToDataBlob(dhParams.pk, dhKeyPairSpec.pk); - ArrayU8ToDataBlob(dhParams.sk, dhKeyPairSpec.sk); + ArrayU8ToBigInteger(dhParams.params.p, dhKeyPairSpec.base.p); + ArrayU8ToBigInteger(dhParams.params.g, dhKeyPairSpec.base.g); + ArrayU8ToBigInteger(dhParams.pk, dhKeyPairSpec.pk); + ArrayU8ToBigInteger(dhParams.sk, dhKeyPairSpec.sk); } void SetDhPubKeyParamsSpecAttribute(DHPubKeySpec const& dhParams, HcfDhPubKeyParamsSpec &dhPubKeySpec) @@ -235,9 +257,9 @@ void SetDhPubKeyParamsSpecAttribute(DHPubKeySpec const& dhParams, HcfDhPubKeyPar dhPubKeySpec.base.base.specType = static_cast(dhParams.base.specType.get_value()); dhPubKeySpec.base.base.algName = const_cast(dhParams.base.algName.c_str()); dhPubKeySpec.base.length = dhParams.params.l; - ArrayU8ToDataBlob(dhParams.params.p, dhPubKeySpec.base.p); - ArrayU8ToDataBlob(dhParams.params.g, dhPubKeySpec.base.g); - ArrayU8ToDataBlob(dhParams.pk, dhPubKeySpec.pk); + ArrayU8ToBigInteger(dhParams.params.p, dhPubKeySpec.base.p); + ArrayU8ToBigInteger(dhParams.params.g, dhPubKeySpec.base.g); + ArrayU8ToBigInteger(dhParams.pk, dhPubKeySpec.pk); } void SetDhPriKeyParamsSpecAttribute(DHPriKeySpec const& dhParams, HcfDhPriKeyParamsSpec &dhPriKeySpec) @@ -245,9 +267,9 @@ void SetDhPriKeyParamsSpecAttribute(DHPriKeySpec const& dhParams, HcfDhPriKeyPar dhPriKeySpec.base.base.specType = static_cast(dhParams.base.specType.get_value()); dhPriKeySpec.base.base.algName = const_cast(dhParams.base.algName.c_str()); dhPriKeySpec.base.length = dhParams.params.l; - ArrayU8ToDataBlob(dhParams.params.p, dhPriKeySpec.base.p); - ArrayU8ToDataBlob(dhParams.params.g, dhPriKeySpec.base.g); - ArrayU8ToDataBlob(dhParams.sk, dhPriKeySpec.sk); + ArrayU8ToBigInteger(dhParams.params.p, dhPriKeySpec.base.p); + ArrayU8ToBigInteger(dhParams.params.g, dhPriKeySpec.base.g); + ArrayU8ToBigInteger(dhParams.sk, dhPriKeySpec.sk); } void SetDhCommonParamsSpecAttribute(DHCommonParamsSpec const& dhParams, HcfDhCommParamsSpec &dhCommonParamsSpec) @@ -255,69 +277,57 @@ void SetDhCommonParamsSpecAttribute(DHCommonParamsSpec const& dhParams, HcfDhCom dhCommonParamsSpec.base.specType = static_cast(dhParams.base.specType.get_value()); dhCommonParamsSpec.base.algName = const_cast(dhParams.base.algName.c_str()); dhCommonParamsSpec.length = dhParams.l; - ArrayU8ToDataBlob(dhParams.p, dhCommonParamsSpec.p); - ArrayU8ToDataBlob(dhParams.g, dhCommonParamsSpec.g); + ArrayU8ToBigInteger(dhParams.p, dhCommonParamsSpec.p); + ArrayU8ToBigInteger(dhParams.g, dhCommonParamsSpec.g); } -HcfAsyKeyParamsSpec* CreateDSASpec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateDSASpec(OptAsyKeySpec const& asyKeySpec, AsyKeySpecUnion &asyKeySpecUnion) { - static HcfDsaKeyPairParamsSpec dsaKeyPairSpec = {}; - static HcfDsaPubKeyParamsSpec dsaPubKeySpec = {}; - static HcfDsaCommParamsSpec dsaCommonParamsSpec = {}; - if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DSAKEYPAIRSPEC) { - SetDSAKeyPairParamsSpecAttribute(asyKeySpec.get_DSAKEYPAIRSPEC_ref(), dsaKeyPairSpec); - return reinterpret_cast(&dsaKeyPairSpec); + SetDSAKeyPairParamsSpecAttribute(asyKeySpec.get_DSAKEYPAIRSPEC_ref(), asyKeySpecUnion.dsaKeyPairSpec); + return reinterpret_cast(&asyKeySpecUnion.dsaKeyPairSpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DSAPUBKEYSPEC) { - SetDSAPubKeyParamsSpecAttribute(asyKeySpec.get_DSAPUBKEYSPEC_ref(), dsaPubKeySpec); - return reinterpret_cast(&dsaPubKeySpec); + SetDSAPubKeyParamsSpecAttribute(asyKeySpec.get_DSAPUBKEYSPEC_ref(), asyKeySpecUnion.dsaPubKeySpec); + return reinterpret_cast(&asyKeySpecUnion.dsaPubKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DSACOMMONPARAMSSPEC) { - SetDSACommonParamsSpecAttribute(asyKeySpec.get_DSACOMMONPARAMSSPEC_ref(), dsaCommonParamsSpec); - return reinterpret_cast(&dsaCommonParamsSpec); + SetDSACommonParamsSpecAttribute(asyKeySpec.get_DSACOMMONPARAMSSPEC_ref(), asyKeySpecUnion.dsaCommonParamsSpec); + return reinterpret_cast(&asyKeySpecUnion.dsaCommonParamsSpec); } return nullptr; } -HcfAsyKeyParamsSpec* CreateECCSpec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateECCSpec(OptAsyKeySpec const& asyKeySpec, AsyKeySpecUnion &asyKeySpecUnion, + HcfECFieldFp &ecFieldFp) { - static HcfEccKeyPairParamsSpec eccKeyPairSpec = {}; - static HcfEccPubKeyParamsSpec eccPubKeySpec = {}; - static HcfEccPriKeyParamsSpec eccPriKeySpec = {}; - static HcfEccCommParamsSpec eccCommonParamsSpec = {}; - static HcfECFieldFp ecFieldFp = {}; - static HcfECField* ecField = &ecFieldFp.base; - + HcfECField *ecField = &ecFieldFp.base; if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ECCKEYPAIRSPEC) { - eccKeyPairSpec.base.field = ecField; - SetECCKeyPairParamsSpecAttribute(asyKeySpec.get_ECCKEYPAIRSPEC_ref(), eccKeyPairSpec); - return reinterpret_cast(&eccKeyPairSpec); + asyKeySpecUnion.eccKeyPairSpec.base.field = ecField; + SetECCKeyPairParamsSpecAttribute(asyKeySpec.get_ECCKEYPAIRSPEC_ref(), asyKeySpecUnion.eccKeyPairSpec); + return reinterpret_cast(&asyKeySpecUnion.eccKeyPairSpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ECCPUBKEYSPEC) { - eccPubKeySpec.base.field = ecField; - SetECCPubKeyParamsSpecAttribute(asyKeySpec.get_ECCPUBKEYSPEC_ref(), eccPubKeySpec); - return reinterpret_cast(&eccPubKeySpec); + asyKeySpecUnion.eccPubKeySpec.base.field = ecField; + SetECCPubKeyParamsSpecAttribute(asyKeySpec.get_ECCPUBKEYSPEC_ref(), asyKeySpecUnion.eccPubKeySpec); + return reinterpret_cast(&asyKeySpecUnion.eccPubKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ECCPRIKEYSPEC) { - eccPriKeySpec.base.field = ecField; - SetECCPriKeyParamsSpecAttribute(asyKeySpec.get_ECCPRIKEYSPEC_ref(), eccPriKeySpec); - return reinterpret_cast(&eccPriKeySpec); + asyKeySpecUnion.eccPriKeySpec.base.field = ecField; + SetECCPriKeyParamsSpecAttribute(asyKeySpec.get_ECCPRIKEYSPEC_ref(), asyKeySpecUnion.eccPriKeySpec); + return reinterpret_cast(&asyKeySpecUnion.eccPriKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ECCCOMMONPARAMSSPEC) { - eccCommonParamsSpec.field = ecField; - SetECCCommonParamsSpecAttribute(asyKeySpec.get_ECCCOMMONPARAMSSPEC_ref(), eccCommonParamsSpec); - return reinterpret_cast(&eccCommonParamsSpec); + asyKeySpecUnion.eccCommonParamsSpec.field = ecField; + SetECCCommonParamsSpecAttribute(asyKeySpec.get_ECCCOMMONPARAMSSPEC_ref(), asyKeySpecUnion.eccCommonParamsSpec); + return reinterpret_cast(&asyKeySpecUnion.eccCommonParamsSpec); } return nullptr; } -HcfAsyKeyParamsSpec* CreateRSASpec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateRSASpec(OptAsyKeySpec const& asyKeySpec, AsyKeySpecUnion &asyKeySpecUnion) { - static HcfRsaKeyPairParamsSpec rsaKeyPairSpec = {}; - static HcfRsaPubKeyParamsSpec rsaPubKeySpec = {}; - if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::RSAKEYPAIRSPEC) { - SetRSAKeyPairParamsSpecAttribute(asyKeySpec.get_RSAKEYPAIRSPEC_ref(), rsaKeyPairSpec); - return reinterpret_cast(&rsaKeyPairSpec); + SetRSAKeyPairParamsSpecAttribute(asyKeySpec.get_RSAKEYPAIRSPEC_ref(), asyKeySpecUnion.rsaKeyPairSpec); + return reinterpret_cast(&asyKeySpecUnion.rsaKeyPairSpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::RSAPUBKEYSPEC) { - SetRSAPubKeyParamsSpecAttribute(asyKeySpec.get_RSAPUBKEYSPEC_ref(), rsaPubKeySpec); - return reinterpret_cast(&rsaPubKeySpec); + SetRSAPubKeyParamsSpecAttribute(asyKeySpec.get_RSAPUBKEYSPEC_ref(), asyKeySpecUnion.rsaPubKeySpec); + return reinterpret_cast(&asyKeySpecUnion.rsaPubKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::RSACOMMONPARAMSSPEC) { LOGE("RSA not support comm key spec"); return nullptr; @@ -325,82 +335,71 @@ HcfAsyKeyParamsSpec* CreateRSASpec(OptAsyKeySpec const& asyKeySpec) return nullptr; } -HcfAsyKeyParamsSpec* CreateEd25519Spec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateEd25519Spec(OptAsyKeySpec const& asyKeySpec, AsyKeySpecUnion &asyKeySpecUnion) { - static HcfAlg25519KeyPairParamsSpec ed25519KeyPairSpec = {}; - static HcfAlg25519PubKeyParamsSpec ed25519PubKeySpec = {}; - static HcfAlg25519PriKeyParamsSpec ed25519PriKeySpec = {}; - if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ED25519KEYPAIRSPEC) { - SetEd25519KeyPairParamsSpecAttribute(asyKeySpec.get_ED25519KEYPAIRSPEC_ref(), ed25519KeyPairSpec); - return reinterpret_cast(&ed25519KeyPairSpec); + SetEd25519KeyPairParamsSpecAttribute(asyKeySpec.get_ED25519KEYPAIRSPEC_ref(), + asyKeySpecUnion.ed25519KeyPairSpec); + return reinterpret_cast(&asyKeySpecUnion.ed25519KeyPairSpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ED25519PUBKEYSPEC) { - SetEd25519PubKeyParamsSpecAttribute(asyKeySpec.get_ED25519PUBKEYSPEC_ref(), ed25519PubKeySpec); - return reinterpret_cast(&ed25519PubKeySpec); + SetEd25519PubKeyParamsSpecAttribute(asyKeySpec.get_ED25519PUBKEYSPEC_ref(), asyKeySpecUnion.ed25519PubKeySpec); + return reinterpret_cast(&asyKeySpecUnion.ed25519PubKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::ED25519PRIKEYSPEC) { - SetEd25519PriKeyParamsSpecAttribute(asyKeySpec.get_ED25519PRIKEYSPEC_ref(), ed25519PriKeySpec); - return reinterpret_cast(&ed25519PriKeySpec); + SetEd25519PriKeyParamsSpecAttribute(asyKeySpec.get_ED25519PRIKEYSPEC_ref(), asyKeySpecUnion.ed25519PriKeySpec); + return reinterpret_cast(&asyKeySpecUnion.ed25519PriKeySpec); } return nullptr; } -HcfAsyKeyParamsSpec* CreateX25519Spec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateX25519Spec(OptAsyKeySpec const& asyKeySpec, AsyKeySpecUnion &asyKeySpecUnion) { - static HcfAlg25519KeyPairParamsSpec x25519KeyPairSpec = {}; - static HcfAlg25519PubKeyParamsSpec x25519PubKeySpec = {}; - static HcfAlg25519PriKeyParamsSpec x25519PriKeySpec = {}; - if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::X25519KEYPAIRSPEC) { - SetX25519KeyPairParamsSpecAttribute(asyKeySpec.get_X25519KEYPAIRSPEC_ref(), x25519KeyPairSpec); - return reinterpret_cast(&x25519KeyPairSpec); + SetX25519KeyPairParamsSpecAttribute(asyKeySpec.get_X25519KEYPAIRSPEC_ref(), asyKeySpecUnion.x25519KeyPairSpec); + return reinterpret_cast(&asyKeySpecUnion.x25519KeyPairSpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::X25519PUBKEYSPEC) { - SetX25519PubKeyParamsSpecAttribute(asyKeySpec.get_X25519PUBKEYSPEC_ref(), x25519PubKeySpec); - return reinterpret_cast(&x25519PubKeySpec); + SetX25519PubKeyParamsSpecAttribute(asyKeySpec.get_X25519PUBKEYSPEC_ref(), asyKeySpecUnion.x25519PubKeySpec); + return reinterpret_cast(&asyKeySpecUnion.x25519PubKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::X25519PRIKEYSPEC) { - SetX25519PriKeyParamsSpecAttribute(asyKeySpec.get_X25519PRIKEYSPEC_ref(), x25519PriKeySpec); - return reinterpret_cast(&x25519PriKeySpec); + SetX25519PriKeyParamsSpecAttribute(asyKeySpec.get_X25519PRIKEYSPEC_ref(), asyKeySpecUnion.x25519PriKeySpec); + return reinterpret_cast(&asyKeySpecUnion.x25519PriKeySpec); } return nullptr; } -HcfAsyKeyParamsSpec* CreateDHSpec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateDHSpec(OptAsyKeySpec const& asyKeySpec, AsyKeySpecUnion &asyKeySpecUnion) { - static HcfDhKeyPairParamsSpec dhKeyPairSpec = {}; - static HcfDhPubKeyParamsSpec dhPubKeySpec = {}; - static HcfDhPriKeyParamsSpec dhPriKeySpec = {}; - static HcfDhCommParamsSpec dhCommonParamsSpec = {}; - if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DHKEYPAIRSPEC) { - SetDhKeyPairParamsSpecAttribute(asyKeySpec.get_DHKEYPAIRSPEC_ref(), dhKeyPairSpec); - return reinterpret_cast(&dhKeyPairSpec); + SetDhKeyPairParamsSpecAttribute(asyKeySpec.get_DHKEYPAIRSPEC_ref(), asyKeySpecUnion.dhKeyPairSpec); + return reinterpret_cast(&asyKeySpecUnion.dhKeyPairSpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DHPUBKEYSPEC) { - SetDhPubKeyParamsSpecAttribute(asyKeySpec.get_DHPUBKEYSPEC_ref(), dhPubKeySpec); - return reinterpret_cast(&dhPubKeySpec); + SetDhPubKeyParamsSpecAttribute(asyKeySpec.get_DHPUBKEYSPEC_ref(), asyKeySpecUnion.dhPubKeySpec); + return reinterpret_cast(&asyKeySpecUnion.dhPubKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DHPRIKEYSPEC) { - SetDhPriKeyParamsSpecAttribute(asyKeySpec.get_DHPRIKEYSPEC_ref(), dhPriKeySpec); - return reinterpret_cast(&dhPriKeySpec); + SetDhPriKeyParamsSpecAttribute(asyKeySpec.get_DHPRIKEYSPEC_ref(), asyKeySpecUnion.dhPriKeySpec); + return reinterpret_cast(&asyKeySpecUnion.dhPriKeySpec); } else if (asyKeySpec.get_tag() == OptAsyKeySpec::tag_t::DHCOMMONPARAMSSPEC) { - SetDhCommonParamsSpecAttribute(asyKeySpec.get_DHCOMMONPARAMSSPEC_ref(), dhCommonParamsSpec); - return reinterpret_cast(&dhCommonParamsSpec); + SetDhCommonParamsSpecAttribute(asyKeySpec.get_DHCOMMONPARAMSSPEC_ref(), asyKeySpecUnion.dhCommonParamsSpec); + return reinterpret_cast(&asyKeySpecUnion.dhCommonParamsSpec); } return nullptr; } -HcfAsyKeyParamsSpec* CreateParamsSpec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateParamsSpec(OptAsyKeySpec const& asyKeySpec, AsyKeySpecUnion &asyKeySpecUnion, + HcfECFieldFp &ecFieldFp) { const std::string &algName = asyKeySpec.get_ASYKEYSPEC_ref().algName.c_str(); if (algName == DSA_ALG_NAME) { - return CreateDSASpec(asyKeySpec); + return CreateDSASpec(asyKeySpec, asyKeySpecUnion); } else if (algName == ECC_ALG_NAME || algName == SM2_ALG_NAME) { - return CreateECCSpec(asyKeySpec); + return CreateECCSpec(asyKeySpec, asyKeySpecUnion, ecFieldFp); } else if (algName == RSA_ALG_NAME) { - return CreateRSASpec(asyKeySpec); + return CreateRSASpec(asyKeySpec, asyKeySpecUnion); } else if (algName == ED25519_ALG_NAME) { - return CreateEd25519Spec(asyKeySpec); + return CreateEd25519Spec(asyKeySpec, asyKeySpecUnion); } else if (algName == X25519_ALG_NAME) { - return CreateX25519Spec(asyKeySpec); + return CreateX25519Spec(asyKeySpec, asyKeySpecUnion); } else if (algName == DH_ALG_NAME) { - return CreateDHSpec(asyKeySpec); + return CreateDHSpec(asyKeySpec, asyKeySpecUnion); } return nullptr; } @@ -474,7 +473,9 @@ string AsyKeyGeneratorBySpecImpl::GetAlgName() AsyKeyGeneratorBySpec CreateAsyKeyGeneratorBySpec(OptAsyKeySpec const& asyKeySpec) { - HcfAsyKeyParamsSpec *spec = CreateParamsSpec(asyKeySpec); + AsyKeySpecUnion asyKeySpecUnion = {}; + HcfECFieldFp ecFieldFp = {}; + HcfAsyKeyParamsSpec *spec = CreateParamsSpec(asyKeySpec, asyKeySpecUnion, ecFieldFp); if (spec == nullptr) { ANI_LOGE_THROW(HCF_INVALID_PARAMS, "invalid asy key spec!"); return make_holder(); diff --git a/frameworks/js/ani/src/ani_common.cpp b/frameworks/js/ani/src/ani_common.cpp index 5d21345..6ca979e 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 }, }; @@ -80,6 +82,18 @@ static const std::unordered_map SIGN_SPEC_RELATION_MAP = { } // namespace namespace ANI::CryptoFramework { +// template specialization for ArrayU8ToDataBlob +template +void ArrayU8ToDataBlob>(const array &arr, HcfBlob &blob); +template +void ArrayU8ToDataBlob>(const array_view &arr, HcfBlob &blob); + +// template specialization for ArrayU8ToBigInteger +template +bool ArrayU8ToBigInteger>(const array &arr, HcfBigInteger &bigint); +template +bool ArrayU8ToBigInteger>(const array_view &arr, HcfBigInteger &bigint); + int ConvertResultCode(HcfResult res) { if (RESULT_CODE.count(res) > 0) { @@ -88,42 +102,48 @@ int ConvertResultCode(HcfResult res) return ERR_RUNTIME_ERROR; } -template<> -void ArrayU8ToDataBlob(const array &arr, HcfBlob &blob) +template +void ArrayU8ToDataBlob(const T &arr, HcfBlob &blob) { blob.data = arr.empty() ? nullptr : arr.data(); blob.len = arr.size(); } -template<> -void ArrayU8ToDataBlob(const array &arr, HcfBigInteger &blob) +void DataBlobToArrayU8(const HcfBlob &blob, array &arr) { - blob.data = arr.empty() ? nullptr : arr.data(); - blob.len = arr.size(); - if (blob.len > 0 && blob.data[blob.len - 1] == 0) { // remove the sign bit of big integer - blob.len--; - } + arr = array(move_data_t{}, blob.data, blob.len); } -template<> -void DataBlobToArrayU8(const HcfBlob &blob, array &arr) +template +bool ArrayU8ToBigInteger(const T &arr, HcfBigInteger &bigint) { - arr = array(move_data_t{}, blob.data, blob.len); + if (arr.empty()) { + return false; + } + uint8_t sign = arr.back() >> (sizeof(uint8_t) * 8 - 1); + if (sign != 0) { // not support negative of big integer + return false; + } + bigint.data = arr.data(); + bigint.len = arr.size(); + if (bigint.len > 1 && bigint.data[bigint.len - 1] == 0) { // remove the sign bit of big integer + bigint.len--; + } + return true; } -template<> -void DataBlobToArrayU8(const HcfBigInteger &blob, array &arr) +void BigIntegerToArrayU8(const HcfBigInteger &bigint, array &arr) { - arr = array(blob.len + 1); - std::copy(blob.data, blob.data + blob.len, arr.data()); + arr = array(bigint.len + 1); + std::copy(bigint.data, bigint.data + bigint.len, arr.data()); // 0x00 is the sign bit of big integer, it's always a positive number in this implementation - arr[blob.len] = 0x00; + arr[bigint.len] = 0x00; } void StringToDataBlob(const string &str, HcfBlob &blob) { blob.data = str.empty() ? nullptr : reinterpret_cast(const_cast(str.c_str())); - blob.len = str.size(); + blob.len = str.empty() ? 0 : str.size() + 1; } int GetAsyKeySpecType(HcfAsyKeySpecItem item) diff --git a/frameworks/js/ani/src/ani_dh_key_util.cpp b/frameworks/js/ani/src/ani_dh_key_util.cpp index 4ba57df..740f1d9 100644 --- a/frameworks/js/ani/src/ani_dh_key_util.cpp +++ b/frameworks/js/ani/src/ani_dh_key_util.cpp @@ -29,14 +29,14 @@ DHCommonParamsSpec GenDHCommonParamsSpec(int32_t pLen, optional_view sk HcfDhCommParamsSpec *dhCommParamsSpec = nullptr; HcfResult res = HcfDhKeyUtilCreate(pLen, skLenValue, &dhCommParamsSpec); if (res != HCF_SUCCESS) { - ANI_LOGE_THROW(res, "create dhKey obj fail!"); + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "create dhKey obj fail!"); // the error code is consistent with 1.1 return dh; } dh.base.algName = string(dhCommParamsSpec->base.algName); dh.base.specType = AsyKeySpecType(static_cast(dhCommParamsSpec->base.specType)); dh.l = dhCommParamsSpec->length; - DataBlobToArrayU8(dhCommParamsSpec->p, dh.p); - DataBlobToArrayU8(dhCommParamsSpec->g, dh.g); + BigIntegerToArrayU8(dhCommParamsSpec->p, dh.p); + BigIntegerToArrayU8(dhCommParamsSpec->g, dh.g); HcfObjDestroy(dhCommParamsSpec); return dh; } diff --git a/frameworks/js/ani/src/ani_ecc_key_util.cpp b/frameworks/js/ani/src/ani_ecc_key_util.cpp index 3ab8756..e2ab9c3 100644 --- a/frameworks/js/ani/src/ani_ecc_key_util.cpp +++ b/frameworks/js/ani/src/ani_ecc_key_util.cpp @@ -34,16 +34,16 @@ ECCCommonParamsSpec GenECCCommonParamsSpec(string_view curveName) } HcfECFieldFp *tmp = reinterpret_cast(eccCommParamsSpec->field); ecFieldFp.base.fieldType = string(tmp->base.fieldType); - DataBlobToArrayU8(tmp->p, ecFieldFp.p); + BigIntegerToArrayU8(tmp->p, ecFieldFp.p); ecc.field = OptECField::make_ECFIELDFP(ecFieldFp); ecc.base.algName = string(eccCommParamsSpec->base.algName); ecc.base.specType = AsyKeySpecType(static_cast(eccCommParamsSpec->base.specType)); ecc.h = eccCommParamsSpec->h; - DataBlobToArrayU8(eccCommParamsSpec->a, ecc.a); - DataBlobToArrayU8(eccCommParamsSpec->b, ecc.b); - DataBlobToArrayU8(eccCommParamsSpec->g.x, ecc.g.x); - DataBlobToArrayU8(eccCommParamsSpec->g.y, ecc.g.y); - DataBlobToArrayU8(eccCommParamsSpec->n, ecc.n); + BigIntegerToArrayU8(eccCommParamsSpec->a, ecc.a); + BigIntegerToArrayU8(eccCommParamsSpec->b, ecc.b); + BigIntegerToArrayU8(eccCommParamsSpec->g.x, ecc.g.x); + BigIntegerToArrayU8(eccCommParamsSpec->g.y, ecc.g.y); + BigIntegerToArrayU8(eccCommParamsSpec->n, ecc.n); HcfObjDestroy(eccCommParamsSpec); return ecc; } @@ -59,8 +59,8 @@ Point ConvertPoint(string_view curveName, array_view encodedPoint) return {}; } Point point = {}; - DataBlobToArrayU8(hcfPoint.x, point.x); - DataBlobToArrayU8(hcfPoint.y, point.y); + BigIntegerToArrayU8(hcfPoint.x, point.x); + BigIntegerToArrayU8(hcfPoint.y, point.y); FreeEcPointMem(&hcfPoint); return point; } @@ -68,8 +68,13 @@ Point ConvertPoint(string_view curveName, array_view encodedPoint) array GetEncodedPoint(string_view curveName, Point const& point, string_view format) { HcfPoint hcfPoint = {}; - ArrayU8ToDataBlob(point.x, hcfPoint.x); - ArrayU8ToDataBlob(point.y, hcfPoint.y); + bool bigintValid = true; + bigintValid &= ArrayU8ToBigInteger(point.x, hcfPoint.x); + bigintValid &= ArrayU8ToBigInteger(point.y, hcfPoint.y); + if (!bigintValid) { + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "params is invalid."); + return {}; + } HcfBlob outBlob = {}; HcfResult res = HcfGetEncodedPoint(curveName.c_str(), &hcfPoint, format.c_str(), &outBlob); if (res != HCF_SUCCESS) { diff --git a/frameworks/js/ani/src/ani_md.cpp b/frameworks/js/ani/src/ani_md.cpp index d2efc23..2b9f26e 100644 --- a/frameworks/js/ani/src/ani_md.cpp +++ b/frameworks/js/ani/src/ani_md.cpp @@ -50,7 +50,7 @@ DataBlob MdImpl::DigestSync() HcfBlob outBlob = {}; HcfResult res = this->md_->doFinal(this->md_, &outBlob); if (res != HCF_SUCCESS) { - ANI_LOGE_THROW(res, "mac doFinal failed!"); + ANI_LOGE_THROW(res, "md doFinal failed!"); return {}; } array data = {}; diff --git a/frameworks/js/ani/src/ani_pri_key.cpp b/frameworks/js/ani/src/ani_pri_key.cpp index ae5a9de..0d2debd 100644 --- a/frameworks/js/ani/src/ani_pri_key.cpp +++ b/frameworks/js/ani/src/ani_pri_key.cpp @@ -52,7 +52,7 @@ OptKeySpec GetAsyKeySpecBigInt(HcfPriKey *priKey, HcfAsyKeySpecItem item) return OptKeySpec::make_BIGINT(array{}); } array data = {}; - DataBlobToArrayU8(bigint, data); + BigIntegerToArrayU8(bigint, data); HcfBlobDataClearAndFree(reinterpret_cast(&bigint)); return OptKeySpec::make_BIGINT(data); } @@ -78,7 +78,7 @@ string GetEncodedPemInner(const HcfPriKey *self, HcfParamsSpec *params, string_v namespace ANI::CryptoFramework { PriKeyImpl::PriKeyImpl() {} -PriKeyImpl::PriKeyImpl(HcfPriKey *priKey, bool owner /* = false */) : priKey_(priKey), owner_(owner) {} +PriKeyImpl::PriKeyImpl(HcfPriKey *priKey, bool owner /* = true */) : priKey_(priKey), owner_(owner) {} PriKeyImpl::~PriKeyImpl() { diff --git a/frameworks/js/ani/src/ani_pub_key.cpp b/frameworks/js/ani/src/ani_pub_key.cpp index 6b68158..e031c1a 100644 --- a/frameworks/js/ani/src/ani_pub_key.cpp +++ b/frameworks/js/ani/src/ani_pub_key.cpp @@ -52,7 +52,7 @@ OptKeySpec GetAsyKeySpecBigInt(HcfPubKey *pubKey, HcfAsyKeySpecItem item) return OptKeySpec::make_BIGINT(array{}); } array data = {}; - DataBlobToArrayU8(bigint, data); + BigIntegerToArrayU8(bigint, data); HcfFree(bigint.data); return OptKeySpec::make_BIGINT(data); } @@ -61,7 +61,7 @@ OptKeySpec GetAsyKeySpecBigInt(HcfPubKey *pubKey, HcfAsyKeySpecItem item) namespace ANI::CryptoFramework { PubKeyImpl::PubKeyImpl() {} -PubKeyImpl::PubKeyImpl(HcfPubKey *pubKey, bool owner /* = false */) : pubKey_(pubKey), owner_(owner) {} +PubKeyImpl::PubKeyImpl(HcfPubKey *pubKey, bool owner /* = true */) : pubKey_(pubKey), owner_(owner) {} PubKeyImpl::~PubKeyImpl() { 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 0000000..0e99e16 --- /dev/null +++ b/frameworks/js/ani/src/ani_signature_utils.cpp @@ -0,0 +1,36 @@ +/* + * 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) +{ + // api 20 + TH_THROW(std::runtime_error, "GenEccSignatureSpec not implemented"); +} + +array GenEccSignature(EccSignatureSpec const& spec) +{ + // api 20 + 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_sm2_crypto_util.cpp b/frameworks/js/ani/src/ani_sm2_crypto_util.cpp index 33c21db..3fbfef5 100644 --- a/frameworks/js/ani/src/ani_sm2_crypto_util.cpp +++ b/frameworks/js/ani/src/ani_sm2_crypto_util.cpp @@ -21,8 +21,13 @@ namespace ANI::CryptoFramework { DataBlob GenCipherTextBySpec(SM2CipherTextSpec const& spec, optional_view mode) { Sm2CipherTextSpec hcfSpec = {}; - ArrayU8ToDataBlob(spec.xCoordinate, hcfSpec.xCoordinate); - ArrayU8ToDataBlob(spec.yCoordinate, hcfSpec.yCoordinate); + bool bigintValid = true; + bigintValid &= ArrayU8ToBigInteger(spec.xCoordinate, hcfSpec.xCoordinate); + bigintValid &= ArrayU8ToBigInteger(spec.yCoordinate, hcfSpec.yCoordinate); + if (!bigintValid) { + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "params is invalid."); + return {}; + } ArrayU8ToDataBlob(spec.cipherTextData, hcfSpec.cipherTextData); ArrayU8ToDataBlob(spec.hashData, hcfSpec.hashData); string dataMode = mode.has_value() ? mode.value() : ""; @@ -50,8 +55,8 @@ SM2CipherTextSpec GetCipherTextSpec(DataBlob const& cipherText, optional_viewxCoordinate, spec.xCoordinate); - DataBlobToArrayU8(hcfSpec->yCoordinate, spec.yCoordinate); + BigIntegerToArrayU8(hcfSpec->xCoordinate, spec.xCoordinate); + BigIntegerToArrayU8(hcfSpec->yCoordinate, spec.yCoordinate); DataBlobToArrayU8(hcfSpec->cipherTextData, spec.cipherTextData); DataBlobToArrayU8(hcfSpec->hashData, spec.hashData); DestroySm2CipherTextSpec(hcfSpec); diff --git a/frameworks/js/ani/src/ani_sym_key.cpp b/frameworks/js/ani/src/ani_sym_key.cpp index 3cfe429..71b2cda 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 c1205ea..375b8f6 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/frameworks/js/ani/test/test_asy_key_generator.ets b/frameworks/js/ani/test/test_asy_key_generator.ets index b3066c8..7729374 100644 --- a/frameworks/js/ani/test/test_asy_key_generator.ets +++ b/frameworks/js/ani/test/test_asy_key_generator.ets @@ -27,12 +27,12 @@ function testAsyKeyGeneratorSync() { let skBlob = priKey.getEncoded(); let pkStr = utils.uint8ArrayToHexStr(pkBlob.data); let skStr = utils.uint8ArrayToHexStr(skBlob.data); - console.log("AsyKeyGenerator algName: " + generator.algName); - console.log('generateKeyPair pubKey: ' + pkStr); - console.log('generateKeyPair priKey: ' + skStr); + console.println("AsyKeyGenerator algName: " + generator.algName); + console.println('generateKeyPair pubKey: ' + pkStr); + console.println('generateKeyPair priKey: ' + skStr); priKey.clearMem(); } catch (err: BusinessError) { - console.error(`[error] generateKeyPair: ${err.code} ${err.message}`); + console.println(`[error] generateKeyPair: ${err.code} ${err.message}`); } } @@ -66,12 +66,12 @@ function testAsyKeyConvertKeySync() { let skBlob = priKey.getEncoded(); let pkStr = utils.uint8ArrayToHexStr(pkBlob.data); let skStr = utils.uint8ArrayToHexStr(skBlob.data); - console.log("AsyKeyGenerator algName: " + generator.algName); - console.log('convertKey pubKey: ' + pkStr); - console.log('convertKey priKey: ' + skStr); + console.println("AsyKeyGenerator algName: " + generator.algName); + console.println('convertKey pubKey: ' + pkStr); + console.println('convertKey priKey: ' + skStr); priKey.clearMem(); } catch (err: BusinessError) { - console.error(`[error] convertKey: ${err.code} ${err.message}`); + console.println(`[error] convertKey: ${err.code} ${err.message}`); } } @@ -105,97 +105,97 @@ function testConvertPemKeySync() { let priKey = keyPair.priKey; let pkStr = pubKey.getEncodedPem('X509'); let skStr = priKey.getEncodedPem('PKCS8'); - console.log("AsyKeyGenerator algName: " + generator.algName); - console.log('convertPemKey pubKey: ' + pkStr); - console.log('convertPemKey priKey: ' + skStr); + console.println("AsyKeyGenerator algName: " + generator.algName); + console.println('convertPemKey pubKey: ' + pkStr); + console.println('convertPemKey priKey: ' + skStr); priKey.clearMem(); } catch (err: BusinessError) { - console.error(`[error] convertPemKey: ${err.code} ${err.message}`); + console.println(`[error] convertPemKey: ${err.code} ${err.message}`); } } // ERROR: TypeError: Cannot find type 'AsyKeySpecItem'. /* function showEccSpecDetailInfo(keyName: string, key: cryptoFramework.PubKey | cryptoFramework.PriKey) { - console.log('showEccSpecDetailInfo ' + keyName + ':'); + console.println('showEccSpecDetailInfo ' + keyName + ':'); let a = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_A_BN); - console.log('a: ' + a); + console.println('a: ' + a); let b = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_B_BN); - console.log('b: ' + b); + console.println('b: ' + b); let gX = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_G_X_BN); - console.log('gX: ' + gX); + console.println('gX: ' + gX); let gY = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_G_Y_BN); - console.log('gY: ' + gY); + console.println('gY: ' + gY); let n = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_N_BN); - console.log('n: ' + n); + console.println('n: ' + n); let h = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_H_NUM); - console.log('h: ' + h); + console.println('h: ' + h); let fieldType = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FIELD_TYPE_STR); - console.log('fieldType: ' + fieldType); + console.println('fieldType: ' + fieldType); let fieldSize = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FIELD_SIZE_NUM); - console.log('fieldSize: ' + fieldSize); + console.println('fieldSize: ' + fieldSize); let curveName = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_CURVE_NAME_STR); - console.log('curveName: ' + curveName); + console.println('curveName: ' + curveName); if (keyName === 'pubKey') { let pkX = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_PK_X_BN); - console.log('pkX: ' + pkX); + console.println('pkX: ' + pkX); let pkY = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_PK_Y_BN); - console.log('pkY: ' + pkY); + console.println('pkY: ' + pkY); } else if (keyName === 'priKey') { let sk = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_SK_BN); - console.log('sk: ' + sk); + console.println('sk: ' + sk); } } */ function showPubKeyEccSpecDetailInfo(key: cryptoFramework.PubKey) { - console.log('showPubKeyEccSpecDetailInfo:'); + console.println('showPubKeyEccSpecDetailInfo:'); let a = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_A_BN); - console.log('a: ' + a); + console.println('a: ' + a); let b = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_B_BN); - console.log('b: ' + b); + console.println('b: ' + b); let gX = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_G_X_BN); - console.log('gX: ' + gX); + console.println('gX: ' + gX); let gY = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_G_Y_BN); - console.log('gY: ' + gY); + console.println('gY: ' + gY); let n = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_N_BN); - console.log('n: ' + n); + console.println('n: ' + n); let h = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_H_NUM); - console.log('h: ' + h); + console.println('h: ' + h); let fieldType = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FIELD_TYPE_STR); - console.log('fieldType: ' + fieldType); + console.println('fieldType: ' + fieldType); let fieldSize = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FIELD_SIZE_NUM); - console.log('fieldSize: ' + fieldSize); + console.println('fieldSize: ' + fieldSize); let curveName = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_CURVE_NAME_STR); - console.log('curveName: ' + curveName); + console.println('curveName: ' + curveName); let pkX = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_PK_X_BN); - console.log('pkX: ' + pkX); + console.println('pkX: ' + pkX); let pkY = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_PK_Y_BN); - console.log('pkY: ' + pkY); + console.println('pkY: ' + pkY); } function showPriKeyEccSpecDetailInfo(key: cryptoFramework.PriKey) { - console.log('showPriKeyEccSpecDetailInfo:'); + console.println('showPriKeyEccSpecDetailInfo:'); let a = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_A_BN); - console.log('a: ' + a); + console.println('a: ' + a); let b = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_B_BN); - console.log('b: ' + b); + console.println('b: ' + b); let gX = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_G_X_BN); - console.log('gX: ' + gX); + console.println('gX: ' + gX); let gY = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_G_Y_BN); - console.log('gY: ' + gY); + console.println('gY: ' + gY); let n = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_N_BN); - console.log('n: ' + n); + console.println('n: ' + n); let h = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_H_NUM); - console.log('h: ' + h); + console.println('h: ' + h); let fieldType = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FIELD_TYPE_STR); - console.log('fieldType: ' + fieldType); + console.println('fieldType: ' + fieldType); let fieldSize = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FIELD_SIZE_NUM); - console.log('fieldSize: ' + fieldSize); + console.println('fieldSize: ' + fieldSize); let curveName = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_CURVE_NAME_STR); - console.log('curveName: ' + curveName); + console.println('curveName: ' + curveName); let sk = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_SK_BN); - console.log('sk: ' + sk); + console.println('sk: ' + sk); } function testAsyKeySpec() { @@ -225,17 +225,17 @@ function testAsyKeySpec() { showPubKeyEccSpecDetailInfo(pubKey); showPriKeyEccSpecDetailInfo(priKey); } catch (err: BusinessError) { - console.error(`[error] AsyKeySpec: ${err.code} ${err.message}`); + console.println(`[error] AsyKeySpec: ${err.code} ${err.message}`); } } export function testAsyKeyGenerator() { - console.log(">>>>>>>>>>>>>>>>>>>> AsyKeyGeneratorSync"); + console.println(">>>>>>>>>>>>>>>>>>>> AsyKeyGeneratorSync"); testAsyKeyGeneratorSync(); - console.log(">>>>>>>>>>>>>>>>>>>> AsyKeyConvertKeySync"); + console.println(">>>>>>>>>>>>>>>>>>>> AsyKeyConvertKeySync"); testAsyKeyConvertKeySync(); - console.log(">>>>>>>>>>>>>>>>>>>> AsyKeyConvertPemKeySync"); + console.println(">>>>>>>>>>>>>>>>>>>> AsyKeyConvertPemKeySync"); testConvertPemKeySync(); - console.log(">>>>>>>>>>>>>>>>>>>> AsyKeySpec"); + console.println(">>>>>>>>>>>>>>>>>>>> AsyKeySpec"); testAsyKeySpec(); } diff --git a/frameworks/js/ani/test/test_asy_key_generator_by_spec.ets b/frameworks/js/ani/test/test_asy_key_generator_by_spec.ets index 998e154..3ee2996 100644 --- a/frameworks/js/ani/test/test_asy_key_generator_by_spec.ets +++ b/frameworks/js/ani/test/test_asy_key_generator_by_spec.ets @@ -34,12 +34,12 @@ function testRsaKeyPairSpec() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(rsaKeyPairSpec); let keyPair = generatorBySpec.generateKeyPairSync(); if (keyPair !== null) { - console.log('get rsa key pair result success!'); + console.println('get rsa key pair result success!'); } else { - console.error('get rsa key pair result fail!'); + console.println('get rsa key pair result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testRsaKeyPairSpec: ${err.code} ${err.message}`); + console.println(`[error] testRsaKeyPairSpec: ${err.code} ${err.message}`); } } @@ -61,13 +61,13 @@ function testRsaPubKeySpec() { let rsaGeneratorSpec = cryptoFramework.createAsyKeyGeneratorBySpec(rsaPubKeySpec); let pubKey = rsaGeneratorSpec.generatePubKeySync(); if (pubKey !== null) { - console.log('get rsa pub key result success!'); + console.println('get rsa pub key result success!'); } else { - console.error('get rsa pub key result fail!'); + console.println('get rsa pub key result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testRsaPubKeySpec: ${err.code} ${err.message}`); + console.println(`[error] testRsaPubKeySpec: ${err.code} ${err.message}`); } } @@ -94,12 +94,12 @@ function testEccCommonSpec() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(eccCommonSpec); let keyPair = generatorBySpec.generateKeyPairSync(); // Generate an ECC key pair. if (keyPair !== null) { - console.log('get key pair result success!'); + console.println('get key pair result success!'); } else { - console.error('get key pair result fail!'); + console.println('get key pair result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testEccCommonSpec: ${err.code} ${err.message}`); + console.println(`[error] testEccCommonSpec: ${err.code} ${err.message}`); } } @@ -147,12 +147,12 @@ function testEccPriKeySpec() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(eccPriKeySpec); let priKey = generatorBySpec.generatePriKeySync(); if (priKey !== null) { - console.log('get pri key result success!'); + console.println('get pri key result success!'); } else { - console.error('get pri key result fail!'); + console.println('get pri key result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testEccPriKeySpec: ${err.code} ${err.message}`); + console.println(`[error] testEccPriKeySpec: ${err.code} ${err.message}`); } } @@ -189,12 +189,12 @@ function testSm2KeyPairSpec() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(sm2KeyPairSpec); let keyPair = generatorBySpec.generateKeyPairSync(); if (keyPair !== null) { - console.log('get key pair result success!'); + console.println('get key pair result success!'); } else { - console.error('get key pair result fail!'); + console.println('get key pair result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testSm2AsyKeyGeneratorBySpecSync: ${err.code} ${err.message}`); + console.println(`[error] testSm2AsyKeyGeneratorBySpecSync: ${err.code} ${err.message}`); } } @@ -222,12 +222,12 @@ function testDsaKeyPairSpec() { let asyKeyPairSpec = cryptoFramework.createAsyKeyGeneratorBySpec(dsaKeyPairSpec); let keyPair = asyKeyPairSpec.generateKeyPairSync(); if (keyPair !== null) { - console.log('get key pair result success!'); + console.println('get key pair result success!'); } else { - console.error('get key pair result fail!'); + console.println('get key pair result fail!'); } } catch (err: BusinessError) { - console.error(`[error] getDsaCommonParamsSpec: ${err.code} ${err.message}`); + console.println(`[error] getDsaCommonParamsSpec: ${err.code} ${err.message}`); } } @@ -243,12 +243,12 @@ function testDsaPubKeySpec() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(dsaPubKeySpec); let pubKey = generatorBySpec.generatePubKeySync(); if (pubKey !== null) { - console.log('get pub key result success!'); + console.println('get pub key result success!'); } else { - console.error('get pub key result fail!'); + console.println('get pub key result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testDsaPubKeySpec: ${err.code} ${err.message}`); + console.println(`[error] testDsaPubKeySpec: ${err.code} ${err.message}`); } } @@ -264,18 +264,18 @@ function testX25519Keypair() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(X25519CommonParamsSpec); let keyPair = generatorBySpec.generateKeyPairSync(); if (keyPair !== null) { - console.log('get key pair result success!'); + console.println('get key pair result success!'); let pkBlob = keyPair.pubKey.getEncoded(); let skBlob = keyPair.priKey.getEncoded(); let pkStr = utils.uint8ArrayToHexStr(pkBlob.data); let skStr = utils.uint8ArrayToHexStr(skBlob.data); - console.log('pub key: ' + pkStr); - console.log('pri key: ' + skStr); + console.println('pub key: ' + pkStr); + console.println('pri key: ' + skStr); } else { - console.error('get key pair result fail!'); + console.println('get key pair result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testX25519Keypair: ${err.code} ${err.message}`); + console.println(`[error] testX25519Keypair: ${err.code} ${err.message}`); } } @@ -290,12 +290,12 @@ function testX25519PubKeySpec() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(X25519CommonParamsSpec); let pubKey = generatorBySpec.generatePubKeySync(); if (pubKey !== null) { - console.log('get pub key result success!'); + console.println('get pub key result success!'); } else { - console.error('get pub key result fail!'); + console.println('get pub key result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testX25519PubKeySpec: ${err.code} ${err.message}`); + console.println(`[error] testX25519PubKeySpec: ${err.code} ${err.message}`); } } @@ -310,12 +310,12 @@ function testX25519PriKeySpec() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(X25519CommonParamsSpec); let priKey = generatorBySpec.generatePriKeySync(); if (priKey !== null) { - console.log('get pri key result success!'); + console.println('get pri key result success!'); } else { - console.error('get pri key result fail!'); + console.println('get pri key result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testX25519PriKeySpec: ${err.code} ${err.message}`); + console.println(`[error] testX25519PriKeySpec: ${err.code} ${err.message}`); } } @@ -331,12 +331,12 @@ function testEd25519Keypair() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(ed25519CommonParamsSpec); let keyPair = generatorBySpec.generateKeyPairSync(); if (keyPair !== null) { - console.log('get key pair result success!'); + console.println('get key pair result success!'); } else { - console.error('get key pair result fail!'); + console.println('get key pair result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testEd25519Keypair: ${err.code} ${err.message}`); + console.println(`[error] testEd25519Keypair: ${err.code} ${err.message}`); } } @@ -351,12 +351,12 @@ function testEd25519PubKeySpec() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(ed25519CommonParamsSpec); let pubKey = generatorBySpec.generatePubKeySync(); if (pubKey !== null) { - console.log('get pub key result success!'); + console.println('get pub key result success!'); } else { - console.error('get pub key result fail!'); + console.println('get pub key result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testEd25519PubKeySpec: ${err.code} ${err.message}`); + console.println(`[error] testEd25519PubKeySpec: ${err.code} ${err.message}`); } } @@ -371,12 +371,12 @@ function testEd25519PriKeySpec() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(ed25519CommonParamsSpec); let priKey = generatorBySpec.generatePriKeySync(); if (priKey !== null) { - console.log('get pri key result success!'); + console.println('get pri key result success!'); } else { - console.error('get pri key result fail!'); + console.println('get pri key result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testEd25519PriKeySpec: ${err.code} ${err.message}`); + console.println(`[error] testEd25519PriKeySpec: ${err.code} ${err.message}`); } } @@ -399,12 +399,12 @@ function testDhKeyPairSpec() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(dhKeyPairSpec); let keyPair = generatorBySpec.generateKeyPairSync(); if (keyPair !== null) { - console.log('get key pair result success!'); + console.println('get key pair result success!'); } else { - console.error('get key pair result fail!'); + console.println('get key pair result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testDhKeyPairSpec: ${err.code} ${err.message}`); + console.println(`[error] testDhKeyPairSpec: ${err.code} ${err.message}`); } } @@ -426,12 +426,12 @@ function testDhPubKeySpec() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(dhPubKeySpec); let pubKey = generatorBySpec.generatePubKeySync(); if (pubKey !== null) { - console.log('get pub key result success!'); + console.println('get pub key result success!'); } else { - console.error('get pub key result fail!'); + console.println('get pub key result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testDhPubKeySpec: ${err.code} ${err.message}`); + console.println(`[error] testDhPubKeySpec: ${err.code} ${err.message}`); } } @@ -453,46 +453,46 @@ function testDhPriKeySpec() { let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(dhPriKeySpec); let priKey = generatorBySpec.generatePriKeySync(); if (priKey !== null) { - console.log('get pri key result success!'); + console.println('get pri key result success!'); } else { - console.error('get pri key result fail!'); + console.println('get pri key result fail!'); } } catch (err: BusinessError) { - console.error(`[error] testDhPriKeySpec: ${err.code} ${err.message}`); + console.println(`[error] testDhPriKeySpec: ${err.code} ${err.message}`); } } export function testAsyKeyGeneratorBySpec() { - console.log(">>>>>>>>>>>>>>>>>>>> EccCommonSpec"); + console.println(">>>>>>>>>>>>>>>>>>>> EccCommonSpec"); testEccCommonSpec(); - console.log(">>>>>>>>>>>>>>>>>>>> EccPriKeySpec"); + console.println(">>>>>>>>>>>>>>>>>>>> EccPriKeySpec"); testEccPriKeySpec(); - console.log(">>>>>>>>>>>>>>>>>>>> Sm2KeyPairSpec"); + console.println(">>>>>>>>>>>>>>>>>>>> Sm2KeyPairSpec"); testSm2KeyPairSpec(); - console.log(">>>>>>>>>>>>>>>>>>>> DsaKeyPairSpec"); + console.println(">>>>>>>>>>>>>>>>>>>> DsaKeyPairSpec"); testDsaKeyPairSpec(); - console.log(">>>>>>>>>>>>>>>>>>>> DsaPubKeySpec"); + console.println(">>>>>>>>>>>>>>>>>>>> DsaPubKeySpec"); testDsaPubKeySpec(); - console.log(">>>>>>>>>>>>>>>>>>>> RsaKeyPairSpec"); + console.println(">>>>>>>>>>>>>>>>>>>> RsaKeyPairSpec"); testRsaKeyPairSpec(); - console.log(">>>>>>>>>>>>>>>>>>>> RsaPubKeySpec"); + console.println(">>>>>>>>>>>>>>>>>>>> RsaPubKeySpec"); testRsaPubKeySpec(); - console.log(">>>>>>>>>>>>>>>>>>>> X25519Keypair"); + console.println(">>>>>>>>>>>>>>>>>>>> X25519Keypair"); testX25519Keypair(); - console.log(">>>>>>>>>>>>>>>>>>>> X25519PubKeySpec"); + console.println(">>>>>>>>>>>>>>>>>>>> X25519PubKeySpec"); testX25519PubKeySpec(); - console.log(">>>>>>>>>>>>>>>>>>>> X25519PriKeySpec"); + console.println(">>>>>>>>>>>>>>>>>>>> X25519PriKeySpec"); testX25519PriKeySpec(); - console.log(">>>>>>>>>>>>>>>>>>>> Ed25519Keypair"); + console.println(">>>>>>>>>>>>>>>>>>>> Ed25519Keypair"); testEd25519Keypair(); - console.log(">>>>>>>>>>>>>>>>>>>> Ed25519PubKeySpec"); + console.println(">>>>>>>>>>>>>>>>>>>> Ed25519PubKeySpec"); testEd25519PubKeySpec(); - console.log(">>>>>>>>>>>>>>>>>>>> Ed25519PriKeySpec"); + console.println(">>>>>>>>>>>>>>>>>>>> Ed25519PriKeySpec"); testEd25519PriKeySpec(); - console.log(">>>>>>>>>>>>>>>>>>>> DhKeyPairSpec"); + console.println(">>>>>>>>>>>>>>>>>>>> DhKeyPairSpec"); testDhKeyPairSpec(); - console.log(">>>>>>>>>>>>>>>>>>>> DhPubKeySpec"); + console.println(">>>>>>>>>>>>>>>>>>>> DhPubKeySpec"); testDhPubKeySpec(); - console.log(">>>>>>>>>>>>>>>>>>>> DhPriKeySpec"); + console.println(">>>>>>>>>>>>>>>>>>>> DhPriKeySpec"); testDhPriKeySpec(); } diff --git a/frameworks/js/ani/test/test_cipher.ets b/frameworks/js/ani/test/test_cipher.ets index 7a8f09c..4f8c0f1 100644 --- a/frameworks/js/ani/test/test_cipher.ets +++ b/frameworks/js/ani/test/test_cipher.ets @@ -36,7 +36,7 @@ function encrypt(algorithm: string, symKey: cryptoFramework.SymKey, data: crypto params: cryptoFramework.GcmParamsSpec | cryptoFramework.CcmParamsSpec | cryptoFramework.IvParamsSpec | null ): cryptoFramework.DataBlob { let cipher = cryptoFramework.createCipher(algorithm); - console.log("Cipher algName: " + cipher.algName); + console.println("Cipher algName: " + cipher.algName); cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, params); if (algorithm.includes('GCM') || algorithm.includes('CCM')) { let encryptData = cipher.updateSync(data); @@ -79,12 +79,12 @@ function aesCipher(algorithm: string, let encryptText = encrypt(algorithm, symKey, plainText, params); let decryptText = decrypt(algorithm, symKey, encryptText, params); if (plainText.data.toString() === decryptText.data.toString()) { - console.log(`${algorithm} success`); + console.println(`${algorithm} success`); } else { - console.error(`${algorithm} failed`); + console.println(`${algorithm} failed`); } } catch (err: BusinessError) { - console.error(`[error] ${algorithm}: ${err.code} ${err.message}`); + console.println(`[error] ${algorithm}: ${err.code} ${err.message}`); } } @@ -128,23 +128,23 @@ function testCipherSpec() { let mgf1Psrc = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR); let mgfName = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF_NAME_STR); let mdName = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MD_NAME_STR); - console.log("CipherSpec Uint8Array: " + mgf1Psrc.toString()); - console.log("CipherSpec mgfName: " + mgfName); - console.log("CipherSpec mdName: " + mdName); + console.println("CipherSpec Uint8Array: " + mgf1Psrc.toString()); + console.println("CipherSpec mgfName: " + mgfName); + console.println("CipherSpec mdName: " + mdName); } catch (err: BusinessError) { - console.error(`[error] CipherSpec: ${err.code} ${err.message}`); + console.println(`[error] CipherSpec: ${err.code} ${err.message}`); } } export function testCipher() { - console.log(">>>>>>>>>>>>>>>>>>>> AesGcmSync"); + console.println(">>>>>>>>>>>>>>>>>>>> AesGcmSync"); testAesGcmSync(); - console.log(">>>>>>>>>>>>>>>>>>>> AesCcmSync"); + console.println(">>>>>>>>>>>>>>>>>>>> AesCcmSync"); testAesCcmSync(); - console.log(">>>>>>>>>>>>>>>>>>>> AesCbcSync"); + console.println(">>>>>>>>>>>>>>>>>>>> AesCbcSync"); testAesCbcSync(); - console.log(">>>>>>>>>>>>>>>>>>>> AesEcbSync"); + console.println(">>>>>>>>>>>>>>>>>>>> AesEcbSync"); testAesEcbSync(); - console.log(">>>>>>>>>>>>>>>>>>>> CipherSpec"); + console.println(">>>>>>>>>>>>>>>>>>>> CipherSpec"); testCipherSpec(); } diff --git a/frameworks/js/ani/test/test_dh_key_util.ets b/frameworks/js/ani/test/test_dh_key_util.ets index 43026d1..7aaa47c 100644 --- a/frameworks/js/ani/test/test_dh_key_util.ets +++ b/frameworks/js/ani/test/test_dh_key_util.ets @@ -20,18 +20,18 @@ import utils from "./test_utils"; function testGenDHCommonParamsSpec() { try { let spec = cryptoFramework.DHKeyUtil.genDHCommonParamsSpec(2048, 1024); - console.log("DHKeyUtil.genDHCommonParamsSpec:"); - console.log("DHCommonParamsSpec spec.algName: " + spec.algName); - console.log("DHCommonParamsSpec spec.specType: " + spec.specType); - console.log("DHCommonParamsSpec spec.p: " + spec.p); - console.log("DHCommonParamsSpec spec.g: " + spec.g); - console.log("DHCommonParamsSpec spec.l: " + spec.l); + console.println("DHKeyUtil.genDHCommonParamsSpec:"); + console.println("DHCommonParamsSpec spec.algName: " + spec.algName); + console.println("DHCommonParamsSpec spec.specType: " + spec.specType); + console.println("DHCommonParamsSpec spec.p: " + spec.p); + console.println("DHCommonParamsSpec spec.g: " + spec.g); + console.println("DHCommonParamsSpec spec.l: " + spec.l); } catch (err: BusinessError) { - console.error(`[error] DHKeyUtil.genDHCommonParamsSpec: ${err.code} ${err.message}`); + console.println(`[error] DHKeyUtil.genDHCommonParamsSpec: ${err.code} ${err.message}`); } } export function testDHKeyUtil() { - console.log(">>>>>>>>>>>>>>>>>>>> GenDHCommonParamsSpec"); + console.println(">>>>>>>>>>>>>>>>>>>> GenDHCommonParamsSpec"); testGenDHCommonParamsSpec(); } diff --git a/frameworks/js/ani/test/test_ecc_key_util.ets b/frameworks/js/ani/test/test_ecc_key_util.ets index 061423a..ca3db7a 100644 --- a/frameworks/js/ani/test/test_ecc_key_util.ets +++ b/frameworks/js/ani/test/test_ecc_key_util.ets @@ -21,19 +21,19 @@ function testGenECCCommonParamsSpec() { try { let spec = cryptoFramework.ECCKeyUtil.genECCCommonParamsSpec('NID_brainpoolP256r1'); let ecFiledFp = spec.field as cryptoFramework.ECFieldFp; - console.log("ECCKeyUtil.genECCCommonParamsSpec:"); - console.log("ECCCommonParamsSpec spec.algName: " + spec.algName); - console.log("ECCCommonParamsSpec spec.specType: " + spec.specType); - console.log("ECCCommonParamsSpec spec.field.fieldType: " + ecFiledFp.fieldType); - console.log("ECCCommonParamsSpec spec.field.p: " + ecFiledFp.p); - console.log("ECCCommonParamsSpec spec.a: " + spec.a); - console.log("ECCCommonParamsSpec spec.b: " + spec.b); - console.log("ECCCommonParamsSpec spec.g.x: " + spec.g.x); - console.log("ECCCommonParamsSpec spec.g.y: " + spec.g.y); - console.log("ECCCommonParamsSpec spec.n: " + spec.n); - console.log("ECCCommonParamsSpec spec.h: " + spec.h); + console.println("ECCKeyUtil.genECCCommonParamsSpec:"); + console.println("ECCCommonParamsSpec spec.algName: " + spec.algName); + console.println("ECCCommonParamsSpec spec.specType: " + spec.specType); + console.println("ECCCommonParamsSpec spec.field.fieldType: " + ecFiledFp.fieldType); + console.println("ECCCommonParamsSpec spec.field.p: " + ecFiledFp.p); + console.println("ECCCommonParamsSpec spec.a: " + spec.a); + console.println("ECCCommonParamsSpec spec.b: " + spec.b); + console.println("ECCCommonParamsSpec spec.g.x: " + spec.g.x); + console.println("ECCCommonParamsSpec spec.g.y: " + spec.g.y); + console.println("ECCCommonParamsSpec spec.n: " + spec.n); + console.println("ECCCommonParamsSpec spec.h: " + spec.h); } catch (err: BusinessError) { - console.error(`[error] ECCKeyUtil.genECCCommonParamsSpec: ${err.code} ${err.message}`); + console.println(`[error] ECCKeyUtil.genECCCommonParamsSpec: ${err.code} ${err.message}`); } } @@ -47,11 +47,11 @@ function testConvertPoint() { 0x98 ]); let point = cryptoFramework.ECCKeyUtil.convertPoint('NID_brainpoolP256r1', pkData); - console.log("ECCKeyUtil.convertPoint:"); - console.log("Point point.x: " + point.x); - console.log("Point point.y: " + point.y); + console.println("ECCKeyUtil.convertPoint:"); + console.println("Point point.x: " + point.x); + console.println("Point point.y: " + point.y); } catch (err: BusinessError) { - console.error(`[error] ECCKeyUtil.convertPoint: ${err.code} ${err.message}`); + console.println(`[error] ECCKeyUtil.convertPoint: ${err.code} ${err.message}`); } } @@ -62,18 +62,18 @@ function testGetEncodedPoint() { y: BigInt('37705793773900352766227640862738381713868374157045352837992005507978837073048') }; let encoded = cryptoFramework.ECCKeyUtil.getEncodedPoint('NID_brainpoolP256r1', point, 'COMPRESSED'); - console.log("ECCKeyUtil.getEncodedPoint:"); - console.log("Uint8Array encoded: " + encoded); + console.println("ECCKeyUtil.getEncodedPoint:"); + console.println("Uint8Array encoded: " + encoded); } catch (err: BusinessError) { - console.error(`[error] ECCKeyUtil.getEncodedPoint: ${err.code} ${err.message}`); + console.println(`[error] ECCKeyUtil.getEncodedPoint: ${err.code} ${err.message}`); } } export function testECCKeyUtil() { - console.log(">>>>>>>>>>>>>>>>>>>> GenECCCommonParamsSpec"); + console.println(">>>>>>>>>>>>>>>>>>>> GenECCCommonParamsSpec"); testGenECCCommonParamsSpec(); - console.log(">>>>>>>>>>>>>>>>>>>> ConvertPoint"); + console.println(">>>>>>>>>>>>>>>>>>>> ConvertPoint"); testConvertPoint(); - console.log(">>>>>>>>>>>>>>>>>>>> GetEncodedPoint"); + console.println(">>>>>>>>>>>>>>>>>>>> GetEncodedPoint"); testGetEncodedPoint(); } diff --git a/frameworks/js/ani/test/test_kdf.ets b/frameworks/js/ani/test/test_kdf.ets index d7a980d..14e2c1b 100644 --- a/frameworks/js/ani/test/test_kdf.ets +++ b/frameworks/js/ani/test/test_kdf.ets @@ -29,11 +29,11 @@ function testPBKDF2Sync() { let kdf = cryptoFramework.createKdf('PBKDF2|SHA256'); let secret = kdf.generateSecretSync(spec); let str = utils.uint8ArrayToHexStr(secret.data); - console.log("PBKDF2 algName: " + kdf.algName); - console.log("PBKDF2: " + str); + console.println("PBKDF2 algName: " + kdf.algName); + console.println("PBKDF2: " + str); } catch (err: BusinessError) { - console.error(`[error] PBKDF2: ${err.code} ${err.message}`); + console.println(`[error] PBKDF2: ${err.code} ${err.message}`); } } @@ -49,10 +49,10 @@ function testHKDFSync() { let kdf = cryptoFramework.createKdf('HKDF|SHA256|EXTRACT_AND_EXPAND'); let secret = kdf.generateSecretSync(spec); let str = utils.uint8ArrayToHexStr(secret.data); - console.log("HKDF algName: " + kdf.algName); - console.log("HKDF: " + str); + console.println("HKDF algName: " + kdf.algName); + console.println("HKDF: " + str); } catch (err: BusinessError) { - console.error(`[error] HKDF: ${err.code} ${err.message}`); + console.println(`[error] HKDF: ${err.code} ${err.message}`); } } @@ -71,18 +71,18 @@ function testScryptSync() { let kdf = cryptoFramework.createKdf('SCRYPT'); let secret = kdf.generateSecretSync(spec); let str = utils.uint8ArrayToHexStr(secret.data); - console.log("Scrypt algName: " + kdf.algName); - console.log("Scrypt: " + str); + console.println("Scrypt algName: " + kdf.algName); + console.println("Scrypt: " + str); } catch (err: BusinessError) { - console.error(`[error] Scrypt: ${err.code} ${err.message}`); + console.println(`[error] Scrypt: ${err.code} ${err.message}`); } } export function testKdf() { - console.log(">>>>>>>>>>>>>>>>>>>> PBKDF2Sync"); + console.println(">>>>>>>>>>>>>>>>>>>> PBKDF2Sync"); testPBKDF2Sync(); - console.log(">>>>>>>>>>>>>>>>>>>> HKDFSync"); + console.println(">>>>>>>>>>>>>>>>>>>> HKDFSync"); testHKDFSync(); - console.log(">>>>>>>>>>>>>>>>>>>> ScryptSync"); + console.println(">>>>>>>>>>>>>>>>>>>> ScryptSync"); testScryptSync(); } diff --git a/frameworks/js/ani/test/test_key_agreement.ets b/frameworks/js/ani/test/test_key_agreement.ets index 3cbfa21..7291fc3 100644 --- a/frameworks/js/ani/test/test_key_agreement.ets +++ b/frameworks/js/ani/test/test_key_agreement.ets @@ -38,22 +38,22 @@ function testKeyAgreementSync() let keyPairA = generator.convertKeySync({ data: pubKeyArray }, { data: priKeyArray }); let keyPairB = generator.generateKeyPairSync(); let keyAgreement = cryptoFramework.createKeyAgreement('ECC256'); - console.log("KeyAgreement algName: " + keyAgreement.algName); + console.println("KeyAgreement algName: " + keyAgreement.algName); let secret1 = keyAgreement.generateSecretSync(keyPairA.priKey, keyPairB.pubKey); let secret2 = keyAgreement.generateSecretSync(keyPairB.priKey, keyPairA.pubKey); if (secret1.data.toString() === secret2.data.toString()) { - console.log('KeyAgreement ecdh result success'); - console.log('KeyAgreement ecdh secret.data: ' + utils.uint8ArrayToHexStr(secret1.data)); + console.println('KeyAgreement ecdh result success'); + console.println('KeyAgreement ecdh secret.data: ' + utils.uint8ArrayToHexStr(secret1.data)); } else { - console.error('KeyAgreement ecdh result failed'); + console.println('KeyAgreement ecdh result failed'); } } catch (err: BusinessError) { - console.error(`[error] KeyAgreement: ${err.code} ${err.message}`); + console.println(`[error] KeyAgreement: ${err.code} ${err.message}`); } } export function testKeyAgreement() { - console.log(">>>>>>>>>>>>>>>>>>>> KeyAgreementSync"); + console.println(">>>>>>>>>>>>>>>>>>>> KeyAgreementSync"); testKeyAgreementSync(); } diff --git a/frameworks/js/ani/test/test_mac.ets b/frameworks/js/ani/test/test_mac.ets index c042492..7932c2c 100644 --- a/frameworks/js/ani/test/test_mac.ets +++ b/frameworks/js/ani/test/test_mac.ets @@ -38,11 +38,11 @@ function testMacSync() { }); let output = mac.doFinalSync(); let str = utils.uint8ArrayToHexStr(output.data); - console.log("CMAC algName: " + mac.algName); - console.log("CMAC-AES128: " + str); + console.println("CMAC algName: " + mac.algName); + console.println("CMAC-AES128: " + str); } catch (err: BusinessError) { - console.error(`[error] CMAC-AES128: ${err.code} ${err.message}`); + console.println(`[error] CMAC-AES128: ${err.code} ${err.message}`); } } @@ -63,17 +63,17 @@ function testMacAsync() { }); let output = await mac.doFinal(); let str = utils.uint8ArrayToHexStr(output.data); - console.log("HMAC algName: " + mac.algName); - console.log("HMAC-SHA256: " + str); + console.println("HMAC algName: " + mac.algName); + console.println("HMAC-SHA256: " + str); } catch (err: BusinessError) { - console.error(`[error] HMAC-SHA256: ${err.code} ${err.message}`); + console.println(`[error] HMAC-SHA256: ${err.code} ${err.message}`); } } export function testMac() { - console.log(">>>>>>>>>>>>>>>>>>>> MacSync"); + console.println(">>>>>>>>>>>>>>>>>>>> MacSync"); testMacSync(); - console.log(">>>>>>>>>>>>>>>>>>>> MacAsync"); + console.println(">>>>>>>>>>>>>>>>>>>> MacAsync"); testMacAsync(); } diff --git a/frameworks/js/ani/test/test_main.ets b/frameworks/js/ani/test/test_main.ets index 1ed3e68..eabb66b 100644 --- a/frameworks/js/ani/test/test_main.ets +++ b/frameworks/js/ani/test/test_main.ets @@ -13,6 +13,7 @@ * limitations under the License. */ +import utils from "./test_utils"; import { testRandom } from "./test_rand"; import { testMd } from "./test_md"; import { testMac } from "./test_mac"; @@ -43,4 +44,7 @@ function main() { testDHKeyUtil(); testECCKeyUtil(); testSM2CryptoUtil(); + + utils.doFullGC(); + utils.stopTaskPool(); } diff --git a/frameworks/js/ani/test/test_md.ets b/frameworks/js/ani/test/test_md.ets index 614a5bb..1e2e802 100644 --- a/frameworks/js/ani/test/test_md.ets +++ b/frameworks/js/ani/test/test_md.ets @@ -27,12 +27,12 @@ function testMdSync() { }); let output = md.digestSync(); let str = utils.uint8ArrayToHexStr(output.data); - console.log("MD5 algName: " + md.algName); - console.log("MD5: " + str); + console.println("MD5 algName: " + md.algName); + console.println("MD5: " + str); let length = md.getMdLength(); - console.log("MD5 length: " + length); + console.println("MD5 length: " + length); } catch (err: BusinessError) { - console.log(`[error] MD5: ${err.code} ${err.message}`); + console.println(`[error] MD5: ${err.code} ${err.message}`); } } @@ -46,17 +46,17 @@ function testMdAsync() { }); let output = await md.digest(); let str = utils.uint8ArrayToHexStr(output.data); - console.log("MD5: " + str); + console.println("MD5: " + str); let length = md.getMdLength(); - console.log("MD5 length: " + length); + console.println("MD5 length: " + length); } catch (err: BusinessError) { - console.log(`[error] MD5: ${err.code} ${err.message}`); + console.println(`[error] MD5: ${err.code} ${err.message}`); } } export function testMd() { - console.log(">>>>>>>>>>>>>>>>>>>> MdSync"); + console.println(">>>>>>>>>>>>>>>>>>>> MdSync"); testMdSync(); - console.log(">>>>>>>>>>>>>>>>>>>> MdAsync"); + console.println(">>>>>>>>>>>>>>>>>>>> MdAsync"); testMdAsync(); } diff --git a/frameworks/js/ani/test/test_rand.ets b/frameworks/js/ani/test/test_rand.ets index 94d72fa..e39a0f9 100644 --- a/frameworks/js/ani/test/test_rand.ets +++ b/frameworks/js/ani/test/test_rand.ets @@ -28,10 +28,10 @@ function testRandomSync() { }); let output = random.generateRandomSync(16); let str = utils.uint8ArrayToHexStr(output.data); - console.log("Random algName: " + random.algName); - console.log("Random: " + str); + console.println("Random algName: " + random.algName); + console.println("Random: " + str); } catch (err: BusinessError) { - console.error(`[error] Random: ${err.code} ${err.message}`); + console.println(`[error] Random: ${err.code} ${err.message}`); } } @@ -45,42 +45,16 @@ function testRandomAsync() { }); let output = await random.generateRandom(16); let str = utils.uint8ArrayToHexStr(output.data); - console.log("Random algName: " + random.algName); - console.log("Random: " + str); + console.println("Random algName: " + random.algName); + console.println("Random: " + str); } catch (err: BusinessError) { - console.error(`[error] Random: ${err.code} ${err.message}`); - } -} - -function testRandomCallback() { - try { - let data = "Hello World"; - let dataBytes = utils.stringToUint8Array(data); - let random = cryptoFramework.createRandom(); - random.setSeed({ - data: dataBytes - }); - random.generateRandom(16, (err, dataBlob) => { - console.log(`Random: ${err}`); - console.log(`Random: ${err.code} ${err.message}`); - if (err) { - console.error(`[error] Random: ${err.code} ${err.message}`); - } else { - let str = utils.uint8ArrayToHexStr(dataBlob.data); - console.log("Random algName: " + random.algName); - console.log("Random: " + str); - } - }); - } catch (err: BusinessError) { - console.error(`[error] Random: ${err.code} ${err.message}`); + console.println(`[error] Random: ${err.code} ${err.message}`); } } export function testRandom() { - console.log(">>>>>>>>>>>>>>>>>>>> RandomSync"); + console.println(">>>>>>>>>>>>>>>>>>>> RandomSync"); testRandomSync(); - console.log(">>>>>>>>>>>>>>>>>>>> RandomAsync"); + console.println(">>>>>>>>>>>>>>>>>>>> RandomAsync"); testRandomAsync(); - console.log(">>>>>>>>>>>>>>>>>>>> RandomCallback"); - testRandomCallback(); } diff --git a/frameworks/js/ani/test/test_sign.ets b/frameworks/js/ani/test/test_sign.ets index 851a909..24ec27e 100644 --- a/frameworks/js/ani/test/test_sign.ets +++ b/frameworks/js/ani/test/test_sign.ets @@ -23,7 +23,7 @@ function sign(algName: string, priKey: cryptoFramework.PriKey, signer.initSync(priKey); signer.updateSync(input1); let signData = signer.signSync(input2); - console.log("sign algName: " + signer.algName); + console.println("sign algName: " + signer.algName); return signData; } @@ -33,7 +33,7 @@ function verify(algName: string, pubKey: cryptoFramework.PubKey, signData: crypt verifier.initSync(pubKey); verifier.updateSync(input1); let res = verifier.verifySync(input2, signData); - console.log("verify algName: " + verifier.algName); + console.println("verify algName: " + verifier.algName); return res; } @@ -49,17 +49,17 @@ function testSignSync() { data: utils.stringToUint8Array(str2) }; let generator = cryptoFramework.createAsyKeyGenerator("SM2_256"); - console.log("AsyKeyGenerator algName: " + generator.algName); + console.println("AsyKeyGenerator algName: " + generator.algName); let keyPair = generator.generateKeyPairSync(); let signData = sign(algName, keyPair.priKey, input1, input2); let res = verify(algName, keyPair.pubKey, signData, input1, input2); if (res === true) { - console.log('Sign success'); + console.println('Sign success'); } else { - console.error('Sign failed'); + console.println('Sign failed'); } } catch (err: BusinessError) { - console.error(`[error] Sign: ${err.code} ${err.message}`); + console.println(`[error] Sign: ${err.code} ${err.message}`); } } @@ -70,17 +70,17 @@ function testSignSpec() { let saltLen = signer.getSignSpec(cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM); let mdName = signer.getSignSpec(cryptoFramework.SignSpecItem.PSS_MD_NAME_STR); let mgfName = signer.getSignSpec(cryptoFramework.SignSpecItem.PSS_MGF_NAME_STR); - console.log("SignSpec saltLen: " + saltLen); - console.log("SignSpec mdName: " + mdName); - console.log("SignSpec mgfName: " + mgfName); + console.println("SignSpec saltLen: " + saltLen); + console.println("SignSpec mdName: " + mdName); + console.println("SignSpec mgfName: " + mgfName); } catch (err: BusinessError) { - console.error(`[error] SignSpec: ${err.code} ${err.message}`); + console.println(`[error] SignSpec: ${err.code} ${err.message}`); } } export function testSign() { - console.log(">>>>>>>>>>>>>>>>>>>> SignSync"); + console.println(">>>>>>>>>>>>>>>>>>>> SignSync"); testSignSync(); - console.log(">>>>>>>>>>>>>>>>>>>> SignSpec"); + console.println(">>>>>>>>>>>>>>>>>>>> SignSpec"); testSignSpec(); } diff --git a/frameworks/js/ani/test/test_sm2_crypto_util.ets b/frameworks/js/ani/test/test_sm2_crypto_util.ets index 4e3b4e1..1bee988 100644 --- a/frameworks/js/ani/test/test_sm2_crypto_util.ets +++ b/frameworks/js/ani/test/test_sm2_crypto_util.ets @@ -31,11 +31,11 @@ function testGenCipherTextBySpec() { ]) } let cipherText = cryptoFramework.SM2CryptoUtil.genCipherTextBySpec(spec, 'C1C3C2'); - console.log("SM2CryptoUtil.genCipherTextBySpec:"); - console.log("CipherText cipherText.data: " + cipherText.data); + console.println("SM2CryptoUtil.genCipherTextBySpec:"); + console.println("CipherText cipherText.data: " + cipherText.data); } catch (err: BusinessError) { - console.error(`[error] SM2CryptoUtil.genCipherTextBySpec: ${err.code} ${err.message}`); + console.println(`[error] SM2CryptoUtil.genCipherTextBySpec: ${err.code} ${err.message}`); } } @@ -55,20 +55,20 @@ function testGetCipherTextSpec() { data: cipherTextArray }; let spec: cryptoFramework.SM2CipherTextSpec = cryptoFramework.SM2CryptoUtil.getCipherTextSpec(cipherText, 'C1C3C2'); - console.log("SM2CryptoUtil.getCipherTextSpec:"); - console.log("SM2CipherTextSpec spec.xCoordinate: " + spec.xCoordinate); - console.log("SM2CipherTextSpec spec.yCoordinate: " + spec.yCoordinate); - console.log("SM2CipherTextSpec spec.cipherTextData: " + utils.uint8ArrayToHexStr(spec.cipherTextData)); - console.log("SM2CipherTextSpec spec.hashData: " + utils.uint8ArrayToHexStr(spec.hashData)); + console.println("SM2CryptoUtil.getCipherTextSpec:"); + console.println("SM2CipherTextSpec spec.xCoordinate: " + spec.xCoordinate); + console.println("SM2CipherTextSpec spec.yCoordinate: " + spec.yCoordinate); + console.println("SM2CipherTextSpec spec.cipherTextData: " + utils.uint8ArrayToHexStr(spec.cipherTextData)); + console.println("SM2CipherTextSpec spec.hashData: " + utils.uint8ArrayToHexStr(spec.hashData)); } catch (err: BusinessError) { - console.error(`[error] SM2CryptoUtil.getCipherTextSpec: ${err.code} ${err.message}`); + console.println(`[error] SM2CryptoUtil.getCipherTextSpec: ${err.code} ${err.message}`); } } export function testSM2CryptoUtil() { - console.log(">>>>>>>>>>>>>>>>>>>> GenCipherTextBySpec"); + console.println(">>>>>>>>>>>>>>>>>>>> GenCipherTextBySpec"); testGenCipherTextBySpec(); - console.log(">>>>>>>>>>>>>>>>>>>> GetCipherTextSpec"); + console.println(">>>>>>>>>>>>>>>>>>>> GetCipherTextSpec"); testGetCipherTextSpec(); } diff --git a/frameworks/js/ani/test/test_sym_key_generator.ets b/frameworks/js/ani/test/test_sym_key_generator.ets index 88c10b8..68f9715 100644 --- a/frameworks/js/ani/test/test_sym_key_generator.ets +++ b/frameworks/js/ani/test/test_sym_key_generator.ets @@ -23,13 +23,13 @@ function testSymKeyGeneratorSync() { let symKey = generator.generateSymKeySync(); let encodedKey = symKey.getEncoded(); let str = utils.uint8ArrayToHexStr(encodedKey.data); - console.log("SymKeyGenerator algName: " + generator.algName); - console.log("SymKeyGenerator symKey.algName: " + symKey.algName); - console.log("SymKeyGenerator symKey.format: " + symKey.format); - console.log("generateSymKey: " + str); + console.println("SymKeyGenerator algName: " + generator.algName); + console.println("SymKeyGenerator symKey.algName: " + symKey.algName); + console.println("SymKeyGenerator symKey.format: " + symKey.format); + console.println("generateSymKey: " + str); symKey.clearMem(); } catch (err: BusinessError) { - console.error(`[error] generateSymKey: ${err.code} ${err.message}`); + console.println(`[error] generateSymKey: ${err.code} ${err.message}`); } } @@ -42,19 +42,19 @@ function testSymKeyConvertKeySync() { let symKey = generator.convertKeySync(keyBlob); let encodedKey = symKey.getEncoded(); let str = utils.uint8ArrayToHexStr(encodedKey.data); - console.log("SymKeyGenerator algName: " + generator.algName); - console.log("SymKeyGenerator symKey.algName: " + symKey.algName); - console.log("SymKeyGenerator symKey.format: " + symKey.format); - console.log("convertKey: " + str); + console.println("SymKeyGenerator algName: " + generator.algName); + console.println("SymKeyGenerator symKey.algName: " + symKey.algName); + console.println("SymKeyGenerator symKey.format: " + symKey.format); + console.println("convertKey: " + str); symKey.clearMem(); } catch (err: BusinessError) { - console.error(`[error] convertKey: ${err.code} ${err.message}`); + console.println(`[error] convertKey: ${err.code} ${err.message}`); } } export function testSymKeyGenerator() { - console.log(">>>>>>>>>>>>>>>>>>>> SymKeyGeneratorSync"); + console.println(">>>>>>>>>>>>>>>>>>>> SymKeyGeneratorSync"); testSymKeyGeneratorSync(); - console.log(">>>>>>>>>>>>>>>>>>>> SymKeyConvertKeySync"); + console.println(">>>>>>>>>>>>>>>>>>>> SymKeyConvertKeySync"); testSymKeyConvertKeySync(); } diff --git a/frameworks/js/ani/test/test_utils.ets b/frameworks/js/ani/test/test_utils.ets index f5db02f..45337a8 100644 --- a/frameworks/js/ani/test/test_utils.ets +++ b/frameworks/js/ani/test/test_utils.ets @@ -57,15 +57,11 @@ export function uint8ArrayToString(data: Uint8Array): string { } export function doFullGC(): void { - try { - GC.waitForFinishGC(GC.startGC(GC.Cause.FULL, GC.IN_PLACE_MODE)); - } catch (err) { - assertTrue(false, "Unexpected exception during GC"); - } + GC.waitForFinishGC(GC.startGC(GC.Cause.FULL, GC.IN_PLACE_MODE)); Coroutine.Schedule(); } -export function StopTaskPool(): void { +export function stopTaskPool(): void { CoroutineExtras.stopTaskpool(); } } // namespace utils diff --git a/frameworks/js/ani/test/test_verify.ets b/frameworks/js/ani/test/test_verify.ets index 1111e5e..923949e 100644 --- a/frameworks/js/ani/test/test_verify.ets +++ b/frameworks/js/ani/test/test_verify.ets @@ -62,14 +62,14 @@ function testVerifySync() { verify.initSync(keyPair.pubKey); verify.updateSync(inputData); let res = verify.verifySync(null, signDataBytes); - console.log("Verify algName: " + verify.algName); + console.println("Verify algName: " + verify.algName); if (res === true) { - console.log('Verify success'); + console.println('Verify success'); } else { - console.error('Verify failed'); + console.println('Verify failed'); } } catch (err: BusinessError) { - console.error(`[error] Verify: ${err.code} ${err.message}`); + console.println(`[error] Verify: ${err.code} ${err.message}`); } } @@ -89,12 +89,12 @@ function testRecoverSync() { verifier.initSync(keyPair.pubKey); let rawSignData = verifier.recoverSync(signData); if (rawSignData !== null) { - console.log("Recover rawSignData: " + utils.uint8ArrayToHexStr(rawSignData.data)); + console.println("Recover rawSignData: " + utils.uint8ArrayToHexStr(rawSignData.data)); } else { - console.error("Recover failed"); + console.println("Recover failed"); } } catch (err: BusinessError) { - console.error(`[error] Recover: ${err.code} ${err.message}`); + console.println(`[error] Recover: ${err.code} ${err.message}`); } } @@ -105,19 +105,19 @@ function testVerifySpec() { let saltLen = verifyer.getVerifySpec(cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM); let mdName = verifyer.getVerifySpec(cryptoFramework.SignSpecItem.PSS_MD_NAME_STR); let mgfName = verifyer.getVerifySpec(cryptoFramework.SignSpecItem.PSS_MGF_NAME_STR); - console.log("VerifySpec saltLen: " + saltLen); - console.log("VerifySpec mdName: " + mdName); - console.log("VerifySpec mgfName: " + mgfName); + console.println("VerifySpec saltLen: " + saltLen); + console.println("VerifySpec mdName: " + mdName); + console.println("VerifySpec mgfName: " + mgfName); } catch (err: BusinessError) { - console.error(`[error] VerifySpec: ${err.code} ${err.message}`); + console.println(`[error] VerifySpec: ${err.code} ${err.message}`); } } export function testVerify() { - console.log(">>>>>>>>>>>>>>>>>>>> VerifySync"); + console.println(">>>>>>>>>>>>>>>>>>>> VerifySync"); testVerifySync(); - console.log(">>>>>>>>>>>>>>>>>>>> RecoverSync"); + console.println(">>>>>>>>>>>>>>>>>>>> RecoverSync"); testRecoverSync(); - console.log(">>>>>>>>>>>>>>>>>>>> VerifySpec"); + console.println(">>>>>>>>>>>>>>>>>>>> VerifySpec"); testVerifySpec(); } diff --git a/interfaces/inner_api/common/result.h b/interfaces/inner_api/common/result.h index 24088e2..a0e72a9 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, -- Gitee