diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-05-31 18:22:07 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-06-01 12:55:08 +0200 |
commit | aaaca386bc6e41508ef955c6b27df776be73c24e (patch) | |
tree | 100e036b85487546deaa3b7ef21ad9e336ff17fe /openpgp | |
parent | 65045ec849d2d755ddde4de624dac050ca1304bb (diff) |
openpgp: Simplify tests.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/message/mod.rs | 21 | ||||
-rw-r--r-- | openpgp/src/packet/signature/mod.rs | 82 | ||||
-rw-r--r-- | openpgp/src/packet/signature/subpacket.rs | 10 | ||||
-rw-r--r-- | openpgp/src/parse/stream.rs | 8 | ||||
-rw-r--r-- | openpgp/src/serialize/stream.rs | 9 | ||||
-rw-r--r-- | openpgp/src/tpk/mod.rs | 23 |
6 files changed, 51 insertions, 102 deletions
diff --git a/openpgp/src/message/mod.rs b/openpgp/src/message/mod.rs index c0d701b0..e422a7aa 100644 --- a/openpgp/src/message/mod.rs +++ b/openpgp/src/message/mod.rs @@ -440,7 +440,6 @@ mod tests { use SymmetricAlgorithm; use PublicKeyAlgorithm; use SignatureType; - use crypto::KeyPair; use crypto::s2k::S2K; use crypto::mpis::{Ciphertext, MPI}; use packet::prelude::*; @@ -802,15 +801,9 @@ mod tests { let key: Key = ::packet::key::Key4::generate_ecc(true, ::constants::Curve::Ed25519) .unwrap().into(); - let sec = - if let Some(SecretKey::Unencrypted { ref mpis }) = key.secret() { - mpis.clone() - } else { - panic!() - }; + let mut pair = key.clone().into_keypair().unwrap(); let sig = ::packet::signature::Builder::new(SignatureType::Binary) - .sign_hash(&mut KeyPair::new(key, sec).unwrap(), - hash, hash.context().unwrap()).unwrap(); + .sign_hash(&mut pair, hash, hash.context().unwrap()).unwrap(); // 0: OnePassSig // => bad. @@ -920,15 +913,9 @@ mod tests { let key: Key = ::packet::key::Key4::generate_ecc(true, ::constants::Curve::Ed25519) .unwrap().into(); - let sec = - if let Some(SecretKey::Unencrypted { ref mpis }) = key.secret() { - mpis.clone() - } else { - panic!() - }; + let mut pair = key.clone().into_keypair().unwrap(); let sig = ::packet::signature::Builder::new(SignatureType::Binary) - .sign_hash(&mut KeyPair::new(key, sec).unwrap(), - hash, hash.context().unwrap()).unwrap(); + .sign_hash(&mut pair, hash, hash.context().unwrap()).unwrap(); // 0: Signature // => bad. diff --git a/openpgp/src/packet/signature/mod.rs b/openpgp/src/packet/signature/mod.rs index f71eab51..c67b57c7 100644 --- a/openpgp/src/packet/signature/mod.rs +++ b/openpgp/src/packet/signature/mod.rs @@ -932,7 +932,6 @@ impl From<Signature4> for super::Signature { mod test { use nettle::{Random, Yarrow}; use super::*; - use crypto::KeyPair; use crypto::mpis::MPI; use TPK; use parse::Parse; @@ -1079,8 +1078,6 @@ mod test { #[test] fn sign_verify() { - use packet::key::SecretKey; - let hash_algo = HashAlgorithm::SHA512; let mut hash = vec![0; hash_algo.context().unwrap().digest_size()]; Yarrow::default().random(&mut hash); @@ -1094,30 +1091,25 @@ mod test { "emmelie-dorothea-dina-samantha-awina-ed25519-private.pgp", ] { let tpk = TPK::from_bytes(::tests::key(key)).unwrap(); - let pair = tpk.primary(); - - if let Some(SecretKey::Unencrypted{ mpis: ref sec }) = pair.secret() { - let mut sig = Builder::new(SignatureType::Binary); - let mut hash = hash_algo.context().unwrap(); - - // Make signature. - let sig = sig.sign_hash(&mut KeyPair::new(pair.clone(), - sec.clone()).unwrap(), - hash_algo, hash).unwrap(); - - // Good signature. - let mut hash = hash_algo.context().unwrap(); - sig.hash(&mut hash); - let mut digest = vec![0u8; hash.digest_size()]; - hash.digest(&mut digest); - assert!(sig.verify_hash(&pair, hash_algo, &digest).unwrap()); - - // Bad signature. - digest[0] ^= 0xff; - assert!(! sig.verify_hash(&pair, hash_algo, &digest).unwrap()); - } else { - panic!("secret key is encrypted/missing"); - } + let mut pair = tpk.primary().clone().into_keypair() + .expect("secret key is encrypted/missing"); + + let mut sig = Builder::new(SignatureType::Binary); + let mut hash = hash_algo.context().unwrap(); + + // Make signature. + let sig = sig.sign_hash(&mut pair, hash_algo, hash).unwrap(); + + // Good signature. + let mut hash = hash_algo.context().unwrap(); + sig.hash(&mut hash); + let mut digest = vec![0u8; hash.digest_size()]; + hash.digest(&mut digest); + assert!(sig.verify_hash(pair.public(), hash_algo, &digest).unwrap()); + + // Bad signature. + digest[0] ^= 0xff; + assert!(! sig.verify_hash(pair.public(), hash_algo, &digest).unwrap()); } } @@ -1125,26 +1117,18 @@ mod test { fn sign_message() { use time; use constants::Curve; - use packet::key::SecretKey; let key: Key = Key4::generate_ecc(true, Curve::Ed25519) .unwrap().into(); let msg = b"Hello, World"; - - match key.secret() { - Some(SecretKey::Unencrypted{ ref mpis }) => { - let sig = Builder::new(SignatureType::Binary) - .set_signature_creation_time(time::now()).unwrap() - .set_issuer_fingerprint(key.fingerprint()).unwrap() - .set_issuer(key.keyid()).unwrap() - .sign_message( - &mut KeyPair::new(key.clone(), mpis.clone()).unwrap(), - HashAlgorithm::SHA512, msg).unwrap(); - - assert!(sig.verify_message(&key, msg).unwrap()); - } - _ => unreachable!() - }; + let mut pair = key.into_keypair().unwrap(); + let sig = Builder::new(SignatureType::Binary) + .set_signature_creation_time(time::now()).unwrap() + .set_issuer_fingerprint(pair.public().fingerprint()).unwrap() + .set_issuer(pair.public().keyid()).unwrap() + .sign_message(&mut pair, HashAlgorithm::SHA512, msg).unwrap(); + + assert!(sig.verify_message(pair.public(), msg).unwrap()); } #[test] @@ -1166,6 +1150,7 @@ mod test { #[test] fn sign_with_short_ed25519_secret_key() { + use packet::key::SecretKey; use conversions::Time; use nettle; use time; @@ -1188,16 +1173,19 @@ mod test { scalar: MPI::new(&sec[..]), }; let key = Key4::new(time::now().canonicalize(), - PublicKeyAlgorithm::EdDSA, public_mpis, None) - .unwrap().into(); + PublicKeyAlgorithm::EdDSA, + public_mpis, Some(SecretKey::Unencrypted { + mpis: private_mpis, + })) + .unwrap(); + let mut pair = key.into_keypair().unwrap(); let msg = b"Hello, World"; let mut hash = HashAlgorithm::SHA256.context().unwrap(); hash.update(&msg[..]); Builder::new(SignatureType::Text) - .sign_hash(&mut KeyPair::new(key, private_mpis).unwrap(), - HashAlgorithm::SHA256, hash).unwrap(); + .sign_hash(&mut pair, HashAlgorithm::SHA256, hash).unwrap(); } #[test] diff --git a/openpgp/src/packet/signature/subpacket.rs b/openpgp/src/packet/signature/subpacket.rs index d36bd5e4..5734e340 100644 --- a/openpgp/src/packet/signature/subpacket.rs +++ b/openpgp/src/packet/signature/subpacket.rs @@ -2405,8 +2405,6 @@ impl signature::Builder { #[test] fn accessors() { - use packet::key::SecretKey; - use crypto::KeyPair; use constants::Curve; let pk_algo = PublicKeyAlgorithm::EdDSA; @@ -2415,12 +2413,7 @@ fn accessors() { let mut sig = signature::Builder::new(::constants::SignatureType::Binary); let mut key: ::packet::Key = ::packet::key::Key4::generate_ecc(true, Curve::Ed25519).unwrap().into(); - let sec = if let Some(SecretKey::Unencrypted { ref mpis }) = key.secret() { - mpis.clone() - } else { - panic!() - }; - let mut keypair = KeyPair::new(key.clone(), sec.clone()).unwrap(); + let mut keypair = key.clone().into_keypair().unwrap(); // Cook up a timestamp without ns resolution. let now = time::Tm::from_pgp(time::now_utc().to_pgp().unwrap()); @@ -2488,7 +2481,6 @@ fn accessors() { assert_eq!(sig_.revocable(), Some(false)); key.set_creation_time(now); - let mut keypair = KeyPair::new(key.clone(), sec).unwrap(); sig = sig.set_key_expiration_time(Some(five_minutes)).unwrap(); let sig_ = sig.clone().sign_hash(&mut keypair, hash_algo, hash.clone()).unwrap(); diff --git a/openpgp/src/parse/stream.rs b/openpgp/src/parse/stream.rs index 6a379687..06dc0ff2 100644 --- a/openpgp/src/parse/stream.rs +++ b/openpgp/src/parse/stream.rs @@ -1796,8 +1796,6 @@ mod test { use constants::DataFormat; use tpk::{TPKBuilder, CipherSuite}; use serialize::stream::{LiteralWriter, Signer, Message}; - use packet::key::SecretKey; - use crypto::KeyPair; use std::io::Write; let (tpk, _) = TPKBuilder::new() @@ -1809,11 +1807,7 @@ mod test { let mut buf = vec![]; { let key = tpk.keys_all().signing_capable().nth(0).unwrap().2; - let sec = match key.secret() { - Some(SecretKey::Unencrypted { ref mpis }) => mpis, - _ => unreachable!(), - }; - let mut keypair = KeyPair::new(key.clone(), sec.clone()).unwrap(); + let mut keypair = key.clone().into_keypair().unwrap(); let m = Message::new(&mut buf); let signer = Signer::new(m, vec![&mut keypair], None).unwrap(); diff --git a/openpgp/src/serialize/stream.rs b/openpgp/src/serialize/stream.rs index 70ebd31f..07a7d84f 100644 --- a/openpgp/src/serialize/stream.rs +++ b/openpgp/src/serialize/stream.rs @@ -1343,7 +1343,6 @@ mod test { #[test] fn signature() { use crypto::KeyPair; - use packet::key::SecretKey; use std::collections::HashMap; use Fingerprint; @@ -1361,12 +1360,8 @@ mod test { let mut o = vec![]; { let mut signers = keys.iter().map(|(_, key)| { - match key.secret() { - Some(SecretKey::Unencrypted { ref mpis }) => - KeyPair::new(key.clone(), mpis.clone()).unwrap(), - s => - panic!("expected unencrypted secret key, got: {:?}", s), - } + key.clone().into_keypair() + .expect("expected unencrypted secret key") }).collect::<Vec<KeyPair>>(); let m = Message::new(&mut o); diff --git a/openpgp/src/tpk/mod.rs b/openpgp/src/tpk/mod.rs index c16b7e56..30fd6676 100644 --- a/openpgp/src/tpk/mod.rs +++ b/openpgp/src/tpk/mod.rs @@ -2795,7 +2795,6 @@ impl TPK { #[cfg(test)] mod test { - use crypto::KeyPair; use serialize::Serialize; use super::*; @@ -3675,7 +3674,7 @@ mod test { #[test] fn revoked_time() { use packet::Features; - use packet::key::{Key4, SecretKey}; + use packet::key::Key4; use constants::Curve; use rand::{thread_rng, Rng, distributions::Open01}; /* @@ -3692,11 +3691,8 @@ mod test { let t2 = time::strptime("2001-1-1", "%F").unwrap(); let t3 = time::strptime("2002-1-1", "%F").unwrap(); let key: Key = Key4::generate_ecc(true, Curve::Ed25519).unwrap().into(); + let mut pair = key.clone().into_keypair().unwrap(); let (bind1, rev, bind2) = { - let mpis = match key.secret() { - Some(SecretKey::Unencrypted{ ref mpis }) => mpis, - _ => unreachable!(), - }; let bind1 = signature::Builder::new(SignatureType::DirectKey) .set_features(&Features::sequoia()).unwrap() .set_key_flags(&KeyFlags::default()).unwrap() @@ -3705,17 +3701,15 @@ mod test { .set_issuer_fingerprint(key.fingerprint()).unwrap() .set_issuer(key.keyid()).unwrap() .set_preferred_hash_algorithms(vec![HashAlgorithm::SHA512]).unwrap() - .sign_primary_key_binding( - &mut KeyPair::new(key.clone(), mpis.clone()).unwrap(), - HashAlgorithm::SHA512).unwrap(); + .sign_primary_key_binding(&mut pair, + HashAlgorithm::SHA512).unwrap(); let rev = signature::Builder::new(SignatureType::KeyRevocation) .set_signature_creation_time(t2).unwrap() .set_issuer_fingerprint(key.fingerprint()).unwrap() .set_issuer(key.keyid()).unwrap() - .sign_primary_key_binding( - &mut KeyPair::new(key.clone(), mpis.clone()).unwrap(), - HashAlgorithm::SHA512).unwrap(); + .sign_primary_key_binding(&mut pair, + HashAlgorithm::SHA512).unwrap(); let bind2 = signature::Builder::new(SignatureType::DirectKey) .set_features(&Features::sequoia()).unwrap() @@ -3725,9 +3719,8 @@ mod test { .set_issuer_fingerprint(key.fingerprint()).unwrap() .set_issuer(key.keyid()).unwrap() .set_preferred_hash_algorithms(vec![HashAlgorithm::SHA512]).unwrap() - .sign_primary_key_binding( - &mut KeyPair::new(key.clone(), mpis.clone()).unwrap(), - HashAlgorithm::SHA512).unwrap(); + .sign_primary_key_binding(&mut pair, + HashAlgorithm::SHA512).unwrap(); (bind1, rev, bind2) }; |