@@ -416,6 +416,13 @@ def __ne__(self, other: typing.Any) -> bool:
416416 def __hash__ (self ) -> int :
417417 return hash ((self .n , self .e , self .d , self .p , self .q , self .exp1 , self .exp2 , self .coef ))
418418
419+ def _get_blinding_factor (self ) -> int :
420+ for _ in range (1000 ):
421+ blind_r = rsa .randnum .randint (self .n - 1 )
422+ if rsa .prime .are_relatively_prime (self .n , blind_r ):
423+ return blind_r
424+ raise RuntimeError ('unable to find blinding factor' )
425+
419426 def blinded_decrypt (self , encrypted : int ) -> int :
420427 """Decrypts the message using blinding to prevent side-channel attacks.
421428
@@ -426,7 +433,7 @@ def blinded_decrypt(self, encrypted: int) -> int:
426433 :rtype: int
427434 """
428435
429- blind_r = rsa . randnum . randint ( self .n - 1 )
436+ blind_r = self ._get_blinding_factor ( )
430437 blinded = self .blind (encrypted , blind_r ) # blind before decrypting
431438 decrypted = rsa .core .decrypt_int (blinded , self .d , self .n )
432439
@@ -442,7 +449,7 @@ def blinded_encrypt(self, message: int) -> int:
442449 :rtype: int
443450 """
444451
445- blind_r = rsa . randnum . randint ( self .n - 1 )
452+ blind_r = self ._get_blinding_factor ( )
446453 blinded = self .blind (message , blind_r ) # blind before encrypting
447454 encrypted = rsa .core .encrypt_int (blinded , self .d , self .n )
448455 return self .unblind (encrypted , blind_r )
0 commit comments