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_cipher_key_test.h"
19
20 #include "hks_api.h"
21 #include "hks_param.h"
22 #include "hks_test_curve25519.h"
23 #include "hks_test_file_operator.h"
24 #include "hks_test_mem.h"
25
26 #include "securec.h"
27
28 using namespace testing::ext;
29 namespace {
30 #ifndef _CUT_AUTHENTICATE_
31 class HksSafeCipherKeyTest : 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 HksSafeCipherKeyTest::SetUpTestCase(void)
43 {
44 }
45
TearDownTestCase(void)46 void HksSafeCipherKeyTest::TearDownTestCase(void)
47 {
48 }
49
SetUp()50 void HksSafeCipherKeyTest::SetUp()
51 {
52 EXPECT_EQ(HksInitialize(), 0);
53 }
54
TearDown()55 void HksSafeCipherKeyTest::TearDown()
56 {
57 }
58
59 const char *g_storePath = "/storage/maindata/hks_client/key";
60 const char *g_testEd25519 = "test_ed25519";
CompareTwoKey(const struct HksBlob * keyAliasOne,const struct HksBlob * keyAliasTwo)61 static int32_t CompareTwoKey(const struct HksBlob *keyAliasOne, const struct HksBlob *keyAliasTwo)
62 {
63 uint32_t sizeOne = HksTestFileSize(g_storePath, (char *)keyAliasOne->data);
64 uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne);
65 if (bufOne == NULL) {
66 return HKS_ERROR_MALLOC_FAIL;
67 }
68 uint32_t sizeRead = HksTestFileRead(g_storePath, (char *)keyAliasOne->data, 0, bufOne, sizeOne);
69
70 uint32_t sizeTwo = HksTestFileSize(g_storePath, (char *)keyAliasTwo->data);
71 uint8_t *bufTwo = (uint8_t *)HksTestMalloc(sizeTwo);
72 if (bufTwo == NULL) {
73 HksTestFree(bufOne);
74 return HKS_ERROR_MALLOC_FAIL;
75 }
76 sizeRead = HksTestFileRead(g_storePath, (char *)keyAliasTwo->data, 0, bufTwo, sizeOne);
77 int32_t ret = memcmp(bufOne, bufTwo, sizeRead);
78 HksTestFree(bufOne);
79 HksTestFree(bufTwo);
80 return ret;
81 }
82
83 /**
84 * @tc.name: HksSafeCipherKeyTest.HksSafeCipherKeyTest001
85 * @tc.desc: The static function will return true;
86 * @tc.type: FUNC
87 */
88 HWTEST_F(HksSafeCipherKeyTest, HksSafeCipherKeyTest001, TestSize.Level1)
89 {
90 struct HksBlob ed25519Alias = { strlen(g_testEd25519), (uint8_t *)g_testEd25519 };
91 int32_t ret = TestGenerateEd25519Key(ed25519Alias);
92 uint8_t pubKey[32] = {0};
93 uint32_t pubKeyLen = 32;
94 struct HksBlob pubKeyInfo = { pubKeyLen, pubKey };
95 ret = HksExportPublicKey(&ed25519Alias, NULL, &pubKeyInfo);
96 EXPECT_EQ(ret, 0);
97 ret = HksDeleteKey(&ed25519Alias, NULL);
98 EXPECT_EQ(ret, 0);
99
100 struct HksBlob newAliasOne = { strlen("test_ed25519_1"), (uint8_t *)"test_ed25519_1" };
101 ret = TestImportEd25519(newAliasOne, &pubKeyInfo);
102 EXPECT_EQ(ret, 0);
103
104 struct HksBlob newAliasTwo = { strlen("test_ed25519_2"), (uint8_t *)"test_ed25519_2" };
105 ret = TestImportEd25519(newAliasTwo, &pubKeyInfo);
106 EXPECT_EQ(ret, 0);
107
108 ret = CompareTwoKey(&newAliasOne, &newAliasTwo);
109
110 EXPECT_NE(ret, 0);
111
112 ret = HksDeleteKey(&newAliasOne, NULL);
113 EXPECT_EQ(ret, 0);
114 ret = HksDeleteKey(&newAliasTwo, NULL);
115 EXPECT_EQ(ret, 0);
116 }
117 #endif /* _CUT_AUTHENTICATE_ */
118 }