TianoCore EDK2 master
Loading...
Searching...
No Matches
BnTests.c
Go to the documentation of this file.
1
9#include "TestBaseCryptLib.h"
10
11//
12// Debug data
13//
14#define MAX_TEST_DATA_SIZE 512
15#define BYTES_OF_OPERATION_A 60
16#define BITS_OF_OPERATION_A 480// (8 * 60)
17
18GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnOperationA[] = {
19 0x00, 0x00, 0x00, 0x00, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
20 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
21 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
22 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
23};
24
25GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnOperationB[] = {
26 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad,
27 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
28 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
29 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23, 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7
30};
31
32GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnOperationC[] = {
33 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed
34};
35
36GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnOperationExp[] = {
37 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63
38};
39
40GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnOperationMod[] = {
41 0x48, 0xbe, 0xcb, 0xd5, 0x36, 0x2e, 0x93, 0x0b, 0x51, 0x45, 0x9c, 0x7d, 0xe7, 0xfe, 0x47, 0xaa,
42 0xc5, 0xd3, 0x4b, 0x4f, 0x06, 0x24, 0xb4, 0x31, 0x83, 0x55, 0xb5, 0xf0, 0xda, 0x14, 0xca, 0x46
43};
44
45// BnOperationA + BnOperationB
46GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnResultSum[] = {
47 0xb0, 0x03, 0x61, 0xa4, 0x29, 0x78, 0xf5, 0x57, 0x80, 0x52, 0x72, 0xab, 0xa0, 0x20, 0x56, 0xde,
48 0xdd, 0xe7, 0x6f, 0x8d, 0xcf, 0x4c, 0xdd, 0x2d, 0xc0, 0x3f, 0x2c, 0x4f, 0xe6, 0x1c, 0x23, 0xa1,
49 0x48, 0xbe, 0xcb, 0xd5, 0x36, 0x2e, 0x93, 0x0b, 0x51, 0x45, 0x9c, 0x7d, 0xe7, 0xfe, 0x47, 0xaa,
50 0xc5, 0xd3, 0x4b, 0x4f, 0x06, 0x24, 0xb4, 0x31, 0x83, 0x55, 0xb5, 0xf0, 0xda, 0x14, 0xca, 0x46
51};
52
53// (BnOperationA + BnOperationC) % BnOperationMod
54GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnResultSumMod[] = {
55 0x16, 0x0a, 0xcf, 0x78, 0x20, 0xac, 0x31, 0x53, 0xd9, 0x0f, 0x22, 0xfc, 0x08, 0x8d, 0xde, 0x0d,
56 0x29, 0xf4, 0x07, 0xdd, 0xfa, 0xf5, 0x61, 0xd4, 0x1a, 0xe5, 0xa1, 0xef, 0x4a, 0x37, 0xfe, 0xec
57};
58
59// (BnOperationA * BnOperationC) % BnOperationMod
60GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnResultMulMod[] = {
61 0x01, 0xDB, 0xD2, 0x82, 0xC9, 0x24, 0x66, 0x2A, 0x96, 0x05, 0x11, 0xF2, 0x31, 0xF0, 0xCB, 0x28,
62 0xBA, 0x5C, 0xBE, 0x7D, 0xEE, 0x37, 0x25, 0xB1, 0x24, 0x7E, 0x15, 0xAB, 0xCD, 0x86, 0x8E, 0x39
63};
64
65// BnOperationA / BnOperationMod
66GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnResultDiv[] = {
67 0x02, 0x06, 0xA6, 0xDC, 0x2E, 0x97, 0x05, 0xEA, 0xCD, 0xF7, 0xAB, 0xCD, 0xE5, 0x9C, 0x33, 0x03,
68 0xCE, 0x3D, 0x7E, 0x63, 0x23, 0xB2, 0xEC, 0xED, 0x96, 0x9D, 0xC9, 0xBB, 0x78
69};
70
71// BnOperationA % BnOperationMod
72GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnResultMod[] = {
73 0x06, 0x2A, 0x8D, 0x06, 0x9D, 0x14, 0x53, 0x3B, 0x05, 0xD9, 0x86, 0x00, 0xA5, 0xB9, 0x05, 0x7F,
74 0xC1, 0x82, 0xEC, 0x23, 0x44, 0x23, 0xC8, 0xA2, 0x42, 0xB3, 0x43, 0xB8, 0x7C, 0xD6, 0xB1, 0xCF
75};
76
77// BnOperationA % BnOperationMod
78GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnResultInverseMod[] = {
79 0x3a, 0xeb, 0xc5, 0x98, 0x9c, 0x22, 0xd6, 0x76, 0x7d, 0x1c, 0xc6, 0xd6, 0xbb, 0x1b, 0xed, 0xfd,
80 0x0f, 0x34, 0xbf, 0xe0, 0x2b, 0x4a, 0x26, 0xc3, 0xc0, 0xd9, 0x57, 0xc7, 0x11, 0xc0, 0xd6, 0x35
81};
82
83// BnOperationA % BnOperationMod
84GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnResultExpMod[] = {
85 0x39, 0xf8, 0x74, 0xa0, 0xe8, 0x02, 0x8b, 0xf2, 0x22, 0x62, 0x82, 0x4c, 0xe0, 0xed, 0x63, 0x48,
86 0xb9, 0xa2, 0xaa, 0xbc, 0xba, 0xb1, 0xd3, 0x6a, 0x02, 0xfd, 0xf3, 0x0e, 0x3a, 0x19, 0x39, 0x37
87};
88
89// BnOperationA >> 128
90GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnResultRShift[] = {
91 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
92 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 0x0a, 0x9e, 0xee, 0xe6,0x4b, 0x55, 0xd3, 0x9a,
93 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23,0xa3, 0xfe, 0xeb, 0xbd
94};
95
96// 0x12345678
97GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 BnResultUIntSet[] = { 0x12, 0x34, 0x56, 0x78 };
98
99typedef struct {
100 VOID *BnA;
101 VOID *BnB;
102 VOID *BnC;
103 VOID *BnD;
104 VOID *BnCTX;
106
108
109//
110// Debug function
111//
112STATIC
113BOOLEAN
114EqualBn2Bn (
115 CONST VOID *Expected,
116 CONST VOID *Actual
117 )
118{
119 if (BigNumCmp (Expected, Actual) == 0) {
120 return TRUE;
121 }
122
123 return FALSE;
124}
125
126STATIC
127BOOLEAN
128EqualBn2Bin (
129 CONST VOID *Bn,
130 CONST UINT8 *Buffer,
131 CONST UINTN BufferSize
132 )
133{
134 UINTN BnTestBufferSize;
135 UINT8 BnTestBuffer[MAX_TEST_DATA_SIZE];
136
137 BnTestBufferSize = BigNumToBin (Bn, BnTestBuffer);
138 if (BnTestBufferSize == BufferSize) {
139 if (CompareMem (Buffer, BnTestBuffer, BnTestBufferSize) == 0) {
140 return TRUE;
141 }
142 }
143
144 return FALSE;
145}
146
148EFIAPI
149TestVerifyBnPreReq (
150 UNIT_TEST_CONTEXT Context
151 )
152{
153 BN_TEST_CONTEXT *BnContext;
154
155 BnContext = Context;
156 BnContext->BnCTX = BigNumNewContext ();
157 BnContext->BnA = BigNumInit ();
158 BnContext->BnB = BigNumInit ();
159 BnContext->BnC = BigNumInit ();
160 BnContext->BnD = BigNumInit ();
161 if ( (BnContext->BnCTX == NULL)
162 || (BnContext->BnA == NULL)
163 || (BnContext->BnB == NULL)
164 || (BnContext->BnC == NULL)
165 || (BnContext->BnD == NULL)
166 )
167 {
168 return UNIT_TEST_ERROR_TEST_FAILED;
169 }
170
171 return UNIT_TEST_PASSED;
172}
173
174VOID
175EFIAPI
176TestVerifyBnCleanUp (
177 UNIT_TEST_CONTEXT Context
178 )
179{
180 BN_TEST_CONTEXT *BnContext;
181
182 BnContext = Context;
183 BigNumContextFree (BnContext->BnCTX);
184 BigNumFree (BnContext->BnA, TRUE);
185 BigNumFree (BnContext->BnB, TRUE);
186 BigNumFree (BnContext->BnC, TRUE);
187 BigNumFree (BnContext->BnD, TRUE);
188}
189
191EFIAPI
192TestVerifyBn (
193 IN UNIT_TEST_CONTEXT Context
194 )
195{
196 BN_TEST_CONTEXT *BnContext;
197 UINTN Num;
198 CONST VOID *BnOne;
199
200 BnContext = Context;
201
202 // Calculation tests
203 BnContext->BnA = BigNumFromBin (BnOperationA, sizeof (BnOperationA));
204 BnContext->BnB = BigNumFromBin (BnOperationB, sizeof (BnOperationB));
205 // C=A+B
206 BigNumAdd (BnContext->BnA, BnContext->BnB, BnContext->BnC);
207 UT_ASSERT_TRUE (EqualBn2Bin (BnContext->BnC, BnResultSum, sizeof (BnResultSum)));
208 // D=C-A=B
209 BigNumSub (BnContext->BnC, BnContext->BnA, BnContext->BnD);
210 UT_ASSERT_TRUE (EqualBn2Bn (BnContext->BnB, BnContext->BnD));
211 // C=(A+B)%D
212 BnContext->BnD = BigNumFromBin (BnOperationMod, sizeof (BnOperationMod));
213 BigNumAddMod (BnContext->BnA, BnContext->BnB, BnContext->BnD, BnContext->BnC);
214 UT_ASSERT_TRUE (EqualBn2Bin (BnContext->BnC, BnResultSumMod, sizeof (BnResultSumMod)));
215 // C=(A*B)%D
216 BigNumMulMod (BnContext->BnA, BnContext->BnB, BnContext->BnD, BnContext->BnC);
217 UT_ASSERT_TRUE (EqualBn2Bin (BnContext->BnC, BnResultMulMod, sizeof (BnResultMulMod)));
218 // C=A/D
219 BigNumDiv (BnContext->BnA, BnContext->BnD, BnContext->BnC);
220 UT_ASSERT_TRUE (EqualBn2Bin (BnContext->BnC, BnResultDiv, sizeof (BnResultDiv)));
221 // C=A%D
222 BigNumMod (BnContext->BnA, BnContext->BnD, BnContext->BnC);
223 UT_ASSERT_TRUE (EqualBn2Bin (BnContext->BnC, BnResultMod, sizeof (BnResultMod)));
224 // 1=(A*C)%D
225 BigNumInverseMod (BnContext->BnA, BnContext->BnD, BnContext->BnC);
226 UT_ASSERT_TRUE (EqualBn2Bin (BnContext->BnC, BnResultInverseMod, sizeof (BnResultInverseMod)));
227 // C=(A^B)%D
228 BnContext->BnB = BigNumFromBin (BnOperationExp, sizeof (BnOperationExp));
229 BigNumExpMod (BnContext->BnA, BnContext->BnB, BnContext->BnD, BnContext->BnC);
230 UT_ASSERT_TRUE (EqualBn2Bin (BnContext->BnC, BnResultExpMod, sizeof (BnResultExpMod)));
231 // C=A>>128
232 BigNumRShift (BnContext->BnA, 128, BnContext->BnC);
233 UT_ASSERT_TRUE (EqualBn2Bin (BnContext->BnC, BnResultRShift, sizeof (BnResultRShift)));
234 // C=0x12345678
235 BigNumSetUint (BnContext->BnC, 0x12345678);
236 UT_ASSERT_TRUE (EqualBn2Bin (BnContext->BnC, BnResultUIntSet, sizeof (BnResultUIntSet)));
237 // Bn compare
238 UT_ASSERT_TRUE (BigNumIsWord (BnContext->BnC, 0x12345678));
239 UT_ASSERT_FALSE (BigNumIsWord (BnContext->BnC, 0x12345600));
240 UT_ASSERT_FALSE (BigNumIsOdd (BnContext->BnC));
241 UT_ASSERT_TRUE (BigNumIsOdd (BnContext->BnA));
242
243 // Other tests
244 BigNumConstTime (BnContext->BnA);
245 Num = BigNumBytes (BnContext->BnA);
246 UT_ASSERT_EQUAL (Num, BYTES_OF_OPERATION_A);
247 Num = BigNumBits (BnContext->BnA);
248 UT_ASSERT_EQUAL (Num, BITS_OF_OPERATION_A);
249 BnOne = BigNumValueOne ();
250 if (BnOne == NULL) {
251 return UNIT_TEST_ERROR_TEST_FAILED;
252 }
253
254 UT_ASSERT_TRUE (BigNumIsWord (BnOne, 0x1));
255
256 return UNIT_TEST_PASSED;
257}
258
259TEST_DESC mBnTest[] = {
260 //
261 // -----Description----------------Class---------------------Function-----------Pre----------------Post---------Context
262 //
263 { "TestVerifyBn()", "CryptoPkg.BaseCryptLib.BigNumber", TestVerifyBn, TestVerifyBnPreReq, TestVerifyBnCleanUp, &mBnContext },
264};
265
266UINTN mBnTestNum = ARRAY_SIZE (mBnTest);
UINT64 UINTN
BOOLEAN EFIAPI BigNumExpMod(IN CONST VOID *BnA, IN CONST VOID *BnP, IN CONST VOID *BnM, OUT VOID *BnRes)
Definition: CryptBn.c:177
BOOLEAN EFIAPI BigNumMulMod(IN CONST VOID *BnA, IN CONST VOID *BnB, IN CONST VOID *BnM, OUT VOID *BnRes)
Definition: CryptBn.c:284
BOOLEAN EFIAPI BigNumInverseMod(IN CONST VOID *BnA, IN CONST VOID *BnM, OUT VOID *BnRes)
Definition: CryptBn.c:212
VOID *EFIAPI BigNumFromBin(IN CONST UINT8 *Buf, IN UINTN Len)
Definition: CryptBn.c:35
BOOLEAN EFIAPI BigNumDiv(IN CONST VOID *BnA, IN CONST VOID *BnB, OUT VOID *BnRes)
Definition: CryptBn.c:249
UINTN EFIAPI BigNumBits(IN CONST VOID *Bn)
Definition: CryptBn.c:334
VOID *EFIAPI BigNumNewContext(VOID)
Definition: CryptBn.c:509
VOID EFIAPI BigNumFree(IN VOID *Bn, IN BOOLEAN Clear)
Definition: CryptBn.c:70
BOOLEAN EFIAPI BigNumIsWord(IN CONST VOID *Bn, IN UINTN Num)
Definition: CryptBn.c:368
INTN EFIAPI BigNumCmp(IN CONST VOID *BnA, IN CONST VOID *BnB)
Definition: CryptBn.c:317
BOOLEAN EFIAPI BigNumAdd(IN CONST VOID *BnA, IN CONST VOID *BnB, OUT VOID *BnRes)
Definition: CryptBn.c:96
VOID EFIAPI BigNumConstTime(IN VOID *Bn)
Definition: CryptBn.c:459
CONST VOID *EFIAPI BigNumValueOne(VOID)
Definition: CryptBn.c:420
BOOLEAN EFIAPI BigNumSetUint(IN VOID *Bn, IN UINTN Val)
Definition: CryptBn.c:541
VOID EFIAPI BigNumContextFree(IN VOID *BnCtx)
Definition: CryptBn.c:523
BOOLEAN EFIAPI BigNumIsOdd(IN CONST VOID *Bn)
Definition: CryptBn.c:386
BOOLEAN EFIAPI BigNumSub(IN CONST VOID *BnA, IN CONST VOID *BnB, OUT VOID *BnRes)
Definition: CryptBn.c:119
BOOLEAN EFIAPI BigNumAddMod(IN CONST VOID *BnA, IN CONST VOID *BnB, IN CONST VOID *BnM, OUT VOID *BnRes)
Definition: CryptBn.c:562
BOOLEAN EFIAPI BigNumMod(IN CONST VOID *BnA, IN CONST VOID *BnB, OUT VOID *BnRes)
Definition: CryptBn.c:142
VOID *EFIAPI BigNumInit(VOID)
Definition: CryptBn.c:18
BOOLEAN EFIAPI BigNumRShift(IN CONST VOID *Bn, IN UINTN N, OUT VOID *BnRes)
Definition: CryptBn.c:441
UINTN EFIAPI BigNumBytes(IN CONST VOID *Bn)
Definition: CryptBn.c:350
INTN EFIAPI BigNumToBin(IN CONST VOID *Bn, OUT UINT8 *Buf)
Definition: CryptBn.c:54
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
#define IN
Definition: Base.h:279
#define GLOBAL_REMOVE_IF_UNREFERENCED
Definition: Base.h:48
VOID * UNIT_TEST_CONTEXT
Definition: UnitTestLib.h:54
#define UT_ASSERT_TRUE(Expression)
Definition: UnitTestLib.h:350
#define UT_ASSERT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:375
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16
#define UT_ASSERT_FALSE(Expression)
Definition: UnitTestLib.h:362