Simplify with keyword-only arguments

This commit is contained in:
Helder Eijs 2025-03-08 22:51:33 +01:00
parent 6a692cc84c
commit 6e71a82251
2 changed files with 7 additions and 19 deletions

View file

@ -2,7 +2,7 @@ import struct
from enum import IntEnum from enum import IntEnum
from types import ModuleType from types import ModuleType
from typing import Optional, TypedDict, NotRequired, Unpack from typing import Optional
from .KDF import _HKDF_extract, _HKDF_expand from .KDF import _HKDF_extract, _HKDF_expand
from .DH import key_agreement, import_x25519_public_key, import_x448_public_key from .DH import key_agreement, import_x25519_public_key, import_x448_public_key
@ -363,16 +363,12 @@ class HPKE_Cipher:
return pt return pt
class RequestParams(TypedDict): def new(*, receiver_key: EccKey,
enc: NotRequired[bytes]
sender_key: NotRequired[EccKey]
psk: NotRequired[tuple[bytes, bytes]]
info: NotRequired[bytes]
def new(receiver_key: EccKey,
aead_id: AEAD, aead_id: AEAD,
**kwargs: Unpack[RequestParams]): enc: Optional[bytes] = None,
sender_key: Optional[EccKey] = None,
psk: Optional[tuple[bytes, bytes]] = None,
info: bytes = b''):
"""Create an HPKE context which can be used """Create an HPKE context which can be used
by the sender to seal (encrypt) a message by the sender to seal (encrypt) a message
or by the receiver to unseal (decrypt) it. or by the receiver to unseal (decrypt) it.
@ -440,14 +436,6 @@ def new(receiver_key: EccKey,
.. _HPKE: https://datatracker.ietf.org/doc/rfc9180/ .. _HPKE: https://datatracker.ietf.org/doc/rfc9180/
""" """
sender_key = kwargs.pop('sender_key', None)
psk = kwargs.pop('psk', None)
info = kwargs.pop('info', b'')
enc = kwargs.pop('enc', None)
if kwargs:
raise ValueError(f"Unknown parameters: {list(kwargs.keys())}")
if aead_id not in AEAD: if aead_id not in AEAD:
raise ValueError(f"Unknown AEAD cipher ID {aead_id:#x}") raise ValueError(f"Unknown AEAD cipher ID {aead_id:#x}")

View file

@ -33,7 +33,7 @@ def get_tests(config={}):
from Crypto.SelfTest.Protocol import test_SecretSharing from Crypto.SelfTest.Protocol import test_SecretSharing
tests += test_SecretSharing.get_tests(config=config) tests += test_SecretSharing.get_tests(config=config)
if sys.version_info >= (3, 11): if sys.version_info >= (3, 9):
from Crypto.SelfTest.Protocol import test_HPKE from Crypto.SelfTest.Protocol import test_HPKE
tests += test_HPKE.get_tests(config=config) tests += test_HPKE.get_tests(config=config)