diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2021-10-06 14:31:34 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2022-05-12 18:02:03 +0200 |
commit | 692f26a354e27339598e25ae21ac6545e4aa4b65 (patch) | |
tree | b152ec08bdbedd773b53a5a173dc2c23346b0c9e | |
parent | 9190e983315c9421ae4b6829997f161141a733c5 (diff) |
openpgp: Add Curve::field_size.
-rw-r--r-- | openpgp/NEWS | 1 | ||||
-rw-r--r-- | openpgp/src/types/mod.rs | 22 |
2 files changed, 23 insertions, 0 deletions
diff --git a/openpgp/NEWS b/openpgp/NEWS index 77998d4e..967ee426 100644 --- a/openpgp/NEWS +++ b/openpgp/NEWS @@ -6,6 +6,7 @@ ** New functionality - AEADAlgorithm::nonce_size replaces AEADAlgorithm::iv_size - crypto::backend + - Curve::field_size - MPI::is_zero - MPI::zero - packet::Any diff --git a/openpgp/src/types/mod.rs b/openpgp/src/types/mod.rs index e87a0e2c..29775a16 100644 --- a/openpgp/src/types/mod.rs +++ b/openpgp/src/types/mod.rs @@ -347,6 +347,28 @@ impl Curve { Unknown(_) => None, } } + + /// Returns the curve's field size in bytes. + /// + /// # Examples + /// + /// ```rust + /// # fn main() -> sequoia_openpgp::Result<()> { + /// use sequoia_openpgp as openpgp; + /// use openpgp::types::Curve; + /// + /// assert_eq!(Curve::NistP256.field_size()?, 32); + /// assert_eq!(Curve::NistP384.field_size()?, 48); + /// assert_eq!(Curve::NistP521.field_size()?, 66); + /// assert_eq!(Curve::Ed25519.field_size()?, 32); + /// assert!(Curve::Unknown(Box::new([0x2B, 0x11])).field_size().is_err()); + /// # Ok(()) } + /// ``` + pub fn field_size(&self) -> Result<usize> { + self.bits() + .map(|bits| (bits + 7) / 8) + .ok_or_else(|| Error::UnsupportedEllipticCurve(self.clone()).into()) + } } impl fmt::Display for Curve { |