Skip to content

Commit

Permalink
Merge pull request #297 from tomato42/multithread_rsa
Browse files Browse the repository at this point in the history
make Python_RSAKey multithread safe
  • Loading branch information
tomato42 committed Jul 30, 2018
2 parents 3029e01 + d1c2bc7 commit 0c1b894
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions tlslite/utils/python_rsakey.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See the LICENSE file for legal information regarding use of this file.

"""Pure-Python RSA implementation."""

import threading
from .cryptomath import *
from .asn1parser import ASN1Parser
from .rsakey import *
Expand Down Expand Up @@ -33,34 +33,37 @@ def __init__(self, n=0, e=0, d=0, p=0, q=0, dP=0, dQ=0, qInv=0):
self.qInv = qInv
self.blinder = 0
self.unblinder = 0
self._lock = threading.Lock()

def hasPrivateKey(self):
return self.d != 0

def _rawPrivateKeyOp(self, m):
#Create blinding values, on the first pass:
if not self.blinder:
self.unblinder = getRandomNumber(2, self.n)
self.blinder = powMod(invMod(self.unblinder, self.n), self.e,
self.n)

#Blind the input
m = (m * self.blinder) % self.n

#Perform the RSA operation
with self._lock:
# Create blinding values, on the first pass:
if not self.blinder:
self.unblinder = getRandomNumber(2, self.n)
self.blinder = powMod(invMod(self.unblinder, self.n), self.e,
self.n)
unblinder = self.unblinder
blinder = self.blinder

# Update blinding values
self.blinder = (self.blinder * self.blinder) % self.n
self.unblinder = (self.unblinder * self.unblinder) % self.n

# Blind the input
m = (m * blinder) % self.n

# Perform the RSA operation
c = self._rawPrivateKeyOpHelper(m)

#Unblind the output
c = (c * self.unblinder) % self.n
# Unblind the output
c = (c * unblinder) % self.n

#Update blinding values
self.blinder = (self.blinder * self.blinder) % self.n
self.unblinder = (self.unblinder * self.unblinder) % self.n

#Return the output
# Return the output
return c


def _rawPrivateKeyOpHelper(self, m):
#Non-CRT version
#c = powMod(m, self.d, self.n)
Expand Down

0 comments on commit 0c1b894

Please sign in to comment.