Skip to content

Commit 17945a8

Browse files
committed
feat: add support for vendor defined key types
Signed-off-by: Mete Can Eriş <[email protected]>
1 parent ff533d8 commit 17945a8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

cryptoki/src/object.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,32 @@ impl KeyType {
12131213
/// HKDF key
12141214
pub const HKDF: KeyType = KeyType { val: CKK_HKDF };
12151215

1216+
/// Create vendor defined key type
1217+
///
1218+
/// # Arguments
1219+
///
1220+
/// * `val` - The value of vendor defined key type
1221+
///
1222+
/// # Errors
1223+
///
1224+
/// If `val` is less then `CKK_VENDOR_DEFINED`, a `Error::InvalidValue` will be returned
1225+
///
1226+
/// # Examples
1227+
/// ```rust
1228+
/// use cryptoki::object::KeyType;
1229+
/// use cryptoki_sys::CKK_VENDOR_DEFINED;
1230+
///
1231+
/// let some_key_type: KeyType =
1232+
/// KeyType::new_vendor_defined(CKK_VENDOR_DEFINED | 0x14).unwrap();
1233+
/// ```
1234+
pub fn new_vendor_defined(val: CK_KEY_TYPE) -> Result<KeyType> {
1235+
if val < CKK_VENDOR_DEFINED {
1236+
Err(Error::InvalidValue)
1237+
} else {
1238+
Ok(KeyType { val })
1239+
}
1240+
}
1241+
12161242
fn stringify(key_type: CK_KEY_TYPE) -> String {
12171243
match key_type {
12181244
CKK_RSA => String::from(stringify!(CKK_RSA)),
@@ -1259,6 +1285,7 @@ impl KeyType {
12591285
CKK_EC_EDWARDS => String::from(stringify!(CKK_EC_EDWARDS)),
12601286
CKK_EC_MONTGOMERY => String::from(stringify!(CKK_EC_MONTGOMERY)),
12611287
CKK_HKDF => String::from(stringify!(CKK_HKDF)),
1288+
CKK_VENDOR_DEFINED..=MAX_CU_ULONG => String::from(stringify!(key_type)),
12621289
_ => format!("unknown ({key_type:08x})"),
12631290
}
12641291
}
@@ -1333,6 +1360,7 @@ impl TryFrom<CK_KEY_TYPE> for KeyType {
13331360
CKK_EC_EDWARDS => Ok(KeyType::EC_EDWARDS),
13341361
CKK_EC_MONTGOMERY => Ok(KeyType::EC_MONTGOMERY),
13351362
CKK_HKDF => Ok(KeyType::HKDF),
1363+
CKK_VENDOR_DEFINED..=MAX_CU_ULONG => KeyType::new_vendor_defined(key_type),
13361364
_ => {
13371365
error!("Key type {} is not supported.", key_type);
13381366
Err(Error::NotSupported)

0 commit comments

Comments
 (0)