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