-
Notifications
You must be signed in to change notification settings - Fork 31
Fenrir fixes (2026-06-23) #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
julek-wolfssl
wants to merge
4
commits into
wolfSSL:master
Choose a base branch
from
julek-wolfssl:fenrir/20260623
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+269
−29
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
53b3f6c
Remove contradictory cipher mode validation (F-4015)
julek-wolfssl 39acd41
Reject ChaCha encrypt/decrypt before set_iv (F-4463)
julek-wolfssl 2ecf721
Reject unsafe HMAC copy() (F-5428)
julek-wolfssl b13aeea
Address Copilot review on wolfcrypt-py (F-4015, F-4463)
julek-wolfssl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # test_chacha_iv.py | ||
| # | ||
| # Copyright (C) 2006-2022 wolfSSL Inc. | ||
| # | ||
| # This file is part of wolfSSL. (formerly known as CyaSSL) | ||
| # | ||
| # wolfSSL is free software; you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation; either version 2 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # wolfSSL is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program; if not, write to the Free Software | ||
| # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | ||
|
|
||
| # pylint: disable=missing-docstring, import-error | ||
|
|
||
| import pytest | ||
| from wolfcrypt._ffi import lib as _lib | ||
| from wolfcrypt.exceptions import WolfCryptError | ||
|
|
||
| pytestmark = pytest.mark.skipif( | ||
| not _lib.CHACHA_ENABLED, reason="ChaCha not enabled") | ||
|
|
||
| KEY = b"\x01" * 32 | ||
| NONCE = b"\x02" * 12 | ||
|
|
||
|
|
||
| def test_encrypt_before_set_iv_raises(): | ||
| """ | ||
| F-4463: encrypt() before set_iv() must not feed an empty IV buffer to | ||
| wc_Chacha_SetIV (which unconditionally reads 12 bytes). It must raise. | ||
| """ | ||
| from wolfcrypt.ciphers import ChaCha | ||
|
|
||
| cipher = ChaCha(KEY) | ||
| with pytest.raises(WolfCryptError): | ||
| cipher.encrypt(b"A" * 16) | ||
|
|
||
|
|
||
| def test_decrypt_before_set_iv_raises(): | ||
| from wolfcrypt.ciphers import ChaCha | ||
|
|
||
| cipher = ChaCha(KEY) | ||
| with pytest.raises(WolfCryptError): | ||
| cipher.decrypt(b"A" * 16) | ||
|
|
||
|
|
||
| def test_encrypt_decrypt_after_set_iv_roundtrips(): | ||
| from wolfcrypt.ciphers import ChaCha | ||
|
|
||
| enc = ChaCha(KEY) | ||
| enc.set_iv(NONCE) | ||
| plaintext = b"the quick brown fox" | ||
| ciphertext = enc.encrypt(plaintext) | ||
|
|
||
| dec = ChaCha(KEY) | ||
| dec.set_iv(NONCE) | ||
| assert dec.decrypt(ciphertext) == plaintext | ||
|
|
||
|
|
||
| def test_failed_set_iv_keeps_encrypt_blocked(monkeypatch): | ||
| """ | ||
| If re-keying fails inside set_iv(), the IV must be treated as not set so | ||
| encrypt()/decrypt() stay blocked rather than running with a stale or | ||
| partially-applied IV. | ||
| """ | ||
| from wolfcrypt.ciphers import ChaCha | ||
|
|
||
| cipher = ChaCha(KEY) | ||
| # First, establish a valid IV so a later failure would otherwise leave | ||
| # _iv_set True under the old ordering. | ||
| cipher.set_iv(NONCE) | ||
|
|
||
| monkeypatch.setattr(cipher, "_set_key", lambda direction: -1) | ||
| with pytest.raises(WolfCryptError): | ||
| cipher.set_iv(NONCE) | ||
| monkeypatch.undo() # restore real _set_key | ||
|
|
||
| # The failed re-key must have cleared the "IV is set" state, so encrypt() | ||
| # refuses here. Under the old ordering _iv_set stayed True and this | ||
| # encrypt() would instead run with a stale IV. | ||
| with pytest.raises(WolfCryptError): | ||
| cipher.encrypt(b"A" * 16) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # test_cipher_modes.py | ||
| # | ||
| # Copyright (C) 2006-2022 wolfSSL Inc. | ||
| # | ||
| # This file is part of wolfSSL. (formerly known as CyaSSL) | ||
| # | ||
| # wolfSSL is free software; you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation; either version 2 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # wolfSSL is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program; if not, write to the Free Software | ||
| # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | ||
|
|
||
| # pylint: disable=missing-docstring, import-error, protected-access | ||
|
|
||
| import pytest | ||
| from wolfcrypt.ciphers import ( | ||
| _FEEDBACK_MODES, | ||
| MODE_CBC, MODE_CTR, MODE_ECB, MODE_CFB, MODE_OFB, | ||
| ) | ||
| from wolfcrypt._ffi import lib as _lib | ||
|
|
||
|
|
||
| def test_feedback_modes_only_advertises_supported(): | ||
| """ | ||
| F-4015: _FEEDBACK_MODES is used as the "supported modes" gate in | ||
| _Cipher.__init__. It must not advertise modes that the constructor | ||
| then turns around and rejects. | ||
| """ | ||
| assert MODE_CBC in _FEEDBACK_MODES | ||
| assert MODE_CTR in _FEEDBACK_MODES | ||
| for unsupported in (MODE_ECB, MODE_CFB, MODE_OFB): | ||
| assert unsupported not in _FEEDBACK_MODES | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not _lib.AES_ENABLED, reason="AES not enabled") | ||
| def test_unsupported_mode_gives_single_consistent_error(): | ||
| """ | ||
| F-4015: previously MODE_ECB passed the first "is supported" check and | ||
| then hit a contradictory "not supported by this cipher" branch. The | ||
| rejection must now be a single, consistent message. | ||
| """ | ||
| from wolfcrypt.ciphers import Aes | ||
|
|
||
| key = b"0" * 16 | ||
| iv = b"0" * 16 | ||
| with pytest.raises(ValueError) as exc_info: | ||
| Aes.new(key, MODE_ECB, iv) | ||
|
|
||
| assert "by this cipher" not in str(exc_info.value) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # test_hmac_copy.py | ||
| # | ||
| # Copyright (C) 2006-2022 wolfSSL Inc. | ||
| # | ||
| # This file is part of wolfSSL. (formerly known as CyaSSL) | ||
| # | ||
| # wolfSSL is free software; you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation; either version 2 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # wolfSSL is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program; if not, write to the Free Software | ||
| # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA | ||
|
|
||
| # pylint: disable=missing-docstring, import-error | ||
|
|
||
| import pytest | ||
| from wolfcrypt._ffi import lib as _lib | ||
|
|
||
| pytestmark = pytest.mark.skipif( | ||
| not (_lib.HMAC_ENABLED and _lib.SHA256_ENABLED), | ||
| reason="HMAC-SHA256 not enabled") | ||
|
|
||
| KEY = b"wolfCrypt is the best crypto around" | ||
|
|
||
|
|
||
| def test_hmac_copy_raises_not_implemented(): | ||
| """ | ||
| F-5428: _Hmac inherited _Hash.copy(), which for HMAC fell back to a | ||
| byte-level memmove and returned an object aliasing the original's C | ||
| state (use-after-free risk in async/HW builds). wolfCrypt has no safe | ||
| public copy, so HMAC copy() must refuse rather than alias. | ||
| """ | ||
| from wolfcrypt.hashes import HmacSha256 | ||
|
|
||
| hmac = HmacSha256.new(KEY, b"some message") | ||
| with pytest.raises(NotImplementedError): | ||
| hmac.copy() | ||
|
|
||
|
|
||
| def test_hmac_digest_unaffected_by_copy_removal(): | ||
| """digest()/hexdigest() must keep working and remain repeatable.""" | ||
| from wolfcrypt.hashes import HmacSha256 | ||
|
|
||
| hmac = HmacSha256.new(KEY, b"some message") | ||
| first = hmac.hexdigest() | ||
| second = hmac.hexdigest() | ||
| assert first == second | ||
|
|
||
| hmac.update(b" more") | ||
| assert hmac.hexdigest() != first |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.