dcrypt_symmetric/cipher.rs
1//! Symmetric cipher traits for dcrypt-symmetric
2//!
3//! This module defines the core traits used by all symmetric
4//! encryption algorithms in the library.
5
6#[cfg(not(feature = "std"))]
7use alloc::vec::Vec;
8
9use crate::error::Result;
10
11/// Common trait for all symmetric encryption algorithms
12pub trait SymmetricCipher {
13 /// The key type used by this cipher
14 type Key;
15
16 /// Creates a new cipher instance with the given key
17 fn new(key: &Self::Key) -> Result<Self>
18 where
19 Self: Sized;
20
21 /// Returns the name of this cipher
22 fn name() -> &'static str;
23}
24
25/// Trait for Authenticated Encryption with Associated Data
26pub trait Aead: SymmetricCipher {
27 /// The nonce/IV type used by this cipher
28 type Nonce;
29
30 /// Encrypts plaintext with associated data
31 fn encrypt(&self, nonce: &Self::Nonce, plaintext: &[u8], aad: Option<&[u8]>)
32 -> Result<Vec<u8>>;
33
34 /// Decrypts ciphertext with associated data
35 /// Returns an error if authentication fails
36 fn decrypt(
37 &self,
38 nonce: &Self::Nonce,
39 ciphertext: &[u8],
40 aad: Option<&[u8]>,
41 ) -> Result<Vec<u8>>;
42
43 /// Generates a secure random nonce
44 fn generate_nonce() -> Self::Nonce;
45}