diff --git a/wolfcrypt/__init__.py b/wolfcrypt/__init__.py index dccbda3..1b15dae 100644 --- a/wolfcrypt/__init__.py +++ b/wolfcrypt/__init__.py @@ -18,6 +18,10 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA +import os +import sys +from typing import TYPE_CHECKING + from wolfcrypt._version import __version__, __wolfssl_version__ __title__ = "wolfcrypt" @@ -36,9 +40,6 @@ __all__ = [ "ciphers", "hashes", "random", "pwdbased", "cryptocb" ] -import os -import sys - top_level_py = os.path.basename(sys.argv[0]) # The code below is intended to only be used after the CFFI is built, so we @@ -46,8 +47,10 @@ top_level_py = os.path.basename(sys.argv[0]) if top_level_py not in ["setup.py", "build_ffi.py"]: from wolfcrypt._ffi import ffi as _ffi from wolfcrypt._ffi import lib as _lib - if _lib.CRYPTO_CB_ENABLED: - from wolfcrypt.cryptocb import CryptoCallback + + if TYPE_CHECKING: + if _lib.CRYPTO_CB_ENABLED: + from wolfcrypt.cryptocb import CryptoCallback from wolfcrypt.exceptions import WolfCryptApiError ret = _lib.wolfCrypt_Init() diff --git a/wolfcrypt/_ffi/lib.pyi b/wolfcrypt/_ffi/lib.pyi index ca50324..d08d444 100644 --- a/wolfcrypt/_ffi/lib.pyi +++ b/wolfcrypt/_ffi/lib.pyi @@ -18,9 +18,12 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -from _cffi_backend import FFI +from collections.abc import Callable +from dataclasses import dataclass from typing import TypeAlias +from _cffi_backend import FFI + INVALID_DEVID: int AES_ENABLED: int @@ -30,6 +33,7 @@ ASN_ENABLED: int CHACHA_ENABLED: int CHACHA_STREAM_ENABLED: int CHACHA20_POLY1305_ENABLED: int +CRYPTO_CB_ENABLED: int DES3_ENABLED: int ECC_ENABLED: int ED25519_ENABLED: int @@ -282,6 +286,20 @@ WC_MGF1SHA256: int WC_MGF1SHA384: int WC_MGF1SHA512: int +WC_ALGO_TYPE_NONE: int +WC_ALGO_TYPE_HASH: int +WC_ALGO_TYPE_CIPHER: int +WC_ALGO_TYPE_PK: int +WC_ALGO_TYPE_RNG: int +WC_ALGO_TYPE_SEED: int +WC_ALGO_TYPE_HMAC: int +WC_ALGO_TYPE_CMAC: int +WC_ALGO_TYPE_CERT: int +WC_ALGO_TYPE_KDF: int +WC_ALGO_TYPE_COPY: int +WC_ALGO_TYPE_FREE: int +WC_ALGO_TYPE_MAX: int + WC_HASH_TYPE_NONE: int WC_HASH_TYPE_MD2: int WC_HASH_TYPE_MD4: int @@ -328,6 +346,7 @@ RNG: TypeAlias = FFI.CData def wc_SetSeed_Cb(cb: FFI.CData) -> int: ... def wolfCrypt_SetPrivateKeyReadEnable_fips(enable: int, key_type: int) -> int: ... def wc_GetErrorString(error: int) -> FFI.CData: ... +def wolfCrypt_Init() -> int: ... def wc_InitRngNonce_ex(rng: RNG, nonce: bytes, nonce_size: int, heap: FFI.CData, device_id: int) -> int: ... def wc_RNG_GenerateByte(rng: RNG, buffer: FFI.CData) -> int: ... @@ -569,3 +588,36 @@ def wc_dilithium_sign_msg_with_seed(msg: bytes, msg_len: int, sig: BytePtr, sig_ def wc_MlDsaKey_GetPrivLen(key: DilithiumKey, len: IntPtr) -> int: ... def wc_MlDsaKey_GetPubLen(key: DilithiumKey, len: IntPtr) -> int: ... def wc_MlDsaKey_GetSigLen(key: DilithiumKey, len: IntPtr) -> int: ... + +@dataclass +class wc_HashInfo: + type: int + data: FFI.CData + data_size: int + digest: FFI.CData + sha: FFI.CData + sha256: FFI.CData + sha384: FFI.CData + sha512: FFI.CData + sha3: FFI.CData + ctx: FFI.CData + + +@dataclass +class wc_RngInfo: + rng: RNG + out: FFI.CData + sz: int + + +@dataclass +class wc_CryptoInfo: + algo_type: int + hash: wc_HashInfo + rng: wc_RngInfo + + +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: ... +def py_wc_crypto_callback(dev_id: int, info: wc_CryptoInfo, ctx: FFI.CData) -> int: ... diff --git a/wolfcrypt/cryptocb.py b/wolfcrypt/cryptocb.py index 566c762..89f6a05 100644 --- a/wolfcrypt/cryptocb.py +++ b/wolfcrypt/cryptocb.py @@ -24,8 +24,7 @@ from __future__ import annotations import logging from collections import defaultdict -from types import TracebackType -from typing import Final +from typing import TYPE_CHECKING, Final from typing_extensions import Self @@ -33,6 +32,9 @@ from wolfcrypt._ffi import ffi as _ffi from wolfcrypt._ffi import lib as _lib from wolfcrypt.exceptions import WolfCryptError +if TYPE_CHECKING: + from types import TracebackType + log = logging.getLogger(__name__) if _lib.CRYPTO_CB_ENABLED: @@ -100,7 +102,7 @@ if _lib.CRYPTO_CB_ENABLED: def __del__(self) -> None: self._unregister() - def callback(self, device_id: int, info: _ffi.CData) -> int: + def callback(self, device_id: int, info: _lib.wc_CryptoInfo) -> int: log.debug("device_id=%d algo_type = %s", device_id, ALGO_TYPE_NAME[info.algo_type]) try: if info.algo_type == _lib.WC_ALGO_TYPE_HASH: @@ -137,7 +139,7 @@ if _lib.CRYPTO_CB_ENABLED: except NotImplementedError: return _lib.CRYPTOCB_UNAVAILABLE - def rng_callback(self, device_id: int, rng, size: int) -> bytes: + def rng_callback(self, device_id: int, rng: _lib.RNG, size: int) -> bytes: raise NotImplementedError def hash_update_callback(self, device_id: int, hash_type: int, data: bytes) -> None: