diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2023-04-28 16:47:26 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2023-04-28 16:51:32 +0200 |
commit | f3d3d69786b9b31fa96c32c43dba4f96f682b383 (patch) | |
tree | e9e0ec8b29e81be7f39fd74b2438dccc24013943 | |
parent | ede54555b7ec2e30cdbfceaea61c2bd7965a7d5f (diff) |
openpgp: Rework creation of plausible secret key material.
- Introduce SecretKeyMaterial::arbitrary_for that given a public key
algorithm will create plausible secrets for that. This function
can be re-used in impl Arbitrary for Key.
-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, |