From 3ae4c755693ab9eadb778d6cff87c0beba1f811b Mon Sep 17 00:00:00 2001 From: Quentin Le Sceller Date: Tue, 2 Nov 2021 11:40:31 -0400 Subject: [PATCH] Update digest, hmac, ripemd160, sha2 and pbkdf2 crates (#3655) --- Cargo.lock | 132 ++++++++++++++++++++--------------- keychain/Cargo.toml | 10 +-- keychain/src/base58.rs | 8 +-- keychain/src/extkey_bip32.rs | 31 ++++---- keychain/src/mnemonic.rs | 8 +-- 5 files changed, 103 insertions(+), 86 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a2ee9c76d..c2c433a27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "addr2line" version = "0.12.1" @@ -123,16 +125,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "base64" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" -dependencies = [ - "byteorder", - "safemem", -] - [[package]] name = "base64" version = "0.11.0" @@ -145,6 +137,12 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d1ccbaf7d9ec9537465a97bf19edc1a4e158ecb49fc16178202238c569cc42" +[[package]] +name = "base64ct" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a32fd6af2b5827bce66c29053ba0e7c42b9dcab01835835058558c10851a46b" + [[package]] name = "bindgen" version = "0.56.0" @@ -209,12 +207,11 @@ dependencies = [ [[package]] name = "block-buffer" -version = "0.3.3" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a076c298b9ecdb530ed9d967e74a6027d6a7478924520acddcddc24c1c8ab3ab" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" dependencies = [ - "arrayref", - "byte-tools", + "generic-array", ] [[package]] @@ -233,12 +230,6 @@ version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" -[[package]] -name = "byte-tools" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" - [[package]] name = "byteorder" version = "1.3.4" @@ -363,6 +354,15 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac" +[[package]] +name = "cpufeatures" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66c99696f6c9dd7f35d486b9d04d7e6e202aa3e8c40d553f2fdf5e7e0c6a71ef" +dependencies = [ + "libc", +] + [[package]] name = "crc32fast" version = "1.2.0" @@ -428,12 +428,12 @@ dependencies = [ [[package]] name = "crypto-mac" -version = "0.6.2" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7afa06d05a046c7a47c3a849907ec303504608c927f4e85f7bfff22b7180d971" +checksum = "b1d1a86f49236c215f271d40892d5fc950490551400b02ef360692c29815c714" dependencies = [ - "constant_time_eq", "generic-array", + "subtle", ] [[package]] @@ -561,9 +561,9 @@ checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" [[package]] name = "digest" -version = "0.7.6" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03b072242a8cbaf9c145665af9d250c59af3b958f83ed6824e13533cf76d5b90" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" dependencies = [ "generic-array", ] @@ -698,12 +698,6 @@ dependencies = [ "synstructure 0.12.4", ] -[[package]] -name = "fake-simd" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" - [[package]] name = "filetime" version = "0.2.10" @@ -875,11 +869,12 @@ checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" [[package]] name = "generic-array" -version = "0.9.0" +version = "0.14.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" +checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" dependencies = [ "typenum", + "version_check", ] [[package]] @@ -1246,9 +1241,9 @@ dependencies = [ [[package]] name = "hmac" -version = "0.6.3" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "733e1b3ac906631ca01ebb577e9bb0f5e37a454032b9036b5eaea4013ed6f99a" +checksum = "2a2a2320eb7ec0ebe8da8f744d7812d9fc4cb4d09344ac01898dbcb6a20ae69b" dependencies = [ "crypto-mac", "digest", @@ -1458,9 +1453,9 @@ checksum = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f" [[package]] name = "libc" -version = "0.2.71" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" +checksum = "a7f823d141fe0a24df1e23b4af4e3c7ba9e5966ec514ea068c93024aa7deb765" [[package]] name = "libgit2-sys" @@ -1882,6 +1877,12 @@ version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + [[package]] name = "openssl-probe" version = "0.1.2" @@ -1953,18 +1954,26 @@ dependencies = [ ] [[package]] -name = "pbkdf2" -version = "0.2.3" +name = "password-hash" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c09cddfbfc98de7f76931acf44460972edb4023eb14d0c6d4018800e552d8e0" +checksum = "fd482dfb8cfba5a93ec0f91e1c0f66967cb2fdc1a8dba646c4f9202c5d05d785" dependencies = [ - "base64 0.9.3", - "byteorder", - "constant_time_eq", + "base64ct", + "rand_core 0.6.3", + "subtle", +] + +[[package]] +name = "pbkdf2" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d95f5254224e617595d2cc3cc73ff0a5eaf2637519e25f03388154e9378b6ffa" +dependencies = [ + "base64ct", "crypto-mac", - "generic-array", "hmac", - "rand 0.5.6", + "password-hash", "sha2", ] @@ -2215,6 +2224,12 @@ dependencies = [ "getrandom 0.1.14", ] +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" + [[package]] name = "rand_hc" version = "0.1.0" @@ -2356,13 +2371,13 @@ dependencies = [ [[package]] name = "ripemd160" -version = "0.7.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "482aa56cc68aaeccdaaff1cc5a72c247da8bbad3beb174ca5741f274c22883fb" +checksum = "2eca4ecc81b7f313189bf73ce724400a07da2a6dac19588b03c8bd76a2dcc251" dependencies = [ "block-buffer", - "byte-tools", "digest", + "opaque-debug", ] [[package]] @@ -2426,12 +2441,6 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" -[[package]] -name = "safemem" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" - [[package]] name = "same-file" version = "1.0.6" @@ -2561,14 +2570,15 @@ dependencies = [ [[package]] name = "sha2" -version = "0.7.1" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9eb6be24e4c23a84d7184280d2722f7f2731fcdd4a9d886efbfe4413e4847ea0" +checksum = "b362ae5752fd2137731f9fa25fd4d9058af34666ca1966fb969119cc35719f12" dependencies = [ "block-buffer", - "byte-tools", + "cfg-if 1.0.0", + "cpufeatures", "digest", - "fake-simd", + "opaque-debug", ] [[package]] @@ -2650,6 +2660,12 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" +[[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + [[package]] name = "supercow" version = "0.1.0" diff --git a/keychain/Cargo.toml b/keychain/Cargo.toml index 996ecd289..5a2f06650 100644 --- a/keychain/Cargo.toml +++ b/keychain/Cargo.toml @@ -20,10 +20,10 @@ serde_json = "1" lazy_static = "1" zeroize = { version = "1.1", features =["zeroize_derive"] } -digest = "0.7" -hmac = "0.6" -ripemd160 = "0.7" -sha2 = "0.7" -pbkdf2 = "0.2" +digest = "0.9" +hmac = "0.11" +ripemd160 = "0.9" +sha2 = "0.9" +pbkdf2 = "0.8" grin_util = { path = "../util", version = "5.2.0-alpha.1" } diff --git a/keychain/src/base58.rs b/keychain/src/base58.rs index 15f4d6a7f..4398befec 100644 --- a/keychain/src/base58.rs +++ b/keychain/src/base58.rs @@ -38,11 +38,11 @@ use byteorder::{ByteOrder, LittleEndian}; fn sha256d_hash(data: &[u8]) -> [u8; 32] { let mut ret = [0; 32]; let mut sha2 = Sha256::new(); - sha2.input(data); - ret.copy_from_slice(sha2.result().as_slice()); + sha2.update(data); + ret.copy_from_slice(sha2.finalize().as_slice()); sha2 = Sha256::new(); - sha2.input(&ret); - ret.copy_from_slice(sha2.result().as_slice()); + sha2.update(&ret); + ret.copy_from_slice(sha2.finalize().as_slice()); ret } diff --git a/keychain/src/extkey_bip32.rs b/keychain/src/extkey_bip32.rs index 321852c26..7b78337bd 100644 --- a/keychain/src/extkey_bip32.rs +++ b/keychain/src/extkey_bip32.rs @@ -44,7 +44,7 @@ use byteorder::{BigEndian, ByteOrder, ReadBytesExt}; use digest::generic_array::GenericArray; use digest::Digest; -use hmac::{Hmac, Mac}; +use hmac::{Hmac, Mac, NewMac}; use ripemd160::Ripemd160; use sha2::{Sha256, Sha512}; @@ -124,28 +124,28 @@ impl BIP32Hasher for BIP32GrinHasher { b"IamVoldemort".to_owned() } fn init_sha512(&mut self, seed: &[u8]) { - self.hmac_sha512 = HmacSha512::new_varkey(seed).expect("HMAC can take key of any size"); + self.hmac_sha512 = HmacSha512::new_from_slice(seed).expect("HMAC can take key of any size"); } fn append_sha512(&mut self, value: &[u8]) { - self.hmac_sha512.input(value); + self.hmac_sha512.update(value); } fn result_sha512(&mut self) -> [u8; 64] { let mut result = [0; 64]; - result.copy_from_slice(self.hmac_sha512.result().code().as_slice()); + result.copy_from_slice(&self.hmac_sha512.to_owned().finalize().into_bytes()); result } fn sha_256(&self, input: &[u8]) -> [u8; 32] { let mut sha2_res = [0; 32]; let mut sha2 = Sha256::new(); - sha2.input(input); - sha2_res.copy_from_slice(sha2.result().as_slice()); + sha2.update(input); + sha2_res.copy_from_slice(sha2.finalize().as_slice()); sha2_res } fn ripemd_160(&self, input: &[u8]) -> [u8; 20] { let mut ripemd_res = [0; 20]; let mut ripemd = Ripemd160::new(); - ripemd.input(input); - ripemd_res.copy_from_slice(ripemd.result().as_slice()); + ripemd.update(input); + ripemd_res.copy_from_slice(ripemd.finalize().as_slice()); ripemd_res } } @@ -701,28 +701,29 @@ mod tests { b"Bitcoin seed".to_owned() } fn init_sha512(&mut self, seed: &[u8]) { - self.hmac_sha512 = HmacSha512::new_varkey(seed).expect("HMAC can take key of any size"); + self.hmac_sha512 = + HmacSha512::new_from_slice(seed).expect("HMAC can take key of any size"); } fn append_sha512(&mut self, value: &[u8]) { - self.hmac_sha512.input(value); + self.hmac_sha512.update(value); } fn result_sha512(&mut self) -> [u8; 64] { let mut result = [0; 64]; - result.copy_from_slice(self.hmac_sha512.result().code().as_slice()); + result.copy_from_slice(&self.hmac_sha512.to_owned().finalize().into_bytes()); result } fn sha_256(&self, input: &[u8]) -> [u8; 32] { let mut sha2_res = [0; 32]; let mut sha2 = Sha256::new(); - sha2.input(input); - sha2_res.copy_from_slice(sha2.result().as_slice()); + sha2.update(input); + sha2_res.copy_from_slice(sha2.finalize().as_slice()); sha2_res } fn ripemd_160(&self, input: &[u8]) -> [u8; 20] { let mut ripemd_res = [0; 20]; let mut ripemd = Ripemd160::new(); - ripemd.input(input); - ripemd_res.copy_from_slice(ripemd.result().as_slice()); + ripemd.update(input); + ripemd_res.copy_from_slice(ripemd.finalize().as_slice()); ripemd_res } } diff --git a/keychain/src/mnemonic.rs b/keychain/src/mnemonic.rs index 75cad8594..9d87d6c9d 100644 --- a/keychain/src/mnemonic.rs +++ b/keychain/src/mnemonic.rs @@ -99,8 +99,8 @@ pub fn to_entropy(mnemonic: &str) -> Result, Error> { let mut hash = [0; 32]; let mut sha2sum = Sha256::default(); - sha2sum.input(&entropy); - hash.copy_from_slice(sha2sum.result().as_slice()); + sha2sum.update(&entropy); + hash.copy_from_slice(sha2sum.finalize().as_slice()); let actual = (hash[0] >> (8 - checksum_bits)) & mask; @@ -124,8 +124,8 @@ pub fn from_entropy(entropy: &[u8]) -> Result { let mut hash = [0; 32]; let mut sha2sum = Sha256::default(); - sha2sum.input(entropy); - hash.copy_from_slice(sha2sum.result().as_slice()); + sha2sum.update(entropy); + hash.copy_from_slice(sha2sum.finalize().as_slice()); let checksum = (hash[0] >> 8 - checksum_bits) & mask;