| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- # This file is dual licensed under the terms of the Apache License, Version
- # 2.0, and the BSD License. See the LICENSE file in the root of this repository
- # for complete details.
- from __future__ import annotations
- import abc
- from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
- from cryptography.hazmat.bindings._rust import openssl as rust_openssl
- from cryptography.hazmat.primitives import _serialization
- from cryptography.utils import Buffer
- class MLDSA44PublicKey(metaclass=abc.ABCMeta):
- @classmethod
- def from_public_bytes(cls, data: bytes) -> MLDSA44PublicKey:
- from cryptography.hazmat.backends.openssl.backend import backend
- if not backend.mldsa_supported():
- raise UnsupportedAlgorithm(
- "ML-DSA-44 is not supported by this backend.",
- _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
- )
- return rust_openssl.mldsa.from_mldsa44_public_bytes(data)
- @abc.abstractmethod
- def public_bytes(
- self,
- encoding: _serialization.Encoding,
- format: _serialization.PublicFormat,
- ) -> bytes:
- """
- The serialized bytes of the public key.
- """
- @abc.abstractmethod
- def public_bytes_raw(self) -> bytes:
- """
- The raw bytes of the public key.
- Equivalent to public_bytes(Raw, Raw).
- The public key is 1,312 bytes for MLDSA-44.
- """
- @abc.abstractmethod
- def verify(
- self,
- signature: Buffer,
- data: Buffer,
- context: Buffer | None = None,
- ) -> None:
- """
- Verify the signature.
- """
- @abc.abstractmethod
- def __eq__(self, other: object) -> bool:
- """
- Checks equality.
- """
- @abc.abstractmethod
- def __copy__(self) -> MLDSA44PublicKey:
- """
- Returns a copy.
- """
- @abc.abstractmethod
- def __deepcopy__(self, memo: dict) -> MLDSA44PublicKey:
- """
- Returns a deep copy.
- """
- if hasattr(rust_openssl, "mldsa"):
- MLDSA44PublicKey.register(rust_openssl.mldsa.MLDSA44PublicKey)
- class MLDSA44PrivateKey(metaclass=abc.ABCMeta):
- @classmethod
- def generate(cls) -> MLDSA44PrivateKey:
- from cryptography.hazmat.backends.openssl.backend import backend
- if not backend.mldsa_supported():
- raise UnsupportedAlgorithm(
- "ML-DSA-44 is not supported by this backend.",
- _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
- )
- return rust_openssl.mldsa.generate_mldsa44_key()
- @classmethod
- def from_seed_bytes(cls, data: Buffer) -> MLDSA44PrivateKey:
- from cryptography.hazmat.backends.openssl.backend import backend
- if not backend.mldsa_supported():
- raise UnsupportedAlgorithm(
- "ML-DSA-44 is not supported by this backend.",
- _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
- )
- return rust_openssl.mldsa.from_mldsa44_seed_bytes(data)
- @abc.abstractmethod
- def public_key(self) -> MLDSA44PublicKey:
- """
- The MLDSA44PublicKey derived from the private key.
- """
- @abc.abstractmethod
- def private_bytes(
- self,
- encoding: _serialization.Encoding,
- format: _serialization.PrivateFormat,
- encryption_algorithm: _serialization.KeySerializationEncryption,
- ) -> bytes:
- """
- The serialized bytes of the private key.
- This method only returns the serialization of the seed form of the
- private key, never the expanded one.
- """
- @abc.abstractmethod
- def private_bytes_raw(self) -> bytes:
- """
- The raw bytes of the private key.
- Equivalent to private_bytes(Raw, Raw, NoEncryption()).
- This method only returns the seed form of the private key (32 bytes).
- """
- @abc.abstractmethod
- def sign(self, data: Buffer, context: Buffer | None = None) -> bytes:
- """
- Signs the data.
- """
- @abc.abstractmethod
- def __copy__(self) -> MLDSA44PrivateKey:
- """
- Returns a copy.
- """
- @abc.abstractmethod
- def __deepcopy__(self, memo: dict) -> MLDSA44PrivateKey:
- """
- Returns a deep copy.
- """
- if hasattr(rust_openssl, "mldsa"):
- MLDSA44PrivateKey.register(rust_openssl.mldsa.MLDSA44PrivateKey)
- class MLDSA65PublicKey(metaclass=abc.ABCMeta):
- @classmethod
- def from_public_bytes(cls, data: bytes) -> MLDSA65PublicKey:
- from cryptography.hazmat.backends.openssl.backend import backend
- if not backend.mldsa_supported():
- raise UnsupportedAlgorithm(
- "ML-DSA-65 is not supported by this backend.",
- _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
- )
- return rust_openssl.mldsa.from_mldsa65_public_bytes(data)
- @abc.abstractmethod
- def public_bytes(
- self,
- encoding: _serialization.Encoding,
- format: _serialization.PublicFormat,
- ) -> bytes:
- """
- The serialized bytes of the public key.
- """
- @abc.abstractmethod
- def public_bytes_raw(self) -> bytes:
- """
- The raw bytes of the public key.
- Equivalent to public_bytes(Raw, Raw).
- The public key is 1,952 bytes for MLDSA-65.
- """
- @abc.abstractmethod
- def verify(
- self,
- signature: Buffer,
- data: Buffer,
- context: Buffer | None = None,
- ) -> None:
- """
- Verify the signature.
- """
- @abc.abstractmethod
- def __eq__(self, other: object) -> bool:
- """
- Checks equality.
- """
- @abc.abstractmethod
- def __copy__(self) -> MLDSA65PublicKey:
- """
- Returns a copy.
- """
- @abc.abstractmethod
- def __deepcopy__(self, memo: dict) -> MLDSA65PublicKey:
- """
- Returns a deep copy.
- """
- if hasattr(rust_openssl, "mldsa"):
- MLDSA65PublicKey.register(rust_openssl.mldsa.MLDSA65PublicKey)
- class MLDSA65PrivateKey(metaclass=abc.ABCMeta):
- @classmethod
- def generate(cls) -> MLDSA65PrivateKey:
- from cryptography.hazmat.backends.openssl.backend import backend
- if not backend.mldsa_supported():
- raise UnsupportedAlgorithm(
- "ML-DSA-65 is not supported by this backend.",
- _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
- )
- return rust_openssl.mldsa.generate_mldsa65_key()
- @classmethod
- def from_seed_bytes(cls, data: Buffer) -> MLDSA65PrivateKey:
- from cryptography.hazmat.backends.openssl.backend import backend
- if not backend.mldsa_supported():
- raise UnsupportedAlgorithm(
- "ML-DSA-65 is not supported by this backend.",
- _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
- )
- return rust_openssl.mldsa.from_mldsa65_seed_bytes(data)
- @abc.abstractmethod
- def public_key(self) -> MLDSA65PublicKey:
- """
- The MLDSA65PublicKey derived from the private key.
- """
- @abc.abstractmethod
- def private_bytes(
- self,
- encoding: _serialization.Encoding,
- format: _serialization.PrivateFormat,
- encryption_algorithm: _serialization.KeySerializationEncryption,
- ) -> bytes:
- """
- The serialized bytes of the private key.
- This method only returns the serialization of the seed form of the
- private key, never the expanded one.
- """
- @abc.abstractmethod
- def private_bytes_raw(self) -> bytes:
- """
- The raw bytes of the private key.
- Equivalent to private_bytes(Raw, Raw, NoEncryption()).
- This method only returns the seed form of the private key (32 bytes).
- """
- @abc.abstractmethod
- def sign(self, data: Buffer, context: Buffer | None = None) -> bytes:
- """
- Signs the data.
- """
- @abc.abstractmethod
- def __copy__(self) -> MLDSA65PrivateKey:
- """
- Returns a copy.
- """
- @abc.abstractmethod
- def __deepcopy__(self, memo: dict) -> MLDSA65PrivateKey:
- """
- Returns a deep copy.
- """
- if hasattr(rust_openssl, "mldsa"):
- MLDSA65PrivateKey.register(rust_openssl.mldsa.MLDSA65PrivateKey)
- class MLDSA87PublicKey(metaclass=abc.ABCMeta):
- @classmethod
- def from_public_bytes(cls, data: bytes) -> MLDSA87PublicKey:
- from cryptography.hazmat.backends.openssl.backend import backend
- if not backend.mldsa_supported():
- raise UnsupportedAlgorithm(
- "ML-DSA-87 is not supported by this backend.",
- _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
- )
- return rust_openssl.mldsa.from_mldsa87_public_bytes(data)
- @abc.abstractmethod
- def public_bytes(
- self,
- encoding: _serialization.Encoding,
- format: _serialization.PublicFormat,
- ) -> bytes:
- """
- The serialized bytes of the public key.
- """
- @abc.abstractmethod
- def public_bytes_raw(self) -> bytes:
- """
- The raw bytes of the public key.
- Equivalent to public_bytes(Raw, Raw).
- The public key is 2,592 bytes for MLDSA-87.
- """
- @abc.abstractmethod
- def verify(
- self,
- signature: Buffer,
- data: Buffer,
- context: Buffer | None = None,
- ) -> None:
- """
- Verify the signature.
- """
- @abc.abstractmethod
- def __eq__(self, other: object) -> bool:
- """
- Checks equality.
- """
- @abc.abstractmethod
- def __copy__(self) -> MLDSA87PublicKey:
- """
- Returns a copy.
- """
- @abc.abstractmethod
- def __deepcopy__(self, memo: dict) -> MLDSA87PublicKey:
- """
- Returns a deep copy.
- """
- if hasattr(rust_openssl, "mldsa"):
- MLDSA87PublicKey.register(rust_openssl.mldsa.MLDSA87PublicKey)
- class MLDSA87PrivateKey(metaclass=abc.ABCMeta):
- @classmethod
- def generate(cls) -> MLDSA87PrivateKey:
- from cryptography.hazmat.backends.openssl.backend import backend
- if not backend.mldsa_supported():
- raise UnsupportedAlgorithm(
- "ML-DSA-87 is not supported by this backend.",
- _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
- )
- return rust_openssl.mldsa.generate_mldsa87_key()
- @classmethod
- def from_seed_bytes(cls, data: Buffer) -> MLDSA87PrivateKey:
- from cryptography.hazmat.backends.openssl.backend import backend
- if not backend.mldsa_supported():
- raise UnsupportedAlgorithm(
- "ML-DSA-87 is not supported by this backend.",
- _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
- )
- return rust_openssl.mldsa.from_mldsa87_seed_bytes(data)
- @abc.abstractmethod
- def public_key(self) -> MLDSA87PublicKey:
- """
- The MLDSA87PublicKey derived from the private key.
- """
- @abc.abstractmethod
- def private_bytes(
- self,
- encoding: _serialization.Encoding,
- format: _serialization.PrivateFormat,
- encryption_algorithm: _serialization.KeySerializationEncryption,
- ) -> bytes:
- """
- The serialized bytes of the private key.
- This method only returns the serialization of the seed form of the
- private key, never the expanded one.
- """
- @abc.abstractmethod
- def private_bytes_raw(self) -> bytes:
- """
- The raw bytes of the private key.
- Equivalent to private_bytes(Raw, Raw, NoEncryption()).
- This method only returns the seed form of the private key (32 bytes).
- """
- @abc.abstractmethod
- def sign(self, data: Buffer, context: Buffer | None = None) -> bytes:
- """
- Signs the data.
- """
- @abc.abstractmethod
- def __copy__(self) -> MLDSA87PrivateKey:
- """
- Returns a copy.
- """
- @abc.abstractmethod
- def __deepcopy__(self, memo: dict) -> MLDSA87PrivateKey:
- """
- Returns a deep copy.
- """
- if hasattr(rust_openssl, "mldsa"):
- MLDSA87PrivateKey.register(rust_openssl.mldsa.MLDSA87PrivateKey)
|