classMLKEM_512(BaseKEM):
variant: PQAVariantspec: AlgoSpecdef__init__(self, variant: const.PQAVariant=None, *, allow_fallback: bool=True) ->None:
"""Initializes the MLKEM_512 key encapsulation mechanism algorithm instance with compiled C extension binaries."""@propertydefparam_sizes(self) ->KEMParamSizes:
"""Returns the size of the params of this KEM algorithm."""defkeygen(self) ->tuple[bytes, bytes]:
"""Returns a tuple of public key and secret key bytes."""defencaps(self, public_key: bytes) ->tuple[bytes, bytes]:
"""Returns a tuple of ciphertext and shared secret bytes."""defdecaps(self, secret_key: bytes, cipher_text: bytes) ->bytes:
"""Returns the bytes of the decapsulated shared secret."""defarmor(self, key_bytes: bytes) ->str:
"""Returns a base64-encoded ASCII string of the key bytes."""defdearmor(self, armored_key: str) ->bytes:
"""Returns the key bytes from an armored key ASCII string."""
classMLKEM_768(BaseKEM):
variant: PQAVariantspec: AlgoSpecdef__init__(self, variant: const.PQAVariant=None, *, allow_fallback: bool=True) ->None:
"""Initializes the MLKEM_768 key encapsulation mechanism algorithm instance with compiled C extension binaries."""@propertydefparam_sizes(self) ->KEMParamSizes:
"""Returns the size of the params of this KEM algorithm."""defkeygen(self) ->tuple[bytes, bytes]:
"""Returns a tuple of public key and secret key bytes."""defencaps(self, public_key: bytes) ->tuple[bytes, bytes]:
"""Returns a tuple of ciphertext and shared secret bytes."""defdecaps(self, secret_key: bytes, cipher_text: bytes) ->bytes:
"""Returns the bytes of the decapsulated shared secret."""defarmor(self, key_bytes: bytes) ->str:
"""Returns a base64-encoded ASCII string of the key bytes."""defdearmor(self, armored_key: str) ->bytes:
"""Returns the key bytes from an armored key ASCII string."""
classMLKEM_1024(BaseKEM):
variant: PQAVariantspec: AlgoSpecdef__init__(self, variant: const.PQAVariant=None, *, allow_fallback: bool=True) ->None:
"""Initializes the MLKEM_1024 key encapsulation mechanism algorithm instance with compiled C extension binaries."""@propertydefparam_sizes(self) ->KEMParamSizes:
"""Returns the size of the params of this KEM algorithm."""defkeygen(self) ->tuple[bytes, bytes]:
"""Returns a tuple of public key and secret key bytes."""defencaps(self, public_key: bytes) ->tuple[bytes, bytes]:
"""Returns a tuple of ciphertext and shared secret bytes."""defdecaps(self, secret_key: bytes, cipher_text: bytes) ->bytes:
"""Returns the bytes of the decapsulated shared secret."""defarmor(self, key_bytes: bytes) ->str:
"""Returns a base64-encoded ASCII string of the key bytes."""defdearmor(self, armored_key: str) ->bytes:
"""Returns the key bytes from an armored key ASCII string."""
classMLDSA_44(BaseDSS):
variant: PQAVariantspec: AlgoSpecdef__init__(self, variant: const.PQAVariant=None, *, allow_fallback: bool=True) ->None:
"""Initializes the MLDSA_44 digital signature scheme algorithm instance with compiled C extension binaries."""@propertydefparam_sizes(self) ->DSSParamSizes:
"""Returns the size of the params of this DSS algorithm."""defkeygen(self) ->tuple[bytes, bytes]:
"""Returns a tuple of public key and secret key bytes."""defsign(self, secret_key: bytes, message: bytes) ->bytes:
"""Returns bytes of the generated signature."""defverify(
self,
public_key: bytes,
message: bytes,
signature: bytes,
*,
raises: bool=True
) ->bool:
"""Returns True on successful signature verification."""defsign_file(
self,
secret_key: str|bytes,
data_file: str|Path,
callback: Optional[Callable] =None
) ->SignedFile:
"""Computes and signs the hash of the data file to generate the signature."""defverify_file(
self,
public_key: str|bytes,
data_file: str|Path,
signature: bytes,
callback: Optional[Callable] =None,
*,
raises: bool=True
) ->bool:
"""Verifies the signature against the computed hash of the data file."""defarmor(self, key_bytes: bytes) ->str:
"""Returns a base64-encoded ASCII string of the key bytes."""defdearmor(self, armored_key: str) ->bytes:
"""Returns the key bytes from an armored key ASCII string."""
classMLDSA_65(BaseDSS):
variant: PQAVariantspec: AlgoSpecdef__init__(self, variant: const.PQAVariant=None, *, allow_fallback: bool=True) ->None:
"""Initializes the MLDSA_65 digital signature scheme algorithm instance with compiled C extension binaries."""@propertydefparam_sizes(self) ->DSSParamSizes:
"""Returns the size of the params of this DSS algorithm."""defkeygen(self) ->tuple[bytes, bytes]:
"""Returns a tuple of public key and secret key bytes."""defsign(self, secret_key: bytes, message: bytes) ->bytes:
"""Returns bytes of the generated signature."""defverify(
self,
public_key: bytes,
message: bytes,
signature: bytes,
*,
raises: bool=True
) ->bool:
"""Returns True on successful signature verification."""defsign_file(
self,
secret_key: str|bytes,
data_file: str|Path,
callback: Optional[Callable] =None
) ->SignedFile:
"""Computes and signs the hash of the data file to generate the signature."""defverify_file(
self,
public_key: str|bytes,
data_file: str|Path,
signature: bytes,
callback: Optional[Callable] =None,
*,
raises: bool=True
) ->bool:
"""Verifies the signature against the computed hash of the data file."""defarmor(self, key_bytes: bytes) ->str:
"""Returns a base64-encoded ASCII string of the key bytes."""defdearmor(self, armored_key: str) ->bytes:
"""Returns the key bytes from an armored key ASCII string."""
classMLDSA_87(BaseDSS):
variant: PQAVariantspec: AlgoSpecdef__init__(self, variant: const.PQAVariant=None, *, allow_fallback: bool=True) ->None:
"""Initializes the MLDSA_87 digital signature scheme algorithm instance with compiled C extension binaries."""@propertydefparam_sizes(self) ->DSSParamSizes:
"""Returns the size of the params of this DSS algorithm."""defkeygen(self) ->tuple[bytes, bytes]:
"""Returns a tuple of public key and secret key bytes."""defsign(self, secret_key: bytes, message: bytes) ->bytes:
"""Returns bytes of the generated signature."""defverify(
self,
public_key: bytes,
message: bytes,
signature: bytes,
*,
raises: bool=True
) ->bool:
"""Returns True on successful signature verification."""defsign_file(
self,
secret_key: str|bytes,
data_file: str|Path,
callback: Optional[Callable] =None
) ->SignedFile:
"""Computes and signs the hash of the data file to generate the signature."""defverify_file(
self,
public_key: str|bytes,
data_file: str|Path,
signature: bytes,
callback: Optional[Callable] =None,
*,
raises: bool=True
) ->bool:
"""Verifies the signature against the computed hash of the data file."""defarmor(self, key_bytes: bytes) ->str:
"""Returns a base64-encoded ASCII string of the key bytes."""defdearmor(self, armored_key: str) ->bytes:
"""Returns the key bytes from an armored key ASCII string."""
classFALCON_512(BaseDSS):
variant: PQAVariantspec: AlgoSpecdef__init__(self, variant: const.PQAVariant=None, *, allow_fallback: bool=True) ->None:
"""Initializes the FALCON_512 digital signature scheme algorithm instance with compiled C extension binaries."""@propertydefparam_sizes(self) ->DSSParamSizes:
"""Returns the size of the params of this DSS algorithm."""defkeygen(self) ->tuple[bytes, bytes]:
"""Returns a tuple of public key and secret key bytes."""defsign(self, secret_key: bytes, message: bytes) ->bytes:
"""Returns bytes of the generated signature."""defverify(
self,
public_key: bytes,
message: bytes,
signature: bytes,
*,
raises: bool=True
) ->bool:
"""Returns True on successful signature verification."""defsign_file(
self,
secret_key: str|bytes,
data_file: str|Path,
callback: Optional[Callable] =None
) ->SignedFile:
"""Computes and signs the hash of the data file to generate the signature."""defverify_file(
self,
public_key: str|bytes,
data_file: str|Path,
signature: bytes,
callback: Optional[Callable] =None,
*,
raises: bool=True
) ->bool:
"""Verifies the signature against the computed hash of the data file."""defarmor(self, key_bytes: bytes) ->str:
"""Returns a base64-encoded ASCII string of the key bytes."""defdearmor(self, armored_key: str) ->bytes:
"""Returns the key bytes from an armored key ASCII string."""
classFALCON_1024(BaseDSS):
variant: PQAVariantspec: AlgoSpecdef__init__(self, variant: const.PQAVariant=None, *, allow_fallback: bool=True) ->None:
"""Initializes the FALCON_1024 digital signature scheme algorithm instance with compiled C extension binaries."""@propertydefparam_sizes(self) ->DSSParamSizes:
"""Returns the size of the params of this DSS algorithm."""defkeygen(self) ->tuple[bytes, bytes]:
"""Returns a tuple of public key and secret key bytes."""defsign(self, secret_key: bytes, message: bytes) ->bytes:
"""Returns bytes of the generated signature."""defverify(
self,
public_key: bytes,
message: bytes,
signature: bytes,
*,
raises: bool=True
) ->bool:
"""Returns True on successful signature verification."""defsign_file(
self,
secret_key: str|bytes,
data_file: str|Path,
callback: Optional[Callable] =None
) ->SignedFile:
"""Computes and signs the hash of the data file to generate the signature."""defverify_file(
self,
public_key: str|bytes,
data_file: str|Path,
signature: bytes,
callback: Optional[Callable] =None,
*,
raises: bool=True
) ->bool:
"""Verifies the signature against the computed hash of the data file."""defarmor(self, key_bytes: bytes) ->str:
"""Returns a base64-encoded ASCII string of the key bytes."""defdearmor(self, armored_key: str) ->bytes:
"""Returns the key bytes from an armored key ASCII string."""
classFAST_SPHINCS(BaseDSS):
variant: PQAVariantspec: AlgoSpecdef__init__(self, variant: const.PQAVariant=None, *, allow_fallback: bool=True) ->None:
"""Initializes the FAST_SPHINCS digital signature scheme algorithm instance with compiled C extension binaries."""@propertydefparam_sizes(self) ->DSSParamSizes:
"""Returns the size of the params of this DSS algorithm."""defkeygen(self) ->tuple[bytes, bytes]:
"""Returns a tuple of public key and secret key bytes."""defsign(self, secret_key: bytes, message: bytes) ->bytes:
"""Returns bytes of the generated signature."""defverify(
self,
public_key: bytes,
message: bytes,
signature: bytes,
*,
raises: bool=True
) ->bool:
"""Returns True on successful signature verification."""defsign_file(
self,
secret_key: str|bytes,
data_file: str|Path,
callback: Optional[Callable] =None
) ->SignedFile:
"""Computes and signs the hash of the data file to generate the signature."""defverify_file(
self,
public_key: str|bytes,
data_file: str|Path,
signature: bytes,
callback: Optional[Callable] =None,
*,
raises: bool=True
) ->bool:
"""Verifies the signature against the computed hash of the data file."""defarmor(self, key_bytes: bytes) ->str:
"""Returns a base64-encoded ASCII string of the key bytes."""defdearmor(self, armored_key: str) ->bytes:
"""Returns the key bytes from an armored key ASCII string."""
classSMALL_SPHINCS(BaseDSS):
variant: PQAVariantspec: AlgoSpecdef__init__(self, variant: const.PQAVariant=None, *, allow_fallback: bool=True) ->None:
"""Initializes the SMALL_SPHINCS digital signature scheme algorithm instance with compiled C extension binaries."""@propertydefparam_sizes(self) ->DSSParamSizes:
"""Returns the size of the params of this DSS algorithm."""defkeygen(self) ->tuple[bytes, bytes]:
"""Returns a tuple of public key and secret key bytes."""defsign(self, secret_key: bytes, message: bytes) ->bytes:
"""Returns bytes of the generated signature."""defverify(
self,
public_key: bytes,
message: bytes,
signature: bytes,
*,
raises: bool=True
) ->bool:
"""Returns True on successful signature verification."""defsign_file(
self,
secret_key: str|bytes,
data_file: str|Path,
callback: Optional[Callable] =None
) ->SignedFile:
"""Computes and signs the hash of the data file to generate the signature."""defverify_file(
self,
public_key: str|bytes,
data_file: str|Path,
signature: bytes,
callback: Optional[Callable] =None,
*,
raises: bool=True
) ->bool:
"""Verifies the signature against the computed hash of the data file."""defarmor(self, key_bytes: bytes) ->str:
"""Returns a base64-encoded ASCII string of the key bytes."""defdearmor(self, armored_key: str) ->bytes:
"""Returns the key bytes from an armored key ASCII string."""
classKryptonFile:
def__init__(
self,
secret_key: bytes,
context: bytes=b'',
callback: Optional[Callable] =None,
chunk_size: ChunkSize.Atd=None
) ->None:
""" Creates a new KryptonFile instance for encrypting and/or decrypting multiple files of arbitrary sizes with the same secret key using the same configuration. """defencrypt(
self,
data_file: str|Path,
output_file: str|Path,
header: bytes=b''
) ->None:
""" Reads plaintext from the `data_file` in chunks and encrypts them into ciphertext, writing the encrypted ciphertext chunks into the output_file. The header data is also written into the `output_file`. """defdecrypt_to_file(
self,
encrypted_file: str|Path,
output_file: str|Path
) ->bytes:
""" Reads ciphertext from the `encrypted_file` in chunks and decrypts them into plaintext, writing the decrypted plaintext chunks into the output_file. """defdecrypt_to_memory(self, encrypted_file: str|Path) ->DecryptedFile:
""" Reads ciphertext from the `encrypted_file` in chunks and decrypts them into plaintext, storing the entire decrypted plaintext into memory. """@classmethoddefread_file_header(cls, encrypted_file: str|Path) ->bytes:
"""Reads the header bytes from a Krypton ciphertext file."""
classKryptonKEM:
def__init__(
self,
kem_class: Type[BaseKEM],
kdf_params: KDFParams=None,
context: bytes=b"quantcrypt",
callback: Optional[Callable] =None,
chunk_size: ChunkSize.Atd=None
) ->None:
""" Creates a new KryptonKEM instance for encrypting and/or decrypting multiple files of arbitrary sizes with KEM public and private keys using the same configuration. Internally uses **KryptonFile** class. """defencrypt(
self,
public_key: str|bytes,
data_file: str|Path,
output_file: str|Path
) ->None:
""" Encapsulates the provided public_key into a shared secret, which is transformed with Argon2.Key into a 64 byte key for the KryptonFile class. Then, encrypts the plaintext data from the data_file and writes it into the output_file along with any necessary metadata to decrypt the file with the secret_key of the KEM keypair. """defdecrypt_to_file(
self,
secret_key: str|bytes,
encrypted_file: str|Path,
output_file: str|Path=None
) ->bytes:
""" Decapsulates the shared secret from the file metadata using the provided KEM secret key, which is then transformed with Argon2.Key into a 64 byte key for the KryptonFile class. Then, decrypts the ciphertext data from the encrypted file and writes the plaintext into the output_file, recreating the original plaintext file. """defdecrypt_to_memory(
self,
secret_key: str|bytes,
encrypted_file: str|Path
) ->DecryptedFile:
""" Decapsulates the shared secret from the file metadata using the provided KEM secret key, which is then transformed with Argon2.Key into a 64 byte key for the KryptonFile class. Then, decrypts the ciphertext data from the encrypted file and writes the plaintext into memory. **Note:** Do NOT decrypt huge files (>100MB) into memory, use your best judgement. """
classKKDF:
def__new__(
cls,
master: bytes,
key_len: int=32,
num_keys: int=1,
salt: bytes=None,
context: bytes=None
) ->tuple[bytes, ...]:
""" Computes the KMAC-KDF derived secret keys immediately on class instantiation using the provided parameters. Returns the generated keys as a tuple of bytes. """
classAlgoSpecsList(list):
defpqclean_names(self: list[AlgoSpec]) ->list[str]:
"""Returns the pqclean_names of all contained AlgoSpecs."""defarmor_names(self: list[AlgoSpec], pqa_type: PQAType|None=None) ->list[str]:
"""Returns the armor_names of all contained AlgoSpecs. Permits filtering by PQAType."""deffilter(self, armor_names: list[str], invert: bool=False) ->list[AlgoSpec]:
"""Returns specs which exist in armor_names. Can invert filtering behavior."""SupportedAlgos: AlgoSpecList[AlgoSpec]
classCompiler:
@classmethoddefrun(cls,
target_variants: list[const.PQAVariant] =None,
target_algos: list[const.AlgoSpec] =None,
*,
in_subprocess: bool=False,
verbose: bool=False,
debug: bool=False,
) ->subprocess.Popen|list[Target]:
""" Compiles the target variants of the target algorithms and stores the compiled binaries within the internals of the QuantCrypt library. """