diff --git a/frameworks/js/ani/BUILD.gn b/frameworks/js/ani/BUILD.gn index 121a489a6073ea8c04310db21209a37c35b35b7c..e760d74cfa72b6278a2fb1f863a823136a7668c3 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 87c524711c78a40711c04b02962461bde1056d5d..4bed2a4e32928ab65761600a60dcf5443ab887a3 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 279a1918f9ffcde603d0757c349cd6064caeb031..8bce08c6ab26cab7f5b2b74d5ed81a9668756864 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 8567d95e7630b58926504a2014f4287cfabbdd16..cb3a6ba68b920a2955047a5fa524767f99fd4c73 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 9373261fab57728e0e502fcb4861ab45b5515bf6..11f6579d3daa86924ca4bee2ba2ff8ffecd518f5 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 dc6180ddd7bf25dbb235abff6093e07fb4028c59..d335c59ab0dad9b62c3c968faaf226ecb27218ad 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 0000000000000000000000000000000000000000..670a713f61662fca71f25b85bae22c8cd8cdf2d2 --- /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 7b55ec5e41b6db690b575da8d27f024843484ec1..e3187c6ddca9e3bc2ab7f780fea1d73bdedc47cd 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 386418c2ad00bb1404867337e2ec6289a1f26226..c14df61616246fec01583f8fe9cf7da2c574f9bf 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 4410005c851c44f154d903828aafb96bdd28120c..6cf888598473e4af100e0b2e58e693b135e9bca5 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 2c1124dd34840eb5ceb283a959194410814d36d0..c150797b6b78693fecf10d1263366f258cb7fa2b 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 14e75b83c37fecdf984e167434b2e1fd75c6d30a..764164d4727c5d80745c87279972626562741106 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 80507c509dfe070fcf15f4a32e20609528be88a3..b1da751e0890df186d2a8d954e49881f401bcd46 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 5b4f2795f037cac1b7bb5fc3c6671cf710075cdd..915d9085760962efd2070bce6258282e4d716cbb 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 0880c78077dd3061df47cd04c443b2512c5eed92..939e96d49c5de56baf7d1ab04a1084a551643cdd 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 87e3672bfe6f62d09ae7bcbf2a23a3dd083c6762..99830f650df8dcfcc3523550fcc8bbdf86867775 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 0c228f2c3b80e6d89c3a57b681a9af40f9d7f0f4..3bd49bc1d3dbcb392b708590da8e395280f703ce 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 7765f481930cb00261ada33e16befc8941eac9a1..f3836b50776e94b21f49f6bd75ef5cb62834111b 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 be1da4a6941876f19a777210bcbbdeff4fa5e901..df8880fb14ef9b31d3ffecf95782344ed132523f 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 b37d5898f6a590478294ed04c77cd695e2ff1556..614a5bbc605b2f9e298d65ec4a6dea6177ef4bfa 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 b299f03a3a207fba2decb11b842587c42329aa82..c4fe3ac244d2edaea168f594e5ce44779c19c970 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 d216e15bf0c9960f63d2741422bc292cddda35ec..56ebfc76279d822a112fdbff78d2d8de0df2d31d 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 62c2338a168532038a2c710e9835b44e2cb8592a..e2a26b648cda7295b64a028084ddc0668cfedf8a 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 40e121b42f52dac2fe6e02a7801c517b4b08c0e2..1df4171fc673fa279f965f43a61b296b4d81b980 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; }