From ce1f18a88110fca6ed847c22d89ac6826fb1bebb Mon Sep 17 00:00:00 2001 From: sebastian-carpenter Date: Wed, 11 Nov 2026 10:47:27 -0700 Subject: [PATCH] extend ruff linting --- pyproject.toml | 31 +++++++++++++++++++-- scripts/build_ffi.py | 17 +++++++++--- tests/test_asn.py | 4 +++ tests/test_ciphers.py | 62 +++++++++++++++--------------------------- tests/test_hashes.py | 19 +++++++------ tests/test_pwdbased.py | 2 ++ wolfcrypt/__init__.py | 3 +- wolfcrypt/_ffi/lib.pyi | 8 +----- wolfcrypt/_version.py | 1 - wolfcrypt/ciphers.py | 19 ++----------- wolfcrypt/cryptocb.py | 2 -- wolfcrypt/hashes.py | 6 +--- wolfcrypt/utils.py | 1 + wolfcrypt/wc_types.py | 1 + 14 files changed, 89 insertions(+), 87 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 08af907..c41454b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,7 +41,8 @@ dev = [ "mypy", "pytest", "pytest-cov", - "ruff", + # ruff preview rules/behaviour (used for linting) can change in any release + "ruff==0.17.4", "sphinx", "sphinx-rtd-theme", "ty", @@ -89,9 +90,33 @@ indent-width = 4 target-version = "py310" [tool.ruff.lint] -select = ["E", "F", "B", "UP", "ANN", "C4", "DTZ", "EXE", "FA", "INT", "ISC", "ICN", "LOG", "G", "RSE", "SLOT", "TID", "TC", "FLY", "PERF", "W", "FURB"] +select = [ + "E", "F", "B", "UP", "ANN", "C4", "DTZ", "EXE", "FA", "INT", "ISC", "ICN", + "LOG", "G", "RSE", "SLOT", "TID", "TC", "FLY", "PERF", "W", "FURB", + # pycodestyle whitespace/blank-line checks. + # These are listed by exact code because of explicit-preview-rules below. + "E111", "E112", "E113", "E114", "E115", "E116", "E117", + "E201", "E202", "E203", "E204", + "E211", + "E221", "E222", "E223", "E224", "E225", "E227", "E228", + "E231", + "E241", "E242", + "E251", "E252", + "E261", "E262", "E265", "E266", + "E271", "E272", "E273", "E274", "E275", + "E301", "E302", "E303", "E304", "E305", "E306", + "W391", +] +# E501: line too long ignore = ["E501"] +# Preview is needed for the pycodestyle rules selected above. +# explicit-preview-rules limits new preview rules to the exact codes listed, but +# preview mode still changes the behaviour of some stable rules everywhere. Ruff +# is pinned in the dev group for this reason. +preview = true +explicit-preview-rules = true + # Allow fix for all enabled rules (when `--fix`) is provided. fixable = ["ALL"] unfixable = [] @@ -102,6 +127,8 @@ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" [tool.ruff.lint.per-file-ignores] "scripts/*.py" = ["ANN"] "tests/*.py" = ["ANN"] +# Sphinx's generated conf.py keeps its commented-out defaults as "#name = ...". +"docs/conf.py" = ["E265"] [tool.ruff.format] # Like Black, use double quotes for strings. diff --git a/scripts/build_ffi.py b/scripts/build_ffi.py index 7e64853..9bc384a 100644 --- a/scripts/build_ffi.py +++ b/scripts/build_ffi.py @@ -28,14 +28,17 @@ import shutil from wolfcrypt._version import __wolfssl_version__ as version + def local_path(path): """ Return path relative to the root of this project """ current = os.path.abspath(os.getcwd()) return os.path.abspath(os.path.join(current, path)) + WOLFSSL_SRC_PATH = local_path("lib/wolfssl") + def wolfssl_inc_dirs(local_wolfssl=None, fips=False): """Returns the wolfSSL include directories needed to build the CFFI. """ @@ -66,6 +69,7 @@ def wolfssl_inc_dirs(local_wolfssl=None, fips=False): return include_paths + def wolfssl_lib_dir(local_wolfssl=None, fips=False): """Returns the directory containg the wolfSSL library. """ @@ -100,6 +104,7 @@ def wolfssl_lib_dir(local_wolfssl=None, fips=False): return lib_dir + def call(cmd): print(f"Calling: '{cmd}' from working directory {os.getcwd()}") @@ -108,6 +113,7 @@ def call(cmd): subprocess.check_call(cmd, shell=True, env=os.environ) os.environ["PATH"] = old_env + @contextmanager def chdir(new_path, mkdir=False): old_path = os.getcwd() @@ -284,6 +290,7 @@ def make(configure_flags, fips=False): call("make") call("make install") + def get_libwolfssl(): if sys.platform == "win32": libwolfssl_path = os.path.join(wolfssl_lib_dir(), "wolfssl.lib") @@ -302,11 +309,13 @@ def get_libwolfssl(): else: return True + def generate_libwolfssl(fips): ensure_wolfssl_src(version) prefix = os.path.join(WOLFSSL_SRC_PATH, get_platform(), version) make(make_flags(prefix, fips)) + def get_features(local_wolfssl, features): fips = False fips_file = None @@ -395,7 +404,7 @@ def get_features(local_wolfssl, features): ] have_mldsa_no_context_support = re.search(r'#define\s+(' + '|'.join(mldsa_no_context_defines) + r')\s+', '\n'.join(defines)) features["ML_DSA_NO_CTX"] = 1 if have_mldsa_no_context_support else 0 - features["ML_KEM"] = 1 if '#define WOLFSSL_HAVE_MLKEM' in defines else 0 + features["ML_KEM"] = 1 if '#define WOLFSSL_HAVE_MLKEM' in defines else 0 features["HKDF"] = 1 if "#define HAVE_HKDF" in defines else 0 # Unlike the other fatures, HASHDRBG is enabled by default in random.h, unless WC_NO_HASHDRBG or # CUSTOM_RAND_GENERATE_BLOCK is defined. @@ -428,6 +437,7 @@ def get_features(local_wolfssl, features): return features + def build_ffi(local_wolfssl, features): cffi_include_dirs = wolfssl_inc_dirs(local_wolfssl, features["FIPS"]) cffi_libraries = [] @@ -540,7 +550,7 @@ def build_ffi(local_wolfssl, features): int HMAC_COPY_ENABLED = {features["HMAC_COPY"]}; """ - ffibuilder.set_source( "wolfcrypt._ffi", init_source_string, + ffibuilder.set_source("wolfcrypt._ffi", init_source_string, include_dirs=cffi_include_dirs, library_dirs=[wolfssl_lib_dir(local_wolfssl, features["FIPS"])], libraries=cffi_libraries) @@ -1186,7 +1196,6 @@ def build_ffi(local_wolfssl, features): int wc_ecc_set_rng(ecc_key* key, WC_RNG* rng); """ - if features["ED25519"]: cdef += """ typedef struct {...; } ed25519_key; @@ -1418,7 +1427,6 @@ def build_ffi(local_wolfssl, features): static const int WC_HASH_TYPE_SHA3_512; """ - cdef += """ typedef struct { int algo_type; /* enum wc_AlgoType */ @@ -1544,6 +1552,7 @@ def build_ffi(local_wolfssl, features): ffibuilder.cdef(cdef) + def main(ffibuilder): # Default features. features = { diff --git a/tests/test_asn.py b/tests/test_asn.py index 6904d30..30ccc03 100644 --- a/tests/test_asn.py +++ b/tests/test_asn.py @@ -36,6 +36,7 @@ certs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "certs") + @pytest.fixture def pem_der_conversion_vectors(): TestVector = namedtuple("TestVector", "pem der type") @@ -61,6 +62,7 @@ def pem_der_conversion_vectors(): return vectors + @pytest.fixture def signature_vectors(): TestVector = namedtuple("TestVector", """data signature hash_cls pub_key @@ -97,6 +99,7 @@ def signature_vectors(): return vectors + def test_pem_der_conversion(pem_der_conversion_vectors): for vector in pem_der_conversion_vectors: computed_der = pem_to_der(vector.pem, vector.type) @@ -105,6 +108,7 @@ def test_pem_der_conversion(pem_der_conversion_vectors): computed_pem = der_to_pem(vector.der, vector.type) assert computed_pem == vector.pem + def test_signature(signature_vectors): for vector in signature_vectors: assert make_signature(vector.data, vector.hash_cls, vector.priv_key) == vector.signature diff --git a/tests/test_ciphers.py b/tests/test_ciphers.py index ffbf29c..69c434b 100644 --- a/tests/test_ciphers.py +++ b/tests/test_ciphers.py @@ -68,27 +68,27 @@ def vectors(): vectorArray = {} if _lib.AES_ENABLED: - vectorArray[Aes]=TestVector( + vectorArray[Aes] = TestVector( key="0123456789abcdef", iv="1234567890abcdef", plaintext=t2b("now is the time "), ciphertext=h2b("959492575f4281532ccc9d4677a233cb"), - ciphertext_ctr = h2b('287528ddf484b1055debbe751eb52b8a') + ciphertext_ctr=h2b('287528ddf484b1055debbe751eb52b8a') ) # ty: ignore[missing-argument] if _lib.CHACHA_ENABLED: - vectorArray[ChaCha]=TestVector( + vectorArray[ChaCha] = TestVector( key="0123456789abcdef0123456789abcdef", iv="1234567890ab", ) # ty: ignore[missing-argument] if _lib.DES3_ENABLED: - vectorArray[Des3]=TestVector( + vectorArray[Des3] = TestVector( key=h2b("0123456789abcdeffedeba987654321089abcdef01234567"), iv=h2b("1234567890abcdef"), plaintext=t2b("Now is the time for all "), ciphertext=h2b("43a0297ed184f80e8964843212d508981894157487127db0") ) # ty: ignore[missing-argument] if _lib.RSA_ENABLED: - vectorArray[RsaPublic]=TestVector( + vectorArray[RsaPublic] = TestVector( key=h2b( "30819F300D06092A864886F70D010101050003818D0030818902818100BC" "730EA849F374A2A9EF18A5DA559921F9C8ECB36D48E53535757737ECD161" @@ -98,7 +98,7 @@ def vectors(): "2700F6916865A90203010001"), pem=os.path.join(certs_dir, "server-keyPub.pem") ) # ty: ignore[missing-argument] - vectorArray[RsaPrivate]=TestVector( + vectorArray[RsaPrivate] = TestVector( key=h2b( "3082025C02010002818100BC730EA849F374A2A9EF18A5DA559921F9C8EC" "B36D48E53535757737ECD161905F3ED9E4D5DF94CAC1A9D719DA86C9E84D" @@ -166,7 +166,7 @@ def vectors(): ) # ty: ignore[missing-argument] if _lib.ECC_ENABLED: - vectorArray[EccPublic]=TestVector( + vectorArray[EccPublic] = TestVector( key=h2b( "3059301306072A8648CE3D020106082A8648CE3D0301070342000455BFF4" "0F44509A3DCE9BB7F0C54DF5707BD4EC248E1980EC5A4CA22403622C9BDA" @@ -178,7 +178,7 @@ def vectors(): "daefa2351243847616c6569506cc01a9bdf6751a42f7bda9b236225fc75d7fb4" ) ) # ty: ignore[missing-argument] - vectorArray[EccPrivate]=TestVector( + vectorArray[EccPrivate] = TestVector( key=h2b( "30770201010420F8CF926BBD1E28F1A8ABA1234F3274188850AD7EC7EC92" "F88F974DAF568965C7A00A06082A8648CE3D030107A1440342000455BFF4" @@ -194,26 +194,26 @@ def vectors(): ) # ty: ignore[missing-argument] if _lib.ED25519_ENABLED: - vectorArray[Ed25519Private]=TestVector( - key = h2b( - "47CD22B276161AA18BA1E0D13DBE84FE4840E4395D784F555A92E8CF739B" - "F86B" + vectorArray[Ed25519Private] = TestVector( + key=h2b( + "47CD22B276161AA18BA1E0D13DBE84FE4840E4395D784F555A92E8CF739B" + "F86B" ) ) # ty: ignore[missing-argument] - vectorArray[Ed25519Public]=TestVector( + vectorArray[Ed25519Public] = TestVector( key=h2b( "8498C65F4841145F9C51E8BFF4504B5527E0D5753964B7CB3C707A2B9747" "FC96" ) ) # ty: ignore[missing-argument] if _lib.ED448_ENABLED: - vectorArray[Ed448Private]=TestVector( + vectorArray[Ed448Private] = TestVector( key=h2b("c2b29804e9a893c9e275cac1f8a3033f3d4b78b79eb427ed359fdeb8" "82d657c129c7930936b181971b795167ad18cabeeb52b59b94f115ad" "59" ) ) # ty: ignore[missing-argument] - vectorArray[Ed448Public]=TestVector( + vectorArray[Ed448Public] = TestVector( key=h2b("89fb2b5a5ab67dd317794cc5f1700cace295b043f3ad73a66299e10a" "d3fc0a28289ddd1c641598a354113867a42e82ad844b4d858d92e4e7" "80" @@ -221,12 +221,14 @@ def vectors(): ) # ty: ignore[missing-argument] return vectorArray + algo_params = [] if _lib.AES_ENABLED: algo_params.append(Aes) # ty: ignore[possibly-unresolved-reference] if _lib.DES3_ENABLED: algo_params.append(Des3) # ty: ignore[possibly-unresolved-reference] + @pytest.fixture(params=algo_params) def cipher_cls(request): return request.param @@ -238,6 +240,7 @@ def cipher_new(cipher_cls, vectors): MODE_CBC, vectors[cipher_cls].iv) + def test_block_cipher(cipher_cls, vectors): key = vectors[cipher_cls].key iv = vectors[cipher_cls].iv @@ -260,7 +263,6 @@ def test_block_cipher(cipher_cls, vectors): with pytest.raises(ValueError): cipher_cls.new(key, MODE_CBC, iv[:-1]) # invalid iv length - # Test AES in counter mode if ciphertext_ctr is not None: cipher_obj = cipher_cls.new(key, MODE_CTR, iv) @@ -274,7 +276,6 @@ def test_block_cipher(cipher_cls, vectors): assert cipher_obj.encrypt(plaintext) == ciphertext - # many encryptions cipher_obj = cipher_new(cipher_cls, vectors) result = t2b("") @@ -311,6 +312,7 @@ def test_block_cipher(cipher_cls, vectors): with pytest.raises(ValueError): cipher_obj.decrypt(ciphertext[:-1]) + if _lib.CHACHA_ENABLED: @pytest.fixture def chacha_obj(vectors): @@ -401,7 +403,6 @@ def test_new_rsa_raises(vectors): with pytest.raises(WolfCryptError): # invalid key size RsaPrivate.make_key(16384) # ty: ignore[possibly-missing-attribute] - def test_rsa_encrypt_decrypt(rsa_private, rsa_public): plaintext = t2b("Everyone gets Friday off.") @@ -450,7 +451,6 @@ def test_rsa_encrypt_decrypt_pad_oaep(rsa_private_oaep, rsa_public_oaep): assert 1024 / 8 == len(ciphertext) == rsa_private_oaep.output_size assert plaintext == rsa_private_oaep.decrypt_oaep(ciphertext) - def test_rsa_pkcs8_encrypt_decrypt(rsa_private_pkcs8, rsa_public): plaintext = t2b("Everyone gets Friday off.") @@ -467,7 +467,6 @@ def test_rsa_pkcs8_encrypt_decrypt(rsa_private_pkcs8, rsa_public): assert 1024 / 8 == len(ciphertext) == rsa_private_pkcs8.output_size assert plaintext == rsa_private_pkcs8.decrypt(ciphertext) - def test_rsa_sign_verify(rsa_private, rsa_public): plaintext = t2b("Everyone gets Friday off.") @@ -555,12 +554,10 @@ def test_rsa_pkcs8_sign_verify(rsa_private_pkcs8, rsa_public): def ecc_private(vectors): return EccPrivate(vectors[EccPrivate].key) - @pytest.fixture def ecc_public(vectors): return EccPublic(vectors[EccPublic].key) - def test_new_ecc_raises(vectors): with pytest.raises(WolfCryptError): EccPrivate(vectors[EccPrivate].key[:-1]) # invalid key length @@ -574,14 +571,12 @@ def test_new_ecc_raises(vectors): with pytest.raises(WolfCryptError): # invalid key size EccPrivate.make_key(1024) - def test_key_encoding(vectors): priv = EccPrivate() pub = EccPublic() raw_priv = EccPrivate() raw_pub = EccPublic() - # Test default encode/decode key priv.decode_key(vectors[EccPrivate].key) pub.decode_key(vectors[EccPublic].key) @@ -612,7 +607,6 @@ def test_key_encoding(vectors): assert qx[0:32] == vectors[EccPublic].raw_key[0:32] assert qy[0:32] == vectors[EccPublic].raw_key[32:64] - def test_ecc_decode_key_raw_rejects_wrong_length(vectors): """ wc_ecc_import_unsigned reads exactly curve_size bytes from each @@ -645,12 +639,9 @@ def test_ecc_decode_key_raw_rejects_wrong_length(vectors): raw_pub.decode_key_raw(qx_good, qy_good) raw_priv.decode_key_raw(qx_good, qy_good, d_good) - - def test_x963(ecc_private, ecc_public): assert ecc_private.export_x963() == ecc_public.export_x963() - def test_ecc_sign_verify(ecc_private, ecc_public): plaintext = "Everyone gets Friday off." @@ -685,7 +676,7 @@ def test_ecc_sign_verify_raw(ecc_private, ecc_public): plaintext = "Everyone gets Friday off." # normal usage, sign with private, verify with public - r,s = ecc_private.sign_raw(plaintext) + r, s = ecc_private.sign_raw(plaintext) assert len(r) + len(s) <= 2 * ecc_private.size assert ecc_public.verify_raw(r, s, plaintext) @@ -698,7 +689,6 @@ def test_ecc_sign_verify_raw(ecc_private, ecc_public): # using the known public key. assert ecc_private.verify_raw(r, s, plaintext) - def test_ecc_make_shared_secret(): a = EccPrivate.make_key(32, rng=Random()) a_pub = EccPublic() @@ -725,12 +715,10 @@ def test_ecc_make_key_no_rng(): def ed25519_private(vectors): return Ed25519Private(vectors[Ed25519Private].key, vectors[Ed25519Public].key) - @pytest.fixture def ed25519_public(vectors): return Ed25519Public(vectors[Ed25519Public].key) - def test_new_ed25519_raises(vectors): with pytest.raises(WolfCryptError): Ed25519Private(vectors[Ed25519Private].key[:-1]) # invalid key length @@ -741,7 +729,6 @@ def test_new_ed25519_raises(vectors): with pytest.raises(WolfCryptError): # invalid key size Ed25519Private.make_key(1024) - def test_ed25519_key_encoding(vectors): priv = Ed25519Private() pub = Ed25519Public() @@ -750,10 +737,9 @@ def test_ed25519_key_encoding(vectors): pub.decode_key(vectors[Ed25519Public].key) assert priv.encode_key()[0] == vectors[Ed25519Private].key - assert priv.encode_key()[1] == vectors[Ed25519Public].key # Automatically re-generated from private-only + assert priv.encode_key()[1] == vectors[Ed25519Public].key # Automatically re-generated from private-only assert pub.encode_key() == vectors[Ed25519Public].key - def test_ed25519_sign_verify(ed25519_private, ed25519_public): plaintext = "Everyone gets Friday off." @@ -776,12 +762,10 @@ def test_ed25519_sign_verify(ed25519_private, ed25519_public): def ed448_private(vectors): return Ed448Private(vectors[Ed448Private].key, vectors[Ed448Public].key) - @pytest.fixture def ed448_public(vectors): return Ed448Public(vectors[Ed448Public].key) - def test_new_ed448_raises(vectors): with pytest.raises(WolfCryptError): Ed448Private(vectors[Ed448Private].key[:-1]) # invalid key length @@ -792,7 +776,6 @@ def test_new_ed448_raises(vectors): with pytest.raises(WolfCryptError): # invalid key size Ed448Private.make_key(1024) - def test_ed448_key_encoding(vectors): priv = Ed448Private() pub = Ed448Public() @@ -801,10 +784,9 @@ def test_ed448_key_encoding(vectors): pub.decode_key(vectors[Ed448Public].key) assert priv.encode_key()[0] == vectors[Ed448Private].key - assert priv.encode_key()[1] == vectors[Ed448Public].key # Automatically re-generated from private-only + assert priv.encode_key()[1] == vectors[Ed448Public].key # Automatically re-generated from private-only assert pub.encode_key() == vectors[Ed448Public].key - def test_ed448_sign_verify(ed448_private, ed448_public): plaintext = "Everyone gets Friday off." diff --git a/tests/test_hashes.py b/tests/test_hashes.py index 12b282a..696593d 100644 --- a/tests/test_hashes.py +++ b/tests/test_hashes.py @@ -61,52 +61,52 @@ def vectors(): vectorArray = {} if _lib.SHA_ENABLED: - vectorArray[Sha]=TestVector( + vectorArray[Sha] = TestVector( digest=t2b("1b6182d68ae91ce0853bd9c6b6edfedd4b6a510d") ) if _lib.SHA256_ENABLED: - vectorArray[Sha256]=TestVector( + vectorArray[Sha256] = TestVector( digest=t2b("96e02e7b1cbcd6f104fe1fdb4652027a" "5505b68652b70095c6318f9dce0d1844") ) if _lib.SHA384_ENABLED: - vectorArray[Sha384]=TestVector( + vectorArray[Sha384] = TestVector( digest=t2b("4c79d80531203a16f91bee325f18c6aada47f9382fe44fc1" "1f92917837e9b7902f5dccb7d3656f667a1dce3460bc884b") ) if _lib.SHA512_ENABLED: - vectorArray[Sha512]=TestVector( + vectorArray[Sha512] = TestVector( digest=t2b("88fcf67ffd8558d713f9cedcd852db47" "9e6573f0bd9955610a993f609637553c" "e8fff55e644ee8a106aae19c07f91b3f" "2a2a6d40dfa7302c0fa6a1a9a5bfa03f") ) if _lib.SHA3_ENABLED: - vectorArray[Sha3]=TestVector( + vectorArray[Sha3] = TestVector( digest=t2b("6170dedf06f83c3305ec18b7558384a5" "a62d86e42c143d416aaec32f971986c1" "e84edf61df308cc6d8c310d1956e1908") ) if _lib.HMAC_ENABLED: if _lib.SHA_ENABLED: - vectorArray[HmacSha]=TestVector( + vectorArray[HmacSha] = TestVector( digest=t2b("7ab9aca2c87c7c45ba2ffa52f719fdbd8fbff62d") ) if _lib.SHA256_ENABLED: - vectorArray[HmacSha256]=TestVector( + vectorArray[HmacSha256] = TestVector( digest=t2b("9041ac8c66fc350a1a0d5f4fff9d8ef74721d5a43ec8893a2" "875cf69576c45c2") ) if _lib.SHA384_ENABLED: - vectorArray[HmacSha384]=TestVector( + vectorArray[HmacSha384] = TestVector( digest=t2b("f8c589ddf5489404f85c3c718a8345f207fb1ed6c6f5ecb09" "8e8be8aeb1aaa9f0c6dd84c141410b29a47a1a2b3a85ae0") ) if _lib.SHA512_ENABLED: - vectorArray[HmacSha512]=TestVector( + vectorArray[HmacSha512] = TestVector( digest=t2b("7708a12ca110cd81a334bd4e8bddc4314acd3ed218bbff7c6" "486e149fc145e9f5c05f05e919f7c2bc027266e986679984c" "3ade1a14084ad7627a65c3671a2d05") @@ -138,6 +138,7 @@ def vectors(): if _lib.SHA512_ENABLED: hmac_params.append(HmacSha512) # ty: ignore[possibly-unresolved-reference] + @pytest.fixture(params=(hash_params + hmac_params)) def hash_cls(request): return request.param diff --git a/tests/test_pwdbased.py b/tests/test_pwdbased.py index 2444860..264d3ea 100644 --- a/tests/test_pwdbased.py +++ b/tests/test_pwdbased.py @@ -33,6 +33,7 @@ if _lib.HMAC_ENABLED: from wolfcrypt.hashes import HmacSha + @pytest.fixture def pbkdf2_vectors(): TestVector = namedtuple("TestVector", """password salt iterations key_length @@ -55,6 +56,7 @@ def pbkdf2_vectors(): return vectors + def test_pbkdf2(pbkdf2_vectors): for vector in pbkdf2_vectors: key = PBKDF2(vector.password, vector.salt, vector.iterations, diff --git a/wolfcrypt/__init__.py b/wolfcrypt/__init__.py index a257a04..941cc45 100644 --- a/wolfcrypt/__init__.py +++ b/wolfcrypt/__init__.py @@ -38,7 +38,8 @@ __all__ = [ "__title__", "__summary__", "__uri__", "__version__", "__wolfssl_version__", "__author__", "__email__", "__license__", "__copyright__", - "ciphers", "hashes", "random", "pwdbased", "cryptocb" + # Submodules: valid in __all__, but preview-mode ruff F822 reports them as undefined. + "ciphers", "hashes", "random", "pwdbased", "cryptocb" # noqa: F822 ] top_level_py = os.path.basename(sys.argv[0]) diff --git a/wolfcrypt/_ffi/lib.pyi b/wolfcrypt/_ffi/lib.pyi index bb700ce..bbd319b 100644 --- a/wolfcrypt/_ffi/lib.pyi +++ b/wolfcrypt/_ffi/lib.pyi @@ -467,7 +467,7 @@ RsaKey: TypeAlias = FFI.CData def wc_InitRsaKey(key: RsaKey, heap: FFI.CData) -> int: ... def wc_RsaSetRNG(key: RsaKey, rng: RNG) -> int: ... def wc_FreeRsaKey(key: RsaKey) -> int: ... -def wc_RsaPublicKeyDecode(input: bytes, in_out_idx: IntPtr, key: RsaKey, input_len:int ) -> int: ... +def wc_RsaPublicKeyDecode(input: bytes, in_out_idx: IntPtr, key: RsaKey, input_len: int) -> int: ... def wc_RsaPublicEncrypt(plaintext: bytes, plaintext_len: int, out: BytePtr, out_len: int, key: RsaKey, rng: RNG) -> int: ... def wc_RsaPublicEncrypt_ex(plaintext: bytes, plaintext_len: int, out: BytePtr, out_len: int, key: RsaKey, rng: RNG, pad_type: int, hash_type: int, mgf: int, label: bytes | BytePtr, label_len: int) -> int: ... def wc_RsaSSL_Verify(signature: bytes, signature_len: int, out: BytePtr, out_len: int, key: RsaKey) -> int: ... @@ -485,7 +485,6 @@ def wc_RsaPSS_Sign(digest: bytes, digest_len: int, out: BytePtr, out_len: int, h def wc_GetPkcs8TraditionalOffset(input: BytePtr, in_out_idx: IntPtr, size: int) -> int: ... def wc_RsaKeyToPublicDer(key: RsaKey, output: BytePtr, in_len: int) -> int: ... - MpInt: TypeAlias = FFI.CData def mp_init(a: MpInt) -> int: ... @@ -601,14 +600,12 @@ class wc_HashInfo: digest: FFI.CData u: FFI.CData - @dataclass class wc_RngInfo: rng: RNG out: FFI.CData sz: int - @dataclass class wc_CopyInfo: algo: int @@ -616,7 +613,6 @@ class wc_CopyInfo: src: FFI.CData dst: FFI.CData - @dataclass class wc_FreeInfo: algo: int @@ -624,7 +620,6 @@ class wc_FreeInfo: subType: int obj: FFI.CData - @dataclass class wc_CryptoInfo: algo_type: int @@ -633,7 +628,6 @@ class wc_CryptoInfo: copy: wc_CopyInfo free: wc_FreeInfo - def wc_CryptoCb_RegisterDevice(dev_id: int, cb: Callable[[int, wc_CryptoInfo, FFI.CData], int], ctx: FFI.CData) -> int: ... def wc_CryptoCb_UnRegisterDevice(dev_id: int) -> None: ... def wc_CryptoCb_DefaultDevID() -> int: ... diff --git a/wolfcrypt/_version.py b/wolfcrypt/_version.py index ea6fd3b..bf2c7de 100644 --- a/wolfcrypt/_version.py +++ b/wolfcrypt/_version.py @@ -9,4 +9,3 @@ # MAJOR.MINOR.BUILD-POST __version__ = "5.9.2-0" - diff --git a/wolfcrypt/ciphers.py b/wolfcrypt/ciphers.py index 7b31a29..103b5b0 100644 --- a/wolfcrypt/ciphers.py +++ b/wolfcrypt/ciphers.py @@ -544,7 +544,6 @@ def final(self, authTag: BytesOrStr | None = None) -> bytes | None: raise WolfCryptApiError("Decryption error", ret) - if _lib.CHACHA_ENABLED: class ChaCha(_Cipher): """ @@ -626,6 +625,7 @@ def _encrypt(self, destination: _ffi.CData, source: bytes) -> int: assert self._enc is not None return _lib.wc_Chacha_Process(self._enc, destination, source, len(source)) + @override def _decrypt(self, destination: _ffi.CData, source: bytes) -> int: assert self._dec is not None @@ -806,8 +806,6 @@ def _get_mgf(self) -> None: else: self._mgf = _lib.WC_MGF1NONE - - class RsaPublic(_Rsa, SupportsRsaVerify): def __init__(self, key: BytesOrStr, hash_type: int | None = None, rng: Random | None = None) -> None: super().__init__(rng) @@ -940,7 +938,6 @@ def verify_pss(self, plaintext: BytesOrStr, signature: BytesOrStr) -> bool: return ret == 0 - class RsaPrivate(RsaPublic, SupportsRsaSign): if _lib.KEYGEN_ENABLED: @classmethod @@ -1013,7 +1010,6 @@ def encode_key(self) -> tuple[bytes, bytes]: priv = _ffi.new(f"byte[{self.size * 4}]") pub = _ffi.new(f"byte[{self.size * 4}]") - ret = _lib.wc_RsaKeyToDer(self.native_object, priv, self.size) if ret <= 0: # pragma: no cover raise WolfCryptApiError("Private RSA key error", ret) @@ -1158,7 +1154,6 @@ def size(self) -> int: def max_signature_size(self) -> int: return _lib.wc_ecc_sig_size(self.native_object) - class EccPublic(_Ecc): def __init__(self, key: BytesOrStr | None = None) -> None: _Ecc.__init__(self) @@ -1322,7 +1317,6 @@ def verify_raw(self, R: bytes, S: bytes, data: BytesOrStr) -> bool: _lib.mp_clear(mpR) _lib.mp_clear(mpS) - class EccPrivate(EccPublic): def __init__(self, key: BytesOrStr | None = None, rng: Random | None = None) -> None: @@ -1547,7 +1541,6 @@ def size(self) -> int: def max_signature_size(self) -> int: return _lib.wc_ed25519_sig_size(self.native_object) - class Ed25519Public(_Ed25519): def __init__(self, key: BytesOrStr | None = None) -> None: _Ed25519.__init__(self) @@ -1609,8 +1602,6 @@ def verify(self, signature: bytes, data: BytesOrStr) -> bool: return status[0] == 1 - - class Ed25519Private(Ed25519Public): def __init__(self, key: BytesOrStr | None = None, pub: bytes | None = None) -> None: _Ed25519.__init__(self) @@ -1620,7 +1611,7 @@ def __init__(self, key: BytesOrStr | None = None, pub: bytes | None = None) -> N if key and not pub: self.decode_key(key) if key and pub: - self.decode_key(key,pub) + self.decode_key(key, pub) @classmethod def make_key(cls, size: int, rng: Random | None = None) -> Ed25519Private: @@ -1749,7 +1740,6 @@ def size(self) -> int: def max_signature_size(self) -> int: return _lib.wc_ed448_sig_size(self.native_object) - class Ed448Public(_Ed448): def __init__(self, key: BytesOrStr | None = None) -> None: _Ed448.__init__(self) @@ -1819,8 +1809,6 @@ def verify(self, signature: bytes, data: BytesOrStr, ctx: BytesOrStr | None = No return status[0] == 1 - - class Ed448Private(Ed448Public): def __init__(self, key: BytesOrStr | None = None, pub: bytes | None = None) -> None: _Ed448.__init__(self) @@ -1914,7 +1902,7 @@ def encode_key(self) -> tuple[bytes, bytes]: return _ffi.buffer(key, priv_size[0])[:], _ffi.buffer(pubkey, pub_size[0])[:] - def sign(self, plaintext: BytesOrStr, ctx : BytesOrStr | None = None) -> bytes: + def sign(self, plaintext: BytesOrStr, ctx: BytesOrStr | None = None) -> bytes: """ Signs **plaintext**, using the private key data in the object. @@ -2637,7 +2625,6 @@ def sign_with_seed(self, message: BytesOrStr, seed: bytes, ctx: BytesOrStr | Non if ret < 0: # pragma: no cover raise WolfCryptApiError("wc_dilithium_sign_msg_with_seed() error", ret) - if in_size != out_size[0]: raise WolfCryptError(f"{in_size=} and {out_size[0]=} don't match") diff --git a/wolfcrypt/cryptocb.py b/wolfcrypt/cryptocb.py index df781b5..9f0f428 100644 --- a/wolfcrypt/cryptocb.py +++ b/wolfcrypt/cryptocb.py @@ -100,7 +100,6 @@ NATIVE_HASH_COPY[_lib.WC_HASH_TYPE_SHA3_384] = ("wc_Sha3 *", _lib.wc_Sha3_384_Copy) NATIVE_HASH_COPY[_lib.WC_HASH_TYPE_SHA3_512] = ("wc_Sha3 *", _lib.wc_Sha3_512_Copy) - class CryptoCallback: """ Registers a wolfCrypt crypto callback device for **device_id**. @@ -265,6 +264,5 @@ def _unregister(self) -> None: def default_device_id(cls) -> int: return _lib.wc_CryptoCb_DefaultDevID() - def _address(ptr: FFI.CData) -> int: return int(_ffi.cast("uintptr_t", ptr)) diff --git a/wolfcrypt/hashes.py b/wolfcrypt/hashes.py index 5ae2e02..f4cd671 100644 --- a/wolfcrypt/hashes.py +++ b/wolfcrypt/hashes.py @@ -497,7 +497,6 @@ def new(cls, key: BytesOrStr, string: BytesOrStr | None = None, *, device_id: in """ return cls(key, string, device_id) - @property @abstractmethod def _type(self) -> int: ... @@ -534,7 +533,6 @@ def _update(self, data: bytes) -> int: def _final(self, obj: FFI.CData, ret: FFI.CData) -> int: return _lib.wc_HmacFinal(obj, ret) - if _lib.SHA_ENABLED: class HmacSha(_Hmac): """ @@ -546,7 +544,6 @@ class HmacSha(_Hmac): _type = _TYPE_SHA digest_size = Sha.digest_size # ty: ignore[possibly-unresolved-reference] - if _lib.SHA256_ENABLED: class HmacSha256(_Hmac): """ @@ -558,7 +555,6 @@ class HmacSha256(_Hmac): _type = _TYPE_SHA256 digest_size = Sha256.digest_size # ty: ignore[possibly-unresolved-reference] - if _lib.SHA384_ENABLED: class HmacSha384(_Hmac): """ @@ -570,7 +566,6 @@ class HmacSha384(_Hmac): _type = _TYPE_SHA384 digest_size = Sha384.digest_size # ty: ignore[possibly-unresolved-reference] - if _lib.SHA512_ENABLED: class HmacSha512(_Hmac): """ @@ -582,6 +577,7 @@ class HmacSha512(_Hmac): _type = _TYPE_SHA512 digest_size = Sha512.digest_size # ty: ignore[possibly-unresolved-reference] + def hash_type_to_cls(hash_type: int) -> type[_Hash] | None: if _lib.SHA_ENABLED and hash_type == _lib.WC_HASH_TYPE_SHA: hash_cls = Sha diff --git a/wolfcrypt/utils.py b/wolfcrypt/utils.py index 8cd1e08..4bdf538 100644 --- a/wolfcrypt/utils.py +++ b/wolfcrypt/utils.py @@ -28,6 +28,7 @@ BytesOrStr: TypeAlias = bytes | bytearray | memoryview | str + def t2b(string: BytesOrStr) -> bytes: """ Converts text to bytes. diff --git a/wolfcrypt/wc_types.py b/wolfcrypt/wc_types.py index e5794bf..b5f97d6 100644 --- a/wolfcrypt/wc_types.py +++ b/wolfcrypt/wc_types.py @@ -29,6 +29,7 @@ class SupportsRsaSign(Protocol): def sign(self, plaintext: BytesOrStr) -> bytes: raise NotImplementedError + class SupportsRsaVerify(Protocol): @abstractmethod def verify(self, signature: BytesOrStr) -> bytes: