Skip to content

Commit 2f6317b

Browse files
Start adding typing information.
1 parent 4c1c578 commit 2f6317b

7 files changed

Lines changed: 118 additions & 17 deletions

File tree

tests/test_random.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def test_byte(rng):
3434

3535

3636
def test_bytes(rng):
37-
assert len(rng.bytes(1)) == 1
38-
assert len(rng.bytes(8)) == 8
39-
assert len(rng.bytes(128)) == 128
37+
assert len(rng.randbytes(1)) == 1
38+
assert len(rng.randbytes(8)) == 8
39+
assert len(rng.randbytes(128)) == 128
4040

4141
@pytest.fixture
4242
def rng_nonce():
@@ -47,4 +47,4 @@ def test_nonce_byte(rng_nonce):
4747

4848
@pytest.mark.parametrize("length", (1, 8, 128))
4949
def test_nonce_bytes(rng_nonce, length):
50-
assert len(rng_nonce.bytes(length)) == length
50+
assert len(rng_nonce.randbytes(length)) == length

wolfcrypt/_ffi/__init__.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import _cffi_backend
2+
3+
ffi: _cffi_backend.FFI
4+
5+
__all__ = ["lib"]

wolfcrypt/_ffi/lib.pyi

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
from _cffi_backend import FFI
3+
from typing_extensions import TypeAlias
4+
5+
INVALID_DEVID: int
6+
7+
AES_ENABLED: int
8+
AES_SIV_ENABLED: int
9+
AESGCM_STREAM_ENABLED: int
10+
ASN_ENABLED: int
11+
CHACHA_ENABLED: int
12+
CHACHA_STREAM_ENABLED: int
13+
CHACHA20_POLY1305_ENABLED: int
14+
DES3_ENABLED: int
15+
ECC_ENABLED: int
16+
ED25519_ENABLED: int
17+
ED448_ENABLED: int
18+
FIPS_ENABLED: int
19+
HMAC_ENABLED: int
20+
KEYGEN_ENABLED: int
21+
HKDF_ENABLED: int
22+
ML_DSA_ENABLED: int
23+
ML_KEM_ENABLED: int
24+
MPAPI_ENABLED: int
25+
PWDBASED_ENABLED: int
26+
RSA_ENABLED: int
27+
RSA_PSS_ENABLED: int
28+
SHA_ENABLED: int
29+
SHA3_ENABLED: int
30+
SHA256_ENABLED: int
31+
SHA384_ENABLED: int
32+
SHA512_ENABLED: int
33+
WC_RNG_SEED_CB_ENABLED: int
34+
35+
FIPS_VERSION: int
36+
37+
WC_MGF1NONE: int
38+
WC_MGF1SHA1: int
39+
WC_MGF1SHA224: int
40+
WC_MGF1SHA256: int
41+
WC_MGF1SHA384: int
42+
WC_MGF1SHA512: int
43+
44+
WC_HASH_TYPE_NONE: int
45+
WC_HASH_TYPE_MD2: int
46+
WC_HASH_TYPE_MD4: int
47+
WC_HASH_TYPE_MD5: int
48+
WC_HASH_TYPE_SHA: int
49+
WC_HASH_TYPE_SHA224: int
50+
WC_HASH_TYPE_SHA256: int
51+
WC_HASH_TYPE_SHA384: int
52+
WC_HASH_TYPE_SHA512: int
53+
WC_HASH_TYPE_MD5_SHA: int
54+
WC_HASH_TYPE_SHA3_224: int
55+
WC_HASH_TYPE_SHA3_256: int
56+
WC_HASH_TYPE_SHA3_384: int
57+
WC_HASH_TYPE_SHA3_512: int
58+
WC_HASH_TYPE_BLAKE2B: int
59+
WC_HASH_TYPE_BLAKE2S: int
60+
61+
WC_ML_KEM_512: int
62+
WC_ML_KEM_768: int
63+
WC_ML_KEM_1024: int
64+
65+
WC_ML_DSA_44: int
66+
WC_ML_DSA_65: int
67+
WC_ML_DSA_87: int
68+
69+
WC_KEYTYPE_ALL: int
70+
71+
RNG: TypeAlias = FFI.CData
72+
73+
def wc_InitRngNonce_ex(rng: RNG, nonce: FFI.CData, nonce_size: int, heap: FFI.CData, device_id: int) -> int: ...
74+
def wc_RNG_GenerateByte(rng: RNG, buffer: FFI.CData) -> int: ...
75+
def wc_RNG_GenerateBlock(rng: RNG, buffer: FFI.CData, len: int) -> int: ...
76+
def wc_FreeRng(rng: RNG) -> None: ...
77+
def wc_SetSeed_Cb(cdata: FFI.CData) -> int: ...
78+
79+
def wc_FreeRsaKey() -> None: ...
80+
81+
def wc_ecc_free() -> None: ...
82+
83+
def wc_ed25519_free() -> None: ...
84+
85+
def wc_ed448_free() -> None: ...
86+
87+
def wc_KyberKey_DecodePrivateKey() -> int: ...
88+
def wc_KyberKey_Decapsulate() -> int: ...
89+
90+
def wolfCrypt_SetPrivateKeyReadEnable_fips(a: int, b: int) -> int: ...

