summaryrefslogtreecommitdiffstats
path: root/openpgp/src/crypto/backend/openssl/kdf.rs
blob: 11c48667caafe998c764cc0feacafbdbefa800ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use openssl::{
    md::Md,
    pkey::Id,
    pkey_ctx::PkeyCtx,
};

use crate::{
    Result,
    crypto::{
        SessionKey,
        backend::interface::Kdf,
    },
};

impl Kdf for super::Backend {
    fn hkdf_sha256(ikm: &SessionKey, salt: Option<&[u8]>, info: &[u8],
                   okm: &mut SessionKey)
                   -> Result<()>
    {
        let mut pkey = PkeyCtx::new_id(Id::HKDF)?;
        pkey.derive_init()?;
        pkey.set_hkdf_md(Md::sha256())?;
        pkey.set_hkdf_key(&ikm)?;
        if let Some(salt) = salt {
            pkey.set_hkdf_salt(salt)?;
        }
        pkey.add_hkdf_info(info)?;
        pkey.derive(Some(okm))?;
        Ok(())
    }
}