diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2023-03-01 16:25:59 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2023-03-01 18:08:57 +0100 |
commit | 913904754ddd585d93522bc45e8d9e830d278f9a (patch) | |
tree | 14e20277ddcdde82806e55586ef8748a5af27d90 /openpgp/src/types/mod.rs | |
parent | ea5606fd9cd2096301e7e4b690f9998d6a36bff9 (diff) |
openpgp: Add support for brainpoolP384r1.
- One of the brainpool curves was not included in our enum Curve,
because at the time we implemented ECC support, it wasn't part of
the RFC4880bis document.
- Unfortunately, we failed to mark enum Curve as non-exhaustive, so
we cannot add a variant without breaking the API.
- We can, however, support the curve by matching on its OID.
Diffstat (limited to 'openpgp/src/types/mod.rs')
-rw-r--r-- | openpgp/src/types/mod.rs | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/openpgp/src/types/mod.rs b/openpgp/src/types/mod.rs index 863880e5..698b65d1 100644 --- a/openpgp/src/types/mod.rs +++ b/openpgp/src/types/mod.rs @@ -365,6 +365,13 @@ pub enum Curve { /// Unknown curve. Unknown(Box<[u8]>), } +impl Curve { + /// Hack! Curve is not non-exhaustive, so we cannot easily add + /// a variant. + pub(crate) fn is_brainpoolp384(&self) -> bool { + self.oid() == BRAINPOOL_P384_OID + } +} assert_send_and_sync!(Curve); impl Curve { @@ -398,6 +405,7 @@ impl Curve { NistP384 => Some(384), NistP521 => Some(521), BrainpoolP256 => Some(256), + Unknown(_) if self.is_brainpoolp384() => Some(384), BrainpoolP512 => Some(512), Ed25519 => Some(256), Cv25519 => Some(256), @@ -455,6 +463,8 @@ impl fmt::Display for Curve { NistP384 => f.write_str("NIST curve P-384"), NistP521 => f.write_str("NIST curve P-521"), BrainpoolP256 => f.write_str("brainpoolP256r1"), + Unknown(_) if self.is_brainpoolp384() => + f.write_str("brainpoolP384r1"), BrainpoolP512 => f.write_str("brainpoolP512r1"), Ed25519 => f.write_str("D.J. Bernstein's \"Twisted\" Edwards curve Ed25519"), @@ -469,6 +479,8 @@ impl fmt::Display for Curve { NistP384 => f.write_str("NIST P-384"), NistP521 => f.write_str("NIST P-521"), BrainpoolP256 => f.write_str("brainpoolP256r1"), + Unknown(_) if self.is_brainpoolp384() => + f.write_str("brainpoolP384r1"), BrainpoolP512 => f.write_str("brainpoolP512r1"), Ed25519 => f.write_str("Ed25519"), @@ -486,6 +498,8 @@ const NIST_P384_OID: &[u8] = &[0x2B, 0x81, 0x04, 0x00, 0x22]; const NIST_P521_OID: &[u8] = &[0x2B, 0x81, 0x04, 0x00, 0x23]; const BRAINPOOL_P256_OID: &[u8] = &[0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x07]; +const BRAINPOOL_P384_OID: &[u8] = + &[0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0B]; const BRAINPOOL_P512_OID: &[u8] = &[0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0D]; const ED25519_OID: &[u8] = @@ -513,6 +527,7 @@ impl Curve { NIST_P384_OID => Curve::NistP384, NIST_P521_OID => Curve::NistP521, BRAINPOOL_P256_OID => Curve::BrainpoolP256, + BRAINPOOL_P384_OID => Curve::Unknown(BRAINPOOL_P384_OID.into()), BRAINPOOL_P512_OID => Curve::BrainpoolP512, ED25519_OID => Curve::Ed25519, CV25519_OID => Curve::Cv25519, @@ -568,6 +583,7 @@ impl Curve { Curve::NistP384 => Ok(384), Curve::NistP521 => Ok(521), Curve::BrainpoolP256 => Ok(256), + Curve::Unknown(_) if self.is_brainpoolp384() => Ok(384), Curve::BrainpoolP512 => Ok(512), Curve::Ed25519 => Ok(256), Curve::Cv25519 => Ok(256), @@ -596,15 +612,16 @@ impl Curve { #[cfg(test)] impl Arbitrary for Curve { fn arbitrary(g: &mut Gen) -> Self { - match u8::arbitrary(g) % 8 { + match u8::arbitrary(g) % 9 { 0 => Curve::NistP256, 1 => Curve::NistP384, 2 => Curve::NistP521, 3 => Curve::BrainpoolP256, - 4 => Curve::BrainpoolP512, - 5 => Curve::Ed25519, - 6 => Curve::Cv25519, - 7 => Curve::Unknown({ + 4 => Curve::Unknown(BRAINPOOL_P384_OID.into()), + 5 => Curve::BrainpoolP512, + 6 => Curve::Ed25519, + 7 => Curve::Cv25519, + 8 => Curve::Unknown({ let mut k = <Vec<u8>>::arbitrary(g); k.truncate(255); k.into_boxed_slice() |