Commit Graph

122 Commits (a59a4f8b965c8ad42c6af95c80fc3873e77cd0cc)

Author SHA1 Message Date
sebastian-carpenter a59a4f8b96 update license to GPLv3 2026-07-09 18:24:23 +02:00
David Garske 905682a036
Merge pull request #130 from mjdemilliano/add-hashdrbg-reseed
Random: add DRBG reseed support
2026-07-08 12:28:45 -07:00
David Garske 1cb9b5d6c1
Merge pull request #133 from roberthdevries/pipeline-use-uv-and-ruff
Change workflow to using uv and ruff.
2026-07-08 12:26:11 -07:00
Robert de Vries 1a2829a4cd Fixed a few type annotations and added bad type tests.
Also added one note to the ChangeLog.
2026-07-08 15:22:37 +02:00
Martijn de Milliano c1be234aa6 Process review comments:
- Update lib.pyi
- Remove obsolete comment from test_reseed_multiple
- Update HASHDRBG detection logic to match random.h
2026-07-07 19:14:59 +02:00
Robert de Vries 04b58c0038 Fix ruff errors 2026-07-06 22:29:31 +02:00
Robert de Vries 147021d07c Enable and fix test_chacha_enc_dec. 2026-07-06 22:02:48 +02:00
Robert de Vries 00c57b5e91 Rebased on master and fixed any (extra) issues found with ruff and ty. 2026-07-06 22:02:48 +02:00
Robert de Vries 747243cc50 Address more review comments.
Tests are now also type checked as this helps verifying the correctness
of the type annotations.
2026-07-06 22:02:48 +02:00
Robert de Vries 1fb34e5cd8 Address review comments and annotate overrides. 2026-07-06 22:02:48 +02:00
Robert de Vries 2081edd6d7 Add typing information to ciphers.py
This has some fallout in random.py to simplify checks.
Also one test is slightly adapted to produced the desired failure.
2026-07-06 22:02:48 +02:00
Martijn de Milliano 499bfaa62f Process review comments
- Make test_reseed_multiple deterministic
- Add HAVE_HASHDRBG to windows/non_fips/user_settings.h
2026-07-05 16:30:18 +02:00
Robert de Vries 7edbd3e45c Enable more ruff rules requiring only minor or no fixes.
Extra rules:
All E pycodestyle error rules
C4   flake8-comprehensions
DTZ  flake8-datetimez
EXE  flake8-executable
FA   flake8-future-annotations
INT  flake8-gettext
ISC  flake8-implicit-str-concat
ICN  flake8-import-conventions
LOG  flake8-logging
G    flake8-logging-format
RSE  flake8-raise
SLOT flake8-slots
TID  flake8-tidy-imports
TC   flake8-type-checking
FLY  flynt
PERF Perflint
W    pycodestyle warnings
FURB refurb

Ignore E501:  line too long
2026-07-04 16:54:56 +02:00
David Garske c69d2f1eea
Merge pull request #134 from mjdemilliano/prepare-v5.9.2
Prepare for v5.9.2 release
2026-07-02 16:42:48 -07:00
Martijn de Milliano 0124b33202 Process review comments for random and test_random
- add pragma no cover
- format using ruff
- fix module factor in test_random
2026-06-30 20:47:41 +02:00
Martijn de Milliano 9cf73e0c42 Update skip reason for test_encrypt_short_tag 2026-06-30 18:55:36 +02:00
Martijn de Milliano 0c1e8f2b6a Update tests for reseeding 2026-06-30 17:45:11 +02:00
Martijn de Milliano 28b0c9a6bf Process review comments on test_encrypt_short_tag 2026-06-30 16:25:51 +02:00
Martijn de Milliano c4d926950c AES-GCM stream tests
Test response by library explicitly when called with unsupported
number of tag bytes.
2026-06-29 18:41:17 +02:00
Martijn de Milliano 0466adbf1b ML-DSA: Add missing test cases
- Test signing with empty context
- Test deterministic signing with context
2026-06-29 18:41:17 +02:00
Martijn de Milliano afcebbf946 Rename flag to ML_DSA_NO_CTX_ENABLED 2026-06-29 18:41:17 +02:00
Martijn de Milliano e6f4632030 Random: add DRBG reseed support 2026-06-26 14:26:46 +02:00
Martijn de Milliano fc5ea53605 Update AESGCM test
Read minimum supported tag size from settings and make the
test less ambiguous.
2026-06-26 00:31:05 +02:00
Martijn de Milliano aec3f2d553 Add tests for no-context signing 2026-06-26 00:10:34 +02:00
Juliusz Sosinowicz b13aeea62b Address Copilot review on wolfcrypt-py (F-4015, F-4463)
- ChaCha.set_iv(): only mark _iv_set after _set_key() succeeds, and clear
  it first, so a failed re-key cannot leave encrypt()/decrypt() unblocked
  with a stale or partially-applied IV. Add a regression test.
- Update _Cipher.new()/encrypt()/decrypt() docstrings that still referred
  to CFB/segment-size behavior to match the actually supported modes
  (MODE_CBC, MODE_CTR) and their IV requirements.
