Skip to content

Commit c211285

Browse files
authored
Address TripleDES + Camellia deprecations (#4881)
1 parent 8e7e73b commit c211285

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

scapy/layers/ipsec.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,17 @@ def data_for_encryption(self):
215215
)
216216
except ImportError:
217217
decrepit_algorithms = algorithms
218+
219+
# cryptography's TripleDES can be used to simulate DES behavior
220+
DES = lambda key: decrepit_algorithms.TripleDES(key * 3)
221+
DES.key_sizes = decrepit_algorithms.TripleDES.key_sizes
222+
DES.block_size = decrepit_algorithms.TripleDES.block_size
218223
else:
219224
log_loading.info("Can't import python-cryptography v1.7+. "
220225
"Disabled IPsec encryption/authentication.")
221226
default_backend = None
222227
InvalidTag = Exception
223-
Cipher = algorithms = modes = None
228+
Cipher = algorithms = modes = DES = None
224229

225230
###############################################################################
226231

@@ -573,9 +578,9 @@ def decrypt(self, sa, esp, key, icv_size=None, esn_en=False, esn=0):
573578
format_mode_iv=_salt_format_mode_iv) # noqa: E501
574579

575580
# Using a TripleDES cipher algorithm for DES is done by using the same 64
576-
# bits key 3 times (done by cryptography when given a 64 bits key)
581+
# bits key 3 times
577582
CRYPT_ALGOS['DES'] = CryptAlgo('DES',
578-
cipher=decrepit_algorithms.TripleDES,
583+
cipher=DES,
579584
mode=modes.CBC,
580585
key_size=(8,))
581586
CRYPT_ALGOS['3DES'] = CryptAlgo('3DES',

scapy/layers/tls/crypto/cipher_block.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
except ImportError:
3434
decrepit_algorithms = algorithms
3535

36+
# cryptography's TripleDES can be used to simulate DES behavior
37+
DES = lambda key: decrepit_algorithms.TripleDES(key * 3)
38+
39+
try:
40+
# cryptography > 47.0
41+
Camellia = decrepit_algorithms.Camellia
42+
except AttributeError:
43+
Camellia = algorithms.Camellia
44+
3645

3746
_tls_block_cipher_algs = {}
3847

@@ -134,7 +143,7 @@ class Cipher_AES_256_CBC(Cipher_AES_128_CBC):
134143
key_len = 32
135144

136145
class Cipher_CAMELLIA_128_CBC(_BlockCipher):
137-
pc_cls = algorithms.Camellia
146+
pc_cls = Camellia
138147
pc_cls_mode = modes.CBC
139148
block_size = 16
140149
key_len = 16
@@ -149,13 +158,13 @@ class Cipher_CAMELLIA_256_CBC(Cipher_CAMELLIA_128_CBC):
149158

150159
if conf.crypto_valid:
151160
class Cipher_DES_ECB(_BlockCipher):
152-
pc_cls = decrepit_algorithms.TripleDES
161+
pc_cls = staticmethod(DES)
153162
pc_cls_mode = modes.ECB
154163
block_size = 8
155164
key_len = 8
156165

157166
class Cipher_DES_CBC(_BlockCipher):
158-
pc_cls = decrepit_algorithms.TripleDES
167+
pc_cls = staticmethod(DES)
159168
pc_cls_mode = modes.CBC
160169
block_size = 8
161170
key_len = 8

scapy/libs/rfc3961.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@
9797
raise ImportError("To use kerberos cryptography, you need to install cryptography.")
9898

9999

100-
# cryptography's TripleDES allow the usage of a 56bit key, which thus behaves like DES
101-
DES = decrepit_algorithms.TripleDES
100+
# cryptography's TripleDES can be used to simulate DES behavior
101+
def DES(key: bytes) -> decrepit_algorithms.TripleDES:
102+
return decrepit_algorithms.TripleDES(key * 3)
102103

103104

104105
# https://go.microsoft.com/fwlink/?LinkId=186039

0 commit comments

Comments
 (0)