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