Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 480
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
Uh oh!
There was an error while loading. Please reload this page.
Modular handshake #494
Changes from all commits
eb6a371b71b7ea92eac9b986f25fffbf5ec19b770017fda758169b31f1002c5f0fc10beb297f95492717256b6f5b4921e9299b6f7944177a0fbd8956cf5a076f4e76ac2227b66bae4892df93caeda13bf4e6b25af1940e94deb2905e9c3504b4cb98029bb6654b7464fe705a9be5e2a52e4e659a4fff76File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| use util::byte_utils; | ||
| use util::chacha20poly1305rfc::ChaCha20Poly1305RFC; | ||
Collaborator 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. ContributorAuthor 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. Collaborator 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; | ||
Contributor 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()) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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 :).