diff options
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/crypto/mpi.rs | 45 | ||||
-rw-r--r-- | openpgp/src/packet/key.rs | 37 | ||||
-rw-r--r-- | openpgp/src/types/mod.rs | 2 |
3 files changed, 32 insertions, 52 deletions
diff --git a/openpgp/src/crypto/mpi.rs b/openpgp/src/crypto/mpi.rs index a5fa4f63..fa27beb7 100644 --- a/openpgp/src/crypto/mpi.rs +++ b/openpgp/src/crypto/mpi.rs @@ -894,42 +894,51 @@ impl Hash for SecretKeyMaterial { } #[cfg(test)] -impl Arbitrary for SecretKeyMaterial { - fn arbitrary(g: &mut Gen) -> Self { - use crate::arbitrary_helper::gen_arbitrary_from_range; - - match gen_arbitrary_from_range(0..6, g) { - 0 => SecretKeyMaterial::RSA { +impl SecretKeyMaterial { + pub(crate) fn arbitrary_for(g: &mut Gen, pk: PublicKeyAlgorithm) -> Result<Self> { + use self::PublicKeyAlgorithm::*; + #[allow(deprecated)] + match pk { + RSAEncryptSign | RSASign | RSAEncrypt => Ok(SecretKeyMaterial::RSA { d: MPI::arbitrary(g).into(), p: MPI::arbitrary(g).into(), q: MPI::arbitrary(g).into(), u: MPI::arbitrary(g).into(), - }, + }), - 1 => SecretKeyMaterial::DSA { + DSA => Ok(SecretKeyMaterial::DSA { x: MPI::arbitrary(g).into(), - }, + }), - 2 => SecretKeyMaterial::ElGamal { + ElGamalEncryptSign | ElGamalEncrypt => Ok(SecretKeyMaterial::ElGamal { x: MPI::arbitrary(g).into(), - }, + }), - 3 => SecretKeyMaterial::EdDSA { + EdDSA => Ok(SecretKeyMaterial::EdDSA { scalar: MPI::arbitrary(g).into(), - }, + }), - 4 => SecretKeyMaterial::ECDSA { + ECDSA => Ok(SecretKeyMaterial::ECDSA { scalar: MPI::arbitrary(g).into(), - }, + }), - 5 => SecretKeyMaterial::ECDH { + ECDH => Ok(SecretKeyMaterial::ECDH { scalar: MPI::arbitrary(g).into(), - }, + }), - _ => unreachable!(), + Private(_) | Unknown(_) => + Err(Error::UnsupportedPublicKeyAlgorithm(pk).into()), } } } +#[cfg(test)] +impl Arbitrary for SecretKeyMaterial { + fn arbitrary(g: &mut Gen) -> Self { + let pk = *g.choose(&crate::types::PUBLIC_KEY_ALGORITHM_VARIANTS) + .expect("not empty"); + Self::arbitrary_for(g, pk).expect("only known variants") + } +} /// Checksum method for secret key material. /// diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs index a95facad..9dcce43e 100644 --- a/openpgp/src/packet/key.rs +++ b/openpgp/src/packet/key.rs @@ -1749,40 +1749,11 @@ impl Arbitrary for Key4<SecretParts, SubordinateRole> { #[cfg(test)] impl Arbitrary for Key4<SecretParts, UnspecifiedRole> { fn arbitrary(g: &mut Gen) -> Self { - use PublicKeyAlgorithm::*; - use mpi::MPI; - let key = Key4::arbitrary(g); - let mut secret: SecretKeyMaterial = match key.pk_algo() { - RSAEncryptSign => mpi::SecretKeyMaterial::RSA { - d: MPI::arbitrary(g).into(), - p: MPI::arbitrary(g).into(), - q: MPI::arbitrary(g).into(), - u: MPI::arbitrary(g).into(), - }, - - DSA => mpi::SecretKeyMaterial::DSA { - x: MPI::arbitrary(g).into(), - }, - - ElGamalEncrypt => mpi::SecretKeyMaterial::ElGamal { - x: MPI::arbitrary(g).into(), - }, - - EdDSA => mpi::SecretKeyMaterial::EdDSA { - scalar: MPI::arbitrary(g).into(), - }, - - ECDSA => mpi::SecretKeyMaterial::ECDSA { - scalar: MPI::arbitrary(g).into(), - }, - - ECDH => mpi::SecretKeyMaterial::ECDH { - scalar: MPI::arbitrary(g).into(), - }, - - _ => unreachable!("only valid algos, normalizes to these values"), - }.into(); + let mut secret: SecretKeyMaterial = + mpi::SecretKeyMaterial::arbitrary_for(g, key.pk_algo()) + .expect("only known algos used") + .into(); if <bool>::arbitrary(g) { secret.encrypt_in_place(&Password::from(Vec::arbitrary(g))) diff --git a/openpgp/src/types/mod.rs b/openpgp/src/types/mod.rs index 8402655d..76c0494b 100644 --- a/openpgp/src/types/mod.rs +++ b/openpgp/src/types/mod.rs @@ -128,7 +128,7 @@ pub enum PublicKeyAlgorithm { assert_send_and_sync!(PublicKeyAlgorithm); #[allow(deprecated)] -const PUBLIC_KEY_ALGORITHM_VARIANTS: [PublicKeyAlgorithm; 9] = [ +pub(crate) const PUBLIC_KEY_ALGORITHM_VARIANTS: [PublicKeyAlgorithm; 9] = [ PublicKeyAlgorithm::RSAEncryptSign, PublicKeyAlgorithm::RSAEncrypt, PublicKeyAlgorithm::RSASign, |