1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! # Key ID module
//!
//! Used to select the correct public key from the other party from the set of known public keys
//!
//! # Example
//! ```
//! extern crate sodiumoxide;
//! extern crate proj_crypto;
//!
//! use proj_crypto::asymmetric::key_exchange::gen_keypair;
//! use proj_crypto::asymmetric::PublicKey;
//! use proj_crypto::asymmetric::key_id::*;
//! use std::collections::HashMap;
//!
//! # fn main() {
//! sodiumoxide::init();
//!
//! // keypairs
//! let (me, one, two, three) = (gen_keypair(), gen_keypair(), gen_keypair(), gen_keypair());
//!
//! let mut db: HashMap<PublicKeyId, PublicKey> = HashMap::new();
//! db.insert(id_of_pk(&one.0), one.0.clone());
//! db.insert(id_of_pk(&two.0), two.0.clone());
//! db.insert(id_of_pk(&three.0), three.0.clone());
//!
//! let pk = find_public_key(&id_of_pk(&two.0), &db).unwrap();
//! # }
//! ```


/*  This file is part of project-crypto.
    project-crypto is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    project-crypto is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with project-crypto.  If not, see http://www.gnu.org/licenses/.*/

use sodiumoxide::crypto::hash::sha256;
use std::collections::HashMap;
use std::hash;
use super::*;

/// Public Key - just an alias. Implements drop() so the memory will be wiped when it goes out of scope
/// The type of the identifier used to specify the other party's public key.
/// This has to be a custom struct so that I can implement std::hash::Hash
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct PublicKeyId {
    /// The structure just encapsulates this sha256 digest
    pub digest: sha256::Digest,
}

impl hash::Hash for PublicKeyId {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.digest[..].hash(state)
    }
}

/// Given an identifier, attempts to find the correct public key to use in LongKeys.
pub fn find_public_key(id: &PublicKeyId, trusted_keys: &HashMap<PublicKeyId, PublicKey>) -> Option<PublicKey> {
    let pk = match trusted_keys.get(&id) {
        Some(k) => k,
        None => return None,
    };

    // check that the hash matches
    // note that sodiumoxide overrides == with a constant time operation 
    if &id_of_pk(&pk) != id {
        return None;
    }

    Some(pk.clone())
}

/// Calculate the ID of a public key
pub fn id_of_pk(pk: &PublicKey) -> PublicKeyId {
    PublicKeyId {
        digest: sha256::hash(&pk[..]),
    }
}

/******************* Tests *******************/
#[cfg(test)]
mod tests {
    use super::*;
    extern crate sodiumoxide;
    use std::collections::HashMap;
    use super::super::key_exchange::gen_keypair;

    #[test]
    fn find_public_key_correct() {
        sodiumoxide::init();

        // keypairs
        let (one, two, three) = (gen_keypair(), gen_keypair(), gen_keypair());

        let mut db: HashMap<PublicKeyId, PublicKey> = HashMap::new();

        db.insert(id_of_pk(&one.0), one.0.clone());
        db.insert(id_of_pk(&two.0), two.0.clone());
        db.insert(id_of_pk(&three.0), three.0.clone());

        // say we get a connection from identity 'two'
        // their public key will be this
        let _ = find_public_key(&id_of_pk(&two.0), &db).unwrap(); // the unwrap is the test
    }

    #[test]
    #[should_panic]
    fn find_public_key_unknown() {
        sodiumoxide::init();

        // keypairs
        let (one, two, unknown) = (gen_keypair(), gen_keypair(), gen_keypair());

        let mut db: HashMap<PublicKeyId, PublicKey> = HashMap::new();

        db.insert(id_of_pk(&one.0), one.0.clone());
        db.insert(id_of_pk(&two.0), two.0.clone());

        // unknown is not in the map so the unwrap panics
        let _ = find_public_key(&id_of_pk(&unknown.0), &db).unwrap(); // the unwrap is the test
    }

    #[test]
    #[should_panic]
    fn find_public_key_bad_id() {
        sodiumoxide::init();

        // keypairs
        let (one, two, three) = (gen_keypair(), gen_keypair(), gen_keypair());

        let mut db: HashMap<PublicKeyId, PublicKey> = HashMap::new();

        db.insert(id_of_pk(&one.0), one.0.clone());
        db.insert(id_of_pk(&one.0), two.0.clone()); // here is the error
        db.insert(id_of_pk(&three.0), three.0.clone());

        // say we get a connection from identity 'two'
        // their public key will be this
        let _ = find_public_key(&id_of_pk(&two.0), &db).unwrap(); // the unwrap is the test
    }
}