|
6 | 6 |
|
7 | 7 | import collections |
8 | 8 | import contextlib |
9 | | -import itertools |
10 | 9 | import typing |
11 | 10 |
|
12 | 11 | from cryptography import utils, x509 |
|
33 | 32 | ) |
34 | 33 | from cryptography.hazmat.primitives.ciphers.algorithms import ( |
35 | 34 | AES, |
36 | | - AES128, |
37 | | - AES256, |
38 | | - ARC4, |
39 | | - SM4, |
40 | | - Camellia, |
41 | | - ChaCha20, |
42 | | - TripleDES, |
43 | | - _BlowfishInternal, |
44 | | - _CAST5Internal, |
45 | | - _IDEAInternal, |
46 | | - _SEEDInternal, |
47 | 35 | ) |
48 | 36 | from cryptography.hazmat.primitives.ciphers.modes import ( |
49 | 37 | CBC, |
50 | | - CFB, |
51 | | - CFB8, |
52 | | - CTR, |
53 | | - ECB, |
54 | | - GCM, |
55 | | - OFB, |
56 | | - XTS, |
57 | 38 | Mode, |
58 | 39 | ) |
59 | 40 | from cryptography.hazmat.primitives.serialization.pkcs12 import ( |
|
69 | 50 |
|
70 | 51 | # Not actually supported, just used as a marker for some serialization tests. |
71 | 52 | class _RC2: |
72 | | - pass |
| 53 | + key_size = 128 |
73 | 54 |
|
74 | 55 |
|
75 | 56 | class Backend: |
@@ -132,7 +113,6 @@ def __init__(self) -> None: |
132 | 113 | tuple[type[CipherAlgorithm], type[Mode]], |
133 | 114 | typing.Callable, |
134 | 115 | ] = {} |
135 | | - self._register_default_ciphers() |
136 | 116 | self._dh_types = [self._lib.EVP_PKEY_DH] |
137 | 117 | if self._lib.Cryptography_HAS_EVP_PKEY_DHX: |
138 | 118 | self._dh_types.append(self._lib.EVP_PKEY_DHX) |
@@ -220,93 +200,7 @@ def cipher_supported(self, cipher: CipherAlgorithm, mode: Mode) -> bool: |
220 | 200 | if not isinstance(cipher, self._fips_ciphers): |
221 | 201 | return False |
222 | 202 |
|
223 | | - try: |
224 | | - adapter = self._cipher_registry[type(cipher), type(mode)] |
225 | | - except KeyError: |
226 | | - return False |
227 | | - evp_cipher = adapter(self, cipher, mode) |
228 | | - return self._ffi.NULL != evp_cipher |
229 | | - |
230 | | - def register_cipher_adapter(self, cipher_cls, mode_cls, adapter) -> None: |
231 | | - if (cipher_cls, mode_cls) in self._cipher_registry: |
232 | | - raise ValueError( |
233 | | - f"Duplicate registration for: {cipher_cls} {mode_cls}." |
234 | | - ) |
235 | | - self._cipher_registry[cipher_cls, mode_cls] = adapter |
236 | | - |
237 | | - def _register_default_ciphers(self) -> None: |
238 | | - for cipher_cls in [AES, AES128, AES256]: |
239 | | - for mode_cls in [CBC, CTR, ECB, OFB, CFB, CFB8, GCM]: |
240 | | - self.register_cipher_adapter( |
241 | | - cipher_cls, |
242 | | - mode_cls, |
243 | | - GetCipherByName( |
244 | | - "{cipher.name}-{cipher.key_size}-{mode.name}" |
245 | | - ), |
246 | | - ) |
247 | | - for mode_cls in [CBC, CTR, ECB, OFB, CFB]: |
248 | | - self.register_cipher_adapter( |
249 | | - Camellia, |
250 | | - mode_cls, |
251 | | - GetCipherByName("{cipher.name}-{cipher.key_size}-{mode.name}"), |
252 | | - ) |
253 | | - for mode_cls in [CBC, CFB, CFB8, OFB]: |
254 | | - self.register_cipher_adapter( |
255 | | - TripleDES, mode_cls, GetCipherByName("des-ede3-{mode.name}") |
256 | | - ) |
257 | | - self.register_cipher_adapter( |
258 | | - TripleDES, ECB, GetCipherByName("des-ede3") |
259 | | - ) |
260 | | - # ChaCha20 uses the Long Name "chacha20" in OpenSSL, but in LibreSSL |
261 | | - # it uses "chacha" |
262 | | - self.register_cipher_adapter( |
263 | | - ChaCha20, |
264 | | - type(None), |
265 | | - GetCipherByName( |
266 | | - "chacha" if self._lib.CRYPTOGRAPHY_IS_LIBRESSL else "chacha20" |
267 | | - ), |
268 | | - ) |
269 | | - self.register_cipher_adapter(AES, XTS, _get_xts_cipher) |
270 | | - for mode_cls in [ECB, CBC, OFB, CFB, CTR]: |
271 | | - self.register_cipher_adapter( |
272 | | - SM4, mode_cls, GetCipherByName("sm4-{mode.name}") |
273 | | - ) |
274 | | - # Don't register legacy ciphers if they're unavailable. Hypothetically |
275 | | - # this wouldn't be necessary because we test availability by seeing if |
276 | | - # we get an EVP_CIPHER * in the _CipherContext __init__, but OpenSSL 3 |
277 | | - # will return a valid pointer even though the cipher is unavailable. |
278 | | - if ( |
279 | | - self._binding._legacy_provider_loaded |
280 | | - or not self._lib.CRYPTOGRAPHY_OPENSSL_300_OR_GREATER |
281 | | - ): |
282 | | - for mode_cls in [CBC, CFB, OFB, ECB]: |
283 | | - self.register_cipher_adapter( |
284 | | - _BlowfishInternal, |
285 | | - mode_cls, |
286 | | - GetCipherByName("bf-{mode.name}"), |
287 | | - ) |
288 | | - for mode_cls in [CBC, CFB, OFB, ECB]: |
289 | | - self.register_cipher_adapter( |
290 | | - _SEEDInternal, |
291 | | - mode_cls, |
292 | | - GetCipherByName("seed-{mode.name}"), |
293 | | - ) |
294 | | - for cipher_cls, mode_cls in itertools.product( |
295 | | - [_CAST5Internal, _IDEAInternal], |
296 | | - [CBC, OFB, CFB, ECB], |
297 | | - ): |
298 | | - self.register_cipher_adapter( |
299 | | - cipher_cls, |
300 | | - mode_cls, |
301 | | - GetCipherByName("{cipher.name}-{mode.name}"), |
302 | | - ) |
303 | | - self.register_cipher_adapter( |
304 | | - ARC4, type(None), GetCipherByName("rc4") |
305 | | - ) |
306 | | - # We don't actually support RC2, this is just used by some tests. |
307 | | - self.register_cipher_adapter( |
308 | | - _RC2, type(None), GetCipherByName("rc2") |
309 | | - ) |
| 203 | + return rust_openssl.ciphers.cipher_supported(cipher, mode) |
310 | 204 |
|
311 | 205 | def pbkdf2_hmac_supported(self, algorithm: hashes.HashAlgorithm) -> bool: |
312 | 206 | return self.hmac_supported(algorithm) |
@@ -1108,34 +1002,4 @@ def _load_pkcs7_certificates(self, p7) -> list[x509.Certificate]: |
1108 | 1002 | return certs |
1109 | 1003 |
|
1110 | 1004 |
|
1111 | | -class GetCipherByName: |
1112 | | - def __init__(self, fmt: str): |
1113 | | - self._fmt = fmt |
1114 | | - |
1115 | | - def __call__(self, backend: Backend, cipher: CipherAlgorithm, mode: Mode): |
1116 | | - cipher_name = self._fmt.format(cipher=cipher, mode=mode).lower() |
1117 | | - evp_cipher = backend._lib.EVP_get_cipherbyname( |
1118 | | - cipher_name.encode("ascii") |
1119 | | - ) |
1120 | | - |
1121 | | - # try EVP_CIPHER_fetch if present |
1122 | | - if ( |
1123 | | - evp_cipher == backend._ffi.NULL |
1124 | | - and backend._lib.Cryptography_HAS_300_EVP_CIPHER |
1125 | | - ): |
1126 | | - evp_cipher = backend._lib.EVP_CIPHER_fetch( |
1127 | | - backend._ffi.NULL, |
1128 | | - cipher_name.encode("ascii"), |
1129 | | - backend._ffi.NULL, |
1130 | | - ) |
1131 | | - |
1132 | | - backend._consume_errors() |
1133 | | - return evp_cipher |
1134 | | - |
1135 | | - |
1136 | | -def _get_xts_cipher(backend: Backend, cipher: AES, mode): |
1137 | | - cipher_name = f"aes-{cipher.key_size // 2}-xts" |
1138 | | - return backend._lib.EVP_get_cipherbyname(cipher_name.encode("ascii")) |
1139 | | - |
1140 | | - |
1141 | 1005 | backend = Backend() |
0 commit comments