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

Go to the source code of this file.

Functions

STATIC OC_BN_WORD BigNumMontInverse (IN CONST OC_BN_WORD *A)
 
OC_BN_WORD BigNumCalculateMontParams (IN OUT OC_BN_WORD *RSqrMod, IN OC_BN_NUM_WORDS NumWords, IN CONST OC_BN_WORD *N, IN OC_BN_WORD *Scratch)
 
STATIC OC_BN_WORD BigNumWordAddMul (OUT OC_BN_WORD *Hi, IN OC_BN_WORD C, IN OC_BN_WORD A, IN OC_BN_WORD B)
 
STATIC OC_BN_WORD BigNumWordAddMulCarry (OUT OC_BN_WORD *Hi, IN OC_BN_WORD C, IN OC_BN_WORD A, IN OC_BN_WORD B, IN OC_BN_WORD Carry)
 
STATIC VOID BigNumMontMulRow (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWords, IN OC_BN_WORD AWord, IN CONST OC_BN_WORD *B, IN CONST OC_BN_WORD *N, IN OC_BN_WORD N0Inv)
 
STATIC VOID BigNumMontMul (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWords, IN CONST OC_BN_WORD *A, IN CONST OC_BN_WORD *B, IN CONST OC_BN_WORD *N, IN OC_BN_WORD N0Inv)
 
STATIC VOID BigNumMontMulRow0 (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWords, IN CONST OC_BN_WORD *N, IN OC_BN_WORD N0Inv)
 
STATIC VOID BigNumMontMul1 (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWords, IN CONST OC_BN_WORD *A, IN CONST OC_BN_WORD *N, IN OC_BN_WORD N0Inv)
 
BOOLEAN BigNumPowMod (IN OUT OC_BN_WORD *Result, IN OC_BN_NUM_WORDS NumWords, IN CONST OC_BN_WORD *A, IN UINT32 B, IN CONST OC_BN_WORD *N, IN OC_BN_WORD N0Inv, IN CONST OC_BN_WORD *RSqrMod, IN OC_BN_WORD *ATmp)
 

Function Documentation

◆ BigNumCalculateMontParams()

OC_BN_WORD BigNumCalculateMontParams ( IN OUT OC_BN_WORD * RSqrMod,
IN OC_BN_NUM_WORDS NumWords,
IN CONST OC_BN_WORD * N,
IN OC_BN_WORD * Scratch )

Calculates the Montgomery Inverse and R^2 mod N.

Parameters
[in,out]RSqrModThe buffer to return R^2 mod N into.
[in]NumWordsThe number of Words of RSqrMod and N. Must be at most OC_BN_MONT_MAX_LEN.
[in]NThe Montgomery Modulus.
[in]ScratchScratch buffer BIG_NUM_MONT_PARAMS_SCRATCH_SIZE(NumWords).
Returns
The Montgomery Inverse of N.

Definition at line 156 of file BigNumMontgomery.c.

◆ BigNumMontInverse()

STATIC OC_BN_WORD BigNumMontInverse ( IN CONST OC_BN_WORD * A)

This library performs arbitrary precision Montgomery operations. All results are returned into caller-provided buffers. The caller is responsible to ensure the buffers can hold the full result of the operation.

https://chromium.googlesource.com/chromiumos/platform/ec/+/master/common/rsa.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. Calculates the Montgomery Inverse -1 / A mod 2^#Bits(Word). This algorithm is based on the Extended Euclidean Algorithm, which returns 1 / A mod 2^#Bits(Word).

Parameters
[in]AThe number to calculate the Montgomery Inverse of.
Return values
0The Montgomery Inverse of A could not be computed.
otherThe Montgomery Inverse of A.

Definition at line 47 of file BigNumMontgomery.c.

◆ BigNumMontMul()

STATIC VOID BigNumMontMul ( IN OUT OC_BN_WORD * Result,
IN OC_BN_NUM_WORDS NumWords,
IN CONST OC_BN_WORD * A,
IN CONST OC_BN_WORD * B,
IN CONST OC_BN_WORD * N,
IN OC_BN_WORD N0Inv )

Calculates the Montgomery product of A and B mod N.

Parameters
[in,out]ResultThe result buffer.
[in]NumWordsThe number of Words of Result, A, B and N.
[in]AThe multiplicant.
[in]BThe multiplier.
[in]NThe modulus.
[in]N0InvThe Montgomery Inverse of N.

