diff --git a/src/dsa/mod.rs b/src/dsa/mod.rs index 54ae7e5..94de91f 100644 --- a/src/dsa/mod.rs +++ b/src/dsa/mod.rs @@ -17,16 +17,16 @@ use super::KeyPair; pub struct DSAKeyPair { - pub private: DSAPrivKey

, - pub public: DSAPubKey

+ pub private: DSAPrivateKey

, + pub public: DSAPublicKey

} impl KeyPair for DSAKeyPair

{ - type Private = DSAPrivKey

; - type Public = DSAPubKey

; + type Private = DSAPrivateKey

; + type Public = DSAPublicKey

; - fn new(public: DSAPubKey

, private: DSAPrivKey

) -> DSAKeyPair

+ fn new(public: DSAPublicKey

, private: DSAPrivateKey

) -> DSAKeyPair

{ DSAKeyPair{ private, public } } @@ -67,8 +67,8 @@ macro_rules! generate_dsa_pair { // 7. y = g^x mod p let y = params.g.modexp(&$ltype::from(&x), ¶ms.p); // 8. Return SUCCESS, x, and y. - let private = DSAPrivKey::new(params.clone(), x); - let public = DSAPubKey::new(params.clone(), y); + let private = DSAPrivateKey::<$ptype>::new(params.clone(), x); + let public = DSAPublicKey::<$ptype>::new(params.clone(), y); DSAKeyPair { private, public } } } diff --git a/src/dsa/private.rs b/src/dsa/private.rs index 84ec652..cc7400e 100644 --- a/src/dsa/private.rs +++ b/src/dsa/private.rs @@ -5,49 +5,29 @@ use dsa::params::*; use dsa::rfc6979::*; use hmac::{Hmac,Mac}; -pub trait DSAPrivateKey { - type Params; - type L; - type N; - - /// Generate a new private key using the given DSA parameters and private - /// key value. - fn new(params: Self::Params, x: Self::N) -> Self; - /// Generate a DSA signature for the given message, using the appropriate - /// hash included in the type invocation. - fn sign(&self, m: &[u8]) -> DSASignature - where - Hash: BlockInput + Clone + Default + Digest + FixedOutput + Input + Reset, - Hmac: Mac; -} - -pub struct DSAPrivKey +pub struct DSAPrivateKey { pub(crate) params: Params, pub(crate) x: Params::N } pub enum DSAPrivate { - DSA1024Private(DSAPrivKey), - DSA2048SmallPrivate(DSAPrivKey), - DSA2048Private(DSAPrivKey), - DSA3072Private(DSAPrivKey) + DSA1024Private(DSAPrivateKey), + DSA2048SmallPrivate(DSAPrivateKey), + DSA2048Private(DSAPrivateKey), + DSA3072Private(DSAPrivateKey) } macro_rules! privkey_impls { ($ptype: ident, $ltype: ident, $ntype: ident, $big: ident, $bigger: ident, $biggest: ident) => { - impl DSAPrivateKey for DSAPrivKey<$ptype> + impl DSAPrivateKey<$ptype> { - type Params = $ptype; - type L = $ltype; - type N = $ntype; - - fn new(params: $ptype, x: $ntype) -> DSAPrivKey<$ptype> + pub fn new(params: $ptype, x: $ntype) -> DSAPrivateKey<$ptype> { - DSAPrivKey{ params, x } + DSAPrivateKey{ params, x } } - fn sign(&self, m: &[u8]) -> DSASignature<$ntype> + pub fn sign(&self, m: &[u8]) -> DSASignature<$ntype> where Hash: BlockInput + Clone + Default + Digest + FixedOutput + Input + Reset, Hmac: Mac @@ -155,7 +135,7 @@ macro_rules! generate_tests { let s = $nt::from_bytes(sbytes); let params = $params::new(p,g,q); - let private = DSAPrivKey::<$params>::new(params, x); + let private = DSAPrivateKey::<$params>::new(params, x); let sig = match h { 224 => private.sign::(mbytes), 256 => private.sign::(mbytes), diff --git a/src/dsa/public.rs b/src/dsa/public.rs index 8538d96..afcc418 100644 --- a/src/dsa/public.rs +++ b/src/dsa/public.rs @@ -7,45 +7,28 @@ use simple_asn1::{ASN1Block,ASN1Class,ASN1EncodeErr,ToASN1}; use std::cmp::min; use utils::TranslateNums; -pub trait DSAPublicKey { - type Params : DSAParameters; - type L; - type N; - - /// Generate a new public key given the parameters and public value. - fn new(params: Self::Params, y: Self::L) -> Self; - /// Verify the given signature against the given message, using the - /// appropriate hash function. - fn verify(&self, m: &[u8], sig: &DSASignature) -> bool - where Hash: Digest; -} - -pub struct DSAPubKey { +pub struct DSAPublicKey { pub(crate) params: Params, pub(crate) y: Params::L } pub enum DSAPublic { - DSAPublicL1024N160(DSAPubKey), - DSAPublicL2048N224(DSAPubKey), - DSAPublicL2048N256(DSAPubKey), - DSAPublicL3072N256(DSAPubKey) + DSAPublicL1024N160(DSAPublicKey), + DSAPublicL2048N224(DSAPublicKey), + DSAPublicL2048N256(DSAPublicKey), + DSAPublicL3072N256(DSAPublicKey) } macro_rules! pubkey_impls { ($ptype: ident, $ltype: ident, $ntype: ident, $dbl: ident, $bdbl: ident) => { - impl DSAPublicKey for DSAPubKey<$ptype> + impl DSAPublicKey<$ptype> { - type Params = $ptype; - type L = $ltype; - type N = $ntype; - - fn new(params: $ptype, y: $ltype) -> DSAPubKey<$ptype> + pub fn new(params: $ptype, y: $ltype) -> DSAPublicKey<$ptype> { - DSAPubKey{ params, y } + DSAPublicKey{ params, y } } - fn verify(&self, m: &[u8], sig: &DSASignature<$ntype>) -> bool + pub fn verify(&self, m: &[u8], sig: &DSASignature<$ntype>) -> bool where Hash: Digest { if sig.r >= self.params.q { @@ -80,7 +63,7 @@ macro_rules! pubkey_impls { } } - impl ToASN1 for DSAPubKey<$ptype> { + impl ToASN1 for DSAPublicKey<$ptype> { type Error = ASN1EncodeErr; fn to_asn1_class(&self, c: ASN1Class) @@ -136,7 +119,7 @@ macro_rules! generate_tests { let s = $nt::from_bytes(sbytes); let params = $params::new(p,g,q); - let public = DSAPubKey::<$params>::new(params, y); + let public = DSAPublicKey::<$params>::new(params, y); let sig = DSASignature::<$nt>::new(r, s); match h { 224 => assert!(public.verify::(mbytes, &sig)), diff --git a/src/dsa/tests.rs b/src/dsa/tests.rs index f45fdb3..996aad2 100644 --- a/src/dsa/tests.rs +++ b/src/dsa/tests.rs @@ -4,8 +4,8 @@ use sha1::Sha1; use sha2::{Sha224,Sha256,Sha384,Sha512}; use simple_asn1::{der_decode,der_encode}; use dsa::params::{DSAParameters,L1024N160,L2048N256}; -use dsa::private::{DSAPrivateKey,DSAPrivKey}; -use dsa::public::{DSAPublicKey,DSAPubKey}; +use dsa::private::DSAPrivateKey; +use dsa::public::DSAPublicKey; use dsa::rfc6979::KIterator; macro_rules! run_rfc6979_test { @@ -99,8 +99,8 @@ fn appendix_a21() { let params = L1024N160::new(p, g, q); let x = U192::from_bytes(&xbytes); let y = U1024::from_bytes(&ybytes); - let private = DSAPrivKey::::new(params.clone(), x); - let public = DSAPubKey::::new(params.clone(), y); + let private = DSAPrivateKey::::new(params.clone(), x); + let public = DSAPublicKey::::new(params.clone(), y); // let sample: [u8; 6] = [115, 97, 109, 112, 108, 101]; // "sample", ASCII let test: [u8; 4] = [116, 101, 115, 116]; // "test", ASCII @@ -359,8 +359,8 @@ fn appendix_a22() { let params = L2048N256::new(p, g, q); let x = U256::from_bytes(&xbytes); let y = U2048::from_bytes(&ybytes); - let private = DSAPrivKey::::new(params.clone(), x); - let public = DSAPubKey::::new(params.clone(), y); + let private = DSAPrivateKey::::new(params.clone(), x); + let public = DSAPublicKey::::new(params.clone(), y); // let sample: [u8; 6] = [115, 97, 109, 112, 108, 101]; // "sample", ASCII let test: [u8; 4] = [116, 101, 115, 116]; // "test", ASCII diff --git a/src/ssh/dsa.rs b/src/ssh/dsa.rs index 12fd1a0..99ed967 100644 --- a/src/ssh/dsa.rs +++ b/src/ssh/dsa.rs @@ -1,5 +1,5 @@ use cryptonum::unsigned::*; -use dsa::{DSAKeyPair,DSAParameters,DSAPubKey,DSAPublicKey,DSAPrivKey,DSAPrivateKey,L1024N160}; +use dsa::{DSAKeyPair,DSAParameters,DSAPublicKey,DSAPrivateKey,L1024N160}; use std::io::{Read,Write}; use ssh::errors::{SSHKeyParseError,SSHKeyRenderError}; use ssh::frame::*; @@ -22,7 +22,7 @@ impl SSHKey for DSAKeyPair { let pubparams = L1024N160::new(pubp, pubg, pubq); let puby: U1024 = parse_openssh_number(inp)?; for _ in inp.bytes() { return Err(SSHKeyParseError::UnknownTrailingData); } - Ok(DSAPubKey::::new(pubparams.clone(), puby.clone())) + Ok(DSAPublicKey::::new(pubparams.clone(), puby.clone())) } fn parse_ssh_private_info(inp: &mut I) -> Result<(Self::Private,String),SSHKeyParseError> @@ -43,7 +43,7 @@ impl SSHKey for DSAKeyPair { let _ = parse_openssh_buffer(inp)?; // a copy of y we don't need let privx = parse_openssh_number(inp)?; - let privkey = DSAPrivKey::::new(privparams, privx); + let privkey = DSAPrivateKey::::new(privparams, privx); let comment = parse_openssh_string(inp)?; for (idx,byte) in inp.bytes().enumerate() { if ((idx+1) as u8) != byte? { diff --git a/src/ssh/mod.rs b/src/ssh/mod.rs index 0606aef..aa77e74 100644 --- a/src/ssh/mod.rs +++ b/src/ssh/mod.rs @@ -142,7 +142,7 @@ pub fn write_ssh_keyfile(path: P, x: &KP, comment: &str) -> Result<(),SSHK #[cfg(test)] -use dsa::{DSAKeyPair,DSAPublicKey,DSAPrivateKey,DSAPubKey,L1024N160}; +use dsa::{DSAKeyPair,DSAPublicKey,L1024N160}; #[cfg(test)] use sha2::Sha256; @@ -183,7 +183,7 @@ fn read_dsa_examples() { match load_ssh_pubkeys::,String>(ppath) { Err(e4) => assert!(false, format!("pubkey error: {:?}", e4)), Ok(pubkeys) => { - let _ : Vec<(DSAPubKey,String)> = pubkeys; + let _ : Vec<(DSAPublicKey,String)> = pubkeys; for (pubkey, comment3) in pubkeys { assert_eq!(pubkey.params.p, keypair.public.params.p, "public key check (p)"); assert_eq!(pubkey.params.q, keypair.public.params.q, "public key check (q)"); diff --git a/src/x509/mod.rs b/src/x509/mod.rs index 5a0580e..978d51d 100644 --- a/src/x509/mod.rs +++ b/src/x509/mod.rs @@ -6,7 +6,7 @@ mod name; mod publickey; mod validity; -use dsa::{DSAPublic,DSAPublicKey}; +use dsa::DSAPublic; use ecdsa::{ECDSAPublic,ECCPublicKey}; use rsa::{SIGNING_HASH_SHA1,SIGNING_HASH_SHA224,SIGNING_HASH_SHA256,SIGNING_HASH_SHA384,SIGNING_HASH_SHA512}; use sha1::Sha1; diff --git a/src/x509/publickey.rs b/src/x509/publickey.rs index e821111..bc12cbd 100644 --- a/src/x509/publickey.rs +++ b/src/x509/publickey.rs @@ -1,5 +1,5 @@ use cryptonum::unsigned::{U3072,U2048,U1024,U256,U192}; -use dsa::{DSAPublic,DSAPublicKey,DSAPubKey,DSAParameters}; +use dsa::{DSAPublic,DSAPublicKey,DSAParameters}; use dsa::{L3072N256,L2048N256,L2048N224,L1024N160}; use ecdsa::{ECDSAEncodeErr,ECDSAPublic,ECCPubKey}; use ecdsa::curve::{P192,P224,P256,P384,P521}; @@ -174,7 +174,7 @@ fn decode_dsa_key(info: ASN1Block, key: &ASN1Block) -> Result::new(params, y); + let key = DSAPublicKey::::new(params, y); let reskey = DSAPublic::DSAPublicL3072N256(key); return Ok(reskey); } @@ -195,7 +195,7 @@ fn decode_dsa_key(info: ASN1Block, key: &ASN1Block) -> Result::new(params, y); + let key = DSAPublicKey::::new(params, y); let reskey = DSAPublic::DSAPublicL2048N256(key); return Ok(reskey); } @@ -213,7 +213,7 @@ fn decode_dsa_key(info: ASN1Block, key: &ASN1Block) -> Result::new(params, y); + let key = DSAPublicKey::::new(params, y); let reskey = DSAPublic::DSAPublicL2048N224(key); return Ok(reskey); } @@ -233,7 +233,7 @@ fn decode_dsa_key(info: ASN1Block, key: &ASN1Block) -> Result::new(params, y); + let key = DSAPublicKey::::new(params, y); let reskey = DSAPublic::DSAPublicL1024N160(key); return Ok(reskey); }