• 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 #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_RSA_KEY_001_PARAMS = {
38     .spec = {
39         .algType = HKS_ALG_RSA,
40         .keyLen = HKS_RSA_KEY_SIZE_512,
41         .algParam = nullptr,
42     },
43 #if defined(HKS_SUPPORT_RSA_C) && defined(HKS_SUPPORT_RSA_GENERATE_KEY)
44     .generateKeyResult = HKS_SUCCESS,
45 #else
46     .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
47 #endif
48 };
49 
50 const TestCaseParams HKS_CRYPTO_HAL_RSA_KEY_002_PARAMS = {
51     .spec = {
52         .algType = HKS_ALG_RSA,
53         .keyLen = HKS_RSA_KEY_SIZE_768,
54         .algParam = nullptr,
55     },
56 #if defined(HKS_SUPPORT_RSA_C) && defined(HKS_SUPPORT_RSA_GENERATE_KEY)
57     .generateKeyResult = HKS_SUCCESS,
58 #else
59     .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
60 #endif
61 };
62 
63 const TestCaseParams HKS_CRYPTO_HAL_RSA_KEY_003_PARAMS = {
64     .spec = {
65         .algType = HKS_ALG_RSA,
66         .keyLen = HKS_RSA_KEY_SIZE_1024,
67         .algParam = nullptr,
68     },
69 #if defined(HKS_SUPPORT_RSA_C) && defined(HKS_SUPPORT_RSA_GENERATE_KEY)
70     .generateKeyResult = HKS_SUCCESS,
71 #else
72     .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
73 #endif
74 };
75 
76 const TestCaseParams HKS_CRYPTO_HAL_RSA_KEY_004_PARAMS = {
77     .spec = {
78         .algType = HKS_ALG_RSA,
79         .keyLen = HKS_RSA_KEY_SIZE_2048,
80         .algParam = nullptr,
81     },
82 #if defined(HKS_SUPPORT_RSA_C) && defined(HKS_SUPPORT_RSA_GENERATE_KEY)
83     .generateKeyResult = HKS_SUCCESS,
84 #else
85     .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
86 #endif
87 };
88 
89 const TestCaseParams HKS_CRYPTO_HAL_RSA_KEY_005_PARAMS = {
90     .spec = {
91         .algType = HKS_ALG_RSA,
92         .keyLen = HKS_RSA_KEY_SIZE_3072,
93         .algParam = nullptr,
94     },
95 #if defined(HKS_SUPPORT_RSA_C) && defined(HKS_SUPPORT_RSA_GENERATE_KEY)
96     .generateKeyResult = HKS_SUCCESS,
97 #else
98     .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
99 #endif
100 };
101 
102 const TestCaseParams HKS_CRYPTO_HAL_RSA_KEY_006_PARAMS = {
103     .spec = {
104         .algType = HKS_ALG_RSA,
105         .keyLen = HKS_RSA_KEY_SIZE_4096,
106         .algParam = nullptr,
107     },
108 #if defined(HKS_SUPPORT_RSA_C) && defined(HKS_SUPPORT_RSA_GENERATE_KEY)
109     .generateKeyResult = HKS_SUCCESS,
110 #else
111     .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
112 #endif
113 };
114 }  // namespace
115 
116 class HksCryptoHalRsaKey : public HksCryptoHalCommon, public testing::Test {
117 public:
118     static void SetUpTestCase(void);
119     static void TearDownTestCase(void);
120     void SetUp();
121     void TearDown();
122 protected:
RunTestCase(const TestCaseParams & testCaseParams) const123     void RunTestCase(const TestCaseParams &testCaseParams) const
124     {
125         HksBlob key = { .size = 0, .data = nullptr };
126         ASSERT_EQ(HksCryptoHalGenerateKey(&testCaseParams.spec, &key), testCaseParams.generateKeyResult);
127         if (testCaseParams.generateKeyResult == HKS_SUCCESS) {
128             ASSERT_NE((uint32_t)0, key.size);
129             ASSERT_NE(nullptr, key.data);
130             HksFree(key.data);
131         }
132     }
133 };
134 
SetUpTestCase(void)135 void HksCryptoHalRsaKey::SetUpTestCase(void)
136 {
137 }
138 
TearDownTestCase(void)139 void HksCryptoHalRsaKey::TearDownTestCase(void)
140 {
141 }
142 
SetUp()143 void HksCryptoHalRsaKey::SetUp()
144 {
145     EXPECT_EQ(HksCryptoAbilityInit(), 0);
146 }
147 
TearDown()148 void HksCryptoHalRsaKey::TearDown()
149 {
150 }
151 
152 /**
153  * @tc.number    : HksCryptoHalRsaKey_001
154  * @tc.name      : HksCryptoHalRsaKey_001
155  * @tc.desc      : Using HksCryptoHalGenerateKey Generate RSA-512bit key.
156  */
157 HWTEST_F(HksCryptoHalRsaKey, HksCryptoHalRsaKey_001, Function | SmallTest | Level1)
158 {
159     RunTestCase(HKS_CRYPTO_HAL_RSA_KEY_001_PARAMS);
160 }
161 
162 /**
163  * @tc.number    : HksCryptoHalRsaKey_002
164  * @tc.name      : HksCryptoHalRsaKey_002
165  * @tc.desc      : Using HksCryptoHalGenerateKey Generate RSA-768bit key.
166  */
167 HWTEST_F(HksCryptoHalRsaKey, HksCryptoHalRsaKey_002, Function | SmallTest | Level1)
168 {
169     RunTestCase(HKS_CRYPTO_HAL_RSA_KEY_002_PARAMS);
170 }
171 
172 /**
173  * @tc.number    : HksCryptoHalRsaKey_003
174  * @tc.name      : HksCryptoHalRsaKey_003
175  * @tc.desc      : Using HksCryptoHalGenerateKey Generate RSA-1024bit key.
176  */
177 HWTEST_F(HksCryptoHalRsaKey, HksCryptoHalRsaKey_003, Function | SmallTest | Level1)
178 {
179     RunTestCase(HKS_CRYPTO_HAL_RSA_KEY_003_PARAMS);
180 }
181 
182 /**
183  * @tc.number    : HksCryptoHalRsaKey_004
184  * @tc.name      : HksCryptoHalRsaKey_004
185  * @tc.desc      : Using HksCryptoHalGenerateKey Generate RSA-2048bit key.
186  */
187 HWTEST_F(HksCryptoHalRsaKey, HksCryptoHalRsaKey_004, Function | SmallTest | Level1)
188 {
189     RunTestCase(HKS_CRYPTO_HAL_RSA_KEY_004_PARAMS);
190 }
191 
192 /**
193  * @tc.number    : HksCryptoHalRsaKey_005
194  * @tc.name      : HksCryptoHalRsaKey_005
195  * @tc.desc      : Using HksCryptoHalGenerateKey Generate RSA-3072bit key.
196  */
197 HWTEST_F(HksCryptoHalRsaKey, HksCryptoHalRsaKey_005, Function | SmallTest | Level1)
198 {
199     RunTestCase(HKS_CRYPTO_HAL_RSA_KEY_005_PARAMS);
200 }
201 
202 /**
203  * @tc.number    : HksCryptoHalRsaKey_006
204  * @tc.name      : HksCryptoHalRsaKey_006
205  * @tc.desc      : Using HksCryptoHalGenerateKey Generate RSA-4096bit key.
206  */
207 HWTEST_F(HksCryptoHalRsaKey, HksCryptoHalRsaKey_006, Function | SmallTest | Level1)
208 {
209     RunTestCase(HKS_CRYPTO_HAL_RSA_KEY_006_PARAMS);
210 }
211 
212 /**
213  * @tc.number    : HksCryptoHalRsaKey_007
214  * @tc.name      : HksCryptoHalRsaKey_007
215  * @tc.desc      : Generate key and export public key with RSA.
216  */
217 HWTEST_F(HksCryptoHalRsaKey, HksCryptoHalRsaKey_007, Function | SmallTest | Level1)
218 {
219     int32_t ret;
220 
221     HksKeySpec spec = {
222         .algType = HKS_ALG_RSA,
223         .keyLen = HKS_RSA_KEY_SIZE_2048,
224     };
225 
226     HksBlob key = { .size = 0, .data = NULL };
227 
228     ret = HksCryptoHalGenerateKey(&spec, &key);
229     ASSERT_EQ(ret, HKS_SUCCESS);
230 
231     KeyMaterialRsa *keyMaterial = (KeyMaterialRsa *)key.data;
232     ASSERT_NE(keyMaterial, nullptr);
233 
234     uint32_t keyOutLen = sizeof(KeyMaterialRsa) + keyMaterial->nSize + keyMaterial->eSize;
235     HksBlob keyOut = { .size = keyOutLen, .data = (uint8_t *)HksMalloc(keyOutLen) };
236     ASSERT_NE(keyOut.data, nullptr);
237 
238     ret = HksCryptoHalGetPubKey(&key, &keyOut);
239     ASSERT_EQ(ret, HKS_SUCCESS);
240     HKS_FREE_BLOB(key);
241     HKS_FREE_BLOB(keyOut);
242 }
243 }  // namespace UnitTest
244 }  // namespace Huks
245 }  // namespace Security
246 }  // namespace OHOS