mldsa.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. from __future__ import annotations
  5. import abc
  6. from cryptography.exceptions import UnsupportedAlgorithm, _Reasons
  7. from cryptography.hazmat.bindings._rust import openssl as rust_openssl
  8. from cryptography.hazmat.primitives import _serialization
  9. from cryptography.utils import Buffer
  10. class MLDSA44PublicKey(metaclass=abc.ABCMeta):
  11. @classmethod
  12. def from_public_bytes(cls, data: bytes) -> MLDSA44PublicKey:
  13. from cryptography.hazmat.backends.openssl.backend import backend
  14. if not backend.mldsa_supported():
  15. raise UnsupportedAlgorithm(
  16. "ML-DSA-44 is not supported by this backend.",
  17. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  18. )
  19. return rust_openssl.mldsa.from_mldsa44_public_bytes(data)
  20. @abc.abstractmethod
  21. def public_bytes(
  22. self,
  23. encoding: _serialization.Encoding,
  24. format: _serialization.PublicFormat,
  25. ) -> bytes:
  26. """
  27. The serialized bytes of the public key.
  28. """
  29. @abc.abstractmethod
  30. def public_bytes_raw(self) -> bytes:
  31. """
  32. The raw bytes of the public key.
  33. Equivalent to public_bytes(Raw, Raw).
  34. The public key is 1,312 bytes for MLDSA-44.
  35. """
  36. @abc.abstractmethod
  37. def verify(
  38. self,
  39. signature: Buffer,
  40. data: Buffer,
  41. context: Buffer | None = None,
  42. ) -> None:
  43. """
  44. Verify the signature.
  45. """
  46. @abc.abstractmethod
  47. def verify_mu(
  48. self,
  49. signature: Buffer,
  50. mu: Buffer,
  51. ) -> None:
  52. """
  53. Verify the signature over a precomputed mu (message representative).
  54. mu must be 64 bytes.
  55. """
  56. @abc.abstractmethod
  57. def __eq__(self, other: object) -> bool:
  58. """
  59. Checks equality.
  60. """
  61. @abc.abstractmethod
  62. def __copy__(self) -> MLDSA44PublicKey:
  63. """
  64. Returns a copy.
  65. """
  66. @abc.abstractmethod
  67. def __deepcopy__(self, memo: dict) -> MLDSA44PublicKey:
  68. """
  69. Returns a deep copy.
  70. """
  71. if hasattr(rust_openssl, "mldsa"):
  72. MLDSA44PublicKey.register(rust_openssl.mldsa.MLDSA44PublicKey)
  73. class MLDSA44PrivateKey(metaclass=abc.ABCMeta):
  74. @classmethod
  75. def generate(cls) -> MLDSA44PrivateKey:
  76. from cryptography.hazmat.backends.openssl.backend import backend
  77. if not backend.mldsa_supported():
  78. raise UnsupportedAlgorithm(
  79. "ML-DSA-44 is not supported by this backend.",
  80. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  81. )
  82. return rust_openssl.mldsa.generate_mldsa44_key()
  83. @classmethod
  84. def from_seed_bytes(cls, data: Buffer) -> MLDSA44PrivateKey:
  85. from cryptography.hazmat.backends.openssl.backend import backend
  86. if not backend.mldsa_supported():
  87. raise UnsupportedAlgorithm(
  88. "ML-DSA-44 is not supported by this backend.",
  89. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  90. )
  91. return rust_openssl.mldsa.from_mldsa44_seed_bytes(data)
  92. @abc.abstractmethod
  93. def public_key(self) -> MLDSA44PublicKey:
  94. """
  95. The MLDSA44PublicKey derived from the private key.
  96. """
  97. @abc.abstractmethod
  98. def private_bytes(
  99. self,
  100. encoding: _serialization.Encoding,
  101. format: _serialization.PrivateFormat,
  102. encryption_algorithm: _serialization.KeySerializationEncryption,
  103. ) -> bytes:
  104. """
  105. The serialized bytes of the private key.
  106. This method only returns the serialization of the seed form of the
  107. private key, never the expanded one.
  108. """
  109. @abc.abstractmethod
  110. def private_bytes_raw(self) -> bytes:
  111. """
  112. The raw bytes of the private key.
  113. Equivalent to private_bytes(Raw, Raw, NoEncryption()).
  114. This method only returns the seed form of the private key (32 bytes).
  115. """
  116. @abc.abstractmethod
  117. def sign(self, data: Buffer, context: Buffer | None = None) -> bytes:
  118. """
  119. Signs the data.
  120. """
  121. @abc.abstractmethod
  122. def sign_mu(self, mu: Buffer) -> bytes:
  123. """
  124. Signs a precomputed mu (message representative).
  125. mu must be 64 bytes and already incorporates the context, so no
  126. context is accepted here.
  127. """
  128. @abc.abstractmethod
  129. def __copy__(self) -> MLDSA44PrivateKey:
  130. """
  131. Returns a copy.
  132. """
  133. @abc.abstractmethod
  134. def __deepcopy__(self, memo: dict) -> MLDSA44PrivateKey:
  135. """
  136. Returns a deep copy.
  137. """
  138. if hasattr(rust_openssl, "mldsa"):
  139. MLDSA44PrivateKey.register(rust_openssl.mldsa.MLDSA44PrivateKey)
  140. class MLDSA65PublicKey(metaclass=abc.ABCMeta):
  141. @classmethod
  142. def from_public_bytes(cls, data: bytes) -> MLDSA65PublicKey:
  143. from cryptography.hazmat.backends.openssl.backend import backend
  144. if not backend.mldsa_supported():
  145. raise UnsupportedAlgorithm(
  146. "ML-DSA-65 is not supported by this backend.",
  147. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  148. )
  149. return rust_openssl.mldsa.from_mldsa65_public_bytes(data)
  150. @abc.abstractmethod
  151. def public_bytes(
  152. self,
  153. encoding: _serialization.Encoding,
  154. format: _serialization.PublicFormat,
  155. ) -> bytes:
  156. """
  157. The serialized bytes of the public key.
  158. """
  159. @abc.abstractmethod
  160. def public_bytes_raw(self) -> bytes:
  161. """
  162. The raw bytes of the public key.
  163. Equivalent to public_bytes(Raw, Raw).
  164. The public key is 1,952 bytes for MLDSA-65.
  165. """
  166. @abc.abstractmethod
  167. def verify(
  168. self,
  169. signature: Buffer,
  170. data: Buffer,
  171. context: Buffer | None = None,
  172. ) -> None:
  173. """
  174. Verify the signature.
  175. """
  176. @abc.abstractmethod
  177. def verify_mu(
  178. self,
  179. signature: Buffer,
  180. mu: Buffer,
  181. ) -> None:
  182. """
  183. Verify the signature over a precomputed mu (message representative).
  184. mu must be 64 bytes.
  185. """
  186. @abc.abstractmethod
  187. def __eq__(self, other: object) -> bool:
  188. """
  189. Checks equality.
  190. """
  191. @abc.abstractmethod
  192. def __copy__(self) -> MLDSA65PublicKey:
  193. """
  194. Returns a copy.
  195. """
  196. @abc.abstractmethod
  197. def __deepcopy__(self, memo: dict) -> MLDSA65PublicKey:
  198. """
  199. Returns a deep copy.
  200. """
  201. if hasattr(rust_openssl, "mldsa"):
  202. MLDSA65PublicKey.register(rust_openssl.mldsa.MLDSA65PublicKey)
  203. class MLDSA65PrivateKey(metaclass=abc.ABCMeta):
  204. @classmethod
  205. def generate(cls) -> MLDSA65PrivateKey:
  206. from cryptography.hazmat.backends.openssl.backend import backend
  207. if not backend.mldsa_supported():
  208. raise UnsupportedAlgorithm(
  209. "ML-DSA-65 is not supported by this backend.",
  210. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  211. )
  212. return rust_openssl.mldsa.generate_mldsa65_key()
  213. @classmethod
  214. def from_seed_bytes(cls, data: Buffer) -> MLDSA65PrivateKey:
  215. from cryptography.hazmat.backends.openssl.backend import backend
  216. if not backend.mldsa_supported():
  217. raise UnsupportedAlgorithm(
  218. "ML-DSA-65 is not supported by this backend.",
  219. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  220. )
  221. return rust_openssl.mldsa.from_mldsa65_seed_bytes(data)
  222. @abc.abstractmethod
  223. def public_key(self) -> MLDSA65PublicKey:
  224. """
  225. The MLDSA65PublicKey derived from the private key.
  226. """
  227. @abc.abstractmethod
  228. def private_bytes(
  229. self,
  230. encoding: _serialization.Encoding,
  231. format: _serialization.PrivateFormat,
  232. encryption_algorithm: _serialization.KeySerializationEncryption,
  233. ) -> bytes:
  234. """
  235. The serialized bytes of the private key.
  236. This method only returns the serialization of the seed form of the
  237. private key, never the expanded one.
  238. """
  239. @abc.abstractmethod
  240. def private_bytes_raw(self) -> bytes:
  241. """
  242. The raw bytes of the private key.
  243. Equivalent to private_bytes(Raw, Raw, NoEncryption()).
  244. This method only returns the seed form of the private key (32 bytes).
  245. """
  246. @abc.abstractmethod
  247. def sign(self, data: Buffer, context: Buffer | None = None) -> bytes:
  248. """
  249. Signs the data.
  250. """
  251. @abc.abstractmethod
  252. def sign_mu(self, mu: Buffer) -> bytes:
  253. """
  254. Signs a precomputed mu (message representative).
  255. mu must be 64 bytes and already incorporates the context, so no
  256. context is accepted here.
  257. """
  258. @abc.abstractmethod
  259. def __copy__(self) -> MLDSA65PrivateKey:
  260. """
  261. Returns a copy.
  262. """
  263. @abc.abstractmethod
  264. def __deepcopy__(self, memo: dict) -> MLDSA65PrivateKey:
  265. """
  266. Returns a deep copy.
  267. """
  268. if hasattr(rust_openssl, "mldsa"):
  269. MLDSA65PrivateKey.register(rust_openssl.mldsa.MLDSA65PrivateKey)
  270. class MLDSA87PublicKey(metaclass=abc.ABCMeta):
  271. @classmethod
  272. def from_public_bytes(cls, data: bytes) -> MLDSA87PublicKey:
  273. from cryptography.hazmat.backends.openssl.backend import backend
  274. if not backend.mldsa_supported():
  275. raise UnsupportedAlgorithm(
  276. "ML-DSA-87 is not supported by this backend.",
  277. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  278. )
  279. return rust_openssl.mldsa.from_mldsa87_public_bytes(data)
  280. @abc.abstractmethod
  281. def public_bytes(
  282. self,
  283. encoding: _serialization.Encoding,
  284. format: _serialization.PublicFormat,
  285. ) -> bytes:
  286. """
  287. The serialized bytes of the public key.
  288. """
  289. @abc.abstractmethod
  290. def public_bytes_raw(self) -> bytes:
  291. """
  292. The raw bytes of the public key.
  293. Equivalent to public_bytes(Raw, Raw).
  294. The public key is 2,592 bytes for MLDSA-87.
  295. """
  296. @abc.abstractmethod
  297. def verify(
  298. self,
  299. signature: Buffer,
  300. data: Buffer,
  301. context: Buffer | None = None,
  302. ) -> None:
  303. """
  304. Verify the signature.
  305. """
  306. @abc.abstractmethod
  307. def verify_mu(
  308. self,
  309. signature: Buffer,
  310. mu: Buffer,
  311. ) -> None:
  312. """
  313. Verify the signature over a precomputed mu (message representative).
  314. mu must be 64 bytes.
  315. """
  316. @abc.abstractmethod
  317. def __eq__(self, other: object) -> bool:
  318. """
  319. Checks equality.
  320. """
  321. @abc.abstractmethod
  322. def __copy__(self) -> MLDSA87PublicKey:
  323. """
  324. Returns a copy.
  325. """
  326. @abc.abstractmethod
  327. def __deepcopy__(self, memo: dict) -> MLDSA87PublicKey:
  328. """
  329. Returns a deep copy.
  330. """
  331. if hasattr(rust_openssl, "mldsa"):
  332. MLDSA87PublicKey.register(rust_openssl.mldsa.MLDSA87PublicKey)
  333. class MLDSA87PrivateKey(metaclass=abc.ABCMeta):
  334. @classmethod
  335. def generate(cls) -> MLDSA87PrivateKey:
  336. from cryptography.hazmat.backends.openssl.backend import backend
  337. if not backend.mldsa_supported():
  338. raise UnsupportedAlgorithm(
  339. "ML-DSA-87 is not supported by this backend.",
  340. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  341. )
  342. return rust_openssl.mldsa.generate_mldsa87_key()
  343. @classmethod
  344. def from_seed_bytes(cls, data: Buffer) -> MLDSA87PrivateKey:
  345. from cryptography.hazmat.backends.openssl.backend import backend
  346. if not backend.mldsa_supported():
  347. raise UnsupportedAlgorithm(
  348. "ML-DSA-87 is not supported by this backend.",
  349. _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM,
  350. )
  351. return rust_openssl.mldsa.from_mldsa87_seed_bytes(data)
  352. @abc.abstractmethod
  353. def public_key(self) -> MLDSA87PublicKey:
  354. """
  355. The MLDSA87PublicKey derived from the private key.
  356. """
  357. @abc.abstractmethod
  358. def private_bytes(
  359. self,
  360. encoding: _serialization.Encoding,
  361. format: _serialization.PrivateFormat,
  362. encryption_algorithm: _serialization.KeySerializationEncryption,
  363. ) -> bytes:
  364. """
  365. The serialized bytes of the private key.
  366. This method only returns the serialization of the seed form of the
  367. private key, never the expanded one.
  368. """
  369. @abc.abstractmethod
  370. def private_bytes_raw(self) -> bytes:
  371. """
  372. The raw bytes of the private key.
  373. Equivalent to private_bytes(Raw, Raw, NoEncryption()).
  374. This method only returns the seed form of the private key (32 bytes).
  375. """
  376. @abc.abstractmethod
  377. def sign(self, data: Buffer, context: Buffer | None = None) -> bytes:
  378. """
  379. Signs the data.
  380. """
  381. @abc.abstractmethod
  382. def sign_mu(self, mu: Buffer) -> bytes:
  383. """
  384. Signs a precomputed mu (message representative).
  385. mu must be 64 bytes and already incorporates the context, so no
  386. context is accepted here.
  387. """
  388. @abc.abstractmethod
  389. def __copy__(self) -> MLDSA87PrivateKey:
  390. """
  391. Returns a copy.
  392. """
  393. @abc.abstractmethod
  394. def __deepcopy__(self, memo: dict) -> MLDSA87PrivateKey:
  395. """
  396. Returns a deep copy.
  397. """
  398. if hasattr(rust_openssl, "mldsa"):
  399. MLDSA87PrivateKey.register(rust_openssl.mldsa.MLDSA87PrivateKey)
  400. MLDSAMuHasher = rust_openssl.mldsa.MLDSAMuHasher