Skip to content

Commit 35884b7

Browse files
Add mechanisms for simple key derivation
Signed-off-by: Jacob Prud'homme <[email protected]>
1 parent 58e80c3 commit 35884b7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

cryptoki/src/mechanism/mod.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use std::ptr::null_mut;
2525
use vendor_defined::VendorDefinedMechanism;
2626

2727
use crate::error::Error;
28+
use crate::mechanism::misc::{ExtractParams, KeyDerivationStringData};
2829
use crate::mechanism::rsa::PkcsOaepParams;
30+
use crate::object::ObjectHandle;
2931
pub use mechanism_info::MechanismInfo;
3032

3133
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
@@ -343,6 +345,28 @@ impl MechanismType {
343345
val: CKM_SP800_108_DOUBLE_PIPELINE_KDF,
344346
};
345347

348+
// Other simple key derivation mechanisms
349+
/// Concatenation of a base key and another key
350+
pub const CONCATENATE_BASE_AND_KEY: MechanismType = MechanismType {
351+
val: CKM_CONCATENATE_BASE_AND_KEY,
352+
};
353+
/// Concatenation of a base key and data (i.e. data appended)
354+
pub const CONCATENATE_BASE_AND_DATA: MechanismType = MechanismType {
355+
val: CKM_CONCATENATE_BASE_AND_DATA,
356+
};
357+
/// Concatenation of data and a base key (i.e. data prepended)
358+
pub const CONCATENATE_DATA_AND_BASE: MechanismType = MechanismType {
359+
val: CKM_CONCATENATE_DATA_AND_BASE,
360+
};
361+
/// XOR-ing of a base key and data
362+
pub const XOR_BASE_AND_DATA: MechanismType = MechanismType {
363+
val: CKM_XOR_BASE_AND_DATA,
364+
};
365+
/// Extraction of a key from bits of another key
366+
pub const EXTRACT_KEY_FROM_KEY: MechanismType = MechanismType {
367+
val: CKM_EXTRACT_KEY_FROM_KEY,
368+
};
369+
346370
// ML-KEM
347371
/// ML-KEM key pair generation mechanism
348372
pub const ML_KEM_KEY_PAIR_GEN: MechanismType = MechanismType {
@@ -899,6 +923,11 @@ impl TryFrom<CK_MECHANISM_TYPE> for MechanismType {
899923
CKM_SP800_108_COUNTER_KDF => Ok(MechanismType::SP800_108_COUNTER_KDF),
900924
CKM_SP800_108_FEEDBACK_KDF => Ok(MechanismType::SP800_108_FEEDBACK_KDF),
901925
CKM_SP800_108_DOUBLE_PIPELINE_KDF => Ok(MechanismType::SP800_108_DOUBLE_PIPELINE_KDF),
926+
CKM_CONCATENATE_BASE_AND_KEY => Ok(MechanismType::CONCATENATE_BASE_AND_KEY),
927+
CKM_CONCATENATE_BASE_AND_DATA => Ok(MechanismType::CONCATENATE_BASE_AND_DATA),
928+
CKM_CONCATENATE_DATA_AND_BASE => Ok(MechanismType::CONCATENATE_DATA_AND_BASE),
929+
CKM_XOR_BASE_AND_DATA => Ok(MechanismType::XOR_BASE_AND_DATA),
930+
CKM_EXTRACT_KEY_FROM_KEY => Ok(MechanismType::EXTRACT_KEY_FROM_KEY),
902931
CKM_ML_KEM_KEY_PAIR_GEN => Ok(MechanismType::ML_KEM_KEY_PAIR_GEN),
903932
CKM_ML_KEM => Ok(MechanismType::ML_KEM),
904933
CKM_ML_DSA_KEY_PAIR_GEN => Ok(MechanismType::ML_DSA_KEY_PAIR_GEN),
@@ -1143,6 +1172,18 @@ pub enum Mechanism<'a> {
11431172
/// NIST SP 800-108 KDF (aka KBKDF) mechanism in double pipeline-mode
11441173
KbkdfDoublePipeline(kbkdf::KbkdfParams<'a>),
11451174

1175+
// Other simple key derivation mechanisms
1176+
/// Concatenation of a base key and another key
1177+
ConcatenateBaseAndKey(ObjectHandle),
1178+
/// Concatenation of a base key and data (i.e. data appended)
1179+
ConcatenateBaseAndData(KeyDerivationStringData<'a>),
1180+
/// Concatenation of data and a base key (i.e. data prepended)
1181+
ConcatenateDataAndBase(KeyDerivationStringData<'a>),
1182+
/// XOR-ing of a base key and data
1183+
XorBaseAndData(KeyDerivationStringData<'a>),
1184+
/// Extraction of a key from bits of another key
1185+
ExtractKeyFromKey(ExtractParams),
1186+
11461187
// ML-KEM
11471188
/// ML-KEM key pair generation mechanism
11481189
MlKemKeyPairGen,
@@ -1262,6 +1303,12 @@ impl Mechanism<'_> {
12621303
Mechanism::KbkdfFeedback(_) => MechanismType::SP800_108_FEEDBACK_KDF,
12631304
Mechanism::KbkdfDoublePipeline(_) => MechanismType::SP800_108_DOUBLE_PIPELINE_KDF,
12641305

1306+
Mechanism::ConcatenateBaseAndKey(_) => MechanismType::CONCATENATE_BASE_AND_KEY,
1307+
Mechanism::ConcatenateBaseAndData(_) => MechanismType::CONCATENATE_BASE_AND_DATA,
1308+
Mechanism::ConcatenateDataAndBase(_) => MechanismType::CONCATENATE_DATA_AND_BASE,
1309+
Mechanism::XorBaseAndData(_) => MechanismType::XOR_BASE_AND_DATA,
1310+
Mechanism::ExtractKeyFromKey(_) => MechanismType::EXTRACT_KEY_FROM_KEY,
1311+
12651312
Mechanism::MlKemKeyPairGen => MechanismType::ML_KEM_KEY_PAIR_GEN,
12661313
Mechanism::MlKem => MechanismType::ML_KEM,
12671314

@@ -1335,6 +1382,11 @@ impl From<&Mechanism<'_>> for CK_MECHANISM {
13351382
make_mechanism(mechanism, params.inner())
13361383
}
13371384
Mechanism::KbkdfFeedback(params) => make_mechanism(mechanism, params.inner()),
1385+
Mechanism::ConcatenateBaseAndKey(params) => make_mechanism(mechanism, params),
1386+
Mechanism::ConcatenateBaseAndData(params)
1387+
| Mechanism::ConcatenateDataAndBase(params)
1388+
| Mechanism::XorBaseAndData(params) => make_mechanism(mechanism, params),
1389+
Mechanism::ExtractKeyFromKey(params) => make_mechanism(mechanism, params),
13381390
Mechanism::HashMlDsa(params) => make_mechanism(mechanism, params),
13391391
Mechanism::MlDsa(params)
13401392
| Mechanism::HashMlDsaSha224(params)

cryptoki/src/object.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
10061006
}
10071007

10081008
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1009+
#[repr(transparent)]
10091010
/// Token specific identifier for an object
10101011
pub struct ObjectHandle {
10111012
handle: CK_OBJECT_HANDLE,

0 commit comments

Comments
 (0)