-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from Revertron/feature/encryption
Implemented P2P traffic encryption.
- Loading branch information
Showing
16 changed files
with
2,159 additions
and
478 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "alfis" | ||
version = "0.5.9" | ||
version = "0.6.0" | ||
authors = ["Revertron <[email protected]>"] | ||
edition = "2018" | ||
build = "build.rs" | ||
|
@@ -18,14 +18,17 @@ toml = "0.5.8" | |
digest = "0.9.0" | ||
sha2 = "0.9.5" | ||
ed25519-dalek = "1.0.1" | ||
x25519-dalek = "1.1.1" | ||
ecies-ed25519 = "0.5.1" | ||
chacha20poly1305 = "0.8.0" | ||
signature = "1.3.0" | ||
blakeout = "0.3.0" | ||
num_cpus = "1.13.0" | ||
byteorder = "1.4.3" | ||
serde = { version = "1.0.126", features = ["derive"] } | ||
serde_json = "1.0.64" | ||
bincode = "1.3.3" | ||
serde_cbor = "0.11.1" | ||
base64 = "0.13.0" | ||
num-bigint = "0.4.0" | ||
num-traits = "0.2.14" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use chacha20poly1305::{ChaCha20Poly1305, Key, Nonce}; | ||
use chacha20poly1305::aead::{Aead, NewAead}; | ||
use std::fmt::{Debug, Formatter}; | ||
use std::fmt; | ||
|
||
pub const ZERO_NONCE: [u8; 12] = [0u8; 12]; | ||
const FAILURE: &str = "encryption failure!"; | ||
|
||
/// A small wrap-up to use Chacha20 encryption for domain names. | ||
#[derive(Clone)] | ||
pub struct Chacha { | ||
cipher: ChaCha20Poly1305, | ||
nonce: [u8; 12] | ||
} | ||
|
||
impl Chacha { | ||
pub fn new(key: &[u8], nonce: &[u8]) -> Self { | ||
let key = Key::from_slice(key); | ||
let cipher = ChaCha20Poly1305::new(key); | ||
let mut buf = [0u8; 12]; | ||
buf.copy_from_slice(nonce); | ||
Chacha { cipher, nonce: buf } | ||
} | ||
|
||
pub fn encrypt(&self, data: &[u8]) -> Vec<u8> { | ||
let nonce = Nonce::from(self.nonce.clone()); | ||
self.cipher.encrypt(&nonce, data.as_ref()).expect(FAILURE) | ||
} | ||
|
||
pub fn decrypt(&self, data: &[u8]) -> Vec<u8> { | ||
let nonce = Nonce::from(self.nonce.clone()); | ||
self.cipher.decrypt(&nonce, data.as_ref()).expect(FAILURE) | ||
} | ||
|
||
pub fn get_nonce(&self) -> &[u8; 12] { | ||
&self.nonce | ||
} | ||
} | ||
|
||
impl Debug for Chacha { | ||
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { | ||
fmt.write_str("ChaCha20Poly1305") | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::crypto::Chacha; | ||
use crate::{to_hex}; | ||
|
||
#[test] | ||
pub fn test_chacha() { | ||
let buf = b"178135D209C697625E3EC71DA5C760382E54936F824EE5083908DA66B14ECE18"; | ||
let chacha1 = Chacha::new(b"178135D209C697625E3EC71DA5C76038", &buf[..12]); | ||
let bytes1 = chacha1.encrypt(b"TEST"); | ||
println!("{}", to_hex(&bytes1)); | ||
|
||
let chacha2 = Chacha::new(b"178135D209C697625E3EC71DA5C76038", &buf[..12]); | ||
let bytes2 = chacha2.decrypt(&bytes1); | ||
assert_eq!(String::from_utf8(bytes2).unwrap(), "TEST"); | ||
|
||
let bytes2 = chacha2.encrypt(b"TEST"); | ||
|
||
assert_eq!(bytes1, bytes2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
mod crypto_box; | ||
mod chacha; | ||
|
||
pub use crypto_box::CryptoBox; | ||
pub use crypto_box::CryptoBox; | ||
pub use chacha::Chacha; | ||
pub use chacha::ZERO_NONCE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.