Skip to main content

crypto_core/
algorithm.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5/// Asymmetric algorithm identifiers used for signatures and key agreement.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum Algorithm {
8    /// Ed25519 EdDSA signature algorithm.
9    Ed25519,
10    /// X25519 Diffie-Hellman key agreement.
11    X25519,
12    /// NIST P-256 (secp256r1) ECDSA signature and ECDH key agreement.
13    P256,
14    /// NIST P-384 (secp384r1) ECDSA signature algorithm.
15    P384,
16    /// NIST P-521 (secp521r1) ECDSA signature algorithm.
17    P521,
18    /// secp256k1 ECDSA signature algorithm.
19    Secp256k1,
20    /// ML-DSA-44 post-quantum signature algorithm.
21    MlDsa44,
22    /// ML-DSA-65 post-quantum signature algorithm.
23    MlDsa65,
24    /// ML-DSA-87 post-quantum signature algorithm.
25    MlDsa87,
26    /// ML-KEM-512 post-quantum key encapsulation.
27    MlKem512,
28    /// ML-KEM-768 post-quantum key encapsulation.
29    MlKem768,
30    /// ML-KEM-1024 post-quantum key encapsulation.
31    MlKem1024,
32    /// X-Wing hybrid KEM over X25519 and ML-KEM-768.
33    XWing768,
34    /// X-Wing hybrid KEM over X25519 and ML-KEM-1024.
35    XWing1024,
36}
37
38impl Algorithm {
39    /// Canonical protocol identifier string.
40    /// MUST match multicodec `alg` strings exactly.
41    pub fn as_str(self) -> &'static str {
42        match self {
43            Algorithm::Ed25519 => "Ed25519",
44            Algorithm::X25519 => "X25519",
45            Algorithm::P256 => "P-256",
46            Algorithm::P384 => "P-384",
47            Algorithm::P521 => "P-521",
48            Algorithm::Secp256k1 => "secp256k1",
49            Algorithm::MlDsa44 => "ML-DSA-44",
50            Algorithm::MlDsa65 => "ML-DSA-65",
51            Algorithm::MlDsa87 => "ML-DSA-87",
52            Algorithm::MlKem512 => "ML-KEM-512",
53            Algorithm::MlKem768 => "ML-KEM-768",
54            Algorithm::MlKem1024 => "ML-KEM-1024",
55            Algorithm::XWing768 => "X-Wing-768",
56            Algorithm::XWing1024 => "X-Wing-1024",
57        }
58    }
59
60    /// True if this algorithm produces digital signatures
61    pub fn is_signature(self) -> bool {
62        matches!(
63            self,
64            Algorithm::Ed25519
65                | Algorithm::P256
66                | Algorithm::P384
67                | Algorithm::P521
68                | Algorithm::Secp256k1
69                | Algorithm::MlDsa44
70                | Algorithm::MlDsa65
71                | Algorithm::MlDsa87
72        )
73    }
74
75    /// True if this algorithm is for key agreement / encapsulation
76    pub fn is_key_agreement(self) -> bool {
77        matches!(
78            self,
79            Algorithm::X25519
80                | Algorithm::P256
81                | Algorithm::MlKem512
82                | Algorithm::MlKem768
83                | Algorithm::MlKem1024
84                | Algorithm::XWing768
85                | Algorithm::XWing1024
86        )
87    }
88}
89
90impl core::fmt::Display for Algorithm {
91    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
92        write!(f, "{}", self.as_str())
93    }
94}
95
96/// Authenticated encryption (AEAD) algorithm identifiers.
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
98pub enum AeadAlgorithm {
99    /// AES-256 in Galois/Counter Mode.
100    Aes256Gcm,
101    /// AES-256 in GCM-SIV (nonce-misuse-resistant) mode.
102    Aes256GcmSiv,
103    /// ChaCha20-Poly1305 with a 96-bit RFC 8439 nonce.
104    ChaCha20Poly1305,
105    /// XChaCha20-Poly1305 with a 192-bit extended nonce.
106    XChaCha20Poly1305,
107}
108
109impl AeadAlgorithm {
110    /// Canonical protocol identifier string.
111    pub fn as_str(self) -> &'static str {
112        match self {
113            AeadAlgorithm::Aes256Gcm => "AES-256-GCM",
114            AeadAlgorithm::Aes256GcmSiv => "AES-256-GCM-SIV",
115            AeadAlgorithm::ChaCha20Poly1305 => "ChaCha20-Poly1305",
116            AeadAlgorithm::XChaCha20Poly1305 => "XChaCha20-Poly1305",
117        }
118    }
119}
120
121impl core::fmt::Display for AeadAlgorithm {
122    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
123        write!(f, "{}", self.as_str())
124    }
125}
126
127/// Hash algorithm identifiers.
128#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
129pub enum HashAlgorithm {
130    /// SHA-2 with a 256-bit digest.
131    Sha2_256,
132    /// SHA-2 with a 384-bit digest.
133    Sha2_384,
134    /// SHA-2 with a 512-bit digest.
135    Sha2_512,
136    /// SHA-3 with a 224-bit digest.
137    Sha3_224,
138    /// SHA-3 with a 256-bit digest.
139    Sha3_256,
140    /// SHA-3 with a 384-bit digest.
141    Sha3_384,
142    /// SHA-3 with a 512-bit digest.
143    Sha3_512,
144}
145
146impl HashAlgorithm {
147    /// Canonical protocol identifier string.
148    pub fn as_str(self) -> &'static str {
149        match self {
150            HashAlgorithm::Sha2_256 => "SHA2-256",
151            HashAlgorithm::Sha2_384 => "SHA2-384",
152            HashAlgorithm::Sha2_512 => "SHA2-512",
153            HashAlgorithm::Sha3_224 => "SHA3-224",
154            HashAlgorithm::Sha3_256 => "SHA3-256",
155            HashAlgorithm::Sha3_384 => "SHA3-384",
156            HashAlgorithm::Sha3_512 => "SHA3-512",
157        }
158    }
159}
160
161impl core::fmt::Display for HashAlgorithm {
162    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
163        write!(f, "{}", self.as_str())
164    }
165}
166
167/// Message authentication code algorithm identifiers.
168#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
169pub enum MacAlgorithm {
170    /// HMAC using SHA-256.
171    HmacSha256,
172    /// HMAC using SHA-512.
173    HmacSha512,
174}
175
176impl MacAlgorithm {
177    /// Canonical protocol identifier string.
178    pub fn as_str(self) -> &'static str {
179        match self {
180            MacAlgorithm::HmacSha256 => "HMAC-SHA-256",
181            MacAlgorithm::HmacSha512 => "HMAC-SHA-512",
182        }
183    }
184}
185
186impl core::fmt::Display for MacAlgorithm {
187    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
188        write!(f, "{}", self.as_str())
189    }
190}