-
Notifications
You must be signed in to change notification settings - Fork 407
Modular handshake #494
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
Modular handshake #494
Changes from all commits
eb6a371
b71b7ea
92eac9b
986f25f
ffbf5ec
19b7700
17fda75
8169b31
f1002c5
f0fc10b
eb297f9
5492717
256b6f5
b4921e9
299b6f7
944177a
0fbd895
6cf5a07
6f4e76a
c2227b6
6bae489
2df93ca
eda13bf
4e6b25a
f1940e9
4deb290
5e9c350
4b4cb98
029bb66
54b7464
fe705a9
be5e2a5
2e4e659
a4fff76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use util::byte_utils; | ||
use util::chacha20poly1305rfc::ChaCha20Poly1305RFC; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this in ln::peers? It seems to be pure crypto functions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the AEAD-based encryption methods are only used for handshakes and peer message encryption IIRC, and not for the onion construction. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, but its also a pure-crypto primitive. I guess my preference is for such things (even if it implements a lightning protocol crypto primitive) to be in some kind of crypto module. |
||
pub const TAG_SIZE: usize = 16; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like there's a few places where this can be used throughout the file. |
||
|
||
pub fn encrypt(key: &[u8], nonce: u64, associated_data: &[u8], plaintext: &[u8]) -> Vec<u8> { | ||
let mut nonce_bytes = [0; 12]; | ||
nonce_bytes[4..].copy_from_slice(&byte_utils::le64_to_array(nonce)); | ||
|
||
let mut chacha = ChaCha20Poly1305RFC::new(key, &nonce_bytes, associated_data); | ||
let mut ciphertext = vec![0u8; plaintext.len()]; | ||
let mut authentication_tag = [0u8; 16]; | ||
chacha.encrypt(plaintext, &mut ciphertext, &mut authentication_tag); | ||
|
||
let mut tagged_ciphertext = ciphertext; | ||
tagged_ciphertext.extend_from_slice(&authentication_tag); | ||
tagged_ciphertext | ||
} | ||
|
||
pub fn decrypt(key: &[u8], nonce: u64, associated_data: &[u8], tagged_ciphertext: &[u8]) -> Result<Vec<u8>, String> { | ||
let mut nonce_bytes = [0; 12]; | ||
nonce_bytes[4..].copy_from_slice(&byte_utils::le64_to_array(nonce)); | ||
|
||
let length = tagged_ciphertext.len(); | ||
if length < 16 { | ||
return Err("ciphertext cannot be shorter than tag length of 16 bytes".to_string()); | ||
} | ||
let end_index = length - 16; | ||
let ciphertext = &tagged_ciphertext[0..end_index]; | ||
let authentication_tag = &tagged_ciphertext[end_index..length]; | ||
|
||
let mut chacha = ChaCha20Poly1305RFC::new(key, &nonce_bytes, associated_data); | ||
let mut plaintext = vec![0u8; length - 16]; | ||
let success = chacha.decrypt(ciphertext, &mut plaintext, authentication_tag); | ||
if success { | ||
Ok(plaintext.to_vec()) | ||
} else { | ||
Err("invalid hmac".to_string()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this up one so its not in
ln
? If we're gonna put almost everything in one top-level module, it seems like we should just not have that module :).