pub fn extract(version: Version, cipher: Cipher, prk: &SymKey, label: &str) -> Res<Self> {
        const ZERO: &[u8] = &[0; 12];

        let l = label.as_bytes();
        let mut secret: *mut PK11SymKey = null_mut();

        let (mech, key_size) = match cipher {
            TLS_AES_128_GCM_SHA256 => (CK_MECHANISM_TYPE::from(CKM_AES_ECB), 16),
            TLS_AES_256_GCM_SHA384 => (CK_MECHANISM_TYPE::from(CKM_AES_ECB), 32),
            TLS_CHACHA20_POLY1305_SHA256 => (CK_MECHANISM_TYPE::from(CKM_CHACHA20), 32),
            _ => unreachable!(),
        };

        // Note that this doesn't allow for passing null() for the handshake hash.
        // A zero-length slice produces an identical result.
        unsafe {
            SSL_HkdfExpandLabelWithMech(
                version,
                cipher,
                **prk,
                null(),
                0,
                l.as_ptr().cast(),
                c_uint::try_from(l.len())?,
                mech,
                key_size,
                &mut secret,
            )
        }?;
        let key = SymKey::from_ptr(secret).or(Err(Error::HkdfError))?;

        let res = match cipher {
            TLS_AES_128_GCM_SHA256 | TLS_AES_256_GCM_SHA384 => {
                let context_ptr = unsafe {
                    PK11_CreateContextBySymKey(
                        mech,
                        CK_ATTRIBUTE_TYPE::from(CKA_ENCRYPT),
                        *key,
                        &Item::wrap(&ZERO[..0]), // Borrow a zero-length slice of ZERO.
                    )
                };
                let context = Context::from_ptr(context_ptr).or(Err(Error::CipherInitFailure))?;
                Self::Aes(Rc::new(RefCell::new(context)))
            }
            TLS_CHACHA20_POLY1305_SHA256 => Self::Chacha(key),
            _ => unreachable!(),
        };

        debug_assert_eq!(
            res.block_size(),
            usize::try_from(unsafe { PK11_GetBlockSize(mech, null_mut()) }).unwrap()
        );
        Ok(res)
    }
// Cast for PK11_GetBlockSize is safe.
Cast for PK11_GetBlockSize is safe.
Cast for PK11_GetBlockSize is safe .
pub fn extract(version: Version, cipher: Cipher, prk: &SymKey, label: &str) -> Res<Self> {
        const ZERO: &[u8] = &[0; 12];
        let l = label.as_bytes();
        let mut secret: *mut PK11SymKey = null_mut();
        let (mech, key_size) = match cipher {
            TLS_AES_128_GCM_SHA256 => (CK_MECHANISM_TYPE::from(CKM_AES_ECB), 16),
            TLS_AES_256_GCM_SHA384 => (CK_MECHANISM_TYPE::from(CKM_AES_ECB), 32),
            TLS_CHACHA20_POLY1305_SHA256 => (CK_MECHANISM_TYPE::from(CKM_CHACHA20), 32),
            _ => unreachable!(),
        };
        // Note that this doesn't allow for passing null() for the handshake hash.
        // A zero-length slice produces an identical result.
        unsafe {
            SSL_HkdfExpandLabelWithMech(
                version,
                cipher,
                **prk,
                null(),
                0,
                l.as_ptr().cast(),
                c_uint::try_from(l.len())?,
                mech,
                key_size,
                &mut secret,
            )
        }?;
        let key = SymKey::from_ptr(secret).or(Err(Error::HkdfError))?;
        let res = match cipher {
            TLS_AES_128_GCM_SHA256 | TLS_AES_256_GCM_SHA384 => {
                let context_ptr = unsafe {
                    PK11_CreateContextBySymKey(
                        mech,
                        CK_ATTRIBUTE_TYPE::from(CKA_ENCRYPT),
                        *key,
                        &Item::wrap(&ZERO[..0]), // Borrow a zero-length slice of ZERO.
                    )
                };
                let context = Context::from_ptr(context_ptr).or(Err(Error::CipherInitFailure))?;
                Self::Aes(Rc::new(RefCell::new(context)))
            }
            TLS_CHACHA20_POLY1305_SHA256 => Self::Chacha(key),
            _ => unreachable!(),
        };
        debug_assert_eq!(
            res.block_size(),
            usize::try_from(unsafe { PK11_GetBlockSize(mech, null_mut()) }).unwrap()
        );
        Ok(res)
    }
pub fn extract ( version : Version , cipher : Cipher , prk : & SymKey , label : & str ) -> Res < Self > { const ZERO : & [ u8 ] = & [ 0 ; 12 ] ; let l = label . as_bytes ( ) ; let mut secret : * mut PK11SymKey = null_mut ( ) ; let ( mech , key_size ) = match cipher { TLS_AES_128_GCM_SHA256 => ( CK_MECHANISM_TYPE :: from ( CKM_AES_ECB ) , 16 ) , TLS_AES_256_GCM_SHA384 => ( CK_MECHANISM_TYPE :: from ( CKM_AES_ECB ) , 32 ) , TLS_CHACHA20_POLY1305_SHA256 => ( CK_MECHANISM_TYPE :: from ( CKM_CHACHA20 ) , 32 ) , _ => unreachable ! ( ) , } ; // Note that this doesn't allow for passing null() for the handshake hash. // A zero-length slice produces an identical result. unsafe { SSL_HkdfExpandLabelWithMech ( version , cipher , * * prk , null ( ) , 0 , l . as_ptr ( ) . cast ( ) , c_uint :: try_from ( l . len ( ) ) ? , mech , key_size , & mut secret , ) } ? ; let key = SymKey :: from_ptr ( secret ) . or ( Err ( Error :: HkdfError ) ) ? ; let res = match cipher { TLS_AES_128_GCM_SHA256 | TLS_AES_256_GCM_SHA384 => { let context_ptr = unsafe { PK11_CreateContextBySymKey ( mech , CK_ATTRIBUTE_TYPE :: from ( CKA_ENCRYPT ) , * key , & Item :: wrap ( & ZERO [ .. 0 ] ) , // Borrow a zero-length slice of ZERO. ) } ; let context = Context :: from_ptr ( context_ptr ) . or ( Err ( Error :: CipherInitFailure ) ) ? ; Self :: Aes ( Rc :: new ( RefCell :: new ( context ) ) ) } TLS_CHACHA20_POLY1305_SHA256 => Self :: Chacha ( key ) , _ => unreachable ! ( ) , } ; debug_assert_eq ! ( res block_size ( ) usize try_from ( unsafe { PK11_GetBlockSize ( mech null_mut ( ) ) } ) unwrap ( ) ) ; Ok ( res ) }
Cast for PK11_GetBlockSize is safe.
Cast for PK11_GetBlockSize is safe .
version
Version
cipher
Cipher
prk
&SymKey
label
&str