Metadata-Version: 2.4
Name: clsc256
Version: 0.9.0
Summary: Experimental CLSC-256-AEAD research implementation and verification tools
Author-email: Sergey Minasyan <portsidelearn@gmail.com>
License: MIT
Project-URL: Homepage, https://github.com/awaitLearn/clsc256
Project-URL: Repository, https://github.com/awaitLearn/clsc256
Project-URL: Issues, https://github.com/awaitLearn/clsc256/issues
Keywords: cryptography,aead,sponge,ratchet,post-quantum,research
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Security :: Cryptography
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: audit
Requires-Dist: cryptography>=42; extra == "audit"
Dynamic: license-file

# CLSC-256-AEAD v8 — research build 0.9.0

Experimental authenticated encryption for a two-party channel, pure Python, zero runtime dependencies:

- **SpongeARX512** - 512-bit ARX permutation used as a duplex sponge (rate 256 / capacity 256 bits).
  The research build includes an explicit inverse for its 4-word round function and full 512-bit
  permutation, with algebraic notes and inverse-property regression tests.
- **BLAKE2b** key schedule, per-message keys, 128-bit key commitment and a 256-bit outer MAC.
- **Module-LWE KEM** (rank 2, q=3329, n=256) with Fujisaki-Okamoto transform and implicit rejection
  for a post-quantum ratchet. ML-KEM-512-*like* parameters; **not** FIPS 203.
- One chain per direction, skipped-message keys (reordering / loss), replay protection.

> **Status: research code. No formal proof, no third-party review.** Do not use it as the only
> protection of anything that matters. For production use ChaCha20-Poly1305 / AES-GCM (+ ML-KEM-768).

## Install & use

```bash
pip install clsc256
```

```python
import os
from clsc256 import CLSC256Session, SessionRole

root = os.urandom(32)                      # shared secret from your key exchange
alice = CLSC256Session(root, SessionRole.INITIATOR)
bob   = CLSC256Session(root, SessionRole.RESPONDER)

pkt = alice.encrypt(b"hello", aad=b"session:42")
assert bob.decrypt(pkt, aad=b"session:42") == b"hello"

# optional post-quantum ratchet: exchange KEM public keys over an AUTHENTICATED channel
alice.set_peer_kem_public_key(bob.local_kem_public_key)
bob.set_peer_kem_public_key(alice.local_kem_public_key)
```

Rules: one session object per endpoint, opposite roles on the two sides; packets that carry a PQ
ratchet step (`flags == 1`) must not be lost (reliable transport or retransmit). Ordinary packets may
be lost or reordered (up to `MAX_SKIP = 64`).

## What the audit found and what v8 does about it

| Problem (found by attacking v4 / v7) | Fix in v8 |
|---|---|
| v4: one forged packet permanently desynchronised the session | Ratchet state changes only after full authentication |
| v4: no direction separation (reflection) | Separate chain per direction |
| v4/v7: RNG failure or cloned state leaked "equal prefix" of messages | Hedged synthetic nonce bound to the message |
| v7: PQ ratchet never worked (sender and receiver derived different keys) | One shared derivation function; tested over many epochs |
| v7: a lost packet killed the session (skipped keys used the wrong nonce) | Message keys derive from chain + seq only; delayed packets decrypt |
| v7: a forged packet could erase a stored key of a delayed message | Key is deleted only after successful decryption |
| v7: KEM accepted modified ciphertexts (no FO) | FO transform with implicit rejection |
| v7: simultaneous PQ steps desynchronised both sides | Previous KEM secret key kept; packets name their target key |
| v7: outer MAC did not frame AAD length | Lengths of AAD and header are MAC'd |
| v7: S-box/MDS layer with linearly dependent masks, no security gain | Removed; the sponge keystream is XORed directly |
| README claims that were wrong (256-bit collision margin, "dynamic S-box", sqrt constants, ML-KEM) | Corrected below |

## Mathematical verification added in research build 0.9.0

`docs/PERMUTATION_SPEC.md` gives the state space, round equations, inverse equations, and a
construction-level argument that each round is bijective. `SpongeARX512.unpermute()` is the
executable inverse. The test suite checks the inverse over multiple round counts and checks the
Module-LWE negacyclic polynomial multiplication against a simple reference implementation.

Run the reproducible bit-diffusion experiment with:

```bash
PYTHONPATH=src python audit/permutation_analysis.py
```

This experiment measures avalanche propagation for selected round counts. It is a diagnostic,
not a cryptanalytic attack and not evidence of pseudorandomness or security.

## Honest limits

- Sponge generic security is about **2^128** (capacity 256 bits), not 2^256.
- Round constants are digits of pi; rotations are the BLAKE2b ones. The round function is proven
  bijective by construction, but there is no differential/linear/rotational or other serious
  cryptanalysis of the permutation. Avalanche measurements are not a security proof.
- Pure Python: ~300 KB/s, **not constant-time**, `zeroize` is best-effort.
- Symmetric root key must come from your own authenticated key exchange. KEM public keys must be authenticated too, otherwise the first PQ epoch adds no protection.
- Forward secrecy: old chain keys are erased. If the *root key* leaks, recorded traffic is readable (the PQ ratchet only protects against *later* state compromise).
- A truncated stream (dropped final packets) is not detected; add your own end-of-stream message.
- A lost PQ-step packet stalls the receiver until it is retransmitted (it then recovers).

## Reproduce the audit

```bash
pip install -e .[audit]
python -m unittest discover tests
PYTHONPATH=src python audit/permutation_analysis.py
cd audit && PYTHONPATH=../src python audit_v8.py
```

`audit_v8.py` reports median end-to-end throughput against the AES-GCM and ChaCha20-Poly1305
APIs. This is explicitly a diagnostic comparison, not a like-for-like protocol benchmark: CLSC
includes session state, key derivation, padding, and its sponge, while the standard cases use
library AEAD calls with a counter nonce. Do not interpret the numbers as a security ranking.

License: MIT.
