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-04-11 20:24:14 -06:00
|
|
|
use generic_array::GenericArray;
|
2018-05-10 13:01:42 -07:00
|
|
|
use generic_array::typenum::{U32, U64};
|
2018-03-06 12:48:26 -07:00
|
|
|
use ring::signature::Ed25519KeyPair;
|
|
|
|
use ring::{rand, signature};
|
|
|
|
use untrusted;
|
|
|
|
|
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-05-10 17:15:53 -07:00
|
|
|
let pkcs8_bytes = signature::Ed25519KeyPair::generate_pkcs8(&rng).expect("generate_pkcs8 in signature pb fn new");
|
|
|
|
signature::Ed25519KeyPair::from_pkcs8(untrusted::Input::from(&pkcs8_bytes)).expect("from_pcks8 in signature pb fn new")
|
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 {
|
|
|
|
let peer_public_key = untrusted::Input::from(peer_public_key_bytes);
|
|
|
|
let msg = untrusted::Input::from(msg_bytes);
|
|
|
|
let sig = untrusted::Input::from(self);
|
|
|
|
signature::verify(&signature::ED25519, peer_public_key, msg, sig).is_ok()
|
|
|
|
}
|
2018-03-06 12:48:26 -07:00
|
|
|
}
|