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 <gtest/gtest.h>
17
18 #include "cm_test_common.h"
19
20 #include "cert_manager_api.h"
21
22 #include "cm_log.h"
23 #include "cm_mem.h"
24
25 using namespace testing::ext;
26 using namespace CertmanagerTest;
27 namespace {
28 static constexpr uint32_t DEFAULT_SIGNATURE_LEN = 1024;
29 static constexpr uint32_t MAX_SESSION_NUM_MORE_1 = 16; /* max session count is 15 */
30
31 class CmFinishTest : 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 CmFinishTest::SetUpTestCase(void)
43 {
44 SetATPermission();
45 }
46
TearDownTestCase(void)47 void CmFinishTest::TearDownTestCase(void)
48 {
49 }
50
SetUp()51 void CmFinishTest::SetUp()
52 {
53 }
54
TearDown()55 void CmFinishTest::TearDown()
56 {
57 }
58
59 static const uint8_t g_uriData[] = "oh:t=ak;o=TestFinishSignVerify;u=0;a=0";
60 static const CmBlob g_keyUri = { sizeof(g_uriData), (uint8_t *)g_uriData };
61
62 static const uint8_t g_uriDataSysCred[] = "oh:t=sk;o=TestFinishSignVerify;u=100;a=0";
63 static const CmBlob g_keyUriSysCred = { sizeof(g_uriDataSysCred), (uint8_t *)g_uriDataSysCred };
64
65 static const uint8_t g_messageData[] = "This_is_test_message_for_test_sign_and_verify";
66
TestInstallAppCert(uint32_t alg,uint32_t store)67 static void TestInstallAppCert(uint32_t alg, uint32_t store)
68 {
69 uint8_t aliasData[] = "TestFinishSignVerify";
70 struct CmBlob alias = { sizeof(aliasData), aliasData };
71
72 int32_t ret = TestGenerateAppCert(&alias, alg, store);
73 EXPECT_EQ(ret, CM_SUCCESS) << "TestGenerateAppCert failed, retcode:" << ret;
74 }
75
TestUninstallAppCert(uint32_t store)76 static void TestUninstallAppCert(uint32_t store)
77 {
78 int32_t ret = CmUninstallAppCert(store == CM_SYS_CREDENTIAL_STORE ? &g_keyUriSysCred : &g_keyUri, store);
79 EXPECT_EQ(ret, CM_SUCCESS) << "CmUninstallAppCert failed, retcode:" << ret;
80 }
81
TestSign(const struct CmBlob * keyUri,const struct CmSignatureSpec * spec,const struct CmBlob * message,struct CmBlob * signature)82 static void TestSign(const struct CmBlob *keyUri, const struct CmSignatureSpec *spec,
83 const struct CmBlob *message, struct CmBlob *signature)
84 {
85 uint64_t handleValue = 0;
86 struct CmBlob handle = { sizeof(uint64_t), (uint8_t *)&handleValue };
87
88 int32_t ret = CmInit(keyUri, spec, &handle);
89 EXPECT_EQ(ret, CM_SUCCESS) << "TestSign CmInit test failed";
90
91 ret = CmUpdate(&handle, message);
92 EXPECT_EQ(ret, CM_SUCCESS) << "TestSign CmUpdate test failed";
93
94 struct CmBlob inDataFinish = { 0, nullptr };
95 ret = CmFinish(&handle, &inDataFinish, signature);
96 EXPECT_EQ(ret, CM_SUCCESS) << "TestSign CmFinish test failed";
97
98 ret = CmAbort(&handle);
99 EXPECT_EQ(ret, CM_SUCCESS) << "TestSign CmAbort test failed";
100 }
101
TestVerify(const struct CmBlob * keyUri,const struct CmSignatureSpec * spec,const struct CmBlob * message,const struct CmBlob * signature,bool isValidSignature)102 static void TestVerify(const struct CmBlob *keyUri, const struct CmSignatureSpec *spec,
103 const struct CmBlob *message, const struct CmBlob *signature, bool isValidSignature)
104 {
105 uint64_t handleValue = 0;
106 struct CmBlob handle = { sizeof(uint64_t), (uint8_t *)&handleValue };
107
108 int32_t ret = CmInit(keyUri, spec, &handle);
109 EXPECT_EQ(ret, CM_SUCCESS) << "TestVerify CmInit test failed";
110
111 ret = CmUpdate(&handle, message);
112 EXPECT_EQ(ret, CM_SUCCESS) << "TestVerify CmUpdate test failed";
113
114 struct CmBlob inDataFinish = { signature->size, signature->data };
115 if (!isValidSignature && signature->size > 0) {
116 signature->data[0] += 0x01; /* change the first byte of signature, ignore data flipping */
117 }
118
119 struct CmBlob outDataFinish = { 0, nullptr };
120 ret = CmFinish(&handle, &inDataFinish, &outDataFinish);
121 if (isValidSignature) {
122 EXPECT_EQ(ret, CM_SUCCESS) << "TestVerify CmFinish test failed";
123 } else {
124 EXPECT_EQ(ret, CMR_ERROR_KEY_OPERATION_FAILED) << "TestVerify CmFinish test failed";
125 }
126
127 ret = CmAbort(&handle);
128 EXPECT_EQ(ret, CM_SUCCESS) << "TestVerify CmAbort test failed";
129 }
130
131
SignVerify(const struct CmBlob * keyUri,bool isValidSignature,struct CmSignatureSpec * spec)132 static void SignVerify(const struct CmBlob *keyUri, bool isValidSignature, struct CmSignatureSpec *spec)
133 {
134 struct CmBlob message = { 0, nullptr };
135 uint8_t srcData[] = {
136 0xc2, 0xa7, 0xc5, 0x33, 0x79, 0xb0, 0xcd, 0x86, 0x74, 0x09, 0x98, 0x16, 0xd5, 0x85, 0x1b, 0xd6,
137 0x87, 0xe3, 0xe0, 0x53, 0x7d, 0xe0, 0xff, 0x1d, 0xdb, 0x27, 0x98, 0xe8, 0x87, 0xe5, 0xb7, 0x03,
138 };
139 if (spec->digest != CM_DIGEST_NONE) {
140 message.size = sizeof(g_messageData);
141 message.data = const_cast<uint8_t *>(g_messageData);
142 } else {
143 message.size = sizeof(srcData);
144 message.data = srcData;
145 }
146
147 uint8_t signData[DEFAULT_SIGNATURE_LEN] = {0};
148 struct CmBlob signature = { DEFAULT_SIGNATURE_LEN, signData };
149
150 /* sign */
151 spec->purpose = CM_KEY_PURPOSE_SIGN;
152 TestSign(keyUri, spec, &message, &signature);
153
154 /* verify */
155 spec->purpose = CM_KEY_PURPOSE_VERIFY;
156 TestVerify(keyUri, spec, &message, &signature, isValidSignature);
157 }
158
TestSignVerify(uint32_t alg,bool isValidSignature,struct CmSignatureSpec * spec)159 static void TestSignVerify(uint32_t alg, bool isValidSignature, struct CmSignatureSpec *spec)
160 {
161 TestInstallAppCert(alg, CM_CREDENTIAL_STORE);
162 SignVerify(&g_keyUri, isValidSignature, spec);
163 TestUninstallAppCert(CM_CREDENTIAL_STORE);
164 }
165
TestSignVerifyWithCredType(uint32_t alg,uint32_t store,bool isValidSignature,struct CmSignatureSpec * spec)166 static void TestSignVerifyWithCredType(uint32_t alg, uint32_t store, bool isValidSignature,
167 struct CmSignatureSpec *spec)
168 {
169 TestInstallAppCert(alg, store);
170 SignVerify(store == CM_SYS_CREDENTIAL_STORE ? &g_keyUriSysCred : &g_keyUri, isValidSignature, spec);
171 TestUninstallAppCert(store);
172 }
173
ProducerSessionMaxTest(void)174 static void ProducerSessionMaxTest(void)
175 {
176 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
177 uint64_t handle[MAX_SESSION_NUM_MORE_1];
178 int32_t ret;
179
180 for (uint32_t i = 0; i < MAX_SESSION_NUM_MORE_1; ++i) {
181 struct CmBlob handleBlob = { sizeof(uint64_t), (uint8_t *)&handle[i] };
182 ret = CmInit(&g_keyUri, &spec, &handleBlob);
183 EXPECT_EQ(ret, CM_SUCCESS) << "cm init failed, index[" << i << "]";
184 }
185
186 for (uint32_t i = 0; i < MAX_SESSION_NUM_MORE_1; ++i) {
187 uint8_t tmpInput[] = "thisIstestForSessionMaxTestInData";
188 struct CmBlob updateInput = { sizeof(tmpInput), tmpInput };
189 struct CmBlob handleBlob = { sizeof(uint64_t), (uint8_t *)&handle[i] };
190
191 int32_t expectRet = CM_SUCCESS;
192 if (i == 0) {
193 expectRet = CMR_ERROR_NOT_EXIST;
194 }
195 ret = CmUpdate(&handleBlob, &updateInput);
196 EXPECT_EQ(ret, expectRet) << "update failed, i:" << i;
197
198 uint8_t tmpOutput[DEFAULT_SIGNATURE_LEN] = {0};
199 struct CmBlob finishInput = { 0, nullptr };
200 struct CmBlob finishOutput = { sizeof(tmpOutput), tmpOutput };
201 ret = CmFinish(&handleBlob, &finishInput, &finishOutput);
202 EXPECT_EQ(ret, expectRet) << "finish failed, i:" << i;
203 }
204
205 for (uint32_t i = 0; i < MAX_SESSION_NUM_MORE_1; ++i) {
206 struct CmBlob handleBlob = { sizeof(uint64_t), (uint8_t *)&handle[i] };
207 ret = CmAbort(&handleBlob);
208 EXPECT_EQ(ret, CM_SUCCESS) << "abort failed, i:" << i;
209 }
210 }
211
212
213 /**
214 * @tc.name: CmFinishTest001
215 * @tc.desc: Test CmFinish handle is null
216 * @tc.type: FUNC
217 * @tc.require: AR000H0MIA /SR000H09NA
218 */
219 HWTEST_F(CmFinishTest, CmFinishTest001, TestSize.Level0)
220 {
221 struct CmBlob *handle = nullptr;
222 struct CmBlob inData = { 0, nullptr };
223 struct CmBlob outData = { 0, nullptr };
224
225 int32_t ret = CmFinish(handle, &inData, &outData);
226 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
227 }
228
229 /**
230 * @tc.name: CmFinishTest002
231 * @tc.desc: Test CmFinish handle size is 0
232 * @tc.type: FUNC
233 * @tc.require: AR000H0MIA /SR000H09NA
234 */
235 HWTEST_F(CmFinishTest, CmFinishTest002, TestSize.Level0)
236 {
237 uint64_t handleValue = 0;
238 struct CmBlob handle = { 0, (uint8_t *)&handleValue };
239 struct CmBlob inData = { 0, nullptr };
240 struct CmBlob outData = { 0, nullptr };
241
242 int32_t ret = CmFinish(&handle, &inData, &outData);
243 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
244 }
245
246 /**
247 * @tc.name: CmFinishTest003
248 * @tc.desc: Test CmFinish handle data is null
249 * @tc.type: FUNC
250 * @tc.require: AR000H0MIA /SR000H09NA
251 */
252 HWTEST_F(CmFinishTest, CmFinishTest003, TestSize.Level0)
253 {
254 uint64_t handleValue = 0;
255 struct CmBlob handle = { sizeof(handleValue), nullptr };
256 struct CmBlob inData = { 0, nullptr };
257 struct CmBlob outData = { 0, nullptr };
258
259 int32_t ret = CmFinish(&handle, &inData, &outData);
260 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
261 }
262
263 /**
264 * @tc.name: CmFinishTest004
265 * @tc.desc: Test CmFinish inData is null
266 * @tc.type: FUNC
267 * @tc.require: AR000H0MIA /SR000H09NA
268 */
269 HWTEST_F(CmFinishTest, CmFinishTest004, TestSize.Level0)
270 {
271 uint64_t handleValue = 0;
272 struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
273 struct CmBlob *inData = nullptr;
274 struct CmBlob outData = { 0, nullptr };
275
276 int32_t ret = CmFinish(&handle, inData, &outData);
277 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
278 }
279
280 /**
281 * @tc.name: CmFinishTest005
282 * @tc.desc: Test CmFinish outData is null
283 * @tc.type: FUNC
284 * @tc.require: AR000H0MIA /SR000H09NA
285 */
286 HWTEST_F(CmFinishTest, CmFinishTest005, TestSize.Level0)
287 {
288 uint64_t handleValue = 0;
289 struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
290 struct CmBlob inData = { 0, nullptr };
291 struct CmBlob *outData = nullptr;
292
293 int32_t ret = CmFinish(&handle, &inData, outData);
294 EXPECT_EQ(ret, CMR_ERROR_INVALID_ARGUMENT);
295 }
296
297 /**
298 * @tc.name: CmFinishTest006
299 * @tc.desc: Test CmFinish handle not exist
300 * @tc.type: FUNC
301 * @tc.require: AR000H0MIA /SR000H09NA
302 */
303 HWTEST_F(CmFinishTest, CmFinishTest006, TestSize.Level0)
304 {
305 uint64_t handleValue = 0;
306 struct CmBlob handle = { sizeof(handleValue), (uint8_t *)&handleValue };
307 struct CmBlob inData = { 0, nullptr };
308 struct CmBlob outData = { 0, nullptr };
309
310 int32_t ret = CmFinish(&handle, &inData, &outData);
311 EXPECT_EQ(ret, CMR_ERROR_NOT_EXIST);
312 }
313
314 /**
315 * @tc.name: CmFinishTest007
316 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, pss sha256
317 * @tc.type: FUNC
318 * @tc.require: AR000H0MIA /SR000H09NA
319 */
320 HWTEST_F(CmFinishTest, CmFinishTest007, TestSize.Level0)
321 {
322 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
323 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
324 }
325
326 /**
327 * @tc.name: CmFinishTest008
328 * @tc.desc: Test CmFinish normal case: caller is producer, ecc sign verify, sha256
329 * @tc.type: FUNC
330 * @tc.require: AR000H0MIA /SR000H09NA
331 */
332 HWTEST_F(CmFinishTest, CmFinishTest008, TestSize.Level0)
333 {
334 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SHA256 };
335 TestSignVerify(CERT_KEY_ALG_ECC, true, &spec);
336 }
337
338 /**
339 * @tc.name: CmFinishTest009
340 * @tc.desc: Test CmFinish abnormal case: caller is producer, rsa sign verify(sign invalid)
341 * @tc.type: FUNC
342 * @tc.require: AR000H0MIA /SR000H09NA
343 */
344 HWTEST_F(CmFinishTest, CmFinishTest009, TestSize.Level0)
345 {
346 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
347 TestSignVerify(CERT_KEY_ALG_RSA, false, &spec);
348 }
349
350 /**
351 * @tc.name: CmFinishTest010
352 * @tc.desc: Test CmFinish abnormal case: caller is producer, ecc sign verify(sign invalid)
353 * @tc.type: FUNC
354 * @tc.require: AR000H0MIA /SR000H09NA
355 */
356 HWTEST_F(CmFinishTest, CmFinishTest010, TestSize.Level0)
357 {
358 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SHA256 };
359 TestSignVerify(CERT_KEY_ALG_ECC, false, &spec);
360 }
361
362 /**
363 * @tc.name: CmFinishTest011
364 * @tc.desc: Test CmFinish normal case: normal case: caller is producer, max times + 1(first fail)
365 * @tc.type: FUNC
366 * @tc.require: AR000H0MIA /SR000H09NA
367 */
368 HWTEST_F(CmFinishTest, CmFinishTest011, TestSize.Level0)
369 {
370 TestInstallAppCert(CERT_KEY_ALG_ECC, CM_CREDENTIAL_STORE);
371 ProducerSessionMaxTest();
372 TestUninstallAppCert(CM_CREDENTIAL_STORE);
373 }
374
375 /**
376 * @tc.name: CmFinishTestPerformance012
377 * @tc.desc: 1000 times normal case: caller is producer, ecc sign verify
378 * @tc.type: FUNC
379 * @tc.require: AR000H0MIA /SR000H09NA
380 */
381 HWTEST_F(CmFinishTest, CmFinishTestPerformance012, TestSize.Level1)
382 {
383 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
384 for (uint32_t i = 0; i < PERFORMACE_COUNT; ++i) {
385 TestSignVerify(CERT_KEY_ALG_ECC, true, &spec);
386 }
387 }
388
389 /**
390 * @tc.name: CmFinishTestPerformance013
391 * @tc.desc: 1000 times normal case: caller is producer, rsa sign verify
392 * @tc.type: FUNC
393 * @tc.require: AR000H0MIA /SR000H09NA
394 */
395 HWTEST_F(CmFinishTest, CmFinishTestPerformance013, TestSize.Level1)
396 {
397 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA256 };
398 for (uint32_t i = 0; i < PERFORMACE_COUNT; ++i) {
399 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
400 }
401 }
402
403 /**
404 * @tc.name: CmFinishTest014
405 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, pss, sha512
406 * @tc.type: FUNC
407 * @tc.require: AR000H0MIA /SR000H09NA
408 */
409 HWTEST_F(CmFinishTest, CmFinishTest014, TestSize.Level0)
410 {
411 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA512 };
412 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
413 }
414
415 /**
416 * @tc.name: CmFinishTest015
417 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, pss, sha384
418 * @tc.type: FUNC
419 * @tc.require: AR000H0MIA /SR000H09NA
420 */
421 HWTEST_F(CmFinishTest, CmFinishTest015, TestSize.Level0)
422 {
423 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA384 };
424 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
425 }
426
427 #ifdef DEPS_HKS_UNTRUSTED_RUNNING_ENV
428 /**
429 * @tc.name: CmFinishTest016
430 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, pss, sha224
431 * @tc.type: FUNC
432 * @tc.require: AR000H0MIA /SR000H09NA
433 */
434 HWTEST_F(CmFinishTest, CmFinishTest016, TestSize.Level0)
435 {
436 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA224 };
437 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
438 }
439
440 /**
441 * @tc.name: CmFinishTest017
442 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, pss, sha1
443 * @tc.type: FUNC
444 * @tc.require: AR000H0MIA /SR000H09NA
445 */
446 HWTEST_F(CmFinishTest, CmFinishTest017, TestSize.Level0)
447 {
448 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA1 };
449 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
450 }
451 #endif
452
453 /**
454 * @tc.name: CmFinishTest018
455 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, pkcs1, nosha
456 * @tc.type: FUNC
457 * @tc.require: AR000H0MIA /SR000H09NA
458 */
459 HWTEST_F(CmFinishTest, CmFinishTest018, TestSize.Level0)
460 {
461 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PKCS1_V1_5, CM_DIGEST_NONE };
462 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
463 }
464
465 #ifdef DEPS_HKS_UNTRUSTED_RUNNING_ENV
466 /**
467 * @tc.name: CmFinishTest019
468 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, pkcs1, md5
469 * @tc.type: FUNC
470 * @tc.require: AR000H0MIA /SR000H09NA
471 */
472 HWTEST_F(CmFinishTest, CmFinishTest019, TestSize.Level0)
473 {
474 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PKCS1_V1_5, CM_DIGEST_MD5 };
475 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
476 }
477
478 /**
479 * @tc.name: CmFinishTest020
480 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, pkcs1, sha224
481 * @tc.type: FUNC
482 * @tc.require: AR000H0MIA /SR000H09NA
483 */
484 HWTEST_F(CmFinishTest, CmFinishTest020, TestSize.Level0)
485 {
486 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PKCS1_V1_5, CM_DIGEST_SHA224 };
487 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
488 }
489 #endif
490
491 /**
492 * @tc.name: CmFinishTest021
493 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, pkcs1, sha256
494 * @tc.type: FUNC
495 * @tc.require: AR000H0MIA /SR000H09NA
496 */
497 HWTEST_F(CmFinishTest, CmFinishTest021, TestSize.Level0)
498 {
499 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PKCS1_V1_5, CM_DIGEST_SHA256 };
500 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
501 }
502
503 /**
504 * @tc.name: CmFinishTest022
505 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, pkcs1, sha384
506 * @tc.type: FUNC
507 * @tc.require: AR000H0MIA /SR000H09NA
508 */
509 HWTEST_F(CmFinishTest, CmFinishTest022, TestSize.Level0)
510 {
511 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PKCS1_V1_5, CM_DIGEST_SHA384 };
512 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
513 }
514
515 /**
516 * @tc.name: CmFinishTest023
517 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, pkcs1, sha512
518 * @tc.type: FUNC
519 * @tc.require: AR000H0MIA /SR000H09NA
520 */
521 HWTEST_F(CmFinishTest, CmFinishTest023, TestSize.Level0)
522 {
523 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PKCS1_V1_5, CM_DIGEST_SHA512 };
524 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
525 }
526
527 #ifdef DEPS_HKS_UNTRUSTED_RUNNING_ENV
528 /**
529 * @tc.name: CmFinishTest024
530 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, pkcs1, sha1
531 * @tc.type: FUNC
532 * @tc.require: AR000H0MIA /SR000H09NA
533 */
534 HWTEST_F(CmFinishTest, CmFinishTest024, TestSize.Level0)
535 {
536 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PKCS1_V1_5, CM_DIGEST_SHA1 };
537 TestSignVerify(CERT_KEY_ALG_RSA, true, &spec);
538 }
539
540 /**
541 * @tc.name: CmFinishTest025
542 * @tc.desc: Test CmFinish normal case: caller is producer, ecc sign verify, sha1
543 * @tc.type: FUNC
544 * @tc.require: AR000H0MIA /SR000H09NA
545 */
546 HWTEST_F(CmFinishTest, CmFinishTest025, TestSize.Level0)
547 {
548 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SHA1 };
549 TestSignVerify(CERT_KEY_ALG_ECC, true, &spec);
550 }
551
552 /**
553 * @tc.name: CmFinishTest026
554 * @tc.desc: Test CmFinish normal case: caller is producer, ecc sign verify, sha224
555 * @tc.type: FUNC
556 * @tc.require: AR000H0MIA /SR000H09NA
557 */
558 HWTEST_F(CmFinishTest, CmFinishTest026, TestSize.Level0)
559 {
560 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SHA224 };
561 TestSignVerify(CERT_KEY_ALG_ECC, true, &spec);
562 }
563 #endif
564
565 /**
566 * @tc.name: CmFinishTest027
567 * @tc.desc: Test CmFinish normal case: caller is producer, ecc sign verify, sha384
568 * @tc.type: FUNC
569 * @tc.require: AR000H0MIA /SR000H09NA
570 */
571 HWTEST_F(CmFinishTest, CmFinishTest027, TestSize.Level0)
572 {
573 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SHA384 };
574 TestSignVerify(CERT_KEY_ALG_ECC, true, &spec);
575 }
576
577 /**
578 * @tc.name: CmFinishTest028
579 * @tc.desc: Test CmFinish normal case: caller is producer, ecc sign verify, sha512
580 * @tc.type: FUNC
581 * @tc.require: AR000H0MIA /SR000H09NA
582 */
583 HWTEST_F(CmFinishTest, CmFinishTest028, TestSize.Level0)
584 {
585 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SHA512 };
586 TestSignVerify(CERT_KEY_ALG_ECC, true, &spec);
587 }
588
589 #ifdef DEPS_HKS_UNTRUSTED_RUNNING_ENV
590 /**
591 * @tc.name: CmFinishTest029
592 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, 512
593 * @tc.type: FUNC
594 * @tc.require: AR000H0MIA /SR000H09NA
595 */
596 HWTEST_F(CmFinishTest, CmFinishTest029, TestSize.Level0)
597 {
598 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA224 };
599 TestSignVerify(CERT_KEY_ALG_RSA_512, true, &spec);
600 }
601
602 /**
603 * @tc.name: CmFinishTest030
604 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, 1024
605 * @tc.type: FUNC
606 * @tc.require: AR000H0MIA /SR000H09NA
607 */
608 HWTEST_F(CmFinishTest, CmFinishTest030, TestSize.Level0)
609 {
610 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PSS, CM_DIGEST_SHA384 };
611 TestSignVerify(CERT_KEY_ALG_RSA_1024, true, &spec);
612 }
613 #endif
614
615 /**
616 * @tc.name: CmFinishTest031
617 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, 3072
618 * @tc.type: FUNC
619 * @tc.require: AR000H0MIA /SR000H09NA
620 */
621 HWTEST_F(CmFinishTest, CmFinishTest031, TestSize.Level0)
622 {
623 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PKCS1_V1_5, CM_DIGEST_NONE };
624 TestSignVerify(CERT_KEY_ALG_RSA_3072, true, &spec);
625 }
626
627 /**
628 * @tc.name: CmFinishTest032
629 * @tc.desc: Test CmFinish normal case: caller is producer, rsa sign verify, 4096
630 * @tc.type: FUNC
631 * @tc.require: AR000H0MIA /SR000H09NA
632 */
633 HWTEST_F(CmFinishTest, CmFinishTest032, TestSize.Level0)
634 {
635 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, CM_PADDING_PKCS1_V1_5, CM_DIGEST_SHA512 };
636 TestSignVerify(CERT_KEY_ALG_RSA_4096, true, &spec);
637 }
638
639 #ifdef DEPS_HKS_UNTRUSTED_RUNNING_ENV
640 /**
641 * @tc.name: CmFinishTest033
642 * @tc.desc: Test CmFinish normal case: caller is producer, ecc sign verify, P224
643 * @tc.type: FUNC
644 * @tc.require: AR000H0MIA /SR000H09NA
645 */
646 HWTEST_F(CmFinishTest, CmFinishTest033, TestSize.Level0)
647 {
648 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SHA256 };
649 TestSignVerify(CERT_KEY_ALG_ECC_P224, true, &spec);
650 }
651 #endif
652
653 /**
654 * @tc.name: CmFinishTest034
655 * @tc.desc: Test CmFinish normal case: caller is producer, ecc sign verify, P384
656 * @tc.type: FUNC
657 * @tc.require: AR000H0MIA /SR000H09NA
658 */
659 HWTEST_F(CmFinishTest, CmFinishTest034, TestSize.Level0)
660 {
661 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SHA384 };
662 TestSignVerify(CERT_KEY_ALG_ECC_P384, true, &spec);
663 }
664
665 /**
666 * @tc.name: CmFinishTest035
667 * @tc.desc: Test CmFinish normal case: caller is producer, ecc sign verify, P521
668 * @tc.type: FUNC
669 * @tc.require: AR000H0MIA /SR000H09NA
670 */
671 HWTEST_F(CmFinishTest, CmFinishTest035, TestSize.Level0)
672 {
673 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SHA512 };
674 TestSignVerify(CERT_KEY_ALG_ECC_P521, true, &spec);
675 }
676
677 /**
678 * @tc.name: CmFinishTest036
679 * @tc.desc: Test CmFinish normal case: caller is producer, ed25519 sign verify
680 * @tc.type: FUNC
681 * @tc.require: AR000H0MIA /SR000H09NA
682 */
683 HWTEST_F(CmFinishTest, CmFinishTest036, TestSize.Level0)
684 {
685 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, 0 };
686 TestSignVerify(CERT_KEY_ALG_ED25519, true, &spec);
687 }
688
689 /**
690 * @tc.name: CmFinishTest037
691 * @tc.desc: Test CmFinish normal case: caller is producer, sm2 sign verify, sm3, public cred
692 * @tc.type: FUNC
693 */
694 HWTEST_F(CmFinishTest, CmFinishTest037, TestSize.Level0)
695 {
696 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SM3 };
697 TestSignVerify(CERT_KEY_ALG_SM2_1, true, &spec);
698 }
699
700 /**
701 * @tc.name: CmFinishTest038
702 * @tc.desc: Test CmFinish normal case: caller is producer, sm2 sign verify, sm3 with data2, public cred
703 * @tc.type: FUNC
704 */
705 HWTEST_F(CmFinishTest, CmFinishTest038, TestSize.Level0)
706 {
707 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SM3 };
708 TestSignVerify(CERT_KEY_ALG_SM2_2, true, &spec);
709 }
710
711 /**
712 * @tc.name: CmFinishTest039
713 * @tc.desc: Test CmFinish normal case: caller is producer, sm2 sign verify, sm3 with data2, private cred
714 * @tc.type: FUNC
715 */
716 HWTEST_F(CmFinishTest, CmFinishTest039, TestSize.Level0)
717 {
718 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SM3 };
719 TestSignVerifyWithCredType(CERT_KEY_ALG_SM2_2, CM_PRI_CREDENTIAL_STORE, true, &spec);
720 }
721
722 /**
723 * @tc.name: CmFinishTest040
724 * @tc.desc: Test CmFinish normal case: caller is producer, sm2 sign verify, sm3 with data2, system cred
725 * @tc.type: FUNC
726 */
727 HWTEST_F(CmFinishTest, CmFinishTest040, TestSize.Level0)
728 {
729 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SM3 };
730 TestSignVerifyWithCredType(CERT_KEY_ALG_SM2_2, CM_SYS_CREDENTIAL_STORE, true, &spec);
731 }
732
733 /**
734 * @tc.name: CmFinishTest041
735 * @tc.desc: Test CmFinish abnormal case: caller is producer, sm2 sign verify(sign invalid)
736 * @tc.type: FUNC
737 */
738 HWTEST_F(CmFinishTest, CmFinishTest041, TestSize.Level0)
739 {
740 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SM3 };
741 TestSignVerify(CERT_KEY_ALG_SM2_1, false, &spec);
742 }
743
744 /**
745 * @tc.name: CmFinishTest042
746 * @tc.desc: Test CmFinish normal case: caller is producer, sm2 sign verify, digest_none with data2, public cred
747 * @tc.type: FUNC
748 */
749 HWTEST_F(CmFinishTest, CmFinishTest042, TestSize.Level0)
750 {
751 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_NONE };
752 TestSignVerify(CERT_KEY_ALG_SM2_2, true, &spec);
753 }
754
755 /**
756 * @tc.name: CmFinishTestPerformance043
757 * @tc.desc: 1000 times normal case: caller is producer, sm2 sign verify
758 * @tc.type: FUNC
759 */
760 HWTEST_F(CmFinishTest, CmFinishTestPerformance043, TestSize.Level1)
761 {
762 struct CmSignatureSpec spec = { CM_KEY_PURPOSE_SIGN, 0, CM_DIGEST_SM3 };
763 for (uint32_t i = 0; i < PERFORMACE_COUNT; ++i) {
764 TestSignVerify(CERT_KEY_ALG_SM2_1, true, &spec);
765 }
766 }
767 } // end of namespace
768
769