提交 d78f19d7 编写于 作者: O openharmony_ci 提交者: Gitee

!581 create hks_L2 testcase_0913

Merge pull request !581 from 胡吉翔/security_20210913
......@@ -90,6 +90,8 @@ lite_component("acts_component") {
"//test/xts/acts/startup_lite/syspara_posix:ActsParameterTest",
#"//test/xts/acts/security_lite/huks_posix:ActsSecurityDataTest",
"//test/xts/acts/security_lite/permission_posix/pms:ActsPMSTest",
#"//test/xts/acts/multimedia_lite/camera_lite_posix/camera_native:ActsMediaCameraTest",
#"//test/xts/acts/multimedia_lite/media_lite_posix/player_native:ActsMediaPlayerTest",
#"//test/xts/acts/multimedia_lite/media_lite_posix/recorder_native:ActsMediaRecorderTest",
......
......@@ -17,10 +17,11 @@ group("security") {
testonly = true
if (is_standard_system) {
deps = [
"authentication_standard/deviceauth_p2p:DeviceauthP2PFunctionCPPTest",
"permission/permissionkitcpp:PermissionKitFunctionCPPTest",
"permission/permissionkitcpp:PermissionKitPerformanceCPPTest",
"permission/permissionkitcpp:PermissionKitReliabilityCPPTest",
# "authentication_standard/deviceauth_p2p:DeviceauthP2PFunctionCPPTest",
# "permission/permissionkitcpp:PermissionKitFunctionCPPTest",
# "permission/permissionkitcpp:PermissionKitPerformanceCPPTest",
# "permission/permissionkitcpp:PermissionKitReliabilityCPPTest",
"dataprotect/huks_standard_adapter:huks_std_function_test",
]
} else {
deps =
......
# 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("//build/test.gni")
import("//test/xts/tools/build/suite.gni")
#module_output_path = "hits/huks_standard"
########################################################
config("module_private_config") {
visibility = [ ":*" ]
include_dirs = [
"//utils/native/base/include",
"//third_party/bounds_checking_function/include",
#"unittest/include",
#"include",
"//test/xts/acts/security_lite/huks/common/include",
]
}
########################################################
ohos_moduletest_suite("huks_std_function_test") {
#subsystem_name = "acts"
#part_name = "huks_standard"
#module_output_path = subsystem_name + "/" + part_name
#module_output_path = module_output_path
configs = [ ":module_private_config" ]
cflags_cc = [ "-DHILOG_ENABLE" ]
defines = [ "_STANDARD_SYSTEM_" ]
sources = [
"src/hks_aes_test.cpp",
"src/hks_agreement_test.cpp",
"src/hks_bn_exp_mod_test.cpp",
"src/hks_cipher_test.cpp",
"src/hks_curve25519_test.cpp",
"src/hks_delete_test.cpp",
"src/hks_derive_test.cpp",
"src/hks_exist_test.cpp",
"src/hks_generate_key_test.cpp",
"src/hks_generate_random_test.cpp",
"src/hks_hash_test.cpp",
"src/hks_mac_test.cpp",
"src/hks_modify_key_test.cpp",
"src/hks_others_test.cpp",
"src/hks_safe_cipher_key_test.cpp",
"src/hks_safe_compare_key_test.cpp",
#"src/hks_test_api_performance.c",
#"src/hks_test_cipher.c",
#"src/hks_test_common.c",
#"src/hks_test_mem.c",
#"src/hks_test_aes.c",
#"src/hks_test_curve25519.c",
#"src/hks_test_file_operator.c",
]
include_dirs = [
"//utils/native/base/include",
"//third_party/bounds_checking_function/include",
"include",
]
deps = [
"//base/security/huks/frameworks/huks_standard/main:huks_standard_frameworks",
"//base/security/huks/interfaces/innerkits/huks_standard/main:libhukssdk",
"//test/xts/acts/security_lite/huks/common:huks_new_test_common",
"//third_party/bounds_checking_function:libsec_static",
"//utils/native/base:utils",
]
}
group("Huksstandardtest") {
testonly = true
deps = [ ":huks_std_function_test" ]
}
{
"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/ActsSecurityHuksstdtest.bin"
}
}
\ No newline at end of file
/*
* 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 <gtest/gtest.h>
#include "hks_aes_test.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);
}
}
/*
* 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 <gtest/gtest.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"
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(&paramStruct);
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_ */
}
/*
* 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 <gtest/gtest.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"
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
/*
* 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 <gtest/gtest.h>
#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
/*
* 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 <gtest/gtest.h>
#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
/*
* 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 <gtest/gtest.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"
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
/*
* 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 <gtest/gtest.h>
#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(&paramStruct);
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
/*
* 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 <gtest/gtest.h>
#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
/*
* 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 <gtest/gtest.h>
#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 = { &paramSet,
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(&paramStruct);
HKS_TEST_ASSERT(ret == 0);
struct HksParamSet *paramSetOut = NULL;
ret = TestConstructGenerateKeyParamSetOut(&paramSetOut,
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(&paramSet);
HksFreeParamSet(&paramSetOut);
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
/*
* 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 <gtest/gtest.h>
#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
/*
* 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 <gtest/gtest.h>
#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(&paramSet,
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(&paramSet);
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_ */
}
/*
* 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 <gtest/gtest.h>
#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(&paramStructTrue);
} else {
struct TestMacParamSetStructure paramStructFalse = {
&macParamSet,
macParamSetParams->paramSetExist,
macParamSetParams->setPurpose, macParamSetParams->purpose,
macParamSetParams->setDigest, macParamSetParams->digest, false, false
};
ret = TestConstructMacParamSet(&paramStructFalse);
}
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_ */
}
/*
* 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 <gtest/gtest.h>
#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;
#ifdef _STANDARD_SYSTEM_
const char *g_storePath = "/data/data/maindata/+0+0+0+0/key";
#endif
#ifdef _LITEONE_SYSTEM_
const char *g_storePath = "/storage/maindata/hks_client/key";
#endif
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 = {
&paramSet,
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(&paramStruct);
HKS_TEST_ASSERT(ret == 0);
struct HksParamSet *paramSetOut = NULL;
if (genKeyParamSetParamsOut != NULL) {
ret = TestConstructGenerateKeyParamSetOut(&paramSet,
genKeyParamSetParamsOut->paramSetExist, genKeyParamSetParamsOut->paramSetSize);
HKS_TEST_ASSERT(ret == 0);
}
ret = HksGenerateKey(keyAlias, paramSet, paramSetOut);
HKS_TEST_ASSERT(ret == 0);
HksFreeParamSet(&paramSet);
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_ */
}
\ No newline at end of file
/*
* 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 <gtest/gtest.h>
#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
/*
* 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 <gtest/gtest.h>
#include "hks_safe_cipher_key_test.h"
#include "hks_api.h"
#include "hks_param.h"
#include "hks_test_api_performance.h"
#include "hks_test_curve25519.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 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()
{
}
#ifdef _STANDARD_SYSTEM_
const char *g_storePath = "/data/data/maindata/+0+0+0+0/key";
#endif
#ifdef _LITEONE_SYSTEM_
const char *g_storePath = "/storage/maindata/hks_client/key";
#endif
const char *g_testEd25519 = "test_ed25519";
static int32_t CompareTwoKey(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, 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
/*
* 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 <gtest/gtest.h>
#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()
{
}
#ifdef _STANDARD_SYSTEM_
const char *g_storePath = "/data/data/maindata/+0+0+0+0/key";
#endif
#ifdef _LITEONE_SYSTEM_
const char *g_storePath = "/storage/maindata/hks_client/key";
#endif
const char *g_testOne = "TestOne";
const char *g_testTwo = "TestTwo";
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 = {
&paramSet,
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(&paramStruct);
HKS_TEST_ASSERT(ret == 0);
struct HksParamSet *paramSetOut = NULL;
ret = TestConstructGenerateKeyParamSetOut(&paramSetOut,
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(&paramSet);
HksFreeParamSet(&paramSetOut);
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册