Test our implementation against Cryptography module's

This commit is contained in:
2023-10-29 10:59:32 +00:00
parent 8e38985bf8
commit baf3ef31a5
4 changed files with 22 additions and 4 deletions
+3 -2
View File
@@ -20,10 +20,11 @@ Clone the repository, use Meson build:
meson setup build meson setup build
cd build cd build
ninja 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 ## Python module
+2 -1
View File
@@ -1,8 +1,9 @@
import secrets import secrets
import sys
import cha import cha
import numpy as np import numpy as np
import sys
class ChaRandom(np.random.BitGenerator): class ChaRandom(np.random.BitGenerator):
def __init__(self, seed=None): def __init__(self, seed=None):
+1 -1
View File
@@ -43,7 +43,7 @@ static inline uint64_t _cha_block(cha_ctx* ctx, uint8_t* begin, uint8_t* end) {
c = end; c = end;
// Leftover bytes are stored in ctx for next call // Leftover bytes are stored in ctx for next call
ctx->uncount = 64 - bytes; ctx->uncount = 64 - bytes;
memcpy(ctx->unconsumed, x + bytes, ctx->uncount); memcpy(ctx->unconsumed, (uint8_t*)x + bytes, ctx->uncount);
break; break;
} }
memcpy(c, x, 64); memcpy(c, x, 64);
+16
View File
@@ -7,6 +7,7 @@ from randquik import cha
def test_cipherstreams_fullblocks(): def test_cipherstreams_fullblocks():
"""Requests in multiple of ChaCha20 block size 64 bytes"""
key = token_bytes(32) key = token_bytes(32)
nonce = token_bytes(16) nonce = token_bytes(16)
c0 = Cipher(ChaCha20(key, nonce), None, None).encryptor() c0 = Cipher(ChaCha20(key, nonce), None, None).encryptor()
@@ -21,6 +22,7 @@ def test_cipherstreams_fullblocks():
def test_cipherstreams_partial_updates(): def test_cipherstreams_partial_updates():
"""Odd-sized requests that retain leftover buffers"""
key = token_bytes(32) key = token_bytes(32)
nonce = token_bytes(16) nonce = token_bytes(16)
c0 = Cipher(ChaCha20(key, nonce), None, None).encryptor() c0 = Cipher(ChaCha20(key, nonce), None, None).encryptor()
@@ -32,3 +34,17 @@ def test_cipherstreams_partial_updates():
ct1 = c1(bytearray(N)) ct1 = c1(bytearray(N))
assert len(ct0) == len(ct1) assert len(ct0) == len(ct1)
assert ct0.hex() == ct1.hex() 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()