• 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 #include <gtest/gtest.h>
17 
18 #include "hks_safe_compare_key_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_file_operator.h"
25 #include "hks_test_log.h"
26 #include "hks_test_mem.h"
27 
28 using namespace testing::ext;
29 namespace {
30 #ifndef _CUT_AUTHENTICATE_
31 class HksSafeCompareKeyTest : public testing::Test {
32 public:
33     static void SetUpTestCase(void);
34 
35     static void TearDownTestCase(void);
36 
37     void SetUp();
38 
39     void TearDown();
40 };
41 
SetUpTestCase(void)42 void HksSafeCompareKeyTest::SetUpTestCase(void)
43 {
44 }
45 
TearDownTestCase(void)46 void HksSafeCompareKeyTest::TearDownTestCase(void)
47 {
48 }
49 
SetUp()50 void HksSafeCompareKeyTest::SetUp()
51 {
52     EXPECT_EQ(HksInitialize(), 0);
53 }
54 
TearDown()55 void HksSafeCompareKeyTest::TearDown()
56 {
57 }
58 
59 const char *g_storePath = "/storage/maindata/hks_client/key/";
60 const char *g_testOne = "TestOne";
61 const char *g_testTwo = "TestTwo";
62 
63 const struct HksTestGenKeyParams g_testGenKeyParams[] = {
64     /* x25519: tee sign/verify */
65     { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
66         {
67             true,
68             true, HKS_ALG_X25519,
69             true, HKS_CURVE25519_KEY_SIZE_256,
70             true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY,
71             false, 0,
72             false, 0,
73             false, 0,
74             false, 0
75         },
76         { false, 0 },
77     },
78 };
79 
SafeTestGenerateKey(struct HksBlob * keyAlias)80 static int32_t SafeTestGenerateKey(struct HksBlob *keyAlias)
81 {
82     uint32_t index = 0;
83     uint32_t performTimes = 1;
84 
85     struct HksParamSet *paramSet = NULL;
86     struct GenerateKeyParamSetStructure paramStruct = {
87         &paramSet,
88         g_testGenKeyParams[index].paramSetParams.paramSetExist,
89         g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg,
90         g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize,
91         g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose,
92         g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest,
93         g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding,
94         g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode,
95         g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag,
96         g_testGenKeyParams[index].paramSetParams.keyStorageFlag
97     };
98     int32_t ret = TestConstructGenerateKeyParamSet(&paramStruct);
99     HKS_TEST_ASSERT(ret == 0);
100 
101     struct HksParamSet *paramSetOut = NULL;
102     ret = TestConstructGenerateKeyParamSetOut(&paramSetOut,
103         g_testGenKeyParams[index].paramSetParamsOut.paramSetExist,
104         g_testGenKeyParams[index].paramSetParamsOut.paramSetSize);
105     HKS_TEST_ASSERT(ret == 0);
106 
107     ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes);
108     if (ret != g_testGenKeyParams[index].expectResult) {
109         HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret);
110     }
111     EXPECT_EQ(ret, g_testGenKeyParams[index].expectResult);
112 
113     if (ret == g_testGenKeyParams[index].expectResult) {
114         ret = 0;
115     } else {
116         ret = 1;
117     }
118     HksFreeParamSet(&paramSet);
119     HksFreeParamSet(&paramSetOut);
120     return ret;
121 }
122 
CompareKeyData(struct HksBlob * keyAliasOne,struct HksBlob * keyAliasTwo)123 static int32_t CompareKeyData(struct HksBlob *keyAliasOne, struct HksBlob *keyAliasTwo)
124 {
125     uint32_t sizeOne = HksTestFileSize(g_storePath, (char *)keyAliasOne->data);
126     uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne);
127     if (bufOne == NULL) {
128         return HKS_ERROR_MALLOC_FAIL;
129     }
130     uint32_t sizeRead = HksTestFileRead(g_storePath, (char *)keyAliasOne->data, 0, bufOne, sizeOne);
131 
132     uint32_t sizeTwo = HksTestFileSize(g_storePath, (char *)keyAliasTwo->data);
133     uint8_t *bufTwo = (uint8_t *)HksTestMalloc(sizeTwo);
134     if (bufTwo == NULL) {
135         HksTestFree(bufOne);
136         return HKS_ERROR_MALLOC_FAIL;
137     }
138     sizeRead = HksTestFileRead(g_storePath, (char *)keyAliasTwo->data, 0, bufTwo, sizeOne);
139     int32_t ret = memcmp(bufOne, bufTwo, sizeOne);
140     HksTestFree(bufOne);
141     HksTestFree(bufTwo);
142     return ret;
143 }
144 
145 /**
146  * @tc.name: HksSafeCompareKeyTest.HksSafeCompareKeyTest001
147  * @tc.desc: The static function will return true;
148  * @tc.type: FUNC
149  */
150 HWTEST_F(HksSafeCompareKeyTest, HksSafeCompareKeyTest001, TestSize.Level1)
151 {
152     struct HksBlob keyAliasOne = { strlen(g_testOne), (uint8_t *)g_testOne };
153     int32_t ret = SafeTestGenerateKey(&keyAliasOne);
154     EXPECT_EQ(ret, 0);
155 
156     struct HksBlob keyAliasTwo = { strlen(g_testTwo), (uint8_t *)g_testTwo };
157     ret = SafeTestGenerateKey(&keyAliasTwo);
158     EXPECT_EQ(ret, 0);
159 
160     ret = CompareKeyData(&keyAliasOne, &keyAliasTwo);
161     EXPECT_NE(ret, 0);
162 }
163 #endif /* _CUT_AUTHENTICATE_ */
164 }