OpenCore
1.0.4
OpenCore Bootloader
|
#include "BigNumLibInternal.h"
Go to the source code of this file.
Macros | |
#define | OC_BN_MAX_VAL ((OC_BN_WORD)0U - 1U) |
Functions | |
STATIC_ASSERT (OC_BN_WORD_SIZE==sizeof(UINT32)||OC_BN_WORD_SIZE==sizeof(UINT64), "OC_BN_WORD_SIZE and OC_BN_WORD_NUM_BITS usages must be adapted.") | |
OC_BN_WORD | BigNumSwapWord (IN OC_BN_WORD Word) |
STATIC VOID | BigNumLeftShiftWords (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWordsResult, IN CONST OC_BN_WORD *A, IN OC_BN_NUM_WORDS NumWordsA, IN UINTN Exponent) |
STATIC VOID | BigNumLeftShiftWordsAndBits (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWordsResult, IN CONST OC_BN_WORD *A, IN OC_BN_NUM_WORDS NumWordsA, IN UINTN NumWords, IN UINT8 NumBits) |
STATIC VOID | BigNumLeftShift (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWordsResult, IN CONST OC_BN_WORD *A, IN OC_BN_NUM_WORDS NumWordsA, IN UINTN Exponent) |
STATIC VOID | BigNumRightShiftWords (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWordsResult, IN CONST OC_BN_WORD *A, IN OC_BN_NUM_WORDS NumWordsA, IN UINTN Exponent) |
STATIC VOID | BigNumRightShiftBitsSmall (IN OUT OC_BN_WORD *A, IN OC_BN_NUM_WORDS NumWords, IN UINT8 Exponent) |
STATIC VOID | BigNumRightShiftWordsAndBits (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWordsResult, IN CONST OC_BN_WORD *A, IN OC_BN_NUM_WORDS NumWordsA, IN UINTN NumWords, IN UINT8 NumBits) |
STATIC VOID | BigNumRightShift (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWordsResult, IN CONST OC_BN_WORD *A, IN OC_BN_NUM_WORDS NumWordsA, IN UINTN Exponent) |
OC_BN_WORD | BigNumWordMul (OUT OC_BN_WORD *Hi, IN OC_BN_WORD A, IN OC_BN_WORD B) |
VOID | BigNumSub (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWords, IN CONST OC_BN_WORD *A, IN CONST OC_BN_WORD *B) |
STATIC UINT8 | BigNumSignificantBitsWord (IN OC_BN_WORD Word) |
STATIC OC_BN_NUM_WORDS | BigNumMostSignificantWord (IN CONST OC_BN_WORD *A, IN OC_BN_NUM_WORDS NumWords) |
OC_BN_NUM_BITS | BigNumSignificantBits (IN CONST OC_BN_WORD *A, IN OC_BN_NUM_WORDS NumWords) |
VOID | BigNumOrWord (IN OUT OC_BN_WORD *A, IN OC_BN_NUM_WORDS NumWords, IN OC_BN_WORD Value, IN UINTN Exponent) |
INTN | BigNumCmp (IN CONST OC_BN_WORD *A, IN OC_BN_NUM_WORDS NumWords, IN CONST OC_BN_WORD *B) |
VOID | BigNumMod (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWordsRest, IN CONST OC_BN_WORD *A, IN OC_BN_NUM_WORDS NumWordsA, IN CONST OC_BN_WORD *B, IN OC_BN_WORD *Scratch) |
VOID | BigNumParseBuffer (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWords, IN CONST UINT8 *Buffer, IN UINTN BufferSize) |
#define OC_BN_MAX_VAL ((OC_BN_WORD)0U - 1U) |
This library performs unsigned arbitrary precision arithmetic operations. All results are returned into caller-provided buffers. The caller is responsible to ensure the buffers can hold a value of the precision it desires. Too large results will be truncated without further notification for public APIs.
https://github.com/kokke/tiny-bignum-c has served as a template for several algorithmic ideas.
This code is not to be considered general-purpose but solely to support cryptographic operations such as RSA encryption. As such, there are arbitrary limitations, such as requirement of equal precision, to limit the complexity of the operations to the bare minimum required to support such use caes.
SECURITY: Currently, no security measures have been taken. This code is vulnerable to both timing and side channel attacks for value leakage. However, its current purpose is the verification of public binaries with public certificates, for which this is perfectly acceptable, especially in regards to performance.
Copyright (C) 2019, Download-Fritz. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Definition at line 36 of file BigNumPrimitives.c.
INTN BigNumCmp | ( | IN CONST OC_BN_WORD * | A, |
IN OC_BN_NUM_WORDS | NumWords, | ||
IN CONST OC_BN_WORD * | B ) |
Returns the relative order of A and B. A and B must have the same precision.
[in] | A | The first number to compare. |
[in] | NumWords | The number of Words to compare. |
[in] | B | The second number to compare. |
< | 0 A is lower than B. |
0 | A is as big as B. |
> | 0 A is greater than B. |
Definition at line 540 of file BigNumPrimitives.c.
STATIC VOID BigNumLeftShift | ( | IN OUT OC_BN_WORD * | Result, |
IN OC_BN_NUM_WORDS | NumWordsResult, | ||
IN CONST OC_BN_WORD * | A, | ||
IN OC_BN_NUM_WORDS | NumWordsA, | ||
IN UINTN | Exponent ) |
Calculates the left-shift of A by Exponent Bits.
[in,out] | Result | The buffer to return the result into. |
[in] | NumWordsResult | The number of Words of Result. |
[in] | A | The number to shift. |
[in] | NumWordsA | The number of Words of A. |
[in] | Exponent | The amount of Bits to shift by. |
Definition at line 165 of file BigNumPrimitives.c.
STATIC VOID BigNumLeftShiftWords | ( | IN OUT OC_BN_WORD * | Result, |
IN OC_BN_NUM_WORDS | NumWordsResult, | ||
IN CONST OC_BN_WORD * | A, | ||
IN OC_BN_NUM_WORDS | NumWordsA, | ||
IN UINTN | Exponent ) |
Shifts A left by Exponent Words.
[in,out] | Result | The buffer to return the result into. |
[in] | NumWordsResult | The number of Words of Result. |
[in,out] | A | The number to be word-shifted. |
[in] | NumWordsA | The number of Words of A. |
[in] | Exponent | The Word shift exponent. |
Definition at line 71 of file BigNumPrimitives.c.
STATIC VOID BigNumLeftShiftWordsAndBits | ( | IN OUT OC_BN_WORD * | Result, |
IN OC_BN_NUM_WORDS | NumWordsResult, | ||
IN CONST OC_BN_WORD * | A, | ||
IN OC_BN_NUM_WORDS | NumWordsA, | ||
IN UINTN | NumWords, | ||
IN UINT8 | NumBits ) |
Shifts A left by Exponent Bits for 0 < Exponent < #Bits(Word). Result must have the exact precision to carry the result.
[in,out] | Result | The buffer to return the result into. |
[in] | NumWordsResult | The number of Words of Result. |
[in] | A | The base. |
[in] | NumWordsA | The number of Words of A. |
[in] | NumWords | The Word shift exponent. |
[in] | NumBits | The Bit shift exponent. |
Definition at line 110 of file BigNumPrimitives.c.
VOID BigNumMod | ( | IN OUT OC_BN_WORD * | Result, |
IN OC_BN_NUM_WORDS | NumWordsRest, | ||
IN CONST OC_BN_WORD * | A, | ||
IN OC_BN_NUM_WORDS | NumWordsA, | ||
IN CONST OC_BN_WORD * | B, | ||
IN OC_BN_WORD * | Scratch ) |
Calculates the remainder of A and B.
[in,out] | Result | The buffer to return the result into. |
[in] | NumWordsRest | The number of Words of Result and B. |
[in] | A | The dividend. |
[in] | NumWordsA | The number of Words of A. |
[in] | B | The divisor. |
[in] | Scratch | Scratch buffer 2 * OC_BN_SIZE (NumWordsA). |
Definition at line 566 of file BigNumPrimitives.c.
STATIC OC_BN_NUM_WORDS BigNumMostSignificantWord | ( | IN CONST OC_BN_WORD * | A, |
IN OC_BN_NUM_WORDS | NumWords ) |
Returns the most significant word index of A.
[in] | A | The number to gather the most significant Word index of. |
[in] | NumWords | The number of Words of A. |
Definition at line 481 of file BigNumPrimitives.c.
VOID BigNumOrWord | ( | IN OUT OC_BN_WORD * | A, |
IN OC_BN_NUM_WORDS | NumWords, | ||
IN OC_BN_WORD | Value, | ||
IN UINTN | Exponent ) |
Calculates the binary union of A and (Value << Exponent).
[in,out] | A | The number to OR with and store the result into. |
[in] | NumWords | The number of Words of A. |
[in] | Value | The Word value to OR with. |
[in] | Exponent | The Word shift exponent. |
Definition at line 518 of file BigNumPrimitives.c.
VOID BigNumParseBuffer | ( | IN OUT OC_BN_WORD * | Result, |
IN OC_BN_NUM_WORDS | NumWords, | ||
IN CONST UINT8 * | Buffer, | ||
IN UINTN | BufferSize ) |
Parses a data array into a number. The buffer size must be a multiple of the Word size. The length of Result must precisely fit the required size.
[in,out] | Result | The buffer to store the result in. |
[in] | NumWords | The number of Words of Result. |
[in] | Buffer | The buffer to parse. |
[in] | BufferSize | The size, in bytes, of Buffer. |
Definition at line 748 of file BigNumPrimitives.c.
STATIC VOID BigNumRightShift | ( | IN OUT OC_BN_WORD * | Result, |
IN OC_BN_NUM_WORDS | NumWordsResult, | ||
IN CONST OC_BN_WORD * | A, | ||
IN OC_BN_NUM_WORDS | NumWordsA, | ||
IN UINTN | Exponent ) |
Calculates the right-shift of A by Exponent Bits.
[in,out] | Result | The buffer to return the result into. |
[in] | NumWordsResult | The number of Words of Result. |
[in] | A | The number to shift. |
[in] | NumWordsA | The number of Words of A. |
[in] | Exponent | The amount of Bits to shift by. |
Definition at line 335 of file BigNumPrimitives.c.
STATIC VOID BigNumRightShiftBitsSmall | ( | IN OUT OC_BN_WORD * | A, |
IN OC_BN_NUM_WORDS | NumWords, | ||
IN UINT8 | Exponent ) |
Shifts A right by Exponent Bits for 0 < Exponent < #Bits(Word).
[in,out] | A | The base. |
[in] | NumWords | The number of Words of A. |
[in] | Exponent | The Bit shift exponent. |
Definition at line 239 of file BigNumPrimitives.c.
STATIC VOID BigNumRightShiftWords | ( | IN OUT OC_BN_WORD * | Result, |
IN OC_BN_NUM_WORDS | NumWordsResult, | ||
IN CONST OC_BN_WORD * | A, | ||
IN OC_BN_NUM_WORDS | NumWordsA, | ||
IN UINTN | Exponent ) |
Shifts A right by Exponent Words.
[in,out] | Result | The buffer to return the result into. |
[in] | NumWordsResult | The number of Words of Result. |
[in,out] | A | The number to be word-shifted. |
[in] | NumWordsA | The number of Words of A. |
[in] | Exponent | The Word shift exponent. |
Definition at line 210 of file BigNumPrimitives.c.
STATIC VOID BigNumRightShiftWordsAndBits | ( | IN OUT OC_BN_WORD * | Result, |
IN OC_BN_NUM_WORDS | NumWordsResult, | ||
IN CONST OC_BN_WORD * | A, | ||
IN OC_BN_NUM_WORDS | NumWordsA, | ||
IN UINTN | NumWords, | ||
IN UINT8 | NumBits ) |
Shifts A right by Exponent Bits for 0 < Exponent < #Bits(Word).
[in,out] | Result | The buffer to return the result into. |
[in] | NumWordsResult | The number of Words of Result. |
[in] | A | The base. |
[in] | NumWordsA | The number of Words of A. |
[in] | NumWords | The Word shift exponent. |
[in] | NumBits | The Bit shift exponent. |
Definition at line 276 of file BigNumPrimitives.c.
OC_BN_NUM_BITS BigNumSignificantBits | ( | IN CONST OC_BN_WORD * | A, |
IN OC_BN_NUM_WORDS | NumWords ) |
Returns the number of significant bits in a number. Logically matches OpenSSL's BN_num_bits.
[in] | A | The number to gather the number of significant bits of. |
[in] | NumWords | The number of Words of A. |
Definition at line 503 of file BigNumPrimitives.c.
STATIC UINT8 BigNumSignificantBitsWord | ( | IN OC_BN_WORD | Word | ) |
Returns the number of significant bits in a Word.
[in] | Word | The word to gather the number of significant bits of. |
Definition at line 449 of file BigNumPrimitives.c.
VOID BigNumSub | ( | IN OUT OC_BN_WORD * | Result, |
IN OC_BN_NUM_WORDS | NumWords, | ||
IN CONST OC_BN_WORD * | A, | ||
IN CONST OC_BN_WORD * | B ) |
Calulates the difference of A and B. A must have the same precision as B. Result must have a precision at most as bit as A and B.
[in,out] | Result | The buffer to return the result into. |
[in] | NumWords | The number of Words of Result, minimum number of Words of A and B. |
[in] | A | The minuend. |
[in] | B | The subtrahend. |
Definition at line 404 of file BigNumPrimitives.c.
OC_BN_WORD BigNumSwapWord | ( | IN OC_BN_WORD | Word | ) |
Swaps the byte order of Word.
[in] | Word | The Word to swap. |
Definition at line 44 of file BigNumPrimitives.c.
OC_BN_WORD BigNumWordMul | ( | OUT OC_BN_WORD * | Hi, |
IN OC_BN_WORD | A, | ||
IN OC_BN_WORD | B ) |
Calculates the product of A and B.
[out] | Hi | Buffer in which the high Word of the result is returned. |
[in] | A | The multiplicant. |
[in] | B | The multiplier. |
Definition at line 379 of file BigNumPrimitives.c.
STATIC_ASSERT | ( | OC_BN_WORD_SIZE | = =sizeof(UINT32)||OC_BN_WORD_SIZE==sizeof(UINT64), |
"OC_BN_WORD_SIZE and OC_BN_WORD_NUM_BITS usages must be adapted." | ) |