55
66use core:: { convert:: TryInto , marker:: PhantomData , pin:: Pin , ptr} ;
77
8+ use cryptoki_sys:: {
9+ CK_ATTRIBUTE , CK_ATTRIBUTE_PTR , CK_DERIVED_KEY , CK_DERIVED_KEY_PTR , CK_INVALID_HANDLE ,
10+ CK_OBJECT_HANDLE , CK_OBJECT_HANDLE_PTR , CK_PRF_DATA_PARAM , CK_PRF_DATA_PARAM_PTR ,
11+ CK_SP800_108_BYTE_ARRAY , CK_SP800_108_COUNTER , CK_SP800_108_COUNTER_FORMAT ,
12+ CK_SP800_108_DKM_LENGTH , CK_SP800_108_DKM_LENGTH_FORMAT , CK_SP800_108_DKM_LENGTH_SUM_OF_KEYS ,
13+ CK_SP800_108_DKM_LENGTH_SUM_OF_SEGMENTS , CK_SP800_108_FEEDBACK_KDF_PARAMS ,
14+ CK_SP800_108_ITERATION_VARIABLE , CK_SP800_108_KDF_PARAMS , CK_ULONG ,
15+ } ;
16+
817use crate :: object:: { Attribute , ObjectHandle } ;
918
1019use super :: MechanismType ;
@@ -23,7 +32,7 @@ pub enum Endianness {
2332/// This structure wraps a `CK_SP800_108_COUNTER_FORMAT` structure.
2433#[ derive( Debug , Clone , Copy ) ]
2534#[ repr( transparent) ]
26- pub struct KbkdfCounterFormat ( cryptoki_sys :: CK_SP800_108_COUNTER_FORMAT ) ;
35+ pub struct KbkdfCounterFormat ( CK_SP800_108_COUNTER_FORMAT ) ;
2736
2837impl KbkdfCounterFormat {
2938 /// Construct encoding format for KDF's internal counter variable.
@@ -34,7 +43,7 @@ impl KbkdfCounterFormat {
3443 ///
3544 /// * `width_in_bits` - The number of bits used to represent the counter value.
3645 pub fn new ( endianness : Endianness , width_in_bits : usize ) -> Self {
37- Self ( cryptoki_sys :: CK_SP800_108_COUNTER_FORMAT {
46+ Self ( CK_SP800_108_COUNTER_FORMAT {
3847 bLittleEndian : ( endianness == Endianness :: Little ) . into ( ) ,
3948 ulWidthInBits : width_in_bits
4049 . try_into ( )
@@ -59,7 +68,7 @@ pub enum DkmLengthMethod {
5968/// This structure wraps a `CK_SP800_108_DKM_LENGTH_FORMAT` structure.
6069#[ derive( Debug , Clone , Copy ) ]
6170#[ repr( transparent) ]
62- pub struct KbkdfDkmLengthFormat ( cryptoki_sys :: CK_SP800_108_DKM_LENGTH_FORMAT ) ;
71+ pub struct KbkdfDkmLengthFormat ( CK_SP800_108_DKM_LENGTH_FORMAT ) ;
6372
6473impl KbkdfDkmLengthFormat {
6574 /// Construct encoding format for length value of DKM (derived key material) from KDF.
@@ -76,17 +85,15 @@ impl KbkdfDkmLengthFormat {
7685 endianness : Endianness ,
7786 width_in_bits : usize ,
7887 ) -> Self {
79- Self ( cryptoki_sys :: CK_SP800_108_DKM_LENGTH_FORMAT {
88+ Self ( CK_SP800_108_DKM_LENGTH_FORMAT {
8089 dkmLengthMethod : match dkm_length_method {
81- DkmLengthMethod :: SumOfKeys => cryptoki_sys:: CK_SP800_108_DKM_LENGTH_SUM_OF_KEYS ,
82- DkmLengthMethod :: SumOfSegments => {
83- cryptoki_sys:: CK_SP800_108_DKM_LENGTH_SUM_OF_SEGMENTS
84- }
90+ DkmLengthMethod :: SumOfKeys => CK_SP800_108_DKM_LENGTH_SUM_OF_KEYS ,
91+ DkmLengthMethod :: SumOfSegments => CK_SP800_108_DKM_LENGTH_SUM_OF_SEGMENTS ,
8592 } ,
8693 bLittleEndian : ( endianness == Endianness :: Little ) . into ( ) ,
87- ulWidthInBits : width_in_bits. try_into ( ) . expect (
88- "bit width of KBKDF DKM length value does not fit in CK_ULONG" ,
89- ) ,
94+ ulWidthInBits : width_in_bits
95+ . try_into ( )
96+ . expect ( "bit width of KBKDF DKM length value does not fit in CK_ULONG" ) ,
9097 } )
9198 }
9299}
@@ -118,7 +125,7 @@ pub enum PrfDataParamType<'a> {
118125#[ derive( Debug , Clone , Copy ) ]
119126#[ repr( transparent) ]
120127pub struct PrfDataParam < ' a > {
121- inner : cryptoki_sys :: CK_PRF_DATA_PARAM ,
128+ inner : CK_PRF_DATA_PARAM ,
122129 /// Marker type to ensure we don't outlive the data
123130 _marker : PhantomData < & ' a [ u8 ] > ,
124131}
@@ -132,33 +139,28 @@ impl<'a> PrfDataParam<'a> {
132139 pub fn new ( type_ : PrfDataParamType < ' a > ) -> Self {
133140 Self {
134141 inner : match type_ {
135- PrfDataParamType :: IterationVariable ( None ) => cryptoki_sys :: CK_PRF_DATA_PARAM {
136- type_ : cryptoki_sys :: CK_SP800_108_ITERATION_VARIABLE ,
142+ PrfDataParamType :: IterationVariable ( None ) => CK_PRF_DATA_PARAM {
143+ type_ : CK_SP800_108_ITERATION_VARIABLE ,
137144 pValue : ptr:: null_mut ( ) ,
138145 ulValueLen : 0 ,
139146 } ,
140- PrfDataParamType :: IterationVariable ( Some ( counter_format) ) => {
141- cryptoki_sys:: CK_PRF_DATA_PARAM {
142- type_ : cryptoki_sys:: CK_SP800_108_ITERATION_VARIABLE ,
143- pValue : counter_format as * const _ as * mut _ ,
144- ulValueLen : size_of :: < cryptoki_sys:: CK_SP800_108_COUNTER_FORMAT > ( )
145- as cryptoki_sys:: CK_ULONG ,
146- }
147- }
148- PrfDataParamType :: Counter ( counter_format) => cryptoki_sys:: CK_PRF_DATA_PARAM {
149- type_ : cryptoki_sys:: CK_SP800_108_COUNTER ,
147+ PrfDataParamType :: IterationVariable ( Some ( counter_format) ) => CK_PRF_DATA_PARAM {
148+ type_ : CK_SP800_108_ITERATION_VARIABLE ,
149+ pValue : counter_format as * const _ as * mut _ ,
150+ ulValueLen : size_of :: < CK_SP800_108_COUNTER_FORMAT > ( ) as CK_ULONG ,
151+ } ,
152+ PrfDataParamType :: Counter ( counter_format) => CK_PRF_DATA_PARAM {
153+ type_ : CK_SP800_108_COUNTER ,
150154 pValue : counter_format as * const _ as * mut _ ,
151- ulValueLen : size_of :: < cryptoki_sys:: CK_SP800_108_COUNTER_FORMAT > ( )
152- as cryptoki_sys:: CK_ULONG ,
155+ ulValueLen : size_of :: < CK_SP800_108_COUNTER_FORMAT > ( ) as CK_ULONG ,
153156 } ,
154- PrfDataParamType :: DkmLength ( dkm_length_format) => cryptoki_sys :: CK_PRF_DATA_PARAM {
155- type_ : cryptoki_sys :: CK_SP800_108_DKM_LENGTH ,
157+ PrfDataParamType :: DkmLength ( dkm_length_format) => CK_PRF_DATA_PARAM {
158+ type_ : CK_SP800_108_DKM_LENGTH ,
156159 pValue : dkm_length_format as * const _ as * mut _ ,
157- ulValueLen : size_of :: < cryptoki_sys:: CK_SP800_108_DKM_LENGTH_FORMAT > ( )
158- as cryptoki_sys:: CK_ULONG ,
160+ ulValueLen : size_of :: < CK_SP800_108_DKM_LENGTH_FORMAT > ( ) as CK_ULONG ,
159161 } ,
160- PrfDataParamType :: ByteArray ( data) => cryptoki_sys :: CK_PRF_DATA_PARAM {
161- type_ : cryptoki_sys :: CK_SP800_108_BYTE_ARRAY ,
162+ PrfDataParamType :: ByteArray ( data) => CK_PRF_DATA_PARAM {
163+ type_ : CK_SP800_108_BYTE_ARRAY ,
162164 pValue : data. as_ptr ( ) as * mut _ ,
163165 ulValueLen : data
164166 . len ( )
@@ -174,8 +176,8 @@ impl<'a> PrfDataParam<'a> {
174176/// Container for information on an additional key to be derived.
175177#[ derive( Debug ) ]
176178pub struct DerivedKey {
177- template : Pin < Box < [ cryptoki_sys :: CK_ATTRIBUTE ] > > ,
178- handle : cryptoki_sys :: CK_OBJECT_HANDLE ,
179+ template : Pin < Box < [ CK_ATTRIBUTE ] > > ,
180+ handle : CK_OBJECT_HANDLE ,
179181}
180182
181183impl DerivedKey {
@@ -185,35 +187,35 @@ impl DerivedKey {
185187 ///
186188 /// * `template` - The template for the key to be derived.
187189 pub fn new ( template : & [ Attribute ] ) -> Self {
188- let template: Box < [ cryptoki_sys :: CK_ATTRIBUTE ] > = template. iter ( ) . map ( Into :: into) . collect ( ) ;
190+ let template: Box < [ CK_ATTRIBUTE ] > = template. iter ( ) . map ( Into :: into) . collect ( ) ;
189191 let template = Pin :: new ( template) ;
190192
191193 Self {
192194 template,
193- handle : cryptoki_sys :: CK_INVALID_HANDLE ,
195+ handle : CK_INVALID_HANDLE ,
194196 }
195197 }
196198
197199 /// Return handle for derived key, if it has been created yet
198200 pub fn handle ( & self ) -> Option < ObjectHandle > {
199- if self . handle == cryptoki_sys :: CK_INVALID_HANDLE {
201+ if self . handle == CK_INVALID_HANDLE {
200202 None
201203 } else {
202204 Some ( ObjectHandle :: new ( self . handle ) )
203205 }
204206 }
205207}
206208
207- impl From < & mut DerivedKey > for cryptoki_sys :: CK_DERIVED_KEY {
209+ impl From < & mut DerivedKey > for CK_DERIVED_KEY {
208210 fn from ( value : & mut DerivedKey ) -> Self {
209- cryptoki_sys :: CK_DERIVED_KEY {
210- pTemplate : value. template . as_ptr ( ) as cryptoki_sys :: CK_ATTRIBUTE_PTR ,
211+ CK_DERIVED_KEY {
212+ pTemplate : value. template . as_ptr ( ) as CK_ATTRIBUTE_PTR ,
211213 ulAttributeCount : value
212214 . template
213215 . len ( )
214216 . try_into ( )
215217 . expect ( "number of attributes in template does not fit in CK_ULONG" ) ,
216- phKey : & mut value. handle as cryptoki_sys :: CK_OBJECT_HANDLE_PTR ,
218+ phKey : & mut value. handle as CK_OBJECT_HANDLE_PTR ,
217219 }
218220 }
219221}
@@ -224,9 +226,9 @@ impl From<&mut DerivedKey> for cryptoki_sys::CK_DERIVED_KEY {
224226#[ derive( Debug ) ]
225227pub struct KbkdfParams < ' a > {
226228 /// Holds own data so that we have a contiguous memory region to give to backend
227- _additional_derived_keys : Option < Pin < Box < [ cryptoki_sys :: CK_DERIVED_KEY ] > > > ,
229+ _additional_derived_keys : Option < Pin < Box < [ CK_DERIVED_KEY ] > > > ,
228230
229- inner : cryptoki_sys :: CK_SP800_108_KDF_PARAMS ,
231+ inner : CK_SP800_108_KDF_PARAMS ,
230232 /// Marker type to ensure we don't outlive the data
231233 _marker : PhantomData < & ' a [ u8 ] > ,
232234}
@@ -251,17 +253,17 @@ impl<'a> KbkdfParams<'a> {
251253 . map ( |keys| {
252254 keys. iter_mut ( )
253255 . map ( Into :: into)
254- . collect :: < Box < [ cryptoki_sys :: CK_DERIVED_KEY ] > > ( )
256+ . collect :: < Box < [ CK_DERIVED_KEY ] > > ( )
255257 } )
256258 . map ( Pin :: new) ;
257259
258- let inner = cryptoki_sys :: CK_SP800_108_KDF_PARAMS {
260+ let inner = CK_SP800_108_KDF_PARAMS {
259261 prfType : prf_mechanism. into ( ) ,
260262 ulNumberOfDataParams : prf_data_params
261263 . len ( )
262264 . try_into ( )
263265 . expect ( "number of PRF data parameters does not fit in CK_ULONG" ) ,
264- pDataParams : prf_data_params. as_ptr ( ) as cryptoki_sys :: CK_PRF_DATA_PARAM_PTR ,
266+ pDataParams : prf_data_params. as_ptr ( ) as CK_PRF_DATA_PARAM_PTR ,
265267 ulAdditionalDerivedKeys : additional_derived_keys. as_ref ( ) . map_or ( 0 , |keys| {
266268 keys. len ( )
267269 . try_into ( )
@@ -270,7 +272,7 @@ impl<'a> KbkdfParams<'a> {
270272 pAdditionalDerivedKeys : additional_derived_keys
271273 . as_mut ( )
272274 . map_or ( ptr:: null_mut ( ) , |keys| {
273- keys. as_mut_ptr ( ) as cryptoki_sys :: CK_DERIVED_KEY_PTR
275+ keys. as_mut_ptr ( ) as CK_DERIVED_KEY_PTR
274276 } ) ,
275277 } ;
276278
@@ -282,7 +284,7 @@ impl<'a> KbkdfParams<'a> {
282284 }
283285 }
284286
285- pub ( crate ) fn inner ( & self ) -> & cryptoki_sys :: CK_SP800_108_KDF_PARAMS {
287+ pub ( crate ) fn inner ( & self ) -> & CK_SP800_108_KDF_PARAMS {
286288 & self . inner
287289 }
288290}
@@ -293,9 +295,9 @@ impl<'a> KbkdfParams<'a> {
293295#[ derive( Debug ) ]
294296pub struct KbkdfFeedbackParams < ' a > {
295297 /// Holds own data so that we have a contiguous memory region to give to backend
296- _additional_derived_keys : Option < Pin < Box < [ cryptoki_sys :: CK_DERIVED_KEY ] > > > ,
298+ _additional_derived_keys : Option < Pin < Box < [ CK_DERIVED_KEY ] > > > ,
297299
298- inner : cryptoki_sys :: CK_SP800_108_FEEDBACK_KDF_PARAMS ,
300+ inner : CK_SP800_108_FEEDBACK_KDF_PARAMS ,
299301 /// Marker type to ensure we don't outlive the data
300302 _marker : PhantomData < & ' a [ u8 ] > ,
301303}
@@ -323,17 +325,17 @@ impl<'a> KbkdfFeedbackParams<'a> {
323325 . map ( |keys| {
324326 keys. iter_mut ( )
325327 . map ( Into :: into)
326- . collect :: < Box < [ cryptoki_sys :: CK_DERIVED_KEY ] > > ( )
328+ . collect :: < Box < [ CK_DERIVED_KEY ] > > ( )
327329 } )
328330 . map ( Pin :: new) ;
329331
330- let inner = cryptoki_sys :: CK_SP800_108_FEEDBACK_KDF_PARAMS {
332+ let inner = CK_SP800_108_FEEDBACK_KDF_PARAMS {
331333 prfType : prf_mechanism. into ( ) ,
332334 ulNumberOfDataParams : prf_data_params
333335 . len ( )
334336 . try_into ( )
335337 . expect ( "number of PRF data parameters does not fit in CK_ULONG" ) ,
336- pDataParams : prf_data_params. as_ptr ( ) as cryptoki_sys :: CK_PRF_DATA_PARAM_PTR ,
338+ pDataParams : prf_data_params. as_ptr ( ) as CK_PRF_DATA_PARAM_PTR ,
337339 ulIVLen : iv. map_or ( 0 , |iv| {
338340 iv. len ( )
339341 . try_into ( )
@@ -348,7 +350,7 @@ impl<'a> KbkdfFeedbackParams<'a> {
348350 pAdditionalDerivedKeys : additional_derived_keys
349351 . as_mut ( )
350352 . map_or ( ptr:: null_mut ( ) , |keys| {
351- keys. as_mut_ptr ( ) as cryptoki_sys :: CK_DERIVED_KEY_PTR
353+ keys. as_mut_ptr ( ) as CK_DERIVED_KEY_PTR
352354 } ) ,
353355 } ;
354356
@@ -360,7 +362,7 @@ impl<'a> KbkdfFeedbackParams<'a> {
360362 }
361363 }
362364
363- pub ( crate ) fn inner ( & self ) -> & cryptoki_sys :: CK_SP800_108_FEEDBACK_KDF_PARAMS {
365+ pub ( crate ) fn inner ( & self ) -> & CK_SP800_108_FEEDBACK_KDF_PARAMS {
364366 & self . inner
365367 }
366368}
0 commit comments