Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chacha20 poly1305 #11

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 29 additions & 49 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion i6-pack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ workspace = true
clap = "4"
tar = "0.4"
zstd = {version = "0.13", features = ["zstdmt"]}
aes-gcm = "0.10"
chacha20poly1305 = "0.10"
rand = "0.8"
hmac = "0.12"
uuid = {version = "1", features = ["v4"]}
Expand Down
24 changes: 10 additions & 14 deletions i6-pack/src/encryption.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use aes_gcm::{
aead::{Aead, KeyInit},
Aes256Gcm, Key, Nonce,
use chacha20poly1305::{
aead::{Aead, AeadCore, KeyInit, OsRng},
ChaCha20Poly1305, Nonce,
};

use argon2::{self, password_hash::SaltString, Argon2, PasswordHasher};

use hmac::digest::{generic_array::GenericArray, typenum};
use rand::RngCore;
use std::fs::File;
Expand All @@ -16,15 +19,7 @@ fn generate_salt() -> [u8; SALT_LEN] {
salt
}

fn generate_nonce() -> Nonce<typenum::U12> {
let mut nonce = [0u8; NONCE_LEN];
rand::thread_rng().fill_bytes(&mut nonce);
*Nonce::from_slice(&nonce)
}

fn derive_key_from_password_argon2(password: &str, salt: &[u8]) -> [u8; 32] {
use argon2::{self, password_hash::SaltString, Argon2, PasswordHasher};

let argon2 = Argon2::default();
let salt = SaltString::encode_b64(salt).unwrap();
let password_hash = argon2.hash_password(password.as_bytes(), &salt).unwrap();
Expand All @@ -41,8 +36,8 @@ pub fn encrypt_file(
) -> io::Result<()> {
let salt = generate_salt();
let key = derive_key_from_password_argon2(password, &salt);
let cipher = Aes256Gcm::new(Key::<Aes256Gcm>::from_slice(&key));
let nonce = generate_nonce();
let cipher = ChaCha20Poly1305::new((&key).into());
let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);

let file_content = std::fs::read(input_file)?;
let ciphertext = cipher
Expand All @@ -53,6 +48,7 @@ pub fn encrypt_file(
output.write_all(&salt)?; // Prepend salt
output.write_all(nonce.as_slice())?; // Prepend nonce
output.write_all(&ciphertext)?;

Ok(())
}

Expand All @@ -67,7 +63,7 @@ pub fn decrypt_file(
let (salt, nonce) = salt_and_nonce.split_at(SALT_LEN); // Extract salt

let key = derive_key_from_password_argon2(password, salt);
let cipher = Aes256Gcm::new(Key::<Aes256Gcm>::from_slice(&key));
let cipher = ChaCha20Poly1305::new((&key).into());

let nonce = GenericArray::from_slice(nonce);
let plaintext = match cipher.decrypt(nonce, ciphertext) {
Expand Down
5 changes: 5 additions & 0 deletions tooling/ci.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/usr/bin/env bash

# Deps
# cargo install --locked cargo-audit cargo-edit cargo-udeps cargo-geiger cargo-crev cargo-deny

set -Eeuo pipefail

ci () {

cargo audit
cargo upgrade --verbose
cargo update --verbose
Expand Down
3 changes: 3 additions & 0 deletions tooling/prepare_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# ./prepare_release.sh 0.1.25
# ```

# Deps
# cargo install --locked git-cliff

set -Eeuo pipefail

ci () {
Expand Down
Loading