mirror of
https://github.com/acidanthera/OpenCorePkg.git
synced 2025-12-08 19:25:01 +00:00
OcCryptoLib: Use caller-provided scratch in BigNumCalculateMontParams
This commit is contained in:
parent
1da000afab
commit
59da77c87f
@ -76,12 +76,21 @@ BigNumSwapWord (
|
||||
// Montgomery arithmetics
|
||||
//
|
||||
|
||||
/**
|
||||
1 + 2 * NumWords for RSqr, and then twice more than that for Mod.
|
||||
|
||||
@param[in] NumWords The number of Words of RSqrMod and N.
|
||||
**/
|
||||
#define BIG_NUM_MONT_PARAMS_SCRATCH_SIZE(NumWords) \
|
||||
((1 + 2 * NumWords) * 3 * OC_BN_WORD_SIZE)
|
||||
|
||||
/**
|
||||
Calculates the Montgomery Inverse and R^2 mod N.
|
||||
|
||||
@param[in,out] RSqrMod The buffer to return R^2 mod N into.
|
||||
@param[in] NumWords The number of Words of RSqrMod and N.
|
||||
@param[in] N The Montgomery Modulus.
|
||||
@param[in] Scratch Scratch buffer BIG_NUM_MONT_PARAMS_SCRATCH_SIZE(NumWords).
|
||||
|
||||
@returns The Montgomery Inverse of N.
|
||||
|
||||
@ -90,7 +99,8 @@ OC_BN_WORD
|
||||
BigNumCalculateMontParams (
|
||||
IN OUT OC_BN_WORD *RSqrMod,
|
||||
IN OC_BN_NUM_WORDS NumWords,
|
||||
IN CONST OC_BN_WORD *N
|
||||
IN CONST OC_BN_WORD *N,
|
||||
IN OC_BN_WORD *Scratch
|
||||
);
|
||||
|
||||
/**
|
||||
|
||||
@ -154,15 +154,14 @@ OC_BN_WORD
|
||||
BigNumCalculateMontParams (
|
||||
IN OUT OC_BN_WORD *RSqrMod,
|
||||
IN OC_BN_NUM_WORDS NumWords,
|
||||
IN CONST OC_BN_WORD *N
|
||||
IN CONST OC_BN_WORD *N,
|
||||
IN OC_BN_WORD *Scratch
|
||||
)
|
||||
{
|
||||
OC_BN_WORD N0Inv;
|
||||
UINT32 NumBits;
|
||||
UINTN SizeScratch;
|
||||
OC_BN_NUM_WORDS NumWordsRSqr;
|
||||
OC_BN_NUM_WORDS NumWordsMod;
|
||||
OC_BN_WORD *Scratch;
|
||||
OC_BN_WORD *RSqr;
|
||||
|
||||
ASSERT (RSqrMod != NULL);
|
||||
@ -191,15 +190,6 @@ BigNumCalculateMontParams (
|
||||
//
|
||||
NumWordsRSqr = (OC_BN_NUM_WORDS)(1 + 2 * NumWords);
|
||||
NumWordsMod = 2 * NumWordsRSqr;
|
||||
SizeScratch = (NumWordsRSqr + NumWordsMod) * OC_BN_WORD_SIZE;
|
||||
if (SizeScratch > OC_BN_MAX_SIZE) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
Scratch = AllocatePool (SizeScratch);
|
||||
if (Scratch == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
RSqr = Scratch + NumWordsMod;
|
||||
|
||||
@ -214,8 +204,6 @@ BigNumCalculateMontParams (
|
||||
|
||||
BigNumMod (RSqrMod, NumWords, RSqr, NumWordsRSqr, N, Scratch);
|
||||
|
||||
FreePool (Scratch);
|
||||
|
||||
return N0Inv;
|
||||
}
|
||||
|
||||
|
||||
@ -502,6 +502,7 @@ RsaVerifySigDataFromData (
|
||||
OC_BN_NUM_WORDS ModulusNumWords;
|
||||
|
||||
VOID *Memory;
|
||||
VOID *Scratch;
|
||||
OC_BN_WORD *N;
|
||||
OC_BN_WORD *RSqrMod;
|
||||
|
||||
@ -529,17 +530,20 @@ RsaVerifySigDataFromData (
|
||||
"An overflow verification must be added"
|
||||
);
|
||||
|
||||
Memory = AllocatePool (2 * ModulusSize);
|
||||
Memory = AllocatePool (
|
||||
2 * ModulusSize + BIG_NUM_MONT_PARAMS_SCRATCH_SIZE (ModulusNumWords)
|
||||
);
|
||||
if (Memory == NULL) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
N = (OC_BN_WORD *)Memory;
|
||||
RSqrMod = (OC_BN_WORD *)((UINTN)N + ModulusSize);
|
||||
Scratch = (UINT8 *)Memory + 2 * ModulusSize;
|
||||
|
||||
BigNumParseBuffer (N, ModulusNumWords, Modulus, ModulusSize);
|
||||
|
||||
N0Inv = BigNumCalculateMontParams (RSqrMod, ModulusNumWords, N);
|
||||
N0Inv = BigNumCalculateMontParams (RSqrMod, ModulusNumWords, N, Scratch);
|
||||
if (N0Inv == 0) {
|
||||
FreePool (Memory);
|
||||
return FALSE;
|
||||
|
||||
@ -32,15 +32,21 @@ int verifyRsa (CONST OC_RSA_PUBLIC_KEY *PublicKey, char *Name)
|
||||
UINTN ModulusSize = PublicKey->Hdr.NumQwords * sizeof (UINT64);
|
||||
|
||||
OC_BN_WORD *RSqrMod = malloc(ModulusSize);
|
||||
if (RSqrMod == NULL) {
|
||||
OC_BN_WORD *Scratch = malloc(
|
||||
BIG_NUM_MONT_PARAMS_SCRATCH_SIZE(ModulusSize / OC_BN_WORD_SIZE)
|
||||
);
|
||||
if (RSqrMod == NULL || Scratch == NULL) {
|
||||
printf ("memory allocation error!\n");
|
||||
free(RSqrMod);
|
||||
free(Scratch);
|
||||
return -1;
|
||||
}
|
||||
|
||||
N0Inv = BigNumCalculateMontParams (
|
||||
RSqrMod,
|
||||
ModulusSize / OC_BN_WORD_SIZE,
|
||||
(CONST OC_BN_WORD *) PublicKey->Data
|
||||
(CONST OC_BN_WORD *) PublicKey->Data,
|
||||
Scratch
|
||||
);
|
||||
|
||||
printf (
|
||||
@ -56,6 +62,7 @@ int verifyRsa (CONST OC_RSA_PUBLIC_KEY *PublicKey, char *Name)
|
||||
(unsigned long long) PublicKey->Hdr.N0Inv
|
||||
);
|
||||
|
||||
free(Scratch);
|
||||
free(RSqrMod);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user