]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Bn/CryptBn.c
1 /** @file Big number API implementation based on OpenSSL
3 Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
8 #include "InternalCryptLib.h"
9 #include <openssl/bn.h>
12 Allocate new Big Number.
14 @retval New BigNum opaque structure or NULL on failure.
26 Allocate new Big Number and assign the provided value to it.
28 @param[in] Buf Big endian encoded buffer.
29 @param[in] Len Buffer length.
31 @retval New BigNum opaque structure or NULL on failure.
40 return BN_bin2bn (Buf
, (INT32
)Len
, NULL
);
44 Convert the absolute value of Bn into big-endian form and store it at Buf.
45 The Buf array should have at least BigNumBytes() in it.
47 @param[in] Bn Big number to convert.
48 @param[out] Buf Output buffer.
50 @retval The length of the big-endian number placed at Buf or -1 on error.
59 return BN_bn2bin (Bn
, Buf
);
65 @param[in] Bn Big number to free.
66 @param[in] Clear TRUE if the buffer should be cleared.
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.
87 @param[in] BnA Big number.
88 @param[in] BnB Big number.
89 @param[out] BnRes The result of BnA + BnB.
91 @retval TRUE On success.
92 @retval FALSE Otherwise.
102 return (BOOLEAN
)BN_add (BnRes
, BnA
, BnB
);
106 Subtract two Big Numbers.
107 Please note, all "out" Big number arguments should be properly initialized
108 by calling to BigNumInit() or BigNumFromBin() functions.
110 @param[in] BnA Big number.
111 @param[in] BnB Big number.
112 @param[out] BnRes The result of BnA - BnB.
114 @retval TRUE On success.
115 @retval FALSE Otherwise.
125 return (BOOLEAN
)BN_sub (BnRes
, BnA
, BnB
);
129 Calculate remainder: BnRes = BnA % BnB.
130 Please note, all "out" Big number arguments should be properly initialized
131 by calling to BigNumInit() or BigNumFromBin() functions.
133 @param[in] BnA Big number.
134 @param[in] BnB Big number.
135 @param[out] BnRes The result of BnA % BnB.
137 @retval TRUE On success.
138 @retval FALSE Otherwise.
156 RetVal
= (BOOLEAN
)BN_mod (BnRes
, BnA
, BnB
, Ctx
);
163 Compute BnA to the BnP-th power modulo BnM.
164 Please note, all "out" Big number arguments should be properly initialized
165 by calling to BigNumInit() or BigNumFromBin() functions.
167 @param[in] BnA Big number.
168 @param[in] BnP Big number (power).
169 @param[in] BnM Big number (modulo).
170 @param[out] BnRes The result of (BnA ^ BnP) % BnM.
172 @retval TRUE On success.
173 @retval FALSE Otherwise.
192 RetVal
= (BOOLEAN
)BN_mod_exp (BnRes
, BnA
, BnP
, BnM
, Ctx
);
199 Compute BnA inverse modulo BnM.
200 Please note, all "out" Big number arguments should be properly initialized
201 by calling to BigNumInit() or BigNumFromBin() functions.
203 @param[in] BnA Big number.
204 @param[in] BnM Big number (modulo).
205 @param[out] BnRes The result, such that (BnA * BnRes) % BnM == 1.
207 @retval TRUE On success.
208 @retval FALSE Otherwise.
227 if (BN_mod_inverse (BnRes
, BnA
, BnM
, Ctx
) != NULL
) {
236 Divide two Big Numbers.
237 Please note, all "out" Big number arguments should be properly initialized
238 by calling to BigNumInit() or BigNumFromBin() functions.
240 @param[in] BnA Big number.
241 @param[in] BnB Big number.
242 @param[out] BnRes The result, such that BnA / BnB.
244 @retval TRUE On success.
245 @retval FALSE Otherwise.
263 RetVal
= (BOOLEAN
)BN_div (BnRes
, NULL
, BnA
, BnB
, Ctx
);
270 Multiply two Big Numbers modulo BnM.
271 Please note, all "out" Big number arguments should be properly initialized
272 by calling to BigNumInit() or BigNumFromBin() functions.
274 @param[in] BnA Big number.
275 @param[in] BnB Big number.
276 @param[in] BnM Big number (modulo).
277 @param[out] BnRes The result, such that (BnA * BnB) % BnM.
279 @retval TRUE On success.
280 @retval FALSE Otherwise.
299 RetVal
= (BOOLEAN
)BN_mod_mul (BnRes
, BnA
, BnB
, BnM
, Ctx
);
306 Compare two Big Numbers.
308 @param[in] BnA Big number.
309 @param[in] BnB Big number.
311 @retval 0 BnA == BnB.
313 @retval -1 BnA < BnB.
322 return BN_cmp (BnA
, BnB
);
326 Get number of bits in Bn.
328 @param[in] Bn Big number.
330 @retval Number of bits.
338 return BN_num_bits (Bn
);
342 Get number of bytes in Bn.
344 @param[in] Bn Big number.
346 @retval Number of bytes.
354 return BN_num_bytes (Bn
);
358 Checks if Big Number equals to the given Num.
360 @param[in] Bn Big number.
361 @param[in] Num Number.
363 @retval TRUE iff Bn == Num.
364 @retval FALSE otherwise.
373 return (BOOLEAN
)BN_is_word (Bn
, Num
);
377 Checks if Big Number is odd.
379 @param[in] Bn Big number.
381 @retval TRUE Bn is odd (Bn % 2 == 1).
382 @retval FALSE otherwise.
390 return (BOOLEAN
)BN_is_odd (Bn
);
396 @param[out] BnDst Destination.
397 @param[in] BnSrc Source.
399 @retval BnDst on success.
400 @retval NULL otherwise.
409 return BN_copy (BnDst
, BnSrc
);
413 Get constant Big number with value of "1".
414 This may be used to save expensive allocations.
416 @retval Big Number with value of 1.
424 return BN_value_one ();
428 Shift right Big Number.
429 Please note, all "out" Big number arguments should be properly initialized
430 by calling to BigNumInit() or BigNumFromBin() functions.
432 @param[in] Bn Big number.
433 @param[in] N Number of bits to shift.
434 @param[out] BnRes The result.
436 @retval TRUE On success.
437 @retval FALSE Otherwise.
447 return (BOOLEAN
)BN_rshift (BnRes
, Bn
, (INT32
)N
);
451 Mark Big Number for constant time computations.
452 This function should be called before any constant time computations are
453 performed on the given Big number.
455 @param[in] Bn Big number
463 BN_set_flags (Bn
, BN_FLG_CONSTTIME
);
467 Calculate square modulo.
468 Please note, all "out" Big number arguments should be properly initialized
469 by calling to BigNumInit() or BigNumFromBin() functions.
471 @param[in] BnA Big number.
472 @param[in] BnM Big number (modulo).
473 @param[out] BnRes The result, such that (BnA ^ 2) % BnM.
475 @retval TRUE On success.
476 @retval FALSE Otherwise.
494 RetVal
= (BOOLEAN
)BN_mod_sqr (BnRes
, BnA
, BnM
, Ctx
);
501 Create new Big Number computation context. This is an opaque structure
502 which should be passed to any function that requires it. The BN context is
503 needed to optimize calculations and expensive allocations.
505 @retval Big Number context struct or NULL on failure.
513 return BN_CTX_new ();
517 Free Big Number context that was allocated with BigNumNewContext().
519 @param[in] BnCtx Big number context to free.
531 Set Big Number to a given value.
533 @param[in] Bn Big number to set.
534 @param[in] Val Value to set.
536 @retval TRUE On success.
537 @retval FALSE Otherwise.
546 return (BOOLEAN
)BN_set_word (Bn
, Val
);
550 Add two Big Numbers modulo BnM.
552 @param[in] BnA Big number.
553 @param[in] BnB Big number.
554 @param[in] BnM Big number (modulo).
555 @param[out] BnRes The result, such that (BnA + BnB) % BnM.
557 @retval TRUE On success.
558 @retval FALSE Otherwise.
577 RetVal
= (BOOLEAN
)BN_mod_add (BnRes
, BnA
, BnB
, BnM
, Ctx
);