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 /// ECDSA P-384 with SHA-256 and ASN.1 DER signatures.
107 EcdsaP384Sha256,
108}
109
110/// The encoding of public-key bytes.
111#[non_exhaustive]
112#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
113pub enum PublicKeyEncoding {
114 /// Complete DER-encoded SubjectPublicKeyInfo.
115 SubjectPublicKeyInfoDer,
116 /// SEC1 uncompressed elliptic-curve point.
117 EcUncompressedPoint,
118 /// Raw 32-byte Ed25519 public key.
119 Ed25519Raw,
120 /// PKCS#1 DER `RSAPublicKey`.
121 RsaPkcs1Der,
122}
123
124/// Borrowed public-key bytes with an explicit encoding.
125#[derive(Debug, Clone, Copy)]
126pub struct PublicKey<'a> {
127 /// Encoding of `bytes`.
128 pub encoding: PublicKeyEncoding,
129 /// Encoded public key.
130 pub bytes: &'a [u8],
131}
132
133/// A provider capability identifier.
134#[non_exhaustive]
135#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
136pub enum CryptoAlgorithm {
137 /// Stateless hash operation.
138 Hash(HashAlgorithm),
139 /// HMAC generation and verification.
140 Hmac(HmacAlgorithm),
141 /// Authenticated encryption.
142 Aead(AeadAlgorithm),
143 /// Stream encryption.
144 StreamCipher(StreamCipherAlgorithm),
145 /// Single-block encryption.
146 BlockCipher(BlockCipherAlgorithm),
147 /// CBC encryption and decryption.
148 Cbc(CbcAlgorithm),
149 /// Ephemeral key agreement.
150 KeyExchange(KeyExchangeAlgorithm),
151 /// Signature verification.
152 Signature(SignatureScheme),
153 /// Signing-key generation.
154 SigningKeyGeneration(SignatureScheme),
155 /// PKCS#8 signing-key import.
156 SigningKeyImport(SignatureScheme),
157}