0
0
mirror of https://github.com/signalapp/libsignal.git synced 2024-09-20 12:02:18 +02:00

Update hash traits

This commit is contained in:
Jack Lloyd 2021-02-19 13:36:16 -05:00
parent 680b58dc1c
commit 41ae2adbf8
4 changed files with 57 additions and 140 deletions

110
Cargo.lock generated
View File

@ -40,7 +40,7 @@ dependencies = [
"polyval",
"serde",
"serde_json",
"subtle 2.3.0",
"subtle",
]
[[package]]
@ -50,7 +50,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e8bdbc97ba3854ecf597a3b69d7bd30a719dee72d22ce6313c84dbf2c8f2694"
dependencies = [
"cipher",
"opaque-debug 0.3.0",
"opaque-debug",
]
[[package]]
@ -60,7 +60,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea2e11f5e94c2f7d386164cc2aa1f97823fed6f259e486940a71c174dd01b0ce"
dependencies = [
"cipher",
"opaque-debug 0.3.0",
"opaque-debug",
]
[[package]]
@ -123,18 +123,6 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "block-buffer"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b"
dependencies = [
"block-padding 0.1.5",
"byte-tools",
"byteorder",
"generic-array 0.12.3",
]
[[package]]
name = "block-buffer"
version = "0.9.0"
@ -150,19 +138,10 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57a0e8073e8baa88212fb5823574c02ebccb395136ba9a164ab89379ec6072f0"
dependencies = [
"block-padding 0.2.1",
"block-padding",
"cipher",
]
[[package]]
name = "block-padding"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5"
dependencies = [
"byte-tools",
]
[[package]]
name = "block-padding"
version = "0.2.1"
@ -187,12 +166,6 @@ version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820"
[[package]]
name = "byte-tools"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7"
[[package]]
name = "byteorder"
version = "1.3.4"
@ -352,16 +325,6 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "crypto-mac"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5"
dependencies = [
"generic-array 0.12.3",
"subtle 1.0.0",
]
[[package]]
name = "crypto-mac"
version = "0.9.1"
@ -369,7 +332,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58bcd97a54c7ca5ce2f6eb16f6bede5b0ab5f0055fedc17d2f0b4466e21671ca"
dependencies = [
"generic-array 0.14.4",
"subtle 2.3.0",
"subtle",
]
[[package]]
@ -419,7 +382,7 @@ dependencies = [
"packed_simd",
"rand_core",
"serde",
"subtle 2.3.0",
"subtle",
"zeroize",
]
@ -433,7 +396,7 @@ dependencies = [
"digest 0.9.0",
"packed_simd",
"rand_core",
"subtle 2.3.0",
"subtle",
"zeroize",
]
@ -471,12 +434,6 @@ dependencies = [
"version_check",
]
[[package]]
name = "fake-simd"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
[[package]]
name = "fixedbitset"
version = "0.2.0"
@ -650,23 +607,13 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35"
[[package]]
name = "hmac"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dcb5e64cda4c23119ab41ba960d1e170a774c8e4b9d9e6a9bc18aabf5e59695"
dependencies = [
"crypto-mac 0.7.0",
"digest 0.8.1",
]
[[package]]
name = "hmac"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "deae6d9dbb35ec2c502d62b8f7b1c000a0822c3b0794ba36b3149c0a1c840dff"
dependencies = [
"crypto-mac 0.9.1",
"crypto-mac",
"digest 0.9.0",
]
@ -838,13 +785,13 @@ dependencies = [
"curve25519-dalek 3.0.0",
"futures",
"hex",
"hmac 0.9.0",
"hmac",
"log",
"prost",
"prost-build",
"rand",
"sha2 0.9.3",
"subtle 2.3.0",
"sha2",
"subtle",
"x25519-dalek",
]
@ -992,12 +939,6 @@ version = "11.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a170cebd8021a008ea92e4db85a72f80b35df514ec664b296fdcbb654eac0b2c"
[[package]]
name = "opaque-debug"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
[[package]]
name = "opaque-debug"
version = "0.3.0"
@ -1078,8 +1019,9 @@ name = "poksho"
version = "0.7.0"
dependencies = [
"curve25519-dalek 2.0.0",
"hmac 0.7.1",
"sha2 0.8.2",
"hex",
"hmac",
"sha2",
]
[[package]]
@ -1376,29 +1318,17 @@ dependencies = [
"serde",
]
[[package]]
name = "sha2"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69"
dependencies = [
"block-buffer 0.7.3",
"digest 0.8.1",
"fake-simd",
"opaque-debug 0.2.3",
]
[[package]]
name = "sha2"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa827a14b29ab7f44778d14a88d3cb76e949c45083f7dbfa507d0cb699dc12de"
dependencies = [
"block-buffer 0.9.0",
"block-buffer",
"cfg-if 1.0.0",
"cpuid-bool",
"digest 0.9.0",
"opaque-debug 0.3.0",
"opaque-debug",
]
[[package]]
@ -1437,12 +1367,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "subtle"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee"
[[package]]
name = "subtle"
version = "2.3.0"
@ -1547,7 +1471,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8326b2c654932e3e4f9196e69d08fdf7cfd718e1dc6f66b347e6024a0c961402"
dependencies = [
"generic-array 0.14.4",
"subtle 2.3.0",
"subtle",
]
[[package]]

