Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add hashing api on comms repo (see issue #4393) #4429

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions comms/core/src/peer_manager/hashing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2021, The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use digest::Digest;
use tari_crypto::{
hash_domain,
hashing::{DomainSeparatedHasher, LengthExtensionAttackResistant},
};

hash_domain!(
CommsCorePeerManagerDomain,
"com.tari.tari_project.comms.core.peer_manager",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"com.tari.tari_project.comms.core.peer_manager",
"com.tari.comms.core.peer_manager",

1
);

pub(crate) const IDENTITY_SIGNATURE: &str = "identity_signature";

pub(crate) fn comms_core_peer_manager_domain<D: Digest + LengthExtensionAttackResistant>(
label: &'static str,
) -> DomainSeparatedHasher<D, CommsCorePeerManagerDomain> {
DomainSeparatedHasher::<D, CommsCorePeerManagerDomain>::new_with_label(label)
}
18 changes: 10 additions & 8 deletions comms/core/src/peer_manager/identity_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
use std::convert::{TryFrom, TryInto};

use chrono::{DateTime, NaiveDateTime, Utc};
use digest::Digest;
use prost::Message;
use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
use tari_crypto::keys::PublicKey as PublicKeyTrait;
use tari_crypto::{hashing::DomainSeparatedHasher, keys::PublicKey as PublicKeyTrait};
use tari_utilities::ByteArray;

use super::hashing::{comms_core_peer_manager_domain, CommsCorePeerManagerDomain, IDENTITY_SIGNATURE};
use crate::{
message::MessageExt,
multiaddr::Multiaddr,
Expand Down Expand Up @@ -73,8 +73,9 @@ impl IdentitySignature {
features,
addresses,
updated_at,
);
let signature = Signature::sign(secret_key.clone(), secret_nonce, &challenge.finalize())
)
.finalize();
let signature = Signature::sign(secret_key.clone(), secret_nonce, challenge.as_ref())
.expect("unreachable panic: challenge hash digest is the correct length");
Self {
version: Self::LATEST_VERSION,
Expand Down Expand Up @@ -125,8 +126,9 @@ impl IdentitySignature {
features,
addresses,
self.updated_at,
);
self.signature.verify_challenge(public_key, &challenge.finalize())
)
.finalize();
self.signature.verify_challenge(public_key, challenge.as_ref())
}

fn construct_challenge<'a, I: IntoIterator<Item = &'a Multiaddr>>(
Expand All @@ -136,9 +138,9 @@ impl IdentitySignature {
features: PeerFeatures,
addresses: I,
updated_at: DateTime<Utc>,
) -> CommsChallenge {
) -> DomainSeparatedHasher<CommsChallenge, CommsCorePeerManagerDomain> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think we can return a [u8;32] out here - not sure why I returned the CommsChallenge type

// e = H(P||R||m)
let challenge = CommsChallenge::new()
let challenge = comms_core_peer_manager_domain::<CommsChallenge>(IDENTITY_SIGNATURE)
.chain(public_key.as_bytes())
.chain(public_nonce.as_bytes())
.chain(version.to_le_bytes())
Expand Down
2 changes: 2 additions & 0 deletions comms/core/src/peer_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub use error::PeerManagerError;
mod identity_signature;
pub use identity_signature::IdentitySignature;

mod hashing;

pub mod node_id;
pub use node_id::NodeId;

Expand Down
24 changes: 20 additions & 4 deletions comms/dht/src/dedup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ use digest::Digest;
use futures::{future::BoxFuture, task::Context};
use log::*;
use tari_comms::{pipeline::PipelineError, types::CommsChallenge};
use tari_crypto::{
hash_domain,
hashing::{DomainSeparatedHasher, LengthExtensionAttackResistant},
};
use tari_utilities::hex::Hex;
use tower::{layer::Layer, Service, ServiceExt};

Expand All @@ -42,17 +46,29 @@ use crate::{
};

const LOG_TARGET: &str = "comms::dht::dedup";
const DEDUP_MESSAGE_HASH_LABEL: &str = "dedup.meesage_hash";

hash_domain!(CommsDhtDedupDomain, "com.tari.tari_project.comms.dht", 1);

fn comms_dht_dedup_message_hash<D: Digest + LengthExtensionAttackResistant>(
label: &'static str,
) -> DomainSeparatedHasher<D, CommsDhtDedupDomain> {
DomainSeparatedHasher::<D, CommsDhtDedupDomain>::new_with_label(label)
}

pub fn hash_inbound_message(msg: &DhtInboundMessage) -> [u8; 32] {
create_message_hash(&msg.dht_header.message_signature, &msg.body)
}

pub fn create_message_hash(message_signature: &[u8], body: &[u8]) -> [u8; 32] {
CommsChallenge::new()
let result = comms_dht_dedup_message_hash::<CommsChallenge>(DEDUP_MESSAGE_HASH_LABEL)
.chain(message_signature)
.chain(&body)
.finalize()
.into()
.finalize();

let mut out = [0u8; 32];
out.copy_from_slice(result.as_ref());
out
}

/// # DHT Deduplication middleware
Expand Down Expand Up @@ -197,7 +213,7 @@ mod test {
#[test]
fn deterministic_hash() {
const TEST_MSG: &[u8] = b"test123";
const EXPECTED_HASH: &str = "90cccd774db0ac8c6ea2deff0e26fc52768a827c91c737a2e050668d8c39c224";
const EXPECTED_HASH: &str = "d6333668f259f677703fbe4e89152ee41c7c01f6dec502befc63120246523ffe";

let node_identity = make_node_identity();
let dht_message = make_dht_inbound_message(
Expand Down