Remove the DSAPubKey/DSAPublicKey split.

This commit is contained in:
2019-04-13 15:53:17 -07:00
parent 6613f85ff3
commit 026b321f7c
8 changed files with 45 additions and 82 deletions

View File

@@ -17,16 +17,16 @@ use super::KeyPair;
pub struct DSAKeyPair<P: DSAParameters> pub struct DSAKeyPair<P: DSAParameters>
{ {
pub private: DSAPrivKey<P>, pub private: DSAPrivateKey<P>,
pub public: DSAPubKey<P> pub public: DSAPublicKey<P>
} }
impl<P: DSAParameters> KeyPair for DSAKeyPair<P> impl<P: DSAParameters> KeyPair for DSAKeyPair<P>
{ {
type Private = DSAPrivKey<P>; type Private = DSAPrivateKey<P>;
type Public = DSAPubKey<P>; type Public = DSAPublicKey<P>;
fn new(public: DSAPubKey<P>, private: DSAPrivKey<P>) -> DSAKeyPair<P> fn new(public: DSAPublicKey<P>, private: DSAPrivateKey<P>) -> DSAKeyPair<P>
{ {
DSAKeyPair{ private, public } DSAKeyPair{ private, public }
} }
@@ -67,8 +67,8 @@ macro_rules! generate_dsa_pair {
// 7. y = g^x mod p // 7. y = g^x mod p
let y = params.g.modexp(&$ltype::from(&x), &params.p); let y = params.g.modexp(&$ltype::from(&x), &params.p);
// 8. Return SUCCESS, x, and y. // 8. Return SUCCESS, x, and y.
let private = DSAPrivKey::new(params.clone(), x); let private = DSAPrivateKey::<$ptype>::new(params.clone(), x);
let public = DSAPubKey::new(params.clone(), y); let public = DSAPublicKey::<$ptype>::new(params.clone(), y);
DSAKeyPair { private, public } DSAKeyPair { private, public }
} }
} }

View File

