Skip to content

Commit 6e9f2ba

Browse files
committed
Fixes from review
1 parent 01219a4 commit 6e9f2ba

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

tests/test_chacha20poly1305.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ def test_invalid_key_size():
5050
with pytest.raises(ValueError):
5151
ChaCha20Poly1305(b"tooshort")
5252

53+
def test_encrypt_invalid_iv_length():
54+
key = h2b("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f")
55+
chacha = ChaCha20Poly1305(key)
56+
with pytest.raises(ValueError):
57+
chacha.encrypt(b"aad", b"short", b"plaintext")
58+
59+
def test_decrypt_invalid_iv_length():
60+
key = h2b("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f")
61+
chacha = ChaCha20Poly1305(key)
62+
with pytest.raises(ValueError):
63+
chacha.decrypt(b"aad", b"short", b"\x00" * 16, b"ciphertext")
64+
65+
def test_decrypt_invalid_tag_length():
66+
key = h2b("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f")
67+
chacha = ChaCha20Poly1305(key)
68+
with pytest.raises(ValueError):
69+
chacha.decrypt(b"aad", b"\x00" * 12, b"short", b"ciphertext")
70+
5371
def test_decrypt_bad_tag():
5472
key = h2b("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f")
5573
iv = h2b("07000000404142434445464748")

wolfcrypt/ciphers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ def encrypt(self, aad, iv, plaintext):
561561
"""
562562
aad = t2b(aad)
563563
iv = t2b(iv)
564+
if len(iv) != 12:
565+
raise ValueError("iv must be 12 bytes, got %d" % len(iv))
564566
plaintext = t2b(plaintext)
565567
ciphertext = _ffi.new("byte[%d]" % len(plaintext))
566568
authTag = _ffi.new("byte[%d]" % self._tag_bytes)
@@ -587,7 +589,12 @@ def decrypt(self, aad, iv, authTag, ciphertext):
587589
"""
588590
aad = t2b(aad)
589591
iv = t2b(iv)
592+
if len(iv) != 12:
593+
raise ValueError("iv must be 12 bytes, got %d" % len(iv))
590594
authTag = t2b(authTag)
595+
if len(authTag) != self._tag_bytes:
596+
raise ValueError("authTag must be %d bytes, got %d" %
597+
(self._tag_bytes, len(authTag)))
591598
ciphertext = t2b(ciphertext)
592599
plaintext = _ffi.new("byte[%d]" % len(ciphertext))
593600
ret = _lib.wc_ChaCha20Poly1305_Decrypt(

0 commit comments

Comments
 (0)