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
15 #include "SecurityDataHuks.h"
16 #include "hks_client.h"
17 #include "hks_types.h"
18 #include <securec.h>
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <hks_file_api.h>
24 #include "gtest/gtest.h"
25 #include <stdarg.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include "hks_errno.h"
29 #include <sys/time.h>
30 #include <hks_hardware_api.h>
31 using namespace std;
32 using namespace testing::ext;
33
FopenS(FILE ** fp,const char * filename,const char * modes)34 errno_t FopenS(FILE **fp, const char *filename, const char *modes)
35 {
36 *fp = fopen(filename, modes);
37 return 0;
38 }
39
FileSize(const char * filename)40 int32_t FileSize(const char *filename)
41 {
42 FILE *fp = nullptr;
43 errno_t err;
44 int32_t size;
45
46 if (filename == nullptr) {
47 return -1;
48 }
49
50 err = FopenS(&fp, filename, "rb");
51 if (err != 0 || fp == nullptr) {
52 return -1;
53 }
54
55 if (fseek(fp, 0L, SEEK_END) != 0) {
56 fclose(fp);
57 return -1;
58 }
59
60 size = ftell(fp);
61 fclose(fp);
62
63 return size;
64 }
FileWrite(const char * filename,uint32_t offset,const uint8_t * buf,uint32_t len)65 int32_t FileWrite(const char *filename, uint32_t offset, const uint8_t *buf, uint32_t len)
66 {
67 FILE *fp = nullptr;
68 errno_t err;
69 size_t size;
70
71 if (filename == NULL || buf == NULL) {
72 return -1;
73 }
74
75 err = FopenS(&fp, filename, "wb+");
76 if (err != 0 || fp == nullptr) {
77 return -1;
78 }
79
80 size = fwrite(buf, 1, len, fp);
81 fclose(fp);
82
83 if (size != len) {
84 return -1;
85 }
86
87 return 0;
88 }
FileRead(const char * filename,uint32_t offset,uint8_t * buf,uint32_t len)89 int32_t FileRead(const char *filename, uint32_t offset, uint8_t *buf, uint32_t len)
90 {
91 FILE* fp = nullptr;
92 errno_t err;
93 size_t size;
94
95 if (filename == NULL || buf == NULL) {
96 return -1;
97 }
98
99 if (access(filename, 0) == -1) {
100 return 0;
101 }
102
103 err = FopenS(&fp, filename, "rb");
104 if (err == NUM2) {
105 return 0;
106 }
107 if (err != 0 || fp == nullptr) {
108 return -1;
109 }
110
111 size = fread(buf, 1, len, fp);
112 fclose(fp);
113
114 if (size == 0) {
115 return -1;
116 }
117
118 return size;
119 }
120
GetTimeMs()121 uint64_t GetTimeMs()
122 {
123 struct timeval timeVal;
124 gettimeofday(&timeVal, nullptr);
125
126 return (uint64_t)NUM1000000 * timeVal.tv_sec + timeVal.tv_usec;
127 }
128
AddLog(const char * logType,const char * tag,const char * func,const char * format,const va_list * ap)129 void AddLog(const char* logType, const char *tag, const char *func, const char *format, const va_list* ap)
130 {
131 char* buf = (char*)malloc(NUM2048);
132 if (buf == nullptr) {
133 return;
134 }
135 int offset = sprintf_s(buf, (NUM2048), "[%s][%llu]%s %s: ", logType, (unsigned long long)GetTimeMs(), tag, func);
136 if (offset >= 0) {
137 offset += vsprintf_s(buf + offset, (NUM2048) - offset, format, *ap);
138 }
139
140 free(buf);
141 buf = nullptr;
142 }
143
Logi(const char * tag,const char * func,const char * format,...)144 void Logi(const char *tag, const char *func, const char *format, ...)
145 {
146 va_list args;
147 va_start(args, format);
148 AddLog("Info", tag, func, format, &args);
149 va_end(args);
150 }
151
Logw(const char * tag,const char * func,const char * format,...)152 void Logw(const char *tag, const char *func, const char *format, ...)
153 {
154 va_list args;
155 va_start(args, format);
156 AddLog("Warn", tag, func, format, &args);
157 va_end(args);
158 }
159
Loge(const char * tag,const char * func,const char * format,...)160 void Loge(const char *tag, const char *func, const char *format, ...)
161 {
162 va_list args;
163 va_start(args, format);
164 AddLog("Error", tag, func, format, &args);
165 va_end(args);
166 }
167
Logd(const char * tag,const char * func,const char * format,...)168 void Logd(const char *tag, const char *func, const char *format, ...)
169 {
170 va_list args;
171 va_start(args, format);
172 AddLog("Debug", tag, func, format, &args);
173 va_end(args);
174 }
175
176 uint8_t g_hksHardwareUdidId[32] = {
177 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
178 0x09, 0x0A, 0x0C, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
179 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
180 0x19, 0x1A, 0x1C, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
181 };
182
HksTestGetHardwareUdid(uint8_t * udid,uint32_t udidLen)183 int32_t HksTestGetHardwareUdid(uint8_t* udid, uint32_t udidLen)
184 {
185 int err;
186 if ((udid == NULL) || (udidLen != NUM32)) {
187 return -1;
188 }
189
190 int32_t rc = 1234567;
191 if (rc <= 0) {
192 err = memcpy_s(udid, NUM32, g_hksHardwareUdidId, NUM32);
193 if (err != EOK) {
194 return -1;
195 }
196 }
197
198 char buf[128] = {0};
199 uint32_t offset = 0;
200 for (uint32_t i = 0; i < udidLen; ++i) {
201 offset += sprintf_s(buf + offset, NUM128 - offset, "%02x ", udid[i]);
202 }
203
204 Logd("[hks_tester]", __func__, buf);
205 return 0;
206 }
207
HksStBlobInit1(struct hks_blob * blob,size_t nmemb,size_t size,uint8_t type)208 void HksStBlobInit1(struct hks_blob *blob, size_t nmemb, size_t size, uint8_t type)
209 {
210 if (blob == nullptr || nmemb == 0 || size == 0) {
211 EXPECT_EQ(0, 1);
212 return;
213 }
214 blob->data = (uint8_t *)calloc(nmemb, size);
215 if (blob->data == NULL) {
216 EXPECT_EQ(0, 1);
217 return;
218 }
219 if (memset_s(blob->data, size, 0, size) != EOK) {
220 EXPECT_EQ(0, 1);
221 return;
222 }
223 blob->size = size;
224 blob->type = type;
225 }
226
HksBlobDestroyT1(struct hks_blob * blob)227 void HksBlobDestroyT1(struct hks_blob *blob)
228 {
229 if (blob == nullptr) {
230 EXPECT_EQ(0, 1);
231 return;
232 }
233 if (blob && blob->data) {
234 if (memset_s(blob->data, blob->size, 0, blob->size) != EOK) {
235 EXPECT_EQ(0, 1);
236 return;
237 }
238 HKS_FREE_PTR1(blob->data);
239 }
240 blob->data = NULL;
241 blob->size = 0;
242 blob->type = HKS_BLOB_TYPE_RAW;
243 }
244
HexStringToByte(const char * str,int nLen,unsigned char * pHex)245 void HexStringToByte(const char *str, int nLen, unsigned char *pHex)
246 {
247 unsigned int number = 4;
248 if (nLen % NUM2) {
249 EXPECT_EQ(0, 1);
250 return;
251 }
252 int nHexLen = nLen / NUM2;
253 unsigned char nibble[2];
254
255 if (nHexLen >= MAX_INT) {
256 return;
257 }
258 for (int i = 0; i < nHexLen; i++) {
259 nibble[0] = str[i * NUM2];
260 nibble[1] = str[i * NUM2 + NUM1];
261 for (int j = 0; j < NUM2; j++) {
262 if (nibble[j] <= 'F' && nibble[j] >= 'A') {
263 nibble[j] = nibble[j] - 'A' + NUM10;
264 } else if (nibble[j] <= 'f' && nibble[j] >= 'a') {
265 nibble[j] = nibble[j] - 'a' + NUM10;
266 } else if (nibble[j] >= '0' && nibble[j] <= '9') {
267 nibble[j] = nibble[j] - '0';
268 } else {
269 EXPECT_EQ(0, 1);
270 return;
271 }
272 }
273 pHex[i] = nibble[0] << number;
274 pHex[i] |= nibble[1];
275 }
276 }
277
BuildBlobData(struct hks_blob * param,const char * str,uint8_t type,uint32_t size,uint8_t isDataNull)278 void BuildBlobData(struct hks_blob *param, const char *str, uint8_t type, uint32_t size, uint8_t isDataNull)
279 {
280 if (param == nullptr) {
281 EXPECT_EQ(0, 1);
282 return;
283 }
284 param->type = type;
285 param->size = size;
286 if (isDataNull == 1)
287 param->data = NULL;
288 else {
289 if (size + NUM2 == 0) {
290 EXPECT_EQ(0, 1);
291 return;
292 }
293 unsigned char *buff = (unsigned char *)malloc(size + NUM2);
294 if (buff == nullptr) {
295 EXPECT_EQ(0, 1);
296 return;
297 }
298 if (memset_s(buff, size + NUM2, 0, size + NUM2) != EOK) {
299 EXPECT_EQ(0, 1);
300 free(buff);
301 buff = nullptr;
302 return;
303 }
304 HexStringToByte(str, size * NUM2, buff);
305 param->data = (uint8_t *)buff;
306 }
307 }
308
309 class SecurityDataHuksGenDelTestSuite : public testing::Test {
310 protected:
311 // SetUpTestCase: Testsuit setup, run before 1st testcase
SetUpTestCase(void)312 static void SetUpTestCase(void) {}
313 // TearDownTestCase: Testsuit teardown, run after last testcase
TearDownTestCase(void)314 static void TearDownTestCase(void) {}
315 // Testcase setup
SetUp()316 virtual void SetUp()
317 {
318 int32_t status;
319 struct hks_file_callbacks fileCallbacks;
320
321 fileCallbacks.read = FileRead;
322 fileCallbacks.write = FileWrite;
323 fileCallbacks.file_size = FileSize;
324
325 status = hks_register_file_callbacks(&fileCallbacks);
326 EXPECT_EQ(0, status);
327
328 struct hks_log_f_group logFunc;
329 logFunc.log_info = Logi;
330 logFunc.log_warn = Logw;
331 logFunc.log_error = Loge;
332 logFunc.log_debug = Logd;
333
334 status = hks_register_log_interface(&logFunc);
335 EXPECT_EQ(0, status);
336
337 status = hks_register_get_hardware_udid_callback(HksTestGetHardwareUdid);
338
339 EXPECT_EQ(0, status);
340
341 status = hks_init();
342 if (status != 0) {
343 status = hks_refresh_key_info();
344 }
345 EXPECT_EQ(0, status);
346 }
347 // Testcase teardown
TearDown()348 virtual void TearDown() {}
349 };
350
351 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++generate key
352 // begin+++++++++++++++++++++++++++++++++++++++++++++++++++0000-0120
353
354 /* *
355 * @tc.number : SUB_SEC_DataPro_HuksL1_0000
356 * @tc.name : Generate key, normal input parameters keyAlias and keyParam
357 * @tc.desc : [C- SECURITY -1500]
358 */
359 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0000, Function | MediumTest | Level1)
360 {
361 char testFileName[] = "keyalias1";
362 char testFileName1[] = "key_auth_id1";
363 int32_t statusGenerate;
364 int32_t statusDelete;
365 struct hks_key_param keyParam;
366 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
367 keyParam.key_auth_id.size = sizeof(testFileName1);
368 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
369 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
370 keyParam.key_len = 0;
371 keyParam.key_usage = 0;
372 keyParam.key_pad = 0;
373 struct hks_blob keyAliasGenerate;
374 HksStBlobInit1(&keyAliasGenerate, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
375 if (memcpy_s(keyAliasGenerate.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
376 HksBlobDestroyT1(&keyAliasGenerate);
377 EXPECT_EQ(0, 1);
378 return;
379 }
380 char tmpGenerate[NUM3] = { 0 };
381 if (strcat_s((char *)keyAliasGenerate.data,
382 strlen((char *)keyAliasGenerate.data) + strlen(tmpGenerate) + 1,
383 tmpGenerate) != EOK) {
384 HksBlobDestroyT1(&keyAliasGenerate);
385 EXPECT_EQ(0, 1);
386 return;
387 }
388 keyAliasGenerate.size = strlen((char *)keyAliasGenerate.data);
389 statusGenerate = hks_generate_key(&keyAliasGenerate, &keyParam);
390 EXPECT_EQ(0, statusGenerate);
391 HksBlobDestroyT1(&keyAliasGenerate);
392 struct hks_blob keyAliasDelete;
393 HksStBlobInit1(&keyAliasDelete, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
394 if (memcpy_s(keyAliasDelete.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
395 HksBlobDestroyT1(&keyAliasDelete);
396 EXPECT_EQ(0, 1);
397 return;
398 }
399 char tmpDelete[NUM3] = { 0 };
400 if (strcat_s((char *)keyAliasDelete.data,
401 strlen((char *)keyAliasDelete.data) + strlen(tmpDelete) + 1,
402 tmpDelete) != EOK) {
403 HksBlobDestroyT1(&keyAliasDelete);
404 EXPECT_EQ(0, 1);
405 return;
406 }
407 keyAliasDelete.size = strlen((char *)keyAliasDelete.data);
408 statusDelete = hks_delete_key(&keyAliasDelete);
409 EXPECT_EQ(0, statusDelete);
410 HksBlobDestroyT1(&keyAliasDelete);
411 };
412
413 /* *
414 * @tc.number : SUB_SEC_DataPro_HuksL1_0010
415 * @tc.name : Generate key, abnormal input parameters keyAlias is null
416 * @tc.desc : [C- SECURITY -1500]
417 */
418 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0010, Function | MediumTest | Level2)
419 {
420 char testFileName1[] = "key_auth_id1";
421 struct hks_blob *keyAlias = nullptr;
422 int32_t status;
423
424 struct hks_key_param keyParam;
425 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
426 keyParam.key_auth_id.size = sizeof(testFileName1);
427 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
428 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
429 keyParam.key_len = 0;
430 keyParam.key_usage = 0;
431 keyParam.key_pad = 0;
432
433 status = hks_generate_key(keyAlias, &keyParam);
434 EXPECT_EQ(NUM1000, status);
435 }
436
437 /* *
438 * @tc.number : SUB_SEC_DataPro_HuksL1_0020
439 * @tc.name : Generate key, abnormal input parameters keyAlias.size is 0
440 * @tc.desc : [C- SECURITY -1500]
441 */
442 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0020, Function | MediumTest | Level2)
443 {
444 char testFileName[] = "keyalias1";
445 char testFileName1[] = "key_auth_id1";
446 struct hks_blob keyAlias;
447 int32_t status;
448
449 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
450 keyAlias.data = (uint8_t *)testFileName;
451 keyAlias.size = 0;
452
453 struct hks_key_param keyParam;
454 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
455 keyParam.key_auth_id.size = sizeof(testFileName1);
456 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
457 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
458 keyParam.key_len = 0;
459 keyParam.key_usage = 0;
460 keyParam.key_pad = 0;
461
462 status = hks_generate_key(&keyAlias, &keyParam);
463 EXPECT_EQ(NUM135, status);
464 }
465
466 /* *
467 * @tc.number : SUB_SEC_DataPro_HuksL1_0030
468 * @tc.name : Generate key, abnormal input parameters keyAlias.size is more than 64
469 * @tc.desc : [C- SECURITY -1500]
470 */
471 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0030, Function | MediumTest | Level2)
472 {
473 char testFileName[] = "keyalias1";
474 char testFileName1[] = "key_auth_id1";
475 struct hks_blob keyAlias;
476 int32_t status;
477
478 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
479 keyAlias.data = (uint8_t *)testFileName;
480 keyAlias.size = NUM65;
481
482 struct hks_key_param keyParam;
483 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
484 keyParam.key_auth_id.size = sizeof(testFileName1);
485 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
486 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
487 keyParam.key_len = 0;
488 keyParam.key_usage = 0;
489 keyParam.key_pad = 0;
490
491 status = hks_generate_key(&keyAlias, &keyParam);
492 EXPECT_EQ(NUM135, status);
493 }
494
495 /* *
496 * @tc.number : SUB_SEC_DataPro_HuksL1_0040
497 * @tc.name : Generate key, abnormal input parameters keyAlias.type is not equal to HKS_BLOB_TYPE_ALIAS
498 * @tc.desc : [C- SECURITY -1500]
499 */
500 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0040, Function | MediumTest | Level2)
501 {
502 char testFileName[] = "keyalias1";
503 char testFileName1[] = "key_auth_id1";
504 struct hks_blob keyAlias;
505 int32_t status;
506
507 keyAlias.type = 0;
508 keyAlias.data = (uint8_t *)testFileName;
509 keyAlias.size = sizeof(testFileName);
510
511 struct hks_key_param keyParam;
512 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
513 keyParam.key_auth_id.size = sizeof(testFileName1);
514 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
515 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
516 keyParam.key_len = 0;
517 keyParam.key_usage = 0;
518 keyParam.key_pad = 0;
519
520 status = hks_generate_key(&keyAlias, &keyParam);
521 EXPECT_EQ(NUM135, status);
522 }
523
524 /* *
525 * @tc.number : SUB_SEC_DataPro_HuksL1_0050
526 * @tc.name : Generate key, abnormal input parameters keyAlias.data is null
527 * @tc.desc : [C- SECURITY -1500]
528 */
529 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0050, Function | MediumTest | Level2)
530 {
531 char testFileName[] = "keyalias1";
532 char testFileName1[] = "key_auth_id1";
533 struct hks_blob keyAlias;
534
535 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
536 keyAlias.data = NULL;
537 keyAlias.size = sizeof(testFileName);
538
539 struct hks_key_param keyParam;
540 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
541 keyParam.key_auth_id.size = sizeof(testFileName1);
542 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
543 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
544 keyParam.key_len = 0;
545 keyParam.key_usage = 0;
546 keyParam.key_pad = 0;
547
548 int32_t status = hks_generate_key(&keyAlias, &keyParam);
549 EXPECT_EQ(NUM135, status);
550 }
551
552 /* *
553 * @tc.number : SUB_SEC_DataPro_HuksL1_0060
554 * @tc.name : Generate key, abnormal input parameters keyParam is null
555 * @tc.desc : [C- SECURITY -1500]
556 */
557 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0060, Function | MediumTest | Level2)
558 {
559 char testFileName[] = "keyalias1";
560 struct hks_blob keyAlias;
561 int32_t status;
562
563 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
564 keyAlias.data = (uint8_t *)testFileName;
565 keyAlias.size = sizeof(testFileName);
566
567 struct hks_key_param *keyParam = nullptr;
568
569 status = hks_generate_key(&keyAlias, keyParam);
570 EXPECT_EQ(NUM1000, status);
571 }
572
573 /* *
574 * @tc.number : SUB_SEC_DataPro_HuksL1_0070
575 * @tc.name : Generate key, abnormal input parameters keyParam.key_type
576 is not equal to HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519
577 * @tc.desc : [C- SECURITY -1500]
578 */
579 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0070, Function | MediumTest | Level2)
580 {
581 char testFileName[] = "keyalias1";
582 char testFileName1[] = "key_auth_id1";
583 struct hks_blob keyAlias;
584 int32_t status;
585
586 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
587 keyAlias.data = (uint8_t *)testFileName;
588 keyAlias.size = sizeof(testFileName);
589
590 struct hks_key_param keyParam;
591 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
592 keyParam.key_auth_id.size = sizeof(testFileName1);
593 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
594 keyParam.key_type = 1;
595 keyParam.key_len = 0;
596 keyParam.key_usage = 0;
597 keyParam.key_pad = 0;
598
599 status = hks_generate_key(&keyAlias, &keyParam);
600 EXPECT_EQ(NUM134, status);
601 }
602
603 /* *
604 * @tc.number : SUB_SEC_DataPro_HuksL1_0080
605 * @tc.name : Generate key, abnormal input parameters keyParam.key_auth_id.size is 0
606 * @tc.desc : [C- SECURITY -1500]
607 */
608 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0080, Function | MediumTest | Level2)
609 {
610 char testFileName[] = "keyalias1";
611 char testFileName1[] = "key_auth_id1";
612 struct hks_blob keyAlias;
613 int32_t status;
614
615 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
616 keyAlias.data = (uint8_t *)testFileName;
617 keyAlias.size = sizeof(testFileName);
618
619 struct hks_key_param keyParam;
620 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
621 keyParam.key_auth_id.size = 0;
622 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
623 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
624 keyParam.key_len = 0;
625 keyParam.key_usage = 0;
626 keyParam.key_pad = 0;
627
628 status = hks_generate_key(&keyAlias, &keyParam);
629 EXPECT_EQ(NUM135, status);
630 }
631
632 /* *
633 * @tc.number : SUB_SEC_DataPro_HuksL1_0090
634 * @tc.name : Generate key, abnormal input parameters keyParam.key_auth_id.size is more than 64
635 * @tc.desc : [C- SECURITY -1500]
636 */
637 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0090, Function | MediumTest | Level2)
638 {
639 char testFileName[] = "keyalias1";
640 char testFileName1[] = "key_auth_id1";
641 struct hks_blob keyAlias;
642 int32_t status;
643
644 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
645 keyAlias.data = (uint8_t *)testFileName;
646 keyAlias.size = sizeof(testFileName);
647
648 struct hks_key_param keyParam;
649 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
650 keyParam.key_auth_id.size = NUM65;
651 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
652 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
653 keyParam.key_len = 0;
654 keyParam.key_usage = 0;
655 keyParam.key_pad = 0;
656
657 status = hks_generate_key(&keyAlias, &keyParam);
658 EXPECT_EQ(NUM135, status);
659 }
660
661 /* *
662 * @tc.number : SUB_SEC_DataPro_HuksL1_0100
663 * @tc.name : Generate key, abnormal input parameters keyParam.key_auth_id.type
664 is not equal to HKS_BLOB_TYPE_AUTHID
665 * @tc.desc : [C- SECURITY -1500]
666 */
667 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0100, Function | MediumTest | Level2)
668 {
669 char testFileName[] = "keyalias1";
670 char testFileName1[] = "key_auth_id1";
671 struct hks_blob keyAlias;
672 int32_t status;
673
674 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
675 keyAlias.data = (uint8_t *)testFileName;
676 keyAlias.size = sizeof(testFileName);
677
678 struct hks_key_param keyParam;
679 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
680 keyParam.key_auth_id.size = sizeof(testFileName1);
681 keyParam.key_auth_id.type = 0;
682 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
683 keyParam.key_len = 0;
684 keyParam.key_usage = 0;
685 keyParam.key_pad = 0;
686
687 status = hks_generate_key(&keyAlias, &keyParam);
688 EXPECT_EQ(NUM135, status);
689 }
690
691 /* *
692 * @tc.number : SUB_SEC_DataPro_HuksL1_0110
693 * @tc.name : Generate key, abnormal input parameters keyParam.key_auth_id.data is null
694 * @tc.desc : [C- SECURITY -1500]
695 */
696 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0110, Function | MediumTest | Level2)
697 {
698 char testFileName[] = "keyalias1";
699 char testFileName1[] = "key_auth_id1";
700 struct hks_blob keyAlias;
701 int32_t status;
702
703 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
704 keyAlias.data = (uint8_t *)testFileName;
705 keyAlias.size = sizeof(testFileName);
706
707 struct hks_key_param keyParam;
708 keyParam.key_auth_id.data = NULL;
709 keyParam.key_auth_id.size = sizeof(testFileName1);
710 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
711 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
712 keyParam.key_len = 0;
713 keyParam.key_usage = 0;
714 keyParam.key_pad = 0;
715
716 status = hks_generate_key(&keyAlias, &keyParam);
717 EXPECT_EQ(NUM1000, status);
718 }
719
720 /* *
721 * @tc.number : SUB_SEC_DataPro_HuksL1_0120
722 * @tc.name : Generate key, the number of stored keys is more than 20
723 * @tc.desc : [C- SECURITY -1500]
724 */
725 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataGenerateKey0120, Function | MediumTest | Level2)
726 {
727 char testFileName[] = "keyalias1", testFileName1[] = "key_auth_id1";
728 struct hks_key_param keyParam;
729 keyParam.key_auth_id.data = (uint8_t*)testFileName1; keyParam.key_auth_id.size = sizeof(testFileName1);
730 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID; keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
731 keyParam.key_len = 0; keyParam.key_usage = 0; keyParam.key_pad = 0;
732 for (int j = 0; j < NUM21; j++) {
733 struct hks_blob keyAlias;
734 HksStBlobInit1(&keyAlias, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
735 if (memcpy_s(keyAlias.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
736 HksBlobDestroyT1(&keyAlias);
737 EXPECT_EQ(0, 1);
738 return;
739 }
740 char tmp[NUM3] = { 0 };
741 sprintf_s(tmp, sizeof(tmp), "%d", j);
742 if (strcat_s((char*)keyAlias.data, strlen((char*)keyAlias.data) + strlen(tmp) + 1, tmp) != EOK) {
743 HksBlobDestroyT1(&keyAlias);
744 EXPECT_EQ(0, 1);
745 return;
746 }
747 keyAlias.size = strlen((char*)keyAlias.data);
748 int status = hks_generate_key(&keyAlias, &keyParam);
749 if (j < NUM20){EXPECT_EQ(0, status); }
750 else {EXPECT_EQ(NUM142, status); }
751 HksBlobDestroyT1(&keyAlias);
752 }
753 for (int j = 0; j < NUM20; j++) {
754 struct hks_blob keyAlias;
755 HksStBlobInit1(&keyAlias, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
756 if (memcpy_s(keyAlias.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
757 HksBlobDestroyT1(&keyAlias);
758 EXPECT_EQ(0, 1);
759 return;
760 }
761 char tmp[NUM3] = { 0 };
762 sprintf_s(tmp, sizeof(tmp), "%d", j);
763 if (strcat_s((char*)keyAlias.data, strlen((char*)keyAlias.data) + strlen(tmp) + 1, tmp) != EOK) {
764 HksBlobDestroyT1(&keyAlias);
765 EXPECT_EQ(0, 1);
766 return;
767 }
768 keyAlias.size = strlen((char*)keyAlias.data);
769 int status = hks_delete_key(&keyAlias);
770 EXPECT_EQ(0, status);
771 HksBlobDestroyT1(&keyAlias);
772 }
773 }
774
775
776 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++generate key
777 // end+++++++++++++++++++++++++++++++++++++++++++++++++++++0000-0120
778
779 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Delete key
780 // begin+++++++++++++++++++++++++++++++++++++++++++++++++++++0130-0190
781
782 /* *
783 * @tc.number : SUB_SEC_DataPro_HuksL1_0130
784 * @tc.name : Delete key, normal input parameters keyAlias and keyParam
785 * @tc.desc : [C- SECURITY -1500]
786 */
787 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0130, Function | MediumTest | Level1)
788 {
789 char testFileName[] = "keyalias1";
790 char testFileName1[] = "key_auth_id1";
791 int32_t statusGenerate;
792 int32_t statusDelete;
793 struct hks_key_param keyParam;
794 keyParam.key_auth_id.data = (uint8_t *)testFileName1;
795 keyParam.key_auth_id.size = sizeof(testFileName1);
796 keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;
797 keyParam.key_type = HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519;
798 keyParam.key_len = 0;
799 keyParam.key_usage = 0;
800 keyParam.key_pad = 0;
801 struct hks_blob keyAliasGenerate;
802 HksStBlobInit1(&keyAliasGenerate, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
803 if (memcpy_s(keyAliasGenerate.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
804 HksBlobDestroyT1(&keyAliasGenerate);
805 EXPECT_EQ(0, 1);
806 return;
807 }
808 char tmpGenerate[NUM3] = { 0 };
809 if (strcat_s((char *)keyAliasGenerate.data,
810 strlen((char *)keyAliasGenerate.data) + strlen(tmpGenerate) + 1,
811 tmpGenerate) != EOK) {
812 HksBlobDestroyT1(&keyAliasGenerate);
813 EXPECT_EQ(0, 1);
814 return;
815 }
816 keyAliasGenerate.size = strlen((char *)keyAliasGenerate.data);
817 statusGenerate = hks_generate_key(&keyAliasGenerate, &keyParam);
818 EXPECT_EQ(0, statusGenerate);
819 HksBlobDestroyT1(&keyAliasGenerate);
820 struct hks_blob keyAliasDelete;
821 HksStBlobInit1(&keyAliasDelete, sizeof(uint8_t), sizeof(testFileName) + NUM3, HKS_BLOB_TYPE_ALIAS);
822 if (memcpy_s(keyAliasDelete.data, sizeof(testFileName), testFileName, sizeof(testFileName)) != EOK) {
823 HksBlobDestroyT1(&keyAliasDelete);
824 EXPECT_EQ(0, 1);
825 return;
826 }
827 char tmpDelete[NUM3] = { 0 };
828 if (strcat_s((char *)keyAliasDelete.data,
829 strlen((char *)keyAliasDelete.data) + strlen(tmpDelete) + 1,
830 tmpDelete) != EOK) {
831 HksBlobDestroyT1(&keyAliasDelete);
832 EXPECT_EQ(0, 1);
833 return;
834 }
835 keyAliasDelete.size = strlen((char *)keyAliasDelete.data);
836 statusDelete = hks_delete_key(&keyAliasDelete);
837 EXPECT_EQ(0, statusDelete);
838 HksBlobDestroyT1(&keyAliasDelete);
839 };
840
841 /* *
842 * @tc.number : SUB_SEC_DataPro_HuksL1_0140
843 * @tc.name : Delete key, abnormal input parameters keyAlias is null
844 * @tc.desc : [C- SECURITY -1500]
845 */
846 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0140, Function | MediumTest | Level2)
847 {
848 int32_t status;
849 struct hks_blob *keyAlias = nullptr;
850
851 status = hks_delete_key(keyAlias);
852 EXPECT_EQ(NUM1000, status);
853 }
854
855 /* *
856 * @tc.number : SUB_SEC_DataPro_HuksL1_0150
857 * @tc.name : Delete key, abnormal input parameters keyAlias.data is null
858 * @tc.desc : [C- SECURITY -1500]
859 */
860 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0150, Function | MediumTest | Level2)
861 {
862 int32_t status;
863 char testFileName[] = "Test_file_north_interfaces";
864 struct hks_blob keyAlias = { 0 };
865
866 keyAlias.data = NULL;
867 keyAlias.size = sizeof(testFileName);
868 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
869
870 status = hks_delete_key(&keyAlias);
871 EXPECT_EQ(NUM135, status);
872 }
873
874 /* *
875 * @tc.number : SUB_SEC_DataPro_HuksL1_0160
876 * @tc.name : Delete key, abnormal input parameters keyAlias.type is not equal to HKS_BLOB_TYPE_ALIAS
877 * @tc.desc : [C- SECURITY -1500]
878 */
879 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0160, Function | MediumTest | Level2)
880 {
881 int32_t status;
882 char testFileName[] = "Test_file_north_interfaces";
883 struct hks_blob keyAlias = { 0 };
884
885 keyAlias.data = (uint8_t *)testFileName;
886 keyAlias.size = sizeof(testFileName);
887 keyAlias.type = 0;
888
889 status = hks_delete_key(&keyAlias);
890 EXPECT_EQ(NUM135, status);
891 }
892
893 /* *
894 * @tc.number : SUB_SEC_DataPro_HuksL1_0170
895 * @tc.name : Delete key, abnormal input parameters keyAlias.size is 0
896 * @tc.desc : [C- SECURITY -1500]
897 */
898 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0170, Function | MediumTest | Level2)
899 {
900 int32_t status;
901 char testFileName[] = "Test_file_north_interfaces";
902 struct hks_blob keyAlias = { 0 };
903
904 keyAlias.data = (uint8_t *)testFileName;
905 keyAlias.size = 0;
906 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
907
908 status = hks_delete_key(&keyAlias);
909 EXPECT_EQ(NUM135, status);
910 }
911
912 /* *
913 * @tc.number : SUB_SEC_DataPro_HuksL1_0180
914 * @tc.name : Delete key, abnormal input parameters keyAlias.size is more than 64
915 * @tc.desc : [C- SECURITY -1500]
916 */
917 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0180, Function | MediumTest | Level2)
918 {
919 int32_t status;
920 char testFileName[] = "Test_file_north_interfaces";
921 struct hks_blob keyAlias = { 0 };
922
923 keyAlias.data = (uint8_t *)testFileName;
924 keyAlias.size = NUM65;
925 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
926
927 status = hks_delete_key(&keyAlias);
928 EXPECT_EQ(NUM135, status);
929 }
930
931 /* *
932 * @tc.number : SUB_SEC_DataPro_HuksL1_0190
933 * @tc.name : Delete key, the key does not exist
934 * @tc.desc : [C- SECURITY -1500]
935 */
936 HWTEST_F(SecurityDataHuksGenDelTestSuite, securityDataDeleteKey0190, Function | MediumTest | Level2)
937 {
938 int32_t status;
939 char testFileName[] = "Test_file_north_interfaces";
940 struct hks_blob keyAlias = { 0 };
941
942 keyAlias.data = (uint8_t *)testFileName;
943 keyAlias.size = sizeof(testFileName);
944 keyAlias.type = HKS_BLOB_TYPE_ALIAS;
945
946 status = hks_delete_key(&keyAlias);
947 EXPECT_EQ(NUM1010, status);
948 }
949 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Delete key
950 // end+++++++++++++++++++++++++++++++++++++++++++++++++++++++0130-0190
951