Skip to content

Add opt-in weak-key bypass for DES3 - #919

Open
shashank-amireddy wants to merge 5 commits into
Legrandin:masterfrom
shashank-amireddy:fix_720
Open

Add opt-in weak-key bypass for DES3#919
shashank-amireddy wants to merge 5 commits into
Legrandin:masterfrom
shashank-amireddy:fix_720

Conversation

@shashank-amireddy

Copy link
Copy Markdown

This PR adds an opt-in allow_weak_keys=True parameter to Crypto.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.py
  • Added a focused self-test for allow_weak_keys=True

Copilot AI review requested due to automatic review settings July 5, 2026 18:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_keys plumbing through DES3.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=True permits degenerate keys (but it is currently not executed by the module’s get_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 thread lib/Crypto/SelfTest/Cipher/test_DES3.py Outdated
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):

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread lib/Crypto/Cipher/DES3.py Outdated
Comment thread lib/Crypto/Cipher/DES3.py Outdated
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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread lib/Crypto/Cipher/DES3.py Outdated
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 thread Doc/src/cipher/des3.rst
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.
Comment thread lib/Crypto/Cipher/DES3.pyi Outdated
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: ...

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@shashank-amireddy

shashank-amireddy commented Jul 20, 2026

Copy link
Copy Markdown
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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

raise ValueError("Triple DES key degenerates to single DES")

2 participants