Skip to content

Commit 0913660

Browse files
authored
Merge pull request #411 from inikolcev/check_m2crypto_supported_ciphers
check if the ciphers are supported by m2crypto before using them
2 parents 1bb89a9 + a54e566 commit 0913660

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

tlslite/utils/cryptomath.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@
2727
# **************************************************************************
2828

2929
# Try to load M2Crypto/OpenSSL
30+
# pylint: disable=invalid-name
3031
try:
3132
from M2Crypto import m2
3233
m2cryptoLoaded = True
34+
M2CRYPTO_AES_CTR = False
35+
if hasattr(m2, 'aes_192_ctr'):
36+
M2CRYPTO_AES_CTR = True
3337

3438
try:
3539
with open('/proc/sys/crypto/fips_enabled', 'r') as fipsFile:
@@ -39,8 +43,13 @@
3943
# looks like we're running in container, likely not FIPS mode
4044
m2cryptoLoaded = True
4145

46+
# If AES-CBC is not available, don't use m2crypto
47+
if not hasattr(m2, 'aes_192_cbc'):
48+
m2cryptoLoaded = False
49+
4250
except ImportError:
4351
m2cryptoLoaded = False
52+
# pylint: enable=invalid-name
4453

4554
#Try to load GMPY
4655
try:

tlslite/utils/openssl_aes.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@
55

66
from .cryptomath import *
77
from .aes import *
8+
from .python_aes import Python_AES_CTR
89

910
if m2cryptoLoaded:
1011

1112
def new(key, mode, IV):
1213
# IV argument name is a part of the interface
1314
# pylint: disable=invalid-name
15+
"""
16+
Try using AES CTR from m2crpyto,
17+
if it is not available fall back to the
18+
python implementation.
19+
"""
1420
if mode == 2:
1521
return OpenSSL_AES(key, mode, IV)
1622
elif mode == 6:
17-
return OpenSSL_CTR(key, mode, IV)
23+
if M2CRYPTO_AES_CTR:
24+
return OpenSSL_CTR(key, mode, IV)
25+
return Python_AES_CTR(key, mode, IV)
1826
else:
1927
raise NotImplementedError()
2028

0 commit comments

Comments
 (0)