1use std::sync::Arc;
2
3use crate::{
4 AeadAlgorithm, BlockCipherAlgorithm, CbcAlgorithm, CryptoAlgorithm, CryptoError, HashAlgorithm,
5 HmacAlgorithm, KeyExchangeAlgorithm, PublicKey, SecretVec, SignatureScheme,
6 StreamCipherAlgorithm,
7};
8
9pub trait RTCCryptoProvider: Send + Sync {
11 fn name(&self) -> &'static str;
13
14 fn crypto(&self) -> &dyn RTCCrypto;
16
17 fn random(&self) -> &dyn RTCRandom;
19}
20
21pub trait RTCRandom: Send + Sync {
23 fn fill(&self, output: &mut [u8]) -> Result<(), CryptoError>;
25}
26
27pub trait RTCCrypto: Send + Sync {
29 fn supports(&self, algorithm: CryptoAlgorithm) -> bool;
31
32 fn hash(&self, algorithm: HashAlgorithm, _data: &[u8]) -> Result<Vec<u8>, CryptoError> {
34 Err(CryptoError::UnsupportedAlgorithm(CryptoAlgorithm::Hash(
35 algorithm,
36 )))
37 }
38
39 fn block_encrypt(
41 &self,
42 algorithm: BlockCipherAlgorithm,
43 _key: &[u8],
44 _block: &mut [u8],
45 ) -> Result<(), CryptoError> {
46 Err(CryptoError::UnsupportedAlgorithm(
47 CryptoAlgorithm::BlockCipher(algorithm),
48 ))
49 }
50
51 fn new_hmac(&self, algorithm: HmacAlgorithm, _key: &[u8]) -> Result<Box<dyn Mac>, CryptoError> {
58 Err(CryptoError::UnsupportedAlgorithm(CryptoAlgorithm::Hmac(
59 algorithm,
60 )))
61 }
62
63 fn new_stream_cipher(
65 &self,
66 algorithm: StreamCipherAlgorithm,
67 _key: &[u8],
68 ) -> Result<Box<dyn StreamCipher>, CryptoError> {
69 Err(CryptoError::UnsupportedAlgorithm(
70 CryptoAlgorithm::StreamCipher(algorithm),
71 ))
72 }
73
74 fn new_aead(
76 &self,
77 algorithm: AeadAlgorithm,
78 _key: &[u8],
79 ) -> Result<Box<dyn AeadCipher>, CryptoError> {
80 Err(CryptoError::UnsupportedAlgorithm(CryptoAlgorithm::Aead(
81 algorithm,
82 )))
83 }
84
85 fn new_cbc(
87 &self,
88 algorithm: CbcAlgorithm,
89 _key: &[u8],
90 ) -> Result<Box<dyn CbcCipher>, CryptoError> {
91 Err(CryptoError::UnsupportedAlgorithm(CryptoAlgorithm::Cbc(
92 algorithm,
93 )))
94 }
95
96 fn start_key_exchange(
98 &self,
99 algorithm: KeyExchangeAlgorithm,
100 ) -> Result<Box<dyn ActiveKeyExchange>, CryptoError> {
101 Err(CryptoError::UnsupportedAlgorithm(
102 CryptoAlgorithm::KeyExchange(algorithm),
103 ))
104 }
105
106 fn generate_signing_key(
108 &self,
109 scheme: SignatureScheme,
110 ) -> Result<Arc<dyn SigningKey>, CryptoError> {
111 Err(CryptoError::UnsupportedAlgorithm(
112 CryptoAlgorithm::SigningKeyGeneration(scheme),
113 ))
114 }
115
116 fn import_signing_key(
118 &self,
119 scheme: SignatureScheme,
120 _pkcs8_der: &[u8],
121 ) -> Result<Arc<dyn SigningKey>, CryptoError> {
122 Err(CryptoError::UnsupportedAlgorithm(
123 CryptoAlgorithm::SigningKeyImport(scheme),
124 ))
125 }
126
127 fn verify_signature(
129 &self,
130 scheme: SignatureScheme,
131 _public_key: PublicKey<'_>,
132 _message: &[u8],
133 _signature: &[u8],
134 ) -> Result<(), CryptoError> {
135 Err(CryptoError::UnsupportedAlgorithm(
136 CryptoAlgorithm::Signature(scheme),
137 ))
138 }
139}
140
141pub trait Mac: Send {
152 fn output_len(&self) -> usize;
154
155 fn sign(&mut self, input: &[&[u8]], output: &mut [u8]) -> Result<(), CryptoError>;
160
161 fn verify(&mut self, input: &[&[u8]], expected: &[u8]) -> Result<(), CryptoError>;
163}
164
165pub trait StreamCipher: Send {
167 fn apply_keystream(&mut self, iv: &[u8], data: &mut [u8]) -> Result<(), CryptoError>;
169}
170
171pub trait AeadCipher: Send {
173 fn tag_len(&self) -> usize;
175
176 fn seal_in_place(
178 &mut self,
179 nonce: &[u8],
180 aad: &[u8],
181 plaintext_and_ciphertext: &mut [u8],
182 tag_out: &mut [u8],
183 ) -> Result<(), CryptoError>;
184
185 fn open_in_place(
187 &mut self,
188 nonce: &[u8],
189 aad: &[u8],
190 ciphertext_and_plaintext: &mut [u8],
191 tag: &[u8],
192 ) -> Result<(), CryptoError>;
193}
194
195pub trait CbcCipher: Send {
197 fn block_len(&self) -> usize;
199
200 fn encrypt_blocks(&mut self, iv: &[u8], blocks: &mut [u8]) -> Result<(), CryptoError>;
202
203 fn decrypt_blocks(&mut self, iv: &[u8], blocks: &mut [u8]) -> Result<(), CryptoError>;
205}
206
207pub trait ActiveKeyExchange: Send {
209 fn algorithm(&self) -> KeyExchangeAlgorithm;
211
212 fn public_key(&self) -> &[u8];
214
215 fn complete(self: Box<Self>, peer_public_key: &[u8]) -> Result<SecretVec, CryptoError>;
217}
218
219pub trait SigningKey: Send + Sync {
221 fn supports(&self, scheme: SignatureScheme) -> bool;
223
224 fn public_key(&self) -> PublicKey<'_>;
226
227 fn sign(&self, scheme: SignatureScheme, message: &[u8]) -> Result<Vec<u8>, CryptoError>;
229
230 fn to_pkcs8_der(&self) -> Result<Option<SecretVec>, CryptoError> {
232 Ok(None)
233 }
234}