Skip to content

Commit 42a2de4

Browse files
authored
Use constant time comparison when available (#59)
* Use constant time comparison when available * Update setup.py
1 parent d4fb132 commit 42a2de4

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

cryptolens_python2.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,17 @@ def EMSA_PKCS1_V15_ENCODE(M, emLen):
8181
def RSAASSA_PKCS1_V15_VERIFY((n,e), M, S):
8282
s = HelperMethods.OS2IP(S)
8383
m = HelperMethods.RSAVP1((n,e), s)
84-
if m is None:return False
84+
if m is None: return False
8585
EM = HelperMethods.I2OSP(m, 256)
8686
if EM is None: return False
87-
EM2 = HelperMethods.EMSA_PKCS1_V15_ENCODE(M, 256) # Can return None, but it's OK since EM is not None
88-
return EM == EM2
87+
EM2 = HelperMethods.EMSA_PKCS1_V15_ENCODE(M, 256)
88+
if EM2 is None: return False
89+
90+
try:
91+
import hmac
92+
return hmac.compare_digest(EM, EM2)
93+
except (ImportError, AttributeError):
94+
return EM == EM2
8995

9096

9197
@staticmethod
@@ -590,4 +596,4 @@ def from_string(rsaPubKeyString):
590596
<RSAKeyValue><Modulus>...</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
591597
"""
592598
rsaKey = xml.etree.ElementTree.fromstring(rsaPubKeyString)
593-
return RSAPublicKey(rsaKey.find('Modulus').text, rsaKey.find('Exponent').text)
599+
return RSAPublicKey(rsaKey.find('Modulus').text, rsaKey.find('Exponent').text)

licensing/internal.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,15 @@ def RSAASSA_PKCS1_V15_VERIFY(pair, M, S):
9191
if m is None: return False
9292
EM = HelperMethods.I2OSP(m, 256)
9393
if EM is None: return False
94-
EM2 = HelperMethods.EMSA_PKCS1_V15_ENCODE(M, 256) # Can return None, but it's OK since EM is not None
95-
return EM == EM2
96-
94+
EM2 = HelperMethods.EMSA_PKCS1_V15_ENCODE(M, 256)
95+
if EM2 is None: return False
96+
97+
try:
98+
import hmac
99+
return hmac.compare_digest(EM, EM2)
100+
except (ImportError, AttributeError):
101+
return EM == EM2
102+
97103
@staticmethod
98104
def verify_signature(response, rsaPublicKey):
99105
"""

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
setup(
33
name = 'licensing', # How you named your package folder (MyLib)
44
packages = ['licensing'], # Chose the same as "name"
5-
version = '0.39', # Start with a small number and increase it with every change you make
5+
version = '0.40', # Start with a small number and increase it with every change you make
66
license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository
77
description = 'Client library for Cryptolens licensing Web API.', # Give a short description about your library
88
author = 'Cryptolens AB', # Type in your name
99
author_email = '[email protected]', # Type in your E-Mail
1010
url = 'https://cryptolens.io', # Provide either the link to your github or to your website
11-
download_url = 'https://github.com/Cryptolens/cryptolens-python/archive/v_39.tar.gz', # I explain this later on
11+
download_url = 'https://github.com/Cryptolens/cryptolens-python/archive/v_40.tar.gz', # I explain this later on
1212
keywords = ['software licensing', 'licensing library', 'cryptolens'], # Keywords that define your package best
1313
classifiers=[
1414
#'Development Status :: 5 - Stable', # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package

0 commit comments

Comments
 (0)