From 90805301f739e2c583ef749aed35d304063a5a2f Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 20 Feb 2024 14:11:51 +0100 Subject: openpgp: Add test for curve point representations. --- openpgp/src/packet/key.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) 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 = + 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(()) + } } -- cgit v1.2.3