Skip to content

Commit

Permalink
No allocations in decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
expenses committed Feb 10, 2020
1 parent 3ecf756 commit f0ac676
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/resolvers/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::types::{Cipher, Dh, Hash, Random};
use crate::constants::TAGLEN;
use crate::params::{CipherChoice, DHChoice, HashChoice};
#[cfg(feature = "pqclean_kyber1024")] use crate::params::KemChoice;
use std::io::{Cursor, Write};
use super::CryptoResolver;

/// The default resolver provided by snow. This resolver is designed to
Expand Down Expand Up @@ -171,25 +170,20 @@ impl Cipher for CipherChaChaPoly {
let mut nonce_bytes = [0u8; 12];
copy_slices!(&nonce.to_le_bytes(), &mut nonce_bytes[4..]);

let mut buf = Cursor::new(out);
let mut buffer = ciphertext[..ciphertext.len()-TAGLEN].to_vec();
let message_len = ciphertext.len() - TAGLEN;

copy_slices!(ciphertext[..message_len], out);

let result = ChaCha20Poly1305::new(self.key.into()).decrypt_in_place_detached(
&nonce_bytes.into(),
authtext,
&mut buffer,
ciphertext[ciphertext.len()-TAGLEN..].into(),
&mut out[..message_len],
ciphertext[message_len..].into(),
);

match result {
Ok(_) => {
buf.write(&buffer).unwrap();

if buf.position() > usize::max_value() as u64 {
panic!("usize overflow");
} else {
Ok(buf.position() as usize)
}
}
Err(_) => Err(()),
Ok(_) => Ok(message_len),
Err(_) => Err(())
}
}
}
Expand Down

0 comments on commit f0ac676

Please sign in to comment.