• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include "hks_api.h"
23 #include "hks_param.h"
24 #include "hks_test_api_performance.h"
25 #include "hks_test_common.h"
26 #include "hks_test_log.h"
27 #include "cmsis_os2.h"
28 #include "ohos_types.h"
29 
30 #include <unistd.h>
31 #define DEFAULT_X25519_PARAM_SET_OUT 104
32 #define TEST_TASK_STACK_SIZE      0x2000
33 #define WAIT_TO_TEST_DONE         4
34 
35 static osPriority_t g_setPriority;
36 
37 /*
38  * @tc.register: register a test suit named "CalcMultiTest"
39  * @param: test subsystem name
40  * @param: c_example module name
41  * @param: CalcMultiTest test suit name
42  */
43 LITE_TEST_SUIT(security, securityData, HksGenerateKeyTest);
44 
ExecHksInitialize(void const * argument)45 static void ExecHksInitialize(void const *argument)
46 {
47     LiteTestPrint("HksInitialize Begin!\n");
48     TEST_ASSERT_TRUE(HksInitialize() == 0);
49     LiteTestPrint("HksInitialize End!\n");
50     osThreadExit();
51 }
52 
53 /**
54  * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
55  * @return: true——setup success
56  */
HksGenerateKeyTestSetUp()57 static BOOL HksGenerateKeyTestSetUp()
58 {
59     LiteTestPrint("setup\n");
60     osThreadId_t id;
61     osThreadAttr_t attr;
62     g_setPriority = osPriorityAboveNormal6;
63     attr.name = "test";
64     attr.attr_bits = 0U;
65     attr.cb_mem = NULL;
66     attr.cb_size = 0U;
67     attr.stack_mem = NULL;
68     attr.stack_size = TEST_TASK_STACK_SIZE;
69     attr.priority = g_setPriority;
70     id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
71     sleep(WAIT_TO_TEST_DONE);
72     LiteTestPrint("HksGenerateKeyTestSetUp End2!\n");
73     return TRUE;
74 }
75 
76 /**
77  * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
78  * @return: true——teardown success
79  */
HksGenerateKeyTestTearDown()80 static BOOL HksGenerateKeyTestTearDown()
81 {
82     LiteTestPrint("tearDown\n");
83     return TRUE;
84 }
85 
86 
87 static const struct HksTestGenKeyParams g_testGenKeyParams[] = {
88     /* x25519: ree sign/verify */
89     { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, {
90         true,
91         true, HKS_ALG_X25519,
92         true, HKS_CURVE25519_KEY_SIZE_256,
93         true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY,
94         false, 0,
95         false, 0,
96         false, 0,
97         true, HKS_STORAGE_TEMP },
98         { true, DEFAULT_X25519_PARAM_SET_OUT },
99     },
100 };
101 
ExecHksGenerateKeyTest001(void const * argument)102 static void ExecHksGenerateKeyTest001(void const *argument)
103 {
104     LiteTestPrint("HksGenerateKeyTest001 Begin!\n");
105 
106     uint32_t times = 1;
107     uint32_t index = 0;
108     uint32_t performTimes = 1;
109     struct HksBlob *keyAlias = NULL;
110     int32_t ret = TestConstuctBlob(&keyAlias,
111         g_testGenKeyParams[index].keyAliasParams.blobExist,
112         g_testGenKeyParams[index].keyAliasParams.blobSize,
113         g_testGenKeyParams[index].keyAliasParams.blobDataExist,
114         g_testGenKeyParams[index].keyAliasParams.blobDataSize);
115     TEST_ASSERT_TRUE(ret == 0);
116 
117     struct HksParamSet *paramSet = NULL;
118     struct GenerateKeyParamSetStructure paramStruct = { &paramSet,
119         g_testGenKeyParams[index].paramSetParams.paramSetExist,
120         g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg,
121         g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize,
122         g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose,
123         g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest,
124         g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding,
125         g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode,
126         g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag,
127         g_testGenKeyParams[index].paramSetParams.keyStorageFlag };
128     ret = TestConstructGenerateKeyParamSet(&paramStruct);
129     TEST_ASSERT_TRUE(ret == 0);
130 
131     struct HksParamSet *paramSetOut = NULL;
132     ret = TestConstructGenerateKeyParamSetOut(&paramSetOut,
133         g_testGenKeyParams[index].paramSetParamsOut.paramSetExist,
134         g_testGenKeyParams[index].paramSetParamsOut.paramSetSize);
135     TEST_ASSERT_TRUE(ret == 0);
136 
137     ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes);
138     if (ret != g_testGenKeyParams[index].expectResult) {
139         HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret);
140     }
141     TEST_ASSERT_TRUE(ret == g_testGenKeyParams[index].expectResult);
142 
143     if ((ret == HKS_SUCCESS) &&
144         !(g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag == true) &&
145         (g_testGenKeyParams[index].paramSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
146         HKS_TEST_ASSERT(HksDeleteKey(keyAlias, NULL) == 0);
147     }
148     TestFreeBlob(&keyAlias);
149     HksFreeParamSet(&paramSet);
150     HksFreeParamSet(&paramSetOut);
151     HKS_TEST_LOG_I("[%u]TestGenerateKey, Testcase_GenerateKey_[%03u] pass!", times, g_testGenKeyParams[index].testId);
152     TEST_ASSERT_TRUE(ret == 0);
153 
154     LiteTestPrint("HksGenerateKeyTest001 End!\n");
155     osThreadExit();
156 }
157 
158 /**
159  * @tc.name: HksGenerateKeyTest.HksGenerateKeyTest001
160  * @tc.desc: The static function will return true;
161  * @tc.type: FUNC
162  */
LITE_TEST_CASE(HksGenerateKeyTest,HksGenerateKeyTest001,Level1)163 LITE_TEST_CASE(HksGenerateKeyTest, HksGenerateKeyTest001, Level1)
164 {
165     osThreadId_t id;
166     osThreadAttr_t attr;
167     g_setPriority = osPriorityAboveNormal6;
168     attr.name = "test";
169     attr.attr_bits = 0U;
170     attr.cb_mem = NULL;
171     attr.cb_size = 0U;
172     attr.stack_mem = NULL;
173     attr.stack_size = TEST_TASK_STACK_SIZE;
174     attr.priority = g_setPriority;
175     id = osThreadNew((osThreadFunc_t)ExecHksGenerateKeyTest001, NULL, &attr);
176     sleep(WAIT_TO_TEST_DONE);
177     LiteTestPrint("HksGenerateKeyTest001 End2!\n");
178 }
179 
180 RUN_TEST_SUITE(HksGenerateKeyTest);
181 #endif /* _CUT_AUTHENTICATE_ */
182 
183