From df4e8000bcc8ec5bd4042918ca4fe5e05964c954 Mon Sep 17 00:00:00 2001 From: kang1024 Date: Fri, 30 May 2025 16:48:37 +0800 Subject: [PATCH] =?UTF-8?q?taihe::array=E8=BD=AC=E6=8D=A2=E5=8C=BA?= =?UTF-8?q?=E5=88=86HcfBlob=E5=92=8CHcfBigInteger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: kang1024 --- frameworks/js/ani/inc/ani_common.h | 11 +- .../ani/src/ani_asy_key_generator_by_spec.cpp | 134 +++++++++--------- frameworks/js/ani/src/ani_common.cpp | 30 ++-- frameworks/js/ani/src/ani_dh_key_util.cpp | 4 +- frameworks/js/ani/src/ani_ecc_key_util.cpp | 20 +-- frameworks/js/ani/src/ani_pri_key.cpp | 4 +- frameworks/js/ani/src/ani_pub_key.cpp | 4 +- frameworks/js/ani/src/ani_sm2_crypto_util.cpp | 8 +- frameworks/js/ani/test/test_main.ets | 4 + frameworks/js/ani/test/test_utils.ets | 2 +- 10 files changed, 109 insertions(+), 112 deletions(-) diff --git a/frameworks/js/ani/inc/ani_common.h b/frameworks/js/ani/inc/ani_common.h index 8598be9..09035c4 100644 --- a/frameworks/js/ani/inc/ani_common.h +++ b/frameworks/js/ani/inc/ani_common.h @@ -57,16 +57,13 @@ constexpr int SPEC_ITEM_TYPE_UINT8ARR = 4; int ConvertResultCode(HcfResult res); -template -void ArrayU8ToDataBlob(const array &arr, T &blob); - -template -void DataBlobToArrayU8(const T &blob, array &arr); - +void ArrayU8ToDataBlob(const array &arr, HcfBlob &blob); +void DataBlobToArrayU8(const HcfBlob &blob, array &arr); +void ArrayU8ToBigInteger(const array &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/src/ani_asy_key_generator_by_spec.cpp b/frameworks/js/ani/src/ani_asy_key_generator_by_spec.cpp index f551a92..e79c693 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 @@ -38,30 +38,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 +75,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 +99,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 +122,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 +142,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 +174,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 +183,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 +191,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 +199,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 +208,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 +216,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 +224,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 +235,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 +245,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,8 +255,8 @@ 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) diff --git a/frameworks/js/ani/src/ani_common.cpp b/frameworks/js/ani/src/ani_common.cpp index 5d21345..ccdff8a 100644 --- a/frameworks/js/ani/src/ani_common.cpp +++ b/frameworks/js/ani/src/ani_common.cpp @@ -88,36 +88,32 @@ int ConvertResultCode(HcfResult res) return ERR_RUNTIME_ERROR; } -template<> -void ArrayU8ToDataBlob(const array &arr, HcfBlob &blob) +void ArrayU8ToDataBlob(const array &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) +void ArrayU8ToBigInteger(const array &arr, HcfBigInteger &bigint) { - arr = array(move_data_t{}, blob.data, blob.len); + bigint.data = arr.empty() ? nullptr : arr.data(); + bigint.len = arr.size(); + if (bigint.len > 0 && bigint.data[bigint.len - 1] == 0) { // remove the sign bit of big integer + bigint.len--; + } } -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) diff --git a/frameworks/js/ani/src/ani_dh_key_util.cpp b/frameworks/js/ani/src/ani_dh_key_util.cpp index 4ba57df..4c7294e 100644 --- a/frameworks/js/ani/src/ani_dh_key_util.cpp +++ b/frameworks/js/ani/src/ani_dh_key_util.cpp @@ -35,8 +35,8 @@ DHCommonParamsSpec GenDHCommonParamsSpec(int32_t pLen, optional_view sk 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..4de03d1 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,8 @@ 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); + ArrayU8ToBigInteger(point.x, hcfPoint.x); + ArrayU8ToBigInteger(point.y, hcfPoint.y); 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_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_sm2_crypto_util.cpp b/frameworks/js/ani/src/ani_sm2_crypto_util.cpp index 33c21db..4a69d99 100644 --- a/frameworks/js/ani/src/ani_sm2_crypto_util.cpp +++ b/frameworks/js/ani/src/ani_sm2_crypto_util.cpp @@ -21,8 +21,8 @@ namespace ANI::CryptoFramework { DataBlob GenCipherTextBySpec(SM2CipherTextSpec const& spec, optional_view mode) { Sm2CipherTextSpec hcfSpec = {}; - ArrayU8ToDataBlob(spec.xCoordinate, hcfSpec.xCoordinate); - ArrayU8ToDataBlob(spec.yCoordinate, hcfSpec.yCoordinate); + ArrayU8ToBigInteger(spec.xCoordinate, hcfSpec.xCoordinate); + ArrayU8ToBigInteger(spec.yCoordinate, hcfSpec.yCoordinate); ArrayU8ToDataBlob(spec.cipherTextData, hcfSpec.cipherTextData); ArrayU8ToDataBlob(spec.hashData, hcfSpec.hashData); string dataMode = mode.has_value() ? mode.value() : ""; @@ -50,8 +50,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/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_utils.ets b/frameworks/js/ani/test/test_utils.ets index f5db02f..9d8fa46 100644 --- a/frameworks/js/ani/test/test_utils.ets +++ b/frameworks/js/ani/test/test_utils.ets @@ -65,7 +65,7 @@ export function doFullGC(): void { Coroutine.Schedule(); } -export function StopTaskPool(): void { +export function stopTaskPool(): void { CoroutineExtras.stopTaskpool(); } } // namespace utils -- Gitee