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_mac_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 #include "securec.h"
27
28 using namespace testing::ext;
29 namespace {
30 class HksMacTest : public testing::Test {
31 public:
32 static void SetUpTestCase(void);
33
34 static void TearDownTestCase(void);
35
36 void SetUp();
37
38 void TearDown();
39 };
40
SetUpTestCase(void)41 void HksMacTest::SetUpTestCase(void)
42 {
43 }
44
TearDownTestCase(void)45 void HksMacTest::TearDownTestCase(void)
46 {
47 }
48
SetUp()49 void HksMacTest::SetUp()
50 {
51 EXPECT_EQ(HksInitialize(), 0);
52 }
53
TearDown()54 void HksMacTest::TearDown()
55 {
56 }
57
58 const int HKS_TEST_MAC_REE_KEY_SIZE_32 = 32;
59 const int HKS_DEFAULT_MAC_SRCDATA_SIZE = 253;
60 const int HKS_DEFAULT_MAC_SHA256_SIZE = 32;
61
62
63 const struct HksTestMacParams g_testMacParams[] = {
64 /* success: ree-sha256 */
65 { 0, HKS_SUCCESS, HKS_TEST_MAC_TYPE_REE, { 0 }, { 0 },
66 { true, HKS_TEST_MAC_REE_KEY_SIZE_32, true, HKS_TEST_MAC_REE_KEY_SIZE_32 },
67 { true, true, HKS_KEY_PURPOSE_MAC, true, HKS_DIGEST_SHA256 },
68 { true, HKS_DEFAULT_MAC_SRCDATA_SIZE, true, HKS_DEFAULT_MAC_SRCDATA_SIZE },
69 { true, HKS_DEFAULT_MAC_SHA256_SIZE, true, HKS_DEFAULT_MAC_SHA256_SIZE }
70 },
71
72 /* success: tee-sha256 */
73 { 1, HKS_SUCCESS, HKS_TEST_MAC_TYPE_TEE, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
74 { true, true, HKS_ALG_AES, true, HKS_AES_KEY_SIZE_256, true, HKS_KEY_PURPOSE_MAC,
75 true, HKS_DIGEST_SHA256, false, 0, false, 0 },
76 { 0 },
77 { true, true, HKS_KEY_PURPOSE_MAC, true, HKS_DIGEST_SHA256 },
78 { true, HKS_DEFAULT_MAC_SRCDATA_SIZE, true, HKS_DEFAULT_MAC_SRCDATA_SIZE },
79 { true, HKS_DEFAULT_MAC_SHA256_SIZE, true, HKS_DEFAULT_MAC_SHA256_SIZE }
80 },
81 };
82
ConstructDataToBlob(struct HksBlob ** srcData,struct HksBlob ** macData,const struct HksTestBlobParams * srcDataParams,const struct HksTestBlobParams * macDataParams)83 static int32_t ConstructDataToBlob(struct HksBlob **srcData, struct HksBlob **macData,
84 const struct HksTestBlobParams *srcDataParams, const struct HksTestBlobParams *macDataParams)
85 {
86 int32_t ret = TestConstuctBlob(srcData,
87 srcDataParams->blobExist,
88 srcDataParams->blobSize,
89 srcDataParams->blobDataExist,
90 srcDataParams->blobDataSize);
91 HKS_TEST_ASSERT(ret == 0);
92
93 ret = TestConstuctBlob(macData,
94 macDataParams->blobExist,
95 macDataParams->blobSize,
96 macDataParams->blobDataExist,
97 macDataParams->blobDataSize);
98 HKS_TEST_ASSERT(ret == 0);
99 return ret;
100 }
101
Mac(const struct HksBlob * key,const struct HksBlob * srcData,struct HksBlob * macData,const struct HksTestMacParamSet * macParamSetParams,enum HksTestMacType macType)102 static int32_t Mac(const struct HksBlob *key, const struct HksBlob *srcData, struct HksBlob *macData,
103 const struct HksTestMacParamSet *macParamSetParams, enum HksTestMacType macType)
104 {
105 struct HksParamSet *macParamSet = NULL;
106 int32_t ret;
107 if (macType == HKS_TEST_MAC_TYPE_REE) {
108 struct TestMacParamSetStructure paramStructTrue = {
109 &macParamSet,
110 macParamSetParams->paramSetExist,
111 macParamSetParams->setPurpose, macParamSetParams->purpose,
112 macParamSetParams->setDigest, macParamSetParams->digest, true, false
113 };
114 ret = TestConstructMacParamSet(¶mStructTrue);
115 } else {
116 struct TestMacParamSetStructure paramStructFalse = {
117 &macParamSet,
118 macParamSetParams->paramSetExist,
119 macParamSetParams->setPurpose, macParamSetParams->purpose,
120 macParamSetParams->setDigest, macParamSetParams->digest, false, false
121 };
122 ret = TestConstructMacParamSet(¶mStructFalse);
123 }
124 HKS_TEST_ASSERT(ret == 0);
125
126 ret = HksMacRun(key, macParamSet, srcData, macData, 1);
127 HksFreeParamSet(&macParamSet);
128 return ret;
129 }
130
BaseTestMac(uint32_t index)131 static int32_t BaseTestMac(uint32_t index)
132 {
133 /* 1. generate key */
134 struct HksBlob *key = NULL;
135 int32_t ret;
136
137 if (g_testMacParams[index].macType == HKS_TEST_MAC_TYPE_REE) {
138 ret = TestConstuctBlob(&key,
139 g_testMacParams[index].keyParams.blobExist,
140 g_testMacParams[index].keyParams.blobSize,
141 g_testMacParams[index].keyParams.blobDataExist,
142 g_testMacParams[index].keyParams.blobDataSize);
143 } else {
144 if (g_testMacParams[index].keyAliasParams.blobExist) {
145 ret = HuksGenerateKey(&key, &(g_testMacParams[index].keyAliasParams),
146 &g_testMacParams[index].genKeyParamSetParams, NULL);
147 } else {
148 ret = TestConstuctBlob(&key,
149 g_testMacParams[index].keyParams.blobExist,
150 g_testMacParams[index].keyParams.blobSize,
151 g_testMacParams[index].keyParams.blobDataExist,
152 g_testMacParams[index].keyParams.blobDataSize);
153 }
154 }
155 HKS_TEST_ASSERT(ret == 0);
156
157 /* 2. mac */
158 struct HksBlob *srcData = NULL;
159 struct HksBlob *macData = NULL;
160 ret = ConstructDataToBlob(&srcData, &macData,
161 &g_testMacParams[index].srcDataParams, &g_testMacParams[index].macParams);
162 HKS_TEST_ASSERT(ret == 0);
163
164 ret = Mac(key, srcData, macData, &g_testMacParams[index].macParamSetParams, g_testMacParams[index].macType);
165 if (ret != g_testMacParams[index].expectResult) {
166 HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testMacParams[index].testId, ret);
167 }
168 HKS_TEST_ASSERT(ret == g_testMacParams[index].expectResult);
169
170 /* 3. deletekey */
171 if ((g_testMacParams[index].macType == HKS_TEST_MAC_TYPE_TEE) &&
172 (g_testMacParams[index].keyAliasParams.blobExist)) {
173 ret = HksDeleteKey(key, NULL);
174 HKS_TEST_ASSERT(ret == 0);
175 }
176 TestFreeBlob(&key);
177 TestFreeBlob(&srcData);
178 TestFreeBlob(&macData);
179 return ret;
180 }
181
182 /**
183 * @tc.name: HksMacTest.HksMacTest001
184 * @tc.desc: The static function will return true;
185 * @tc.type: FUNC
186 */
187 HWTEST_F(HksMacTest, HksMacTest001, TestSize.Level1)
188 {
189 int32_t ret = BaseTestMac(0);
190 ASSERT_TRUE(ret == 0);
191 }
192
193 #ifndef _CUT_AUTHENTICATE_
194 /**
195 * @tc.name: HksMacTest.HksMacTest002
196 * @tc.desc: The static function will return true;
197 * @tc.type: FUNC
198 */
199 HWTEST_F(HksMacTest, HksMacTest002, TestSize.Level1)
200 {
201 int32_t ret = BaseTestMac(1);
202 ASSERT_TRUE(ret == 0);
203 }
204 #endif /* _CUT_AUTHENTICATE_ */
205 }
206