summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--openpgp/src/armor.rs7
-rw-r--r--openpgp/src/crypto/hash.rs54
-rw-r--r--openpgp/src/crypto/mpi.rs4
-rw-r--r--openpgp/src/keyid.rs21
-rw-r--r--openpgp/src/packet/pkesk.rs14
-rw-r--r--openpgp/src/parse.rs11
-rw-r--r--openpgp/src/serialize.rs10
-rw-r--r--openpgp/src/serialize/stream/writer/mod.rs10
-rw-r--r--openpgp/src/utils.rs22
9 files changed, 49 insertions, 104 deletions
diff --git a/openpgp/src/armor.rs b/openpgp/src/armor.rs
index a60fd11e..812169c3 100644
--- a/openpgp/src/armor.rs
+++ b/openpgp/src/armor.rs
@@ -303,12 +303,9 @@ impl<W: Write> Writer<W> {
write!(self.sink, "{}", LINE_ENDING)?;
}
+ // 24-bit CRC
let crc = self.crc.finalize();
- let bytes: [u8; 3] = [
- (crc >> 16) as u8,
- (crc >> 8) as u8,
- (crc >> 0) as u8,
- ];
+ let bytes = &crc.to_be_bytes()[1..4];
// CRC and footer.
write!(self.sink, "={}{}{}{}",
diff --git a/openpgp/src/crypto/hash.rs b/openpgp/src/crypto/hash.rs
index 9605318f..59072726 100644
--- a/openpgp/src/crypto/hash.rs
+++ b/openpgp/src/crypto/hash.rs
@@ -230,16 +230,13 @@ pub trait Hash {
impl Hash for UserID {
/// Update the Hash with a hash of the user id.
fn hash(&self, hash: &mut Context) {
- let mut header = [0; 5];
+ let len = self.value().len() as u32;
+ let mut header = [0; 5];
header[0] = 0xB4;
- let len = self.value().len() as u32;
- header[1] = (len >> 24) as u8;
- header[2] = (len >> 16) as u8;
- header[3] = (len >> 8) as u8;
- header[4] = (len) as u8;
+ header[1..5].copy_from_slice(&len.to_be_bytes());
- hash.update(&header[..]);
+ hash.update(header);
hash.update(self.value());
}
}
@@ -247,16 +244,13 @@ impl Hash for UserID {
impl Hash for UserAttribute {
/// Update the Hash with a hash of the user attribute.
fn hash(&self, hash: &mut Context) {
- let mut header = [0; 5];
+ let len = self.value().len() as u32;
+ let mut header = [0; 5];
header[0] = 0xD1;
- let len = self.value().len() as u32;
- header[1] = (len >> 24) as u8;
- header[2] = (len >> 16) as u8;
- header[3] = (len >> 8) as u8;
- header[4] = (len) as u8;
+ header[1..5].copy_from_slice(&len.to_be_bytes());
- hash.update(&header[..]);
+ hash.update(&header);
hash.update(self.value());
}
}
@@ -269,18 +263,17 @@ impl<P, R> Hash for Key4<P, R>
fn hash(&self, hash: &mut Context) {
use crate::serialize::MarshalInto;
- // We hash 8 bytes plus the MPIs. But, the len doesn't
+ // We hash 9 bytes plus the MPIs. But, the len doesn't
// include the tag (1 byte) or the length (2 bytes).
- let len = (9 - 3) + self.mpis().serialized_len();
+ let len = (9 - 3) + self.mpis().serialized_len() as u16;
- let mut header : Vec<u8> = Vec::with_capacity(9);
+ let mut header: Vec<u8> = Vec::with_capacity(9);
// Tag. Note: we use this whether
header.push(0x99);
- // Length (big endian).
- header.push(((len >> 8) & 0xFF) as u8);
- header.push((len & 0xFF) as u8);
+ // Length (2 bytes, big endian).
+ header.extend_from_slice(&len.to_be_bytes());
// Version.
header.push(4);
@@ -290,10 +283,7 @@ impl<P, R> Hash for Key4<P, R>
Timestamp::try_from(self.creation_time())
.unwrap_or_else(|_| Timestamp::try_from(0).unwrap())
.into();
- header.push((creation_time >> 24) as u8);
- header.push((creation_time >> 16) as u8);
- header.push((creation_time >> 8) as u8);
- header.push((creation_time >> 0) as u8);
+ header.extend_from_slice(&creation_time.to_be_bytes());
// Algorithm.
header.push(self.pk_algo().into());
@@ -350,10 +340,9 @@ impl Hash for signature::SignatureBuilder {
header[2] = self.pk_algo().into();
header[3] = self.hash_algo().into();
- // The length of the hashed area, as a 16-bit endian number.
- let len = hashed_area.len();
- header[4] = (len >> 8) as u8;
- header[5] = len as u8;
+ // The length of the hashed area, as a 16-bit big endian number.
+ let len = hashed_area.len() as u16;
+ header[4..6].copy_from_slice(&len.to_be_bytes());
hash.update(&header[..]);
hash.update(&hashed_area);
@@ -371,15 +360,12 @@ impl Hash for signature::SignatureBuilder {
// See https://tools.ietf.org/html/rfc4880#section-5.2.4
let mut trailer = [0u8; 6];
- trailer[0] = 0x4;
+ trailer[0] = 4;
trailer[1] = 0xff;
// The signature packet's length, not including the previous
// two bytes and the length.
- let len = header.len() + hashed_area.len();
- trailer[2] = (len >> 24) as u8;
- trailer[3] = (len >> 16) as u8;
- trailer[4] = (len >> 8) as u8;
- trailer[5] = len as u8;
+ let len = (header.len() + hashed_area.len()) as u32;
+ trailer[2..6].copy_from_slice(&len.to_be_bytes());
hash.update(&trailer[..]);
}
diff --git a/openpgp/src/crypto/mpi.rs b/openpgp/src/crypto/mpi.rs
index a23cc64f..13bc0007 100644
--- a/openpgp/src/crypto/mpi.rs
+++ b/openpgp/src/crypto/mpi.rs
@@ -177,9 +177,9 @@ impl fmt::Debug for MPI {
impl Hash for MPI {
/// Update the Hash with a hash of the MPIs.
fn hash(&self, hash: &mut hash::Context) {
- let len = &[(self.bits() >> 8) as u8 & 0xFF, self.bits() as u8];
+ let len = self.bits() as u16;
- hash.update(len);
+ hash.update(&len.to_be_bytes());
hash.update(&self.value);
}
}
diff --git a/openpgp/src/keyid.rs b/openpgp/src/keyid.rs
index ddd8b247..fb511316 100644
--- a/openpgp/src/keyid.rs
+++ b/openpgp/src/keyid.rs
@@ -117,16 +117,7 @@ impl From<Fingerprint> for KeyID {
impl KeyID {
/// Converts a u64 to a KeyID.
pub fn new(data: u64) -> KeyID {
- let bytes = [
- (data >> (7 * 8)) as u8,
- (data >> (6 * 8)) as u8,
- (data >> (5 * 8)) as u8,
- (data >> (4 * 8)) as u8,
- (data >> (3 * 8)) as u8,
- (data >> (2 * 8)) as u8,
- (data >> (1 * 8)) as u8,
- (data >> (0 * 8)) as u8
- ];
+ let bytes = data.to_be_bytes();
Self::from_bytes(&bytes[..])
}
@@ -134,15 +125,7 @@ impl KeyID {
pub fn as_u64(&self) -> Result<u64> {
match &self {
KeyID::V4(ref b) =>
- Ok(0u64
- | ((b[0] as u64) << (7 * 8))
- | ((b[1] as u64) << (6 * 8))
- | ((b[2] as u64) << (5 * 8))
- | ((b[3] as u64) << (4 * 8))
- | ((b[4] as u64) << (3 * 8))
- | ((b[5] as u64) << (2 * 8))
- | ((b[6] as u64) << (1 * 8))
- | ((b[7] as u64) << (0 * 8))),
+ Ok(u64::from_be_bytes(*b)),
KeyID::Invalid(_) =>
Err(Error::InvalidArgument("Invalid KeyID".into()).into()),
}
diff --git a/openpgp/src/packet/pkesk.rs b/openpgp/src/packet/pkesk.rs
index 6aa7eb66..0724e50f 100644
--- a/openpgp/src/packet/pkesk.rs
+++ b/openpgp/src/packet/pkesk.rs
@@ -70,11 +70,15 @@ impl PKESK3 {
psk.push(algo.into());
psk.extend_from_slice(session_key);
- // Compute the sum modulo 65536.
- let checksum
- = session_key.iter().map(|&x| x as usize).sum::<usize>() & 0xffff;
- psk.push((checksum >> 8) as u8);
- psk.push((checksum >> 0) as u8);
+ // Compute the sum modulo 65536, i.e. as u16.
+ let checksum = session_key
+ .iter()
+ .cloned()
+ .map(u16::from)
+ .fold(0u16, u16::wrapping_add);
+
+ psk.extend_from_slice(&checksum.to_be_bytes());
+
let psk: SessionKey = psk.into();
let esk = recipient.encrypt(&psk)?;
Ok(PKESK3{
diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs
index b5e38747..7d7d2ecf 100644
--- a/openpgp/src/parse.rs
+++ b/openpgp/src/parse.rs
@@ -1553,13 +1553,10 @@ impl SubpacketLength {
if Self::len_optimal_encoding(len) == 5 {
None
} else {
- Some(vec![
- octet1,
- (len >> 24) as u8,
- (len >> 16) as u8,
- (len >> 8) as u8,
- (len >> 0) as u8,
- ])
+ let mut out = Vec::with_capacity(5);
+ out.push(octet1);
+ out.extend_from_slice(&len.to_be_bytes());
+ Some(out)
}))
}
}
diff --git a/openpgp/src/serialize.rs b/openpgp/src/serialize.rs
index 552593bc..a9ff6a7e 100644
--- a/openpgp/src/serialize.rs
+++ b/openpgp/src/serialize.rs
@@ -580,19 +580,15 @@ fn test_generic_export_into() {
}
fn write_byte(o: &mut dyn std::io::Write, b: u8) -> io::Result<()> {
- let b : [u8; 1] = [b; 1];
- o.write_all(&b[..])
+ o.write_all(&[b])
}
fn write_be_u16(o: &mut dyn std::io::Write, n: u16) -> io::Result<()> {
- let b : [u8; 2] = [ ((n >> 8) & 0xFF) as u8, (n & 0xFF) as u8 ];
- o.write_all(&b[..])
+ o.write_all(&n.to_be_bytes())
}
fn write_be_u32(o: &mut dyn std::io::Write, n: u32) -> io::Result<()> {
- let b : [u8; 4] = [ (n >> 24) as u8, ((n >> 16) & 0xFF) as u8,
- ((n >> 8) & 0xFF) as u8, (n & 0xFF) as u8 ];
- o.write_all(&b[..])
+ o.write_all(&n.to_be_bytes())
}
// Compute the log2 of an integer. (This is simply the most
diff --git a/openpgp/src/serialize/stream/writer/mod.rs b/openpgp/src/serialize/stream/writer/mod.rs
index 705d0048..8aca6891 100644
--- a/openpgp/src/serialize/stream/writer/mod.rs
+++ b/openpgp/src/serialize/stream/writer/mod.rs
@@ -100,21 +100,17 @@ pub(crate) trait Stackable<'a, C> : io::Write + fmt::Debug {
/// Writes a byte.
fn write_u8(&mut self, b: u8) -> io::Result<()> {
- let b : [u8; 1] = [b; 1];
- self.write_all(&b[..])
+ self.write_all(&[b])
}
/// Writes a big endian `u16`.
fn write_be_u16(&mut self, n: u16) -> io::Result<()> {
- let b : [u8; 2] = [ ((n >> 8) & 0xFF) as u8, (n & 0xFF) as u8 ];
- self.write_all(&b[..])
+ self.write_all(&n.to_be_bytes())
}
/// Writes a big endian `u32`.
fn write_be_u32(&mut self, n: u32) -> io::Result<()> {
- let b : [u8; 4] = [ (n >> 24) as u8, ((n >> 16) & 0xFF) as u8,
- ((n >> 8) & 0xFF) as u8, (n & 0xFF) as u8 ];
- self.write_all(&b[..])
+ self.write_all(&n.to_be_bytes())
}
}
diff --git a/openpgp/src/utils.rs b/openpgp/src/utils.rs
index 5abc968b..d77dfb67 100644
--- a/openpgp/src/utils.rs
+++ b/openpgp/src/utils.rs
@@ -1,27 +1,13 @@
//! Utility functions that don't fit anywhere else.
+use std::convert::TryFrom;
pub fn read_be_u64(b: &[u8]) -> u64 {
- assert_eq!(b.len(), 8);
- ((b[0] as u64) << 56) as u64
- | ((b[1] as u64) << 48)
- | ((b[2] as u64) << 40)
- | ((b[3] as u64) << 32)
- | ((b[4] as u64) << 24)
- | ((b[5] as u64) << 16)
- | ((b[6] as u64) << 8)
- | ((b[7] as u64) << 0)
+ let array = <[u8; 8]>::try_from(b).unwrap();
+ u64::from_be_bytes(array)
}
pub fn write_be_u64(b: &mut [u8], n: u64) {
- assert_eq!(b.len(), 8);
- b[0] = (n >> 56) as u8;
- b[1] = (n >> 48) as u8;
- b[2] = (n >> 40) as u8;
- b[3] = (n >> 32) as u8;
- b[4] = (n >> 24) as u8;
- b[5] = (n >> 16) as u8;
- b[6] = (n >> 8) as u8;
- b[7] = (n >> 0) as u8;
+ b.copy_from_slice(&n.to_be_bytes());
}
#[cfg(test)]