1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include "hks_generate_key_test.h"
19
20 #include "hks_api.h"
21 #include "hks_param.h"
22 #include "hks_test_api_performance.h"
23 #include "hks_test_common.h"
24 #include "hks_test_log.h"
25
26 using namespace testing::ext;
27 namespace {
28 #ifndef _CUT_AUTHENTICATE_
29 class HksGenerateKeyTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32
33 static void TearDownTestCase(void);
34
35 void SetUp();
36
37 void TearDown();
38 };
39
SetUpTestCase(void)40 void HksGenerateKeyTest::SetUpTestCase(void)
41 {
42 }
43
TearDownTestCase(void)44 void HksGenerateKeyTest::TearDownTestCase(void)
45 {
46 }
47
SetUp()48 void HksGenerateKeyTest::SetUp()
49 {
50 EXPECT_EQ(HksInitialize(), 0);
51 }
52
TearDown()53 void HksGenerateKeyTest::TearDown()
54 {
55 }
56
57 const int DEFAULT_X25519_PARAM_SET_OUT = 104;
58
59 const struct HksTestGenKeyParams g_testGenKeyParams[] = {
60 /* x25519: ree sign/verify */
61 { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, {
62 true,
63 true, HKS_ALG_X25519,
64 true, HKS_CURVE25519_KEY_SIZE_256,
65 true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY,
66 false, 0,
67 false, 0,
68 false, 0,
69 true, HKS_STORAGE_TEMP },
70 { true, DEFAULT_X25519_PARAM_SET_OUT },
71 },
72 };
73
74 /**
75 * @tc.name: HksGenerateKeyTest.HksGenerateKeyTest001
76 * @tc.desc: The static function will return true;
77 * @tc.type: FUNC
78 */
79 HWTEST_F(HksGenerateKeyTest, HksGenerateKeyTest001, TestSize.Level1)
80 {
81 uint32_t times = 1;
82 uint32_t index = 0;
83 uint32_t performTimes = 1;
84 struct HksBlob *keyAlias = NULL;
85 int32_t ret = TestConstuctBlob(&keyAlias,
86 g_testGenKeyParams[index].keyAliasParams.blobExist,
87 g_testGenKeyParams[index].keyAliasParams.blobSize,
88 g_testGenKeyParams[index].keyAliasParams.blobDataExist,
89 g_testGenKeyParams[index].keyAliasParams.blobDataSize);
90 HKS_TEST_ASSERT(ret == 0);
91
92 struct HksParamSet *paramSet = NULL;
93 struct GenerateKeyParamSetStructure paramStruct = { ¶mSet,
94 g_testGenKeyParams[index].paramSetParams.paramSetExist,
95 g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg,
96 g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize,
97 g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose,
98 g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest,
99 g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding,
100 g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode,
101 g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag,
102 g_testGenKeyParams[index].paramSetParams.keyStorageFlag };
103 ret = TestConstructGenerateKeyParamSet(¶mStruct);
104 HKS_TEST_ASSERT(ret == 0);
105
106 struct HksParamSet *paramSetOut = NULL;
107 ret = TestConstructGenerateKeyParamSetOut(¶mSetOut,
108 g_testGenKeyParams[index].paramSetParamsOut.paramSetExist,
109 g_testGenKeyParams[index].paramSetParamsOut.paramSetSize);
110 HKS_TEST_ASSERT(ret == 0);
111
112 ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes);
113 if (ret != g_testGenKeyParams[index].expectResult) {
114 HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret);
115 }
116 HKS_TEST_ASSERT(ret == g_testGenKeyParams[index].expectResult);
117
118 if ((ret == HKS_SUCCESS) &&
119 !(g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag == true) &&
120 (g_testGenKeyParams[index].paramSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
121 HKS_TEST_ASSERT(HksDeleteKey(keyAlias, NULL) == 0);
122 }
123 TestFreeBlob(&keyAlias);
124 HksFreeParamSet(¶mSet);
125 HksFreeParamSet(¶mSetOut);
126 HKS_TEST_LOG_I("[%u]TestGenerateKey, Testcase_GenerateKey_[%03u] pass!", times, g_testGenKeyParams[index].testId);
127 ASSERT_TRUE(ret == 0);
128 }
129 #endif /* _CUT_AUTHENTICATE_ */
130 }