2026-06-24 08:48:54 +00:00
Juliusz Sosinowicz 2ecf721695 Reject unsafe HMAC copy() (F-5428)
_Hmac inherited _Hash.copy(), which - lacking a wolfCrypt copy function
for Hmac - fell back to a byte-level memmove and returned an object
marked _shallow_copy that aliases the original's internal C state. In
async or hardware-accelerated builds those internal pointers are shared,
so freeing the original leaves the copy with stale state
(use-after-free, wrong MACs, or corruption). wolfCrypt exposes no safe
public Hmac copy, so override copy() to raise NotImplementedError.
digest()/hexdigest() are unaffected. Update the shared hash tests to
expect this for HMAC.
2026-06-23 12:43:03 +00:00
Juliusz Sosinowicz 39acd415fc Reject ChaCha encrypt/decrypt before set_iv (F-4463)
ChaCha.__init__ leaves _IV_nonce empty and requires set_iv() before
use, but encrypt()/decrypt() (inherited from _Cipher) did not check
this. The first call ran _set_key(), which passed the empty nonce to
wc_Chacha_SetIV() - a function that unconditionally reads 12 bytes -
reading past the buffer and silently producing output with an
undefined IV. Track an _iv_set flag and override encrypt()/decrypt()
to raise WolfCryptError until set_iv() has been called.
2026-06-23 11:35:58 +00:00
Juliusz Sosinowicz 53b3f6cef9 Remove contradictory cipher mode validation (F-4015)
_FEEDBACK_MODES advertised MODE_ECB/MODE_CFB/MODE_OFB as supported, but
_Cipher.__init__ then rejected every mode other than CBC/CTR with a
contradictory 'not supported by this cipher' error after they had
already passed the 'is supported' check. Prune _FEEDBACK_MODES to the
modes the cipher actually implements (CBC, CTR) so unsupported modes
get a single, accurate rejection, and drop the now-dead else branch.
2026-06-23 11:30:00 +00:00
David Garske d36990dad3
Merge pull request #122 from roberthdevries/mark-unused-variable
Mark unused variable in test code with a leading underscore.
2026-06-08 15:44:52 -07:00
Martijn de Milliano eb93f657bf Test AESGCM stream: ignore failures for small auth tags
Smaller authentication tags may not be supported by the library.
This fix makes the test work for the default case that tags
should be minimum 12 bytes in size.
2026-06-04 18:05:58 +02:00
Martijn de Milliano 52886c4034 ML-DSA: Deal with signing without context not always supported
In the newer wolfSSL signing and verifying without context is
not available unless it is explicitly enabled.

This change modifies the Python binding and test suite to
accommodate this.
2026-06-04 18:03:35 +02:00
Robert de Vries b15d6f47c4 Mark unused variable in test code with a leading underscore. 2026-05-18 20:21:53 +02:00
Robert de Vries f3ae55b2b1 Fix duplicate and unsorted imports in test_ciphers.py 2026-05-18 19:47:25 +02:00
Robert de Vries 18701e3b62 Use format specifiers instead of percent format.
printf-style string formatting has a number of quirks, and leads to less
readable code than using str.format calls or f-strings.
In general, prefer the newer str.format and f-strings constructs over
printf-style string formatting.
2026-05-17 16:14:52 +02:00
Andrew Hutchings ef4195f99a Address Fenrir findings
F-3340: AesGcmStream.final decrypt path passed len(authTag) straight to
wc_AesGcmDecryptFinal, letting a caller truncate the verification window
(forgery probability ~2^-32 instead of 2^-128 for a 4-byte tag against a
16-byte configuration). Reject len(authTag) != self._tag_bytes and pass
self._tag_bytes to wolfSSL, mirroring ChaCha20Poly1305.decrypt. Added
test_decrypt_rejects_wrong_tag_length. Also fixed test_encrypt_short_tag
which was relying on the bug (decrypt side defaulted to tag_bytes=16
against a 12-byte tag).

F-3089: Declare label as const byte* in the wc_RsaPublicEncrypt_ex and
wc_RsaPrivateDecrypt_ex cdefs so CFFI can accept Python bytes without
exposing a writable pointer into immutable memory. wolfSSL does not
modify label.

F-3090: Declare nonce as const byte* in the wc_InitRngNonce and
wc_InitRngNonce_ex cdefs for the same reason.

F-1983, F-1984: Add minimum + upper bounds to requirements/{prod,test,
docs}.txt so a hijacked release of cffi, tox, pytest, types-cffi,
Sphinx, or sphinx_rtd_theme does not get pulled silently on the next
pip install. setup.txt resolves transitively via prod.txt.
2026-05-12 13:15:35 +01:00
Andrew Hutchings 98215b60f6 Address Copilot concerns 2026-05-11 13:05:23 +01:00
Andrew Hutchings 57cca0e90d Validate raw element lengths in EccPublic/EccPrivate.decode_key_raw
wc_ecc_import_unsigned takes no length parameters for qx/qy/d: it
reads exactly curve_size bytes from each pointer based on curve_id,
via mp_read_unsigned_bin in wc_ecc_import_raw_private. The Python
decode_key_raw wrappers handed the user-supplied buffers straight
through without any length check, so a shorter buffer caused the C
library to read past the end of the Python buffer (OOB read of
adjacent memory, potentially leaking it into the imported key or
segfaulting). A longer buffer silently dropped the extra bytes.

