Simplify ECDSA struct/trait split.
This commit is contained in:
@@ -10,18 +10,19 @@ use rand::Rng;
|
||||
use rand::distributions::Standard;
|
||||
use self::curve::{EllipticCurve,P192,P224,P256,P384,P521};
|
||||
use self::point::{ECCPoint,Point};
|
||||
pub use self::private::{ECCPrivateKey,ECCPrivate};
|
||||
pub use self::public::{ECCPublicKey,ECDSAPublic,ECCPubKey};
|
||||
pub use self::private::ECCPrivateKey;
|
||||
pub use self::public::{ECDSAPublic,ECCPublicKey};
|
||||
pub use self::public::{ECDSADecodeErr,ECDSAEncodeErr};
|
||||
|
||||
pub trait ECDSAKeyPair<Public,Private> {
|
||||
fn generate<G: Rng>(g: &mut G) -> (Public, Private);
|
||||
pub struct ECDSAKeyPair<Curve: EllipticCurve> {
|
||||
pub public: ECCPublicKey<Curve>,
|
||||
pub private: ECCPrivateKey<Curve>
|
||||
}
|
||||
|
||||
macro_rules! generate_impl {
|
||||
($curve: ident, $un: ident, $si: ident) => {
|
||||
impl ECDSAKeyPair<ECCPubKey<$curve>,ECCPrivate<$curve>> for $curve {
|
||||
fn generate<G: Rng>(rng: &mut G) -> (ECCPubKey<$curve>, ECCPrivate<$curve>)
|
||||
impl ECDSAKeyPair<$curve> {
|
||||
pub fn generate<G: Rng>(rng: &mut G) -> ECDSAKeyPair<$curve>
|
||||
{
|
||||
loop {
|
||||
let size = ($curve::size() + 7) / 8;
|
||||
@@ -38,9 +39,10 @@ macro_rules! generate_impl {
|
||||
|
||||
let d = $si::from(&proposed_d);
|
||||
let public_point = Point::<$curve>::default().scale(&d);
|
||||
let public = ECCPubKey::<$curve>::new(public_point);
|
||||
let private = ECCPrivate::<$curve>::new(proposed_d);
|
||||
return (public, private);
|
||||
let public = ECCPublicKey::<$curve>::new(public_point);
|
||||
let private = ECCPrivateKey::<$curve>::new(proposed_d);
|
||||
|
||||
return ECDSAKeyPair{ public, private };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,33 +6,21 @@ use ecdsa::curve::{EllipticCurve,P192,P224,P256,P384,P521};
|
||||
use ecdsa::point::{ECCPoint,Point};
|
||||
use hmac::{Hmac,Mac};
|
||||
|
||||
pub struct ECCPrivate<Curve: EllipticCurve> {
|
||||
pub struct ECCPrivateKey<Curve: EllipticCurve> {
|
||||
d: Curve::Unsigned
|
||||
}
|
||||
|
||||
pub trait ECCPrivateKey {
|
||||
type Unsigned;
|
||||
|
||||
fn new(d: Self::Unsigned) -> Self;
|
||||
fn sign<Hash>(&self, m: &[u8]) -> DSASignature<Self::Unsigned>
|
||||
where
|
||||
Hash: BlockInput + Clone + Default + Digest + FixedOutput + Input + Reset,
|
||||
Hmac<Hash>: Mac;
|
||||
}
|
||||
|
||||
macro_rules! generate_privates
|
||||
{
|
||||
($curve: ident, $base: ident, $sig: ident, $dbl: ident, $quad: ident) => {
|
||||
impl ECCPrivateKey for ECCPrivate<$curve>
|
||||
impl ECCPrivateKey<$curve>
|
||||
{
|
||||
type Unsigned = $base;
|
||||
|
||||
fn new(d: $base) -> ECCPrivate<$curve>
|
||||
pub fn new(d: $base) -> ECCPrivateKey<$curve>
|
||||
{
|
||||
ECCPrivate{ d }
|
||||
ECCPrivateKey{ d }
|
||||
}
|
||||
|
||||
fn sign<Hash>(&self, m: &[u8]) -> DSASignature<$base>
|
||||
pub fn sign<Hash>(&self, m: &[u8]) -> DSASignature<$base>
|
||||
where
|
||||
Hash: BlockInput + Clone + Default + Digest + FixedOutput + Input + Reset,
|
||||
Hmac<Hash>: Mac
|
||||
@@ -136,7 +124,7 @@ macro_rules! sign_test_body
|
||||
let r = $base::from_bytes(rbytes);
|
||||
let s = $base::from_bytes(sbytes);
|
||||
|
||||
let private = ECCPrivate::<$curve>::new(d);
|
||||
let private = ECCPrivateKey::<$curve>::new(d);
|
||||
let sig = match usize::from(h) {
|
||||
224 => private.sign::<Sha224>(mbytes),
|
||||
256 => private.sign::<Sha256>(mbytes),
|
||||
|
||||
@@ -8,27 +8,16 @@ use hmac::{Hmac,Mac};
|
||||
use simple_asn1::{ASN1Block,ASN1Class,ASN1DecodeErr,ASN1EncodeErr,FromASN1,ToASN1};
|
||||
use std::cmp::min;
|
||||
|
||||
pub struct ECCPubKey<Curve: EllipticCurve> {
|
||||
pub struct ECCPublicKey<Curve: EllipticCurve> {
|
||||
q: Point<Curve>
|
||||
}
|
||||
|
||||
pub enum ECDSAPublic {
|
||||
ECCPublicP192(ECCPubKey<P192>),
|
||||
ECCPublicP224(ECCPubKey<P224>),
|
||||
ECCPublicP256(ECCPubKey<P256>),
|
||||
ECCPublicP384(ECCPubKey<P384>),
|
||||
ECCPublicP521(ECCPubKey<P521>),
|
||||
}
|
||||
|
||||
pub trait ECCPublicKey {
|
||||
type Curve : EllipticCurve;
|
||||
type Unsigned;
|
||||
|
||||
fn new(d: Point<Self::Curve>) -> Self;
|
||||
fn verify<Hash>(&self, m: &[u8], sig: &DSASignature<Self::Unsigned>) -> bool
|
||||
where
|
||||
Hash: BlockInput + Clone + Default + Digest + FixedOutput + Input + Reset,
|
||||
Hmac<Hash>: Mac;
|
||||
ECCPublicP192(ECCPublicKey<P192>),
|
||||
ECCPublicP224(ECCPublicKey<P224>),
|
||||
ECCPublicP256(ECCPublicKey<P256>),
|
||||
ECCPublicP384(ECCPublicKey<P384>),
|
||||
ECCPublicP521(ECCPublicKey<P521>),
|
||||
}
|
||||
|
||||
pub enum ECDSAEncodeErr {
|
||||
@@ -58,17 +47,14 @@ impl From<ASN1DecodeErr> for ECDSADecodeErr {
|
||||
|
||||
macro_rules! public_impl {
|
||||
($curve: ident, $un: ident, $si: ident) => {
|
||||
impl ECCPublicKey for ECCPubKey<$curve>
|
||||
impl ECCPublicKey<$curve>
|
||||
{
|
||||
type Curve = $curve;
|
||||
type Unsigned = $un;
|
||||
|
||||
fn new(q: Point<$curve>) -> ECCPubKey<$curve>
|
||||
pub fn new(q: Point<$curve>) -> ECCPublicKey<$curve>
|
||||
{
|
||||
ECCPubKey{ q }
|
||||
ECCPublicKey{ q }
|
||||
}
|
||||
|
||||
fn verify<Hash>(&self, m: &[u8], sig: &DSASignature<Self::Unsigned>) -> bool
|
||||
pub fn verify<Hash>(&self, m: &[u8], sig: &DSASignature<$un>) -> bool
|
||||
where
|
||||
Hash: BlockInput + Clone + Default + Digest + FixedOutput + Input + Reset,
|
||||
Hmac<Hash>: Mac
|
||||
@@ -103,7 +89,7 @@ macro_rules! public_impl {
|
||||
}
|
||||
}
|
||||
|
||||
impl ToASN1 for ECCPubKey<$curve> {
|
||||
impl ToASN1 for ECCPublicKey<$curve> {
|
||||
type Error = ECDSAEncodeErr;
|
||||
|
||||
fn to_asn1_class(&self, c: ASN1Class) -> Result<Vec<ASN1Block>,ECDSAEncodeErr>
|
||||
@@ -136,10 +122,10 @@ macro_rules! public_impl {
|
||||
}
|
||||
}
|
||||
|
||||
impl FromASN1 for ECCPubKey<$curve> {
|
||||
impl FromASN1 for ECCPublicKey<$curve> {
|
||||
type Error = ECDSADecodeErr;
|
||||
|
||||
fn from_asn1(bs: &[ASN1Block]) -> Result<(ECCPubKey<$curve>,&[ASN1Block]),ECDSADecodeErr>
|
||||
fn from_asn1(bs: &[ASN1Block]) -> Result<(ECCPublicKey<$curve>,&[ASN1Block]),ECDSADecodeErr>
|
||||
{
|
||||
let (x, rest) = bs.split_first().ok_or(ECDSADecodeErr::NoKeyFound)?;
|
||||
if let ASN1Block::BitString(_, _, _, target) = x {
|
||||
@@ -155,7 +141,7 @@ macro_rules! public_impl {
|
||||
let x = $un::from_bytes(xbstr);
|
||||
let y = $un::from_bytes(ybstr);
|
||||
let point = Point::<$curve>{ x: $si::from(x), y: $si::from(y) };
|
||||
let res = ECCPubKey::<$curve>::new(point);
|
||||
let res = ECCPublicKey::<$curve>::new(point);
|
||||
Ok((res, rest))
|
||||
} else {
|
||||
Err(ECDSADecodeErr::InvalidKeyFormat)
|
||||
@@ -201,7 +187,7 @@ macro_rules! verify_test_body
|
||||
let s = $un::from_bytes(sbytes);
|
||||
|
||||
let point = Point::<$curve>{ x: $si::from(x), y: $si::from(y) };
|
||||
let public = ECCPubKey::<$curve>::new(point);
|
||||
let public = ECCPublicKey::<$curve>::new(point);
|
||||
let sig = DSASignature::new(r, s);
|
||||
match usize::from(h) {
|
||||
224 => assert!(public.verify::<Sha224>(mbytes, &sig)),
|
||||
|
||||
@@ -7,7 +7,7 @@ mod publickey;
|
||||
mod validity;
|
||||
|
||||
use dsa::DSAPublic;
|
||||
use ecdsa::{ECDSAPublic,ECCPublicKey};
|
||||
use ecdsa::ECDSAPublic;
|
||||
use rsa::{SIGNING_HASH_SHA1,SIGNING_HASH_SHA224,SIGNING_HASH_SHA256,SIGNING_HASH_SHA384,SIGNING_HASH_SHA512};
|
||||
use sha1::Sha1;
|
||||
use sha2::{Sha224,Sha256,Sha384,Sha512};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use cryptonum::unsigned::{U3072,U2048,U1024,U256,U192};
|
||||
use dsa::{DSAPublic,DSAPublicKey,DSAParameters};
|
||||
use dsa::{L3072N256,L2048N256,L2048N224,L1024N160};
|
||||
use ecdsa::{ECDSAEncodeErr,ECDSAPublic,ECCPubKey};
|
||||
use ecdsa::{ECDSAEncodeErr,ECDSAPublic,ECCPublicKey};
|
||||
use ecdsa::curve::{P192,P224,P256,P384,P521};
|
||||
use num::BigUint;
|
||||
use rsa::RSAPublic;
|
||||
@@ -271,27 +271,27 @@ fn decode_ecdsa_key(info: ASN1Block, keybls: &[ASN1Block]) -> Result<ECDSAPublic
|
||||
{
|
||||
if let ASN1Block::ObjectIdentifier(_, _, oid) = info {
|
||||
if oid == oid!(1,2,840,10045,3,1,1) {
|
||||
let (res, _) = ECCPubKey::<P192>::from_asn1(keybls)?;
|
||||
let (res, _) = ECCPublicKey::<P192>::from_asn1(keybls)?;
|
||||
return Ok(ECDSAPublic::ECCPublicP192(res));
|
||||
}
|
||||
|
||||
if oid == oid!(1,3,132,0,33) {
|
||||
let (res, _) = ECCPubKey::<P224>::from_asn1(keybls)?;
|
||||
let (res, _) = ECCPublicKey::<P224>::from_asn1(keybls)?;
|
||||
return Ok(ECDSAPublic::ECCPublicP224(res));
|
||||
}
|
||||
|
||||
if oid == oid!(1,2,840,10045,3,1,7) {
|
||||
let (res, _) = ECCPubKey::<P256>::from_asn1(keybls)?;
|
||||
let (res, _) = ECCPublicKey::<P256>::from_asn1(keybls)?;
|
||||
return Ok(ECDSAPublic::ECCPublicP256(res));
|
||||
}
|
||||
|
||||
if oid == oid!(1,3,132,0,34) {
|
||||
let (res, _) = ECCPubKey::<P384>::from_asn1(keybls)?;
|
||||
let (res, _) = ECCPublicKey::<P384>::from_asn1(keybls)?;
|
||||
return Ok(ECDSAPublic::ECCPublicP384(res));
|
||||
}
|
||||
|
||||
if oid == oid!(1,3,132,0,35) {
|
||||
let (res, _) = ECCPubKey::<P521>::from_asn1(keybls)?;
|
||||
let (res, _) = ECCPublicKey::<P521>::from_asn1(keybls)?;
|
||||
return Ok(ECDSAPublic::ECCPublicP521(res));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user