aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hazmat/primitives
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2017-09-28 23:46:49 +0800
committerAlex Gaynor <alex.gaynor@gmail.com>2017-09-28 11:46:49 -0400
commit62ebb429fe94693e5b94480025f3f3e0556b83b1 (patch)
treed4ecaceab10179e4ead9fc21e20b873dfe1fcbb9 /tests/hazmat/primitives
parentba61c2738e5a79480d135c280316e29080a4a777 (diff)
downloadcryptography-62ebb429fe94693e5b94480025f3f3e0556b83b1.tar.gz
cryptography-62ebb429fe94693e5b94480025f3f3e0556b83b1.tar.bz2
cryptography-62ebb429fe94693e5b94480025f3f3e0556b83b1.zip
add ChaCha20 support (#3919)
* add ChaCha20 support * review feedback * 256 divided by 8 is what again? * ...
Diffstat (limited to 'tests/hazmat/primitives')
-rw-r--r--tests/hazmat/primitives/test_chacha20.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/hazmat/primitives/test_chacha20.py b/tests/hazmat/primitives/test_chacha20.py
new file mode 100644
index 00000000..16ef97ed
--- /dev/null
+++ b/tests/hazmat/primitives/test_chacha20.py
@@ -0,0 +1,60 @@
+# 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 struct
+
+import pytest
+
+from cryptography.hazmat.backends.interfaces import CipherBackend
+from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
+
+from .utils import _load_all_params
+from ...utils import load_nist_vectors
+
+
+@pytest.mark.supported(
+ only_if=lambda backend: backend.cipher_supported(
+ algorithms.ChaCha20(b"\x00" * 32, b"0" * 16), None
+ ),
+ skip_message="Does not support ChaCha20",
+)
+@pytest.mark.requires_backend_interface(interface=CipherBackend)
+class TestChaCha20(object):
+ @pytest.mark.parametrize(
+ "vector",
+ _load_all_params(
+ os.path.join("ciphers", "ChaCha20"),
+ ["rfc7539.txt"],
+ load_nist_vectors
+ )
+ )
+ def test_vectors(self, vector, backend):
+ key = binascii.unhexlify(vector["key"])
+ nonce = binascii.unhexlify(vector["nonce"])
+ ibc = struct.pack("<i", int(vector["initial_block_counter"]))
+ pt = binascii.unhexlify(vector["plaintext"])
+ encryptor = Cipher(
+ algorithms.ChaCha20(key, ibc + nonce), None, backend
+ ).encryptor()
+ computed_ct = encryptor.update(pt) + encryptor.finalize()
+ assert binascii.hexlify(computed_ct) == vector["ciphertext"]
+
+ def test_key_size(self):
+ chacha = algorithms.ChaCha20(b"0" * 32, b"0" * 16)
+ assert chacha.key_size == 256
+
+ def test_invalid_key_size(self):
+ with pytest.raises(ValueError):
+ algorithms.ChaCha20(b"wrongsize", b"0" * 16)
+
+ def test_invalid_nonce(self):
+ with pytest.raises(ValueError):
+ algorithms.ChaCha20(b"0" * 32, b"0")
+
+ with pytest.raises(TypeError):
+ algorithms.ChaCha20(b"0" * 32, object())