11//! EdDSA mechanism types
22
33use cryptoki_sys:: * ;
4- use std:: marker:: PhantomData ;
4+ use std:: { convert :: TryInto , ffi :: c_void , marker:: PhantomData , ptr :: null_mut } ;
55
6- /// EdDSA parameters .
6+ /// EdDSA signature schemes .
77///
88/// The EdDSA mechanism, denoted CKM_EDDSA, is a mechanism for
99/// single-part and multipart signatures and verification for
@@ -13,6 +13,88 @@ use std::marker::PhantomData;
1313/// For curves according to RFC 8032, this mechanism has an
1414/// optional parameter, a CK_EDDSA_PARAMS structure.
1515///
16+ /// | Signature Scheme | Mechanism Param | phFlag | Context Data |
17+ /// |------------------|-----------------|--------|--------------|
18+ /// | Ed25519 | Not Required | N/A | N/A |
19+ /// | Ed25519ctx | Required | False | Optional |
20+ /// | Ed25519ph | Required | True | Optional |
21+ /// | Ed448 | Required | False | Optional |
22+ /// | Ed448ph | Required | True | Optional |
23+ ///
24+ /// The absence or presence of the parameter as well as its
25+ /// content is used to identify which signature scheme is to be
26+ /// used.
27+ #[ derive( Debug , Clone , Copy ) ]
28+ pub enum EddsaSignatureScheme < ' a > {
29+ /// Pure EdDSA mode where the scheme is implicitly defined
30+ /// by the curve.
31+ Pure ,
32+ /// Ed25519 signature scheme without additional parameters.
33+ Ed25519 ,
34+ /// Ed25519 signature scheme with optional context-specific
35+ /// data.
36+ Ed25519ctx ( & ' a [ u8 ] ) ,
37+ /// Ed25519 signature scheme with pre-hashing and optional
38+ /// context-specific data.
39+ Ed25519ph ( & ' a [ u8 ] ) ,
40+ /// Ed448 signature scheme with optional context-specific data.
41+ Ed448 ( & ' a [ u8 ] ) ,
42+ /// Ed448 signature scheme with pre-hashing and optional
43+ /// context-specific data.
44+ Ed448ph ( & ' a [ u8 ] ) ,
45+ }
46+
47+ impl EddsaSignatureScheme < ' _ > {
48+ /// Convert an `EddsaSignatureScheme` into the corresponding
49+ /// parameters.
50+ ///
51+ /// This function prepares the appropriate parameters for
52+ /// the mechanism based on the signature scheme variant.
53+ ///
54+ /// # Returns
55+ ///
56+ /// A pointer the mechanism-specific parameters.
57+ ///
58+ /// For `Pure` and `Ed25519`, this returns `null_mut()` as no
59+ /// additional parameters are required. For other schemes, a
60+ /// pointer to the an `CK_EDDSA_PARAMS` structure is returned.
61+ pub fn into_params ( & self ) -> * mut c_void {
62+ match self {
63+ EddsaSignatureScheme :: Pure | EddsaSignatureScheme :: Ed25519 => null_mut ( ) ,
64+ EddsaSignatureScheme :: Ed448 ( context) | EddsaSignatureScheme :: Ed25519ctx ( context) => {
65+ & CK_EDDSA_PARAMS {
66+ phFlag : false . into ( ) ,
67+ pContextData : context. as_ptr ( ) as * mut _ ,
68+ ulContextDataLen : context
69+ . len ( )
70+ . try_into ( )
71+ . expect ( "usize can not fit in CK_ULONG" ) ,
72+ } as * const CK_EDDSA_PARAMS as * mut _
73+ }
74+ EddsaSignatureScheme :: Ed448ph ( context) | EddsaSignatureScheme :: Ed25519ph ( context) => {
75+ & CK_EDDSA_PARAMS {
76+ phFlag : true . into ( ) ,
77+ pContextData : context. as_ptr ( ) as * mut _ ,
78+ ulContextDataLen : context
79+ . len ( )
80+ . try_into ( )
81+ . expect ( "usize can not fit in CK_ULONG" ) ,
82+ } as * const CK_EDDSA_PARAMS as * mut _
83+ }
84+ }
85+ }
86+ }
87+
88+ /// EdDSA parameters.
89+ ///
90+ /// The EdDSA mechanism, denoted CKM_EDDSA, is a mechanism for
91+ /// single-part and multipart signatures and verification for
92+ /// EdDSA. This mechanism implements the five EdDSA signature
93+ /// schemes defined in RFC 8032 and RFC 8410.
94+ ///
95+ /// For curves according to RFC 8032, this mechanism has an
96+ /// optional parameter, a CK_EDDSA_PARAMS structure.
97+ ///
1698/// The absence or presence of the parameter as well as its
1799/// content is used to identify which signature scheme is to be
18100/// used.
@@ -29,7 +111,7 @@ use std::marker::PhantomData;
29111#[ derive( Copy , Debug , Clone ) ]
30112#[ repr( transparent) ]
31113pub struct EddsaParams < ' a > {
32- inner : CK_EDDSA_PARAMS ,
114+ inner : Option < CK_EDDSA_PARAMS > ,
33115 _marker : PhantomData < & ' a [ u8 ] > ,
34116}
35117
@@ -39,23 +121,55 @@ impl EddsaParams<'_> {
39121 /// # Arguments
40122 ///
41123 /// * `params` - The CK_EDDSA_PARAMS structure.
42- pub fn new ( params : CK_EDDSA_PARAMS ) -> Self {
124+ ///
125+ /// # Returns
126+ ///
127+ /// A new EddsaParams struct.
128+ pub fn new ( scheme : EddsaSignatureScheme ) -> Self {
129+ let params =
130+ match scheme {
131+ EddsaSignatureScheme :: Pure | EddsaSignatureScheme :: Ed25519 => None ,
132+ EddsaSignatureScheme :: Ed25519ctx ( context)
133+ | EddsaSignatureScheme :: Ed448 ( context) => Some ( {
134+ CK_EDDSA_PARAMS {
135+ phFlag : false . into ( ) ,
136+ pContextData : context. as_ptr ( ) as * mut _ ,
137+ ulContextDataLen : context
138+ . len ( )
139+ . try_into ( )
140+ . expect ( "usize can not fit in CK_ULONG" ) ,
141+ }
142+ } ) ,
143+ EddsaSignatureScheme :: Ed25519ph ( context)
144+ | EddsaSignatureScheme :: Ed448ph ( context) => Some ( {
145+ CK_EDDSA_PARAMS {
146+ phFlag : true . into ( ) ,
147+ pContextData : context. as_ptr ( ) as * mut _ ,
148+ ulContextDataLen : context
149+ . len ( )
150+ . try_into ( )
151+ . expect ( "usize can not fit in CK_ULONG" ) ,
152+ }
153+ } ) ,
154+ } ;
155+
43156 Self {
44157 inner : params,
45158 _marker : PhantomData ,
46159 }
47160 }
48- }
49161
50- impl Default for EddsaParams < ' _ > {
51- /// Provide a default instance of `EddsaParams`.
162+ /// Retrieve the inner `CK_EDDSA_PARAMS` struct, if present.
52163 ///
53- /// This initializes `EddsaParams` with the default value
54- /// of the `CK_EDDSA_PARAMS` structure.
55- fn default ( ) -> Self {
56- Self {
57- inner : CK_EDDSA_PARAMS :: default ( ) ,
58- _marker : PhantomData ,
59- }
164+ /// This method provides a reference to the `CK_EDDSA_PARAMS`
165+ /// struct encapsulated within the `EddsaParams`, if the signature
166+ /// scheme requires additional parameters.
167+ ///
168+ /// # Returns
169+ ///
170+ /// `Some(&CK_EDDSA_PARAMS)` if the signature scheme has associated
171+ /// parameters, otherwise `None`.
172+ pub fn inner ( & self ) -> Option < & CK_EDDSA_PARAMS > {
173+ self . inner . as_ref ( )
60174 }
61175}
0 commit comments