summaryrefslogtreecommitdiffstats
path: root/openpgp/src/crypto
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2023-04-28 16:47:26 +0200
committerJustus Winter <justus@sequoia-pgp.org>2023-04-28 16:51:32 +0200
commitf3d3d69786b9b31fa96c32c43dba4f96f682b383 (patch)
treee9e0ec8b29e81be7f39fd74b2438dccc24013943 /openpgp/src/crypto
parentede54555b7ec2e30cdbfceaea61c2bd7965a7d5f (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.
Diffstat (limited to 'openpgp/src/crypto')
-rw-r--r--openpgp/src/crypto/mpi.rs45
1 files changed, 27 insertions, 18 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.
///