Skip to content

Commit 89f4c56

Browse files
Changed paradigm for how additional derived keys should be retrieved after KDF operation
Signed-off-by: Jacob Prud'homme <[email protected]>
1 parent 07ba6f1 commit 89f4c56

File tree

3 files changed

+15
-75
lines changed

3 files changed

+15
-75
lines changed

cryptoki/src/mechanism/kbkdf.rs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,16 @@ impl DerivedKey {
182182

183183
Self {
184184
template,
185-
handle: 0,
185+
handle: cryptoki_sys::CK_INVALID_HANDLE,
186+
}
187+
}
188+
189+
/// Return handle for derived key, if it has been created yet
190+
pub fn handle(&self) -> Option<ObjectHandle> {
191+
if self.handle == cryptoki_sys::CK_INVALID_HANDLE {
192+
None
193+
} else {
194+
Some(ObjectHandle::new(self.handle))
186195
}
187196
}
188197
}
@@ -207,7 +216,7 @@ impl From<&mut DerivedKey> for cryptoki_sys::CK_DERIVED_KEY {
207216
#[derive(Debug)]
208217
pub struct KbkdfParams<'a> {
209218
/// Holds own data so that we have a contiguous memory region to give to backend
210-
additional_derived_keys: Option<Pin<Box<[cryptoki_sys::CK_DERIVED_KEY]>>>,
219+
_additional_derived_keys: Option<Pin<Box<[cryptoki_sys::CK_DERIVED_KEY]>>>,
211220

212221
inner: cryptoki_sys::CK_SP800_108_KDF_PARAMS,
213222
/// Marker type to ensure we don't outlive the data
@@ -258,7 +267,7 @@ impl<'a> KbkdfParams<'a> {
258267
};
259268

260269
Self {
261-
additional_derived_keys,
270+
_additional_derived_keys: additional_derived_keys,
262271

263272
inner,
264273
_marker: PhantomData,
@@ -268,18 +277,6 @@ impl<'a> KbkdfParams<'a> {
268277
pub(crate) fn inner(&self) -> &cryptoki_sys::CK_SP800_108_KDF_PARAMS {
269278
&self.inner
270279
}
271-
272-
/// The additional keys derived by the KDF, as per the params
273-
pub(crate) fn additional_derived_keys(&self) -> Option<Vec<ObjectHandle>> {
274-
self.additional_derived_keys.as_ref().map(|keys| {
275-
keys.iter()
276-
.map(|key| {
277-
// SAFETY: a value is always provided during construction
278-
ObjectHandle::new(unsafe { *key.phKey })
279-
})
280-
.collect()
281-
})
282-
}
283280
}
284281

285282
/// NIST SP 800-108 (aka KBKDF) feedback-mode parameters.
@@ -288,7 +285,7 @@ impl<'a> KbkdfParams<'a> {
288285
#[derive(Debug)]
289286
pub struct KbkdfFeedbackParams<'a> {
290287
/// Holds own data so that we have a contiguous memory region to give to backend
291-
additional_derived_keys: Option<Pin<Box<[cryptoki_sys::CK_DERIVED_KEY]>>>,
288+
_additional_derived_keys: Option<Pin<Box<[cryptoki_sys::CK_DERIVED_KEY]>>>,
292289

293290
inner: cryptoki_sys::CK_SP800_108_FEEDBACK_KDF_PARAMS,
294291
/// Marker type to ensure we don't outlive the data
@@ -348,7 +345,7 @@ impl<'a> KbkdfFeedbackParams<'a> {
348345
};
349346

350347
Self {
351-
additional_derived_keys,
348+
_additional_derived_keys: additional_derived_keys,
352349

353350
inner,
354351
_marker: PhantomData,
@@ -358,16 +355,4 @@ impl<'a> KbkdfFeedbackParams<'a> {
358355
pub(crate) fn inner(&self) -> &cryptoki_sys::CK_SP800_108_FEEDBACK_KDF_PARAMS {
359356
&self.inner
360357
}
361-
362-
/// The additional keys derived by the KDF, as per the params
363-
pub(crate) fn additional_derived_keys(&self) -> Option<Vec<ObjectHandle>> {
364-
self.additional_derived_keys.as_ref().map(|keys| {
365-
keys.iter()
366-
.map(|key| {
367-
// SAFETY: a value is always provided during construction
368-
ObjectHandle::new(unsafe { *key.phKey })
369-
})
370-
.collect()
371-
})
372-
}
373358
}

cryptoki/src/mechanism/mod.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use vendor_defined::VendorDefinedMechanism;
2424

2525
use crate::error::Error;
2626
use crate::mechanism::rsa::PkcsOaepParams;
27-
use crate::object::ObjectHandle;
2827
pub use mechanism_info::MechanismInfo;
2928

3029
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
@@ -1227,20 +1226,3 @@ impl MessageParam<'_> {
12271226
}
12281227
}
12291228
}
1230-
1231-
/// Trait for mechanism types that define additional keys to be derived in their parameters
1232-
pub trait HasAdditionalDerivedKeys {
1233-
/// Get the object handles for the additional keys that were derived
1234-
fn additional_derived_keys(&self) -> Vec<ObjectHandle>;
1235-
}
1236-
1237-
impl HasAdditionalDerivedKeys for &Mechanism<'_> {
1238-
fn additional_derived_keys(&self) -> Vec<ObjectHandle> {
1239-
match self {
1240-
Mechanism::KbkdfCounter(params) => params.additional_derived_keys().unwrap_or_default(),
1241-
Mechanism::KbkdfFeedback(params) => params.additional_derived_keys().unwrap_or_default(),
1242-
Mechanism::KbkdfDoublePipeline(params) => params.additional_derived_keys().unwrap_or_default(),
1243-
_ => unimplemented!("The given mechanism doesn't define additional keys to derive"), // TODO: this or return an option?
1244-
}
1245-
}
1246-
}

