Skip to content

Commit 3cb3c34

Browse files
Cleaned up code with implementation of From for DerivedKey
Signed-off-by: Jacob Prud'homme <[email protected]>
1 parent d2b09c4 commit 3cb3c34

File tree

1 file changed

+33
-44
lines changed

1 file changed

+33
-44
lines changed

cryptoki/src/mechanism/kbkdf.rs

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ impl DerivedKey {
230230
///
231231
/// * `template` - The template for the key to be derived.
232232
pub fn new(template: &[Attribute]) -> Self {
233-
let template: Box<[cryptoki_sys::CK_ATTRIBUTE]> =
234-
template.iter().map(|attr| attr.into()).collect();
233+
let template: Box<[cryptoki_sys::CK_ATTRIBUTE]> = template.iter().map(Into::into).collect();
235234
let template = Pin::new(template);
236235

237236
Self {
@@ -241,6 +240,20 @@ impl DerivedKey {
241240
}
242241
}
243242

243+
impl From<&mut DerivedKey> for cryptoki_sys::CK_DERIVED_KEY {
244+
fn from(value: &mut DerivedKey) -> Self {
245+
cryptoki_sys::CK_DERIVED_KEY {
246+
pTemplate: value.template.as_ptr() as cryptoki_sys::CK_ATTRIBUTE_PTR,
247+
ulAttributeCount: value
248+
.template
249+
.len()
250+
.try_into()
251+
.expect("number of attributes in template does not fit in CK_ULONG"),
252+
phKey: &mut value.handle as cryptoki_sys::CK_OBJECT_HANDLE_PTR,
253+
}
254+
}
255+
}
256+
244257
/// NIST SP 800-108 (aka KBKDF) counter-mode parameters.
245258
///
246259
/// This structure wraps a `CK_SP800_108_KDF_PARAMS` structure.
@@ -270,21 +283,13 @@ impl<'a> KbkdfCounterParams<'a> {
270283
prf_data_params: &'a [PrfCounterDataParam<'a>],
271284
additional_derived_keys: Option<&'a mut [DerivedKey]>,
272285
) -> Self {
273-
let additional_derived_keys: Option<Box<[cryptoki_sys::CK_DERIVED_KEY]>> =
274-
additional_derived_keys.map(|keys| {
286+
let mut additional_derived_keys = additional_derived_keys
287+
.map(|keys| {
275288
keys.iter_mut()
276-
.map(|key| cryptoki_sys::CK_DERIVED_KEY {
277-
pTemplate: key.template.as_ptr() as cryptoki_sys::CK_ATTRIBUTE_PTR,
278-
ulAttributeCount: key
279-
.template
280-
.len()
281-
.try_into()
282-
.expect("number of attributes in template does not fit in CK_ULONG"),
283-
phKey: &mut key.handle as cryptoki_sys::CK_OBJECT_HANDLE_PTR,
284-
})
285-
.collect()
286-
});
287-
let mut additional_derived_keys = additional_derived_keys.map(|keys| Pin::new(keys));
289+
.map(Into::into)
290+
.collect::<Box<[cryptoki_sys::CK_DERIVED_KEY]>>()
291+
})
292+
.map(Pin::new);
288293

289294
let inner = cryptoki_sys::CK_SP800_108_KDF_PARAMS {
290295
prfType: prf_mechanism.into(),
@@ -362,21 +367,13 @@ impl<'a> KbkdfFeedbackParams<'a> {
362367
iv: Option<&'a [u8]>,
363368
additional_derived_keys: Option<&'a mut [DerivedKey]>,
364369
) -> Self {
365-
let additional_derived_keys: Option<Box<[cryptoki_sys::CK_DERIVED_KEY]>> =
366-
additional_derived_keys.map(|keys| {
370+
let mut additional_derived_keys = additional_derived_keys
371+
.map(|keys| {
367372
keys.iter_mut()
368-
.map(|key| cryptoki_sys::CK_DERIVED_KEY {
369-
pTemplate: key.template.as_ptr() as cryptoki_sys::CK_ATTRIBUTE_PTR,
370-
ulAttributeCount: key
371-
.template
372-
.len()
373-
.try_into()
374-
.expect("number of attributes in template does not fit in CK_ULONG"),
375-
phKey: &mut key.handle as cryptoki_sys::CK_OBJECT_HANDLE_PTR,
376-
})
377-
.collect()
378-
});
379-
let mut additional_derived_keys = additional_derived_keys.map(|keys| Pin::new(keys));
373+
.map(Into::into)
374+
.collect::<Box<[cryptoki_sys::CK_DERIVED_KEY]>>()
375+
})
376+
.map(Pin::new);
380377

381378
let inner = cryptoki_sys::CK_SP800_108_FEEDBACK_KDF_PARAMS {
382379
prfType: prf_mechanism.into(),
@@ -457,21 +454,13 @@ impl<'a> KbkdfDoublePipelineParams<'a> {
457454
prf_data_params: &'a [PrfDataParam<'a>],
458455
additional_derived_keys: Option<&'a mut [DerivedKey]>,
459456
) -> Self {
460-
let additional_derived_keys: Option<Box<[cryptoki_sys::CK_DERIVED_KEY]>> =
461-
additional_derived_keys.map(|keys| {
457+
let mut additional_derived_keys = additional_derived_keys
458+
.map(|keys| {
462459
keys.iter_mut()
463-
.map(|key| cryptoki_sys::CK_DERIVED_KEY {
464-
pTemplate: key.template.as_ptr() as cryptoki_sys::CK_ATTRIBUTE_PTR,
465-
ulAttributeCount: key
466-
.template
467-
.len()
468-
.try_into()
469-
.expect("number of attributes in template does not fit in CK_ULONG"),
470-
phKey: &mut key.handle as cryptoki_sys::CK_OBJECT_HANDLE_PTR,
471-
})
472-
.collect()
473-
});
474-
let mut additional_derived_keys = additional_derived_keys.map(|keys| Pin::new(keys));
460+
.map(Into::into)
461+
.collect::<Box<[cryptoki_sys::CK_DERIVED_KEY]>>()
462+
})
463+
.map(Pin::new);
475464

476465
let inner = cryptoki_sys::CK_SP800_108_KDF_PARAMS {
477466
prfType: prf_mechanism.into(),

0 commit comments

Comments
 (0)