summaryrefslogtreecommitdiffstats
path: root/openpgp/src/crypto
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2019-10-30 17:00:22 +0100
committerNeal H. Walfield <neal@pep.foundation>2019-10-30 17:10:55 +0100
commit001d04bfc89c3dc9ab06f703ff15cbb1c66f1dc9 (patch)
tree6db79487ee671d08157d37a488127d33e2965697 /openpgp/src/crypto
parentf893274486c61e0a4709e8d36e88c72efdc1446e (diff)
openpgp,buffered-reader: Optimize Vec<u8>::truncate manually
- On debug builds, Vec<u8>::truncate is very, very slow. For instance, running the decrypt_test_stream test takes 51 seconds on my (Neal's) computer using Vec<u8>::truncate and <0.1 seconds using `unsafe { v.set_len(len); }`. The issue is that the compiler calls drop on every element that is dropped, even though a u8 doesn't have a drop implementation. The compiler optimizes this away at high optimization levels, but those levels make debugging harder.
Diffstat (limited to 'openpgp/src/crypto')
-rw-r--r--openpgp/src/crypto/ecdh.rs3
-rw-r--r--openpgp/src/crypto/symmetric.rs7
2 files changed, 6 insertions, 4 deletions
diff --git a/openpgp/src/crypto/ecdh.rs b/openpgp/src/crypto/ecdh.rs
index d16de842..15228f59 100644
--- a/openpgp/src/crypto/ecdh.rs
+++ b/openpgp/src/crypto/ecdh.rs
@@ -1,5 +1,6 @@
//! Elliptic Curve Diffie-Hellman.
+use crate::vec_truncate;
use crate::Error;
use crate::packet::{
Key,
@@ -426,7 +427,7 @@ pub fn pkcs5_unpad(sk: Protected, target_len: usize) -> Result<Protected> {
}
if good {
- buf.truncate(target_len);
+ vec_truncate(&mut buf, target_len);
Ok(buf.into())
} else {
let sk: Protected = buf.into();
diff --git a/openpgp/src/crypto/symmetric.rs b/openpgp/src/crypto/symmetric.rs
index 5ca999ad..e1d78275 100644
--- a/openpgp/src/crypto/symmetric.rs
+++ b/openpgp/src/crypto/symmetric.rs
@@ -7,6 +7,7 @@ use std::fmt;
use crate::Result;
use crate::Error;
use crate::SymmetricAlgorithm;
+use crate::vec_truncate;
use buffered_reader::BufferedReader;
@@ -221,7 +222,7 @@ impl<R: io::Read> io::Read for Decryptor<R> {
Ok(amount) => {
short_read = amount < to_copy;
to_copy = amount;
- ciphertext.truncate(to_copy);
+ vec_truncate(&mut ciphertext, to_copy);
},
// We encountered an error, but we did read some.
Err(_) if pos > 0 => return Ok(pos),
@@ -251,7 +252,7 @@ impl<R: io::Read> io::Read for Decryptor<R> {
Ok(amount) => {
// Make sure `ciphertext` is not larger than the
// amount of data that was actually read.
- ciphertext.truncate(amount);
+ vec_truncate(&mut ciphertext, amount);
// Make sure we don't read more than is available.
to_copy = cmp::min(to_copy, ciphertext.len());
@@ -265,7 +266,7 @@ impl<R: io::Read> io::Read for Decryptor<R> {
while self.buffer.len() < ciphertext.len() {
self.buffer.push(0u8);
}
- self.buffer.truncate(ciphertext.len());
+ vec_truncate(&mut self.buffer, ciphertext.len());
self.dec.decrypt(&mut self.iv, &mut self.buffer, &ciphertext[..])
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput,