Browse Source

MessageContent

master
ennucore 5 years ago
parent
commit
9d46637251
  1. 28
      src/crypto.rs
  2. 14
      src/ironforce.rs
  3. 21
      src/lib.rs
  4. 101
      src/message.rs
  5. 4
      src/transport.rs
  6. 8
      src/tunnel.rs

28
src/crypto.rs

@ -8,37 +8,43 @@ use rand::rngs::OsRng;
use alloc::vec::Vec; use alloc::vec::Vec;
use crate::message::Sign;
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct PublicKey { pub struct PublicKey {
pub key: [u8; 32] pub key: [u8; 32]
} }
pub struct KeyPack { pub struct Keys {
pub pair: Keypair, pub pair: Keypair,
csprng: OsRng, csprng: OsRng,
} }
impl KeyPack { impl Keys {
pub fn gen() -> KeyPack { pub fn gen() -> Keys {
let mut osrng = OsRng {}; let mut osrng = OsRng {};
KeyPack { pair: Keypair::generate(&mut osrng), csprng: osrng } Keys { pair: Keypair::generate(&mut osrng), csprng: osrng }
} }
} }
impl PublicKey { pub fn verify_sign(msg: &crate::message::Message) -> bool {
pub fn verify_sign(&self, msg: &crate::message::Message) -> bool { match &msg.clone().sign {
PK::from_bytes(&self.key).unwrap() Sign::NoSign => true,
Sign::Signed(key, sign) => {
PK::from_bytes(&key.key).unwrap()
.verify( .verify(
msg.get_hash().as_ref(), msg.get_hash().as_ref(),
&Signature::from_bytes(&msg.clone().sign.into_boxed_slice()).unwrap(), &Signature::from_bytes(sign.clone().into_boxed_slice().as_ref()).unwrap(),
).is_ok() ).is_ok()
} }
}
} }
impl KeyPack { impl Keys {
pub fn sign(&self, msg: &crate::message::Message) -> Vec<u8> { pub fn sign(&self, content: &Vec<u8>) -> Vec<u8> {
self.pair.sign(msg.hash.as_ref()).to_bytes().to_vec() self.pair.sign(content.as_ref()).to_bytes().to_vec()
} }
pub fn get_public(&self) -> PublicKey { pub fn get_public(&self) -> PublicKey {

14
src/ironforce.rs

@ -1,5 +1,5 @@
use crate::transport::Transport; use crate::transport::Transport;
use crate::crypto::{PublicKey, KeyPack}; use crate::crypto::{PublicKey, Keys};
use crate::message::{Message, MsgType}; use crate::message::{Message, MsgType};
use alloc::vec::Vec; use alloc::vec::Vec;
@ -7,12 +7,12 @@ use alloc::vec::Vec;
pub struct IronForce { pub struct IronForce {
transport: Transport, transport: Transport,
key_pack: KeyPack, key_pack: Keys,
} }
impl IronForce { impl IronForce {
pub fn new() -> IronForce { pub fn new() -> IronForce {
IronForce { transport: Transport::new(), key_pack: KeyPack::gen() } IronForce { transport: Transport::new(), key_pack: Keys::gen() }
} }
fn is_valid_message(&self, msg: &Message) -> bool { fn is_valid_message(&self, msg: &Message) -> bool {
@ -33,19 +33,19 @@ impl IronForce {
} }
pub fn send_message_to(&self, to: PublicKey, body: &Vec<u8>) { pub fn send_message_to(&self, to: PublicKey, body: &Vec<u8>) {
self.transport.send(&self.new_message(MsgType::ToTarget(to), body)) self.transport.send(&self.new_message(MsgType::UniCast(to), body))
} }
fn handle_message(&self, msg: &Message) { fn handle_message(&self, msg: &Message) {
if self.is_valid_message(msg) { if self.is_valid_message(msg) {
match &msg.msg_type { match &msg.content.msg_type {
MsgType::Service => { MsgType::Service => {
self.service_msg(&msg.body) self.service_msg(&msg.content.body)
}, },
MsgType::MultiCast => { MsgType::MultiCast => {
}, },
MsgType::ToTarget(Target) => { MsgType::UniCast(Target) => {
} }
} }

21
src/lib.rs

@ -9,10 +9,10 @@ mod message;
mod crypto; mod crypto;
mod error; mod error;
mod way; mod way;
mod tunnel;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
mod iron_force { mod iron_force {
#[test] #[test]
fn creation_works() { fn creation_works() {
@ -23,45 +23,48 @@ mod tests {
mod message { mod message {
use crate::message::{MsgType, Message}; use crate::message::{MsgType, Message};
use crate::crypto::KeyPack; use crate::crypto::Keys;
use alloc::vec::Vec; use alloc::vec::Vec;
#[test] #[test]
fn serialization() { fn serialization() {
let key_pack = KeyPack::gen(); let key_pack = Keys::gen();
let msg = Message::new( let msg = Message::new(
MsgType::MultiCast, Vec::<u8>::new(), &key_pack, MsgType::MultiCast, Vec::<u8>::new(), &key_pack,
); );
let serialized = &msg.ser(); let serialized = &msg.ser();
let msg2 = Message::deserialize(serialized.to_vec()); let msg2 = Message::deserialize(serialized.to_vec());
assert_eq!(msg2.msg_type, MsgType::MultiCast); assert_eq!(msg2.content.msg_type, MsgType::MultiCast);
} }
#[test] #[test]
fn valid_message_creation() { fn valid_message_creation() {
let msg = Message::new( let msg = Message::new(
MsgType::MultiCast, Vec::<u8>::new(), &KeyPack::gen(), MsgType::MultiCast, Vec::<u8>::new(), &Keys::gen(),
); );
assert!(&msg.verify()); assert!(&msg.verify());
} }
#[test] #[test]
fn test_invalid_sign() { fn test_invalid_sign() {
let key_pack = KeyPack::gen(); let key_pack = Keys::gen();
let mut msg = Message::new( let mut msg = Message::new(
MsgType::MultiCast, Vec::<u8>::new(), &key_pack, MsgType::MultiCast, Vec::<u8>::new(), &key_pack,
); );
msg.sign = [0; 64].to_vec(); msg.sign = crate::message::Sign::Signed(key_pack.get_public(), [0; 64].to_vec());
assert!(!&msg.verify()); assert!(!&msg.verify());
msg.hash = [0; 64].to_vec(); msg.hash = [0; 64].to_vec();
msg.sign = key_pack.sign(&msg); msg.sign = crate::message::Sign::Signed(
key_pack.get_public(),
key_pack.sign(&msg.content_hash)
);
assert!(!&msg.verify()); assert!(!&msg.verify());
} }
#[test] #[test]
fn test_hash() { fn test_hash() {
let msg = Message::new( let msg = Message::new(
MsgType::MultiCast, Vec::<u8>::new(), &crate::crypto::KeyPack::gen(), MsgType::MultiCast, Vec::<u8>::new(), &crate::crypto::Keys::gen(),
); );
msg.get_hash(); msg.get_hash();
} }

101
src/message.rs

@ -1,56 +1,107 @@
use crate::crypto::{PublicKey, KeyPack}; use crate::crypto::{PublicKey, Keys, verify_sign};
use sha2::Digest; use sha2::Digest;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use alloc::vec::Vec; use alloc::vec::Vec;
use pinecone::{from_bytes, to_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)] #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub enum MsgType { pub enum MsgType {
MultiCast, MultiCast, // No source, sign and tunnel
ToTarget(PublicKey), UniCast(PublicKey /* destination */), // has source, sign and tunnel
Service, Service, // Has source and sign but no tunnel
} }
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] #[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 msg_type: MsgType,
pub body: Vec<u8>, 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 { impl Message {
pub fn get_hash(&self) -> Vec<u8> { pub fn get_hash(&self) -> Vec<u8> {
let mut hasher = sha2::Sha256::new(); let mut msg2 = self.clone();
let msg2 = Message { msg2.hash = Vec::<u8>::new();
msg_type: self.msg_type.clone(), get_hash(&msg2.ser())
body: self.body.clone(),
source: self.source.clone(),
hash: Vec::<u8>::new(),
sign: Vec::<u8>::new(),
};
hasher.input(msg2.ser());
hasher.result().to_vec()
} }
pub fn new(msg_type: MsgType, body: Vec<u8>, key_pack: &KeyPack) -> Message { pub fn new(msg_type: MsgType, body: Vec<u8>, key_pack: &Keys) -> Message {
let mut msg = Message { msg_type, body, source: key_pack.get_public(), hash: Vec::<u8>::new(), sign: Vec::<u8>::new() }; 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.hash = msg.get_hash();
msg.sign = key_pack.sign(&msg);
msg msg
} }
pub fn verify(&self) -> bool { 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> { pub fn ser(&self) -> Vec<u8> {
to_vec(&self).expect("Serialization failed") to_vec(&self).expect("Message serialization failed")
} }
pub fn deserialize(serialized: Vec<u8>) -> Self { pub fn deserialize(serialized: Vec<u8>) -> Self {
from_bytes(&serialized).expect("Deserialization failed") from_bytes(&serialized).expect("Message deserialization failed")
} }
} }

4
src/transport.rs

@ -28,7 +28,7 @@ impl Transport {
pub fn receive(&self) -> Message { pub fn receive(&self) -> Message {
Message::new(MsgType::Service, Message::new(MsgType::Service,
Vec::<u8>::new(), Vec::<u8>::new(),
&crate::crypto::KeyPack::gen()) &crate::crypto::Keys::gen())
} }
} }
@ -47,6 +47,6 @@ impl Transport {
pub fn receive(&self) -> Message { pub fn receive(&self) -> Message {
Message::new(MsgType::Service, Message::new(MsgType::Service,
Vec::<u8>::new(), Vec::<u8>::new(),
&crate::crypto::KeyPack::gen()) &crate::crypto::Keys::gen())
} }
} }

8
src/tunnel.rs

@ -0,0 +1,8 @@
use crate::crypto::PublicKey;
use alloc::vec::Vec;
use serde::{Serialize, Deserialize};
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct Tunnel {
nodes: Vec<PublicKey>
}
Loading…
Cancel
Save