1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 /* BEGIN_HEADER */
17
18 #include <stdint.h>
19 #include <stddef.h>
20 #include <stdbool.h>
21 #include <pthread.h>
22 #include "crypt_eal_init.h"
23 #include "securec.h"
24 #include "bsl_errno.h"
25 #include "bsl_sal.h"
26 #include "crypt_errno.h"
27 #include "crypt_algid.h"
28 #include "crypt_types.h"
29 #include "crypt_eal_rand.h"
30 #include "crypt_eal_implprovider.h"
31 #include "drbg_local.h"
32 #include "eal_md_local.h"
33 #include "crypt_drbg_local.h"
34 #include "bsl_err_internal.h"
35 #include "bsl_err.h"
36 #include "bsl_params.h"
37 #include "crypt_params_key.h"
38 #include "crypt_provider.h"
39 /* END_HEADER */
40
41 #define CTR_AES128_SEEDLEN (32)
42 #define AES_BLOCK_LEN (16)
43 #define TEST_DRBG_DATA_SIZE (256)
44 #define DRBG_OUTPUT_SIZE (1024)
45 #define DRBG_MAX_OUTPUT_SIZE (65536)
46 #define DRBG_MAX_ADIN_SIZE (65536)
47
48 typedef struct {
49 bool entropyState;
50 bool nonceState;
51 } CallBackCtl_t;
52
53 typedef enum {
54 RAND_AES128_KEYLEN = 16,
55 RAND_AES192_KEYLEN = 24,
56 RAND_AES256_KEYLEN = 32,
57 } RAND_AES_KeyLen;
58
59 CallBackCtl_t g_callBackCtl = { 0 };
60
61 typedef struct {
62 CRYPT_Data *entropy;
63 CRYPT_Data *nonce;
64 CRYPT_Data *pers;
65
66 CRYPT_Data *addin1;
67 CRYPT_Data *entropyPR1;
68
69 CRYPT_Data *addin2;
70 CRYPT_Data *entropyPR2;
71
72 CRYPT_Data *retBits;
73 } DRBG_Vec_t;
74
75 #define DRBG_FREE(ptr) \
76 do { \
77 if ((ptr) != NULL) { \
78 free(ptr); \
79 } \
80 } while (0)
81
PthreadRWLockNew(BSL_SAL_ThreadLockHandle * lock)82 static int32_t PthreadRWLockNew(BSL_SAL_ThreadLockHandle *lock)
83 {
84 if (lock == NULL) {
85 return BSL_SAL_ERR_BAD_PARAM;
86 }
87 pthread_rwlock_t *newLock;
88 newLock = (pthread_rwlock_t *)malloc(sizeof(pthread_rwlock_t));
89 if (newLock == NULL) {
90 return BSL_MALLOC_FAIL;
91 }
92 if (pthread_rwlock_init(newLock, NULL) != 0) {
93 return BSL_SAL_ERR_UNKNOWN;
94 }
95 *lock = newLock;
96 return BSL_SUCCESS;
97 }
98
PthreadRWLockFree(BSL_SAL_ThreadLockHandle lock)99 static void PthreadRWLockFree(BSL_SAL_ThreadLockHandle lock)
100 {
101 if (lock == NULL) {
102 return;
103 }
104 pthread_rwlock_destroy((pthread_rwlock_t *)lock);
105 DRBG_FREE(lock);
106 }
107
PthreadRWLockReadLock(BSL_SAL_ThreadLockHandle lock)108 static int32_t PthreadRWLockReadLock(BSL_SAL_ThreadLockHandle lock)
109 {
110 if (lock == NULL) {
111 return BSL_SAL_ERR_BAD_PARAM;
112 }
113 if (pthread_rwlock_rdlock((pthread_rwlock_t *)lock) != 0) {
114 return BSL_SAL_ERR_UNKNOWN;
115 }
116 return BSL_SUCCESS;
117 }
118
PthreadRWLockWriteLock(BSL_SAL_ThreadLockHandle lock)119 static int32_t PthreadRWLockWriteLock(BSL_SAL_ThreadLockHandle lock)
120 {
121 if (lock == NULL) {
122 return BSL_SAL_ERR_BAD_PARAM;
123 }
124 if (pthread_rwlock_wrlock((pthread_rwlock_t *)lock) != 0) {
125 return BSL_SAL_ERR_UNKNOWN;
126 }
127 return BSL_SUCCESS;
128 }
129
PthreadRWLockUnlock(BSL_SAL_ThreadLockHandle lock)130 static int32_t PthreadRWLockUnlock(BSL_SAL_ThreadLockHandle lock)
131 {
132 if (lock == NULL) {
133 return BSL_SAL_ERR_BAD_PARAM;
134 }
135 if (pthread_rwlock_unlock((pthread_rwlock_t *)lock) != 0) {
136 return BSL_SAL_ERR_UNKNOWN;
137 }
138 return BSL_SUCCESS;
139 }
140
PthreadGetId(void)141 static uint64_t PthreadGetId(void)
142 {
143 return (uint64_t)pthread_self();
144 }
145
RegThreadFunc(void)146 static void RegThreadFunc(void)
147 {
148 BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_NEW_CB_FUNC, PthreadRWLockNew);
149 BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_FREE_CB_FUNC, PthreadRWLockFree);
150 BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_READ_LOCK_CB_FUNC, PthreadRWLockReadLock);
151 BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_WRITE_LOCK_CB_FUNC, PthreadRWLockWriteLock);
152 BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_LOCK_UNLOCK_CB_FUNC, PthreadRWLockUnlock);
153 BSL_SAL_CallBack_Ctrl(BSL_SAL_THREAD_GET_ID_CB_FUNC, PthreadGetId);
154 }
155
seedCtxFree(DRBG_Vec_t * seedCtx)156 static void seedCtxFree(DRBG_Vec_t *seedCtx)
157 {
158 if (seedCtx != NULL) {
159 DRBG_FREE(seedCtx->entropy);
160 DRBG_FREE(seedCtx->entropyPR1);
161 DRBG_FREE(seedCtx->entropyPR2);
162 DRBG_FREE(seedCtx->addin1);
163 DRBG_FREE(seedCtx->addin2);
164 DRBG_FREE(seedCtx->nonce);
165 DRBG_FREE(seedCtx->retBits);
166 DRBG_FREE(seedCtx->pers);
167 }
168 free(seedCtx);
169 }
170
seedCtxMem(void)171 static DRBG_Vec_t *seedCtxMem(void)
172 {
173 DRBG_Vec_t *seedCtx;
174
175 seedCtx = calloc(1u, sizeof(DRBG_Vec_t));
176 ASSERT_TRUE(seedCtx != NULL);
177 seedCtx->entropy = calloc(1u, sizeof(CRYPT_Data));
178 ASSERT_TRUE(seedCtx->entropy != NULL);
179 seedCtx->entropyPR1 = calloc(1u, sizeof(CRYPT_Data));
180 ASSERT_TRUE(seedCtx->entropyPR1 != NULL);
181 seedCtx->entropyPR2 = calloc(1u, sizeof(CRYPT_Data));
182 ASSERT_TRUE(seedCtx->entropyPR2 != NULL);
183 seedCtx->addin1 = calloc(1u, sizeof(CRYPT_Data));
184 ASSERT_TRUE(seedCtx->addin1 != NULL);
185 seedCtx->addin2 = calloc(1u, sizeof(CRYPT_Data));
186 ASSERT_TRUE(seedCtx->addin2 != NULL);
187 seedCtx->nonce = calloc(1u, sizeof(CRYPT_Data));
188 ASSERT_TRUE(seedCtx->nonce != NULL);
189 seedCtx->retBits = calloc(1u, sizeof(CRYPT_Data));
190 ASSERT_TRUE(seedCtx->retBits != NULL);
191 seedCtx->pers = calloc(1u, sizeof(CRYPT_Data));
192 ASSERT_TRUE(seedCtx->pers != NULL);
193
194 return seedCtx;
195 EXIT:
196 seedCtxFree(seedCtx);
197 return NULL;
198 }
199
200 /* Initializes the drbg context seed. Internally, ensure that the parameters are correct. */
seedCtxCfg(DRBG_Vec_t * seedCtx,Hex * entropy,Hex * nonce,Hex * pers,Hex * addin1,Hex * entropyPR1,Hex * addin2,Hex * entropyPR2,Hex * retBits)201 static void seedCtxCfg(DRBG_Vec_t *seedCtx, Hex *entropy, Hex *nonce, Hex *pers, Hex *addin1, Hex *entropyPR1,
202 Hex *addin2, Hex *entropyPR2, Hex *retBits)
203 {
204 seedCtx->entropy->data = entropy->x;
205 seedCtx->entropy->len = entropy->len;
206 seedCtx->nonce->data = nonce->x;
207 seedCtx->nonce->len = nonce->len;
208 seedCtx->pers->data = pers->x;
209 seedCtx->pers->len = pers->len;
210 seedCtx->addin1->data = addin1->x;
211 seedCtx->addin1->len = addin1->len;
212 seedCtx->entropyPR1->data = entropyPR1->x;
213 seedCtx->entropyPR1->len = entropyPR1->len;
214 seedCtx->addin2->data = addin2->x;
215 seedCtx->addin2->len = addin2->len;
216 seedCtx->entropyPR2->data = entropyPR2->x;
217 seedCtx->entropyPR2->len = entropyPR2->len;
218 seedCtx->retBits->data = retBits->x;
219 seedCtx->retBits->len = retBits->len;
220 }
221
getEntropyError(void * ctx,CRYPT_Data * entropy,uint32_t strength,CRYPT_Range * lenRange)222 static int32_t getEntropyError(void *ctx, CRYPT_Data *entropy, uint32_t strength, CRYPT_Range *lenRange)
223 {
224 (void)strength;
225 (void)lenRange;
226 CallBackCtl_t *state = (CallBackCtl_t *)ctx;
227 if (state->entropyState != 0) {
228 entropy = NULL;
229 return CRYPT_DRBG_FAIL_GET_ENTROPY;
230 }
231 uint32_t entroyLen = sizeof(uint8_t) * TEST_DRBG_DATA_SIZE;
232 entropy->data = calloc(1u, entroyLen);
233 entropy->len = entroyLen;
234 return CRYPT_SUCCESS;
235 }
236
cleanEntropyError(void * ctx,CRYPT_Data * entropy)237 static void cleanEntropyError(void *ctx, CRYPT_Data *entropy)
238 {
239 (void)ctx;
240 if (entropy != NULL && entropy->data != NULL) {
241 free(entropy->data);
242 }
243 return;
244 }
245
getNonceError(void * ctx,CRYPT_Data * nonce,uint32_t strength,CRYPT_Range * lenRange)246 static int32_t getNonceError(void *ctx, CRYPT_Data *nonce, uint32_t strength, CRYPT_Range *lenRange)
247 {
248 (void)strength;
249 (void)lenRange;
250 CallBackCtl_t *state = (CallBackCtl_t *)ctx;
251 if (state->nonceState != 0) {
252 nonce = NULL;
253 return CRYPT_DRBG_FAIL_GET_NONCE;
254 }
255 uint32_t nonceLen = sizeof(uint8_t) * TEST_DRBG_DATA_SIZE;
256 nonce->data = calloc(1u, nonceLen);
257 nonce->len = nonceLen;
258 return CRYPT_SUCCESS;
259 }
260
cleanNonceError(void * ctx,CRYPT_Data * nonce)261 static void cleanNonceError(void *ctx, CRYPT_Data *nonce)
262 {
263 (void)ctx;
264 if (nonce != NULL && nonce->data != NULL) {
265 free(nonce->data);
266 }
267 return;
268 }
269
getEntropy(void * ctx,CRYPT_Data * entropy,uint32_t strength,CRYPT_Range * lenRange)270 static int32_t getEntropy(void *ctx, CRYPT_Data *entropy, uint32_t strength, CRYPT_Range *lenRange)
271 {
272 (void)strength;
273 if (ctx == NULL || entropy == NULL || lenRange == NULL) {
274 return CRYPT_NULL_INPUT;
275 }
276 DRBG_Vec_t *seedCtx = (DRBG_Vec_t *)ctx;
277
278 if (seedCtx->entropy->len > lenRange->max || seedCtx->entropy->len < lenRange->min) {
279 return CRYPT_DRBG_INVALID_LEN;
280 }
281
282 entropy->data = seedCtx->entropy->data;
283 entropy->len = seedCtx->entropy->len;
284
285 return CRYPT_SUCCESS;
286 }
287
cleanEntropy(void * ctx,CRYPT_Data * entropy)288 static void cleanEntropy(void *ctx, CRYPT_Data *entropy)
289 {
290 if (ctx == NULL || entropy == NULL) {
291 return;
292 }
293 return;
294 }
295
getNonce(void * ctx,CRYPT_Data * nonce,uint32_t strength,CRYPT_Range * lenRange)296 static int32_t getNonce(void *ctx, CRYPT_Data *nonce, uint32_t strength, CRYPT_Range *lenRange)
297 {
298 (void)strength;
299 if (ctx == NULL || nonce == NULL || lenRange == NULL) {
300 return CRYPT_NULL_INPUT;
301 }
302
303 DRBG_Vec_t *seedCtx = (DRBG_Vec_t *)ctx;
304
305 if (seedCtx->nonce->len > lenRange->max || seedCtx->nonce->len < lenRange->min) {
306 return CRYPT_DRBG_INVALID_LEN;
307 }
308
309 nonce->data = seedCtx->nonce->data;
310 nonce->len = seedCtx->nonce->len;
311
312 return CRYPT_SUCCESS;
313 }
314
cleanNonce(void * ctx,CRYPT_Data * nonce)315 static void cleanNonce(void *ctx, CRYPT_Data *nonce)
316 {
317 if (ctx == NULL || nonce == NULL) {
318 return;
319 }
320 return;
321 }
322
getEntropyUnCheckPara(void * ctx,CRYPT_Data * entropy,uint32_t strength,CRYPT_Range * lenRange)323 static int32_t getEntropyUnCheckPara(void *ctx, CRYPT_Data *entropy, uint32_t strength, CRYPT_Range *lenRange)
324 {
325 (void)strength;
326 (void)lenRange;
327 DRBG_Vec_t *seedCtx = (DRBG_Vec_t *)ctx;
328 entropy->data = seedCtx->entropy->data;
329 entropy->len = seedCtx->entropy->len;
330
331 return CRYPT_SUCCESS;
332 }
333
getNonceUnCheckPara(void * ctx,CRYPT_Data * nonce,uint32_t strength,CRYPT_Range * lenRange)334 static int32_t getNonceUnCheckPara(void *ctx, CRYPT_Data *nonce, uint32_t strength, CRYPT_Range *lenRange)
335 {
336 (void)strength;
337 (void)lenRange;
338 DRBG_Vec_t *seedCtx = (DRBG_Vec_t *)ctx;
339
340 nonce->data = seedCtx->nonce->data;
341 nonce->len = seedCtx->nonce->len;
342
343 return CRYPT_SUCCESS;
344 }
345
regSeedMeth(CRYPT_RandSeedMethod * seedMeth)346 static void regSeedMeth(CRYPT_RandSeedMethod *seedMeth)
347 {
348 seedMeth->getEntropy = getEntropy;
349 seedMeth->cleanEntropy = cleanEntropy;
350 seedMeth->getNonce = getNonce;
351 seedMeth->cleanNonce = cleanNonce;
352 }
353
drbgDataInit(CRYPT_Data * data,uint32_t size)354 static void drbgDataInit(CRYPT_Data *data, uint32_t size)
355 {
356 uint8_t *dataTmp = NULL;
357 if (size != 0) {
358 dataTmp = malloc(sizeof(uint8_t) * size);
359 if (dataTmp == NULL) {
360 return;
361 }
362 (void)memset_s(dataTmp, size, 0, size);
363 }
364 data->data = dataTmp;
365 data->len = size;
366 }
367
drbgDataFree(CRYPT_Data * data)368 static void drbgDataFree(CRYPT_Data *data)
369 {
370 if (data != NULL) {
371 if (data->data != NULL) {
372 free(data->data);
373 }
374 }
375 }
376
377 /* Mapping between RAND and specific random number generation algorithms */
378 static const int g_drbgMethodMap[] = {
379 CRYPT_MD_SHA1,
380 CRYPT_MD_SHA224,
381 CRYPT_MD_SHA256,
382 CRYPT_MD_SHA384,
383 CRYPT_MD_SHA512,
384 CRYPT_MD_SM3,
385 CRYPT_MAC_HMAC_SHA1,
386 CRYPT_MAC_HMAC_SHA224,
387 CRYPT_MAC_HMAC_SHA256,
388 CRYPT_MAC_HMAC_SHA384,
389 CRYPT_MAC_HMAC_SHA512,
390 CRYPT_SYM_AES128,
391 CRYPT_SYM_AES192,
392 CRYPT_SYM_AES256,
393 CRYPT_SYM_AES128,
394 CRYPT_SYM_AES192,
395 CRYPT_SYM_AES256
396 };
397
GetAesKeyLen(int id,uint32_t * keyLen)398 static uint32_t GetAesKeyLen(int id, uint32_t *keyLen)
399 {
400 switch (id) {
401 case CRYPT_SYM_AES128:
402 *keyLen = RAND_AES128_KEYLEN;
403 break;
404 case CRYPT_SYM_AES192:
405 *keyLen = RAND_AES192_KEYLEN;
406 break;
407 case CRYPT_SYM_AES256:
408 *keyLen = RAND_AES256_KEYLEN;
409 break;
410 default:
411 return CRYPT_DRBG_ALG_NOT_SUPPORT;
412 }
413 return CRYPT_SUCCESS;
414 }
415
InitSeedCtx(CRYPT_RAND_AlgId id,DRBG_Vec_t * seedCtx,CRYPT_Data * data)416 static void InitSeedCtx(CRYPT_RAND_AlgId id, DRBG_Vec_t *seedCtx, CRYPT_Data *data)
417 {
418 if (id < CRYPT_RAND_AES128_CTR || id > CRYPT_RAND_AES256_CTR) {
419 drbgDataInit(data, TEST_DRBG_DATA_SIZE);
420 seedCtx->entropy = data;
421 seedCtx->nonce = data;
422 } else {
423 uint32_t keyLen = 0;
424 GetAesKeyLen(g_drbgMethodMap[id - CRYPT_RAND_SHA1], &keyLen);
425 drbgDataInit(data, (AES_BLOCK_LEN + keyLen));
426 seedCtx->entropy = data;
427 }
428 return;
429 }
430
sdvCryptEalRandSeedAdinApiTest(uint8_t * addin,uint32_t addinLen)431 static int sdvCryptEalRandSeedAdinApiTest(uint8_t *addin, uint32_t addinLen)
432 {
433 int ret;
434 uint8_t *output = NULL;
435
436 CRYPT_Data data = { 0 };
437 CRYPT_RandSeedMethod seedMeth = { 0 };
438 DRBG_Vec_t seedCtx = { 0 };
439
440 TestMemInit();
441 regSeedMeth(&seedMeth);
442 drbgDataInit(&data, TEST_DRBG_DATA_SIZE);
443
444 seedCtx.entropy = &data;
445 seedCtx.nonce = &data;
446 ASSERT_EQ(CRYPT_EAL_RandInit(CRYPT_RAND_SHA256, &seedMeth, &seedCtx, NULL, 0), CRYPT_SUCCESS);
447
448 output = malloc(sizeof(uint8_t) * DRBG_OUTPUT_SIZE);
449 ASSERT_TRUE(output != NULL);
450 (void)memset_s(output, sizeof(uint8_t) * DRBG_OUTPUT_SIZE, 0, sizeof(uint8_t) * DRBG_OUTPUT_SIZE);
451 ret = CRYPT_EAL_RandbytesWithAdin(output, DRBG_OUTPUT_SIZE, NULL, 0);
452 ASSERT_EQ(ret, CRYPT_SUCCESS);
453
454 ret = CRYPT_EAL_RandSeedWithAdin(addin, addinLen);
455 ASSERT_EQ(ret, CRYPT_SUCCESS);
456
457 ret = CRYPT_EAL_RandSeed();
458 ASSERT_EQ(ret, CRYPT_SUCCESS);
459
460 EXIT:
461 CRYPT_EAL_RandDeinit();
462 drbgDataFree(&data);
463 free(output);
464 return ret;
465 }
466
sdvCryptEalDrbgSeedAdinApiTest(uint8_t * addin,uint32_t addinLen)467 static int sdvCryptEalDrbgSeedAdinApiTest(uint8_t *addin, uint32_t addinLen)
468 {
469 int ret;
470 uint8_t *output = NULL;
471
472 CRYPT_Data data = { 0 };
473 CRYPT_RandSeedMethod seedMeth = { 0 };
474 DRBG_Vec_t seedCtx = { 0 };
475 void *drbgCtx = NULL;
476
477 TestMemInit();
478 regSeedMeth(&seedMeth);
479 drbgDataInit(&data, TEST_DRBG_DATA_SIZE);
480
481 seedCtx.entropy = &data;
482 seedCtx.nonce = &data;
483 drbgCtx = CRYPT_EAL_DrbgNew(CRYPT_RAND_SHA256, &seedMeth, &seedCtx);
484 ASSERT_TRUE(drbgCtx != NULL);
485 ASSERT_TRUE(CRYPT_EAL_DrbgInstantiate(drbgCtx, NULL, 0) == CRYPT_SUCCESS);
486
487 output = malloc(sizeof(uint8_t) * DRBG_OUTPUT_SIZE);
488 ASSERT_TRUE(output != NULL);
489 (void)memset_s(output, sizeof(uint8_t) * DRBG_OUTPUT_SIZE, 0, sizeof(uint8_t) * DRBG_OUTPUT_SIZE);
490 ret = CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, output, DRBG_OUTPUT_SIZE, NULL, 0);
491 ASSERT_EQ(ret, CRYPT_SUCCESS);
492
493 ret = CRYPT_EAL_DrbgSeedWithAdin(drbgCtx, addin, addinLen);
494 ASSERT_EQ(ret, CRYPT_SUCCESS);
495
496 ret = CRYPT_EAL_DrbgSeed(drbgCtx);
497 ASSERT_EQ(ret, CRYPT_SUCCESS);
498
499 EXIT:
500 CRYPT_EAL_DrbgDeinit(drbgCtx);
501 drbgDataFree(&data);
502 free(output);
503 return ret;
504 }
505
sdvCryptEalThreadTest(void * drbgCtx)506 static void sdvCryptEalThreadTest(void *drbgCtx)
507 {
508 int i = 0;
509 int ret;
510 uint8_t *output = NULL;
511
512 output = malloc(sizeof(uint8_t) * DRBG_OUTPUT_SIZE);
513 ASSERT_TRUE(output != NULL);
514
515 for (i = 0; i < 100; i++) { // Perform 100 * 2 times random number generation in the thread.
516 ret = CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, output, sizeof(uint8_t) * DRBG_OUTPUT_SIZE, NULL, 0);
517 ASSERT_EQ(ret, CRYPT_SUCCESS);
518
519 ret = CRYPT_EAL_DrbgSeedWithAdin(drbgCtx, NULL, 0);
520 ASSERT_EQ(ret, CRYPT_SUCCESS);
521
522 ret = CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, output, sizeof(uint8_t) * DRBG_OUTPUT_SIZE, NULL, 0);
523 ASSERT_EQ(ret, CRYPT_SUCCESS);
524 }
525
526 EXIT:
527 DRBG_FREE(output);
528 return;
529 }
530
sdvCryptGlobalThreadTest(void)531 static void sdvCryptGlobalThreadTest(void)
532 {
533 int i = 0;
534 uint8_t *output = NULL;
535
536 output = malloc(sizeof(uint8_t) * DRBG_OUTPUT_SIZE);
537 ASSERT_TRUE(output != NULL);
538
539 for (i = 0; i < 100; i++) { // Perform 100 times random number generation in the thread.
540 ASSERT_EQ(CRYPT_EAL_Randbytes(output, sizeof(uint8_t) * DRBG_OUTPUT_SIZE), CRYPT_SUCCESS);
541 }
542
543 EXIT:
544 DRBG_FREE(output);
545 return;
546 }
547
548 /**
549 * @test SDV_CRYPT_DRBG_RAND_INIT_API_TC001
550 * @title Use different algorithm ID to initialize the DRBG.
551 * @precon nan
552 * @brief
553 * 1.Initialize the random number seed, expected result 1.
554 * 2.Call CRYPT_EAL_RandInit, expected result 2.
555 * 3.Call CRYPT_EAL_DrbgNew, expected result 3.
556 * @expect
557 * 1.successful.
558 * 2.Success with or without a random number seed.
559 * 3.successful.
560 */
561 /* BEGIN_CASE */
SDV_CRYPT_DRBG_RAND_INIT_API_TC001(int algId)562 void SDV_CRYPT_DRBG_RAND_INIT_API_TC001(int algId)
563 {
564 if (IsRandAlgDisabled(algId)) {
565 SKIP_TEST();
566 }
567 void *drbg = NULL;
568 CRYPT_RandSeedMethod seedMeth = { 0 };
569 CRYPT_Data data = { 0 };
570 DRBG_Vec_t seedCtx = { 0 };
571
572 TestMemInit();
573 seedMeth.getEntropy = getEntropy;
574 seedMeth.cleanEntropy = cleanEntropy;
575
576 InitSeedCtx(algId, &seedCtx, &data);
577 ASSERT_EQ(CRYPT_EAL_RandInit(algId, &seedMeth, (void *)&seedCtx, NULL, 0), CRYPT_SUCCESS);
578 CRYPT_EAL_RandDeinit();
579 ASSERT_EQ(CRYPT_EAL_RandInit(algId, NULL, NULL, NULL, 0), CRYPT_SUCCESS);
580 CRYPT_EAL_RandDeinit();
581 drbg = CRYPT_EAL_DrbgNew(algId, &seedMeth, &seedCtx);
582 ASSERT_TRUE(drbg != NULL);
583 ASSERT_TRUE(CRYPT_EAL_DrbgInstantiate(drbg, NULL, 0) == CRYPT_SUCCESS);
584
585 EXIT:
586 CRYPT_EAL_RandDeinit();
587 CRYPT_EAL_DrbgDeinit(drbg);
588 drbgDataFree(&data);
589 return;
590 }
591 /* END_CASE */
592
593 /**
594 * @test SDV_CRYPT_DRBG_RAND_INIT_API_TC002
595 * @title DRBG initialization test,the value of data is 0 or 255.
596 * @precon nan
597 * @brief
598 * 1.Initialize the random number seed, expected result 1.
599 * 2.Call CRYPT_EAL_RandInit, expected result 2.
600 * 3.Call CRYPT_EAL_DrbgNew, expected result 3.
601 * @expect
602 * 1.successful.
603 * 2.successful.
604 * 3.successful.
605 */
606 /* BEGIN_CASE */
SDV_CRYPT_DRBG_RAND_INIT_API_TC002(int agId,int value,int size)607 void SDV_CRYPT_DRBG_RAND_INIT_API_TC002(int agId, int value, int size)
608 {
609 uint8_t *pers = malloc(size);
610 ASSERT_TRUE(pers != NULL);
611 ASSERT_EQ(memset_s(pers, size, value, size), 0);
612 void *drbg = NULL;
613 CRYPT_RandSeedMethod seedMeth = { 0 };
614 CRYPT_Data data = { 0 };
615 DRBG_Vec_t seedCtx = { 0 };
616
617 TestMemInit();
618 regSeedMeth(&seedMeth);
619 drbgDataInit(&data, size);
620
621 seedCtx.entropy = &data;
622 seedCtx.nonce = &data;
623
624 ASSERT_EQ(CRYPT_EAL_RandInit(agId, &seedMeth, (void *)&seedCtx, pers, size), CRYPT_SUCCESS);
625 CRYPT_EAL_RandDeinit();
626 drbg = CRYPT_EAL_DrbgNew(agId, &seedMeth, &seedCtx);
627 ASSERT_TRUE(drbg != NULL);
628 CRYPT_EAL_DrbgDeinit(drbg);
629
630 EXIT:
631 CRYPT_EAL_RandDeinit();
632 drbgDataFree(&data);
633 free(pers);
634 return;
635 }
636 /* END_CASE */
637
638 /**
639 * @test SDV_CRYPT_DRBG_RAND_INIT_API_TC003
640 * @title Test the impact of persLen on DRGB initialization.
641 * @precon nan
642 * @brief
643 * 1.Initialize the random number seed, expected result 1.
644 * 2.Call CRYPT_EAL_RandInit, expected result 2.
645 * 3.Call CRYPT_EAL_DrbgNew, expected result 3.
646 * @expect
647 * 1.successful.
648 * 2.successful.
649 * 3.successful.
650 */
651 /* BEGIN_CASE */
SDV_CRYPT_DRBG_RAND_INIT_API_TC003(int agId,int size)652 void SDV_CRYPT_DRBG_RAND_INIT_API_TC003(int agId, int size)
653 {
654 uint8_t *pers = malloc(TEST_DRBG_DATA_SIZE);
655 ASSERT_TRUE(pers != NULL);
656 void *drbg = NULL;
657 CRYPT_RandSeedMethod seedMeth = { 0 };
658 CRYPT_Data data = { 0 };
659 DRBG_Vec_t seedCtx = { 0 };
660
661 TestMemInit();
662 regSeedMeth(&seedMeth);
663 drbgDataInit(&data, size);
664
665 seedCtx.entropy = &data;
666 seedCtx.nonce = &data;
667
668 ASSERT_EQ(CRYPT_EAL_RandInit(agId, &seedMeth, (void *)&seedCtx, pers, size), CRYPT_SUCCESS);
669 CRYPT_EAL_RandDeinit();
670 drbg = CRYPT_EAL_DrbgNew(agId, &seedMeth, &seedCtx);
671 ASSERT_TRUE(drbg != NULL);
672 CRYPT_EAL_DrbgDeinit(drbg);
673
674 EXIT:
675 CRYPT_EAL_RandDeinit();
676 drbgDataFree(&data);
677 free(pers);
678 return;
679 }
680 /* END_CASE */
681
682 /**
683 * @test SDV_CRYPT_DRBG_RAND_INIT_API_TC004
684 * @title DRBG is initialized repeatedly.
685 * @precon nan
686 * @brief
687 * 1.Initialize the random number seed, expected result 1.
688 * 2.Call CRYPT_EAL_RandInit, expected result 2.
689 * 3.Call CRYPT_EAL_RandInit again, expected result 3.
690 * @expect
691 * 1.successful.
692 * 2.successful.
693 * 3.return failed.
694 */
695 /* BEGIN_CASE */
SDV_CRYPT_DRBG_RAND_INIT_API_TC004(int algId)696 void SDV_CRYPT_DRBG_RAND_INIT_API_TC004(int algId)
697 {
698 CRYPT_Data data = { 0 };
699 CRYPT_RandSeedMethod seedMeth = { 0 };
700 DRBG_Vec_t seedCtx = { 0 };
701
702 TestMemInit();
703 regSeedMeth(&seedMeth);
704 drbgDataInit(&data, TEST_DRBG_DATA_SIZE);
705
706 seedCtx.entropy = &data;
707 seedCtx.nonce = &data;
708
709 ASSERT_EQ(CRYPT_EAL_RandInit(algId, &seedMeth, (void *)&seedCtx, NULL, 0), CRYPT_SUCCESS);
710
711 ASSERT_EQ(CRYPT_EAL_RandInit(algId, &seedMeth, (void *)&seedCtx, NULL, 0), CRYPT_EAL_ERR_DRBG_REPEAT_INIT);
712
713 EXIT:
714 CRYPT_EAL_RandDeinit();
715 drbgDataFree(&data);
716 return;
717 }
718 /* END_CASE */
719
720 /**
721 * @test SDV_CRYPT_DRBG_RAND_INIT_API_TC005
722 * @title DRBG initialization test,the configuration context parameter is empty.
723 * @precon nan
724 * @brief
725 * 1.Initialize the random number seed, expected result 1.
726 * 2.Call CRYPT_EAL_RandInit, expected result 2.
727 * 3.Call CRYPT_EAL_DrbgNew, expected result 3.
728 * @expect
729 * 1.successful.
730 * 2.successful.
731 * 3.return NULL.
732 */
733 /* BEGIN_CASE */
SDV_CRYPT_DRBG_RAND_INIT_API_TC005(int algId)734 void SDV_CRYPT_DRBG_RAND_INIT_API_TC005(int algId)
735 {
736 DRBG_Vec_t seedCtx = { 0 };
737 void *drbg = NULL;
738
739 ASSERT_EQ(CRYPT_EAL_RandInit(algId, NULL, NULL, NULL, 0), CRYPT_SUCCESS);
740 CRYPT_EAL_RandDeinit();
741 ASSERT_NE(CRYPT_EAL_RandInit(algId, NULL, &seedCtx, NULL, 0), CRYPT_SUCCESS);
742 drbg = CRYPT_EAL_DrbgNew(algId, NULL, NULL);
743 ASSERT_TRUE(drbg != NULL);
744 CRYPT_EAL_DrbgDeinit(drbg);
745 drbg = CRYPT_EAL_DrbgNew(algId, NULL, &seedCtx);
746 ASSERT_TRUE(drbg == NULL);
747 EXIT:
748 CRYPT_EAL_RandDeinit();
749 CRYPT_EAL_DrbgDeinit(drbg);
750 return;
751 }
752 /* END_CASE */
753
754 /**
755 * @test SDV_CRYPT_DRBG_RAND_INIT_API_TC006
756 * @title DRBG initialization test,use the abnormal persLen.
757 * @precon nan
758 * @brief
759 * 1.Initialize the random number seed, expected result 1.
760 * 2.Call CRYPT_EAL_RandInit, expected result 2.
761 * 3.Call CRYPT_EAL_DrbgNew, expected result 3.
762 * @expect
763 * 1.successful.
764 * 2.return failed.
765 * 3.return NULL.
766 */
767 /* BEGIN_CASE */
SDV_CRYPT_DRBG_RAND_INIT_API_TC006(int algId,int keyLen)768 void SDV_CRYPT_DRBG_RAND_INIT_API_TC006(int algId, int keyLen)
769 {
770 CRYPT_Data data = { 0 };
771 CRYPT_RandSeedMethod seedMeth = { 0 };
772 DRBG_Vec_t seedCtx = { 0 };
773 uint8_t *pers = NULL;
774 void *drbg = NULL;
775
776 pers = malloc(TEST_DRBG_DATA_SIZE + keyLen);
777 ASSERT_TRUE(pers != NULL);
778 TestMemInit();
779 regSeedMeth(&seedMeth);
780 drbgDataInit(&data, keyLen + 16);
781 seedCtx.entropy = &data;
782 seedCtx.nonce = &data;
783 ASSERT_NE(CRYPT_EAL_RandInit(algId, &seedMeth, (void *)&seedCtx, pers, keyLen + 16 + 1), CRYPT_SUCCESS);
784 CRYPT_EAL_RandDeinit();
785 drbg = CRYPT_EAL_DrbgNew(algId, &seedMeth, &seedCtx);
786 ASSERT_TRUE(drbg != NULL);
787 ASSERT_NE(CRYPT_EAL_DrbgInstantiate(drbg, pers, keyLen + 16 + 1), CRYPT_SUCCESS);
788 CRYPT_EAL_DrbgDeinit(drbg);
789 ASSERT_EQ(CRYPT_EAL_RandInit(algId, &seedMeth, (void *)&seedCtx, pers, keyLen + 16), CRYPT_SUCCESS);
790 drbg = CRYPT_EAL_DrbgNew(algId, &seedMeth, &seedCtx);
791 ASSERT_TRUE(drbg != NULL);
792 EXIT:
793 CRYPT_EAL_DrbgDeinit(drbg);
794 CRYPT_EAL_RandDeinit();
795 free(data.data);
796 free(pers);
797 return;
798 }
799 /* END_CASE */
800
801 /**
802 * @test SDV_CRYPT_DRBG_RAND_SEED_ADIN_API_TC001
803 * @title When the RAND is initialized and a random number has been generated,
804 the counter is reset when the random number is obtained from personal data.
805 * @precon nan
806 * @brief
807 * 1.Call sdvCryptEalRandSeedAdinApiTest,addinData is NULL, expected result 1.
808 * 2.Call sdvCryptEalRandSeedAdinApiTest,addinData not NULL, expected result 2.
809 * @expect
810 * 1.All operations succeeded.
811 * 2.All operations succeeded.
812 */
813 /* BEGIN_CASE */
SDV_CRYPT_DRBG_RAND_SEED_ADIN_API_TC001(void)814 void SDV_CRYPT_DRBG_RAND_SEED_ADIN_API_TC001(void)
815 {
816 uint8_t *addinData = NULL;
817
818 ASSERT_EQ(sdvCryptEalRandSeedAdinApiTest(NULL, 0), CRYPT_SUCCESS);
819
820 addinData = malloc(sizeof(uint8_t) * DRBG_MAX_ADIN_SIZE);
821 ASSERT_TRUE(addinData != NULL);
822 ASSERT_EQ(sdvCryptEalRandSeedAdinApiTest(addinData, sizeof(uint8_t) * DRBG_MAX_ADIN_SIZE), CRYPT_SUCCESS);
823
824 EXIT:
825 free(addinData);
826 return;
827 }
828 /* END_CASE */
829
830 /**
831 * @test SDV_CRYPT_DRBG_DRBG_SEED_ADIN_API_TC001
832 * @title When the DRBG is initialized and a random number has been generated,
833 the counter is reset when the random number is obtained from personal data.
834 * @precon nan
835 * @brief
836 * 1.Call sdvCryptEalDrbgSeedAdinApiTest,addinData is NULL, expected result 1.
837 * 2.Call sdvCryptEalDrbgSeedAdinApiTest,addinData not NULL, expected result 2.
838 * @expect
839 * 1.All operations succeeded.
840 * 2.All operations succeeded.
841 */
842 /* BEGIN_CASE */
SDV_CRYPT_DRBG_DRBG_SEED_ADIN_API_TC001(void)843 void SDV_CRYPT_DRBG_DRBG_SEED_ADIN_API_TC001(void)
844 {
845 uint8_t *addinData = NULL;
846
847 ASSERT_EQ(sdvCryptEalDrbgSeedAdinApiTest(NULL, 0), CRYPT_SUCCESS);
848
849 addinData = malloc(sizeof(uint8_t) * DRBG_MAX_ADIN_SIZE);
850 ASSERT_TRUE(addinData != NULL);
851 ASSERT_EQ(sdvCryptEalDrbgSeedAdinApiTest(addinData, sizeof(uint8_t) * DRBG_MAX_ADIN_SIZE), CRYPT_SUCCESS);
852
853 EXIT:
854 free(addinData);
855 return;
856 }
857 /* END_CASE */
858
859 /**
860 * @test SDV_CRYPT_DRBG_RAND_SEED_ADIN_API_TC002
861 * @title Call random interface before CRYPT_EAL_RandInit.
862 * @precon nan
863 * @brief
864 * 1.Call CRYPT_EAL_RandbytesWithAdin, expected result 1.
865 * 2.Call CRYPT_EAL_Randbytes, expected result 2.
866 * 3.Call CRYPT_EAL_RandSeedWithAdin, expected result 3.
867 * 4.Call CRYPT_EAL_RandSeed, expected result 4.
868 * @expect
869 * 1.return CRYPT_EAL_ERR_GLOBAL_DRBG_NULL.
870 * 2.return CRYPT_EAL_ERR_GLOBAL_DRBG_NULL.
871 * 3.return CRYPT_EAL_ERR_GLOBAL_DRBG_NULL.
872 * 4.return CRYPT_EAL_ERR_GLOBAL_DRBG_NULL.
873 */
874 /* BEGIN_CASE */
SDV_CRYPT_DRBG_RAND_SEED_ADIN_API_TC002(void)875 void SDV_CRYPT_DRBG_RAND_SEED_ADIN_API_TC002(void)
876 {
877 uint8_t data[TEST_DRBG_DATA_SIZE] = {0};
878 ASSERT_EQ(CRYPT_EAL_RandbytesWithAdin(data, TEST_DRBG_DATA_SIZE, NULL, 0), CRYPT_EAL_ERR_GLOBAL_DRBG_NULL);
879 ASSERT_EQ(CRYPT_EAL_Randbytes(data, TEST_DRBG_DATA_SIZE), CRYPT_EAL_ERR_GLOBAL_DRBG_NULL);
880 ASSERT_EQ(CRYPT_EAL_RandSeedWithAdin(NULL, 0), CRYPT_EAL_ERR_GLOBAL_DRBG_NULL);
881 ASSERT_EQ(CRYPT_EAL_RandSeed(), CRYPT_EAL_ERR_GLOBAL_DRBG_NULL);
882 EXIT:
883 return;
884 }
885 /* END_CASE */
886
887 /**
888 * @test SDV_CRYPT_DRBG_RAND_BYTES_ADIN_ERR_PARA_API_TC001
889 * @title CRYPT_EAL_RandbytesWithAdin abnormal parameter test.
890 * @precon nan
891 * @brief
892 * 1.Call CRYPT_EAL_DrbgbytesWithAdin,use normal parameters, expected result 1.
893 * 2.Call CRYPT_EAL_DrbgbytesWithAdin,the array length is abnormal, expected result 2.
894 * @expect
895 * 1.All interface succeeded.
896 * 2.The interface returns an exception.
897 */
898 /* BEGIN_CASE */
SDV_CRYPT_DRBG_RAND_BYTES_ADIN_ERR_PARA_API_TC001(int algId)899 void SDV_CRYPT_DRBG_RAND_BYTES_ADIN_ERR_PARA_API_TC001(int algId)
900 {
901 uint8_t *output = malloc(sizeof(uint8_t) * (DRBG_MAX_OUTPUT_SIZE + 1));
902 ASSERT_TRUE(output != NULL);
903 uint8_t *addin = malloc(sizeof(uint8_t) * DRBG_MAX_ADIN_SIZE);
904 ASSERT_TRUE(addin != NULL);
905
906 CRYPT_RandSeedMethod seedMeth = { 0 };
907 CRYPT_Data data = { 0 };
908 DRBG_Vec_t seedCtx = { 0 };
909 regSeedMeth(&seedMeth);
910 drbgDataInit(&data, TEST_DRBG_DATA_SIZE);
911 seedCtx.entropy = &data;
912 seedCtx.nonce = &data;
913
914 TestMemInit();
915 CRYPT_EAL_RndCtx *drbgCtx = CRYPT_EAL_DrbgNew(algId, &seedMeth, &seedCtx);
916 ASSERT_TRUE(drbgCtx != NULL);
917 ASSERT_TRUE(CRYPT_EAL_DrbgInstantiate(drbgCtx, NULL, 0) == CRYPT_SUCCESS);
918
919 memset_s(addin, DRBG_MAX_ADIN_SIZE, 0, DRBG_MAX_ADIN_SIZE);
920 ASSERT_EQ(CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, output, DRBG_MAX_OUTPUT_SIZE, addin, DRBG_MAX_ADIN_SIZE),
921 CRYPT_SUCCESS);
922
923 memset_s(addin, DRBG_MAX_ADIN_SIZE, 'F', DRBG_MAX_ADIN_SIZE);
924 ASSERT_EQ(CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, output, DRBG_MAX_OUTPUT_SIZE, addin, DRBG_MAX_ADIN_SIZE),
925 CRYPT_SUCCESS);
926
927 ASSERT_EQ(CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, output, DRBG_MAX_OUTPUT_SIZE, NULL, 0), CRYPT_SUCCESS);
928 ASSERT_EQ(CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, output, DRBG_MAX_OUTPUT_SIZE, addin, 0), CRYPT_SUCCESS);
929
930 memset_s(addin, DRBG_MAX_ADIN_SIZE, 0, DRBG_MAX_ADIN_SIZE);
931 ASSERT_NE(CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, output, 0, addin, DRBG_MAX_ADIN_SIZE), CRYPT_SUCCESS);
932 ASSERT_EQ(CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, output, DRBG_MAX_OUTPUT_SIZE + 1, addin, DRBG_MAX_ADIN_SIZE),
933 CRYPT_SUCCESS);
934 ASSERT_NE(CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, NULL, 0, addin, DRBG_MAX_ADIN_SIZE), CRYPT_SUCCESS);
935
936 EXIT:
937 free(addin);
938 free(output);
939 CRYPT_EAL_DrbgDeinit(drbgCtx);
940 drbgDataFree(&data);
941 return;
942 }
943 /* END_CASE */
944
945 /**
946 * @test SDV_CRYPT_DRBG_RAND_BYTES_ERR_PARA_API_TC001
947 * @title Test the CRYPT_EAL_Randbytes interface for generating random numbers.
948 * @precon nan
949 * @brief
950 * 1.Initialize the random number seed, expected result 1.
951 * 2.Call CRYPT_EAL_Randbytes,use normal parameters, expected result 2.
952 * 3.Call CRYPT_EAL_Randbytes,the array length is abnormal, expected result 3.
953 * @expect
954 * 1.successful.
955 * 2.All interface succeeded.
956 * 3.The interface returns an exception.
957 */
958 /* BEGIN_CASE */
SDV_CRYPT_DRBG_RAND_BYTES_ERR_PARA_API_TC001(void)959 void SDV_CRYPT_DRBG_RAND_BYTES_ERR_PARA_API_TC001(void)
960 {
961 uint8_t *output = malloc(DRBG_MAX_OUTPUT_SIZE + 1);
962 ASSERT_TRUE(output != NULL);
963
964 CRYPT_RandSeedMethod seedMeth = { 0 };
965 CRYPT_Data data = { 0 };
966 DRBG_Vec_t seedCtx = { 0 };
967
968 TestMemInit();
969 regSeedMeth(&seedMeth);
970 drbgDataInit(&data, TEST_DRBG_DATA_SIZE);
971
972 seedCtx.nonce = &data;
973 seedCtx.entropy = &data;
974 ASSERT_EQ(CRYPT_EAL_RandInit(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx, NULL, 0), CRYPT_SUCCESS);
975
976 ASSERT_EQ(CRYPT_EAL_Randbytes(output, DRBG_OUTPUT_SIZE), CRYPT_SUCCESS);
977 ASSERT_EQ(CRYPT_EAL_Randbytes(output, DRBG_MAX_OUTPUT_SIZE), CRYPT_SUCCESS);
978
979 ASSERT_NE(CRYPT_EAL_Randbytes(output, 0), CRYPT_SUCCESS);
980 ASSERT_EQ(CRYPT_EAL_Randbytes(output, DRBG_MAX_OUTPUT_SIZE + 1), CRYPT_SUCCESS); // MAX SIZE + 1
981 ASSERT_EQ(CRYPT_EAL_Randbytes(NULL, 0), CRYPT_NULL_INPUT);
982 EXIT:
983 CRYPT_EAL_RandDeinit();
984 drbgDataFree(&data);
985 free(output);
986 return;
987 }
988 /* END_CASE */
989
990 /**
991 * @test SDV_CRYPT_DRBG_BYTES_ERR_PARA_API_TC001
992 * @title Test the CRYPT_EAL_Drbgbytes interface for generating random numbers.
993 * @precon nan
994 * @brief
995 * 1.Initialize the random number seed, expected result 1.
996 * 2.Call CRYPT_EAL_Drbgbytes,use normal parameters, expected result 2.
997 * 3.Call CRYPT_EAL_Drbgbytes,the array length is abnormal, expected result 3.
998 * @expect
999 * 1.successful.
1000 * 2.All interface succeeded.
1001 * 3.The interface returns an exception.
1002 */
1003 /* BEGIN_CASE */
SDV_CRYPT_DRBG_BYTES_ERR_PARA_API_TC001(void)1004 void SDV_CRYPT_DRBG_BYTES_ERR_PARA_API_TC001(void)
1005 {
1006 uint8_t *output = malloc(DRBG_MAX_OUTPUT_SIZE + 1);
1007 ASSERT_TRUE(output != NULL);
1008 CRYPT_RandSeedMethod seedMeth = { 0 };
1009 CRYPT_Data data = { 0 };
1010 DRBG_Vec_t seedCtx = { 0 };
1011 void *drbg = NULL;
1012
1013 TestMemInit();
1014 regSeedMeth(&seedMeth);
1015 drbgDataInit(&data, TEST_DRBG_DATA_SIZE);
1016
1017 seedCtx.nonce = &data;
1018 seedCtx.entropy = &data;
1019 drbg = CRYPT_EAL_DrbgNew(CRYPT_RAND_SHA256, &seedMeth, &seedCtx);
1020 ASSERT_TRUE(drbg != NULL);
1021 ASSERT_TRUE(CRYPT_EAL_DrbgInstantiate(drbg, NULL, 0) == CRYPT_SUCCESS);
1022
1023 ASSERT_EQ(CRYPT_EAL_Drbgbytes(drbg, output, DRBG_OUTPUT_SIZE), CRYPT_SUCCESS);
1024 ASSERT_EQ(CRYPT_EAL_Drbgbytes(drbg, output, DRBG_MAX_OUTPUT_SIZE), CRYPT_SUCCESS);
1025
1026 ASSERT_NE(CRYPT_EAL_Drbgbytes(drbg, output, 0), CRYPT_SUCCESS);
1027 ASSERT_EQ(CRYPT_EAL_Drbgbytes(drbg, output, DRBG_MAX_OUTPUT_SIZE + 1), CRYPT_SUCCESS); // MAX SIZE + 1
1028 ASSERT_NE(CRYPT_EAL_Drbgbytes(drbg, NULL, 0), CRYPT_SUCCESS);
1029 ASSERT_EQ(CRYPT_EAL_Drbgbytes(NULL, output, DRBG_OUTPUT_SIZE), CRYPT_NULL_INPUT);
1030
1031 EXIT:
1032 CRYPT_EAL_DrbgDeinit(drbg);
1033 drbgDataFree(&data);
1034 free(output);
1035 return;
1036 }
1037 /* END_CASE */
1038
1039 /**
1040 * @test SDV_CRYPT_DRBG_RAND_SEED_ADIN_ERR_PARA_API_TC001
1041 * @title Test the CRYPT_EAL_RandSeedWithAdin interface.
1042 * @precon nan
1043 * @brief
1044 * 1.Initialize the random number seed, expected result 1.
1045 * 2.Call CRYPT_EAL_RandSeedWithAdin,use exception parameters, expected result 2.
1046 * @expect
1047 * 1.successful.
1048 * 2.The interface returns an exception.
1049 */
1050 /* BEGIN_CASE */
SDV_CRYPT_DRBG_RAND_SEED_ADIN_ERR_PARA_API_TC001(void)1051 void SDV_CRYPT_DRBG_RAND_SEED_ADIN_ERR_PARA_API_TC001(void)
1052 {
1053 uint32_t addinLen = sizeof(uint8_t) * DRBG_MAX_ADIN_SIZE;
1054 uint8_t *addin = malloc(addinLen);
1055 ASSERT_TRUE(addin != NULL);
1056 memset_s(addin, addinLen, 0, addinLen);
1057
1058 TestMemInit();
1059 ASSERT_NE(CRYPT_EAL_RandSeedWithAdin(addin, 0), CRYPT_SUCCESS);
1060
1061 ASSERT_NE(CRYPT_EAL_RandSeedWithAdin(addin, addinLen), CRYPT_SUCCESS);
1062
1063 ASSERT_NE(CRYPT_EAL_RandSeedWithAdin(NULL, addinLen), CRYPT_SUCCESS);
1064
1065 ASSERT_NE(CRYPT_EAL_RandSeedWithAdin(NULL, 0), CRYPT_SUCCESS);
1066
1067 EXIT:
1068 free(addin);
1069 return;
1070 }
1071 /* END_CASE */
1072
1073 /**
1074 * @test SDV_CRYPT_DRBG_SEED_ADIN_ERR_PARA_API_TC001
1075 * @title Test the CRYPT_EAL_DrbgSeedWithAdin interface.
1076 * @precon nan
1077 * @brief
1078 * 1.Initialize the random number seed, expected result 1.
1079 * 2.Call CRYPT_EAL_DrbgSeedWithAdin,use normal parameters, expected result 2.
1080 * 3.Call CRYPT_EAL_DrbgSeedWithAdin,the array length is abnormal, expected result 3.
1081 * @expect
1082 * 1.successful.
1083 * 2.All interface succeeded.
1084 * 3.The interface returns an exception.
1085 */
1086 /* BEGIN_CASE */
SDV_CRYPT_DRBG_SEED_ADIN_ERR_PARA_API_TC001(void)1087 void SDV_CRYPT_DRBG_SEED_ADIN_ERR_PARA_API_TC001(void)
1088 {
1089 uint8_t *addin;
1090 uint32_t addinLen = sizeof(uint8_t) * DRBG_MAX_ADIN_SIZE;
1091 CRYPT_RandSeedMethod seedMeth = { 0 };
1092 CRYPT_Data data = { 0 };
1093 DRBG_Vec_t seedCtx = { 0 };
1094 void *drbg = NULL;
1095
1096 addin = malloc(addinLen);
1097 ASSERT_TRUE(addin != NULL);
1098 memset_s(addin, addinLen, 0, addinLen);
1099
1100 TestMemInit();
1101 regSeedMeth(&seedMeth);
1102 drbgDataInit(&data, TEST_DRBG_DATA_SIZE);
1103
1104 seedCtx.nonce = &data;
1105 seedCtx.entropy = &data;
1106 drbg = CRYPT_EAL_DrbgNew(CRYPT_RAND_SHA256, &seedMeth, &seedCtx);
1107 ASSERT_TRUE(drbg != NULL);
1108 ASSERT_TRUE(CRYPT_EAL_DrbgInstantiate(drbg, NULL, 0) == CRYPT_SUCCESS);
1109
1110 ASSERT_EQ(CRYPT_EAL_DrbgSeedWithAdin(drbg, addin, 0), CRYPT_SUCCESS);
1111 ASSERT_EQ(CRYPT_EAL_DrbgSeedWithAdin(drbg, addin, addinLen), CRYPT_SUCCESS);
1112 ASSERT_EQ(CRYPT_EAL_DrbgSeedWithAdin(drbg, NULL, 0), CRYPT_SUCCESS);
1113
1114 ASSERT_NE(CRYPT_EAL_DrbgSeedWithAdin(NULL, addin, addinLen), CRYPT_SUCCESS);
1115 ASSERT_NE(CRYPT_EAL_DrbgSeedWithAdin(drbg, NULL, addinLen), CRYPT_SUCCESS);
1116
1117 EXIT:
1118 CRYPT_EAL_DrbgDeinit(drbg);
1119 free(addin);
1120 free(data.data);
1121 return;
1122 }
1123 /* END_CASE */
1124
1125 /**
1126 * @test SDV_CRYPT_DRBG_SEED_ADIN_ERR_PARA_API_TC001
1127 * @title Random number generation test.
1128 * @precon nan
1129 * @brief
1130 * 1.Initialize the random number seed, expected result 1.
1131 * 2.Call CRYPT_EAL_RandbytesWithAdin num times, expected result 2.
1132 * @expect
1133 * 1.successful.
1134 * 2.All interface succeeded.
1135 */
1136 /* BEGIN_CASE */
SDV_CRYPT_DRBG_RAND_NUM_FUNC_TC001(int agId,int num,int dataSize)1137 void SDV_CRYPT_DRBG_RAND_NUM_FUNC_TC001(int agId, int num, int dataSize)
1138 {
1139 if (IsRandAlgDisabled(agId)) {
1140 SKIP_TEST();
1141 }
1142 int i;
1143 uint8_t *output = NULL;
1144 CRYPT_RandSeedMethod seedMeth = { 0 };
1145 DRBG_Vec_t seedCtx = { 0 };
1146 CRYPT_Data data = { 0 };
1147
1148 TestMemInit();
1149 regSeedMeth(&seedMeth);
1150 drbgDataInit(&data, dataSize);
1151
1152 seedCtx.entropy = &data;
1153 seedCtx.nonce = &data;
1154 ASSERT_EQ(CRYPT_EAL_RandInit(agId, &seedMeth, (void *)&seedCtx, NULL, 0), CRYPT_SUCCESS);
1155
1156 output = malloc(sizeof(uint8_t) * DRBG_OUTPUT_SIZE);
1157 ASSERT_TRUE(output != NULL);
1158 for (i = 0; i < num; i++) {
1159 ASSERT_EQ(CRYPT_EAL_RandbytesWithAdin(output, sizeof(uint8_t) * DRBG_OUTPUT_SIZE, NULL, 0), CRYPT_SUCCESS);
1160 }
1161
1162 EXIT:
1163 CRYPT_EAL_RandDeinit();
1164 drbgDataFree(&data);
1165 free(output);
1166 return;
1167 }
1168 /* END_CASE */
1169
1170 /**
1171 * @test SDV_CRYPT_DRBG_NUM_FUNC_TC001
1172 * @title Random number generation test.
1173 * @precon nan
1174 * @brief
1175 * 1.Initialize the random number seed, expected result 1.
1176 * 2.Call CRYPT_EAL_DrbgbytesWithAdin num times, expected result 2.
1177 * @expect
1178 * 1.successful.
1179 * 2.All interface succeeded.
1180 */
1181 /* BEGIN_CASE */
SDV_CRYPT_DRBG_NUM_FUNC_TC001(int agId,int num,int dataSize)1182 void SDV_CRYPT_DRBG_NUM_FUNC_TC001(int agId, int num, int dataSize)
1183 {
1184 if (IsRandAlgDisabled(agId)) {
1185 SKIP_TEST();
1186 }
1187 int i;
1188 uint8_t *output = NULL;
1189 CRYPT_RandSeedMethod seedMeth = { 0 };
1190 DRBG_Vec_t seedCtx = { 0 };
1191 CRYPT_Data data = { 0 };
1192 void *drbgCtx = NULL;
1193
1194 TestMemInit();
1195 regSeedMeth(&seedMeth);
1196 drbgDataInit(&data, dataSize);
1197
1198 seedCtx.entropy = &data;
1199 seedCtx.nonce = &data;
1200 drbgCtx = CRYPT_EAL_DrbgNew(agId, &seedMeth, &seedCtx);
1201 ASSERT_TRUE(drbgCtx != NULL);
1202 ASSERT_TRUE(CRYPT_EAL_DrbgInstantiate(drbgCtx, NULL, 0) == CRYPT_SUCCESS);
1203
1204 output = malloc(sizeof(uint8_t) * DRBG_OUTPUT_SIZE);
1205 ASSERT_TRUE(output != NULL);
1206 for (i = 0; i < num; i++) {
1207 ASSERT_EQ(CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, output, sizeof(uint8_t) * DRBG_OUTPUT_SIZE, NULL, 0), CRYPT_SUCCESS);
1208 }
1209
1210 EXIT:
1211 CRYPT_EAL_DrbgDeinit(drbgCtx);
1212 drbgDataFree(&data);
1213 free(output);
1214 return;
1215 }
1216 /* END_CASE */
1217
1218 /**
1219 * @test SDV_CRYPT_DRBG_PTHREAD_FUNC_TC001
1220 * @title DRGB multi-thread function test.
1221 * @precon nan
1222 * @brief
1223 * 1.Initialize the random number seed, expected result 1.
1224 * 2.Create 10 threads for execute CRYPT_EAL_Randbytes, expected result 2.
1225 * @expect
1226 * 1.init successful.
1227 * 2.All threads are executed successfully..
1228 */
1229 /* BEGIN_CASE */
SDV_CRYPT_DRBG_PTHREAD_FUNC_TC001(int agId)1230 void SDV_CRYPT_DRBG_PTHREAD_FUNC_TC001(int agId)
1231 {
1232 CRYPT_Data data = { 0 };
1233 CRYPT_RandSeedMethod seedMeth = { 0 };
1234 DRBG_Vec_t seedCtx = { 0 };
1235
1236 TestMemInit();
1237 RegThreadFunc();
1238 regSeedMeth(&seedMeth);
1239 drbgDataInit(&data, TEST_DRBG_DATA_SIZE);
1240
1241 seedCtx.entropy = &data;
1242 seedCtx.nonce = &data;
1243
1244 ASSERT_EQ(CRYPT_EAL_RandInit(agId, &seedMeth, &seedCtx, NULL, 0), CRYPT_SUCCESS);
1245 for(uint32_t iter = 0; iter < 10; iter++) {
1246 pthread_t thrd;
1247 ASSERT_EQ(pthread_create(&thrd, NULL, (void *)sdvCryptGlobalThreadTest, NULL), 0);
1248 pthread_join(thrd, NULL);
1249 }
1250
1251 EXIT:
1252 CRYPT_EAL_RandDeinit();
1253 drbgDataFree(&data);
1254 return;
1255 }
1256 /* END_CASE */
1257
1258 /**
1259 * @test SDV_CRYPT_DRBG_CLEANENTROPY_FUNC_TC001
1260 * @title Failed to obtain the entropy source test.
1261 * @precon nan
1262 * @brief
1263 * 1.Register the interface that fails to obtain the entropy source, expected result 1.
1264 * 2.Initialize the random number seed, expected result 2.
1265 * @expect
1266 * 1.register successful.
1267 * 2.Failed to initialize the random number seed.
1268 */
1269 /* BEGIN_CASE */
SDV_CRYPT_DRBG_CLEANENTROPY_FUNC_TC001(int agId)1270 void SDV_CRYPT_DRBG_CLEANENTROPY_FUNC_TC001(int agId)
1271 {
1272 CallBackCtl_t seedCtx = { 0 };
1273 CRYPT_RandSeedMethod seedMeth = {
1274 .getEntropy = getEntropyError,
1275 .cleanEntropy = cleanEntropyError,
1276 .getNonce = getNonceError,
1277 .cleanNonce = cleanNonceError,
1278 };
1279 void *drbg = NULL;
1280
1281 TestMemInit();
1282 seedCtx.entropyState = 1;
1283 ASSERT_NE(CRYPT_EAL_RandInit(agId, &seedMeth, (void *)&seedCtx, NULL, 0), CRYPT_SUCCESS);
1284 drbg = CRYPT_EAL_DrbgNew(agId, &seedMeth, (void *)&seedCtx);
1285 ASSERT_TRUE(drbg != NULL);
1286 ASSERT_NE(CRYPT_EAL_DrbgInstantiate(drbg, NULL, 0), CRYPT_SUCCESS);
1287 EXIT:
1288 CRYPT_EAL_DrbgDeinit(drbg);
1289 CRYPT_EAL_RandDeinit();
1290 return;
1291 }
1292 /* END_CASE */
1293
1294 /**
1295 * @test SDV_CRYPT_DRBG_GETENTROPY_FUNC_TC001
1296 * @title Failed to obtain the entropy source test.
1297 * @precon nan
1298 * @brief
1299 * 1.Do not register the entropy source obtaining function., expected result 1.
1300 * 2.Initialize the random number seed, expected result 2.
1301 * @expect
1302 * 1.register successful.
1303 * 2.Failed to initialize the random number seed.
1304 */
1305 /* BEGIN_CASE */
SDV_CRYPT_DRBG_GETENTROPY_FUNC_TC001(int agId)1306 void SDV_CRYPT_DRBG_GETENTROPY_FUNC_TC001(int agId)
1307 {
1308 CallBackCtl_t seedCtx = { 0 };
1309 CRYPT_RandSeedMethod seedMeth = {
1310 .getEntropy = NULL,
1311 .cleanEntropy = cleanEntropyError,
1312 .getNonce = getNonceError,
1313 .cleanNonce = cleanNonceError,
1314 };
1315 void *drbg = NULL;
1316
1317 TestMemInit();
1318 ASSERT_NE(CRYPT_EAL_RandInit(agId, &seedMeth, (void *)&seedCtx, NULL, 0), CRYPT_SUCCESS);
1319 drbg = CRYPT_EAL_DrbgNew(agId, &seedMeth, (void *)&seedCtx);
1320 ASSERT_TRUE(drbg != NULL);
1321 ASSERT_EQ(CRYPT_EAL_DrbgInstantiate(drbg, NULL, 0), CRYPT_SUCCESS);
1322 EXIT:
1323 CRYPT_EAL_DrbgDeinit(drbg);
1324 CRYPT_EAL_RandDeinit();
1325 return;
1326 }
1327 /* END_CASE */
1328
1329 /**
1330 * @test SDV_CRYPT_DRBG_GETENTROPY_FUNC_TC002
1331 * @title To verify that the entropy data is empty and the length is 0 or a non-zero value.
1332 * @precon nan
1333 * @brief
1334 * 1.Registering the callback function, expected result 1.
1335 * 2.The entropy->data is empty and the length is 0,initialize the random number seed, expected result 2.
1336 * 2.The entropy->data is empty and the length not 0,initialize the random number seed, expected result 3.
1337 * @expect
1338 * 1.Register successful.
1339 * 2.Failed to initialize the random number seed.
1340 * 3.Failed to initialize the random number seed.
1341 */
1342 /* BEGIN_CASE */
SDV_CRYPT_DRBG_GETENTROPY_FUNC_TC002(void)1343 void SDV_CRYPT_DRBG_GETENTROPY_FUNC_TC002(void)
1344 {
1345 DRBG_Vec_t seedCtx = { 0 };
1346 CRYPT_RandSeedMethod seedMeth = {
1347 .getEntropy = getEntropyUnCheckPara,
1348 .cleanEntropy = cleanEntropy,
1349 .getNonce = NULL,
1350 .cleanNonce = NULL,
1351 };
1352 void *drbg = NULL;
1353
1354 TestMemInit();
1355 seedCtx.entropy = calloc(1u, sizeof(CRYPT_Data));
1356 ASSERT_NE(CRYPT_EAL_RandInit(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx, NULL, 0), CRYPT_SUCCESS);
1357 CRYPT_EAL_RandDeinit();
1358 drbg = CRYPT_EAL_DrbgNew(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx);
1359 ASSERT_TRUE(drbg != NULL);
1360 ASSERT_NE(CRYPT_EAL_DrbgInstantiate(drbg, NULL, 0), CRYPT_SUCCESS);
1361 CRYPT_EAL_DrbgDeinit(drbg);
1362 seedCtx.entropy->len = 1; // Set the entropy length to 1 verify that the data is empty but the data length is not 0.
1363 ASSERT_NE(CRYPT_EAL_RandInit(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx, NULL, 0), CRYPT_SUCCESS);
1364 drbg = CRYPT_EAL_DrbgNew(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx);
1365 ASSERT_TRUE(drbg != NULL);
1366 ASSERT_NE(CRYPT_EAL_DrbgInstantiate(drbg, NULL, 0), CRYPT_SUCCESS);
1367
1368 EXIT:
1369 CRYPT_EAL_DrbgDeinit(drbg);
1370 CRYPT_EAL_RandDeinit();
1371 DRBG_FREE(seedCtx.entropy);
1372 return;
1373 }
1374 /* END_CASE */
1375
1376 /**
1377 * @test SDV_CRYPT_DRBG_GETNONCE_FUNC_TC001
1378 * @title Test that the nonce data is empty and the length is 0 or a non-zero value.
1379 * @precon nan
1380 * @brief
1381 * 1.Registering the callback function, expected result 1.
1382 * 2.The nonce->data is empty and the length is 0,initialize the random number seed, expected result 2.
1383 * 2.The nonce->data is empty and the length not 0,initialize the random number seed, expected result 3.
1384 * @expect
1385 * 1.Register successful.
1386 * 2.Failed to initialize the random number seed.
1387 * 3.Failed to initialize the random number seed.
1388 */
1389 /* BEGIN_CASE */
SDV_CRYPT_DRBG_GETNONCE_FUNC_TC001(void)1390 void SDV_CRYPT_DRBG_GETNONCE_FUNC_TC001(void)
1391 {
1392 DRBG_Vec_t seedCtx = { 0 };
1393 CRYPT_RandSeedMethod seedMeth = {
1394 .getEntropy = getEntropyError,
1395 .cleanEntropy = cleanEntropyError,
1396 .getNonce = getNonceUnCheckPara,
1397 .cleanNonce = cleanNonceError,
1398 };
1399 void *drbg = NULL;
1400
1401 TestMemInit();
1402 seedCtx.nonce = calloc(1u, sizeof(CRYPT_Data));
1403 ASSERT_NE(CRYPT_EAL_RandInit(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx, NULL, 0), CRYPT_SUCCESS);
1404 CRYPT_EAL_RandDeinit();
1405 drbg = CRYPT_EAL_DrbgNew(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx);
1406 ASSERT_TRUE(drbg != NULL);
1407 ASSERT_NE(CRYPT_EAL_DrbgInstantiate(drbg, NULL, 0), CRYPT_SUCCESS);
1408 CRYPT_EAL_DrbgDeinit(drbg);
1409
1410 seedCtx.nonce->len = 1; // Set the nonce length to 1 verify that the data is empty but the data length is not 0.
1411 ASSERT_NE(CRYPT_EAL_RandInit(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx, NULL, 0), CRYPT_SUCCESS);
1412 drbg = CRYPT_EAL_DrbgNew(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx);
1413 ASSERT_TRUE(drbg != NULL);
1414 ASSERT_NE(CRYPT_EAL_DrbgInstantiate(drbg, NULL, 0), CRYPT_SUCCESS);
1415
1416 EXIT:
1417 CRYPT_EAL_DrbgDeinit(drbg);
1418 CRYPT_EAL_RandDeinit();
1419 DRBG_FREE(seedCtx.nonce);
1420 return;
1421 }
1422 /* END_CASE */
1423
1424 /**
1425 * @test SDV_CRYPT_DRBG_GETNONCE_FUNC_TC002
1426 * @title Failed to obtain nonce during DRBG initialization.
1427 * @precon nan
1428 * @brief
1429 * 1.Registering the interface for failed to obtain the nonce, expected result 1.
1430 * 2.Initializing the DRBG, expected result 2.
1431 * @expect
1432 * 1.Register successful.
1433 * 2.Failed to initialize the random number seed.
1434 */
1435 /* BEGIN_CASE */
SDV_CRYPT_DRBG_GETNONCE_FUNC_TC002(void)1436 void SDV_CRYPT_DRBG_GETNONCE_FUNC_TC002(void)
1437 {
1438 DRBG_Vec_t seedCtx = { 0 };
1439 CRYPT_RandSeedMethod seedMeth = {
1440 .getEntropy = getEntropyError,
1441 .cleanEntropy = cleanEntropyError,
1442 .getNonce = getNonce,
1443 .cleanNonce = cleanNonce,
1444 };
1445 void *drbg = NULL;
1446
1447 TestMemInit();
1448 seedCtx.nonce = calloc(1u, sizeof(CRYPT_Data));
1449 ASSERT_NE(CRYPT_EAL_RandInit(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx, NULL, 0), CRYPT_SUCCESS);
1450
1451 drbg = CRYPT_EAL_DrbgNew(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx);
1452 ASSERT_TRUE(drbg != NULL);
1453 ASSERT_NE(CRYPT_EAL_DrbgInstantiate(drbg, NULL, 0), CRYPT_SUCCESS);
1454
1455 EXIT:
1456 CRYPT_EAL_DrbgDeinit(drbg);
1457 CRYPT_EAL_RandDeinit();
1458 DRBG_FREE(seedCtx.nonce);
1459 return;
1460 }
1461 /* END_CASE */
1462
1463 /**
1464 * @test SDV_CRYPT_DRBG_GETNONCE_FUNC_TC003
1465 * @title Failed to obtain nonce during DRBG instantiation.
1466 * @precon nan
1467 * @brief
1468 * 1.Registering the interface for failed to obtain the nonce, expected result 1.
1469 * 2.Initializing the DRBG, expected result 2.
1470 * @expect
1471 * 1.Register successful.
1472 * 2.Failed to initialize the random number seed.
1473 */
1474 /* BEGIN_CASE */
SDV_CRYPT_DRBG_GETNONCE_FUNC_TC003(void)1475 void SDV_CRYPT_DRBG_GETNONCE_FUNC_TC003(void)
1476 {
1477 CallBackCtl_t seedCtx = { 0 };
1478 CRYPT_RandSeedMethod seedMeth = {
1479 .getNonce = getNonceError,
1480 .cleanNonce = cleanNonceError,
1481 .getEntropy = getEntropyError,
1482 .cleanEntropy = cleanEntropyError,
1483 };
1484 void *drbg = NULL;
1485 TestMemInit();
1486
1487 seedCtx.nonceState = 1;
1488 ASSERT_TRUE(CRYPT_EAL_RandInit(CRYPT_RAND_SHA224, &seedMeth, (void *)&seedCtx, NULL, 0) != CRYPT_SUCCESS);
1489
1490 drbg = CRYPT_EAL_DrbgNew(CRYPT_RAND_SHA224, &seedMeth, (void *)&seedCtx);
1491 ASSERT_TRUE(drbg != NULL);
1492 ASSERT_NE(CRYPT_EAL_DrbgInstantiate(drbg, NULL, 0), CRYPT_SUCCESS);
1493
1494 EXIT:
1495 CRYPT_EAL_DrbgDeinit(drbg);
1496 CRYPT_EAL_RandDeinit();
1497 return;
1498 }
1499 /* END_CASE */
1500
1501 /**
1502 * @test SDV_CRYPT_DRBG_INSTANTIATE_FUNC_TC001
1503 * @title The personal data provided during DRBG instantiation is empty.
1504 * @precon nan
1505 * @brief
1506 * 1.set the personal data is empty.
1507 * 2.Initializing the DRBG, expected result 1.
1508 * 3.Uninitializing the DRBG, expected result 2.
1509 * @expect
1510 * 1.The DRBG is successfully initialized regardless of whether the data field is empty.
1511 * 2.The DRBG is successfully uninitialized.
1512 */
1513 /* BEGIN_CASE */
SDV_CRYPT_DRBG_INSTANTIATE_FUNC_TC001(void)1514 void SDV_CRYPT_DRBG_INSTANTIATE_FUNC_TC001(void)
1515 {
1516 CRYPT_Data *pers = NULL;
1517 DRBG_Vec_t seedCtx = { 0 };
1518 CRYPT_RandSeedMethod seedMeth = {
1519 .getEntropy = getEntropyError,
1520 .cleanEntropy = cleanEntropyError,
1521 .getNonce = getNonceError,
1522 .cleanNonce = cleanNonceError,
1523 };
1524
1525 TestMemInit();
1526 pers = calloc(1u, sizeof(CRYPT_Data));
1527
1528 ASSERT_EQ(CRYPT_EAL_RandInit(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx, pers->data, pers->len),
1529 CRYPT_SUCCESS);
1530 CRYPT_EAL_RandDeinit();
1531 void *drbg = CRYPT_EAL_DrbgNew(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx);
1532 ASSERT_TRUE(drbg != NULL);
1533 ASSERT_EQ(CRYPT_EAL_DrbgInstantiate(drbg, pers->data, pers->len), CRYPT_SUCCESS);
1534 CRYPT_EAL_DrbgDeinit(drbg);
1535 drbg = NULL;
1536 pers->data = calloc(DRBG_MAX_ADIN_SIZE + 1, sizeof(uint8_t));
1537 pers->len = DRBG_MAX_ADIN_SIZE + 1;
1538 ASSERT_EQ(CRYPT_EAL_RandInit(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx, pers->data, pers->len),
1539 CRYPT_SUCCESS);
1540 drbg = CRYPT_EAL_DrbgNew(CRYPT_RAND_SHA256, &seedMeth, (void *)&seedCtx);
1541 ASSERT_TRUE(drbg != NULL);
1542 ASSERT_EQ(CRYPT_EAL_DrbgInstantiate(drbg, pers->data, pers->len), CRYPT_SUCCESS);
1543
1544 EXIT:
1545 CRYPT_EAL_DrbgDeinit(drbg);
1546 CRYPT_EAL_RandDeinit();
1547 DRBG_FREE(pers->data);
1548 DRBG_FREE(pers);
1549 return;
1550 }
1551 /* END_CASE */
1552
1553 /**
1554 * @test SDV_CRYPT_DRBG_DUP_API_TC001
1555 * @title Test the DRBG dup interface.
1556 * @precon nan
1557 * @brief
1558 * 1.Call DRBG_NewHashCtx create ctx, expected result 1.
1559 * 2.Call dup function,give an empty input parameter, expected result 2.
1560 * 3.Call dup function,give the correct DRBG context, expected result 3.
1561 * @expect
1562 * 1.successful.
1563 * 2.The interface returns a null pointer.
1564 * 3.The interface returns new ctx.
1565 */
1566 /* BEGIN_CASE */
SDV_CRYPT_DRBG_DUP_API_TC001(int algId)1567 void SDV_CRYPT_DRBG_DUP_API_TC001(int algId)
1568 {
1569 CRYPT_RandSeedMethod seedMeth = { 0 };
1570 CRYPT_Data data = { 0 };
1571 DRBG_Vec_t seedCtx = { 0 };
1572
1573 TestMemInit();
1574 regSeedMeth(&seedMeth);
1575 drbgDataInit(&data, TEST_DRBG_DATA_SIZE);
1576
1577 seedCtx.nonce = &data;
1578 seedCtx.entropy = &data;
1579 CRYPT_EAL_RndCtx *drbg = CRYPT_EAL_DrbgNew(algId, &seedMeth, &seedCtx);
1580 ASSERT_TRUE(drbg != NULL);
1581 DRBG_Ctx *ctx = (DRBG_Ctx*)(drbg->ctx);
1582 DRBG_Ctx *newCtx = ctx->meth->dup(ctx);
1583 ASSERT_TRUE(newCtx != NULL);
1584 ASSERT_TRUE(ctx->meth->dup(NULL) == NULL);
1585
1586 EXIT:
1587 CRYPT_EAL_DrbgDeinit(drbg);
1588 DRBG_Free(newCtx);
1589 drbgDataFree(&data);
1590 }
1591 /* END_CASE */
1592
1593 /**
1594 * @test SDV_CRYPT_DRBG_PTHREAD_FUNC_TC002
1595 * @title DRGB multi-thread function test.
1596 * @precon nan
1597 * @brief
1598 * 1.Initialize 10 drbgCtx, expected result 1.
1599 * 2.Create 10 threads for execute CRYPT_EAL_DrbgbytesWithAdin, expected result 2.
1600 * @expect
1601 * 1.init successful.
1602 * 2.All threads are executed successfully..
1603 */
1604 /* BEGIN_CASE */
SDV_CRYPT_DRBG_PTHREAD_FUNC_TC002(int agId)1605 void SDV_CRYPT_DRBG_PTHREAD_FUNC_TC002(int agId)
1606 {
1607 if (IsRandAlgDisabled(agId)) {
1608 SKIP_TEST();
1609 }
1610 CRYPT_Data data = { 0 };
1611 CRYPT_RandSeedMethod seedMeth = { 0 };
1612 DRBG_Vec_t seedCtx = { 0 };
1613
1614 TestMemInit();
1615 RegThreadFunc();
1616 regSeedMeth(&seedMeth);
1617 drbgDataInit(&data, TEST_DRBG_DATA_SIZE);
1618
1619 seedCtx.entropy = &data;
1620 seedCtx.nonce = &data;
1621 for (uint32_t iter = 0; iter < 10; iter++) {
1622 pthread_t thrd;
1623 void *drbgCtx = CRYPT_EAL_DrbgNew(agId, &seedMeth, &seedCtx);
1624 ASSERT_TRUE(drbgCtx != NULL);
1625 ASSERT_TRUE(CRYPT_EAL_DrbgInstantiate(drbgCtx, NULL, 0) == CRYPT_SUCCESS);
1626 ASSERT_EQ(pthread_create(&thrd, NULL, (void *)sdvCryptEalThreadTest, drbgCtx), 0);
1627 pthread_join(thrd, NULL);
1628 CRYPT_EAL_DrbgDeinit(drbgCtx);
1629 drbgCtx = NULL;
1630 }
1631 EXIT:
1632 drbgDataFree(&data);
1633 return;
1634 }
1635 /* END_CASE */
1636
1637 /**
1638 * @test SDV_CRYPT_DRBG_PTHREAD_FUNC_TC003
1639 * @title DRGB multi-thread function test.
1640 * @precon nan
1641 * @brief
1642 * 1.Initialize drbgCtx, expected result 1.
1643 * 2.Create 10 threads for execute CRYPT_EAL_DrbgbytesWithAdin, expected result 2.
1644 * @expect
1645 * 1.init successful.
1646 * 2.All threads are executed successfully..
1647 */
1648 /* BEGIN_CASE */
SDV_CRYPT_DRBG_PTHREAD_FUNC_TC003(int agId)1649 void SDV_CRYPT_DRBG_PTHREAD_FUNC_TC003(int agId)
1650 {
1651 CRYPT_Data data = { 0 };
1652 CRYPT_RandSeedMethod seedMeth = { 0 };
1653 DRBG_Vec_t seedCtx = { 0 };
1654
1655 TestMemInit();
1656 RegThreadFunc();
1657 regSeedMeth(&seedMeth);
1658 drbgDataInit(&data, TEST_DRBG_DATA_SIZE);
1659
1660 seedCtx.entropy = &data;
1661 seedCtx.nonce = &data;
1662 void *drbgCtx = CRYPT_EAL_DrbgNew(agId, &seedMeth, &seedCtx);
1663 ASSERT_TRUE(drbgCtx != NULL);
1664 ASSERT_TRUE(CRYPT_EAL_DrbgInstantiate(drbgCtx, NULL, 0) == CRYPT_SUCCESS);
1665 for (uint32_t iter = 0; iter < 10; iter++) {
1666 pthread_t thrd;
1667 ASSERT_EQ(pthread_create(&thrd, NULL, (void *)sdvCryptEalThreadTest, drbgCtx), 0);
1668 pthread_join(thrd, NULL);
1669 }
1670 EXIT:
1671 CRYPT_EAL_DrbgDeinit(drbgCtx);
1672 drbgDataFree(&data);
1673 return;
1674 }
1675 /* END_CASE */
1676
getEntropyWithoutSeedCtx(void * ctx,CRYPT_Data * entropy,uint32_t strength,CRYPT_Range * lenRange)1677 static int32_t getEntropyWithoutSeedCtx(void *ctx, CRYPT_Data *entropy, uint32_t strength, CRYPT_Range *lenRange)
1678 {
1679 (void)ctx;
1680 (void)strength;
1681 (void)lenRange;
1682 uint32_t entroyLen = sizeof(uint8_t) * TEST_DRBG_DATA_SIZE;
1683 entropy->data = calloc(1u, entroyLen);
1684 entropy->len = entroyLen;
1685 return CRYPT_SUCCESS;
1686 }
1687
getEntropyWithoutSeedCtxSpecial(void * ctx,CRYPT_Data * entropy,uint32_t strength,CRYPT_Range * lenRange)1688 static int32_t getEntropyWithoutSeedCtxSpecial(void *ctx, CRYPT_Data *entropy, uint32_t strength, CRYPT_Range *lenRange)
1689 {
1690 (void)ctx;
1691 (void)strength;
1692 (void)lenRange;
1693 uint32_t entroyLen = sizeof(uint8_t) * CTR_AES128_SEEDLEN;
1694 entropy->data = calloc(1u, entroyLen);
1695 entropy->len = entroyLen;
1696 return CRYPT_SUCCESS;
1697 }
1698
getNonceWithoutSeedCtx(void * ctx,CRYPT_Data * nonce,uint32_t strength,CRYPT_Range * lenRange)1699 static int32_t getNonceWithoutSeedCtx(void *ctx, CRYPT_Data *nonce, uint32_t strength, CRYPT_Range *lenRange)
1700 {
1701 (void)ctx;
1702 (void)strength;
1703 (void)lenRange;
1704 uint32_t nonceLen = sizeof(uint8_t) * TEST_DRBG_DATA_SIZE;
1705 nonce->data = calloc(1u, nonceLen);
1706 nonce->len = nonceLen;
1707 return CRYPT_SUCCESS;
1708 }
1709
1710 /**
1711 * @test SDV_CRYPT_EAL_RAND_BYTES_FUNC_TC001
1712 * @title Generating random numbers based on entropy sources.
1713 * @precon nan
1714 * @brief
1715 * 1.Initialize the random number seed, expected result 1.
1716 * 2.Call CRYPT_EAL_RandbytesWithAdin, expected result 2.
1717 * 3.Call CRYPT_EAL_Randbytes get random numbers, expected result 3.
1718 * @expect
1719 * 1.init successful.
1720 * 2.successful.
1721 * 3.Random number generated successfully.
1722 */
1723 /* BEGIN_CASE */
SDV_CRYPT_EAL_RAND_BYTES_FUNC_TC001(int id,Hex * entropy,Hex * nonce,Hex * pers,Hex * addin1,Hex * entropyPR1,Hex * addin2,Hex * entropyPR2,Hex * retBits)1724 void SDV_CRYPT_EAL_RAND_BYTES_FUNC_TC001(int id, Hex *entropy, Hex *nonce, Hex *pers, Hex *addin1, Hex *entropyPR1,
1725 Hex *addin2, Hex *entropyPR2, Hex *retBits)
1726 {
1727 if (IsRandAlgDisabled(id)){
1728 SKIP_TEST();
1729 }
1730 uint8_t output[DRBG_MAX_OUTPUT_SIZE];
1731 CRYPT_RandSeedMethod seedMeth = { 0 };
1732 DRBG_Vec_t *seedCtx;
1733 regSeedMeth(&seedMeth);
1734
1735 TestMemInit();
1736
1737 seedCtx = seedCtxMem();
1738 ASSERT_TRUE(seedCtx != NULL);
1739 seedCtxCfg(seedCtx, entropy, nonce, pers, addin1, entropyPR1, addin2, entropyPR2, retBits);
1740
1741 ASSERT_EQ(CRYPT_EAL_RandInit((CRYPT_RAND_AlgId)id, &seedMeth, (void *)seedCtx, seedCtx->pers->data,
1742 seedCtx->pers->len), CRYPT_SUCCESS);
1743
1744 ASSERT_EQ(CRYPT_EAL_RandbytesWithAdin(output, sizeof(uint8_t) * retBits->len, addin1->x, addin1->len),
1745 CRYPT_SUCCESS);
1746
1747 ASSERT_EQ(CRYPT_EAL_Randbytes(output, sizeof(uint8_t) * retBits->len), CRYPT_SUCCESS);
1748 ASSERT_EQ(CRYPT_EAL_RandIsValidAlgId(id), true);
1749 EXIT:
1750 CRYPT_EAL_RandDeinit();
1751 seedCtxFree(seedCtx);
1752 return;
1753 }
1754 /* END_CASE */
1755
1756 /**
1757 * @test SDV_CRYPT_EAL_DRBG_BYTES_FUNC_TC001
1758 * @title Generating random numbers based on entropy sources.
1759 * @precon nan
1760 * @brief
1761 * 1.Initialize the random number seed, expected result 1.
1762 * 2.Call CRYPT_EAL_DrbgbytesWithAdin, expected result 2.
1763 * 3.Call CRYPT_EAL_Drbgbytes get random numbers, expected result 3.
1764 * @expect
1765 * 1.Init successful.
1766 * 2.Successful.
1767 * 3.Random number generated successfully.
1768 */
1769 /* BEGIN_CASE */
SDV_CRYPT_EAL_DRBG_BYTES_FUNC_TC001(int id,Hex * entropy,Hex * nonce,Hex * pers,Hex * addin1,Hex * entropyPR1,Hex * addin2,Hex * entropyPR2,Hex * retBits)1770 void SDV_CRYPT_EAL_DRBG_BYTES_FUNC_TC001(int id, Hex *entropy, Hex *nonce, Hex *pers, Hex *addin1, Hex *entropyPR1,
1771 Hex *addin2, Hex *entropyPR2, Hex *retBits)
1772 {
1773 if (IsRandAlgDisabled(id)){
1774 SKIP_TEST();
1775 }
1776 uint8_t *output = NULL;
1777 CRYPT_RandSeedMethod seedMeth = { 0 };
1778 DRBG_Vec_t *seedCtx;
1779 void *drbgCtx = NULL;
1780 regSeedMeth(&seedMeth);
1781
1782 TestMemInit();
1783
1784 seedCtx = seedCtxMem();
1785 ASSERT_TRUE(seedCtx != NULL);
1786 seedCtxCfg(seedCtx, entropy, nonce, pers, addin1, entropyPR1, addin2, entropyPR2, retBits);
1787 drbgCtx = CRYPT_EAL_DrbgNew(id, &seedMeth, seedCtx);
1788 ASSERT_TRUE(drbgCtx != NULL);
1789 ASSERT_TRUE(CRYPT_EAL_DrbgInstantiate(drbgCtx, NULL, 0) == CRYPT_SUCCESS);
1790
1791 output = malloc(sizeof(uint8_t) * retBits->len);
1792 ASSERT_TRUE(output != NULL);
1793
1794 ASSERT_EQ(CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, output, sizeof(uint8_t) * retBits->len, addin1->x, addin1->len),
1795 CRYPT_SUCCESS);
1796
1797 ASSERT_EQ(CRYPT_EAL_Drbgbytes(drbgCtx, output, sizeof(uint8_t) * retBits->len), CRYPT_SUCCESS);
1798
1799 EXIT:
1800 CRYPT_EAL_DrbgDeinit(drbgCtx);
1801 seedCtxFree(seedCtx);
1802 free(output);
1803 return;
1804 }
1805 /* END_CASE */
1806
1807 /**
1808 * @test SDV_CRYPT_EAL_RAND_BYTES_FUNC_TC002
1809 * @title Generating random numbers based on entropy sources,the user only provides the seed method,
1810 not the seed context.
1811 * @precon nan
1812 * @brief
1813 * 1.Initialize the random number seed, expected result 1.
1814 * 2.Call CRYPT_EAL_RandSeed, expected result 2.
1815 * 3.Call CRYPT_EAL_Randbytes get random numbers, expected result 3.
1816 * @expect
1817 * 1.init successful.
1818 * 2.successful.
1819 * 3.Random number generated successfully.
1820 */
1821 /* BEGIN_CASE */
SDV_CRYPT_EAL_RAND_BYTES_FUNC_TC002(int id)1822 void SDV_CRYPT_EAL_RAND_BYTES_FUNC_TC002(int id)
1823 {
1824 if (IsRandAlgDisabled(id)){
1825 SKIP_TEST();
1826 }
1827 uint8_t output[DRBG_MAX_OUTPUT_SIZE];
1828 CRYPT_RandSeedMethod seedMeth = {
1829 .getEntropy = getEntropyWithoutSeedCtx,
1830 .cleanEntropy = cleanEntropyError,
1831 .getNonce = getNonceWithoutSeedCtx,
1832 .cleanNonce = cleanNonceError,
1833 };
1834
1835 TestMemInit();
1836
1837 /* The DRBG-CTR mode requires the entropy source length of a specific length, and seedMeth needs to generate
1838 entropy of the corresponding length.(DRBG-CTR AES128/AES192/AES256 length is 32, 40, 48).
1839 */
1840 if (id == CRYPT_RAND_AES128_CTR || id == CRYPT_RAND_AES192_CTR || id == CRYPT_RAND_AES256_CTR) {
1841 seedMeth.getEntropy = getEntropyWithoutSeedCtxSpecial;
1842 seedMeth.getNonce = NULL;
1843 seedMeth.cleanNonce = NULL;
1844 }
1845 ASSERT_EQ(CRYPT_EAL_RandInit((CRYPT_RAND_AlgId)id, &seedMeth, NULL, NULL, 0), CRYPT_SUCCESS);
1846 ASSERT_EQ(CRYPT_EAL_RandSeed(), CRYPT_SUCCESS);
1847 ASSERT_EQ(CRYPT_EAL_Randbytes(output, DRBG_MAX_OUTPUT_SIZE), CRYPT_SUCCESS);
1848
1849 EXIT:
1850 CRYPT_EAL_RandDeinit();
1851 return;
1852 }
1853 /* END_CASE */
1854
1855 /**
1856 * @test SDV_CRYPT_EAL_RAND_DEFAULT_PROVIDER_BYTES_FUNC_TC001
1857 * @title Default provider testing
1858 * @precon nan
1859 * @brief
1860 * Load the default provider and use the test vector to test its correctness
1861 * Generating random numbers based on entropy sources
1862 * 1.Initialize the random number seed, expected result 1.
1863 * 2.Call CRYPT_EAL_RandbytesWithAdin, expected result 2.
1864 * 3.Call CRYPT_EAL_Randbytes get random numbers, expected result 3.
1865 * @expect
1866 * 1.init successful.
1867 * 2.successful.
1868 * 3.Random number generated successfully.
1869 */
1870 /* BEGIN_CASE */
SDV_CRYPT_EAL_RAND_DEFAULT_PROVIDER_BYTES_FUNC_TC001(int id,Hex * entropy,Hex * nonce,Hex * pers,Hex * addin1,Hex * entropyPR1,Hex * addin2,Hex * entropyPR2,Hex * retBits)1871 void SDV_CRYPT_EAL_RAND_DEFAULT_PROVIDER_BYTES_FUNC_TC001(int id, Hex *entropy, Hex *nonce, Hex *pers,
1872 Hex *addin1, Hex *entropyPR1, Hex *addin2, Hex *entropyPR2, Hex *retBits)
1873 {
1874 #ifndef HITLS_CRYPTO_PROVIDER
1875 (void)id;
1876 (void)entropy;
1877 (void)nonce;
1878 (void)pers;
1879 (void)addin1;
1880 (void)entropyPR1;
1881 (void)addin2;
1882 (void)entropyPR2;
1883 (void)retBits;
1884 SKIP_TEST();
1885 #else
1886 if (IsRandAlgDisabled(id)) {
1887 SKIP_TEST();
1888 }
1889 uint8_t output[DRBG_MAX_OUTPUT_SIZE];
1890 CRYPT_RandSeedMethod seedMeth = { 0 };
1891 DRBG_Vec_t *seedCtx;
1892 regSeedMeth(&seedMeth);
1893
1894 TestMemInit();
1895
1896 seedCtx = seedCtxMem();
1897 ASSERT_TRUE(seedCtx != NULL);
1898 seedCtxCfg(seedCtx, entropy, nonce, pers, addin1, entropyPR1, addin2, entropyPR2, retBits);
1899
1900 BSL_Param param[6] = {0};
1901 ASSERT_EQ(BSL_PARAM_InitValue(¶m[0],
1902 CRYPT_PARAM_RAND_SEEDCTX, BSL_PARAM_TYPE_CTX_PTR, seedCtx, 0), BSL_SUCCESS);
1903 ASSERT_EQ(BSL_PARAM_InitValue(¶m[1],
1904 CRYPT_PARAM_RAND_SEED_GETENTROPY, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.getEntropy, 0), BSL_SUCCESS);
1905 ASSERT_EQ(BSL_PARAM_InitValue(¶m[2],
1906 CRYPT_PARAM_RAND_SEED_CLEANENTROPY, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.cleanEntropy, 0), BSL_SUCCESS);
1907 ASSERT_EQ(BSL_PARAM_InitValue(¶m[3],
1908 CRYPT_PARAM_RAND_SEED_GETNONCE, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.getNonce, 0), BSL_SUCCESS);
1909 ASSERT_EQ(BSL_PARAM_InitValue(¶m[4],
1910 CRYPT_PARAM_RAND_SEED_CLEANNONCE, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.cleanNonce, 0), BSL_SUCCESS);
1911
1912 ASSERT_EQ(CRYPT_EAL_ProviderRandInitCtx(NULL, (CRYPT_RAND_AlgId)id, "provider=default",
1913 seedCtx->pers->data, seedCtx->pers->len, param), CRYPT_SUCCESS);
1914 ASSERT_EQ(CRYPT_EAL_RandbytesWithAdinEx(NULL, output, sizeof(uint8_t) * retBits->len, addin1->x, addin1->len),
1915 CRYPT_SUCCESS);
1916
1917 ASSERT_EQ(CRYPT_EAL_RandbytesEx(NULL, output, sizeof(uint8_t) * retBits->len), CRYPT_SUCCESS);
1918 ASSERT_EQ(CRYPT_EAL_RandIsValidAlgId(id), true);
1919 EXIT:
1920 CRYPT_EAL_RandDeinitEx(NULL);
1921 seedCtxFree(seedCtx);
1922 return;
1923 #endif
1924 }
1925 /* END_CASE */
1926
1927 /**
1928 * @test SDV_CRYPT_EAL_DRBG_DEFAULT_PROVIDER_BYTES_FUNC_TC001
1929 * @title Default provider testing
1930 * @precon nan
1931 * @brief
1932 * Load the default provider and use the test vector to test its correctness
1933 * Generating random numbers based on entropy sources.
1934 * 1.Initialize the random number seed, expected result 1.
1935 * 2.Call CRYPT_EAL_DrbgbytesWithAdin, expected result 2.
1936 * 3.Call CRYPT_EAL_Drbgbytes get random numbers, expected result 3.
1937 * @expect
1938 * 1.Init successful.
1939 * 2.Successful.
1940 * 3.Random number generated successfully.
1941 */
1942 /* BEGIN_CASE */
SDV_CRYPT_EAL_DRBG_DEFAULT_PROVIDER_BYTES_FUNC_TC001(int id,Hex * entropy,Hex * nonce,Hex * pers,Hex * addin1,Hex * entropyPR1,Hex * addin2,Hex * entropyPR2,Hex * retBits)1943 void SDV_CRYPT_EAL_DRBG_DEFAULT_PROVIDER_BYTES_FUNC_TC001(int id, Hex *entropy, Hex *nonce, Hex *pers,
1944 Hex *addin1, Hex *entropyPR1, Hex *addin2, Hex *entropyPR2, Hex *retBits)
1945 {
1946 #ifndef HITLS_CRYPTO_PROVIDER
1947 (void)id;
1948 (void)entropy;
1949 (void)nonce;
1950 (void)pers;
1951 (void)addin1;
1952 (void)entropyPR1;
1953 (void)addin2;
1954 (void)entropyPR2;
1955 (void)retBits;
1956 SKIP_TEST();
1957 #else
1958 if (IsRandAlgDisabled(id)) {
1959 SKIP_TEST();
1960 }
1961 uint8_t *output = NULL;
1962 CRYPT_RandSeedMethod seedMeth = { 0 };
1963 DRBG_Vec_t *seedCtx;
1964 void *drbgCtx = NULL;
1965 regSeedMeth(&seedMeth);
1966
1967 TestMemInit();
1968
1969 seedCtx = seedCtxMem();
1970 ASSERT_TRUE(seedCtx != NULL);
1971 seedCtxCfg(seedCtx, entropy, nonce, pers, addin1, entropyPR1, addin2, entropyPR2, retBits);
1972
1973 BSL_Param param[6] = {0};
1974 ASSERT_EQ(BSL_PARAM_InitValue(¶m[0],
1975 CRYPT_PARAM_RAND_SEEDCTX, BSL_PARAM_TYPE_CTX_PTR, seedCtx, 0), BSL_SUCCESS);
1976 ASSERT_EQ(BSL_PARAM_InitValue(¶m[1],
1977 CRYPT_PARAM_RAND_SEED_GETENTROPY, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.getEntropy, 0), BSL_SUCCESS);
1978 ASSERT_EQ(BSL_PARAM_InitValue(¶m[2],
1979 CRYPT_PARAM_RAND_SEED_CLEANENTROPY, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.cleanEntropy, 0), BSL_SUCCESS);
1980 ASSERT_EQ(BSL_PARAM_InitValue(¶m[3],
1981 CRYPT_PARAM_RAND_SEED_GETNONCE, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.getNonce, 0), BSL_SUCCESS);
1982 ASSERT_EQ(BSL_PARAM_InitValue(¶m[4],
1983 CRYPT_PARAM_RAND_SEED_CLEANNONCE, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.cleanNonce, 0), BSL_SUCCESS);
1984
1985 drbgCtx = CRYPT_EAL_ProviderDrbgNewCtx(NULL, (CRYPT_RAND_AlgId)id, "provider=default", param);
1986 ASSERT_TRUE(drbgCtx != NULL);
1987 ASSERT_TRUE(CRYPT_EAL_DrbgInstantiate(drbgCtx, NULL, 0) == CRYPT_SUCCESS);
1988
1989 output = malloc(sizeof(uint8_t) * retBits->len);
1990 ASSERT_TRUE(output != NULL);
1991
1992 ASSERT_EQ(CRYPT_EAL_DrbgbytesWithAdin(drbgCtx, output, sizeof(uint8_t) * retBits->len, addin1->x, addin1->len),
1993 CRYPT_SUCCESS);
1994 ASSERT_EQ(CRYPT_EAL_Drbgbytes(drbgCtx, output, sizeof(uint8_t) * retBits->len), CRYPT_SUCCESS);
1995
1996 EXIT:
1997 CRYPT_EAL_DrbgDeinit(drbgCtx);
1998 seedCtxFree(seedCtx);
1999 free(output);
2000 return;
2001 #endif
2002 }
2003 /* END_CASE */
2004
2005 /**
2006 * @test SDV_CRYPT_EAL_RAND_DEFAULT_PROVIDER_BYTES_FUNC_TC002
2007 * @title Default provider testing
2008 * @precon nan
2009 * @brief
2010 * Load the default provider and use the test vector to test its correctness
2011 * Generating random numbers based on entropy sources,the user only provides the seed method,
2012 * not the seed context.
2013 * 1.Initialize the random number seed, expected result 1.
2014 * 2.Call CRYPT_EAL_RandSeed, expected result 2.
2015 * 3.Call CRYPT_EAL_Randbytes get random numbers, expected result 3.
2016 * 4.Initialize the random number without seedMeth, expected result 4.
2017 * @expect
2018 * 1.init successful.
2019 * 2.successful.
2020 * 3.Random number generated successfully.
2021 * 4.init successful.
2022 */
2023 /* BEGIN_CASE */
SDV_CRYPT_EAL_RAND_DEFAULT_PROVIDER_BYTES_FUNC_TC002(int id)2024 void SDV_CRYPT_EAL_RAND_DEFAULT_PROVIDER_BYTES_FUNC_TC002(int id)
2025 {
2026 #ifndef HITLS_CRYPTO_PROVIDER
2027 (void)id;
2028 SKIP_TEST();
2029 #else
2030 if (IsRandAlgDisabled(id)) {
2031 SKIP_TEST();
2032 }
2033 uint8_t output[DRBG_MAX_OUTPUT_SIZE];
2034 CRYPT_RandSeedMethod seedMeth = {
2035 .getEntropy = getEntropyWithoutSeedCtx,
2036 .cleanEntropy = cleanEntropyError,
2037 .getNonce = getNonceWithoutSeedCtx,
2038 .cleanNonce = cleanNonceError,
2039 };
2040
2041 TestMemInit();
2042
2043 /* The DRBG-CTR mode requires the entropy source length of a specific length, and seedMeth needs to generate
2044 entropy of the corresponding length.(DRBG-CTR AES128/AES192/AES256 length is 32, 40, 48).
2045 */
2046 if (id == CRYPT_RAND_AES128_CTR || id == CRYPT_RAND_AES192_CTR || id == CRYPT_RAND_AES256_CTR) {
2047 seedMeth.getEntropy = getEntropyWithoutSeedCtxSpecial;
2048 seedMeth.getNonce = NULL;
2049 seedMeth.cleanNonce = NULL;
2050 }
2051 BSL_Param param[6] = {0};
2052 ASSERT_EQ(BSL_PARAM_InitValue(¶m[0],
2053 CRYPT_PARAM_RAND_SEEDCTX, BSL_PARAM_TYPE_CTX_PTR, NULL, 0), BSL_SUCCESS);
2054 ASSERT_EQ(BSL_PARAM_InitValue(¶m[1],
2055 CRYPT_PARAM_RAND_SEED_GETENTROPY, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.getEntropy, 0), BSL_SUCCESS);
2056 ASSERT_EQ(BSL_PARAM_InitValue(¶m[2],
2057 CRYPT_PARAM_RAND_SEED_CLEANENTROPY, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.cleanEntropy, 0), BSL_SUCCESS);
2058 ASSERT_EQ(BSL_PARAM_InitValue(¶m[3],
2059 CRYPT_PARAM_RAND_SEED_GETNONCE, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.getNonce, 0), BSL_SUCCESS);
2060 ASSERT_EQ(BSL_PARAM_InitValue(¶m[4],
2061 CRYPT_PARAM_RAND_SEED_CLEANNONCE, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.cleanNonce, 0), BSL_SUCCESS);
2062
2063 ASSERT_EQ(CRYPT_EAL_ProviderRandInitCtx(NULL, (CRYPT_RAND_AlgId)id, "provider=default", NULL, 0, param), CRYPT_SUCCESS);
2064 ASSERT_EQ(CRYPT_EAL_RandSeedEx(NULL), CRYPT_SUCCESS);
2065 ASSERT_EQ(CRYPT_EAL_RandbytesEx(NULL, output, DRBG_MAX_OUTPUT_SIZE), CRYPT_SUCCESS);
2066 CRYPT_EAL_DrbgDeinit(CRYPT_EAL_GetGlobalLibCtx()->drbg);
2067 CRYPT_EAL_GetGlobalLibCtx()->drbg = NULL;
2068 param[1] = (BSL_Param){0, 0, NULL, 0, 0};
2069 ASSERT_EQ(CRYPT_EAL_ProviderRandInitCtx(NULL, (CRYPT_RAND_AlgId)id, "provider=default", NULL, 0, param), CRYPT_SUCCESS);
2070 ASSERT_EQ(CRYPT_EAL_RandSeedEx(NULL), CRYPT_SUCCESS);
2071 ASSERT_EQ(CRYPT_EAL_RandbytesEx(NULL, output, DRBG_MAX_OUTPUT_SIZE), CRYPT_SUCCESS);
2072
2073 EXIT:
2074 CRYPT_EAL_RandDeinitEx(NULL);
2075 return;
2076 #endif
2077 }
2078 /* END_CASE */
2079
2080 /* BEGIN_CASE */
SDV_CRYPT_EAL_RAND_DEFAULT_PROVIDER_BYTES_FUNC_TC003(int id)2081 void SDV_CRYPT_EAL_RAND_DEFAULT_PROVIDER_BYTES_FUNC_TC003(int id)
2082 {
2083 #ifndef HITLS_CRYPTO_PROVIDER
2084 (void)id;
2085 SKIP_TEST();
2086 #else
2087 if (IsRandAlgDisabled(id)) {
2088 SKIP_TEST();
2089 }
2090 uint8_t output[DRBG_MAX_OUTPUT_SIZE];
2091 CRYPT_RandSeedMethod seedMeth = {
2092 .getEntropy = getEntropyWithoutSeedCtx,
2093 .cleanEntropy = cleanEntropyError,
2094 .getNonce = getNonceWithoutSeedCtx,
2095 .cleanNonce = cleanNonceError,
2096 };
2097 TestMemInit();
2098
2099 BSL_Param param[6] = {0};
2100 ASSERT_EQ(BSL_PARAM_InitValue(¶m[0],
2101 CRYPT_PARAM_RAND_SEED_GETNONCE, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.getNonce, 0), BSL_SUCCESS);
2102 ASSERT_EQ(BSL_PARAM_InitValue(¶m[1],
2103 CRYPT_PARAM_RAND_SEED_CLEANNONCE, BSL_PARAM_TYPE_FUNC_PTR, seedMeth.cleanNonce, 0), BSL_SUCCESS);
2104 ASSERT_EQ(BSL_PARAM_InitValue(¶m[2],
2105 CRYPT_PARAM_RAND_SEEDCTX, BSL_PARAM_TYPE_CTX_PTR, NULL, 0), BSL_SUCCESS);
2106
2107 ASSERT_EQ(CRYPT_EAL_ProviderRandInitCtx(NULL, (CRYPT_RAND_AlgId)id, "provider=default", NULL, 0, NULL),
2108 CRYPT_SUCCESS);
2109 ASSERT_EQ(CRYPT_EAL_RandSeedEx(NULL), CRYPT_SUCCESS);
2110 ASSERT_EQ(CRYPT_EAL_RandbytesEx(NULL, output, DRBG_MAX_OUTPUT_SIZE), CRYPT_SUCCESS);
2111 CRYPT_EAL_DrbgDeinit(CRYPT_EAL_GetGlobalLibCtx()->drbg);
2112 CRYPT_EAL_GetGlobalLibCtx()->drbg = NULL;
2113 ASSERT_EQ(CRYPT_EAL_ProviderRandInitCtx(NULL, (CRYPT_RAND_AlgId)id, "provider=default", NULL, 0, param),
2114 CRYPT_EAL_ERR_DRBG_INIT_FAIL);
2115 param[2] = (BSL_Param){0, 0, NULL, 0, 0};
2116 ASSERT_EQ(CRYPT_EAL_ProviderRandInitCtx(NULL, (CRYPT_RAND_AlgId)id, "provider=default", NULL, 0, param),
2117 CRYPT_EAL_ERR_DRBG_INIT_FAIL);
2118 EXIT:
2119 CRYPT_EAL_RandDeinitEx(NULL);
2120 return;
2121 #endif
2122 }
2123 /* END_CASE */
2124
2125