From ca8e1615068efba728c2e8faf16f04ed0d1f6e29 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 16 Mar 2015 20:57:09 -0500 Subject: AES keywrap support --- tests/hazmat/primitives/test_keywrap.py | 112 ++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 tests/hazmat/primitives/test_keywrap.py (limited to 'tests') diff --git a/tests/hazmat/primitives/test_keywrap.py b/tests/hazmat/primitives/test_keywrap.py new file mode 100644 index 00000000..f49cdade --- /dev/null +++ b/tests/hazmat/primitives/test_keywrap.py @@ -0,0 +1,112 @@ +# This file is dual licensed under the terms of the Apache License, Version +# 2.0, and the BSD License. See the LICENSE file in the root of this repository +# for complete details. + +from __future__ import absolute_import, division, print_function + +import binascii +import os + +import pytest + +from cryptography.hazmat.backends.interfaces import CipherBackend +from cryptography.hazmat.primitives import keywrap +from cryptography.hazmat.primitives.ciphers import algorithms, modes + +from .utils import _load_all_params +from ...utils import load_nist_vectors + + +@pytest.mark.requires_backend_interface(interface=CipherBackend) +class TestAESKeyWrap(object): + @pytest.mark.parametrize( + "params", + _load_all_params( + os.path.join("keywrap", "kwtestvectors"), + ["KW_AE_128.txt", "KW_AE_192.txt", "KW_AE_256.txt"], + load_nist_vectors + ) + ) + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 16), modes.ECB() + ), + skip_message="Does not support AES key wrap (RFC 3394)", + ) + def test_wrap(self, backend, params): + wrapping_key = binascii.unhexlify(params["k"]) + key_to_wrap = binascii.unhexlify(params["p"]) + wrapped_key = keywrap.aes_key_wrap(wrapping_key, key_to_wrap, backend) + assert params["c"] == binascii.hexlify(wrapped_key) + + @pytest.mark.parametrize( + "params", + _load_all_params( + os.path.join("keywrap", "kwtestvectors"), + ["KW_AD_128.txt", "KW_AD_192.txt", "KW_AD_256.txt"], + load_nist_vectors + ) + ) + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 16), modes.ECB() + ), + skip_message="Does not support AES key wrap (RFC 3394)", + ) + def test_unwrap(self, backend, params): + wrapping_key = binascii.unhexlify(params["k"]) + wrapped_key = binascii.unhexlify(params["c"]) + if params.get("fail") is True: + with pytest.raises(keywrap.InvalidUnwrap): + keywrap.aes_key_unwrap(wrapping_key, wrapped_key, backend) + else: + unwrapped_key = keywrap.aes_key_unwrap( + wrapping_key, wrapped_key, backend + ) + assert params["p"] == binascii.hexlify(unwrapped_key) + + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 16), modes.ECB() + ), + skip_message="Does not support AES key wrap (RFC 3394)", + ) + def test_wrap_invalid_key_length(self, backend): + with pytest.raises(ValueError): + keywrap.aes_key_wrap(b"badkey", b"sixteen_byte_key", backend) + + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 16), modes.ECB() + ), + skip_message="Does not support AES key wrap (RFC 3394)", + ) + def test_unwrap_invalid_key_length(self, backend): + with pytest.raises(ValueError): + keywrap.aes_key_unwrap(b"badkey", b"\x00" * 24, backend) + + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 16), modes.ECB() + ), + skip_message="Does not support AES key wrap (RFC 3394)", + ) + def test_wrap_invalid_key_to_wrap_length(self, backend): + with pytest.raises(ValueError): + keywrap.aes_key_wrap(b"sixteen_byte_key", b"\x00" * 15, backend) + + with pytest.raises(ValueError): + keywrap.aes_key_wrap(b"sixteen_byte_key", b"\x00" * 23, backend) + + @pytest.mark.supported( + only_if=lambda backend: backend.cipher_supported( + algorithms.AES("\x00" * 16), modes.ECB() + ), + skip_message="Does not support AES key wrap (RFC 3394)", + ) + def test_unwrap_invalid_wrapped_key_length(self, backend): + with pytest.raises(ValueError): + keywrap.aes_key_unwrap(b"sixteen_byte_key", b"\x00" * 16, backend) + + with pytest.raises(ValueError): + keywrap.aes_key_unwrap(b"sixteen_byte_key", b"\x00" * 27, backend) -- cgit v1.2.3 From cee3736564033cce48f39ab5653f3ba323da0e10 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 17 Oct 2015 09:40:05 -0500 Subject: make skip message more verbose --- tests/hazmat/primitives/test_keywrap.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/primitives/test_keywrap.py b/tests/hazmat/primitives/test_keywrap.py index f49cdade..b1cf5ed8 100644 --- a/tests/hazmat/primitives/test_keywrap.py +++ b/tests/hazmat/primitives/test_keywrap.py @@ -31,7 +31,8 @@ class TestAESKeyWrap(object): only_if=lambda backend: backend.cipher_supported( algorithms.AES("\x00" * 16), modes.ECB() ), - skip_message="Does not support AES key wrap (RFC 3394)", + skip_message="Does not support AES key wrap (RFC 3394) because AES-ECB" + " is unsupported", ) def test_wrap(self, backend, params): wrapping_key = binascii.unhexlify(params["k"]) @@ -51,7 +52,8 @@ class TestAESKeyWrap(object): only_if=lambda backend: backend.cipher_supported( algorithms.AES("\x00" * 16), modes.ECB() ), - skip_message="Does not support AES key wrap (RFC 3394)", + skip_message="Does not support AES key wrap (RFC 3394) because AES-ECB" + " is unsupported", ) def test_unwrap(self, backend, params): wrapping_key = binascii.unhexlify(params["k"]) @@ -69,7 +71,8 @@ class TestAESKeyWrap(object): only_if=lambda backend: backend.cipher_supported( algorithms.AES("\x00" * 16), modes.ECB() ), - skip_message="Does not support AES key wrap (RFC 3394)", + skip_message="Does not support AES key wrap (RFC 3394) because AES-ECB" + " is unsupported", ) def test_wrap_invalid_key_length(self, backend): with pytest.raises(ValueError): @@ -79,7 +82,8 @@ class TestAESKeyWrap(object): only_if=lambda backend: backend.cipher_supported( algorithms.AES("\x00" * 16), modes.ECB() ), - skip_message="Does not support AES key wrap (RFC 3394)", + skip_message="Does not support AES key wrap (RFC 3394) because AES-ECB" + " is unsupported", ) def test_unwrap_invalid_key_length(self, backend): with pytest.raises(ValueError): @@ -89,7 +93,8 @@ class TestAESKeyWrap(object): only_if=lambda backend: backend.cipher_supported( algorithms.AES("\x00" * 16), modes.ECB() ), - skip_message="Does not support AES key wrap (RFC 3394)", + skip_message="Does not support AES key wrap (RFC 3394) because AES-ECB" + " is unsupported", ) def test_wrap_invalid_key_to_wrap_length(self, backend): with pytest.raises(ValueError): @@ -98,12 +103,6 @@ class TestAESKeyWrap(object): with pytest.raises(ValueError): keywrap.aes_key_wrap(b"sixteen_byte_key", b"\x00" * 23, backend) - @pytest.mark.supported( - only_if=lambda backend: backend.cipher_supported( - algorithms.AES("\x00" * 16), modes.ECB() - ), - skip_message="Does not support AES key wrap (RFC 3394)", - ) def test_unwrap_invalid_wrapped_key_length(self, backend): with pytest.raises(ValueError): keywrap.aes_key_unwrap(b"sixteen_byte_key", b"\x00" * 16, backend) -- cgit v1.2.3 From 68bab79cda9ce48d85e2611e9ada9d5f7f44321c Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Thu, 22 Oct 2015 11:37:34 -0500 Subject: add comments on test cases to explain reasons a bit better --- tests/hazmat/primitives/test_keywrap.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'tests') diff --git a/tests/hazmat/primitives/test_keywrap.py b/tests/hazmat/primitives/test_keywrap.py index b1cf5ed8..f1238c9a 100644 --- a/tests/hazmat/primitives/test_keywrap.py +++ b/tests/hazmat/primitives/test_keywrap.py @@ -75,6 +75,7 @@ class TestAESKeyWrap(object): " is unsupported", ) def test_wrap_invalid_key_length(self, backend): + # The wrapping key must be of length [16, 24, 32] with pytest.raises(ValueError): keywrap.aes_key_wrap(b"badkey", b"sixteen_byte_key", backend) @@ -97,15 +98,19 @@ class TestAESKeyWrap(object): " is unsupported", ) def test_wrap_invalid_key_to_wrap_length(self, backend): + # Keys to wrap must be at least 16 bytes long with pytest.raises(ValueError): keywrap.aes_key_wrap(b"sixteen_byte_key", b"\x00" * 15, backend) + # Keys to wrap must be a multiple of 8 bytes with pytest.raises(ValueError): keywrap.aes_key_wrap(b"sixteen_byte_key", b"\x00" * 23, backend) def test_unwrap_invalid_wrapped_key_length(self, backend): + # Keys to unwrap must be at least 24 bytes with pytest.raises(ValueError): keywrap.aes_key_unwrap(b"sixteen_byte_key", b"\x00" * 16, backend) + # Keys to unwrap must be a multiple of 8 bytes with pytest.raises(ValueError): keywrap.aes_key_unwrap(b"sixteen_byte_key", b"\x00" * 27, backend) -- cgit v1.2.3