2018-03-30 10:43:38 -07:00
|
|
|
//! The `signature` module provides functionality for public, and private keys.
|
2018-03-06 12:48:26 -07:00
|
|
|
|
2018-05-10 13:01:42 -07:00
|
|
|
use generic_array::typenum::{U32, U64};
|
2018-05-29 21:20:28 -06:00
|
|
|
use generic_array::GenericArray;
|
2018-05-09 00:03:05 -04:00
|
|
|
use rand::{ChaChaRng, Rng, SeedableRng};
|
2018-05-12 15:42:27 -04:00
|
|
|
use rayon::prelude::*;
|
2018-05-09 00:03:05 -04:00
|
|
|
use ring::error::Unspecified;
|
|
|
|
use ring::rand::SecureRandom;
|
2018-03-06 12:48:26 -07:00
|
|
|
use ring::signature::Ed25519KeyPair;
|
|
|
|
use ring::{rand, signature};
|
2018-07-12 15:42:01 -06:00
|
|
|
use serde_json;
|
2018-05-11 14:07:41 -04:00
|
|
|
use std::cell::RefCell;
|
2018-07-12 15:42:01 -06:00
|
|
|
use std::error;
|
|
|
|
use std::fs::File;
|
|
|
|
use untrusted::Input;
|
2018-03-06 12:48:26 -07:00
|
|
|
|
2018-03-07 11:05:06 -07:00
|
|
|
pub type KeyPair = Ed25519KeyPair;
|
2018-03-06 12:48:26 -07:00
|
|
|
pub type PublicKey = GenericArray<u8, U32>;
|
|
|
|
pub type Signature = GenericArray<u8, U64>;
|
|
|
|
|
2018-03-07 15:32:22 -07:00
|
|
|
pub trait KeyPairUtil {
|
|
|
|
fn new() -> Self;
|
|
|
|
fn pubkey(&self) -> PublicKey;
|
2018-03-06 12:48:26 -07:00
|
|
|
}
|
|
|
|
|
2018-03-07 15:32:22 -07:00
|
|
|
impl KeyPairUtil for Ed25519KeyPair {
|
|
|
|
/// Return a new ED25519 keypair
|
|
|
|
fn new() -> Self {
|
|
|
|
let rng = rand::SystemRandom::new();
|
2018-07-12 15:42:01 -06:00
|
|
|
let pkcs8_bytes = Ed25519KeyPair::generate_pkcs8(&rng).expect("generate_pkcs8");
|
|
|
|
Ed25519KeyPair::from_pkcs8(Input::from(&pkcs8_bytes)).expect("from_pcks8")
|
2018-03-07 15:32:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the public key for the given keypair
|
|
|
|
fn pubkey(&self) -> PublicKey {
|
|
|
|
GenericArray::clone_from_slice(self.public_key_bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait SignatureUtil {
|
|
|
|
fn verify(&self, peer_public_key_bytes: &[u8], msg_bytes: &[u8]) -> bool;
|
2018-03-06 12:48:26 -07:00
|
|
|
}
|
|
|
|
|
2018-03-07 15:32:22 -07:00
|
|
|
impl SignatureUtil for GenericArray<u8, U64> {
|
|
|
|
fn verify(&self, peer_public_key_bytes: &[u8], msg_bytes: &[u8]) -> bool {
|
2018-07-12 15:42:01 -06:00
|
|
|
let peer_public_key = Input::from(peer_public_key_bytes);
|
|
|
|
let msg = Input::from(msg_bytes);
|
|
|
|
let sig = Input::from(self);
|
2018-03-07 15:32:22 -07:00
|
|
|
signature::verify(&signature::ED25519, peer_public_key, msg, sig).is_ok()
|
|
|
|
}
|
2018-03-06 12:48:26 -07:00
|
|
|
}
|
2018-05-09 00:03:05 -04:00
|
|
|
|
|
|
|
pub struct GenKeys {
|
2018-05-11 14:07:41 -04:00
|
|
|
// This is necessary because the rng needs to mutate its state to remain
|
|
|
|
// deterministic, and the fill trait requires an immuatble reference to self
|
|
|
|
generator: RefCell<ChaChaRng>,
|
2018-05-09 00:03:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GenKeys {
|
2018-06-11 14:04:51 -06:00
|
|
|
pub fn new(seed: [u8; 32]) -> GenKeys {
|
|
|
|
let rng = ChaChaRng::from_seed(seed);
|
2018-05-09 00:03:05 -04:00
|
|
|
GenKeys {
|
2018-05-11 14:07:41 -04:00
|
|
|
generator: RefCell::new(rng),
|
2018-05-09 00:03:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_key(&self) -> Vec<u8> {
|
|
|
|
KeyPair::generate_pkcs8(self).unwrap().to_vec()
|
|
|
|
}
|
2018-05-11 12:55:05 -04:00
|
|
|
|
2018-06-11 14:04:51 -06:00
|
|
|
pub fn gen_n_seeds(&self, n: i64) -> Vec<[u8; 32]> {
|
2018-05-12 15:42:27 -04:00
|
|
|
let mut rng = self.generator.borrow_mut();
|
2018-05-23 14:04:07 -06:00
|
|
|
(0..n).map(|_| rng.gen()).collect()
|
2018-05-12 15:42:27 -04:00
|
|
|
}
|
|
|
|
|
2018-05-23 14:04:07 -06:00
|
|
|
pub fn gen_n_keypairs(&self, n: i64) -> Vec<KeyPair> {
|
|
|
|
self.gen_n_seeds(n)
|
2018-05-23 12:23:39 -06:00
|
|
|
.into_par_iter()
|
2018-05-12 15:42:27 -04:00
|
|
|
.map(|seed| {
|
2018-06-11 14:04:51 -06:00
|
|
|
let pkcs8 = GenKeys::new(seed).new_key();
|
2018-07-12 15:42:01 -06:00
|
|
|
KeyPair::from_pkcs8(Input::from(&pkcs8)).unwrap()
|
2018-05-11 12:55:05 -04:00
|
|
|
})
|
2018-05-23 12:23:39 -06:00
|
|
|
.collect()
|
2018-05-11 12:55:05 -04:00
|
|
|
}
|
2018-05-09 00:03:05 -04:00
|
|
|
}
|
|
|
|
|
2018-05-13 18:14:10 -04:00
|
|
|
impl SecureRandom for GenKeys {
|
2018-05-09 00:03:05 -04:00
|
|
|
fn fill(&self, dest: &mut [u8]) -> Result<(), Unspecified> {
|
2018-05-11 14:07:41 -04:00
|
|
|
let mut rng = self.generator.borrow_mut();
|
2018-06-11 14:04:51 -06:00
|
|
|
rng.fill(dest);
|
2018-05-09 00:03:05 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2018-05-11 12:55:05 -04:00
|
|
|
|
2018-07-12 15:42:01 -06:00
|
|
|
pub fn read_keypair(path: &str) -> Result<KeyPair, Box<error::Error>> {
|
|
|
|
let file = File::open(path.to_string())?;
|
|
|
|
let pkcs8: Vec<u8> = serde_json::from_reader(file)?;
|
|
|
|
let keypair = Ed25519KeyPair::from_pkcs8(Input::from(&pkcs8))?;
|
|
|
|
Ok(keypair)
|
|
|
|
}
|
|
|
|
|
2018-05-23 12:23:39 -06:00
|
|
|
#[cfg(test)]
|
2018-05-11 12:55:05 -04:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
|
|
|
#[test]
|
2018-05-23 14:04:07 -06:00
|
|
|
fn test_new_key_is_deterministic() {
|
2018-06-11 14:04:51 -06:00
|
|
|
let seed = [0u8; 32];
|
|
|
|
let rng0 = GenKeys::new(seed);
|
|
|
|
let rng1 = GenKeys::new(seed);
|
2018-05-11 12:55:05 -04:00
|
|
|
|
|
|
|
for _ in 0..100 {
|
2018-05-23 14:04:07 -06:00
|
|
|
assert_eq!(rng0.new_key(), rng1.new_key());
|
2018-05-11 12:55:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 14:04:51 -06:00
|
|
|
fn gen_n_pubkeys(seed: [u8; 32], n: i64) -> HashSet<PublicKey> {
|
|
|
|
GenKeys::new(seed)
|
2018-05-23 14:04:07 -06:00
|
|
|
.gen_n_keypairs(n)
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| x.pubkey())
|
|
|
|
.collect()
|
|
|
|
}
|
2018-05-11 12:55:05 -04:00
|
|
|
|
2018-05-23 14:04:07 -06:00
|
|
|
#[test]
|
|
|
|
fn test_gen_n_pubkeys_deterministic() {
|
2018-06-11 14:04:51 -06:00
|
|
|
let seed = [0u8; 32];
|
|
|
|
assert_eq!(gen_n_pubkeys(seed, 50), gen_n_pubkeys(seed, 50));
|
2018-05-11 12:55:05 -04:00
|
|
|
}
|
|
|
|
}
|