-
Notifications
You must be signed in to change notification settings - Fork 74
Add AES Key Derivation Support (ECB,CBC Encrypt) #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danni
merged 5 commits into
pyauth:master
from
aXedge-arm:aes_key_derivation_extensions
Oct 29, 2020
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5cbf5e3
Add AES Key Derivation Support (ECB,CBC Encrypt)
a88e84e
Add AES Key Derivation Support (ECB,CBC Encrypt)
84d28f2
Add AES Key Derivation Support (ECB,CBC Encrypt)
a53f845
Add AES Key Derivation Support (ECB,CBC Encrypt)
33faa60
Add AES Key Derivation Support (ECB,CBC Encrypt)
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ setuptools_scm | |
# Used for tests | ||
oscrypto | ||
cryptography | ||
parameterized | ||
|
||
sphinx | ||
sphinx-rtd-theme | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
from . import TestCase, requires, FIXME | ||
|
||
from parameterized import parameterized | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing from |
||
|
||
class AESTests(TestCase): | ||
|
||
|
@@ -123,3 +124,211 @@ def test_wrap(self): | |
|
||
self.assertEqual(key[pkcs11.Attribute.VALUE], | ||
key2[pkcs11.Attribute.VALUE]) | ||
|
||
@parameterized.expand([ | ||
("POSITIVE_128_BIT", 128, 16, TestCase.assertIsNotNone), | ||
("POSITIVE_128_BIT_LONG_IV", 128, 32, TestCase.assertIsNotNone), | ||
("NEGATIVE_128_BIT_BAD_IV", 128, 15, TestCase.assertIsNone), | ||
("POSITIVE_256_BIT_LONG_IV", 256, 32, TestCase.assertIsNotNone), | ||
("NEGATIVE_256_BIT_SHORT_IV", 256, 16, TestCase.assertIsNone), | ||
("NEGATIVE_256_BIT_BAD_IV", 256, 31, TestCase.assertIsNone), | ||
]) | ||
@requires(Mechanism.AES_ECB_ENCRYPT_DATA) | ||
@FIXME.opencryptoki # can't set key attributes | ||
def test_derive_using_ecb_encrypt(self, test_type, test_key_length, iv_length, assert_fn): | ||
"""Function to test AES Key Derivation using the ECB_ENCRYPT Mechanism. | ||
|
||
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 | ||
""" | ||
|
||
# Create the Master Key | ||
capabilities = pkcs11.defaults.DEFAULT_KEY_CAPABILITIES[pkcs11.KeyType.AES] | ||
capabilities |= pkcs11.MechanismFlag.DERIVE | ||
key = self.session.generate_key(pkcs11.KeyType.AES, key_length=test_key_length, | ||
capabilities=capabilities, | ||
template={ | ||
pkcs11.Attribute.EXTRACTABLE: True, | ||
pkcs11.Attribute.DERIVE: True, | ||
pkcs11.Attribute.SENSITIVE: False, | ||
}) | ||
|
||
self.assertTrue(key is not None, "Failed to create {}-bit Master Key".format(test_key_length)) | ||
|
||
# Derive a Key from the Master Key | ||
iv = b'0' * iv_length | ||
try: | ||
derived_key = key.derive_key(pkcs11.KeyType.AES, key_length=test_key_length, | ||
capabilities=capabilities, | ||
mechanism=Mechanism.AES_ECB_ENCRYPT_DATA, | ||
mechanism_param=iv, | ||
template={ | ||
pkcs11.Attribute.EXTRACTABLE: True, | ||
pkcs11.Attribute.SENSITIVE: False, | ||
}) | ||
except (pkcs11.exceptions.MechanismParamInvalid, | ||
pkcs11.exceptions.FunctionFailed) as e: | ||
derived_key = None | ||
|
||
assert_fn(self, derived_key, "{}-bit Key Derivation Failure".format(test_key_length)) | ||
|
||
@parameterized.expand([ | ||
("POSITIVE_128_BIT", 128, 16), | ||
("POSITIVE_256_BIT_LONG_IV", 256, 32), | ||
]) | ||
@requires(Mechanism.AES_ECB_ENCRYPT_DATA) | ||
@FIXME.opencryptoki # can't set key attributes | ||
def test_encrypt_with_key_derived_using_ecb_encrypt(self, test_type, test_key_length, iv_length): | ||
"""Function to test Data Encryption/Decryption using a Derived AES Key. | ||
|
||
Function to test Data Encryption/Decryption using an AES Key | ||
Derived by the ECB_ENCRYPT Mechanism. | ||
|
||
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 | ||
""" | ||
|
||
# Create the Master Key | ||
capabilities = pkcs11.defaults.DEFAULT_KEY_CAPABILITIES[pkcs11.KeyType.AES] | ||
capabilities |= pkcs11.MechanismFlag.DERIVE | ||
key = self.session.generate_key(pkcs11.KeyType.AES, key_length=test_key_length, | ||
capabilities=capabilities, | ||
template={ | ||
pkcs11.Attribute.EXTRACTABLE: True, | ||
pkcs11.Attribute.DERIVE: True, | ||
pkcs11.Attribute.SENSITIVE: False, | ||
}) | ||
|
||
self.assertTrue(key is not None, "Failed to create {}-bit Master Key".format(test_key_length)) | ||
|
||
# Derive a Key from the Master Key | ||
iv = b'0' * iv_length | ||
try: | ||
derived_key = key.derive_key(pkcs11.KeyType.AES, key_length=test_key_length, | ||
capabilities=capabilities, | ||
mechanism=Mechanism.AES_ECB_ENCRYPT_DATA, | ||
mechanism_param=iv, | ||
template={ | ||
pkcs11.Attribute.EXTRACTABLE: True, | ||
pkcs11.Attribute.SENSITIVE: False, | ||
}) | ||
except (pkcs11.exceptions.MechanismParamInvalid, | ||
pkcs11.exceptions.FunctionFailed) as e: | ||
derived_key = None | ||
|
||
self.assertTrue(derived_key is not None, "Failed to derive {}-bit Derived Key".format(test_key_length)) | ||
|
||
# Test capability of Key to Encrypt/Decrypt data | ||
data = b'HELLO WORLD' * 1024 | ||
|
||
iv = self.session.generate_random(128) | ||
crypttext = self.key.encrypt(data, mechanism_param=iv) | ||
text = self.key.decrypt(crypttext, mechanism_param=iv) | ||
|
||
self.assertEqual(text, data) | ||
|
||
@parameterized.expand([ | ||
("POSITIVE_128_BIT", 128, 16, 16, TestCase.assertIsNotNone), | ||
("POSITIVE_128_BIT_LONG_DATA", 128, 16, 64, TestCase.assertIsNotNone), | ||
("NEGATIVE_128_BIT_BAD_IV", 128, 15, 16, TestCase.assertIsNone), | ||
("NEGATIVE_128_BIT_BAD_DATA", 128, 16, 31, TestCase.assertIsNone), | ||
("POSITIVE_256_BIT", 256, 16, 32, TestCase.assertIsNotNone), | ||
("POSITIVE_256_BIT_LONG_DATA", 256, 16, 64, TestCase.assertIsNotNone), | ||
("NEGATIVE_256_BIT_BAD_IV", 256, 15, 16, TestCase.assertIsNone), | ||
("NEGATIVE_256_BIT_BAD_DATA", 256, 16, 31, TestCase.assertIsNone), | ||
("NEGATIVE_256_BIT_SHORT_DATA", 256, 16, 16, TestCase.assertIsNone), | ||
]) | ||
@requires(Mechanism.AES_CBC_ENCRYPT_DATA) | ||
@FIXME.opencryptoki # can't set key attributes | ||
def test_derive_using_cbc_encrypt(self, test_type, test_key_length, iv_length, data_length, assert_fn): | ||
"""Function to test AES Key Derivation using the CBC_ENCRYPT Mechanism. | ||
|
||
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 | ||
""" | ||
|
||
# Create the Master Key | ||
capabilities = pkcs11.defaults.DEFAULT_KEY_CAPABILITIES[pkcs11.KeyType.AES] | ||
capabilities |= pkcs11.MechanismFlag.DERIVE | ||
key = self.session.generate_key(pkcs11.KeyType.AES, key_length=test_key_length, | ||
capabilities=capabilities, | ||
template={ | ||
pkcs11.Attribute.EXTRACTABLE: True, | ||
pkcs11.Attribute.DERIVE: True, | ||
pkcs11.Attribute.SENSITIVE: False, | ||
}) | ||
|
||
self.assertTrue(key is not None, "Failed to create {}-bit Master Key".format(test_key_length)) | ||
|
||
# Derive a Key from the Master Key | ||
iv = b'0' * iv_length | ||
data = b'1' * data_length | ||
try: | ||
derived_key = key.derive_key(pkcs11.KeyType.AES, key_length=test_key_length, | ||
capabilities=capabilities, | ||
mechanism=Mechanism.AES_CBC_ENCRYPT_DATA, | ||
mechanism_param=(iv, data), | ||
template={ | ||
pkcs11.Attribute.EXTRACTABLE: True, | ||
pkcs11.Attribute.SENSITIVE: False, | ||
}) | ||
except (pkcs11.exceptions.MechanismParamInvalid, | ||
pkcs11.exceptions.FunctionFailed, | ||
IndexError) as e: | ||
derived_key = None | ||
|
||
assert_fn(self, derived_key, "{}-bit Key Derivation Failure".format(test_key_length)) | ||
|
||
@parameterized.expand([ | ||
("POSITIVE_128_BIT", 128, 16, 16), | ||
("POSITIVE_256_BIT", 256, 16, 32), | ||
("POSITIVE_256_BIT_LONG_DATA", 256, 16, 64), | ||
]) | ||
@requires(Mechanism.AES_CBC_ENCRYPT_DATA) | ||
@FIXME.opencryptoki # can't set key attributes | ||
def test_encrypt_with_key_derived_using_cbc_encrypt(self, test_type, test_key_length, iv_length, data_length): | ||
"""Function to test Data Encryption/Decryption using a Derived AES Key. | ||
|
||
Function to test Data Encryption/Decryption using an AES Key | ||
Derived by the CBC_ENCRYPT Mechanism. | ||
|
||
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 | ||
""" | ||
|
||
# Create the Master Key | ||
capabilities = pkcs11.defaults.DEFAULT_KEY_CAPABILITIES[pkcs11.KeyType.AES] | ||
capabilities |= pkcs11.MechanismFlag.DERIVE | ||
key = self.session.generate_key(pkcs11.KeyType.AES, key_length=test_key_length, | ||
capabilities=capabilities, | ||
template={ | ||
pkcs11.Attribute.EXTRACTABLE: True, | ||
pkcs11.Attribute.DERIVE: True, | ||
pkcs11.Attribute.SENSITIVE: False, | ||
}) | ||
|
||
self.assertTrue(key is not None, "Failed to create {}-bit Master Key".format(test_key_length)) | ||
|
||
# Derive a Key from the Master Key | ||
iv = b'0' * iv_length | ||
data = b'1' * data_length | ||
try: | ||
derived_key = key.derive_key(pkcs11.KeyType.AES, key_length=test_key_length, | ||
capabilities=capabilities, | ||
mechanism=Mechanism.AES_CBC_ENCRYPT_DATA, | ||
mechanism_param=(iv, data), | ||
template={ | ||
pkcs11.Attribute.EXTRACTABLE: True, | ||
pkcs11.Attribute.SENSITIVE: False, | ||
}) | ||
except (pkcs11.exceptions.MechanismParamInvalid, | ||
pkcs11.exceptions.FunctionFailed, | ||
IndexError) as e: | ||
derived_key = None | ||
|
||
self.assertTrue(derived_key is not None, "Failed to derive {}-bit Derived Key".format(test_key_length)) | ||
|
||
# Test capability of Key to Encrypt/Decrypt data | ||
data = b'HELLO WORLD' * 1024 | ||
|
||
iv = self.session.generate_random(128) | ||
crypttext = self.key.encrypt(data, mechanism_param=iv) | ||
text = self.key.decrypt(crypttext, mechanism_param=iv) | ||
|
||
self.assertEqual(text, data) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is getting to be a good indication that the mechanism types above should be implemented as subclasses of MechanismWithParam (which should be renamed),
mechanism_param
keyword should be removed and this class allowed as a parameter tomechanism
.This would allow
mechanism=RSA_PKCS
,mechanism=Mechanism(AES_CBC, param=iv)
ormechanism=mechanisms.RSA_PKCS_OAEP(params=...)
with proper typing and a much better point for implementing extensions beyond this package.That doesn't have to be done now, just a note.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So shall this be added as a feature request, or do you want to take this up now?
If one has been created, I could add its link here in order to complete the tracking of this suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you wanted to give refactoring it a go, I'd be keen. I've opened #98 to track it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi Danni, if it's okay with you, I'll attempt that once these changes have been merged.