@@ -5,49 +5,29 @@ use dsa::params::*;
use dsa::rfc6979::*; use dsa::rfc6979::*;
use hmac::{Hmac,Mac}; use hmac::{Hmac,Mac};
pub trait DSAPrivateKey { pub struct DSAPrivateKey<Params: DSAParameters>
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<Hash>(&self, m: &[u8]) -> DSASignature<Self::N>
where
Hash: BlockInput + Clone + Default + Digest + FixedOutput + Input + Reset,
Hmac<Hash>: Mac;
}
pub struct DSAPrivKey<Params: DSAParameters>
{ {
pub(crate) params: Params, pub(crate) params: Params,
pub(crate) x: Params::N pub(crate) x: Params::N
} }
pub enum DSAPrivate { pub enum DSAPrivate {
DSA1024Private(DSAPrivKey<L1024N160>), DSA1024Private(DSAPrivateKey<L1024N160>),
DSA2048SmallPrivate(DSAPrivKey<L2048N224>), DSA2048SmallPrivate(DSAPrivateKey<L2048N224>),
DSA2048Private(DSAPrivKey<L2048N256>), DSA2048Private(DSAPrivateKey<L2048N256>),
DSA3072Private(DSAPrivKey<L3072N256>) DSA3072Private(DSAPrivateKey<L3072N256>)
} }
macro_rules! privkey_impls { macro_rules! privkey_impls {
($ptype: ident, $ltype: ident, $ntype: ident, $big: ident, $bigger: ident, $biggest: ident) => { ($ptype: ident, $ltype: ident, $ntype: ident, $big: ident, $bigger: ident, $biggest: ident) => {
impl DSAPrivateKey for DSAPrivKey<$ptype> impl DSAPrivateKey<$ptype>
{ {
type Params = $ptype; pub fn new(params: $ptype, x: $ntype) -> DSAPrivateKey<$ptype>
type L = $ltype;
type N = $ntype;
fn new(params: $ptype, x: $ntype) -> DSAPrivKey<$ptype>
{ {
DSAPrivKey{ params, x } DSAPrivateKey{ params, x }
} }
fn sign<Hash>(&self, m: &[u8]) -> DSASignature<$ntype> pub fn sign<Hash>(&self, m: &[u8]) -> DSASignature<$ntype>
where where
Hash: BlockInput + Clone + Default + Digest + FixedOutput + Input + Reset, Hash: BlockInput + Clone + Default + Digest + FixedOutput + Input + Reset,
Hmac<Hash>: Mac Hmac<Hash>: Mac
@@ -155,7 +135,7 @@ macro_rules! generate_tests {
let s = $nt::from_bytes(sbytes); let s = $nt::from_bytes(sbytes);
let params = $params::new(p,g,q); 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 { let sig = match h {
224 => private.sign::<Sha224>(mbytes), 224 => private.sign::<Sha224>(mbytes),
256 => private.sign::<Sha256>(mbytes), 256 => private.sign::<Sha256>(mbytes),

View File

@@ -7,45 +7,28 @@ use simple_asn1::{ASN1Block,ASN1Class,ASN1EncodeErr,ToASN1};
use std::cmp::min; use std::cmp::min;
use utils::TranslateNums; use utils::TranslateNums;
pub trait DSAPublicKey { pub struct DSAPublicKey<Params: DSAParameters> {
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<Hash>(&self, m: &[u8], sig: &DSASignature<Self::N>) -> bool
where Hash: Digest;
}
pub struct DSAPubKey<Params: DSAParameters> {
pub(crate) params: Params, pub(crate) params: Params,
pub(crate) y: Params::L pub(crate) y: Params::L
} }
pub enum DSAPublic { pub enum DSAPublic {
DSAPublicL1024N160(DSAPubKey<L1024N160>), DSAPublicL1024N160(DSAPublicKey<L1024N160>),
DSAPublicL2048N224(DSAPubKey<L2048N224>), DSAPublicL2048N224(DSAPublicKey<L2048N224>),
DSAPublicL2048N256(DSAPubKey<L2048N256>), DSAPublicL2048N256(DSAPublicKey<L2048N256>),
DSAPublicL3072N256(DSAPubKey<L3072N256>) DSAPublicL3072N256(DSAPublicKey<L3072N256>)
} }
macro_rules! pubkey_impls { macro_rules! pubkey_impls {
($ptype: ident, $ltype: ident, $ntype: ident, $dbl: ident, $bdbl: ident) => { ($ptype: ident, $ltype: ident, $ntype: ident, $dbl: ident, $bdbl: ident) => {
impl DSAPublicKey for DSAPubKey<$ptype> impl DSAPublicKey<$ptype>
{ {
type Params = $ptype; pub fn new(params: $ptype, y: $ltype) -> DSAPublicKey<$ptype>
type L = $ltype;
type N = $ntype;
fn new(params: $ptype, y: $ltype) -> DSAPubKey<$ptype>
{ {
DSAPubKey{ params, y } DSAPublicKey{ params, y }
} }
fn verify<Hash>(&self, m: &[u8], sig: &DSASignature<$ntype>) -> bool pub fn verify<Hash>(&self, m: &[u8], sig: &DSASignature<$ntype>) -> bool
where Hash: Digest where Hash: Digest
{ {
if sig.r >= self.params.q { 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; type Error = ASN1EncodeErr;
fn to_asn1_class(&self, c: ASN1Class) fn to_asn1_class(&self, c: ASN1Class)
@@ -136,7 +119,7 @@ macro_rules! generate_tests {
let s = $nt::from_bytes(sbytes); let s = $nt::from_bytes(sbytes);
let params = $params::new(p,g,q); 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); let sig = DSASignature::<$nt>::new(r, s);
match h { match h {
224 => assert!(public.verify::<Sha224>(mbytes, &sig)), 224 => assert!(public.verify::<Sha224>(mbytes, &sig)),

View File

@@ -4,8 +4,8 @@ use sha1::Sha1;
use sha2::{Sha224,Sha256,Sha384,Sha512}; use sha2::{Sha224,Sha256,Sha384,Sha512};
use simple_asn1::{der_decode,der_encode}; use simple_asn1::{der_decode,der_encode};
use dsa::params::{DSAParameters,L1024N160,L2048N256}; use dsa::params::{DSAParameters,L1024N160,L2048N256};
use dsa::private::{DSAPrivateKey,DSAPrivKey}; use dsa::private::DSAPrivateKey;
use dsa::public::{DSAPublicKey,DSAPubKey}; use dsa::public::DSAPublicKey;
use dsa::rfc6979::KIterator; use dsa::rfc6979::KIterator;
macro_rules! run_rfc6979_test { macro_rules! run_rfc6979_test {
@@ -99,8 +99,8 @@ fn appendix_a21() {
let params = L1024N160::new(p, g, q); let params = L1024N160::new(p, g, q);
let x = U192::from_bytes(&xbytes); let x = U192::from_bytes(&xbytes);
let y = U1024::from_bytes(&ybytes); let y = U1024::from_bytes(&ybytes);
let private = DSAPrivKey::<L1024N160>::new(params.clone(), x); let private = DSAPrivateKey::<L1024N160>::new(params.clone(), x);
let public = DSAPubKey::<L1024N160>::new(params.clone(), y); let public = DSAPublicKey::<L1024N160>::new(params.clone(), y);
// //
let sample: [u8; 6] = [115, 97, 109, 112, 108, 101]; // "sample", ASCII let sample: [u8; 6] = [115, 97, 109, 112, 108, 101]; // "sample", ASCII
let test: [u8; 4] = [116, 101, 115, 116]; // "test", 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 params = L2048N256::new(p, g, q);
let x = U256::from_bytes(&xbytes); let x = U256::from_bytes(&xbytes);
let y = U2048::from_bytes(&ybytes); let y = U2048::from_bytes(&ybytes);
let private = DSAPrivKey::<L2048N256>::new(params.clone(), x); let private = DSAPrivateKey::<L2048N256>::new(params.clone(), x);
let public = DSAPubKey::<L2048N256>::new(params.clone(), y); let public = DSAPublicKey::<L2048N256>::new(params.clone(), y);
// //
let sample: [u8; 6] = [115, 97, 109, 112, 108, 101]; // "sample", ASCII let sample: [u8; 6] = [115, 97, 109, 112, 108, 101]; // "sample", ASCII
let test: [u8; 4] = [116, 101, 115, 116]; // "test", ASCII let test: [u8; 4] = [116, 101, 115, 116]; // "test", ASCII

View File

@@ -1,5 +1,5 @@
use cryptonum::unsigned::*; 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 std::io::{Read,Write};
use ssh::errors::{SSHKeyParseError,SSHKeyRenderError}; use ssh::errors::{SSHKeyParseError,SSHKeyRenderError};
use ssh::frame::*; use ssh::frame::*;
@@ -22,7 +22,7 @@ impl SSHKey for DSAKeyPair<L1024N160> {
let pubparams = L1024N160::new(pubp, pubg, pubq); let pubparams = L1024N160::new(pubp, pubg, pubq);
let puby: U1024 = parse_openssh_number(inp)?; let puby: U1024 = parse_openssh_number(inp)?;
for _ in inp.bytes() { return Err(SSHKeyParseError::UnknownTrailingData); } for _ in inp.bytes() { return Err(SSHKeyParseError::UnknownTrailingData); }
Ok(DSAPubKey::<L1024N160>::new(pubparams.clone(), puby.clone())) Ok(DSAPublicKey::<L1024N160>::new(pubparams.clone(), puby.clone()))
} }
fn parse_ssh_private_info<I: Read>(inp: &mut I) -> Result<(Self::Private,String),SSHKeyParseError> fn parse_ssh_private_info<I: Read>(inp: &mut I) -> Result<(Self::Private,String),SSHKeyParseError>
@@ -43,7 +43,7 @@ impl SSHKey for DSAKeyPair<L1024N160> {
let _ = parse_openssh_buffer(inp)?; // a copy of y we don't need let _ = parse_openssh_buffer(inp)?; // a copy of y we don't need
let privx = parse_openssh_number(inp)?; let privx = parse_openssh_number(inp)?;
let privkey = DSAPrivKey::<L1024N160>::new(privparams, privx); let privkey = DSAPrivateKey::<L1024N160>::new(privparams, privx);
let comment = parse_openssh_string(inp)?; let comment = parse_openssh_string(inp)?;
for (idx,byte) in inp.bytes().enumerate() { for (idx,byte) in inp.bytes().enumerate() {
if ((idx+1) as u8) != byte? { if ((idx+1) as u8) != byte? {

View File

@@ -142,7 +142,7 @@ pub fn write_ssh_keyfile<KP,P>(path: P, x: &KP, comment: &str) -> Result<(),SSHK
#[cfg(test)] #[cfg(test)]
use dsa::{DSAKeyPair,DSAPublicKey,DSAPrivateKey,DSAPubKey,L1024N160}; use dsa::{DSAKeyPair,DSAPublicKey,L1024N160};
#[cfg(test)] #[cfg(test)]
use sha2::Sha256; use sha2::Sha256;
@@ -183,7 +183,7 @@ fn read_dsa_examples() {
match load_ssh_pubkeys::<DSAKeyPair<L1024N160>,String>(ppath) { match load_ssh_pubkeys::<DSAKeyPair<L1024N160>,String>(ppath) {
Err(e4) => assert!(false, format!("pubkey error: {:?}", e4)), Err(e4) => assert!(false, format!("pubkey error: {:?}", e4)),
Ok(pubkeys) => { Ok(pubkeys) => {
let _ : Vec<(DSAPubKey<L1024N160>,String)> = pubkeys; let _ : Vec<(DSAPublicKey<L1024N160>,String)> = pubkeys;
for (pubkey, comment3) in pubkeys { for (pubkey, comment3) in pubkeys {
assert_eq!(pubkey.params.p, keypair.public.params.p, "public key check (p)"); 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)"); assert_eq!(pubkey.params.q, keypair.public.params.q, "public key check (q)");

View File

@@ -6,7 +6,7 @@ mod name;
mod publickey; mod publickey;
mod validity; mod validity;
use dsa::{DSAPublic,DSAPublicKey}; use dsa::DSAPublic;
use ecdsa::{ECDSAPublic,ECCPublicKey}; use ecdsa::{ECDSAPublic,ECCPublicKey};
use rsa::{SIGNING_HASH_SHA1,SIGNING_HASH_SHA224,SIGNING_HASH_SHA256,SIGNING_HASH_SHA384,SIGNING_HASH_SHA512}; use rsa::{SIGNING_HASH_SHA1,SIGNING_HASH_SHA224,SIGNING_HASH_SHA256,SIGNING_HASH_SHA384,SIGNING_HASH_SHA512};
use sha1::Sha1; use sha1::Sha1;

View File

@@ -1,5 +1,5 @@
use cryptonum::unsigned::{U3072,U2048,U1024,U256,U192}; 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 dsa::{L3072N256,L2048N256,L2048N224,L1024N160};
use ecdsa::{ECDSAEncodeErr,ECDSAPublic,ECCPubKey}; use ecdsa::{ECDSAEncodeErr,ECDSAPublic,ECCPubKey};
use ecdsa::curve::{P192,P224,P256,P384,P521}; use ecdsa::curve::{P192,P224,P256,P384,P521};
@@ -174,7 +174,7 @@ fn decode_dsa_key(info: ASN1Block, key: &ASN1Block) -> Result<DSAPublic,X509Pars
let (iblk,_) = blocks.split_first().ok_or(X509ParseError::InvalidDSAKey)?; let (iblk,_) = blocks.split_first().ok_or(X509ParseError::InvalidDSAKey)?;
if let ASN1Block::Integer(_,_,ynum) = iblk { if let ASN1Block::Integer(_,_,ynum) = iblk {
let y = U3072::from_num(ynum).ok_or(X509ParseError::InvalidDSAKey)?; let y = U3072::from_num(ynum).ok_or(X509ParseError::InvalidDSAKey)?;
let key = DSAPubKey::<L3072N256>::new(params, y); let key = DSAPublicKey::<L3072N256>::new(params, y);
let reskey = DSAPublic::DSAPublicL3072N256(key); let reskey = DSAPublic::DSAPublicL3072N256(key);
return Ok(reskey); return Ok(reskey);
} }
@@ -195,7 +195,7 @@ fn decode_dsa_key(info: ASN1Block, key: &ASN1Block) -> Result<DSAPublic,X509Pars
let (iblk,_) = blocks.split_first().ok_or(X509ParseError::InvalidDSAKey)?; let (iblk,_) = blocks.split_first().ok_or(X509ParseError::InvalidDSAKey)?;
if let ASN1Block::Integer(_,_,ynum) = iblk { if let ASN1Block::Integer(_,_,ynum) = iblk {
let y = U2048::from_num(ynum).ok_or(X509ParseError::InvalidDSAKey)?; let y = U2048::from_num(ynum).ok_or(X509ParseError::InvalidDSAKey)?;
let key = DSAPubKey::<L2048N256>::new(params, y); let key = DSAPublicKey::<L2048N256>::new(params, y);
let reskey = DSAPublic::DSAPublicL2048N256(key); let reskey = DSAPublic::DSAPublicL2048N256(key);
return Ok(reskey); return Ok(reskey);
} }
@@ -213,7 +213,7 @@ fn decode_dsa_key(info: ASN1Block, key: &ASN1Block) -> Result<DSAPublic,X509Pars
let (iblk,_) = blocks.split_first().ok_or(X509ParseError::InvalidDSAKey)?; let (iblk,_) = blocks.split_first().ok_or(X509ParseError::InvalidDSAKey)?;
if let ASN1Block::Integer(_,_,ynum) = iblk { if let ASN1Block::Integer(_,_,ynum) = iblk {
let y = U2048::from_num(ynum).ok_or(X509ParseError::InvalidDSAKey)?; let y = U2048::from_num(ynum).ok_or(X509ParseError::InvalidDSAKey)?;
let key = DSAPubKey::<L2048N224>::new(params, y); let key = DSAPublicKey::<L2048N224>::new(params, y);
let reskey = DSAPublic::DSAPublicL2048N224(key); let reskey = DSAPublic::DSAPublicL2048N224(key);
return Ok(reskey); return Ok(reskey);
} }
@@ -233,7 +233,7 @@ fn decode_dsa_key(info: ASN1Block, key: &ASN1Block) -> Result<DSAPublic,X509Pars
let (iblk,_) = blocks.split_first().ok_or(X509ParseError::InvalidDSAKey)?; let (iblk,_) = blocks.split_first().ok_or(X509ParseError::InvalidDSAKey)?;
if let ASN1Block::Integer(_,_,ynum) = iblk { if let ASN1Block::Integer(_,_,ynum) = iblk {
let y = U1024::from_num(ynum).ok_or(X509ParseError::InvalidDSAKey)?; let y = U1024::from_num(ynum).ok_or(X509ParseError::InvalidDSAKey)?;
let key = DSAPubKey::<L1024N160>::new(params, y); let key = DSAPublicKey::<L1024N160>::new(params, y);
let reskey = DSAPublic::DSAPublicL1024N160(key); let reskey = DSAPublic::DSAPublicL1024N160(key);
return Ok(reskey); return Ok(reskey);
} }