Skip to content

Commit deab10c

Browse files
author
Ivan Nikolchev
committed
add m2crypto ctr, aesgcm and aesccm support
1 parent 33815fc commit deab10c

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

tlslite/utils/openssl_aes.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
def new(key, mode, IV):
1212
# IV argument name is a part of the interface
1313
# pylint: disable=invalid-name
14-
return OpenSSL_AES(key, mode, IV)
14+
if mode == 2:
15+
return OpenSSL_AES(key, mode, IV)
16+
elif mode == 6:
17+
return OpenSSL_CTR(key, mode, IV)
18+
else:
19+
raise NotImplementedError()
20+
1521

1622
class OpenSSL_AES(AES):
1723

@@ -58,3 +64,53 @@ def decrypt(self, ciphertext):
5864
def __del__(self):
5965
if self._context is not None:
6066
m2.cipher_ctx_free(self._context)
67+
68+
69+
class OpenSSL_CTR(AES):
70+
71+
def __init__(self, key, mode, IV):
72+
# IV argument/field names are a part of the interface
73+
# pylint: disable=invalid-name
74+
AES.__init__(self, key, mode, IV, "openssl")
75+
self._IV = IV
76+
self.key = key
77+
self._context = None
78+
self._encrypt = None
79+
if len(key) not in (16, 24, 32):
80+
raise AssertionError()
81+
82+
@property
83+
def counter(self):
84+
return self._IV
85+
86+
@counter.setter
87+
def counter(self, ctr):
88+
if self._context is not None:
89+
m2.cipher_ctx_free(self._context)
90+
self._IV = ctr
91+
self._init_context()
92+
93+
def _init_context(self, encrypt=True):
94+
if len(self.key) == 16:
95+
cipherType = m2.aes_128_ctr()
96+
if len(self.key) == 24:
97+
cipherType = m2.aes_192_ctr()
98+
if len(self.key) == 32:
99+
cipherType = m2.aes_256_ctr()
100+
self._context = m2.cipher_ctx_new()
101+
m2.cipher_init(self._context, cipherType, self.key, self._IV,
102+
int(encrypt))
103+
m2.cipher_set_padding(self._context, 0)
104+
self._encrypt = encrypt
105+
106+
def encrypt(self, plaintext):
107+
ciphertext = m2.cipher_update(self._context, plaintext)
108+
return bytearray(ciphertext)
109+
110+
def decrypt(self, ciphertext):
111+
plaintext = m2.cipher_update(self._context, ciphertext)
112+
return bytearray(plaintext)
113+
114+
def __del__(self):
115+
if self._context is not None:
116+
m2.cipher_ctx_free(self._context)

tlslite/utils/openssl_aesccm.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Author: Ivan Nikolchev
2+
# See the LICENSE file for legal information regarding use of this file.
3+
4+
"""AESCCM with CTR and CBC from m2crypto"""
5+
6+
from tlslite.utils.cryptomath import m2cryptoLoaded
7+
from tlslite.utils.aesccm import AESCCM
8+
from tlslite.utils import openssl_aes
9+
10+
11+
if m2cryptoLoaded:
12+
def new(key, tagLength=16):
13+
return OPENSSL_AESCCM(key, "openssl", bytearray(16), tagLength)
14+
15+
16+
class OPENSSL_AESCCM(AESCCM):
17+
def __init__(self, key, implementation, rawAesEncrypt, tagLength):
18+
super(OPENSSL_AESCCM, self).__init__(key, implementation, rawAesEncrypt, tagLength)
19+
20+
self._ctr = openssl_aes.new(key, 6, bytearray(b'\x00' * 16))
21+
self._cbc = openssl_aes.new(key, 2, bytearray(b'\x00' * 16))

tlslite/utils/openssl_aesgcm.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Author: Ivan Nikolchev
2+
# See the LICENSE file for legal information regarding use of this file.
3+
4+
"""AESGCM with CTR from m2crypto"""
5+
6+
from tlslite.utils.cryptomath import m2cryptoLoaded
7+
from tlslite.utils.aesgcm import AESGCM
8+
from tlslite.utils import openssl_aes
9+
from tlslite.utils.rijndael import Rijndael
10+
11+
if m2cryptoLoaded:
12+
def new(key):
13+
return OPENSSL_AESGCM(key, "openssl", Rijndael(key, 16).encrypt)
14+
15+
16+
class OPENSSL_AESGCM(AESGCM):
17+
def __init__(self, key, implementation, rawAesEncrypt):
18+
super(OPENSSL_AESGCM, self).__init__(key, implementation, rawAesEncrypt)
19+
20+
self._ctr = openssl_aes.new(key, 6, bytearray(b'\x00' * 16))

0 commit comments

Comments
 (0)