Fixes from review

pull/84/head
Eric Blankenhorn 2026-03-02 11:54:06 -06:00
parent 01219a4659
commit 6e9f2ba1dc
2 changed files with 25 additions and 0 deletions

View File

@ -50,6 +50,24 @@ if _lib.CHACHA20_POLY1305_ENABLED:
with pytest.raises(ValueError):
ChaCha20Poly1305(b"tooshort")
def test_encrypt_invalid_iv_length():
key = h2b("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f")
chacha = ChaCha20Poly1305(key)
with pytest.raises(ValueError):
chacha.encrypt(b"aad", b"short", b"plaintext")
def test_decrypt_invalid_iv_length():
key = h2b("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f")
chacha = ChaCha20Poly1305(key)
with pytest.raises(ValueError):
chacha.decrypt(b"aad", b"short", b"\x00" * 16, b"ciphertext")
def test_decrypt_invalid_tag_length():
key = h2b("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f")
chacha = ChaCha20Poly1305(key)
with pytest.raises(ValueError):
chacha.decrypt(b"aad", b"\x00" * 12, b"short", b"ciphertext")
def test_decrypt_bad_tag():
key = h2b("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f")
iv = h2b("07000000404142434445464748")

View File

@ -561,6 +561,8 @@ if _lib.CHACHA20_POLY1305_ENABLED:
"""
aad = t2b(aad)
iv = t2b(iv)
if len(iv) != 12:
raise ValueError("iv must be 12 bytes, got %d" % len(iv))
plaintext = t2b(plaintext)
ciphertext = _ffi.new("byte[%d]" % len(plaintext))
authTag = _ffi.new("byte[%d]" % self._tag_bytes)
@ -587,7 +589,12 @@ if _lib.CHACHA20_POLY1305_ENABLED:
"""
aad = t2b(aad)
iv = t2b(iv)
if len(iv) != 12:
raise ValueError("iv must be 12 bytes, got %d" % len(iv))
authTag = t2b(authTag)
if len(authTag) != self._tag_bytes:
raise ValueError("authTag must be %d bytes, got %d" %
(self._tag_bytes, len(authTag)))
ciphertext = t2b(ciphertext)
plaintext = _ffi.new("byte[%d]" % len(ciphertext))
ret = _lib.wc_ChaCha20Poly1305_Decrypt(