Definition at line 400 of file BigNumMontgomery.c.

◆ BigNumMontMul1()

STATIC VOID BigNumMontMul1 ( IN OUT OC_BN_WORD * Result,
IN OC_BN_NUM_WORDS NumWords,
IN CONST OC_BN_WORD * A,
IN CONST OC_BN_WORD * N,
IN OC_BN_WORD N0Inv )

This is an optimized version of the call BigNumMontMul (C, 1, A, N, N0Inv)

Parameters
[in,out]ResultThe result buffer.
[in]NumWordsThe number of Words of Result, A and N.
[in]AThe multiplicant.
[in]NThe modulus.
[in]N0InvThe Montgomery Inverse of N.

Definition at line 519 of file BigNumMontgomery.c.

◆ BigNumMontMulRow()

STATIC VOID BigNumMontMulRow ( IN OUT OC_BN_WORD * Result,
IN OC_BN_NUM_WORDS NumWords,
IN OC_BN_WORD AWord,
IN CONST OC_BN_WORD * B,
IN CONST OC_BN_WORD * N,
IN OC_BN_WORD N0Inv )

Calculates a row of the product of A and B mod N.

Parameters
[in,out]ResultThe result buffer.
[in]NumWordsThe number of Words of Result, B and N.
[in]AWordThe current row's Word of the multiplicant.
[in]BThe multiplier.
[in]NThe modulus.
[in]N0InvThe Montgomery Inverse of N.

Definition at line 292 of file BigNumMontgomery.c.

◆ BigNumMontMulRow0()

STATIC VOID BigNumMontMulRow0 ( IN OUT OC_BN_WORD * Result,
IN OC_BN_NUM_WORDS NumWords,
IN CONST OC_BN_WORD * N,
IN OC_BN_WORD N0Inv )

This is an optimized version of the call BigNumMontMulRow (C, 0, A, N, N0Inv)

Calculates a row of the product of 0 and A mod N.

Parameters
[in,out]ResultThe result buffer.
[in]NumWordsThe number of Words of Result and N.
[in]NThe modulus.
[in]N0InvThe Montgomery Inverse of N.

Definition at line 449 of file BigNumMontgomery.c.

◆ BigNumPowMod()

BOOLEAN BigNumPowMod ( IN OUT OC_BN_WORD * Result,
IN OC_BN_NUM_WORDS NumWords,
IN CONST OC_BN_WORD * A,
IN UINT32 B,
IN CONST OC_BN_WORD * N,
IN OC_BN_WORD N0Inv,
IN CONST OC_BN_WORD * RSqrMod,
IN OC_BN_WORD * ATmp )

Caulculates the exponentiation of A with B mod N.

Parameters
[in,out]ResultThe buffer to return the result into.
[in]NumWordsThe number of Words of Result, A, N and RSqrMod.
[in]AThe base.
[in]BThe exponent.
[in]NThe modulus.
[in]N0InvThe Montgomery Inverse of N.
[in]RSqrModMontgomery's R^2 mod N.
[in]ATmpScratch buffer of NumWords.
Returns
Whether the operation was completes successfully.

Definition at line 555 of file BigNumMontgomery.c.

◆ BigNumWordAddMul()

STATIC OC_BN_WORD BigNumWordAddMul ( OUT OC_BN_WORD * Hi,
IN OC_BN_WORD C,
IN OC_BN_WORD A,
IN OC_BN_WORD B )

Calculates the sum of C and the product of A and B.

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

Definition at line 221 of file BigNumMontgomery.c.

◆ BigNumWordAddMulCarry()

STATIC OC_BN_WORD BigNumWordAddMulCarry ( OUT OC_BN_WORD * Hi,
IN OC_BN_WORD C,
IN OC_BN_WORD A,
IN OC_BN_WORD B,
IN OC_BN_WORD Carry )

Calculates the sum of C, the product of A and B, and Carry.

Parameters
[out]HiBuffer in which the high Word of the result is returned.
[in]CThe addend.
[in]AThe multiplicant.
[in]BThe multiplier.
[in]CarryThe carry of the previous multiplication.
Returns
The low Word of the result.

Definition at line 256 of file BigNumMontgomery.c.