5
5
6
6
use core:: { convert:: TryInto , marker:: PhantomData , pin:: Pin , ptr} ;
7
7
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
+
8
17
use crate :: object:: { Attribute , ObjectHandle } ;
9
18
10
19
use super :: MechanismType ;
@@ -23,7 +32,7 @@ pub enum Endianness {
23
32
/// This structure wraps a `CK_SP800_108_COUNTER_FORMAT` structure.
24
33
#[ derive( Debug , Clone , Copy ) ]
25
34
#[ repr( transparent) ]
26
- pub struct KbkdfCounterFormat ( cryptoki_sys :: CK_SP800_108_COUNTER_FORMAT ) ;
35
+ pub struct KbkdfCounterFormat ( CK_SP800_108_COUNTER_FORMAT ) ;
27
36
28
37
impl KbkdfCounterFormat {
29
38
/// Construct encoding format for KDF's internal counter variable.
@@ -34,7 +43,7 @@ impl KbkdfCounterFormat {
34
43
///
35
44
/// * `width_in_bits` - The number of bits used to represent the counter value.
36
45
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 {
38
47
bLittleEndian : ( endianness == Endianness :: Little ) . into ( ) ,
39
48
ulWidthInBits : width_in_bits
40
49
. try_into ( )
@@ -59,7 +68,7 @@ pub enum DkmLengthMethod {
59
68
/// This structure wraps a `CK_SP800_108_DKM_LENGTH_FORMAT` structure.
60
69
#[ derive( Debug , Clone , Copy ) ]
61
70
#[ repr( transparent) ]
62
- pub struct KbkdfDkmLengthFormat ( cryptoki_sys :: CK_SP800_108_DKM_LENGTH_FORMAT ) ;
71
+ pub struct KbkdfDkmLengthFormat ( CK_SP800_108_DKM_LENGTH_FORMAT ) ;
63
72
64
73
impl KbkdfDkmLengthFormat {
65
74
/// Construct encoding format for length value of DKM (derived key material) from KDF.
@@ -76,17 +85,15 @@ impl KbkdfDkmLengthFormat {
76
85
endianness : Endianness ,
77
86
width_in_bits : usize ,
78
87
) -> Self {
79
- Self ( cryptoki_sys :: CK_SP800_108_DKM_LENGTH_FORMAT {
88
+ Self ( CK_SP800_108_DKM_LENGTH_FORMAT {
80
89
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 ,
85
92
} ,
86
93
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" ) ,
90
97
} )
91
98
}
92
99
}
@@ -118,7 +125,7 @@ pub enum PrfDataParamType<'a> {
118
125
#[ derive( Debug , Clone , Copy ) ]
119
126
#[ repr( transparent) ]
120
127
pub struct PrfDataParam < ' a > {
121
- inner : cryptoki_sys :: CK_PRF_DATA_PARAM ,
128
+ inner : CK_PRF_DATA_PARAM ,
122
129
/// Marker type to ensure we don't outlive the data
123
130
_marker : PhantomData < & ' a [ u8 ] > ,
124
131
}
@@ -132,33 +139,28 @@ impl<'a> PrfDataParam<'a> {
132
139
pub fn new ( type_ : PrfDataParamType < ' a > ) -> Self {
133
140
Self {
134
141
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 ,
137
144
pValue : ptr:: null_mut ( ) ,
138
145
ulValueLen : 0 ,
139
146
} ,
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 ,
150
154
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 ,
153
156
} ,
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 ,
156
159
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 ,
159
161
} ,
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 ,
162
164
pValue : data. as_ptr ( ) as * mut _ ,
163
165
ulValueLen : data
164
166
. len ( )
@@ -174,8 +176,8 @@ impl<'a> PrfDataParam<'a> {
174
176
/// Container for information on an additional key to be derived.
175
177
#[ derive( Debug ) ]
176
178
pub 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 ,
179
181
}
180
182
181
183
impl DerivedKey {
@@ -185,35 +187,35 @@ impl DerivedKey {
185
187
///
186
188
/// * `template` - The template for the key to be derived.
187
189
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 ( ) ;
189
191
let template = Pin :: new ( template) ;
190
192
191
193
Self {
192
194
template,
193
- handle : cryptoki_sys :: CK_INVALID_HANDLE ,
195
+ handle : CK_INVALID_HANDLE ,
194
196
}
195
197
}
196
198
197
199
/// Return handle for derived key, if it has been created yet
198
200
pub fn handle ( & self ) -> Option < ObjectHandle > {
199
- if self . handle == cryptoki_sys :: CK_INVALID_HANDLE {
201
+ if self . handle == CK_INVALID_HANDLE {
200
202
None
201
203
} else {
202
204
Some ( ObjectHandle :: new ( self . handle ) )
203
205
}
204
206
}
205
207
}
206
208
207
- impl From < & mut DerivedKey > for cryptoki_sys :: CK_DERIVED_KEY {
209
+ impl From < & mut DerivedKey > for CK_DERIVED_KEY {
208
210
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 ,
211
213
ulAttributeCount : value
212
214
. template
213
215
. len ( )
214
216
. try_into ( )
215
217
. 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 ,
217
219
}
218
220
}
219
221
}
@@ -224,9 +226,9 @@ impl From<&mut DerivedKey> for cryptoki_sys::CK_DERIVED_KEY {
224
226
#[ derive( Debug ) ]
225
227
pub struct KbkdfParams < ' a > {
226
228
/// 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 ] > > > ,
228
230
229
- inner : cryptoki_sys :: CK_SP800_108_KDF_PARAMS ,
231
+ inner : CK_SP800_108_KDF_PARAMS ,
230
232
/// Marker type to ensure we don't outlive the data
231
233
_marker : PhantomData < & ' a [ u8 ] > ,
232
234
}
@@ -251,17 +253,17 @@ impl<'a> KbkdfParams<'a> {
251
253
. map ( |keys| {
252
254
keys. iter_mut ( )
253
255
. map ( Into :: into)
254
- . collect :: < Box < [ cryptoki_sys :: CK_DERIVED_KEY ] > > ( )
256
+ . collect :: < Box < [ CK_DERIVED_KEY ] > > ( )
255
257
} )
256
258
. map ( Pin :: new) ;
257
259
258
- let inner = cryptoki_sys :: CK_SP800_108_KDF_PARAMS {
260
+ let inner = CK_SP800_108_KDF_PARAMS {
259
261
prfType : prf_mechanism. into ( ) ,
260
262
ulNumberOfDataParams : prf_data_params
261
263
. len ( )
262
264
. try_into ( )
263
265
. 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 ,
265
267
ulAdditionalDerivedKeys : additional_derived_keys. as_ref ( ) . map_or ( 0 , |keys| {
266
268
keys. len ( )
267
269
. try_into ( )
@@ -270,7 +272,7 @@ impl<'a> KbkdfParams<'a> {
270
272
pAdditionalDerivedKeys : additional_derived_keys
271
273
. as_mut ( )
272
274
. 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
274
276
} ) ,
275
277
} ;
276
278
@@ -282,7 +284,7 @@ impl<'a> KbkdfParams<'a> {
282
284
}
283
285
}
284
286
285
- pub ( crate ) fn inner ( & self ) -> & cryptoki_sys :: CK_SP800_108_KDF_PARAMS {
287
+ pub ( crate ) fn inner ( & self ) -> & CK_SP800_108_KDF_PARAMS {
286
288
& self . inner
287
289
}
288
290
}
@@ -293,9 +295,9 @@ impl<'a> KbkdfParams<'a> {
293
295
#[ derive( Debug ) ]
294
296
pub struct KbkdfFeedbackParams < ' a > {
295
297
/// 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 ] > > > ,
297
299
298
- inner : cryptoki_sys :: CK_SP800_108_FEEDBACK_KDF_PARAMS ,
300
+ inner : CK_SP800_108_FEEDBACK_KDF_PARAMS ,
299
301
/// Marker type to ensure we don't outlive the data
300
302
_marker : PhantomData < & ' a [ u8 ] > ,
301
303
}
@@ -323,17 +325,17 @@ impl<'a> KbkdfFeedbackParams<'a> {
323
325
. map ( |keys| {
324
326
keys. iter_mut ( )
325
327
. map ( Into :: into)
326
- . collect :: < Box < [ cryptoki_sys :: CK_DERIVED_KEY ] > > ( )
328
+ . collect :: < Box < [ CK_DERIVED_KEY ] > > ( )
327
329
} )
328
330
. map ( Pin :: new) ;
329
331
330
- let inner = cryptoki_sys :: CK_SP800_108_FEEDBACK_KDF_PARAMS {
332
+ let inner = CK_SP800_108_FEEDBACK_KDF_PARAMS {
331
333
prfType : prf_mechanism. into ( ) ,
332
334
ulNumberOfDataParams : prf_data_params
333
335
. len ( )
334
336
. try_into ( )
335
337
. 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 ,
337
339
ulIVLen : iv. map_or ( 0 , |iv| {
338
340
iv. len ( )
339
341
. try_into ( )
@@ -348,7 +350,7 @@ impl<'a> KbkdfFeedbackParams<'a> {
348
350
pAdditionalDerivedKeys : additional_derived_keys
349
351
. as_mut ( )
350
352
. 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
352
354
} ) ,
353
355
} ;
354
356
@@ -360,7 +362,7 @@ impl<'a> KbkdfFeedbackParams<'a> {
360
362
}
361
363
}
362
364
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 {
364
366
& self . inner
365
367
}
366
368
}
0 commit comments