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 #ifndef _CUT_AUTHENTICATE_
17
18 #include "hks_generate_key_test.h"
19
20 #include <hctest.h>
21 #include "hi_watchdog.h"
22 #include "hks_api.h"
23 #include "hks_param.h"
24 #include "hks_test_adapt_for_de.h"
25 #include "hks_test_api_performance.h"
26 #include "hks_test_common.h"
27 #include "hks_test_log.h"
28
29 #define DEFAULT_X25519_PARAM_SET_OUT 104
30
31 /*
32 * @tc.register: register a test suit named "CalcMultiTest"
33 * @param: test subsystem name
34 * @param: c_example module name
35 * @param: CalcMultiTest test suit name
36 */
37 LITE_TEST_SUIT(husk, huks_lite, HksGenerateKeyTest);
38
39 /**
40 * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
41 * @return: true——setup success
42 */
HksGenerateKeyTestSetUp()43 static BOOL HksGenerateKeyTestSetUp()
44 {
45 LiteTestPrint("setup\n");
46 hi_watchdog_disable();
47 TEST_ASSERT_TRUE(HksInitialize() == 0);
48 return TRUE;
49 }
50
51 /**
52 * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
53 * @return: true——teardown success
54 */
HksGenerateKeyTestTearDown()55 static BOOL HksGenerateKeyTestTearDown()
56 {
57 LiteTestPrint("tearDown\n");
58 hi_watchdog_enable();
59 return TRUE;
60 }
61
62
63 static const struct HksTestGenKeyParams g_testGenKeyParams[] = {
64 /* x25519: ree sign/verify */
65 { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, {
66 true,
67 true, HKS_ALG_X25519,
68 true, HKS_CURVE25519_KEY_SIZE_256,
69 true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY,
70 false, 0,
71 false, 0,
72 false, 0,
73 true, HKS_STORAGE_TEMP },
74 { true, DEFAULT_X25519_PARAM_SET_OUT },
75 },
76 };
77
78 /**
79 * @tc.name: HksGenerateKeyTest.HksGenerateKeyTest001
80 * @tc.desc: The static function will return true;
81 * @tc.type: FUNC
82 */
LITE_TEST_CASE(HksGenerateKeyTest,HksGenerateKeyTest001,Level1)83 LITE_TEST_CASE(HksGenerateKeyTest, HksGenerateKeyTest001, Level1)
84 {
85 uint32_t times = 1;
86 uint32_t index = 0;
87 uint32_t performTimes = 1;
88 struct HksBlob *keyAlias = NULL;
89 int32_t ret = TestConstuctBlob(&keyAlias,
90 g_testGenKeyParams[index].keyAliasParams.blobExist,
91 g_testGenKeyParams[index].keyAliasParams.blobSize,
92 g_testGenKeyParams[index].keyAliasParams.blobDataExist,
93 g_testGenKeyParams[index].keyAliasParams.blobDataSize);
94 TEST_ASSERT_TRUE(ret == 0);
95
96 struct HksParamSet *paramSet = NULL;
97 struct GenerateKeyParamSetStructure paramStruct = { ¶mSet,
98 g_testGenKeyParams[index].paramSetParams.paramSetExist,
99 g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg,
100 g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize,
101 g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose,
102 g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest,
103 g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding,
104 g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode,
105 g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag,
106 g_testGenKeyParams[index].paramSetParams.keyStorageFlag };
107 ret = TestConstructGenerateKeyParamSet(¶mStruct);
108 TEST_ASSERT_TRUE(ret == 0);
109
110 struct HksParamSet *paramSetOut = NULL;
111 ret = TestConstructGenerateKeyParamSetOut(¶mSetOut,
112 g_testGenKeyParams[index].paramSetParamsOut.paramSetExist,
113 g_testGenKeyParams[index].paramSetParamsOut.paramSetSize);
114 TEST_ASSERT_TRUE(ret == 0);
115
116 ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes);
117 if (ret != g_testGenKeyParams[index].expectResult) {
118 HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret);
119 }
120 TEST_ASSERT_TRUE(ret == g_testGenKeyParams[index].expectResult);
121
122 if ((ret == HKS_SUCCESS) &&
123 !(g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag) &&
124 (g_testGenKeyParams[index].paramSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
125 HKS_TEST_ASSERT(HksDeleteKeyForDe(keyAlias, NULL) == 0);
126 }
127 TestFreeBlob(&keyAlias);
128 HksFreeParamSet(¶mSet);
129 HksFreeParamSet(¶mSetOut);
130 HKS_TEST_LOG_I("[%u]TestGenerateKey, Testcase_GenerateKey_[%03u] pass!", times, g_testGenKeyParams[index].testId);
131 TEST_ASSERT_TRUE(ret == 0);
132 }
133
134 RUN_TEST_SUITE(HksGenerateKeyTest);
135 #endif /* _CUT_AUTHENTICATE_ */
136
137