-
Notifications
You must be signed in to change notification settings - Fork 29
Add typing: asn, exceptions, hashes, pwdbased, utils. #125
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,23 +20,26 @@ | |
|
|
||
| # pylint: disable=no-member,no-name-in-module | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import hmac as _hmac | ||
|
|
||
| from wolfcrypt._ffi import ffi as _ffi | ||
| from wolfcrypt._ffi import lib as _lib | ||
| from wolfcrypt.exceptions import WolfCryptError, WolfCryptApiError | ||
| from wolfcrypt.hashes import _Hash | ||
|
|
||
| if _lib.SHA_ENABLED: | ||
| from wolfcrypt.hashes import Sha | ||
| from wolfcrypt.hashes import Sha # ty: ignore[possibly-missing-import] | ||
| if _lib.SHA256_ENABLED: | ||
| from wolfcrypt.hashes import Sha256 | ||
| from wolfcrypt.hashes import Sha256 # ty: ignore[possibly-missing-import] | ||
| if _lib.SHA384_ENABLED: | ||
| from wolfcrypt.hashes import Sha384 | ||
| from wolfcrypt.hashes import Sha384 # ty: ignore[possibly-missing-import] | ||
| if _lib.SHA512_ENABLED: | ||
| from wolfcrypt.hashes import Sha512 | ||
| from wolfcrypt.hashes import Sha512 # ty: ignore[possibly-missing-import] | ||
|
|
||
| if _lib.ASN_ENABLED: | ||
| def pem_to_der(pem, pem_type): | ||
| def pem_to_der(pem: bytes, pem_type: int) -> bytes: | ||
| der = _ffi.new("DerBuffer**") | ||
| ret = _lib.wc_PemToDer(pem, len(pem), pem_type, der, _ffi.NULL, | ||
| _ffi.NULL, _ffi.NULL) | ||
|
|
@@ -49,7 +52,7 @@ def pem_to_der(pem, pem_type): | |
| _lib.wc_FreeDer(der) | ||
| return result | ||
|
|
||
| def der_to_pem(der, pem_type): | ||
| def der_to_pem(der: bytes, pem_type: int) -> bytes: | ||
| pem_length = _lib.wc_DerToPemEx(der, len(der), _ffi.NULL, 0, _ffi.NULL, | ||
| pem_type) | ||
| if pem_length <= 0: | ||
|
|
@@ -63,7 +66,7 @@ def der_to_pem(der, pem_type): | |
|
|
||
| return _ffi.buffer(pem, pem_length)[:] | ||
|
|
||
| def hash_oid_from_class(hash_cls): | ||
| def hash_oid_from_class(hash_cls: type[_Hash]) -> int: | ||
| if _lib.SHA_ENABLED and hash_cls == Sha: | ||
| return _lib.SHAh | ||
| elif _lib.SHA256_ENABLED and hash_cls == Sha256: | ||
|
|
@@ -75,7 +78,7 @@ def hash_oid_from_class(hash_cls): | |
| else: | ||
| raise WolfCryptError(f"Unknown hash class {hash_cls.__name__}") | ||
|
|
||
| def make_signature(data, hash_cls, key=None): | ||
| def make_signature(data: bytes, hash_cls: type[_Hash], key = None) -> bytes: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 [Medium] asn.py leaves function arguments unannotated while enabling ANN ruff rules This PR adds Fix: Complete the annotations in asn.py (and verify ruff ANN passes for wolfcrypt/) so the newly enabled rule does not fail lint. |
||
| hash_obj = hash_cls() | ||
| hash_obj.update(data) | ||
| digest = hash_obj.digest() | ||
|
|
@@ -93,7 +96,7 @@ def make_signature(data, hash_cls, key=None): | |
| else: | ||
| return plaintext_sig | ||
|
|
||
| def check_signature(signature, data, hash_cls, pub_key): | ||
| def check_signature(signature: bytes, data: bytes, hash_cls: type[_Hash], pub_key) -> bool: | ||
| computed_signature = make_signature(data, hash_cls) | ||
| decrypted_signature = pub_key.verify(signature) | ||
| return _hmac.compare_digest(computed_signature, decrypted_signature) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 [Low] test_mldsa now relies on cffi's low-level TypeError instead of an explicit guard
The removed code in
sign_with_seed/make_key_from_seedpreviously raised a clear, library-owned TypeError ('seed must support the buffer protocol...') when given a non-buffer seed. With that guard gone, the test was changed fromsign_with_seed(message, "")tosign_with_seed(message, " " * ML_DSA_SIGNATURE_SEED_LENGTH)so that the length check passes and the str then reaches CFFI, which raises TypeError. The test still passes, but it now asserts on CFFI's internal type rejection ('must be a bytes or list or tuple, not str') rather than a wolfcrypt-controlled error. This couples the test to CFFI internals and produces a less user-friendly error for callers.Fix: Optional: re-add an explicit type guard for clearer errors; otherwise acceptable as-is.