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 #include <gtest/gtest.h>
17 #include <iostream>
18
19 #include "hks_ability.h"
20 #include "hks_config.h"
21 #include "hks_crypto_hal.h"
22 #include "hks_crypto_hal_common.h"
23 #include "hks_mem.h"
24
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace Security {
28 namespace Huks {
29 namespace UnitTest {
30 namespace {
31 struct TestCaseParams {
32 HksKeySpec spec = {0};
33
34 HksErrorCode generateKeyResult = HksErrorCode::HKS_SUCCESS;
35 };
36
37 const TestCaseParams HKS_CRYPTO_HAL_ECC_KEY_001_PARAMS = {
38 .spec = {
39 .algType = HKS_ALG_ECC,
40 .keyLen = HKS_ECC_KEY_SIZE_224,
41 .algParam = nullptr,
42 },
43 #if defined(HKS_SUPPORT_ECC_C) && defined(HKS_SUPPORT_ECC_GENERATE_KEY)
44 .generateKeyResult = HKS_SUCCESS,
45 #else
46 .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
47 #endif
48 };
49
50 const TestCaseParams HKS_CRYPTO_HAL_ECC_KEY_002_PARAMS = {
51 .spec = {
52 .algType = HKS_ALG_ECC,
53 .keyLen = HKS_ECC_KEY_SIZE_256,
54 .algParam = nullptr,
55 },
56 #if defined(HKS_SUPPORT_ECC_C) && defined(HKS_SUPPORT_ECC_GENERATE_KEY)
57 .generateKeyResult = HKS_SUCCESS,
58 #else
59 .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
60 #endif
61 };
62
63 const TestCaseParams HKS_CRYPTO_HAL_ECC_KEY_003_PARAMS = {
64 .spec = {
65 .algType = HKS_ALG_ECC,
66 .keyLen = HKS_ECC_KEY_SIZE_384,
67 .algParam = nullptr,
68 },
69 #if defined(HKS_SUPPORT_ECC_C) && defined(HKS_SUPPORT_ECC_GENERATE_KEY)
70 .generateKeyResult = HKS_SUCCESS,
71 #else
72 .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
73 #endif
74 };
75
76 const TestCaseParams HKS_CRYPTO_HAL_ECC_KEY_004_PARAMS = {
77 .spec = {
78 .algType = HKS_ALG_ECC,
79 .keyLen = HKS_ECC_KEY_SIZE_521,
80 .algParam = nullptr,
81 },
82 #if defined(HKS_SUPPORT_ECC_C) && defined(HKS_SUPPORT_ECC_GENERATE_KEY)
83 .generateKeyResult = HKS_SUCCESS,
84 #else
85 .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
86 #endif
87 };
88 } // namespace
89
90 class HksCryptoHalEccKey : public HksCryptoHalCommon, public testing::Test {
91 public:
92 static void SetUpTestCase(void);
93 static void TearDownTestCase(void);
94 void SetUp();
95 void TearDown();
96 protected:
RunTestCase(const TestCaseParams & testCaseParams) const97 void RunTestCase(const TestCaseParams &testCaseParams) const
98 {
99 HksBlob key = { .size = 0, .data = nullptr };
100 ASSERT_EQ(HksCryptoHalGenerateKey(&testCaseParams.spec, &key), testCaseParams.generateKeyResult);
101 if (testCaseParams.generateKeyResult == HKS_SUCCESS) {
102 ASSERT_NE((uint32_t)0, key.size);
103 ASSERT_NE(nullptr, key.data);
104 HksFree(key.data);
105 }
106 }
107 };
108
SetUpTestCase(void)109 void HksCryptoHalEccKey::SetUpTestCase(void)
110 {
111 }
112
TearDownTestCase(void)113 void HksCryptoHalEccKey::TearDownTestCase(void)
114 {
115 }
116
SetUp()117 void HksCryptoHalEccKey::SetUp()
118 {
119 EXPECT_EQ(HksCryptoAbilityInit(), 0);
120 }
121
TearDown()122 void HksCryptoHalEccKey::TearDown()
123 {
124 }
125
126 /**
127 * @tc.number : HksCryptoHalEccKey_001
128 * @tc.name : HksCryptoHalEccKey_001
129 * @tc.desc : Using HksCryptoHalGenerateKey Generate ECC-224bit key.
130 */
131 HWTEST_F(HksCryptoHalEccKey, HksCryptoHalEccKey_001, Function | SmallTest | Level0)
132 {
133 RunTestCase(HKS_CRYPTO_HAL_ECC_KEY_001_PARAMS);
134 }
135
136 /**
137 * @tc.number : HksCryptoHalEccKey_002
138 * @tc.name : HksCryptoHalEccKey_002
139 * @tc.desc : Using HksCryptoHalGenerateKey Generate ECC-256bit key.
140 */
141 HWTEST_F(HksCryptoHalEccKey, HksCryptoHalEccKey_002, Function | SmallTest | Level0)
142 {
143 RunTestCase(HKS_CRYPTO_HAL_ECC_KEY_002_PARAMS);
144 }
145
146 /**
147 * @tc.number : HksCryptoHalEccKey_003
148 * @tc.name : HksCryptoHalEccKey_003
149 * @tc.desc : Using HksCryptoHalGenerateKey Generate ECC-384bit key.
150 */
151 HWTEST_F(HksCryptoHalEccKey, HksCryptoHalEccKey_003, Function | SmallTest | Level0)
152 {
153 RunTestCase(HKS_CRYPTO_HAL_ECC_KEY_003_PARAMS);
154 }
155
156 /**
157 * @tc.number : HksCryptoHalEccKey_004
158 * @tc.name : HksCryptoHalEccKey_004
159 * @tc.desc : Using HksCryptoHalGenerateKey Generate ECC-521bit key.
160 */
161 HWTEST_F(HksCryptoHalEccKey, HksCryptoHalEccKey_004, Function | SmallTest | Level0)
162 {
163 RunTestCase(HKS_CRYPTO_HAL_ECC_KEY_004_PARAMS);
164 }
165
166 /**
167 * @tc.number : HksCryptoHalEccKey_005
168 * @tc.name : HksCryptoHalEccKey_005
169 * @tc.desc : Generate key and export public key with ECC.
170 */
171 HWTEST_F(HksCryptoHalEccKey, HksCryptoHalEccKey_005, Function | SmallTest | Level0)
172 {
173 int32_t ret;
174
175 HksKeySpec spec = {
176 .algType = HKS_ALG_ECC,
177 .keyLen = HKS_ECC_KEY_SIZE_384,
178 };
179
180 HksBlob key = { .size = 0, .data = NULL };
181
182 ret = HksCryptoHalGenerateKey(&spec, &key);
183 ASSERT_EQ(ret, HKS_SUCCESS);
184
185 KeyMaterialEcc *keyMaterial = (KeyMaterialEcc *)key.data;
186 ASSERT_NE(keyMaterial, nullptr);
187
188 uint32_t keyOutLen = sizeof(KeyMaterialEcc) + keyMaterial->xSize + keyMaterial->ySize;
189 HksBlob keyOut = { .size = keyOutLen, .data = (uint8_t *)HksMalloc(keyOutLen) };
190 ASSERT_NE(keyOut.data, nullptr);
191
192 ret = HksCryptoHalGetPubKey(&key, &keyOut);
193 ASSERT_EQ(ret, HKS_SUCCESS);
194 HKS_FREE_BLOB(key);
195 HKS_FREE_BLOB(keyOut);
196 }
197 } // namespace UnitTest
198 } // namespace Huks
199 } // namespace Security
200 } // namespace OHOS