From 34ba5f683ffb74c1264abbc8669254eadc34c79b Mon Sep 17 00:00:00 2001 From: ennucore Date: Thu, 18 Nov 2021 20:51:10 +0300 Subject: [PATCH] Add crypto.rs with todos and tests --- src/crypto.rs | 96 ++++++++++++++++++++++++++++++--------------------- src/lib.rs | 2 ++ 2 files changed, 59 insertions(+), 39 deletions(-) diff --git a/src/crypto.rs b/src/crypto.rs index 602704f..cff1a90 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -1,63 +1,81 @@ -extern crate rand; - -use crate::ed25519_dalek::ed25519::signature::Signature; -use crate::ed25519_dalek::Signer; -use crate::ed25519_dalek::Verifier; use alloc::vec::Vec; -use ed25519_dalek::{Keypair, PublicKey as PK}; -use rand::rngs::OsRng; use serde::{Deserialize, Serialize}; +/// Public key of a node #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] pub struct PublicKey { pub key: [u8; 32], } impl PublicKey { - pub fn verify_sign(&self, raw: &Vec, sign: &Vec) -> bool { - PK::from_bytes(&self.key) - .unwrap() - .verify( - raw, - &Signature::from_bytes(sign.clone().into_boxed_slice().as_ref()).unwrap(), - ) - .is_ok() + /// Check if the sign is valid for gived data and key + pub fn verify_sign(&self, _data: &[u8], _sign: &[u8]) -> bool { + todo!() } -} -pub struct Keys { - pub pair: Keypair, -} - -impl Keys { - pub fn gen() -> Keys { - Keys { - pair: Keypair::generate(&mut OsRng {}), - } + /// Encrypt some data for a user with this public key + pub fn encrypt_data(&self, _data: &[u8]) -> Vec { + todo!() } } -#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] -pub enum Sign { - NoSign, - Signed(PublicKey /* source */, Vec /* sign */), -} +/// Key pair (public and secret) for a node, should be stored locally +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Keys {} -pub fn verify_sign(data: &Vec, sign: &Sign) -> bool { - match sign { - Sign::NoSign => true, - Sign::Signed(key, sign) => key.verify_sign(data, sign), +impl Keys { + /// Generate new random key + pub fn generate() -> Keys { + todo!() } } impl Keys { - pub fn sign(&self, content: &Vec) -> Vec { - self.pair.sign(content.as_ref()).to_bytes().to_vec() + /// Sign content using these keys + pub fn sign(&self, _content: &[u8]) -> Vec { + todo!() + } + + /// Decrypt data + pub fn decrypt_data(&self, _data_encrypted: &[u8]) -> Option> { + todo!() } + /// Get public key pub fn get_public(&self) -> PublicKey { - PublicKey { - key: *self.clone().pair.public.as_bytes(), - } + todo!() } } + +#[test] +fn test_encrypt() { + let data = vec![0, 5, 8, 135, 67]; + let keys = Keys::generate(); + assert_eq!( + keys.decrypt_data(keys.get_public().encrypt_data(data)), + Some(data) + ); +} + +#[test] +fn test_invalid_encrypt() { + let data = vec![0, 5, 8, 135, 67]; + let keys_1 = Keys::generate(); + let keys_2 = Keys::generate(); + assert!(keys_2.decrypt_data(keys_1.get_public().encrypt_data(data)) != Some(data)); +} + +#[test] +fn test_signing() { + let data = vec![0, 5, 8, 135, 67]; + let keys = Keys::generate(); + assert!(keys.get_public().verify_sign(data, keys.sign(data))); +} + +#[test] +fn test_invalid_signing() { + let data = vec![0, 5, 8, 135, 67]; + let keys_1 = Keys::generate(); + let keys_2 = Keys::generate(); + assert!(keys_2.get_public().verify_sign(data, keys_1.sign(data))); +} diff --git a/src/lib.rs b/src/lib.rs index f41498a..e332a88 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,8 @@ extern crate alloc; extern crate rand; +mod crypto; + #[cfg(test)] mod tests { #[test]