diff options
Diffstat (limited to 'packages/meshbay-common/tests/test_webcrypto.py')
| -rw-r--r-- | packages/meshbay-common/tests/test_webcrypto.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/packages/meshbay-common/tests/test_webcrypto.py b/packages/meshbay-common/tests/test_webcrypto.py new file mode 100644 index 0000000..25bcd5c --- /dev/null +++ b/packages/meshbay-common/tests/test_webcrypto.py @@ -0,0 +1,46 @@ +"""Tests for AES-256-GCM webcrypto variant.""" + +import os +import pytest +import blake3 +from meshbay_common.crypto import generate_gek +from meshbay_common.webcrypto import chunk_key_aes, encrypt_chunk_aes, decrypt_chunk_aes + + +def test_aes_roundtrip(): + gek = generate_gek() + data = os.urandom(1024 * 1024) # 1 MB + fh = blake3.blake3(data).digest() + key = chunk_key_aes(gek, fh, 0) + nonce, ct = encrypt_chunk_aes(key, data) + assert decrypt_chunk_aes(key, nonce, ct) == data + + +def test_aes_key_distinct_from_chacha_key(): + """AES and ChaCha20 keys for the same chunk must differ.""" + from meshbay_common.crypto import chunk_key as chacha_key + gek = generate_gek() + data = os.urandom(100) + fh = blake3.blake3(data).digest() + aes_k = chunk_key_aes(gek, fh, 0) + chacha_k = chacha_key(gek, fh, 0) + assert aes_k != chacha_k + + +def test_aes_wrong_key_rejected(): + gek = generate_gek() + data = b"private content" + fh = blake3.blake3(data).digest() + key = chunk_key_aes(gek, fh, 0) + nonce, ct = encrypt_chunk_aes(key, data) + wrong_key = chunk_key_aes(generate_gek(), fh, 0) + with pytest.raises(Exception): + decrypt_chunk_aes(wrong_key, nonce, ct) + + +def test_aes_chunk_keys_unique_per_chunk(): + gek = generate_gek() + data = os.urandom(32) + fh = blake3.blake3(data).digest() + keys = {chunk_key_aes(gek, fh, i) for i in range(5)} + assert len(keys) == 5 # all distinct |