Skip to content

Commit a6811b2

Browse files
committed
refactor RC2 support to work in an upcoming cryptography release
1 parent dedddd9 commit a6811b2

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

scapy/layers/tls/crypto/cipher_block.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
from cryptography.hazmat.primitives.ciphers import (Cipher, algorithms, modes, # noqa: E501
2121
BlockCipherAlgorithm,
2222
CipherAlgorithm)
23-
from cryptography.hazmat.backends.openssl.backend import (backend,
24-
GetCipherByName)
23+
from cryptography.hazmat.backends.openssl.backend import backend
2524

2625

2726
_tls_block_cipher_algs = {}
@@ -191,24 +190,41 @@ class Cipher_SEED_CBC(_BlockCipher):
191190
# silently not declared, and the corresponding suites will have 'usable' False.
192191

193192
if conf.crypto_valid:
194-
class _ARC2(BlockCipherAlgorithm, CipherAlgorithm):
195-
name = "RC2"
196-
block_size = 64
197-
key_sizes = frozenset([128])
198-
199-
def __init__(self, key):
200-
self.key = algorithms._verify_key_size(self, key)
201-
202-
@property
203-
def key_size(self):
204-
return len(self.key) * 8
205-
206-
_gcbn_format = "{cipher.name}-{mode.name}"
207-
if GetCipherByName(_gcbn_format)(backend, _ARC2, modes.CBC) != \
208-
backend._ffi.NULL:
209-
193+
try:
194+
from cryptography.hazmat.decrepit.ciphers.algorithms import RC2
195+
rc2_available = backend.cipher_supported(
196+
RC2(b"0" * 16), modes.CBC(b"0" * 8)
197+
)
198+
except ImportError:
199+
# Legacy path for cryptography < 43.0.0
200+
from cryptography.hazmat.backends.openssl.backend import (
201+
GetCipherByName
202+
)
203+
_gcbn_format = "{cipher.name}-{mode.name}"
204+
205+
class RC2(BlockCipherAlgorithm, CipherAlgorithm):
206+
name = "RC2"
207+
block_size = 64
208+
key_sizes = frozenset([128])
209+
210+
def __init__(self, key):
211+
self.key = algorithms._verify_key_size(self, key)
212+
213+
@property
214+
def key_size(self):
215+
return len(self.key) * 8
216+
if GetCipherByName(_gcbn_format)(backend, RC2, modes.CBC) != \
217+
backend._ffi.NULL:
218+
rc2_available = True
219+
backend.register_cipher_adapter(RC2,
220+
modes.CBC,
221+
GetCipherByName(_gcbn_format))
222+
else:
223+
rc2_available = False
224+
225+
if rc2_available:
210226
class Cipher_RC2_CBC(_BlockCipher):
211-
pc_cls = _ARC2
227+
pc_cls = RC2
212228
pc_cls_mode = modes.CBC
213229
block_size = 8
214230
key_len = 16
@@ -217,10 +233,6 @@ class Cipher_RC2_CBC_40(Cipher_RC2_CBC):
217233
expanded_key_len = 16
218234
key_len = 5
219235

220-
backend.register_cipher_adapter(Cipher_RC2_CBC.pc_cls,
221-
Cipher_RC2_CBC.pc_cls_mode,
222-
GetCipherByName(_gcbn_format))
223-
224236
_sslv2_block_cipher_algs["RC2_128_CBC"] = Cipher_RC2_CBC
225237

226238

0 commit comments

Comments
 (0)