From f1d32bc045977f0791767b60dc135e52b607f0a5 Mon Sep 17 00:00:00 2001 From: Flaviu Tamas Date: Sun, 16 Aug 2026 15:27:46 -0400 Subject: [PATCH] Correct some type annotations I noticed that some of the type annotations are either incomplete or out of date. This makes it harder to use the package correctly, since it's not possible to tell at build time that the API is correctly used. --- lib/Crypto/Cipher/AES.pyi | 5 +++-- lib/Crypto/Cipher/_mode_ocb.pyi | 12 +++--------- lib/Crypto/Hash/Poly1305.pyi | 1 + lib/Crypto/Protocol/KDF.pyi | 25 +++++++++++++++++++------ lib/Crypto/Signature/eddsa.pyi | 2 +- lib/Crypto/Signature/pss.pyi | 8 ++++++-- lib/Crypto/Util/Counter.pyi | 19 +++++++++++++++---- 7 files changed, 48 insertions(+), 24 deletions(-) diff --git a/lib/Crypto/Cipher/AES.pyi b/lib/Crypto/Cipher/AES.pyi index aa4ec80f0..4843557c4 100644 --- a/lib/Crypto/Cipher/AES.pyi +++ b/lib/Crypto/Cipher/AES.pyi @@ -1,4 +1,4 @@ -from typing import Dict, Optional, Tuple, Union, overload +from typing import Optional, Tuple, Union, overload from typing_extensions import Literal Buffer=bytes|bytearray|memoryview @@ -16,6 +16,7 @@ from Crypto.Cipher._mode_siv import SivMode from Crypto.Cipher._mode_ocb import OcbMode from Crypto.Cipher._mode_kw import KWMode from Crypto.Cipher._mode_kwp import KWPMode +from Crypto.Util.Counter import CounterParams MODE_ECB: Literal[1] MODE_CBC: Literal[2] @@ -91,7 +92,7 @@ def new(key: Buffer, mode: Literal[6], nonce : Optional[Buffer] = ..., initial_value : Union[int, Buffer] = ..., - counter : Dict = ..., + counter : Optional[CounterParams] = ..., use_aesni : bool = ...) -> \ CtrMode: ... diff --git a/lib/Crypto/Cipher/_mode_ocb.pyi b/lib/Crypto/Cipher/_mode_ocb.pyi index a1909fc38..42d70c231 100644 --- a/lib/Crypto/Cipher/_mode_ocb.pyi +++ b/lib/Crypto/Cipher/_mode_ocb.pyi @@ -1,5 +1,5 @@ from types import ModuleType -from typing import Union, Any, Optional, Tuple, Dict, overload +from typing import Union, Any, Optional, Tuple, Dict Buffer = Union[bytes, bytearray, memoryview] @@ -15,14 +15,8 @@ class OcbMode(object): def update(self, assoc_data: Buffer) -> OcbMode: ... - @overload - def encrypt(self, plaintext: Buffer) -> bytes: ... - @overload - def encrypt(self, plaintext: Buffer, output: Union[bytearray, memoryview]) -> None: ... - @overload - def decrypt(self, plaintext: Buffer) -> bytes: ... - @overload - def decrypt(self, plaintext: Buffer, output: Union[bytearray, memoryview]) -> None: ... + def encrypt(self, plaintext: Optional[Buffer] = ...) -> bytes: ... + def decrypt(self, plaintext: Optional[Buffer] = ...) -> bytes: ... def digest(self) -> bytes: ... def hexdigest(self) -> str: ... diff --git a/lib/Crypto/Hash/Poly1305.pyi b/lib/Crypto/Hash/Poly1305.pyi index f97a14a01..f4d0a229b 100644 --- a/lib/Crypto/Hash/Poly1305.pyi +++ b/lib/Crypto/Hash/Poly1305.pyi @@ -7,6 +7,7 @@ class Poly1305_MAC(object): block_size: int digest_size: int oid: str + nonce: bytes def __init__(self, r : int, diff --git a/lib/Crypto/Protocol/KDF.pyi b/lib/Crypto/Protocol/KDF.pyi index 80691e0f9..49dbfcc5d 100644 --- a/lib/Crypto/Protocol/KDF.pyi +++ b/lib/Crypto/Protocol/KDF.pyi @@ -1,5 +1,5 @@ from types import ModuleType -from typing import Optional, Callable, Tuple, Union, Dict, Any, overload +from typing import Optional, Callable, List, Union, Dict, Any, overload from typing_extensions import Literal Buffer=bytes|bytearray|memoryview @@ -7,8 +7,8 @@ Buffer=bytes|bytearray|memoryview RNG = Callable[[int], bytes] PRF = Callable[[bytes, bytes], bytes] -def PBKDF1(password: str, salt: bytes, dkLen: int, count: Optional[int]=1000, hashAlgo: Optional[ModuleType]=None) -> bytes: ... -def PBKDF2(password: str, salt: bytes, dkLen: Optional[int]=16, count: Optional[int]=1000, prf: Optional[RNG]=None, hmac_hash_module: Optional[ModuleType]=None) -> bytes: ... +def PBKDF1(password: Union[str, Buffer], salt: Buffer, dkLen: int, count: Optional[int]=1000, hashAlgo: Optional[ModuleType]=None) -> bytes: ... +def PBKDF2(password: Union[str, Buffer], salt: Union[str, Buffer], dkLen: Optional[int]=16, count: Optional[int]=1000, prf: Optional[RNG]=None, hmac_hash_module: Optional[ModuleType]=None) -> bytes: ... class _S2V(object): def __init__(self, key: bytes, ciphermod: ModuleType, cipher_params: Optional[Dict[Any, Any]]=None) -> None: ... @@ -20,9 +20,15 @@ class _S2V(object): def _HKDF_extract(salt: Buffer, ikm: Buffer, hashmod: ModuleType) -> bytes: ... def _HKDF_expand(prk: Buffer, info: Buffer, L: int, hashmod) -> bytes : ... -def HKDF(master: bytes, key_len: int, salt: bytes, hashmod: ModuleType, num_keys: Optional[int]=1, context: Optional[bytes]=None) -> Union[bytes, Tuple[bytes, ...]]: ... +@overload +def HKDF(master: Buffer, key_len: int, salt: Buffer, hashmod: ModuleType, num_keys: Literal[1]=1, context: Optional[Buffer]=None) -> bytes: ... +@overload +def HKDF(master: Buffer, key_len: int, salt: Buffer, hashmod: ModuleType, num_keys: int, context: Optional[Buffer]=None) -> Union[bytes, List[bytes]]: ... -def scrypt(password: str, salt: str, key_len: int, N: int, r: int, p: int, num_keys: Optional[int]=1) -> Union[bytes, Tuple[bytes, ...]]: ... +@overload +def scrypt(password: Union[str, Buffer], salt: Union[str, Buffer], key_len: int, N: int, r: int, p: int, num_keys: Literal[1]=1) -> bytes: ... +@overload +def scrypt(password: Union[str, Buffer], salt: Union[str, Buffer], key_len: int, N: int, r: int, p: int, num_keys: int) -> Union[bytes, List[bytes]]: ... def _bcrypt_decode(data: bytes) -> bytes: ... def _bcrypt_hash(password:bytes , cost: int, salt: bytes, constant:bytes, invert:bool) -> bytes: ... @@ -36,9 +42,16 @@ def SP800_108_Counter(master: Buffer, num_keys: Literal[None] = None, label: Buffer = b'', context: Buffer = b'') -> bytes: ... +@overload +def SP800_108_Counter(master: Buffer, + key_len: int, + prf: PRF, + num_keys: Literal[1], + label: Buffer = b'', context: Buffer = b'') -> bytes: ... + @overload def SP800_108_Counter(master: Buffer, key_len: int, prf: PRF, num_keys: int, - label: Buffer = b'', context: Buffer = b'') -> Tuple[bytes]: ... + label: Buffer = b'', context: Buffer = b'') -> Union[bytes, List[bytes]]: ... diff --git a/lib/Crypto/Signature/eddsa.pyi b/lib/Crypto/Signature/eddsa.pyi index 060d5938f..f79b4303f 100644 --- a/lib/Crypto/Signature/eddsa.pyi +++ b/lib/Crypto/Signature/eddsa.pyi @@ -6,7 +6,7 @@ class Hash(Protocol): def digest(self) -> bytes: ... class XOF(Protocol): - def read(self, len: int) -> bytes: ... + def read(self, length: int) -> bytes: ... def import_public_key(encoded: bytes) -> EccKey: ... def import_private_key(encoded: bytes) -> EccKey: ... diff --git a/lib/Crypto/Signature/pss.pyi b/lib/Crypto/Signature/pss.pyi index d4088e166..11ff054e8 100644 --- a/lib/Crypto/Signature/pss.pyi +++ b/lib/Crypto/Signature/pss.pyi @@ -3,15 +3,19 @@ from typing_extensions import Protocol from Crypto.PublicKey.RSA import RsaKey +Buffer = Union[bytes, bytearray, memoryview] class Hash(Protocol): + digest_size: int def digest(self) -> bytes: ... - def update(self, bytes) -> None: ... + def update(self, data: Buffer) -> None: ... + def new(self) -> Hash: ... class HashModule(Protocol): + digest_size: int @staticmethod - def new(data: Optional[bytes]) -> Hash: ... + def new() -> Hash: ... MaskFunction = Callable[[bytes, int, Union[Hash, HashModule]], bytes] diff --git a/lib/Crypto/Util/Counter.pyi b/lib/Crypto/Util/Counter.pyi index fa2ffdd8f..5fc21d423 100644 --- a/lib/Crypto/Util/Counter.pyi +++ b/lib/Crypto/Util/Counter.pyi @@ -1,5 +1,16 @@ -from typing import Optional, Union, Dict +from typing import Union +from typing_extensions import TypedDict -def new(nbits: int, prefix: Optional[bytes]=..., suffix: Optional[bytes]=..., initial_value: Optional[int]=1, - little_endian: Optional[bool]=False, allow_wraparound: Optional[bool]=False) -> \ - Dict[str, Union[int, bytes, bool]]: ... +Buffer = Union[bytes, bytearray, memoryview] +CounterBuffer = Union[bytes, bytearray] + +class CounterParams(TypedDict): + counter_len: int + prefix: CounterBuffer + suffix: Buffer + initial_value: int + little_endian: bool + +def new(nbits: int, prefix: CounterBuffer=..., suffix: Buffer=..., initial_value: int=1, + little_endian: bool=False, allow_wraparound: bool=False) -> \ + CounterParams: ...