Skip to content

Commit

Permalink
age: Use rayon for processing STREAM chunks in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Dec 29, 2020
1 parent b224739 commit b81fbe2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions age/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ rust-embed = "5"

# Performance
num_cpus = "1.0"
rayon = "1.5"

# Common CLI dependencies
console = { version = "0.13", optional = true }
Expand Down
40 changes: 22 additions & 18 deletions age/src/primitives/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use chacha20poly1305::{
};
use lazy_static::lazy_static;
use pin_project::pin_project;
use rayon::prelude::*;
use secrecy::{ExposeSecret, SecretVec};
use std::cmp;
use std::convert::TryInto;
Expand Down Expand Up @@ -53,9 +54,9 @@ impl Nonce {
self.0 = u128::from(val) << 8;
}

fn increment_counter(&mut self) {
fn increment_counter(&mut self, by: usize) {
// Increment the 11-byte counter
self.0 += 1 << 8;
self.0 += (by as u128) << 8;
if self.0 >> (8 * 12) != 0 {
panic!("We overflowed the nonce!");
}
Expand Down Expand Up @@ -194,26 +195,29 @@ impl Stream {
let num_chunks = chunks.len();
let mut encrypted = vec![0; chunks_len + TAG_SIZE * num_chunks];

for (i, (encrypted, chunk)) in encrypted
encrypted
.chunks_mut(ENCRYPTED_CHUNK_SIZE)
.zip(chunks)
.enumerate()
{
if i + 1 == num_chunks {
self.nonce.set_last(last).unwrap();
}
.par_bridge()
.for_each_with(self.nonce, |nonce, (i, (encrypted, chunk))| {
nonce.increment_counter(i);
if i + 1 == num_chunks {
nonce.set_last(last).unwrap();
}

let (buffer, tag) = encrypted.split_at_mut(chunk.len());
buffer.copy_from_slice(chunk);
tag.copy_from_slice(
self.aead
.encrypt_in_place_detached(&self.nonce.to_bytes().into(), &[], buffer)
.expect("we will never hit chacha20::MAX_BLOCKS because of the chunk size")
.as_slice(),
);
let (buffer, tag) = encrypted.split_at_mut(chunk.len());
buffer.copy_from_slice(chunk);
tag.copy_from_slice(
self.aead
.encrypt_in_place_detached(&nonce.to_bytes().into(), &[], buffer)
.expect("we will never hit chacha20::MAX_BLOCKS because of the chunk size")
.as_slice(),
);
});

self.nonce.increment_counter();
}
self.nonce.increment_counter(num_chunks);
self.nonce.set_last(last).unwrap();

Ok(encrypted)
}
Expand Down Expand Up @@ -248,7 +252,7 @@ impl Stream {
)
.map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "decryption error"))?;

self.nonce.increment_counter();
self.nonce.increment_counter(1);
}

Ok(SecretVec::new(decrypted))
Expand Down

0 comments on commit b81fbe2

Please sign in to comment.