Skip to content

Commit 9a0092a

Browse files
Create new parameters for simple key derivation mechanisms
These include the CKM_{CONCATENATE,XOR}_{BASE,DATA}_AND_{DATA,BASE}, CKM_CONCATENATE_BASE_AND_KEY and CKM_EXTRACT_KEY_FROM_KEY mechanisms Signed-off-by: Jacob Prud'homme <2160185+jacobprudhomme@users.noreply.github.com>
1 parent 3ac2766 commit 9a0092a

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

cryptoki/src/mechanism/misc.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2025 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//! Miscellaneous mechanisms:
4+
//! - Simple key derivation mechanisms
5+
//! See: <https://docs.oasis-open.org/pkcs11/pkcs11-spec/v3.1/os/pkcs11-spec-v3.1-os.html#_Toc111203697>
6+
7+
use std::marker::PhantomData;
8+
9+
use cryptoki_sys::*;
10+
11+
/// A parameter used as input for one of the simple key derivation mechanisms
12+
/// that takes a bytestring as input (CKM_CONCATENATE_BASE_AND_DATA,
13+
/// CKM_CONCATENATE_DATA_AND_BASE, CKM_XOR_BASE_AND_DATA).
14+
#[derive(Debug, Clone, Copy)]
15+
#[repr(transparent)]
16+
pub struct KeyDerivationStringData<'a> {
17+
inner: CK_KEY_DERIVATION_STRING_DATA,
18+
/// Marker type to ensure we don't outlive the data
19+
_marker: PhantomData<&'a [u8]>,
20+
}
21+
22+
impl<'a> KeyDerivationStringData<'a> {
23+
/// Construct parameter for simple key derivation mechanisms that take a
24+
/// bytestring as one of their inputs.
25+
///
26+
/// # Arguments
27+
///
28+
/// * `data` - The bytestring to use as input to the key derivation method.
29+
pub fn new(data: &'a [u8]) -> Self {
30+
Self {
31+
inner: CK_KEY_DERIVATION_STRING_DATA {
32+
pData: data.as_ptr() as *mut _,
33+
ulLen: data
34+
.len()
35+
.try_into()
36+
.expect("length of data does not fit in CK_ULONG"),
37+
},
38+
_marker: PhantomData,
39+
}
40+
}
41+
}
42+
43+
/// A parameter indicating the index of the base key from which to extract the
44+
/// derived key.
45+
#[derive(Debug, Clone, Copy)]
46+
#[repr(transparent)]
47+
pub struct ExtractParams(CK_EXTRACT_PARAMS);
48+
49+
impl ExtractParams {
50+
/// Construct parameter from index to extract the derived key from the base
51+
/// key.
52+
///
53+
/// # Arguments
54+
///
55+
/// * `index` - The index from which to extract the derived key from the base key.
56+
pub fn new(index: usize) -> Self {
57+
Self(
58+
index
59+
.try_into()
60+
.expect("given usize value does not fit into CK_ULONG"),
61+
)
62+
}
63+
}

cryptoki/src/mechanism/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod elliptic_curve;
1010
pub mod hkdf;
1111
pub mod kbkdf;
1212
mod mechanism_info;
13+
pub mod misc;
1314
pub mod rsa;
1415
pub mod vendor_defined;
1516

0 commit comments

Comments
 (0)