Skip to content

Add an implementation of mutual message exchange #2829

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ members = [
"lightning-rapid-gossip-sync",
"lightning-custom-message",
"lightning-transaction-sync",
"mutual-message-exchange",
"possiblyrandom",
]

Expand Down
1 change: 1 addition & 0 deletions ci/ci-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ WORKSPACE_MEMBERS=(
lightning-rapid-gossip-sync
lightning-custom-message
lightning-transaction-sync
mutual-message-exchange
possiblyrandom
)

Expand Down
11 changes: 4 additions & 7 deletions lightning/src/crypto/chacha20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,14 @@ mod real_chacha {
}

/// Get one block from a ChaCha stream.
pub fn get_single_block(key: &[u8; 32], nonce: &[u8; 16]) -> [u8; 32] {
pub fn get_single_block(key: &[u8; 32], nonce: &[u8; 16]) -> [u8; 64] {
let mut chacha = ChaCha20 {
state: ChaCha20::expand(key, nonce),
output: [0u8; BLOCK_SIZE],
offset: 64,
};
let mut chacha_bytes = [0; 32];
chacha.process_in_place(&mut chacha_bytes);
chacha_bytes
chacha.update();
chacha.output
}

/// Encrypts `src` into `dest` using a single block from a ChaCha stream. Passing `dest` as
Expand Down Expand Up @@ -342,8 +341,6 @@ pub use self::fuzzy_chacha::ChaCha20;
mod test {
use core::iter::repeat;

use crate::prelude::*;

use super::ChaCha20;

#[test]
Expand Down Expand Up @@ -587,7 +584,7 @@ mod test {
let mut chacha20 = ChaCha20::new(&key, nonce_12bytes);
// Seek its counter to the block at counter_pos.
chacha20.seek_to_block(u32::from_le_bytes(counter_pos.try_into().unwrap()));
let mut block_bytes = [0; 32];
let mut block_bytes = [0; 64];
chacha20.process_in_place(&mut block_bytes);

assert_eq!(ChaCha20::get_single_block(&key, &nonce_16bytes), block_bytes);
Expand Down
2 changes: 0 additions & 2 deletions lightning/src/crypto/poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

use core::cmp::min;

use crate::prelude::*;

#[derive(Clone, Copy)]
pub struct Poly1305 {
r: [u32; 5],
Expand Down
5 changes: 4 additions & 1 deletion lightning/src/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2507,7 +2507,10 @@ impl EntropySource for RandomBytes {
let index = self.index.next();
let mut nonce = [0u8; 16];
nonce[..8].copy_from_slice(&index.to_be_bytes());
ChaCha20::get_single_block(&self.seed, &nonce)
let block = ChaCha20::get_single_block(&self.seed, &nonce);
let mut half_block = [0; 32];
half_block.copy_from_slice(&block[..32]);
half_block
}
}

Expand Down
21 changes: 21 additions & 0 deletions mutual-message-exchange/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "mutual-message-exchange"
version = "0.1.0"
authors = ["Matt Corallo"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/lightningdevkit/rust-lightning/"
description = """
An implementation of a trivial mutual authentication scheme where a message can be exchanged iff two parties mutually trust each other (out of individual trusted-keys lists).
"""
edition = "2021"

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]

[features]

[dependencies]
bitcoin_hashes = { version = "^0.12.0", default-features = false }
secp256k1 = { version = "^0.27.0", default-features = false, features = ["alloc"] }

[dev-dependencies]
1 change: 1 addition & 0 deletions mutual-message-exchange/src/chacha20.rs
1 change: 1 addition & 0 deletions mutual-message-exchange/src/chacha20poly1305rfc.rs
Loading
Loading