sim: Change to `ring` instead of `rust-crypto`
The ring crate's SHA256 is slightly slower than the one from
rust-crypto, but is much cleaner to use. It also has all of the hashing
operations we need.
This crate is somewhat opinionated, e.g. will not sign messages without
also performing the signature itself. Unfortunately, this means we'll
end up computing signatures of the same data many times, and perhaps
this should be improved, since we're always using the same data.
Signed-off-by: David Brown <david.brown@linaro.org>
diff --git a/sim/src/main.rs b/sim/src/main.rs
index aa7c75d..614def3 100644
--- a/sim/src/main.rs
+++ b/sim/src/main.rs
@@ -1,5 +1,5 @@
#[macro_use] extern crate log;
-extern crate crypto;
+extern crate ring;
extern crate env_logger;
extern crate enumflags;
#[macro_use] extern crate enumflags_derive;
diff --git a/sim/src/tlv.rs b/sim/src/tlv.rs
index 071af0c..2df43cc 100644
--- a/sim/src/tlv.rs
+++ b/sim/src/tlv.rs
@@ -8,8 +8,7 @@
//! Because of this header, we have to make two passes. The first pass will compute the size of
//! the TLV, and the second pass will build the data for the TLV.
-use crypto::digest::Digest;
-use crypto::sha2::Sha256;
+use ring::digest;
#[derive(EnumFlags, Copy, Clone, Debug)]
#[repr(u32)]
@@ -38,7 +37,7 @@
flags: Flags,
kinds: Vec<TlvKinds>,
size: u16,
- hasher: Sha256,
+ hasher: digest::Context,
}
impl TlvGen {
@@ -48,7 +47,7 @@
flags: Flags::SHA256,
kinds: vec![TlvKinds::SHA256],
size: 4 + 32,
- hasher: Sha256::new(),
+ hasher: digest::Context::new(&digest::SHA256),
}
}
@@ -64,26 +63,23 @@
/// Add bytes to the covered hash.
pub fn add_bytes(&mut self, bytes: &[u8]) {
- self.hasher.input(bytes);
+ self.hasher.update(bytes);
}
/// Compute the TLV given the specified block of data.
- pub fn make_tlv(mut self) -> Vec<u8> {
+ pub fn make_tlv(self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
if self.kinds.contains(&TlvKinds::SHA256) {
- let hash_size = self.hasher.output_bytes();
- assert!(hash_size == 32); // Assumption made.
+ let hash = self.hasher.finish();
+ let hash = hash.as_ref();
- let mut hash = vec![0; hash_size];
-
- self.hasher.result(&mut hash);
-
+ assert!(hash.len() == 32);
result.push(TlvKinds::SHA256 as u8);
result.push(0);
result.push(32);
result.push(0);
- result.append(&mut hash);
+ result.extend_from_slice(hash);
}
result