• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 "hi_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 /*
34  * @tc.register: register a test suit named "CalcMultiTest"
35  * @param: test subsystem name
36  * @param: c_example module name
37  * @param: CalcMultiTest test suit name
38  */
39 LITE_TEST_SUIT(husk, huks_lite, HksSafeCipherKeyTest);
40 
41 /**
42  * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
43  * @return: true——setup success
44  */
HksSafeCipherKeyTestSetUp()45 static BOOL HksSafeCipherKeyTestSetUp()
46 {
47     LiteTestPrint("setup\n");
48     hi_watchdog_disable();
49     TEST_ASSERT_TRUE(HksInitialize() == 0);
50     return TRUE;
51 }
52 
53 /**
54  * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
55  * @return: true——teardown success
56  */
HksSafeCipherKeyTestTearDown()57 static BOOL HksSafeCipherKeyTestTearDown()
58 {
59     LiteTestPrint("tearDown\n");
60     hi_watchdog_enable();
61     return TRUE;
62 }
63 
64 static struct HksBlob g_storageImageBuffer;
65 
GetKeyOffsetByKeyAlias(const struct HksBlob * keyAlias,uint32_t * keyOffset)66 static int32_t GetKeyOffsetByKeyAlias(const struct HksBlob *keyAlias, uint32_t *keyOffset)
67 {
68     struct HksBlob storageBuf = g_storageImageBuffer;
69 
70     /* 1. get imageBuffer total Len */
71     struct HksStoreHeaderInfo *keyInfoHead = (struct HksStoreHeaderInfo *)storageBuf.data;
72     uint32_t keyCount = keyInfoHead->keyCount;
73 
74     /* 2. traverse imageBuffer to search for keyAlias */
75     uint32_t offset = sizeof(*keyInfoHead);
76     for (uint32_t i = 0; i < keyCount; ++i) {
77         uint8_t *tmpBuf = storageBuf.data + offset;
78         struct HksStoreKeyInfo *keyInfo = (struct HksStoreKeyInfo *)tmpBuf;
79         if (keyInfo->aliasSize == keyAlias->size) {
80             if (HksMemCmp(keyAlias->data, tmpBuf + sizeof(*keyInfo), keyAlias->size) == 0) {
81                 *keyOffset = offset;
82                 return HKS_SUCCESS;
83             }
84         }
85 
86         offset += keyInfo->keyInfoLen;
87     }
88 
89     return HKS_ERROR_NOT_EXIST;
90 }
91 
CompareTwoKey(const struct HksBlob * keyAliasOne,const struct HksBlob * keyAliasTwo)92 static int32_t CompareTwoKey(const struct HksBlob *keyAliasOne, const struct HksBlob *keyAliasTwo)
93 {
94     uint32_t sizeOne = HksTestFileSize(g_storePath, "hks_keystore");
95     uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne);
96     if (bufOne == NULL) {
97         return HKS_ERROR_MALLOC_FAIL;
98     }
99 
100     uint32_t sizeRead = HksTestFileRead(g_storePath, "hks_keystore", 0, bufOne, sizeOne);
101     TEST_ASSERT_TRUE(sizeRead > 0);
102 
103     g_storageImageBuffer.data = bufOne;
104     g_storageImageBuffer.size = sizeOne;
105 
106     int32_t offset1;
107     int ret = GetKeyOffsetByKeyAlias(keyAliasOne, &offset1);
108     TEST_ASSERT_TRUE(ret == 0);
109 
110     struct HksStoreKeyInfo *keyInfo1 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset1);
111 
112     int32_t offset2;
113     ret = GetKeyOffsetByKeyAlias(keyAliasTwo, &offset2);
114     TEST_ASSERT_TRUE(ret == 0);
115 
116     struct HksStoreKeyInfo *keyInfo2 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset2);
117 
118     TEST_ASSERT_TRUE(keyInfo1->keyInfoLen == keyInfo2->keyInfoLen);
119 
120     ret = memcmp(keyInfo1, keyInfo2, keyInfo1->keyInfoLen);
121     HksTestFree(bufOne);
122     return ret;
123 }
124 
125 /**
126  * @tc.name: HksSafeCipherKeyTest.HksSafeCipherKeyTest001
127  * @tc.desc: The static function will return true;
128  * @tc.type: FUNC
129  */
LITE_TEST_CASE(HksSafeCipherKeyTest,HksSafeCipherKeyTest001,Level1)130 LITE_TEST_CASE(HksSafeCipherKeyTest, HksSafeCipherKeyTest001, Level1)
131 {
132     struct HksBlob ed25519Alias = { strlen(g_testEd25519), (uint8_t *)g_testEd25519 };
133     uint8_t pubKey[32] = {0};
134     uint32_t pubKeyLen = 32;
135     struct HksBlob pubKeyInfo = { pubKeyLen, pubKey };
136     int32_t ret = TestGenerateEd25519Key(ed25519Alias);
137     TEST_ASSERT_TRUE(ret == 0);
138 
139     ret = HksExportPublicKey(&ed25519Alias, NULL, &pubKeyInfo);
140     TEST_ASSERT_TRUE(ret == 0);
141 
142     ret = HksDeleteKey(&ed25519Alias, NULL);
143     TEST_ASSERT_TRUE(ret == 0);
144 
145     struct HksBlob newAliasOne = { strlen("test_ed25519_1"), (uint8_t *)"test_ed25519_1" };
146     ret = TestImportEd25519(newAliasOne, &pubKeyInfo);
147     TEST_ASSERT_TRUE(ret == 0);
148 
149     struct HksBlob newAliasTwo = { strlen("test_ed25519_2"), (uint8_t *)"test_ed25519_2" };
150     ret = TestImportEd25519(newAliasTwo, &pubKeyInfo);
151     TEST_ASSERT_TRUE(ret == 0);
152 
153     ret = CompareTwoKey(&newAliasOne, &newAliasTwo);
154     TEST_ASSERT_TRUE(ret != 0);
155 
156     ret = HksDeleteKey(&newAliasOne, NULL);
157     HKS_TEST_ASSERT(ret == 0);
158     TEST_ASSERT_TRUE(ret == 0);
159     ret = HksDeleteKey(&newAliasTwo, NULL);
160     TEST_ASSERT_TRUE(ret == 0);
161 }
162 RUN_TEST_SUITE(HksSafeCipherKeyTest);
163 
164 #endif
165 #endif /* _CUT_AUTHENTICATE_ */
166