Commit 3f2563f6 authored by Steffen Eiden's avatar Steffen Eiden
Browse files

Add get_security_bits for PKey

parent 5aadcab9
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -402,6 +402,7 @@ cfg_if! {
        extern "C" {
            pub fn EVP_PKEY_get_id(pkey: *const EVP_PKEY) -> c_int;
            pub fn EVP_PKEY_get_bits(key: *const EVP_PKEY) -> c_int;
            pub fn EVP_PKEY_get_security_bits(key: *const EVP_PKEY) -> c_int;
        }

        #[inline]
@@ -413,6 +414,12 @@ cfg_if! {
        pub unsafe fn EVP_PKEY_bits(pkey: *const EVP_PKEY) -> c_int {
            EVP_PKEY_get_bits(pkey)
        }

        #[inline]
        pub unsafe fn EVP_PKEY_security_bits(pkey: *const EVP_PKEY) -> c_int {
            EVP_PKEY_get_security_bits(pkey)
        }

    } else {
        extern "C" {
            pub fn EVP_PKEY_id(pkey: *const EVP_PKEY) -> c_int;
@@ -420,6 +427,7 @@ cfg_if! {
        const_ptr_api! {
            extern "C" {
                pub fn EVP_PKEY_bits(key: #[const_ptr_if(any(ossl110, libressl280))] EVP_PKEY) -> c_int;
                pub fn EVP_PKEY_security_bits(pkey: #[const_ptr_if(any(ossl110, libressl280))] EVP_PKEY) -> c_int;
            }
        }
    }
+17 −0
Original line number Diff line number Diff line
@@ -229,6 +229,14 @@ where
        unsafe { ffi::EVP_PKEY_bits(self.as_ptr()) as u32 }
    }

    ///Returns the number of security bits.
    ///
    ///Bits of security is defined in NIST SP800-57.
    #[corresponds(EVP_PKEY_security_bits)]
    pub fn security_bits(&self) -> u32 {
        unsafe { ffi::EVP_PKEY_security_bits(self.as_ptr()) as u32 }
    }

    /// Compares the public component of this key with another.
    #[corresponds(EVP_PKEY_cmp)]
    pub fn public_eq<U>(&self, other: &PKeyRef<U>) -> bool
@@ -1018,6 +1026,15 @@ mod tests {
        assert_eq!(ec_key.private_key(), ec_key_.private_key());
    }

    #[test]
    fn test_security_bits() {
        let group = crate::ec::EcGroup::from_curve_name(crate::nid::Nid::SECP521R1).unwrap();
        let ec_key = EcKey::generate(&group).unwrap();
        let pkey: PKey<Private> = ec_key.clone().try_into().unwrap();

        assert_eq!(pkey.security_bits(), 256);
    }

    #[test]
    #[cfg(not(boringssl))]
    fn test_dh_conversion() {