View File

@ -1,7 +1,5 @@
#
# Copyright (C) 2020 Signal Messenger, LLC.
# All rights reserved.
#
# Copyright 2020 Signal Messenger, LLC.
# SPDX-License-Identifier: AGPL-3.0-only
#
@ -13,8 +11,11 @@ edition = "2018"
license = "AGPL-3.0-only"
[dependencies]
sha2 = "0.8.0"
hmac = "0.7.1"
sha2 = "0.9"
hmac = "0.9.0"
[dev-dependencies]
hex = "0.4"
[dependencies.curve25519-dalek]
features = ["serde", "alloc"]

View File

@ -3,7 +3,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
//
use hmac::{Hmac, Mac};
use hmac::{Hmac, Mac, NewMac};
use sha2::Sha256;
use std::cmp;
@ -41,7 +41,7 @@ impl ShoApi for ShoHmacSha256 {
self.hasher = Hmac::<Sha256>::new_varkey(&self.cv).unwrap();
self.mode = Mode::ABSORBING;
}
self.hasher.input(input);
self.hasher.update(input);
}
// called after absorb() only; streaming squeeze not yet supported
@ -49,9 +49,9 @@ impl ShoApi for ShoHmacSha256 {
if let Mode::RATCHETED = self.mode {
panic!();
}
self.hasher.input(&[0x00]);
self.hasher.update(&[0x00]);
self.cv
.copy_from_slice(&self.hasher.clone().result().code()[..]);
.copy_from_slice(&self.hasher.clone().finalize().into_bytes());
self.hasher.reset();
self.mode = Mode::RATCHETED;
}
@ -65,18 +65,18 @@ impl ShoApi for ShoHmacSha256 {
let mut i = 0;
while i * HASH_LEN < outlen {
let mut output_hasher = output_hasher_prefix.clone();
output_hasher.input(&(i as u64).to_be_bytes());
output_hasher.input(&[0x01]);
let digest = output_hasher.result().code();
output_hasher.update(&(i as u64).to_be_bytes());
output_hasher.update(&[0x01]);
let digest = output_hasher.finalize().into_bytes();
let num_bytes = cmp::min(HASH_LEN, outlen - i * HASH_LEN);
output.extend_from_slice(&digest[0..num_bytes]);
i += 1
}
let mut next_hasher = output_hasher_prefix;
next_hasher.input(&(outlen as u64).to_be_bytes());
next_hasher.input(&[0x02]);
self.cv.copy_from_slice(&next_hasher.result().code()[..]);
next_hasher.update(&(outlen as u64).to_be_bytes());
next_hasher.update(&[0x02]);
self.cv.copy_from_slice(&next_hasher.finalize().into_bytes()[..]);
self.mode = Mode::RATCHETED;
output
}
@ -93,7 +93,7 @@ mod tests {
sho.absorb_and_ratchet(b"asdasd");
let out = sho.squeeze_and_ratchet(64);
/*
println!("{}", hex::encode(out));
println!("{}", hex::encode(&out));
*/
assert!(
out == vec![
@ -109,7 +109,7 @@ mod tests {
sho.absorb_and_ratchet(b"asdasd");
let out = sho.squeeze_and_ratchet(65);
/*
println!("{}", hex::encode(out));
println!("{}", hex::encode(&out));
*/
assert!(
out == vec![
@ -137,7 +137,7 @@ mod tests {
sho.squeeze_and_ratchet(129);
sho.absorb_and_ratchet(b"def");
let out = sho.squeeze_and_ratchet(63);
println!("{}", hex::encode(out));
println!("{}", hex::encode(&out));
assert!(
out == vec![
0xc5, 0xc1, 0x3b, 0xcc, 0x65, 0x96, 0xc2, 0x5f, 0xc4, 0x51, 0x4e, 0xac, 0x92, 0x69,

View File

@ -39,11 +39,11 @@ impl ShoApi for ShoSha256 {
fn absorb(&mut self, input: &[u8]) {
if let Mode::RATCHETED = self.mode {
self.hasher.input(&[0u8; BLOCK_LEN][..]);
self.hasher.input(&self.cv);
self.hasher.update(&[0u8; BLOCK_LEN]);
self.hasher.update(&self.cv);
self.mode = Mode::ABSORBING;
}
self.hasher.input(input);
self.hasher.update(input);
}
// called after absorb() only; streaming squeeze not yet supported
@ -51,8 +51,9 @@ impl ShoApi for ShoSha256 {
if let Mode::RATCHETED = self.mode {
panic!();
}
self.cv
.copy_from_slice(&Sha256::digest(&self.hasher.result_reset()[..])[..]);
// Double hash
self.cv.copy_from_slice(&Sha256::digest(&self.hasher.finalize_reset()[..])[..]);
self.mode = Mode::RATCHETED;
}
@ -62,25 +63,25 @@ impl ShoApi for ShoSha256 {
}
let mut output = Vec::<u8>::new();
let mut output_hasher_prefix = Sha256::new();
output_hasher_prefix.input(&[0u8; BLOCK_LEN - 1][..]);
output_hasher_prefix.input(&[1u8]); // domain separator byte
output_hasher_prefix.input(self.cv);
output_hasher_prefix.update(&[0u8; BLOCK_LEN - 1]);
output_hasher_prefix.update(&[1u8]); // domain separator byte
output_hasher_prefix.update(self.cv);
let mut i = 0;
while i * HASH_LEN < outlen {
let mut output_hasher = output_hasher_prefix.clone();
output_hasher.input((i as u64).to_be_bytes());
let digest = output_hasher.result();
output_hasher.update((i as u64).to_be_bytes());
let digest = output_hasher.finalize();
let num_bytes = cmp::min(HASH_LEN, outlen - i * HASH_LEN);
output.extend_from_slice(&digest[0..num_bytes]);
i += 1
}
let mut next_hasher = Sha256::new();
next_hasher.input(&[0u8; BLOCK_LEN - 1][..]);
next_hasher.input(&[2u8]); // domain separator byte
next_hasher.input(self.cv);
next_hasher.input((outlen as u64).to_be_bytes());
self.cv.copy_from_slice(&next_hasher.result()[..]);
next_hasher.update(&[0u8; BLOCK_LEN - 1]);
next_hasher.update(&[2u8]); // domain separator byte
next_hasher.update(self.cv);
next_hasher.update((outlen as u64).to_be_bytes());
self.cv.copy_from_slice(&next_hasher.finalize()[..]);
self.mode = Mode::RATCHETED;
output
}
@ -96,12 +97,9 @@ mod tests {
let mut sho = ShoSha256::new(b"asd");
sho.absorb_and_ratchet(b"asdasd");
let out = sho.squeeze_and_ratchet(64);
/*
for b in out.iter() {
print!("0x{:02x}, ", b);
}
println!("");
*/
println!("{}", hex::encode(&out));
assert!(
out == vec![
0xeb, 0xe4, 0xef, 0x29, 0xe1, 0x8a, 0xa5, 0x41, 0x37, 0xed, 0xd8, 0x9c, 0x23, 0xf8,
@ -116,10 +114,7 @@ mod tests {
sho.absorb_and_ratchet(b"asdasd");
let out = sho.squeeze_and_ratchet(65);
/*
for b in out.iter() {
print!("0x{:02x}, ", b);
}
println!("");
println!("{}", hex::encode(&out));
*/
assert!(
out == vec![
@ -148,10 +143,7 @@ mod tests {
sho.absorb_and_ratchet(b"def");
let out = sho.squeeze_and_ratchet(63);
/*
for b in out.iter() {
print!("0x{:02x}, ", b);
}
println!("");
println!("{}", hex::encode(&out));
*/
assert!(
out == vec![