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: address points in issue #4138 and companions #4336

Merged
merged 24 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2f10996
address points in issue #4138 and companions
jorgeantonio21 Jul 22, 2022
a103aba
Merge branch 'development' into ja-derived-keys
jorgeantonio21 Jul 22, 2022
33da112
correct bugs in chacha20poly1305 utilization
jorgeantonio21 Jul 22, 2022
0b17607
address domain separation regarding challenge generation for mac origin
jorgeantonio21 Jul 22, 2022
da661f8
run cargo fmt
jorgeantonio21 Jul 22, 2022
aee19eb
add tests for failure modes of new authentication encryption for key …
jorgeantonio21 Jul 22, 2022
b6e29d5
renaming
jorgeantonio21 Jul 22, 2022
95a9948
run cargo fmt
jorgeantonio21 Jul 22, 2022
ed61369
clippy: too many lines
jorgeantonio21 Jul 22, 2022
20b9323
rename origin_mac to message_signature
jorgeantonio21 Jul 24, 2022
58c5e5a
run cargo fmt
jorgeantonio21 Jul 24, 2022
cc743e3
add output type for ecdh exchange
jorgeantonio21 Jul 25, 2022
5600252
add new use of hashing API
jorgeantonio21 Jul 25, 2022
c2bb431
merge message-challenge-with-api branch
jorgeantonio21 Jul 26, 2022
2fa7515
add changes
jorgeantonio21 Jul 26, 2022
27f1ab6
add generic constant length array size for generate_ecdh_secret method
jorgeantonio21 Jul 26, 2022
ea94992
add minor changes
jorgeantonio21 Jul 26, 2022
c3a1d0a
Merge branch 'development' into ja-derived-keys
jorgeantonio21 Jul 27, 2022
52d8e8d
update tari-crypto tag version on cargo.toml
jorgeantonio21 Jul 27, 2022
0801a20
merge development
jorgeantonio21 Jul 28, 2022
065620a
refactor to use of hash_domain! macro
jorgeantonio21 Jul 28, 2022
c06b98c
remove generic from ecdh excahnge method
jorgeantonio21 Jul 28, 2022
c13ace2
Merge branch 'development' into ja-derived-keys
jorgeantonio21 Jul 28, 2022
b89f8af
Merge branch 'development' into ja-derived-keys
aviator-app[bot] Jul 28, 2022
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
99 changes: 97 additions & 2 deletions comms/dht/src/crypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ pub fn decrypt_with_chacha20_poly1305(
let nonce_ga = chacha20poly1305::Nonce::from_slice(&nonce);

let cipher = ChaCha20Poly1305::new(&cipher_key.0);
let decrypted_signature = cipher.decrypt(nonce_ga, cipher_signature).map_err(|_| DhtOutboundError::CipherError(String::from("Authenticated decryption failed")))?;
let decrypted_signature = cipher
.decrypt(nonce_ga, cipher_signature)
.map_err(|_| DhtOutboundError::CipherError(String::from("Authenticated decryption failed")))?;

Ok(decrypted_signature)
}
Expand Down Expand Up @@ -158,10 +160,11 @@ pub fn encrypt_with_chacha20_poly1305(
let nonce_ga = chacha20poly1305::Nonce::from_slice(&nonce);
let cipher = ChaCha20Poly1305::new(&cipher_key.0);

// length of encrypted equals signature.len() + 16 (the latter being the tag size for ChaCha20-poly1305)
let encrypted = cipher
.encrypt(nonce_ga, signature)
.map_err(|_| DhtOutboundError::CipherError(String::from("Authenticated encryption failed")))?;

Ok(encrypted)
}

Expand Down Expand Up @@ -248,4 +251,96 @@ mod test {
let secret_msg = "Last enemy position 0830h AJ 9863".as_bytes().to_vec();
assert_eq!(plain_text, secret_msg);
}

