rtc_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}
10
11/// Algorithms accepted by the HMAC operations.
12#[non_exhaustive]
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub enum HmacAlgorithm {
15 /// HMAC-SHA1.
16 Sha1,
17 /// HMAC-SHA256.
18 Sha256,
19}
20
21impl HmacAlgorithm {
22 /// Returns the native tag length in bytes.
23 #[must_use]
24 pub const fn output_len(self) -> usize {
25 match self {
26 Self::Sha1 => 20,
27 Self::Sha256 => 32,
28 }
29 }
30}
31
32/// Authenticated-encryption algorithms.
33#[non_exhaustive]
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
35pub enum AeadAlgorithm {
36 /// AES-128-GCM.
37 Aes128Gcm,
38 /// AES-256-GCM.
39 Aes256Gcm,
40 /// AES-128-CCM with a 16-byte tag.
41 Aes128Ccm,
42 /// AES-128-CCM with an 8-byte tag.
43 Aes128Ccm8,
44 /// ChaCha20-Poly1305.
45 ChaCha20Poly1305,
46}
47
48/// Stream-cipher algorithms.
49#[non_exhaustive]
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
51pub enum StreamCipherAlgorithm {
52 /// AES-128 in counter mode.
53 Aes128Ctr,
54 /// AES-256 in counter mode.
55 Aes256Ctr,
56}
57
58/// Single-block encryption algorithms.
59#[non_exhaustive]
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
61pub enum BlockCipherAlgorithm {
62 /// AES-128.
63 Aes128,
64 /// AES-256.
65 Aes256,
66}
67
68/// CBC algorithms.
69#[non_exhaustive]
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
71pub enum CbcAlgorithm {
72 /// AES-256-CBC.
73 Aes256Cbc,
74}
75
76/// Ephemeral key-agreement algorithms.
77#[non_exhaustive]
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
79pub enum KeyExchangeAlgorithm {
80 /// ECDH over NIST P-256.
81 P256,
82 /// ECDH over NIST P-384.
83 P384,
84 /// X25519.
85 X25519,
86}
87
88/// Signature schemes currently used by DTLS.
89#[non_exhaustive]
90#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
91pub enum SignatureScheme {
92 /// Ed25519.
93 Ed25519,
94 /// ECDSA P-256 with SHA-256 and ASN.1 DER signatures.
95 EcdsaP256Sha256,
96 /// ECDSA P-384 with SHA-384 and ASN.1 DER signatures.
97 EcdsaP384Sha384,
98 /// RSA PKCS#1 v1.5 with SHA-1, for legacy verification only.
99 RsaPkcs1Sha1,
100 /// RSA PKCS#1 v1.5 with SHA-256.
101 RsaPkcs1Sha256,
102 /// RSA PKCS#1 v1.5 with SHA-384.
103 RsaPkcs1Sha384,
104 /// RSA PKCS#1 v1.5 with SHA-512.
105 RsaPkcs1Sha512,
106}
107
108/// The encoding of public-key bytes.
109#[non_exhaustive]
110#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
111pub enum PublicKeyEncoding {
112 /// Complete DER-encoded SubjectPublicKeyInfo.
113 SubjectPublicKeyInfoDer,
114 /// SEC1 uncompressed elliptic-curve point.
115 EcUncompressedPoint,
116 /// Raw 32-byte Ed25519 public key.
117 Ed25519Raw,
118 /// PKCS#1 DER `RSAPublicKey`.
119 RsaPkcs1Der,
120}
121
122/// Borrowed public-key bytes with an explicit encoding.
123#[derive(Debug, Clone, Copy)]
124pub struct PublicKey<'a> {
125 /// Encoding of `bytes`.
126 pub encoding: PublicKeyEncoding,
127 /// Encoded public key.
128 pub bytes: &'a [u8],
129}
130
131/// A provider capability identifier.
132#[non_exhaustive]
133#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
134pub enum CryptoAlgorithm {
135 /// Stateless hash operation.
136 Hash(HashAlgorithm),
137 /// HMAC generation and verification.
138 Hmac(HmacAlgorithm),
139 /// Authenticated encryption.
140 Aead(AeadAlgorithm),
141 /// Stream encryption.
142 StreamCipher(StreamCipherAlgorithm),
143 /// Single-block encryption.
144 BlockCipher(BlockCipherAlgorithm),
145 /// CBC encryption and decryption.
146 Cbc(CbcAlgorithm),
147 /// Ephemeral key agreement.
148 KeyExchange(KeyExchangeAlgorithm),
149 /// Signature verification.
150 Signature(SignatureScheme),
151 /// Signing-key generation.
152 SigningKeyGeneration(SignatureScheme),
153 /// PKCS#8 signing-key import.
154 SigningKeyImport(SignatureScheme),
155}