diff --git a/build_lite/BUILD.gn b/build_lite/BUILD.gn index 87bd9c9ac3499bc0653f81fccdf8bcc2ec815758..989a9741889a99d1a90153db4b10a1dfb4842512 100644 --- a/build_lite/BUILD.gn +++ b/build_lite/BUILD.gn @@ -12,8 +12,8 @@ # limitations under the License. import("//build/lite/config/subsystem/aafwk/config.gni") -import("//test/xts/tools/lite/build/suite_lite.gni") import("//build/lite/config/test.gni") +import("//test/xts/tools/lite/build/suite_lite.gni") lite_component("acts_component") { all_features = [] @@ -40,7 +40,9 @@ lite_component("acts_component") { "//test/xts/acts/iot_hardware_lite/iot_controller_hal:ActsWifiIotTest", "//test/xts/acts/kernel_lite/kernelcmsis_hal:ActsCMSISTest", "//test/xts/acts/utils_lite/kv_store_hal:ActsKvStoreTest", + #"//test/xts/acts/security_lite/huks_hal:ActsSecurityDataTest", + "//test/xts/acts/security_lite/huks/liteos_m_adapter:ActsHuksHalFunctionTest", "//test/xts/acts/hiviewdfx_lite/hilog_hal:ActsDfxFuncTest", "//test/xts/acts/hiviewdfx_lite/hievent_hal:ActsHieventLiteTest", @@ -59,13 +61,14 @@ lite_component("acts_component") { "//test/xts/acts/startup_lite/bootstrap_posix:ActsBootstrapTest", "//test/xts/acts/multimedia_lite/camera_lite_posix/camera_native:ActsMediaCameraTest", "//test/xts/acts/open_posix_testsuite/conformance/interfaces:ActsOpenPosixTest", + #"//test/xts/acts/multimedia_lite/media_lite_posix/player_native:ActsMediaPlayerTest", "//test/xts/acts/distributed_schedule_lite/system_ability_manager_posix:ActsSamgrTest", #"//test/xts/acts/distributedschedule_lite/distributed_schedule_posix:ActsDMSTest", "//test/xts/acts/security_lite/permission_posix/pms:ActsPMSTest", "//test/xts/acts/hiviewdfx_lite/hilog_posix:ActsHilogTest", - + "//test/xts/acts/multimedia_lite/media_lite_posix/recorder_native:ActsMediaRecorderTest", #"//test/xts/acts/graphic_lite/graphic_utils:uikit_test1", @@ -85,6 +88,7 @@ lite_component("acts_component") { all_features += [ "//test/xts/acts/utils_lite/kv_store_posix:ActsKvStoreTest", "//test/xts/acts/startup_lite/syspara_posix:ActsParameterTest", + #"//test/xts/acts/security_lite/huks_posix:ActsSecurityDataTest", #"//test/xts/acts/multimedia_lite/camera_lite_posix/camera_native:ActsMediaCameraTest", #"//test/xts/acts/multimedia_lite/media_lite_posix/player_native:ActsMediaPlayerTest", diff --git a/security_lite/BUILD.gn b/security_lite/BUILD.gn index c5a5a405b3d5e34a6f5f15e72b5746549fc5015f..6cc06f6bb1cd6e1bb8b971b7aae344d5ca8d0fcf 100755 --- a/security_lite/BUILD.gn +++ b/security_lite/BUILD.gn @@ -14,6 +14,7 @@ group("securitytest") { deps = [ #"//test/xts/acts/security_lite/huks_posix:ActsSecurityDataTest", + "//test/xts/acts/security_lite/huks/liteos_a_adapter:Hukslitetest", "//test/xts/acts/security_lite/permission_posix/capability:ActsCapabilityTest", "//test/xts/acts/security_lite/permission_posix/dac:ActsDacTest", ] diff --git a/security_lite/huks/common/BUILD.gn b/security_lite/huks/common/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ecbb46dba41211c4bd33c3b8c6b85af03916f6e0 --- /dev/null +++ b/security_lite/huks/common/BUILD.gn @@ -0,0 +1,66 @@ +# +# Copyright (c) 2020 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. +# + +config("hilog_dir") { + include_dirs = + [ "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog/" ] +} +config("hilog_lite_dir") { + include_dirs = + [ "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite/" ] +} + +config("huks_test_common") { + include_dirs = [ + "//base/security/huks/interfaces/innerkits/huks_standard/main/include", + "include", + ] +} + +static_library("huks_new_test_common") { + sources = [ + "hks_test_aes.c", + "hks_test_api_performance.c", + "hks_test_cipher.c", + "hks_test_common.c", + "hks_test_curve25519.c", + "hks_test_file_operator.c", + + #"hks_test_log.c", + "hks_test_mem.c", + ] + + deps = [] + + include_dirs = [ + "//utils/native/base/include", + "//third_party/bounds_checking_function/include", + + #"unittest/include", + "include", + ] + + configs += [ ":huks_test_common" ] + + if (defined(ohos_lite)) { + if (ohos_kernel_type == "liteos_m") { + configs += [ ":hilog_lite_dir" ] + deps += [ "//base/hiviewdfx/hilog_lite/frameworks/mini:hilog_lite" ] + } else if (ohos_kernel_type == "liteos_a") { + configs += [ ":hilog_dir" ] + deps += [ "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared" ] + } + } +} diff --git a/security_lite/huks/common/hks_test_aes.c b/security_lite/huks/common/hks_test_aes.c new file mode 100644 index 0000000000000000000000000000000000000000..20617152c113ac9c679da3f69bac63c48da6fa73 --- /dev/null +++ b/security_lite/huks/common/hks_test_aes.c @@ -0,0 +1,588 @@ +/* + * Copyright (c) 2021 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 "hks_test_aes.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_common.h" +#include "hks_test_log.h" + +#define TEST_PLAIN_TEST "This is a plain text! Hello world and thanks for watching AES^^" +#define TEST_AES_12 12 +#define TEST_AES_16 16 +#define TEST_AES_32 32 +#define TEST_AES_128 128 +#define TEST_AES_256 256 + +static uint8_t g_buffer[TEST_AES_256]; +static uint32_t g_bufferSize = TEST_AES_256; + +uint8_t g_nonce[TEST_AES_12] = "hahahahahah"; +uint8_t g_aad[TEST_AES_16] = "bababababababab"; +uint8_t g_iv[TEST_AES_16] = "aabaabaabaabaab"; + +static int32_t ConstructParamSetEncryptDecryptAesPre(uint32_t mode, uint32_t padding, bool isEncrypt, + struct HksParamSet **paramSet) +{ + int32_t ret = HksInitParamSet(paramSet); + if (ret != 0) { + HKS_TEST_LOG_E("HksAddParamInit failed!\n"); + return ret; + } + + do { + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = HKS_ALG_AES; + ret = HksAddParams(*paramSet, (const struct HksParam *)&algParam, 1); + if (ret != 0) { + HKS_TEST_LOG_E("HksAddParam algParam failed!\n"); + break; + } + + struct HksParam modeParam = {0}; + modeParam.tag = HKS_TAG_BLOCK_MODE; + modeParam.uint32Param = mode; + ret = HksAddParams(*paramSet, (const struct HksParam *)&modeParam, 1); + if (ret != 0) { + HKS_TEST_LOG_E("HksAddParam algParam failed!\n"); + break; + } + + struct HksParam padParam = {0}; + padParam.tag = HKS_TAG_PADDING; + padParam.uint32Param = padding; + ret = HksAddParams(*paramSet, (const struct HksParam *)&padParam, 1); + if (ret != 0) { + HKS_TEST_LOG_E("HksAddParam algParam failed!\n"); + break; + } + + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + if (isEncrypt) { + purposeParam.uint32Param = HKS_KEY_PURPOSE_ENCRYPT; + } else { + purposeParam.uint32Param = HKS_KEY_PURPOSE_DECRYPT; + } + ret = HksAddParams(*paramSet, (const struct HksParam *)&purposeParam, 1); + if (ret != 0) { + HKS_TEST_LOG_E("HksAddParam algParam failed!\n"); + break; + } + } while (0); + return ret; +} + +static int32_t ConstructParamSetEncryptDecryptAesPost(uint32_t mode, struct HksParamSet **paramSet) +{ + int32_t ret; + do { + struct HksParam aadParam = {0}; + aadParam.tag = HKS_TAG_ASSOCIATED_DATA; + aadParam.blob.data = g_aad; + aadParam.blob.size = sizeof(g_aad); + ret = HksAddParams(*paramSet, (const struct HksParam *)&aadParam, 1); + if (ret != 0) { + HKS_TEST_LOG_E("HksAddParam aadParam failed!\n"); + break; + } + + if (mode == HKS_MODE_CBC) { + struct HksParam ivParam = {0}; + ivParam.tag = HKS_TAG_IV; + ivParam.blob.data = g_iv; + ivParam.blob.size = sizeof(g_iv); + ret = HksAddParams(*paramSet, (const struct HksParam *)&ivParam, 1); + if (ret != 0) { + HKS_TEST_LOG_E("HksAddParam ivParam failed!\n"); + break; + } + } else { + struct HksParam nonceParam = {0}; + nonceParam.tag = HKS_TAG_NONCE; + nonceParam.blob.data = g_nonce; + nonceParam.blob.size = sizeof(g_nonce); + ret = HksAddParams(*paramSet, (const struct HksParam *)&nonceParam, 1); + if (ret != 0) { + HKS_TEST_LOG_E("HksAddParam nonceParam failed!\n"); + break; + } + } + + ret = HksBuildParamSet(paramSet); + if (ret != 0) { + HKS_TEST_LOG_E("HksAddParamFinal failed!\n"); + break; + } + } while (0); + + return ret; +} + +static int32_t ConstructParamSetEncryptDecryptAes(uint32_t mode, uint32_t padding, bool isEncrypt, + struct HksParamSet **paramSet) +{ + int32_t ret = ConstructParamSetEncryptDecryptAesPre(mode, padding, isEncrypt, paramSet); + HKS_TEST_ASSERT(ret == 0); + ret = ConstructParamSetEncryptDecryptAesPost(mode, paramSet); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +#define TEST_KEY_AUTH_ID "This is a test auth id for generate ed25519 key, keyauthIdvalid!" +void GenerateBaseKey(const struct HksBlob *alias) +{ + HKS_TEST_LOG_I("Test_Aes_gen_by_derive BASEKEY!\n"); + struct HksParamSet *paramSet = NULL; + HKS_TEST_ASSERT(HksInitParamSet(¶mSet) == 0); + + struct HksBlob authId = { strlen(TEST_KEY_AUTH_ID), (uint8_t *)TEST_KEY_AUTH_ID }; + + struct HksParam tmpParams[] = { + { .tag = HKS_TAG_ALGORITHM, + .uint32Param = HKS_ALG_ED25519 }, + { .tag = HKS_TAG_KEY_SIZE, + .uint32Param = TEST_AES_256 }, + { .tag = HKS_TAG_PURPOSE, + .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY }, + { .tag = HKS_TAG_DIGEST, + .uint32Param = HKS_DIGEST_SHA512 }, + { .tag = HKS_TAG_PADDING, + .uint32Param = HKS_PADDING_NONE }, + { .tag = HKS_TAG_KEY_AUTH_ID, + .blob = authId }, + { .tag = HKS_TAG_KEY_GENERATE_TYPE, + .uint32Param = HKS_KEY_GENERATE_TYPE_AGREE }, /* no use */ + }; + + HKS_TEST_ASSERT(HksAddParams(paramSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0])) == 0); + HKS_TEST_ASSERT(HksBuildParamSet(¶mSet) == 0); + HKS_TEST_ASSERT(HksGenerateKey(alias, paramSet, NULL) == 0); + + HksFreeParamSet(¶mSet); +} + +#define TEST_AES_KEY_BASE_NAME_1 "test_aes_key_by_derive_base_name_ed25519_1" +#define TEST_AES_KEY_BASE_NAME_2 "test_aes_key_by_derive_base_name_ed25519_2" +#define TEST_AES_KEY_BASE_NAME_1_PUBKEY "test_aes_key_by_derive_base_name_ed25519_1_pubkey" +#define TEST_AES_KEY_BASE_NAME_2_PUBKEY "test_aes_key_by_derive_base_name_ed25519_2_pubkey" + +#define TEST_AES_KEY_AGREE_NAME1 "test_aes_key_by_agree_name_keyAlias1" +#define TEST_AES_KEY_AGREE_NAME2 "test_aes_key_by_agree_name_keyAlias2" + +static void PlainPubKey(const struct HksBlob *baseKey, const struct HksBlob *peerPubKey, + struct HksParamSet *paramSet) +{ + struct HksParam tmpParams[] = { + { .tag = HKS_TAG_ALGORITHM, + .uint32Param = HKS_ALG_AES }, + { .tag = HKS_TAG_KEY_SIZE, + .uint32Param = TEST_AES_256 }, + { .tag = HKS_TAG_PURPOSE, + .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT }, + { .tag = HKS_TAG_BLOCK_MODE, + .uint32Param = HKS_MODE_GCM }, + { .tag = HKS_TAG_PADDING, + .uint32Param = HKS_PADDING_NONE }, + { .tag = HKS_TAG_KEY_GENERATE_TYPE, + .uint32Param = HKS_KEY_GENERATE_TYPE_AGREE }, + { .tag = HKS_TAG_AGREE_PRIVATE_KEY_ALIAS, + .blob = *baseKey }, + { .tag = HKS_TAG_AGREE_PUBLIC_KEY, + .blob = *peerPubKey }, + { .tag = HKS_TAG_AGREE_PUBLIC_KEY_IS_KEY_ALIAS, + .boolParam = false }, + { .tag = HKS_TAG_AGREE_ALG, + .uint32Param = HKS_ALG_ED25519 }, + }; + + HKS_TEST_ASSERT(HksAddParams(paramSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0])) == 0); +} + +static void SetKeyAliasTrue(const struct HksBlob *baseKey, const struct HksBlob *peerPubKey, + struct HksParamSet *paramSet) +{ + struct HksParam tmpParams[] = { + { .tag = HKS_TAG_ALGORITHM, + .uint32Param = HKS_ALG_AES }, + { .tag = HKS_TAG_KEY_SIZE, + .uint32Param = TEST_AES_256 }, + { .tag = HKS_TAG_PURPOSE, + .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT }, + { .tag = HKS_TAG_BLOCK_MODE, + .uint32Param = HKS_MODE_GCM }, + { .tag = HKS_TAG_PADDING, + .uint32Param = HKS_PADDING_NONE }, + { .tag = HKS_TAG_KEY_GENERATE_TYPE, + .uint32Param = HKS_KEY_GENERATE_TYPE_AGREE }, + { .tag = HKS_TAG_AGREE_PRIVATE_KEY_ALIAS, + .blob = *baseKey }, + { .tag = HKS_TAG_AGREE_PUBLIC_KEY_IS_KEY_ALIAS, + .boolParam = true }, + { .tag = HKS_TAG_AGREE_PUBLIC_KEY, + .blob = *peerPubKey }, + { .tag = HKS_TAG_AGREE_ALG, + .uint32Param = HKS_ALG_ED25519 }, + }; + + HKS_TEST_ASSERT(HksAddParams(paramSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0])) == 0); +} + +static void SetKeyAliasWrong(const struct HksBlob *baseKey, const struct HksBlob *peerPubKey, + struct HksParamSet *paramSet) +{ + struct HksParam tmpParams[] = { + { .tag = HKS_TAG_ALGORITHM, + .uint32Param = HKS_ALG_AES }, + { .tag = HKS_TAG_KEY_SIZE, + .uint32Param = TEST_AES_256 }, + { .tag = HKS_TAG_PURPOSE, + .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT }, + { .tag = HKS_TAG_BLOCK_MODE, + .uint32Param = HKS_MODE_GCM }, + { .tag = HKS_TAG_PADDING, + .uint32Param = HKS_PADDING_NONE }, + { .tag = HKS_TAG_KEY_GENERATE_TYPE, + .uint32Param = HKS_KEY_GENERATE_TYPE_AGREE }, + { .tag = HKS_TAG_AGREE_PRIVATE_KEY_ALIAS, + .blob = *baseKey }, + { .tag = HKS_TAG_AGREE_PUBLIC_KEY, + .blob = *peerPubKey }, + { .tag = HKS_TAG_AGREE_ALG, + .uint32Param = HKS_ALG_ED25519 }, + }; + + HKS_TEST_ASSERT(HksAddParams(paramSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0])) == 0); +} + +static void GenerateAesAgreeKey(const struct HksBlob *alias, const struct HksBlob *baseKey, + const struct HksBlob *peerPubKey, bool isPlainPubKey, bool isSetKeyAliasTrue) +{ + HKS_TEST_LOG_I("Test_Aes_gen_by_agree key!\n"); + struct HksParamSet *paramSet = NULL; + HKS_TEST_ASSERT(HksInitParamSet(¶mSet) == 0); + + if (isPlainPubKey) { + PlainPubKey(baseKey, peerPubKey, paramSet); + } else { + if (isSetKeyAliasTrue) { + SetKeyAliasTrue(baseKey, peerPubKey, paramSet); + } else { + SetKeyAliasWrong(baseKey, peerPubKey, paramSet); + } + } + HKS_TEST_ASSERT(HksBuildParamSet(¶mSet) == 0); + HKS_TEST_ASSERT(HksGenerateKey(alias, paramSet, NULL) == 0); + HKS_TEST_LOG_I("End Test_Aes_gen_by_agree key!\n"); + + HksFreeParamSet(¶mSet); +} + +static void ExportPubKey(const struct HksBlob *alias, struct HksBlob *pubKey) +{ + HKS_TEST_ASSERT(HksExportPublicKey(alias, NULL, pubKey) == 0); +} + +static void ImportPubKey(const struct HksBlob *alias, const struct HksBlob *pubKey) +{ + HKS_TEST_LOG_I("Test_Import pubKey!\n"); + struct HksParamSet *paramSet = NULL; + HKS_TEST_ASSERT(HksInitParamSet(¶mSet) == 0); + + struct HksBlob authId = { strlen(TEST_KEY_AUTH_ID), (uint8_t *)TEST_KEY_AUTH_ID }; + + struct HksParam tmpParams[] = { + { .tag = HKS_TAG_ALGORITHM, + .uint32Param = HKS_ALG_ED25519 }, + { .tag = HKS_TAG_KEY_SIZE, + .uint32Param = TEST_AES_256 }, + { .tag = HKS_TAG_PURPOSE, + .uint32Param = HKS_KEY_PURPOSE_VERIFY }, + { .tag = HKS_TAG_DIGEST, + .uint32Param = HKS_DIGEST_SHA512 }, + { .tag = HKS_TAG_PADDING, + .uint32Param = HKS_PADDING_NONE }, + { .tag = HKS_TAG_KEY_AUTH_ID, + .blob = authId }, + { .tag = HKS_TAG_KEY_GENERATE_TYPE, + .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT }, /* no use */ + }; + + HKS_TEST_ASSERT(HksAddParams(paramSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0])) == 0); + HKS_TEST_ASSERT(HksBuildParamSet(¶mSet) == 0); + HKS_TEST_ASSERT(HksImportKey(alias, paramSet, pubKey) == 0); + + HksFreeParamSet(¶mSet); +} + +#define TEST_AES_KEY_BASE_NAME "test_aes_key_by_derive_base_name_ed25519" +#define TEST_AES_KEY_DERIVE_NAME1 "test_aes_key_by_derive_name_keyAlias1" +#define TEST_AES_KEY_DERIVE_NAME2 "test_aes_key_by_derive_name_keyAlias2" +int32_t TestAes256ByAgree() +{ + HKS_TEST_LOG_I("enter"); + /* generate ed25519 key */ + struct HksBlob baseKeyAlias1 = { strlen(TEST_AES_KEY_BASE_NAME_1), (uint8_t *)TEST_AES_KEY_BASE_NAME_1 }; + GenerateBaseKey(&baseKeyAlias1); + + struct HksBlob baseKeyAlias2 = { strlen(TEST_AES_KEY_BASE_NAME_2), (uint8_t *)TEST_AES_KEY_BASE_NAME_2 }; + GenerateBaseKey(&baseKeyAlias2); + + uint8_t pubKey1[TEST_AES_128] = {0}; + uint8_t pubKey2[TEST_AES_128] = {0}; + struct HksBlob pubKeyBlob1 = { TEST_AES_128, pubKey1 }; + struct HksBlob pubKeyBlob2 = { TEST_AES_128, pubKey2 }; + ExportPubKey(&baseKeyAlias1, &pubKeyBlob1); + ExportPubKey(&baseKeyAlias2, &pubKeyBlob2); + + struct HksBlob pubKeyAlias1 = { + strlen(TEST_AES_KEY_BASE_NAME_1_PUBKEY), (uint8_t *)TEST_AES_KEY_BASE_NAME_1_PUBKEY + }; + struct HksBlob pubKeyAlias2 = { + strlen(TEST_AES_KEY_BASE_NAME_2_PUBKEY), (uint8_t *)TEST_AES_KEY_BASE_NAME_2_PUBKEY + }; + ImportPubKey(&pubKeyAlias1, &pubKeyBlob1); + ImportPubKey(&pubKeyAlias2, &pubKeyBlob2); + + /* generate aes key 1 */ + struct HksBlob aesKeyAlias1 = { strlen(TEST_AES_KEY_AGREE_NAME1), (uint8_t *)TEST_AES_KEY_AGREE_NAME1 }; + GenerateAesAgreeKey(&aesKeyAlias1, &baseKeyAlias1, &pubKeyAlias2, false, false); + + /* generate aes key 2 */ + struct HksBlob aesKeyAlias2 = { strlen(TEST_AES_KEY_DERIVE_NAME2), (uint8_t *)TEST_AES_KEY_DERIVE_NAME2 }; + GenerateAesAgreeKey(&aesKeyAlias2, &baseKeyAlias2, &pubKeyAlias1, false, false); + + /* encrypt by aes key 1 */ + struct HksParamSet *paramSet = NULL; + int32_t ret = ConstructParamSetEncryptDecryptAes(HKS_MODE_GCM, HKS_PADDING_NONE, true, ¶mSet); + HKS_TEST_ASSERT(ret == 0); + struct HksBlob plainText1 = { strlen(TEST_PLAIN_TEST) + 1, (uint8_t*)TEST_PLAIN_TEST }; + struct HksBlob cipherText1 = { TEST_AES_256, g_buffer }; + (void)memset_s(cipherText1.data, cipherText1.size, 0, cipherText1.size); + HKS_TEST_ASSERT(HksEncrypt(&aesKeyAlias1, paramSet, &plainText1, &cipherText1) == 0); + g_bufferSize = cipherText1.size; + + HksFreeParamSet(¶mSet); + + /* decrypt by aes key 2 */ + ret = ConstructParamSetEncryptDecryptAes(HKS_MODE_GCM, HKS_PADDING_NONE, false, ¶mSet); + HKS_TEST_ASSERT(ret == 0); + struct HksBlob cipherText = { g_bufferSize, g_buffer }; + uint8_t tmp[TEST_AES_256] = {0}; + struct HksBlob plainText = { TEST_AES_256, tmp }; + ret = HksDecrypt(&aesKeyAlias2, paramSet, &cipherText, &plainText); + HKS_TEST_ASSERT(ret == 0); + HKS_TEST_LOG_I("ConstructParamSetEncryptDecryptAes plainText: %s", plainText.data); + HksFreeParamSet(¶mSet); + + HksDeleteKey(&baseKeyAlias1, NULL); + HksDeleteKey(&baseKeyAlias2, NULL); + HksDeleteKey(&pubKeyAlias1, NULL); + HksDeleteKey(&pubKeyAlias2, NULL); + HksDeleteKey(&aesKeyAlias1, NULL); + HksDeleteKey(&aesKeyAlias2, NULL); + HKS_TEST_LOG_I("end"); + return ret; +} + +int32_t TestAes256ByAgree1() +{ + HKS_TEST_LOG_I("enter"); + + /* generate ed25519 key */ + struct HksBlob baseKeyAlias1 = { strlen(TEST_AES_KEY_BASE_NAME_1), (uint8_t *)TEST_AES_KEY_BASE_NAME_1 }; + GenerateBaseKey(&baseKeyAlias1); + + struct HksBlob baseKeyAlias2 = { strlen(TEST_AES_KEY_BASE_NAME_2), (uint8_t *)TEST_AES_KEY_BASE_NAME_2 }; + GenerateBaseKey(&baseKeyAlias2); + + uint8_t pubKey1[TEST_AES_128] = {0}; + uint8_t pubKey2[TEST_AES_128] = {0}; + struct HksBlob pubKeyBlob1 = { TEST_AES_128, pubKey1 }; + struct HksBlob pubKeyBlob2 = { TEST_AES_128, pubKey2 }; + ExportPubKey(&baseKeyAlias1, &pubKeyBlob1); + ExportPubKey(&baseKeyAlias2, &pubKeyBlob2); + + struct HksBlob pubKeyAlias1 = { + strlen(TEST_AES_KEY_BASE_NAME_1_PUBKEY), (uint8_t *)TEST_AES_KEY_BASE_NAME_1_PUBKEY + }; + struct HksBlob pubKeyAlias2 = { + strlen(TEST_AES_KEY_BASE_NAME_2_PUBKEY), (uint8_t *)TEST_AES_KEY_BASE_NAME_2_PUBKEY + }; + ImportPubKey(&pubKeyAlias1, &pubKeyBlob1); + ImportPubKey(&pubKeyAlias2, &pubKeyBlob2); + + /* generate aes key 1 */ + struct HksBlob aesKeyAlias1 = { strlen(TEST_AES_KEY_AGREE_NAME1), (uint8_t *)TEST_AES_KEY_AGREE_NAME1 }; + GenerateAesAgreeKey(&aesKeyAlias1, &baseKeyAlias1, &pubKeyAlias2, false, true); + + /* generate aes key 2 */ + struct HksBlob aesKeyAlias2 = { strlen(TEST_AES_KEY_DERIVE_NAME2), (uint8_t *)TEST_AES_KEY_DERIVE_NAME2 }; + GenerateAesAgreeKey(&aesKeyAlias2, &baseKeyAlias2, &pubKeyAlias1, false, true); + + /* encrypt by aes key 1 */ + struct HksParamSet *paramSet = NULL; + int32_t ret = ConstructParamSetEncryptDecryptAes(HKS_MODE_GCM, HKS_PADDING_NONE, true, ¶mSet); + HKS_TEST_ASSERT(ret == 0); + struct HksBlob plainText1 = { strlen(TEST_PLAIN_TEST) + 1, (uint8_t*)TEST_PLAIN_TEST }; + struct HksBlob cipherText1 = { TEST_AES_256, g_buffer }; + (void)memset_s(cipherText1.data, cipherText1.size, 0, cipherText1.size); + HKS_TEST_ASSERT(HksEncrypt(&aesKeyAlias1, paramSet, &plainText1, &cipherText1) == 0); + g_bufferSize = cipherText1.size; + + HksFreeParamSet(¶mSet); + + /* decrypt by aes key 2 */ + ret = ConstructParamSetEncryptDecryptAes(HKS_MODE_GCM, HKS_PADDING_NONE, false, ¶mSet); + HKS_TEST_ASSERT(ret == 0); + struct HksBlob cipherText = { g_bufferSize, g_buffer }; + uint8_t tmp[TEST_AES_256] = {0}; + struct HksBlob plainText = { TEST_AES_256, tmp }; + ret = HksDecrypt(&aesKeyAlias2, paramSet, &cipherText, &plainText); + HKS_TEST_ASSERT(ret == 0); + HKS_TEST_LOG_I("ConstructParamSetEncryptDecryptAes plainText: %s", plainText.data); + HksFreeParamSet(¶mSet); + + HksDeleteKey(&baseKeyAlias1, NULL); + HksDeleteKey(&baseKeyAlias2, NULL); + HksDeleteKey(&pubKeyAlias1, NULL); + HksDeleteKey(&pubKeyAlias2, NULL); + HksDeleteKey(&aesKeyAlias1, NULL); + HksDeleteKey(&aesKeyAlias2, NULL); + HKS_TEST_LOG_I("end"); + return ret; +} + +int32_t TestAes256ByAgree2() +{ + HKS_TEST_LOG_I("enter"); + /* generate ed25519 key */ + struct HksBlob baseKeyAlias1 = { strlen(TEST_AES_KEY_BASE_NAME_1), (uint8_t *)TEST_AES_KEY_BASE_NAME_1 }; + GenerateBaseKey(&baseKeyAlias1); + + struct HksBlob baseKeyAlias2 = { strlen(TEST_AES_KEY_BASE_NAME_2), (uint8_t *)TEST_AES_KEY_BASE_NAME_2 }; + GenerateBaseKey(&baseKeyAlias2); + + uint8_t pubKey1[TEST_AES_128] = {0}; + uint8_t pubKey2[TEST_AES_128] = {0}; + struct HksBlob pubKeyBlob1 = { TEST_AES_128, pubKey1 }; + struct HksBlob pubKeyBlob2 = { TEST_AES_128, pubKey2 }; + ExportPubKey(&baseKeyAlias1, &pubKeyBlob1); + ExportPubKey(&baseKeyAlias2, &pubKeyBlob2); + + /* generate aes key 1 */ + struct HksBlob aesKeyAlias1 = { strlen(TEST_AES_KEY_AGREE_NAME1), (uint8_t *)TEST_AES_KEY_AGREE_NAME1 }; + GenerateAesAgreeKey(&aesKeyAlias1, &baseKeyAlias1, &pubKeyBlob2, true, false); + + /* generate aes key 2 */ + struct HksBlob aesKeyAlias2 = { strlen(TEST_AES_KEY_DERIVE_NAME2), (uint8_t *)TEST_AES_KEY_DERIVE_NAME2 }; + GenerateAesAgreeKey(&aesKeyAlias2, &baseKeyAlias2, &pubKeyBlob1, true, false); + + /* encrypt by aes key 1 */ + struct HksParamSet *paramSet = NULL; + int32_t ret = ConstructParamSetEncryptDecryptAes(HKS_MODE_GCM, HKS_PADDING_NONE, true, ¶mSet); + HKS_TEST_ASSERT(ret == 0); + struct HksBlob plainText1 = { strlen(TEST_PLAIN_TEST) + 1, (uint8_t*)TEST_PLAIN_TEST }; + struct HksBlob cipherText1 = { TEST_AES_256, g_buffer }; + (void)memset_s(cipherText1.data, cipherText1.size, 0, cipherText1.size); + HKS_TEST_ASSERT(HksEncrypt(&aesKeyAlias1, paramSet, &plainText1, &cipherText1) == 0); + g_bufferSize = cipherText1.size; + + HksFreeParamSet(¶mSet); + + /* decrypt by aes key 2 */ + ret = ConstructParamSetEncryptDecryptAes(HKS_MODE_GCM, HKS_PADDING_NONE, false, ¶mSet); + HKS_TEST_ASSERT(ret == 0); + struct HksBlob cipherText = { g_bufferSize, g_buffer }; + uint8_t tmp[TEST_AES_256] = {0}; + struct HksBlob plainText = { TEST_AES_256, tmp }; + ret = HksDecrypt(&aesKeyAlias2, paramSet, &cipherText, &plainText); + HKS_TEST_ASSERT(ret == 0); + HKS_TEST_LOG_I("ConstructParamSetEncryptDecryptAes plainText: %s", plainText.data); + HksFreeParamSet(¶mSet); + + HksDeleteKey(&baseKeyAlias1, NULL); + HksDeleteKey(&baseKeyAlias2, NULL); + HksDeleteKey(&aesKeyAlias1, NULL); + HksDeleteKey(&aesKeyAlias2, NULL); + HKS_TEST_LOG_I("end"); + return ret; +} + +int32_t TestAes256ByLocal() +{ + HKS_TEST_LOG_I("enter"); + + /* generate aes key 1 */ + char testKey[TEST_AES_32]; + struct HksBlob keyBlob; + + for (int i = 0; i < TEST_AES_32; ++i) { + testKey[i] = i + TEST_AES_32; + } + + keyBlob.data = (uint8_t *)(testKey); + keyBlob.size = sizeof(testKey); + + /* encrypt by aes key 1 */ + struct HksParamSet *paramSet = NULL; + struct HksParam algParam = { + .tag = HKS_TAG_IS_KEY_ALIAS, + .uint32Param = 0 + }; + + int32_t ret = ConstructParamSetEncryptDecryptAes(HKS_MODE_GCM, HKS_PADDING_NONE, true, ¶mSet); + HKS_TEST_ASSERT(ret == 0); + + ret = HksAddParams(paramSet, (const struct HksParam *)&algParam, 1); + if (ret != 0) { + HKS_TEST_LOG_E("HksAddParam algParam failed!\n"); + HksFreeParamSet(¶mSet); + return ret; + } + + struct HksBlob plainText1 = { strlen(TEST_PLAIN_TEST) + 1, (uint8_t*)TEST_PLAIN_TEST }; + struct HksBlob cipherText1 = { TEST_AES_256, g_buffer }; + (void)memset_s(cipherText1.data, cipherText1.size, 0, cipherText1.size); + HKS_TEST_ASSERT(HksEncrypt(&keyBlob, paramSet, &plainText1, &cipherText1) == 0); + g_bufferSize = cipherText1.size; + + HksFreeParamSet(¶mSet); + + /* decrypt by aes key 2 */ + ret = ConstructParamSetEncryptDecryptAes(HKS_MODE_GCM, HKS_PADDING_NONE, false, ¶mSet); + HKS_TEST_ASSERT(ret == 0); + algParam.tag = HKS_TAG_IS_KEY_ALIAS; + algParam.uint32Param = 0; + ret = HksAddParams(paramSet, (const struct HksParam *)&algParam, 1); + if (ret != 0) { + HKS_TEST_LOG_E("HksAddParam algParam failed!\n"); + HksFreeParamSet(¶mSet); + return ret; + } + + struct HksBlob cipherText = { g_bufferSize, g_buffer }; + uint8_t tmp[TEST_AES_256] = {0}; + struct HksBlob plainText = { TEST_AES_256, tmp }; + ret = HksDecrypt(&keyBlob, paramSet, &cipherText, &plainText); + HKS_TEST_ASSERT(ret == 0); + HKS_TEST_ASSERT(memcmp(plainText.data, plainText1.data, plainText.size) == 0); + HksFreeParamSet(¶mSet); + + HKS_TEST_LOG_I("end"); + return ret; +} + diff --git a/security_lite/huks/common/hks_test_api_performance.c b/security_lite/huks/common/hks_test_api_performance.c new file mode 100644 index 0000000000000000000000000000000000000000..30989871193cd363ac39fc7ef018f4d0c35d3748 --- /dev/null +++ b/security_lite/huks/common/hks_test_api_performance.c @@ -0,0 +1,233 @@ +/* + * Copyright (c) 2021 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 + +#include "hks_test_api_performance.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_log.h" + +#define USEC_MINT 1000 +#define USEC_UINT 1000000 + +int32_t HksGenerateKeyRun(const struct HksBlob *keyAlias, const struct HksParamSet *paramSetIn, + struct HksParamSet *paramSetOut, uint32_t performTimes) +{ + int32_t ret; + /* if keyAlias exist, the key will be generated and refreshed */ + for (uint32_t i = 0; i < performTimes; ++i) { + ret = HksGenerateKey(keyAlias, paramSetIn, paramSetOut); + if (ret != HKS_SUCCESS) { + return ret; + } + } + return HKS_SUCCESS; +} + +int32_t HksEncryptRun(const struct HksBlob *key, const struct HksParamSet *paramSet, + const struct HksBlob *plainText, struct HksBlob *cipherText, uint32_t performTimes) +{ + int32_t ret; + uint32_t oriCipherTestSize = 0; + if (cipherText != NULL) { + oriCipherTestSize = cipherText->size; + } + + for (uint32_t i = 0; i < performTimes; ++i) { + if (cipherText != NULL) { + (void)memset_s(cipherText->data, oriCipherTestSize, 0, oriCipherTestSize); + cipherText->size = oriCipherTestSize; + } + + ret = HksEncrypt(key, paramSet, plainText, cipherText); + if (ret != HKS_SUCCESS) { + return ret; + } + } + return HKS_SUCCESS; +} + +int32_t HksDecryptRun(const struct HksBlob *key, const struct HksParamSet *paramSet, + const struct HksBlob *cipherText, struct HksBlob *plainText, uint32_t performTimes) +{ + int32_t ret; + uint32_t oriPlainTextSize = 0; + if (plainText != NULL) { + oriPlainTextSize = plainText->size; + } + + for (uint32_t i = 0; i < performTimes; ++i) { + if (plainText != NULL) { + (void)memset_s(plainText->data, oriPlainTextSize, 0, oriPlainTextSize); + plainText->size = oriPlainTextSize; + } + ret = HksDecrypt(key, paramSet, cipherText, plainText); + if (ret != HKS_SUCCESS) { + return ret; + } + } + return HKS_SUCCESS; +} + +int32_t HksMacRun(const struct HksBlob *key, const struct HksParamSet *paramSet, + const struct HksBlob *srcData, struct HksBlob *mac, uint32_t performTimes) +{ + int32_t ret; + uint32_t oriMacSize = 0; + if (mac != NULL) { + oriMacSize = mac->size; + } + + for (uint32_t i = 0; i < performTimes; ++i) { + if (mac != NULL) { + (void)memset_s(mac->data, oriMacSize, 0, oriMacSize); + mac->size = oriMacSize; + } + ret = HksMac(key, paramSet, srcData, mac); + if (ret != HKS_SUCCESS) { + return ret; + } + } + return HKS_SUCCESS; +} + +int32_t HksDeriveKeyRun(const struct HksParamSet *paramSet, const struct HksBlob *masterKey, + struct HksBlob *derivedKey, uint32_t performTimes) +{ + int32_t ret; + uint32_t oriDerivedKeySize = 0; + if (derivedKey != NULL) { + oriDerivedKeySize = derivedKey->size; + } + + for (uint32_t i = 0; i < performTimes; ++i) { + if (derivedKey != NULL) { + (void)memset_s(derivedKey->data, oriDerivedKeySize, 0, oriDerivedKeySize); + derivedKey->size = oriDerivedKeySize; + } + ret = HksDeriveKey(paramSet, masterKey, derivedKey); + if (ret != HKS_SUCCESS) { + return ret; + } + } + return HKS_SUCCESS; +} + +int32_t HksDeleteKeyRun(const struct HksBlob *keyAlias, uint32_t performTimes) +{ + (void)performTimes; + return HksDeleteKey(keyAlias, NULL); +} + +int32_t HksKeyExistRun(const struct HksBlob *keyAlias, uint32_t performTimes) +{ + int32_t ret; + for (uint32_t i = 0; i < performTimes; ++i) { + ret = HksKeyExist(keyAlias, NULL); + if (ret != HKS_SUCCESS) { + return ret; + } + } + return HKS_SUCCESS; +} + +int32_t HksGenerateRandomRun(struct HksBlob *random, uint32_t performTimes) +{ + int32_t ret; + uint32_t oriRandomSize = 0; + if (random != NULL) { + oriRandomSize = random->size; + } + + for (uint32_t i = 0; i < performTimes; ++i) { + if (random != NULL) { + (void)memset_s(random->data, oriRandomSize, 0, oriRandomSize); + random->size = oriRandomSize; + } + ret = HksGenerateRandom(NULL, random); + if (ret != HKS_SUCCESS) { + return ret; + } + } + return HKS_SUCCESS; +} + +int32_t HksAgreeKeyRun(const struct HksParamSet *paramSet, const struct HksBlob *privateKey, + const struct HksBlob *peerPublicKey, struct HksBlob *agreedKey, uint32_t performTimes) +{ + int32_t ret; + uint32_t oriAgreedKeySize = 0; + if (agreedKey != NULL) { + oriAgreedKeySize = agreedKey->size; + } + + for (uint32_t i = 0; i < performTimes; ++i) { + if (agreedKey != NULL) { + (void)memset_s(agreedKey->data, oriAgreedKeySize, 0, oriAgreedKeySize); + agreedKey->size = oriAgreedKeySize; + } + ret = HksAgreeKey(paramSet, privateKey, peerPublicKey, agreedKey); + if (ret != HKS_SUCCESS) { + return ret; + } + } + return HKS_SUCCESS; +} + +int32_t HksHashRun(const struct HksParamSet *paramSet, + const struct HksBlob *srcData, struct HksBlob *hash, uint32_t performTimes) +{ + int32_t ret; + uint32_t oriHashSize = 0; + if (hash != NULL) { + oriHashSize = hash->size; + } + + for (uint32_t i = 0; i < performTimes; ++i) { + if (hash != NULL) { + (void)memset_s(hash->data, oriHashSize, 0, oriHashSize); + hash->size = oriHashSize; + } + + ret = HksHash(paramSet, srcData, hash); + if (ret != HKS_SUCCESS) { + return ret; + } + } + return HKS_SUCCESS; +} + +int32_t HksBnExpModRun(struct HksBlob *x, const struct HksBlob *a, + const struct HksBlob *e, const struct HksBlob *n, uint32_t performTimes) +{ + int32_t ret; + uint32_t oriXSize = 0; + if (x != NULL) { + oriXSize = x->size; + } + + for (uint32_t i = 0; i < performTimes; ++i) { + if (x != NULL) { + (void)memset_s(x->data, oriXSize, 0, oriXSize); + x->size = oriXSize; + } + ret = HksBnExpMod(x, a, e, n); + if (ret != HKS_SUCCESS) { + return ret; + } + } + return HKS_SUCCESS; +} diff --git a/security_lite/huks/common/hks_test_cipher.c b/security_lite/huks/common/hks_test_cipher.c new file mode 100644 index 0000000000000000000000000000000000000000..7d6d57ff22e50ff5eed6528ca9de0c2ab1036975 --- /dev/null +++ b/security_lite/huks/common/hks_test_cipher.c @@ -0,0 +1,743 @@ +/* + * Copyright (c) 2021 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 "hks_test_cipher.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" + +#define DEFAULT_AES_CIPHER_PLAIN_SIZE 1000 +#define AES_DEFAULT_GCM_NONCE_LENGTH 12 +#define AES_DEFAULT_AAD_LEN 4 +#define DEFAULT_AES_LOCAL_PARAM_SET_OUT_SIZE 256 +#define AES_LOCAL_KEY_SIZE_128 16 +#define AES_LOCAL_KEY_SIZE_256 32 + +static const struct HksTestCipherParams g_testCipherParams[] = { + /* success: local aes256-gcm-none */ + { 0, HKS_SUCCESS, { false, 0, false, 0 }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + true, HKS_STORAGE_TEMP + }, + { false, 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* encrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_ENCRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN, + true, false + }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* decrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN, + true, false + }, + { + true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { + true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16 }, + { + true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { + true, AES_LOCAL_KEY_SIZE_256, true, AES_LOCAL_KEY_SIZE_256 } + }, + + /* success: aes256-gcm-none */ + { 1, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0 + }, + { false, 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* encrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_ENCRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN + }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* decrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN + }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16 }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { false, 0, false, 0 } + }, +}; + +static const struct HksTestEncryptParams g_testEncryptParams[] = { + /* aes gcm nonce not set */ + { 0, HKS_ERROR_CHECK_GET_NONCE_FAIL, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM + }, + { 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* encrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_ENCRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + false, 0, + true, AES_DEFAULT_AAD_LEN, + false, true + }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16 }, + { false, 0, false, 0 } + }, + + /* aes gcm nonce size invalid */ + { 1, HKS_ERROR_INVALID_NONCE, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM + }, + { 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* encrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_ENCRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH - 1, + true, AES_DEFAULT_AAD_LEN, + false, true + }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16 }, + { false, 0, false, 0 } + }, + + /* aes gcm aad not set */ + { 2, HKS_ERROR_CHECK_GET_AAD_FAIL, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM + }, + { 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* encrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_ENCRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + false, 0, + false, true + }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16 }, + { false, 0, false, 0 } + }, + /* aes gcm inSize == 0 */ + { 3, HKS_ERROR_INVALID_ARGUMENT, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM + }, + { 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* encrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_ENCRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN, + false, true + }, + { true, 0, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16 }, + { false, 0, false, 0 } + }, + + /* aes gcm outSize too small */ + { 4, HKS_ERROR_BUFFER_TOO_SMALL, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM }, + { 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* encrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_ENCRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN, + false, true + }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16 - 1, true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16 - 1 }, + { false, 0, false, 0 } + }, +}; + +static const struct HksTestDecryptParams g_testDecryptParams[] = { + /* aes gcm nonce not set */ + { 0, HKS_ERROR_CHECK_GET_NONCE_FAIL, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM + }, + { 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* decrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + false, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN, + false, true + }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE - 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE - 16 }, + { false, 0, false, 0 } + }, + + /* aes gcm nonce size invalid */ + { 1, HKS_ERROR_INVALID_NONCE, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM + }, + { 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* decrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH - 1, + true, AES_DEFAULT_AAD_LEN, + false, true + }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE - 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE - 16 }, + { false, 0, false, 0 } + }, + + /* aes gcm aad not set */ + { 2, HKS_ERROR_CHECK_GET_AAD_FAIL, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM + }, + { 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* decrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + false, 0, + false, true + }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE - 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE - 16 }, + { false, 0, false, 0 } + }, + /* aes gcm inSize <= 16 */ + { 3, HKS_ERROR_INVALID_ARGUMENT, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM + }, + { 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* decrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN, + false, true + }, + { true, 16, true, 16 }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE - 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE - 16 }, + { false, 0, false, 0 } + }, + + /* aes gcm outSize too small */ + { 4, HKS_ERROR_BUFFER_TOO_SMALL, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM + }, + { 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* decrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN, + false, true + }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE - 16 - 1, true, DEFAULT_AES_CIPHER_PLAIN_SIZE - 16 -1 }, + { false, 0, false, 0 } + }, +}; + +static int32_t ConstructDataToBlob(struct HksBlob **inData, struct HksBlob **outData, + const struct HksTestBlobParams *inTextParams, const struct HksTestBlobParams *outTextParams) +{ + int32_t ret = TestConstuctBlob(inData, + inTextParams->blobExist, + inTextParams->blobSize, + inTextParams->blobDataExist, + inTextParams->blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + ret = TestConstuctBlob(outData, + outTextParams->blobExist, + outTextParams->blobSize, + outTextParams->blobDataExist, + outTextParams->blobDataSize); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +static int32_t Encrypt(struct CipherEncryptStructure *encryptStruct) +{ + int32_t ret; + struct HksParamSet *encryptParamSet = NULL; + + if (encryptStruct->cipherParms->cipherType == HKS_TEST_CIPHER_TYPE_RSA) { + struct TestRsaCipherParamSet rsaCipherParamStructure = { + &encryptParamSet, + encryptStruct->cipherParms->paramSetExist, + encryptStruct->cipherParms->setAlg, encryptStruct->cipherParms->alg, + encryptStruct->cipherParms->setPurpose, encryptStruct->cipherParms->purpose, + encryptStruct->cipherParms->setDigest, encryptStruct->cipherParms->digest, + encryptStruct->cipherParms->setPadding, encryptStruct->cipherParms->padding + }; + ret = TestConstructRsaCipherParamSet(&rsaCipherParamStructure); + } else { + uint32_t ivSize = encryptStruct->cipherParms->ivSize; + uint32_t nonceSize = encryptStruct->cipherParms->nonceSize; + uint32_t aadSize = encryptStruct->cipherParms->aadSize; + if (ivSize != 0) { + ret = TestConstuctBlob(encryptStruct->ivData, true, ivSize, true, ivSize); + HKS_TEST_ASSERT(ret == 0); + } + if (nonceSize != 0) { + ret = TestConstuctBlob(encryptStruct->nonceData, true, nonceSize, true, nonceSize); + HKS_TEST_ASSERT(ret == 0); + } + if (aadSize != 0) { + ret = TestConstuctBlob(encryptStruct->aadData, true, aadSize, true, aadSize); + HKS_TEST_ASSERT(ret == 0); + } + struct AesCipherParamSetStructure enParamStruct = { + &encryptParamSet, + encryptStruct->cipherParms->paramSetExist, + encryptStruct->cipherParms->setAlg, encryptStruct->cipherParms->alg, + encryptStruct->cipherParms->setPurpose, encryptStruct->cipherParms->purpose, + encryptStruct->cipherParms->setPadding, encryptStruct->cipherParms->padding, + encryptStruct->cipherParms->setBlockMode, encryptStruct->cipherParms->mode, + encryptStruct->cipherParms->setIv, *(encryptStruct->ivData), + encryptStruct->cipherParms->setNonce, *(encryptStruct->nonceData), + encryptStruct->cipherParms->setAad, *(encryptStruct->aadData), + encryptStruct->cipherParms->setIsKeyAlias, encryptStruct->cipherParms->isKeyAlias + }; + ret = TestConstructAesCipherParamSet(&enParamStruct); + HKS_TEST_ASSERT(ret == 0); + } + + ret = HksEncryptRun(encryptStruct->keyAlias, encryptParamSet, encryptStruct->plainData, encryptStruct->cipherData, + encryptStruct->performTimes); + HksFreeParamSet(&encryptParamSet); + return ret; +} + +static int32_t DecryptCipher(struct CipherDecryptStructure *decryptStruct) +{ + int32_t ret = TestConstuctBlob(decryptStruct->decryptedData, + decryptStruct->cipherParms->decryptedTextParams.blobExist, + decryptStruct->cipherParms->decryptedTextParams.blobSize, + decryptStruct->cipherParms->decryptedTextParams.blobDataExist, + decryptStruct->cipherParms->decryptedTextParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *decryptParamSet = NULL; + if (decryptStruct->cipherParms->decryptParamSetParams.cipherType == HKS_TEST_CIPHER_TYPE_RSA) { + struct TestRsaCipherParamSet rsaDeCipherParamStructure = { + &decryptParamSet, + decryptStruct->cipherParms->decryptParamSetParams.paramSetExist, + decryptStruct->cipherParms->decryptParamSetParams.setAlg, + decryptStruct->cipherParms->decryptParamSetParams.alg, + decryptStruct->cipherParms->decryptParamSetParams.setPurpose, + decryptStruct->cipherParms->decryptParamSetParams.purpose, + decryptStruct->cipherParms->decryptParamSetParams.setDigest, + decryptStruct->cipherParms->decryptParamSetParams.digest, + decryptStruct->cipherParms->decryptParamSetParams.setPadding, + decryptStruct->cipherParms->decryptParamSetParams.padding + }; + ret = TestConstructRsaCipherParamSet(&rsaDeCipherParamStructure); + } else { + struct AesCipherParamSetStructure deParamStruct = { + &decryptParamSet, + decryptStruct->cipherParms->decryptParamSetParams.paramSetExist, + decryptStruct->cipherParms->decryptParamSetParams.setAlg, + decryptStruct->cipherParms->decryptParamSetParams.alg, + decryptStruct->cipherParms->decryptParamSetParams.setPurpose, + decryptStruct->cipherParms->decryptParamSetParams.purpose, + decryptStruct->cipherParms->decryptParamSetParams.setPadding, + decryptStruct->cipherParms->decryptParamSetParams.padding, + decryptStruct->cipherParms->decryptParamSetParams.setBlockMode, + decryptStruct->cipherParms->decryptParamSetParams.mode, + decryptStruct->cipherParms->decryptParamSetParams.setIv, decryptStruct->ivData, + decryptStruct->cipherParms->decryptParamSetParams.setNonce, decryptStruct->nonceData, + decryptStruct->cipherParms->decryptParamSetParams.setAad, decryptStruct->aadData, + decryptStruct->cipherParms->decryptParamSetParams.setIsKeyAlias, + decryptStruct->cipherParms->decryptParamSetParams.isKeyAlias + }; + ret = TestConstructAesCipherParamSet(&deParamStruct); + HKS_TEST_ASSERT(ret == 0); + } + + ret = HksDecryptRun(decryptStruct->keyAlias, decryptParamSet, decryptStruct->cipherData, + *(decryptStruct->decryptedData), decryptStruct->performTimes); + HksFreeParamSet(&decryptParamSet); + return ret; +} + +static int32_t Decrypt(struct OnlyDecryptStructure *onlyDecryptStruct) +{ + struct HksParamSet *decryptParamSet = NULL; + int32_t ret; + if (onlyDecryptStruct->cipherParms->cipherType == HKS_TEST_CIPHER_TYPE_RSA) { + struct TestRsaCipherParamSet rsaCipherParamStructure = { + &decryptParamSet, + onlyDecryptStruct->cipherParms->paramSetExist, + onlyDecryptStruct->cipherParms->setAlg, onlyDecryptStruct->cipherParms->alg, + onlyDecryptStruct->cipherParms->setPurpose, onlyDecryptStruct->cipherParms->purpose, + onlyDecryptStruct->cipherParms->setDigest, onlyDecryptStruct->cipherParms->digest, + onlyDecryptStruct->cipherParms->setPadding, onlyDecryptStruct->cipherParms->padding + }; + ret = TestConstructRsaCipherParamSet(&rsaCipherParamStructure); + } else { + uint32_t ivSize = onlyDecryptStruct->cipherParms->ivSize; + uint32_t nonceSize = onlyDecryptStruct->cipherParms->nonceSize; + uint32_t aadSize = onlyDecryptStruct->cipherParms->aadSize; + if (ivSize != 0) { + ret = TestConstuctBlob(onlyDecryptStruct->ivData, true, ivSize, true, ivSize); + HKS_TEST_ASSERT(ret == 0); + } + if (nonceSize != 0) { + ret = TestConstuctBlob(onlyDecryptStruct->nonceData, true, nonceSize, true, nonceSize); + HKS_TEST_ASSERT(ret == 0); + } + if (aadSize != 0) { + ret = TestConstuctBlob(onlyDecryptStruct->aadData, true, aadSize, true, aadSize); + HKS_TEST_ASSERT(ret == 0); + } + struct AesCipherParamSetStructure onlyDeParamStruct = { + &decryptParamSet, + onlyDecryptStruct->cipherParms->paramSetExist, + onlyDecryptStruct->cipherParms->setAlg, onlyDecryptStruct->cipherParms->alg, + onlyDecryptStruct->cipherParms->setPurpose, onlyDecryptStruct->cipherParms->purpose, + onlyDecryptStruct->cipherParms->setPadding, onlyDecryptStruct->cipherParms->padding, + onlyDecryptStruct->cipherParms->setBlockMode, onlyDecryptStruct->cipherParms->mode, + onlyDecryptStruct->cipherParms->setIv, *(onlyDecryptStruct->ivData), + onlyDecryptStruct->cipherParms->setNonce, *(onlyDecryptStruct->nonceData), + onlyDecryptStruct->cipherParms->setAad, *(onlyDecryptStruct->aadData), + onlyDecryptStruct->cipherParms->setIsKeyAlias, onlyDecryptStruct->cipherParms->isKeyAlias + }; + ret = TestConstructAesCipherParamSet(&onlyDeParamStruct); + } + HKS_TEST_ASSERT(ret == 0); + + ret = HksDecryptRun(onlyDecryptStruct->keyAlias, decryptParamSet, onlyDecryptStruct->cipherData, + onlyDecryptStruct->decryptedData, onlyDecryptStruct->performTimes); + HksFreeParamSet(&decryptParamSet); + return ret; +} + + +int32_t BaseTestCipher(uint32_t times, uint32_t index, uint32_t performTimes) +{ + /* 1. generate key */ + struct HksBlob *keyAlias = NULL; + int32_t ret; + if ((g_testCipherParams[index].genKeyParamSetParams.setKeyStorageFlag) && + g_testCipherParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP) { + ret = GenerateLocalRandomKey(&keyAlias, &g_testCipherParams[index].localKeyParams); + } else { + ret = GenerateKey(&keyAlias, &g_testCipherParams[index].keyAliasParams, + &g_testCipherParams[index].genKeyParamSetParams, &g_testCipherParams[index].genKeyParamSetParamsOut); + } + HKS_TEST_ASSERT(ret == 0); + struct HksBlob *plainData = NULL; + struct HksBlob *cipherData = NULL; + ret = ConstructDataToBlob(&plainData, &cipherData, + &g_testCipherParams[index].plainTextParams, &g_testCipherParams[index].cipherTextParams); + HKS_TEST_ASSERT(ret == 0); + struct HksBlob *ivData = NULL; + struct HksBlob *nonceData = NULL; + struct HksBlob *aadData = NULL; + /* 2. encrypt */ + struct CipherEncryptStructure testEncryptStruct = { + keyAlias, &g_testCipherParams[index].encryptParamSetParams, + plainData, cipherData, &ivData, &nonceData, &aadData, performTimes + }; + ret = Encrypt(&testEncryptStruct); + HKS_TEST_ASSERT(ret == g_testCipherParams[index].expectResult); + + /* 3. decrypt */ + struct HksBlob *decryptedData = NULL; + struct CipherDecryptStructure testDecryptStruct = { + keyAlias, &g_testCipherParams[index], cipherData, + &decryptedData, ivData, nonceData, aadData, performTimes + }; + ret = DecryptCipher(&testDecryptStruct); + HKS_TEST_ASSERT(ret == g_testCipherParams[index].expectResult); + + HKS_TEST_ASSERT(plainData->size == decryptedData->size); + HKS_TEST_ASSERT(memcmp(plainData->data, decryptedData->data, plainData->size) == 0); + if (!((g_testCipherParams[index].genKeyParamSetParams.setKeyStorageFlag) && + g_testCipherParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) { + HKS_TEST_ASSERT(HksDeleteKey(keyAlias, NULL) == 0); + } + TestFreeBlob(&keyAlias); + TestFreeBlob(&plainData); + TestFreeBlob(&cipherData); + TestFreeBlob(&decryptedData); + TestFreeBlob(&ivData); + TestFreeBlob(&nonceData); + TestFreeBlob(&aadData); + return (ret != g_testCipherParams[index].expectResult); +} + +int32_t BaseTestEncrypt(uint32_t times, uint32_t index, uint32_t performTimes) +{ + /* 1. generate key */ + struct HksBlob *keyAlias = NULL; + int32_t ret; + if (g_testEncryptParams[index].encryptParamSetParams.setIsKeyAlias && + !g_testEncryptParams[index].encryptParamSetParams.isKeyAlias) { + ret = GenerateLocalRandomKey(&keyAlias, &g_testEncryptParams[index].localKeyParams); + } else { + if (g_testEncryptParams[index].keyAliasParams.blobExist) { + ret = GenerateKey(&keyAlias, &g_testEncryptParams[index].keyAliasParams, + &g_testEncryptParams[index].genKeyParamSetParams, NULL); + } else { + ret = TestConstuctBlob(&keyAlias, + g_testEncryptParams[index].encryptAliasParams.blobExist, + g_testEncryptParams[index].encryptAliasParams.blobSize, + g_testEncryptParams[index].encryptAliasParams.blobDataExist, + g_testEncryptParams[index].encryptAliasParams.blobDataSize); + } + } + HKS_TEST_ASSERT(ret == 0); + + struct HksBlob *plainData = NULL; + struct HksBlob *cipherData = NULL; + ret = ConstructDataToBlob(&plainData, &cipherData, + &g_testEncryptParams[index].inDataParams, &g_testEncryptParams[index].outDataParams); + HKS_TEST_ASSERT(ret == 0); + + struct HksBlob *ivData = NULL; + struct HksBlob *nonceData = NULL; + struct HksBlob *aadData = NULL; + /* 2. encrypt */ + struct CipherEncryptStructure encryptStruct = { + keyAlias, &g_testEncryptParams[index].encryptParamSetParams, plainData, cipherData, &ivData, + &nonceData, &aadData, performTimes + }; + ret = Encrypt(&encryptStruct); + HKS_TEST_ASSERT(ret == g_testEncryptParams[index].expectResult); + + /* 4. delete key */ + if (g_testEncryptParams[index].keyAliasParams.blobExist) { + HKS_TEST_ASSERT(HksDeleteKey(keyAlias, NULL) == 0); + } + TestFreeBlob(&keyAlias); + TestFreeBlob(&plainData); + TestFreeBlob(&cipherData); + TestFreeBlob(&ivData); + TestFreeBlob(&nonceData); + TestFreeBlob(&aadData); + return (ret != g_testEncryptParams[index].expectResult); +} + +int32_t BaseTestDecrypt(uint32_t times, uint32_t index, uint32_t performTimes) +{ + /* 1. generate key */ + struct HksBlob *keyAlias = NULL; + int32_t ret; + if (g_testDecryptParams[index].decryptParamSetParams.setIsKeyAlias && + !g_testDecryptParams[index].decryptParamSetParams.isKeyAlias) { + ret = GenerateLocalRandomKey(&keyAlias, &g_testDecryptParams[index].localKeyParams); + } else { + if (g_testDecryptParams[index].keyAliasParams.blobExist) { + ret = GenerateKey(&keyAlias, &g_testDecryptParams[index].keyAliasParams, + &g_testDecryptParams[index].genKeyParamSetParams, NULL); + } else { + ret = TestConstuctBlob(&keyAlias, + g_testDecryptParams[index].decryptAliasParams.blobExist, + g_testDecryptParams[index].decryptAliasParams.blobSize, + g_testDecryptParams[index].decryptAliasParams.blobDataExist, + g_testDecryptParams[index].decryptAliasParams.blobDataSize); + } + } + HKS_TEST_ASSERT(ret == 0); + + struct HksBlob *cipherData = NULL; + struct HksBlob *decryptedData = NULL; + ret = ConstructDataToBlob(&cipherData, &decryptedData, + &g_testDecryptParams[index].inDataParams, &g_testDecryptParams[index].outDataParams); + HKS_TEST_ASSERT(ret == 0); + + struct HksBlob *ivData = NULL; + struct HksBlob *nonceData = NULL; + struct HksBlob *aadData = NULL; + /* 3. encrypt */ + struct OnlyDecryptStructure onlyDecryptStruct = { + keyAlias, &g_testDecryptParams[index].decryptParamSetParams, cipherData, decryptedData, &ivData, + &nonceData, &aadData, performTimes + }; + ret = Decrypt(&onlyDecryptStruct); + HKS_TEST_ASSERT(ret == g_testDecryptParams[index].expectResult); + + /* 4. delete key */ + if (g_testDecryptParams[index].keyAliasParams.blobExist) { + HKS_TEST_ASSERT(HksDeleteKey(keyAlias, NULL) == 0); + } + TestFreeBlob(&keyAlias); + TestFreeBlob(&decryptedData); + TestFreeBlob(&cipherData); + TestFreeBlob(&ivData); + TestFreeBlob(&nonceData); + TestFreeBlob(&aadData); + return (ret != g_testDecryptParams[index].expectResult); +} \ No newline at end of file diff --git a/security_lite/huks/common/hks_test_common.c b/security_lite/huks/common/hks_test_common.c new file mode 100644 index 0000000000000000000000000000000000000000..208c962254e22fc3ceb0242e9863251ae12edc77 --- /dev/null +++ b/security_lite/huks/common/hks_test_common.c @@ -0,0 +1,736 @@ +/* + * Copyright (c) 2021 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 "hks_test_common.h" + +#include +#include + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_log.h" +#include "hks_test_mem.h" + +#include "securec.h" + +#define HKS_TEST_1024 1024 +#define HKS_TEST_COMMON_8 8 +#define HKS_TEST_COMMON_128 128 + +void TestFreeBlob(struct HksBlob **key) +{ + if (key == NULL || *key == NULL) { + return; + } + HksTestFree((*key)->data); + HksTestFree(*key); +} + +int32_t TestConstuctBlob(struct HksBlob **blob, bool blobExist, uint32_t blobSize, bool blobDataExist, + uint32_t realBlobDataSize) +{ + if (!blobExist) { // blob not exist + return 0; // do nothing, return null + } + + *blob = (struct HksBlob *)HksTestMalloc(sizeof(struct HksBlob)); + if (*blob == NULL) { + return HKS_ERROR_MALLOC_FAIL; + } + (void)memset_s(*blob, sizeof(struct HksBlob), 0, sizeof(struct HksBlob)); + + if (blobDataExist) { + (*blob)->size = realBlobDataSize; + (*blob)->data = (uint8_t *)HksTestMalloc(realBlobDataSize); + if ((*blob)->data == NULL) { + return HKS_ERROR_MALLOC_FAIL; + } + (void)memset_s((*blob)->data, realBlobDataSize, 0, realBlobDataSize); + uint32_t offset = 0; + for (uint32_t i = 0; i < (realBlobDataSize - 1) / HKS_TEST_1024; i++) { + struct HksBlob tmp; + tmp.size = HKS_TEST_1024; + tmp.data = (uint8_t *)HksTestMalloc(tmp.size); + HKS_TEST_ASSERT(tmp.data != NULL); + (void)memset_s(tmp.data, tmp.size, 0, tmp.size); + + HKS_TEST_ASSERT(HksGenerateRandom(NULL, &tmp) == 0); + + HKS_TEST_ASSERT(memcpy_s((*blob)->data + offset, realBlobDataSize - offset, tmp.data, tmp.size) == EOK); + offset += tmp.size; + HksTestFree(tmp.data); + } + + uint32_t remainSize = realBlobDataSize - offset; + struct HksBlob tmp; + tmp.size = remainSize; + tmp.data = (uint8_t *)HksTestMalloc(tmp.size); + HKS_TEST_ASSERT(tmp.data != NULL); + (void)memset_s(tmp.data, tmp.size, 0, tmp.size); + + HKS_TEST_ASSERT(HksGenerateRandom(NULL, &tmp) == 0); + HKS_TEST_ASSERT(memcpy_s((*blob)->data + offset, remainSize, tmp.data, tmp.size) == EOK); + offset += tmp.size; + HksTestFree(tmp.data); + } else { + (*blob)->data = NULL; + } + + (*blob)->size = blobSize; + return 0; +} + +static int32_t TestGenerateKeyParamSetPre(struct GenerateKeyParamSetStructure *paramStruct, + struct HksParamSet *paramSet) +{ + if (!paramStruct->paramSetExist) { + return 0; // do nothing, return null + } + int32_t ret = 1; + + if (paramStruct->setAlg) { + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = paramStruct->alg; + ret = HksAddParams(paramSet, (const struct HksParam *)&algParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setKeySize) { + struct HksParam keySizeParam = {0}; + keySizeParam.tag = HKS_TAG_KEY_SIZE; + keySizeParam.uint32Param = paramStruct->keySize; + ret = HksAddParams(paramSet, (const struct HksParam *)&keySizeParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setPurpose) { + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + purposeParam.uint32Param = paramStruct->purpose; + ret = HksAddParams(paramSet, (const struct HksParam *)&purposeParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setDigest) { + struct HksParam digestParam = {0}; + digestParam.tag = HKS_TAG_DIGEST; + digestParam.uint32Param = paramStruct->digest; + ret = HksAddParams(paramSet, (const struct HksParam *)&digestParam, 1); + HKS_TEST_LOG_I("HksAddParams ret = 0x%X", ret); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setPadding) { + struct HksParam paddingParam = {0}; + paddingParam.tag = HKS_TAG_PADDING; + paddingParam.uint32Param = paramStruct->padding; + ret = HksAddParams(paramSet, (const struct HksParam *)&paddingParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + return ret; +} + +static int32_t TestGenerateKeyParamSetPost(struct GenerateKeyParamSetStructure *paramStruct, + struct HksParamSet *paramSet) +{ + if (!paramStruct->paramSetExist) { + return 0; // do nothing, return null + } + int32_t ret; + if (paramStruct->setBlockMode) { + struct HksParam keyMode = {0}; + keyMode.tag = HKS_TAG_BLOCK_MODE; + keyMode.uint32Param = paramStruct->mode; + ret = HksAddParams(paramSet, (const struct HksParam *)&keyMode, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setKeyStorageFlag) { + struct HksParam keyStorageFlagParam = {0}; + keyStorageFlagParam.tag = HKS_TAG_KEY_STORAGE_FLAG; + keyStorageFlagParam.uint32Param = paramStruct->keyStorageFlag; + ret = HksAddParams(paramSet, (const struct HksParam *)&keyStorageFlagParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + struct HksParam tmpParam = {0}; + tmpParam.tag = HKS_TAG_KEY_ROLE; + tmpParam.uint32Param = 0xFFFFFFFF; + ret = HksAddParams(paramSet, (const struct HksParam *)&tmpParam, 1); + HKS_TEST_ASSERT(ret == 0); + + ret = HksBuildParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + *(paramStruct->outParamSet) = paramSet; + return ret; +} + +int32_t TestConstructGenerateKeyParamSet(struct GenerateKeyParamSetStructure *paramStruct) +{ + struct HksParamSet *paramSet = NULL; + int32_t ret = HksInitParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + ret = TestGenerateKeyParamSetPre(paramStruct, paramSet); + HKS_TEST_ASSERT(ret == 0); + ret = TestGenerateKeyParamSetPost(paramStruct, paramSet); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +int32_t TestConstructGenerateKeyParamSetOut( + struct HksParamSet **outParamSet, + bool paramSetExist, uint32_t paramSetSize) +{ + if (!paramSetExist) { + return 0; // do nothing, return null + } + struct HksParamSet *tempParamSet = (struct HksParamSet *)HksTestMalloc(paramSetSize); + if (tempParamSet == NULL) { + return -1; // -1 is error code + } + (void)memset_s(tempParamSet, paramSetSize, 0, paramSetSize); + tempParamSet->paramSetSize = paramSetSize; + + *outParamSet = tempParamSet; + return 0; +} + +int32_t TestConstructRsaCipherParamSet(struct TestRsaCipherParamSet *paramStruct) +{ + struct GenerateKeyParamSetStructure genParamStruct = { + paramStruct->outParamSet, paramStruct->paramSetExist, + paramStruct->setAlg, paramStruct->alg, + false, 0, + paramStruct->setPurpose, paramStruct->purpose, + paramStruct->setDigest, paramStruct->digest, + paramStruct->setPadding, paramStruct->padding, + false, 0, + false, 0 + }; + return TestConstructGenerateKeyParamSet(&genParamStruct); +} + +int32_t TestAesCipherParamSetPre(struct AesCipherParamSetStructure *paramStruct, struct HksParamSet *paramSet) +{ + if (!paramStruct->paramSetExist) { + return 0; // do nothing, return null + } + int32_t ret = 1; + if (paramStruct->setAlg) { + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = paramStruct->alg; + ret = HksAddParams(paramSet, (const struct HksParam *)&algParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setPurpose) { + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + purposeParam.uint32Param = paramStruct->purpose; + ret = HksAddParams(paramSet, (const struct HksParam *)&purposeParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setPadding) { + struct HksParam paddingParam = {0}; + paddingParam.tag = HKS_TAG_PADDING; + paddingParam.uint32Param = paramStruct->padding; + ret = HksAddParams(paramSet, (const struct HksParam *)&paddingParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setBlockMode) { + struct HksParam keyMode = {0}; + keyMode.tag = HKS_TAG_BLOCK_MODE; + keyMode.uint32Param = paramStruct->mode; + ret = HksAddParams(paramSet, (const struct HksParam *)&keyMode, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setIv) { + struct HksParam ivParam = {0}; + ivParam.tag = HKS_TAG_IV; + ivParam.blob = *(paramStruct->ivBlob); + ret = HksAddParams(paramSet, (const struct HksParam *)&ivParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + return ret; +} + +int32_t TestAesCipherParamSetPost(struct AesCipherParamSetStructure *paramStruct, struct HksParamSet *paramSet) +{ + if (!paramStruct->paramSetExist) { + return 0; // do nothing, return null + } + int32_t ret; + if (paramStruct->setNonce) { + struct HksParam nonceParam = {0}; + nonceParam.tag = HKS_TAG_NONCE; + nonceParam.blob = *(paramStruct->nonceBlob); + ret = HksAddParams(paramSet, (const struct HksParam *)&nonceParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setAad) { + struct HksParam aadParam = {0}; + aadParam.tag = HKS_TAG_ASSOCIATED_DATA; + aadParam.blob = *(paramStruct->aadBlob); + ret = HksAddParams(paramSet, (const struct HksParam *)&aadParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setIsKeyAlias) { + struct HksParam isKeyAilasParam = {0}; + isKeyAilasParam.tag = HKS_TAG_IS_KEY_ALIAS; + isKeyAilasParam.boolParam = paramStruct->isKeyAlias; + ret = HksAddParams(paramSet, (const struct HksParam *)&isKeyAilasParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + struct HksParam tmpParam = {0}; + tmpParam.tag = HKS_TAG_KEY_ROLE; + tmpParam.uint32Param = 0xFFFFFFFF; + ret = HksAddParams(paramSet, (const struct HksParam *)&tmpParam, 1); + HKS_TEST_ASSERT(ret == 0); + + ret = HksBuildParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + *(paramStruct->outParamSet) = paramSet; + return ret; +} + +int32_t TestConstructAesCipherParamSet(struct AesCipherParamSetStructure *paramStruct) +{ + struct HksParamSet *paramSet = NULL; + int32_t ret = HksInitParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + ret = TestAesCipherParamSetPre(paramStruct, paramSet); + HKS_TEST_ASSERT(ret == 0); + ret = TestAesCipherParamSetPost(paramStruct, paramSet); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +int32_t TestConstructMacParamSet(struct TestMacParamSetStructure *paramStruct) +{ + if (!paramStruct->paramSetExist) { + return 0; // do nothing, return null + } + struct HksParamSet *paramSet = NULL; + int32_t ret = HksInitParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + if (paramStruct->setPurpose) { + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + purposeParam.uint32Param = paramStruct->purpose; + ret = HksAddParams(paramSet, (const struct HksParam *)&purposeParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setDigest) { + struct HksParam digestParam = {0}; + digestParam.tag = HKS_TAG_DIGEST; + digestParam.uint32Param = paramStruct->digest; + ret = HksAddParams(paramSet, (const struct HksParam *)&digestParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setKeyAlias) { + struct HksParam keyIsKeyAlias = {0}; + keyIsKeyAlias.tag = HKS_TAG_IS_KEY_ALIAS; + keyIsKeyAlias.boolParam = paramStruct->isKeyAlias; + ret = HksAddParams(paramSet, (const struct HksParam *)&keyIsKeyAlias, 1); + HKS_TEST_ASSERT(ret == 0); + } + + struct HksParam tmpParam = {0}; + tmpParam.tag = HKS_TAG_KEY_ROLE; + tmpParam.uint32Param = 0xFFFFFFFF; + ret = HksAddParams(paramSet, (const struct HksParam *)&tmpParam, 1); + HKS_TEST_ASSERT(ret == 0); + + ret = HksBuildParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + *(paramStruct->outParamSet) = paramSet; + return ret; +} + +int32_t TestConstructAgreeParamSet(struct TestAgreeParamSetStructure *paramStruct) +{ + if (!paramStruct->paramSetExist) { + return 0; // do nothing, return null + } + struct HksParamSet *paramSet = NULL; + int32_t ret = HksInitParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + if (paramStruct->setAlg) { + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = paramStruct->alg; + ret = HksAddParams(paramSet, (const struct HksParam *)&algParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setKeySize) { + struct HksParam keySizeParam = {0}; + keySizeParam.tag = HKS_TAG_KEY_SIZE; + keySizeParam.uint32Param = paramStruct->keySize; + ret = HksAddParams(paramSet, (const struct HksParam *)&keySizeParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setIsKeyAlias) { + struct HksParam isKeyAliasParam = {0}; + isKeyAliasParam.tag = HKS_TAG_IS_KEY_ALIAS; + isKeyAliasParam.uint32Param = paramStruct->isKeyAlias; + ret = HksAddParams(paramSet, (const struct HksParam *)&isKeyAliasParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + struct HksParam tmpParam = {0}; + tmpParam.tag = HKS_TAG_KEY_ROLE; + tmpParam.uint32Param = 0xFFFFFFFF; + ret = HksAddParams(paramSet, (const struct HksParam *)&tmpParam, 1); + HKS_TEST_ASSERT(ret == 0); + + ret = HksBuildParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + *(paramStruct->outParamSet) = paramSet; + return ret; +} + + +int32_t TestDeriveParamSetPre(struct TestDeriveParamSetStructure *paramStruct, struct HksParamSet *paramSet) +{ + if (!paramStruct->paramSetExist) { + return 0; // do nothing, return null + } + int32_t ret = 1; + if (paramStruct->setAlg) { + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = paramStruct->alg; + ret = HksAddParams(paramSet, (const struct HksParam *)&algParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setPurpose) { + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + purposeParam.uint32Param = paramStruct->purpose; + ret = HksAddParams(paramSet, (const struct HksParam *)&purposeParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setDigest) { + struct HksParam digestParam = {0}; + digestParam.tag = HKS_TAG_DIGEST; + digestParam.uint32Param = paramStruct->digest; + ret = HksAddParams(paramSet, (const struct HksParam *)&digestParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setIteration) { + struct HksParam iterationParam = {0}; + iterationParam.tag = HKS_TAG_ITERATION; + iterationParam.uint32Param = paramStruct->iteration; + ret = HksAddParams(paramSet, (const struct HksParam *)&iterationParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + return ret; +} + +int32_t TestDeriveParamSetPost(struct TestDeriveParamSetStructure *paramStruct, struct HksParamSet *paramSet) +{ + if (!paramStruct->paramSetExist) { + return 0; // do nothing, return null + } + int32_t ret; + if (paramStruct->setSalt) { + struct HksParam saltParam = {0}; + saltParam.tag = HKS_TAG_SALT; + saltParam.blob = *(paramStruct->saltBlob); + ret = HksAddParams(paramSet, (const struct HksParam *)&saltParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setInfo) { + struct HksParam infoParam = {0}; + infoParam.tag = HKS_TAG_INFO; + infoParam.blob = *(paramStruct->infoBlob); + ret = HksAddParams(paramSet, (const struct HksParam *)&infoParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + if (paramStruct->setIsKeyAlias) { + struct HksParam isKeyAliasParam = {0}; + isKeyAliasParam.tag = HKS_TAG_IS_KEY_ALIAS; + isKeyAliasParam.boolParam = paramStruct->isKeyAlias; + ret = HksAddParams(paramSet, (const struct HksParam *)&isKeyAliasParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + struct HksParam tmpParam = {0}; + tmpParam.tag = HKS_TAG_KEY_ROLE; + tmpParam.uint32Param = 0xFFFFFFFF; + ret = HksAddParams(paramSet, (const struct HksParam *)&tmpParam, 1); + HKS_TEST_ASSERT(ret == 0); + + ret = HksBuildParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + *(paramStruct->outParamSet) = paramSet; + return ret; +} + +int32_t TestConstructDeriveParamSet(struct TestDeriveParamSetStructure *paramStruct) +{ + struct HksParamSet *paramSet = NULL; + int32_t ret = HksInitParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + ret = TestDeriveParamSetPre(paramStruct, paramSet); + HKS_TEST_ASSERT(ret == 0); + ret = TestDeriveParamSetPost(paramStruct, paramSet); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +int32_t TestConstructHashParamSet( + struct HksParamSet **outParamSet, + bool paramSetExist, + bool setDigest, uint32_t digest) +{ + if (!paramSetExist) { + return 0; // do nothing, return null + } + struct HksParamSet *paramSet = NULL; + int32_t ret = HksInitParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + if (setDigest) { + struct HksParam digestParam = {0}; + digestParam.tag = HKS_TAG_DIGEST; + digestParam.uint32Param = digest; + ret = HksAddParams(paramSet, (const struct HksParam *)&digestParam, 1); + HKS_TEST_ASSERT(ret == 0); + } + + struct HksParam tmpParam = {0}; + tmpParam.tag = HKS_TAG_KEY_ROLE; + tmpParam.uint32Param = 0xFFFFFFFF; + ret = HksAddParams(paramSet, (const struct HksParam *)&tmpParam, 1); + HKS_TEST_ASSERT(ret == 0); + + ret = HksBuildParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + *outParamSet = paramSet; + return ret; +} + +int32_t GenerateKey(struct HksBlob **keyAlias, const struct HksTestBlobParams *keyAliasParams, + const struct HksTestGenKeyParamsParamSet *genKeyParamSetParams, + const struct HksTestGenKeyParamsParamSetOut *genKeyParamSetParamsOut) +{ + int32_t ret = TestConstuctBlob(keyAlias, + keyAliasParams->blobExist, + keyAliasParams->blobSize, + keyAliasParams->blobDataExist, + keyAliasParams->blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *paramSet = NULL; + struct GenerateKeyParamSetStructure paramStruct = { + ¶mSet, + genKeyParamSetParams->paramSetExist, + genKeyParamSetParams->setAlg, genKeyParamSetParams->alg, + genKeyParamSetParams->setKeySize, genKeyParamSetParams->keySize, + genKeyParamSetParams->setPurpose, genKeyParamSetParams->purpose, + genKeyParamSetParams->setDigest, genKeyParamSetParams->digest, + genKeyParamSetParams->setPadding, genKeyParamSetParams->padding, + genKeyParamSetParams->setBlockMode, genKeyParamSetParams->mode, + genKeyParamSetParams->setKeyStorageFlag, genKeyParamSetParams->keyStorageFlag + }; + ret = TestConstructGenerateKeyParamSet(¶mStruct); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *paramSetOut = NULL; + if (genKeyParamSetParamsOut != NULL) { + ret = TestConstructGenerateKeyParamSetOut(¶mSet, + genKeyParamSetParamsOut->paramSetExist, genKeyParamSetParamsOut->paramSetSize); + HKS_TEST_ASSERT(ret == 0); + } + + ret = HksGenerateKey(*keyAlias, paramSet, paramSetOut); + HKS_TEST_ASSERT(ret == 0); + + HksFreeParamSet(¶mSet); + return ret; +} + +int32_t GenerateLocalRandomKey(struct HksBlob **keyAlias, const struct HksTestBlobParams *localKeyParams) +{ + int32_t ret = TestConstuctBlob(keyAlias, + localKeyParams->blobExist, + localKeyParams->blobSize, + localKeyParams->blobDataExist, + localKeyParams->blobDataSize); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +int32_t TestConstructBlobOut(struct HksBlob **blob, + bool blobExist, + uint32_t blobSize, + bool blobDataExist, + uint32_t realBlobDataSize) +{ + if (!blobExist) { // blob not exist + return 0; // do nothing, return null + } + + *blob = (struct HksBlob *)HksTestMalloc(sizeof(struct HksBlob)); + if (*blob == NULL) { + HKS_TEST_LOG_E("malloc fail"); + return HKS_ERROR_MALLOC_FAIL; + } + (void)memset_s(*blob, sizeof(struct HksBlob), 0, sizeof(struct HksBlob)); + + if (blobDataExist) { + (*blob)->size = realBlobDataSize; + (*blob)->data = (uint8_t *)HksTestMalloc(realBlobDataSize); + if ((*blob)->data == NULL) { + HKS_TEST_LOG_E("malloc fail"); + return HKS_ERROR_MALLOC_FAIL; + } + (void)memset_s((*blob)->data, realBlobDataSize, 0, realBlobDataSize); + } else { + (*blob)->data = NULL; + } + + (*blob)->size = blobSize; + return 0; +} + +int32_t GenerateLocalX25519Key(struct HksBlob **privateKey, struct HksBlob **publicKey, + const struct HksTestBlobParams *localPrivateKeyParams, const struct HksTestBlobParams *localPublicKeyParams) +{ + int32_t ret; + if ((privateKey != NULL) && (localPrivateKeyParams != NULL)) { + ret = TestConstuctBlob(privateKey, + localPrivateKeyParams->blobExist, + localPrivateKeyParams->blobSize, + localPrivateKeyParams->blobDataExist, + localPrivateKeyParams->blobDataSize); + HKS_TEST_ASSERT(ret == 0); + } + + if ((publicKey != NULL) && (localPublicKeyParams != NULL)) { + ret = TestConstuctBlob(publicKey, + localPublicKeyParams->blobExist, + localPublicKeyParams->blobSize, + localPublicKeyParams->blobDataExist, + localPublicKeyParams->blobDataSize); + HKS_TEST_ASSERT(ret == 0); + } + + struct HksParamSet *paramSet = NULL; + struct GenerateKeyParamSetStructure paramStruct = { + ¶mSet, true, true, HKS_ALG_X25519, + true, HKS_CURVE25519_KEY_SIZE_256, true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY, false, 0, + false, 0, false, 0, true, HKS_STORAGE_TEMP + }; + ret = TestConstructGenerateKeyParamSet(¶mStruct); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *paramSetOut = NULL; + ret = TestConstructGenerateKeyParamSetOut(¶mSetOut, true, HKS_TEST_COMMON_128); + HKS_TEST_ASSERT(ret == 0); + + ret = HksGenerateKey(NULL, paramSet, paramSetOut); + HKS_TEST_ASSERT(ret == 0); + + if ((publicKey != NULL) && ((*publicKey) != NULL) && + (localPublicKeyParams->blobDataExist) && + (localPublicKeyParams->blobDataSize == (HKS_CURVE25519_KEY_SIZE_256 / HKS_TEST_COMMON_8))) { + (void)memcpy_s((*publicKey)->data, (*publicKey)->size, + paramSetOut->params[0].blob.data, paramSetOut->params[0].blob.size); + } + + if ((privateKey != NULL) && ((*privateKey) != NULL) && + (localPrivateKeyParams->blobDataExist) && + (localPrivateKeyParams->blobDataSize == (HKS_CURVE25519_KEY_SIZE_256 / HKS_TEST_COMMON_8))) { + (void)memcpy_s((*privateKey)->data, (*privateKey)->size, + paramSetOut->params[1].blob.data, paramSetOut->params[1].blob.size); + } + + HksFreeParamSet(¶mSet); + HksFreeParamSet(¶mSetOut); + return ret; +} + +int32_t TestGenDefaultKeyAndGetAlias(struct HksBlob **keyAlias) +{ + struct HksTestGenKeyParams genKeyParam = { + 0xffffffff, HKS_SUCCESS, + { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, { + true, + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_128, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + true, HKS_DIGEST_SHA256, + true, HKS_PADDING_NONE, + true, HKS_MODE_CBC }, + { false, 0 } + }; + + int32_t ret = TestConstuctBlob(keyAlias, + genKeyParam.keyAliasParams.blobExist, + genKeyParam.keyAliasParams.blobSize, + genKeyParam.keyAliasParams.blobDataExist, + genKeyParam.keyAliasParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *paramSet = NULL; + struct GenerateKeyParamSetStructure paramStruct = { + ¶mSet, genKeyParam.paramSetParams.paramSetExist, + genKeyParam.paramSetParams.setAlg, genKeyParam.paramSetParams.alg, + genKeyParam.paramSetParams.setKeySize, genKeyParam.paramSetParams.keySize, + genKeyParam.paramSetParams.setPurpose, genKeyParam.paramSetParams.purpose, + genKeyParam.paramSetParams.setDigest, genKeyParam.paramSetParams.digest, + genKeyParam.paramSetParams.setPadding, genKeyParam.paramSetParams.padding, + genKeyParam.paramSetParams.setBlockMode, genKeyParam.paramSetParams.mode, + false, 0 + }; + ret = TestConstructGenerateKeyParamSet(¶mStruct); + HKS_TEST_ASSERT(ret == 0); + + ret = HksGenerateKey(*keyAlias, paramSet, NULL); + HKS_TEST_ASSERT(ret == 0); + + HksFreeParamSet(¶mSet); + return ret; +} diff --git a/security_lite/huks/common/hks_test_curve25519.c b/security_lite/huks/common/hks_test_curve25519.c new file mode 100644 index 0000000000000000000000000000000000000000..605a5156beb3849af1f8b4421bf273f824cc9905 --- /dev/null +++ b/security_lite/huks/common/hks_test_curve25519.c @@ -0,0 +1,467 @@ +/* + * Copyright (c) 2021 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 "hks_test_curve25519.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_log.h" +#include "hks_type.h" + +#define TEST_ALIAS_ED25519 "test_ed25519" +#define TEST_PLAIN_TEST_ED25519 "This is a plain text! Hello world and thanks for watching ED25519~" +#define TEST_CURVE_256 256 +#define TEST_CURVE_512 512 + +static uint8_t g_buffer[TEST_CURVE_256]; +static uint32_t g_bufferSize = TEST_CURVE_256; +static uint8_t g_pubKey[TEST_CURVE_512] = {0}; +static uint32_t g_pubKeyLen = TEST_CURVE_512; + +int32_t TestGenerateEd25519Key(struct HksBlob alias) +{ + HKS_TEST_LOG_I("Test_GenerateEd25519!\n"); + struct HksParamSet *paramSet = NULL; + int32_t ret = HksInitParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = HKS_ALG_ED25519; + ret = HksAddParams(paramSet, &algParam, 1); + HKS_TEST_ASSERT(ret == 0); + struct HksParam keySizeParam = {0}; + keySizeParam.tag = HKS_TAG_KEY_SIZE; + keySizeParam.uint32Param = TEST_CURVE_256; + ret = HksAddParams(paramSet, &keySizeParam, 1); + HKS_TEST_ASSERT(ret == 0); + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + purposeParam.uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY; + ret = HksAddParams(paramSet, &purposeParam, 1); + HKS_TEST_ASSERT(ret == 0); + struct HksParam digestParam = {0}; + digestParam.tag = HKS_TAG_DIGEST; + digestParam.uint32Param = HKS_DIGEST_SHA512; + ret = HksAddParams(paramSet, &digestParam, 1); + HKS_TEST_ASSERT(ret == 0); + struct HksParam paddingParam = {0}; + paddingParam.tag = HKS_TAG_PADDING; + paddingParam.uint32Param = HKS_PADDING_NONE; + ret = HksAddParams(paramSet, &paddingParam, 1); + HKS_TEST_ASSERT(ret == 0); + ret = HksBuildParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + ret = HksGenerateKey(&alias, paramSet, NULL); + HKS_TEST_ASSERT(ret == 0); + HksFreeParamSet(¶mSet); + return ret; +} + +static int32_t TestSignEd25519(struct HksBlob alias) +{ + struct HksBlob msg = {strlen(TEST_PLAIN_TEST_ED25519), (uint8_t *)TEST_PLAIN_TEST_ED25519}; + + struct HksParamSet *paramSet = NULL; + int32_t ret = HksInitParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = HKS_ALG_ED25519; + ret = HksAddParams(paramSet, &algParam, 1); + HKS_TEST_ASSERT(ret == 0); + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + purposeParam.uint32Param = HKS_KEY_PURPOSE_SIGN; + ret = HksAddParams(paramSet, &purposeParam, 1); + HKS_TEST_ASSERT(ret == 0); + struct HksParam digestParam = {0}; + digestParam.tag = HKS_TAG_DIGEST; + digestParam.uint32Param = HKS_DIGEST_SHA512; + ret = HksAddParams(paramSet, &digestParam, 1); + HKS_TEST_ASSERT(ret == 0); + struct HksParam paddingParam = {0}; + paddingParam.tag = HKS_TAG_PADDING; + paddingParam.uint32Param = HKS_PADDING_NONE; + ret = HksAddParams(paramSet, &paddingParam, 1); + HKS_TEST_ASSERT(ret == 0); + ret = HksBuildParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + struct HksBlob signature = { TEST_CURVE_256, g_buffer }; + ret = HksSign(&alias, paramSet, &msg, &signature); + HKS_TEST_ASSERT(ret == 0); + g_bufferSize = signature.size; + HKS_TEST_LOG_I("TestSignEd25519 signature size is %u", signature.size); + HksFreeParamSet(¶mSet); + return ret; +} + +static int32_t TestVerifyEd25519(struct HksBlob alias) +{ + struct HksBlob msg = {strlen(TEST_PLAIN_TEST_ED25519), (uint8_t *)TEST_PLAIN_TEST_ED25519}; + + HKS_TEST_LOG_I("TestVerifyEd25519!\n"); + struct HksParamSet *paramSet = NULL; + int32_t ret = HksInitParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = HKS_ALG_ED25519; + ret = HksAddParams(paramSet, &algParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + purposeParam.uint32Param = HKS_KEY_PURPOSE_VERIFY; + ret = HksAddParams(paramSet, &purposeParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam digestParam = {0}; + digestParam.tag = HKS_TAG_DIGEST; + digestParam.uint32Param = HKS_DIGEST_SHA512; + ret = HksAddParams(paramSet, &digestParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam paddingParam = {0}; + paddingParam.tag = HKS_TAG_PADDING; + paddingParam.uint32Param = HKS_PADDING_NONE; + ret = HksAddParams(paramSet, &paddingParam, 1); + HKS_TEST_ASSERT(ret == 0); + + ret = HksBuildParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + struct HksBlob signature = { g_bufferSize, g_buffer }; + ret = HksVerify(&alias, paramSet, &msg, &signature); + HKS_TEST_ASSERT(ret == 0); + HksFreeParamSet(¶mSet); + return ret; +} + +int32_t TestImportEd25519(struct HksBlob alias, struct HksBlob *pubKeyInfo) +{ + HKS_TEST_LOG_I("TestImportEd25519!\n"); + struct HksParamSet *paramSet = NULL; + int32_t ret = HksInitParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = HKS_ALG_ED25519; + ret = HksAddParams(paramSet, &algParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam keySizeParam = {0}; + keySizeParam.tag = HKS_TAG_KEY_SIZE; + keySizeParam.uint32Param = TEST_CURVE_256; + ret = HksAddParams(paramSet, &keySizeParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + purposeParam.uint32Param = HKS_KEY_PURPOSE_VERIFY; + ret = HksAddParams(paramSet, &purposeParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam digestParam = {0}; + digestParam.tag = HKS_TAG_DIGEST; + digestParam.uint32Param = HKS_DIGEST_SHA512; + ret = HksAddParams(paramSet, &digestParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam paddingParam = {0}; + paddingParam.tag = HKS_TAG_PADDING; + paddingParam.uint32Param = HKS_PADDING_NONE; + ret = HksAddParams(paramSet, &paddingParam, 1); + HKS_TEST_ASSERT(ret == 0); + + ret = HksBuildParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + ret = HksImportKey(&alias, paramSet, pubKeyInfo); + HKS_TEST_ASSERT(ret == 0); + HksFreeParamSet(¶mSet); + return ret; +} + +static int32_t TestExportImportEd25519SignVerify(struct HksBlob alias) +{ + uint8_t pubKey[32] = {0}; + uint32_t pubKeyLen = 32; + struct HksBlob pubKeyInfo = { pubKeyLen, pubKey }; + int32_t ret = TestGenerateEd25519Key(alias); + HKS_TEST_ASSERT(ret == 0); + + ret = HksExportPublicKey(&alias, NULL, &pubKeyInfo); + HKS_TEST_ASSERT(ret == 0); + + ret = TestSignEd25519(alias); + HKS_TEST_ASSERT(ret == 0); + + ret = HksDeleteKey(&alias, NULL); + HKS_TEST_ASSERT(ret == 0); + + struct HksBlob newAlias = { strlen("test_ed25519_2"), (uint8_t *)"test_ed25519_2" }; + ret = TestImportEd25519(newAlias, &pubKeyInfo); + HKS_TEST_ASSERT(ret == 0); + ret = TestVerifyEd25519(newAlias); + HKS_TEST_ASSERT(ret == 0); + + ret = HksDeleteKey(&newAlias, NULL); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +int32_t TestCurve25519All() +{ + struct HksBlob ed25519Alias = { strlen(TEST_ALIAS_ED25519), (uint8_t *)TEST_ALIAS_ED25519 }; + int32_t ret = TestGenerateEd25519Key(ed25519Alias); + HKS_TEST_ASSERT(ret == 0); + ret = TestSignEd25519(ed25519Alias); + HKS_TEST_ASSERT(ret == 0); + ret = TestVerifyEd25519(ed25519Alias); + HKS_TEST_ASSERT(ret == 0); + ret = HksDeleteKey(&ed25519Alias, NULL); + HKS_TEST_ASSERT(ret == 0); + + ret = TestExportImportEd25519SignVerify(ed25519Alias); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +static int32_t BuildTeeSignParamSet(struct HksParamSet **paramSet) +{ + int32_t ret = HksInitParamSet(paramSet); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = HKS_ALG_ED25519; + ret = HksAddParams(*paramSet, &algParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + purposeParam.uint32Param = HKS_KEY_PURPOSE_SIGN; + ret = HksAddParams(*paramSet, &purposeParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam digestParam = {0}; + digestParam.tag = HKS_TAG_DIGEST; + digestParam.uint32Param = HKS_DIGEST_SHA512; + ret = HksAddParams(*paramSet, &digestParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam paddingParam = {0}; + paddingParam.tag = HKS_TAG_PADDING; + paddingParam.uint32Param = HKS_PADDING_NONE; + ret = HksAddParams(*paramSet, &paddingParam, 1); + HKS_TEST_ASSERT(ret == 0); + + ret = HksBuildParamSet(paramSet); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +static int32_t BuildLocalVerifyParamSet(struct HksParamSet **paramSet) +{ + int32_t ret = HksInitParamSet(paramSet); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = HKS_ALG_ED25519; + ret = HksAddParams(*paramSet, &algParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam isKeyAlias = {0}; + isKeyAlias.tag = HKS_TAG_IS_KEY_ALIAS; + isKeyAlias.boolParam = false; + ret = HksAddParams(*paramSet, &isKeyAlias, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam keySizeParam = {0}; + keySizeParam.tag = HKS_TAG_KEY_SIZE; + keySizeParam.uint32Param = TEST_CURVE_256; + ret = HksAddParams(*paramSet, &keySizeParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + purposeParam.uint32Param = HKS_KEY_PURPOSE_VERIFY; + ret = HksAddParams(*paramSet, &purposeParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam digestParam = {0}; + digestParam.tag = HKS_TAG_DIGEST; + digestParam.uint32Param = HKS_DIGEST_SHA512; + ret = HksAddParams(*paramSet, &digestParam, 1); + HKS_TEST_ASSERT(ret == 0); + + ret = HksBuildParamSet(paramSet); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +int32_t TestEd25519SignTeeVerifyLocal() +{ + HKS_TEST_LOG_D("TestEd25519SignTeeVerifyLocal enter!"); + + struct HksBlob ed25519Alias = { strlen(TEST_ALIAS_ED25519), (uint8_t *)TEST_ALIAS_ED25519 }; + struct HksBlob msg = {strlen(TEST_PLAIN_TEST_ED25519), (uint8_t *)TEST_PLAIN_TEST_ED25519}; + struct HksBlob signature = { TEST_CURVE_256, g_buffer }; + struct HksParamSet *paramSetSign = NULL; + struct HksParamSet *paramSetVerify = NULL; + struct HksBlob pubKeyInfo = { g_pubKeyLen, g_pubKey }; + + int32_t ret = TestGenerateEd25519Key(ed25519Alias); + HKS_TEST_ASSERT(ret == 0); + + ret = HksExportPublicKey(&ed25519Alias, NULL, &pubKeyInfo); + HKS_TEST_ASSERT(ret == 0); + HKS_TEST_LOG_I("HksExportPublicKey puKey size is %u", pubKeyInfo.size); + + ret = BuildTeeSignParamSet(¶mSetSign); + HKS_TEST_ASSERT(ret == 0); + ret = HksSign(&ed25519Alias, paramSetSign, &msg, &signature); + HKS_TEST_ASSERT(ret == 0); + HKS_TEST_LOG_I("Test_Ed25519_Sign_TEE signature size is %u", signature.size); + + ret = BuildLocalVerifyParamSet(¶mSetVerify); + HKS_TEST_ASSERT(ret == 0); + ret = HksVerify(&pubKeyInfo, paramSetVerify, &msg, &signature); + HKS_TEST_ASSERT(ret == 0); + HKS_TEST_LOG_I("Test_Ed25519_Verify_Local Success"); + + ret = HksDeleteKey(&ed25519Alias, NULL); + HKS_TEST_ASSERT(ret == 0); + + HksFreeParamSet(¶mSetSign); + HksFreeParamSet(¶mSetVerify); + HKS_TEST_LOG_D("TestEd25519SignTeeVerifyLocal End!\n"); + return ret; +} + +static int32_t TestSignEd25519Wrong(struct HksBlob alias) +{ + struct HksBlob msg = {strlen(TEST_PLAIN_TEST_ED25519), (uint8_t *)TEST_PLAIN_TEST_ED25519}; + + struct HksParamSet *paramSet = NULL; + int32_t ret = HksInitParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = HKS_ALG_ED25519; + ret = HksAddParams(paramSet, &algParam, 1); + HKS_TEST_ASSERT(ret == 0); + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + purposeParam.uint32Param = HKS_KEY_PURPOSE_VERIFY; + ret = HksAddParams(paramSet, &purposeParam, 1); + HKS_TEST_ASSERT(ret == 0); + struct HksParam digestParam = {0}; + digestParam.tag = HKS_TAG_DIGEST; + digestParam.uint32Param = HKS_DIGEST_SHA256; + ret = HksAddParams(paramSet, &digestParam, 1); + HKS_TEST_ASSERT(ret == 0); + struct HksParam paddingParam = {0}; + paddingParam.tag = HKS_TAG_PADDING; + paddingParam.uint32Param = HKS_PADDING_PKCS5; + ret = HksAddParams(paramSet, &paddingParam, 1); + HKS_TEST_ASSERT(ret == 0); + ret = HksBuildParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + struct HksBlob signature = { TEST_CURVE_256, g_buffer }; + ret = HksSign(&alias, paramSet, &msg, &signature); + HKS_TEST_ASSERT(ret != 0); + g_bufferSize = signature.size; + HKS_TEST_LOG_I("TestSignEd25519 signature size is %u", signature.size); + HksFreeParamSet(¶mSet); + return ret; +} + +int32_t TestCurve25519SignWrong() +{ + struct HksBlob ed25519Alias = { strlen(TEST_ALIAS_ED25519), (uint8_t *)TEST_ALIAS_ED25519 }; + int32_t ret = TestGenerateEd25519Key(ed25519Alias); + HKS_TEST_ASSERT(ret == 0); + ret = TestSignEd25519Wrong(ed25519Alias); + HKS_TEST_ASSERT(ret != 0); + int32_t retTwo = HksDeleteKey(&ed25519Alias, NULL); + HKS_TEST_ASSERT(retTwo == 0); + if ((ret != 0) && (retTwo == 0)) { + return 0; + } + return 1; +} + +static int32_t TestVerifyEd25519Wrong(struct HksBlob alias) +{ + struct HksBlob msg = {strlen(TEST_PLAIN_TEST_ED25519), (uint8_t *)TEST_PLAIN_TEST_ED25519}; + + HKS_TEST_LOG_I("TestVerifyEd25519!\n"); + struct HksParamSet *paramSet = NULL; + int32_t ret = HksInitParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam algParam = {0}; + algParam.tag = HKS_TAG_ALGORITHM; + algParam.uint32Param = HKS_ALG_ED25519; + ret = HksAddParams(paramSet, &algParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam purposeParam = {0}; + purposeParam.tag = HKS_TAG_PURPOSE; + purposeParam.uint32Param = HKS_KEY_PURPOSE_SIGN; + ret = HksAddParams(paramSet, &purposeParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam digestParam = {0}; + digestParam.tag = HKS_TAG_DIGEST; + digestParam.uint32Param = HKS_DIGEST_SHA256; + ret = HksAddParams(paramSet, &digestParam, 1); + HKS_TEST_ASSERT(ret == 0); + + struct HksParam paddingParam = {0}; + paddingParam.tag = HKS_TAG_PADDING; + paddingParam.uint32Param = HKS_PADDING_PKCS5; + ret = HksAddParams(paramSet, &paddingParam, 1); + HKS_TEST_ASSERT(ret == 0); + + ret = HksBuildParamSet(¶mSet); + HKS_TEST_ASSERT(ret == 0); + + struct HksBlob signature = { g_bufferSize, g_buffer }; + ret = HksVerify(&alias, paramSet, &msg, &signature); + HKS_TEST_ASSERT(ret != 0); + HksFreeParamSet(¶mSet); + return ret; +} + +int32_t TestCurve25519verifyWrong() +{ + struct HksBlob ed25519Alias = { strlen(TEST_ALIAS_ED25519), (uint8_t *)TEST_ALIAS_ED25519 }; + int32_t ret = TestGenerateEd25519Key(ed25519Alias); + HKS_TEST_ASSERT(ret == 0); + ret = TestSignEd25519(ed25519Alias); + HKS_TEST_ASSERT(ret == 0); + ret = TestVerifyEd25519Wrong(ed25519Alias); + HKS_TEST_ASSERT(ret != 0); + int32_t retTwo = HksDeleteKey(&ed25519Alias, NULL); + HKS_TEST_ASSERT(retTwo == 0); + if ((ret != 0) && (retTwo == 0)) { + return 0; + } + return 1; +} \ No newline at end of file diff --git a/security_lite/huks/common/hks_test_file_operator.c b/security_lite/huks/common/hks_test_file_operator.c new file mode 100644 index 0000000000000000000000000000000000000000..b2f8334d876b652e82635cfdf1e16114149cf00e --- /dev/null +++ b/security_lite/huks/common/hks_test_file_operator.c @@ -0,0 +1,337 @@ +/* + * Copyright (c) 2021 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 "hks_test_file_operator.h" + +#ifndef __LITEOS_M__ +#include +#include +#include +#include +#include +#include +#include + +/* use product definitions temporarily */ +#define DEFAULT_FILE_PERMISSION 0666 +#else + +#include + +#endif /* _STORAGE_LITE_ */ + + +#include "hks_param.h" +#include "hks_test_log.h" +#include "hks_test_mem.h" +#include "hks_type.h" +#include "securec.h" + +#define HKS_MAX_FILE_NAME_LEN 512 + +static int32_t GetFileName(const char *path, const char *fileName, char *fullFileName, uint32_t fullFileNameLen) +{ + if (path != NULL) { + if (strncpy_s(fullFileName, fullFileNameLen, path, strlen(path)) != EOK) { + return HKS_ERROR_INTERNAL_ERROR; + } + + if (path[strlen(path) - 1] != '/') { + if (strncat_s(fullFileName, fullFileNameLen, "/", strlen("/")) != EOK) { + return HKS_ERROR_INTERNAL_ERROR; + } + } + + if (strncat_s(fullFileName, fullFileNameLen, fileName, strlen(fileName)) != EOK) { + return HKS_ERROR_INTERNAL_ERROR; + } + } else { + if (strncpy_s(fullFileName, fullFileNameLen, fileName, strlen(fileName)) != EOK) { + return HKS_ERROR_INTERNAL_ERROR; + } + } + + return HKS_SUCCESS; +} + +static int32_t GetFullFileName(const char *path, const char *fileName, char **fullFileName) +{ + uint32_t nameLen = HKS_MAX_FILE_NAME_LEN; + char *tmpFileName = (char *)HksTestMalloc(nameLen); + if (tmpFileName == NULL) { + return HKS_ERROR_MALLOC_FAIL; + } + (void)memset_s(tmpFileName, nameLen, 0, nameLen); + + int32_t ret = GetFileName(path, fileName, tmpFileName, nameLen); + if (ret != HKS_SUCCESS) { + HKS_TEST_LOG_E("get full fileName failed"); + HksTestFree(tmpFileName); + return ret; + } + + *fullFileName = tmpFileName; + return HKS_SUCCESS; +} + +#ifndef __LITEOS_M__ +static int32_t IsFileExist(const char *fileName) +{ + if (access(fileName, F_OK) != 0) { + return HKS_ERROR_NOT_EXIST; + } + + return HKS_SUCCESS; +} + +static uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +{ + (void)offset; + if (IsFileExist(fileName) != HKS_SUCCESS) { + return 0; + } + + char filePath[PATH_MAX + 1] = {0}; + (void)realpath(fileName, filePath); + if (strstr(filePath, "../") != NULL) { + HKS_TEST_LOG_E("invalid filePath, path %s", filePath); + return 0; + } + + FILE *fp = fopen(filePath, "rb"); + if (fp == NULL) { + HKS_TEST_LOG_E("failed to open file"); + return 0; + } + + uint32_t size = fread(buf, 1, len, fp); + if (fclose(fp) < 0) { + HKS_TEST_LOG_E("failed to close file"); + return 0; + } + + return size; +} + +static uint32_t FileSize(const char *fileName) +{ + if (IsFileExist(fileName) != HKS_SUCCESS) { + return 0; + } + + struct stat fileStat; + (void)memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat)); + if (stat(fileName, &fileStat) != 0) { + HKS_TEST_LOG_E("file stat fail."); + return 0; + } + + return fileStat.st_size; +} + +static int32_t FileWrite(const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len) +{ + (void)offset; + char filePath[PATH_MAX + 1] = {0}; + if (memcpy_s(filePath, sizeof(filePath) - 1, fileName, strlen(fileName)) != EOK) { + return HKS_ERROR_BAD_STATE; + } + (void)realpath(fileName, filePath); + if (strstr(filePath, "../") != NULL) { + HKS_TEST_LOG_E("invalid filePath, path %s", filePath); + return HKS_ERROR_INVALID_KEY_FILE; + } + + /* caller function ensures that the folder exists */ + FILE *fp = fopen(filePath, "wb+"); + if (fp == NULL) { + HKS_TEST_LOG_E("open file fail"); + return HKS_ERROR_OPEN_FILE_FAIL; + } + + if (chmod(filePath, S_IRUSR | S_IWUSR) < 0) { + HKS_TEST_LOG_E("chmod file fail."); + fclose(fp); + return HKS_ERROR_OPEN_FILE_FAIL; + } + + uint32_t size = fwrite(buf, 1, len, fp); + if (size != len) { + HKS_TEST_LOG_E("write file size fail."); + fclose(fp); + return HKS_ERROR_WRITE_FILE_FAIL; + } + + if (fclose(fp) < 0) { + HKS_TEST_LOG_E("failed to close file"); + return HKS_ERROR_CLOSE_FILE_FAIL; + } + + return HKS_SUCCESS; +} + +int32_t HksIsFileExist(const char *path, const char *fileName) +{ + if (fileName == NULL) { + return HKS_ERROR_NULL_POINTER; + } + + char *fullFileName = NULL; + int32_t ret = GetFullFileName(path, fileName, &fullFileName); + if (ret != HKS_SUCCESS) { + return ret; + } + + ret = IsFileExist(fullFileName); + HksTestFree(fullFileName); + return ret; +} +#else +static uint32_t FileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +{ + /* now offset is 0, but we maybe extend hi1131 file interfaces in the future */ + if (offset != 0) { + return HKS_ERROR_INVALID_ARGUMENT; + } + + unsigned int fileSize; + int32_t ret = UtilsFileStat(fileName, &fileSize); + if (ret < 0) { + HKS_TEST_LOG_E("stat file failed, errno = 0x%x", ret); + return 0; + } + + if (len > fileSize) { + HKS_TEST_LOG_E("read data over file size!\n, file size = %d\n, buf len = %d\n", fileSize, len); + return 0; + } + + int fd = UtilsFileOpen(fileName, O_RDONLY_FS, 0); + if (fd < 0) { + HKS_TEST_LOG_E("failed to open file, errno = 0x%x", fd); + return 0; + } + + ret = UtilsFileRead(fd, buf, len); + UtilsFileClose(fd); + if (ret < 0) { + HKS_TEST_LOG_E("failed to read file, errno = 0x%x", ret); + return 0; + } + + return len; +} + +static int32_t FileWrite(const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len) +{ + /* now offset is 0, but we maybe extend hi1131 file interfaces in the future */ + if (offset != 0) { + return HKS_ERROR_INVALID_ARGUMENT; + } + + int fd = UtilsFileOpen(fileName, O_CREAT_FS | O_TRUNC_FS | O_RDWR_FS, 0); + if (fd < 0) { + HKS_TEST_LOG_E("failed to open key file, errno = 0x%x\n", fd); + return HKS_ERROR_OPEN_FILE_FAIL; + } + + int32_t ret = UtilsFileWrite(fd, buf, len); + if (ret < 0) { + HKS_TEST_LOG_E("failed to write key file, errno = 0x%x\n", ret); + ret = HKS_ERROR_WRITE_FILE_FAIL; + } + + ret = UtilsFileClose(fd); + if (ret < 0) { + HKS_TEST_LOG_E("failed to close file, errno = 0x%x\n", ret); + ret = HKS_ERROR_CLOSE_FILE_FAIL; + } + + return ret; +} + +static uint32_t FileSize(const char *fileName) +{ + unsigned int fileSize; + int32_t ret = UtilsFileStat(fileName, &fileSize); + if (ret < 0) { + HKS_TEST_LOG_E("stat file failed, errno = 0x%x", ret); + return 0; + } + return fileSize; +} + +int32_t HksTestFileRemove(const char *path, const char *fileName) +{ + char *fullFileName = NULL; + int32_t ret = GetFullFileName(path, fileName, &fullFileName); + if (ret != HKS_SUCCESS) { + return 0; + } + + return UtilsFileDelete(fullFileName); +} + +#endif +uint32_t HksTestFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len) +{ + if ((fileName == NULL) || (buf == NULL) || (len == 0)) { + return 0; + } + + char *fullFileName = NULL; + int32_t ret = GetFullFileName(path, fileName, &fullFileName); + if (ret != HKS_SUCCESS) { + return 0; + } + + uint32_t size = FileRead(fullFileName, offset, buf, len); + HksTestFree(fullFileName); + return size; +} + +int32_t HksTestFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len) +{ + if ((fileName == NULL) || (buf == NULL) || (len == 0)) { + return HKS_ERROR_INVALID_ARGUMENT; + } + + char *fullFileName = NULL; + int32_t ret = GetFullFileName(path, fileName, &fullFileName); + if (ret != HKS_SUCCESS) { + return ret; + } + + ret = FileWrite(fullFileName, offset, buf, len); + HksTestFree(fullFileName); + return ret; +} + +uint32_t HksTestFileSize(const char *path, const char *fileName) +{ + if (fileName == NULL) { + return 0; + } + + char *fullFileName = NULL; + int32_t ret = GetFullFileName(path, fileName, &fullFileName); + if (ret != HKS_SUCCESS) { + return 0; + } + + uint32_t size = FileSize(fullFileName); + HksTestFree(fullFileName); + return size; +} diff --git a/security_lite/huks/common/hks_test_log.c b/security_lite/huks/common/hks_test_log.c new file mode 100644 index 0000000000000000000000000000000000000000..65f0abcb0336477ad631e1fb6995bf2e1a96928a --- /dev/null +++ b/security_lite/huks/common/hks_test_log.c @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2020-2021 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 "hks_test_log.h" + +#include "securec.h" + +#include "stdlib.h" + +#include "time.h" + +#define MAX_LOG_BUFF_LEN 512 +#define WAIT_TO_LOG_DONE 100000 + +void HksTestLog(uint32_t logLevel, const char *funcName, int32_t lineNo, const char *format, ...) +{ + char buf[MAX_LOG_BUFF_LEN] = { 0 }; + + va_list ap; + va_start(ap, format); + int32_t ret = vsnprintf_s(buf, MAX_LOG_BUFF_LEN, MAX_LOG_BUFF_LEN - 1, format, ap); + va_end(ap); + if (ret < 0) { + printf("hks log concatenate error."); + return; + } + + switch (logLevel) { + case HKS_LOG_LEVEL_I: + printf("[INFO] %s [%d]: %s\n", funcName, lineNo, buf); + break; + case HKS_LOG_LEVEL_E: + printf("[ERROR] %s [%d]: %s\n", funcName, lineNo, buf); + break; + case HKS_LOG_LEVEL_W: + printf("[WARNING] %s [%d]: %s\n", funcName, lineNo, buf); + break; + case HKS_LOG_LEVEL_D: + printf("[DEBUG] %s [%d]: %s\n", funcName, lineNo, buf); + break; + default: + return; + } + + usleep(WAIT_TO_LOG_DONE); +} + +void HksAssertLog(bool test) +{ + if (!(test)) { + HKS_TEST_LOG_E("[ASSERT] :fail\n"); + } +} diff --git a/security_lite/huks/common/hks_test_mem.c b/security_lite/huks/common/hks_test_mem.c new file mode 100644 index 0000000000000000000000000000000000000000..c2d749ed26442ca5c75a06afce163bb36a6445db --- /dev/null +++ b/security_lite/huks/common/hks_test_mem.c @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 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 "hks_test_mem.h" + +#include +#include + +void *HksTestMalloc(size_t size) +{ + return malloc(size); +} + +void HksTestFree(void *ptr) +{ + free(ptr); +} diff --git a/security_lite/huks/common/include/hks_aes_test.h b/security_lite/huks/common/include/hks_aes_test.h new file mode 100644 index 0000000000000000000000000000000000000000..b28e7f0d4a93194a12dcbbf6f55f08e9fc5c6ad2 --- /dev/null +++ b/security_lite/huks/common/include/hks_aes_test.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2021 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 HUKS_AES_TEST_H +#define HUKS_AES_TEST_H + +int HksAesTest001(void); + +int HksAesTest002(void); + +int HksAesTest003(void); + +#endif diff --git a/security_lite/huks/common/include/hks_agreement_test.h b/security_lite/huks/common/include/hks_agreement_test.h new file mode 100644 index 0000000000000000000000000000000000000000..4c8c8082fa80ae1c245597156afb058a95450315 --- /dev/null +++ b/security_lite/huks/common/include/hks_agreement_test.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2021 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 HUKS_AGREEMENT_TEST_H +#define HUKS_AGREEMENT_TEST_H + +int HksAgreementTest001(void); + +#endif diff --git a/security_lite/huks/common/include/hks_bn_exp_mod_test.h b/security_lite/huks/common/include/hks_bn_exp_mod_test.h new file mode 100644 index 0000000000000000000000000000000000000000..daa07ba18c212cac08327d06f52bd0a6bc7723d0 --- /dev/null +++ b/security_lite/huks/common/include/hks_bn_exp_mod_test.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2021 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 HUKS_BN_EXP_MOD_TEST_H +#define HUKS_BN_EXP_MOD_TEST_H + +int HksBnExpModTest001(void); + +#endif diff --git a/security_lite/huks/common/include/hks_cipher_test.h b/security_lite/huks/common/include/hks_cipher_test.h new file mode 100644 index 0000000000000000000000000000000000000000..4c2a27bc36c81c4e794d17a1ff8e17a411193eba --- /dev/null +++ b/security_lite/huks/common/include/hks_cipher_test.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2021 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 HUKS_CIPHER_TEST_H +#define HUKS_CIPHER_TEST_H + +int HksCipherTest001(void); + +int HksCipherTest002(void); + +int HksCipherTest003(void); + +int HksCipherTest004(void); + +int HksCipherTest005(void); + +int HksCipherTest006(void); + +int HksCipherTest007(void); + +int HksCipherTest008(void); + +int HksCipherTest009(void); + +int HksCipherTest010(void); + +int HksCipherTest011(void); + +int HksCipherTest012(void); + +#endif diff --git a/security_lite/huks/common/include/hks_curve25519_test.h b/security_lite/huks/common/include/hks_curve25519_test.h new file mode 100644 index 0000000000000000000000000000000000000000..1e7cf3bc90bfc957d910c0f5998edb6d8fad6fd2 --- /dev/null +++ b/security_lite/huks/common/include/hks_curve25519_test.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2021 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 HUKS_CURVE25519_TEST_H +#define HUKS_CURVE25519_TEST_H + +int HksCurve25519Test001(void); + +int HksCurve25519Test002(void); + +int HksCurve25519Test003(void); + +int HksCurve25519Test004(void); + +#endif \ No newline at end of file diff --git a/security_lite/huks/common/include/hks_delete_test.h b/security_lite/huks/common/include/hks_delete_test.h new file mode 100644 index 0000000000000000000000000000000000000000..426745e71e1c177fb8f9b46d0f98a6fb31f7e842 --- /dev/null +++ b/security_lite/huks/common/include/hks_delete_test.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2021 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 HUKS_DELETE_TEST_H +#define HUKS_DELETE_TEST_H + +int HksDeleteTest001(void); + +#endif diff --git a/security_lite/huks/common/include/hks_derive_test.h b/security_lite/huks/common/include/hks_derive_test.h new file mode 100644 index 0000000000000000000000000000000000000000..356c4f42fc978f1ade3f9b01b4960453e77732f0 --- /dev/null +++ b/security_lite/huks/common/include/hks_derive_test.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2021 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 HUKS_DERIVE_TEST_H +#define HUKS_DERIVE_TEST_H + +int HksDeriveTest001(void); + +int HksDeriveTest002(void); + +#endif diff --git a/security_lite/huks/common/include/hks_exist_test.h b/security_lite/huks/common/include/hks_exist_test.h new file mode 100644 index 0000000000000000000000000000000000000000..90c044bf7ca01d94a59b7a83739e20356bb7ac30 --- /dev/null +++ b/security_lite/huks/common/include/hks_exist_test.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2021 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 HUKS_EXIST_TEST_H +#define HUKS_EXIST_TEST_H + +int HksExistTest001(void); + +#endif diff --git a/security_lite/huks/common/include/hks_generate_key_test.h b/security_lite/huks/common/include/hks_generate_key_test.h new file mode 100644 index 0000000000000000000000000000000000000000..8a829b66d946ed8bbcc28905e39843ca93ee6c76 --- /dev/null +++ b/security_lite/huks/common/include/hks_generate_key_test.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2021 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 HUKS_GENERATE_KEY_TEST_H +#define HUKS_GENERATE_KEY_TEST_H + +int HksGenerateKeyTest001(void); + +#endif diff --git a/security_lite/huks/common/include/hks_generate_random_test.h b/security_lite/huks/common/include/hks_generate_random_test.h new file mode 100644 index 0000000000000000000000000000000000000000..cfb5718cbf127e22172146d2d6f6d9c979fd8990 --- /dev/null +++ b/security_lite/huks/common/include/hks_generate_random_test.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2021 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 HUKS_GENERATE_RANDOM_TEST_H +#define HUKS_GENERATE_RANDOM_TEST_H + +int HksGenerateRandomTest001(void); + +#endif diff --git a/security_lite/huks/common/include/hks_hash_test.h b/security_lite/huks/common/include/hks_hash_test.h new file mode 100644 index 0000000000000000000000000000000000000000..2b7e1ee0be13cd716a80141b67a3c3b57ce109c8 --- /dev/null +++ b/security_lite/huks/common/include/hks_hash_test.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2021 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 HUKS_HASH_TEST_H +#define HUKS_HASH_TEST_H + +int HksHashTest001(void); + +#endif diff --git a/security_lite/huks/common/include/hks_mac_test.h b/security_lite/huks/common/include/hks_mac_test.h new file mode 100644 index 0000000000000000000000000000000000000000..0c0ba45844660791b006aca482d6ccddd24a4b8d --- /dev/null +++ b/security_lite/huks/common/include/hks_mac_test.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2021 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 HUKS_MAC_TEST_H +#define HUKS_MAC_TEST_H + +int HksMacTest001(void); + +int HksMacTest002(void); + +#endif diff --git a/security_lite/huks/common/include/hks_modify_key_test.h b/security_lite/huks/common/include/hks_modify_key_test.h new file mode 100644 index 0000000000000000000000000000000000000000..d1c41bc4005e84f5e5070e6fbf41a3805d6c83da --- /dev/null +++ b/security_lite/huks/common/include/hks_modify_key_test.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2021 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 HUKS_MODIFY_KEY_TEST_H +#define HUKS_MODIFY_KEY_TEST_H + +int HksModifyKeyTest001(void); + +#endif diff --git a/security_lite/huks/common/include/hks_others_test.h b/security_lite/huks/common/include/hks_others_test.h new file mode 100644 index 0000000000000000000000000000000000000000..639a481d0538287b28ec494accb4298d1c9278e1 --- /dev/null +++ b/security_lite/huks/common/include/hks_others_test.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2021 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 HUKS_OTHERS_TEST_H +#define HUKS_OTHERS_TEST_H + +int HksOthersTest001(void); + +int HksOthersTest002(void); + +int HksOthersTest003(void); + +int HksOthersTest004(void); + +int HksOthersTest005(void); + +int HksOthersTest006(void); + +int HksOthersTest007(void); + +int HksOthersTest008(void); + +#endif diff --git a/security_lite/huks/common/include/hks_safe_cipher_key_test.h b/security_lite/huks/common/include/hks_safe_cipher_key_test.h new file mode 100644 index 0000000000000000000000000000000000000000..574d32a5b2944b1eee58005e28d92afb1e3e9a7a --- /dev/null +++ b/security_lite/huks/common/include/hks_safe_cipher_key_test.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2021 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 HUKS_SAFE_CIPHER_KEY_TEST_H +#define HUKS_SAFE_CIPHER_KEY_TEST_H + +int HksSafeCipherKeyTest001(void); + +#endif diff --git a/security_lite/huks/common/include/hks_safe_compare_key_test.h b/security_lite/huks/common/include/hks_safe_compare_key_test.h new file mode 100644 index 0000000000000000000000000000000000000000..f9af96e01e25c89919a9a31647817355bc8e75db --- /dev/null +++ b/security_lite/huks/common/include/hks_safe_compare_key_test.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2021 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 HUKS_SAFE_COMPARE_KEY_TEST_H +#define HUKS_SAFE_COMPARE_KEY_TEST_H + +int HksSafeCompareKeyTest001(void); + +#endif diff --git a/security_lite/huks/common/include/hks_test_aes.h b/security_lite/huks/common/include/hks_test_aes.h new file mode 100644 index 0000000000000000000000000000000000000000..148e94f6fed30001df5025a0cb73d8a2449e0b83 --- /dev/null +++ b/security_lite/huks/common/include/hks_test_aes.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 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 HKS_TEST_AES_H +#define HKS_TEST_AES_H +#include "hks_type.h" +#include "securec.h" +#define TEST_ALIAS_AES "test_key_alias_aes" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t TestAes256ByAgree(); + +int32_t TestAes256ByAgree1(); + +int32_t TestAes256ByAgree2(); + +int32_t TestAes256ByLocal(); +#ifdef __cplusplus +} +#endif + +#endif /* HKS_TEST_AES_H */ diff --git a/security_lite/huks/common/include/hks_test_api_performance.h b/security_lite/huks/common/include/hks_test_api_performance.h new file mode 100644 index 0000000000000000000000000000000000000000..5f626b04d20de7849a5e5b420be1c1be30e4227d --- /dev/null +++ b/security_lite/huks/common/include/hks_test_api_performance.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 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 HKS_TEST_API_PERFORMANCE_H +#define HKS_TEST_API_PERFORMANCE_H + +#include "hks_type.h" + +#include "securec.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t HksGenerateKeyRun(const struct HksBlob *keyAlias, const struct HksParamSet *paramSetIn, + struct HksParamSet *paramSetOut, uint32_t performTimes); + +int32_t HksEncryptRun(const struct HksBlob *key, const struct HksParamSet *paramSet, + const struct HksBlob *plainText, struct HksBlob *cipherText, uint32_t performTimes); + +int32_t HksDecryptRun(const struct HksBlob *key, const struct HksParamSet *paramSet, + const struct HksBlob *cipherText, struct HksBlob *plainText, uint32_t performTimes); + +int32_t HksMacRun(const struct HksBlob *key, const struct HksParamSet *paramSet, + const struct HksBlob *srcData, struct HksBlob *mac, uint32_t performTimes); + +int32_t HksDeriveKeyRun(const struct HksParamSet *paramSet, const struct HksBlob *masterKey, + struct HksBlob *derivedKey, uint32_t performTimes); + +int32_t HksDeleteKeyRun(const struct HksBlob *keyAlias, uint32_t performTimes); + +int32_t HksKeyExistRun(const struct HksBlob *keyAlias, uint32_t performTimes); + +int32_t HksGenerateRandomRun(struct HksBlob *random, uint32_t performTimes); + +int32_t HksAgreeKeyRun(const struct HksParamSet *paramSet, const struct HksBlob *privateKey, + const struct HksBlob *peerPublicKey, struct HksBlob *agreedKey, uint32_t performTimes); + +int32_t HksHashRun(const struct HksParamSet *paramSet, + const struct HksBlob *srcData, struct HksBlob *hash, uint32_t performTimes); + +int32_t HksBnExpModRun(struct HksBlob *x, const struct HksBlob *a, + const struct HksBlob *e, const struct HksBlob *n, uint32_t performTimes); + +#ifdef __cplusplus +} +#endif + +#endif /* HKS_TEST_API_PERFORMANCE_H */ \ No newline at end of file diff --git a/security_lite/huks/common/include/hks_test_cipher.h b/security_lite/huks/common/include/hks_test_cipher.h new file mode 100644 index 0000000000000000000000000000000000000000..a45288c886202c8fdd860ff4fcc50536adfa2381 --- /dev/null +++ b/security_lite/huks/common/include/hks_test_cipher.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021 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 HKS_TEST_CIPHER_H +#define HKS_TEST_CIPHER_H + +#include "hks_type.h" + +struct CipherEncryptStructure { + const struct HksBlob *keyAlias; + const struct HksTestCipherParamsParamSet *cipherParms; + struct HksBlob *plainData; + struct HksBlob *cipherData; + struct HksBlob **ivData; + struct HksBlob **nonceData; + struct HksBlob **aadData; + uint32_t performTimes; +}; + +struct OnlyDecryptStructure { + struct HksBlob *keyAlias; + const struct HksTestCipherParamsParamSet *cipherParms; + struct HksBlob *cipherData; + struct HksBlob *decryptedData; + struct HksBlob **ivData; + struct HksBlob **nonceData; + struct HksBlob **aadData; + uint32_t performTimes; +}; + +struct CipherDecryptStructure { + struct HksBlob *keyAlias; + const struct HksTestCipherParams *cipherParms; + struct HksBlob *cipherData; + struct HksBlob **decryptedData; + struct HksBlob *ivData; + struct HksBlob *nonceData; + struct HksBlob *aadData; + uint32_t performTimes; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t BaseTestCipher(uint32_t times, uint32_t index, uint32_t performTimes); + +int32_t BaseTestEncrypt(uint32_t times, uint32_t index, uint32_t performTimes); + +int32_t BaseTestDecrypt(uint32_t times, uint32_t index, uint32_t performTimes); + +#ifdef __cplusplus +} +#endif + +#endif /* HKS_TEST_COMMON_H */ \ No newline at end of file diff --git a/security_lite/huks/common/include/hks_test_common.h b/security_lite/huks/common/include/hks_test_common.h new file mode 100644 index 0000000000000000000000000000000000000000..1b4b06fd8b08c699f1d6eaf780f1770bd53c1492 --- /dev/null +++ b/security_lite/huks/common/include/hks_test_common.h @@ -0,0 +1,433 @@ +/* + * Copyright (c) 2021 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 HKS_TEST_COMMON_H +#define HKS_TEST_COMMON_H + +#include "hks_type.h" +#include "securec.h" + +#define GOTO_ERROR_IF_FAIL(ret, err) \ + if ((ret) != 0) { \ + goto err; \ + } + +#define GOTO_ERROR_IF_SUCCESS(ret, err) \ + if ((ret) == 0) { \ + goto err; \ + } + +#define HKS_SLICE_MAX_LEN 64 * 1024 + +#define DEFAULT_TEST_CASE_ID 0xFFFFFFFF +#define DEFAULT_EXECUTION_TIMES 0xFFFFFFFF +#define DEFAULT_PERFORM_TIMES 0xFFFFFFFF + +#define MAX_ARGS_LEN 20 +#define DEFAULT_KEY_ALIAS_SIZE 64 +#define DEFAULT_SLICE_SIZE (2 * 128 + 1) + +#define DEFAULT_PARAM_SET_OUT_SIZE 4096 +#define HKS_MAX_RANDOM_LEN 1024 +#define MAX_OUT_BLOB_SIZE (5 * 1024 * 1024) + +#define TEST_MAX_FILE_NAME_LEN 512 + +#define DEFAULT_LOCAL_KEY_SIZE 256 + +/* inputparams: struct HksBlob *blob */ +struct HksTestBlobParams { + bool blobExist; /* blob null or not */ + uint32_t blobSize; /* blob->size value */ + bool blobDataExist; /* blob->data null or not */ + uint32_t blobDataSize; /* blob->data malloc size */ +}; + +/* inputparams: struct HksParamSet *paramSet */ +struct HksTestGenKeyParamsParamSet { + bool paramSetExist; /* paramSet null or not */ + bool setAlg; + uint32_t alg; + bool setKeySize; + uint32_t keySize; + bool setPurpose; + uint32_t purpose; + bool setDigest; + uint32_t digest; + bool setPadding; + uint32_t padding; + bool setBlockMode; + uint32_t mode; + bool setKeyStorageFlag; + uint32_t keyStorageFlag; +}; + +struct HksTestGenKeyParamsParamSetOut { + bool paramSetExist; /* paramSet null or not */ + uint32_t paramSetSize; +}; + +enum HksTestCipherType { + HKS_TEST_CIPHER_TYPE_AES, + HKS_TEST_CIPHER_TYPE_RSA, +}; + +struct TestRsaCipherParamSet { + struct HksParamSet **outParamSet; + bool paramSetExist; + bool setAlg; + uint32_t alg; + bool setPurpose; + uint32_t purpose; + bool setDigest; + uint32_t digest; + bool setPadding; + uint32_t padding; +}; + +struct HksTestCipherParamsParamSet { + enum HksTestCipherType cipherType; + bool paramSetExist; /* paramSet null or not */ + bool setAlg; + uint32_t alg; + bool setPurpose; + uint32_t purpose; + bool setDigest; + uint32_t digest; + bool setPadding; + uint32_t padding; + bool setBlockMode; + uint32_t mode; + bool setIv; + uint32_t ivSize; + bool setNonce; + uint32_t nonceSize; + bool setAad; + uint32_t aadSize; + bool setIsKeyAlias; + bool isKeyAlias; +}; + +struct HksTestMacParamSet { + bool paramSetExist; /* paramSet null or not */ + bool setPurpose; + uint32_t purpose; + bool setDigest; + uint32_t digest; +}; + +struct HksTestDeriveParamSet { + bool paramSetExist; /* paramSet null or not */ + bool setAlg; + uint32_t alg; + bool setPurpose; + uint32_t purpose; + bool setDigest; + uint32_t digest; + bool setIteration; + uint32_t iteration; + bool setSalt; + uint32_t saltSize; + bool setInfo; + uint32_t infoSize; + bool setIsKeyAlias; + bool isKeyAlias; +}; + +struct HksTestAgreeParamSet { + bool paramSetExist; /* paramSet null or not */ + bool setAlg; + uint32_t alg; + bool setKeySize; + uint32_t keySize; + bool setIsKeyAlias; + bool isKeyAlias; +}; + +enum HksTestMacType { + HKS_TEST_MAC_TYPE_REE, + HKS_TEST_MAC_TYPE_TEE, +}; + +struct HksTestHashParamsParamSet { + bool paramSetExist; /* paramSet null or not */ + bool setDigest; + uint32_t digest; +}; + +struct HksTestParamSetOut { + bool paramSetExist; + uint32_t paramSetSize; + uint32_t realParamSetSize; +}; + +struct HksTestGenKeyParams { + uint32_t testId; + int32_t expectResult; + struct HksTestBlobParams keyAliasParams; + struct HksTestGenKeyParamsParamSet paramSetParams; + struct HksTestGenKeyParamsParamSetOut paramSetParamsOut; +}; + +struct HksTestCipherParams { + uint32_t testId; + int32_t expectResult; + struct HksTestBlobParams keyAliasParams; + struct HksTestGenKeyParamsParamSet genKeyParamSetParams; + struct HksTestGenKeyParamsParamSetOut genKeyParamSetParamsOut; + struct HksTestCipherParamsParamSet encryptParamSetParams; + struct HksTestCipherParamsParamSet decryptParamSetParams; + struct HksTestBlobParams plainTextParams; + struct HksTestBlobParams cipherTextParams; + struct HksTestBlobParams decryptedTextParams; + struct HksTestBlobParams localKeyParams; +}; + +struct HksTestEncryptParams { + uint32_t testId; + int32_t expectResult; + struct HksTestBlobParams keyAliasParams; + struct HksTestGenKeyParamsParamSet genKeyParamSetParams; + struct HksTestBlobParams encryptAliasParams; + struct HksTestCipherParamsParamSet encryptParamSetParams; + struct HksTestBlobParams inDataParams; + struct HksTestBlobParams outDataParams; + struct HksTestBlobParams localKeyParams; +}; + +struct HksTestDecryptParams { + uint32_t testId; + int32_t expectResult; + struct HksTestBlobParams keyAliasParams; + struct HksTestGenKeyParamsParamSet genKeyParamSetParams; + struct HksTestBlobParams decryptAliasParams; + struct HksTestCipherParamsParamSet decryptParamSetParams; + struct HksTestBlobParams inDataParams; + struct HksTestBlobParams outDataParams; + struct HksTestBlobParams localKeyParams; +}; + +struct HksTestMacParams { + uint32_t testId; + int32_t expectResult; + enum HksTestMacType macType; + struct HksTestBlobParams keyAliasParams; + struct HksTestGenKeyParamsParamSet genKeyParamSetParams; + struct HksTestBlobParams keyParams; + struct HksTestMacParamSet macParamSetParams; + struct HksTestBlobParams srcDataParams; + struct HksTestBlobParams macParams; +}; + +struct HksTestDeriveParams { + uint32_t testId; + int32_t expectResult; + struct HksTestBlobParams keyAliasParams; + struct HksTestGenKeyParamsParamSet genKeyParamSetParams; + struct HksTestBlobParams masterKeyParams; + struct HksTestDeriveParamSet deriveParamSetParams; + struct HksTestBlobParams derivedKeyParams; + struct HksTestBlobParams localKeyParams; +}; + +struct HksTestKeyExistParams { + uint32_t testId; + int32_t expectResult; + bool isGenKey; + struct HksTestBlobParams keyAliasParams; +}; + +struct HksTestGetKeyParamSetParams { + uint32_t testId; + int32_t expectResult; + bool isGenKey; + struct HksTestBlobParams keyAliasParams; + struct HksTestParamSetOut paramSetOutParams; +}; + +struct HksTestGenRandomParams { + uint32_t testId; + int32_t expectResult; + struct HksTestBlobParams randomParams; +}; + +struct HksTestHashParams { + uint32_t testId; + int32_t expectResult; + struct HksTestHashParamsParamSet paramSetParams; + struct HksTestBlobParams srcDataParams; + struct HksTestBlobParams hashParams; +}; + +struct HksTestBnExpModParams { + uint32_t testId; + int32_t expectResult; + bool isTestValue; + struct HksTestBlobParams xParams; + struct HksTestBlobParams aParams; + struct HksTestBlobParams eParams; + struct HksTestBlobParams nParams; +}; + +struct HksTestAgreeParams { + uint32_t testId; + int32_t expectResult; + struct HksTestBlobParams keyAlias1Params; + struct HksTestBlobParams keyAlias2Params; + struct HksTestGenKeyParamsParamSet genKeyParamSetParams; + struct HksTestBlobParams privateKeyParams; + struct HksTestAgreeParamSet agreeParamSetParams; + struct HksTestBlobParams peerPublicParams; + struct HksTestBlobParams agreedKeyParams; + struct HksTestBlobParams localPrivateKeyParams; + struct HksTestBlobParams localPublicKeyParams; +}; + +struct TestAgreeParamSetStructure { + struct HksParamSet **outParamSet; + bool paramSetExist; + bool setAlg; + uint32_t alg; + bool setKeySize; + uint32_t keySize; + bool setIsKeyAlias; + bool isKeyAlias; +}; + +struct GenerateKeyParamSetStructure { + struct HksParamSet **outParamSet; + bool paramSetExist; + bool setAlg; + uint32_t alg; + bool setKeySize; + uint32_t keySize; + bool setPurpose; + uint32_t purpose; + bool setDigest; + uint32_t digest; + bool setPadding; + uint32_t padding; + bool setBlockMode; + uint32_t mode; + bool setKeyStorageFlag; + uint32_t keyStorageFlag; +}; + +struct AesCipherParamSetStructure { + struct HksParamSet **outParamSet; + bool paramSetExist; + bool setAlg; + uint32_t alg; + bool setPurpose; + uint32_t purpose; + bool setPadding; + uint32_t padding; + bool setBlockMode; + uint32_t mode; + bool setIv; + const struct HksBlob *ivBlob; + bool setNonce; + const struct HksBlob *nonceBlob; + bool setAad; + const struct HksBlob *aadBlob; + bool setIsKeyAlias; + bool isKeyAlias; +}; + +struct TestMacParamSetStructure { + struct HksParamSet **outParamSet; + bool paramSetExist; + bool setPurpose; + uint32_t purpose; + bool setDigest; + uint32_t digest; + bool setKeyAlias; + bool isKeyAlias; +}; + +struct TestDeriveParamSetStructure { + struct HksParamSet **outParamSet; + bool paramSetExist; + bool setAlg; + uint32_t alg; + bool setPurpose; + uint32_t purpose; + bool setDigest; + uint32_t digest; + bool setIteration; + uint32_t iteration; + bool setSalt; + const struct HksBlob *saltBlob; + bool setInfo; + const struct HksBlob *infoBlob; + bool setIsKeyAlias; + bool isKeyAlias; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +void TestFreeBlob(struct HksBlob **key); + +int32_t TestConstuctBlob(struct HksBlob **blob, + bool blobExist, + uint32_t blobSize, + bool blobDataExist, + uint32_t realBlobDataSize); + +int32_t TestConstructGenerateKeyParamSetOut( + struct HksParamSet **outParamSet, + bool paramSetExist, uint32_t paramSetSize); + +int32_t TestConstructGenerateKeyParamSet(struct GenerateKeyParamSetStructure *paramStruct); + +int32_t TestConstructRsaCipherParamSet(struct TestRsaCipherParamSet *paramStruct); + +int32_t TestConstructAesCipherParamSet(struct AesCipherParamSetStructure *paramStruct); + +int32_t TestConstructMacParamSet(struct TestMacParamSetStructure *paramStruct); + +int32_t TestConstructAgreeParamSet(struct TestAgreeParamSetStructure *paramStruct); + +int32_t GenerateLocalX25519Key(struct HksBlob **privateKey, struct HksBlob **publicKey, + const struct HksTestBlobParams *localPrivateKeyParams, const struct HksTestBlobParams *localPublicKeyParams); + +int32_t TestConstructDeriveParamSet(struct TestDeriveParamSetStructure *paramStruct); + +int32_t TestConstructHashParamSet( + struct HksParamSet **outParamSet, + bool paramSetExist, + bool setDigest, uint32_t digest); + +int32_t GenerateKey(struct HksBlob **keyAlias, const struct HksTestBlobParams *keyAliasParams, + const struct HksTestGenKeyParamsParamSet *genKeyParamSetParams, + const struct HksTestGenKeyParamsParamSetOut *genKeyParamSetParamsOut); + +int32_t GenerateLocalRandomKey(struct HksBlob **keyAlias, const struct HksTestBlobParams *localKeyParams); + +int32_t TestConstructBlobOut(struct HksBlob **blob, + bool blobExist, + uint32_t blobSize, + bool blobDataExist, + uint32_t realBlobDataSize); + +int32_t TestGenDefaultKeyAndGetAlias(struct HksBlob **keyAlias); + +#ifdef __cplusplus +} +#endif + +#endif /* HKS_TEST_COMMON_H */ diff --git a/security_lite/huks/common/include/hks_test_curve25519.h b/security_lite/huks/common/include/hks_test_curve25519.h new file mode 100644 index 0000000000000000000000000000000000000000..68b88ec5d51e65efe3fa1ebe06458fe8232f3b04 --- /dev/null +++ b/security_lite/huks/common/include/hks_test_curve25519.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 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 HKS_TEST_CURVE25519 +#define HKS_TEST_CURVE25519 +#include "hks_type.h" + +#include "securec.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t TestGenerateEd25519Key(struct HksBlob alias); + +int32_t TestImportEd25519(struct HksBlob alias, struct HksBlob *pubKeyInfo); + +int32_t TestCurve25519All(); + +int32_t TestEd25519SignTeeVerifyLocal(); + +int32_t TestCurve25519SignWrong(); + +int32_t TestCurve25519verifyWrong(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/security_lite/huks/common/include/hks_test_file_operator.h b/security_lite/huks/common/include/hks_test_file_operator.h new file mode 100644 index 0000000000000000000000000000000000000000..0673ec79598cf375403936c35108ea52a74f833d --- /dev/null +++ b/security_lite/huks/common/include/hks_test_file_operator.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 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 "hks_type.h" + +#ifndef HKS_TEST_FILE_OPERATOR_H +#define HKS_TEST_FILE_OPERATOR_H + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t HksIsFileExist(const char *path, const char *fileName); + +uint32_t HksTestFileSize(const char *path, const char *fileName); + +uint32_t HksTestFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len); + +int32_t HksTestFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len); + +int32_t HksTestFileRemove(const char *path, const char *fileName); +#ifdef __cplusplus +} +#endif + +#endif /* HKS_TEST_FILE_OPERATOR_H */ diff --git a/security_lite/huks/common/include/hks_test_log.h b/security_lite/huks/common/include/hks_test_log.h new file mode 100644 index 0000000000000000000000000000000000000000..ca245317839be19cfbdc3c691cc7553865ebeacb --- /dev/null +++ b/security_lite/huks/common/include/hks_test_log.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2021 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 HKS_TEST_LOG_H +#define HKS_TEST_LOG_H + +#include "stdint.h" +#include "stdbool.h" + +#define HKS_TEST_ASSERT(test) \ + if (!(test)) { \ + printf("[ASSERT][%s](%d):fail\n", __func__, __LINE__); \ + } + +#ifndef _CUT_LOG_ +#define HKS_TEST_LOG_E(fmt...) \ +do { \ + printf("[ERROR]\t[%s](%d): ", __func__, __LINE__); \ + printf(fmt); \ + printf("\r\n"); \ +} while (0) + +#define HKS_TEST_LOG_I(fmt...) \ +do { \ + printf("[INFO]\t[%s](%d): ", __func__, __LINE__); \ + printf(fmt); \ + printf("\r\n"); \ +} while (0) + +#define HKS_TEST_LOG_W(fmt...) \ +do { \ + printf("[WARN]\t[%s](%d): ", __func__, __LINE__); \ + printf(fmt); \ + printf("\r\n"); \ +} while (0) + +#define HKS_TEST_LOG_D(fmt...) \ +do { \ + printf("[DEBUG]\t[%s](%d): ", __func__, __LINE__); \ + printf(fmt); \ + printf("\r\n"); \ +} while (0) +#else +#define HKS_TEST_LOG_E(...) +#define HKS_TEST_LOG_I(...) +#define HKS_TEST_LOG_W(...) +#define HKS_TEST_LOG_D(...) +#endif + +#endif /* HKS_TEST_LOG_H */ diff --git a/security_lite/huks/common/include/hks_test_mem.h b/security_lite/huks/common/include/hks_test_mem.h new file mode 100644 index 0000000000000000000000000000000000000000..f0b7fba910fb6c015e8187c14c57df0a06e2fd02 --- /dev/null +++ b/security_lite/huks/common/include/hks_test_mem.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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 HKS_TEST_MEM_H +#define HKS_TEST_MEM_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void *HksTestMalloc(size_t size); +void HksTestFree(void *ptr); + +#ifdef __cplusplus +} +#endif + +#endif /* HKS_TEST_MEM_H */ diff --git a/security_lite/huks/liteos_a_adapter/BUILD.gn b/security_lite/huks/liteos_a_adapter/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..dd5d027459eabee737bc2b460ee218fd53400eae --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/BUILD.gn @@ -0,0 +1,64 @@ +# +# Copyright (c) 2021 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. +# + +import("//base/security/huks/build/config.gni") +import("//build/lite/config/component/lite_component.gni") +import("//build/lite/config/test.gni") +import("//test/xts/tools/lite/build/suite_lite.gni") + +hcpptest_suite("huks_lite_function_test") { + suite_name = "acts" + sources = [ + "hks_aes_test.cpp", + "hks_agreement_test.cpp", + "hks_bn_exp_mod_test.cpp", + "hks_cipher_test.cpp", + "hks_curve25519_test.cpp", + "hks_delete_test.cpp", + "hks_derive_test.cpp", + "hks_exist_test.cpp", + "hks_generate_key_test.cpp", + "hks_generate_random_test.cpp", + "hks_hash_test.cpp", + "hks_mac_test.cpp", + "hks_modify_key_test.cpp", + "hks_others_test.cpp", + "hks_safe_cipher_key_test.cpp", + "hks_safe_compare_key_test.cpp", + ] + + include_dirs = [ + "//utils/native/base/include", + "//third_party/bounds_checking_function/include", + "//base/security/huks/huawei_property/include", + "../common/include", + ] + + deps = [ + "//base/security/huks/interfaces/innerkits/huks_lite:huks_3.0_sdk", + "//test/xts/acts/security_lite/huks/common:huks_new_test_common", + ] + cflags = [ "-Wno-error" ] + + defines = [ "_LITEONE_SYSTEM_" ] + + if (disable_authenticate == true) { + defines = [ "_CUT_AUTHENTICATE_" ] + } +} + +group("Hukslitetest") { + deps = [ ":huks_lite_function_test" ] +} diff --git a/security_lite/huks/liteos_a_adapter/Test.json b/security_lite/huks/liteos_a_adapter/Test.json new file mode 100644 index 0000000000000000000000000000000000000000..f4981473f032c0cdc280791f07909eb781845260 --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/Test.json @@ -0,0 +1,25 @@ +{ + "description": "Config for hcpptest demo test cases", + "environment": [ + { + "type": "device", + "label": "ipcamera" + } + ], + "kits": [ + { + "type": "MountKit", + "server": "NfsServer", + "mount": [ + { + "source": "testcases/security", + "target": "/test_root/security" + } + ] + } + ], + "driver": { + "type": "CppTestLite", + "execute": "/test_root/security/ActsSecurityHukslitetest.bin" + } +} \ No newline at end of file diff --git a/security_lite/huks/liteos_a_adapter/hks_aes_test.cpp b/security_lite/huks/liteos_a_adapter/hks_aes_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b905c3d774791bab8dca4ca94d445ac3d2b0f136 --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_aes_test.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_aes_test.h" + +#include "securec.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_aes.h" + +using namespace testing::ext; +namespace { +class HksAesTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksAesTest::SetUpTestCase(void) +{ +} + +void HksAesTest::TearDownTestCase(void) +{ +} + +void HksAesTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksAesTest::TearDown() +{ +} + +#ifndef _CUT_AUTHENTICATE_ +/** + * @tc.name: HksCipherTest.HksCipherTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksAesTest, HksAesTest001, TestSize.Level1) +{ + ASSERT_TRUE(TestAes256ByAgree() == 0); +} + +/** + * @tc.name: HksCipherTest.HksCipherTest002 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksAesTest, HksAesTest002, TestSize.Level1) +{ + ASSERT_TRUE(TestAes256ByAgree1() == 0); +} + +/** + * @tc.name: HksCipherTest.HksCipherTest003 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksAesTest, HksAesTest003, TestSize.Level1) +{ + ASSERT_TRUE(TestAes256ByAgree2() == 0); +} +#endif /* _CUT_AUTHENTICATE_ */ + +/** + * @tc.name: HksCipherTest.HksCipherTest003 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksAesTest, HksAesTest004, TestSize.Level1) +{ + ASSERT_EQ(TestAes256ByLocal(), 0); +} +} diff --git a/security_lite/huks/liteos_a_adapter/hks_agreement_test.cpp b/security_lite/huks/liteos_a_adapter/hks_agreement_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1752786e5adbf0c5f74549d24f0cfbeaa95fcaa4 --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_agreement_test.cpp @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_agreement_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "hks_type.h" + +using namespace testing::ext; +namespace { +#ifndef _CUT_AUTHENTICATE_ + +class HksAgreementTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksAgreementTest::SetUpTestCase(void) +{ +} + +void HksAgreementTest::TearDownTestCase(void) +{ +} + +void HksAgreementTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksAgreementTest::TearDown() +{ +} + +const int TMP_SIZE = 512; +const int X25519_KEY_SIZE = 32; +const struct HksTestAgreeParams g_testAgreeParams[] = { + /* ree x25519 success */ + { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { true, true, HKS_ALG_X25519, true, HKS_CURVE25519_KEY_SIZE_256, true, + HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY, true, HKS_DIGEST_SHA256, + false, 0, false, 0, true, HKS_STORAGE_TEMP }, + { 0 }, + { true, true, HKS_ALG_X25519, true, HKS_CURVE25519_KEY_SIZE_256, true, false }, + { true, TMP_SIZE, true, TMP_SIZE }, + { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE }, + { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE }, + { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE } + }, +}; + +static int32_t AgreeKey(const struct HksTestAgreeParamSet *agreeParamSetParams, struct HksBlob *privateKey, + struct HksBlob *peerPublicKey, struct HksBlob *agreedKey) +{ + struct HksParamSet *agreeParamSet = NULL; + struct TestAgreeParamSetStructure paramStruct = { + &agreeParamSet, + agreeParamSetParams->paramSetExist, + agreeParamSetParams->setAlg, agreeParamSetParams->alg, + agreeParamSetParams->setKeySize, agreeParamSetParams->keySize, + agreeParamSetParams->setIsKeyAlias, agreeParamSetParams->isKeyAlias + }; + int32_t ret = TestConstructAgreeParamSet(¶mStruct); + HKS_TEST_ASSERT(ret == 0); + + ret = HksAgreeKeyRun(agreeParamSet, privateKey, peerPublicKey, agreedKey, 1); + HksFreeParamSet(&agreeParamSet); + return ret; +} + +/** + * @tc.name: HksDeleteTest.HksDeleteTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksAgreementTest, HksAgreementTest001, TestSize.Level1) +{ + /* 1. generate key */ + struct HksBlob *privateKey = NULL; + struct HksBlob *peerPubKeyAlias = NULL; + struct HksBlob *peerPublicKey = NULL; + int32_t ret; + + if (g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag && + (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) { + ret = GenerateLocalX25519Key(&privateKey, NULL, &g_testAgreeParams[0].localPrivateKeyParams, NULL); + ASSERT_TRUE(ret == 0); + ret = GenerateLocalX25519Key(NULL, &peerPublicKey, NULL, &g_testAgreeParams[0].localPublicKeyParams); + ASSERT_TRUE(ret == 0); + } + /* 2. agreeKey */ + struct HksBlob *agreeKey = NULL; + ret = TestConstuctBlob(&agreeKey, + g_testAgreeParams[0].agreedKeyParams.blobExist, + g_testAgreeParams[0].agreedKeyParams.blobSize, + g_testAgreeParams[0].agreedKeyParams.blobDataExist, + g_testAgreeParams[0].agreedKeyParams.blobDataSize); + ASSERT_TRUE(ret == 0); + + ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey, peerPublicKey, agreeKey); + ASSERT_TRUE(ret == g_testAgreeParams[0].expectResult); + + /* 3. delete key */ + if (!(g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag && + (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) && + ((g_testAgreeParams[0].keyAlias1Params.blobExist) && + (g_testAgreeParams[0].keyAlias2Params.blobExist))) { + ret = HksDeleteKey(privateKey, NULL); + ASSERT_TRUE(ret == 0); + ret = HksDeleteKey(peerPubKeyAlias, NULL); + ASSERT_TRUE(ret == 0); + } + TestFreeBlob(&privateKey); + TestFreeBlob(&peerPubKeyAlias); + TestFreeBlob(&peerPublicKey); + TestFreeBlob(&agreeKey); + ASSERT_TRUE(ret == 0); +} + +/** + * @tc.name: HksDeleteTest.HksDeleteTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksAgreementTest, HksAgreementTest002, TestSize.Level1) +{ + /* 1. generate key */ + struct HksBlob *privateKey = NULL; + struct HksBlob *peerPubKeyAlias = NULL; + struct HksBlob *peerPublicKey = NULL; + struct HksBlob *privateKey2 = NULL; + struct HksBlob *peerPubKeyAlias2 = NULL; + struct HksBlob *peerPublicKey2 = NULL; + int32_t ret; + + if (g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag && + (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) { + ret = GenerateLocalX25519Key(&privateKey, &peerPublicKey, &g_testAgreeParams[0].localPrivateKeyParams, \ + &g_testAgreeParams[0].localPublicKeyParams); + ASSERT_TRUE(ret == 0); + ret = GenerateLocalX25519Key(&privateKey2, &peerPublicKey2, &g_testAgreeParams[0].localPrivateKeyParams, \ + &g_testAgreeParams[0].localPublicKeyParams); + ASSERT_TRUE(ret == 0); + } + /* 2. agreeKey */ + struct HksBlob *agreeKey = NULL; + ret = TestConstuctBlob(&agreeKey, g_testAgreeParams[0].agreedKeyParams.blobExist, \ + g_testAgreeParams[0].agreedKeyParams.blobSize, g_testAgreeParams[0].agreedKeyParams.blobDataExist, \ + g_testAgreeParams[0].agreedKeyParams.blobDataSize); + ASSERT_TRUE(ret == 0); + + struct HksBlob *agreeKey2 = NULL; + ret = TestConstuctBlob(&agreeKey2, g_testAgreeParams[0].agreedKeyParams.blobExist, \ + g_testAgreeParams[0].agreedKeyParams.blobSize, g_testAgreeParams[0].agreedKeyParams.blobDataExist, \ + g_testAgreeParams[0].agreedKeyParams.blobDataSize); + ASSERT_TRUE(ret == 0); + + ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey, peerPublicKey2, agreeKey); + ASSERT_TRUE(ret == g_testAgreeParams[0].expectResult); + + ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey2, peerPublicKey, agreeKey2); + ASSERT_TRUE(ret == g_testAgreeParams[0].expectResult); + EXPECT_EQ(agreeKey->size, agreeKey2->size); + EXPECT_EQ(memcmp(agreeKey->data, agreeKey2->data, agreeKey->size), 0); + + /* 3. delete key */ + if (!(g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag && \ + (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) && \ + ((g_testAgreeParams[0].keyAlias1Params.blobExist) && (g_testAgreeParams[0].keyAlias2Params.blobExist))) { + ASSERT_TRUE(HksDeleteKey(privateKey, NULL) == 0); + ASSERT_TRUE(HksDeleteKey(peerPubKeyAlias, NULL) == 0); + } + TestFreeBlob(&privateKey); + TestFreeBlob(&peerPubKeyAlias); + TestFreeBlob(&peerPublicKey); + TestFreeBlob(&agreeKey); +} + + +#endif /* _CUT_AUTHENTICATE_ */ +} \ No newline at end of file diff --git a/security_lite/huks/liteos_a_adapter/hks_bn_exp_mod_test.cpp b/security_lite/huks/liteos_a_adapter/hks_bn_exp_mod_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa182c31769573dec7864b826eeb75aa539ec5ff --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_bn_exp_mod_test.cpp @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_bn_exp_mod_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "hks_type.h" + +using namespace testing::ext; +namespace { +class HksBnExpModTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksBnExpModTest::SetUpTestCase(void) +{ +} + +void HksBnExpModTest::TearDownTestCase(void) +{ +} + +void HksBnExpModTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksBnExpModTest::TearDown() +{ +} + +const int DEFAULT_X_SIZE = 256; +const int DEFAULT_A_SIZE = 256; +const int DEFAULT_E_SIZE = 256; +const int DEFAULT_N_SIZE = 256; +const int HKS_TEST_2 = 2; +const int HKS_TEST_8 = 8; + +const struct HksTestBnExpModParams g_testBnExpModParams[] = { + /* normal case */ + { 0, HKS_SUCCESS, false, + { true, DEFAULT_X_SIZE, true, DEFAULT_X_SIZE }, + { true, DEFAULT_A_SIZE, true, DEFAULT_A_SIZE }, + { true, DEFAULT_E_SIZE, true, DEFAULT_E_SIZE }, + { true, DEFAULT_N_SIZE, true, DEFAULT_N_SIZE } + }, +}; + +static int32_t TestValue() +{ + HKS_TEST_LOG_I("test value"); + uint8_t bufX[HKS_TEST_8] = { 0, 0, 0, 0, 0, 0, 0, 0x40 }; + uint8_t bufA[HKS_TEST_8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + uint8_t bufE[HKS_TEST_2] = { 0, 2 }; + uint8_t bufN[HKS_TEST_8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + uint8_t tmpBufX[HKS_TEST_8] = {0}; + struct HksBlob tmpX = { HKS_TEST_8, tmpBufX }; + struct HksBlob tmpA = { HKS_TEST_8, bufA }; + struct HksBlob tmpE = { HKS_TEST_2, bufE }; + struct HksBlob tmpN = { HKS_TEST_8, bufN }; + int32_t ret = HksBnExpModRun(&tmpX, &tmpA, &tmpE, &tmpN, 1); + for (int i = 0; i < HKS_TEST_8; ++i) { + HKS_TEST_LOG_I("%x, %x", tmpBufX[i], bufX[i]); + HKS_TEST_ASSERT(tmpBufX[i] == bufX[i]); + } + return ret; +} + +/** + * @tc.name: HksBnExpModTest.HksBnExpModTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksBnExpModTest, HksBnExpModTest001, TestSize.Level1) +{ + int32_t ret; + if (g_testBnExpModParams[0].isTestValue) { + ret = TestValue(); + ASSERT_TRUE(ret == 0); + } else { + struct HksBlob *x = NULL; + struct HksBlob *a = NULL; + struct HksBlob *e = NULL; + struct HksBlob *n = NULL; + ret = TestConstuctBlob(&x, g_testBnExpModParams[0].xParams.blobExist, + g_testBnExpModParams[0].xParams.blobSize, g_testBnExpModParams[0].xParams.blobDataExist, + g_testBnExpModParams[0].xParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + ret = TestConstructBlobOut(&a, g_testBnExpModParams[0].aParams.blobExist, + g_testBnExpModParams[0].aParams.blobSize, g_testBnExpModParams[0].aParams.blobDataExist, + g_testBnExpModParams[0].aParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + ret = TestConstuctBlob(&e, g_testBnExpModParams[0].eParams.blobExist, + g_testBnExpModParams[0].eParams.blobSize, g_testBnExpModParams[0].eParams.blobDataExist, + g_testBnExpModParams[0].eParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + ret = TestConstuctBlob(&n, g_testBnExpModParams[0].nParams.blobExist, + g_testBnExpModParams[0].nParams.blobSize, g_testBnExpModParams[0].nParams.blobDataExist, + g_testBnExpModParams[0].nParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + if ((n != NULL) && (n->data != NULL) && (n->size != 0)) { + n->data[n->size - 1] = n->data[n->size - 1] | 0x00000001; /* make sure n is odd */ + } + + ret = HksBnExpModRun(x, a, e, n, 1); + + HKS_TEST_ASSERT(ret == g_testBnExpModParams[0].expectResult); + + TestFreeBlob(&x); + TestFreeBlob(&a); + TestFreeBlob(&e); + TestFreeBlob(&n); + ASSERT_TRUE(ret == 0); + } +} +} \ No newline at end of file diff --git a/security_lite/huks/liteos_a_adapter/hks_cipher_test.cpp b/security_lite/huks/liteos_a_adapter/hks_cipher_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22de5c8e6eff1ce40a3fcdba29b13f37bf5fa610 --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_cipher_test.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_cipher_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_cipher.h" + +using namespace testing::ext; +namespace { +class HksCipherTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksCipherTest::SetUpTestCase(void) +{ +} + +void HksCipherTest::TearDownTestCase(void) +{ +} + +void HksCipherTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksCipherTest::TearDown() +{ +} + +/** + * @tc.name: HksCipherTest.HksCipherTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksCipherTest, HksCipherTest001, TestSize.Level1) +{ + ASSERT_TRUE(BaseTestCipher(1, 0, 1) == 0); +} + +#ifndef _CUT_AUTHENTICATE_ +HWTEST_F(HksCipherTest, HksCipherTest002, TestSize.Level1) +{ + ASSERT_TRUE(BaseTestCipher(1, 1, 1) == 0); +} + +HWTEST_F(HksCipherTest, HksCipherTest003, TestSize.Level1) +{ + ASSERT_TRUE(BaseTestEncrypt(1, 0, 1) == 0); +} + +HWTEST_F(HksCipherTest, HksCipherTest004, TestSize.Level1) +{ + ASSERT_TRUE(BaseTestEncrypt(1, 1, 1) == 0); +} + +HWTEST_F(HksCipherTest, HksCipherTest005, TestSize.Level1) +{ + ASSERT_TRUE(BaseTestEncrypt(1, 2, 1) == 0); +} + +HWTEST_F(HksCipherTest, HksCipherTest006, TestSize.Level1) +{ + ASSERT_TRUE(BaseTestEncrypt(1, 3, 1) == 0); +} + +HWTEST_F(HksCipherTest, HksCipherTest007, TestSize.Level1) +{ + ASSERT_TRUE(BaseTestEncrypt(1, 4, 1) == 0); +} + +HWTEST_F(HksCipherTest, HksCipherTest008, TestSize.Level1) +{ + ASSERT_TRUE(BaseTestDecrypt(1, 0, 1) == 0); +} + +HWTEST_F(HksCipherTest, HksCipherTest009, TestSize.Level1) +{ + ASSERT_TRUE(BaseTestDecrypt(1, 1, 1) == 0); +} + +HWTEST_F(HksCipherTest, HksCipherTest010, TestSize.Level1) +{ + ASSERT_TRUE(BaseTestDecrypt(1, 2, 1) == 0); +} + +HWTEST_F(HksCipherTest, HksCipherTest011, TestSize.Level1) +{ + ASSERT_TRUE(BaseTestDecrypt(1, 3, 1) == 0); +} + +HWTEST_F(HksCipherTest, HksCipherTest012, TestSize.Level1) +{ + ASSERT_TRUE(BaseTestDecrypt(1, 4, 1) == 0); +} +#endif /* _CUT_AUTHENTICATE_ */ +} \ No newline at end of file diff --git a/security_lite/huks/liteos_a_adapter/hks_curve25519_test.cpp b/security_lite/huks/liteos_a_adapter/hks_curve25519_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13bd2c35fdd108c9ddea4982164144630770bfb8 --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_curve25519_test.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_curve25519_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_curve25519.h" + +using namespace testing::ext; +namespace { +#ifndef _CUT_AUTHENTICATE_ +class HksCurve25519Test : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksCurve25519Test::SetUpTestCase(void) +{ +} + +void HksCurve25519Test::TearDownTestCase(void) +{ +} + +void HksCurve25519Test::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksCurve25519Test::TearDown() +{ +} + +/** + * @tc.name: HksCurve25519Test.HksCurve25519Test001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksCurve25519Test, HksCurve25519Test001, TestSize.Level1) +{ + ASSERT_TRUE(TestCurve25519All() == 0); +} + +/** + * @tc.name: HksCurve25519Test.HksCurve25519Test002 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksCurve25519Test, HksCurve25519Test002, TestSize.Level1) +{ + ASSERT_TRUE(TestEd25519SignTeeVerifyLocal() == 0); +} + +/** + * @tc.name: HksCurve25519Test.HksCurve25519Test003 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksCurve25519Test, HksCurve25519Test003, TestSize.Level1) +{ + ASSERT_TRUE(TestCurve25519SignWrong() == 0); +} + +/** + * @tc.name: HksCurve25519Test.HksCurve25519Test004 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksCurve25519Test, HksCurve25519Test004, TestSize.Level1) +{ + ASSERT_TRUE(TestCurve25519verifyWrong() == 0); +} +#endif /* _CUT_AUTHENTICATE_ */ +} \ No newline at end of file diff --git a/security_lite/huks/liteos_a_adapter/hks_delete_test.cpp b/security_lite/huks/liteos_a_adapter/hks_delete_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db5c83415324679d9d7245590f7ae9f6efbfda1c --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_delete_test.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_delete_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "hks_type.h" + +using namespace testing::ext; +namespace { +#ifndef _CUT_AUTHENTICATE_ +class HksDeleteTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksDeleteTest::SetUpTestCase(void) +{ +} + +void HksDeleteTest::TearDownTestCase(void) +{ +} + +void HksDeleteTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksDeleteTest::TearDown() +{ +} + +const struct HksTestKeyExistParams g_testKeyExistParams[] = { + /* normal case */ + { 0, HKS_SUCCESS, true, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE } }, +}; + +/** + * @tc.name: HksDeleteTest.HksDeleteTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksDeleteTest, HksDeleteTest001, TestSize.Level1) +{ + int32_t ret; + struct HksBlob *keyAlias = NULL; + if (g_testKeyExistParams[0].isGenKey) { + HKS_TEST_ASSERT(TestGenDefaultKeyAndGetAlias(&keyAlias) == 0); + } else { + ret = TestConstuctBlob(&keyAlias, + g_testKeyExistParams[0].keyAliasParams.blobExist, + g_testKeyExistParams[0].keyAliasParams.blobSize, + g_testKeyExistParams[0].keyAliasParams.blobDataExist, + g_testKeyExistParams[0].keyAliasParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + } + + ret = HksDeleteKeyRun(keyAlias, 1); + HKS_TEST_ASSERT(ret == g_testKeyExistParams[0].expectResult); + + TestFreeBlob(&keyAlias); + ASSERT_TRUE(ret == 0); +} + +#endif /* _CUT_AUTHENTICATE_ */ +} \ No newline at end of file diff --git a/security_lite/huks/liteos_a_adapter/hks_derive_test.cpp b/security_lite/huks/liteos_a_adapter/hks_derive_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f0f33a8fabec402ea5825e4b2457ddade5395ce --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_derive_test.cpp @@ -0,0 +1,223 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_derive_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" + +using namespace testing::ext; +namespace { +class HksDeriveTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksDeriveTest::SetUpTestCase(void) +{ +} + +void HksDeriveTest::TearDownTestCase(void) +{ +} + +void HksDeriveTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksDeriveTest::TearDown() +{ +} + +const int DEFAULT_DERIVE_SIZE = 32; +const int DEFAULT_INFO_SIZE = 55; +const int DEFAULT_SALT_SIZE = 16; + +const struct HksTestDeriveParams g_testDeriveParams[] = { + /* hkdf-sha256-salt-info */ + { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_DERIVE, + true, HKS_DIGEST_SHA256, + false, 0, + false, 0, + false, 0 }, + { 0 }, + { + true, /* derive params */ + true, HKS_ALG_HKDF, + true, HKS_KEY_PURPOSE_DERIVE, + true, HKS_DIGEST_SHA256, + false, 0, + true, DEFAULT_SALT_SIZE, + true, DEFAULT_INFO_SIZE, + false, true }, + { + true, DEFAULT_DERIVE_SIZE, true, DEFAULT_DERIVE_SIZE }, + { + false, 0, false, 0 } + }, + + /* local: hkdf-sha256-salt-info */ + { 1, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_DERIVE, + true, HKS_DIGEST_SHA256, + false, 0, + false, 0, + true, HKS_STORAGE_TEMP }, + { 0 }, + { + true, /* derive params */ + true, HKS_ALG_HKDF, + true, HKS_KEY_PURPOSE_DERIVE, + true, HKS_DIGEST_SHA256, + false, 0, + true, DEFAULT_SALT_SIZE, + true, DEFAULT_INFO_SIZE, + true, false }, + { + true, DEFAULT_DERIVE_SIZE, true, DEFAULT_DERIVE_SIZE }, + { + true, DEFAULT_LOCAL_KEY_SIZE, true, DEFAULT_LOCAL_KEY_SIZE } + }, +}; + +static int32_t DeriveKey(const struct HksTestDeriveParamSet *deriveParamSetParams, const struct HksBlob *masterKey, + struct HksBlob *derivedKey, struct HksBlob **saltData, struct HksBlob **infoData) +{ + struct HksParamSet *deriveParamSet = NULL; + uint32_t saltSize = deriveParamSetParams->saltSize; + uint32_t infoSize = deriveParamSetParams->infoSize; + if (saltSize != 0) { + HKS_TEST_ASSERT(TestConstuctBlob(saltData, true, saltSize, true, saltSize) == 0); + } + if (infoSize != 0) { + HKS_TEST_ASSERT(TestConstuctBlob(infoData, true, infoSize, true, infoSize) == 0); + } + struct TestDeriveParamSetStructure paramStruct = { + &deriveParamSet, + deriveParamSetParams->paramSetExist, + deriveParamSetParams->setAlg, deriveParamSetParams->alg, + deriveParamSetParams->setPurpose, deriveParamSetParams->purpose, + deriveParamSetParams->setDigest, deriveParamSetParams->digest, + deriveParamSetParams->setIteration, deriveParamSetParams->iteration, + deriveParamSetParams->setSalt, *saltData, + deriveParamSetParams->setInfo, *infoData, + deriveParamSetParams->setIsKeyAlias, deriveParamSetParams->isKeyAlias + }; + int32_t ret = TestConstructDeriveParamSet(¶mStruct); + HKS_TEST_ASSERT(ret == 0); + + ret = HksDeriveKeyRun(deriveParamSet, masterKey, derivedKey, 1); + HksFreeParamSet(&deriveParamSet); + return ret; +} + +static int32_t BaseTestDerive(uint32_t index) +{ + /* 1. generate key */ + struct HksBlob *keyAlias = NULL; + int32_t ret; + if (g_testDeriveParams[index].genKeyParamSetParams.setKeyStorageFlag && + (g_testDeriveParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) { + ret = GenerateLocalRandomKey(&keyAlias, &g_testDeriveParams[index].localKeyParams); + } else { + if (g_testDeriveParams[index].keyAliasParams.blobExist) { + ret = GenerateKey(&keyAlias, &g_testDeriveParams[index].keyAliasParams, + &g_testDeriveParams[index].genKeyParamSetParams, NULL); + } else { + ret = TestConstuctBlob(&keyAlias, + g_testDeriveParams[index].masterKeyParams.blobExist, + g_testDeriveParams[index].masterKeyParams.blobSize, + g_testDeriveParams[index].masterKeyParams.blobDataExist, + g_testDeriveParams[index].masterKeyParams.blobDataSize); + } + } + HKS_TEST_ASSERT(ret == 0); + + /* 2. derive */ + struct HksBlob *derivedKey = NULL; + ret = TestConstuctBlob(&derivedKey, + g_testDeriveParams[index].derivedKeyParams.blobExist, + g_testDeriveParams[index].derivedKeyParams.blobSize, + g_testDeriveParams[index].derivedKeyParams.blobDataExist, + g_testDeriveParams[index].derivedKeyParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + struct HksBlob *saltData = NULL; + struct HksBlob *infoData = NULL; + ret = DeriveKey(&g_testDeriveParams[index].deriveParamSetParams, keyAlias, derivedKey, &saltData, &infoData); + if (ret != g_testDeriveParams[index].expectResult) { + HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testDeriveParams[index].testId, ret); + } + HKS_TEST_ASSERT(ret == g_testDeriveParams[index].expectResult); + + /* 3. delete key */ + if (!(g_testDeriveParams[index].genKeyParamSetParams.setKeyStorageFlag && + (g_testDeriveParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) && + (g_testDeriveParams[index].keyAliasParams.blobExist)) { + HKS_TEST_ASSERT(HksDeleteKey(keyAlias, NULL) == 0); + } + TestFreeBlob(&keyAlias); + TestFreeBlob(&derivedKey); + TestFreeBlob(&saltData); + TestFreeBlob(&infoData); + + return ret; +} + +#ifndef _CUT_AUTHENTICATE_ +/** + * @tc.name: HksDeriveTest.HksDeriveTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksDeriveTest, HksDeriveTest001, TestSize.Level1) +{ + int32_t ret = BaseTestDerive(0); + ASSERT_TRUE(ret == 0); +} +#endif /* _CUT_AUTHENTICATE_ */ + +/** + * @tc.name: HksDeriveTest.HksDeriveTest002 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksDeriveTest, HksDeriveTest002, TestSize.Level1) +{ + int32_t ret = BaseTestDerive(1); + ASSERT_TRUE(ret == 0); +} +} \ No newline at end of file diff --git a/security_lite/huks/liteos_a_adapter/hks_exist_test.cpp b/security_lite/huks/liteos_a_adapter/hks_exist_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d347851e78c93e106004b7c174e2987e17d432ae --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_exist_test.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_exist_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "hks_type.h" + +using namespace testing::ext; +namespace { +#ifndef _CUT_AUTHENTICATE_ +class HksExistTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksExistTest::SetUpTestCase(void) +{ +} + +void HksExistTest::TearDownTestCase(void) +{ +} + +void HksExistTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksExistTest::TearDown() +{ +} + +const struct HksTestKeyExistParams g_testKeyExistParams[] = { + /* normal case */ + { 0, HKS_SUCCESS, true, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE } }, +}; + +/** + * @tc.name: HksExistTest.HksExistTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksExistTest, HksExistTest001, TestSize.Level1) +{ + int32_t ret; + struct HksBlob *keyAlias = NULL; + if (g_testKeyExistParams[0].isGenKey) { + HKS_TEST_ASSERT(TestGenDefaultKeyAndGetAlias(&keyAlias) == 0); + ret = HksKeyExistRun(keyAlias, 1); + HKS_TEST_ASSERT(ret == g_testKeyExistParams[0].expectResult); + HKS_TEST_ASSERT(HksDeleteKey(keyAlias, NULL) == HKS_SUCCESS); + } else { + ret = TestConstuctBlob(&keyAlias, + g_testKeyExistParams[0].keyAliasParams.blobExist, + g_testKeyExistParams[0].keyAliasParams.blobSize, + g_testKeyExistParams[0].keyAliasParams.blobDataExist, + g_testKeyExistParams[0].keyAliasParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + ret = HksKeyExistRun(keyAlias, 1); + if (ret != g_testKeyExistParams[0].expectResult) { + HKS_TEST_LOG_I("HksKeyExistRun 2 failed, ret[%u] = %d", g_testKeyExistParams[0].testId, ret); + } + HKS_TEST_ASSERT(ret == g_testKeyExistParams[0].expectResult); + } + TestFreeBlob(&keyAlias); + ASSERT_TRUE(ret == 0); +} +#endif /* _CUT_AUTHENTICATE_ */ +} \ No newline at end of file diff --git a/security_lite/huks/liteos_a_adapter/hks_generate_key_test.cpp b/security_lite/huks/liteos_a_adapter/hks_generate_key_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43894c0e3b5bbe1bc83290dd21b388f7d10642e0 --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_generate_key_test.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_generate_key_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" + +using namespace testing::ext; +namespace { +#ifndef _CUT_AUTHENTICATE_ +class HksGenerateKeyTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksGenerateKeyTest::SetUpTestCase(void) +{ +} + +void HksGenerateKeyTest::TearDownTestCase(void) +{ +} + +void HksGenerateKeyTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksGenerateKeyTest::TearDown() +{ +} + +const int DEFAULT_X25519_PARAM_SET_OUT = 104; + +const struct HksTestGenKeyParams g_testGenKeyParams[] = { + /* x25519: ree sign/verify */ + { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, { + true, + true, HKS_ALG_X25519, + true, HKS_CURVE25519_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY, + false, 0, + false, 0, + false, 0, + true, HKS_STORAGE_TEMP }, + { true, DEFAULT_X25519_PARAM_SET_OUT }, + }, +}; + +/** + * @tc.name: HksGenerateKeyTest.HksGenerateKeyTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksGenerateKeyTest, HksGenerateKeyTest001, TestSize.Level1) +{ + uint32_t times = 1; + uint32_t index = 0; + uint32_t performTimes = 1; + struct HksBlob *keyAlias = NULL; + int32_t ret = TestConstuctBlob(&keyAlias, + g_testGenKeyParams[index].keyAliasParams.blobExist, + g_testGenKeyParams[index].keyAliasParams.blobSize, + g_testGenKeyParams[index].keyAliasParams.blobDataExist, + g_testGenKeyParams[index].keyAliasParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *paramSet = NULL; + struct GenerateKeyParamSetStructure paramStruct = { ¶mSet, + g_testGenKeyParams[index].paramSetParams.paramSetExist, + g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg, + g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize, + g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose, + g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest, + g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding, + g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode, + g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag, + g_testGenKeyParams[index].paramSetParams.keyStorageFlag }; + ret = TestConstructGenerateKeyParamSet(¶mStruct); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *paramSetOut = NULL; + ret = TestConstructGenerateKeyParamSetOut(¶mSetOut, + g_testGenKeyParams[index].paramSetParamsOut.paramSetExist, + g_testGenKeyParams[index].paramSetParamsOut.paramSetSize); + HKS_TEST_ASSERT(ret == 0); + + ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes); + if (ret != g_testGenKeyParams[index].expectResult) { + HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret); + } + HKS_TEST_ASSERT(ret == g_testGenKeyParams[index].expectResult); + + if ((ret == HKS_SUCCESS) && + !(g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag == true) && + (g_testGenKeyParams[index].paramSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) { + HKS_TEST_ASSERT(HksDeleteKey(keyAlias, NULL) == 0); + } + TestFreeBlob(&keyAlias); + HksFreeParamSet(¶mSet); + HksFreeParamSet(¶mSetOut); + HKS_TEST_LOG_I("[%u]TestGenerateKey, Testcase_GenerateKey_[%03u] pass!", times, g_testGenKeyParams[index].testId); + ASSERT_TRUE(ret == 0); +} +#endif /* _CUT_AUTHENTICATE_ */ +} \ No newline at end of file diff --git a/security_lite/huks/liteos_a_adapter/hks_generate_random_test.cpp b/security_lite/huks/liteos_a_adapter/hks_generate_random_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5ad2e91ee01764a2c6e29f06ecbde6787c8c362 --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_generate_random_test.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_generate_random_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "hks_type.h" + +using namespace testing::ext; +namespace { +class HksGenerateRandomTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksGenerateRandomTest::SetUpTestCase(void) +{ +} + +void HksGenerateRandomTest::TearDownTestCase(void) +{ +} + +void HksGenerateRandomTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksGenerateRandomTest::TearDown() +{ +} + +const struct HksTestGenRandomParams g_testGenRandomParams[] = { + /* normal case */ + { 0, HKS_SUCCESS, { true, HKS_MAX_RANDOM_LEN, true, HKS_MAX_RANDOM_LEN } }, +}; +/** + * @tc.name: HksGenerateRandomTest.HksGenerateRandomTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksGenerateRandomTest, HksGenerateRandomTest001, TestSize.Level1) +{ + int32_t ret; + struct HksBlob *random = NULL; + + ret = TestConstructBlobOut(&random, + g_testGenRandomParams[0].randomParams.blobExist, + g_testGenRandomParams[0].randomParams.blobSize, + g_testGenRandomParams[0].randomParams.blobDataExist, + g_testGenRandomParams[0].randomParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + ret = HksGenerateRandomRun(random, 1); + if (ret != g_testGenRandomParams[0].expectResult) { + HKS_TEST_LOG_I("HksGenerateRandomRun failed, ret[%u] = %d", g_testGenRandomParams[0].testId, ret); + } + HKS_TEST_ASSERT(ret == g_testGenRandomParams[0].expectResult); + + TestFreeBlob(&random); + ASSERT_TRUE(ret == 0); +} +} \ No newline at end of file diff --git a/security_lite/huks/liteos_a_adapter/hks_hash_test.cpp b/security_lite/huks/liteos_a_adapter/hks_hash_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6cbaf20a7d3f503fcdf09da7d911da4550e8465 --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_hash_test.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_hash_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "hks_type.h" + +using namespace testing::ext; +namespace { +#ifndef _CUT_AUTHENTICATE_ + +class HksHashTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksHashTest::SetUpTestCase(void) +{ +} + +void HksHashTest::TearDownTestCase(void) +{ +} + +void HksHashTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksHashTest::TearDown() +{ +} + +const int DEFAULT_SRC_DATA_SIZE = 200; +const int DIGEST_SHA256_HASH_SIZE = 32; + +const struct HksTestHashParams g_testHashParams[] = { + /* normal case */ + { 0, HKS_SUCCESS, + { true, true, HKS_DIGEST_SHA256 }, + { true, DEFAULT_SRC_DATA_SIZE, true, DEFAULT_SRC_DATA_SIZE }, + { true, DIGEST_SHA256_HASH_SIZE, true, DIGEST_SHA256_HASH_SIZE } + }, +}; + +/** + * @tc.name: HksHashTest.HksHashTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksHashTest, HksHashTest001, TestSize.Level1) +{ + struct HksParamSet *paramSet = NULL; + struct HksBlob *srcData = NULL; + struct HksBlob *hash = NULL; + + int32_t ret = TestConstructHashParamSet(¶mSet, + g_testHashParams[0].paramSetParams.paramSetExist, + g_testHashParams[0].paramSetParams.setDigest, g_testHashParams[0].paramSetParams.digest); + HKS_TEST_ASSERT(ret == 0); + + ret = TestConstuctBlob(&srcData, + g_testHashParams[0].srcDataParams.blobExist, + g_testHashParams[0].srcDataParams.blobSize, + g_testHashParams[0].srcDataParams.blobDataExist, + g_testHashParams[0].srcDataParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + ret = TestConstructBlobOut(&hash, + g_testHashParams[0].hashParams.blobExist, + g_testHashParams[0].hashParams.blobSize, + g_testHashParams[0].hashParams.blobDataExist, + g_testHashParams[0].hashParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + ret = HksHashRun(paramSet, srcData, hash, 1); + if (ret != g_testHashParams[0].expectResult) { + HKS_TEST_LOG_I("HksHashRun failed, ret[%u] = %d", g_testHashParams[0].testId, ret); + } + HKS_TEST_ASSERT(ret == g_testHashParams[0].expectResult); + + HksFreeParamSet(¶mSet); + TestFreeBlob(&srcData); + TestFreeBlob(&hash); + HKS_TEST_LOG_I("[%u]TestHash, Testcase_Hash_[%03u] pass!", 1, g_testHashParams[0].testId); + ASSERT_TRUE(ret == 0); +} +#endif /* _CUT_AUTHENTICATE_ */ +} diff --git a/security_lite/huks/liteos_a_adapter/hks_mac_test.cpp b/security_lite/huks/liteos_a_adapter/hks_mac_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd4ce35a4e96fa776e449054570f6135276d855d --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_mac_test.cpp @@ -0,0 +1,203 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_mac_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" + +using namespace testing::ext; +namespace { +class HksMacTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksMacTest::SetUpTestCase(void) +{ +} + +void HksMacTest::TearDownTestCase(void) +{ +} + +void HksMacTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksMacTest::TearDown() +{ +} + +const int HKS_TEST_MAC_REE_KEY_SIZE_32 = 32; +const int HKS_DEFAULT_MAC_SRCDATA_SIZE = 253; +const int HKS_DEFAULT_MAC_SHA256_SIZE = 32; + + +const struct HksTestMacParams g_testMacParams[] = { + /* success: ree-sha256 */ + { 0, HKS_SUCCESS, HKS_TEST_MAC_TYPE_REE, { 0 }, { 0 }, + { true, HKS_TEST_MAC_REE_KEY_SIZE_32, true, HKS_TEST_MAC_REE_KEY_SIZE_32 }, + { true, true, HKS_KEY_PURPOSE_MAC, true, HKS_DIGEST_SHA256 }, + { true, HKS_DEFAULT_MAC_SRCDATA_SIZE, true, HKS_DEFAULT_MAC_SRCDATA_SIZE }, + { true, HKS_DEFAULT_MAC_SHA256_SIZE, true, HKS_DEFAULT_MAC_SHA256_SIZE } + }, + + /* success: tee-sha256 */ + { 1, HKS_SUCCESS, HKS_TEST_MAC_TYPE_TEE, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { true, true, HKS_ALG_AES, true, HKS_AES_KEY_SIZE_256, true, HKS_KEY_PURPOSE_MAC, + true, HKS_DIGEST_SHA256, false, 0, false, 0 }, + { 0 }, + { true, true, HKS_KEY_PURPOSE_MAC, true, HKS_DIGEST_SHA256 }, + { true, HKS_DEFAULT_MAC_SRCDATA_SIZE, true, HKS_DEFAULT_MAC_SRCDATA_SIZE }, + { true, HKS_DEFAULT_MAC_SHA256_SIZE, true, HKS_DEFAULT_MAC_SHA256_SIZE } + }, +}; + +static int32_t ConstructDataToBlob(struct HksBlob **srcData, struct HksBlob **macData, + const struct HksTestBlobParams *srcDataParams, const struct HksTestBlobParams *macDataParams) +{ + int32_t ret = TestConstuctBlob(srcData, + srcDataParams->blobExist, + srcDataParams->blobSize, + srcDataParams->blobDataExist, + srcDataParams->blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + ret = TestConstuctBlob(macData, + macDataParams->blobExist, + macDataParams->blobSize, + macDataParams->blobDataExist, + macDataParams->blobDataSize); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +static int32_t Mac(const struct HksBlob *key, const struct HksBlob *srcData, struct HksBlob *macData, + const struct HksTestMacParamSet *macParamSetParams, enum HksTestMacType macType) +{ + struct HksParamSet *macParamSet = NULL; + int32_t ret; + if (macType == HKS_TEST_MAC_TYPE_REE) { + struct TestMacParamSetStructure paramStructTrue = { + &macParamSet, + macParamSetParams->paramSetExist, + macParamSetParams->setPurpose, macParamSetParams->purpose, + macParamSetParams->setDigest, macParamSetParams->digest, true, false + }; + ret = TestConstructMacParamSet(¶mStructTrue); + } else { + struct TestMacParamSetStructure paramStructFalse = { + &macParamSet, + macParamSetParams->paramSetExist, + macParamSetParams->setPurpose, macParamSetParams->purpose, + macParamSetParams->setDigest, macParamSetParams->digest, false, false + }; + ret = TestConstructMacParamSet(¶mStructFalse); + } + HKS_TEST_ASSERT(ret == 0); + + ret = HksMacRun(key, macParamSet, srcData, macData, 1); + HksFreeParamSet(&macParamSet); + return ret; +} + +static int32_t BaseTestMac(uint32_t index) +{ + /* 1. generate key */ + struct HksBlob *key = NULL; + int32_t ret; + + if (g_testMacParams[index].macType == HKS_TEST_MAC_TYPE_REE) { + ret = TestConstuctBlob(&key, + g_testMacParams[index].keyParams.blobExist, + g_testMacParams[index].keyParams.blobSize, + g_testMacParams[index].keyParams.blobDataExist, + g_testMacParams[index].keyParams.blobDataSize); + } else { + if (g_testMacParams[index].keyAliasParams.blobExist) { + ret = GenerateKey(&key, &(g_testMacParams[index].keyAliasParams), + &g_testMacParams[index].genKeyParamSetParams, NULL); + } else { + ret = TestConstuctBlob(&key, + g_testMacParams[index].keyParams.blobExist, + g_testMacParams[index].keyParams.blobSize, + g_testMacParams[index].keyParams.blobDataExist, + g_testMacParams[index].keyParams.blobDataSize); + } + } + HKS_TEST_ASSERT(ret == 0); + + /* 2. mac */ + struct HksBlob *srcData = NULL; + struct HksBlob *macData = NULL; + ret = ConstructDataToBlob(&srcData, &macData, + &g_testMacParams[index].srcDataParams, &g_testMacParams[index].macParams); + HKS_TEST_ASSERT(ret == 0); + + ret = Mac(key, srcData, macData, &g_testMacParams[index].macParamSetParams, g_testMacParams[index].macType); + if (ret != g_testMacParams[index].expectResult) { + HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testMacParams[index].testId, ret); + } + HKS_TEST_ASSERT(ret == g_testMacParams[index].expectResult); + + /* 3. deletekey */ + if ((g_testMacParams[index].macType == HKS_TEST_MAC_TYPE_TEE) && + (g_testMacParams[index].keyAliasParams.blobExist)) { + ret = HksDeleteKey(key, NULL); + HKS_TEST_ASSERT(ret == 0); + } + TestFreeBlob(&key); + TestFreeBlob(&srcData); + TestFreeBlob(&macData); + return ret; +} + +/** + * @tc.name: HksMacTest.HksMacTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksMacTest, HksMacTest001, TestSize.Level1) +{ + int32_t ret = BaseTestMac(0); + ASSERT_TRUE(ret == 0); +} + +#ifndef _CUT_AUTHENTICATE_ +/** + * @tc.name: HksMacTest.HksMacTest002 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksMacTest, HksMacTest002, TestSize.Level1) +{ + int32_t ret = BaseTestMac(1); + ASSERT_TRUE(ret == 0); +} +#endif /* _CUT_AUTHENTICATE_ */ +} diff --git a/security_lite/huks/liteos_a_adapter/hks_modify_key_test.cpp b/security_lite/huks/liteos_a_adapter/hks_modify_key_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b09995458a65273153e54719cb1b3ab1805b17a --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_modify_key_test.cpp @@ -0,0 +1,357 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_modify_key_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_cipher.h" +#include "hks_test_common.h" +#include "hks_test_file_operator.h" +#include "hks_test_log.h" +#include "hks_test_mem.h" + +#include "securec.h" + +using namespace testing::ext; +namespace { +#ifndef _CUT_AUTHENTICATE_ + +class HksModifyKeyTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksModifyKeyTest::SetUpTestCase(void) +{ +} + +void HksModifyKeyTest::TearDownTestCase(void) +{ +} + +void HksModifyKeyTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksModifyKeyTest::TearDown() +{ +} + +const int DEFAULT_AES_CIPHER_PLAIN_SIZE = 1000; +const int AES_DEFAULT_GCM_NONCE_LENGTH = 12; +const int AES_DEFAULT_AAD_LEN = 4; +const char *g_storePath = "/storage/maindata/hks_client/key"; +const char *g_testName = "TestName"; + +const struct HksTestCipherParams g_testCipherParams[] = { + /* success: aes256-gcm-none */ + { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0 + }, + { false, 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* encrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_ENCRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN + }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* decrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN + }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16 }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { false, 0, false, 0 } + }, +}; + +static int32_t ConstructDataToBlob(struct HksBlob **inData, struct HksBlob **outData, + const struct HksTestBlobParams *inTextParams, const struct HksTestBlobParams *outTextParams) +{ + int32_t ret = TestConstuctBlob(inData, + inTextParams->blobExist, + inTextParams->blobSize, + inTextParams->blobDataExist, + inTextParams->blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + ret = TestConstuctBlob(outData, + outTextParams->blobExist, + outTextParams->blobSize, + outTextParams->blobDataExist, + outTextParams->blobDataSize); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +static int32_t Encrypt(struct CipherEncryptStructure *encryptStruct) +{ + int32_t ret; + struct HksParamSet *encryptParamSet = NULL; + + uint32_t ivSize = encryptStruct->cipherParms->ivSize; + uint32_t nonceSize = encryptStruct->cipherParms->nonceSize; + uint32_t aadSize = encryptStruct->cipherParms->aadSize; + if (ivSize != 0) { + ret = TestConstuctBlob(encryptStruct->ivData, true, ivSize, true, ivSize); + HKS_TEST_ASSERT(ret == 0); + } + if (nonceSize != 0) { + ret = TestConstuctBlob(encryptStruct->nonceData, true, nonceSize, true, nonceSize); + HKS_TEST_ASSERT(ret == 0); + } + if (aadSize != 0) { + ret = TestConstuctBlob(encryptStruct->aadData, true, aadSize, true, aadSize); + HKS_TEST_ASSERT(ret == 0); + } + struct AesCipherParamSetStructure enParamStruct = { + &encryptParamSet, + encryptStruct->cipherParms->paramSetExist, + encryptStruct->cipherParms->setAlg, encryptStruct->cipherParms->alg, + encryptStruct->cipherParms->setPurpose, encryptStruct->cipherParms->purpose, + encryptStruct->cipherParms->setPadding, encryptStruct->cipherParms->padding, + encryptStruct->cipherParms->setBlockMode, encryptStruct->cipherParms->mode, + encryptStruct->cipherParms->setIv, *(encryptStruct->ivData), + encryptStruct->cipherParms->setNonce, *(encryptStruct->nonceData), + encryptStruct->cipherParms->setAad, *(encryptStruct->aadData), + encryptStruct->cipherParms->setIsKeyAlias, encryptStruct->cipherParms->isKeyAlias + }; + ret = TestConstructAesCipherParamSet(&enParamStruct); + HKS_TEST_ASSERT(ret == 0); + + ret = HksEncryptRun(encryptStruct->keyAlias, encryptParamSet, encryptStruct->plainData, encryptStruct->cipherData, + encryptStruct->performTimes); + HksFreeParamSet(&encryptParamSet); + return ret; +} + +static int32_t DecryptCipher(struct CipherDecryptStructure *decryptStruct) +{ + int32_t ret = TestConstuctBlob(decryptStruct->decryptedData, + decryptStruct->cipherParms->decryptedTextParams.blobExist, + decryptStruct->cipherParms->decryptedTextParams.blobSize, + decryptStruct->cipherParms->decryptedTextParams.blobDataExist, + decryptStruct->cipherParms->decryptedTextParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *decryptParamSet = NULL; + struct AesCipherParamSetStructure deParamStruct = { + &decryptParamSet, + decryptStruct->cipherParms->decryptParamSetParams.paramSetExist, + decryptStruct->cipherParms->decryptParamSetParams.setAlg, + decryptStruct->cipherParms->decryptParamSetParams.alg, + decryptStruct->cipherParms->decryptParamSetParams.setPurpose, + decryptStruct->cipherParms->decryptParamSetParams.purpose, + decryptStruct->cipherParms->decryptParamSetParams.setPadding, + decryptStruct->cipherParms->decryptParamSetParams.padding, + decryptStruct->cipherParms->decryptParamSetParams.setBlockMode, + decryptStruct->cipherParms->decryptParamSetParams.mode, + decryptStruct->cipherParms->decryptParamSetParams.setIv, decryptStruct->ivData, + decryptStruct->cipherParms->decryptParamSetParams.setNonce, decryptStruct->nonceData, + decryptStruct->cipherParms->decryptParamSetParams.setAad, decryptStruct->aadData, + decryptStruct->cipherParms->decryptParamSetParams.setIsKeyAlias, + decryptStruct->cipherParms->decryptParamSetParams.isKeyAlias + }; + ret = TestConstructAesCipherParamSet(&deParamStruct); + HKS_TEST_ASSERT(ret == 0); + + ret = HksDecryptRun(decryptStruct->keyAlias, decryptParamSet, decryptStruct->cipherData, + *(decryptStruct->decryptedData), decryptStruct->performTimes); + HksFreeParamSet(&decryptParamSet); + return ret; +} + +int32_t GenerateKeyTwo(struct HksBlob *keyAlias, const struct HksTestBlobParams *keyAliasParams, + const struct HksTestGenKeyParamsParamSet *genKeyParamSetParams, + const struct HksTestGenKeyParamsParamSetOut *genKeyParamSetParamsOut) +{ + struct HksParamSet *paramSet = NULL; + struct GenerateKeyParamSetStructure paramStruct = { + ¶mSet, + genKeyParamSetParams->paramSetExist, + genKeyParamSetParams->setAlg, genKeyParamSetParams->alg, + genKeyParamSetParams->setKeySize, genKeyParamSetParams->keySize, + genKeyParamSetParams->setPurpose, genKeyParamSetParams->purpose, + genKeyParamSetParams->setDigest, genKeyParamSetParams->digest, + genKeyParamSetParams->setPadding, genKeyParamSetParams->padding, + genKeyParamSetParams->setBlockMode, genKeyParamSetParams->mode, + genKeyParamSetParams->setKeyStorageFlag, genKeyParamSetParams->keyStorageFlag + }; + int32_t ret = TestConstructGenerateKeyParamSet(¶mStruct); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *paramSetOut = NULL; + if (genKeyParamSetParamsOut != NULL) { + ret = TestConstructGenerateKeyParamSetOut(¶mSet, + genKeyParamSetParamsOut->paramSetExist, genKeyParamSetParamsOut->paramSetSize); + HKS_TEST_ASSERT(ret == 0); + } + + ret = HksGenerateKey(keyAlias, paramSet, paramSetOut); + HKS_TEST_ASSERT(ret == 0); + + HksFreeParamSet(¶mSet); + return ret; +} + +static int32_t ModifyKey(struct HksBlob *keyAlias) +{ + uint32_t sizeOne = HksTestFileSize(g_storePath, (char *)keyAlias->data); + uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne); + if (bufOne == NULL) { + return HKS_ERROR_MALLOC_FAIL; + } + uint32_t sizeRead = HksTestFileRead(g_storePath, (char *)keyAlias->data, 0, bufOne, sizeOne); + (void)memset_s(bufOne, sizeRead, 0, sizeRead); + + int32_t ret = HksTestFileWrite(g_storePath, (char *)keyAlias->data, 0, bufOne, sizeOne); + HksTestFree(bufOne); + + return ret; +} + + +int32_t BaseTestCipherProcess(struct HksBlob *keyAlias, uint32_t index) +{ + struct HksBlob *plainData = NULL; + struct HksBlob *cipherData = NULL; + int32_t ret = ConstructDataToBlob(&plainData, &cipherData, + &g_testCipherParams[index].plainTextParams, &g_testCipherParams[index].cipherTextParams); + HKS_TEST_ASSERT(ret == 0); + struct HksBlob *ivData = NULL; + struct HksBlob *nonceData = NULL; + struct HksBlob *aadData = NULL; + struct HksBlob *decryptedData = NULL; + /* 2. encrypt */ + do { + struct CipherEncryptStructure testEncryptStruct = { + keyAlias, &g_testCipherParams[index].encryptParamSetParams, + plainData, cipherData, &ivData, &nonceData, &aadData, 1 + }; + ret = Encrypt(&testEncryptStruct); + if (ret != g_testCipherParams[index].expectResult) { + break; + } + /* 3. decrypt */ + struct CipherDecryptStructure testDecryptStruct = { + keyAlias, &g_testCipherParams[index], cipherData, + &decryptedData, ivData, nonceData, aadData, 1 + }; + ret = DecryptCipher(&testDecryptStruct); + if (ret != g_testCipherParams[index].expectResult) { + break; + } + + if (ret == g_testCipherParams[index].expectResult) { + if (plainData->size != decryptedData->size) { + break; + }; + ret = memcmp(plainData->data, decryptedData->data, plainData->size); + } + } while (0); + TestFreeBlob(&plainData); + TestFreeBlob(&cipherData); + TestFreeBlob(&decryptedData); + TestFreeBlob(&ivData); + TestFreeBlob(&nonceData); + TestFreeBlob(&aadData); + return ret; +} + +/** + * @tc.name: HksModifyKeyTest.HksModifyKeyTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksModifyKeyTest, HksModifyKeyTest001, TestSize.Level1) +{ + uint32_t index = 0; + struct HksBlob keyAlias = { strlen(g_testName), (uint8_t *)g_testName }; + int32_t ret = GenerateKeyTwo(&keyAlias, &g_testCipherParams[index].keyAliasParams, + &g_testCipherParams[index].genKeyParamSetParams, &g_testCipherParams[index].genKeyParamSetParamsOut); + EXPECT_EQ(ret, 0); + + ret = BaseTestCipherProcess(&keyAlias, 0); + EXPECT_EQ(ret, 0); + + struct HksBlob *plainData = NULL; + struct HksBlob *cipherData = NULL; + ret = ConstructDataToBlob(&plainData, &cipherData, + &g_testCipherParams[index].plainTextParams, &g_testCipherParams[index].cipherTextParams); + EXPECT_EQ(ret, 0); + struct HksBlob *ivData = NULL; + struct HksBlob *nonceData = NULL; + struct HksBlob *aadData = NULL; + + /* 2. encrypt */ + struct CipherEncryptStructure testEncryptStruct = { + &keyAlias, &g_testCipherParams[index].encryptParamSetParams, + plainData, cipherData, &ivData, &nonceData, &aadData, 1 + }; + ret = Encrypt(&testEncryptStruct); + EXPECT_EQ(ret, 0); + ret = ModifyKey(&keyAlias); + EXPECT_EQ(ret, 0); + /* 3. decrypt */ + struct HksBlob *decryptedData = NULL; + struct CipherDecryptStructure testDecryptStruct = { + &keyAlias, &g_testCipherParams[index], cipherData, + &decryptedData, ivData, nonceData, aadData, 1 + }; + ret = DecryptCipher(&testDecryptStruct); + + HKS_TEST_ASSERT(ret != g_testCipherParams[index].expectResult); + TestFreeBlob(&plainData); + TestFreeBlob(&cipherData); + TestFreeBlob(&decryptedData); + TestFreeBlob(&ivData); + TestFreeBlob(&nonceData); + TestFreeBlob(&aadData); + EXPECT_NE(ret, 0); +} +#endif /* _CUT_AUTHENTICATE_ */ +} diff --git a/security_lite/huks/liteos_a_adapter/hks_others_test.cpp b/security_lite/huks/liteos_a_adapter/hks_others_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..359c21681029e888557bf19c04d5586fad15caa7 --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_others_test.cpp @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_others_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_type.h" + +using namespace testing::ext; +namespace { +#ifndef _CUT_AUTHENTICATE_ +class HksOthersTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksOthersTest::SetUpTestCase(void) +{ +} + +void HksOthersTest::TearDownTestCase(void) +{ +} + +void HksOthersTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksOthersTest::TearDown() +{ +} + +/** + * @tc.name: HksOthersTest.HksOthersTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksOthersTest, HksOthersTest001, TestSize.Level1) +{ + int32_t ret = HksGetKeyParamSet(NULL, NULL, NULL); + ASSERT_TRUE(ret == HKS_ERROR_NULL_POINTER); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest002 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksOthersTest, HksOthersTest002, TestSize.Level1) +{ + int32_t ret = HksGetKeyInfoList(NULL, NULL, NULL); + ASSERT_TRUE(ret == HKS_ERROR_NULL_POINTER); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest003 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksOthersTest, HksOthersTest003, TestSize.Level1) +{ + int32_t ret = HksAttestKey(NULL, NULL, NULL); + ASSERT_TRUE(ret == HKS_ERROR_NOT_SUPPORTED); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest004 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksOthersTest, HksOthersTest004, TestSize.Level1) +{ + int32_t ret = HksGetCertificateChain(NULL, NULL, NULL); + ASSERT_TRUE(ret == HKS_ERROR_NOT_SUPPORTED); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest005 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksOthersTest, HksOthersTest005, TestSize.Level1) +{ + int32_t ret = HksWrapKey(NULL, NULL, NULL, NULL); + ASSERT_TRUE(ret == HKS_ERROR_NOT_SUPPORTED); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest006 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksOthersTest, HksOthersTest006, TestSize.Level1) +{ + int32_t ret = HksUnwrapKey(NULL, NULL, NULL, NULL); + ASSERT_TRUE(ret == HKS_ERROR_NOT_SUPPORTED); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest006 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksOthersTest, HksOthersTest007, TestSize.Level1) +{ + int32_t ret = HksGetSdkVersion(NULL); + ASSERT_TRUE(ret == HKS_ERROR_NULL_POINTER); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest008 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksOthersTest, HksOthersTest008, TestSize.Level1) +{ + int32_t ret = HksInitialize(); + ASSERT_TRUE(ret == HKS_SUCCESS); +} +#endif /* _CUT_AUTHENTICATE_ */ +} \ No newline at end of file diff --git a/security_lite/huks/liteos_a_adapter/hks_safe_cipher_key_test.cpp b/security_lite/huks/liteos_a_adapter/hks_safe_cipher_key_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b670ac89ee9c57515aa179d44a0e9efec230f5ec --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_safe_cipher_key_test.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_safe_cipher_key_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_curve25519.h" +#include "hks_test_file_operator.h" +#include "hks_test_mem.h" + +using namespace testing::ext; +namespace { +#ifndef _CUT_AUTHENTICATE_ +class HksSafeCipherKeyTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksSafeCipherKeyTest::SetUpTestCase(void) +{ +} + +void HksSafeCipherKeyTest::TearDownTestCase(void) +{ +} + +void HksSafeCipherKeyTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksSafeCipherKeyTest::TearDown() +{ +} + +const char *g_storePath = "/storage/maindata/hks_client/key"; +const char *g_testEd25519 = "test_ed25519"; + +static int32_t CompareTwoKey(const struct HksBlob *keyAliasOne, const struct HksBlob *keyAliasTwo) +{ + uint32_t sizeOne = HksTestFileSize(g_storePath, (char *)keyAliasOne->data); + uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne); + if (bufOne == NULL) { + return HKS_ERROR_MALLOC_FAIL; + } + uint32_t sizeRead = HksTestFileRead(g_storePath, (char *)keyAliasOne->data, 0, bufOne, sizeOne); + + uint32_t sizeTwo = HksTestFileSize(g_storePath, (char *)keyAliasTwo->data); + uint8_t *bufTwo = (uint8_t *)HksTestMalloc(sizeTwo); + if (bufTwo == NULL) { + HksTestFree(bufOne); + return HKS_ERROR_MALLOC_FAIL; + } + sizeRead = HksTestFileRead(g_storePath, (char *)keyAliasTwo->data, 0, bufTwo, sizeOne); + int32_t ret = memcmp(bufOne, bufTwo, sizeRead); + HksTestFree(bufOne); + HksTestFree(bufTwo); + return ret; +} + +/** + * @tc.name: HksSafeCipherKeyTest.HksSafeCipherKeyTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksSafeCipherKeyTest, HksSafeCipherKeyTest001, TestSize.Level1) +{ + struct HksBlob ed25519Alias = { strlen(g_testEd25519), (uint8_t *)g_testEd25519 }; + int32_t ret = TestGenerateEd25519Key(ed25519Alias); + uint8_t pubKey[32] = {0}; + uint32_t pubKeyLen = 32; + struct HksBlob pubKeyInfo = { pubKeyLen, pubKey }; + ret = HksExportPublicKey(&ed25519Alias, NULL, &pubKeyInfo); + EXPECT_EQ(ret, 0); + ret = HksDeleteKey(&ed25519Alias, NULL); + EXPECT_EQ(ret, 0); + + struct HksBlob newAliasOne = { strlen("test_ed25519_1"), (uint8_t *)"test_ed25519_1" }; + ret = TestImportEd25519(newAliasOne, &pubKeyInfo); + EXPECT_EQ(ret, 0); + + struct HksBlob newAliasTwo = { strlen("test_ed25519_2"), (uint8_t *)"test_ed25519_2" }; + ret = TestImportEd25519(newAliasTwo, &pubKeyInfo); + EXPECT_EQ(ret, 0); + + ret = CompareTwoKey(&newAliasOne, &newAliasTwo); + EXPECT_NE(ret, 0); + + ret = HksDeleteKey(&newAliasOne, NULL); + EXPECT_EQ(ret, 0); + ret = HksDeleteKey(&newAliasTwo, NULL); + EXPECT_EQ(ret, 0); +} +#endif /* _CUT_AUTHENTICATE_ */ +} \ No newline at end of file diff --git a/security_lite/huks/liteos_a_adapter/hks_safe_compare_key_test.cpp b/security_lite/huks/liteos_a_adapter/hks_safe_compare_key_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b03096058ace43b8cdd07afa5f2269fa381a6d49 --- /dev/null +++ b/security_lite/huks/liteos_a_adapter/hks_safe_compare_key_test.cpp @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2021 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 + +#include "hks_safe_compare_key_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_file_operator.h" +#include "hks_test_log.h" +#include "hks_test_mem.h" + +using namespace testing::ext; +namespace { +#ifndef _CUT_AUTHENTICATE_ +class HksSafeCompareKeyTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void HksSafeCompareKeyTest::SetUpTestCase(void) +{ +} + +void HksSafeCompareKeyTest::TearDownTestCase(void) +{ +} + +void HksSafeCompareKeyTest::SetUp() +{ + EXPECT_EQ(HksInitialize(), 0); +} + +void HksSafeCompareKeyTest::TearDown() +{ +} + +const char *g_storePath = "/storage/maindata/hks_client/key/"; +const char *g_testOne = "TestOne"; +const char *g_testTwo = "TestTwo"; + +const struct HksTestGenKeyParams g_testGenKeyParams[] = { + /* x25519: tee sign/verify */ + { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, + true, HKS_ALG_X25519, + true, HKS_CURVE25519_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY, + false, 0, + false, 0, + false, 0, + false, 0 + }, + { false, 0 }, + }, +}; + +static int32_t SafeTestGenerateKey(struct HksBlob *keyAlias) +{ + uint32_t index = 0; + uint32_t performTimes = 1; + + struct HksParamSet *paramSet = NULL; + struct GenerateKeyParamSetStructure paramStruct = { + ¶mSet, + g_testGenKeyParams[index].paramSetParams.paramSetExist, + g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg, + g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize, + g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose, + g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest, + g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding, + g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode, + g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag, + g_testGenKeyParams[index].paramSetParams.keyStorageFlag + }; + int32_t ret = TestConstructGenerateKeyParamSet(¶mStruct); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *paramSetOut = NULL; + ret = TestConstructGenerateKeyParamSetOut(¶mSetOut, + g_testGenKeyParams[index].paramSetParamsOut.paramSetExist, + g_testGenKeyParams[index].paramSetParamsOut.paramSetSize); + HKS_TEST_ASSERT(ret == 0); + + ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes); + if (ret != g_testGenKeyParams[index].expectResult) { + HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret); + } + EXPECT_EQ(ret, g_testGenKeyParams[index].expectResult); + + if (ret == g_testGenKeyParams[index].expectResult) { + ret = 0; + } else { + ret = 1; + } + HksFreeParamSet(¶mSet); + HksFreeParamSet(¶mSetOut); + return ret; +} + +static int32_t CompareKeyData(struct HksBlob *keyAliasOne, struct HksBlob *keyAliasTwo) +{ + uint32_t sizeOne = HksTestFileSize(g_storePath, (char *)keyAliasOne->data); + uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne); + if (bufOne == NULL) { + return HKS_ERROR_MALLOC_FAIL; + } + uint32_t sizeRead = HksTestFileRead(g_storePath, (char *)keyAliasOne->data, 0, bufOne, sizeOne); + + uint32_t sizeTwo = HksTestFileSize(g_storePath, (char *)keyAliasTwo->data); + uint8_t *bufTwo = (uint8_t *)HksTestMalloc(sizeTwo); + if (bufTwo == NULL) { + HksTestFree(bufOne); + return HKS_ERROR_MALLOC_FAIL; + } + sizeRead = HksTestFileRead(g_storePath, (char *)keyAliasTwo->data, 0, bufTwo, sizeOne); + int32_t ret = memcmp(bufOne, bufTwo, sizeOne); + HksTestFree(bufOne); + HksTestFree(bufTwo); + return ret; +} + +/** + * @tc.name: HksSafeCompareKeyTest.HksSafeCompareKeyTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +HWTEST_F(HksSafeCompareKeyTest, HksSafeCompareKeyTest001, TestSize.Level1) +{ + struct HksBlob keyAliasOne = { strlen(g_testOne), (uint8_t *)g_testOne }; + int32_t ret = SafeTestGenerateKey(&keyAliasOne); + EXPECT_EQ(ret, 0); + + struct HksBlob keyAliasTwo = { strlen(g_testTwo), (uint8_t *)g_testTwo }; + ret = SafeTestGenerateKey(&keyAliasTwo); + EXPECT_EQ(ret, 0); + + ret = CompareKeyData(&keyAliasOne, &keyAliasTwo); + EXPECT_NE(ret, 0); +} +#endif /* _CUT_AUTHENTICATE_ */ +} \ No newline at end of file diff --git a/security_lite/huks/liteos_m_adapter/BUILD.gn b/security_lite/huks/liteos_m_adapter/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e2ad7835ae27c86a26fbdc62f83856dde5788387 --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/BUILD.gn @@ -0,0 +1,56 @@ +# +# Copyright (c) 2021 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. +# + +import("//base/security/huks/build/config.gni") +import("//test/xts/tools/lite/build/suite_lite.gni") + +hctest_suite("ActsHuksHalFunctionTest") { + suite_name = "acts" + sources = [ + "hks_aes_test.c", + "hks_agreement_test.c", + "hks_bn_exp_mod_test.c", + "hks_cipher_test.c", + "hks_curve25519_test.c", + "hks_delete_test.c", + "hks_derive_test.c", + "hks_exist_test.c", + "hks_generate_key_test.c", + "hks_generate_random_test.c", + "hks_hash_test.c", + "hks_mac_test.c", + "hks_modify_key_test.c", + "hks_others_test.c", + "hks_safe_cipher_key_test.c", + "hks_safe_compare_key_test.c", + ] + include_dirs = [ + "//base/security/huks/interfaces/innerkits/huks_standard/main/include", + "../common/include", + ] + + deps = [ + #"//base/security/huks/huawei_property/source:huks_new", + "//test/xts/acts/security_lite/huks/common:huks_new_test_common", + ] + + cflags = [ + "-D_CUT_ED25519_", + "-D_CUT_LOG_", + ] + if (disable_authenticate == true) { + cflags += [ "-D_CUT_AUTHENTICATE_" ] + } +} diff --git a/security_lite/huks/liteos_m_adapter/Test.tmpl b/security_lite/huks/liteos_m_adapter/Test.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..be8407e1286041a5af2bdc75a15633337376eb9d --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/Test.tmpl @@ -0,0 +1,19 @@ +{ + "description": "Config for $module test cases", + "environment": [ + { + "type": "device", + "label": "wifiiot" + } + ], + "kits": [ + { + "type": "DeployKit", + "timeout": "20000", + "burn_file": "$subsystem/$module.bin" + } + ], + "driver": { + "type": "CTestLite" + } +} \ No newline at end of file diff --git a/security_lite/huks/liteos_m_adapter/hks_aes_test.c b/security_lite/huks/liteos_m_adapter/hks_aes_test.c new file mode 100644 index 0000000000000000000000000000000000000000..8fb7a69df7aee02a11f904f56bfd048bd11bb6f5 --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_aes_test.c @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2021 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 "hctest.h" +#include +#include "hks_api.h" +#include "hks_param.h" +#include "securec.h" + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksAesTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksAesTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksAesTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + +#ifndef _CUT_AUTHENTICATE_ +#ifndef _CUT_ED25519_ +/** + * @tc.name: HksCipherTest.HksCipherTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksAesTest, HksAesTest001, Level1) +{ + TEST_ASSERT_TRUE(TestAes256ByAgree() == 0); +} + +/** + * @tc.name: HksCipherTest.HksCipherTest002 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksAesTest, HksAesTest002, Level1) +{ + TEST_ASSERT_TRUE(TestAes256ByAgree1() == 0); +} + +/** + * @tc.name: HksCipherTest.HksCipherTest003 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksAesTest, HksAesTest003, Level1) +{ + TEST_ASSERT_TRUE(TestAes256ByAgree2() == 0); +} +#endif +#endif /* _CUT_AUTHENTICATE_ */ + +/** + * @tc.name: HksCipherTest.HksCipherTest004 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksAesTest, HksAesTest004, Level1) +{ + TEST_ASSERT_EQUAL(TestAes256ByLocal(), 0); +} + +RUN_TEST_SUITE(HksAesTest); diff --git a/security_lite/huks/liteos_m_adapter/hks_agreement_test.c b/security_lite/huks/liteos_m_adapter/hks_agreement_test.c new file mode 100644 index 0000000000000000000000000000000000000000..087d649cb6571e72c666b6f277bde4e293f4742b --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_agreement_test.c @@ -0,0 +1,216 @@ +/* + * Copyright (C) 2021 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 "hctest.h" +#include "hi_watchdog.h" +#include "hks_agreement_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "hks_type.h" + +#ifndef _CUT_AUTHENTICATE_ + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksAgreementTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksAgreementTestSetUp() +{ + LiteTestPrint("setup\n"); + + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksAgreementTestTearDown() +{ + LiteTestPrint("tearDown\n"); + + hi_watchdog_enable(); + return TRUE; +} + +#define TMP_SIZE 512 +#define X25519_KEY_SIZE 32 +static const struct HksTestAgreeParams g_testAgreeParams[] = { + /* ree x25519 success */ + { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { true, true, HKS_ALG_X25519, true, HKS_CURVE25519_KEY_SIZE_256, true, + HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY, true, HKS_DIGEST_SHA256, + false, 0, false, 0, true, HKS_STORAGE_TEMP }, + { 0 }, + { true, true, HKS_ALG_X25519, true, HKS_CURVE25519_KEY_SIZE_256, true, false }, + { true, TMP_SIZE, true, TMP_SIZE }, + { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE }, + { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE }, + { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE } + }, +}; + +static int32_t AgreeKey(const struct HksTestAgreeParamSet *agreeParamSetParams, struct HksBlob *privateKey, + struct HksBlob *peerPublicKey, struct HksBlob *agreedKey) +{ + struct HksParamSet *agreeParamSet = NULL; + struct TestAgreeParamSetStructure paramStruct = { + &agreeParamSet, + agreeParamSetParams->paramSetExist, + agreeParamSetParams->setAlg, agreeParamSetParams->alg, + agreeParamSetParams->setKeySize, agreeParamSetParams->keySize, + agreeParamSetParams->setIsKeyAlias, agreeParamSetParams->isKeyAlias + }; + int32_t ret = TestConstructAgreeParamSet(¶mStruct); + HKS_TEST_ASSERT(ret == 0); + + ret = HksAgreeKeyRun(agreeParamSet, privateKey, peerPublicKey, agreedKey, 1); + HksFreeParamSet(&agreeParamSet); + return ret; +} + +/** + * @tc.name: HksDeleteTest.HksDeleteTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksAgreementTest, HksAgreementTest001, Level1) +{ + /* 1. generate key */ + struct HksBlob *privateKey = NULL; + struct HksBlob *peerPubKeyAlias = NULL; + struct HksBlob *peerPublicKey = NULL; + int32_t ret; + + if (g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag && + (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) { + ret = GenerateLocalX25519Key(&privateKey, NULL, &g_testAgreeParams[0].localPrivateKeyParams, NULL); + HKS_TEST_ASSERT(ret == 0); + ret = GenerateLocalX25519Key(NULL, &peerPublicKey, NULL, &g_testAgreeParams[0].localPublicKeyParams); + HKS_TEST_ASSERT(ret == 0); + } + /* 2. agreeKey */ + struct HksBlob *agreeKey = NULL; + ret = TestConstuctBlob(&agreeKey, + g_testAgreeParams[0].agreedKeyParams.blobExist, + g_testAgreeParams[0].agreedKeyParams.blobSize, + g_testAgreeParams[0].agreedKeyParams.blobDataExist, + g_testAgreeParams[0].agreedKeyParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey, peerPublicKey, agreeKey); + HKS_TEST_ASSERT(ret == g_testAgreeParams[0].expectResult); + + /* 3. delete key */ + if (!(g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag && + (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) && + ((g_testAgreeParams[0].keyAlias1Params.blobExist) && + (g_testAgreeParams[0].keyAlias2Params.blobExist))) { + ret = HksDeleteKey(privateKey, NULL); + HKS_TEST_ASSERT(ret == 0); + ret = HksDeleteKey(peerPubKeyAlias, NULL); + HKS_TEST_ASSERT(ret == 0); + } + TestFreeBlob(&privateKey); + TestFreeBlob(&peerPubKeyAlias); + TestFreeBlob(&peerPublicKey); + TestFreeBlob(&agreeKey); + TEST_ASSERT_TRUE(ret == 0); +} + + +/** + * @tc.name: HksDeleteTest.HksDeleteTest002 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksAgreementTest, HksAgreementTest002, Level1) +{ + /* 1. generate key */ + struct HksBlob *privateKey = NULL; + struct HksBlob *peerPubKeyAlias = NULL; + struct HksBlob *peerPublicKey = NULL; + struct HksBlob *privateKey1 = NULL; + struct HksBlob *peerPubKeyAlias1 = NULL; + struct HksBlob *peerPublicKey1 = NULL; + int32_t ret; + + if (g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag && + (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) { + ret = GenerateLocalX25519Key(&privateKey, &peerPublicKey, &g_testAgreeParams[0].localPrivateKeyParams, \ + &g_testAgreeParams[0].localPublicKeyParams); + TEST_ASSERT_TRUE(ret == 0); + ret = GenerateLocalX25519Key(&privateKey1, &peerPublicKey1, &g_testAgreeParams[0].localPrivateKeyParams, \ + &g_testAgreeParams[0].localPublicKeyParams); + TEST_ASSERT_TRUE(ret == 0); + } + /* 2. agreeKey */ + struct HksBlob *agreeKey = NULL; + ret = TestConstuctBlob(&agreeKey, g_testAgreeParams[0].agreedKeyParams.blobExist, \ + g_testAgreeParams[0].agreedKeyParams.blobSize, g_testAgreeParams[0].agreedKeyParams.blobDataExist, \ + g_testAgreeParams[0].agreedKeyParams.blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + + ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey, peerPublicKey1, agreeKey); + TEST_ASSERT_TRUE(ret == g_testAgreeParams[0].expectResult); + + struct HksBlob *agreeKey1 = NULL; + ret = TestConstuctBlob(&agreeKey1, g_testAgreeParams[0].agreedKeyParams.blobExist, \ + g_testAgreeParams[0].agreedKeyParams.blobSize, g_testAgreeParams[0].agreedKeyParams.blobDataExist, \ + g_testAgreeParams[0].agreedKeyParams.blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + + ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey1, peerPublicKey, agreeKey1); + TEST_ASSERT_TRUE(ret == g_testAgreeParams[0].expectResult); + TEST_ASSERT_EQUAL(agreeKey->size, agreeKey1->size); + TEST_ASSERT_TRUE(memcmp(agreeKey->data, agreeKey1->data, agreeKey->size) == 0); + + /* 3. delete key */ + if (!(g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag && + (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) && + ((g_testAgreeParams[0].keyAlias1Params.blobExist) && (g_testAgreeParams[0].keyAlias2Params.blobExist))) { + TEST_ASSERT_TRUE(HksDeleteKey(privateKey, NULL) == 0); + TEST_ASSERT_TRUE(HksDeleteKey(peerPubKeyAlias, NULL) == 0); + TEST_ASSERT_TRUE(HksDeleteKey(privateKey1, NULL) == 0); + TEST_ASSERT_TRUE(HksDeleteKey(peerPubKeyAlias1, NULL) == 0); + } + TestFreeBlob(&privateKey); + TestFreeBlob(&peerPubKeyAlias); + TestFreeBlob(&peerPublicKey); + TestFreeBlob(&agreeKey); + TestFreeBlob(&privateKey1); + TestFreeBlob(&peerPubKeyAlias1); + TestFreeBlob(&peerPublicKey1); + TestFreeBlob(&agreeKey1); +} + +RUN_TEST_SUITE(HksAgreementTest); +#endif /* _CUT_AUTHENTICATE_ */ + diff --git a/security_lite/huks/liteos_m_adapter/hks_bn_exp_mod_test.c b/security_lite/huks/liteos_m_adapter/hks_bn_exp_mod_test.c new file mode 100644 index 0000000000000000000000000000000000000000..6c5c0fd92832e41b8e6326713dbd57e46fbc681c --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_bn_exp_mod_test.c @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2021 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 "hctest.h" +#include "hi_watchdog.h" +#include "hks_bn_exp_mod_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "hks_type.h" + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksBnExpModTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksBnExpModTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksBnExpModTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + + +#define DEFAULT_X_SIZE 256 +#define DEFAULT_A_SIZE 256 +#define DEFAULT_E_SIZE 256 +#define DEFAULT_N_SIZE 256 +#define HKS_TEST_2 2 +#define HKS_TEST_8 8 + +static const struct HksTestBnExpModParams g_testBnExpModParams[] = { + /* normal case */ + { 0, HKS_SUCCESS, false, + { true, DEFAULT_X_SIZE, true, DEFAULT_X_SIZE }, + { true, DEFAULT_A_SIZE, true, DEFAULT_A_SIZE }, + { true, DEFAULT_E_SIZE, true, DEFAULT_E_SIZE }, + { true, DEFAULT_N_SIZE, true, DEFAULT_N_SIZE } + }, +}; + +static int32_t TestValue() +{ + HKS_TEST_LOG_I("test value"); + uint8_t bufX[HKS_TEST_8] = { 0, 0, 0, 0, 0, 0, 0, 0x40 }; + uint8_t bufA[HKS_TEST_8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + uint8_t bufE[HKS_TEST_2] = { 0, 2 }; + uint8_t bufN[HKS_TEST_8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + uint8_t tmpBufX[HKS_TEST_8] = {0}; + struct HksBlob tmpX = { HKS_TEST_8, tmpBufX }; + struct HksBlob tmpA = { HKS_TEST_8, bufA }; + struct HksBlob tmpE = { HKS_TEST_2, bufE }; + struct HksBlob tmpN = { HKS_TEST_8, bufN }; + int32_t ret = HksBnExpModRun(&tmpX, &tmpA, &tmpE, &tmpN, 1); + for (int i = 0; i < HKS_TEST_8; ++i) { + HKS_TEST_LOG_I("%x, %x", tmpBufX[i], bufX[i]); + HKS_TEST_ASSERT(tmpBufX[i] == bufX[i]); + } + return ret; +} + +/** + * @tc.name: HksBnExpModTest.HksBnExpModTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksBnExpModTest, HksBnExpModTest001, Level1) +{ + int32_t ret; + if (g_testBnExpModParams[0].isTestValue) { + ret = TestValue(); + TEST_ASSERT_TRUE(ret == 0); + } else { + struct HksBlob *x = NULL; + struct HksBlob *a = NULL; + struct HksBlob *e = NULL; + struct HksBlob *n = NULL; + ret = TestConstuctBlob(&x, g_testBnExpModParams[0].xParams.blobExist, + g_testBnExpModParams[0].xParams.blobSize, g_testBnExpModParams[0].xParams.blobDataExist, + g_testBnExpModParams[0].xParams.blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + + ret = TestConstructBlobOut(&a, g_testBnExpModParams[0].aParams.blobExist, + g_testBnExpModParams[0].aParams.blobSize, g_testBnExpModParams[0].aParams.blobDataExist, + g_testBnExpModParams[0].aParams.blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + + ret = TestConstuctBlob(&e, g_testBnExpModParams[0].eParams.blobExist, + g_testBnExpModParams[0].eParams.blobSize, g_testBnExpModParams[0].eParams.blobDataExist, + g_testBnExpModParams[0].eParams.blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + + ret = TestConstuctBlob(&n, g_testBnExpModParams[0].nParams.blobExist, + g_testBnExpModParams[0].nParams.blobSize, g_testBnExpModParams[0].nParams.blobDataExist, + g_testBnExpModParams[0].nParams.blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + if ((n != NULL) && (n->data != NULL) && (n->size != 0)) { + n->data[n->size - 1] = n->data[n->size - 1] | 0x00000001; /* make sure n is odd */ + } + + ret = HksBnExpModRun(x, a, e, n, 1); + + TEST_ASSERT_TRUE(ret == g_testBnExpModParams[0].expectResult); + + TestFreeBlob(&x); + TestFreeBlob(&a); + TestFreeBlob(&e); + TestFreeBlob(&n); + TEST_ASSERT_TRUE(ret == 0); + } +} + +RUN_TEST_SUITE(HksBnExpModTest); diff --git a/security_lite/huks/liteos_m_adapter/hks_cipher_test.c b/security_lite/huks/liteos_m_adapter/hks_cipher_test.c new file mode 100644 index 0000000000000000000000000000000000000000..f52ef87af2e1df7415a3f38511876b8ebf9453d4 --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_cipher_test.c @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2021 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 "hks_cipher_test.h" + +#include "hctest.h" +#include "hi_watchdog.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_cipher.h" +#include "securec.h" + +#define TEST_INDEX_0 0 +#define TEST_INDEX_1 1 +#define TEST_INDEX_2 2 +#define TEST_INDEX_3 3 +#define TEST_INDEX_4 4 + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksCipherTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksCipherTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksCipherTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + + +/** + * @tc.name: HksCipherTest.HksCipherTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksCipherTest, HksCipherTest001, Level1) +{ + TEST_ASSERT_TRUE(BaseTestCipher(1, TEST_INDEX_0, 1) == 0); +} + +#ifndef _CUT_AUTHENTICATE_ + +LITE_TEST_CASE(HksCipherTest, HksCipherTest002, Level1) +{ + TEST_ASSERT_TRUE(BaseTestCipher(1, TEST_INDEX_1, 1) == 0); +} + +LITE_TEST_CASE(HksCipherTest, HksCipherTest003, Level1) +{ + TEST_ASSERT_TRUE(BaseTestEncrypt(1, TEST_INDEX_0, 1) == 0); +} + +LITE_TEST_CASE(HksCipherTest, HksCipherTest004, Level1) +{ + TEST_ASSERT_TRUE(BaseTestEncrypt(1, TEST_INDEX_1, 1) == 0); +} + +LITE_TEST_CASE(HksCipherTest, HksCipherTest005, Level1) +{ + TEST_ASSERT_TRUE(BaseTestEncrypt(1, TEST_INDEX_2, 1) == 0); +} + +LITE_TEST_CASE(HksCipherTest, HksCipherTest006, Level1) +{ + TEST_ASSERT_TRUE(BaseTestEncrypt(1, TEST_INDEX_3, 1) == 0); +} + +LITE_TEST_CASE(HksCipherTest, HksCipherTest007, Level1) +{ + TEST_ASSERT_TRUE(BaseTestEncrypt(1, TEST_INDEX_4, 1) == 0); +} + +LITE_TEST_CASE(HksCipherTest, HksCipherTest008, Level1) +{ + TEST_ASSERT_TRUE(BaseTestDecrypt(1, TEST_INDEX_0, 1) == 0); +} + +LITE_TEST_CASE(HksCipherTest, HksCipherTest009, Level1) +{ + TEST_ASSERT_TRUE(BaseTestDecrypt(1, TEST_INDEX_1, 1) == 0); +} + +LITE_TEST_CASE(HksCipherTest, HksCipherTest010, Level1) +{ + TEST_ASSERT_TRUE(BaseTestDecrypt(1, TEST_INDEX_2, 1) == 0); +} + +LITE_TEST_CASE(HksCipherTest, HksCipherTest011, Level1) +{ + TEST_ASSERT_TRUE(BaseTestDecrypt(1, TEST_INDEX_3, 1) == 0); +} + +LITE_TEST_CASE(HksCipherTest, HksCipherTest012, Level1) +{ + TEST_ASSERT_TRUE(BaseTestDecrypt(1, TEST_INDEX_4, 1) == 0); +} +#endif /* _CUT_AUTHENTICATE_ */ + +RUN_TEST_SUITE(HksCipherTest); diff --git a/security_lite/huks/liteos_m_adapter/hks_curve25519_test.c b/security_lite/huks/liteos_m_adapter/hks_curve25519_test.c new file mode 100644 index 0000000000000000000000000000000000000000..b8ece9c8d11aa6897afaf431e76b3a4c55b6d3fb --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_curve25519_test.c @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2021 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 _CUT_AUTHENTICATE_ +#ifndef _CUT_ED25519_ +#include "hks_curve25519_test.h" + +#include "cmsis_os2.h" +#include "hctest.h" +#include "hi_watchdog.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_curve25519.h" +#include "los_config.h" +#include "ohos_types.h" +#include "securec.h" + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksCurve25519Test); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksCurve25519TestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksCurve25519TestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + + +/** + * @tc.name: HksCurve25519Test.HksCurve25519Test001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksCurve25519Test, HksCurve25519Test001, Level1) +{ + LiteTestPrint("HksCurve25519Test001 Begin!\n"); + TEST_ASSERT_TRUE(TestCurve25519All() == 0); + LiteTestPrint("HksCurve25519Test001 End!\n"); +} + +#define TEST_TASK_STACK_SIZE 0x2000 +#define WAIT_TO_TEST_DONE 4 + +static osPriority_t g_setPriority; + +static void LocalHksCurve25519Test002(void const *argument) +{ + LiteTestPrint("HksCurve25519Test002 Begin!\n"); + TEST_ASSERT_TRUE(TestEd25519SignTeeVerifyLocal() == 0); + LiteTestPrint("HksCurve25519Test002 End!\n"); + + osThreadExit(); +} + +/** + * @tc.name: HksCurve25519Test.HksCurve25519Test002 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksCurve25519Test, HksCurve25519Test002, Level1) +{ + osThreadId_t id; + osThreadAttr_t attr; + g_setPriority = osPriorityAboveNormal6; + attr.name = "test"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = TEST_TASK_STACK_SIZE; + attr.priority = g_setPriority; + id = osThreadNew((osThreadFunc_t)LocalHksCurve25519Test002, NULL, &attr); + + sleep(WAIT_TO_TEST_DONE); + + LiteTestPrint("HksCurve25519Test002 End2!\n"); +} + +/** + * @tc.name: HksCurve25519Test.HksCurve25519Test003 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksCurve25519Test, HksCurve25519Test003, Level1) +{ + TEST_ASSERT_TRUE(TestCurve25519SignWrong() == 0); +} + +/** + * @tc.name: HksCurve25519Test.HksCurve25519Test004 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksCurve25519Test, HksCurve25519Test004, Level1) +{ + TEST_ASSERT_TRUE(TestCurve25519verifyWrong() == 0); +} + +RUN_TEST_SUITE(HksCurve25519Test); +#endif +#endif /* _CUT_AUTHENTICATE_ */ + diff --git a/security_lite/huks/liteos_m_adapter/hks_delete_test.c b/security_lite/huks/liteos_m_adapter/hks_delete_test.c new file mode 100644 index 0000000000000000000000000000000000000000..14502eb601e5aaba4c6e6189a71132dc4802b83d --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_delete_test.c @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2021 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 _CUT_AUTHENTICATE_ + +#include "hctest.h" +#include "hi_watchdog.h" +#include "hks_delete_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "hks_type.h" + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksDeleteTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksDeleteTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksDeleteTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + + +static const struct HksTestKeyExistParams g_testKeyExistParams[] = { + /* normal case */ + { 0, HKS_SUCCESS, true, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE } }, +}; + +/** + * @tc.name: HksDeleteTest.HksDeleteTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksDeleteTest, HksDeleteTest001, Level1) +{ + int32_t ret; + struct HksBlob *keyAlias = NULL; + if (g_testKeyExistParams[0].isGenKey) { + HKS_TEST_ASSERT(TestGenDefaultKeyAndGetAlias(&keyAlias) == 0); + } else { + ret = TestConstuctBlob(&keyAlias, + g_testKeyExistParams[0].keyAliasParams.blobExist, + g_testKeyExistParams[0].keyAliasParams.blobSize, + g_testKeyExistParams[0].keyAliasParams.blobDataExist, + g_testKeyExistParams[0].keyAliasParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + } + + ret = HksDeleteKeyRun(keyAlias, 1); + HKS_TEST_ASSERT(ret == g_testKeyExistParams[0].expectResult); + + TestFreeBlob(&keyAlias); + TEST_ASSERT_TRUE(ret == 0); +} + +RUN_TEST_SUITE(HksDeleteTest); +#endif /* _CUT_AUTHENTICATE_ */ + diff --git a/security_lite/huks/liteos_m_adapter/hks_derive_test.c b/security_lite/huks/liteos_m_adapter/hks_derive_test.c new file mode 100644 index 0000000000000000000000000000000000000000..48aef7831478f8a5a0354479c4aecdc91937b70d --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_derive_test.c @@ -0,0 +1,225 @@ +/* + * Copyright (C) 2021 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 "hi_watchdog.h" +#include + +#include "hks_derive_test.h" + +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" + +#define DEFAULT_DERIVE_SIZE 32 +#define DEFAULT_INFO_SIZE 55 +#define DEFAULT_SALT_SIZE 16 + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksDeriveTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksDeriveTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksDeriveTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + + +static const struct HksTestDeriveParams g_testDeriveParams[] = { + /* hkdf-sha256-salt-info */ + { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_DERIVE, + true, HKS_DIGEST_SHA256, + false, 0, + false, 0, + false, 0 }, + { 0 }, + { + true, /* derive params */ + true, HKS_ALG_HKDF, + true, HKS_KEY_PURPOSE_DERIVE, + true, HKS_DIGEST_SHA256, + false, 0, + true, DEFAULT_SALT_SIZE, + true, DEFAULT_INFO_SIZE, + false, true }, + { + true, DEFAULT_DERIVE_SIZE, true, DEFAULT_DERIVE_SIZE }, + { + false, 0, false, 0 } + }, + + /* local: hkdf-sha256-salt-info */ + { 1, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_DERIVE, + true, HKS_DIGEST_SHA256, + false, 0, + false, 0, + true, HKS_STORAGE_TEMP }, + { 0 }, + { + true, /* derive params */ + true, HKS_ALG_HKDF, + true, HKS_KEY_PURPOSE_DERIVE, + true, HKS_DIGEST_SHA256, + false, 0, + true, DEFAULT_SALT_SIZE, + true, DEFAULT_INFO_SIZE, + true, false }, + { + true, DEFAULT_DERIVE_SIZE, true, DEFAULT_DERIVE_SIZE }, + { + true, DEFAULT_LOCAL_KEY_SIZE, true, DEFAULT_LOCAL_KEY_SIZE } + }, +}; + +static int32_t DeriveKey(const struct HksTestDeriveParamSet *deriveParamSetParams, const struct HksBlob *masterKey, + struct HksBlob *derivedKey, struct HksBlob **saltData, struct HksBlob **infoData) +{ + struct HksParamSet *deriveParamSet = NULL; + uint32_t saltSize = deriveParamSetParams->saltSize; + uint32_t infoSize = deriveParamSetParams->infoSize; + if (saltSize != 0) { + HKS_TEST_ASSERT(TestConstuctBlob(saltData, true, saltSize, true, saltSize) == 0); + } + if (infoSize != 0) { + HKS_TEST_ASSERT(TestConstuctBlob(infoData, true, infoSize, true, infoSize) == 0); + } + struct TestDeriveParamSetStructure paramStruct = { + &deriveParamSet, + deriveParamSetParams->paramSetExist, + deriveParamSetParams->setAlg, deriveParamSetParams->alg, + deriveParamSetParams->setPurpose, deriveParamSetParams->purpose, + deriveParamSetParams->setDigest, deriveParamSetParams->digest, + deriveParamSetParams->setIteration, deriveParamSetParams->iteration, + deriveParamSetParams->setSalt, *saltData, + deriveParamSetParams->setInfo, *infoData, + deriveParamSetParams->setIsKeyAlias, deriveParamSetParams->isKeyAlias + }; + int32_t ret = TestConstructDeriveParamSet(¶mStruct); + HKS_TEST_ASSERT(ret == 0); + + ret = HksDeriveKeyRun(deriveParamSet, masterKey, derivedKey, 1); + HksFreeParamSet(&deriveParamSet); + return ret; +} + +static int32_t BaseTestDerive(uint32_t index) +{ + /* 1. generate key */ + struct HksBlob *keyAlias = NULL; + int32_t ret; + if (g_testDeriveParams[index].genKeyParamSetParams.setKeyStorageFlag && + (g_testDeriveParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) { + ret = GenerateLocalRandomKey(&keyAlias, &g_testDeriveParams[index].localKeyParams); + } else { + if (g_testDeriveParams[index].keyAliasParams.blobExist) { + ret = GenerateKey(&keyAlias, &g_testDeriveParams[index].keyAliasParams, + &g_testDeriveParams[index].genKeyParamSetParams, NULL); + } else { + ret = TestConstuctBlob(&keyAlias, + g_testDeriveParams[index].masterKeyParams.blobExist, + g_testDeriveParams[index].masterKeyParams.blobSize, + g_testDeriveParams[index].masterKeyParams.blobDataExist, + g_testDeriveParams[index].masterKeyParams.blobDataSize); + } + } + TEST_ASSERT_TRUE(ret == 0); + + /* 2. derive */ + struct HksBlob *derivedKey = NULL; + ret = TestConstuctBlob(&derivedKey, + g_testDeriveParams[index].derivedKeyParams.blobExist, + g_testDeriveParams[index].derivedKeyParams.blobSize, + g_testDeriveParams[index].derivedKeyParams.blobDataExist, + g_testDeriveParams[index].derivedKeyParams.blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + + struct HksBlob *saltData = NULL; + struct HksBlob *infoData = NULL; + ret = DeriveKey(&g_testDeriveParams[index].deriveParamSetParams, keyAlias, derivedKey, &saltData, &infoData); + if (ret != g_testDeriveParams[index].expectResult) { + HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testDeriveParams[index].testId, ret); + } + TEST_ASSERT_TRUE(ret == g_testDeriveParams[index].expectResult); + + /* 3. delete key */ + if (!(g_testDeriveParams[index].genKeyParamSetParams.setKeyStorageFlag && + (g_testDeriveParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) && + (g_testDeriveParams[index].keyAliasParams.blobExist)) { + TEST_ASSERT_TRUE(HksDeleteKey(keyAlias, NULL) == 0); + } + TestFreeBlob(&keyAlias); + TestFreeBlob(&derivedKey); + TestFreeBlob(&saltData); + TestFreeBlob(&infoData); + + return ret; +} + +#ifndef _CUT_AUTHENTICATE_ +/** + * @tc.name: HksDeriveTest.HksDeriveTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksDeriveTest, HksDeriveTest001, Level1) +{ + int32_t ret = BaseTestDerive(0); + TEST_ASSERT_TRUE(ret == 0); +} +#endif /* _CUT_AUTHENTICATE_ */ + +/** + * @tc.name: HksDeriveTest.HksDeriveTest002 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksDeriveTest, HksDeriveTest002, Level1) +{ + int32_t ret = BaseTestDerive(1); + TEST_ASSERT_TRUE(ret == 0); +} +RUN_TEST_SUITE(HksDeriveTest); diff --git a/security_lite/huks/liteos_m_adapter/hks_exist_test.c b/security_lite/huks/liteos_m_adapter/hks_exist_test.c new file mode 100644 index 0000000000000000000000000000000000000000..cddee59b05be947646e24ef5d50e79992e72cb42 --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_exist_test.c @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2021 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 _CUT_AUTHENTICATE_ + +#include "hctest.h" +#include "hi_watchdog.h" +#include "hks_api.h" +#include "hks_exist_test.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "hks_type.h" + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksExistTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksExistTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksExistTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + + +static const struct HksTestKeyExistParams g_testKeyExistParams[] = { + /* normal case */ + { 0, HKS_SUCCESS, true, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE } }, +}; + +/** + * @tc.name: HksExistTest.HksExistTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksExistTest, HksExistTest001, Level1) +{ + int32_t ret; + struct HksBlob *keyAlias = NULL; + if (g_testKeyExistParams[0].isGenKey) { + TEST_ASSERT_TRUE(TestGenDefaultKeyAndGetAlias(&keyAlias) == 0); + ret = HksKeyExistRun(keyAlias, 1); + TEST_ASSERT_TRUE(ret == g_testKeyExistParams[0].expectResult); + TEST_ASSERT_TRUE(HksDeleteKey(keyAlias, NULL) == HKS_SUCCESS); + } else { + ret = TestConstuctBlob(&keyAlias, + g_testKeyExistParams[0].keyAliasParams.blobExist, + g_testKeyExistParams[0].keyAliasParams.blobSize, + g_testKeyExistParams[0].keyAliasParams.blobDataExist, + g_testKeyExistParams[0].keyAliasParams.blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + ret = HksKeyExistRun(keyAlias, 1); + if (ret != g_testKeyExistParams[0].expectResult) { + HKS_TEST_LOG_I("HksKeyExistRun 2 failed, ret[%u] = %d", g_testKeyExistParams[0].testId, ret); + } + TEST_ASSERT_TRUE(ret == g_testKeyExistParams[0].expectResult); + } + TestFreeBlob(&keyAlias); + TEST_ASSERT_TRUE(ret == 0); +} +RUN_TEST_SUITE(HksExistTest); +#endif /* _CUT_AUTHENTICATE_ */ + diff --git a/security_lite/huks/liteos_m_adapter/hks_generate_key_test.c b/security_lite/huks/liteos_m_adapter/hks_generate_key_test.c new file mode 100644 index 0000000000000000000000000000000000000000..87c5b3c53c9c1280e3333cee04e0b2aab61608b3 --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_generate_key_test.c @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2021 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 _CUT_AUTHENTICATE_ + +#include "hks_generate_key_test.h" + +#include +#include "hi_watchdog.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" + +#define DEFAULT_X25519_PARAM_SET_OUT 104 + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksGenerateKeyTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksGenerateKeyTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksGenerateKeyTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + + +static const struct HksTestGenKeyParams g_testGenKeyParams[] = { + /* x25519: ree sign/verify */ + { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, { + true, + true, HKS_ALG_X25519, + true, HKS_CURVE25519_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY, + false, 0, + false, 0, + false, 0, + true, HKS_STORAGE_TEMP }, + { true, DEFAULT_X25519_PARAM_SET_OUT }, + }, +}; + +/** + * @tc.name: HksGenerateKeyTest.HksGenerateKeyTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksGenerateKeyTest, HksGenerateKeyTest001, Level1) +{ + uint32_t times = 1; + uint32_t index = 0; + uint32_t performTimes = 1; + struct HksBlob *keyAlias = NULL; + int32_t ret = TestConstuctBlob(&keyAlias, + g_testGenKeyParams[index].keyAliasParams.blobExist, + g_testGenKeyParams[index].keyAliasParams.blobSize, + g_testGenKeyParams[index].keyAliasParams.blobDataExist, + g_testGenKeyParams[index].keyAliasParams.blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + + struct HksParamSet *paramSet = NULL; + struct GenerateKeyParamSetStructure paramStruct = { ¶mSet, + g_testGenKeyParams[index].paramSetParams.paramSetExist, + g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg, + g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize, + g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose, + g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest, + g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding, + g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode, + g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag, + g_testGenKeyParams[index].paramSetParams.keyStorageFlag }; + ret = TestConstructGenerateKeyParamSet(¶mStruct); + TEST_ASSERT_TRUE(ret == 0); + + struct HksParamSet *paramSetOut = NULL; + ret = TestConstructGenerateKeyParamSetOut(¶mSetOut, + g_testGenKeyParams[index].paramSetParamsOut.paramSetExist, + g_testGenKeyParams[index].paramSetParamsOut.paramSetSize); + TEST_ASSERT_TRUE(ret == 0); + + ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes); + if (ret != g_testGenKeyParams[index].expectResult) { + HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret); + } + TEST_ASSERT_TRUE(ret == g_testGenKeyParams[index].expectResult); + + if ((ret == HKS_SUCCESS) && + !(g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag == true) && + (g_testGenKeyParams[index].paramSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) { + HKS_TEST_ASSERT(HksDeleteKey(keyAlias, NULL) == 0); + } + TestFreeBlob(&keyAlias); + HksFreeParamSet(¶mSet); + HksFreeParamSet(¶mSetOut); + HKS_TEST_LOG_I("[%u]TestGenerateKey, Testcase_GenerateKey_[%03u] pass!", times, g_testGenKeyParams[index].testId); + TEST_ASSERT_TRUE(ret == 0); +} + +RUN_TEST_SUITE(HksGenerateKeyTest); +#endif /* _CUT_AUTHENTICATE_ */ + diff --git a/security_lite/huks/liteos_m_adapter/hks_generate_random_test.c b/security_lite/huks/liteos_m_adapter/hks_generate_random_test.c new file mode 100644 index 0000000000000000000000000000000000000000..80b846fcfbf3ceed62533b52fba30bbabf496ed7 --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_generate_random_test.c @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2021 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 "hks_generate_random_test.h" + +#include "hctest.h" +#include "hi_watchdog.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "hks_type.h" + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksGenerateRandomTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksGenerateRandomTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksGenerateRandomTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + +static const struct HksTestGenRandomParams g_testGenRandomParams[] = { + /* normal case */ + { 0, HKS_SUCCESS, { true, HKS_MAX_RANDOM_LEN, true, HKS_MAX_RANDOM_LEN } }, +}; +/** + * @tc.name: HksGenerateRandomTest.HksGenerateRandomTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksGenerateRandomTest, HksGenerateRandomTest001, Level1) +{ + int32_t ret; + struct HksBlob *random = NULL; + + ret = TestConstructBlobOut(&random, + g_testGenRandomParams[0].randomParams.blobExist, + g_testGenRandomParams[0].randomParams.blobSize, + g_testGenRandomParams[0].randomParams.blobDataExist, + g_testGenRandomParams[0].randomParams.blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + + ret = HksGenerateRandomRun(random, 1); + if (ret != g_testGenRandomParams[0].expectResult) { + HKS_TEST_LOG_I("HksGenerateRandomRun failed, ret[%u] = %d", g_testGenRandomParams[0].testId, ret); + } + TEST_ASSERT_TRUE(ret == g_testGenRandomParams[0].expectResult); + + TestFreeBlob(&random); + TEST_ASSERT_TRUE(ret == 0); +} + +RUN_TEST_SUITE(HksGenerateRandomTest); diff --git a/security_lite/huks/liteos_m_adapter/hks_hash_test.c b/security_lite/huks/liteos_m_adapter/hks_hash_test.c new file mode 100644 index 0000000000000000000000000000000000000000..b8dfb9f9028ec137702509a1dc7df104a88a6757 --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_hash_test.c @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2021 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 _CUT_AUTHENTICATE_ + +#include "hks_hash_test.h" + +#include +#include "hi_watchdog.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "hks_type.h" + + +#define DEFAULT_SRC_DATA_SIZE 200 + +#define DIGEST_SHA256_HASH_SIZE 32 + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksHashTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksHashTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksHashTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + + +static const struct HksTestHashParams g_testHashParams[] = { + /* normal case */ + { 0, HKS_SUCCESS, + { true, true, HKS_DIGEST_SHA256 }, + { true, DEFAULT_SRC_DATA_SIZE, true, DEFAULT_SRC_DATA_SIZE }, + { true, DIGEST_SHA256_HASH_SIZE, true, DIGEST_SHA256_HASH_SIZE } + }, +}; + +/** + * @tc.name: HksHashTest.HksHashTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksHashTest, HksHashTest001, Level1) +{ + struct HksParamSet *paramSet = NULL; + struct HksBlob *srcData = NULL; + struct HksBlob *hash = NULL; + + int32_t ret = TestConstructHashParamSet(¶mSet, + g_testHashParams[0].paramSetParams.paramSetExist, + g_testHashParams[0].paramSetParams.setDigest, g_testHashParams[0].paramSetParams.digest); + TEST_ASSERT_TRUE(ret == 0); + + ret = TestConstuctBlob(&srcData, + g_testHashParams[0].srcDataParams.blobExist, + g_testHashParams[0].srcDataParams.blobSize, + g_testHashParams[0].srcDataParams.blobDataExist, + g_testHashParams[0].srcDataParams.blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + + ret = TestConstructBlobOut(&hash, + g_testHashParams[0].hashParams.blobExist, + g_testHashParams[0].hashParams.blobSize, + g_testHashParams[0].hashParams.blobDataExist, + g_testHashParams[0].hashParams.blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + + ret = HksHashRun(paramSet, srcData, hash, 1); + if (ret != g_testHashParams[0].expectResult) { + HKS_TEST_LOG_I("HksHashRun failed, ret[%u] = %d", g_testHashParams[0].testId, ret); + } + TEST_ASSERT_TRUE(ret == g_testHashParams[0].expectResult); + + HksFreeParamSet(¶mSet); + TestFreeBlob(&srcData); + TestFreeBlob(&hash); + HKS_TEST_LOG_I("[%u]TestHash, Testcase_Hash_[%03u] pass!", 1, g_testHashParams[0].testId); + TEST_ASSERT_TRUE(ret == 0); +} +RUN_TEST_SUITE(HksHashTest); + +#endif /* _CUT_AUTHENTICATE_ */ + diff --git a/security_lite/huks/liteos_m_adapter/hks_mac_test.c b/security_lite/huks/liteos_m_adapter/hks_mac_test.c new file mode 100644 index 0000000000000000000000000000000000000000..294bcdc83c492109e85fe468b98b8e5634ba56cf --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_mac_test.c @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2021 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 "hks_mac_test.h" + +#include +#include "hi_watchdog.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_log.h" + +#define HKS_TEST_MAC_REE_KEY_SIZE_32 32 +#define HKS_DEFAULT_MAC_SRCDATA_SIZE 253 +#define HKS_DEFAULT_MAC_SHA256_SIZE 32 + + +static const struct HksTestMacParams g_testMacParams[] = { + /* success: ree-sha256 */ + { 0, HKS_SUCCESS, HKS_TEST_MAC_TYPE_REE, { 0 }, { 0 }, + { true, HKS_TEST_MAC_REE_KEY_SIZE_32, true, HKS_TEST_MAC_REE_KEY_SIZE_32 }, + { true, true, HKS_KEY_PURPOSE_MAC, true, HKS_DIGEST_SHA256 }, + { true, HKS_DEFAULT_MAC_SRCDATA_SIZE, true, HKS_DEFAULT_MAC_SRCDATA_SIZE }, + { true, HKS_DEFAULT_MAC_SHA256_SIZE, true, HKS_DEFAULT_MAC_SHA256_SIZE } + }, + + /* success: tee-sha256 */ + { 1, HKS_SUCCESS, HKS_TEST_MAC_TYPE_TEE, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { true, true, HKS_ALG_AES, true, HKS_AES_KEY_SIZE_256, true, HKS_KEY_PURPOSE_MAC, + true, HKS_DIGEST_SHA256, false, 0, false, 0 }, + { 0 }, + { true, true, HKS_KEY_PURPOSE_MAC, true, HKS_DIGEST_SHA256 }, + { true, HKS_DEFAULT_MAC_SRCDATA_SIZE, true, HKS_DEFAULT_MAC_SRCDATA_SIZE }, + { true, HKS_DEFAULT_MAC_SHA256_SIZE, true, HKS_DEFAULT_MAC_SHA256_SIZE } + }, +}; + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksMacTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksMacTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksMacTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + + +static int32_t ConstructDataToBlob(struct HksBlob **srcData, struct HksBlob **macData, + const struct HksTestBlobParams *srcDataParams, const struct HksTestBlobParams *macDataParams) +{ + int32_t ret = TestConstuctBlob(srcData, + srcDataParams->blobExist, + srcDataParams->blobSize, + srcDataParams->blobDataExist, + srcDataParams->blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + + ret = TestConstuctBlob(macData, + macDataParams->blobExist, + macDataParams->blobSize, + macDataParams->blobDataExist, + macDataParams->blobDataSize); + TEST_ASSERT_TRUE(ret == 0); + return ret; +} + +static int32_t Mac(const struct HksBlob *key, const struct HksBlob *srcData, struct HksBlob *macData, + const struct HksTestMacParamSet *macParamSetParams, enum HksTestMacType macType) +{ + struct HksParamSet *macParamSet = NULL; + int32_t ret; + if (macType == HKS_TEST_MAC_TYPE_REE) { + struct TestMacParamSetStructure paramStructTrue = { + &macParamSet, + macParamSetParams->paramSetExist, + macParamSetParams->setPurpose, macParamSetParams->purpose, + macParamSetParams->setDigest, macParamSetParams->digest, true, false + }; + ret = TestConstructMacParamSet(¶mStructTrue); + } else { + struct TestMacParamSetStructure paramStructFalse = { + &macParamSet, + macParamSetParams->paramSetExist, + macParamSetParams->setPurpose, macParamSetParams->purpose, + macParamSetParams->setDigest, macParamSetParams->digest, false, false + }; + ret = TestConstructMacParamSet(¶mStructFalse); + } + TEST_ASSERT_TRUE(ret == 0); + + ret = HksMacRun(key, macParamSet, srcData, macData, 1); + HksFreeParamSet(&macParamSet); + return ret; +} + +static int32_t BaseTestMac(uint32_t index) +{ + /* 1. generate key */ + struct HksBlob *key = NULL; + int32_t ret; + + if (g_testMacParams[index].macType == HKS_TEST_MAC_TYPE_REE) { + ret = TestConstuctBlob(&key, + g_testMacParams[index].keyParams.blobExist, + g_testMacParams[index].keyParams.blobSize, + g_testMacParams[index].keyParams.blobDataExist, + g_testMacParams[index].keyParams.blobDataSize); + } else { + if (g_testMacParams[index].keyAliasParams.blobExist) { + ret = GenerateKey(&key, &(g_testMacParams[index].keyAliasParams), + &g_testMacParams[index].genKeyParamSetParams, NULL); + } else { + ret = TestConstuctBlob(&key, + g_testMacParams[index].keyParams.blobExist, + g_testMacParams[index].keyParams.blobSize, + g_testMacParams[index].keyParams.blobDataExist, + g_testMacParams[index].keyParams.blobDataSize); + } + } + TEST_ASSERT_TRUE(ret == 0); + + /* 2. mac */ + struct HksBlob *srcData = NULL; + struct HksBlob *macData = NULL; + ret = ConstructDataToBlob(&srcData, &macData, + &g_testMacParams[index].srcDataParams, &g_testMacParams[index].macParams); + TEST_ASSERT_TRUE(ret == 0); + + ret = Mac(key, srcData, macData, &g_testMacParams[index].macParamSetParams, g_testMacParams[index].macType); + if (ret != g_testMacParams[index].expectResult) { + HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testMacParams[index].testId, ret); + } + TEST_ASSERT_TRUE(ret == g_testMacParams[index].expectResult); + + /* 3. deletekey */ + if ((g_testMacParams[index].macType == HKS_TEST_MAC_TYPE_TEE) && + (g_testMacParams[index].keyAliasParams.blobExist)) { + ret = HksDeleteKey(key, NULL); + TEST_ASSERT_TRUE(ret == 0); + } + TestFreeBlob(&key); + TestFreeBlob(&srcData); + TestFreeBlob(&macData); + return ret; +} + +/** + * @tc.name: HksMacTest.HksMacTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksMacTest, HksMacTest001, Level1) +{ + int32_t ret = BaseTestMac(0); + TEST_ASSERT_TRUE(ret == 0); +} + +#ifndef _CUT_AUTHENTICATE_ +/** + * @tc.name: HksMacTest.HksMacTest002 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksMacTest, HksMacTest002, Level1) +{ + int32_t ret = BaseTestMac(1); + TEST_ASSERT_TRUE(ret == 0); +} +#endif /* _CUT_AUTHENTICATE_ */ + +RUN_TEST_SUITE(HksMacTest); diff --git a/security_lite/huks/liteos_m_adapter/hks_modify_key_test.c b/security_lite/huks/liteos_m_adapter/hks_modify_key_test.c new file mode 100644 index 0000000000000000000000000000000000000000..b391222b31ebecaeb18eedd88833f11166723294 --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_modify_key_test.c @@ -0,0 +1,368 @@ +/* + * Copyright (C) 2021 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 _CUT_AUTHENTICATE_ + + +#include "hks_modify_key_test.h" + +#include +#include "hi_watchdog.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_cipher.h" +#include "hks_test_common.h" +#include "hks_test_log.h" +#include "stdlib.h" + +#define DEFAULT_AES_CIPHER_PLAIN_SIZE 1000 +#define AES_DEFAULT_GCM_NONCE_LENGTH 12 +#define AES_DEFAULT_AAD_LEN 4 +static const char *g_storePath = "./hks_store/"; +static const char *g_testName = "TestName"; + +static const struct HksTestCipherParams g_testCipherParams[] = { + /* success: aes256-gcm-none */ + { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, /* genKey params */ + true, HKS_ALG_AES, + true, HKS_AES_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0 + }, + { false, 0 }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* encrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_ENCRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN + }, + { + HKS_TEST_CIPHER_TYPE_AES, true, /* decrypt params */ + true, HKS_ALG_AES, + true, HKS_KEY_PURPOSE_DECRYPT, + false, 0, + true, HKS_PADDING_NONE, + true, HKS_MODE_GCM, + false, 0, + true, AES_DEFAULT_GCM_NONCE_LENGTH, + true, AES_DEFAULT_AAD_LEN + }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16 }, + { true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE }, + { false, 0, false, 0 } + }, +}; + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksModifyKeyTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksModifyKeyTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +static void HksTestRemoveFile() +{ +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksModifyKeyTestTearDown() +{ + LiteTestPrint("tearDown\n"); + HksTestRemoveFile(); + hi_watchdog_enable(); + return TRUE; +} + + +static int32_t ConstructDataToBlob(struct HksBlob **inData, struct HksBlob **outData, + const struct HksTestBlobParams *inTextParams, const struct HksTestBlobParams *outTextParams) +{ + int32_t ret = TestConstuctBlob(inData, + inTextParams->blobExist, + inTextParams->blobSize, + inTextParams->blobDataExist, + inTextParams->blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + ret = TestConstuctBlob(outData, + outTextParams->blobExist, + outTextParams->blobSize, + outTextParams->blobDataExist, + outTextParams->blobDataSize); + HKS_TEST_ASSERT(ret == 0); + return ret; +} + +static int32_t Encrypt(struct CipherEncryptStructure *encryptStruct) +{ + int32_t ret; + struct HksParamSet *encryptParamSet = NULL; + + uint32_t ivSize = encryptStruct->cipherParms->ivSize; + uint32_t nonceSize = encryptStruct->cipherParms->nonceSize; + uint32_t aadSize = encryptStruct->cipherParms->aadSize; + if (ivSize != 0) { + ret = TestConstuctBlob(encryptStruct->ivData, true, ivSize, true, ivSize); + HKS_TEST_ASSERT(ret == 0); + } + if (nonceSize != 0) { + ret = TestConstuctBlob(encryptStruct->nonceData, true, nonceSize, true, nonceSize); + HKS_TEST_ASSERT(ret == 0); + } + if (aadSize != 0) { + ret = TestConstuctBlob(encryptStruct->aadData, true, aadSize, true, aadSize); + HKS_TEST_ASSERT(ret == 0); + } + struct AesCipherParamSetStructure enParamStruct = { + &encryptParamSet, + encryptStruct->cipherParms->paramSetExist, + encryptStruct->cipherParms->setAlg, encryptStruct->cipherParms->alg, + encryptStruct->cipherParms->setPurpose, encryptStruct->cipherParms->purpose, + encryptStruct->cipherParms->setPadding, encryptStruct->cipherParms->padding, + encryptStruct->cipherParms->setBlockMode, encryptStruct->cipherParms->mode, + encryptStruct->cipherParms->setIv, *(encryptStruct->ivData), + encryptStruct->cipherParms->setNonce, *(encryptStruct->nonceData), + encryptStruct->cipherParms->setAad, *(encryptStruct->aadData), + encryptStruct->cipherParms->setIsKeyAlias, encryptStruct->cipherParms->isKeyAlias + }; + ret = TestConstructAesCipherParamSet(&enParamStruct); + HKS_TEST_ASSERT(ret == 0); + + ret = HksEncryptRun(encryptStruct->keyAlias, encryptParamSet, encryptStruct->plainData, encryptStruct->cipherData, + encryptStruct->performTimes); + HksFreeParamSet(&encryptParamSet); + return ret; +} + +static int32_t DecryptCipher(struct CipherDecryptStructure *decryptStruct) +{ + int32_t ret = TestConstuctBlob(decryptStruct->decryptedData, + decryptStruct->cipherParms->decryptedTextParams.blobExist, + decryptStruct->cipherParms->decryptedTextParams.blobSize, + decryptStruct->cipherParms->decryptedTextParams.blobDataExist, + decryptStruct->cipherParms->decryptedTextParams.blobDataSize); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *decryptParamSet = NULL; + struct AesCipherParamSetStructure deParamStruct = { + &decryptParamSet, + decryptStruct->cipherParms->decryptParamSetParams.paramSetExist, + decryptStruct->cipherParms->decryptParamSetParams.setAlg, + decryptStruct->cipherParms->decryptParamSetParams.alg, + decryptStruct->cipherParms->decryptParamSetParams.setPurpose, + decryptStruct->cipherParms->decryptParamSetParams.purpose, + decryptStruct->cipherParms->decryptParamSetParams.setPadding, + decryptStruct->cipherParms->decryptParamSetParams.padding, + decryptStruct->cipherParms->decryptParamSetParams.setBlockMode, + decryptStruct->cipherParms->decryptParamSetParams.mode, + decryptStruct->cipherParms->decryptParamSetParams.setIv, decryptStruct->ivData, + decryptStruct->cipherParms->decryptParamSetParams.setNonce, decryptStruct->nonceData, + decryptStruct->cipherParms->decryptParamSetParams.setAad, decryptStruct->aadData, + decryptStruct->cipherParms->decryptParamSetParams.setIsKeyAlias, + decryptStruct->cipherParms->decryptParamSetParams.isKeyAlias + }; + ret = TestConstructAesCipherParamSet(&deParamStruct); + HKS_TEST_ASSERT(ret == 0); + + ret = HksDecryptRun(decryptStruct->keyAlias, decryptParamSet, decryptStruct->cipherData, + *(decryptStruct->decryptedData), decryptStruct->performTimes); + HksFreeParamSet(&decryptParamSet); + return ret; +} + +int32_t GenerateKeyTwo(const struct HksBlob *keyAlias, const struct HksTestBlobParams *keyAliasParams, + const struct HksTestGenKeyParamsParamSet *genKeyParamSetParams, + const struct HksTestGenKeyParamsParamSetOut *genKeyParamSetParamsOut) +{ + struct HksParamSet *paramSet = NULL; + struct GenerateKeyParamSetStructure paramStruct = { + ¶mSet, + genKeyParamSetParams->paramSetExist, + genKeyParamSetParams->setAlg, genKeyParamSetParams->alg, + genKeyParamSetParams->setKeySize, genKeyParamSetParams->keySize, + genKeyParamSetParams->setPurpose, genKeyParamSetParams->purpose, + genKeyParamSetParams->setDigest, genKeyParamSetParams->digest, + genKeyParamSetParams->setPadding, genKeyParamSetParams->padding, + genKeyParamSetParams->setBlockMode, genKeyParamSetParams->mode, + genKeyParamSetParams->setKeyStorageFlag, genKeyParamSetParams->keyStorageFlag + }; + int32_t ret = TestConstructGenerateKeyParamSet(¶mStruct); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *paramSetOut = NULL; + if (genKeyParamSetParamsOut != NULL) { + ret = TestConstructGenerateKeyParamSetOut(¶mSet, + genKeyParamSetParamsOut->paramSetExist, genKeyParamSetParamsOut->paramSetSize); + HKS_TEST_ASSERT(ret == 0); + } + + ret = HksGenerateKey(keyAlias, paramSet, paramSetOut); + HKS_TEST_ASSERT(ret == 0); + + HksFreeParamSet(¶mSet); + return ret; +} + +int32_t BaseTestCipherProcess(const struct HksBlob *keyAlias, uint32_t index) +{ + struct HksBlob *plainData = NULL; + struct HksBlob *cipherData = NULL; + int32_t ret = ConstructDataToBlob(&plainData, &cipherData, + &g_testCipherParams[index].plainTextParams, &g_testCipherParams[index].cipherTextParams); + HKS_TEST_ASSERT(ret == 0); + struct HksBlob *ivData = NULL; + struct HksBlob *nonceData = NULL; + struct HksBlob *aadData = NULL; + struct HksBlob *decryptedData = NULL; + /* 2. encrypt */ + do { + struct CipherEncryptStructure testEncryptStruct = { + keyAlias, &g_testCipherParams[index].encryptParamSetParams, + plainData, cipherData, &ivData, &nonceData, &aadData, 1 + }; + ret = Encrypt(&testEncryptStruct); + if (ret != g_testCipherParams[index].expectResult) { + break; + } + /* 3. decrypt */ + struct CipherDecryptStructure testDecryptStruct = { + keyAlias, &g_testCipherParams[index], cipherData, + &decryptedData, ivData, nonceData, aadData, 1 + }; + ret = DecryptCipher(&testDecryptStruct); + if (ret != g_testCipherParams[index].expectResult) { + break; + } + + if (ret == g_testCipherParams[index].expectResult) { + if (plainData->size != decryptedData->size) { + break; + }; + ret = memcmp(plainData->data, decryptedData->data, plainData->size); + } + } while (0); + TestFreeBlob(&plainData); + TestFreeBlob(&cipherData); + TestFreeBlob(&decryptedData); + TestFreeBlob(&ivData); + TestFreeBlob(&nonceData); + TestFreeBlob(&aadData); + return ret; +} + +enum HksStorageType { + HKS_STORAGE_TYPE_KEY = 0, + HKS_STORAGE_TYPE_CERTCHAIN, + HKS_STORAGE_TYPE_ROOT_KEY, +}; + +extern int32_t HksStoreKeyBlob(const struct HksBlob *processName, const struct HksBlob *keyAlias, + enum HksStorageType storageType, const struct HksBlob *keyBlob); +int32_t __attribute__((weak)) HksStoreKeyBlob(const struct HksBlob *processName, const struct HksBlob *keyAlias, + enum HksStorageType storageType, const struct HksBlob *keyBlob) +{ + (void)processName; + (void)keyAlias; + (void)storageType; + (void)keyBlob; +} + +/** + * @tc.name: HksModifyKeyTest.HksModifyKeyTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksModifyKeyTest, HksModifyKeyTest001, Level1) +{ + uint32_t index = 0; + struct HksBlob keyAlias = { strlen(g_testName), (uint8_t *)g_testName }; + int32_t ret = GenerateKeyTwo(&keyAlias, &g_testCipherParams[index].keyAliasParams, + &g_testCipherParams[index].genKeyParamSetParams, + &g_testCipherParams[index].genKeyParamSetParamsOut); + TEST_ASSERT_TRUE(ret == 0); + + ret = BaseTestCipherProcess(&keyAlias, 0); + TEST_ASSERT_TRUE(ret == 0); + + struct HksBlob *plainData = NULL; + struct HksBlob *cipherData = NULL; + ret = ConstructDataToBlob(&plainData, &cipherData, + &g_testCipherParams[index].plainTextParams, &g_testCipherParams[index].cipherTextParams); + TEST_ASSERT_TRUE(ret == 0); + struct HksBlob *ivData = NULL; + struct HksBlob *nonceData = NULL; + struct HksBlob *aadData = NULL; + + /* 2. encrypt */ + struct CipherEncryptStructure testEncryptStruct = { + &keyAlias, &g_testCipherParams[index].encryptParamSetParams, + plainData, cipherData, &ivData, &nonceData, &aadData, 1 + }; + ret = Encrypt(&testEncryptStruct); + TEST_ASSERT_TRUE(ret == 0); + + ret = GenerateKeyTwo(&keyAlias, &g_testCipherParams[index].keyAliasParams, + &g_testCipherParams[index].genKeyParamSetParams, + &g_testCipherParams[index].genKeyParamSetParamsOut); + TEST_ASSERT_TRUE(ret == 0); + + /* 3. decrypt */ + struct HksBlob *decryptedData = NULL; + struct CipherDecryptStructure testDecryptStruct = { + &keyAlias, &g_testCipherParams[index], cipherData, + &decryptedData, ivData, nonceData, aadData, 1 + }; + ret = DecryptCipher(&testDecryptStruct); + + HKS_TEST_ASSERT(ret != g_testCipherParams[index].expectResult); + TestFreeBlob(&plainData); + TestFreeBlob(&cipherData); + TestFreeBlob(&decryptedData); + TestFreeBlob(&ivData); + TestFreeBlob(&nonceData); + TestFreeBlob(&aadData); + TEST_ASSERT_TRUE(ret != 0); +} + +RUN_TEST_SUITE(HksModifyKeyTest); +#endif /* _CUT_AUTHENTICATE_ */ diff --git a/security_lite/huks/liteos_m_adapter/hks_others_test.c b/security_lite/huks/liteos_m_adapter/hks_others_test.c new file mode 100644 index 0000000000000000000000000000000000000000..ba3157ba6da20169266efb3daf7897fe8275fd3b --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_others_test.c @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2021 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 _CUT_AUTHENTICATE_ + +#include "hks_others_test.h" + +#include +#include "hi_watchdog.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_type.h" + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksOthersTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksOthersTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksOthersTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + +/** + * @tc.name: HksOthersTest.HksOthersTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksOthersTest, HksOthersTest001, Level1) +{ + int32_t ret = HksGetKeyParamSet(NULL, NULL, NULL); + TEST_ASSERT_TRUE(ret == HKS_ERROR_NULL_POINTER); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest002 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksOthersTest, HksOthersTest002, Level1) +{ + int32_t ret = HksGetKeyInfoList(NULL, NULL, NULL); + TEST_ASSERT_TRUE(ret == HKS_ERROR_NULL_POINTER); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest003 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksOthersTest, HksOthersTest003, Level1) +{ + int32_t ret = HksAttestKey(NULL, NULL, NULL); + TEST_ASSERT_TRUE(ret == HKS_ERROR_NOT_SUPPORTED); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest004 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksOthersTest, HksOthersTest004, Level1) +{ + int32_t ret = HksGetCertificateChain(NULL, NULL, NULL); + TEST_ASSERT_TRUE(ret == HKS_ERROR_NOT_SUPPORTED); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest005 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksOthersTest, HksOthersTest005, Level1) +{ + int32_t ret = HksWrapKey(NULL, NULL, NULL, NULL); + TEST_ASSERT_TRUE(ret == HKS_ERROR_NOT_SUPPORTED); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest006 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksOthersTest, HksOthersTest006, Level1) +{ + int32_t ret = HksUnwrapKey(NULL, NULL, NULL, NULL); + TEST_ASSERT_TRUE(ret == HKS_ERROR_NOT_SUPPORTED); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest006 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksOthersTest, HksOthersTest007, Level1) +{ + int32_t ret = HksGetSdkVersion(NULL); + TEST_ASSERT_TRUE(ret == HKS_ERROR_NULL_POINTER); +} + +/** + * @tc.name: HksOthersTest.HksOthersTest008 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksOthersTest, HksOthersTest008, Level1) +{ + int32_t ret = HksInitialize(); + TEST_ASSERT_TRUE(ret == HKS_SUCCESS); +} +RUN_TEST_SUITE(HksOthersTest); + +#endif /* _CUT_AUTHENTICATE_ */ diff --git a/security_lite/huks/liteos_m_adapter/hks_safe_cipher_key_test.c b/security_lite/huks/liteos_m_adapter/hks_safe_cipher_key_test.c new file mode 100644 index 0000000000000000000000000000000000000000..6e14a71bbb5b5277f35b96dcd2af5f6434e7e778 --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_safe_cipher_key_test.c @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2021 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 _CUT_AUTHENTICATE_ +#ifndef _CUT_ED25519_ + +#include "hks_safe_cipher_key_test.h" + +#include +#include "hi_watchdog.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_curve25519.h" +#include "hks_test_log.h" +#include "hks_test_mem.h" +#include "stdlib.h" + +const char *g_storePath = "/storage/"; +const char *g_testEd25519 = "test_ed25519"; + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksSafeCipherKeyTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksSafeCipherKeyTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksSafeCipherKeyTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + +static struct HksBlob g_storageImageBuffer; + +static int32_t GetKeyOffsetByKeyAlias(const struct HksBlob *keyAlias, uint32_t *keyOffset) +{ + struct HksBlob storageBuf = g_storageImageBuffer; + + /* 1. get imageBuffer total Len */ + struct HksStoreHeaderInfo *keyInfoHead = (struct HksStoreHeaderInfo *)storageBuf.data; + uint32_t keyCount = keyInfoHead->keyCount; + uint32_t totalLen = keyInfoHead->totalLen; + + /* 2. traverse imageBuffer to search for keyAlias */ + uint32_t offset = sizeof(*keyInfoHead); + for (uint16_t i = 0; i < keyCount; ++i) { + uint8_t *tmpBuf = storageBuf.data + offset; + struct HksStoreKeyInfo *keyInfo = (struct HksStoreKeyInfo *)tmpBuf; + if (keyInfo->aliasSize == keyAlias->size) { + if (HksMemCmp(keyAlias->data, tmpBuf + sizeof(*keyInfo), keyAlias->size) == 0) { + *keyOffset = offset; + return HKS_SUCCESS; + } + } + + offset += keyInfo->keyInfoLen; + } + + return HKS_ERROR_NOT_EXIST; +} + +static int32_t CompareTwoKey(const struct HksBlob *keyAliasOne, const struct HksBlob *keyAliasTwo) +{ + uint32_t sizeOne = HksTestFileSize(g_storePath, "hks_keystore"); + uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne); + if (bufOne == NULL) { + return HKS_ERROR_MALLOC_FAIL; + } + + uint32_t sizeRead = HksTestFileRead(g_storePath, "hks_keystore", 0, bufOne, sizeOne); + TEST_ASSERT_TRUE(sizeRead > 0); + + g_storageImageBuffer.data = bufOne; + g_storageImageBuffer.size = sizeOne; + + int32_t offset1; + int ret = GetKeyOffsetByKeyAlias(keyAliasOne, &offset1); + TEST_ASSERT_TRUE(ret == 0); + + struct HksStoreKeyInfo *keyInfo1 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset1); + + int32_t offset2; + ret = GetKeyOffsetByKeyAlias(keyAliasTwo, &offset2); + TEST_ASSERT_TRUE(ret == 0); + + struct HksStoreKeyInfo *keyInfo2 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset2); + + TEST_ASSERT_TRUE(keyInfo1->keyInfoLen == keyInfo2->keyInfoLen); + + ret = memcmp(keyInfo1, keyInfo2, keyInfo1->keyInfoLen); + HksTestFree(bufOne); + return ret; +} + +/** + * @tc.name: HksSafeCipherKeyTest.HksSafeCipherKeyTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksSafeCipherKeyTest, HksSafeCipherKeyTest001, Level1) +{ + struct HksBlob ed25519Alias = { strlen(g_testEd25519), (uint8_t *)g_testEd25519 }; + uint8_t pubKey[32] = {0}; + uint32_t pubKeyLen = 32; + struct HksBlob pubKeyInfo = { pubKeyLen, pubKey }; + int32_t ret = TestGenerateEd25519Key(ed25519Alias); + TEST_ASSERT_TRUE(ret == 0); + + ret = HksExportPublicKey(&ed25519Alias, NULL, &pubKeyInfo); + TEST_ASSERT_TRUE(ret == 0); + + ret = HksDeleteKey(&ed25519Alias, NULL); + TEST_ASSERT_TRUE(ret == 0); + + struct HksBlob newAliasOne = { strlen("test_ed25519_1"), (uint8_t *)"test_ed25519_1" }; + ret = TestImportEd25519(newAliasOne, &pubKeyInfo); + TEST_ASSERT_TRUE(ret == 0); + + struct HksBlob newAliasTwo = { strlen("test_ed25519_2"), (uint8_t *)"test_ed25519_2" }; + ret = TestImportEd25519(newAliasTwo, &pubKeyInfo); + TEST_ASSERT_TRUE(ret == 0); + + ret = CompareTwoKey(&newAliasOne, &newAliasTwo); + TEST_ASSERT_TRUE(ret != 0); + + ret = HksDeleteKey(&newAliasOne, NULL); + HKS_TEST_ASSERT(ret == 0); + TEST_ASSERT_TRUE(ret == 0); + ret = HksDeleteKey(&newAliasTwo, NULL); + TEST_ASSERT_TRUE(ret == 0); +} +RUN_TEST_SUITE(HksSafeCipherKeyTest); + +#endif +#endif /* _CUT_AUTHENTICATE_ */ diff --git a/security_lite/huks/liteos_m_adapter/hks_safe_compare_key_test.c b/security_lite/huks/liteos_m_adapter/hks_safe_compare_key_test.c new file mode 100644 index 0000000000000000000000000000000000000000..a7f57103b475fc161fccb50c546912ddf82019d9 --- /dev/null +++ b/security_lite/huks/liteos_m_adapter/hks_safe_compare_key_test.c @@ -0,0 +1,211 @@ +/* + * Copyright (C) 2021 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 _CUT_AUTHENTICATE_ + +#include "hks_safe_compare_key_test.h" + +#include +#include "hi_watchdog.h" +#include "hks_api.h" +#include "hks_param.h" +#include "hks_test_api_performance.h" +#include "hks_test_common.h" +#include "hks_test_file_operator.h" +#include "hks_test_log.h" +#include "hks_test_mem.h" + +static const char *g_storePath = "/storage/"; +static const char *g_testOne = "TestOne"; +static const char *g_testTwo = "TestTwo"; + +/* + * @tc.register: register a test suit named "CalcMultiTest" + * @param: test subsystem name + * @param: c_example module name + * @param: CalcMultiTest test suit name + */ +LITE_TEST_SUIT(security, securityData, HksSafeCompareKeyTest); + +/** + * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp" + * @return: true——setup success + */ +static BOOL HksSafeCompareKeyTestSetUp() +{ + LiteTestPrint("setup\n"); + hi_watchdog_disable(); + TEST_ASSERT_TRUE(HksInitialize() == 0); + return TRUE; +} + +/** + * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown" + * @return: true——teardown success + */ +static BOOL HksSafeCompareKeyTestTearDown() +{ + LiteTestPrint("tearDown\n"); + hi_watchdog_enable(); + return TRUE; +} + + +static const struct HksTestGenKeyParams g_testGenKeyParams[] = { + /* x25519: tee sign/verify */ + { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, + { + true, + true, HKS_ALG_X25519, + true, HKS_CURVE25519_KEY_SIZE_256, + true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY, + false, 0, + false, 0, + false, 0, + false, 0 + }, + { false, 0 }, + }, +}; + +static int32_t SafeTestGenerateKey(struct HksBlob *keyAlias) +{ + uint32_t index = 0; + uint32_t performTimes = 1; + + struct HksParamSet *paramSet = NULL; + struct GenerateKeyParamSetStructure paramStruct = { + ¶mSet, + g_testGenKeyParams[index].paramSetParams.paramSetExist, + g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg, + g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize, + g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose, + g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest, + g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding, + g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode, + g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag, + g_testGenKeyParams[index].paramSetParams.keyStorageFlag + }; + int32_t ret = TestConstructGenerateKeyParamSet(¶mStruct); + HKS_TEST_ASSERT(ret == 0); + + struct HksParamSet *paramSetOut = NULL; + ret = TestConstructGenerateKeyParamSetOut(¶mSetOut, + g_testGenKeyParams[index].paramSetParamsOut.paramSetExist, + g_testGenKeyParams[index].paramSetParamsOut.paramSetSize); + HKS_TEST_ASSERT(ret == 0); + + ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes); + if (ret != g_testGenKeyParams[index].expectResult) { + HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret); + } + HKS_TEST_ASSERT(ret == g_testGenKeyParams[index].expectResult); + + if (ret == g_testGenKeyParams[index].expectResult) { + ret = 0; + } + HksFreeParamSet(¶mSet); + HksFreeParamSet(¶mSetOut); + return ret; +} + + +static struct HksBlob g_storageImageBuffer; + +static int32_t GetKeyOffsetByKeyAlias(const struct HksBlob *keyAlias, uint32_t *keyOffset) +{ + struct HksBlob storageBuf = g_storageImageBuffer; + + /* 1. get imageBuffer total Len */ + struct HksStoreHeaderInfo *keyInfoHead = (struct HksStoreHeaderInfo *)storageBuf.data; + uint32_t keyCount = keyInfoHead->keyCount; + uint32_t totalLen = keyInfoHead->totalLen; + + /* 2. traverse imageBuffer to search for keyAlias */ + uint32_t offset = sizeof(*keyInfoHead); + for (uint16_t i = 0; i < keyCount; ++i) { + uint8_t *tmpBuf = storageBuf.data + offset; + struct HksStoreKeyInfo *keyInfo = (struct HksStoreKeyInfo *)tmpBuf; + if (keyInfo->aliasSize == keyAlias->size) { + if (HksMemCmp(keyAlias->data, tmpBuf + sizeof(*keyInfo), keyAlias->size) == 0) { + *keyOffset = offset; + return HKS_SUCCESS; + } + } + + offset += keyInfo->keyInfoLen; + } + + return HKS_ERROR_NOT_EXIST; +} + + +static int32_t CompareKeyData(struct HksBlob *keyAliasOne, struct HksBlob *keyAliasTwo) +{ + uint32_t sizeOne = HksTestFileSize(g_storePath, "hks_keystore"); + if (sizeOne == 0) { + return 0; + } + uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne); + if (bufOne == NULL) { + return HKS_ERROR_MALLOC_FAIL; + } + + uint32_t sizeRead = HksTestFileRead(g_storePath, "hks_keystore", 0, bufOne, sizeOne); + TEST_ASSERT_TRUE(sizeRead > 0); + + g_storageImageBuffer.data = bufOne; + g_storageImageBuffer.size = sizeOne; + + int32_t offset1; + int ret = GetKeyOffsetByKeyAlias(keyAliasOne, &offset1); + TEST_ASSERT_TRUE(ret == 0); + + struct HksStoreKeyInfo *keyInfo1 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset1); + + int32_t offset2; + ret = GetKeyOffsetByKeyAlias(keyAliasTwo, &offset2); + TEST_ASSERT_TRUE(ret == 0); + + struct HksStoreKeyInfo *keyInfo2 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset2); + + TEST_ASSERT_TRUE(keyInfo1->keyInfoLen == keyInfo2->keyInfoLen); + + ret = memcmp(keyInfo1, keyInfo2, keyInfo1->keyInfoLen); + HksTestFree(bufOne); + return ret; +} + +/** + * @tc.name: HksSafeCompareKeyTest.HksSafeCompareKeyTest001 + * @tc.desc: The static function will return true; + * @tc.type: FUNC + */ +LITE_TEST_CASE(HksSafeCompareKeyTest, HksSafeCompareKeyTest001, Level1) +{ + struct HksBlob keyAliasOne = { strlen(g_testOne), (uint8_t *)g_testOne }; + int32_t ret = SafeTestGenerateKey(&keyAliasOne); + HKS_TEST_ASSERT(ret == 0); + struct HksBlob keyAliasTwo = { strlen(g_testTwo), (uint8_t *)g_testTwo }; + ret = SafeTestGenerateKey(&keyAliasTwo); + HKS_TEST_ASSERT(ret == 0); + + ret = CompareKeyData(&keyAliasOne, &keyAliasTwo); + HKS_TEST_ASSERT(ret != 0); + TEST_ASSERT_TRUE(ret != 0); +} +RUN_TEST_SUITE(HksSafeCompareKeyTest); + +#endif /* _CUT_AUTHENTICATE_ */