#[test]
fn sanity_check() {
let domain_separated_hash =
DomainSeparatedHasher::<Blake256, GenericHashDomain>::new(DOMAIN_SEPARATION_KEY_SIGNATURE_LABEL)
.chain(&[10, 12, 13, 82, 93, 101, 87, 28, 27, 17, 11, 35, 43])
.finalize()
.into_vec();

// Domain separation uses Challenge = Blake256, thus its output has 32-byte length
let key = AuthenticatedCipherKey(*chacha20poly1305::Key::from_slice(domain_separated_hash.as_bytes()));

let signature = b"Top secret message, handle with care".as_slice();
let n = signature.len();
let nonce = [0u8; size_of::<chacha20poly1305::Nonce>()];

let nonce_ga = chacha20poly1305::Nonce::from_slice(&nonce);
let cipher = ChaCha20Poly1305::new(&key.0);

let encrypted = cipher
.encrypt(nonce_ga, signature)
.map_err(|_| DhtOutboundError::CipherError(String::from("Authenticated encryption failed")))
.unwrap();

assert_eq!(encrypted.len(), n + 16);
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could remove the magic number by getting the tag size programmatically.

}

#[test]
fn decryption_fails_in_case_tag_is_manipulated() {
let (sk, pk) = CommsPublicKey::random_keypair(&mut OsRng);
let key_data = generate_ecdh_secret(&sk, &pk);
let key = generate_key_signature_for_authenticated_encryption(&key_data);

let signature = b"Top secret message, handle with care".as_slice();

let mut encrypted = encrypt_with_chacha20_poly1305(&key, signature).unwrap();

// sanity check to validate that encrypted.len() = signature.len() + 16
assert_eq!(encrypted.len(), signature.len() + 16);

// manipulate tag and check that decryption fails
let n = encrypted.len();
encrypted[n - 1] += 1;

// decryption should fail
assert!(decrypt_with_chacha20_poly1305(&key, encrypted.as_slice())
.unwrap_err()
.to_string()
.contains("Authenticated decryption failed"));
}

#[test]
fn decryption_fails_in_case_body_message_is_manipulated() {
let (sk, pk) = CommsPublicKey::random_keypair(&mut OsRng);
let key_data = generate_ecdh_secret(&sk, &pk);
let key = generate_key_signature_for_authenticated_encryption(&key_data);

let signature = b"Top secret message, handle with care".as_slice();

let mut encrypted = encrypt_with_chacha20_poly1305(&key, signature).unwrap();

// manipulate encrypted message body and check that decryption fails
encrypted[0] += 1;

// decryption should fail
assert!(decrypt_with_chacha20_poly1305(&key, encrypted.as_slice())
.unwrap_err()
.to_string()
.contains("Authenticated decryption failed"));
}

#[test]
fn decryption_fails_if_message_sned_to_incorrect_node() {
let (sk, pk) = CommsPublicKey::random_keypair(&mut OsRng);
let (other_sk, other_pk) = CommsPublicKey::random_keypair(&mut OsRng);

let key_data = generate_ecdh_secret(&sk, &pk);
let other_key_data = generate_ecdh_secret(&other_sk, &other_pk);

let key = generate_key_signature_for_authenticated_encryption(&key_data);
let other_key = generate_key_signature_for_authenticated_encryption(&other_key_data);

let signature = b"Top secret message, handle with care".as_slice();

let encrypted = encrypt_with_chacha20_poly1305(&key, signature).unwrap();

// decryption should fail
assert!(decrypt_with_chacha20_poly1305(&other_key, encrypted.as_slice())
.unwrap_err()
.to_string()
.contains("Authenticated decryption failed"));
}
}
3 changes: 2 additions & 1 deletion comms/dht/src/store_forward/saf_handler/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,8 @@ mod test {
false,
MessageTag::new(),
false,
).unwrap();
)
.unwrap();

let since = Utc::now().checked_sub_signed(chrono::Duration::seconds(60)).unwrap();
let mut message = DecryptedDhtMessage::succeeded(
Expand Down