192f3ab15Sopenharmony_ci//! Symmetric ciphers.
292f3ab15Sopenharmony_ci
392f3ab15Sopenharmony_ci#[cfg(ossl300)]
492f3ab15Sopenharmony_ciuse crate::cvt_p;
592f3ab15Sopenharmony_ci#[cfg(ossl300)]
692f3ab15Sopenharmony_ciuse crate::error::ErrorStack;
792f3ab15Sopenharmony_ci#[cfg(ossl300)]
892f3ab15Sopenharmony_ciuse crate::lib_ctx::LibCtxRef;
992f3ab15Sopenharmony_ciuse crate::nid::Nid;
1092f3ab15Sopenharmony_ciuse cfg_if::cfg_if;
1192f3ab15Sopenharmony_ciuse foreign_types::{ForeignTypeRef, Opaque};
1292f3ab15Sopenharmony_ciuse openssl_macros::corresponds;
1392f3ab15Sopenharmony_ci#[cfg(ossl300)]
1492f3ab15Sopenharmony_ciuse std::ffi::CString;
1592f3ab15Sopenharmony_ciuse std::ops::{Deref, DerefMut};
1692f3ab15Sopenharmony_ci#[cfg(ossl300)]
1792f3ab15Sopenharmony_ciuse std::ptr;
1892f3ab15Sopenharmony_ci
1992f3ab15Sopenharmony_cicfg_if! {
2092f3ab15Sopenharmony_ci    if #[cfg(any(boringssl, ossl110, libressl273))] {
2192f3ab15Sopenharmony_ci        use ffi::{EVP_CIPHER_block_size, EVP_CIPHER_iv_length, EVP_CIPHER_key_length};
2292f3ab15Sopenharmony_ci    } else {
2392f3ab15Sopenharmony_ci        use libc::c_int;
2492f3ab15Sopenharmony_ci
2592f3ab15Sopenharmony_ci        #[allow(bad_style)]
2692f3ab15Sopenharmony_ci        pub unsafe fn EVP_CIPHER_iv_length(ptr: *const ffi::EVP_CIPHER) -> c_int {
2792f3ab15Sopenharmony_ci            (*ptr).iv_len
2892f3ab15Sopenharmony_ci        }
2992f3ab15Sopenharmony_ci
3092f3ab15Sopenharmony_ci        #[allow(bad_style)]
3192f3ab15Sopenharmony_ci        pub unsafe fn EVP_CIPHER_block_size(ptr: *const ffi::EVP_CIPHER) -> c_int {
3292f3ab15Sopenharmony_ci            (*ptr).block_size
3392f3ab15Sopenharmony_ci        }
3492f3ab15Sopenharmony_ci
3592f3ab15Sopenharmony_ci        #[allow(bad_style)]
3692f3ab15Sopenharmony_ci        pub unsafe fn EVP_CIPHER_key_length(ptr: *const ffi::EVP_CIPHER) -> c_int {
3792f3ab15Sopenharmony_ci            (*ptr).key_len
3892f3ab15Sopenharmony_ci        }
3992f3ab15Sopenharmony_ci    }
4092f3ab15Sopenharmony_ci}
4192f3ab15Sopenharmony_ci
4292f3ab15Sopenharmony_cicfg_if! {
4392f3ab15Sopenharmony_ci    if #[cfg(ossl300)] {
4492f3ab15Sopenharmony_ci        use foreign_types::ForeignType;
4592f3ab15Sopenharmony_ci
4692f3ab15Sopenharmony_ci        type Inner = *mut ffi::EVP_CIPHER;
4792f3ab15Sopenharmony_ci
4892f3ab15Sopenharmony_ci        impl Drop for Cipher {
4992f3ab15Sopenharmony_ci            #[inline]
5092f3ab15Sopenharmony_ci            fn drop(&mut self) {
5192f3ab15Sopenharmony_ci                unsafe {
5292f3ab15Sopenharmony_ci                    ffi::EVP_CIPHER_free(self.as_ptr());
5392f3ab15Sopenharmony_ci                }
5492f3ab15Sopenharmony_ci            }
5592f3ab15Sopenharmony_ci        }
5692f3ab15Sopenharmony_ci
5792f3ab15Sopenharmony_ci        impl ForeignType for Cipher {
5892f3ab15Sopenharmony_ci            type CType = ffi::EVP_CIPHER;
5992f3ab15Sopenharmony_ci            type Ref = CipherRef;
6092f3ab15Sopenharmony_ci
6192f3ab15Sopenharmony_ci            #[inline]
6292f3ab15Sopenharmony_ci            unsafe fn from_ptr(ptr: *mut Self::CType) -> Self {
6392f3ab15Sopenharmony_ci                Cipher(ptr)
6492f3ab15Sopenharmony_ci            }
6592f3ab15Sopenharmony_ci
6692f3ab15Sopenharmony_ci            #[inline]
6792f3ab15Sopenharmony_ci            fn as_ptr(&self) -> *mut Self::CType {
6892f3ab15Sopenharmony_ci                self.0
6992f3ab15Sopenharmony_ci            }
7092f3ab15Sopenharmony_ci        }
7192f3ab15Sopenharmony_ci
7292f3ab15Sopenharmony_ci        impl Deref for Cipher {
7392f3ab15Sopenharmony_ci            type Target = CipherRef;
7492f3ab15Sopenharmony_ci
7592f3ab15Sopenharmony_ci            #[inline]
7692f3ab15Sopenharmony_ci            fn deref(&self) -> &Self::Target {
7792f3ab15Sopenharmony_ci                unsafe {
7892f3ab15Sopenharmony_ci                    CipherRef::from_ptr(self.as_ptr())
7992f3ab15Sopenharmony_ci                }
8092f3ab15Sopenharmony_ci            }
8192f3ab15Sopenharmony_ci        }
8292f3ab15Sopenharmony_ci
8392f3ab15Sopenharmony_ci        impl DerefMut for Cipher {
8492f3ab15Sopenharmony_ci            #[inline]
8592f3ab15Sopenharmony_ci            fn deref_mut(&mut self) -> &mut Self::Target {
8692f3ab15Sopenharmony_ci                unsafe {
8792f3ab15Sopenharmony_ci                    CipherRef::from_ptr_mut(self.as_ptr())
8892f3ab15Sopenharmony_ci                }
8992f3ab15Sopenharmony_ci            }
9092f3ab15Sopenharmony_ci        }
9192f3ab15Sopenharmony_ci    } else {
9292f3ab15Sopenharmony_ci        enum Inner {}
9392f3ab15Sopenharmony_ci
9492f3ab15Sopenharmony_ci        impl Deref for Cipher {
9592f3ab15Sopenharmony_ci            type Target = CipherRef;
9692f3ab15Sopenharmony_ci
9792f3ab15Sopenharmony_ci            #[inline]
9892f3ab15Sopenharmony_ci            fn deref(&self) -> &Self::Target {
9992f3ab15Sopenharmony_ci                match self.0 {}
10092f3ab15Sopenharmony_ci            }
10192f3ab15Sopenharmony_ci        }
10292f3ab15Sopenharmony_ci
10392f3ab15Sopenharmony_ci        impl DerefMut for Cipher {
10492f3ab15Sopenharmony_ci            #[inline]
10592f3ab15Sopenharmony_ci            fn deref_mut(&mut self) -> &mut Self::Target {
10692f3ab15Sopenharmony_ci                match self.0 {}
10792f3ab15Sopenharmony_ci            }
10892f3ab15Sopenharmony_ci        }
10992f3ab15Sopenharmony_ci    }
11092f3ab15Sopenharmony_ci}
11192f3ab15Sopenharmony_ci
11292f3ab15Sopenharmony_ci/// A symmetric cipher.
11392f3ab15Sopenharmony_cipub struct Cipher(Inner);
11492f3ab15Sopenharmony_ci
11592f3ab15Sopenharmony_ciunsafe impl Sync for Cipher {}
11692f3ab15Sopenharmony_ciunsafe impl Send for Cipher {}
11792f3ab15Sopenharmony_ci
11892f3ab15Sopenharmony_ciimpl Cipher {
11992f3ab15Sopenharmony_ci    /// Looks up the cipher for a certain nid.
12092f3ab15Sopenharmony_ci    #[corresponds(EVP_get_cipherbynid)]
12192f3ab15Sopenharmony_ci    pub fn from_nid(nid: Nid) -> Option<&'static CipherRef> {
12292f3ab15Sopenharmony_ci        unsafe {
12392f3ab15Sopenharmony_ci            let ptr = ffi::EVP_get_cipherbyname(ffi::OBJ_nid2sn(nid.as_raw()));
12492f3ab15Sopenharmony_ci            if ptr.is_null() {
12592f3ab15Sopenharmony_ci                None
12692f3ab15Sopenharmony_ci            } else {
12792f3ab15Sopenharmony_ci                Some(CipherRef::from_ptr(ptr as *mut _))
12892f3ab15Sopenharmony_ci            }
12992f3ab15Sopenharmony_ci        }
13092f3ab15Sopenharmony_ci    }
13192f3ab15Sopenharmony_ci
13292f3ab15Sopenharmony_ci    /// Fetches a cipher object corresponding to the specified algorithm name and properties.
13392f3ab15Sopenharmony_ci    ///
13492f3ab15Sopenharmony_ci    /// Requires OpenSSL 3.0.0 or newer.
13592f3ab15Sopenharmony_ci    #[corresponds(EVP_CIPHER_fetch)]
13692f3ab15Sopenharmony_ci    #[cfg(ossl300)]
13792f3ab15Sopenharmony_ci    pub fn fetch(
13892f3ab15Sopenharmony_ci        ctx: Option<&LibCtxRef>,
13992f3ab15Sopenharmony_ci        algorithm: &str,
14092f3ab15Sopenharmony_ci        properties: Option<&str>,
14192f3ab15Sopenharmony_ci    ) -> Result<Self, ErrorStack> {
14292f3ab15Sopenharmony_ci        let algorithm = CString::new(algorithm).unwrap();
14392f3ab15Sopenharmony_ci        let properties = properties.map(|s| CString::new(s).unwrap());
14492f3ab15Sopenharmony_ci
14592f3ab15Sopenharmony_ci        unsafe {
14692f3ab15Sopenharmony_ci            let ptr = cvt_p(ffi::EVP_CIPHER_fetch(
14792f3ab15Sopenharmony_ci                ctx.map_or(ptr::null_mut(), ForeignTypeRef::as_ptr),
14892f3ab15Sopenharmony_ci                algorithm.as_ptr(),
14992f3ab15Sopenharmony_ci                properties.map_or(ptr::null_mut(), |s| s.as_ptr()),
15092f3ab15Sopenharmony_ci            ))?;
15192f3ab15Sopenharmony_ci
15292f3ab15Sopenharmony_ci            Ok(Cipher::from_ptr(ptr))
15392f3ab15Sopenharmony_ci        }
15492f3ab15Sopenharmony_ci    }
15592f3ab15Sopenharmony_ci
15692f3ab15Sopenharmony_ci    pub fn aes_128_ecb() -> &'static CipherRef {
15792f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_ecb() as *mut _) }
15892f3ab15Sopenharmony_ci    }
15992f3ab15Sopenharmony_ci
16092f3ab15Sopenharmony_ci    pub fn aes_128_cbc() -> &'static CipherRef {
16192f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_cbc() as *mut _) }
16292f3ab15Sopenharmony_ci    }
16392f3ab15Sopenharmony_ci
16492f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
16592f3ab15Sopenharmony_ci    pub fn aes_128_xts() -> &'static CipherRef {
16692f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_xts() as *mut _) }
16792f3ab15Sopenharmony_ci    }
16892f3ab15Sopenharmony_ci
16992f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
17092f3ab15Sopenharmony_ci    pub fn aes_128_ctr() -> &'static CipherRef {
17192f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_ctr() as *mut _) }
17292f3ab15Sopenharmony_ci    }
17392f3ab15Sopenharmony_ci
17492f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
17592f3ab15Sopenharmony_ci    pub fn aes_128_cfb1() -> &'static CipherRef {
17692f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_cfb1() as *mut _) }
17792f3ab15Sopenharmony_ci    }
17892f3ab15Sopenharmony_ci
17992f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
18092f3ab15Sopenharmony_ci    pub fn aes_128_cfb128() -> &'static CipherRef {
18192f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_cfb128() as *mut _) }
18292f3ab15Sopenharmony_ci    }
18392f3ab15Sopenharmony_ci
18492f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
18592f3ab15Sopenharmony_ci    pub fn aes_128_cfb8() -> &'static CipherRef {
18692f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_cfb8() as *mut _) }
18792f3ab15Sopenharmony_ci    }
18892f3ab15Sopenharmony_ci
18992f3ab15Sopenharmony_ci    pub fn aes_128_gcm() -> &'static CipherRef {
19092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_gcm() as *mut _) }
19192f3ab15Sopenharmony_ci    }
19292f3ab15Sopenharmony_ci
19392f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
19492f3ab15Sopenharmony_ci    pub fn aes_128_ccm() -> &'static CipherRef {
19592f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_ccm() as *mut _) }
19692f3ab15Sopenharmony_ci    }
19792f3ab15Sopenharmony_ci
19892f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
19992f3ab15Sopenharmony_ci    pub fn aes_128_ofb() -> &'static CipherRef {
20092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_ofb() as *mut _) }
20192f3ab15Sopenharmony_ci    }
20292f3ab15Sopenharmony_ci
20392f3ab15Sopenharmony_ci    /// Requires OpenSSL 1.1.0 or newer.
20492f3ab15Sopenharmony_ci    #[cfg(ossl110)]
20592f3ab15Sopenharmony_ci    pub fn aes_128_ocb() -> &'static CipherRef {
20692f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_ocb() as *mut _) }
20792f3ab15Sopenharmony_ci    }
20892f3ab15Sopenharmony_ci
20992f3ab15Sopenharmony_ci    /// Requires OpenSSL 1.0.2 or newer.
21092f3ab15Sopenharmony_ci    #[cfg(ossl102)]
21192f3ab15Sopenharmony_ci    pub fn aes_128_wrap() -> &'static CipherRef {
21292f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_wrap() as *mut _) }
21392f3ab15Sopenharmony_ci    }
21492f3ab15Sopenharmony_ci
21592f3ab15Sopenharmony_ci    /// Requires OpenSSL 1.1.0 or newer.
21692f3ab15Sopenharmony_ci    #[cfg(ossl110)]
21792f3ab15Sopenharmony_ci    pub fn aes_128_wrap_pad() -> &'static CipherRef {
21892f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_128_wrap_pad() as *mut _) }
21992f3ab15Sopenharmony_ci    }
22092f3ab15Sopenharmony_ci
22192f3ab15Sopenharmony_ci    pub fn aes_192_ecb() -> &'static CipherRef {
22292f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_ecb() as *mut _) }
22392f3ab15Sopenharmony_ci    }
22492f3ab15Sopenharmony_ci
22592f3ab15Sopenharmony_ci    pub fn aes_192_cbc() -> &'static CipherRef {
22692f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_cbc() as *mut _) }
22792f3ab15Sopenharmony_ci    }
22892f3ab15Sopenharmony_ci
22992f3ab15Sopenharmony_ci    pub fn aes_192_ctr() -> &'static CipherRef {
23092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_ctr() as *mut _) }
23192f3ab15Sopenharmony_ci    }
23292f3ab15Sopenharmony_ci
23392f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
23492f3ab15Sopenharmony_ci    pub fn aes_192_cfb1() -> &'static CipherRef {
23592f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_cfb1() as *mut _) }
23692f3ab15Sopenharmony_ci    }
23792f3ab15Sopenharmony_ci
23892f3ab15Sopenharmony_ci    pub fn aes_192_cfb128() -> &'static CipherRef {
23992f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_cfb128() as *mut _) }
24092f3ab15Sopenharmony_ci    }
24192f3ab15Sopenharmony_ci
24292f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
24392f3ab15Sopenharmony_ci    pub fn aes_192_cfb8() -> &'static CipherRef {
24492f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_cfb8() as *mut _) }
24592f3ab15Sopenharmony_ci    }
24692f3ab15Sopenharmony_ci
24792f3ab15Sopenharmony_ci    pub fn aes_192_gcm() -> &'static CipherRef {
24892f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_gcm() as *mut _) }
24992f3ab15Sopenharmony_ci    }
25092f3ab15Sopenharmony_ci
25192f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
25292f3ab15Sopenharmony_ci    pub fn aes_192_ccm() -> &'static CipherRef {
25392f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_ccm() as *mut _) }
25492f3ab15Sopenharmony_ci    }
25592f3ab15Sopenharmony_ci
25692f3ab15Sopenharmony_ci    pub fn aes_192_ofb() -> &'static CipherRef {
25792f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_ofb() as *mut _) }
25892f3ab15Sopenharmony_ci    }
25992f3ab15Sopenharmony_ci
26092f3ab15Sopenharmony_ci    /// Requires OpenSSL 1.1.0 or newer.
26192f3ab15Sopenharmony_ci    #[cfg(ossl110)]
26292f3ab15Sopenharmony_ci    pub fn aes_192_ocb() -> &'static CipherRef {
26392f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_ocb() as *mut _) }
26492f3ab15Sopenharmony_ci    }
26592f3ab15Sopenharmony_ci
26692f3ab15Sopenharmony_ci    /// Requires OpenSSL 1.0.2 or newer.
26792f3ab15Sopenharmony_ci    #[cfg(ossl102)]
26892f3ab15Sopenharmony_ci    pub fn aes_192_wrap() -> &'static CipherRef {
26992f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_wrap() as *mut _) }
27092f3ab15Sopenharmony_ci    }
27192f3ab15Sopenharmony_ci
27292f3ab15Sopenharmony_ci    /// Requires OpenSSL 1.1.0 or newer.
27392f3ab15Sopenharmony_ci    #[cfg(ossl110)]
27492f3ab15Sopenharmony_ci    pub fn aes_192_wrap_pad() -> &'static CipherRef {
27592f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_192_wrap_pad() as *mut _) }
27692f3ab15Sopenharmony_ci    }
27792f3ab15Sopenharmony_ci
27892f3ab15Sopenharmony_ci    pub fn aes_256_ecb() -> &'static CipherRef {
27992f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_ecb() as *mut _) }
28092f3ab15Sopenharmony_ci    }
28192f3ab15Sopenharmony_ci
28292f3ab15Sopenharmony_ci    pub fn aes_256_cbc() -> &'static CipherRef {
28392f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_cbc() as *mut _) }
28492f3ab15Sopenharmony_ci    }
28592f3ab15Sopenharmony_ci
28692f3ab15Sopenharmony_ci    pub fn aes_256_ctr() -> &'static CipherRef {
28792f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_ctr() as *mut _) }
28892f3ab15Sopenharmony_ci    }
28992f3ab15Sopenharmony_ci
29092f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
29192f3ab15Sopenharmony_ci    pub fn aes_256_cfb1() -> &'static CipherRef {
29292f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_cfb1() as *mut _) }
29392f3ab15Sopenharmony_ci    }
29492f3ab15Sopenharmony_ci
29592f3ab15Sopenharmony_ci    pub fn aes_256_cfb128() -> &'static CipherRef {
29692f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_cfb128() as *mut _) }
29792f3ab15Sopenharmony_ci    }
29892f3ab15Sopenharmony_ci
29992f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
30092f3ab15Sopenharmony_ci    pub fn aes_256_cfb8() -> &'static CipherRef {
30192f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_cfb8() as *mut _) }
30292f3ab15Sopenharmony_ci    }
30392f3ab15Sopenharmony_ci
30492f3ab15Sopenharmony_ci    pub fn aes_256_gcm() -> &'static CipherRef {
30592f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_gcm() as *mut _) }
30692f3ab15Sopenharmony_ci    }
30792f3ab15Sopenharmony_ci
30892f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
30992f3ab15Sopenharmony_ci    pub fn aes_256_ccm() -> &'static CipherRef {
31092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_ccm() as *mut _) }
31192f3ab15Sopenharmony_ci    }
31292f3ab15Sopenharmony_ci
31392f3ab15Sopenharmony_ci    pub fn aes_256_ofb() -> &'static CipherRef {
31492f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_ofb() as *mut _) }
31592f3ab15Sopenharmony_ci    }
31692f3ab15Sopenharmony_ci
31792f3ab15Sopenharmony_ci    /// Requires OpenSSL 1.1.0 or newer.
31892f3ab15Sopenharmony_ci    #[cfg(ossl110)]
31992f3ab15Sopenharmony_ci    pub fn aes_256_ocb() -> &'static CipherRef {
32092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_ocb() as *mut _) }
32192f3ab15Sopenharmony_ci    }
32292f3ab15Sopenharmony_ci
32392f3ab15Sopenharmony_ci    /// Requires OpenSSL 1.0.2 or newer.
32492f3ab15Sopenharmony_ci    #[cfg(ossl102)]
32592f3ab15Sopenharmony_ci    pub fn aes_256_wrap() -> &'static CipherRef {
32692f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_wrap() as *mut _) }
32792f3ab15Sopenharmony_ci    }
32892f3ab15Sopenharmony_ci
32992f3ab15Sopenharmony_ci    /// Requires OpenSSL 1.1.0 or newer.
33092f3ab15Sopenharmony_ci    #[cfg(ossl110)]
33192f3ab15Sopenharmony_ci    pub fn aes_256_wrap_pad() -> &'static CipherRef {
33292f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_aes_256_wrap_pad() as *mut _) }
33392f3ab15Sopenharmony_ci    }
33492f3ab15Sopenharmony_ci
33592f3ab15Sopenharmony_ci    #[cfg(not(osslconf = "OPENSSL_NO_BF"))]
33692f3ab15Sopenharmony_ci    pub fn bf_cbc() -> &'static CipherRef {
33792f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_bf_cbc() as *mut _) }
33892f3ab15Sopenharmony_ci    }
33992f3ab15Sopenharmony_ci
34092f3ab15Sopenharmony_ci    #[cfg(not(osslconf = "OPENSSL_NO_BF"))]
34192f3ab15Sopenharmony_ci    pub fn bf_ecb() -> &'static CipherRef {
34292f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_bf_ecb() as *mut _) }
34392f3ab15Sopenharmony_ci    }
34492f3ab15Sopenharmony_ci
34592f3ab15Sopenharmony_ci    #[cfg(not(osslconf = "OPENSSL_NO_BF"))]
34692f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
34792f3ab15Sopenharmony_ci    pub fn bf_cfb64() -> &'static CipherRef {
34892f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_bf_cfb64() as *mut _) }
34992f3ab15Sopenharmony_ci    }
35092f3ab15Sopenharmony_ci
35192f3ab15Sopenharmony_ci    #[cfg(not(osslconf = "OPENSSL_NO_BF"))]
35292f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
35392f3ab15Sopenharmony_ci    pub fn bf_ofb() -> &'static CipherRef {
35492f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_bf_ofb() as *mut _) }
35592f3ab15Sopenharmony_ci    }
35692f3ab15Sopenharmony_ci
35792f3ab15Sopenharmony_ci    pub fn des_cbc() -> &'static CipherRef {
35892f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_des_cbc() as *mut _) }
35992f3ab15Sopenharmony_ci    }
36092f3ab15Sopenharmony_ci
36192f3ab15Sopenharmony_ci    pub fn des_ecb() -> &'static CipherRef {
36292f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_des_ecb() as *mut _) }
36392f3ab15Sopenharmony_ci    }
36492f3ab15Sopenharmony_ci
36592f3ab15Sopenharmony_ci    pub fn des_ede3() -> &'static CipherRef {
36692f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_des_ede3() as *mut _) }
36792f3ab15Sopenharmony_ci    }
36892f3ab15Sopenharmony_ci
36992f3ab15Sopenharmony_ci    pub fn des_ede3_cbc() -> &'static CipherRef {
37092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_des_ede3_cbc() as *mut _) }
37192f3ab15Sopenharmony_ci    }
37292f3ab15Sopenharmony_ci
37392f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
37492f3ab15Sopenharmony_ci    pub fn des_ede3_cfb64() -> &'static CipherRef {
37592f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_des_ede3_cfb64() as *mut _) }
37692f3ab15Sopenharmony_ci    }
37792f3ab15Sopenharmony_ci
37892f3ab15Sopenharmony_ci    #[cfg(not(osslconf = "OPENSSL_NO_RC4"))]
37992f3ab15Sopenharmony_ci    pub fn rc4() -> &'static CipherRef {
38092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_rc4() as *mut _) }
38192f3ab15Sopenharmony_ci    }
38292f3ab15Sopenharmony_ci
38392f3ab15Sopenharmony_ci    #[cfg(not(any(boringssl, osslconf = "OPENSSL_NO_CAMELLIA")))]
38492f3ab15Sopenharmony_ci    pub fn camellia128_cfb128() -> &'static CipherRef {
38592f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_camellia_128_cfb128() as *mut _) }
38692f3ab15Sopenharmony_ci    }
38792f3ab15Sopenharmony_ci
38892f3ab15Sopenharmony_ci    #[cfg(not(any(boringssl, osslconf = "OPENSSL_NO_CAMELLIA")))]
38992f3ab15Sopenharmony_ci    pub fn camellia128_ecb() -> &'static CipherRef {
39092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_camellia_128_ecb() as *mut _) }
39192f3ab15Sopenharmony_ci    }
39292f3ab15Sopenharmony_ci
39392f3ab15Sopenharmony_ci    #[cfg(not(any(boringssl, osslconf = "OPENSSL_NO_CAMELLIA")))]
39492f3ab15Sopenharmony_ci    pub fn camellia192_cfb128() -> &'static CipherRef {
39592f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_camellia_192_cfb128() as *mut _) }
39692f3ab15Sopenharmony_ci    }
39792f3ab15Sopenharmony_ci
39892f3ab15Sopenharmony_ci    #[cfg(not(any(boringssl, osslconf = "OPENSSL_NO_CAMELLIA")))]
39992f3ab15Sopenharmony_ci    pub fn camellia192_ecb() -> &'static CipherRef {
40092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_camellia_192_ecb() as *mut _) }
40192f3ab15Sopenharmony_ci    }
40292f3ab15Sopenharmony_ci
40392f3ab15Sopenharmony_ci    #[cfg(not(any(boringssl, osslconf = "OPENSSL_NO_CAMELLIA")))]
40492f3ab15Sopenharmony_ci    pub fn camellia256_cfb128() -> &'static CipherRef {
40592f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_camellia_256_cfb128() as *mut _) }
40692f3ab15Sopenharmony_ci    }
40792f3ab15Sopenharmony_ci
40892f3ab15Sopenharmony_ci    #[cfg(not(any(boringssl, osslconf = "OPENSSL_NO_CAMELLIA")))]
40992f3ab15Sopenharmony_ci    pub fn camellia256_ecb() -> &'static CipherRef {
41092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_camellia_256_ecb() as *mut _) }
41192f3ab15Sopenharmony_ci    }
41292f3ab15Sopenharmony_ci
41392f3ab15Sopenharmony_ci    #[cfg(not(any(boringssl, osslconf = "OPENSSL_NO_CAST")))]
41492f3ab15Sopenharmony_ci    pub fn cast5_cfb64() -> &'static CipherRef {
41592f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_cast5_cfb64() as *mut _) }
41692f3ab15Sopenharmony_ci    }
41792f3ab15Sopenharmony_ci
41892f3ab15Sopenharmony_ci    #[cfg(not(any(boringssl, osslconf = "OPENSSL_NO_CAST")))]
41992f3ab15Sopenharmony_ci    pub fn cast5_ecb() -> &'static CipherRef {
42092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_cast5_ecb() as *mut _) }
42192f3ab15Sopenharmony_ci    }
42292f3ab15Sopenharmony_ci
42392f3ab15Sopenharmony_ci    #[cfg(not(any(boringssl, osslconf = "OPENSSL_NO_IDEA")))]
42492f3ab15Sopenharmony_ci    pub fn idea_cfb64() -> &'static CipherRef {
42592f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_idea_cfb64() as *mut _) }
42692f3ab15Sopenharmony_ci    }
42792f3ab15Sopenharmony_ci
42892f3ab15Sopenharmony_ci    #[cfg(not(any(boringssl, osslconf = "OPENSSL_NO_IDEA")))]
42992f3ab15Sopenharmony_ci    pub fn idea_ecb() -> &'static CipherRef {
43092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_idea_ecb() as *mut _) }
43192f3ab15Sopenharmony_ci    }
43292f3ab15Sopenharmony_ci
43392f3ab15Sopenharmony_ci    #[cfg(all(ossl110, not(osslconf = "OPENSSL_NO_CHACHA")))]
43492f3ab15Sopenharmony_ci    pub fn chacha20() -> &'static CipherRef {
43592f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_chacha20() as *mut _) }
43692f3ab15Sopenharmony_ci    }
43792f3ab15Sopenharmony_ci
43892f3ab15Sopenharmony_ci    #[cfg(all(ossl110, not(osslconf = "OPENSSL_NO_CHACHA")))]
43992f3ab15Sopenharmony_ci    pub fn chacha20_poly1305() -> &'static CipherRef {
44092f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_chacha20_poly1305() as *mut _) }
44192f3ab15Sopenharmony_ci    }
44292f3ab15Sopenharmony_ci
44392f3ab15Sopenharmony_ci    #[cfg(not(osslconf = "OPENSSL_NO_SEED"))]
44492f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
44592f3ab15Sopenharmony_ci    pub fn seed_cbc() -> &'static CipherRef {
44692f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_seed_cbc() as *mut _) }
44792f3ab15Sopenharmony_ci    }
44892f3ab15Sopenharmony_ci
44992f3ab15Sopenharmony_ci    #[cfg(not(osslconf = "OPENSSL_NO_SEED"))]
45092f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
45192f3ab15Sopenharmony_ci    pub fn seed_cfb128() -> &'static CipherRef {
45292f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_seed_cfb128() as *mut _) }
45392f3ab15Sopenharmony_ci    }
45492f3ab15Sopenharmony_ci
45592f3ab15Sopenharmony_ci    #[cfg(not(osslconf = "OPENSSL_NO_SEED"))]
45692f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
45792f3ab15Sopenharmony_ci    pub fn seed_ecb() -> &'static CipherRef {
45892f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_seed_ecb() as *mut _) }
45992f3ab15Sopenharmony_ci    }
46092f3ab15Sopenharmony_ci
46192f3ab15Sopenharmony_ci    #[cfg(not(osslconf = "OPENSSL_NO_SEED"))]
46292f3ab15Sopenharmony_ci    #[cfg(not(boringssl))]
46392f3ab15Sopenharmony_ci    pub fn seed_ofb() -> &'static CipherRef {
46492f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_seed_ofb() as *mut _) }
46592f3ab15Sopenharmony_ci    }
46692f3ab15Sopenharmony_ci
46792f3ab15Sopenharmony_ci    #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM4")))]
46892f3ab15Sopenharmony_ci    pub fn sm4_ecb() -> &'static CipherRef {
46992f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_sm4_ecb() as *mut _) }
47092f3ab15Sopenharmony_ci    }
47192f3ab15Sopenharmony_ci
47292f3ab15Sopenharmony_ci    #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM4")))]
47392f3ab15Sopenharmony_ci    pub fn sm4_cbc() -> &'static CipherRef {
47492f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_sm4_cbc() as *mut _) }
47592f3ab15Sopenharmony_ci    }
47692f3ab15Sopenharmony_ci
47792f3ab15Sopenharmony_ci    #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM4")))]
47892f3ab15Sopenharmony_ci    pub fn sm4_ctr() -> &'static CipherRef {
47992f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_sm4_ctr() as *mut _) }
48092f3ab15Sopenharmony_ci    }
48192f3ab15Sopenharmony_ci
48292f3ab15Sopenharmony_ci    #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM4")))]
48392f3ab15Sopenharmony_ci    pub fn sm4_cfb128() -> &'static CipherRef {
48492f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_sm4_cfb128() as *mut _) }
48592f3ab15Sopenharmony_ci    }
48692f3ab15Sopenharmony_ci
48792f3ab15Sopenharmony_ci    #[cfg(all(any(ossl111, libressl291), not(osslconf = "OPENSSL_NO_SM4")))]
48892f3ab15Sopenharmony_ci    pub fn sm4_ofb() -> &'static CipherRef {
48992f3ab15Sopenharmony_ci        unsafe { CipherRef::from_ptr(ffi::EVP_sm4_ofb() as *mut _) }
49092f3ab15Sopenharmony_ci    }
49192f3ab15Sopenharmony_ci}
49292f3ab15Sopenharmony_ci
49392f3ab15Sopenharmony_ci/// A reference to a [`Cipher`].
49492f3ab15Sopenharmony_cipub struct CipherRef(Opaque);
49592f3ab15Sopenharmony_ci
49692f3ab15Sopenharmony_ciimpl ForeignTypeRef for CipherRef {
49792f3ab15Sopenharmony_ci    type CType = ffi::EVP_CIPHER;
49892f3ab15Sopenharmony_ci}
49992f3ab15Sopenharmony_ci
50092f3ab15Sopenharmony_ciunsafe impl Sync for CipherRef {}
50192f3ab15Sopenharmony_ciunsafe impl Send for CipherRef {}
50292f3ab15Sopenharmony_ci
50392f3ab15Sopenharmony_ciimpl CipherRef {
50492f3ab15Sopenharmony_ci    /// Returns the cipher's Nid.
50592f3ab15Sopenharmony_ci    #[corresponds(EVP_CIPHER_nid)]
50692f3ab15Sopenharmony_ci    pub fn nid(&self) -> Nid {
50792f3ab15Sopenharmony_ci        let nid = unsafe { ffi::EVP_CIPHER_nid(self.as_ptr()) };
50892f3ab15Sopenharmony_ci        Nid::from_raw(nid)
50992f3ab15Sopenharmony_ci    }
51092f3ab15Sopenharmony_ci
51192f3ab15Sopenharmony_ci    /// Returns the length of keys used with this cipher.
51292f3ab15Sopenharmony_ci    #[corresponds(EVP_CIPHER_key_length)]
51392f3ab15Sopenharmony_ci    pub fn key_length(&self) -> usize {
51492f3ab15Sopenharmony_ci        unsafe { EVP_CIPHER_key_length(self.as_ptr()) as usize }
51592f3ab15Sopenharmony_ci    }
51692f3ab15Sopenharmony_ci
51792f3ab15Sopenharmony_ci    /// Returns the length of the IV used with this cipher.
51892f3ab15Sopenharmony_ci    ///
51992f3ab15Sopenharmony_ci    /// # Note
52092f3ab15Sopenharmony_ci    ///
52192f3ab15Sopenharmony_ci    /// Ciphers that do not use an IV have an IV length of 0.
52292f3ab15Sopenharmony_ci    #[corresponds(EVP_CIPHER_iv_length)]
52392f3ab15Sopenharmony_ci    pub fn iv_length(&self) -> usize {
52492f3ab15Sopenharmony_ci        unsafe { EVP_CIPHER_iv_length(self.as_ptr()) as usize }
52592f3ab15Sopenharmony_ci    }
52692f3ab15Sopenharmony_ci
52792f3ab15Sopenharmony_ci    /// Returns the block size of the cipher.
52892f3ab15Sopenharmony_ci    ///
52992f3ab15Sopenharmony_ci    /// # Note
53092f3ab15Sopenharmony_ci    ///
53192f3ab15Sopenharmony_ci    /// Stream ciphers have a block size of 1.
53292f3ab15Sopenharmony_ci    #[corresponds(EVP_CIPHER_block_size)]
53392f3ab15Sopenharmony_ci    pub fn block_size(&self) -> usize {
53492f3ab15Sopenharmony_ci        unsafe { EVP_CIPHER_block_size(self.as_ptr()) as usize }
53592f3ab15Sopenharmony_ci    }
53692f3ab15Sopenharmony_ci}
537