@@ -8,6 +8,7 @@ pub mod elliptic_curve;
88pub mod hkdf;
99mod mechanism_info;
1010pub mod rsa;
11+ pub mod vendor_defined;
1112
1213use crate :: error:: Error ;
1314use cryptoki_sys:: * ;
@@ -18,6 +19,7 @@ use std::fmt::Formatter;
1819use std:: mem:: size_of;
1920use std:: ops:: Deref ;
2021use std:: ptr:: null_mut;
22+ use vendor_defined:: VendorDefinedMechanism ;
2123
2224use crate :: mechanism:: rsa:: PkcsOaepParams ;
2325pub use mechanism_info:: MechanismInfo ;
@@ -298,6 +300,31 @@ impl MechanismType {
298300 /// HKDF-DATA mechanism
299301 pub const HKDF_DATA : MechanismType = MechanismType { val : CKM_HKDF_DATA } ;
300302
303+ /// Create vendor defined mechanism
304+ ///
305+ /// # Arguments
306+ ///
307+ /// * `val` - The value of vendor defined mechanism
308+ ///
309+ /// # Errors
310+ ///
311+ /// If `val` is less then `CKM_VENDOR_DEFINED`, a `Error::InvalidValue` will be returned
312+ ///
313+ /// # Examples
314+ /// ```rust
315+ /// use cryptoki::mechanism::{vendor_defined::CKM_VENDOR_DEFINED, MechanismType};
316+ ///
317+ /// let some_custom_mech: MechanismType =
318+ /// MechanismType::new_vendor_defined(CKM_VENDOR_DEFINED | 0x00000001).unwrap();
319+ /// ```
320+ pub fn new_vendor_defined ( val : CK_MECHANISM_TYPE ) -> crate :: error:: Result < MechanismType > {
321+ if val < CKM_VENDOR_DEFINED {
322+ Err ( Error :: InvalidValue )
323+ } else {
324+ Ok ( MechanismType { val } )
325+ }
326+ }
327+
301328 pub ( crate ) fn stringify ( mech : CK_MECHANISM_TYPE ) -> String {
302329 match mech {
303330 CKM_RSA_PKCS_KEY_PAIR_GEN => String :: from ( stringify ! ( CKM_RSA_PKCS_KEY_PAIR_GEN ) ) ,
@@ -937,6 +964,9 @@ pub enum Mechanism<'a> {
937964 HkdfDerive ( hkdf:: HkdfParams < ' a > ) ,
938965 /// HKDF-DATA mechanism
939966 HkdfData ( hkdf:: HkdfParams < ' a > ) ,
967+
968+ /// Vendor defined mechanism
969+ VendorDefined ( VendorDefinedMechanism < ' a > ) ,
940970}
941971
942972impl Mechanism < ' _ > {
@@ -1008,6 +1038,10 @@ impl Mechanism<'_> {
10081038 Mechanism :: HkdfKeyGen => MechanismType :: HKDF_KEY_GEN ,
10091039 Mechanism :: HkdfDerive ( _) => MechanismType :: HKDF_DERIVE ,
10101040 Mechanism :: HkdfData ( _) => MechanismType :: HKDF_DATA ,
1041+
1042+ Mechanism :: VendorDefined ( vm) => MechanismType {
1043+ val : vm. inner . mechanism ,
1044+ } ,
10111045 }
10121046 }
10131047}
@@ -1087,6 +1121,8 @@ impl From<&Mechanism<'_>> for CK_MECHANISM {
10871121 pParameter : null_mut ( ) ,
10881122 ulParameterLen : 0 ,
10891123 } ,
1124+ // Vendor defined mechanisms
1125+ Mechanism :: VendorDefined ( vm) => vm. inner ,
10901126 }
10911127 }
10921128}
0 commit comments