Skip to content

Commit

Permalink
OcCryptoLib: Drop BigNumMod memory allocation
Browse files Browse the repository at this point in the history
BigNumCalculateMontParams previously did not check BigNumMod for memory
allocation failure anyway, which was thus harder to diagnose due to
the Montgomery Inverse of N still returning correctly.
  • Loading branch information
vit9696 committed Mar 6, 2022
1 parent 0f22e66 commit 8d9d387
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
9 changes: 4 additions & 5 deletions Library/OcCryptoLib/BigNumLibInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,16 @@ BigNumOrWord (
@param[in] A The dividend.
@param[in] NumWordsA The number of Words of A.
@param[in] B The divisor.
@returns Whether the operation was completes successfully.
@param[in] Memory Scratch buffer 2 * NumWordsA * OC_BN_WORD_SIZE.
**/
BOOLEAN
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 CONST OC_BN_WORD *B,
IN VOID *Memory
);

/**
Expand Down
9 changes: 8 additions & 1 deletion Library/OcCryptoLib/BigNumMontgomery.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ BigNumCalculateMontParams (
UINTN SizeRSqr;
OC_BN_NUM_WORDS NumWordsRSqr;
OC_BN_WORD *RSqr;
VOID *Memory;

ASSERT (RSqrMod != NULL);
ASSERT (NumWords > 0);
Expand Down Expand Up @@ -209,7 +210,13 @@ BigNumCalculateMontParams (
// 2 * NumBits cannot overflow as per above.
//
BigNumOrWord (RSqr, NumWordsRSqr, 1, 2 * NumBits);
BigNumMod (RSqrMod, NumWords, RSqr, NumWordsRSqr, N);

Memory = AllocatePool (2 * NumWordsRSqr * OC_BN_WORD_SIZE);
if (Memory == NULL) {
return 0;
}
BigNumMod (RSqrMod, NumWords, RSqr, NumWordsRSqr, N, Memory);
FreePool (Memory);

FreePool (RSqr);

Expand Down
15 changes: 3 additions & 12 deletions Library/OcCryptoLib/BigNumPrimitives.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,19 +558,18 @@ BigNumCmp (
return 0;
}

BOOLEAN
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 CONST OC_BN_WORD *B,
IN VOID *Memory
)
{
INTN CmpResult;

VOID *Memory;

OC_BN_WORD *ModTmp;
OC_BN_NUM_BITS SigBitsModTmp;
OC_BN_NUM_WORDS SigWordsModTmp;
Expand Down Expand Up @@ -602,11 +601,6 @@ BigNumMod (
"An overflow verification must be added"
);

Memory = AllocatePool (2 * NumWordsA * OC_BN_WORD_SIZE);
if (Memory == NULL) {
return FALSE;
}

ModTmp = Memory;
BigDiv = &ModTmp[SigWordsModTmp];
SigWordsBigDiv = SigWordsModTmp;
Expand Down Expand Up @@ -745,9 +739,6 @@ BigNumMod (
//
ASSERT (BigNumMostSignificantWord (ModTmp, SigWordsModTmp) + 1 <= NumWordsRest);
CopyMem (Result, ModTmp, NumWordsRest * OC_BN_WORD_SIZE);

FreePool (Memory);
return TRUE;
}

VOID
Expand Down

0 comments on commit 8d9d387

Please sign in to comment.