Skip to main content

rtci_crypto/
algorithm.rs

1/// Algorithms accepted by [`crate::RTCCrypto::hash`].
2#[non_exhaustive]
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4pub enum HashAlgorithm {
5    /// MD5, retained only for STUN long-term credential derivation.
6    Md5,
7    /// SHA-256.
8    Sha256,
9    /// SHA-1, only to verify a certificate fingerprint a peer gives with no stronger hash
10    /// (RFC 8122 section 5).
11    Sha1,
12    /// SHA-224 (certificate fingerprints, RFC 8122 section 5).
13    Sha224,
14    /// SHA-384 (certificate fingerprints, RFC 8122 section 5).
15    Sha384,
16    /// SHA-512 (certificate fingerprints, RFC 8122 section 5).
17    Sha512,
18}
19
20impl HashAlgorithm {
21    /// Returns the digest length in bytes.
22    #[must_use]
23    pub const fn output_len(self) -> usize {
24        match self {
25            Self::Md5 => 16,
26            Self::Sha256 => 32,
27            Self::Sha1 => 20,
28            Self::Sha224 => 28,
29            Self::Sha384 => 48,
30            Self::Sha512 => 64,
31        }
32    }
33}
34
35/// Algorithms accepted by the HMAC operations.
36#[non_exhaustive]
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub enum HmacAlgorithm {
39    /// HMAC-SHA1.
40    Sha1,
41    /// HMAC-SHA256.
42    Sha256,
43    /// HMAC-SHA512, for the SFrame cipher suites keyed with SHA-512 (RFC 9605 section 4.5).
44    Sha512,
45    /// HMAC-SHA384, for HKDF with the TLS 1.3 `_SHA384` cipher suites.
46    Sha384,
47}
48
49impl HmacAlgorithm {
50    /// Returns the native tag length in bytes.
51    #[must_use]
52    pub const fn output_len(self) -> usize {
53        match self {
54            Self::Sha1 => 20,
55            Self::Sha256 => 32,
56            Self::Sha512 => 64,
57            Self::Sha384 => 48,
58        }
59    }
60}
61
62/// Authenticated-encryption algorithms.
63#[non_exhaustive]
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
65pub enum AeadAlgorithm {
66    /// AES-128-GCM.
67    Aes128Gcm,
68    /// AES-256-GCM.
69    Aes256Gcm,
70    /// AES-128-CCM with a 16-byte tag.
71    Aes128Ccm,
72    /// AES-128-CCM with an 8-byte tag.
73    Aes128Ccm8,
74    /// ChaCha20-Poly1305.
75    ChaCha20Poly1305,
76}
77
78/// Stream-cipher algorithms.
79#[non_exhaustive]
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
81pub enum StreamCipherAlgorithm {
82    /// AES-128 in counter mode.
83    Aes128Ctr,
84    /// AES-256 in counter mode.
85    Aes256Ctr,
86    /// AES-192 in counter mode (RFC 6188 `AES_192_CM`).
87    Aes192Ctr,
88}
89
90/// Single-block encryption algorithms.
91#[non_exhaustive]
92#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
93pub enum BlockCipherAlgorithm {
94    /// AES-128.
95    Aes128,
96    /// AES-256.
97    Aes256,
98    /// AES-192 (RFC 6188 `AES_192_CM_PRF`).
99    Aes192,
100}
101
102/// The ciphers a record-number (header-protection) mask is made with: the block cipher or
103/// stream cipher underneath an AEAD, keyed separately (RFC 9147 section 4.2.3; the same
104/// construction as QUIC header protection, RFC 9001 section 5.4).
105#[non_exhaustive]
106#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
107pub enum HeaderMaskAlgorithm {
108    /// AES-128 in ECB mode over the 16-byte sample.
109    Aes128,
110    /// AES-256 in ECB mode over the 16-byte sample.
111    Aes256,
112    /// The ChaCha20 block function, the sample giving the block counter and the nonce.
113    ChaCha20,
114}
115
116impl HeaderMaskAlgorithm {
117    /// Returns the key length in bytes.
118    #[must_use]
119    pub const fn key_len(self) -> usize {
120        match self {
121            Self::Aes128 => 16,
122            Self::Aes256 | Self::ChaCha20 => 32,
123        }
124    }
125}
126
127/// CBC algorithms.
128#[non_exhaustive]
129#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
130pub enum CbcAlgorithm {
131    /// AES-256-CBC.
132    Aes256Cbc,
133}
134
135/// Ephemeral key-agreement algorithms.
136#[non_exhaustive]
137#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
138pub enum KeyExchangeAlgorithm {
139    /// ECDH over NIST P-256.
140    P256,
141    /// ECDH over NIST P-384.
142    P384,
143    /// X25519.
144    X25519,
145    /// X25519MLKEM768 (draft-ietf-tls-ecdhe-mlkem, code point 0x11EC): ML-KEM-768 and
146    /// X25519 side by side. Not a Diffie-Hellman exchange: the initiator's share is an
147    /// ML-KEM encapsulation key followed by an X25519 public key (1216 bytes), the
148    /// responder's an ML-KEM ciphertext followed by an X25519 public key (1120 bytes), and
149    /// the shared secret the ML-KEM secret followed by the X25519 one (64 bytes). A
150    /// responder goes through [`RTCCrypto::key_exchange_respond`](crate::RTCCrypto::key_exchange_respond).
151    X25519MlKem768,
152}
153
154/// Signature schemes currently used by DTLS.
155#[non_exhaustive]
156#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
157pub enum SignatureScheme {
158    /// Ed25519.
159    Ed25519,
160    /// ECDSA P-256 with SHA-256 and ASN.1 DER signatures.
161    EcdsaP256Sha256,
162    /// ECDSA P-384 with SHA-384 and ASN.1 DER signatures.
163    EcdsaP384Sha384,
164    /// RSA PKCS#1 v1.5 with SHA-1, for legacy verification only.
165    RsaPkcs1Sha1,
166    /// RSA PKCS#1 v1.5 with SHA-256.
167    RsaPkcs1Sha256,
168    /// RSA PKCS#1 v1.5 with SHA-384.
169    RsaPkcs1Sha384,
170    /// RSA PKCS#1 v1.5 with SHA-512.
171    RsaPkcs1Sha512,
172    /// ECDSA P-384 with SHA-256 and ASN.1 DER signatures.
173    EcdsaP384Sha256,
174    /// RSASSA-PSS with SHA-256, MGF1 with SHA-256 and a salt as long as the digest, over a
175    /// key with the rsaEncryption OID (TLS 1.3 `rsa_pss_rsae_sha256`, RFC 8446 section 4.2.3).
176    RsaPssSha256,
177    /// RSASSA-PSS with SHA-384 (TLS 1.3 `rsa_pss_rsae_sha384`).
178    RsaPssSha384,
179    /// RSASSA-PSS with SHA-512 (TLS 1.3 `rsa_pss_rsae_sha512`).
180    RsaPssSha512,
181}
182
183/// The encoding of public-key bytes.
184#[non_exhaustive]
185#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
186pub enum PublicKeyEncoding {
187    /// Complete DER-encoded SubjectPublicKeyInfo.
188    SubjectPublicKeyInfoDer,
189    /// SEC1 uncompressed elliptic-curve point.
190    EcUncompressedPoint,
191    /// Raw 32-byte Ed25519 public key.
192    Ed25519Raw,
193    /// PKCS#1 DER `RSAPublicKey`.
194    RsaPkcs1Der,
195}
196
197/// Borrowed public-key bytes with an explicit encoding.
198#[derive(Debug, Clone, Copy)]
199pub struct PublicKey<'a> {
200    /// Encoding of `bytes`.
201    pub encoding: PublicKeyEncoding,
202    /// Encoded public key.
203    pub bytes: &'a [u8],
204}
205
206/// A provider capability identifier.
207#[non_exhaustive]
208#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
209pub enum CryptoAlgorithm {
210    /// Stateless hash operation.
211    Hash(HashAlgorithm),
212    /// HMAC generation and verification.
213    Hmac(HmacAlgorithm),
214    /// Authenticated encryption.
215    Aead(AeadAlgorithm),
216    /// Stream encryption.
217    StreamCipher(StreamCipherAlgorithm),
218    /// Single-block encryption.
219    BlockCipher(BlockCipherAlgorithm),
220    /// Record-number (header-protection) masks.
221    HeaderMask(HeaderMaskAlgorithm),
222    /// CBC encryption and decryption.
223    Cbc(CbcAlgorithm),
224    /// Ephemeral key agreement.
225    KeyExchange(KeyExchangeAlgorithm),
226    /// Signature verification.
227    Signature(SignatureScheme),
228    /// Signing-key generation.
229    SigningKeyGeneration(SignatureScheme),
230    /// PKCS#8 signing-key import.
231    SigningKeyImport(SignatureScheme),
232}