ennucore
5 years ago
6 changed files with 125 additions and 57 deletions
@ -1,56 +1,107 @@
|
||||
use crate::crypto::{PublicKey, KeyPack}; |
||||
use crate::crypto::{PublicKey, Keys, verify_sign}; |
||||
use sha2::Digest; |
||||
use serde::{Serialize, Deserialize}; |
||||
use alloc::vec::Vec; |
||||
use pinecone::{from_bytes, to_vec}; |
||||
use crate::tunnel::Tunnel; |
||||
|
||||
|
||||
fn get_hash(b: &Vec<u8>) -> Vec<u8> { |
||||
let mut hasher = sha2::Sha256::new(); |
||||
hasher.input(b); |
||||
hasher.result().to_vec() |
||||
} |
||||
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
||||
pub enum MsgType { |
||||
MultiCast, |
||||
ToTarget(PublicKey), |
||||
Service, |
||||
MultiCast, // No source, sign and tunnel
|
||||
UniCast(PublicKey /* destination */), // has source, sign and tunnel
|
||||
Service, // Has source and sign but no tunnel
|
||||
} |
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
||||
pub struct Message { |
||||
pub enum Sign { |
||||
NoSign, |
||||
Signed(PublicKey /* source */, Vec<u8> /* sign */) |
||||
} |
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
||||
pub enum MsgTunnel { |
||||
NoTunnel, |
||||
Tunnel(Tunnel) |
||||
} |
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
||||
pub struct MessageContent { |
||||
/// MessageContent is part of the message that is signed
|
||||
pub msg_type: MsgType, |
||||
pub body: Vec<u8>, |
||||
pub source: PublicKey, |
||||
pub hash: Vec<u8>, |
||||
pub sign: Vec<u8>, |
||||
} |
||||
|
||||
impl MessageContent { |
||||
pub fn new(msg_type: MsgType, body: Vec<u8>) -> Self { |
||||
Self { msg_type, body } |
||||
} |
||||
|
||||
pub fn get_hash(&self) -> Vec<u8> { |
||||
get_hash(&self.ser()) |
||||
} |
||||
|
||||
pub fn ser(&self) -> Vec<u8> { |
||||
to_vec(&self).expect("Message content serialization failed") |
||||
} |
||||
|
||||
pub fn deserialize(serialized: Vec<u8>) -> Self { |
||||
from_bytes(&serialized).expect("Message content deserialization failed") |
||||
} |
||||
} |
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
||||
pub struct Message { |
||||
pub content: MessageContent, |
||||
pub content_hash: Vec<u8>, // hash for signing
|
||||
pub hash: Vec<u8>, // hash for checking integrity
|
||||
pub sign: Sign, |
||||
pub tunnel: MsgTunnel |
||||
} |
||||
|
||||
impl Message { |
||||
pub fn get_hash(&self) -> Vec<u8> { |
||||
let mut hasher = sha2::Sha256::new(); |
||||
let msg2 = Message { |
||||
msg_type: self.msg_type.clone(), |
||||
body: self.body.clone(), |
||||
source: self.source.clone(), |
||||
hash: Vec::<u8>::new(), |
||||
sign: Vec::<u8>::new(), |
||||
}; |
||||
hasher.input(msg2.ser()); |
||||
hasher.result().to_vec() |
||||
let mut msg2 = self.clone(); |
||||
msg2.hash = Vec::<u8>::new(); |
||||
get_hash(&msg2.ser()) |
||||
} |
||||
|
||||
pub fn new(msg_type: MsgType, body: Vec<u8>, key_pack: &KeyPack) -> Message { |
||||
let mut msg = Message { msg_type, body, source: key_pack.get_public(), hash: Vec::<u8>::new(), sign: Vec::<u8>::new() }; |
||||
pub fn new(msg_type: MsgType, body: Vec<u8>, key_pack: &Keys) -> Message { |
||||
let content= MessageContent::new(msg_type.clone(), body); |
||||
let content_hash = content.get_hash(); |
||||
let mut msg = Message { |
||||
content, |
||||
content_hash, |
||||
hash: Vec::<u8>::new(), |
||||
sign: Sign::NoSign, |
||||
tunnel: MsgTunnel::NoTunnel |
||||
}; |
||||
msg.sign = match msg_type { |
||||
MsgType::UniCast(_) | MsgType::Service => { |
||||
Sign::Signed(key_pack.get_public(), key_pack.sign(&msg.content_hash)) |
||||
}, |
||||
MsgType::MultiCast => { Sign::NoSign } |
||||
}; |
||||
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) |
||||
self.hash == self.get_hash() && verify_sign(&self) |
||||
} |
||||
|
||||
pub fn ser(self) -> Vec<u8> { |
||||
to_vec(&self).expect("Serialization failed") |
||||
pub fn ser(&self) -> Vec<u8> { |
||||
to_vec(&self).expect("Message serialization failed") |
||||
} |
||||
|
||||
pub fn deserialize(serialized: Vec<u8>) -> Self { |
||||
from_bytes(&serialized).expect("Deserialization failed") |
||||
from_bytes(&serialized).expect("Message deserialization failed") |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue