From 7b142218c15ca9a40fac395313928d01865259c6 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Wed, 14 Sep 2022 14:42:28 +0200 Subject: [PATCH 1/3] Replace 'blake2-rfc with rust-crypto 'blake2' crate --- Cargo.lock | 2 +- Cargo.toml | 1 - primitives/core/Cargo.toml | 6 +++--- primitives/core/src/crypto.rs | 36 +++++++++++++++++++++++++---------- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 34d2296e019ce..85f743c312f1f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9732,7 +9732,7 @@ version = "6.0.0" dependencies = [ "base58", "bitflags", - "blake2-rfc", + "blake2", "byteorder", "criterion", "dyn-clonable", diff --git a/Cargo.toml b/Cargo.toml index 4dbf65dc7e1fe..cc5d0d64298da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -251,7 +251,6 @@ members = [ # This list is ordered alphabetically. [profile.dev.package] blake2 = { opt-level = 3 } -blake2-rfc = { opt-level = 3 } blake2b_simd = { opt-level = 3 } chacha20poly1305 = { opt-level = 3 } cranelift-codegen = { opt-level = 3 } diff --git a/primitives/core/Cargo.toml b/primitives/core/Cargo.toml index bf54f8467d1ef..cbbec37a870d5 100644 --- a/primitives/core/Cargo.toml +++ b/primitives/core/Cargo.toml @@ -48,7 +48,7 @@ bitflags = "1.3" # full crypto ed25519-zebra = { version = "3.0.0", default-features = false, optional = true} -blake2-rfc = { version = "0.2.18", default-features = false, optional = true } +blake2 = { version = "0.10.2", default-features = false, optional = true } schnorrkel = { version = "0.9.1", features = [ "preaudit_deprecated", "u64_backend", @@ -96,7 +96,7 @@ std = [ "hash-db/std", "sp-std/std", "serde", - "blake2-rfc/std", + "blake2/std", "ed25519-zebra", "hex/std", "base58", @@ -128,7 +128,7 @@ std = [ # For the regular wasm runtime builds this should not be used. full_crypto = [ "ed25519-zebra", - "blake2-rfc", + "blake2", "schnorrkel", "hex", "libsecp256k1", diff --git a/primitives/core/src/crypto.rs b/primitives/core/src/crypto.rs index 80b44449dbac1..377ae480edd17 100644 --- a/primitives/core/src/crypto.rs +++ b/primitives/core/src/crypto.rs @@ -132,9 +132,7 @@ impl DeriveJunction { let mut cc: [u8; JUNCTION_ID_LEN] = Default::default(); index.using_encoded(|data| { if data.len() > JUNCTION_ID_LEN { - let hash_result = blake2_rfc::blake2b::blake2b(JUNCTION_ID_LEN, &[], data); - let hash = hash_result.as_bytes(); - cc.copy_from_slice(hash); + cc.copy_from_slice(&sp_core_hashing::blake2_256(data)); } else { cc[0..data.len()].copy_from_slice(data); } @@ -292,7 +290,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray { } let hash = ss58hash(&data[0..body_len + prefix_len]); - let checksum = &hash.as_bytes()[0..CHECKSUM_LEN]; + let checksum = &hash[0..CHECKSUM_LEN]; if data[body_len + prefix_len..body_len + prefix_len + CHECKSUM_LEN] != *checksum { // Invalid checksum. return Err(PublicError::InvalidChecksum) @@ -333,7 +331,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray { }; v.extend(self.as_ref()); let r = ss58hash(&v); - v.extend(&r.as_bytes()[0..2]); + v.extend(&r[0..2]); v.to_base58() } @@ -366,11 +364,13 @@ pub trait Derive: Sized { const PREFIX: &[u8] = b"SS58PRE"; #[cfg(feature = "std")] -fn ss58hash(data: &[u8]) -> blake2_rfc::blake2b::Blake2bResult { - let mut context = blake2_rfc::blake2b::Blake2b::new(64); - context.update(PREFIX); - context.update(data); - context.finalize() +fn ss58hash(data: &[u8]) -> Vec { + use blake2::{Blake2b512, Digest}; + + let mut ctx = Blake2b512::new(); + ctx.update(PREFIX); + ctx.update(data); + ctx.finalize().to_vec() } /// Default prefix number @@ -1311,6 +1311,14 @@ mod tests { path: vec![DeriveJunction::soft("DOT")] }) ); + assert_eq!( + TestPair::from_string("hello world/0123456789012345678901234567890123456789", None), + Ok(TestPair::Standard { + phrase: "hello world".to_owned(), + password: None, + path: vec![DeriveJunction::soft("0123456789012345678901234567890123456789")] + }) + ); assert_eq!( TestPair::from_string("hello world//1", None), Ok(TestPair::Standard { @@ -1327,6 +1335,14 @@ mod tests { path: vec![DeriveJunction::hard("DOT")] }) ); + assert_eq!( + TestPair::from_string("hello world//0123456789012345678901234567890123456789", None), + Ok(TestPair::Standard { + phrase: "hello world".to_owned(), + password: None, + path: vec![DeriveJunction::hard("0123456789012345678901234567890123456789")] + }) + ); assert_eq!( TestPair::from_string("hello world//1/DOT", None), Ok(TestPair::Standard { From a010b4df3b0c949cbc16c73bfc05fd28c75543f5 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Thu, 15 Sep 2022 15:07:53 +0200 Subject: [PATCH 2/3] Bump blake2 to 0.10.4 --- Cargo.lock | 4 ++-- primitives/api/proc-macro/Cargo.toml | 2 +- primitives/core/Cargo.toml | 2 +- primitives/core/hashing/Cargo.toml | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 85f743c312f1f..2091cdd27d46e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -587,9 +587,9 @@ dependencies = [ [[package]] name = "blake2" -version = "0.10.2" +version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b94ba84325db59637ffc528bbe8c7f86c02c57cff5c0e2b9b00f9a851f42f309" +checksum = "b9cf849ee05b2ee5fba5e36f97ff8ec2533916700fc0758d40d92136a42f3388" dependencies = [ "digest 0.10.3", ] diff --git a/primitives/api/proc-macro/Cargo.toml b/primitives/api/proc-macro/Cargo.toml index 0a57be8d7a300..8acc15d6a0591 100644 --- a/primitives/api/proc-macro/Cargo.toml +++ b/primitives/api/proc-macro/Cargo.toml @@ -19,7 +19,7 @@ proc-macro = true quote = "1.0.10" syn = { version = "1.0.98", features = ["full", "fold", "extra-traits", "visit"] } proc-macro2 = "1.0.37" -blake2 = { version = "0.10.2", default-features = false } +blake2 = { version = "0.10.4", default-features = false } proc-macro-crate = "1.1.3" # Required for the doc tests diff --git a/primitives/core/Cargo.toml b/primitives/core/Cargo.toml index cbbec37a870d5..e272024b323d3 100644 --- a/primitives/core/Cargo.toml +++ b/primitives/core/Cargo.toml @@ -48,7 +48,7 @@ bitflags = "1.3" # full crypto ed25519-zebra = { version = "3.0.0", default-features = false, optional = true} -blake2 = { version = "0.10.2", default-features = false, optional = true } +blake2 = { version = "0.10.4", default-features = false, optional = true } schnorrkel = { version = "0.9.1", features = [ "preaudit_deprecated", "u64_backend", diff --git a/primitives/core/hashing/Cargo.toml b/primitives/core/hashing/Cargo.toml index d85e28d1b2e56..9eafcf6ccbd0d 100644 --- a/primitives/core/hashing/Cargo.toml +++ b/primitives/core/hashing/Cargo.toml @@ -13,7 +13,7 @@ documentation = "https://docs.rs/sp-core-hashing" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -blake2 = { version = "0.10.2", default-features = false } +blake2 = { version = "0.10.4", default-features = false } byteorder = { version = "1.3.2", default-features = false } digest = { version = "0.10.3", default-features = false } sha2 = { version = "0.10.2", default-features = false } From ce66b515151a532c02a4e05fccedf6705954bac2 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Thu, 15 Sep 2022 15:10:22 +0200 Subject: [PATCH 3/3] Restore optimization requirements for blake2-rfc --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index cc5d0d64298da..4dbf65dc7e1fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -251,6 +251,7 @@ members = [ # This list is ordered alphabetically. [profile.dev.package] blake2 = { opt-level = 3 } +blake2-rfc = { opt-level = 3 } blake2b_simd = { opt-level = 3 } chacha20poly1305 = { opt-level = 3 } cranelift-codegen = { opt-level = 3 }