variant_ssl/
mac.rs

1use crate::cvt_p;
2use crate::error::ErrorStack;
3use crate::lib_ctx::LibCtxRef;
4use foreign_types::{ForeignType, ForeignTypeRef};
5use openssl_macros::corresponds;
6use std::ffi::CString;
7use std::ptr;
8
9foreign_type_and_impl_send_sync! {
10    type CType = ffi::EVP_MAC;
11    fn drop = ffi::EVP_MAC_free;
12
13    /// A context object used to perform MAC operations.
14    pub struct Mac;
15    /// A reference to a [`HmacCtx`].
16    pub struct MacRef;
17}
18
19impl Mac {
20    /// Fetches an implementation of a MAC algorithm, given a library context libctx and a set of properties.
21    ///
22    /// Requires OpenSSL 3.0.0 or newer.
23    #[corresponds(EVP_MAC_fetch)]
24    pub fn fetch(
25        ctx: Option<&LibCtxRef>,
26        algorithm: &str,
27        properties: Option<&str>,
28    ) -> Result<Self, ErrorStack> {
29        let algorithm = CString::new(algorithm).unwrap();
30        let properties = properties.map(|s| CString::new(s).unwrap());
31
32        unsafe {
33            let ptr = cvt_p(ffi::EVP_MAC_fetch(
34                ctx.map_or(ptr::null_mut(), ForeignTypeRef::as_ptr),
35                algorithm.as_ptr(),
36                properties.as_ref().map_or(ptr::null_mut(), |s| s.as_ptr()),
37            ))?;
38
39            Ok(Mac::from_ptr(ptr))
40        }
41    }
42}