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 #include "hi_watchdog.h"
16 #include <hctest.h>
17
18 #include "hks_derive_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 #define DEFAULT_DERIVE_SIZE 32
27 #define DEFAULT_INFO_SIZE 55
28 #define DEFAULT_SALT_SIZE 16
29
30 /*
31 * @tc.register: register a test suit named "CalcMultiTest"
32 * @param: test subsystem name
33 * @param: c_example module name
34 * @param: CalcMultiTest test suit name
35 */
36 LITE_TEST_SUIT(husk, huks_lite, HksDeriveTest);
37
38 /**
39 * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
40 * @return: true——setup success
41 */
HksDeriveTestSetUp()42 static BOOL HksDeriveTestSetUp()
43 {
44 LiteTestPrint("setup\n");
45 hi_watchdog_disable();
46 TEST_ASSERT_TRUE(HksInitialize() == 0);
47 return TRUE;
48 }
49
50 /**
51 * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
52 * @return: true——teardown success
53 */
HksDeriveTestTearDown()54 static BOOL HksDeriveTestTearDown()
55 {
56 LiteTestPrint("tearDown\n");
57 hi_watchdog_enable();
58 return TRUE;
59 }
60
61
62 static const struct HksTestDeriveParams g_testDeriveParams[] = {
63 /* hkdf-sha256-salt-info */
64 { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
65 {
66 true, /* genKey params */
67 true, HKS_ALG_AES,
68 true, HKS_AES_KEY_SIZE_256,
69 true, HKS_KEY_PURPOSE_DERIVE,
70 true, HKS_DIGEST_SHA256,
71 false, 0,
72 false, 0,
73 false, 0 },
74 { 0 },
75 {
76 true, /* derive params */
77 true, HKS_ALG_HKDF,
78 true, HKS_KEY_PURPOSE_DERIVE,
79 true, HKS_DIGEST_SHA256,
80 false, 0,
81 true, DEFAULT_SALT_SIZE,
82 true, DEFAULT_INFO_SIZE,
83 false, true },
84 {
85 true, DEFAULT_DERIVE_SIZE, true, DEFAULT_DERIVE_SIZE },
86 {
87 false, 0, false, 0 }
88 },
89
90 /* local: hkdf-sha256-salt-info */
91 { 1, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
92 {
93 true, /* genKey params */
94 true, HKS_ALG_AES,
95 true, HKS_AES_KEY_SIZE_256,
96 true, HKS_KEY_PURPOSE_DERIVE,
97 true, HKS_DIGEST_SHA256,
98 false, 0,
99 false, 0,
100 true, HKS_STORAGE_TEMP },
101 { 0 },
102 {
103 true, /* derive params */
104 true, HKS_ALG_HKDF,
105 true, HKS_KEY_PURPOSE_DERIVE,
106 true, HKS_DIGEST_SHA256,
107 false, 0,
108 true, DEFAULT_SALT_SIZE,
109 true, DEFAULT_INFO_SIZE,
110 true, false },
111 {
112 true, DEFAULT_DERIVE_SIZE, true, DEFAULT_DERIVE_SIZE },
113 {
114 true, DEFAULT_LOCAL_KEY_SIZE, true, DEFAULT_LOCAL_KEY_SIZE }
115 },
116 };
117
DeriveKey(const struct HksTestDeriveParamSet * deriveParamSetParams,const struct HksBlob * masterKey,struct HksBlob * derivedKey,struct HksBlob ** saltData,struct HksBlob ** infoData)118 static int32_t DeriveKey(const struct HksTestDeriveParamSet *deriveParamSetParams, const struct HksBlob *masterKey,
119 struct HksBlob *derivedKey, struct HksBlob **saltData, struct HksBlob **infoData)
120 {
121 struct HksParamSet *deriveParamSet = NULL;
122 uint32_t saltSize = deriveParamSetParams->saltSize;
123 uint32_t infoSize = deriveParamSetParams->infoSize;
124 if (saltSize != 0) {
125 HKS_TEST_ASSERT(TestConstuctBlob(saltData, true, saltSize, true, saltSize) == 0);
126 }
127 if (infoSize != 0) {
128 HKS_TEST_ASSERT(TestConstuctBlob(infoData, true, infoSize, true, infoSize) == 0);
129 }
130 struct TestDeriveParamSetStructure paramStruct = {
131 &deriveParamSet,
132 deriveParamSetParams->paramSetExist,
133 deriveParamSetParams->setAlg, deriveParamSetParams->alg,
134 deriveParamSetParams->setPurpose, deriveParamSetParams->purpose,
135 deriveParamSetParams->setDigest, deriveParamSetParams->digest,
136 deriveParamSetParams->setIteration, deriveParamSetParams->iteration,
137 deriveParamSetParams->setSalt, *saltData,
138 deriveParamSetParams->setInfo, *infoData,
139 deriveParamSetParams->setIsKeyAlias, deriveParamSetParams->isKeyAlias
140 };
141 int32_t ret = TestConstructDeriveParamSet(¶mStruct);
142 HKS_TEST_ASSERT(ret == 0);
143
144 ret = HksDeriveKeyRun(deriveParamSet, masterKey, derivedKey, 1);
145 HksFreeParamSet(&deriveParamSet);
146 return ret;
147 }
148
BaseTestDerive(uint32_t index)149 static int32_t BaseTestDerive(uint32_t index)
150 {
151 /* 1. generate key */
152 struct HksBlob *keyAlias = NULL;
153 int32_t ret;
154 if (g_testDeriveParams[index].genKeyParamSetParams.setKeyStorageFlag &&
155 (g_testDeriveParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
156 ret = GenerateLocalRandomKey(&keyAlias, &g_testDeriveParams[index].localKeyParams);
157 } else {
158 if (g_testDeriveParams[index].keyAliasParams.blobExist) {
159 ret = GenerateKey(&keyAlias, &g_testDeriveParams[index].keyAliasParams,
160 &g_testDeriveParams[index].genKeyParamSetParams, NULL);
161 } else {
162 ret = TestConstuctBlob(&keyAlias,
163 g_testDeriveParams[index].masterKeyParams.blobExist,
164 g_testDeriveParams[index].masterKeyParams.blobSize,
165 g_testDeriveParams[index].masterKeyParams.blobDataExist,
166 g_testDeriveParams[index].masterKeyParams.blobDataSize);
167 }
168 }
169 TEST_ASSERT_TRUE(ret == 0);
170
171 /* 2. derive */
172 struct HksBlob *derivedKey = NULL;
173 ret = TestConstuctBlob(&derivedKey,
174 g_testDeriveParams[index].derivedKeyParams.blobExist,
175 g_testDeriveParams[index].derivedKeyParams.blobSize,
176 g_testDeriveParams[index].derivedKeyParams.blobDataExist,
177 g_testDeriveParams[index].derivedKeyParams.blobDataSize);
178 TEST_ASSERT_TRUE(ret == 0);
179
180 struct HksBlob *saltData = NULL;
181 struct HksBlob *infoData = NULL;
182 ret = DeriveKey(&g_testDeriveParams[index].deriveParamSetParams, keyAlias, derivedKey, &saltData, &infoData);
183 if (ret != g_testDeriveParams[index].expectResult) {
184 HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testDeriveParams[index].testId, ret);
185 }
186 TEST_ASSERT_TRUE(ret == g_testDeriveParams[index].expectResult);
187
188 /* 3. delete key */
189 if (!(g_testDeriveParams[index].genKeyParamSetParams.setKeyStorageFlag &&
190 (g_testDeriveParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) &&
191 (g_testDeriveParams[index].keyAliasParams.blobExist)) {
192 TEST_ASSERT_TRUE(HksDeleteKey(keyAlias, NULL) == 0);
193 }
194 TestFreeBlob(&keyAlias);
195 TestFreeBlob(&derivedKey);
196 TestFreeBlob(&saltData);
197 TestFreeBlob(&infoData);
198
199 return ret;
200 }
201
202 #ifndef _CUT_AUTHENTICATE_
203 /**
204 * @tc.name: HksDeriveTest.HksDeriveTest001
205 * @tc.desc: The static function will return true;
206 * @tc.type: FUNC
207 */
LITE_TEST_CASE(HksDeriveTest,HksDeriveTest001,Level1)208 LITE_TEST_CASE(HksDeriveTest, HksDeriveTest001, Level1)
209 {
210 int32_t ret = BaseTestDerive(0);
211 TEST_ASSERT_TRUE(ret == 0);
212 }
213 #endif /* _CUT_AUTHENTICATE_ */
214
215 /**
216 * @tc.name: HksDeriveTest.HksDeriveTest002
217 * @tc.desc: The static function will return true;
218 * @tc.type: FUNC
219 */
LITE_TEST_CASE(HksDeriveTest,HksDeriveTest002,Level1)220 LITE_TEST_CASE(HksDeriveTest, HksDeriveTest002, Level1)
221 {
222 int32_t ret = BaseTestDerive(1);
223 TEST_ASSERT_TRUE(ret == 0);
224 }
225 RUN_TEST_SUITE(HksDeriveTest);
226