1 /*
2 * Copyright (c) 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 "hks_rsa_cipher_part5_test.h"
17 #include "hks_rsa_cipher_test_common.h"
18
19 #include <gtest/gtest.h>
20
21 using namespace testing::ext;
22 namespace Unittest::RsaCipher {
23 class HksRsaCipherPart5Test : public testing::Test {
24 public:
25 static void SetUpTestCase(void);
26
27 static void TearDownTestCase(void);
28
29 void SetUp();
30
31 void TearDown();
32 };
33
SetUpTestCase(void)34 void HksRsaCipherPart5Test::SetUpTestCase(void)
35 {
36 }
37
TearDownTestCase(void)38 void HksRsaCipherPart5Test::TearDownTestCase(void)
39 {
40 }
41
SetUp()42 void HksRsaCipherPart5Test::SetUp()
43 {
44 }
45
TearDown()46 void HksRsaCipherPart5Test::TearDown()
47 {
48 }
49
50 static struct HksParam g_genParams041[] = {
51 {
52 .tag = HKS_TAG_ALGORITHM,
53 .uint32Param = HKS_ALG_RSA
54 }, {
55 .tag = HKS_TAG_PURPOSE,
56 .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT
57 }, {
58 .tag = HKS_TAG_KEY_SIZE,
59 .uint32Param = HKS_RSA_KEY_SIZE_4096
60 }, {
61 .tag = HKS_TAG_PADDING,
62 .uint32Param = HKS_PADDING_OAEP
63 }, {
64 .tag = HKS_TAG_DIGEST,
65 .uint32Param = HKS_DIGEST_SHA384
66 }, {
67 .tag = HKS_TAG_BLOCK_MODE,
68 .uint32Param = HKS_MODE_ECB
69 }
70 };
71 static struct HksParam g_encryptParams041[] = {
72 {
73 .tag = HKS_TAG_ALGORITHM,
74 .uint32Param = HKS_ALG_RSA
75 }, {
76 .tag = HKS_TAG_PURPOSE,
77 .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
78 }, {
79 .tag = HKS_TAG_KEY_SIZE,
80 .uint32Param = HKS_RSA_KEY_SIZE_4096
81 }, {
82 .tag = HKS_TAG_PADDING,
83 .uint32Param = HKS_PADDING_OAEP
84 }, {
85 .tag = HKS_TAG_DIGEST,
86 .uint32Param = HKS_DIGEST_SHA384
87 }, {
88 .tag = HKS_TAG_BLOCK_MODE,
89 .uint32Param = HKS_MODE_ECB
90 }
91 };
92 static struct HksParam g_decryptParams041[] = {
93 {
94 .tag = HKS_TAG_ALGORITHM,
95 .uint32Param = HKS_ALG_RSA
96 }, {
97 .tag = HKS_TAG_PURPOSE,
98 .uint32Param = HKS_KEY_PURPOSE_DECRYPT
99 }, {
100 .tag = HKS_TAG_KEY_SIZE,
101 .uint32Param = HKS_RSA_KEY_SIZE_4096
102 }, {
103 .tag = HKS_TAG_PADDING,
104 .uint32Param = HKS_PADDING_OAEP
105 }, {
106 .tag = HKS_TAG_DIGEST,
107 .uint32Param = HKS_DIGEST_SHA384
108 }, {
109 .tag = HKS_TAG_BLOCK_MODE,
110 .uint32Param = HKS_MODE_ECB
111 }
112 };
113
114 static struct HksParam g_genParams042[] = {
115 {
116 .tag = HKS_TAG_ALGORITHM,
117 .uint32Param = HKS_ALG_RSA
118 }, {
119 .tag = HKS_TAG_PURPOSE,
120 .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT
121 }, {
122 .tag = HKS_TAG_KEY_SIZE,
123 .uint32Param = HKS_RSA_KEY_SIZE_4096
124 }, {
125 .tag = HKS_TAG_PADDING,
126 .uint32Param = HKS_PADDING_OAEP
127 }, {
128 .tag = HKS_TAG_DIGEST,
129 .uint32Param = HKS_DIGEST_SHA512
130 }, {
131 .tag = HKS_TAG_BLOCK_MODE,
132 .uint32Param = HKS_MODE_ECB
133 }
134 };
135 static struct HksParam g_encryptParams042[] = {
136 {
137 .tag = HKS_TAG_ALGORITHM,
138 .uint32Param = HKS_ALG_RSA
139 }, {
140 .tag = HKS_TAG_PURPOSE,
141 .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
142 }, {
143 .tag = HKS_TAG_KEY_SIZE,
144 .uint32Param = HKS_RSA_KEY_SIZE_4096
145 }, {
146 .tag = HKS_TAG_PADDING,
147 .uint32Param = HKS_PADDING_OAEP
148 }, {
149 .tag = HKS_TAG_DIGEST,
150 .uint32Param = HKS_DIGEST_SHA512
151 }, {
152 .tag = HKS_TAG_BLOCK_MODE,
153 .uint32Param = HKS_MODE_ECB
154 }
155 };
156 static struct HksParam g_decryptParams042[] = {
157 {
158 .tag = HKS_TAG_ALGORITHM,
159 .uint32Param = HKS_ALG_RSA
160 }, {
161 .tag = HKS_TAG_PURPOSE,
162 .uint32Param = HKS_KEY_PURPOSE_DECRYPT
163 }, {
164 .tag = HKS_TAG_KEY_SIZE,
165 .uint32Param = HKS_RSA_KEY_SIZE_4096
166 }, {
167 .tag = HKS_TAG_PADDING,
168 .uint32Param = HKS_PADDING_OAEP
169 }, {
170 .tag = HKS_TAG_DIGEST,
171 .uint32Param = HKS_DIGEST_SHA512
172 }, {
173 .tag = HKS_TAG_BLOCK_MODE,
174 .uint32Param = HKS_MODE_ECB
175 }
176 };
177
178 /**
179 * @tc.name: HksRsaCipherPart5Test.HksRsaCipherPart5Test041
180 * @tc.desc: alg-RSA pur-ENCRYPT-DECRYPT size-4096 pad-OAEP dig-SHA384 mode-ECB.
181 * @tc.type: FUNC
182 */
183 HWTEST_F(HksRsaCipherPart5Test, HksRsaCipherPart5Test041, TestSize.Level1)
184 {
185 char tmpKeyAlias[] = "HksRSACipherKeyAliasTest041";
186 struct HksBlob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
187 struct HksBlob inData = { Unittest::RsaCipher::g_inData_32.length(),
188 (uint8_t *)Unittest::RsaCipher::g_inData_32.c_str() };
189
190 struct HksParamSet *genParamSet = nullptr;
191 int32_t ret = InitParamSet(&genParamSet, g_genParams041, sizeof(g_genParams041)/sizeof(HksParam));
192 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(gen) failed.";
193
194 struct HksParamSet *encryptParamSet = nullptr;
195 ret = InitParamSet(&encryptParamSet, g_encryptParams041, sizeof(g_encryptParams041)/sizeof(HksParam));
196 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(encrypt) failed.";
197
198 struct HksParamSet *decryptParamSet = nullptr;
199 ret = InitParamSet(&decryptParamSet, g_decryptParams041, sizeof(g_decryptParams041)/sizeof(HksParam));
200 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(decrypt) failed.";
201
202 ret = HksRsaCipherTestCase(&keyAlias, genParamSet, encryptParamSet, decryptParamSet, &inData);
203 EXPECT_EQ(ret, HKS_SUCCESS) << "this case failed.";
204
205 HksFreeParamSet(&genParamSet);
206 HksFreeParamSet(&encryptParamSet);
207 HksFreeParamSet(&decryptParamSet);
208 }
209
210 /**
211 * @tc.name: HksRsaCipherPart5Test.HksRsaCipherPart5Test042
212 * @tc.desc: alg-RSA pur-ENCRYPT-DECRYPT size-4096 pad-OAEP dig-SHA512 mode-ECB.
213 * @tc.type: FUNC
214 */
215 HWTEST_F(HksRsaCipherPart5Test, HksRsaCipherPart5Test042, TestSize.Level1)
216 {
217 char tmpKeyAlias[] = "HksRSACipherKeyAliasTest042";
218 struct HksBlob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
219 struct HksBlob inData = { Unittest::RsaCipher::g_inData_32.length(),
220 (uint8_t *)Unittest::RsaCipher::g_inData_32.c_str() };
221
222 struct HksParamSet *genParamSet = nullptr;
223 int32_t ret = InitParamSet(&genParamSet, g_genParams042, sizeof(g_genParams042)/sizeof(HksParam));
224 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(gen) failed.";
225
226 struct HksParamSet *encryptParamSet = nullptr;
227 ret = InitParamSet(&encryptParamSet, g_encryptParams042, sizeof(g_encryptParams042)/sizeof(HksParam));
228 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(encrypt) failed.";
229
230 struct HksParamSet *decryptParamSet = nullptr;
231 ret = InitParamSet(&decryptParamSet, g_decryptParams042, sizeof(g_decryptParams042)/sizeof(HksParam));
232 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(decrypt) failed.";
233
234 ret = HksRsaCipherTestCase(&keyAlias, genParamSet, encryptParamSet, decryptParamSet, &inData);
235 EXPECT_EQ(ret, HKS_SUCCESS) << "this case failed.";
236
237 HksFreeParamSet(&genParamSet);
238 HksFreeParamSet(&encryptParamSet);
239 HksFreeParamSet(&decryptParamSet);
240 }
241
242 /**
243 * @tc.name: HksRsaCipherPart5Test.HksRsaCipherPart5Test043
244 * @tc.desc: alg-RSA pur-ENCRYPT-DECRYPT size-4096 pad-OAEP dig-SHA512 mode-ECB. Decrypt-Abort
245 * @tc.type: FUNC
246 */
247 HWTEST_F(HksRsaCipherPart5Test, HksRsaCipherPart5Test043, TestSize.Level1)
248 {
249 char tmpKeyAlias[] = "HksRSACipherKeyAliasTest043";
250 struct HksBlob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
251 struct HksBlob inData = { Unittest::RsaCipher::g_inData_32.length(),
252 (uint8_t *)Unittest::RsaCipher::g_inData_32.c_str() };
253 int32_t ret = HKS_FAILURE;
254
255 /* 1. Generate Key */
256 struct HksParamSet *genParamSet = nullptr;
257 ret = InitParamSet(&genParamSet, g_genParams042, sizeof(g_genParams042)/sizeof(HksParam));
258 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(gen) failed.";
259 ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
260 EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
261
262 /* 2. Export Public Key */
263 uint8_t tmpPublicKey[HKS_RSA_KEY_SIZE_4096] = {0};
264 struct HksBlob publicKey = { HKS_RSA_KEY_SIZE_4096, (uint8_t *)tmpPublicKey };
265 ret = HksExportPublicKey(&keyAlias, genParamSet, &publicKey);
266
267 /* 3. Encrypt Three Stage */
268 struct HksParamSet *encryptParamSet = nullptr;
269 ret = InitParamSet(&encryptParamSet, g_encryptParams042, sizeof(g_encryptParams042)/sizeof(HksParam));
270 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
271
272 /* Import Key */
273 char tmpKey[] = "RSA_Encrypt_Decrypt_KeyAlias";
274 struct HksBlob newKeyAlias = { .size = strlen(tmpKey), .data = (uint8_t *)tmpKey };
275 ret = HksImportKey(&newKeyAlias, encryptParamSet, &publicKey);
276
277 // Init
278 uint8_t handleE[sizeof(uint64_t)] = {0};
279 struct HksBlob handleEncrypt = { sizeof(uint64_t), handleE };
280 ret = HksInit(&newKeyAlias, encryptParamSet, &handleEncrypt);
281 EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
282 // Update & Finish
283 uint8_t cipher[Unittest::RsaCipher::RSA_COMMON_SIZE] = {0};
284 struct HksBlob cipherText = { Unittest::RsaCipher::RSA_COMMON_SIZE, cipher };
285 ret = TestUpdateFinish(&handleEncrypt, encryptParamSet, HKS_KEY_PURPOSE_ENCRYPT, &inData, &cipherText);
286 EXPECT_NE(HksMemCmp(inData.data, cipherText.data, inData.size), HKS_SUCCESS) << "cipherText equals inData";
287
288 /* 4. Decrypt Three Stage */
289 struct HksParamSet *decryptParamSet = nullptr;
290 ret = InitParamSet(&decryptParamSet, g_decryptParams042, sizeof(g_decryptParams042)/sizeof(HksParam));
291 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
292 // Init
293 uint8_t handleD[sizeof(uint64_t)] = {0};
294 struct HksBlob handleDecrypt = { sizeof(uint64_t), handleD };
295 ret = HksInit(&keyAlias, decryptParamSet, &handleDecrypt);
296 EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
297 // Update loop
298 ret = HksTestUpdate(&handleDecrypt, decryptParamSet, &cipherText);
299 EXPECT_EQ(ret, HKS_SUCCESS) << "Update failed.";
300 // Abort
301 ret = HksAbort(&handleDecrypt, decryptParamSet);
302 EXPECT_EQ(ret, HKS_SUCCESS) << "Abort failed.";
303
304 /* 5. Delete Key */
305 ret = HksDeleteKey(&keyAlias, genParamSet);
306 EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
307 ret = HksDeleteKey(&newKeyAlias, encryptParamSet);
308 EXPECT_EQ(ret, HKS_SUCCESS) << "Delete ImportKey failed.";
309
310 HksFreeParamSet(&genParamSet);
311 HksFreeParamSet(&encryptParamSet);
312 HksFreeParamSet(&decryptParamSet);
313 }
314
315 /**
316 * @tc.name: HksRsaCipherPart5Test.HksRsaCipherPart5Test044
317 * @tc.desc: alg-RSA pur-ENCRYPT-DECRYPT size-4096 pad-OAEP dig-SHA512 mode-ECB. Encrypt-Abort
318 * @tc.type: FUNC
319 */
320 HWTEST_F(HksRsaCipherPart5Test, HksRsaCipherPart5Test044, TestSize.Level1)
321 {
322 char tmpKeyAlias[] = "HksRSACipherKeyAliasTest044";
323 struct HksBlob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
324 struct HksBlob inData = { Unittest::RsaCipher::g_inData_32.length(),
325 (uint8_t *)Unittest::RsaCipher::g_inData_32.c_str() };
326 int32_t ret = HKS_FAILURE;
327
328 /* 1. Generate Key */
329 struct HksParamSet *genParamSet = nullptr;
330 ret = InitParamSet(&genParamSet, g_genParams042, sizeof(g_genParams042)/sizeof(HksParam));
331 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(gen) failed.";
332 ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
333 EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
334
335 /* 2. Export Public Key */
336 uint8_t tmpPublicKey[HKS_RSA_KEY_SIZE_4096] = {0};
337 struct HksBlob publicKey = { HKS_RSA_KEY_SIZE_4096, (uint8_t *)tmpPublicKey };
338 ret = HksExportPublicKey(&keyAlias, genParamSet, &publicKey);
339
340 /* 3. Encrypt Three Stage */
341 struct HksParamSet *encryptParamSet = nullptr;
342 ret = InitParamSet(&encryptParamSet, g_encryptParams042, sizeof(g_encryptParams042)/sizeof(HksParam));
343 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
344
345 /* Import Key */
346 char tmpKey[] = "RSA_Encrypt_Decrypt_KeyAlias";
347 struct HksBlob newKeyAlias = { .size = strlen(tmpKey), .data = (uint8_t *)tmpKey };
348 ret = HksImportKey(&newKeyAlias, encryptParamSet, &publicKey);
349 // Init
350 uint8_t handleE[sizeof(uint64_t)] = {0};
351 struct HksBlob handleEncrypt = { sizeof(uint64_t), handleE };
352 ret = HksInit(&newKeyAlias, encryptParamSet, &handleEncrypt);
353 EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
354 // Update loop
355 ret = HksTestUpdate(&handleEncrypt, encryptParamSet, &inData);
356 EXPECT_EQ(ret, HKS_SUCCESS) << "Update failed.";
357 // Abort
358 ret = HksAbort(&handleEncrypt, encryptParamSet);
359 EXPECT_EQ(ret, HKS_SUCCESS) << "Abort failed.";
360
361 /* 4. Delete Key */
362 ret = HksDeleteKey(&keyAlias, genParamSet);
363 EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
364 ret = HksDeleteKey(&newKeyAlias, encryptParamSet);
365 EXPECT_EQ(ret, HKS_SUCCESS) << "Delete ImportKey failed.";
366
367 HksFreeParamSet(&genParamSet);
368 HksFreeParamSet(&encryptParamSet);
369 }
370
371 /**
372 * @tc.name: HksRsaCipherPart5Test.HksRsaCipherPart5Test045
373 * @tc.desc: alg-RSA pur-ENCRYPT-DECRYPT size-4096 pad-OAEP dig-SHA512 mode-ECB. abnormal
374 * @tc.type: FUNC
375 */
376 HWTEST_F(HksRsaCipherPart5Test, HksRsaCipherPart5Test045, TestSize.Level1)
377 {
378 char tmpKeyAlias[] = "HksRSACipherKeyAliasTest045";
379 struct HksBlob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
380 int32_t ret = HKS_FAILURE;
381
382 /* 1. Generate Key */
383 struct HksParamSet *genParamSet = nullptr;
384 ret = InitParamSet(&genParamSet, g_genParams042, sizeof(g_genParams042)/sizeof(HksParam));
385 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(gen) failed.";
386 ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
387 EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
388
389 /* 2. Export Public Key */
390 uint8_t tmpPublicKey[HKS_RSA_KEY_SIZE_4096] = {0};
391 struct HksBlob publicKey = { HKS_RSA_KEY_SIZE_4096, (uint8_t *)tmpPublicKey };
392 ret = HksExportPublicKey(&keyAlias, genParamSet, &publicKey);
393
394 /* 3. Encrypt Three Stage */
395 struct HksParamSet *encryptParamSet = nullptr;
396 ret = InitParamSet(&encryptParamSet, g_encryptParams042, sizeof(g_encryptParams042)/sizeof(HksParam));
397 EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
398
399 /* Import Key */
400 char tmpKey[] = "RSA_Encrypt_Decrypt_KeyAlias";
401 struct HksBlob newKeyAlias = { .size = strlen(tmpKey), .data = (uint8_t *)tmpKey };
402 ret = HksImportKey(&newKeyAlias, encryptParamSet, &publicKey);
403 // Init
404 uint8_t handleE[sizeof(uint64_t)] = {0};
405 struct HksBlob handleEncrypt = { sizeof(uint64_t), handleE };
406 ret = HksInit(NULL, encryptParamSet, &handleEncrypt);
407 EXPECT_NE(ret, HKS_SUCCESS) << "Init should failed.";
408
409 /* 4. Delete Key */
410 ret = HksDeleteKey(&keyAlias, genParamSet);
411 EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
412 ret = HksDeleteKey(&newKeyAlias, encryptParamSet);
413 EXPECT_EQ(ret, HKS_SUCCESS) << "Delete ImportKey failed.";
414
415 HksFreeParamSet(&genParamSet);
416 HksFreeParamSet(&encryptParamSet);
417 }
418 }