Commit Graph

172 Commits (master)

Author SHA1 Message Date
Juliusz Sosinowicz 2fea30cd32 Validate ctx length in MlDsa.verify (F-4014)
MlDsa.sign/sign_with_seed reject a context longer than 255 bytes with
ValueError, but verify did not. With ctxLen now correctly declared as byte in
the cdef, an over-long ctx would surface as a low-level CFFI OverflowError
instead of the consistent ValueError callers get from the signing paths.

Reject len(ctx) > 255 with ValueError in verify, matching sign.
2026-07-16 13:44:55 +00:00
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
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 397725336e Address more review comments. 2026-07-06 22:27:34 +02:00
Robert de Vries 47b0ac5af9 Fix ruff issues. 2026-07-06 22:13: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 7f82b412e3 Address more review comments. 2026-07-06 22:02:48 +02:00
Robert de Vries 97766d708b Address more review comments.
Also added a line to the change log to mention that typing information
has been added.
2026-07-06 22:02:48 +02:00
Robert de Vries 66b2ffb7c8 Addressed more review comments. 2026-07-06 22:02:48 +02:00
Robert de Vries fda2869cce Add py.typed file to indicate that this library has typing annotations. 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
Robert de Vries d1ac3d5f37 Add typing: asn, exceptions, hashes, hkdf, pwdbased, utils. 2026-07-06 22:02:46 +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 38c541ab42 ML-DSA: Move check on signing/verifying without context to top
This makes it more clear what the requirements for calling this
function are and avoids unnecessary computations for objects
which are not used.
2026-07-02 13:21:58 +02: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 319c9200ff Prepare for v5.9.2 release 2026-06-30 16:33:18 +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 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 8d87cb4d13 ML-DSA: fix wording of message and docstrings 2026-06-26 00:05:15 +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
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
Martijn de Milliano 1e20bc9b80 Prepare for v5.9.1 release 2026-06-04 16:59:24 +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 000472761c Fix Ed448 ctx cdef to match wolfSSL header signature
scripts/build_ffi.py declared wc_ed448_sign_msg and wc_ed448_verify_msg
with two cdef discrepancies vs the wolfSSL header (ed448.h):

  - ctx was typed `byte*` instead of `const byte*`. CFFI was therefore
    happy to forward a writable pointer into Python bytes storage.
    The wolfSSL functions are const-correct and do not mutate ctx, so
    this had no runtime impact, but it widened the surface CFFI was
    willing to permit.

  - ctx_len was typed `word32` instead of `byte`. RFC 8032 caps Ed448
    context at 255 bytes, and wolfSSL's prototype reflects that with a
    1-byte parameter. The mismatch happens to work on x86_64 SysV
    because the low byte is what the callee reads, but it is still a
    real ABI discrepancy and would not be guaranteed on other
    platforms or calling conventions.

Update the cdef to const byte* / byte to match the header on both
functions. Add a guard in Ed448Public.verify and Ed448Private.sign so
callers passing a ctx longer than 255 bytes get a clear ValueError
rather than relying on CFFI's narrowing behavior.

ML-DSA's wc_dilithium_sign_ctx_msg / wc_dilithium_verify_ctx_msg cdefs
were also reviewed and already match the wolfSSL header.

F-3086
2026-05-11 12:27:23 +01:00
Andrew Hutchings e961e83656 Avoid passing Python bytes to non-const wc_GetPkcs8TraditionalOffset
wc_GetPkcs8TraditionalOffset is declared in the wolfSSL public header
as taking byte* input (non-const), even though the current
implementation only reads from the buffer via ToTraditionalInline_ex.
RsaPrivate.__init__ was passing a Python bytes object directly into
that non-const parameter on the PKCS#8 fallback path, which lets CFFI
hand the C library a writable pointer into immutable storage. There is
no observable mutation today, but the contract permits it, and any
future wolfSSL change to actually write through the parameter would
silently corrupt the caller's bytes object.

Copy the key into a CFFI-owned byte array for that one call. The
adjacent wc_RsaPrivateKeyDecode calls are unaffected since their cdef
is already const byte*. PKCS#8 key load is a one-time cost at
construction, so the extra allocation is negligible.

Not changing the cdef to const byte* because the wolfSSL public header
itself declares the parameter non-const; diverging from the header
would cause compile warnings in the CFFI-generated glue code. If
wolfSSL ever const-corrects the header upstream, this copy can be
dropped.

F-2664
2026-05-11 12:15:51 +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 ce93174570 Fix Random.__init__ AttributeError on builds without ML-KEM/ML-DSA
Random.__init__ used _lib.INVALID_DEVID as a default parameter value,
but INVALID_DEVID is only declared in the CFFI cdef inside
`if features["ML_KEM"] or features["ML_DSA"]`. Since default parameters
are evaluated at module import time, any wolfSSL build that lacks both
ML-KEM and ML-DSA (e.g. via USE_LOCAL_WOLFSSL pointing at a custom
build) raises AttributeError on `import wolfcrypt.random`, cascading
into wolfcrypt.ciphers and effectively breaking the entire library.

Hardcode the default to -2, matching the convention already used by
_Hmac._init and AesGcmStream.__init__ (which call wc_HmacInit /
wc_AesInit with -2 directly rather than referencing _lib.INVALID_DEVID).

Add a build-no-pqc GitHub Actions job that builds wolfSSL with
--disable-kyber --disable-dilithium, installs wolfcrypt-py against it
via USE_LOCAL_WOLFSSL, and runs an import-smoke step plus the full
pytest suite. The smoke step is the explicit regression guard: it
imports wolfcrypt.random and instantiates Random(), which would fail
at import time if the default ever again references a CFFI symbol that
is only conditionally declared.

F-2659
2026-05-11 12:02:26 +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 4ec45a6511
Merge pull request #110 from roberthdevries/special-wc-api-retval-exceptions
Raise specialized WolfCryptApiError for failed API calls.
2026-05-08 15:59:50 -07:00
Robert de Vries 2bcc7031bd
Revert change to raising WolfCryptError 2026-05-08 17:12:50 +02:00
Robert de Vries 8d80db4914
Pass error return value to exception
If the size return value is negative, an error is detected. In that case raise an exception. Even though a zero is not a valid size value, it would indicate success in other cases. But a 0 return value would not make much sense as a return value for this function.
2026-05-08 16:38:59 +02:00
David Garske e48fb07c43
Merge pull request #113 from roberthdevries/remove-redundant-stuff
Remove redundant parentheses and bytes() calls.
2026-05-05 11:16:44 -07: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 292557e057
Merge pull request #100 from roberthdevries/add-typing
Start adding typing information.
2026-05-05 10:40:52 -07:00
David Garske e7300e98bf
Merge pull request #107 from roberthdevries/export__wolfssl_version__
Add __wolfssl_version__ to exported symbols.
2026-05-05 10:40:29 -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
Robert de Vries 17dad74a20 Remove redundant parentheses and bytes() calls. 2026-04-24 22:51:00 +02:00
Robert de Vries 0ac207d34a Raise specialized WolfCryptApiError for failed API calls.
This exception takes the return value and converts that to
a human-readable string (if error string support is compiled in.)

The specialized exception is derived from WolfCryptError meaning
that existing users will not notice the difference except for the
nicer description.
2026-04-24 22:01:02 +02:00