@@ -135,9 +135,8 @@ def test_wrap(self):
135
135
])
136
136
@requires (Mechanism .AES_ECB_ENCRYPT_DATA )
137
137
@FIXME .opencryptoki # can't set key attributes
138
- def test_derive_ecb_encrypt (self , test_type , test_key_length , iv_length ):
139
- """
140
- Function to test AES Key Derivation using the ECB_ENCRYPT Mechanism.
138
+ def test_derive_using_ecb_encrypt (self , test_type , test_key_length , iv_length ):
139
+ """Function to test AES Key Derivation using the ECB_ENCRYPT Mechanism.
141
140
142
141
Refer to Section 2.15 of http://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/errata01/os/pkcs11-curr-v2.40-errata01-os-complete.html#_Toc441850521
143
142
"""
@@ -175,14 +174,59 @@ def test_derive_ecb_encrypt(self, test_type, test_key_length, iv_length):
175
174
else :
176
175
self .assertTrue (derived_key is not None , f"Failed to derive { test_key_length } -bit Derived Key" )
177
176
178
- # Test capability of Key to Encrypt/Decrypt data
179
- data = b'HELLO WORLD' * 1024
177
+ @parameterized .expand ([
178
+ ("POSITIVE_128_BIT" , 128 , 16 ),
179
+ ("POSITIVE_256_BIT_LONG_IV" , 256 , 32 ),
180
+ ])
181
+ @requires (Mechanism .AES_ECB_ENCRYPT_DATA )
182
+ @FIXME .opencryptoki # can't set key attributes
183
+ def test_encrypt_with_key_derived_using_ecb_encrypt (self , test_type , test_key_length , iv_length ):
184
+ """Function to test Data Encryption/Decryption using a Derived AES Key.
185
+
186
+ Function to test Data Encryption/Decryption using an AES Key
187
+ Derived by the ECB_ENCRYPT Mechanism.
188
+
189
+ Refer to Section 2.15 of http://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/errata01/os/pkcs11-curr-v2.40-errata01-os-complete.html#_Toc441850521
190
+ """
180
191
181
- iv = self .session .generate_random (128 )
182
- crypttext = self .key .encrypt (data , mechanism_param = iv )
183
- text = self .key .decrypt (crypttext , mechanism_param = iv )
192
+ # Create the Master Key
193
+ capabilities = pkcs11 .defaults .DEFAULT_KEY_CAPABILITIES [pkcs11 .KeyType .AES ]
194
+ capabilities |= pkcs11 .MechanismFlag .DERIVE
195
+ key = self .session .generate_key (pkcs11 .KeyType .AES , key_length = test_key_length ,
196
+ capabilities = capabilities ,
197
+ template = {
198
+ pkcs11 .Attribute .EXTRACTABLE : True ,
199
+ pkcs11 .Attribute .DERIVE : True ,
200
+ pkcs11 .Attribute .SENSITIVE : False ,
201
+ })
184
202
185
- self .assertEqual (text , data )
203
+ self .assertTrue (key is not None , f"Failed to create { test_key_length } -bit Master Key" )
204
+
205
+ # Derive a Key from the Master Key
206
+ iv = b'0' * iv_length
207
+ try :
208
+ derived_key = key .derive_key (pkcs11 .KeyType .AES , key_length = test_key_length ,
209
+ capabilities = capabilities ,
210
+ mechanism = Mechanism .AES_ECB_ENCRYPT_DATA ,
211
+ mechanism_param = iv ,
212
+ template = {
213
+ pkcs11 .Attribute .EXTRACTABLE : True ,
214
+ pkcs11 .Attribute .SENSITIVE : False ,
215
+ })
216
+ except (pkcs11 .exceptions .MechanismParamInvalid ,
217
+ pkcs11 .exceptions .FunctionFailed ) as e :
218
+ derived_key = None
219
+
220
+ self .assertTrue (derived_key is not None , f"Failed to derive { test_key_length } -bit Derived Key" )
221
+
222
+ # Test capability of Key to Encrypt/Decrypt data
223
+ data = b'HELLO WORLD' * 1024
224
+
225
+ iv = self .session .generate_random (128 )
226
+ crypttext = self .key .encrypt (data , mechanism_param = iv )
227
+ text = self .key .decrypt (crypttext , mechanism_param = iv )
228
+
229
+ self .assertEqual (text , data )
186
230
187
231
@parameterized .expand ([
188
232
("POSITIVE_128_BIT" , 128 , 16 , 16 ),
@@ -197,9 +241,8 @@ def test_derive_ecb_encrypt(self, test_type, test_key_length, iv_length):
197
241
])
198
242
@requires (Mechanism .AES_CBC_ENCRYPT_DATA )
199
243
@FIXME .opencryptoki # can't set key attributes
200
- def test_derive_cbc_encrypt (self , test_type , test_key_length , iv_length , data_length ):
201
- """
202
- Function to test AES Key Derivation using the CBC_ENCRYPT Mechanism.
244
+ def test_derive_using_cbc_encrypt (self , test_type , test_key_length , iv_length , data_length ):
245
+ """Function to test AES Key Derivation using the CBC_ENCRYPT Mechanism.
203
246
204
247
Refer to Section 2.15 of http://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/errata01/os/pkcs11-curr-v2.40-errata01-os-complete.html#_Toc441850521
205
248
"""
@@ -239,12 +282,59 @@ def test_derive_cbc_encrypt(self, test_type, test_key_length, iv_length, data_le
239
282
else :
240
283
self .assertTrue (derived_key is not None , f"Failed to derive { test_key_length } -bit Derived Key" )
241
284
242
- # Test capability of Key to Encrypt/Decrypt data
243
- data = b'HELLO WORLD' * 1024
285
+ @parameterized .expand ([
286
+ ("POSITIVE_128_BIT" , 128 , 16 , 16 ),
287
+ ("POSITIVE_256_BIT" , 256 , 16 , 32 ),
288
+ ("POSITIVE_256_BIT_LONG_DATA" , 256 , 16 , 64 ),
289
+ ])
290
+ @requires (Mechanism .AES_CBC_ENCRYPT_DATA )
291
+ @FIXME .opencryptoki # can't set key attributes
292
+ def test_encrypt_with_key_derived_using_cbc_encrypt (self , test_type , test_key_length , iv_length , data_length ):
293
+ """Function to test Data Encryption/Decryption using a Derived AES Key.
294
+
295
+ Function to test Data Encryption/Decryption using an AES Key
296
+ Derived by the CBC_ENCRYPT Mechanism.
297
+
298
+ Refer to Section 2.15 of http://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/errata01/os/pkcs11-curr-v2.40-errata01-os-complete.html#_Toc441850521
299
+ """
244
300
245
- iv = self .session .generate_random (128 )
246
- crypttext = self .key .encrypt (data , mechanism_param = iv )
247
- text = self .key .decrypt (crypttext , mechanism_param = iv )
301
+ # Create the Master Key
302
+ capabilities = pkcs11 .defaults .DEFAULT_KEY_CAPABILITIES [pkcs11 .KeyType .AES ]
303
+ capabilities |= pkcs11 .MechanismFlag .DERIVE
304
+ key = self .session .generate_key (pkcs11 .KeyType .AES , key_length = test_key_length ,
305
+ capabilities = capabilities ,
306
+ template = {
307
+ pkcs11 .Attribute .EXTRACTABLE : True ,
308
+ pkcs11 .Attribute .DERIVE : True ,
309
+ pkcs11 .Attribute .SENSITIVE : False ,
310
+ })
248
311
249
- self .assertEqual ( text , data )
312
+ self .assertTrue ( key is not None , f"Failed to create { test_key_length } -bit Master Key" )
250
313
314
+ # Derive a Key from the Master Key
315
+ iv = b'0' * iv_length
316
+ data = b'1' * data_length
317
+ try :
318
+ derived_key = key .derive_key (pkcs11 .KeyType .AES , key_length = test_key_length ,
319
+ capabilities = capabilities ,
320
+ mechanism = Mechanism .AES_CBC_ENCRYPT_DATA ,
321
+ mechanism_param = (iv , data ),
322
+ template = {
323
+ pkcs11 .Attribute .EXTRACTABLE : True ,
324
+ pkcs11 .Attribute .SENSITIVE : False ,
325
+ })
326
+ except (pkcs11 .exceptions .MechanismParamInvalid ,
327
+ pkcs11 .exceptions .FunctionFailed ,
328
+ IndexError ) as e :
329
+ derived_key = None
330
+
331
+ self .assertTrue (derived_key is not None , f"Failed to derive { test_key_length } -bit Derived Key" )
332
+
333
+ # Test capability of Key to Encrypt/Decrypt data
334
+ data = b'HELLO WORLD' * 1024
335
+
336
+ iv = self .session .generate_random (128 )
337
+ crypttext = self .key .encrypt (data , mechanism_param = iv )
338
+ text = self .key .decrypt (crypttext , mechanism_param = iv )
339
+
340
+ self .assertEqual (text , data )
0 commit comments