From 06f10559050c4e0b37664afc9e2f95b701b68bed Mon Sep 17 00:00:00 2001 From: kang1024 Date: Tue, 6 May 2025 19:16:32 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BF=AE=E5=A4=8Dani=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=A0=81=E4=B8=8Enapi=E7=9A=84=E8=A1=8C=E4=B8=BA=E4=B8=8D?= =?UTF-8?q?=E4=B8=80=E8=87=B4=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B2.=20?= =?UTF-8?q?=E9=80=82=E9=85=8D=E5=B7=A5=E5=85=B7=E6=94=AF=E6=8C=81taihe::ar?= =?UTF-8?q?ray=E9=BB=98=E8=AE=A4=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0?= 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 | 51 ++++++++-------- ...rity.cryptoFramework.cryptoFramework.taihe | 8 +-- frameworks/js/ani/inc/ani_common.h | 4 +- .../ani/src/ani_asy_key_generator_by_spec.cpp | 58 +++++++++---------- frameworks/js/ani/src/ani_cipher.cpp | 8 +-- frameworks/js/ani/src/ani_common.cpp | 45 ++++++++++++++ frameworks/js/ani/src/ani_kdf.cpp | 6 +- frameworks/js/ani/src/ani_key.cpp | 4 +- frameworks/js/ani/src/ani_mac.cpp | 4 +- frameworks/js/ani/src/ani_md.cpp | 6 +- frameworks/js/ani/src/ani_pri_key.cpp | 18 ++++-- frameworks/js/ani/src/ani_pub_key.cpp | 18 ++++-- frameworks/js/ani/src/ani_rand.cpp | 4 +- frameworks/js/ani/src/ani_sym_key.cpp | 18 ++++-- frameworks/js/ani/src/ani_verify.cpp | 2 +- frameworks/js/ani/test/build_test.sh | 2 + .../js/ani/test/test_asy_key_generator.ets | 1 + frameworks/js/ani/test/test_cipher.ets | 1 + frameworks/js/ani/test/test_md.ets | 1 + frameworks/js/ani/test/test_rand.ets | 1 + .../js/ani/test/test_sym_key_generator.ets | 1 + frameworks/js/ani/test/test_verify.ets | 3 +- .../src/napi_asy_key_spec_generator.cpp | 2 +- 24 files changed, 177 insertions(+), 90 deletions(-) create mode 100644 frameworks/js/ani/src/ani_common.cpp diff --git a/frameworks/js/ani/BUILD.gn b/frameworks/js/ani/BUILD.gn index 121a489..e760d74 100644 --- a/frameworks/js/ani/BUILD.gn +++ b/frameworks/js/ani/BUILD.gn @@ -45,6 +45,7 @@ taihe_shared_library("crypto_framework_ani") { "${framework_path}/js/ani/src/ani_asy_key_generator.cpp", "${framework_path}/js/ani/src/ani_asy_key_generator_by_spec.cpp", "${framework_path}/js/ani/src/ani_cipher.cpp", + "${framework_path}/js/ani/src/ani_common.cpp", "${framework_path}/js/ani/src/ani_constructor.cpp", "${framework_path}/js/ani/src/ani_kdf.cpp", "${framework_path}/js/ani/src/ani_key.cpp", diff --git a/frameworks/js/ani/dts/cryptoFramework.d.ts b/frameworks/js/ani/dts/cryptoFramework.d.ts index 87c5247..4bed2a4 100644 --- a/frameworks/js/ani/dts/cryptoFramework.d.ts +++ b/frameworks/js/ani/dts/cryptoFramework.d.ts @@ -13,7 +13,8 @@ * limitations under the License. */ -import type { AsyncCallback, Callback } from './@ohos.base'; +import { int, long } from './@ohos.base'; +import type { AsyncCallback } from './@ohos.base'; declare namespace cryptoFramework { enum Result { @@ -71,14 +72,14 @@ declare namespace cryptoFramework { interface PriKey extends Key { clearMem(): void; - getAsyKeySpec(itemType: AsyKeySpecItem): bigint | string | number; + getAsyKeySpec(itemType: AsyKeySpecItem): bigint | string | int; getEncodedDer(format: string): DataBlob; getEncodedPem(format: string): string; getEncodedPem(format: string, config: KeyEncodingConfig): string; } interface PubKey extends Key { - getAsyKeySpec(itemType: AsyKeySpecItem): bigint | string | number; + getAsyKeySpec(itemType: AsyKeySpecItem): bigint | string | int; getEncodedDer(format: string): DataBlob; getEncodedPem(format: string): string; } @@ -89,9 +90,9 @@ declare namespace cryptoFramework { } interface Random { - generateRandom(len: number, callback: AsyncCallback): void; - generateRandom(len: number): Promise; - generateRandomSync(len: number): DataBlob; + generateRandom(len: int, callback: AsyncCallback): void; + generateRandom(len: int): Promise; + generateRandomSync(len: int): DataBlob; setSeed(seed: DataBlob): void; readonly algName: string; } @@ -148,7 +149,7 @@ declare namespace cryptoFramework { doFinal(callback: AsyncCallback): void; doFinal(): Promise; doFinalSync(): DataBlob; - getMacLength(): number; + getMacLength(): int; readonly algName: string; } function createMac(algName: string): Mac; @@ -161,7 +162,7 @@ declare namespace cryptoFramework { digest(callback: AsyncCallback): void; digest(): Promise; digestSync(): DataBlob; - getMdLength(): number; + getMdLength(): int; readonly algName: string; } function createMd(algName: string): Md; @@ -215,9 +216,9 @@ declare namespace cryptoFramework { sign(data: DataBlob): Promise; sign(data: DataBlob | null): Promise; signSync(data: DataBlob | null): DataBlob; - setSignSpec(itemType: SignSpecItem, itemValue: number): void; - setSignSpec(itemType: SignSpecItem, itemValue: number | Uint8Array): void; - getSignSpec(itemType: SignSpecItem): string | number; + setSignSpec(itemType: SignSpecItem, itemValue: int): void; + setSignSpec(itemType: SignSpecItem, itemValue: int | Uint8Array): void; + getSignSpec(itemType: SignSpecItem): string | int; readonly algName: string; } @@ -235,9 +236,9 @@ declare namespace cryptoFramework { verifySync(data: DataBlob | null, signatureData: DataBlob): boolean; recover(signatureData: DataBlob): Promise; recoverSync(signatureData: DataBlob): DataBlob | null; - setVerifySpec(itemType: SignSpecItem, itemValue: number): void; - setVerifySpec(itemType: SignSpecItem, itemValue: number | Uint8Array): void; - getVerifySpec(itemType: SignSpecItem): string | number; + setVerifySpec(itemType: SignSpecItem, itemValue: int): void; + setVerifySpec(itemType: SignSpecItem, itemValue: int | Uint8Array): void; + getVerifySpec(itemType: SignSpecItem): string | int; readonly algName: string; } function createSign(algName: string): Sign; @@ -332,7 +333,7 @@ declare namespace cryptoFramework { b: bigint; g: Point; n: bigint; - h: number; + h: int; } interface ECCPriKeySpec extends AsyKeySpec { @@ -360,7 +361,7 @@ declare namespace cryptoFramework { interface DHCommonParamsSpec extends AsyKeySpec { p: bigint; g: bigint; - l: number; + l: int; } interface DHPriKeySpec extends AsyKeySpec { @@ -380,7 +381,7 @@ declare namespace cryptoFramework { } class DHKeyUtil { - static genDHCommonParamsSpec(pLen: number, skLen?: number): DHCommonParamsSpec; + static genDHCommonParamsSpec(pLen: int, skLen?: int): DHCommonParamsSpec; } interface ED25519PriKeySpec extends AsyKeySpec { @@ -445,25 +446,25 @@ declare namespace cryptoFramework { interface PBKDF2Spec extends KdfSpec { password: string | Uint8Array; salt: Uint8Array; - iterations: number; - keySize: number; + iterations: int; + keySize: int; } interface HKDFSpec extends KdfSpec { key: string | Uint8Array; salt: Uint8Array; info: Uint8Array; - keySize: number; + keySize: int; } interface ScryptSpec extends KdfSpec { passphrase: string | Uint8Array; salt: Uint8Array; - n: number; - r: number; - p: number; - maxMemory: number; - keySize: number; + n: long; + r: long; + p: long; + maxMemory: long; + keySize: int; } interface Kdf { diff --git a/frameworks/js/ani/idl/ohos.security.cryptoFramework.cryptoFramework.taihe b/frameworks/js/ani/idl/ohos.security.cryptoFramework.cryptoFramework.taihe index 279a191..8bce08c 100644 --- a/frameworks/js/ani/idl/ohos.security.cryptoFramework.cryptoFramework.taihe +++ b/frameworks/js/ani/idl/ohos.security.cryptoFramework.cryptoFramework.taihe @@ -213,10 +213,10 @@ struct ScryptSpec { @extends base: KdfSpec; passphrase: OptStrUint8Arr; salt: @typedarray Array; - n: i32; - r: i32; - p: i32; - maxMemory: i32; + n: i64; + r: i64; + p: i64; + maxMemory: i64; keySize: i32; } diff --git a/frameworks/js/ani/inc/ani_common.h b/frameworks/js/ani/inc/ani_common.h index 8567d95..cb3a6ba 100644 --- a/frameworks/js/ani/inc/ani_common.h +++ b/frameworks/js/ani/inc/ani_common.h @@ -31,10 +31,12 @@ namespace ANI::CryptoFramework { using namespace taihe; using namespace ohos::security::cryptoFramework::cryptoFramework; +int ConvertResultCode(HcfResult res); + #define ANI_LOGE_THROW(code, msg) \ do { \ LOGE(msg); \ - set_business_error(code, msg); \ + set_business_error(ConvertResultCode(code), msg); \ } while (0) } // 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 9373261..11f6579 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 @@ -335,7 +335,7 @@ void SetDhCommonParamsSpecAttribute(DHCommonParamsSpec const& dhParams, HcfDhCom dhCommonParamsSpec.length = dhParams.l; } -static HcfAsyKeyParamsSpec* CreateDSASpec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateDSASpec(OptAsyKeySpec const& asyKeySpec) { static HcfDsaKeyPairParamsSpec dsaKeyPairSpec = {}; static HcfDsaPubKeyParamsSpec dsaPubKeySpec = {}; @@ -354,7 +354,7 @@ static HcfAsyKeyParamsSpec* CreateDSASpec(OptAsyKeySpec const& asyKeySpec) return nullptr; } -static HcfAsyKeyParamsSpec* CreateECCSpec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateECCSpec(OptAsyKeySpec const& asyKeySpec) { static HcfEccKeyPairParamsSpec eccKeyPairSpec = {}; static HcfEccPubKeyParamsSpec eccPubKeySpec = {}; @@ -383,7 +383,7 @@ static HcfAsyKeyParamsSpec* CreateECCSpec(OptAsyKeySpec const& asyKeySpec) return nullptr; } -static HcfAsyKeyParamsSpec* CreateRSASpec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateRSASpec(OptAsyKeySpec const& asyKeySpec) { static HcfRsaKeyPairParamsSpec rsaKeyPairSpec = {}; static HcfRsaPubKeyParamsSpec rsaPubKeySpec = {}; @@ -402,7 +402,7 @@ static HcfAsyKeyParamsSpec* CreateRSASpec(OptAsyKeySpec const& asyKeySpec) return nullptr; } -static HcfAsyKeyParamsSpec* CreateEd25519Spec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateEd25519Spec(OptAsyKeySpec const& asyKeySpec) { static HcfAlg25519KeyPairParamsSpec ed25519KeyPairSpec = {}; static HcfAlg25519PubKeyParamsSpec ed25519PubKeySpec = {}; @@ -421,7 +421,7 @@ static HcfAsyKeyParamsSpec* CreateEd25519Spec(OptAsyKeySpec const& asyKeySpec) return nullptr; } -static HcfAsyKeyParamsSpec* CreateX25519Spec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateX25519Spec(OptAsyKeySpec const& asyKeySpec) { static HcfAlg25519KeyPairParamsSpec x25519KeyPairSpec = {}; static HcfAlg25519PubKeyParamsSpec x25519PubKeySpec = {}; @@ -440,7 +440,7 @@ static HcfAsyKeyParamsSpec* CreateX25519Spec(OptAsyKeySpec const& asyKeySpec) return nullptr; } -static HcfAsyKeyParamsSpec* CreateDHSpec(OptAsyKeySpec const& asyKeySpec) +HcfAsyKeyParamsSpec* CreateDHSpec(OptAsyKeySpec const& asyKeySpec) { static HcfDhKeyPairParamsSpec dhKeyPairSpec = {}; static HcfDhPubKeyParamsSpec dhPubKeySpec = {}; @@ -463,8 +463,9 @@ static HcfAsyKeyParamsSpec* CreateDHSpec(OptAsyKeySpec const& asyKeySpec) return nullptr; } -static HcfAsyKeyParamsSpec* CreateSpec(OptAsyKeySpec const& asyKeySpec, const std::string& algName) +HcfAsyKeyParamsSpec* CreateParamsSpec(OptAsyKeySpec const& asyKeySpec) { + const std::string &algName = asyKeySpec.get_ASYKEYSPEC_ref().algName.c_str(); if (algName == DSA_ALG_NAME) { return CreateDSASpec(asyKeySpec); } else if (algName == ECC_ALG_NAME || algName == SM2_ALG_NAME) { @@ -496,13 +497,13 @@ AsyKeyGeneratorBySpecImpl::~AsyKeyGeneratorBySpecImpl() KeyPair AsyKeyGeneratorBySpecImpl::GenerateKeyPairSync() { if (this->generator_ == nullptr) { - ANI_LOGE_THROW(HCF_INVALID_PARAMS, "generator obj is nullptr!"); + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "generator spec obj is nullptr!"); return make_holder(); } HcfKeyPair *keyPair = nullptr; - HcfResult result = this->generator_->generateKeyPair(this->generator_, &keyPair); - if (result != HCF_SUCCESS) { - ANI_LOGE_THROW(result, "generateKeyPair failed"); + HcfResult res = this->generator_->generateKeyPair(this->generator_, &keyPair); + if (res != HCF_SUCCESS) { + ANI_LOGE_THROW(res, "generateKeyPair failed"); return make_holder(); } return make_holder(keyPair); @@ -511,13 +512,13 @@ KeyPair AsyKeyGeneratorBySpecImpl::GenerateKeyPairSync() PriKey AsyKeyGeneratorBySpecImpl::GeneratePriKeySync() { if (this->generator_ == nullptr) { - ANI_LOGE_THROW(HCF_INVALID_PARAMS, "generator obj is nullptr!"); + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "generator spec obj is nullptr!"); return make_holder(); } HcfPriKey *priKey = nullptr; - HcfResult result = this->generator_->generatePriKey(this->generator_, &priKey); - if (result != HCF_SUCCESS) { - ANI_LOGE_THROW(result, "generatePriKey failed"); + HcfResult res = this->generator_->generatePriKey(this->generator_, &priKey); + if (res != HCF_SUCCESS) { + ANI_LOGE_THROW(res, "generatePriKey failed"); return make_holder(); } return make_holder(priKey); @@ -526,13 +527,13 @@ PriKey AsyKeyGeneratorBySpecImpl::GeneratePriKeySync() PubKey AsyKeyGeneratorBySpecImpl::GeneratePubKeySync() { if (this->generator_ == nullptr) { - ANI_LOGE_THROW(HCF_INVALID_PARAMS, "generator obj is nullptr!"); + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "generator spec obj is nullptr!"); return make_holder(); } HcfPubKey *pubKey = nullptr; - HcfResult result = this->generator_->generatePubKey(this->generator_, &pubKey); - if (result != HCF_SUCCESS) { - ANI_LOGE_THROW(result, "generatePubKey failed"); + HcfResult res = this->generator_->generatePubKey(this->generator_, &pubKey); + if (res != HCF_SUCCESS) { + ANI_LOGE_THROW(res, "generatePubKey failed"); return make_holder(); } return make_holder(pubKey); @@ -541,7 +542,7 @@ PubKey AsyKeyGeneratorBySpecImpl::GeneratePubKeySync() string AsyKeyGeneratorBySpecImpl::GetAlgName() { if (this->generator_ == nullptr) { - ANI_LOGE_THROW(HCF_INVALID_PARAMS, "generator obj is nullptr!"); + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "generator spec obj is nullptr!"); return ""; } const char *algName = this->generator_->getAlgName(this->generator_); @@ -550,24 +551,23 @@ string AsyKeyGeneratorBySpecImpl::GetAlgName() AsyKeyGeneratorBySpec CreateAsyKeyGeneratorBySpec(OptAsyKeySpec const& asyKeySpec) { - HcfAsyKeyGeneratorBySpec *generator = nullptr; - const std::string &algName = asyKeySpec.get_ASYKEYSPEC_ref().algName.c_str(); - - HcfAsyKeyParamsSpec *spec = CreateSpec(asyKeySpec, algName); + HcfAsyKeyParamsSpec *spec = CreateParamsSpec(asyKeySpec); if (spec == nullptr) { - ANI_LOGE_THROW(HCF_INVALID_PARAMS, "Unsupported algorithm or key type"); + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "invalid asy key spec!"); return make_holder(); } - HcfResult result = HcfAsyKeyGeneratorBySpecCreate(spec, &generator); - if (result != HCF_SUCCESS) { - ANI_LOGE_THROW(result, "HcfAsyKeyGeneratorBySpecCreate failed"); + HcfAsyKeyGeneratorBySpec *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorBySpecCreate(spec, &generator); + if (res != HCF_SUCCESS) { + ANI_LOGE_THROW(res, "create C generator spec obj fail!"); return make_holder(); } return make_holder(generator); } } // namespace ANI::CryptoFramework + // Since these macros are auto-generate, lint will cause false positive. // NOLINTBEGIN -TH_EXPORT_CPP_API_CreateAsyKeyGeneratorBySpec(CreateAsyKeyGeneratorBySpec); +TH_EXPORT_CPP_API_CreateAsyKeyGeneratorBySpec(ANI::CryptoFramework::CreateAsyKeyGeneratorBySpec); // NOLINTEND diff --git a/frameworks/js/ani/src/ani_cipher.cpp b/frameworks/js/ani/src/ani_cipher.cpp index dc6180d..d335c59 100644 --- a/frameworks/js/ani/src/ani_cipher.cpp +++ b/frameworks/js/ani/src/ani_cipher.cpp @@ -120,14 +120,14 @@ DataBlob CipherImpl::UpdateSync(DataBlob const& input) { if (this->cipher_ == nullptr) { ANI_LOGE_THROW(HCF_INVALID_PARAMS, "cipher obj is nullptr!"); - return { array(nullptr, 0) }; + return {}; } HcfBlob inBlob = { .data = input.data.data(), .len = input.data.size() }; HcfBlob outBlob = { .data = nullptr, .len = 0 }; HcfResult res = this->cipher_->update(this->cipher_, &inBlob, &outBlob); if (res != HCF_SUCCESS) { ANI_LOGE_THROW(res, "cipher update failed!"); - return { array(nullptr, 0) }; + return {}; } array data(move_data_t{}, outBlob.data, outBlob.len); HcfBlobDataClearAndFree(&outBlob); @@ -138,7 +138,7 @@ DataBlob CipherImpl::DoFinalSync(OptDataBlob const& input) { if (this->cipher_ == nullptr) { ANI_LOGE_THROW(HCF_INVALID_PARAMS, "cipher obj is nullptr!"); - return { array(nullptr, 0) }; + return {}; } HcfBlob *inBlob = nullptr; HcfBlob dataBlob = { .data = nullptr, .len = 0 }; @@ -151,7 +151,7 @@ DataBlob CipherImpl::DoFinalSync(OptDataBlob const& input) HcfResult res = this->cipher_->doFinal(this->cipher_, inBlob, &outBlob); if (res != HCF_SUCCESS) { ANI_LOGE_THROW(res, "cipher doFinal failed!"); - return { array(nullptr, 0) }; + return {}; } array data(move_data_t{}, outBlob.data, outBlob.len); HcfBlobDataClearAndFree(&outBlob); diff --git a/frameworks/js/ani/src/ani_common.cpp b/frameworks/js/ani/src/ani_common.cpp new file mode 100644 index 0000000..670a713 --- /dev/null +++ b/frameworks/js/ani/src/ani_common.cpp @@ -0,0 +1,45 @@ +/* + * 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_common.h" +#include + +namespace { +enum ResultCode { + SUCCESS = 0, + INVALID_PARAMS = 401, + NOT_SUPPORT = 801, + ERR_OUT_OF_MEMORY = 17620001, + ERR_RUNTIME_ERROR = 17620002, + ERR_CRYPTO_OPERATION = 17630001, +}; +} // namespace + +namespace ANI::CryptoFramework { +int ConvertResultCode(HcfResult res) +{ + static std::unordered_map resCodeMap = { + { HCF_SUCCESS, SUCCESS }, + { HCF_INVALID_PARAMS, INVALID_PARAMS }, + { HCF_NOT_SUPPORT, NOT_SUPPORT }, + { HCF_ERR_MALLOC, ERR_OUT_OF_MEMORY }, + { HCF_ERR_CRYPTO_OPERATION, ERR_CRYPTO_OPERATION } + }; + if (resCodeMap.count(res) > 0) { + return resCodeMap[res]; + } + return ERR_RUNTIME_ERROR; +} +} // namespace ANI::CryptoFramework diff --git a/frameworks/js/ani/src/ani_kdf.cpp b/frameworks/js/ani/src/ani_kdf.cpp index 7b55ec5..e3187c6 100644 --- a/frameworks/js/ani/src/ani_kdf.cpp +++ b/frameworks/js/ani/src/ani_kdf.cpp @@ -104,7 +104,7 @@ DataBlob KdfImpl::GenerateSecretSync(OptExtKdfSpec const& params) { if (this->kdf_ == nullptr) { ANI_LOGE_THROW(HCF_INVALID_PARAMS, "kdf obj is nullptr!"); - return { array(nullptr, 0) }; + return {}; } HcfKdfParamsSpec *paramsSpec = nullptr; HcfPBKDF2ParamsSpec pbkdf2Spec = {}; @@ -123,12 +123,12 @@ DataBlob KdfImpl::GenerateSecretSync(OptExtKdfSpec const& params) paramsSpec = reinterpret_cast(&scryptSpec); } else { ANI_LOGE_THROW(HCF_INVALID_PARAMS, "invalid kdf spec!"); - return { array(nullptr, 0) }; + return {}; } HcfResult res = this->kdf_->generateSecret(this->kdf_, paramsSpec); if (res != HCF_SUCCESS) { ANI_LOGE_THROW(res, "kdf generateSecret failed!"); - return { array(nullptr, 0) }; + return {}; } array data(move_data_t{}, outBlob.data, outBlob.len); HcfBlobDataClearAndFree(&outBlob); diff --git a/frameworks/js/ani/src/ani_key.cpp b/frameworks/js/ani/src/ani_key.cpp index 386418c..c14df61 100644 --- a/frameworks/js/ani/src/ani_key.cpp +++ b/frameworks/js/ani/src/ani_key.cpp @@ -35,13 +35,13 @@ DataBlob KeyImpl::GetEncoded() { if (this->key_ == nullptr) { ANI_LOGE_THROW(HCF_INVALID_PARAMS, "key obj is nullptr!"); - return { array(nullptr, 0) }; + return {}; } HcfBlob outBlob = { .data = nullptr, .len = 0 }; HcfResult res = this->key_->getEncoded(this->key_, &outBlob); if (res != HCF_SUCCESS) { ANI_LOGE_THROW(res, "getEncoded failed."); - return { array(nullptr, 0) }; + return {}; } array data(move_data_t{}, outBlob.data, outBlob.len); HcfBlobDataClearAndFree(&outBlob); diff --git a/frameworks/js/ani/src/ani_mac.cpp b/frameworks/js/ani/src/ani_mac.cpp index 4410005..6cf8885 100644 --- a/frameworks/js/ani/src/ani_mac.cpp +++ b/frameworks/js/ani/src/ani_mac.cpp @@ -59,13 +59,13 @@ DataBlob MacImpl::DoFinalSync() { if (this->mac_ == nullptr) { ANI_LOGE_THROW(HCF_INVALID_PARAMS, "mac obj is nullptr!"); - return { array(nullptr, 0) }; + return {}; } HcfBlob outBlob = { .data = nullptr, .len = 0 }; HcfResult res = this->mac_->doFinal(this->mac_, &outBlob); if (res != HCF_SUCCESS) { ANI_LOGE_THROW(res, "mac doFinal failed!"); - return { array(nullptr, 0) }; + return {}; } array data(move_data_t{}, outBlob.data, outBlob.len); HcfBlobDataClearAndFree(&outBlob); diff --git a/frameworks/js/ani/src/ani_md.cpp b/frameworks/js/ani/src/ani_md.cpp index 2c1124d..c150797 100644 --- a/frameworks/js/ani/src/ani_md.cpp +++ b/frameworks/js/ani/src/ani_md.cpp @@ -35,7 +35,7 @@ void MdImpl::UpdateSync(DataBlob const& input) HcfBlob inBlob = { .data = input.data.data(), .len = input.data.size() }; HcfResult res = this->md_->update(this->md_, &inBlob); if (res != HCF_SUCCESS) { - ANI_LOGE_THROW(res, "md doFinal failed!"); + ANI_LOGE_THROW(res, "md update failed!"); return; } } @@ -44,13 +44,13 @@ DataBlob MdImpl::DigestSync() { if (this->md_ == nullptr) { ANI_LOGE_THROW(HCF_INVALID_PARAMS, "md obj is nullptr!"); - return { array(nullptr, 0) }; + return {}; } HcfBlob outBlob = { .data = nullptr, .len = 0 }; HcfResult res = this->md_->doFinal(this->md_, &outBlob); if (res != HCF_SUCCESS) { ANI_LOGE_THROW(res, "mac doFinal failed!"); - return { array(nullptr, 0) }; + return {}; } array data(move_data_t{}, outBlob.data, outBlob.len); HcfBlobDataClearAndFree(&outBlob); diff --git a/frameworks/js/ani/src/ani_pri_key.cpp b/frameworks/js/ani/src/ani_pri_key.cpp index 14e75b8..764164d 100644 --- a/frameworks/js/ani/src/ani_pri_key.cpp +++ b/frameworks/js/ani/src/ani_pri_key.cpp @@ -55,13 +55,13 @@ DataBlob PriKeyImpl::GetEncoded() { if (this->priKey_ == nullptr) { ANI_LOGE_THROW(HCF_INVALID_PARAMS, "priKey obj is nullptr!"); - return { array(nullptr, 0) }; + return {}; } HcfBlob outBlob = { .data = nullptr, .len = 0 }; HcfResult res = this->priKey_->base.getEncoded(&this->priKey_->base, &outBlob); if (res != HCF_SUCCESS) { ANI_LOGE_THROW(res, "getEncoded failed."); - return { array(nullptr, 0) }; + return {}; } array data(move_data_t{}, outBlob.data, outBlob.len); HcfBlobDataClearAndFree(&outBlob); @@ -70,11 +70,21 @@ DataBlob PriKeyImpl::GetEncoded() string PriKeyImpl::GetFormat() { - TH_THROW(std::runtime_error, "GetFormat not implemented"); + if (this->priKey_ == nullptr) { + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "priKey obj is nullptr!"); + return ""; + } + const char *format = this->priKey_->base.getFormat(&this->priKey_->base); + return (format == nullptr) ? "" : string(format); } string PriKeyImpl::GetAlgName() { - TH_THROW(std::runtime_error, "GetAlgName not implemented"); + if (this->priKey_ == nullptr) { + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "priKey obj is nullptr!"); + return ""; + } + const char *algName = this->priKey_->base.getAlgorithm(&this->priKey_->base); + return (algName == nullptr) ? "" : string(algName); } } // namespace ANI::CryptoFramework diff --git a/frameworks/js/ani/src/ani_pub_key.cpp b/frameworks/js/ani/src/ani_pub_key.cpp index 80507c5..b1da751 100644 --- a/frameworks/js/ani/src/ani_pub_key.cpp +++ b/frameworks/js/ani/src/ani_pub_key.cpp @@ -55,13 +55,13 @@ DataBlob PubKeyImpl::GetEncoded() { if (this->pubKey_ == nullptr) { ANI_LOGE_THROW(HCF_INVALID_PARAMS, "pubKey obj is nullptr!"); - return { array(nullptr, 0) }; + return {}; } HcfBlob outBlob = { .data = nullptr, .len = 0 }; HcfResult res = this->pubKey_->base.getEncoded(&this->pubKey_->base, &outBlob); if (res != HCF_SUCCESS) { ANI_LOGE_THROW(res, "getEncoded failed."); - return { array(nullptr, 0) }; + return {}; } array data(move_data_t{}, outBlob.data, outBlob.len); HcfBlobDataClearAndFree(&outBlob); @@ -70,11 +70,21 @@ DataBlob PubKeyImpl::GetEncoded() string PubKeyImpl::GetFormat() { - TH_THROW(std::runtime_error, "GetFormat not implemented"); + if (this->pubKey_ == nullptr) { + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "pubKey obj is nullptr!"); + return ""; + } + const char *format = this->pubKey_->base.getFormat(&this->pubKey_->base); + return (format == nullptr) ? "" : string(format); } string PubKeyImpl::GetAlgName() { - TH_THROW(std::runtime_error, "GetAlgName not implemented"); + if (this->pubKey_ == nullptr) { + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "pubKey obj is nullptr!"); + return ""; + } + const char *algName = this->pubKey_->base.getAlgorithm(&this->pubKey_->base); + return (algName == nullptr) ? "" : string(algName); } } // namespace ANI::CryptoFramework diff --git a/frameworks/js/ani/src/ani_rand.cpp b/frameworks/js/ani/src/ani_rand.cpp index 5b4f279..915d908 100644 --- a/frameworks/js/ani/src/ani_rand.cpp +++ b/frameworks/js/ani/src/ani_rand.cpp @@ -30,13 +30,13 @@ DataBlob RandomImpl::GenerateRandomSync(int32_t len) { if (this->rand_ == nullptr) { ANI_LOGE_THROW(HCF_INVALID_PARAMS, "rand obj is nullptr!"); - return { array(nullptr, 0) }; + return {}; } HcfBlob outBlob = { .data = nullptr, .len = 0 }; HcfResult res = this->rand_->generateRandom(this->rand_, len, &outBlob); if (res != HCF_SUCCESS) { ANI_LOGE_THROW(res, "generateRandom failed!"); - return { array(nullptr, 0) }; + return {}; } array data(move_data_t{}, outBlob.data, outBlob.len); HcfBlobDataClearAndFree(&outBlob); diff --git a/frameworks/js/ani/src/ani_sym_key.cpp b/frameworks/js/ani/src/ani_sym_key.cpp index 0880c78..939e96d 100644 --- a/frameworks/js/ani/src/ani_sym_key.cpp +++ b/frameworks/js/ani/src/ani_sym_key.cpp @@ -45,13 +45,13 @@ DataBlob SymKeyImpl::GetEncoded() { if (this->symKey_ == nullptr) { ANI_LOGE_THROW(HCF_INVALID_PARAMS, "symKey obj is nullptr!"); - return { array(nullptr, 0) }; + return {}; } HcfBlob outBlob = { .data = nullptr, .len = 0 }; HcfResult res = this->symKey_->key.getEncoded(&this->symKey_->key, &outBlob); if (res != HCF_SUCCESS) { ANI_LOGE_THROW(res, "getEncoded failed."); - return { array(nullptr, 0) }; + return {}; } array data(move_data_t{}, outBlob.data, outBlob.len); HcfBlobDataClearAndFree(&outBlob); @@ -60,11 +60,21 @@ DataBlob SymKeyImpl::GetEncoded() string SymKeyImpl::GetFormat() { - TH_THROW(std::runtime_error, "GetFormat not implemented"); + if (this->symKey_ == nullptr) { + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "symKey obj is nullptr!"); + return ""; + } + const char *format = this->symKey_->key.getFormat(&this->symKey_->key); + return (format == nullptr) ? "" : string(format); } string SymKeyImpl::GetAlgName() { - TH_THROW(std::runtime_error, "GetAlgName not implemented"); + if (this->symKey_ == nullptr) { + ANI_LOGE_THROW(HCF_INVALID_PARAMS, "symKey obj is nullptr!"); + return ""; + } + const char *algName = this->symKey_->key.getAlgorithm(&this->symKey_->key); + return (algName == nullptr) ? "" : string(algName); } } // namespace ANI::CryptoFramework diff --git a/frameworks/js/ani/src/ani_verify.cpp b/frameworks/js/ani/src/ani_verify.cpp index 87e3672..99830f6 100644 --- a/frameworks/js/ani/src/ani_verify.cpp +++ b/frameworks/js/ani/src/ani_verify.cpp @@ -71,7 +71,7 @@ bool VerifyImpl::VerifySync(OptDataBlob const& data, DataBlob const& signature) HcfBlob signatureData = { .data = signature.data.data(), .len = signature.data.size() }; bool res = this->verify_->verify(this->verify_, inBlob, &signatureData); if (!res) { - ANI_LOGE_THROW(res, "verify verify failed."); + LOGE("verify doFinal failed."); return false; } return true; diff --git a/frameworks/js/ani/test/build_test.sh b/frameworks/js/ani/test/build_test.sh index 0c228f2..3bd49bc 100644 --- a/frameworks/js/ani/test/build_test.sh +++ b/frameworks/js/ani/test/build_test.sh @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +set -e + CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)" ROOT_PATH="$(cd "${CURRENT_DIR}/../../../../../../.." && pwd)" BASE_PATH="${ROOT_PATH}/base/security/crypto_framework" diff --git a/frameworks/js/ani/test/test_asy_key_generator.ets b/frameworks/js/ani/test/test_asy_key_generator.ets index 7765f48..f3836b5 100644 --- a/frameworks/js/ani/test/test_asy_key_generator.ets +++ b/frameworks/js/ani/test/test_asy_key_generator.ets @@ -60,6 +60,7 @@ function testAsyKeyConvertKeySync() { let skBlob = keyPair.priKey.getEncoded(); let pkStr = utils.uint8ArrayToHexStr(pkBlob.data); let skStr = utils.uint8ArrayToHexStr(skBlob.data); + console.log("AsyKeyGenerator algName: " + generator.algName); console.info('convertPemKey pubKey: ' + pkStr); console.info('convertPemKey priKey: ' + skStr); } catch (err: BusinessError) { diff --git a/frameworks/js/ani/test/test_cipher.ets b/frameworks/js/ani/test/test_cipher.ets index be1da4a..df8880f 100644 --- a/frameworks/js/ani/test/test_cipher.ets +++ b/frameworks/js/ani/test/test_cipher.ets @@ -36,6 +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); cipher.initSync(cryptoFramework.CryptoMode.ENCRYPT_MODE, symKey, params); if (algorithm.includes('GCM') || algorithm.includes('CCM')) { let encryptData = cipher.updateSync(data); diff --git a/frameworks/js/ani/test/test_md.ets b/frameworks/js/ani/test/test_md.ets index b37d589..614a5bb 100644 --- a/frameworks/js/ani/test/test_md.ets +++ b/frameworks/js/ani/test/test_md.ets @@ -27,6 +27,7 @@ function testMdSync() { }); let output = md.digestSync(); let str = utils.uint8ArrayToHexStr(output.data); + console.log("MD5 algName: " + md.algName); console.log("MD5: " + str); let length = md.getMdLength(); console.log("MD5 length: " + length); diff --git a/frameworks/js/ani/test/test_rand.ets b/frameworks/js/ani/test/test_rand.ets index b299f03..c4fe3ac 100644 --- a/frameworks/js/ani/test/test_rand.ets +++ b/frameworks/js/ani/test/test_rand.ets @@ -28,6 +28,7 @@ function testRandomSync() { }); let output = random.generateRandomSync(16); let str = utils.uint8ArrayToHexStr(output.data); + console.log("Random algName: " + random.algName); console.log("Random: " + str); } catch (err: BusinessError) { console.error(`[error] Random: ${err.code} ${err.message}`); diff --git a/frameworks/js/ani/test/test_sym_key_generator.ets b/frameworks/js/ani/test/test_sym_key_generator.ets index d216e15..56ebfc7 100644 --- a/frameworks/js/ani/test/test_sym_key_generator.ets +++ b/frameworks/js/ani/test/test_sym_key_generator.ets @@ -23,6 +23,7 @@ function testSymKeyGeneratorSync() { let symKey = generator.generateSymKeySync(); let encodedKey = symKey.getEncoded(); let str = utils.uint8ArrayToHexStr(encodedKey.data); + console.log("SymKeyGenerator algName: " + generator.algName); console.log("generateSymKey: " + str); } catch (err: BusinessError) { console.error(`[error] generateSymKey: ${err.code} ${err.message}`); diff --git a/frameworks/js/ani/test/test_verify.ets b/frameworks/js/ani/test/test_verify.ets index 62c2338..e2a26b6 100644 --- a/frameworks/js/ani/test/test_verify.ets +++ b/frameworks/js/ani/test/test_verify.ets @@ -61,7 +61,8 @@ function testVerifySync() { let verify = cryptoFramework.createVerify("SM2_256|SM3"); verify.initSync(keyPair.pubKey); verify.updateSync(inputData); - let res = verify.verifySync(null, signDataBytes) + let res = verify.verifySync(null, signDataBytes); + console.log("Verify algName: " + verify.algName); if (res === true) { console.info('Verify success'); } else { diff --git a/frameworks/js/napi/crypto/src/napi_asy_key_spec_generator.cpp b/frameworks/js/napi/crypto/src/napi_asy_key_spec_generator.cpp index 40e121b..1df4171 100644 --- a/frameworks/js/napi/crypto/src/napi_asy_key_spec_generator.cpp +++ b/frameworks/js/napi/crypto/src/napi_asy_key_spec_generator.cpp @@ -625,7 +625,7 @@ napi_value NapiAsyKeyGeneratorBySpec::CreateJsAsyKeyGeneratorBySpec(napi_env env HcfResult res = HcfAsyKeyGeneratorBySpecCreate(asyKeySpec, &generator); FreeAsyKeySpec(asyKeySpec); if (res != HCF_SUCCESS) { - napi_throw(env, GenerateBusinessError(env, res, "create C generator by sepc fail.")); + napi_throw(env, GenerateBusinessError(env, res, "create C generator by spec fail.")); LOGE("create C generator by spec fail."); return nullptr; } -- Gitee