p2panda_encryption/data_scheme/
data.rs1use crate::crypto::xchacha20::{XAeadError, XAeadNonce, x_aead_decrypt, x_aead_encrypt};
4use crate::data_scheme::group_secret::GroupSecret;
5
6pub fn encrypt_data(
7 plaintext: &[u8],
8 group_secret: &GroupSecret,
9 nonce: XAeadNonce,
10) -> Result<Vec<u8>, XAeadError> {
11 let ciphertext = x_aead_encrypt(group_secret.as_bytes(), plaintext, nonce, None)?;
12 Ok(ciphertext)
13}
14
15pub fn decrypt_data(
16 ciphertext: &[u8],
17 group_secret: &GroupSecret,
18 nonce: XAeadNonce,
19) -> Result<Vec<u8>, XAeadError> {
20 let plaintext = x_aead_decrypt(group_secret.as_bytes(), ciphertext, nonce, None)?;
21 Ok(plaintext)
22}
23
24#[cfg(test)]
25mod tests {
26 use crate::Rng;
27 use crate::crypto::xchacha20::XAeadNonce;
28 use crate::data_scheme::GroupSecret;
29
30 use super::{decrypt_data, encrypt_data};
31
32 #[test]
33 fn encrypt_decrypt() {
34 let rng = Rng::from_seed([1; 32]);
35
36 let group_secret = GroupSecret::from_rng(&rng).unwrap();
37 let nonce: XAeadNonce = rng.random_array().unwrap();
38
39 let message = encrypt_data(b"Service! Service!", &group_secret, nonce).unwrap();
40 let receive = decrypt_data(&message, &group_secret, nonce).unwrap();
41 assert_eq!(receive, b"Service! Service!");
42 }
43}