//! 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;
/// 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(
data.len() as c_int,
eckey.as_ptr(),
))?;
- Ok(EcdsaSig::from_ptr(sig as *mut _))
+ Ok(EcdsaSig::from_ptr(sig))
}
}
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(
}
}
- /// 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`]
///
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`]
///
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! {
#[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]