From 63e55e3a068b9312d36f05f1e1ced1c45d1b640a Mon Sep 17 00:00:00 2001 From: Tim Kelsey Date: Mon, 29 Jun 2015 15:04:37 +0100 Subject: [PATCH] Adding CryptographicParameters construction to attribute values --- kmip/core/factories/attribute_values.py | 28 +++++++- .../core/factories/test_attribute_values.py | 72 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/kmip/core/factories/attribute_values.py b/kmip/core/factories/attribute_values.py index dbfd9bb..09d8251 100644 --- a/kmip/core/factories/attribute_values.py +++ b/kmip/core/factories/attribute_values.py @@ -20,6 +20,7 @@ from kmip.core.attributes import ContactInformation from kmip.core.attributes import CryptographicAlgorithm from kmip.core.attributes import CryptographicLength from kmip.core.attributes import CryptographicUsageMask +from kmip.core.attributes import CryptographicParameters from kmip.core.attributes import CustomAttribute from kmip.core.attributes import Digest from kmip.core.attributes import Name @@ -27,6 +28,7 @@ from kmip.core.attributes import ObjectGroup from kmip.core.attributes import UniqueIdentifier from kmip.core.attributes import ObjectType from kmip.core.attributes import OperationPolicyName +from kmip.core.attributes import HashingAlgorithm from kmip.core import utils @@ -151,7 +153,31 @@ class AttributeValueFactory(object): return CryptographicLength(length) def _create_cryptographic_parameters(self, params): - raise NotImplementedError() + block_cipher_mode = None + if 'block_cipher_mode' in params: + block_cipher_mode = CryptographicParameters.BlockCipherMode( + params.get('block_cipher_mode')) + + padding_method = None + if 'padding_method' in params: + padding_method = CryptographicParameters.PaddingMethod( + params.get('padding_method')) + + key_role_type = None + if 'key_role_type' in params: + key_role_type = CryptographicParameters.KeyRoleType( + params.get('key_role_type')) + + hashing_algorithm = None + if 'hashing_algorithm' in params: + hashing_algorithm = HashingAlgorithm( + params.get("hashing_algorithm")) + + return CryptographicParameters( + block_cipher_mode=block_cipher_mode, + padding_method=padding_method, + hashing_algorithm=hashing_algorithm, + key_role_type=key_role_type) def _create_cryptographic_domain_parameters(self, params): raise NotImplementedError() diff --git a/kmip/tests/unit/core/factories/test_attribute_values.py b/kmip/tests/unit/core/factories/test_attribute_values.py index 8fd7a74..455c7dd 100644 --- a/kmip/tests/unit/core/factories/test_attribute_values.py +++ b/kmip/tests/unit/core/factories/test_attribute_values.py @@ -16,7 +16,15 @@ from testtools import TestCase from kmip.core.enums import AttributeType +from kmip.core.enums import BlockCipherMode +from kmip.core.enums import HashingAlgorithm +from kmip.core.enums import PaddingMethod +from kmip.core.enums import KeyRoleType + +from kmip.core import attributes +from kmip.core.attributes import CryptographicParameters from kmip.core.attributes import OperationPolicyName + from kmip.core.factories.attribute_values import AttributeValueFactory @@ -60,3 +68,67 @@ class TestAttributeValueFactory(TestCase): def test_create_operation_policy_name_on_none(self): self._test_create_operation_policy_name(None) + + def _test_cryptograpic_parameters(self, obj, block_cipher_mode, + padding_method, key_role_type, + hashing_algorithm): + msg = "expected {0}, received {1}" + self.assertIsInstance(obj, CryptographicParameters, msg.format( + CryptographicParameters, obj.__class__)) + + self.assertEqual(block_cipher_mode, obj.block_cipher_mode, msg.format( + block_cipher_mode, obj.block_cipher_mode)) + + self.assertEqual(padding_method, obj.padding_method, msg.format( + padding_method, obj.padding_method)) + + self.assertEqual(key_role_type, obj.key_role_type, msg.format( + key_role_type, obj.hashing_algorithm)) + + self.assertEqual(hashing_algorithm, obj.hashing_algorithm, msg.format( + hashing_algorithm, obj.hashing_algorithm)) + + def test_create_cryptograpic_parameters_none(self): + cp = self.factory.create_attribute_value( + AttributeType.CRYPTOGRAPHIC_PARAMETERS, + {}) + self._test_cryptograpic_parameters(cp, None, None, None, None) + + def test_create_cryptograpic_parameters_block_cipher_mode(self): + cp = self.factory.create_attribute_value( + AttributeType.CRYPTOGRAPHIC_PARAMETERS, + {'block_cipher_mode': BlockCipherMode.NIST_KEY_WRAP}) + + self._test_cryptograpic_parameters( + cp, CryptographicParameters.BlockCipherMode( + BlockCipherMode.NIST_KEY_WRAP), + None, None, None) + + def test_create_cryptograpic_parameters_padding_method(self): + cp = self.factory.create_attribute_value( + AttributeType.CRYPTOGRAPHIC_PARAMETERS, + {'padding_method': PaddingMethod.ANSI_X9_23}) + + # noqa - E128 continuation line under-indented for visual indent + self._test_cryptograpic_parameters(cp, None, + CryptographicParameters.PaddingMethod(PaddingMethod.ANSI_X9_23), + None, None) # noqa + + def test_create_cryptograpic_parameters_key_role_type(self): + cp = self.factory.create_attribute_value( + AttributeType.CRYPTOGRAPHIC_PARAMETERS, + {'key_role_type': KeyRoleType.KEK}) + + # noqa - E128 continuation line under-indented for visual indent + self._test_cryptograpic_parameters(cp, None, None, + CryptographicParameters.KeyRoleType(KeyRoleType.KEK), + None) # noqa + + def test_create_cryptograpic_parameters_hashing_algorithm(self): + cp = self.factory.create_attribute_value( + AttributeType.CRYPTOGRAPHIC_PARAMETERS, + {'hashing_algorithm': HashingAlgorithm.SHA_512}) + + # noqa - E128 continuation line under-indented for visual indent + self._test_cryptograpic_parameters(cp, None, None, None, + attributes.HashingAlgorithm(HashingAlgorithm.SHA_512)) # noqa