|
| 1 | +# This file is dual licensed under the terms of the Apache License, Version |
| 2 | +# 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | +# for complete details. |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import abc |
| 8 | + |
| 9 | +from cryptography.exceptions import UnsupportedAlgorithm, _Reasons |
| 10 | +from cryptography.hazmat.bindings._rust import openssl as rust_openssl |
| 11 | +from cryptography.hazmat.primitives import _serialization |
| 12 | +from cryptography.utils import Buffer |
| 13 | + |
| 14 | + |
| 15 | +class MlDsa65PublicKey(metaclass=abc.ABCMeta): |
| 16 | + @classmethod |
| 17 | + def from_public_bytes(cls, data: bytes) -> MlDsa65PublicKey: |
| 18 | + from cryptography.hazmat.backends.openssl.backend import backend |
| 19 | + |
| 20 | + if not backend.mldsa_supported(): |
| 21 | + raise UnsupportedAlgorithm( |
| 22 | + "ML-DSA-65 is not supported by this backend.", |
| 23 | + _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, |
| 24 | + ) |
| 25 | + |
| 26 | + return rust_openssl.mldsa65.from_public_bytes(data) |
| 27 | + |
| 28 | + @abc.abstractmethod |
| 29 | + def public_bytes( |
| 30 | + self, |
| 31 | + encoding: _serialization.Encoding, |
| 32 | + format: _serialization.PublicFormat, |
| 33 | + ) -> bytes: |
| 34 | + """ |
| 35 | + The serialized bytes of the public key. |
| 36 | + """ |
| 37 | + |
| 38 | + @abc.abstractmethod |
| 39 | + def public_bytes_raw(self) -> bytes: |
| 40 | + """ |
| 41 | + The raw bytes of the public key. |
| 42 | + Equivalent to public_bytes(Raw, Raw). |
| 43 | + """ |
| 44 | + |
| 45 | + @abc.abstractmethod |
| 46 | + def verify(self, signature: Buffer, data: Buffer) -> None: |
| 47 | + """ |
| 48 | + Verify the signature. |
| 49 | + """ |
| 50 | + |
| 51 | + @abc.abstractmethod |
| 52 | + def verify_with_context( |
| 53 | + self, signature: Buffer, data: Buffer, context: Buffer |
| 54 | + ) -> None: |
| 55 | + """ |
| 56 | + Verify the signature with a context string. |
| 57 | + """ |
| 58 | + |
| 59 | + @abc.abstractmethod |
| 60 | + def __eq__(self, other: object) -> bool: |
| 61 | + """ |
| 62 | + Checks equality. |
| 63 | + """ |
| 64 | + |
| 65 | + @abc.abstractmethod |
| 66 | + def __copy__(self) -> MlDsa65PublicKey: |
| 67 | + """ |
| 68 | + Returns a copy. |
| 69 | + """ |
| 70 | + |
| 71 | + @abc.abstractmethod |
| 72 | + def __deepcopy__(self, memo: dict) -> MlDsa65PublicKey: |
| 73 | + """ |
| 74 | + Returns a deep copy. |
| 75 | + """ |
| 76 | + |
| 77 | + |
| 78 | +if hasattr(rust_openssl, "mldsa65"): |
| 79 | + MlDsa65PublicKey.register(rust_openssl.mldsa65.MlDsa65PublicKey) |
| 80 | + |
| 81 | + |
| 82 | +class MlDsa65PrivateKey(metaclass=abc.ABCMeta): |
| 83 | + @classmethod |
| 84 | + def generate(cls) -> MlDsa65PrivateKey: |
| 85 | + from cryptography.hazmat.backends.openssl.backend import backend |
| 86 | + |
| 87 | + if not backend.mldsa_supported(): |
| 88 | + raise UnsupportedAlgorithm( |
| 89 | + "ML-DSA-65 is not supported by this backend.", |
| 90 | + _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, |
| 91 | + ) |
| 92 | + |
| 93 | + return rust_openssl.mldsa65.generate_key() |
| 94 | + |
| 95 | + @classmethod |
| 96 | + def from_seed_bytes(cls, data: Buffer) -> MlDsa65PrivateKey: |
| 97 | + from cryptography.hazmat.backends.openssl.backend import backend |
| 98 | + |
| 99 | + if not backend.mldsa_supported(): |
| 100 | + raise UnsupportedAlgorithm( |
| 101 | + "ML-DSA-65 is not supported by this backend.", |
| 102 | + _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM, |
| 103 | + ) |
| 104 | + |
| 105 | + return rust_openssl.mldsa65.from_seed_bytes(data) |
| 106 | + |
| 107 | + @abc.abstractmethod |
| 108 | + def public_key(self) -> MlDsa65PublicKey: |
| 109 | + """ |
| 110 | + The MlDsa65PublicKey derived from the private key. |
| 111 | + """ |
| 112 | + |
| 113 | + @abc.abstractmethod |
| 114 | + def private_bytes( |
| 115 | + self, |
| 116 | + encoding: _serialization.Encoding, |
| 117 | + format: _serialization.PrivateFormat, |
| 118 | + encryption_algorithm: (_serialization.KeySerializationEncryption), |
| 119 | + ) -> bytes: |
| 120 | + """ |
| 121 | + The serialized bytes of the private key. |
| 122 | + """ |
| 123 | + |
| 124 | + @abc.abstractmethod |
| 125 | + def private_bytes_raw(self) -> bytes: |
| 126 | + """ |
| 127 | + The raw bytes of the private key (32-byte seed). |
| 128 | + Equivalent to private_bytes(Raw, Raw, NoEncryption()). |
| 129 | + """ |
| 130 | + |
| 131 | + @abc.abstractmethod |
| 132 | + def sign(self, data: Buffer) -> bytes: |
| 133 | + """ |
| 134 | + Signs the data. |
| 135 | + """ |
| 136 | + |
| 137 | + @abc.abstractmethod |
| 138 | + def sign_with_context(self, data: Buffer, context: Buffer) -> bytes: |
| 139 | + """ |
| 140 | + Signs the data with a context string. |
| 141 | + """ |
| 142 | + |
| 143 | + @abc.abstractmethod |
| 144 | + def __copy__(self) -> MlDsa65PrivateKey: |
| 145 | + """ |
| 146 | + Returns a copy. |
| 147 | + """ |
| 148 | + |
| 149 | + @abc.abstractmethod |
| 150 | + def __deepcopy__(self, memo: dict) -> MlDsa65PrivateKey: |
| 151 | + """ |
| 152 | + Returns a deep copy. |
| 153 | + """ |
| 154 | + |
| 155 | + |
| 156 | +if hasattr(rust_openssl, "mldsa65"): |
| 157 | + MlDsa65PrivateKey.register(rust_openssl.mldsa65.MlDsa65PrivateKey) |
0 commit comments