cryptoki/src/session/key_management.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use crate::context::Function;
66
use crate::error::{Result, Rv};
7-
use crate::mechanism::{HasAdditionalDerivedKeys as _, Mechanism};
7+
use crate::mechanism::Mechanism;
88
use crate::object::{Attribute, ObjectHandle};
99
use crate::session::Session;
1010
use cryptoki_sys::{CK_ATTRIBUTE, CK_MECHANISM, CK_MECHANISM_PTR};
@@ -93,33 +93,6 @@ impl Session {
9393
Ok(ObjectHandle::new(handle))
9494
}
9595

96-
/// Derives multiple keys from a base key
97-
pub fn derive_keys(
98-
&self,
99-
mechanism: &Mechanism,
100-
base_key: ObjectHandle,
101-
template: &[Attribute],
102-
) -> Result<(ObjectHandle, Vec<ObjectHandle>)> {
103-
let mut c_mechanism: CK_MECHANISM = mechanism.into();
104-
let mut template: Vec<CK_ATTRIBUTE> = template.iter().map(|attr| attr.into()).collect();
105-
let mut handle = 0;
106-
unsafe {
107-
Rv::from(get_pkcs11!(self.client(), C_DeriveKey)(
108-
self.handle(),
109-
&mut c_mechanism as CK_MECHANISM_PTR,
110-
base_key.handle(),
111-
template.as_mut_ptr(),
112-
template.len().try_into()?,
113-
&mut handle,
114-
))
115-
.into_result(Function::DeriveKey)?;
116-
}
117-
118-
let additional_key_handles = mechanism.additional_derived_keys();
119-
120-
Ok((ObjectHandle::new(handle), additional_key_handles))
121-
}
122-
12396
/// Wrap key
12497
pub fn wrap_key(
12598
&self,

0 commit comments

Comments
 (0)