Clean up the DSA struct parameter types.

This commit is contained in:
2019-04-04 16:32:15 -07:00
parent 54c5244bc5
commit 68ddc7096b
6 changed files with 36 additions and 36 deletions

View File

@@ -14,10 +14,10 @@ use cryptonum::unsigned::*;
use rand::Rng; use rand::Rng;
use rand::distributions::Standard; use rand::distributions::Standard;
pub struct DSAKeyPair<P,L,N> pub struct DSAKeyPair<P: DSAParameters>
{ {
pub private: DSAPrivKey<P,N>, pub private: DSAPrivKey<P>,
pub public: DSAPubKey<P,L> pub public: DSAPubKey<P>
} }
pub trait DSAKeyGeneration pub trait DSAKeyGeneration
@@ -29,7 +29,7 @@ pub trait DSAKeyGeneration
macro_rules! generate_dsa_pair { macro_rules! generate_dsa_pair {
($ptype: ident, $ltype: ident, $ntype: ident, $nbig: ident) => { ($ptype: ident, $ltype: ident, $ntype: ident, $nbig: ident) => {
impl DSAKeyGeneration for DSAKeyPair<$ptype,$ltype,$ntype> impl DSAKeyGeneration for DSAKeyPair<$ptype>
{ {
type Params = $ptype; type Params = $ptype;

View File

@@ -21,28 +21,28 @@ pub trait DSAPrivateKey {
Hmac<Hash>: Mac; Hmac<Hash>: Mac;
} }
pub struct DSAPrivKey<Params,N> pub struct DSAPrivKey<Params: DSAParameters>
{ {
pub(crate) params: Params, pub(crate) params: Params,
pub(crate) x: N pub(crate) x: Params::N
} }
pub enum DSAPrivate { pub enum DSAPrivate {
DSA1024Private(DSAPrivKey<L1024N160,U192>), DSA1024Private(DSAPrivKey<L1024N160>),
DSA2048SmallPrivate(DSAPrivKey<L2048N224,U256>), DSA2048SmallPrivate(DSAPrivKey<L2048N224>),
DSA2048Private(DSAPrivKey<L2048N256,U256>), DSA2048Private(DSAPrivKey<L2048N256>),
DSA3072Private(DSAPrivKey<L3072N256,U256>) DSA3072Private(DSAPrivKey<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,$ntype> impl DSAPrivateKey for DSAPrivKey<$ptype>
{ {
type Params = $ptype; type Params = $ptype;
type L = $ltype; type L = $ltype;
type N = $ntype; type N = $ntype;
fn new(params: $ptype, x: $ntype) -> DSAPrivKey<$ptype,$ntype> fn new(params: $ptype, x: $ntype) -> DSAPrivKey<$ptype>
{ {
DSAPrivKey{ params, x } DSAPrivKey{ params, x }
} }
@@ -155,7 +155,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,$nt>::new(params, x); let private = DSAPrivKey::<$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

@@ -20,27 +20,27 @@ pub trait DSAPublicKey {
where Hash: Digest; where Hash: Digest;
} }
pub struct DSAPubKey<Params,L> { pub struct DSAPubKey<Params: DSAParameters> {
pub(crate) params: Params, pub(crate) params: Params,
pub(crate) y: L pub(crate) y: Params::L
} }
pub enum DSAPublic { pub enum DSAPublic {
DSAPublicL1024N160(DSAPubKey<L1024N160,U1024>), DSAPublicL1024N160(DSAPubKey<L1024N160>),
DSAPublicL2048N224(DSAPubKey<L2048N224,U2048>), DSAPublicL2048N224(DSAPubKey<L2048N224>),
DSAPublicL2048N256(DSAPubKey<L2048N256,U2048>), DSAPublicL2048N256(DSAPubKey<L2048N256>),
DSAPublicL3072N256(DSAPubKey<L3072N256,U3072>) DSAPublicL3072N256(DSAPubKey<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,$ltype> impl DSAPublicKey for DSAPubKey<$ptype>
{ {
type Params = $ptype; type Params = $ptype;
type L = $ltype; type L = $ltype;
type N = $ntype; type N = $ntype;
fn new(params: $ptype, y: $ltype) -> DSAPubKey<$ptype,$ltype> fn new(params: $ptype, y: $ltype) -> DSAPubKey<$ptype>
{ {
DSAPubKey{ params, y } DSAPubKey{ params, y }
} }
@@ -80,7 +80,7 @@ macro_rules! pubkey_impls {
} }
} }
impl ToASN1 for DSAPubKey<$ptype,$ltype> { impl ToASN1 for DSAPubKey<$ptype> {
type Error = ASN1EncodeErr; type Error = ASN1EncodeErr;
fn to_asn1_class(&self, c: ASN1Class) fn to_asn1_class(&self, c: ASN1Class)
@@ -136,7 +136,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,$lt>::new(params, y); let public = DSAPubKey::<$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

@@ -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::new(params.clone(), x); let private = DSAPrivKey::<L1024N160>::new(params.clone(), x);
let public = DSAPubKey::<L1024N160,U1024>::new(params.clone(), y); let public = DSAPubKey::<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,U256>::new(params.clone(), x); let private = DSAPrivKey::<L2048N256>::new(params.clone(), x);
let public = DSAPubKey::<L2048N256,U2048>::new(params.clone(), y); let public = DSAPubKey::<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

@@ -31,7 +31,7 @@ pub trait SSHKey: Sized {
} }
impl SSHKey for DSAKeyPair<L1024N160,U1024,U192> { impl SSHKey for DSAKeyPair<L1024N160> {
fn decode_ssh_private_key(x: &str) -> Result<(Self,String),SSHKeyParseError> fn decode_ssh_private_key(x: &str) -> Result<(Self,String),SSHKeyParseError>
{ {
let bytes = parse_ssh_private_key_data(x)?; let bytes = parse_ssh_private_key_data(x)?;
@@ -74,7 +74,7 @@ impl SSHKey for DSAKeyPair<L1024N160,U1024,U192> {
let pubg = parse_openssh_number(&mut pubkey_cursor)?; let pubg = parse_openssh_number(&mut pubkey_cursor)?;
let pubparams = L1024N160::new(pubp, pubg, pubq); let pubparams = L1024N160::new(pubp, pubg, pubq);
let puby: U1024 = parse_openssh_number(&mut pubkey_cursor)?; let puby: U1024 = parse_openssh_number(&mut pubkey_cursor)?;
let pubkey = DSAPubKey::<L1024N160,U1024>::new(pubparams.clone(), puby.clone()); let pubkey = DSAPubKey::<L1024N160>::new(pubparams.clone(), puby.clone());
// And now we can look at the private key! // And now we can look at the private key!
let mut privkey_cursor = Cursor::new(privkeys); let mut privkey_cursor = Cursor::new(privkeys);
@@ -99,7 +99,7 @@ impl SSHKey for DSAKeyPair<L1024N160,U1024,U192> {
return Err(SSHKeyParseError::InconsistentPublicKeyValue); return Err(SSHKeyParseError::InconsistentPublicKeyValue);
} }
let privkey = DSAPrivKey::<L1024N160,U192>::new(pubparams, privx); let privkey = DSAPrivKey::<L1024N160>::new(pubparams, privx);
let comment = parse_openssh_string(&mut privkey_cursor)?; let comment = parse_openssh_string(&mut privkey_cursor)?;
for (idx,byte) in privkey_cursor.bytes().enumerate() { for (idx,byte) in privkey_cursor.bytes().enumerate() {
if ((idx+1) as u8) != byte? { if ((idx+1) as u8) != byte? {
@@ -161,7 +161,7 @@ fn read_dsa_examples() {
for file in test_files.iter() { for file in test_files.iter() {
let path = format!("testdata/ssh/{}",file); let path = format!("testdata/ssh/{}",file);
let mkeypair = DSAKeyPair::<L1024N160,U1024,U192>::read_ssh_private_key_file(path); let mkeypair = DSAKeyPair::<L1024N160>::read_ssh_private_key_file(path);
match mkeypair { match mkeypair {
Err(e) => assert!(false, format!("reading error: {:?}", e)), Err(e) => assert!(false, format!("reading error: {:?}", e)),
Ok((keypair,comment)) => { Ok((keypair,comment)) => {
@@ -173,7 +173,7 @@ fn read_dsa_examples() {
match keypair.encode_ssh_private_key(&comment) { match keypair.encode_ssh_private_key(&comment) {
Err(e2) => assert!(false, format!("render error: {:?}", e2)), Err(e2) => assert!(false, format!("render error: {:?}", e2)),
Ok(encodedstr) => { Ok(encodedstr) => {
match DSAKeyPair::<L1024N160,U1024,U192>::decode_ssh_private_key(&encodedstr) { match DSAKeyPair::<L1024N160>::decode_ssh_private_key(&encodedstr) {
Err(e3) => assert!(false, format!("reparse error: {:?}", e3)), Err(e3) => assert!(false, format!("reparse error: {:?}", e3)),
Ok((keypair2,comment2)) => { Ok((keypair2,comment2)) => {
assert_eq!(keypair.public.params.p,keypair2.public.params.p,"failed to reparse key pair (p)"); assert_eq!(keypair.public.params.p,keypair2.public.params.p,"failed to reparse key pair (p)");

View File

@@ -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,U3072>::new(params, y); let key = DSAPubKey::<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,U2048>::new(params, y); let key = DSAPubKey::<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,U2048>::new(params, y); let key = DSAPubKey::<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,U1024>::new(params, y); let key = DSAPubKey::<L1024N160>::new(params, y);
let reskey = DSAPublic::DSAPublicL1024N160(key); let reskey = DSAPublic::DSAPublicL1024N160(key);
return Ok(reskey); return Ok(reskey);
} }