Skip to content

Commit

Permalink
Choose blinding factor relatively prime to N
Browse files Browse the repository at this point in the history
This is a requirement for RSA blinding, but wasn't implemented yet.
  • Loading branch information
sybrenstuvel committed Apr 14, 2020
1 parent 1659432 commit 8ed5071
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Added support for SHA3 hashing: SHA3-256, SHA3-384, SHA3-512. This
is natively supported by Python 3.6+ and supported via a third-party
library on Python 3.5.
- Choose blinding factor relatively prime to N. Thanks Christian Heimes for pointing this out.


## Version 4.0 - released 2018-09-16
Expand Down
11 changes: 9 additions & 2 deletions rsa/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,13 @@ def __ne__(self, other: typing.Any) -> bool:
def __hash__(self) -> int:
return hash((self.n, self.e, self.d, self.p, self.q, self.exp1, self.exp2, self.coef))

def _get_blinding_factor(self) -> int:
for _ in range(1000):
blind_r = rsa.randnum.randint(self.n - 1)
if rsa.prime.are_relatively_prime(self.n, blind_r):
return blind_r
raise RuntimeError('unable to find blinding factor')

def blinded_decrypt(self, encrypted: int) -> int:
"""Decrypts the message using blinding to prevent side-channel attacks.
Expand All @@ -426,7 +433,7 @@ def blinded_decrypt(self, encrypted: int) -> int:
:rtype: int
"""

blind_r = rsa.randnum.randint(self.n - 1)
blind_r = self._get_blinding_factor()
blinded = self.blind(encrypted, blind_r) # blind before decrypting
decrypted = rsa.core.decrypt_int(blinded, self.d, self.n)

Expand All @@ -442,7 +449,7 @@ def blinded_encrypt(self, message: int) -> int:
:rtype: int
"""

blind_r = rsa.randnum.randint(self.n - 1)
blind_r = self._get_blinding_factor()
blinded = self.blind(message, blind_r) # blind before encrypting
encrypted = rsa.core.encrypt_int(blinded, self.d, self.n)
return self.unblind(encrypted, blind_r)
Expand Down

0 comments on commit 8ed5071

Please sign in to comment.