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 #ifndef _CUT_ED25519_
18
19 #include "hks_safe_cipher_key_test.h"
20
21 #include <hctest.h>
22
23 #include "hks_api.h"
24 #include "hks_param.h"
25 #include "hks_test_curve25519.h"
26 #include "hks_test_log.h"
27 #include "hks_test_mem.h"
28 #include "stdlib.h"
29
30 const char *g_storePath = "/storage/";
31 const char *g_testEd25519 = "test_ed25519";
32
33 #define TEST_TASK_STACK_SIZE 0x2000
34 #define WAIT_TO_TEST_DONE 4
35
36 static osPriority_t g_setPriority;
37
38 /*
39 * @tc.register: register a test suit named "CalcMultiTest"
40 * @param: test subsystem name
41 * @param: c_example module name
42 * @param: CalcMultiTest test suit name
43 */
44 LITE_TEST_SUIT(security, securityData, HksSafeCipherKeyTest);
45
ExecHksInitialize(void const * argument)46 static void ExecHksInitialize(void const *argument)
47 {
48 LiteTestPrint("HksInitialize Begin!\n");
49 TEST_ASSERT_TRUE(HksInitialize() == 0);
50 LiteTestPrint("HksInitialize End!\n");
51 osThreadExit();
52 }
53
54 /**
55 * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
56 * @return: true——setup success
57 */
HksSafeCipherKeyTestSetUp()58 static BOOL HksSafeCipherKeyTestSetUp()
59 {
60 LiteTestPrint("setup\n");
61 osThreadId_t id;
62 osThreadAttr_t attr;
63 g_setPriority = osPriorityAboveNormal6;
64 attr.name = "test";
65 attr.attr_bits = 0U;
66 attr.cb_mem = NULL;
67 attr.cb_size = 0U;
68 attr.stack_mem = NULL;
69 attr.stack_size = TEST_TASK_STACK_SIZE;
70 attr.priority = g_setPriority;
71 id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
72 sleep(WAIT_TO_TEST_DONE);
73 LiteTestPrint("HksSafeCipherKeyTestSetUp End2!\n");
74
75 return TRUE;
76 }
77
78 /**
79 * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
80 * @return: true——teardown success
81 */
HksSafeCipherKeyTestTearDown()82 static BOOL HksSafeCipherKeyTestTearDown()
83 {
84 LiteTestPrint("tearDown\n");
85 return TRUE;
86 }
87
88 static struct HksBlob g_storageImageBuffer;
89
GetKeyOffsetByKeyAlias(const struct HksBlob * keyAlias,uint32_t * keyOffset)90 static int32_t GetKeyOffsetByKeyAlias(const struct HksBlob *keyAlias, uint32_t *keyOffset)
91 {
92 struct HksBlob storageBuf = g_storageImageBuffer;
93
94 /* 1. get imageBuffer total Len */
95 struct HksStoreHeaderInfo *keyInfoHead = (struct HksStoreHeaderInfo *)storageBuf.data;
96 uint32_t keyCount = keyInfoHead->keyCount;
97 uint32_t totalLen = keyInfoHead->totalLen;
98
99 /* 2. traverse imageBuffer to search for keyAlias */
100 uint32_t offset = sizeof(*keyInfoHead);
101 for (uint32_t i = 0; i < keyCount; ++i) {
102 uint8_t *tmpBuf = storageBuf.data + offset;
103 struct HksStoreKeyInfo *keyInfo = (struct HksStoreKeyInfo *)tmpBuf;
104 if (keyInfo->aliasSize == keyAlias->size) {
105 if (HksMemCmp(keyAlias->data, tmpBuf + sizeof(*keyInfo), keyAlias->size) == 0) {
106 *keyOffset = offset;
107 return HKS_SUCCESS;
108 }
109 }
110
111 offset += keyInfo->keyInfoLen;
112 }
113
114 return HKS_ERROR_NOT_EXIST;
115 }
116
CompareTwoKey(const struct HksBlob * keyAliasOne,const struct HksBlob * keyAliasTwo)117 static int32_t CompareTwoKey(const struct HksBlob *keyAliasOne, const struct HksBlob *keyAliasTwo)
118 {
119 uint32_t sizeOne = HksTestFileSize(g_storePath, "hks_keystore");
120 uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne);
121 if (bufOne == NULL) {
122 return HKS_ERROR_MALLOC_FAIL;
123 }
124
125 uint32_t sizeRead = HksTestFileRead(g_storePath, "hks_keystore", 0, bufOne, sizeOne);
126 TEST_ASSERT_TRUE(sizeRead > 0);
127
128 g_storageImageBuffer.data = bufOne;
129 g_storageImageBuffer.size = sizeOne;
130
131 int32_t offset1;
132 int ret = GetKeyOffsetByKeyAlias(keyAliasOne, &offset1);
133 TEST_ASSERT_TRUE(ret == 0);
134
135 struct HksStoreKeyInfo *keyInfo1 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset1);
136
137 int32_t offset2;
138 ret = GetKeyOffsetByKeyAlias(keyAliasTwo, &offset2);
139 TEST_ASSERT_TRUE(ret == 0);
140
141 struct HksStoreKeyInfo *keyInfo2 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset2);
142
143 TEST_ASSERT_TRUE(keyInfo1->keyInfoLen == keyInfo2->keyInfoLen);
144
145 ret = memcmp(keyInfo1, keyInfo2, keyInfo1->keyInfoLen);
146 HksTestFree(bufOne);
147 return ret;
148 }
149
150 /**
151 * @tc.name: HksSafeCipherKeyTest.HksSafeCipherKeyTest001
152 * @tc.desc: The static function will return true;
153 * @tc.type: FUNC
154 */
LITE_TEST_CASE(HksSafeCipherKeyTest,HksSafeCipherKeyTest001,Level1)155 LITE_TEST_CASE(HksSafeCipherKeyTest, HksSafeCipherKeyTest001, Level1)
156 {
157 struct HksBlob ed25519Alias = { strlen(g_testEd25519), (uint8_t *)g_testEd25519 };
158 uint8_t pubKey[32] = {0};
159 uint32_t pubKeyLen = 32;
160 struct HksBlob pubKeyInfo = { pubKeyLen, pubKey };
161 int32_t ret = TestGenerateEd25519Key(ed25519Alias);
162 TEST_ASSERT_TRUE(ret == 0);
163
164 ret = HksExportPublicKey(&ed25519Alias, NULL, &pubKeyInfo);
165 TEST_ASSERT_TRUE(ret == 0);
166
167 ret = HksDeleteKey(&ed25519Alias, NULL);
168 TEST_ASSERT_TRUE(ret == 0);
169
170 struct HksBlob newAliasOne = { strlen("test_ed25519_1"), (uint8_t *)"test_ed25519_1" };
171 ret = TestImportEd25519(newAliasOne, &pubKeyInfo);
172 TEST_ASSERT_TRUE(ret == 0);
173
174 struct HksBlob newAliasTwo = { strlen("test_ed25519_2"), (uint8_t *)"test_ed25519_2" };
175 ret = TestImportEd25519(newAliasTwo, &pubKeyInfo);
176 TEST_ASSERT_TRUE(ret == 0);
177
178 ret = CompareTwoKey(&newAliasOne, &newAliasTwo);
179 TEST_ASSERT_TRUE(ret != 0);
180
181 ret = HksDeleteKey(&newAliasOne, NULL);
182 HKS_TEST_ASSERT(ret == 0);
183 TEST_ASSERT_TRUE(ret == 0);
184 ret = HksDeleteKey(&newAliasTwo, NULL);
185 TEST_ASSERT_TRUE(ret == 0);
186 }
187 RUN_TEST_SUITE(HksSafeCipherKeyTest);
188
189 #endif
190 #endif /* _CUT_AUTHENTICATE_ */
191