OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
BigNumPrimitives.c File Reference

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)
 

Macro Definition Documentation

◆ OC_BN_MAX_VAL

#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.

Function Documentation

◆ BigNumCmp()

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.

Parameters
[in]AThe first number to compare.
[in]NumWordsThe number of Words to compare.
[in]BThe second number to compare.
Return values
<0 A is lower than B.
0A is as big as B.
>0 A is greater than B.

Definition at line 540 of file BigNumPrimitives.c.

◆ BigNumLeftShift()

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.

Parameters
[in,out]ResultThe buffer to return the result into.
[in]NumWordsResultThe number of Words of Result.
[in]AThe number to shift.
[in]NumWordsAThe number of Words of A.
[in]ExponentThe amount of Bits to shift by.

Definition at line 165 of file BigNumPrimitives.c.

◆ BigNumLeftShiftWords()

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.

Parameters
[in,out]ResultThe buffer to return the result into.
[in]NumWordsResultThe number of Words of Result.
[in,out]AThe number to be word-shifted.
[in]NumWordsAThe number of Words of A.
[in]ExponentThe Word shift exponent.

Definition at line 71 of file BigNumPrimitives.c.

◆ BigNumLeftShiftWordsAndBits()

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.

Parameters
[in,out]ResultThe buffer to return the result into.
[in]NumWordsResultThe number of Words of Result.
[in]AThe base.
[in]NumWordsAThe number of Words of A.
[in]NumWordsThe Word shift exponent.
[in]NumBitsThe Bit shift exponent.

Definition at line 110 of file BigNumPrimitives.c.

◆ BigNumMod()

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.

Parameters
[in,out]ResultThe buffer to return the result into.
[in]NumWordsRestThe number of Words of Result and B.
[in]AThe dividend.
[in]NumWordsAThe number of Words of A.
[in]BThe divisor.
[in]ScratchScratch buffer 2 * OC_BN_SIZE (NumWordsA).

Definition at line 566 of file BigNumPrimitives.c.

◆ BigNumMostSignificantWord()

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.

Parameters
[in]AThe number to gather the most significant Word index of.
[in]NumWordsThe number of Words of A.
Returns
The index of the most significant Word in A.

Definition at line 481 of file BigNumPrimitives.c.

◆ BigNumOrWord()

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).

Parameters
[in,out]AThe number to OR with and store the result into.
[in]NumWordsThe number of Words of A.
[in]ValueThe Word value to OR with.
[in]ExponentThe Word shift exponent.

Definition at line 518 of file BigNumPrimitives.c.

◆ BigNumParseBuffer()

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.

Parameters
[in,out]ResultThe buffer to store the result in.
[in]NumWordsThe number of Words of Result.
[in]BufferThe buffer to parse.
[in]BufferSizeThe size, in bytes, of Buffer.

Definition at line 748 of file BigNumPrimitives.c.

◆ BigNumRightShift()

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.

Parameters
[in,out]ResultThe buffer to return the result into.
[in]NumWordsResultThe number of Words of Result.
[in]AThe number to shift.
[in]NumWordsAThe number of Words of A.
[in]ExponentThe amount of Bits to shift by.

Definition at line 335 of file BigNumPrimitives.c.

◆ BigNumRightShiftBitsSmall()

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).

Parameters
[in,out]AThe base.
[in]NumWordsThe number of Words of A.
[in]ExponentThe Bit shift exponent.

Definition at line 239 of file BigNumPrimitives.c.

◆ BigNumRightShiftWords()

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.

Parameters
[in,out]ResultThe buffer to return the result into.
[in]NumWordsResultThe number of Words of Result.
[in,out]AThe number to be word-shifted.
[in]NumWordsAThe number of Words of A.
[in]ExponentThe Word shift exponent.

Definition at line 210 of file BigNumPrimitives.c.

◆ BigNumRightShiftWordsAndBits()

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).

Parameters
[in,out]ResultThe buffer to return the result into.
[in]NumWordsResultThe number of Words of Result.
[in]AThe base.
[in]NumWordsAThe number of Words of A.
[in]NumWordsThe Word shift exponent.
[in]NumBitsThe Bit shift exponent.

Definition at line 276 of file BigNumPrimitives.c.

◆ BigNumSignificantBits()

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.

Parameters
[in]AThe number to gather the number of significant bits of.
[in]NumWordsThe number of Words of A.
Returns
The number of significant bits in A.

Definition at line 503 of file BigNumPrimitives.c.

◆ BigNumSignificantBitsWord()

STATIC UINT8 BigNumSignificantBitsWord ( IN OC_BN_WORD Word)

Returns the number of significant bits in a Word.

Parameters
[in]WordThe word to gather the number of significant bits of.
Returns
The number of significant bits in Word.

Definition at line 449 of file BigNumPrimitives.c.

◆ BigNumSub()

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.

Parameters
[in,out]ResultThe buffer to return the result into.
[in]NumWordsThe number of Words of Result, minimum number of Words of A and B.
[in]AThe minuend.
[in]BThe subtrahend.

Definition at line 404 of file BigNumPrimitives.c.

◆ BigNumSwapWord()

OC_BN_WORD BigNumSwapWord ( IN OC_BN_WORD Word)

Swaps the byte order of Word.

Parameters
[in]WordThe Word to swap.
Returns
The byte-swapped value of Word.

Definition at line 44 of file BigNumPrimitives.c.

◆ BigNumWordMul()

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.

Parameters
[out]HiBuffer in which the high Word of the result is returned.
[in]AThe multiplicant.
[in]BThe multiplier.
Returns
The low Word of the result.

Definition at line 379 of file BigNumPrimitives.c.

◆ STATIC_ASSERT()

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."  )