@@ -417,6 +417,13 @@ def __ne__(self, other):
417
417
def __hash__ (self ):
418
418
return hash ((self .n , self .e , self .d , self .p , self .q , self .exp1 , self .exp2 , self .coef ))
419
419
420
+ def _get_blinding_factor (self ):
421
+ for _ in range (1000 ):
422
+ blind_r = rsa .randnum .randint (self .n - 1 )
423
+ if rsa .prime .are_relatively_prime (self .n , blind_r ):
424
+ return blind_r
425
+ raise RuntimeError ('unable to find blinding factor' )
426
+
420
427
def blinded_decrypt (self , encrypted ):
421
428
"""Decrypts the message using blinding to prevent side-channel attacks.
422
429
@@ -427,7 +434,7 @@ def blinded_decrypt(self, encrypted):
427
434
:rtype: int
428
435
"""
429
436
430
- blind_r = rsa . randnum . randint ( self .n - 1 )
437
+ blind_r = self ._get_blinding_factor ( )
431
438
blinded = self .blind (encrypted , blind_r ) # blind before decrypting
432
439
decrypted = rsa .core .decrypt_int (blinded , self .d , self .n )
433
440
@@ -443,7 +450,7 @@ def blinded_encrypt(self, message):
443
450
:rtype: int
444
451
"""
445
452
446
- blind_r = rsa . randnum . randint ( self .n - 1 )
453
+ blind_r = self ._get_blinding_factor ( )
447
454
blinded = self .blind (message , blind_r ) # blind before encrypting
448
455
encrypted = rsa .core .encrypt_int (blinded , self .d , self .n )
449
456
return self .unblind (encrypted , blind_r )
0 commit comments