Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #60 from rosatamsen/master
Browse files Browse the repository at this point in the history
Initial run of RustFmt + a few manual fixes
  • Loading branch information
Peter Jankuliak committed Sep 12, 2015
2 parents 3e8bbfc + e79f867 commit 662fcbc
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 150 deletions.
67 changes: 35 additions & 32 deletions src/key_sentinel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

use lru_time_cache::LruCache;
use sodiumoxide::crypto::sign;
use std::collections::{BTreeSet, BTreeMap};
use std::collections::{BTreeMap, BTreeSet};
use key_store::KeyStore;
use std::marker::PhantomData;
use std::fmt::Debug;
use super::{verify_signature, SerialisedClaim};
use super::{SerialisedClaim, verify_signature};
use wrappers::SignW;

#[allow(dead_code)]
Expand All @@ -41,10 +41,11 @@ pub trait GroupClaimTrait<IdTrait> {

#[allow(dead_code)]
pub struct KeySentinel<Request, Name, IdType, GroupClaim>
where Request: Eq + PartialOrd + Ord + Clone,
Name: Eq + PartialOrd + Ord + Clone + Debug,
IdType: Eq + PartialOrd + Ord + Clone + IdTrait<Name>,
GroupClaim: Eq + PartialOrd + Ord + Clone + GroupClaimTrait<IdType>, {
where Request: Eq + PartialOrd + Ord + Clone,
Name: Eq + PartialOrd + Ord + Clone + Debug,
IdType: Eq + PartialOrd + Ord + Clone + IdTrait<Name>,
GroupClaim: Eq + PartialOrd + Ord + Clone + GroupClaimTrait<IdType>
{
cache: LruCache<Request, (KeyStore<Name>, Map<Name, Set<(GroupClaim, SerialisedClaim, SignW)>>)>,
phantom: PhantomData<IdType>,
}
Expand All @@ -57,28 +58,24 @@ impl<Request, Name, IdType, GroupClaim> KeySentinel<Request, Name, IdType, Group

#[allow(dead_code)]
pub fn new() -> KeySentinel<Request, Name, IdType, GroupClaim> {
KeySentinel {
cache: LruCache::with_capacity(MAX_REQUEST_COUNT),
phantom: PhantomData,
}
KeySentinel { cache: LruCache::with_capacity(MAX_REQUEST_COUNT), phantom: PhantomData }
}

#[allow(dead_code)]
pub fn add_identities(&mut self,
request: Request,
sender: Name,
serialised: SerialisedClaim,
signature: sign::Signature,
claim: GroupClaim,
request: Request,
sender: Name,
serialised: SerialisedClaim,
signature: sign::Signature,
claim: GroupClaim,
quorum_size: usize)
-> Option<(Request, Vec<IdType>)> {
-> Option<(Request, Vec<IdType>)> {

let retval = {
let keys_and_claims
= self.cache.entry(request.clone())
let keys_and_claims = self.cache.entry(request.clone())
.or_insert_with(||(KeyStore::new(), Map::new()));

let ref mut keys = &mut keys_and_claims.0;
let ref mut keys = &mut keys_and_claims.0;
let ref mut claims = &mut keys_and_claims.1;

for id in claim.group_identities() {
Expand All @@ -100,7 +97,8 @@ impl<Request, Name, IdType, GroupClaim> KeySentinel<Request, Name, IdType, Group

fn try_selecting_group(key_store: &mut KeyStore<Name>,
claims: &Map<Name, Set<(GroupClaim, SerialisedClaim, SignW)>>,
quorum_size: usize) -> Option<Vec<IdType>> {
quorum_size: usize)
-> Option<Vec<IdType>> {

let verified_claims = claims.iter().filter_map(|(name, claims)| {
for &(ref claim, ref serialised, ref signature) in claims {
Expand All @@ -122,7 +120,8 @@ impl<Request, Name, IdType, GroupClaim> KeySentinel<Request, Name, IdType, Group
key_store: &mut KeyStore<Name>,
serialised: &SerialisedClaim,
signature: &sign::Signature,
quorum_size: usize) -> bool {
quorum_size: usize)
-> bool {
for public_key in key_store.get_accumulated_keys(&author, quorum_size) {
if verify_signature(signature, &public_key, serialised).is_some() {
return true;
Expand All @@ -146,30 +145,32 @@ mod test {

fn generate_random_message() -> Vec<u8> {
let mut arr = [0u8;MESSAGE_SIZE];
for i in (0..MESSAGE_SIZE) { arr[i] = random::<u8>(); }
for i in (0..MESSAGE_SIZE) {
arr[i] = random::<u8>();
}
arr.to_vec()
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug)]
struct TestRequest {
core : usize,
name : TestName
core: usize,
name: TestName,
}

impl TestRequest {
pub fn new(core: usize, name: TestName) -> TestRequest {
TestRequest { core : core, name : name }
TestRequest { core: core, name: name }
}
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
struct TestIdType {
name: TestName,
public_key: [u8;sign::PUBLICKEYBYTES],
public_key: [u8; sign::PUBLICKEYBYTES],
}

impl IdTrait<TestName> for TestIdType {
fn name(&self) -> TestName {
fn name(&self) -> TestName {
self.name.clone()
}

Expand All @@ -179,7 +180,9 @@ mod test {
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
struct TestGroupClaim { identities: Vec<TestIdType>, }
struct TestGroupClaim {
identities: Vec<TestIdType>,
}

impl TestGroupClaim {
pub fn new(identities: Vec<TestIdType>) -> TestGroupClaim {
Expand All @@ -195,12 +198,12 @@ mod test {

#[test]
fn key_sentinel() {
let mut sentinel: KeySentinel<TestRequest, TestName, TestIdType, TestGroupClaim>
= KeySentinel::new();
let mut sentinel: KeySentinel<TestRequest, TestName, TestIdType, TestGroupClaim> =
KeySentinel::new();

let random_message = generate_random_message();
let mut names = Vec::new();
let mut pubs = Vec::new();
let mut names = Vec::new();
let mut pubs = Vec::new();
let mut signatures = Vec::new();

for i in 0..QUORUM + 1 {
Expand Down
45 changes: 28 additions & 17 deletions src/key_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,44 @@ type Map<A, B> = BTreeMap<A,B>;
type Set<A> = BTreeSet<A>;

#[derive(Clone)]
pub struct KeyStore<Name> where Name: Eq + PartialOrd + Ord + Clone {
pub struct KeyStore<Name>
where Name: Eq + PartialOrd + Ord + Clone
{
// +--- Target +--- Sender
// V V
cache: LruCache<Name, Map<KeyData, Set<Name>>>,
}

impl<Name> KeyStore<Name> where Name: Eq + PartialOrd + Ord + Clone {
pub fn new() -> KeyStore<Name> {
KeyStore{ cache: LruCache::with_capacity(NAME_CAPACITY) }
KeyStore { cache: LruCache::with_capacity(NAME_CAPACITY) }
}

pub fn add_key(&mut self, target: Name, sender: Name, key: sign::PublicKey) {
// No self signing.
if target == sender { return; }
if target == sender {
return;
}

let new_map = || { Map::<KeyData, Set<Name>>::new() };
let new_set = || { Set::<Name>::new() };
let new_map = || Map::<KeyData, Set<Name>>::new();
let new_set = || Set::<Name>::new();

self.cache.entry(target).or_insert_with(new_map)
.entry(key.0).or_insert_with(new_set)
.insert(sender);
}

#[allow(dead_code)]
pub fn len(&self) -> usize { self.cache.len() }
pub fn len(&self) -> usize {
self.cache.len()
}

/// Returns a vector of keys belonging to `target`, for whom we've received the key
/// from at least a quorum size of unique senders.
pub fn get_accumulated_keys(&mut self, target: &Name, quorum_size: usize)
-> Vec<sign::PublicKey> {
pub fn get_accumulated_keys(&mut self,
target: &Name,
quorum_size: usize)
-> Vec<sign::PublicKey> {
// Create temp variable to workaround a borrow checker bug
// http://blog.ezyang.com/2013/12/two-bugs-in-the-borrow-checker-every-rust-developer-should-know-about/
self.cache.get(target)
Expand All @@ -68,8 +76,9 @@ impl<Name> KeyStore<Name> where Name: Eq + PartialOrd + Ord + Clone {
.collect::<_>()
}

fn pick_where_quorum_reached<'a>(keys: &'a Map<KeyData, Set<Name>>, quorum: usize)
-> Vec<&'a KeyData> {
fn pick_where_quorum_reached<'a>(keys: &'a Map<KeyData, Set<Name>>,
quorum: usize)
-> Vec<&'a KeyData> {
keys.iter().filter_map(|(key, sender_set)| {
if sender_set.len() >= quorum { Some(key) } else { None }
}).collect::<_>()
Expand All @@ -87,7 +96,9 @@ mod test {

fn random_key() -> sign::PublicKey {
let mut arr = [0u8;sign::PUBLICKEYBYTES];
for i in (0..sign::PUBLICKEYBYTES) { arr[i] = random::<u8>(); }
for i in (0..sign::PUBLICKEYBYTES) {
arr[i] = random::<u8>();
}
sign::PublicKey(arr)
}

Expand All @@ -99,13 +110,13 @@ mod test {

#[test]
fn quorum_reached() {
let target : NameType = 0;
let target: NameType = 0;
let mut ks = KeyStore::<NameType>::new();
let valid_key = random_key();

add_noise(&mut ks, target, 1000);

for i in (1..QUORUM+1) {
for i in (1..QUORUM + 1) {
ks.add_key(target, i as NameType, valid_key);

if i < QUORUM {
Expand All @@ -118,7 +129,7 @@ mod test {

#[test]
fn no_self_sign() {
let target : NameType = 0;
let target: NameType = 0;
let mut ks = KeyStore::<NameType>::new();
let valid_key = random_key();

Expand All @@ -133,14 +144,14 @@ mod test {

#[test]
fn successful_attack() {
let target : NameType = 0;
let target: NameType = 0;
let mut ks = KeyStore::<NameType>::new();
let valid_key1 = random_key();
let valid_key2 = random_key();

add_noise(&mut ks, target, 1000);

for i in (1..QUORUM+1) {
for i in (1..QUORUM + 1) {
ks.add_key(target, i as NameType, valid_key1);

if i < QUORUM {
Expand All @@ -150,7 +161,7 @@ mod test {
}
}

for i in (1..QUORUM+1) {
for i in (1..QUORUM + 1) {
ks.add_key(target, i as NameType, valid_key2);

if i < QUORUM {
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ mod wrappers;
mod refresh_sentinel;
mod statistics;

fn verify_signature(signature: &Signature, public_key: &PublicKey, claim: &SerialisedClaim)
-> Option<SerialisedClaim> {
fn verify_signature(signature: &Signature,
public_key: &PublicKey,
claim: &SerialisedClaim)
-> Option<SerialisedClaim> {

match crypto::sign::verify_detached(&signature, claim, public_key) {
true => Some(claim.clone()),
false => None
false => None,
}
}
Loading

0 comments on commit 662fcbc

Please sign in to comment.