]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLibNull/Bn/CryptBnNull.c
CryptoPkg: Add BigNum support
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLibNull / Bn / CryptBnNull.c
1 /** @file
2 Big number API implementation based on OpenSSL
3
4 Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Library/BaseCryptLib.h>
10 #include <Library/DebugLib.h>
11
12 /**
13 Allocate new Big Number.
14
15 @retval New BigNum opaque structure or NULL on failure.
16 **/
17 VOID *
18 EFIAPI
19 BigNumInit (
20 VOID
21 )
22 {
23 ASSERT (FALSE);
24 return NULL;
25 }
26
27 /**
28 Allocate new Big Number and assign the provided value to it.
29
30 @param[in] Buf Big endian encoded buffer.
31 @param[in] Len Buffer length.
32
33 @retval New BigNum opaque structure or NULL on failure.
34 **/
35 VOID *
36 EFIAPI
37 BigNumFromBin (
38 IN CONST UINT8 *Buf,
39 IN UINTN Len
40 )
41 {
42 ASSERT (FALSE);
43 return NULL;
44 }
45
46 /**
47 Convert the absolute value of Bn into big-endian form and store it at Buf.
48 The Buf array should have at least BigNumBytes() in it.
49
50 @param[in] Bn Big number to convert.
51 @param[out] Buf Output buffer.
52
53 @retval The length of the big-endian number placed at Buf or -1 on error.
54 **/
55 INTN
56 EFIAPI
57 BigNumToBin (
58 IN CONST VOID *Bn,
59 OUT UINT8 *Buf
60 )
61 {
62 ASSERT (FALSE);
63 return -1;
64 }
65
66 /**
67 Free the Big Number.
68
69 @param[in] Bn Big number to free.
70 @param[in] Clear TRUE if the buffer should be cleared.
71 **/
72 VOID
73 EFIAPI
74 BigNumFree (
75 IN VOID *Bn,
76 IN BOOLEAN Clear
77 )
78 {
79 ASSERT (FALSE);
80 }
81
82 /**
83 Calculate the sum of two Big Numbers.
84 Please note, all "out" Big number arguments should be properly initialized
85 by calling to BigNumInit() or BigNumFromBin() functions.
86
87 @param[in] BnA Big number.
88 @param[in] BnB Big number.
89 @param[out] BnRes The result of BnA + BnB.
90
91 @retval TRUE On success.
92 @retval FALSE Otherwise.
93 **/
94 BOOLEAN
95 EFIAPI
96 BigNumAdd (
97 IN CONST VOID *BnA,
98 IN CONST VOID *BnB,
99 OUT VOID *BnRes
100 )
101 {
102 ASSERT (FALSE);
103 return FALSE;
104 }
105
106 /**
107 Subtract two Big Numbers.
108 Please note, all "out" Big number arguments should be properly initialized
109 by calling to BigNumInit() or BigNumFromBin() functions.
110
111 @param[in] BnA Big number.
112 @param[in] BnB Big number.
113 @param[out] BnRes The result of BnA - BnB.
114
115 @retval TRUE On success.
116 @retval FALSE Otherwise.
117 **/
118 BOOLEAN
119 EFIAPI
120 BigNumSub (
121 IN CONST VOID *BnA,
122 IN CONST VOID *BnB,
123 OUT VOID *BnRes
124 )
125 {
126 ASSERT (FALSE);
127 return FALSE;
128 }
129
130 /**
131 Calculate remainder: BnRes = BnA % BnB.
132 Please note, all "out" Big number arguments should be properly initialized
133 by calling to BigNumInit() or BigNumFromBin() functions.
134
135 @param[in] BnA Big number.
136 @param[in] BnB Big number.
137 @param[out] BnRes The result of BnA % BnB.
138
139 @retval TRUE On success.
140 @retval FALSE Otherwise.
141 **/
142 BOOLEAN
143 EFIAPI
144 BigNumMod (
145 IN CONST VOID *BnA,
146 IN CONST VOID *BnB,
147 OUT VOID *BnRes
148 )
149 {
150 ASSERT (FALSE);
151 return FALSE;
152 }
153
154 /**
155 Compute BnA to the BnP-th power modulo BnM.
156 Please note, all "out" Big number arguments should be properly initialized
157 by calling to BigNumInit() or BigNumFromBin() functions.
158
159 @param[in] BnA Big number.
160 @param[in] BnP Big number (power).
161 @param[in] BnM Big number (modulo).
162 @param[out] BnRes The result of (BnA ^ BnP) % BnM.
163
164 @retval TRUE On success.
165 @retval FALSE Otherwise.
166 **/
167 BOOLEAN
168 EFIAPI
169 BigNumExpMod (
170 IN CONST VOID *BnA,
171 IN CONST VOID *BnP,
172 IN CONST VOID *BnM,
173 OUT VOID *BnRes
174 )
175 {
176 ASSERT (FALSE);
177 return FALSE;
178 }
179
180 /**
181 Compute BnA inverse modulo BnM.
182 Please note, all "out" Big number arguments should be properly initialized
183 by calling to BigNumInit() or BigNumFromBin() functions.
184
185 @param[in] BnA Big number.
186 @param[in] BnM Big number (modulo).
187 @param[out] BnRes The result, such that (BnA * BnRes) % BnM == 1.
188
189 @retval TRUE On success.
190 @retval FALSE Otherwise.
191 **/
192 BOOLEAN
193 EFIAPI
194 BigNumInverseMod (
195 IN CONST VOID *BnA,
196 IN CONST VOID *BnM,
197 OUT VOID *BnRes
198 )
199 {
200 ASSERT (FALSE);
201 return FALSE;
202 }
203
204 /**
205 Divide two Big Numbers.
206 Please note, all "out" Big number arguments should be properly initialized
207 by calling to BigNumInit() or BigNumFromBin() functions.
208
209 @param[in] BnA Big number.
210 @param[in] BnB Big number.
211 @param[out] BnRes The result, such that BnA / BnB.
212
213 @retval TRUE On success.
214 @retval FALSE Otherwise.
215 **/
216 BOOLEAN
217 EFIAPI
218 BigNumDiv (
219 IN CONST VOID *BnA,
220 IN CONST VOID *BnB,
221 OUT VOID *BnRes
222 )
223 {
224 ASSERT (FALSE);
225 return FALSE;
226 }
227
228 /**
229 Multiply two Big Numbers modulo BnM.
230 Please note, all "out" Big number arguments should be properly initialized
231 by calling to BigNumInit() or BigNumFromBin() functions.
232
233 @param[in] BnA Big number.
234 @param[in] BnB Big number.
235 @param[in] BnM Big number (modulo).
236 @param[out] BnRes The result, such that (BnA * BnB) % BnM.
237
238 @retval TRUE On success.
239 @retval FALSE Otherwise.
240 **/
241 BOOLEAN
242 EFIAPI
243 BigNumMulMod (
244 IN CONST VOID *BnA,
245 IN CONST VOID *BnB,
246 IN CONST VOID *BnM,
247 OUT VOID *BnRes
248 )
249 {
250 ASSERT (FALSE);
251 return FALSE;
252 }
253
254 /**
255 Compare two Big Numbers.
256
257 @param[in] BnA Big number.
258 @param[in] BnB Big number.
259
260 @retval 0 BnA == BnB.
261 @retval 1 BnA > BnB.
262 @retval -1 BnA < BnB.
263 **/
264 INTN
265 EFIAPI
266 BigNumCmp (
267 IN CONST VOID *BnA,
268 IN CONST VOID *BnB
269 )
270 {
271 ASSERT (FALSE);
272 return 0;
273 }
274
275 /**
276 Get number of bits in Bn.
277
278 @param[in] Bn Big number.
279
280 @retval Number of bits.
281 **/
282 UINTN
283 EFIAPI
284 BigNumBits (
285 IN CONST VOID *Bn
286 )
287 {
288 ASSERT (FALSE);
289 return 0;
290 }
291
292 /**
293 Get number of bytes in Bn.
294
295 @param[in] Bn Big number.
296
297 @retval Number of bytes.
298 **/
299 UINTN
300 EFIAPI
301 BigNumBytes (
302 IN CONST VOID *Bn
303 )
304 {
305 ASSERT (FALSE);
306 return 0;
307 }
308
309 /**
310 Checks if Big Number equals to the given Num.
311
312 @param[in] Bn Big number.
313 @param[in] Num Number.
314
315 @retval TRUE iff Bn == Num.
316 @retval FALSE otherwise.
317 **/
318 BOOLEAN
319 EFIAPI
320 BigNumIsWord (
321 IN CONST VOID *Bn,
322 IN UINTN Num
323 )
324 {
325 ASSERT (FALSE);
326 return FALSE;
327 }
328
329 /**
330 Checks if Big Number is odd.
331
332 @param[in] Bn Big number.
333
334 @retval TRUE Bn is odd (Bn % 2 == 1).
335 @retval FALSE otherwise.
336 **/
337 BOOLEAN
338 EFIAPI
339 BigNumIsOdd (
340 IN CONST VOID *Bn
341 )
342 {
343 ASSERT (FALSE);
344 return FALSE;
345 }
346
347 /**
348 Copy Big number.
349
350 @param[out] BnDst Destination.
351 @param[in] BnSrc Source.
352
353 @retval BnDst on success.
354 @retval NULL otherwise.
355 **/
356 VOID *
357 EFIAPI
358 BigNumCopy (
359 OUT VOID *BnDst,
360 IN CONST VOID *BnSrc
361 )
362 {
363 ASSERT (FALSE);
364 return NULL;
365 }
366
367 /**
368 Get constant Big number with value of "1".
369 This may be used to save expensive allocations.
370
371 @retval Big Number with value of 1.
372 **/
373 CONST VOID *
374 EFIAPI
375 BigNumValueOne (
376 VOID
377 )
378 {
379 ASSERT (FALSE);
380 return NULL;
381 }
382
383 /**
384 Shift right Big Number.
385 Please note, all "out" Big number arguments should be properly initialized
386 by calling to BigNumInit() or BigNumFromBin() functions.
387
388 @param[in] Bn Big number.
389 @param[in] N Number of bits to shift.
390 @param[out] BnRes The result.
391
392 @retval TRUE On success.
393 @retval FALSE Otherwise.
394 **/
395 BOOLEAN
396 EFIAPI
397 BigNumRShift (
398 IN CONST VOID *Bn,
399 IN UINTN N,
400 OUT VOID *BnRes
401 )
402 {
403 ASSERT (FALSE);
404 return FALSE;
405 }
406
407 /**
408 Mark Big Number for constant time computations.
409 This function should be called before any constant time computations are
410 performed on the given Big number.
411
412 @param[in] Bn Big number
413 **/
414 VOID
415 EFIAPI
416 BigNumConstTime (
417 IN VOID *Bn
418 )
419 {
420 ASSERT (FALSE);
421 }
422
423 /**
424 Calculate square modulo.
425 Please note, all "out" Big number arguments should be properly initialized
426 by calling to BigNumInit() or BigNumFromBin() functions.
427
428 @param[in] BnA Big number.
429 @param[in] BnM Big number (modulo).
430 @param[out] BnRes The result, such that (BnA ^ 2) % BnM.
431
432 @retval TRUE On success.
433 @retval FALSE Otherwise.
434 **/
435 BOOLEAN
436 EFIAPI
437 BigNumSqrMod (
438 IN CONST VOID *BnA,
439 IN CONST VOID *BnM,
440 OUT VOID *BnRes
441 )
442 {
443 ASSERT (FALSE);
444 return FALSE;
445 }
446
447 /**
448 Create new Big Number computation context. This is an opaque structure
449 which should be passed to any function that requires it. The BN context is
450 needed to optimize calculations and expensive allocations.
451
452 @retval Big Number context struct or NULL on failure.
453 **/
454 VOID *
455 EFIAPI
456 BigNumNewContext (
457 VOID
458 )
459 {
460 ASSERT (FALSE);
461 return NULL;
462 }
463
464 /**
465 Free Big Number context that was allocated with BigNumNewContext().
466
467 @param[in] BnCtx Big number context to free.
468 **/
469 VOID
470 EFIAPI
471 BigNumContextFree (
472 IN VOID *BnCtx
473 )
474 {
475 ASSERT (FALSE);
476 }
477
478 /**
479 Set Big Number to a given value.
480
481 @param[in] Bn Big number to set.
482 @param[in] Val Value to set.
483
484 @retval TRUE On success.
485 @retval FALSE Otherwise.
486 **/
487 BOOLEAN
488 EFIAPI
489 BigNumSetUint (
490 IN VOID *Bn,
491 IN UINTN Val
492 )
493 {
494 ASSERT (FALSE);
495 return FALSE;
496 }
497
498 /**
499 Add two Big Numbers modulo BnM.
500
501 @param[in] BnA Big number.
502 @param[in] BnB Big number.
503 @param[in] BnM Big number (modulo).
504 @param[out] BnRes The result, such that (BnA + BnB) % BnM.
505
506 @retval TRUE On success.
507 @retval FALSE Otherwise.
508 **/
509 BOOLEAN
510 EFIAPI
511 BigNumAddMod (
512 IN CONST VOID *BnA,
513 IN CONST VOID *BnB,
514 IN CONST VOID *BnM,
515 OUT VOID *BnRes
516 )
517 {
518 ASSERT (FALSE);
519 return FALSE;
520 }