-
Notifications
You must be signed in to change notification settings - Fork 77
feat: add HKDF mechanisms #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2021 Contributors to the Parsec project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
//! Mechanisms of hash-based key derive function (HKDF) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be good to insert a copyright header here, just like so: https://github.com/parallaxsecond/rust-cryptoki/blob/main/cryptoki/src/types.rs#L1-L2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wiktor-k do we use to put 2021 everywhere or the year the file is added instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use 2024 here instead then, until we find a better solution :) |
||
//! See: <https://docs.oasis-open.org/pkcs11/pkcs11-curr/v3.0/os/pkcs11-curr-v3.0-os.html#_Toc30061597> | ||
|
||
use std::{convert::TryInto, marker::PhantomData, ptr::null_mut, slice}; | ||
|
||
use cryptoki_sys::{CKF_HKDF_SALT_DATA, CKF_HKDF_SALT_KEY, CKF_HKDF_SALT_NULL}; | ||
|
||
use crate::object::ObjectHandle; | ||
|
||
use super::MechanismType; | ||
|
||
/// The salt for the extract stage. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you move the doc comment above the |
||
#[derive(Debug, Clone, Copy)] | ||
pub enum HkdfSalt<'a> { | ||
/// CKF_HKDF_SALT_NULL no salt is supplied. | ||
Null, | ||
/// CKF_HKDF_SALT_DATA salt is supplied as a data in pSalt with length ulSaltLen. | ||
Data(&'a [u8]), | ||
/// CKF_HKDF_SALT_KEY salt is supplied as a key in hSaltKey | ||
Key(ObjectHandle), | ||
} | ||
|
||
/// HKDF parameters. | ||
/// | ||
/// This structure wraps a `CK_HKDF_PARAMS` structure. | ||
#[derive(Debug, Clone, Copy)] | ||
#[repr(transparent)] | ||
pub struct HkdfParams<'a> { | ||
inner: cryptoki_sys::CK_HKDF_PARAMS, | ||
/// Marker type to ensure we don't outlive the data | ||
_marker: PhantomData<&'a [u8]>, | ||
} | ||
|
||
impl<'a> HkdfParams<'a> { | ||
/// Construct parameters for hash-based key derive function (HKDF). | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `prf_hash_mechanism` - The base hash used for the HMAC in the underlying HKDF operation | ||
/// | ||
/// * `salt` - The salt for the extract stage, skip extract if `None`. | ||
/// | ||
/// * `info` - The info string for the expand stage, skip expand if `None`. | ||
pub fn new( | ||
prf_hash_mechanism: MechanismType, | ||
salt: Option<HkdfSalt>, | ||
info: Option<&'a [u8]>, | ||
) -> Self { | ||
Self { | ||
inner: cryptoki_sys::CK_HKDF_PARAMS { | ||
bExtract: salt.is_some() as u8, | ||
bExpand: info.is_some() as u8, | ||
prfHashMechanism: *prf_hash_mechanism, | ||
ulSaltType: match salt { | ||
None | Some(HkdfSalt::Null) => CKF_HKDF_SALT_NULL, | ||
Some(HkdfSalt::Data(_)) => CKF_HKDF_SALT_DATA, | ||
Some(HkdfSalt::Key(_)) => CKF_HKDF_SALT_KEY, | ||
}, | ||
pSalt: if let Some(HkdfSalt::Data(data)) = salt { | ||
data.as_ptr() as *mut _ | ||
} else { | ||
null_mut() | ||
}, | ||
ulSaltLen: if let Some(HkdfSalt::Data(data)) = salt { | ||
data.len() | ||
.try_into() | ||
.expect("salt length does not fit in CK_ULONG") | ||
} else { | ||
0 | ||
}, | ||
hSaltKey: if let Some(HkdfSalt::Key(key)) = salt { | ||
key.handle() | ||
} else { | ||
0 | ||
}, | ||
pInfo: if let Some(info) = info { | ||
info.as_ptr() as *mut _ | ||
} else { | ||
null_mut() | ||
}, | ||
ulInfoLen: if let Some(info) = info { | ||
info.len() | ||
.try_into() | ||
.expect("salt length does not fit in CK_ULONG") | ||
} else { | ||
0 | ||
}, | ||
}, | ||
_marker: PhantomData, | ||
} | ||
} | ||
|
||
/// Whether to execute the extract portion of HKDF. | ||
pub fn extract(&self) -> bool { | ||
self.inner.bExtract != 0 | ||
} | ||
|
||
/// Whether to execute the expand portion of HKDF. | ||
pub fn expand(&self) -> bool { | ||
self.inner.bExpand != 0 | ||
} | ||
|
||
/// The salt for the extract stage. | ||
pub fn salt(&self) -> HkdfSalt<'a> { | ||
match self.inner.ulSaltType { | ||
CKF_HKDF_SALT_NULL => HkdfSalt::Null, | ||
CKF_HKDF_SALT_DATA => HkdfSalt::Data(unsafe { | ||
slice::from_raw_parts(self.inner.pSalt, self.inner.ulSaltLen as _) | ||
}), | ||
CKF_HKDF_SALT_KEY => HkdfSalt::Key(ObjectHandle::new(self.inner.hSaltKey)), | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
/// The info string for the expand stage. | ||
pub fn info(&self) -> &'a [u8] { | ||
unsafe { slice::from_raw_parts(self.inner.pInfo, self.inner.ulInfoLen as _) } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.