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