Skip to content

Commit

Permalink
use XChaCha20Poly1305 instead of Aes256Gcm
Browse files Browse the repository at this point in the history
  • Loading branch information
va-an committed Jun 7, 2024
1 parent 9f6e7bf commit 79adcb1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion core/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ tokio-stream = { version = "0.1.6", features = ["signal", "time"] }
cookie = { version = "0.18", features = ["percent-encode"] }
futures = { version = "0.3.30", default-features = false, features = ["std"] }
state = "0.6"
aes-gcm = "0.10.3"
chacha20poly1305 = "0.10.1"

[dependencies.hyper-util]
version = "0.1.3"
Expand Down
27 changes: 12 additions & 15 deletions core/lib/src/config/secret_key.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::fmt;

use aes_gcm::{
AeadCore, Aes256Gcm, Nonce,
aead::{generic_array::GenericArray, Aead, KeyInit, OsRng},
use chacha20poly1305::{
aead::{Aead, AeadCore, KeyInit, OsRng, generic_array::GenericArray},
XChaCha20Poly1305, XNonce,
};
use cookie::Key;
use serde::{de, ser, Deserialize, Serialize};

use crate::request::{Outcome, Request, FromRequest};

const NONCE_LEN: usize = 12;
const NONCE_LEN: usize = 24; // 192-bit
const KEY_LEN: usize = 32;

#[derive(Debug)]
Expand Down Expand Up @@ -218,14 +218,12 @@ impl SecretKey {
.try_into()
.map_err(|_| Error::KeyLengthError)?;

// Create a new AES-256-GCM instance with the provided key
let aead = Aes256Gcm::new(GenericArray::from_slice(&key));
let cipher = XChaCha20Poly1305::new(GenericArray::from_slice(&key));
let nonce = XChaCha20Poly1305::generate_nonce(&mut OsRng);

// Generate a random nonce
let nonce = Aes256Gcm::generate_nonce(&mut OsRng);

// Encrypt the plaintext using the nonce
let ciphertext = aead.encrypt(&nonce, value.as_ref()).map_err(|_| Error::EncryptionError)?;
let ciphertext = cipher
.encrypt(&nonce, value.as_ref())
.map_err(|_| Error::EncryptionError)?;

// Prepare a vector to hold the nonce and ciphertext
let mut encrypted_data = Vec::with_capacity(NONCE_LEN + ciphertext.len());
Expand All @@ -248,19 +246,18 @@ impl SecretKey {

// Split the decoded data into nonce and ciphertext
let (nonce, ciphertext) = encrypted.split_at(NONCE_LEN);
let nonce = Nonce::from_slice(nonce);
let nonce = XNonce::from_slice(nonce);

// Convert the encryption key to a fixed-length array
let key: [u8; KEY_LEN] = self.key
.encryption()
.try_into()
.map_err(|_| Error::KeyLengthError)?;

// Create a new AES-256-GCM instance with the provided key
let aead = Aes256Gcm::new(GenericArray::from_slice(&key));
let cipher = XChaCha20Poly1305::new(GenericArray::from_slice(&key));

// Decrypt the ciphertext using the nonce
let decrypted = aead.decrypt(nonce, ciphertext)
let decrypted = cipher.decrypt(nonce, ciphertext)
.map_err(|_| Error::DecryptionError)?;

Ok(decrypted)
Expand Down

0 comments on commit 79adcb1

Please sign in to comment.