Add opt-in weak-key bypass for DES3 - #919
Open
shashank-amireddy wants to merge 5 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces an opt-in escape hatch for legacy interoperability by adding allow_weak_keys=True to Crypto.Cipher.DES3.new() (and plumbing it into adjust_key_parity) so callers can bypass the existing Triple-DES key degeneracy rejection. The default behavior remains unchanged (degenerate keys still raise ValueError unless explicitly allowed).
Changes:
- Add
allow_weak_keysplumbing throughDES3.new()→_create_base_cipher()→adjust_key_parity(). - Update public type stub and DES3 documentation to describe the new opt-in parameter.
- Add a self-test intended to verify
allow_weak_keys=Truepermits degenerate keys (but it is currently not executed by the module’sget_tests()harness).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/Crypto/SelfTest/Cipher/test_DES3.py | Adds a self-test for allow_weak_keys=True (currently skipped by get_tests() as written). |
| lib/Crypto/Cipher/DES3.pyi | Updates typing stubs for adjust_key_parity() and new() to include allow_weak_keys. |
| lib/Crypto/Cipher/DES3.py | Implements the opt-in bypass for degenerate 3DES keys and documents the new kwarg. |
| Doc/src/cipher/des3.rst | Documents the new allow_weak_keys=True escape hatch for legacy use. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+137
to
+141
| @@ -138,6 +138,23 @@ def runTest(self): | |||
| sub_key1 + sub_key2 + strxor_c(sub_key2, 0x1), | |||
| DES3.MODE_ECB) | |||
|
|
|||
| def test_allow_weak_keys(self): | |||
Comment on lines
184
to
186
| allow_weak_keys = kwargs.pop("allow_weak_keys", False) | ||
| kwargs["allow_weak_keys"] = allow_weak_keys | ||
| return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs) |
Comment on lines
+33
to
35
| allow_weak_keys : bool = ..., | ||
| counter : Dict = ...) -> \ | ||
| Union[EcbMode, CbcMode, CfbMode, OfbMode, CtrMode, OpenPgpMode]: ... |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment on lines
184
to
186
| allow_weak_keys = kwargs.pop("allow_weak_keys", False) | ||
| kwargs["allow_weak_keys"] = allow_weak_keys | ||
| return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs) |
Comment on lines
+31
to
+36
| **By default, this implementation purposefully fails when attempting to | ||
| configure the cipher in Option 3.** | ||
|
|
||
| If you need to use such keys for legacy interoperability, pass | ||
| ``allow_weak_keys=True`` to :func:`Crypto.Cipher.DES3.new` to bypass this | ||
| check. |
| from Crypto.Cipher._mode_eax import EaxMode | ||
|
|
||
| def adjust_key_parity(key_in: bytes) -> bytes: ... | ||
| def adjust_key_parity(key_in: bytes, allow_weak_keys: bool = ...) -> bytes: ... |
Author
|
Hi @Legrandin , since the MyPy integration check issue has been fixed, could you please rerun the workflow for my PR and, if everything passes, merge the branch? Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds an opt-in
allow_weak_keys=Trueparameter toCrypto.Cipher.DES3.new()so callers with legacy interoperability requirements can bypass the degeneracy check that rejects Triple DES keys which collapse to single DES.By default, the existing secure behavior is unchanged: weak keys still raise
ValueError. The change is covered by a regression test and the public type stub and docs were updated to match.Fixes #720
Testing
python -m py_compile DES3.py lib/Crypto/SelfTest/Cipher/test_DES3.pyallow_weak_keys=True