1 /* Copyright (c) 2020-2021 Huawei Device Co., Ltd.
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14 #include "security_data_huks.h"
15 #include "hctest.h"
16 #include "hks_client.h"
17 #include "hks_types.h"
18 #include "securec.h"
19 #include <securec.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
HksStBlobInit1(struct hks_blob * blob,size_t nmemb,size_t size,uint8_t type)25 void HksStBlobInit1(struct hks_blob *blob, size_t nmemb, size_t size, uint8_t type)
26 {
27 if (blob == NULL || nmemb == 0 || size == 0) {
28 TEST_ASSERT_EQUAL_INT(0, 1);
29 return;
30 }
31 blob->data = (uint8_t *)calloc(nmemb, size);
32 if (blob->data == NULL) {
33 TEST_ASSERT_EQUAL_INT(0, 1);
34 return;
35 }
36 if (memset_s(blob->data, size, 0, size) != EOK) {
37 TEST_ASSERT_EQUAL_INT(0, 1);
38 }
39 blob->size = size;
40 blob->type = type;
41 }
HksBlobDestroyT1(struct hks_blob * blob)42 void HksBlobDestroyT1(struct hks_blob *blob)
43 {
44 if (blob == NULL) {
45 return;
46 }
47 if (blob && blob->data) {
48 memset_s(blob->data, blob->size, 0, blob->size);
49 HKS_FREE_PTR1(blob->data);
50 }
51 blob->data = NULL;
52 blob->size = 0;
53 blob->type = HKS_BLOB_TYPE_RAW;
54 }
TestSecShuksGenerateKeyNormal(void)55 int TestSecShuksGenerateKeyNormal(void)
56 {
57 #ifndef _CUT_AUTHENTICATE_
58 char testFileName[] = "keyalias1";
59 char testFileName1[] = "key_auth_id1";
60 struct hks_blob keyAlias;
61
62 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
63 keyAlias.data = (uint8_t *)testFileName;
64 keyAlias.size = sizeof(testFileName);
65
66 struct hks_key_param keyParam;
67 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
68 keyParam.key_auth_id.size = sizeof(testFileName1);
69 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
70 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
71 keyParam.key_len = 0;
72 keyParam.key_usage = 0;
73 keyParam.key_pad = 0;
74 int32_t status = hks_generate_key(&keyAlias, &keyParam);
75 TEST_ASSERT_EQUAL_INT(0, status);
76 #endif
77 return 0;
78 }
TestSecShuksDeleteKeyNormal(void)79 int TestSecShuksDeleteKeyNormal(void)
80 {
81 #ifndef _CUT_AUTHENTICATE_
82 char testFileName[] = "keyalias1";
83 struct hks_blob keyAlias = { 0 };
84
85 keyAlias.data = (uint8_t *)testFileName;
86 keyAlias.size = sizeof(testFileName);
87 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
88
89 int32_t status = hks_delete_key(&keyAlias);
90 TEST_ASSERT_EQUAL_INT(0, status);
91 #endif
92 return 0;
93 }
TestSecShuksExportPublicNormal(struct hks_blob * keyAlias,struct hks_blob * publicKey1)94 int TestSecShuksExportPublicNormal(struct hks_blob *keyAlias, struct hks_blob *publicKey1)
95 {
96 #ifndef _CUT_AUTHENTICATE_
97 int32_t status = hks_export_public_key(keyAlias, publicKey1);
98 TEST_ASSERT_EQUAL_INT(0, status);
99 #endif
100 return 0;
101 }
BuildBlobData(struct hks_blob * param,const char * str,uint8_t type,uint32_t size,uint8_t isDataNull)102 void BuildBlobData(struct hks_blob *param, const char *str, uint8_t type, uint32_t size, uint8_t isDataNull)
103 {
104 if (param == NULL) {
105 TEST_ASSERT_EQUAL_INT(0, 1);
106 return;
107 }
108 param->type = type;
109 param->size = size;
110 if (isDataNull == NUM1) {
111 param->data = NULL;
112 } else {
113 if (size == 0) {
114 return;
115 }
116 unsigned char *buff = (unsigned char *)malloc(size + NUM2);
117 if (buff == NULL) {
118 return;
119 }
120 memset_s(buff, size + NUM2, 0, size + NUM2);
121 HexStringToByte(str, size * NUM2, buff);
122 param->data = (uint8_t *)buff;
123 }
124 }
HexStringToByte(const char * str,int nLen,unsigned char * pHex)125 void HexStringToByte(const char *str, int nLen, unsigned char *pHex)
126 {
127 unsigned int number = 4;
128 if (pHex == NULL) {
129 TEST_ASSERT_EQUAL_INT(0, 1);
130 return;
131 }
132 if (nLen % NUM2) {
133 TEST_ASSERT_EQUAL_INT(0, 1);
134 }
135 int nHexLen = nLen / NUM2;
136 unsigned char nibble[2];
137 if (nHexLen >= MAX_INT) {
138 TEST_ASSERT_EQUAL_INT(0, 1);
139 return;
140 }
141 for (int i = 0; i < nHexLen; i++) {
142 nibble[0] = str[i * NUM2];
143 nibble[1] = str[i * NUM2 + NUM1];
144 for (int j = 0; j < NUM2; j++) {
145 if (nibble[j] <= 'F' && nibble[j] >= 'A') {
146 nibble[j] = nibble[j] - 'A' + NUM10;
147 } else if (nibble[j] <= 'f' && nibble[j] >= 'a') {
148 nibble[j] = nibble[j] - 'a' + NUM10;
149 } else if (nibble[j] >= '0' && nibble[j] <= '9') {
150 nibble[j] = nibble[j] - '0';
151 } else {
152 TEST_ASSERT_EQUAL_INT(0, 1);
153 }
154 }
155 pHex[i] = nibble[0] << number;
156 pHex[i] |= nibble[1];
157 }
158 }
159
TestInitKeyParam1(struct hks_key_param * keyParam,uint32_t keyType,uint32_t keyLen,uint32_t keyMode,uint32_t keyUsage)160 void TestInitKeyParam1(struct hks_key_param *keyParam, uint32_t keyType, uint32_t keyLen, uint32_t keyMode,
161 uint32_t keyUsage)
162 {
163 if (keyParam == NULL) {
164 TEST_ASSERT_EQUAL_INT(0, 1);
165 return;
166 }
167 keyParam->key_type = keyType;
168 keyParam->key_len = keyLen;
169 keyParam->key_pad = HKS_PADDING_NONE;
170 keyParam->key_mode = keyMode;
171 keyParam->key_domain = HKS_ECC_CURVE_ED25519;
172 keyParam->key_usage = keyUsage;
173
174 const char tmpData1[] = "111111";
175 BuildBlobData(&keyParam->key_auth_id, tmpData1, HKS_BLOB_TYPE_AUTH_ID, NUM3, 0);
176 }
177
TestSecShuksImportPublicNormal(void)178 int TestSecShuksImportPublicNormal(void)
179 {
180 #ifndef _CUT_AUTHENTICATE_
181 char testFileName[] = "keyalias1";
182 struct hks_key_param keyParam1 = { 0 };
183
184 keyParam1.key_type = HKS_KEY_TYPE_EDDSA_PUBLIC_KEY_ED25519;
185 keyParam1.key_len = NUM256;
186 keyParam1.key_usage = NUM3;
187 keyParam1.key_pad = NUM4;
188 keyParam1.key_mode = NUM5;
189 keyParam1.key_role = NUM6;
190 keyParam1.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
191 keyParam1.key_auth_id.data = (uint8_t *)testFileName;
192 keyParam1.key_auth_id.size = sizeof(testFileName);
193
194 struct hks_blob publicKey1 = { 0 };
195 HksStBlobInit1(&publicKey1, NUM1, NUM32, HKS_BLOB_TYPE_KEY);
196
197 struct hks_blob keyAlias;
198 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
199 keyAlias.data = (uint8_t *)testFileName;
200 keyAlias.size = sizeof(testFileName);
201
202 int32_t status = hks_import_public_key(&keyAlias, &keyParam1, &publicKey1);
203 TEST_ASSERT_EQUAL_INT(0, status);
204
205 HksBlobDestroyT1(&publicKey1);
206 #endif
207 return 0;
208 }
209
210 /* *
211 * @tc.desc : register a test suite, this suite is used to test basic flow and interface dependency
212 * @param : subsystem name is security
213 * @param : module name is securityData
214 * @param : test suit name is SecurityDataHuksGenDelTestSuite
215 */
216 LITE_TEST_SUIT(security, securityData, SecurityDataHuksGenDelTestSuite);
217
218 /* *
219 * @tc.setup : setup for all testcases
220 * @return : setup result, TRUE is success, FALSE is fail
221 */
SecurityDataHuksGenDelTestSuiteSetUp(void)222 static BOOL SecurityDataHuksGenDelTestSuiteSetUp(void)
223 {
224 int32_t status;
225 status = hks_init();
226 if (status != 0) {
227 status = hks_refresh_key_info();
228 }
229 TEST_ASSERT_EQUAL_INT(0, status);
230 return TRUE;
231 }
232
233 /* *
234 * @tc.teardown : teardown for all testcases
235 * @return : teardown result, TRUE is success, FALSE is fail
236 */
SecurityDataHuksGenDelTestSuiteTearDown(void)237 static BOOL SecurityDataHuksGenDelTestSuiteTearDown(void)
238 {
239 printf("-++++++++++++++++++++++++++++++++++++++++++++-\n");
240 return TRUE;
241 }
242
243 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++generate key
244 // begin+++++++++++++++++++++++++++++++++++++++++++++++++++0000-0120
245
246 /* *
247 * @tc.number : SUB_SEC_DataPro_HuksL0_0000
248 * @tc.name : Generate key, normal input parameters keyAlias and keyParam
249 * @tc.desc : [C- SECURITY -1400]
250 */
251 #ifndef _CUT_AUTHENTICATE_
252 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0000, Function | MediumTest | Level1)
253 {
254 char testFileName[] = "keyalias1";
255 char testFileName1[] = "key_auth_id1";
256 int32_t statusGenerate;
257 int32_t statusDelete;
258 struct hks_key_param keyParam;
259 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
260 keyParam.key_auth_id.size = sizeof(testFileName1);
261 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
262 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
263 keyParam.key_len = 0;
264 keyParam.key_usage = 0;
265 keyParam.key_pad = 0;
266 struct hks_blob keyAliasGenerate;
267 HksStBlobInit1(&keyAliasGenerate, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
268 if (memcpy_s(keyAliasGenerate.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
269 HksBlobDestroyT1(&keyAliasGenerate);
270 TEST_FAIL();
271 return;
272 }
273 char tmpGenerate[3] = { 0 };
274 if (strcat_s((char *)keyAliasGenerate.data,
275 strlen((char *)keyAliasGenerate.data) + strlen(tmpGenerate) + 1,
276 tmpGenerate) != EOK) {
277 HksBlobDestroyT1(&keyAliasGenerate);
278 TEST_FAIL();
279 return;
280 }
281 keyAliasGenerate.size = strlen((char *)keyAliasGenerate.data);
282 statusGenerate = hks_generate_key(&keyAliasGenerate, &keyParam);
283 TEST_ASSERT_EQUAL_INT(0, statusGenerate);
284 HksBlobDestroyT1(&keyAliasGenerate);
285 struct hks_blob keyAliasDelete;
286 HksStBlobInit1(&keyAliasDelete, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
287 if (memcpy_s(keyAliasDelete.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
288 HksBlobDestroyT1(&keyAliasDelete);
289 TEST_FAIL();
290 return;
291 }
292 char tmpDelete[3] = { 0 };
293 if (strcat_s((char *)keyAliasDelete.data,
294 strlen((char *)keyAliasDelete.data) + strlen(tmpDelete) + 1,
295 tmpDelete) != EOK) {
296 HksBlobDestroyT1(&keyAliasDelete);
297 TEST_FAIL();
298 return;
299 }
300 keyAliasDelete.size = strlen((char *)keyAliasDelete.data);
301 statusDelete = hks_delete_key(&keyAliasDelete);
302 TEST_ASSERT_EQUAL_INT(0, statusDelete);
303 HksBlobDestroyT1(&keyAliasDelete);
304 };
305
306 /* *
307 * @tc.number : SUB_SEC_DataPro_HuksL0_0010
308 * @tc.name : Generate key, abnormal input parameters keyAlias is null
309 * @tc.desc : [C- SECURITY -1400]
310 */
311 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0010, Function | MediumTest | Level2)
312 {
313 char testFileName1[] = "key_auth_id1";
314 struct hks_blob *keyAlias = NULL;
315 int32_t status;
316
317 struct hks_key_param keyParam;
318 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
319 keyParam.key_auth_id.size = sizeof(testFileName1);
320 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
321 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
322 keyParam.key_len = 0;
323 keyParam.key_usage = 0;
324 keyParam.key_pad = 0;
325
326 status = hks_generate_key(keyAlias, &keyParam);
327 TEST_ASSERT_EQUAL_INT(NUM1000, status);
328 }
329
330 /* *
331 * @tc.number : SUB_SEC_DataPro_HuksL0_0020
332 * @tc.name : Generate key, abnormal input parameters keyAlias.size is 0
333 * @tc.desc : [C- SECURITY -1400]
334 */
335 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0020, Function | MediumTest | Level2)
336 {
337 char testFileName[] = "keyalias1";
338 char testFileName1[] = "key_auth_id1";
339 struct hks_blob keyAlias;
340 int32_t status;
341
342 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
343 keyAlias.data = (uint8_t *)testFileName;
344 keyAlias.size = 0;
345
346 struct hks_key_param keyParam;
347 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
348 keyParam.key_auth_id.size = sizeof(testFileName1);
349 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
350 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
351 keyParam.key_len = 0;
352 keyParam.key_usage = 0;
353 keyParam.key_pad = 0;
354
355 status = hks_generate_key(&keyAlias, &keyParam);
356 TEST_ASSERT_EQUAL_INT(NUM135, status);
357 }
358
359 /* *
360 * @tc.number : SUB_SEC_DataPro_HuksL0_0030
361 * @tc.name : Generate key, abnormal input parameters keyAlias.size is more than 64
362 * @tc.desc : [C- SECURITY -1400]
363 */
364 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0030, Function | MediumTest | Level2)
365 {
366 char testFileName[] = "keyalias1";
367 char testFileName1[] = "key_auth_id1";
368 struct hks_blob keyAlias;
369 int32_t status;
370
371 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
372 keyAlias.data = (uint8_t *)testFileName;
373 keyAlias.size = NUM65;
374
375 struct hks_key_param keyParam;
376 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
377 keyParam.key_auth_id.size = sizeof(testFileName1);
378 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
379 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
380 keyParam.key_len = 0;
381 keyParam.key_usage = 0;
382 keyParam.key_pad = 0;
383
384 status = hks_generate_key(&keyAlias, &keyParam);
385 TEST_ASSERT_EQUAL_INT(NUM135, status);
386 }
387
388 /* *
389 * @tc.number : SUB_SEC_DataPro_HuksL0_0040
390 * @tc.name : Generate key, abnormal input parameters keyAlias.type is not equal to HKS_BLOB_TYPE_ALIAS
391 * @tc.desc : [C- SECURITY -1400]
392 */
393 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0040, Function | MediumTest | Level2)
394 {
395 char testFileName[] = "keyalias1";
396 char testFileName1[] = "key_auth_id1";
397 struct hks_blob keyAlias;
398 int32_t status;
399
400 keyAlias.type = 0;
401 keyAlias.data = (uint8_t *)testFileName;
402 keyAlias.size = sizeof(testFileName);
403
404 struct hks_key_param keyParam;
405 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
406 keyParam.key_auth_id.size = sizeof(testFileName1);
407 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
408 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
409 keyParam.key_len = 0;
410 keyParam.key_usage = 0;
411 keyParam.key_pad = 0;
412
413 status = hks_generate_key(&keyAlias, &keyParam);
414 TEST_ASSERT_EQUAL_INT(NUM135, status);
415 }
416
417 /* *
418 * @tc.number : SUB_SEC_DataPro_HuksL0_0050
419 * @tc.name : Generate key, abnormal input parameters keyAlias.data is null
420 * @tc.desc : [C- SECURITY -1400]
421 */
422 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0050, Function | MediumTest | Level2)
423 {
424 char testFileName[] = "keyalias1";
425 char testFileName1[] = "key_auth_id1";
426 struct hks_blob keyAlias;
427 int32_t status;
428
429 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
430 keyAlias.data = NULL;
431 keyAlias.size = sizeof(testFileName);
432
433 struct hks_key_param keyParam;
434 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
435 keyParam.key_auth_id.size = sizeof(testFileName1);
436 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
437 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
438 keyParam.key_len = 0;
439 keyParam.key_usage = 0;
440 keyParam.key_pad = 0;
441
442 status = hks_generate_key(&keyAlias, &keyParam);
443 TEST_ASSERT_EQUAL_INT(NUM135, status);
444 }
445
446 /* *
447 * @tc.number : SUB_SEC_DataPro_HuksL0_0060
448 * @tc.name : Generate key, abnormal input parameters keyParam is null
449 * @tc.desc : [C- SECURITY -1400]
450 */
451 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0060, Function | MediumTest | Level2)
452 {
453 char testFileName[] = "keyalias1";
454 struct hks_blob keyAlias;
455 int32_t status;
456
457 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
458 keyAlias.data = (uint8_t *)testFileName;
459 keyAlias.size = sizeof(testFileName);
460
461 struct hks_key_param *keyParam = NULL;
462
463 status = hks_generate_key(&keyAlias, keyParam);
464 TEST_ASSERT_EQUAL_INT(NUM1000, status);
465 }
466
467 /* *
468 * @tc.number : SUB_SEC_DataPro_HuksL0_0070
469 * @tc.name : Generate key, abnormal input parameters keyParam.key_type
470 is not equal to HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519
471 * @tc.desc : [C- SECURITY -1400]
472 */
473 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0070, Function | MediumTest | Level2)
474 {
475 char testFileName[] = "keyalias1";
476 char testFileName1[] = "key_auth_id1";
477 struct hks_blob keyAlias;
478 int32_t status;
479
480 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
481 keyAlias.data = (uint8_t *)testFileName;
482 keyAlias.size = sizeof(testFileName);
483
484 struct hks_key_param keyParam;
485 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
486 keyParam.key_auth_id.size = sizeof(testFileName1);
487 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
488 keyParam.key_type = NUM1;
489 keyParam.key_len = 0;
490 keyParam.key_usage = 0;
491 keyParam.key_pad = 0;
492
493 status = hks_generate_key(&keyAlias, &keyParam);
494 TEST_ASSERT_EQUAL_INT(NUM134, status);
495 }
496
497 /* *
498 * @tc.number : SUB_SEC_DataPro_HuksL0_0080
499 * @tc.name : Generate key, abnormal input parameters keyParam.key_auth_id.size is 0
500 * @tc.desc : [C- SECURITY -1400]
501 */
502 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0080, Function | MediumTest | Level2)
503 {
504 char testFileName[] = "keyalias1";
505 char testFileName1[] = "key_auth_id1";
506 struct hks_blob keyAlias;
507 int32_t status;
508
509 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
510 keyAlias.data = (uint8_t *)testFileName;
511 keyAlias.size = sizeof(testFileName);
512
513 struct hks_key_param keyParam;
514 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
515 keyParam.key_auth_id.size = 0;
516 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
517 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
518 keyParam.key_len = 0;
519 keyParam.key_usage = 0;
520 keyParam.key_pad = 0;
521
522 status = hks_generate_key(&keyAlias, &keyParam);
523 TEST_ASSERT_EQUAL_INT(NUM135, status);
524 }
525
526 /* *
527 * @tc.number : SUB_SEC_DataPro_HuksL0_0090
528 * @tc.name : Generate key, abnormal input parameters keyParam.key_auth_id.size is more than 64
529 * @tc.desc : [C- SECURITY -1400]
530 */
531 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0090, Function | MediumTest | Level2)
532 {
533 char testFileName[] = "keyalias1";
534 char testFileName1[] = "key_auth_id1";
535 struct hks_blob keyAlias;
536 int32_t status;
537
538 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
539 keyAlias.data = (uint8_t *)testFileName;
540 keyAlias.size = sizeof(testFileName);
541
542 struct hks_key_param keyParam;
543 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
544 keyParam.key_auth_id.size = NUM65;
545 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
546 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
547 keyParam.key_len = 0;
548 keyParam.key_usage = 0;
549 keyParam.key_pad = 0;
550
551 status = hks_generate_key(&keyAlias, &keyParam);
552 TEST_ASSERT_EQUAL_INT(NUM135, status);
553 }
554
555 /* *
556 * @tc.number : SUB_SEC_DataPro_HuksL0_0100
557 * @tc.name : Generate key, abnormal input parameters keyParam.key_auth_id.type
558 is not equal to HKS_BLOB_TYPE_AUTHID
559 * @tc.desc : [C- SECURITY -1400]
560 */
561 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0100, Function | MediumTest | Level2)
562 {
563 char testFileName[] = "keyalias1";
564 char testFileName1[] = "key_auth_id1";
565 struct hks_blob keyAlias;
566 int32_t status;
567
568 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
569 keyAlias.data = (uint8_t *)testFileName;
570 keyAlias.size = sizeof(testFileName);
571
572 struct hks_key_param keyParam;
573 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
574 keyParam.key_auth_id.size = sizeof(testFileName1);
575 keyParam.key_auth_id.type = 0;
576 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
577 keyParam.key_len = 0;
578 keyParam.key_usage = 0;
579 keyParam.key_pad = 0;
580
581 status = hks_generate_key(&keyAlias, &keyParam);
582 TEST_ASSERT_EQUAL_INT(NUM135, status);
583 }
584
585 /* *
586 * @tc.number : SUB_SEC_DataPro_HuksL0_0110
587 * @tc.name : Generate key, abnormal input parameters keyParam.key_auth_id.data is null
588 * @tc.desc : [C- SECURITY -1400]
589 */
590 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0110, Function | MediumTest | Level2)
591 {
592 char testFileName[] = "keyalias1";
593 char testFileName1[] = "key_auth_id1";
594 struct hks_blob keyAlias;
595 int32_t status;
596
597 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
598 keyAlias.data = (uint8_t *)testFileName;
599 keyAlias.size = sizeof(testFileName);
600
601 struct hks_key_param keyParam;
602 keyParam.key_auth_id.data = NULL;
603 keyParam.key_auth_id.size = sizeof(testFileName1);
604 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
605 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
606 keyParam.key_len = 0;
607 keyParam.key_usage = 0;
608 keyParam.key_pad = 0;
609
610 status = hks_generate_key(&keyAlias, &keyParam);
611 TEST_ASSERT_EQUAL_INT(NUM1000, status);
612 }
613
614 /* *
615 * @tc.number : SUB_SEC_DataPro_HuksL0_0120
616 * @tc.name : Generate key, the number of stored keys is more than 20
617 * @tc.desc : [C- SECURITY -1400]
618 */
619 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0120, Function | MediumTest | Level2)
620 {
621 int status;
622 char testFileName[] = "keyalias1", testFileName1[] = "key_auth_id1";
623 struct hks_key_param keyParam;
624 keyParam.key_auth_id.data = (uint8_t*)testFileName1; keyParam.key_auth_id.size = sizeof(testFileName1);
625 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID; keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
626 keyParam.key_len = 0; keyParam.key_usage = 0; keyParam.key_pad = 0;
627 for (int j = 0; j < NUM21; j++) {
628 struct hks_blob keyAlias;
629 HksStBlobInit1(&keyAlias, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
630 if (memcpy_s(keyAlias.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
631 HksBlobDestroyT1(&keyAlias); TEST_FAIL();
632 return;
633 }
634 char tmp[3] = { 0 };
635 sprintf_s(tmp, sizeof(tmp), "%d", j);
636 if (strcat_s((char*)keyAlias.data, strlen((char*)keyAlias.data) + strlen(tmp) + 1, tmp) != EOK){
637 HksBlobDestroyT1(&keyAlias); TEST_FAIL();
638 return;
639 }
640 keyAlias.size = strlen((char*)keyAlias.data);
641 status = hks_generate_key(&keyAlias, &keyParam);
642 if (j < NUM20){
643 TEST_ASSERT_EQUAL_INT(0, status);
644 }
645 else {
646 TEST_ASSERT_EQUAL_INT(NUM142, status);
647 }
648 HksBlobDestroyT1(&keyAlias);
649 }
650 for (int j = 0; j < NUM20; j++) {
651 struct hks_blob keyAlias;
652 HksStBlobInit1(&keyAlias, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
653 if (memcpy_s(keyAlias.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
654 HksBlobDestroyT1(&keyAlias); TEST_FAIL();
655 return;
656 }
657 char tmp[3] = { 0 };
658 sprintf_s(tmp, sizeof(tmp), "%d", j);
659 if (strcat_s((char*)keyAlias.data, strlen((char*)keyAlias.data) + strlen(tmp) + 1, tmp) != EOK) {
660 HksBlobDestroyT1(&keyAlias); TEST_FAIL();
661 return;
662 }
663 keyAlias.size = strlen((char*)keyAlias.data);
664 status = hks_delete_key(&keyAlias);
665 TEST_ASSERT_EQUAL_INT(0, status);
666 HksBlobDestroyT1(&keyAlias);
667 }
668 }
669
670
671 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++generate key
672 // end+++++++++++++++++++++++++++++++++++++++++++++++++++++0000-0120
673
674 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Delete key
675 // begin+++++++++++++++++++++++++++++++++++++++++++++++++++++0130-0190
676 /* *
677 * @tc.number : SUB_SEC_DataPro_HuksL0_0130
678 * @tc.name : Delete key, normal input parameters keyAlias and keyParam
679 * @tc.desc : [C- SECURITY -1900]
680 */
681 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0130, Function | MediumTest | Level1)
682 {
683 char testFileName[] = "keyalias1";
684 char testFileName1[] = "key_auth_id1";
685 int32_t statusGenerate;
686 int32_t statusDelete;
687 struct hks_key_param keyParam;
688 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
689 keyParam.key_auth_id.size = sizeof(testFileName1);
690 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
691 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
692 keyParam.key_len = 0;
693 keyParam.key_usage = 0;
694 keyParam.key_pad = 0;
695 struct hks_blob keyAliasGenerate;
696 HksStBlobInit1(&keyAliasGenerate, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
697 if (memcpy_s(keyAliasGenerate.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
698 HksBlobDestroyT1(&keyAliasGenerate);
699 TEST_FAIL();
700 return;
701 }
702 char tmpGenerate[3] = { 0 };
703 if (strcat_s((char *)keyAliasGenerate.data,
704 strlen((char *)keyAliasGenerate.data) + strlen(tmpGenerate) + 1,
705 tmpGenerate) != EOK) {
706 HksBlobDestroyT1(&keyAliasGenerate);
707 TEST_FAIL();
708 return;
709 }
710 keyAliasGenerate.size = strlen((char *)keyAliasGenerate.data);
711 statusGenerate = hks_generate_key(&keyAliasGenerate, &keyParam);
712 TEST_ASSERT_EQUAL_INT(0, statusGenerate);
713 HksBlobDestroyT1(&keyAliasGenerate);
714 struct hks_blob keyAliasDelete;
715 HksStBlobInit1(&keyAliasDelete, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
716 if (memcpy_s(keyAliasDelete.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
717 HksBlobDestroyT1(&keyAliasDelete);
718 TEST_FAIL();
719 return;
720 }
721 char tmpDelete[3] = { 0 };
722 if (strcat_s((char *)keyAliasDelete.data,
723 strlen((char *)keyAliasDelete.data) + strlen(tmpDelete) + 1,
724 tmpDelete) != EOK) {
725 HksBlobDestroyT1(&keyAliasDelete);
726 TEST_FAIL();
727 return;
728 }
729 keyAliasDelete.size = strlen((char *)keyAliasDelete.data);
730 statusDelete = hks_delete_key(&keyAliasDelete);
731 TEST_ASSERT_EQUAL_INT(0, statusDelete);
732 HksBlobDestroyT1(&keyAliasDelete);
733 };
734
735 /* *
736 * @tc.number : SUB_SEC_DataPro_HuksL0_0140
737 * @tc.name : Delete key, abnormal input parameters keyAlias is null
738 * @tc.desc : [C- SECURITY -1900]
739 */
740 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0140, Function | MediumTest | Level2)
741 {
742 int32_t status;
743 struct hks_blob *keyAlias = NULL;
744
745 status = hks_delete_key(keyAlias);
746 TEST_ASSERT_EQUAL_INT(NUM1000, status);
747 }
748
749 /* *
750 * @tc.number : SUB_SEC_DataPro_HuksL0_0150
751 * @tc.name : Delete key, abnormal input parameters keyAlias.data is null
752 * @tc.desc : [C- SECURITY -1900]
753 */
754 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0150, Function | MediumTest | Level2)
755 {
756 int32_t status;
757 char testFileName[] = "Test_file_north_interfaces";
758 struct hks_blob keyAlias = { 0 };
759
760 keyAlias.data = NULL;
761 keyAlias.size = sizeof(testFileName);
762 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
763
764 status = hks_delete_key(&keyAlias);
765 TEST_ASSERT_EQUAL_INT(NUM135, status);
766 }
767
768 /* *
769 * @tc.number : SUB_SEC_DataPro_HuksL0_0160
770 * @tc.name : Delete key, abnormal input parameters keyAlias.type is not equal to HKS_BLOB_TYPE_ALIAS
771 * @tc.desc : [C- SECURITY -1900]
772 */
773 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0160, Function | MediumTest | Level2)
774 {
775 int32_t status;
776 char testFileName[] = "Test_file_north_interfaces";
777 struct hks_blob keyAlias = { 0 };
778
779 keyAlias.data = (uint8_t *)testFileName;
780 keyAlias.size = sizeof(testFileName);
781 keyAlias.type = 0;
782
783 status = hks_delete_key(&keyAlias);
784 TEST_ASSERT_EQUAL_INT(NUM135, status);
785 }
786 /* *
787 * @tc.number : SUB_SEC_DataPro_HuksL0_0170
788 * @tc.name : Delete key, abnormal input parameters keyAlias.size is 0
789 * @tc.desc : [C- SECURITY -1900]
790 */
791 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0170, Function | MediumTest | Level2)
792 {
793 int32_t status;
794 char testFileName[] = "Test_file_north_interfaces";
795 struct hks_blob keyAlias = { 0 };
796
797 keyAlias.data = (uint8_t *)testFileName;
798 keyAlias.size = 0;
799 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
800
801 status = hks_delete_key(&keyAlias);
802 TEST_ASSERT_EQUAL_INT(NUM135, status);
803 }
804 /* *
805 * @tc.number : SUB_SEC_DataPro_HuksL0_0180
806 * @tc.name : Delete key, abnormal input parameters keyAlias.size is more than 64
807 * @tc.desc : [C- SECURITY -1900]
808 */
809 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0180, Function | MediumTest | Level2)
810 {
811 int32_t status;
812 char testFileName[] = "Test_file_north_interfaces";
813 struct hks_blob keyAlias = { 0 };
814
815 keyAlias.data = (uint8_t *)testFileName;
816 keyAlias.size = NUM65;
817 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
818
819 status = hks_delete_key(&keyAlias);
820 TEST_ASSERT_EQUAL_INT(NUM135, status);
821 }
822
823 /* *
824 * @tc.number : SUB_SEC_DataPro_HuksL0_0190
825 * @tc.name : Delete key, the key does not exist
826 * @tc.desc : [C- SECURITY -1900]
827 */
828 LITE_TEST_CASE(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0190, Function | MediumTest | Level2)
829 {
830 int32_t status;
831 char testFileName[] = "Test_file_north_interfaces";
832 struct hks_blob keyAlias = { 0 };
833
834 keyAlias.data = (uint8_t *)testFileName;
835 keyAlias.size = sizeof(testFileName);
836 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
837
838 status = hks_delete_key(&keyAlias);
839 TEST_ASSERT_EQUAL_INT(NUM1010, status);
840 }
841 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Delete key
842 // end+++++++++++++++++++++++++++++++++++++++++++++++++++++++0130-0190
843 #endif
844 RUN_TEST_SUITE(SecurityDataHuksGenDelTestSuite);
845