lib3mf_core/crypto/
encryption.rs1use crate::error::{Lib3mfError, Result};
2use aes_gcm::{
3 Aes256Gcm, Key, Nonce,
4 aead::{Aead, KeyInit},
5};
6use rand::RngCore;
7
8pub fn encrypt_aes256gcm(key: &[u8], plaintext: &[u8]) -> Result<(Vec<u8>, Vec<u8>)> {
18 if key.len() != 32 {
19 return Err(Lib3mfError::Validation(
20 "Invalid key length for AES-256-GCM".to_string(),
21 ));
22 }
23
24 let key = Key::<Aes256Gcm>::from_slice(key);
25 let cipher = Aes256Gcm::new(key);
26
27 let mut nonce_bytes = [0u8; 12];
29 rand::rngs::OsRng.fill_bytes(&mut nonce_bytes);
30 let nonce = Nonce::from_slice(&nonce_bytes);
31
32 let ciphertext = cipher
33 .encrypt(nonce, plaintext)
34 .map_err(|e| Lib3mfError::EncryptionError(format!("Encryption failed: {}", e)))?;
35
36 Ok((ciphertext, nonce_bytes.to_vec()))
37}
38
39pub fn decrypt_aes256gcm(key: &[u8], nonce: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>> {
42 if key.len() != 32 {
43 return Err(Lib3mfError::Validation(
44 "Invalid key length for AES-256-GCM".to_string(),
45 ));
46 }
47 if nonce.len() != 12 {
48 return Err(Lib3mfError::Validation(
49 "Invalid nonce length for AES-256-GCM".to_string(),
50 ));
51 }
52
53 let key = Key::<Aes256Gcm>::from_slice(key);
54 let cipher = Aes256Gcm::new(key);
55 let nonce = Nonce::from_slice(nonce);
56
57 let plaintext = cipher
58 .decrypt(nonce, ciphertext)
59 .map_err(|e| Lib3mfError::EncryptionError(format!("Decryption failed: {}", e)))?;
60
61 Ok(plaintext)
62}