Skip to content

Commit

Permalink
[refactor] hyperledger#3422: Use blake2 crate without going through ursa
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Strygin <dcnick3@users.noreply.github.com>
  • Loading branch information
DCNick3 committed Nov 9, 2023
1 parent 97ed585 commit 34c4819
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
13 changes: 12 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ serde = { workspace = true, features = ["derive"] }
serde_with = { workspace = true, features = ["macros"] }
hex = { workspace = true, features = ["alloc", "serde"] }
openssl-sys = { version = "0.9.93", features = ["vendored"], optional = true }
ursa = { workspace = true, optional = true }
getset = { workspace = true }

ursa = { workspace = true, optional = true }
blake2 = "0.10.6"

[dev-dependencies]
hex-literal = { workspace = true }
serde_json = { workspace = true }
16 changes: 8 additions & 8 deletions crypto/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
use alloc::{borrow::ToOwned as _, format, string::String, vec, vec::Vec};
use core::{hash, marker::PhantomData, num::NonZeroU8, str::FromStr};

#[cfg(all(feature = "std", not(feature = "ffi_import")))]
use blake2::{
digest::{Update, VariableOutput},
Blake2bVar,
};
use derive_more::{DebugCustom, Deref, DerefMut, Display};
#[cfg(any(feature = "std", feature = "ffi_import"))]
use iroha_macro::ffi_impl_opaque;
use iroha_schema::{IntoSchema, TypeId};
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
use serde_with::DeserializeFromStr;
#[cfg(all(feature = "std", not(feature = "ffi_import")))]
use ursa::blake2::{
digest::{Update, VariableOutput},
VarBlake2b,
};

use crate::{error::Error, hex_decode};

Expand Down Expand Up @@ -46,7 +46,7 @@ impl Hash {
/// Length of hash
pub const LENGTH: usize = 32;

/// Wrap the given bytes; they must be prehashed with `VarBlake2b`
/// Wrap the given bytes; they must be prehashed with `Blake2bVar`
pub fn prehashed(mut hash: [u8; Self::LENGTH]) -> Self {
hash[Self::LENGTH - 1] |= 1;
// SAFETY:
Expand All @@ -72,7 +72,7 @@ impl Hash {
// NOTE: Panic is predicated by implementation not user input
#[allow(clippy::missing_panics_doc)]
pub fn new(bytes: impl AsRef<[u8]>) -> Self {
let vec_hash = VarBlake2b::new(Self::LENGTH)
let vec_hash = Blake2bVar::new(Self::LENGTH)
.expect("Failed to initialize variable size hash")
.chain(bytes)
.finalize_boxed();
Expand Down Expand Up @@ -328,7 +328,7 @@ mod tests {
#[cfg(feature = "std")]
#[cfg(not(feature = "ffi_import"))]
fn blake2_32b() {
let mut hasher = VarBlake2b::new(32).unwrap();
let mut hasher = Blake2bVar::new(32).unwrap();
hasher.update(hex_literal::hex!("6920616d2064617461"));
hasher.finalize_variable(|res| {
assert_eq!(
Expand Down
3 changes: 3 additions & 0 deletions crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use core::{fmt, str::FromStr};

#[cfg(feature = "base64")]
pub use base64;
#[cfg(feature = "std")]
#[cfg(not(feature = "ffi_import"))]
pub use blake2;
use derive_more::{DebugCustom, Display};
use error::{Error, NoSuchAlgorithm};
use getset::{CopyGetters, Getters};
Expand Down
10 changes: 4 additions & 6 deletions p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
//! Cryptography are chosen in this module, and encapsulated.
use std::{io, net::AddrParseError};

use iroha_crypto::ursa::{
use iroha_crypto::{
blake2::{
digest::{Update, VariableOutput},
VarBlake2b,
Blake2bVar,
},
encryption::symm::prelude::ChaCha20Poly1305,
kex::x25519::X25519Sha256,
CryptoError,
ursa::{encryption::symm::prelude::ChaCha20Poly1305, kex::x25519::X25519Sha256, CryptoError},
};
pub use network::message::*;
use parity_scale_codec::{Decode, Encode};
Expand Down Expand Up @@ -157,7 +155,7 @@ pub(crate) mod unbounded_with_len {
/// Create Blake2b hash as u64 value
pub fn blake2b_hash(slice: impl AsRef<[u8]>) -> u64 {
const U64_SIZE: usize = core::mem::size_of::<u64>();
let hash = VarBlake2b::new(U64_SIZE)
let hash = Blake2bVar::new(U64_SIZE)
.expect("Failed to create hash with given length")
.chain(&slice)
.finalize_boxed();
Expand Down

0 comments on commit 34c4819

Please sign in to comment.