Skip to content

Commit b17732c

Browse files
Start adding typing information.
Initially only random.py and utils.py.
1 parent 6fbdf3d commit b17732c

4 files changed

Lines changed: 99 additions & 14 deletions

File tree

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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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: bytes, 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: ...

wolfcrypt/random.py

Lines changed: 14 additions & 12 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,47 +35,45 @@ 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):
38+
def __init__(self, nonce=b"", device_id=_lib.INVALID_DEVID) -> None:
3539
self.native_object = _ffi.new("WC_RNG *")
3640

37-
if nonce == _ffi.NULL:
38-
nonce_size = 0
39-
else:
40-
nonce_size = len(nonce)
41-
ret = _lib.wc_InitRngNonce_ex(self.native_object, nonce, nonce_size, _ffi.NULL, device_id)
41+
ret = _lib.wc_InitRngNonce_ex(self.native_object, nonce, len(nonce), _ffi.NULL, device_id)
4242
if ret < 0: # pragma: no cover
4343
self.native_object = None
4444
raise WolfCryptError("RNG init error (%d)" % ret)
4545

4646
# making sure _lib.wc_FreeRng outlives WC_RNG instances
4747
_delete = _lib.wc_FreeRng
4848

49-
def __del__(self):
49+
def __del__(self) -> None:
5050
if self.native_object:
5151
try:
52-
self._delete(self.native_object)
52+
Random._delete(self.native_object)
5353
except AttributeError:
5454
# Can occur during interpreter shutdown
5555
pass
5656

57-
def byte(self):
57+
def byte(self) -> __builtins__.bytes:
5858
"""
5959
Generate and return a random byte.
6060
"""
61-
result = _ffi.new('byte[1]')
61+
result = _ffi.new("byte[1]")
6262

63+
assert self.native_object is not None
6364
ret = _lib.wc_RNG_GenerateByte(self.native_object, result)
6465
if ret < 0: # pragma: no cover
6566
raise WolfCryptError("RNG generate byte error (%d)" % ret)
6667

6768
return _ffi.buffer(result, 1)[:]
6869

69-
def bytes(self, length):
70+
def bytes(self, length) -> __builtins__.bytes:
7071
"""
7172
Generate and return a random sequence of length bytes.
7273
"""
73-
result = _ffi.new('byte[%d]' % length)
74+
result = _ffi.new("byte[%d]" % length)
7475

76+
assert self.native_object is not None
7577
ret = _lib.wc_RNG_GenerateBlock(self.native_object, result, length)
7678
if ret < 0: # pragma: no cover
7779
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)