From 01839b893747ad15b0b233e90a484a64545cbb12 Mon Sep 17 00:00:00 2001 From: ennucore Date: Thu, 19 Mar 2020 17:52:14 +0300 Subject: [PATCH] Signatures and working --- src/crypto.rs | 19 ++++++++++++------- src/lib.rs | 12 ++++++++++-- src/message.rs | 31 ++++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/crypto.rs b/src/crypto.rs index 0ddfe7a..a339826 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -7,9 +7,10 @@ use sha2::Sha512; use serde::{Serialize, Deserialize}; use rand::rngs::OsRng; use self::ed25519_dalek::Digest; +use alloc::vec::Vec; -#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] pub struct PublicKey { pub key: [u8; 32] } @@ -28,17 +29,21 @@ impl KeyPack { } impl PublicKey { - pub fn verify_sign(self, msg: crate::message::Message, sign: Signature) -> bool { - PK::from_bytes(&self.key).unwrap().verify(msg.get_hash().as_ref(), &sign).is_ok() + pub fn verify_sign(&self, msg: &crate::message::Message) -> bool { + PK::from_bytes(&self.key).unwrap() + .verify( + msg.get_hash().as_ref(), + &Signature::from_bytes(&msg.clone().sign.into_boxed_slice()).unwrap() + ).is_ok() } } impl KeyPack { - pub fn sign(self, msg: u64) -> u64 { - 0 + pub fn sign(&self, msg: &crate::message::Message) -> Vec { + self.pair.sign(msg.hash.as_ref()).to_bytes().to_vec() } - pub fn get_public(self) -> PublicKey { - PublicKey { key: *self.pair.public.as_bytes() } + pub fn get_public(&self) -> PublicKey { + PublicKey { key: *self.clone().pair.public.as_bytes() } } } diff --git a/src/lib.rs b/src/lib.rs index f42cc07..583d3f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,17 +26,25 @@ mod tests { fn serialization() { let key_pack = KeyPack::gen(); let msg = Message::new( - MsgType::MultiCast, Vec::::new(), key_pack + MsgType::MultiCast, Vec::::new(), &key_pack ); let serialized = &msg.ser(); let msg2 = Message::deserialize(serialized.to_vec()); assert_eq!(msg2.msg_type, MsgType::MultiCast); } + #[test] + fn valid_message_creation() { + let msg = Message::new( + MsgType::MultiCast, Vec::::new(), &KeyPack::gen() + ); + assert!(msg.verify()); + } + #[test] fn test_hash() { let msg = Message::new( - MsgType::MultiCast, Vec::::new(), crate::crypto::KeyPack::gen() + MsgType::MultiCast, Vec::::new(), &crate::crypto::KeyPack::gen() ); msg.get_hash(); } diff --git a/src/message.rs b/src/message.rs index 50be938..42ea111 100644 --- a/src/message.rs +++ b/src/message.rs @@ -3,36 +3,49 @@ use sha2::Digest; use serde::{Serialize, Deserialize}; use alloc::vec::Vec; use pinecone::{from_bytes, to_vec}; -use serde::private::de::size_hint::from_bounds; +use ed25519_dalek::Signature; -#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] pub enum MsgType { MultiCast, ToTarget(PublicKey), } -#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] pub struct Message { pub msg_type: MsgType, pub body: Vec, pub source: PublicKey, - hash: u64, - sign: u64, + pub hash: Vec, + pub sign: Vec, } impl Message { - pub fn get_hash(self) -> Vec { + pub fn get_hash(&self) -> Vec { let mut hasher = sha2::Sha256::new(); - hasher.input(self.ser()); + let msg2 = Message { + msg_type: self.msg_type.clone(), + body: self.body.clone(), + source: self.source.clone(), + hash: Vec::::new(), + sign: Vec::::new() + }; + hasher.input(msg2.ser()); hasher.result().to_vec() } - pub fn new(msg_type: MsgType, body: Vec, key_pack: KeyPack) -> Message { - let msg = Message { msg_type, body, source: key_pack.get_public(), hash: 0, sign: 0 }; + pub fn new(msg_type: MsgType, body: Vec, key_pack: &KeyPack) -> Message { + let mut msg = Message { msg_type, body, source: key_pack.get_public(), hash: Vec::::new(), sign: Vec::::new() }; + msg.hash = msg.get_hash(); + msg.sign = key_pack.sign(&msg); msg } + pub fn verify(self) -> bool { + self.hash == self.get_hash() && self.source.verify_sign(&self) + } + pub fn ser(self) -> Vec { to_vec(&self).expect("Serialization failed") }