summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2024-02-20 14:11:51 +0100
committerJustus Winter <justus@sequoia-pgp.org>2024-02-20 14:43:02 +0100
commit90805301f739e2c583ef749aed35d304063a5a2f (patch)
treeb155afc053e0fcce83624f4b216cdd762019d2f7
parent18f30697536ac2e71359008252524213e27dea94 (diff)
openpgp: Add test for curve point representations.
-rw-r--r--openpgp/src/packet/key.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs
index 286d4834..f7287f5d 100644
--- a/openpgp/src/packet/key.rs
+++ b/openpgp/src/packet/key.rs
@@ -2704,4 +2704,65 @@ FwPoSAbbsLkNS/iNN2MDGAVYvezYn2QZ
}
Ok(())
}
+
+ #[test]
+ fn ecc_encoding() -> Result<()> {
+ for for_signing in [true, false] {
+ for curve in Curve::variants()
+ .filter(Curve::is_supported)
+ {
+ match curve {
+ Curve::Cv25519 if for_signing => continue,
+ Curve::Ed25519 if ! for_signing => continue,
+ _ => (),
+ }
+
+ use crate::crypto::mpi::{Ciphertext, MPI, PublicKey};
+ eprintln!("curve {}, for signing {:?}", curve, for_signing);
+
+ let key: Key<SecretParts, UnspecifiedRole> =
+ Key4::generate_ecc(for_signing, curve.clone())?.into();
+
+ let compressed = |mpi: &MPI| mpi.value()[0] == 0x40;
+ let uncompressed = |mpi: &MPI| mpi.value()[0] == 0x04;
+
+ match key.mpis() {
+ PublicKey::ECDSA { curve: c, q } if for_signing => {
+ assert!(c == &curve);
+ assert!(uncompressed(q));
+ },
+ PublicKey::EdDSA { curve: c, q } if for_signing => {
+ assert!(c == &curve);
+ assert!(compressed(q));
+ },
+ PublicKey::ECDH { curve: c, q, .. } if ! for_signing => {
+ assert!(c == &curve);
+ if curve == Curve::Cv25519 {
+ assert!(compressed(q));
+ } else {
+ assert!(uncompressed(q));
+ }
+
+ use crate::crypto::SessionKey;
+ let sk = SessionKey::new(32);
+ let ciphertext = key.encrypt(&sk)?;
+ if let Ciphertext::ECDH { e, .. } = &ciphertext {
+ if curve == Curve::Cv25519 {
+ assert!(compressed(e));
+ } else {
+ assert!(uncompressed(e));
+ }
+ } else {
+ panic!("unexpected ciphertext: {:?}", ciphertext);
+ }
+ },
+ mpi => unreachable!(
+ "curve {}, mpi {:?}, for signing {:?}",
+ curve, mpi, for_signing),
+ }
+ }
+ }
+
+ Ok(())
+ }
}