wolfcrypt/ciphers.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,6 @@ class ChaCha(_Cipher):
488488
key_size = None # 16, 24, 32
489489
_key_sizes = [16, 32]
490490
_native_type = "ChaCha *"
491-
_IV_nonce = []
492-
_IV_counter = 0
493491

494492
def __init__(self, key="", size=32):
495493
self._native_object = _ffi.new(self._native_type)
@@ -501,7 +499,7 @@ def __init__(self, key="", size=32):
501499
raise ValueError("Invalid key size %d" % size)
502500
self._key = t2b(key)
503501
self.key_size = size
504-
self._IV_nonce = []
502+
self._IV_nonce = b""
505503
self._IV_counter = 0
506504

507505
def _set_key(self, direction):
@@ -1990,7 +1988,7 @@ def encode_priv_key(self):
19901988

19911989
return _ffi.buffer(priv_key, priv_key_size)[:]
19921990

1993-
def decode_key(self, priv_key: tuple[bytes, str]):
1991+
def decode_key(self, priv_key: bytes):
19941992
"""
19951993
:param priv_key: private key to be imported
19961994
:type priv_key: bytes or str

wolfcrypt/hashes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ class _Hmac(_Hash):
282282
A **PEP 247: Cryptographic Hash Functions** compliant
283283
**Keyed Hash Function Interface**.
284284
"""
285-
digest_size = None
285+
digest_size = 0
286286
_native_type = "Hmac *"
287287
_native_size = _ffi.sizeof("Hmac")
288288

wolfcrypt/random.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
# pylint: disable=no-member,no-name-in-module
2222

23+
from __future__ import annotations
24+
25+
from _cffi_backend import FFI
26+
2327
from wolfcrypt._ffi import ffi as _ffi
2428
from wolfcrypt._ffi import lib as _lib
2529

@@ -31,8 +35,8 @@ class Random(object):
3135
A Cryptographically Secure Pseudo Random Number Generator - CSPRNG
3236
"""
3337

34-
def __init__(self, nonce=_ffi.NULL, device_id=_lib.INVALID_DEVID):
35-
self.native_object = _ffi.new("WC_RNG *")
38+
def __init__(self, nonce=_ffi.NULL, device_id=_lib.INVALID_DEVID) -> None:
39+
self.native_object: FFI.CData | None = _ffi.new("WC_RNG *")
3640

3741
if nonce == _ffi.NULL:
3842
nonce_size = 0
@@ -46,32 +50,34 @@ def __init__(self, nonce=_ffi.NULL, device_id=_lib.INVALID_DEVID):
4650
# making sure _lib.wc_FreeRng outlives WC_RNG instances
4751
_delete = _lib.wc_FreeRng
4852

49-
def __del__(self):
53+
def __del__(self) -> None:
5054
if self.native_object:
5155
try:
52-
self._delete(self.native_object)
56+
Random._delete(self.native_object)
5357
except AttributeError:
5458
# Can occur during interpreter shutdown
5559
pass
5660

57-
def byte(self):
61+
def byte(self) -> bytes:
5862
"""
5963
Generate and return a random byte.
6064
"""
6165
result = _ffi.new('byte[1]')
6266

67+
assert self.native_object is not None
6368
ret = _lib.wc_RNG_GenerateByte(self.native_object, result)
6469
if ret < 0: # pragma: no cover
6570
raise WolfCryptError("RNG generate byte error (%d)" % ret)
6671

6772
return _ffi.buffer(result, 1)[:]
6873

69-
def bytes(self, length):
74+
def randbytes(self, length) -> bytes:
7075
"""
7176
Generate and return a random sequence of length bytes.
7277
"""
7378
result = _ffi.new('byte[%d]' % length)
7479

80+
assert self.native_object is not None
7581
ret = _lib.wc_RNG_GenerateBlock(self.native_object, result, length)
7682
if ret < 0: # pragma: no cover
7783
raise WolfCryptError("RNG generate block error (%d)" % ret)

wolfcrypt/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020

2121
# pylint: disable=unused-import
2222

23+
from __future__ import annotations
24+
2325
from binascii import hexlify as b2h, unhexlify as h2b # noqa: F401
2426

2527

26-
def t2b(string):
28+
def t2b(string: bytes | bytearray | memoryview | str) -> bytes:
2729
"""
2830
Converts text to binary.
2931
3032
Passes through bytes, bytearray, and memoryview unchanged.
3133
Encodes str to UTF-8 bytes.
3234
"""
3335
if isinstance(string, (bytes, bytearray, memoryview)):
34-
return string
36+
return bytes(string)
3537
return str(string).encode("utf-8")

0 commit comments

Comments
 (0)