aboutsummaryrefslogtreecommitdiff
path: root/frozen_deps/Cryptodome/Math
diff options
context:
space:
mode:
Diffstat (limited to 'frozen_deps/Cryptodome/Math')
-rw-r--r--frozen_deps/Cryptodome/Math/Numbers.pyi6
-rw-r--r--frozen_deps/Cryptodome/Math/_IntegerBase.py20
-rw-r--r--frozen_deps/Cryptodome/Math/_IntegerBase.pyi6
-rw-r--r--frozen_deps/Cryptodome/Math/_IntegerCustom.py56
-rw-r--r--frozen_deps/Cryptodome/Math/_IntegerGMP.py20
-rw-r--r--frozen_deps/Cryptodome/Math/_IntegerNative.py45
-rwxr-xr-xfrozen_deps/Cryptodome/Math/_modexp.abi3.sobin294464 -> 213552 bytes
-rwxr-xr-xfrozen_deps/Cryptodome/Math/_modexp.cpython-39-x86_64-linux-gnu.sobin207274 -> 0 bytes
8 files changed, 114 insertions, 39 deletions
diff --git a/frozen_deps/Cryptodome/Math/Numbers.pyi b/frozen_deps/Cryptodome/Math/Numbers.pyi
index 2285a3b..b0206ca 100644
--- a/frozen_deps/Cryptodome/Math/Numbers.pyi
+++ b/frozen_deps/Cryptodome/Math/Numbers.pyi
@@ -1,4 +1,2 @@
-from Cryptodome.Math._IntegerBase import IntegerBase
-
-class Integer(IntegerBase):
- pass
+from Cryptodome.Math._IntegerBase import IntegerBase as Integer
+__all__ = ['Integer']
diff --git a/frozen_deps/Cryptodome/Math/_IntegerBase.py b/frozen_deps/Cryptodome/Math/_IntegerBase.py
index 7d78c4b..03dd591 100644
--- a/frozen_deps/Cryptodome/Math/_IntegerBase.py
+++ b/frozen_deps/Cryptodome/Math/_IntegerBase.py
@@ -390,3 +390,23 @@ class IntegerBase(ABC):
)
return norm_candidate + min_inclusive
+ @staticmethod
+ @abc.abstractmethod
+ def _mult_modulo_bytes(term1, term2, modulus):
+ """Multiply two integers, take the modulo, and encode as big endian.
+ This specialized method is used for RSA decryption.
+
+ Args:
+ term1 : integer
+ The first term of the multiplication, non-negative.
+ term2 : integer
+ The second term of the multiplication, non-negative.
+ modulus: integer
+ The modulus, a positive odd number.
+ :Returns:
+ A byte string, with the result of the modular multiplication
+ encoded in big endian mode.
+ It is as long as the modulus would be, with zero padding
+ on the left if needed.
+ """
+ pass
diff --git a/frozen_deps/Cryptodome/Math/_IntegerBase.pyi b/frozen_deps/Cryptodome/Math/_IntegerBase.pyi
index 362c512..ea23532 100644
--- a/frozen_deps/Cryptodome/Math/_IntegerBase.pyi
+++ b/frozen_deps/Cryptodome/Math/_IntegerBase.pyi
@@ -4,6 +4,8 @@ RandFunc = Callable[[int],int]
class IntegerBase:
+ def __init__(self, value: Union[IntegerBase, int]): ...
+
def __int__(self) -> int: ...
def __str__(self) -> str: ...
def __repr__(self) -> str: ...
@@ -58,4 +60,8 @@ class IntegerBase:
def random(cls, **kwargs: Union[int,RandFunc]) -> IntegerBase : ...
@classmethod
def random_range(cls, **kwargs: Union[int,RandFunc]) -> IntegerBase : ...
+ @staticmethod
+ def _mult_modulo_bytes(term1: Union[IntegerBase, int],
+ term2: Union[IntegerBase, int],
+ modulus: Union[IntegerBase, int]) -> bytes: ...
diff --git a/frozen_deps/Cryptodome/Math/_IntegerCustom.py b/frozen_deps/Cryptodome/Math/_IntegerCustom.py
index 0e23152..20eadca 100644
--- a/frozen_deps/Cryptodome/Math/_IntegerCustom.py
+++ b/frozen_deps/Cryptodome/Math/_IntegerCustom.py
@@ -41,12 +41,18 @@ from Cryptodome.Util._raw_api import (load_pycryptodome_raw_lib,
from Cryptodome.Random.random import getrandbits
c_defs = """
-int monty_pow(const uint8_t *base,
- const uint8_t *exp,
- const uint8_t *modulus,
- uint8_t *out,
- size_t len,
- uint64_t seed);
+int monty_pow(uint8_t *out,
+ const uint8_t *base,
+ const uint8_t *exp,
+ const uint8_t *modulus,
+ size_t len,
+ uint64_t seed);
+
+int monty_multiply(uint8_t *out,
+ const uint8_t *term1,
+ const uint8_t *term2,
+ const uint8_t *modulus,
+ size_t len);
"""
@@ -116,3 +122,41 @@ class IntegerCustom(IntegerNative):
result = bytes_to_long(get_raw_buffer(out))
self._value = result
return self
+
+ @staticmethod
+ def _mult_modulo_bytes(term1, term2, modulus):
+
+ # With modular reduction
+ mod_value = int(modulus)
+ if mod_value < 0:
+ raise ValueError("Modulus must be positive")
+ if mod_value == 0:
+ raise ZeroDivisionError("Modulus cannot be zero")
+
+ # C extension only works with odd moduli
+ if (mod_value & 1) == 0:
+ raise ValueError("Odd modulus is required")
+
+ # C extension only works with non-negative terms smaller than modulus
+ if term1 >= mod_value or term1 < 0:
+ term1 %= mod_value
+ if term2 >= mod_value or term2 < 0:
+ term2 %= mod_value
+
+ modulus_b = long_to_bytes(mod_value)
+ numbers_len = len(modulus_b)
+ term1_b = long_to_bytes(term1, numbers_len)
+ term2_b = long_to_bytes(term2, numbers_len)
+ out = create_string_buffer(numbers_len)
+
+ error = _raw_montgomery.monty_multiply(
+ out,
+ term1_b,
+ term2_b,
+ modulus_b,
+ c_size_t(numbers_len)
+ )
+ if error:
+ raise ValueError("monty_multiply failed with error: %d" % error)
+
+ return get_raw_buffer(out)
diff --git a/frozen_deps/Cryptodome/Math/_IntegerGMP.py b/frozen_deps/Cryptodome/Math/_IntegerGMP.py
index 3ab7c59..f58f044 100644
--- a/frozen_deps/Cryptodome/Math/_IntegerGMP.py
+++ b/frozen_deps/Cryptodome/Math/_IntegerGMP.py
@@ -749,6 +749,26 @@ class IntegerGMP(IntegerBase):
raise ValueError("n must be positive odd for the Jacobi symbol")
return _gmp.mpz_jacobi(a._mpz_p, n._mpz_p)
+ @staticmethod
+ def _mult_modulo_bytes(term1, term2, modulus):
+ if not isinstance(term1, IntegerGMP):
+ term1 = IntegerGMP(term1)
+ if not isinstance(term2, IntegerGMP):
+ term2 = IntegerGMP(term2)
+ if not isinstance(modulus, IntegerGMP):
+ modulus = IntegerGMP(modulus)
+
+ if modulus < 0:
+ raise ValueError("Modulus must be positive")
+ if modulus == 0:
+ raise ZeroDivisionError("Modulus cannot be zero")
+ if (modulus & 1) == 0:
+ raise ValueError("Odd modulus is required")
+
+ numbers_len = len(modulus.to_bytes())
+ result = ((term1 * term2) % modulus).to_bytes(numbers_len)
+ return result
+
# Clean-up
def __del__(self):
diff --git a/frozen_deps/Cryptodome/Math/_IntegerNative.py b/frozen_deps/Cryptodome/Math/_IntegerNative.py
index 9b857ea..5f768e2 100644
--- a/frozen_deps/Cryptodome/Math/_IntegerNative.py
+++ b/frozen_deps/Cryptodome/Math/_IntegerNative.py
@@ -30,7 +30,7 @@
from ._IntegerBase import IntegerBase
-from Cryptodome.Util.number import long_to_bytes, bytes_to_long
+from Cryptodome.Util.number import long_to_bytes, bytes_to_long, inverse, GCD
class IntegerNative(IntegerBase):
@@ -280,13 +280,7 @@ class IntegerNative(IntegerBase):
if self._value == 0:
return 1
- bit_size = 0
- tmp = self._value
- while tmp:
- tmp >>= 1
- bit_size += 1
-
- return bit_size
+ return self._value.bit_length()
def size_in_bytes(self):
return (self.size_in_bits() - 1) // 8 + 1
@@ -318,22 +312,7 @@ class IntegerNative(IntegerBase):
self._value = int(source)
def inplace_inverse(self, modulus):
- modulus = int(modulus)
- if modulus == 0:
- raise ZeroDivisionError("Modulus cannot be zero")
- if modulus < 0:
- raise ValueError("Modulus cannot be negative")
- r_p, r_n = self._value, modulus
- s_p, s_n = 1, 0
- while r_n > 0:
- q = r_p // r_n
- r_p, r_n = r_n, r_p - q * r_n
- s_p, s_n = s_n, s_p - q * s_n
- if r_p != 1:
- raise ValueError("No inverse value can be computed" + str(r_p))
- while s_p < 0:
- s_p += modulus
- self._value = s_p
+ self._value = inverse(self._value, int(modulus))
return self
def inverse(self, modulus):
@@ -342,11 +321,7 @@ class IntegerNative(IntegerBase):
return result
def gcd(self, term):
- r_p, r_n = abs(self._value), abs(int(term))
- while r_n > 0:
- q = r_p // r_n
- r_p, r_n = r_n, r_p - q * r_n
- return self.__class__(r_p)
+ return self.__class__(GCD(abs(self._value), abs(int(term))))
def lcm(self, term):
term = int(term)
@@ -393,3 +368,15 @@ class IntegerNative(IntegerBase):
n1 = n % a1
# Step 8
return s * IntegerNative.jacobi_symbol(n1, a1)
+
+ @staticmethod
+ def _mult_modulo_bytes(term1, term2, modulus):
+ if modulus < 0:
+ raise ValueError("Modulus must be positive")
+ if modulus == 0:
+ raise ZeroDivisionError("Modulus cannot be zero")
+ if (modulus & 1) == 0:
+ raise ValueError("Odd modulus is required")
+
+ number_len = len(long_to_bytes(modulus))
+ return long_to_bytes((term1 * term2) % modulus, number_len)
diff --git a/frozen_deps/Cryptodome/Math/_modexp.abi3.so b/frozen_deps/Cryptodome/Math/_modexp.abi3.so
index 3e0e3b2..d11de72 100755
--- a/frozen_deps/Cryptodome/Math/_modexp.abi3.so
+++ b/frozen_deps/Cryptodome/Math/_modexp.abi3.so
Binary files differ
diff --git a/frozen_deps/Cryptodome/Math/_modexp.cpython-39-x86_64-linux-gnu.so b/frozen_deps/Cryptodome/Math/_modexp.cpython-39-x86_64-linux-gnu.so
deleted file mode 100755
index bb3667e..0000000
--- a/frozen_deps/Cryptodome/Math/_modexp.cpython-39-x86_64-linux-gnu.so
+++ /dev/null
Binary files differ