Add wc_ecc_get_curve_size_from_id to the CFFI cdef, then in both
decode_key_raw methods t2b the inputs, look up the expected curve
size, and raise ValueError if any of qx/qy/d does not match. Reject
unknown curve_id values with a clear message rather than falling
through to wolfSSL with a bogus size.

Add test_ecc_decode_key_raw_rejects_wrong_length covering short qx,
long qy, short d, unknown curve_id, and the happy path on both
EccPublic and EccPrivate.

F-3088
2026-05-11 12:33:41 +01:00
Andrew Hutchings 5e01d07e7f Fix ChaCha._set_key wiping encrypt stream state on first decrypt
ChaCha._set_key ignored its `direction` argument and unconditionally
re-keyed both self._enc and self._dec whenever either was allocated.
Because _Cipher.encrypt and _Cipher.decrypt only call _set_key the
first time their respective context is allocated, doing
`encrypt(...); decrypt(...); encrypt(...)` on the same instance
silently rewound the encryption stream: the first decrypt() allocated
self._dec and re-keyed self._enc back to counter 0, so the second
encrypt() produced ciphertext as if starting from the beginning of the
keystream rather than continuing it. set_iv() relies on the
both-directions reset to change the IV, so that path must be preserved.

Honor the direction argument: direction _ENCRYPTION / _DECRYPTION only
touches the matching context, while direction 0 (the value set_iv
passes) keeps the existing reset-both behavior.

Add two regression tests: one that interleaves encrypt/decrypt/encrypt
and compares against a baseline two-encrypt sequence to catch any
future re-introduction of cross-direction state stomping, and one that
locks in set_iv's reset-both semantics so the fix is not later
narrowed in a way that breaks IV changes.

F-3586
2026-05-11 12:03:48 +01:00
Andrew Hutchings 6c1c1b76bb Fix AesSiv silently mangling associated data
AesSiv._prepare_associated_data only checked for str and bytes when
deciding whether the input was a single associated-data block, so
bytearray and memoryview fell through to the "list of blocks" branch.
Iterating those types yields integers, which t2b() then turned into
ASCII decimal byte-strings (b'16', b'17', ...), producing many bogus
blocks instead of one. The C SIV computation succeeded over wrong
associated data, so encryption/decryption silently produced an
incorrect tag rather than raising. This broke interoperability between
callers passing the same content as different buffer types.

Match the set of types accepted by t2b() by including bytearray and
memoryview in the isinstance check.

Add a parametrized test that reuses the OpenSSL KAT vectors with bytes,
bytearray, and memoryview wrappers; a round-trip test would not have
caught this since both sides mangle identically.

F-1981
2026-05-11 11:47:56 +01:00
David Garske efe131ee96
Merge pull request #109 from kareem-wolfssl/gh108
Wrap _delete/_copy class attrs in staticmethod so self isn't bound as an extra arg.
2026-05-05 10:43:56 -07:00
David Garske b0708870ba
Merge pull request #93 from roberthdevries/rsa-public-add-rng-param
Make the random generator of _Rsa and RsaPublic configurable.
2026-05-05 10:35:04 -07:00
David Garske c17f7f0401
Merge pull request #105 from roberthdevries/missing-definition-ML_DSA_KEYGEN_SEED_LENGTH
Add missing definition ML_DSA_KEYGEN_SEED_LENGTH.
2026-05-05 10:35:01 -07:00
Kareem 8bd719c4a7 Add regression test. 2026-04-22 17:30:12 -07:00
Robert de Vries 377542f1f9 Add rng parameter to from_pem classmethod, add unit tests. 2026-04-17 23:19:29 +02:00
Robert de Vries a114bca77b Add missing definition ML_DSA_KEYGEN_SEED_LENGTH. 2026-04-17 23:19:13 +02:00
Robert de Vries b4ddce17de Keep reference to random number generator in EccPrivate.
Add a test for the case where no random number generator is passed
to EccPrivate.
2026-04-17 23:18:58 +02:00
Robert de Vries 26203d77d2 Fix mutable arguments passed as default arguments.
Function defaults are evaluated once, when the function is defined.

The same mutable object is then shared across all calls to the function.
If the object is modified, those modifications will persist across calls,
which can lead to unexpected behavior.
2026-04-17 23:18:58 +02:00
David Garske 2dd22e3500
Merge pull request #103 from roberthdevries/add-support-for-wcGetErrorString
Add support for wcGetErrorString() including error codes.
2026-04-17 09:37:15 -07:00
Robert de Vries ac7f6fd844 Fix conditional execution of test_error_string.
The pytest.mark.skipif does not prevent evaluation of parameters
on the next line.
2026-04-16 23:28:22 +02:00
Jeremiah Mackey 664e887f09 Fix hash copy semantics and add tests 2026-04-16 14:34:26 +00:00