Skip to content

Commit 3163ff4

Browse files
committed
refactor RC2 support to work in an upcoming cryptography release
1 parent 16e1372 commit 3163ff4

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

scapy/layers/tls/crypto/cipher_block.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -191,36 +191,40 @@ class Cipher_SEED_CBC(_BlockCipher):
191191
# silently not declared, and the corresponding suites will have 'usable' False.
192192

193193
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-
194+
try:
195+
from cryptography.hazmat.decrepit.ciphers.algorithms import RC2
196+
rc2_available = backend.cipher_supported(RC2, modes.CBC)
197+
except ImportError:
198+
# Legacy path for cryptography < 43.0.0
199+
_gcbn_format = "{cipher.name}-{mode.name}"
200+
201+
class RC2(BlockCipherAlgorithm, CipherAlgorithm):
202+
name = "RC2"
203+
block_size = 64
204+
key_sizes = frozenset([128])
205+
206+
def __init__(self, key):
207+
self.key = algorithms._verify_key_size(self, key)
208+
209+
@property
210+
def key_size(self):
211+
return len(self.key) * 8
212+
if GetCipherByName(_gcbn_format)(backend, RC2, modes.CBC) != \
213+
backend._ffi.NULL:
214+
rc2_available = True
215+
backend.register_cipher_adapter(RC2,
216+
modes.CBC,
217+
GetCipherByName(_gcbn_format))
218+
else:
219+
rc2_available = False
220+
221+
if rc2_available:
210222
class Cipher_RC2_CBC(_BlockCipher):
211-
pc_cls = _ARC2
223+
pc_cls = RC2
212224
pc_cls_mode = modes.CBC
213225
block_size = 8
214226
key_len = 16
215227

216-
class Cipher_RC2_CBC_40(Cipher_RC2_CBC):
217-
expanded_key_len = 16
218-
key_len = 5
219-
220-
backend.register_cipher_adapter(Cipher_RC2_CBC.pc_cls,
221-
Cipher_RC2_CBC.pc_cls_mode,
222-
GetCipherByName(_gcbn_format))
223-
224228
_sslv2_block_cipher_algs["RC2_128_CBC"] = Cipher_RC2_CBC
225229

226230

0 commit comments

Comments
 (0)