1use crate::effects::random::RandomCoreEffects;
18use crate::types::identifiers::DeviceId;
19use crate::{AccountId, AuraError};
20use async_trait::async_trait;
21use serde::{Deserialize, Serialize};
22
23pub const MAX_KEY_PACKAGE_BYTES: usize = 65_536;
24pub const MAX_PUBLIC_KEY_PACKAGE_BYTES: usize = 65_536;
25pub const MAX_SIGNING_MESSAGE_BYTES: usize = 65_536;
26pub const MAX_SIGNING_PACKAGE_BYTES: usize = 65_536;
27
28pub type CryptoError = AuraError;
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct KeyDerivationContext {
34 pub app_id: String,
36 pub context: String,
38 pub derivation_path: Vec<u32>,
40 pub account_id: AccountId,
42 pub device_id: DeviceId,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct FrostKeyGenResult {
49 pub key_packages: Vec<Vec<u8>>,
51 pub public_key_package: Vec<u8>,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct FrostSigningPackage {
58 pub message: Vec<u8>,
60 pub package: Vec<u8>,
62 pub participants: Vec<u16>,
64 pub public_key_package: Vec<u8>,
66}
67
68pub use crate::crypto::single_signer::SigningMode;
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct SigningKeyGenResult {
77 pub key_packages: Vec<Vec<u8>>,
82
83 pub public_key_package: Vec<u8>,
88
89 pub mode: SigningMode,
93}
94
95#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
97pub enum KeyGenerationMethod {
98 SingleSigner,
100 DealerBased,
102}
103
104#[async_trait]
119pub trait CryptoCoreEffects: RandomCoreEffects + Send + Sync {
120 async fn kdf_derive(
131 &self,
132 ikm: &[u8],
133 salt: &[u8],
134 info: &[u8],
135 output_len: u32,
136 ) -> Result<Vec<u8>, CryptoError>;
137
138 async fn derive_key(
140 &self,
141 master_key: &[u8],
142 context: &KeyDerivationContext,
143 ) -> Result<Vec<u8>, CryptoError>;
144
145 async fn ed25519_generate_keypair(&self) -> Result<(Vec<u8>, Vec<u8>), CryptoError>;
149
150 async fn ed25519_sign(
152 &self,
153 message: &[u8],
154 private_key: &[u8],
155 ) -> Result<Vec<u8>, CryptoError>;
156
157 async fn ed25519_verify(
159 &self,
160 message: &[u8],
161 signature: &[u8],
162 public_key: &[u8],
163 ) -> Result<bool, CryptoError>;
164
165 fn is_simulated(&self) -> bool;
167
168 fn crypto_capabilities(&self) -> Vec<String>;
170
171 fn constant_time_eq(&self, a: &[u8], b: &[u8]) -> bool;
173
174 fn secure_zero(&self, data: &mut [u8]);
176}
177
178#[async_trait]
180pub trait CryptoExtendedEffects: CryptoCoreEffects + Send + Sync {
181 async fn generate_signing_keys(
184 &self,
185 threshold: u16,
186 max_signers: u16,
187 ) -> Result<SigningKeyGenResult, CryptoError> {
188 let _ = (threshold, max_signers);
189 Err(AuraError::crypto("generate_signing_keys not supported"))
190 }
191
192 async fn generate_signing_keys_with(
200 &self,
201 method: KeyGenerationMethod,
202 threshold: u16,
203 max_signers: u16,
204 ) -> Result<SigningKeyGenResult, CryptoError> {
205 let _ = method;
206 self.generate_signing_keys(threshold, max_signers).await
207 }
208
209 async fn sign_with_key(
210 &self,
211 message: &[u8],
212 key_package: &[u8],
213 mode: SigningMode,
214 ) -> Result<Vec<u8>, CryptoError> {
215 let _ = (message, key_package, mode);
216 Err(AuraError::crypto("sign_with_key not supported"))
217 }
218
219 async fn verify_signature(
220 &self,
221 message: &[u8],
222 signature: &[u8],
223 public_key_package: &[u8],
224 mode: SigningMode,
225 ) -> Result<bool, CryptoError> {
226 let _ = (message, signature, public_key_package, mode);
227 Err(AuraError::crypto("verify_signature not supported"))
228 }
229
230 async fn frost_generate_keys(
233 &self,
234 threshold: u16,
235 max_signers: u16,
236 ) -> Result<FrostKeyGenResult, CryptoError> {
237 let _ = (threshold, max_signers);
238 Err(AuraError::crypto("frost_generate_keys not supported"))
239 }
240
241 async fn frost_generate_nonces(&self, key_package: &[u8]) -> Result<Vec<u8>, CryptoError> {
242 let _ = key_package;
243 Err(AuraError::crypto("frost_generate_nonces not supported"))
244 }
245
246 async fn frost_create_signing_package(
247 &self,
248 message: &[u8],
249 nonces: &[Vec<u8>],
250 participants: &[u16],
251 public_key_package: &[u8],
252 ) -> Result<FrostSigningPackage, CryptoError> {
253 let _ = (message, nonces, participants, public_key_package);
254 Err(AuraError::crypto(
255 "frost_create_signing_package not supported",
256 ))
257 }
258
259 async fn frost_sign_share(
260 &self,
261 signing_package: &FrostSigningPackage,
262 key_share: &[u8],
263 nonces: &[u8],
264 ) -> Result<Vec<u8>, CryptoError> {
265 let _ = (signing_package, key_share, nonces);
266 Err(AuraError::crypto("frost_sign_share not supported"))
267 }
268
269 async fn frost_aggregate_signatures(
270 &self,
271 signing_package: &FrostSigningPackage,
272 signature_shares: &[Vec<u8>],
273 ) -> Result<Vec<u8>, CryptoError> {
274 let _ = (signing_package, signature_shares);
275 Err(AuraError::crypto(
276 "frost_aggregate_signatures not supported",
277 ))
278 }
279
280 async fn frost_verify(
281 &self,
282 message: &[u8],
283 signature: &[u8],
284 group_public_key: &[u8],
285 ) -> Result<bool, CryptoError> {
286 let _ = (message, signature, group_public_key);
287 Err(AuraError::crypto("frost_verify not supported"))
288 }
289
290 async fn ed25519_public_key(&self, private_key: &[u8]) -> Result<Vec<u8>, CryptoError> {
292 let _ = private_key;
293 Err(AuraError::crypto("ed25519_public_key not supported"))
294 }
295
296 async fn chacha20_encrypt(
299 &self,
300 plaintext: &[u8],
301 key: &[u8; 32],
302 nonce: &[u8; 12],
303 ) -> Result<Vec<u8>, CryptoError> {
304 let _ = (plaintext, key, nonce);
305 Err(AuraError::crypto("chacha20_encrypt not supported"))
306 }
307
308 async fn chacha20_decrypt(
309 &self,
310 ciphertext: &[u8],
311 key: &[u8; 32],
312 nonce: &[u8; 12],
313 ) -> Result<Vec<u8>, CryptoError> {
314 let _ = (ciphertext, key, nonce);
315 Err(AuraError::crypto("chacha20_decrypt not supported"))
316 }
317
318 async fn aes_gcm_encrypt(
319 &self,
320 plaintext: &[u8],
321 key: &[u8; 32],
322 nonce: &[u8; 12],
323 ) -> Result<Vec<u8>, CryptoError> {
324 let _ = (plaintext, key, nonce);
325 Err(AuraError::crypto("aes_gcm_encrypt not supported"))
326 }
327
328 async fn aes_gcm_decrypt(
329 &self,
330 ciphertext: &[u8],
331 key: &[u8; 32],
332 nonce: &[u8; 12],
333 ) -> Result<Vec<u8>, CryptoError> {
334 let _ = (ciphertext, key, nonce);
335 Err(AuraError::crypto("aes_gcm_decrypt not supported"))
336 }
337
338 async fn frost_rotate_keys(
341 &self,
342 old_shares: &[Vec<u8>],
343 old_threshold: u16,
344 new_threshold: u16,
345 new_max_signers: u16,
346 ) -> Result<FrostKeyGenResult, CryptoError> {
347 let _ = (old_shares, old_threshold, new_threshold, new_max_signers);
348 Err(AuraError::crypto("frost_rotate_keys not supported"))
349 }
350
351 async fn convert_ed25519_to_x25519_public(
355 &self,
356 ed25519_public_key: &[u8],
357 ) -> Result<[u8; 32], CryptoError> {
358 let _ = ed25519_public_key;
359 Err(AuraError::crypto(
360 "convert_ed25519_to_x25519_public not supported",
361 ))
362 }
363
364 async fn convert_ed25519_to_x25519_private(
366 &self,
367 ed25519_private_key: &[u8],
368 ) -> Result<[u8; 32], CryptoError> {
369 let _ = ed25519_private_key;
370 Err(AuraError::crypto(
371 "convert_ed25519_to_x25519_private not supported",
372 ))
373 }
374}
375
376pub trait CryptoEffects: CryptoCoreEffects + CryptoExtendedEffects {}
378
379impl<T: CryptoCoreEffects + CryptoExtendedEffects + ?Sized> CryptoEffects for T {}
380
381#[async_trait]
384impl<T: CryptoCoreEffects + ?Sized> CryptoCoreEffects for std::sync::Arc<T> {
385 async fn kdf_derive(
386 &self,
387 ikm: &[u8],
388 salt: &[u8],
389 info: &[u8],
390 output_len: u32,
391 ) -> Result<Vec<u8>, CryptoError> {
392 (**self).kdf_derive(ikm, salt, info, output_len).await
393 }
394
395 async fn derive_key(
396 &self,
397 master_key: &[u8],
398 context: &KeyDerivationContext,
399 ) -> Result<Vec<u8>, CryptoError> {
400 (**self).derive_key(master_key, context).await
401 }
402
403 async fn ed25519_generate_keypair(&self) -> Result<(Vec<u8>, Vec<u8>), CryptoError> {
404 (**self).ed25519_generate_keypair().await
405 }
406
407 async fn ed25519_sign(
408 &self,
409 message: &[u8],
410 private_key: &[u8],
411 ) -> Result<Vec<u8>, CryptoError> {
412 (**self).ed25519_sign(message, private_key).await
413 }
414
415 async fn ed25519_verify(
416 &self,
417 message: &[u8],
418 signature: &[u8],
419 public_key: &[u8],
420 ) -> Result<bool, CryptoError> {
421 (**self)
422 .ed25519_verify(message, signature, public_key)
423 .await
424 }
425 fn is_simulated(&self) -> bool {
426 (**self).is_simulated()
427 }
428
429 fn crypto_capabilities(&self) -> Vec<String> {
430 (**self).crypto_capabilities()
431 }
432
433 fn constant_time_eq(&self, a: &[u8], b: &[u8]) -> bool {
434 (**self).constant_time_eq(a, b)
435 }
436
437 fn secure_zero(&self, data: &mut [u8]) {
438 (**self).secure_zero(data);
439 }
440}