From 83a4889378675d5b12ae9c4ee1e66e5780ceec62 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 12 Nov 2024 11:53:58 -0800 Subject: [PATCH 1/6] Ensure that secretbox can accept any bytes-like object as parameter --- src/nacl/bindings/crypto_secretbox.py | 6 ++++++ tests/test_bindings.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/nacl/bindings/crypto_secretbox.py b/src/nacl/bindings/crypto_secretbox.py index d1ad1133..01a36268 100644 --- a/src/nacl/bindings/crypto_secretbox.py +++ b/src/nacl/bindings/crypto_secretbox.py @@ -44,6 +44,9 @@ def crypto_secretbox(message: bytes, nonce: bytes, key: bytes) -> bytes: if len(nonce) != crypto_secretbox_NONCEBYTES: raise exc.ValueError("Invalid nonce") + nonce = ffi.from_buffer(nonce) + key = ffi.from_buffer(key) + padded = b"\x00" * crypto_secretbox_ZEROBYTES + message ciphertext = ffi.new("unsigned char[]", len(padded)) @@ -72,6 +75,9 @@ def crypto_secretbox_open( if len(nonce) != crypto_secretbox_NONCEBYTES: raise exc.ValueError("Invalid nonce") + nonce = ffi.from_buffer(nonce) + key = ffi.from_buffer(key) + padded = b"\x00" * crypto_secretbox_BOXZEROBYTES + ciphertext plaintext = ffi.new("unsigned char[]", len(padded)) diff --git a/tests/test_bindings.py b/tests/test_bindings.py index a89c361c..99c4f651 100644 --- a/tests/test_bindings.py +++ b/tests/test_bindings.py @@ -94,6 +94,25 @@ def test_secretbox_easy(): ) +@pytest.mark.parametrize( + ("encoder", "decoder"), + [ + [bytes, bytearray], + [bytearray, bytes], + [bytearray, bytearray], + ], +) +def test_secretbox_bytearray(encoder, decoder): + key = b"\x00" * c.crypto_secretbox_KEYBYTES + msg = b"message" + nonce = b"\x01" * c.crypto_secretbox_NONCEBYTES + ct = c.crypto_secretbox(encoder(msg), encoder(nonce), encoder(key)) + assert len(ct) == len(msg) + c.crypto_secretbox_BOXZEROBYTES + assert tohex(ct) == "3ae84dfb89728737bd6e2c8cacbaf8af3d34cc1666533a" + msg2 = c.crypto_secretbox_open(decoder(ct), decoder(nonce), decoder(key)) + assert msg2 == msg + + def test_secretbox_wrong_length(): with pytest.raises(ValueError): c.crypto_secretbox(b"", b"", b"") From 61e2384ee551f2910efdd4db25cffbe7fe716203 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 12 Nov 2024 12:04:19 -0800 Subject: [PATCH 2/6] Including mypy in tests extra --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index d6cc581c..7bae6919 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,6 +40,7 @@ dependencies = [ tests = [ "pytest>=3.2.1,!=3.3.0", "hypothesis>=3.27.0", + "mypy>=1.4.1", ] docs = [ "sphinx<7", From 263f9c916019077e99615deac44b9f751dabf099 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 12 Nov 2024 12:04:30 -0800 Subject: [PATCH 3/6] Address typing error --- tests/test_bindings.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/test_bindings.py b/tests/test_bindings.py index 99c4f651..cba3bc27 100644 --- a/tests/test_bindings.py +++ b/tests/test_bindings.py @@ -15,7 +15,7 @@ import hashlib from binascii import hexlify, unhexlify -from typing import List, Tuple +from typing import Callable, List, Tuple from hypothesis import given, settings from hypothesis.strategies import binary, integers @@ -97,12 +97,14 @@ def test_secretbox_easy(): @pytest.mark.parametrize( ("encoder", "decoder"), [ - [bytes, bytearray], - [bytearray, bytes], - [bytearray, bytearray], + (bytes, bytearray), + (bytearray, bytes), + (bytearray, bytearray), ], ) -def test_secretbox_bytearray(encoder, decoder): +def test_secretbox_bytearray( + encoder: Callable[[bytes], bytes], decoder: Callable[[bytes], bytes] +): key = b"\x00" * c.crypto_secretbox_KEYBYTES msg = b"message" nonce = b"\x01" * c.crypto_secretbox_NONCEBYTES From 00d63dea03583e9b34d4d226d3e932ed3d16f8db Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 12 Nov 2024 12:34:02 -0800 Subject: [PATCH 4/6] Adding memoryview to secretbox test matrix --- tests/test_bindings.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_bindings.py b/tests/test_bindings.py index cba3bc27..6e2a4c61 100644 --- a/tests/test_bindings.py +++ b/tests/test_bindings.py @@ -16,6 +16,7 @@ import hashlib from binascii import hexlify, unhexlify from typing import Callable, List, Tuple +import itertools from hypothesis import given, settings from hypothesis.strategies import binary, integers @@ -96,13 +97,12 @@ def test_secretbox_easy(): @pytest.mark.parametrize( ("encoder", "decoder"), - [ - (bytes, bytearray), - (bytearray, bytes), - (bytearray, bytearray), - ], + itertools.product( + [bytes, bytearray, memoryview], + [bytes, bytearray, memoryview], + ), ) -def test_secretbox_bytearray( +def test_secretbox_byteslike( encoder: Callable[[bytes], bytes], decoder: Callable[[bytes], bytes] ): key = b"\x00" * c.crypto_secretbox_KEYBYTES From 778b967f8cc07a2de22cf385dcb2f6ab541bf8ae Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 12 Nov 2024 12:41:36 -0800 Subject: [PATCH 5/6] Including flake8 in tests extra --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 7bae6919..133fe082 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,6 +41,7 @@ tests = [ "pytest>=3.2.1,!=3.3.0", "hypothesis>=3.27.0", "mypy>=1.4.1", + "flake8>=5.0.4", ] docs = [ "sphinx<7", From 0c35533048620b65342c0e0d7518e24a64591961 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Tue, 12 Nov 2024 12:41:43 -0800 Subject: [PATCH 6/6] Addressing linting errors --- tests/test_bindings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_bindings.py b/tests/test_bindings.py index 6e2a4c61..bbcabd5a 100644 --- a/tests/test_bindings.py +++ b/tests/test_bindings.py @@ -14,9 +14,9 @@ import hashlib +import itertools from binascii import hexlify, unhexlify from typing import Callable, List, Tuple -import itertools from hypothesis import given, settings from hypothesis.strategies import binary, integers