]> git.proxmox.com Git - cargo.git/blobdiff - vendor/openssl/src/ecdsa.rs
New upstream version 0.52.0
[cargo.git] / vendor / openssl / src / ecdsa.rs
index adf1e659e00fbdbdf7e203fdb6e850c0e546b78d..57cec96b8376513f33facb8af1118732495ca7a1 100644 (file)
@@ -1,15 +1,17 @@
 //! Low level Elliptic Curve Digital Signature Algorithm (ECDSA) functions.
-use ffi;
+
+use cfg_if::cfg_if;
 use foreign_types::{ForeignType, ForeignTypeRef};
 use libc::c_int;
 use std::mem;
 use std::ptr;
 
-use bn::{BigNum, BigNumRef};
-use ec::EcKeyRef;
-use error::ErrorStack;
-use pkey::{Private, Public};
-use {cvt_n, cvt_p};
+use crate::bn::{BigNum, BigNumRef};
+use crate::ec::EcKeyRef;
+use crate::error::ErrorStack;
+use crate::pkey::{HasPrivate, HasPublic};
+use crate::util::ForeignTypeRefExt;
+use crate::{cvt_n, cvt_p};
 
 foreign_type_and_impl_send_sync! {
     type CType = ffi::ECDSA_SIG;
@@ -33,7 +35,10 @@ impl EcdsaSig {
     /// OpenSSL documentation at [`ECDSA_do_sign`]
     ///
     /// [`ECDSA_do_sign`]: https://www.openssl.org/docs/man1.1.0/crypto/ECDSA_do_sign.html
-    pub fn sign(data: &[u8], eckey: &EcKeyRef<Private>) -> Result<EcdsaSig, ErrorStack> {
+    pub fn sign<T>(data: &[u8], eckey: &EcKeyRef<T>) -> Result<EcdsaSig, ErrorStack>
+    where
+        T: HasPrivate,
+    {
         unsafe {
             assert!(data.len() <= c_int::max_value() as usize);
             let sig = cvt_p(ffi::ECDSA_do_sign(
@@ -41,7 +46,7 @@ impl EcdsaSig {
                 data.len() as c_int,
                 eckey.as_ptr(),
             ))?;
-            Ok(EcdsaSig::from_ptr(sig as *mut _))
+            Ok(EcdsaSig::from_ptr(sig))
         }
     }
 
@@ -56,16 +61,42 @@ impl EcdsaSig {
             let sig = cvt_p(ffi::ECDSA_SIG_new())?;
             ECDSA_SIG_set0(sig, r.as_ptr(), s.as_ptr());
             mem::forget((r, s));
-            Ok(EcdsaSig::from_ptr(sig as *mut _))
+            Ok(EcdsaSig::from_ptr(sig))
         }
     }
 
+    from_der! {
+        /// Decodes a DER-encoded ECDSA signature.
+        ///
+        /// This corresponds to [`d2i_ECDSA_SIG`].
+        ///
+        /// [`d2i_ECDSA_SIG`]: https://www.openssl.org/docs/man1.1.0/crypto/d2i_ECDSA_SIG.html
+        from_der,
+        EcdsaSig,
+        ffi::d2i_ECDSA_SIG
+    }
+}
+
+impl EcdsaSigRef {
+    to_der! {
+        /// Serializes the ECDSA signature into a DER-encoded ECDSASignature structure.
+        ///
+        /// This corresponds to [`i2d_ECDSA_SIG`].
+        ///
+        /// [`i2d_ECDSA_SIG`]: https://www.openssl.org/docs/man1.1.0/crypto/i2d_ECDSA_SIG.html
+        to_der,
+        ffi::i2d_ECDSA_SIG
+    }
+
     /// Verifies if the signature is a valid ECDSA signature using the given public key.
     ///
     /// OpenSSL documentation at [`ECDSA_do_verify`]
     ///
     /// [`ECDSA_do_verify`]: https://www.openssl.org/docs/man1.1.0/crypto/ECDSA_do_verify.html
-    pub fn verify(&self, data: &[u8], eckey: &EcKeyRef<Public>) -> Result<bool, ErrorStack> {
+    pub fn verify<T>(&self, data: &[u8], eckey: &EcKeyRef<T>) -> Result<bool, ErrorStack>
+    where
+        T: HasPublic,
+    {
         unsafe {
             assert!(data.len() <= c_int::max_value() as usize);
             cvt_n(ffi::ECDSA_do_verify(
@@ -78,7 +109,7 @@ impl EcdsaSig {
         }
     }
 
-    /// Returns internal component: `r` of a `EcdsaSig`. (See X9.62 or FIPS 186-2)
+    /// Returns internal component: `r` of an `EcdsaSig`. (See X9.62 or FIPS 186-2)
     ///
     /// OpenSSL documentation at [`ECDSA_SIG_get0`]
     ///
@@ -87,11 +118,11 @@ impl EcdsaSig {
         unsafe {
             let mut r = ptr::null();
             ECDSA_SIG_get0(self.as_ptr(), &mut r, ptr::null_mut());
-            BigNumRef::from_ptr(r as *mut _)
+            BigNumRef::from_const_ptr(r)
         }
     }
 
-    /// Returns internal components: `s` of a `EcdsaSig`. (See X9.62 or FIPS 186-2)
+    /// Returns internal components: `s` of an `EcdsaSig`. (See X9.62 or FIPS 186-2)
     ///
     /// OpenSSL documentation at [`ECDSA_SIG_get0`]
     ///
@@ -100,32 +131,9 @@ impl EcdsaSig {
         unsafe {
             let mut s = ptr::null();
             ECDSA_SIG_get0(self.as_ptr(), ptr::null_mut(), &mut s);
-            BigNumRef::from_ptr(s as *mut _)
+            BigNumRef::from_const_ptr(s)
         }
     }
-
-    from_der! {
-        /// Decodes a DER-encoded ECDSA signature.
-        ///
-        /// This corresponds to [`d2i_ECDSA_SIG`].
-        ///
-        /// [`d2i_ECDSA_SIG`]: https://www.openssl.org/docs/man1.1.0/crypto/d2i_ECDSA_SIG.html
-        from_der,
-        EcdsaSig,
-        ffi::d2i_ECDSA_SIG
-    }
-}
-
-impl EcdsaSigRef {
-    to_der! {
-        /// Serializes the ECDSA signature into a DER-encoded ECDSASignature structure.
-        ///
-        /// This corresponds to [`i2d_ECDSA_SIG`].
-        ///
-        /// [`i2d_ECDSA_SIG`]: https://www.openssl.org/docs/man1.1.0/crypto/i2d_ECDSA_SIG.html
-        to_der,
-        ffi::i2d_ECDSA_SIG
-    }
 }
 
 cfg_if! {
@@ -167,13 +175,13 @@ cfg_if! {
 #[cfg(test)]
 mod test {
     use super::*;
-    use ec::EcGroup;
-    use ec::EcKey;
-    use nid::Nid;
+    use crate::ec::EcGroup;
+    use crate::ec::EcKey;
+    use crate::nid::Nid;
+    use crate::pkey::{Private, Public};
 
     fn get_public_key(group: &EcGroup, x: &EcKey<Private>) -> Result<EcKey<Public>, ErrorStack> {
-        let public_key_point = x.public_key();
-        Ok(EcKey::from_public_key(group, public_key_point)?)
+        EcKey::from_public_key(group, x.public_key())
     }
 
     #[test]