diff --git a/README.md b/README.md index 4b69f54..e115412 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,11 @@ Clone the repository, use Meson build: meson setup build cd build ninja -./randquik +sudo ninja install # optionally +randquik > /dev/null ``` -Alternatively you may compile it by hand with `gcc randquik.c -o randquik -O3 -pthread`. +Alternatively you may compile it by hand with `gcc *.c -o randquik -O3 -pthread`. ## Python module diff --git a/randquik/nprand.py b/randquik/nprand.py index fd16ac2..2f14a29 100644 --- a/randquik/nprand.py +++ b/randquik/nprand.py @@ -1,8 +1,9 @@ import secrets +import sys import cha import numpy as np -import sys + class ChaRandom(np.random.BitGenerator): def __init__(self, seed=None): diff --git a/src/cha1block.h b/src/cha1block.h index dce2673..fb796af 100644 --- a/src/cha1block.h +++ b/src/cha1block.h @@ -43,7 +43,7 @@ static inline uint64_t _cha_block(cha_ctx* ctx, uint8_t* begin, uint8_t* end) { c = end; // Leftover bytes are stored in ctx for next call ctx->uncount = 64 - bytes; - memcpy(ctx->unconsumed, x + bytes, ctx->uncount); + memcpy(ctx->unconsumed, (uint8_t*)x + bytes, ctx->uncount); break; } memcpy(c, x, 64); diff --git a/tests/test_chacha.py b/tests/test_chacha.py index 2f6f7f6..c2903d4 100644 --- a/tests/test_chacha.py +++ b/tests/test_chacha.py @@ -7,6 +7,7 @@ from randquik import cha def test_cipherstreams_fullblocks(): + """Requests in multiple of ChaCha20 block size 64 bytes""" key = token_bytes(32) nonce = token_bytes(16) c0 = Cipher(ChaCha20(key, nonce), None, None).encryptor() @@ -21,6 +22,7 @@ def test_cipherstreams_fullblocks(): def test_cipherstreams_partial_updates(): + """Odd-sized requests that retain leftover buffers""" key = token_bytes(32) nonce = token_bytes(16) c0 = Cipher(ChaCha20(key, nonce), None, None).encryptor() @@ -32,3 +34,17 @@ def test_cipherstreams_partial_updates(): ct1 = c1(bytearray(N)) assert len(ct0) == len(ct1) assert ct0.hex() == ct1.hex() + + +def test_cipherstreams_32leftover(): + """Test the special case where the second response comes entirely from the leftover buffer""" + key = token_bytes(32) + nonce = token_bytes(16) + c0 = Cipher(ChaCha20(key, nonce), None, None).encryptor() + c1 = cha.Cha(key, nonce) + + for N in [512 - 32, 32]: + ct0 = c0.update(bytes(N)) + ct1 = c1(bytearray(N)) + assert len(ct0) == len(ct1) + assert ct0.hex() == ct1.hex()