• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdlib.h>
19 #include "securec.h"
20 #include "bsl_sal.h"
21 #include "crypt_errno.h"
22 #include "crypt_bn.h"
23 #include "bn_basic.h"
24 #include "crypt_eal_rand.h"
25 #include "crypt_util_rand.h"
26 #include "crypto_test_util.h"
27 
28 #if defined(HITLS_SIXTY_FOUR_BITS)
29 #define BN_UINT_MAX UINT64_MAX
30 #define BN_DIGITS_MAX 65
31 #define DH_BN_DIGITS_MAX 129
32 #elif defined(HITLS_THIRTY_TWO_BITS)
33 #define BN_UINT_MAX UINT32_MAX
34 #define BN_DIGITS_MAX 65 * 2
35 #define DH_BN_DIGITS_MAX 129 * 2
36 #else
37 #error
38 #endif
39 
40 #define BITS_OF_BYTE 8
41 #define BIGNUM_REDUNDANCY_BITS 64
42 #define LONG_BN_BYTES_32 32
43 #define SHORT_BN_BITS_128 128
44 #define LONG_BN_BITS_256 256
45 #define UINT8_MAX_NUM 255
46 #define BN_SIZE 1024
47 
48 extern int32_t ModExpInputCheck(
49     const BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *e, const BN_BigNum *m, const BN_Optimizer *opt);
50 extern int32_t ModExpCore(BN_BigNum *x, BN_BigNum *y, const BN_BigNum *e, const BN_BigNum *m, BN_Optimizer *opt);
51 extern int32_t BnGcdCheckInput(const BN_BigNum *r, const BN_BigNum *a, const BN_BigNum *b, const BN_Optimizer *opt);
52 extern int32_t InverseInputCheck(const BN_BigNum *r, const BN_BigNum *x, const BN_BigNum *m, const BN_Optimizer *opt);
53 
TEST_Random(uint8_t * r,uint32_t randLen)54 static int32_t TEST_Random(uint8_t *r, uint32_t randLen)
55 {
56     for (uint32_t i = 0; i < randLen; i++) {
57         r[i] = rand() % UINT8_MAX_NUM;
58     }
59     return 0;
60 }
61 
TEST_RandomEx(void * libCtx,uint8_t * r,uint32_t randLen)62 static int32_t TEST_RandomEx(void *libCtx, uint8_t *r, uint32_t randLen)
63 {
64     (void) libCtx;
65     for (uint32_t i = 0; i < randLen; i++) {
66         r[i] = rand() % UINT8_MAX_NUM;
67     }
68     return 0;
69 }
70 
71 
TEST_GetMax(uint32_t num1,uint32_t num2,uint32_t num3)72 uint32_t TEST_GetMax(uint32_t num1, uint32_t num2, uint32_t num3)
73 {
74     uint32_t res = num1;
75     res = (res > num2) ? res : num2;
76     res = (res > num3) ? res : num3;
77     return res;
78 }
79 
TEST_VectorToBN(int sign,uint8_t * buff,uint32_t length)80 BN_BigNum *TEST_VectorToBN(int sign, uint8_t *buff, uint32_t length)
81 {
82     if (length == 0) {
83         return NULL;
84     }
85     BN_BigNum *bn = BN_Create(length * 8);  // 8 bits per byte
86     if (bn != NULL) {
87         if (BN_Bin2Bn(bn, buff, length) != CRYPT_SUCCESS) {
88             BN_Destroy(bn);
89             bn = NULL;
90         } else {
91             BN_SetSign(bn, sign != 0);
92         }
93     }
94     return bn;
95 }
TEST_RegSimpleRand(void)96 void TEST_RegSimpleRand(void)
97 {
98     CRYPT_RandRegist(TestSimpleRand);
99 }
100 
TEST_BnTestCaseInit(void)101 int32_t TEST_BnTestCaseInit(void)
102 {
103     TEST_RegSimpleRand();
104     TestMemInit();
105     return CRYPT_SUCCESS;
106 }
107 /* END_HEADER */
108 
109 /**
110  * @test   SDV_CRYPTO_BN_CREATE_API_TC001
111  * @title  BN_Create: Invalid parameter.
112  * @precon nan
113  * @brief
114  *    1. Call the BN_Create method, input parameter is BN_MAX_BITS + 1, expected result 1
115  *    2. Call the BN_Create method, input parameter is 0, expected result 2
116  * @expect
117  *    1. Return NULL.
118  *    2. Return non-NULL.
119  */
120 /* BEGIN_CASE */
SDV_CRYPTO_BN_CREATE_API_TC001(void)121 void SDV_CRYPTO_BN_CREATE_API_TC001(void)
122 {
123     BN_BigNum *bn = NULL;
124     TestMemInit();
125 
126     bn = BN_Create((1u << 29) + 1);  // BN_MAX_BITS + 1
127     ASSERT_TRUE(bn == NULL);
128 
129     bn = BN_Create(0);
130     ASSERT_TRUE(bn != NULL);
131 EXIT:
132     BN_Destroy(bn);
133 }
134 /* END_CASE */
135 
136 /**
137  * @test   SDV_CRYPTO_BN_SETSIGN_API_TC001
138  * @title  BN_SetSign: Test invalid parameters and normal functions.
139  * @precon nan
140  * @brief
141  *    1. Create BN_BigNum bn, the initial value is 0.
142  *    2. Call the BN_SetSign method, a is null, expected result 1
143  *    3. Call the BN_SetSign method, a is bn, sign is true, expected result 2
144  *    4. Set bn to 1, expected result 3
145  *    5. Call the BN_SetSign method, a is bn, sign is true/false, expected result 4
146  * @expect
147  *    1. CRYPT_NULL_INPUT
148  *    2. CRYPT_BN_NO_NEGATIVE_ZERO
149  *    3-4. CRYPT_SUCCESS
150  */
151 /* BEGIN_CASE */
SDV_CRYPTO_BN_SETSIGN_API_TC001(void)152 void SDV_CRYPTO_BN_SETSIGN_API_TC001(void)
153 {
154     BN_BigNum *bn = NULL;
155     TestMemInit();
156     ASSERT_TRUE(BN_SetSign(NULL, 0) == CRYPT_NULL_INPUT);
157 
158     bn = BN_Create(BN_MAX_BITS);
159     ASSERT_TRUE(bn != NULL);
160     ASSERT_TRUE(BN_SetSign(bn, true) == CRYPT_BN_NO_NEGATIVE_ZERO);
161 
162     ASSERT_TRUE(BN_SetLimb(bn, 1) == CRYPT_SUCCESS);
163     ASSERT_TRUE(BN_SetSign(bn, false) == CRYPT_SUCCESS);
164 EXIT:
165     BN_Destroy(bn);
166 }
167 /* END_CASE */
168 
169 /**
170  * @test   SDV_CRYPTO_BN_COPY_API_TC001
171  * @title  BN_Copy: The dest parameter space is smaller than src parameter.
172  * @precon nan
173  * @brief
174  *    1. Call the BN_Copy method, parameters are all, expected result 1
175  *    2. Create BN_BigNum bn a and r, the size of r is half of a, expected result 2
176  *    3. Call the BN_Copy method, copy a to r, expected result 3
177  * @expect
178  *    1. CRYPT_NULL_INPUT
179  *    2. CRYPT_SUCCESS
180  *    3. CRYPT_SUCCESS(Big number can be automatically expanded.)
181  */
182 /* BEGIN_CASE */
SDV_CRYPTO_BN_COPY_API_TC001(void)183 void SDV_CRYPTO_BN_COPY_API_TC001(void)
184 {
185     BN_BigNum *r = NULL;
186     BN_BigNum *a = NULL;
187     uint8_t buff[LONG_BN_BYTES_32] = {'F'};
188 
189     TestMemInit();
190     ASSERT_TRUE(BN_Copy(NULL, NULL) == CRYPT_NULL_INPUT);
191 
192     // r.room < a.bits
193     r = BN_Create(SHORT_BN_BITS_128);
194     ASSERT_TRUE(r != NULL);
195     // SHORT_BN_BYTES 16 = 32 / 2
196     ASSERT_TRUE(BN_Bin2Bn(r, buff, sizeof(buff) / 2) == CRYPT_SUCCESS);
197 
198     a = BN_Create(LONG_BN_BITS_256);
199     ASSERT_TRUE(a != NULL);
200     ASSERT_TRUE(BN_Bin2Bn(a, buff, sizeof(buff)) == CRYPT_SUCCESS);
201 
202     ASSERT_TRUE(BN_Copy(r, a) == CRYPT_SUCCESS);
203 
204 EXIT:
205     BN_Destroy(r);
206     BN_Destroy(a);
207 }
208 /* END_CASE */
209 
210 /**
211  * @test   SDV_CRYPTO_BN_ZEROIZE_API_TC001
212  * @title  BN_Zeroize: Invalid parameter.
213  * @precon nan
214  * @brief
215  *    1. Call the BN_Zeroize, parameter is null, expected result 1
216  * @expect
217  *    1. CRYPT_NULL_INPUT
218  */
219 /* BEGIN_CASE */
SDV_CRYPTO_BN_ZEROIZE_API_TC001(void)220 void SDV_CRYPTO_BN_ZEROIZE_API_TC001(void)
221 {
222     ASSERT_TRUE(BN_Zeroize(NULL) == CRYPT_NULL_INPUT);
223 EXIT:
224     return;
225 }
226 /* END_CASE */
227 
228 /**
229  * @test   SDV_CRYPTO_BN_SETLIMB_API_TC001
230  * @title  BN_SetLimb: Test invalid parameters and normal functions.
231  * @precon nan
232  * @brief
233  *    1. Create BN_BigNum bn.
234  *    2. Call the BN_SetLimb method, r is null, w is 0, expected result 1
235  *    3. Call the BN_SetLimb method, r is bn, w is 0, expected result 2
236  * @expect
237  *    1. CRYPT_NULL_INPUT
238  *    2. CRYPT_SUCCESS
239  */
240 /* BEGIN_CASE */
SDV_CRYPTO_BN_SETLIMB_API_TC001(void)241 void SDV_CRYPTO_BN_SETLIMB_API_TC001(void)
242 {
243     TestMemInit();
244     BN_BigNum *bn = BN_Create(1);
245 
246     ASSERT_TRUE(BN_SetLimb(NULL, 0) == CRYPT_NULL_INPUT);
247     ASSERT_TRUE(BN_SetLimb(bn, 0) == CRYPT_SUCCESS);
248 EXIT:
249     BN_Destroy(bn);
250     return;
251 }
252 /* END_CASE */
253 
254 /**
255  * @test   SDV_CRYPTO_BN_SETBIT_API_TC001
256  * @title  BN_SetBit: Test invalid parameters and normal functions.
257  * @precon nan
258  * @brief
259  *    1. Create BN_BigNum bn.
260  *    2. Call the BN_SetBit method, a is null, n is 0, expected result 1
261  *    3. Call the BN_SetBit method, a is bn, n is invalid, expected result 2
262  *    4. Call the BN_SetBit method, a is bn, n is valid, expected result 3
263  * @expect
264  *    1. CRYPT_NULL_INPUT
265  *    2. CRYPT_BN_SPACE_NOT_ENOUGH
266  *    2. CRYPT_SUCCESS
267  */
268 /* BEGIN_CASE */
SDV_CRYPTO_BN_SETBIT_API_TC001(void)269 void SDV_CRYPTO_BN_SETBIT_API_TC001(void)
270 {
271     TestMemInit();
272     BN_BigNum *bn = BN_Create(1);
273 
274     ASSERT_TRUE(BN_SetBit(NULL, 0) == CRYPT_NULL_INPUT);
275     ASSERT_TRUE(BN_SetBit(bn, (uint32_t)sizeof(BN_UINT) << 3) == CRYPT_BN_SPACE_NOT_ENOUGH);
276     ASSERT_TRUE(BN_SetBit(bn, ((uint32_t)sizeof(BN_UINT) << 3) - 1) == CRYPT_SUCCESS);
277 EXIT:
278     BN_Destroy(bn);
279     return;
280 }
281 /* END_CASE */
282 
283 /**
284  * @test   SDV_CRYPTO_BN_GETBIT_API_TC001
285  * @title  BN_GetBit: Test invalid parameters and normal functions.
286  * @precon nan
287  * @brief
288  *    1. Call the BN_GetBit method, a is null, n is 0, expected result 1
289  *    2. Create BN_BigNum bn, bn = 0, expected result 2
290  *    3. Call the BN_GetBit method, get the first bit of bn, expected result 3
291  *    4. Set limb of bn to BN_UINT_MAX, expected result 4
292  *    5. Call the BN_GetBit method, Check whether the number of the specified bit is 1:
293  *       (1) bit = limbBits - 1, expected result 5
294  *       (1) bit = limbBits, expected result 6
295  *       (1) bit = limbBits + 1, expected result 7
296  * @expect
297  *    1. Return 0.
298  *    2. CRYPT_SUCCESS
299  *    3. The first bit of bn is 0.
300  *    4. CRYPT_SUCCESS. The number of bits in the limb is limbBits.
301  *    5. true
302  *    6. false
303  *    7. false
304  */
305 /* BEGIN_CASE */
SDV_CRYPTO_BN_GETBIT_API_TC001(void)306 void SDV_CRYPTO_BN_GETBIT_API_TC001(void)
307 {
308     BN_BigNum *bn = NULL;
309     const int limbBits = sizeof(BN_UINT) * BITS_OF_BYTE;
310 
311     ASSERT_TRUE(BN_GetBit(NULL, 0) == 0);
312 
313     TestMemInit();
314     bn = BN_Create(SHORT_BN_BITS_128);
315     ASSERT_TRUE(bn != NULL);
316 
317     // a = 0 , n = 0
318     ASSERT_TRUE(BN_SetLimb(bn, 0) == CRYPT_SUCCESS);
319     ASSERT_TRUE(BN_GetBit(bn, 0) == 0);
320 
321     ASSERT_TRUE(BN_SetLimb(bn, BN_UINT_MAX) == CRYPT_SUCCESS);
322     ASSERT_TRUE(BN_GetBit(bn, limbBits - 1) == true);
323     ASSERT_TRUE(BN_GetBit(bn, limbBits) == false);
324     ASSERT_TRUE(BN_GetBit(bn, limbBits + 1) == false);
325 EXIT:
326     BN_Destroy(bn);
327 }
328 /* END_CASE */
329 
330 /**
331  * @test   SDV_CRYPTO_BN_CLRBIT_API_TC001
332  * @title  BN_ClrBit: Test invalid parameters and normal functions.
333  * @precon nan
334  * @brief
335  *    1. Call the BN_ClrBit method, a is null, n is 0, expected result 1
336  *    2. Create BN_BigNum bn, and set bn to -1, expected result 2
337  *    3. Call the BN_ClrBit method, set the lowest bit to 0, expected result 3
338  *    4. Set bn to 1, expected result 4
339  *    5. Call the BN_ClrBit method, a is bn, n is BN_UINT_BITS, expected result 5
340  *    6. Call the BN_ClrBit method, a is bn, n is valid, expected result 6
341  * @expect
342  *    1. CRYPT_NULL_INPUT
343  *    2. CRYPT_SUCCESS
344  *    3. CRYPT_SUCCESS, bn changed from - 1 to 0.
345  *    4. CRYPT_SUCCESS
346  *    5. CRYPT_BN_SPACE_NOT_ENOUGH
347  *    6. CRYPT_SUCCESS
348  */
349 /* BEGIN_CASE */
SDV_CRYPTO_BN_CLRBIT_API_TC001(void)350 void SDV_CRYPTO_BN_CLRBIT_API_TC001(void)
351 {
352     BN_BigNum *bn = NULL;
353     ASSERT_TRUE(BN_ClrBit(NULL, 0) == CRYPT_NULL_INPUT);
354 
355     TestMemInit();
356     bn = BN_Create(BN_MAX_BITS);
357 
358     /* bn = -1 */
359     ASSERT_TRUE(BN_SetLimb(bn, 1) == CRYPT_SUCCESS);
360     ASSERT_TRUE(BN_SetSign(bn, 1) == CRYPT_SUCCESS);
361 
362     /* Set the lowest bit to 0. */
363     ASSERT_TRUE(BN_ClrBit(bn, 0) == CRYPT_SUCCESS);
364     ASSERT_TRUE(BN_IsZero(bn));
365     ASSERT_TRUE(BN_IsNegative(bn) == false);
366 
367     /* bn = 1 */
368     ASSERT_TRUE(BN_SetLimb(bn, 1) == CRYPT_SUCCESS);
369 
370     ASSERT_EQ(BN_ClrBit(bn, ((uint32_t)sizeof(BN_UINT) << 3)), CRYPT_BN_SPACE_NOT_ENOUGH);  // BN_UINT_BITS
371     ASSERT_TRUE(BN_ClrBit(bn, ((uint32_t)sizeof(BN_UINT) << 3) - 1) == CRYPT_SUCCESS);      // BN_UINT_BITS - 1
372 EXIT:
373     BN_Destroy(bn);
374     return;
375 }
376 /* END_CASE */
377 
378 /**
379  * @test   SDV_CRYPTO_BN_RSHIFT_FUNC_TC001
380  * @title  BN_Rshift test.
381  * @precon Vectors: hex and its result after right-shifting n bits.
382  * @brief
383  *    1. Convert vectors to bn.
384  *    2. Call BN_Rshift, and compared the result with vector, expected result 1:
385  *       (1) Pointer addresses for parameters are different from each other.
386  *       (2) The address of the output parameter pointer r is the same as that of the input parameter pointer a.
387  *    3. Test the scenario where the output parameter space is insufficient, expected result 1.
388  * @expect
389  *    1. The calculation result is consistent with the vector.
390  */
391 /* BEGIN_CASE */
SDV_CRYPTO_BN_RSHIFT_FUNC_TC001(int sign,Hex * hex,int n,int signRes,Hex * result)392 void SDV_CRYPTO_BN_RSHIFT_FUNC_TC001(int sign, Hex *hex, int n, int signRes, Hex *result)
393 {
394     TestMemInit();
395     BN_BigNum *r = NULL;
396     BN_BigNum *q = NULL;
397     BN_BigNum *p = NULL;
398     BN_BigNum *a = TEST_VectorToBN(sign, hex->x, hex->len);
399     BN_BigNum *res = TEST_VectorToBN(signRes, result->x, result->len);
400     ASSERT_TRUE(a != NULL && res != NULL);
401 
402     r = BN_Create(BN_Bits(a) + BIGNUM_REDUNDANCY_BITS);
403     ASSERT_TRUE(r != NULL);
404     ASSERT_EQ(BN_Rshift(r, a, n), CRYPT_SUCCESS);  // r != a
405     ASSERT_TRUE(BN_Cmp(r, res) == 0);
406 
407     ASSERT_EQ(BN_Copy(r, a), CRYPT_SUCCESS);
408     ASSERT_EQ(BN_Rshift(r, r, n), CRYPT_SUCCESS);  // r == a
409     ASSERT_TRUE(BN_Cmp(r, res) == 0);
410 
411     /* Test the scenario where the output parameter space of q is insufficient */
412     q = BN_Create(0);
413     ASSERT_TRUE(q != NULL);
414     ASSERT_EQ(BN_Rshift(q, a, n), CRYPT_SUCCESS);  // r != a
415     ASSERT_TRUE(BN_Cmp(r, res) == 0);
416 
417 EXIT:
418     BN_Destroy(a);
419     BN_Destroy(r);
420     BN_Destroy(q);
421     BN_Destroy(p);
422     BN_Destroy(res);
423 }
424 /* END_CASE */
425 
426 /**
427  * @test   SDV_CRYPTO_BN_MODINV_API_TC001
428  * @title  BN_ModInv test.
429  * @precon nan
430  * @brief
431  *    1. Call BN_ModInv method:
432  *       (1) all parameters are valid, expected result 1.
433  *       (2) r is null, expected result 2.
434  *       (3) a is null, expected result 3.
435  *       (4) m is null, expected result 4.
436  *       (5) opt is null, expected result 5.
437  *       (6) a is zero or m is zero, expected result 6.
438  *       (7) r.room.bits < m.bits, expected result 7.
439  *       (8) r.room.bits = m.bits, expected result 8.
440  *       (9) r.room.bits > m.bits, expected result 9.
441  * @expect
442  *    1. CRYPT_SUCCESS
443  *    2-5. CRYPT_NULL_INPUT
444  *    6. CRYPT_BN_ERR_DIVISOR_ZERO
445  *    7-9. CRYPT_SUCCESS
446  */
447 /* BEGIN_CASE */
SDV_CRYPTO_BN_MODINV_API_TC001(void)448 void SDV_CRYPTO_BN_MODINV_API_TC001(void)
449 {
450     TestMemInit();
451     uint8_t buff[LONG_BN_BYTES_32];
452     BN_BigNum *a = BN_Create(LONG_BN_BITS_256);
453     BN_BigNum *m = BN_Create(LONG_BN_BITS_256);
454     BN_BigNum *zero = BN_Create(LONG_BN_BITS_256);
455     BN_BigNum *r128 = BN_Create(SHORT_BN_BITS_128);
456     BN_BigNum *r256 = BN_Create(LONG_BN_BITS_256);
457     BN_BigNum *r257 = BN_Create(LONG_BN_BITS_256 + 1);
458     BN_Optimizer *opt = BN_OptimizerCreate();
459     ASSERT_TRUE(a != NULL && m != NULL && r128 != NULL && r256 != NULL && zero != NULL && opt != NULL);
460     ASSERT_TRUE(BN_IsZero(zero) == true);
461 
462     // a = FF...FF (32 bytes)
463     ASSERT_TRUE(memset_s(buff, sizeof(buff), 0xFF, LONG_BN_BYTES_32) == EOK);
464     ASSERT_TRUE(BN_Bin2Bn(a, buff, LONG_BN_BYTES_32) == CRYPT_SUCCESS);
465     // m == FF...FD (32 bytes)
466     buff[LONG_BN_BYTES_32 - 1] = (uint8_t)0xFD;
467     ASSERT_TRUE(BN_Bin2Bn(m, buff, LONG_BN_BYTES_32) == CRYPT_SUCCESS);
468 
469     // NULL
470     ASSERT_TRUE(BN_ModInv(r256, a, m, opt) == CRYPT_SUCCESS);
471     ASSERT_TRUE(BN_ModInv(NULL, a, m, opt) == CRYPT_NULL_INPUT);
472     ASSERT_TRUE(BN_ModInv(r256, NULL, m, opt) == CRYPT_NULL_INPUT);
473     ASSERT_TRUE(BN_ModInv(r256, a, NULL, opt) == CRYPT_NULL_INPUT);
474     ASSERT_TRUE(BN_ModInv(r256, a, m, NULL) == CRYPT_NULL_INPUT);
475 
476     // zero
477     ASSERT_TRUE(BN_ModInv(r256, a, zero, opt) == CRYPT_BN_ERR_DIVISOR_ZERO);
478     ASSERT_TRUE(BN_ModInv(r256, zero, m, opt) == CRYPT_BN_ERR_DIVISOR_ZERO);
479 
480     // r.room.bits < m.bits
481     ASSERT_TRUE(BN_ModInv(r128, a, m, opt) == CRYPT_SUCCESS);
482 
483     // r.room.bits == m.bits
484     ASSERT_TRUE(BN_ModInv(r256, a, m, opt) == CRYPT_SUCCESS);
485 
486     // r.room.bits > m.bits
487     ASSERT_TRUE(BN_ModInv(r257, a, m, opt) == CRYPT_SUCCESS);
488 
489 EXIT:
490     BN_Destroy(a);
491     BN_Destroy(m);
492     BN_Destroy(zero);
493     BN_Destroy(r128);
494     BN_Destroy(r256);
495     BN_Destroy(r257);
496     BN_OptimizerDestroy(opt);
497 }
498 /* END_CASE */
499 
500 /**
501  * @test   SDV_CRYPTO_BN_MODINV_FUNC_TC002
502  * @title  BN_ModInv test.
503  * @precon Vectors: hex1^(-1) mod modulo = result
504  * @brief
505  *    1. Convert vectors to bn.
506  *    2. Call BN_ModInv, and compared the result with vector, expected result 1.
507  *    3. If success is returned in the previous step, continue to test BN_ModInv when the output parameter address
508  *       is the same as the input parameter address. expected result 2:
509  *       (1) The address of the output parameter pointer r is the same as that of the input parameter pointer a.
510  *       (2) The address of the output parameter pointer r is the same as that of the input parameter pointer m.
511  *    4. Test the scenario where the output parameter space is insufficient, expected result 2.
512  * @expect
513  *    1. Reutrn CRYPT_BN_ERR_NO_INVERSE on result is null. Otherwise, CRYPT_SUCCESS and result is same with vector.
514  *    2. The calculation result is consistent with the vector.
515  */
516 /* BEGIN_CASE */
SDV_CRYPTO_BN_MODINV_FUNC_TC002(int sign,Hex * hex,Hex * modulo,Hex * result)517 void SDV_CRYPTO_BN_MODINV_FUNC_TC002(int sign, Hex *hex, Hex *modulo, Hex *result)
518 {
519     TestMemInit();
520     int32_t ret;
521     BN_BigNum *res = NULL;
522     BN_BigNum *r = NULL;
523     BN_Optimizer *opt = BN_OptimizerCreate();
524     BN_BigNum *a = TEST_VectorToBN(sign, hex->x, hex->len);
525     BN_BigNum *m = TEST_VectorToBN(0, modulo->x, modulo->len);
526     ASSERT_TRUE(a != NULL && m != NULL && opt != NULL);
527 
528     r = BN_Create(BN_Bits(a) + BN_Bits(m) + BIGNUM_REDUNDANCY_BITS);
529     ASSERT_TRUE(r != NULL);
530 
531     ASSERT_TRUE(BN_Copy(r, a) == CRYPT_SUCCESS);
532 
533     ret = BN_ModInv(r, a, m, opt);  // r != a
534     if (result->len == 0) {
535         ASSERT_TRUE(ret == CRYPT_BN_ERR_NO_INVERSE);  // No results exist
536     } else {
537         ASSERT_TRUE(ret == CRYPT_SUCCESS);
538         res = BN_Create(result->len * BITS_OF_BYTE);
539         ASSERT_TRUE(res != NULL);
540 
541         ASSERT_TRUE(BN_Bin2Bn(res, result->x, result->len) == CRYPT_SUCCESS);
542 
543         ASSERT_TRUE(BN_Cmp(r, res) == 0);
544 
545         ASSERT_TRUE(BN_Copy(r, a) == CRYPT_SUCCESS);
546         ASSERT_TRUE(BN_ModInv(r, r, m, opt) == CRYPT_SUCCESS);
547         ASSERT_TRUE_AND_LOG("r == a", BN_Cmp(r, res) == 0);
548 
549         ASSERT_TRUE(BN_Copy(r, m) == CRYPT_SUCCESS);
550         ASSERT_TRUE(BN_ModInv(r, a, r, opt) == CRYPT_SUCCESS);
551         ASSERT_TRUE_AND_LOG("r == m", BN_Cmp(r, res) == 0);
552         BN_Destroy(r);
553 
554         /* Test the scenario where the output parameter space is insufficient. */
555         r = BN_Create(0);
556         ASSERT_TRUE(r != NULL);
557         ASSERT_TRUE(BN_ModInv(r, a, m, opt) == CRYPT_SUCCESS);
558         ASSERT_TRUE(BN_Cmp(r, res) == 0);
559         BN_Destroy(r);
560 
561         r = BN_Create(0);
562         ASSERT_TRUE(r != NULL);
563         ASSERT_TRUE(BN_Copy(r, a) == CRYPT_SUCCESS);
564         ASSERT_TRUE(BN_ModInv(r, r, m, opt) == CRYPT_SUCCESS);
565         ASSERT_TRUE_AND_LOG("r == a", BN_Cmp(r, res) == 0);
566         BN_Destroy(r);
567 
568         r = BN_Create(0);
569         ASSERT_TRUE(r != NULL);
570         ASSERT_TRUE(BN_Copy(r, m) == CRYPT_SUCCESS);
571         ASSERT_TRUE(BN_ModInv(r, a, r, opt) == CRYPT_SUCCESS);
572         ASSERT_TRUE_AND_LOG("r == m", BN_Cmp(r, res) == 0);
573     }
574 
575 EXIT:
576     BN_Destroy(a);
577     BN_Destroy(m);
578     BN_Destroy(r);
579     BN_Destroy(res);
580     BN_OptimizerDestroy(opt);
581 }
582 /* END_CASE */
583 
584 /**
585  * @test   SDV_CRYPTO_BN_MOD_EXP_INPUT_CHECK_API_TC001
586  * @title  ModExpInputCheck: Test invalid parameters and normal functions.
587  * @precon nan
588  * @brief
589  *    1. Call the ModExpInputCheck method, parameters are null,expected result 1
590  *    2. Call the ModExpInputCheck method, the size of r is smaller then m, expected result 2
591  *    3. Call the ModExpInputCheck method, m is 0, expected result 3
592  *    4. Call the ModExpInputCheck method, e is a negative number, expected result 4
593  * @expect
594  *    1. CRYPT_NULL_INPUT
595  *    2. CRYPT_SUCCESS
596  *    3. CRYPT_BN_ERR_DIVISOR_ZERO
597  *    4. CRYPT_BN_ERR_EXP_NO_NEGATIVE
598  */
599 /* BEGIN_CASE */
SDV_CRYPTO_BN_MOD_EXP_INPUT_CHECK_API_TC001(void)600 void SDV_CRYPTO_BN_MOD_EXP_INPUT_CHECK_API_TC001(void)
601 {
602     BN_BigNum *bn = NULL;
603     BN_BigNum *r = NULL;
604     BN_BigNum *a = NULL;
605     BN_BigNum *e = NULL;
606     BN_BigNum *m = NULL;
607     BN_Optimizer *opt = NULL;
608     TestMemInit();
609 
610     ASSERT_TRUE(ModExpInputCheck(NULL, NULL, NULL, NULL, NULL) == CRYPT_NULL_INPUT);
611 
612     bn = BN_Create(1);
613     a = BN_Create(BN_MAX_BITS);
614     e = BN_Create(BN_MAX_BITS);
615     opt = BN_OptimizerCreate();
616     r = BN_Create(1);
617     m = BN_Create(BN_MAX_BITS);
618     ASSERT_TRUE(BN_SetBit(m, BN_SIZE) == CRYPT_SUCCESS);
619     ASSERT_TRUE(ModExpInputCheck(r, a, e, m, opt) == CRYPT_SUCCESS);
620 
621     BN_Destroy(r);
622     r = BN_Create(BN_MAX_BITS);
623     ASSERT_TRUE(ModExpInputCheck(r, a, e, bn, opt) == CRYPT_BN_ERR_DIVISOR_ZERO);
624 
625     ASSERT_TRUE(BN_SetBit(e, BN_SIZE) == CRYPT_SUCCESS);
626     ASSERT_TRUE(BN_SetSign(e, true) == CRYPT_SUCCESS);
627     ASSERT_TRUE(ModExpInputCheck(r, a, e, m, opt) == CRYPT_BN_ERR_EXP_NO_NEGATIVE);
628 
629 EXIT:
630     BN_Destroy(bn);
631     BN_Destroy(r);
632     BN_Destroy(a);
633     BN_Destroy(e);
634     BN_Destroy(m);
635     BN_OptimizerDestroy(opt);
636 }
637 /* END_CASE */
638 
639 /**
640  * @test   SDV_CRYPTO_BN_MODEXP_API_TC001
641  * @title  BN_ModExp: Test invalid parameters.
642  * @precon nan
643  * @brief
644  *    1. Call the BN_ModExp method, parameters are null, expected result 1
645  *    2. Call the BN_ModExp method, the size of r is smaller then m, expected result 2
646  * @expect
647  *    1. CRYPT_NULL_INPUT
648  *    2. CRYPT_SUCCESS
649  */
650 /* BEGIN_CASE */
SDV_CRYPTO_BN_MODEXP_API_TC001(void)651 void SDV_CRYPTO_BN_MODEXP_API_TC001(void)
652 {
653     uint8_t buff[LONG_BN_BYTES_32];
654     TestMemInit();
655 
656     BN_BigNum *a = BN_Create(LONG_BN_BITS_256);
657     BN_BigNum *e = BN_Create(LONG_BN_BITS_256);
658     BN_BigNum *r = BN_Create(LONG_BN_BITS_256);
659     BN_BigNum *m = BN_Create(LONG_BN_BITS_256);
660     BN_Optimizer *opt = BN_OptimizerCreate();
661     ASSERT_TRUE(a != NULL);
662     ASSERT_TRUE(e != NULL);
663     ASSERT_TRUE(r != NULL);
664     ASSERT_TRUE(m != NULL);
665     ASSERT_TRUE(opt != NULL);
666     ASSERT_TRUE(memset_s(buff, sizeof(buff), 0xFF, LONG_BN_BYTES_32) == EOK);
667 
668     ASSERT_TRUE(BN_Bin2Bn(a, buff, LONG_BN_BYTES_32) == CRYPT_SUCCESS);
669 
670     ASSERT_TRUE(BN_Bin2Bn(e, buff, LONG_BN_BYTES_32) == CRYPT_SUCCESS);
671 
672     ASSERT_TRUE(BN_Bin2Bn(m, buff, LONG_BN_BYTES_32) == CRYPT_SUCCESS);
673 
674     // NULL
675     ASSERT_TRUE(BN_ModExp(NULL, NULL, NULL, NULL, NULL) == CRYPT_NULL_INPUT);
676 
677     BN_Destroy(r);
678     r = BN_Create(LONG_BN_BITS_256 - BIGNUM_REDUNDANCY_BITS);
679     ASSERT_TRUE(BN_ModExp(r, a, e, m, opt) == CRYPT_SUCCESS);
680 
681 EXIT:
682     BN_Destroy(a);
683     BN_Destroy(e);
684     BN_Destroy(r);
685     BN_Destroy(m);
686     BN_OptimizerDestroy(opt);
687 }
688 /* END_CASE */
689 
690 /**
691  * @test   SDV_CRYPTO_BN_MODEXP_API_TC002
692  * @title  BN_ModExp: Invalid parameter and function test.
693  * @precon nan
694  * @brief
695  *    1. Call the BN_ModExp method, the divisor is 0, expected result 1
696  *    2. Call the BN_ModExp method, the value of the exponent is negative, expected result 2
697  *    3. Call the BN_ModExp method, all parameters are valid, expected result 3
698  * @expect
699  *    1. CRYPT_BN_ERR_DIVISOR_ZERO
700  *    2. CRYPT_BN_ERR_EXP_NO_NEGATIVE
701  *    3. CRYPT_SUCCESS
702  */
703 /* BEGIN_CASE */
SDV_CRYPTO_BN_MODEXP_API_TC002(void)704 void SDV_CRYPTO_BN_MODEXP_API_TC002(void)
705 {
706     uint8_t buff[LONG_BN_BYTES_32];
707     BN_BigNum *a = BN_Create(LONG_BN_BYTES_32 * 8);
708     BN_BigNum *e = BN_Create(LONG_BN_BYTES_32 * 8);
709     BN_BigNum *m = BN_Create(LONG_BN_BYTES_32 * 8);
710     BN_BigNum *r = BN_Create(LONG_BN_BITS_256);
711     BN_BigNum *zero = BN_Create(LONG_BN_BITS_256);
712     BN_BigNum *one = BN_Create(LONG_BN_BITS_256);
713     BN_BigNum *negOne = BN_Create(LONG_BN_BITS_256);
714     BN_Optimizer *opt = BN_OptimizerCreate();
715     TestMemInit();
716 
717     ASSERT_TRUE(r != NULL);
718     ASSERT_TRUE(zero != NULL);
719     ASSERT_TRUE(one != NULL);
720     ASSERT_TRUE(negOne != NULL);
721     ASSERT_TRUE(opt != NULL);
722 
723     ASSERT_TRUE(memset_s(buff, sizeof(buff), 0xFF, LONG_BN_BYTES_32) == EOK);
724 
725     ASSERT_TRUE(a != NULL);
726     ASSERT_TRUE(BN_Bin2Bn(a, buff, LONG_BN_BYTES_32) == CRYPT_SUCCESS);
727     ASSERT_TRUE(BN_SetSign(a, false) == CRYPT_SUCCESS);
728 
729     ASSERT_TRUE(e != NULL);
730     ASSERT_TRUE(BN_Bin2Bn(e, buff, LONG_BN_BYTES_32) == CRYPT_SUCCESS);
731     ASSERT_TRUE(BN_SetSign(e, false) == CRYPT_SUCCESS);
732 
733     ASSERT_TRUE(m != NULL);
734     ASSERT_TRUE(BN_Bin2Bn(m, buff, LONG_BN_BYTES_32) == CRYPT_SUCCESS);
735     ASSERT_TRUE(BN_SetSign(m, false) == CRYPT_SUCCESS);
736 
737     ASSERT_TRUE(BN_Zeroize(zero) == CRYPT_SUCCESS);
738     ASSERT_TRUE(BN_SetLimb(one, 1) == CRYPT_SUCCESS);
739     ASSERT_TRUE(BN_SetLimb(negOne, 1) == CRYPT_SUCCESS);
740     ASSERT_TRUE(BN_SetSign(negOne, true) == CRYPT_SUCCESS);
741 
742     ASSERT_TRUE(BN_ModExp(r, a, e, zero, opt) == CRYPT_BN_ERR_DIVISOR_ZERO);
743     ASSERT_TRUE(BN_ModExp(r, a, negOne, m, opt) == CRYPT_BN_ERR_EXP_NO_NEGATIVE);
744     ASSERT_TRUE(BN_ModExp(r, zero, zero, m, opt) == CRYPT_SUCCESS);
745     ASSERT_TRUE(BN_Cmp(r, one) == 0);
746     ASSERT_TRUE(BN_ModExp(r, zero, zero, one, opt) == CRYPT_SUCCESS);
747     ASSERT_TRUE(BN_Cmp(r, zero) == 0);
748     ASSERT_TRUE(BN_ModExp(r, a, e, negOne, opt) == CRYPT_SUCCESS);
749     ASSERT_TRUE(BN_Cmp(r, zero) == 0);
750 
751 EXIT:
752     BN_Destroy(a);
753     BN_Destroy(e);
754     BN_Destroy(r);
755     BN_Destroy(m);
756     BN_Destroy(zero);
757     BN_Destroy(one);
758     BN_Destroy(negOne);
759     BN_OptimizerDestroy(opt);
760 }
761 /* END_CASE */
762 
763 /**
764  * @test   SDV_CRYPTO_BN_MODEXP_FUNC_TC001
765  * @title  BN_ModExp test.
766  * @precon Vectors: two big numbers, mod, result.
767  * @brief
768  *    1. Convert vectors to big numbers.
769  *    2. Call BN_ModExp to calculate the modular exponentiation, and compared to the vector value, expected result 1:
770  *       (1) Pointer addresses for parameters are different from each other.
771  *       (2) The address of the output parameter pointer r is the same as that of the input parameter pointer a.
772  *       (3) The address of the output parameter pointer r is the same as that of the input parameter pointer e.
773  *       (4) The address of the output parameter pointer r is the same as that of the input parameter pointer m.
774  *    3. Test the scenario where the output parameter space is insufficient, expected result 1.
775  * @expect
776  *    1. The calculation result is consistent with the vector.
777  */
778 /* BEGIN_CASE */
SDV_CRYPTO_BN_MODEXP_FUNC_TC001(int sign1,Hex * hex1,Hex * hex2,Hex * modulo,Hex * result)779 void SDV_CRYPTO_BN_MODEXP_FUNC_TC001(int sign1, Hex *hex1, Hex *hex2, Hex *modulo, Hex *result)
780 {
781     TestMemInit();
782     BN_BigNum *r = NULL;
783     BN_Optimizer *opt = NULL;
784     uint32_t maxBits;
785     BN_BigNum *res = TEST_VectorToBN(0, result->x, result->len);
786     BN_BigNum *a = TEST_VectorToBN(sign1, hex1->x, hex1->len);
787     BN_BigNum *e = TEST_VectorToBN(0, hex2->x, hex2->len);
788     BN_BigNum *m = TEST_VectorToBN(0, modulo->x, modulo->len);
789     ASSERT_TRUE(res != NULL && a != NULL && e != NULL && m != NULL);
790 
791     maxBits = TEST_GetMax(BN_Bits(a), BN_Bits(e), BN_Bits(m));
792     r = BN_Create(maxBits + BIGNUM_REDUNDANCY_BITS);
793     ASSERT_TRUE(r != NULL);
794 
795     opt = BN_OptimizerCreate();
796     ASSERT_TRUE(opt != NULL);
797 
798     ASSERT_TRUE(BN_Copy(r, a) == CRYPT_SUCCESS);
799     ASSERT_TRUE(BN_ModExp(r, a, e, m, opt) == CRYPT_SUCCESS);  // r != a
800     ASSERT_TRUE_AND_LOG("r != a", BN_Cmp(r, res) == 0);
801 
802     ASSERT_TRUE(BN_Copy(r, a) == CRYPT_SUCCESS);
803     ASSERT_TRUE(BN_ModExp(r, r, e, m, opt) == CRYPT_SUCCESS);  // r == a
804     ASSERT_TRUE_AND_LOG("r == a", BN_Cmp(r, res) == 0);
805 
806     ASSERT_TRUE(BN_Copy(r, e) == CRYPT_SUCCESS);
807     ASSERT_TRUE(BN_ModExp(r, a, r, m, opt) == CRYPT_SUCCESS);  // r == e
808     ASSERT_TRUE_AND_LOG("r == e", BN_Cmp(r, res) == 0);
809 
810     ASSERT_TRUE(BN_Copy(r, m) == CRYPT_SUCCESS);
811     ASSERT_TRUE(BN_ModExp(r, a, e, r, opt) == CRYPT_SUCCESS);  // r == m
812     ASSERT_TRUE_AND_LOG("r == m", BN_Cmp(r, res) == 0);
813 
814     // Test the scenario where the output parameter space is insufficient.
815     BN_Destroy(r);
816     r = BN_Create(0);
817     ASSERT_TRUE(r != NULL);
818 
819     ASSERT_TRUE(BN_ModExp(r, a, e, m, opt) == CRYPT_SUCCESS);  // r != a
820     ASSERT_TRUE_AND_LOG("r != a", BN_Cmp(r, res) == 0);
821 
822     BN_Destroy(r);
823     r = BN_Create(0);
824     ASSERT_TRUE(r != NULL);
825     ASSERT_TRUE(BN_Copy(r, a) == CRYPT_SUCCESS);
826     ASSERT_TRUE(BN_ModExp(r, r, e, m, opt) == CRYPT_SUCCESS);  // r == a
827     ASSERT_TRUE_AND_LOG("r == a", BN_Cmp(r, res) == 0);
828 
829     BN_Destroy(r);
830     r = BN_Create(0);
831     ASSERT_TRUE(r != NULL);
832     ASSERT_TRUE(BN_Copy(r, e) == CRYPT_SUCCESS);
833     ASSERT_TRUE(BN_ModExp(r, a, r, m, opt) == CRYPT_SUCCESS);  // r == e
834     ASSERT_TRUE_AND_LOG("r == e", BN_Cmp(r, res) == 0);
835 
836     BN_Destroy(r);
837     r = BN_Create(0);
838     ASSERT_TRUE(r != NULL);
839     ASSERT_TRUE(BN_Copy(r, m) == CRYPT_SUCCESS);
840     ASSERT_TRUE(BN_ModExp(r, a, e, r, opt) == CRYPT_SUCCESS);  // r == m
841     ASSERT_TRUE_AND_LOG("r == m", BN_Cmp(r, res) == 0);
842 
843 EXIT:
844     BN_Destroy(a);
845     BN_Destroy(e);
846     BN_Destroy(m);
847     BN_Destroy(r);
848     BN_Destroy(res);
849     BN_OptimizerDestroy(opt);
850 }
851 /* END_CASE */
852 
853 /**
854  * @test   SDV_CRYPTO_BN_MODEXPCORE_API_TC001
855  * @title  ModExpCore: Invalid parameter.
856  * @precon nan
857  * @brief
858  *    1. Call the ModExpCore method, the divisor is 0, expected result 1
859  * @expect
860  *    1. CRYPT_BN_ERR_DIVISOR_ZERO
861  */
862 /* BEGIN_CASE */
SDV_CRYPTO_BN_MODEXPCORE_API_TC001(void)863 void SDV_CRYPTO_BN_MODEXPCORE_API_TC001(void)
864 {
865     TestMemInit();
866     BN_BigNum *a = BN_Create(LONG_BN_BITS_256);
867     BN_BigNum *e = BN_Create(LONG_BN_BITS_256);
868     BN_BigNum *r = BN_Create(LONG_BN_BITS_256);
869     BN_BigNum *m = BN_Create(LONG_BN_BITS_256);
870     BN_Optimizer *opt = BN_OptimizerCreate();
871     ASSERT_TRUE(BN_SetLimb(e, 10) == CRYPT_SUCCESS);
872     ASSERT_TRUE(ModExpCore(r, a, e, m, opt) == CRYPT_BN_ERR_DIVISOR_ZERO);
873 
874 EXIT:
875     BN_Destroy(a);
876     BN_Destroy(e);
877     BN_Destroy(r);
878     BN_Destroy(m);
879     BN_OptimizerDestroy(opt);
880 }
881 /* END_CASE */
882 
883 /**
884  * @test   SDV_CRYPTO_BN_MOD_API_TC001
885  * @title  BN_Mod: Invalid parameter.
886  * @precon nan
887  * @brief
888  *    1. Call the BN_Mod method, all parameters are null, expected result 1
889  *    2. Call the BN_Mod method, mod is UINT8_MAX_NUM, expected result 2
890  *    3. Call the BN_Mod method, mod is 0, expected result 3
891  * @expect
892  *    1. CRYPT_NULL_INPUT
893  *    2. CRYPT_SUCCESS
894  *    3. CRYPT_BN_ERR_DIVISOR_ZERO
895  */
896 /* BEGIN_CASE */
SDV_CRYPTO_BN_MOD_API_TC001(void)897 void SDV_CRYPTO_BN_MOD_API_TC001(void)
898 {
899     TestMemInit();
900     BN_BigNum *a = BN_Create(LONG_BN_BITS_256);
901     BN_BigNum *r = BN_Create(SHORT_BN_BITS_128);
902     BN_BigNum *m = BN_Create(LONG_BN_BITS_256);
903     BN_Optimizer *opt = BN_OptimizerCreate();
904 
905     ASSERT_TRUE(BN_Mod(NULL, NULL, NULL, NULL) == CRYPT_NULL_INPUT);
906 
907     ASSERT_TRUE(BN_SetBit(m, UINT8_MAX_NUM) == CRYPT_SUCCESS);
908     ASSERT_TRUE(BN_Mod(r, a, m, opt) == CRYPT_SUCCESS);
909 
910     ASSERT_TRUE(BN_SetLimb(m, 0) == CRYPT_SUCCESS);
911     ASSERT_TRUE(BN_Mod(r, a, m, opt) == CRYPT_BN_ERR_DIVISOR_ZERO);
912 
913 EXIT:
914     BN_Destroy(a);
915     BN_Destroy(r);
916     BN_Destroy(m);
917     BN_OptimizerDestroy(opt);
918 }
919 /* END_CASE */
920 
921 /**
922  * @test   SDV_CRYPTO_BN_MOD_FUNC_TC001
923  * @title  BN_Mod test.
924  * @precon Vectors: hex1 mod module = result
925  * @brief
926  *    1. Convert vectors to bn.
927  *    2. Call BN_Mod, and compared the result with vector, expected result 1:
928  *       (1) Pointer addresses for parameters are different from each other.
929  *       (2) The address of the output parameter pointer r is the same as that of the input parameter pointer a.
930  *       (3) The address of the output parameter pointer r is the same as that of the input parameter pointer m.
931  *    3. Test the scenario where the output parameter space is insufficient, expected result 1.
932  * @expect
933  *    1. The calculation result is consistent with the vector.
934  */
935 /* BEGIN_CASE */
SDV_CRYPTO_BN_MOD_FUNC_TC001(int sign1,Hex * hex1,int sign2,Hex * modulo,Hex * result)936 void SDV_CRYPTO_BN_MOD_FUNC_TC001(int sign1, Hex *hex1, int sign2, Hex *modulo, Hex *result)
937 {
938     TestMemInit();
939     BN_BigNum *r = NULL;
940     BN_Optimizer *opt = NULL;
941 
942     BN_BigNum *res = TEST_VectorToBN(0, result->x, result->len);
943     BN_BigNum *a = TEST_VectorToBN(sign1, hex1->x, hex1->len);
944     BN_BigNum *m = TEST_VectorToBN(sign2, modulo->x, modulo->len);
945     ASSERT_TRUE(a != NULL && m != NULL && res != NULL);
946 
947     r = BN_Create(BN_Bits(a) + BN_Bits(m) + BIGNUM_REDUNDANCY_BITS);
948     ASSERT_TRUE(r != NULL);
949 
950     opt = BN_OptimizerCreate();
951     ASSERT_TRUE(opt != NULL);
952 
953     ASSERT_EQ(BN_Copy(r, a), CRYPT_SUCCESS);
954     ASSERT_EQ(BN_Mod(r, a, m, opt), CRYPT_SUCCESS);
955     ASSERT_TRUE_AND_LOG("r != a", BN_Cmp(r, res) == 0);
956 
957     ASSERT_EQ(BN_Copy(r, a), CRYPT_SUCCESS);
958     ASSERT_EQ(BN_Mod(r, r, m, opt), CRYPT_SUCCESS);
959     ASSERT_TRUE_AND_LOG("r == a", BN_Cmp(r, res) == 0);
960 
961     ASSERT_EQ(BN_Copy(r, m), CRYPT_SUCCESS);
962     ASSERT_EQ(BN_Mod(r, a, r, opt), CRYPT_SUCCESS);
963     ASSERT_TRUE_AND_LOG("r == m", BN_Cmp(r, res) == 0);
964 
965     /* Test the scenario where the output parameter space is insufficient. */
966     BN_Destroy(r);
967     r = BN_Create(0);
968     ASSERT_TRUE(r != NULL);
969 
970     ASSERT_EQ(BN_Mod(r, a, m, opt), CRYPT_SUCCESS);
971     ASSERT_TRUE_AND_LOG("r != a", BN_Cmp(r, res) == 0);
972 
973     ASSERT_EQ(BN_Copy(r, m), CRYPT_SUCCESS);
974     ASSERT_EQ(BN_Mod(r, a, r, opt), CRYPT_SUCCESS);
975     ASSERT_TRUE_AND_LOG("r == m", BN_Cmp(r, res) == 0);
976 
977     BN_Destroy(r);
978     r = BN_Create(0);
979     ASSERT_TRUE(r != NULL);
980 
981     ASSERT_EQ(BN_Copy(r, a), CRYPT_SUCCESS);
982     ASSERT_EQ(BN_Mod(r, r, m, opt), CRYPT_SUCCESS);
983     ASSERT_TRUE_AND_LOG("r == a", BN_Cmp(r, res) == 0);
984 EXIT:
985     BN_Destroy(a);
986     BN_Destroy(m);
987     BN_Destroy(r);
988     BN_Destroy(res);
989     BN_OptimizerDestroy(opt);
990 }
991 /* END_CASE */
992 
993 /**
994  * @test   SDV_CRYPTO_BN_PRIMECHECK_API_TC001
995  * @title  BN_PrimeCheck: Invalid parameter.
996  * @precon nan
997  * @brief
998  *    1. Create BN_BigNum bn, set the value of bn to 10, expected result 1
999  *    2. Call the BN_PrimeCheck to check bn, expected result 2
1000  * @expect
1001  *    1. CRYPT_SUCCESS
1002  *    1. CRYPT_BN_NOR_CHECK_PRIME
1003  */
1004 /* BEGIN_CASE */
SDV_CRYPTO_BN_PRIMECHECK_API_TC001(void)1005 void SDV_CRYPTO_BN_PRIMECHECK_API_TC001(void)
1006 {
1007     TestMemInit();
1008     BN_BigNum *bn = BN_Create(LONG_BN_BITS_256);
1009     BN_Optimizer *opt = BN_OptimizerCreate();
1010 
1011     ASSERT_TRUE(BN_SetLimb(bn, 10) == CRYPT_SUCCESS);  // bn == 10
1012     ASSERT_TRUE(BN_PrimeCheck(bn, 0, opt, NULL) == CRYPT_BN_NOR_CHECK_PRIME);
1013 
1014 EXIT:
1015     BN_Destroy(bn);
1016     BN_OptimizerDestroy(opt);
1017 }
1018 /* END_CASE */
1019 
1020 /**
1021  * @test   SDV_CRYPTO_BN_PRIME_CHECK_FUNC_TC001
1022  * @title  BN_PrimeCheck method test.
1023  * @precon A big number and information about whether the big number is prime.
1024  * @brief
1025  *    1. Convert hex to bn.
1026  *    2. Init the drbg.
1027  *    3. Call the BN_PrimeCheck method to check whether the big number is prime, expected result 1
1028  * @expect
1029  *    1. Return CRYPT_SUCCESS on isPrime != 0, CRYPT_BN_NOR_CHECK_PRIME otherwise.
1030  */
1031 /* BEGIN_CASE */
SDV_CRYPTO_BN_PRIME_CHECK_FUNC_TC001(Hex * hex,int isPrime)1032 void SDV_CRYPTO_BN_PRIME_CHECK_FUNC_TC001(Hex *hex, int isPrime)
1033 {
1034 #ifndef HITLS_CRYPTO_DRBG
1035     (void)hex;
1036     (void)isPrime;
1037     SKIP_TEST();
1038 #else
1039     TestMemInit();
1040     int32_t ret;
1041     BN_BigNum *bn = BN_Create(hex->len * BITS_OF_BYTE);
1042     BN_Optimizer *opt = BN_OptimizerCreate();
1043     ASSERT_TRUE(bn != NULL && opt != NULL);
1044 
1045     ASSERT_EQ(BN_Bin2Bn(bn, hex->x, hex->len), CRYPT_SUCCESS);
1046 
1047     ASSERT_EQ(TestRandInit(), CRYPT_SUCCESS);
1048     ret = BN_PrimeCheck(bn, 0, opt, NULL);
1049     if (isPrime != 0) {
1050         ASSERT_EQ(ret, CRYPT_SUCCESS);
1051     } else {
1052         ASSERT_EQ(ret, CRYPT_BN_NOR_CHECK_PRIME);
1053     }
1054 
1055 EXIT:
1056     TestRandDeInit();
1057     BN_Destroy(bn);
1058     BN_OptimizerDestroy(opt);
1059 #endif
1060 }
1061 /* END_CASE */
1062 
PrimeGenCb(BN_CbCtx * callBack,int32_t process,int32_t target)1063 static int32_t PrimeGenCb(BN_CbCtx *callBack, int32_t process, int32_t target)
1064 {
1065     if (callBack == NULL)
1066         return CRYPT_SUCCESS;
1067 
1068     int32_t *limit = BN_CbCtxGetArg(callBack);
1069     if (process < *limit) {
1070         return CRYPT_SUCCESS;
1071     }
1072     (void)target;
1073     printf("now try tims is %d, gen failed\n", process);
1074     return -1;
1075 }
1076 
1077 /**
1078  * @test   SDV_CRYPTO_BN_GENPRIMELIMB_API_TC001
1079  * @title  BN_GenPrime method test.
1080  * @precon nan
1081  * @brief
1082  *    1. Create BN_CbCtx and call the BN_CbCtxSet method to register BN_CallBack method.
1083  *    2. Init the drbg.
1084  *    3. Call the BN_GenPrime method to generate prime, bits is LONG_BN_BITS_256, half is false, expected result 1
1085  *    4. Call the BN_GenPrime method to generate prime, bits < 14, half is true, expected result 2
1086  *    5. Call the BN_GenPrime method to generate prime, bits is 10, expected result 3
1087  * @expect
1088  *    1-3. CRYPT_SUCCESS
1089  */
1090 /* BEGIN_CASE */
SDV_CRYPTO_BN_GENPRIMELIMB_API_TC001(void)1091 void SDV_CRYPTO_BN_GENPRIMELIMB_API_TC001(void)
1092 {
1093     TestMemInit();
1094     BN_Optimizer *opt = BN_OptimizerCreate();
1095     ASSERT_TRUE(opt != NULL);
1096     BN_CbCtx *cb = BN_CbCtxCreate();
1097     ASSERT_TRUE(cb != NULL);
1098     int32_t limit = 256;
1099     BN_CbCtxSet(cb, PrimeGenCb, &limit);
1100     const uint32_t bits = LONG_BN_BITS_256;
1101     bool half = false;
1102     BN_BigNum *r = BN_Create(LONG_BN_BITS_256);
1103     ASSERT_TRUE(r != NULL);
1104 
1105     CRYPT_RandRegist(TEST_Random);
1106     CRYPT_RandRegistEx(TEST_RandomEx);
1107 
1108     ASSERT_TRUE(BN_GenPrime(r, NULL, bits, half, opt, cb) == CRYPT_SUCCESS);
1109 
1110     ASSERT_TRUE(BN_GenPrime(r, NULL, 13, true, opt, cb) == CRYPT_SUCCESS);
1111 
1112     ASSERT_TRUE(BN_GenPrime(r, NULL, 10, half, opt, cb) == CRYPT_SUCCESS);
1113 
1114 EXIT:
1115     BN_CbCtxDestroy(cb);
1116     BN_Destroy(r);
1117     BN_OptimizerDestroy(opt);
1118 }
1119 /* END_CASE */
1120 
1121 /**
1122  * @test   SDV_CRYPTO_BN_ADDLIMB_FUNC_TC001
1123  * @title  BN_AddLimb method test.
1124  * @precon nan
1125  * @brief
1126  *    1. Convert vectors to big numbers.
1127  *    2. Call the BN_AddLimb method to calculate the sum of two numbers, expected result 1.
1128  *    3. If expectRet=CRYPT_SUCCESS, call the BN_Cmp to compare r and resHex, expected result 3.
1129  * @expect
1130  *    1. The return value is the same as 'expectRet'.
1131  *    2. The calculated sum is 0.
1132  *    3. Both are the same.
1133  */
1134 /* BEGIN_CASE */
SDV_CRYPTO_BN_ADDLIMB_FUNC_TC001(int sign,Hex * rHex,int limb,int resSign,Hex * resHex,int expectRet)1135 void SDV_CRYPTO_BN_ADDLIMB_FUNC_TC001(int sign, Hex *rHex, int limb, int resSign, Hex *resHex, int expectRet)
1136 {
1137     TestMemInit();
1138     BN_BigNum *resBn = NULL;
1139     BN_BigNum *a = TEST_VectorToBN(sign, rHex->x, rHex->len);
1140     BN_BigNum *r = TEST_VectorToBN(resSign, resHex->x, resHex->len);
1141     /* a and r can be null. */
1142 
1143     resBn = BN_Create(SHORT_BN_BITS_128);
1144     ASSERT_TRUE(resBn != NULL);
1145 
1146     ASSERT_EQ(BN_AddLimb(resBn, a, limb), expectRet);
1147     if (expectRet == CRYPT_SUCCESS) {
1148         ASSERT_EQ(BN_Cmp(resBn, r), 0);
1149     }
1150 
1151 EXIT:
1152     BN_Destroy(a);
1153     BN_Destroy(r);
1154     BN_Destroy(resBn);
1155 }
1156 /* END_CASE */
1157 
1158 /**
1159  * @test   SDV_CRYPTO_BN_SUB_FUNC_TC001
1160  * @title  BN_Sub test.
1161  * @precon Vectors: hex1 - hex2 = result
1162  * @brief
1163  *    1. Convert vectors to big numbers.
1164  *    2. Call BN_Sub to calculate hex1 minus hex2, and compared the result with vector, expected result 1:
1165  *       (1) Pointer addresses for parameters are different from each other.
1166  *       (2) The address of the output parameter pointer r is the same as that of the input parameter pointer a.
1167  *       (3) The address of the output parameter pointer r is the same as that of the input parameter pointer b.
1168  *    3. Test the scenario where the output parameter space is insufficient, expected result 1.
1169  * @expect
1170  *    1. The calculation result is consistent with the vector.
1171  */
1172 /* BEGIN_CASE */
SDV_CRYPTO_BN_SUB_FUNC_TC001(int sign1,Hex * hex1,int sign2,Hex * hex2,int signRes,Hex * result)1173 void SDV_CRYPTO_BN_SUB_FUNC_TC001(int sign1, Hex *hex1, int sign2, Hex *hex2, int signRes, Hex *result)
1174 {
1175     TestMemInit();
1176     BN_BigNum *r = NULL;
1177     BN_BigNum *n = NULL;
1178     uint32_t maxBits;
1179 
1180     BN_BigNum *res = TEST_VectorToBN(signRes, result->x, result->len);
1181     BN_BigNum *a = TEST_VectorToBN(sign1, hex1->x, hex1->len);
1182     BN_BigNum *b = TEST_VectorToBN(sign2, hex2->x, hex2->len);
1183     ASSERT_TRUE(res != NULL && a != NULL && b != NULL);
1184 
1185     maxBits = TEST_GetMax(BN_Bits(a), BN_Bits(b), 0);
1186     r = BN_Create(maxBits + BIGNUM_REDUNDANCY_BITS);
1187     ASSERT_TRUE(r != NULL);
1188 
1189     ASSERT_EQ(BN_Copy(r, a), CRYPT_SUCCESS);
1190     ASSERT_EQ(BN_Sub(r, a, b), CRYPT_SUCCESS);
1191     ASSERT_TRUE_AND_LOG("r != a", BN_Cmp(r, res) == 0);
1192 
1193     ASSERT_EQ(BN_Copy(r, a), CRYPT_SUCCESS);
1194     ASSERT_EQ(BN_Sub(r, r, b), CRYPT_SUCCESS);
1195     ASSERT_TRUE_AND_LOG("r == a", BN_Cmp(r, res) == 0);
1196 
1197     ASSERT_EQ(BN_Copy(r, b), CRYPT_SUCCESS);
1198     ASSERT_EQ(BN_Sub(r, a, r), CRYPT_SUCCESS);
1199     ASSERT_TRUE_AND_LOG("r == b", BN_Cmp(r, res) == 0);
1200 
1201     /* Test the scenario where the output parameter space is insufficient. */
1202     n = BN_Create(0);
1203     ASSERT_TRUE(n != NULL);
1204 
1205     ASSERT_EQ(BN_Sub(n, a, b), CRYPT_SUCCESS);
1206     ASSERT_TRUE_AND_LOG("n != a", BN_Cmp(n, res) == 0);
1207 
1208     ASSERT_EQ(BN_Sub(a, a, b), CRYPT_SUCCESS);
1209     ASSERT_TRUE_AND_LOG("a == a", BN_Cmp(a, res) == 0);
1210 
1211 EXIT:
1212     BN_Destroy(a);
1213     BN_Destroy(b);
1214     BN_Destroy(r);
1215     BN_Destroy(n);
1216     BN_Destroy(res);
1217 }
1218 /* END_CASE */
1219 
1220 /**
1221  * @test   SDV_CRYPTO_BN_SUBLIMB_API_TC001
1222  * @title  BN_SubLimb method test.
1223  * @precon nan
1224  * @brief
1225  *    1. Create BN_BigNum r and a.
1226  *    2. Call the BN_SubLimb method, parameters are null, expected result 1
1227  *    3. Call the BN_AddLimb method to subtract a from 2, expected result 2
1228  *    4. Call the BN_AddLimb method to subtract a from 1, expected result 3
1229  *    5. Call the BN_AddLimb method to subtract a from 0, expected result 4
1230  * @expect
1231  *    1. CRYPT_NULL_INPUT
1232  *    2. CRYPT_SUCCESS
1233  */
1234 /* BEGIN_CASE */
SDV_CRYPTO_BN_SUBLIMB_API_TC001(void)1235 void SDV_CRYPTO_BN_SUBLIMB_API_TC001(void)
1236 {
1237     BN_BigNum *r = NULL;
1238     BN_BigNum *a = NULL;
1239 
1240     TestMemInit();
1241 
1242     r = BN_Create(LONG_BN_BITS_256);
1243     a = BN_Create(LONG_BN_BITS_256);
1244     ASSERT_TRUE(r != NULL && a != NULL);
1245 
1246     ASSERT_TRUE(BN_SubLimb(NULL, NULL, 0) == CRYPT_NULL_INPUT);
1247 
1248     ASSERT_TRUE(BN_SubLimb(r, a, 2) == CRYPT_SUCCESS);  // r == LONG_BN_BITS_256 - 2
1249 
1250     ASSERT_TRUE(BN_SetBit(a, 1) == CRYPT_SUCCESS);  // a->size == 1
1251     ASSERT_TRUE(BN_SetSign(a, true) == CRYPT_SUCCESS);
1252     ASSERT_TRUE(BN_SubLimb(r, a, 1) == CRYPT_SUCCESS);  // r == LONG_BN_BITS_256 - 1
1253 
1254     ASSERT_TRUE(BN_SetBit(a, SHORT_BN_BITS_128 - 1) == CRYPT_SUCCESS);
1255     ASSERT_TRUE(BN_SubLimb(r, a, 0) == CRYPT_SUCCESS);
1256 
1257     ASSERT_TRUE(BN_SetBit(a, 0) == CRYPT_SUCCESS);
1258     ASSERT_TRUE(BN_SubLimb(r, a, 0) == CRYPT_SUCCESS);
1259 
1260     BN_Destroy(a);
1261     a = NULL;
1262     a = BN_Create(0);
1263     ASSERT_TRUE(a != NULL);
1264     ASSERT_TRUE(BN_SubLimb(r, a, 10) == CRYPT_SUCCESS);
1265 
1266 EXIT:
1267     BN_Destroy(r);
1268     BN_Destroy(a);
1269 }
1270 /* END_CASE */
1271 
1272 /**
1273  * @test   SDV_CRYPTO_BN_SUB_LIMB_FUNC_TC001
1274  * @title  BN_SubLimb test.
1275  * @precon Vectors: hex1 - hex2 = result
1276  * @brief
1277  *    1. Convert vectors to big numbers.
1278  *    2. Call BN_SubLimb to calculate hex1 minus hex2, and compared the result with vector, expected result 1.
1279  *    3. Test the scenario where the output parameter space is insufficient, expected result 1.
1280  * @expect
1281  *    1. The calculation result is consistent with the vector.
1282  */
1283 /* BEGIN_CASE */
SDV_CRYPTO_BN_SUB_LIMB_FUNC_TC001(int sign1,Hex * hex1,Hex * hex2,int signRes,Hex * result)1284 void SDV_CRYPTO_BN_SUB_LIMB_FUNC_TC001(int sign1, Hex *hex1, Hex *hex2, int signRes, Hex *result)
1285 {
1286     TestMemInit();
1287     BN_BigNum *r = NULL;
1288     BN_BigNum *n = NULL;
1289     BN_UINT w = 0;
1290 
1291     BN_BigNum *res = TEST_VectorToBN(signRes, result->x, result->len);
1292     BN_BigNum *a = TEST_VectorToBN(sign1, hex1->x, hex1->len);
1293     ASSERT_TRUE(res != NULL && a != NULL);
1294 
1295     // w
1296     ASSERT_TRUE(sizeof(BN_UINT) >= hex2->len);
1297     for (uint32_t i = 0; i < hex2->len; i++) {
1298         w <<= BITS_OF_BYTE;
1299         w += hex2->x[i];
1300     }
1301 
1302     // r
1303     r = BN_Create(BN_Bits(a) + BIGNUM_REDUNDANCY_BITS);
1304     ASSERT_TRUE(r != NULL);
1305 
1306     ASSERT_EQ(BN_Copy(r, a), CRYPT_SUCCESS);
1307 
1308     ASSERT_EQ(BN_SubLimb(r, a, w), CRYPT_SUCCESS);
1309 
1310     ASSERT_TRUE(BN_Cmp(r, res) == 0);
1311 
1312     /* Test the scenario where the output parameter space is insufficient. */
1313     n = BN_Create(0);
1314     ASSERT_TRUE(n != NULL);
1315 
1316     ASSERT_EQ(BN_Zeroize(a), CRYPT_SUCCESS);
1317 
1318     ASSERT_TRUE(BN_SubLimb(n, a, w) == CRYPT_SUCCESS);
1319 
1320     ASSERT_TRUE(BN_SetLimb(res, w) == CRYPT_SUCCESS);
1321     if (w != 0) {
1322         res->sign = true;
1323     }
1324     ASSERT_TRUE(BN_Cmp(n, res) == 0);
1325 
1326 EXIT:
1327     BN_Destroy(a);
1328     BN_Destroy(r);
1329     BN_Destroy(n);
1330     BN_Destroy(res);
1331 }
1332 /* END_CASE */
1333 
1334 /**
1335  * @test   SDV_CRYPTO_BN_DIV_FUNC_TC001
1336  * @title  BN_Div test.
1337  * @precon Vectors: hex1 / hex2 = resultQ, hex1 % hex2 = resultR
1338  * @brief
1339  *    1. Convert vectors to big numbers.
1340  *    2. Call BN_Div method, and compared the result with vector, expected result 1:
1341  *       (1) Pointer addresses for parameters are different from each other.
1342  *       (2) q == x, r == y.
1343  *    3. Test the scenario where the output parameter space is insufficient, expected result 1.
1344  * @expect
1345  *    1. The calculation result is consistent with the vector.
1346  */
1347 /* BEGIN_CASE */
SDV_CRYPTO_BN_DIV_FUNC_TC001(int sign1,Hex * hex1,int sign2,Hex * hex2,int signQ,Hex * resultQ,int signR,Hex * resultR)1348 void SDV_CRYPTO_BN_DIV_FUNC_TC001(
1349     int sign1, Hex *hex1, int sign2, Hex *hex2, int signQ, Hex *resultQ, int signR, Hex *resultR)
1350 {
1351     TestMemInit();
1352     BN_BigNum *q = NULL;
1353     BN_BigNum *r = NULL;
1354     BN_BigNum *q1 = NULL;
1355     BN_BigNum *n = NULL;
1356     BN_Optimizer *opt = NULL;
1357     uint32_t maxBits;
1358 
1359     BN_BigNum *resQ = TEST_VectorToBN(signQ, resultQ->x, resultQ->len);
1360     BN_BigNum *resR = TEST_VectorToBN(signR, resultR->x, resultR->len);
1361     BN_BigNum *x = TEST_VectorToBN(sign1, hex1->x, hex1->len);
1362     BN_BigNum *y = TEST_VectorToBN(sign2, hex2->x, hex2->len);
1363     ASSERT_TRUE(resQ != NULL && resR != NULL && x != NULL && y != NULL);
1364 
1365     maxBits = TEST_GetMax(BN_Bits(x), BN_Bits(y), 0);
1366     // q
1367     q = BN_Create(maxBits + BIGNUM_REDUNDANCY_BITS);
1368     r = BN_Create(maxBits + BIGNUM_REDUNDANCY_BITS);
1369     ASSERT_TRUE(q != NULL && r != NULL);
1370 
1371     opt = BN_OptimizerCreate();
1372     ASSERT_TRUE(opt != NULL);
1373 
1374     ASSERT_EQ(BN_Copy(q, x), CRYPT_SUCCESS);
1375     ASSERT_EQ(BN_Copy(r, y), CRYPT_SUCCESS);
1376     ASSERT_EQ(BN_Div(q, r, x, y, opt), CRYPT_SUCCESS);
1377     ASSERT_TRUE_AND_LOG("q != x, r != y", BN_Cmp(q, resQ) == 0);
1378     ASSERT_TRUE_AND_LOG("q != x, r != y", BN_Cmp(r, resR) == 0);
1379 
1380     ASSERT_EQ(BN_Copy(q, x), CRYPT_SUCCESS);
1381     ASSERT_EQ(BN_Copy(r, y), CRYPT_SUCCESS);
1382     ASSERT_EQ(BN_Div(q, r, q, r, opt), CRYPT_SUCCESS);
1383     ASSERT_TRUE_AND_LOG("q == x, r == y", BN_Cmp(q, resQ) == 0);
1384     ASSERT_TRUE_AND_LOG("q == x, r == y", BN_Cmp(r, resR) == 0);
1385 
1386     /* Test the scenario where the output parameter space is insufficient. */
1387     n = BN_Create(0);
1388     q1 = BN_Create(0);
1389     ASSERT_TRUE(n != NULL);
1390     ASSERT_TRUE(q1 != NULL);
1391 
1392     ASSERT_EQ(BN_Div(q1, n, x, y, opt), CRYPT_SUCCESS);
1393     ASSERT_TRUE_AND_LOG("q1 != x, n != y", BN_Cmp(q1, resQ) == 0);
1394     ASSERT_TRUE_AND_LOG("q1 != x, n != y", BN_Cmp(n, resR) == 0);
1395 EXIT:
1396     BN_Destroy(x);
1397     BN_Destroy(y);
1398     BN_Destroy(q);
1399     BN_Destroy(r);
1400     BN_Destroy(n);
1401     BN_Destroy(q1);
1402     BN_Destroy(resQ);
1403     BN_Destroy(resR);
1404     BN_OptimizerDestroy(opt);
1405 }
1406 /* END_CASE */
1407 
1408 /**
1409  * @test   SDV_CRYPTO_BN_SQR_API_TC001
1410  * @title  BN_Sqr method test.
1411  * @precon nan
1412  * @brief
1413  *    1. Call the BN_Sqr method, parameters are null, expected result 1
1414  *    2. Call the BN_Sqr method, parameters are valid, expected result 2
1415  * @expect
1416  *    1. CRYPT_NULL_INPUT
1417  *    2. CRYPT_SUCCESS
1418  */
1419 /* BEGIN_CASE */
SDV_CRYPTO_BN_SQR_API_TC001(void)1420 void SDV_CRYPTO_BN_SQR_API_TC001(void)
1421 {
1422     uint8_t buff[LONG_BN_BYTES_32];
1423     BN_BigNum *a = NULL;
1424     BN_BigNum *r = NULL;
1425     BN_BigNum *zero = NULL;
1426     BN_Optimizer *opt = NULL;
1427     TestMemInit();
1428 
1429     a = BN_Create(SHORT_BN_BITS_128);
1430     r = BN_Create(SHORT_BN_BITS_128);
1431     ASSERT_TRUE(a != NULL && r != NULL);
1432     opt = BN_OptimizerCreate();
1433     ASSERT_TRUE(opt != NULL);
1434 
1435     ASSERT_TRUE(BN_Sqr(NULL, NULL, NULL) == CRYPT_NULL_INPUT);
1436 
1437     ASSERT_TRUE(BN_Sqr(r, a, opt) == CRYPT_SUCCESS);
1438 
1439     memset_s(buff, sizeof(buff), 0xFF, LONG_BN_BYTES_32);
1440     ASSERT_TRUE(BN_Bin2Bn(a, buff, sizeof(buff)) == CRYPT_SUCCESS);
1441     ASSERT_TRUE(BN_Sqr(r, a, opt) == CRYPT_SUCCESS);
1442 
1443     ASSERT_TRUE(BN_Bin2Bn(a, buff, sizeof(buff) / 2) == CRYPT_SUCCESS);
1444     ASSERT_TRUE(BN_Sqr(r, a, opt) == CRYPT_SUCCESS);
1445 
1446 EXIT:
1447     BN_Destroy(a);
1448     BN_Destroy(r);
1449     BN_Destroy(zero);
1450     BN_OptimizerDestroy(opt);
1451 }
1452 /* END_CASE */
1453 
1454 /**
1455  * @test   SDV_CRYPTO_BN_SQR_FUNC_TC001
1456  * @title  BN_Sqr method test.
1457  * @precon nan
1458  * @brief
1459  *    1. Call the BN_Copy method, Compute (-1)*(-1) or 1*1, expected result 1
1460  *    2. Compare r and result, expected result 2
1461  * @expect
1462  *    1. CRYPT_SUCCESS
1463  *    2. r = result
1464  */
1465 /* BEGIN_CASE */
SDV_CRYPTO_BN_SQR_FUNC_TC001(int sign1,Hex * hex1,Hex * result)1466 void SDV_CRYPTO_BN_SQR_FUNC_TC001(int sign1, Hex *hex1, Hex *result)
1467 {
1468     TestMemInit();
1469     BN_BigNum *a;
1470     BN_BigNum *res = NULL;
1471     BN_BigNum *r = NULL;
1472     BN_Optimizer *opt = NULL;
1473 
1474     a = BN_Create(hex1->len * BITS_OF_BYTE);
1475     res = BN_Create(result->len * BITS_OF_BYTE);
1476     ASSERT_TRUE(a != NULL && res != NULL);
1477 
1478     ASSERT_TRUE(BN_Bin2Bn(res, result->x, result->len) == CRYPT_SUCCESS);
1479     ASSERT_TRUE(BN_Bin2Bn(a, hex1->x, hex1->len) == CRYPT_SUCCESS);
1480     ASSERT_TRUE(BN_SetSign(a, sign1 != 0) == CRYPT_SUCCESS);
1481 
1482     // r.bits > a.bits * 2
1483     r = BN_Create(BN_Bits(a) * 2 + BIGNUM_REDUNDANCY_BITS);
1484     ASSERT_TRUE(r != NULL);
1485 
1486     opt = BN_OptimizerCreate();
1487     ASSERT_TRUE(opt != NULL);
1488 
1489     ASSERT_TRUE(BN_Copy(r, a) == CRYPT_SUCCESS);
1490     ASSERT_TRUE(BN_Sqr(r, a, opt) == CRYPT_SUCCESS);
1491     ASSERT_TRUE(BN_Cmp(r, res) == 0);
1492 
1493 EXIT:
1494     BN_Destroy(a);
1495     BN_Destroy(r);
1496     BN_Destroy(res);
1497     BN_OptimizerDestroy(opt);
1498 }
1499 /* END_CASE */
1500 
1501 /**
1502  * @test   SDV_CRYPTO_BN_RAND_API_TC001
1503  * @title  BN_Rand: Invalid parameter.
1504  * @precon nan
1505  * @brief
1506  *    1. Call the BN_Rand, parameters are null and 0, expected result 1
1507  *    2. Call the BN_Rand, top is out of maximum value(BN_RAND_TOP_TWOBIT + 1), expected result 2
1508  *    3. Call the BN_Rand, bottom is out of maximum value(BN_RAND_BOTTOM_TWOBIT + 1), expected result 3
1509  *    4. Call the BN_Rand, bit is 0, top and bottom are not 0, expected result 4
1510  *    5. Call the BN_Rand, bit > BN_MAX_BITS, expected result 5
1511  *    6. Call the BN_Rand, bit, top and bottom is 0, expected result 6
1512  * @expect
1513  *    1. CRYPT_NULL_INPUT
1514  *    2-3. CRYPT_BN_ERR_RAND_TOP_BOTTOM
1515  *    4. CRYPT_BN_ERR_RAND_BITS_NOT_ENOUGH
1516  *    5. CRYPT_BN_BITS_TOO_MAX
1517  *    6. CRYPT_SUCCESS
1518  */
1519 /* BEGIN_CASE */
SDV_CRYPTO_BN_RAND_API_TC001(void)1520 void SDV_CRYPTO_BN_RAND_API_TC001(void)
1521 {
1522 #ifndef HITLS_CRYPTO_DRBG
1523     SKIP_TEST();
1524 #else
1525     BN_BigNum *bn = NULL;
1526     TestMemInit();
1527     bn = BN_Create(BITS_OF_BYTE);
1528 
1529     ASSERT_TRUE(BN_Rand(NULL, 0, 0, 0) == CRYPT_NULL_INPUT);
1530     ASSERT_TRUE(
1531         BN_Rand(bn, BITS_OF_BYTE, BN_RAND_TOP_TWOBIT + 1, BN_RAND_BOTTOM_TWOBIT) == CRYPT_BN_ERR_RAND_TOP_BOTTOM);
1532     ASSERT_TRUE(
1533         BN_Rand(bn, BITS_OF_BYTE, BN_RAND_TOP_TWOBIT, BN_RAND_BOTTOM_TWOBIT + 1) == CRYPT_BN_ERR_RAND_TOP_BOTTOM);
1534     ASSERT_TRUE(BN_Rand(bn, 0, BN_RAND_TOP_TWOBIT, BN_RAND_BOTTOM_TWOBIT) == CRYPT_BN_ERR_RAND_BITS_NOT_ENOUGH);
1535 
1536     ASSERT_TRUE(BN_Rand(bn, BN_MAX_BITS + 1, BN_RAND_TOP_TWOBIT, BN_RAND_BOTTOM_TWOBIT) == CRYPT_BN_BITS_TOO_MAX);
1537 
1538     ASSERT_EQ(TestRandInit(), CRYPT_SUCCESS);
1539     ASSERT_TRUE(BN_Rand(bn, 0, 0, 0) == CRYPT_SUCCESS);
1540 EXIT:
1541     BN_Destroy(bn);
1542     return;
1543 #endif
1544 }
1545 /* END_CASE */
1546 
1547 /**
1548  * @test   SDV_CRYPTO_BN_RANDRANGE_API_TC001
1549  * @title  BN_RandRange: Invalid parameter.
1550  * @precon nan
1551  * @brief
1552  *    1. Call the BN_RandRange method, parameters are null, expected result 1
1553  *    2. Call the BN_RandRange method, parameters are 0, expected result 2
1554  *    3. Call the BN_RandRange method, parameters are -1, expected result 3
1555  * @expect
1556  *    1. CRYPT_NULL_INPUT
1557  *    2. CRYPT_BN_ERR_RAND_ZERO
1558  *    3. CRYPT_BN_ERR_RAND_NEGATIVE
1559  */
1560 /* BEGIN_CASE */
SDV_CRYPTO_BN_RANDRANGE_API_TC001(void)1561 void SDV_CRYPTO_BN_RANDRANGE_API_TC001(void)
1562 {
1563     BN_BigNum *bn = NULL;
1564     TestMemInit();
1565     bn = BN_Create(BITS_OF_BYTE);
1566 
1567     ASSERT_TRUE(BN_RandRange(NULL, NULL) == CRYPT_NULL_INPUT);
1568     ASSERT_TRUE(BN_RandRange(bn, bn) == CRYPT_BN_ERR_RAND_ZERO);
1569     ASSERT_TRUE(BN_SetLimb(bn, 1) == CRYPT_SUCCESS);
1570     ASSERT_TRUE(BN_SetSign(bn, true) == CRYPT_SUCCESS);
1571     ASSERT_TRUE(BN_RandRange(bn, bn) == CRYPT_BN_ERR_RAND_NEGATIVE);
1572 EXIT:
1573     BN_Destroy(bn);
1574     return;
1575 }
1576 /* END_CASE */
1577 
1578 /**
1579  * @test   SDV_CRYPTO_BN_BNGCDCHECKINPUT_API_TC001
1580  * @title  BnGcdCheckInput: Invalid parameter.
1581  * @precon nan
1582  * @brief
1583  *    1. Call the BnGcdCheckInput method, parameters are null, expected result 1
1584  *    2. Call the BnGcdCheckInput method, a and b are BN_MAX_BITS, expected result 2
1585  *    3. Call the BnGcdCheckInput method, a and b are 0, expected result 3
1586  * @expect
1587  *    1. CRYPT_NULL_INPUT
1588  *    2. CRYPT_SUCCESS
1589  *    3. CRYPT_BN_ERR_GCD_NO_ZERO
1590  */
1591 /* BEGIN_CASE */
SDV_CRYPTO_BN_BNGCDCHECKINPUT_API_TC001(void)1592 void SDV_CRYPTO_BN_BNGCDCHECKINPUT_API_TC001(void)
1593 {
1594     BN_BigNum *r;
1595     BN_BigNum *a;
1596     BN_BigNum *b;
1597     BN_Optimizer *opt = NULL;
1598 
1599     TestMemInit();
1600     r = BN_Create(BITS_OF_BYTE);
1601     a = BN_Create(LONG_BN_BITS_256);
1602     b = BN_Create(LONG_BN_BITS_256);
1603     opt = BN_OptimizerCreate();
1604 
1605     ASSERT_TRUE(BnGcdCheckInput(NULL, NULL, NULL, NULL) == CRYPT_NULL_INPUT);
1606     ASSERT_TRUE(BN_SetLimb(a, BN_MAX_BITS) == CRYPT_SUCCESS);
1607     ASSERT_TRUE(BN_SetLimb(b, BN_MAX_BITS) == CRYPT_SUCCESS);
1608     ASSERT_TRUE(BnGcdCheckInput(r, a, b, opt) == CRYPT_SUCCESS);
1609 
1610     ASSERT_TRUE(BN_Zeroize(a) == CRYPT_SUCCESS);
1611     ASSERT_TRUE(BN_Zeroize(b) == CRYPT_SUCCESS);
1612     ASSERT_TRUE(BnGcdCheckInput(r, a, b, opt) == CRYPT_BN_ERR_GCD_NO_ZERO);
1613 EXIT:
1614     BN_Destroy(r);
1615     BN_Destroy(a);
1616     BN_Destroy(b);
1617     BN_OptimizerDestroy(opt);
1618     return;
1619 }
1620 /* END_CASE */
1621 
1622 /**
1623  * @test   SDV_CRYPTO_BN_MODINVINPUTCHECK_API_TC001
1624  * @title  InverseInputCheck: Invalid parameter.
1625  * @precon nan
1626  * @brief
1627  *    1. Call the InverseInputCheck method, parameters are null, expected result 1
1628  *    2. Call the InverseInputCheck method, x and m are BN_MAX_BITS, expected result 2
1629  *    3. Call the InverseInputCheck method, x and m are 0, expected result 3
1630  * @expect
1631  *    1. CRYPT_NULL_INPUT
1632  *    2. CRYPT_SUCCESS
1633  *    3. CRYPT_BN_ERR_DIVISOR_ZERO
1634  */
1635 /* BEGIN_CASE */
SDV_CRYPTO_BN_MODINVINPUTCHECK_API_TC001(void)1636 void SDV_CRYPTO_BN_MODINVINPUTCHECK_API_TC001(void)
1637 {
1638     BN_BigNum *r;
1639     BN_BigNum *x;
1640     BN_BigNum *m;
1641     BN_Optimizer *opt = NULL;
1642 
1643     TestMemInit();
1644     r = BN_Create(BITS_OF_BYTE);
1645     x = BN_Create(LONG_BN_BITS_256);
1646     m = BN_Create(LONG_BN_BITS_256);
1647     opt = BN_OptimizerCreate();
1648     ASSERT_TRUE(r != NULL && x != NULL && m != NULL && opt != NULL);
1649 
1650     ASSERT_TRUE(InverseInputCheck(NULL, NULL, NULL, NULL) == CRYPT_NULL_INPUT);
1651     ASSERT_TRUE(BN_SetLimb(x, BN_MAX_BITS) == CRYPT_SUCCESS);
1652     ASSERT_TRUE(BN_SetLimb(m, BN_MAX_BITS) == CRYPT_SUCCESS);
1653     ASSERT_TRUE(InverseInputCheck(r, x, m, opt) == CRYPT_SUCCESS);
1654 
1655     ASSERT_TRUE(BN_Zeroize(x) == CRYPT_SUCCESS);
1656     ASSERT_TRUE(BN_Zeroize(m) == CRYPT_SUCCESS);
1657     ASSERT_TRUE(InverseInputCheck(r, x, m, opt) == CRYPT_BN_ERR_DIVISOR_ZERO);
1658 EXIT:
1659     BN_Destroy(r);
1660     BN_Destroy(x);
1661     BN_Destroy(m);
1662     BN_OptimizerDestroy(opt);
1663     return;
1664 }
1665 /* END_CASE */
1666 
1667 /**
1668  * @test   SDV_CRYPTO_BN_U64_FUNC_TC001
1669  * @title  BN_U64Array2Bn/BN_Bn2U64Array test.
1670  * @precon nan
1671  * @brief
1672  *    1. Randomly generate a 64-bit unsigned array.
1673  *    2. Convert the array to a big number, and then convert the big number to array, expected result 1.
1674  *    3. compare whether the arrays are the same, expected result 2
1675  * @expect
1676  *    1. CRYPT_SUCCESS
1677  *    2. The two arrays are the same.
1678  */
1679 /* BEGIN_CASE */
SDV_CRYPTO_BN_U64_FUNC_TC001(int len)1680 void SDV_CRYPTO_BN_U64_FUNC_TC001(int len)
1681 {
1682     TestMemInit();
1683     BN_BigNum *a = BN_Create(0);
1684     uint64_t *input = calloc(1, len * sizeof(uint64_t));
1685     uint64_t *output = calloc(1, len * sizeof(uint64_t));
1686     uint32_t outlen = len;
1687     for (int i = 0; i < len; i++) {
1688         input[i] = rand();
1689     }
1690     input[len - 1] = 1;
1691 
1692     ASSERT_TRUE(BN_U64Array2Bn(a, input, (uint32_t)len) == CRYPT_SUCCESS);
1693     ASSERT_TRUE(BN_Bn2U64Array(a, output, &outlen) == CRYPT_SUCCESS);
1694     ASSERT_TRUE(memcmp(input, output, outlen * sizeof(uint64_t)) == 0);
1695     input[len - 1] = 0;
1696 
1697     ASSERT_TRUE(BN_U64Array2Bn(a, input, len) == CRYPT_SUCCESS);
1698     ASSERT_TRUE(BN_Bn2U64Array(a, output, &outlen) == CRYPT_SUCCESS);
1699     ASSERT_TRUE(memcmp(input, output, outlen * sizeof(uint64_t)) == 0);
1700 EXIT:
1701     BN_Destroy(a);
1702     free(input);
1703     free(output);
1704 }
1705 /* END_CASE */
1706 
1707 /**
1708  * @test   SDV_CRYPTO_BN_UINT_FUNC_TC001
1709  * @title  BN_Array2BN/BN_BN2Array test.
1710  * @precon nan
1711  * @brief
1712  *    1. Randomly generate a BN_UINT unsigned array.
1713  *    2. Convert the array to a big number, and then convert the big number to array, expected result 1.
1714  *    3. compare whether the arrays are the same, expected result 2
1715  * @expect
1716  *    1. CRYPT_SUCCESS
1717  *    2. The two arrays are the same.
1718  */
1719 /* BEGIN_CASE */
SDV_CRYPTO_BN_UINT_FUNC_TC001(int len)1720 void SDV_CRYPTO_BN_UINT_FUNC_TC001(int len)
1721 {
1722 #if defined(HITLS_CRYPTO_CURVE_SM2_ASM) || (defined(HITLS_CRYPTO_CURVE_NISTP256_ASM) && \
1723     defined(HITLS_CRYPTO_NIST_USE_ACCEL))
1724     TestMemInit();
1725     BN_BigNum *a = BN_Create(0);
1726     BN_UINT *input = calloc(1, len * sizeof(BN_UINT));
1727     BN_UINT *output = calloc(1, len * sizeof(BN_UINT));
1728     for (int i = 0; i < len; i++) {
1729         input[i] = rand();
1730     }
1731 
1732     ASSERT_TRUE(BN_Array2BN(a, input, len) == CRYPT_SUCCESS);
1733     ASSERT_TRUE(BN_BN2Array(a, output, len) == CRYPT_SUCCESS);
1734     ASSERT_TRUE(memcmp(input, output, len * sizeof(BN_UINT)) == 0);
1735 EXIT:
1736     BN_Destroy(a);
1737     free(input);
1738     free(output);
1739 #else
1740     (void)len;
1741 #endif
1742 }
1743 /* END_CASE */
1744 
1745 /**
1746  * @test   SDV_CRYPTO_BN_GCD_FUNC_TC001
1747  * @title  BN_Gcd test.
1748  * @precon Vectors: two big numbers, result.
1749  * @brief
1750  *    1. Convert vectors to big numbers.
1751  *    2. Call BN_Gcd to calculate the modular exponentiation, and compared to the vector value, expected result 1:
1752  *       (1) Pointer addresses for parameters are different from each other.
1753  *       (2) The address of the output parameter pointer r is the same as that of the input parameter pointer a.
1754  *       (3) The address of the output parameter pointer r is the same as that of the input parameter pointer b.
1755  *    3. Test the scenario where the output parameter space is insufficient, expected result 1.
1756  * @expect
1757  *    1. The calculation result is consistent with the vector.
1758  */
1759 /* BEGIN_CASE */
SDV_CRYPTO_BN_GCD_FUNC_TC001(int sign1,Hex * hex1,int sign2,Hex * hex2,Hex * result)1760 void SDV_CRYPTO_BN_GCD_FUNC_TC001(int sign1, Hex *hex1, int sign2, Hex *hex2, Hex *result)
1761 {
1762     TestMemInit();
1763     BN_BigNum *out = NULL;
1764     BN_Optimizer *opt = NULL;
1765     uint32_t maxBits;
1766 
1767     BN_BigNum *bn = TEST_VectorToBN(sign1, hex1->x, hex1->len);
1768     BN_BigNum *bn2 = TEST_VectorToBN(sign2, hex2->x, hex2->len);
1769     BN_BigNum *res = TEST_VectorToBN(0, result->x, result->len);
1770     ASSERT_TRUE(bn != NULL && bn2 != NULL && res != NULL);
1771 
1772     maxBits = TEST_GetMax(BN_Bits(bn), BN_Bits(bn2), 0);
1773     out = BN_Create(maxBits + BIGNUM_REDUNDANCY_BITS);
1774     ASSERT_TRUE(out != NULL);
1775 
1776     opt = BN_OptimizerCreate();
1777     ASSERT_TRUE(opt != NULL);
1778 
1779     ASSERT_EQ(BN_Copy(out, bn), CRYPT_SUCCESS);
1780     ASSERT_EQ(BN_Gcd(out, bn, bn2, opt), CRYPT_SUCCESS);
1781     ASSERT_TRUE_AND_LOG("r != a", BN_Cmp(out, res) == 0);
1782 
1783     ASSERT_EQ(BN_Copy(out, bn), CRYPT_SUCCESS);
1784     ASSERT_EQ(BN_Gcd(out, out, bn2, opt), CRYPT_SUCCESS);
1785     ASSERT_TRUE_AND_LOG("r == a", BN_Cmp(out, res) == 0);
1786 
1787     ASSERT_EQ(BN_Copy(out, bn2), CRYPT_SUCCESS);
1788     ASSERT_EQ(BN_Gcd(out, bn, out, opt), CRYPT_SUCCESS);
1789     ASSERT_TRUE_AND_LOG("r == b", BN_Cmp(out, res) == 0);
1790     BN_Destroy(out);
1791 
1792     /* Test the scenario where the output parameter space is insufficient. */
1793     out = BN_Create(0);
1794     ASSERT_TRUE(out != NULL);
1795 
1796     ASSERT_EQ(BN_Gcd(out, bn, bn2, opt), CRYPT_SUCCESS);
1797     ASSERT_TRUE_AND_LOG("r != a", BN_Cmp(out, res) == 0);
1798     BN_Destroy(out);
1799 
1800     out = BN_Create(0);
1801     ASSERT_TRUE(out != NULL);
1802     ASSERT_EQ(BN_Copy(out, bn), CRYPT_SUCCESS);
1803     ASSERT_EQ(BN_Gcd(out, out, bn2, opt), CRYPT_SUCCESS);
1804     ASSERT_TRUE_AND_LOG("r == a", BN_Cmp(out, res) == 0);
1805     BN_Destroy(out);
1806 
1807     out = BN_Create(0);
1808     ASSERT_TRUE(out != NULL);
1809     ASSERT_EQ(BN_Copy(out, bn2), CRYPT_SUCCESS);
1810     ASSERT_EQ(BN_Gcd(out, bn, out, opt), CRYPT_SUCCESS);
1811     ASSERT_TRUE_AND_LOG("r == b", BN_Cmp(out, res) == 0);
1812 
1813 EXIT:
1814     BN_Destroy(bn);
1815     BN_Destroy(bn2);
1816     BN_Destroy(res);
1817     BN_Destroy(out);
1818     BN_OptimizerDestroy(opt);
1819 }
1820 /* END_CASE */
1821 
1822 /**
1823  * @test   SDV_CRYPTO_BN_CMP_FUNC_TC001
1824  * @title  BN_Cmp test.
1825  * @precon Vectors: two big numbers, result.
1826  * @brief
1827  *    1. Convert vectors to big numbers.
1828  *    2. Call the BN_Cmp method to compare two large numbers, expected result 1.
1829  * @expect
1830  *    1. The comparison results are the same as 'result'.
1831  */
1832 /* BEGIN_CASE */
SDV_CRYPTO_BN_CMP_FUNC_TC001(int sign1,Hex * hex1,int sign2,Hex * hex2,int result)1833 void SDV_CRYPTO_BN_CMP_FUNC_TC001(int sign1, Hex *hex1, int sign2, Hex *hex2, int result)
1834 {
1835     TestMemInit();
1836 
1837     BN_BigNum *bn1 = TEST_VectorToBN(sign1, hex1->x, hex1->len);
1838     BN_BigNum *bn2 = TEST_VectorToBN(sign2, hex2->x, hex2->len);
1839     /* bn1 and bn2 can be null. */
1840 
1841     ASSERT_EQ(BN_Cmp(bn1, bn2), result);
1842 
1843 EXIT:
1844     BN_Destroy(bn1);
1845     BN_Destroy(bn2);
1846 }
1847 /* END_CASE */
1848 
1849 /**
1850  * @test   SDV_CRYPTO_BN_ADD_FUNC_TC001
1851  * @title  BN_Add test.
1852  * @precon a + b = r
1853  * @brief
1854  *    1. Convert vectors to big numbers.
1855  *    2. Call the BN_Add method to calculate the sum of two large numbers, expected result 1.
1856  *    3. If expectRet=CRYPT_SUCCESS and r=null(i.e.a=-b), call BN_IsZero to check whether sum is 0, expected result 2.
1857  *    4. If expectRet=CRYPT_SUCCESS, call the BN_Cmp to compare r and resBn, expected result 3.
1858  * @expect
1859  *    1. The return value is the same as 'expectRet'.
1860  *    2. The calculated sum is 0.
1861  *    3. Both are the same.
1862  */
1863 /* BEGIN_CASE */
SDV_CRYPTO_BN_ADD_FUNC_TC001(int sign1,int sign2,int sign3,Hex * a,Hex * b,Hex * r,int expectRet)1864 void SDV_CRYPTO_BN_ADD_FUNC_TC001(int sign1, int sign2, int sign3, Hex *a, Hex *b, Hex *r, int expectRet)
1865 {
1866     TestMemInit();
1867     BN_BigNum *resBn = NULL;
1868 
1869     BN_BigNum *bn1 = TEST_VectorToBN(sign1, a->x, a->len);
1870     BN_BigNum *bn2 = TEST_VectorToBN(sign2, b->x, b->len);
1871     BN_BigNum *sum = TEST_VectorToBN(sign3, r->x, r->len);
1872     /* bn1, bn2 and sum can be null. */
1873 
1874     resBn = BN_Create(SHORT_BN_BITS_128);
1875     ASSERT_TRUE(resBn != NULL);
1876 
1877     ASSERT_EQ(BN_Add(resBn, bn1, bn2), expectRet);
1878     if (expectRet == CRYPT_SUCCESS) {
1879         if (r->len == 0) {
1880             ASSERT_EQ(BN_IsZero(resBn), 1);
1881         } else {
1882             ASSERT_EQ(BN_Cmp(resBn, sum), 0);
1883         }
1884     }
1885 
1886 EXIT:
1887     BN_Destroy(bn1);
1888     BN_Destroy(bn2);
1889     BN_Destroy(sum);
1890     BN_Destroy(resBn);
1891 }
1892 /* END_CASE */
1893 
1894 /**
1895  * @test   SDV_CRYPTO_BN_TO_BIN_FIX_ZERO_API_TC001
1896  * @title  BN_Bn2BinFixZero test.
1897  * @precon nan
1898  * @brief
1899  *    1. Call the BN_Create method to create bn, parameter is 0, expected result 1.
1900  *    2. Call the BN_Bn2BinFixZero method, expected result 2:
1901  *       (1) bn is null
1902  *       (2) bin is null
1903  *       (3) binLen is 0
1904  *    3. Call the BN_Bn2BinFixZero method, all parameters are valid, expected result 3
1905  *    4. Destroy bn and recreate bn, bits is 128, expected result 4.
1906  *    5. Set the highest bit of bn to 1, expected result 5.
1907  *    6. Call the BN_Bn2BinFixZero method, binLen is 1, expected result 6.
1908  * @expect
1909  *    1. Success.
1910  *    2. CRYPT_NULL_INPUT.
1911  *    3. CRYPT_SUCCESS.
1912  *    4. Success.
1913  *    5. CRYPT_SUCCESS.
1914  *    6. CRYPT_BN_BUFF_LEN_NOT_ENOUGH.
1915  */
1916 /* BEGIN_CASE */
SDV_CRYPTO_BN_TO_BIN_FIX_ZERO_API_TC001(void)1917 void SDV_CRYPTO_BN_TO_BIN_FIX_ZERO_API_TC001(void)
1918 {
1919     TestMemInit();
1920     uint8_t bin[1] = {0};
1921     BN_BigNum *bn = BN_Create(0);
1922     ASSERT_TRUE(bn != NULL);
1923 
1924     ASSERT_EQ(BN_Bn2BinFixZero(NULL, bin, 1), CRYPT_NULL_INPUT);
1925     ASSERT_EQ(BN_Bn2BinFixZero(bn, NULL, 1), CRYPT_NULL_INPUT);
1926     ASSERT_EQ(BN_Bn2BinFixZero(bn, bin, 0), CRYPT_NULL_INPUT);
1927 
1928     // bn bytes is 0
1929     ASSERT_EQ(BN_Bn2BinFixZero(bn, bin, 1), CRYPT_SUCCESS);
1930     BN_Destroy(bn);
1931 
1932     bn = BN_Create(SHORT_BN_BITS_128);
1933     ASSERT_TRUE(bn != NULL);
1934     ASSERT_EQ(BN_SetBit(bn, SHORT_BN_BITS_128 - 1), CRYPT_SUCCESS);
1935 
1936     ASSERT_EQ(BN_Bn2BinFixZero(bn, bin, 1), CRYPT_BN_BUFF_LEN_NOT_ENOUGH);
1937 
1938 EXIT:
1939     BN_Destroy(bn);
1940 }
1941 /* END_CASE */
1942 
1943 /* BEGIN_CASE */
SDV_CRYPTO_BN_Rand_API_TC001(void)1944 void SDV_CRYPTO_BN_Rand_API_TC001(void)
1945 {
1946     TEST_BnTestCaseInit();
1947     BN_BigNum bn[2] = {{0}};
1948     BN_UINT bn_data[DH_BN_DIGITS_MAX * 2] = { 0 };
1949     BN_Init(bn, bn_data, DH_BN_DIGITS_MAX, 2);
1950     ASSERT_TRUE(BN_Rand(&bn[0], 8192, 1, 1) == CRYPT_SUCCESS);
1951     ASSERT_TRUE(BN_RandRange(&bn[1], &bn[0]) == CRYPT_SUCCESS);
1952 EXIT:
1953     return;
1954 }
1955 /* END_CASE */
1956 
1957 /* BEGIN_CASE */
SDV_CRYPTO_BN_Add_API_TC001(void)1958 void SDV_CRYPTO_BN_Add_API_TC001(void)
1959 {
1960     TEST_BnTestCaseInit();
1961     BN_BigNum bn[3] = {{0}};
1962     BN_UINT bn_data[BN_DIGITS_MAX * 3] = { 0 };
1963     BN_Init(bn, bn_data, BN_DIGITS_MAX, 3);
1964     ASSERT_TRUE(BN_Rand(&bn[0], 4096, 1, 1) == CRYPT_SUCCESS);
1965     ASSERT_TRUE(BN_Rand(&bn[1], 4096, 1, 1) == CRYPT_SUCCESS);
1966     ASSERT_TRUE(BN_Add(&bn[2], &bn[0], &bn[1]) == CRYPT_SUCCESS);
1967 EXIT:
1968     return;
1969 }
1970 /* END_CASE */
1971 
1972 
1973 /* BEGIN_CASE */
SDV_CRYPTO_BN_Mul_API_TC001(void)1974 void SDV_CRYPTO_BN_Mul_API_TC001(void)
1975 {
1976     TEST_BnTestCaseInit();
1977     BN_Optimizer *opt = BN_OptimizerCreate();
1978     BN_BigNum bn[2] = {{0}};
1979     BN_BigNum rn = {0};
1980     BN_UINT bn_data[BN_DIGITS_MAX * 2] = { 0 };
1981     BN_UINT r_data[DH_BN_DIGITS_MAX * 2] = { 0 };
1982     BN_Init(bn, bn_data, BN_DIGITS_MAX, 2);
1983     ASSERT_TRUE(BN_Rand(&bn[0], 4096, 1, 1) == CRYPT_SUCCESS);
1984     ASSERT_TRUE(BN_Rand(&bn[1], 4096, 1, 1) == CRYPT_SUCCESS);
1985     BN_Init(&rn, r_data, DH_BN_DIGITS_MAX, 1);
1986     ASSERT_TRUE(BN_Mul(&rn, &bn[0], &bn[1], opt) == CRYPT_SUCCESS);
1987 EXIT:
1988     BN_OptimizerDestroy(opt);
1989     return;
1990 }
1991 /* END_CASE */
1992 
1993 
1994 /* BEGIN_CASE */
SDV_CRYPTO_BN_Div_API_TC001(void)1995 void SDV_CRYPTO_BN_Div_API_TC001(void)
1996 {
1997     TEST_BnTestCaseInit();
1998     BN_Optimizer *opt = BN_OptimizerCreate();
1999 
2000     BN_BigNum bn[4] = {{0}};
2001     BN_UINT bn_data[BN_DIGITS_MAX * 4] = { 0 };
2002     BN_Init(bn, bn_data, BN_DIGITS_MAX, 4);
2003     ASSERT_TRUE(BN_Rand(&bn[2], 4096, 1, 1) == CRYPT_SUCCESS);
2004     ASSERT_TRUE(BN_Rand(&bn[3], 4096, 1, 1) == CRYPT_SUCCESS);
2005     ASSERT_TRUE(BN_Div(&bn[0], &bn[1], &bn[2], &bn[3], opt) == CRYPT_SUCCESS);
2006 EXIT:
2007     BN_OptimizerDestroy(opt);
2008     return;
2009 }
2010 /* END_CASE */
2011 
2012 /* BEGIN_CASE */
SDV_CRYPTO_BN_Mod_API_TC001(void)2013 void SDV_CRYPTO_BN_Mod_API_TC001(void)
2014 {
2015     TEST_BnTestCaseInit();
2016 
2017     BN_Optimizer *opt = BN_OptimizerCreate();
2018     BN_UINT res = 0;
2019     BN_UINT input = 3;
2020     BN_BigNum bn[4] = {{0}};
2021     BN_UINT bn_data[BN_DIGITS_MAX * 4] = { 0 };
2022     BN_Init(bn, bn_data, BN_DIGITS_MAX, 4);
2023     ASSERT_TRUE(BN_Rand(&bn[1], 4096, 1, 1) == CRYPT_SUCCESS);
2024     ASSERT_TRUE(BN_Rand(&bn[2], 4096, 1, 1) == CRYPT_SUCCESS);
2025     ASSERT_TRUE(BN_Rand(&bn[3], 4096, 1, 1) == CRYPT_SUCCESS);
2026     ASSERT_TRUE(BN_ModMul(&bn[0], &bn[1], &bn[2], &bn[3], opt) == CRYPT_SUCCESS);
2027     ASSERT_TRUE(BN_ModSqr(&bn[0], &bn[1], &bn[2], opt) == CRYPT_SUCCESS);
2028     ASSERT_TRUE(BN_ModAdd(&bn[0], &bn[1], &bn[2], &bn[3], opt) == CRYPT_SUCCESS);
2029     ASSERT_TRUE(BN_ModLimb(&res, &bn[1], input) == CRYPT_SUCCESS);
2030     ASSERT_TRUE(BN_AddLimb(&bn[0], &bn[1], input) == CRYPT_SUCCESS);
2031     ASSERT_TRUE(BN_SetLimb(&bn[0], input) == CRYPT_SUCCESS);
2032     ASSERT_TRUE(BN_Cmp(&bn[1], &bn[2]) != CRYPT_SUCCESS);
2033 
2034 EXIT:
2035     BN_OptimizerDestroy(opt);
2036     return;
2037 }
2038 /* END_CASE */
2039 
2040 /* BEGIN_CASE */
SDV_CRYPTO_BN_ModLimb_API_TC001(void)2041 void SDV_CRYPTO_BN_ModLimb_API_TC001(void)
2042 {
2043     TEST_BnTestCaseInit();
2044     BN_Optimizer *opt = BN_OptimizerCreate();
2045     ASSERT_TRUE(opt != NULL);
2046     BN_UINT res = 0;
2047     BN_UINT input = 3;
2048     BN_BigNum bn[2] = {{0}};
2049     BN_UINT bn_data[DH_BN_DIGITS_MAX * 2] = { 0 };
2050     BN_Init(bn, bn_data, DH_BN_DIGITS_MAX, 2);
2051     ASSERT_TRUE(BN_ModLimb(&res, &bn[1], input) == CRYPT_SUCCESS);
2052 EXIT:
2053     BN_OptimizerDestroy(opt);
2054     return;
2055 }
2056 /* END_CASE */
2057 
2058 /* BEGIN_CASE */
SDV_CRYPTO_BN_rshift_API_TC001(void)2059 void SDV_CRYPTO_BN_rshift_API_TC001(void)
2060 {
2061     TEST_BnTestCaseInit();
2062     BN_UINT input = 3;
2063     BN_BigNum bn[2] = {{0}};
2064     BN_UINT bn_data[DH_BN_DIGITS_MAX * 2] = { 0 };
2065     BN_Init(bn, bn_data, DH_BN_DIGITS_MAX, 2);
2066     ASSERT_TRUE(BN_Rand(&bn[1], 4096, 1, 1) == CRYPT_SUCCESS);
2067     ASSERT_TRUE(BN_Rshift(&bn[0], &bn[1], input) == CRYPT_SUCCESS);
2068 EXIT:
2069     return;
2070 }
2071 /* END_CASE */
2072 
2073 /* BEGIN_CASE */
SDV_CRYPTO_BN_ModExp_API_TC001(void)2074 void SDV_CRYPTO_BN_ModExp_API_TC001(void)
2075 {
2076     TEST_BnTestCaseInit();
2077     BN_Optimizer *opt = BN_OptimizerCreate();
2078     ASSERT_TRUE(opt != NULL);
2079     BN_BigNum bn[4] = {{0}};
2080     BN_UINT bn_data[DH_BN_DIGITS_MAX * 4] = { 0 };
2081     BN_Init(bn, bn_data, DH_BN_DIGITS_MAX, 4);
2082     ASSERT_TRUE(BN_Rand(&bn[1], 8192, 1, 1) == CRYPT_SUCCESS);
2083     ASSERT_TRUE(BN_Rand(&bn[2], 8192, 1, 1) == CRYPT_SUCCESS);
2084     ASSERT_TRUE(BN_Rand(&bn[3], 8192, 1, 1) == CRYPT_SUCCESS);
2085     ASSERT_TRUE(BN_ModExp(&bn[0], &bn[1], &bn[2], &bn[3], opt) == CRYPT_SUCCESS);
2086 EXIT:
2087     BN_OptimizerDestroy(opt);
2088     return;
2089 }
2090 /* END_CASE */
2091 
2092 /* BEGIN_CASE */
SDV_CRYPTO_BN_SET_FLAG_TC001(void)2093 void SDV_CRYPTO_BN_SET_FLAG_TC001(void)
2094 {
2095     TEST_BnTestCaseInit();
2096     BN_BigNum a = {0};
2097     ASSERT_TRUE(BN_SetFlag(&a, 0xFF) == CRYPT_BN_FLAG_INVALID);
2098     ASSERT_TRUE(BN_SetFlag(&a, CRYPT_BN_FLAG_STATIC) == CRYPT_SUCCESS);
2099 EXIT:
2100     return;
2101 }
2102 /* END_CASE */
2103 
2104 /* BEGIN_CASE */
SDV_CRYPTO_BN_GETLIMB_API_TC001(void)2105 void SDV_CRYPTO_BN_GETLIMB_API_TC001(void)
2106 {
2107     TEST_BnTestCaseInit();
2108     int32_t ret;
2109 
2110     BN_BigNum *a = BN_Create(LONG_BN_BITS_256);
2111     ASSERT_TRUE(a != NULL);
2112 
2113     ASSERT_TRUE(BN_GetLimb(NULL) == 0);
2114 
2115     ret = BN_SetLimb(a, 0);
2116     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2117     ASSERT_TRUE(BN_GetLimb(a) == 0);
2118 
2119     ret = BN_SetLimb(a, 1);
2120     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2121     ASSERT_TRUE(BN_GetLimb(a) == 1);
2122 
2123     ret = BN_SetLimb(a, 2);
2124     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2125     ASSERT_TRUE(BN_GetLimb(a) == 2);
2126 
2127     ret = BN_SetLimb(a, BN_UINT_MAX - 1);
2128     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2129     ASSERT_TRUE(BN_GetLimb(a) == BN_UINT_MAX - 1);
2130 
2131     ret = BN_SetLimb(a, BN_UINT_MAX);
2132     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2133     ASSERT_TRUE(BN_GetLimb(a) == BN_UINT_MAX);
2134 
2135     ret = BN_Lshift(a, a, BN_UINT_MAX + 1);
2136     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2137     ASSERT_TRUE(BN_GetLimb(a) == BN_UINT_MAX);
2138 
2139 EXIT:
2140     BN_Destroy(a);
2141 }
2142 /* END_CASE */
2143 
2144 /* BEGIN_CASE */
SDV_CRYPTO_BN_MASKBIT_API_TC001(void)2145 void SDV_CRYPTO_BN_MASKBIT_API_TC001(void)
2146 {
2147     TEST_BnTestCaseInit();
2148     ASSERT_TRUE(BN_MaskBit(NULL, 0) == CRYPT_NULL_INPUT);
2149     int32_t ret;
2150     BN_BigNum *bn = BN_Create(1);
2151 
2152     ret = BN_SetBit(bn, 1);
2153     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2154 
2155     BN_SetSign(bn, 1);
2156     ret = BN_MaskBit(bn, 0);
2157     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2158     ASSERT_TRUE(bn->sign == 0);
2159 
2160     ret = BN_SetLimb(bn, BN_UINT_MAX * 2);
2161     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2162     ret = BN_MaskBit(bn, BN_UINT_BITS * 3);
2163     ASSERT_TRUE(ret == CRYPT_BN_SPACE_NOT_ENOUGH);
2164 EXIT:
2165     BN_Destroy(bn);
2166 }
2167 /* END_CASE */
2168 
2169 /* BEGIN_CASE */
SDV_CRYPTO_BN_MULLIMB_API_TC001(void)2170 void SDV_CRYPTO_BN_MULLIMB_API_TC001(void)
2171 {
2172     TEST_BnTestCaseInit();
2173     int32_t ret;
2174     uint8_t buff[LONG_BN_BYTES_32];
2175     BN_BigNum *a = BN_Create(LONG_BN_BITS_256);
2176     BN_BigNum *b = BN_Create(LONG_BN_BITS_256);
2177     BN_BigNum *r1 = BN_Create(LONG_BN_BITS_256);
2178     BN_BigNum *r2 = BN_Create(LONG_BN_BITS_256 * 2); // 512 == 2 * 256
2179     BN_BigNum *zero = BN_Create(LONG_BN_BITS_256);
2180     BN_Optimizer *opt = BN_OptimizerCreate();
2181     ASSERT_TRUE(a != NULL);
2182     ASSERT_TRUE(b != NULL);
2183     ASSERT_TRUE(r1 != NULL);
2184     ASSERT_TRUE(r2 != NULL);
2185     ASSERT_TRUE(zero != NULL);
2186     ASSERT_TRUE(opt != NULL);
2187     memset_s(buff, sizeof(buff), 0xFF, LONG_BN_BYTES_32);
2188 
2189     ret = BN_Bin2Bn(a, buff, LONG_BN_BYTES_32);
2190     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2191     ret = BN_SetLimb(b, BN_UINT_MAX);
2192     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2193     ret = BN_Zeroize(zero);
2194     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2195     ASSERT_TRUE(BN_IsZero(zero));
2196 
2197     // NULL
2198     ASSERT_TRUE(BN_MulLimb(NULL, a, BN_UINT_MAX) == CRYPT_NULL_INPUT);
2199     ASSERT_TRUE(BN_MulLimb(r1, NULL, BN_UINT_MAX) == CRYPT_NULL_INPUT);
2200 
2201     // a == 0
2202     ret = BN_MulLimb(r1, zero, BN_UINT_MAX);
2203     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2204     ASSERT_TRUE(BN_IsZero(r1));
2205 
2206     // w == 0
2207     ret = BN_MulLimb(r1, a, 0);
2208     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2209     ASSERT_TRUE(BN_IsZero(r1));
2210 
2211     // a == 0
2212     ret = BN_MulLimb(r1, a, BN_UINT_MAX);
2213     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2214 
2215     ret = BN_Mul(r2, a, b, opt);
2216     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2217 
2218     ASSERT_TRUE(BN_Cmp(r1, r2) == CRYPT_SUCCESS);
2219 
2220 EXIT:
2221     BN_Destroy(a);
2222     BN_Destroy(b);
2223     BN_Destroy(r1);
2224     BN_Destroy(r2);
2225     BN_Destroy(zero);
2226     BN_OptimizerDestroy(opt);
2227 }
2228 /* END_CASE */
2229 
2230 /* BEGIN_CASE */
SDV_CRYPTO_BN_DIVLIMB_API_TC001(void)2231 void SDV_CRYPTO_BN_DIVLIMB_API_TC001(void)
2232 {
2233     TEST_BnTestCaseInit();
2234     int32_t ret;
2235     uint8_t buff[LONG_BN_BYTES_32];
2236     BN_BigNum *a = BN_Create(LONG_BN_BITS_256);
2237     BN_BigNum *b = BN_Create(LONG_BN_BITS_256);
2238     BN_BigNum *r1 = BN_Create(LONG_BN_BITS_256);
2239     BN_BigNum *r2 = BN_Create(LONG_BN_BITS_256 * 2); // 512 == 2 * 256
2240     BN_BigNum *res2 = BN_Create(LONG_BN_BITS_256 * 2); // 512 == 2 * 256
2241     BN_BigNum *zero = BN_Create(LONG_BN_BITS_256);
2242     BN_Optimizer *opt = BN_OptimizerCreate();
2243     ASSERT_TRUE(a != NULL);
2244     ASSERT_TRUE(b != NULL);
2245     ASSERT_TRUE(r1 != NULL);
2246     ASSERT_TRUE(r2 != NULL);
2247     ASSERT_TRUE(zero != NULL);
2248     ASSERT_TRUE(opt != NULL);
2249     BN_UINT res1;
2250     // BN_UINT res2;
2251     memset_s(buff, sizeof(buff), 0xFF, LONG_BN_BYTES_32);
2252 
2253     ret = BN_Bin2Bn(a, buff, LONG_BN_BYTES_32);
2254     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2255     ret = BN_SetLimb(b, BN_UINT_MAX);
2256     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2257     ret = BN_Zeroize(zero);
2258     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2259     ASSERT_TRUE(BN_IsZero(zero));
2260 
2261     // NULL
2262     ASSERT_TRUE(BN_DivLimb(NULL, &res1, a, BN_UINT_MAX) == CRYPT_SUCCESS);
2263     ASSERT_TRUE(BN_DivLimb(r1, NULL, a, BN_UINT_MAX) == CRYPT_SUCCESS);
2264     ASSERT_TRUE(BN_DivLimb(NULL, NULL, a, BN_UINT_MAX) == CRYPT_NULL_INPUT);
2265     ASSERT_TRUE(BN_DivLimb(r1, &res1, NULL, BN_UINT_MAX) == CRYPT_NULL_INPUT);
2266 
2267     // a == 0
2268     ret = BN_DivLimb(r1, &res1, zero, BN_UINT_MAX);
2269     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2270     ASSERT_TRUE(BN_IsZero(r1));
2271     ASSERT_TRUE(res1 == 0);
2272 
2273     // w == 0
2274     ret = BN_DivLimb(r1, &res1, a, 0);
2275     ASSERT_TRUE(ret == CRYPT_BN_ERR_DIVISOR_ZERO);
2276 
2277     ret = BN_Copy(r1, a);
2278     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2279 
2280     ret = BN_DivLimb(r1, &res1, r1, BN_UINT_MAX);
2281     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2282 
2283     ret = BN_Div(r2, res2, a, b, opt);
2284     ASSERT_TRUE(ret == CRYPT_SUCCESS);
2285 
2286     ASSERT_TRUE(BN_Cmp(r1, r2) == CRYPT_SUCCESS);
2287     ASSERT_TRUE(res1 == res2->data[0]);
2288 EXIT:
2289     BN_Destroy(a);
2290     BN_Destroy(b);
2291     BN_Destroy(r1);
2292     BN_Destroy(r2);
2293     BN_Destroy(res2);
2294     BN_Destroy(zero);
2295     BN_OptimizerDestroy(opt);
2296 }
2297 /* END_CASE */
2298 
2299 /* BEGIN_CASE */
SDV_CRYPTO_BN_EXTEND_API_TC001(void)2300 void SDV_CRYPTO_BN_EXTEND_API_TC001(void)
2301 {
2302     TEST_BnTestCaseInit();
2303     uint32_t word = BITS_TO_BN_UNIT(BN_MAX_BITS) + 1;
2304     BN_BigNum *a = BN_Create(LONG_BN_BITS_256);
2305     ASSERT_TRUE(a != NULL);
2306     ASSERT_TRUE(BN_Extend(a, 0) == CRYPT_SUCCESS);
2307     ASSERT_TRUE(BN_SetFlag(a, CRYPT_BN_FLAG_STATIC) == CRYPT_SUCCESS);
2308     ASSERT_TRUE(BN_Extend(a, word) == CRYPT_BN_NOT_SUPPORT_EXTENSION);
2309     ASSERT_TRUE(BN_SetFlag(a, CRYPT_BN_FLAG_CONSTTIME) == CRYPT_SUCCESS);
2310     ASSERT_TRUE(BN_Extend(a, word) == CRYPT_BN_BITS_TOO_MAX);
2311     ASSERT_TRUE(BN_Extend(a, word - 1) == CRYPT_SUCCESS);
2312 EXIT:
2313     BN_Destroy(a);
2314 }
2315 /* END_CASE */
2316 
2317 /* BEGIN_CASE */
SDV_CRYPTO_BN_FIXSIZE_API_TC001(void)2318 void SDV_CRYPTO_BN_FIXSIZE_API_TC001(void)
2319 {
2320     TEST_BnTestCaseInit();
2321     BN_BigNum *a = BN_Create(LONG_BN_BITS_256);
2322     ASSERT_TRUE(a != NULL);
2323     a->size = 1;
2324     BN_FixSize(a);
2325     ASSERT_TRUE(a->size == 0);
2326 EXIT:
2327     BN_Destroy(a);
2328 }
2329 /* END_CASE */
2330