Merge pull request #6731 from tom-cosgrove-arm/issue-6293-mod_exp

Require input to mbedtls_mpi_core_exp_mod() to already be in Montgomery form
This commit is contained in:
Janos Follath 2022-12-07 08:31:49 +00:00 committed by GitHub
commit 1d26d976e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 12 deletions

View file

@ -251,6 +251,12 @@ class ModOperationCommon(OperationCommon):
# provides earlier/more robust input validation.
self.int_n = hex_to_int(val_n)
def to_montgomery(self, val: int) -> int:
return (val * self.r) % self.int_n
def from_montgomery(self, val: int) -> int:
return (val * self.r_inv) % self.int_n
@property
def boundary(self) -> int:
return self.int_n

View file

@ -759,12 +759,23 @@ class BignumCoreExpMod(BignumCoreTarget, bignum_common.ModOperationCommon):
"""Test cases for bignum core exponentiation."""
symbol = "^"
test_function = "mpi_core_exp_mod"
test_name = "Core modular exponentiation"
test_name = "Core modular exponentiation (Mongtomery form only)"
input_style = "fixed"
def arguments(self) -> List[str]:
# Input 'a' has to be given in Montgomery form
mont_a = self.to_montgomery(self.int_a)
arg_mont_a = self.format_arg('{:x}'.format(mont_a))
return [bignum_common.quote_str(n) for n in [self.arg_n,
arg_mont_a,
self.arg_b]
] + self.result()
def result(self) -> List[str]:
# Result has to be given in Montgomery form too
result = pow(self.int_a, self.int_b, self.int_n)
return [self.format_result(result)]
mont_result = self.to_montgomery(result)
return [self.format_result(mont_result)]
@property
def is_valid(self) -> bool:

View file

@ -92,10 +92,9 @@ class BignumModRawConvertToMont(bignum_common.ModOperationCommon,
arity = 1
def result(self) -> List[str]:
result = (self.int_a * self.r) % self.int_n
result = self.to_montgomery(self.int_a)
return [self.format_result(result)]
class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
BignumModRawTarget):
""" Test cases for mpi_mod_raw_from_mont_rep(). """
@ -106,7 +105,7 @@ class BignumModRawConvertFromMont(bignum_common.ModOperationCommon,
arity = 1
def result(self) -> List[str]:
result = (self.int_a * self.r_inv) % self.int_n
result = self.from_montgomery(self.int_a)
return [self.format_result(result)]