Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
xevisalle committed Apr 5, 2024
1 parent ffb2542 commit a6ffd9b
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,75 @@ use blake3;

use aes_gcm::{
aead::{Aead, AeadCore, KeyInit},
Aes256Gcm, Nonce, Key
Aes256Gcm, Key, Nonce,
};

type NonceSize = <Aes256Gcm as AeadCore>::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<u8>) -> (Vec<u8>, Nonce<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<u8>,
) -> (Vec<u8>, Nonce<NonceSize>) {
// 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());
let key = Key::<Aes256Gcm>::from_slice(key.as_bytes());

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<NonceSize>, ciphertext: Vec<u8>) -> Vec<u8> {
/// Decrypts a ciphertext given a shared DH secret key and a nonce, returning
/// the plaintext
pub fn decrypt(
secret_key: JubJubAffine,
nonce: Nonce<NonceSize>,
ciphertext: Vec<u8>,
) -> Vec<u8> {
// 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::<Aes256Gcm>::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);
}
Expand Down

0 comments on commit a6ffd9b

Please sign in to comment.