Added second keypair for encryption of domain names.

Changed keys file format to include second pair of keys, it will be in TOML now.
Made many adjustments to block and transaction structures.
Changed block serialization to binary format for hashing/mining.
Removed old build dependencies.
This commit is contained in:
Revertron
2021-05-04 16:47:03 +02:00
parent 52695e0988
commit bc6d2fbae3
19 changed files with 349 additions and 249 deletions
-67
View File
@@ -1,67 +0,0 @@
use chacha20poly1305::{ChaCha20Poly1305, Key, Nonce};
use chacha20poly1305::aead::{Aead, NewAead};
use std::fmt::{Debug, Formatter};
use std::fmt;
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
/// A small wrap-up to use Chacha20 encryption for domain names.
#[derive(Clone)]
pub struct Chacha {
pub cipher: ChaCha20Poly1305
}
impl Chacha {
pub fn new(seed: &[u8]) -> Self {
let key = Key::from_slice(seed);
let cipher = ChaCha20Poly1305::new(key);
Chacha { cipher }
}
pub fn encrypt(&self, data: &[u8], nonce: &[u8]) -> Vec<u8> {
let nonce = Nonce::from_slice(nonce);
match self.cipher.encrypt(nonce, data.as_ref()) {
Ok(bytes) => { bytes }
Err(_) => {
warn!("Error encrypting data!");
Vec::new()
}
}
}
pub fn decrypt(&self, data: &[u8], nonce: &[u8]) -> Vec<u8> {
let nonce = Nonce::from_slice(nonce);
match self.cipher.decrypt(nonce, data.as_ref()) {
Ok(bytes) => { bytes }
Err(_) => {
warn!("Error decrypting data!");
Vec::new()
}
}
}
}
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_curved_chacha() {
let buf = b"178135D209C697625E3EC71DA5C760382E54936F824EE5083908DA66B14ECE18";
let keys1 = Chacha::new(b"178135D209C697625E3EC71DA5C76038", );
let bytes = keys1.encrypt(b"TEST", &buf[..12]);
println!("{}", to_hex(&bytes));
let keys2 = Chacha::new(b"178135D209C697625E3EC71DA5C76038");
let bytes2 = keys2.decrypt(&bytes, &buf[..12]);
assert_eq!(String::from_utf8(bytes2).unwrap(), "TEST");
}
}
+88
View File
@@ -0,0 +1,88 @@
use ecies_ed25519::{SecretKey, PublicKey, Error, encrypt, decrypt};
use rand_old::{CryptoRng, RngCore};
use std::fmt::{Debug, Formatter};
use crate::{to_hex, from_hex};
use std::fmt;
pub struct CryptoBox {
pub(crate) secret: SecretKey,
pub(crate) public: PublicKey,
}
impl CryptoBox {
pub fn new(seed: &[u8]) -> Self {
let secret = SecretKey::from_bytes(seed).expect("Unable to parse secret key");
let public = PublicKey::from_secret(&secret);
Self { secret, public }
}
pub fn generate<R>(csprng: &mut R) -> Self where R: CryptoRng + RngCore {
let (secret, public) = ecies_ed25519::generate_keypair(csprng);
Self { secret, public }
}
pub fn from_strings(secret: &str, public: &str) -> Self {
let secret = SecretKey::from_bytes(&from_hex(secret).unwrap()).unwrap();
let public = PublicKey::from_bytes(&from_hex(public).unwrap()).unwrap();
Self { secret, public }
}
pub fn hide(&self, msg: &[u8]) -> Result<Vec<u8>, Error> {
let mut random = rand_old::thread_rng();
encrypt(&self.public, msg, &mut random)
}
pub fn reveal(&self, msg: &[u8]) -> Result<Vec<u8>, Error> {
decrypt(&self.secret, msg)
}
pub fn encrypt(public: &[u8], message: &[u8]) -> Result<Vec<u8>, Error> {
let public = PublicKey::from_bytes(public).unwrap();
let mut random = rand_old::thread_rng();
encrypt(&public, message, &mut random)
}
pub fn decrypt(secret: &[u8], message: &[u8]) -> Result<Vec<u8>, Error> {
let secret = SecretKey::from_bytes(secret).unwrap();
decrypt(&secret, &message)
}
}
impl Debug for CryptoBox {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("CryptoBox")
.field("public", &to_hex(&self.public.to_bytes()))
.finish()
}
}
impl Clone for CryptoBox {
fn clone(&self) -> Self {
let secret = SecretKey::from_bytes(&self.secret.as_bytes()[..]).expect("Unable clone secret key");
let public = PublicKey::from_secret(&secret);
Self { secret, public }
}
}
#[cfg(test)]
mod tests {
use rand::RngCore;
use crate::{to_hex, from_hex};
use ed25519_dalek::Keypair;
use crate::crypto::CryptoBox;
use ecies_ed25519::{encrypt, decrypt, SecretKey, PublicKey};
const TEXT: &str = "Some very secret message";
#[test]
pub fn hide_reveal() {
let mut rng = rand::thread_rng();
let mut buf = [0u8; 32];
rng.fill_bytes(&mut buf);
let coder = CryptoBox::new(&buf);
let encrypted = coder.hide(TEXT.as_bytes()).unwrap();
let decrypted = coder.reveal(&encrypted.as_slice()).unwrap();
assert_eq!(TEXT, &String::from_utf8(decrypted).unwrap());
}
}
+2 -2
View File
@@ -1,3 +1,3 @@
mod chacha;
mod crypto_box;
pub use chacha::Chacha;
pub use crypto_box::CryptoBox;