Skip to content

Commit 33faa60

Browse files
author
Akshitij Malik
committed
Add AES Key Derivation Support (ECB,CBC Encrypt)
Additional Information: 1. Added AES Key Derivation mechanisms described in PKCS11 v2.4.0 Section 2.15: - CKM_AES_ECB_ENCRYPT_DATA - CKM_AES_CBC_ENCRYPT_DATA 2. Incorporated code-review comments: - directly sliced the IV data to extract mechanism_params for CBC_ENCRYPT, - added Unit Tests for ECB_ENCRYPT - test_derive_ecb_encrypt - added Unit Tests for CBC_ENCRYPT - test_derive_cbc_encrypt - updated dev-requirements - split the Unit Tests into 2 phases: - key-derivation tests - data encrypyion/decryption tests - replaced f-strings with python3.5 compatible format strings - negative test rework: - prevent branching by passing assertIsNotNone and assertIsNone as assertion functions for Positive & Negative test cases. Sanity Testing: 1. Build Validation: python setup.py build_ext --inplace 2. AES Testing: export PKCS11_MODULE=XXX export PKCS11_TOKEN_LABEL=XXX export PKCS11_TOKEN_PIN=XXX export PKCS11_TOKEN_SO_PIN=XXX pytest ./tests/test_aes.py Signed-off-by: Akshitij Malik
1 parent a53f845 commit 33faa60

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

tests/test_aes.py

+19-25
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@ def test_wrap(self):
126126
key2[pkcs11.Attribute.VALUE])
127127

128128
@parameterized.expand([
129-
("POSITIVE_128_BIT", 128, 16),
130-
("POSITIVE_128_BIT_LONG_IV", 128, 32),
131-
("NEGATIVE_128_BIT_BAD_IV", 128, 15),
132-
("POSITIVE_256_BIT_LONG_IV", 256, 32),
133-
("NEGATIVE_256_BIT_SHORT_IV", 256, 16),
134-
("NEGATIVE_256_BIT_BAD_IV", 256, 31),
129+
("POSITIVE_128_BIT", 128, 16, TestCase.assertIsNotNone),
130+
("POSITIVE_128_BIT_LONG_IV", 128, 32, TestCase.assertIsNotNone),
131+
("NEGATIVE_128_BIT_BAD_IV", 128, 15, TestCase.assertIsNone),
132+
("POSITIVE_256_BIT_LONG_IV", 256, 32, TestCase.assertIsNotNone),
133+
("NEGATIVE_256_BIT_SHORT_IV", 256, 16, TestCase.assertIsNone),
134+
("NEGATIVE_256_BIT_BAD_IV", 256, 31, TestCase.assertIsNone),
135135
])
136136
@requires(Mechanism.AES_ECB_ENCRYPT_DATA)
137137
@FIXME.opencryptoki # can't set key attributes
138-
def test_derive_using_ecb_encrypt(self, test_type, test_key_length, iv_length):
138+
def test_derive_using_ecb_encrypt(self, test_type, test_key_length, iv_length, assert_fn):
139139
"""Function to test AES Key Derivation using the ECB_ENCRYPT Mechanism.
140140
141141
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
@@ -169,10 +169,7 @@ def test_derive_using_ecb_encrypt(self, test_type, test_key_length, iv_length):
169169
pkcs11.exceptions.FunctionFailed) as e:
170170
derived_key = None
171171

172-
if test_type.startswith("NEGATIVE"):
173-
self.assertTrue(derived_key is None, "Unexpected {}-bit Derived Key".format(test_key_length))
174-
else:
175-
self.assertTrue(derived_key is not None, "Failed to derive {}-bit Derived Key".format(test_key_length))
172+
assert_fn(self, derived_key, "{}-bit Key Derivation Failure".format(test_key_length))
176173

177174
@parameterized.expand([
178175
("POSITIVE_128_BIT", 128, 16),
@@ -229,19 +226,19 @@ def test_encrypt_with_key_derived_using_ecb_encrypt(self, test_type, test_key_le
229226
self.assertEqual(text, data)
230227

231228
@parameterized.expand([
232-
("POSITIVE_128_BIT", 128, 16, 16),
233-
("POSITIVE_128_BIT_LONG_DATA", 128, 16, 64),
234-
("NEGATIVE_128_BIT_BAD_IV", 128, 15, 16),
235-
("NEGATIVE_128_BIT_BAD_DATA", 128, 16, 31),
236-
("POSITIVE_256_BIT", 256, 16, 32),
237-
("POSITIVE_256_BIT_LONG_DATA", 256, 16, 64),
238-
("NEGATIVE_256_BIT_BAD_IV", 256, 15, 16),
239-
("NEGATIVE_256_BIT_BAD_DATA", 256, 16, 31),
240-
("NEGATIVE_256_BIT_SHORT_DATA", 256, 16, 16),
229+
("POSITIVE_128_BIT", 128, 16, 16, TestCase.assertIsNotNone),
230+
("POSITIVE_128_BIT_LONG_DATA", 128, 16, 64, TestCase.assertIsNotNone),
231+
("NEGATIVE_128_BIT_BAD_IV", 128, 15, 16, TestCase.assertIsNone),
232+
("NEGATIVE_128_BIT_BAD_DATA", 128, 16, 31, TestCase.assertIsNone),
233+
("POSITIVE_256_BIT", 256, 16, 32, TestCase.assertIsNotNone),
234+
("POSITIVE_256_BIT_LONG_DATA", 256, 16, 64, TestCase.assertIsNotNone),
235+
("NEGATIVE_256_BIT_BAD_IV", 256, 15, 16, TestCase.assertIsNone),
236+
("NEGATIVE_256_BIT_BAD_DATA", 256, 16, 31, TestCase.assertIsNone),
237+
("NEGATIVE_256_BIT_SHORT_DATA", 256, 16, 16, TestCase.assertIsNone),
241238
])
242239
@requires(Mechanism.AES_CBC_ENCRYPT_DATA)
243240
@FIXME.opencryptoki # can't set key attributes
244-
def test_derive_using_cbc_encrypt(self, test_type, test_key_length, iv_length, data_length):
241+
def test_derive_using_cbc_encrypt(self, test_type, test_key_length, iv_length, data_length, assert_fn):
245242
"""Function to test AES Key Derivation using the CBC_ENCRYPT Mechanism.
246243
247244
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
@@ -277,10 +274,7 @@ def test_derive_using_cbc_encrypt(self, test_type, test_key_length, iv_length, d
277274
IndexError) as e:
278275
derived_key = None
279276

280-
if test_type.startswith("NEGATIVE"):
281-
self.assertTrue(derived_key is None, "Unexpected {}-bit Derived Key".format(test_key_length))
282-
else:
283-
self.assertTrue(derived_key is not None, "Failed to derive {}-bit Derived Key".format(test_key_length))
277+
assert_fn(self, derived_key, "{}-bit Key Derivation Failure".format(test_key_length))
284278

285279
@parameterized.expand([
286280
("POSITIVE_128_BIT", 128, 16, 16),

0 commit comments

Comments
 (0)