diff --git a/src/cipher.rs b/src/cipher.rs index efe19e9..b11dfff 100644 --- a/src/cipher.rs +++ b/src/cipher.rs @@ -12,13 +12,17 @@ use blake3; use aes_gcm::{ aead::{Aead, AeadCore, KeyInit}, - Aes256Gcm, Nonce, Key + Aes256Gcm, Key, Nonce, }; type NonceSize = ::NonceSize; -/// Encrypts a plaintext given a shared DH secret key, returning the ciphertext and a 96-bit nonce -pub fn encrypt(secret_key: JubJubAffine, plaintext: Vec) -> (Vec, Nonce) { +/// Encrypts a plaintext given a shared DH secret key, returning the ciphertext +/// and a 96-bit nonce +pub fn encrypt( + secret_key: JubJubAffine, + plaintext: Vec, +) -> (Vec, Nonce) { // To encrypt using AES256 we need 32-bytes keys. Thus, we hash // the 64-bytes serialization of the DH key into a 32-bytes digest. let key = blake3::hash(&secret_key.to_bytes()); @@ -26,45 +30,57 @@ pub fn encrypt(secret_key: JubJubAffine, plaintext: Vec) -> (Vec, Nonce< let cipher = Aes256Gcm::new(&key); let nonce = Aes256Gcm::generate_nonce(&mut OsRng); - let ciphertext = cipher.encrypt(&nonce, plaintext.as_ref()).expect("Encryption failed."); + let ciphertext = cipher + .encrypt(&nonce, plaintext.as_ref()) + .expect("Encryption failed."); (ciphertext, nonce) } -/// Decrypts a ciphertext given a shared DH secret key and a nonce, returning the plaintext -pub fn decrypt(secret_key: JubJubAffine, nonce: Nonce, ciphertext: Vec) -> Vec { +/// Decrypts a ciphertext given a shared DH secret key and a nonce, returning +/// the plaintext +pub fn decrypt( + secret_key: JubJubAffine, + nonce: Nonce, + ciphertext: Vec, +) -> Vec { // To decrypt using AES256 we need 32-bytes keys. Thus, we hash // the 64-bytes serialization of the DH key into a 32-bytes digest. let key = blake3::hash(&secret_key.to_bytes()); let key = Key::::from_slice(key.as_bytes()); let cipher = Aes256Gcm::new(&key); - cipher.decrypt(&nonce, ciphertext.as_ref()).expect("Decryption failed.") + cipher + .decrypt(&nonce, ciphertext.as_ref()) + .expect("Decryption failed.") } #[cfg(test)] mod tests { - use dusk_jubjub::{GENERATOR, JubJubScalar, JubJubAffine}; use dusk_bls12_381::BlsScalar; use dusk_bytes::DeserializableSlice; + use dusk_jubjub::{JubJubAffine, JubJubScalar, GENERATOR}; - use crate::cipher::{encrypt, decrypt}; + use crate::cipher::{decrypt, encrypt}; #[test] fn test_encrypt_and_decrypt() { // testing values let plaintext = b"00112233445566778899"; let plaintext_scalar = BlsScalar::from(1234u64); - let secret_key = JubJubAffine::from(GENERATOR * JubJubScalar::from(1234u64)); + let secret_key = + JubJubAffine::from(GENERATOR * JubJubScalar::from(1234u64)); let (ciphertext, nonce) = encrypt(secret_key, plaintext.to_vec()); let dec_plaintext = decrypt(secret_key, nonce, ciphertext); assert_eq!(dec_plaintext, plaintext); - let (ciphertext, nonce) = encrypt(secret_key, plaintext_scalar.to_bytes().to_vec()); + let (ciphertext, nonce) = + encrypt(secret_key, plaintext_scalar.to_bytes().to_vec()); let dec_plaintext_scalar = decrypt(secret_key, nonce, ciphertext); - let dec_plaintext_scalar = BlsScalar::from_slice(&dec_plaintext_scalar).unwrap(); + let dec_plaintext_scalar = + BlsScalar::from_slice(&dec_plaintext_scalar).unwrap(); assert_eq!(dec_plaintext_scalar, plaintext_scalar); }