Symmetric building blocks

This no_std crate offers allocation-free caller-buffer APIs for ChaCha20 and HChaCha20, raw Poly1305, AES-128/256 blocks, GHASH/POLYVAL, ChaCha20-Poly1305, XChaCha20-Poly1305, AES-128/256-GCM, AES-128/256-GCM-SIV, and final-standard Ascon-AEAD128. Final Ascon Hash256, XOF128 and CXOF128 share its permutation.

AEAD wrappers own prepared keys. seal(nonce, aad, body) returns a detached 16-byte tag; open(nonce, aad, body, tag) returns Ok(()) only after authentication. Authentication errors clear the complete body, including ciphers whose upstream APIs would otherwise preserve ciphertext. Length preflight errors return MessageTooLong before touching the body. Callers parse after success and supply nonces according to the selected algorithm's requirements. No API obtains randomness or manages application replay.

Every pinned AEAD wrapper exposes MAX_BODY_BYTES, MAX_AAD_BYTES and a pure validate_lengths(body_bytes: u128, aad_bytes: u128) admission check. Bounds apply to the detached body, so the separate tag never increases body capacity. Nonce sizes remain enforced by array types. AES-GCM accepts at most 2^36-32 body bytes as required by SP 800-38D; its pinned backend supports only 2^36 AAD bytes, below the standard's 2^61-1 byte limit. AES-GCM-SIV accepts 2^36 body/AAD bytes per RFC 8452. ChaCha20-Poly1305 and XChaCha20-Poly1305 support 2^38-128 body bytes with this pinned backend and 2^64-1 AAD bytes. The body limit is 64 bytes below RFC 8439's 2^38-64 limit: chacha20 0.9.1 excludes its final counter block. This explicit backend restriction prevents the pinned AEAD's infallible stream call from panicking in its formerly admitted last 63-byte range. The standalone scalar ChaCha primitive retains its full counter range; this change does not redefine the RFC algorithm.

ChaCha20Key retains decoded key words; the standalone block and in-place XOR APIs use the same standard scalar rounds. Counter exhaustion is rejected before mutation. chacha20_xor_reference retains RustCrypto's independent control. Aes128Key/Aes256Key retain round keys and borrow complete caller blocks into the upstream parallel backend. Raw GHASH/POLYVAL accept whole 16-byte blocks without adding AEAD framing or padding.

Poly1305Key is a key-equivalent prepared context, not permission to reuse a one-time key. Independent messages require fresh derived Poly1305 keys. The repeated-context benchmarks use public fixtures only. Tag checks use a full constant-time comparison. Enabled upstream erasure features and owned-key drop erasure do not promise erasure of all caller copies/compiler temporaries.

Sources and variants

RustCrypto pins reproduce the existing qualified source family: ChaCha20 0.9.1, Poly1305 0.8.0, ChaCha20Poly1305 0.10.1, AES 0.8.4, AES-GCM 0.10.3, AES-GCM-SIV 0.11.1, POLYVAL 0.6.2 and GHASH 0.5.1. All disable default features. AES uses constant-time fixslicing on portable wasm64; no secret-indexed table backend is introduced. Registry source identities, VCS pins and MIT/Apache licenses are in provenance/rustcrypto.json and adjacent license files. Scalar ChaCha code is adapted from its pinned upstream software/XChaCha sources, with unchanged 20-round algorithm and explicit caller-buffer bounds.

Ascon is mechanically adapted from the CC0 official ascon/ascon-c reference, commit 446347f21b209f3921c65ece70027c366cbe1693, implementing NIST SP 800-232 (August 2025). This uses final IVs and little-endian bytes, without an old v1.2 adapter. provenance/ascon.json pins the source files and selected official KATs; the original CC0 license/reference files are retained. The named seal_unrolled/open_unrolled methods are an optimization candidate with the same standard permutation and outputs. The compact round loop remains the default until actual IC measurements select a winner.

The named ChaCha candidates keep the same RFC 8439 rounds and standard byte formats. ChaCha20Key::xor_words decodes each nonce once and XORs complete words directly. xor_simd runs one block across SIMD lanes; xor_simd4 runs four counter blocks in parallel before using the one-block path for the tail. block_simd and hchacha20_simd use the one-block path. SIMD candidates require the selected IC wasm SIMD128 execution contract and use word-wise scalar rounds on non-Wasm test hosts. Their names do not imply a measured win.

Poly1305WideKey retains the standard five 26-bit limbs in u64 slots and precomputes their fivefold multiples. It borrows complete message blocks; only partial blocks need a temporary padded block. The ChaCha20Poly1305Wide/XChaCha20Poly1305Wide candidates compose that arithmetic with an explicit ChaChaBackend, hash borrowed AAD/ciphertext and exact length bytes, and retain no per-message allocation. They authenticate before decrypting and preserve complete failure wiping. Their counter implementation supports the full RFC body limit of 2^38-64 bytes, with pure admission checks and small-buffer final-counter probes; the pinned wrappers retain their documented smaller backend bound. All existing defaults remain until matched installed wasm64 measurements select a candidate. Adaptation details and primary-source links are in ChaCha candidate provenance.

Validation and measurements

Tests contain RFC 8439 ChaCha/Poly1305/AEAD, the established HChaCha/XChaCha draft vectors, FIPS AES block vectors, NIST GCM and RFC 8452 GCM-SIV vectors, and official final Ascon AEAD/hash/XOF/CXOF KAT selections. Both Ascon permutation variants run the same KATs. Boundary and tampering tests check partial blocks, changed AAD/tags/ciphertext, complete failure wiping, counter exhaustion and unchanged output after invalid customization. Pure length tests exercise maximum/minimum/rejected huge lengths without large allocations. Small-buffer seek probes verify the pinned ChaCha and XChaCha stream backend's actual final usable block and non-writing overflow rejection.

The symmetric fixture compares fresh/prepared keys and the Ascon permutation candidate. All caller buffers are allocated at setup. In-place timing includes restoring the buffer; symmetric-buffer-copy exposes that control. Checks after timing compare every output byte and expected status. Existing AEAD cases replay their fixed key/nonce/AAD/plaintext fixtures across calls. With distinct=false, the cohort repeats that tuple too. Their fresh/prepared names describe key-context construction, not fresh messages. The additional *-records seal cases require distinct=false and use one prepared key with record-specific input/AAD. Their 96-bit counter prefixes are unique across records and successive driver cohorts, including warmups. All nonce widths preserve that prefix. Matching expected ciphertexts and tags are computed after timing for the current counter. Rolled and unrolled Ascon record cases remain explicit candidates with the same inputs and counters. Raw block cases require complete input blocks and report preparation errors for other sizes. No historical measurement is a fresh qualification here.

The additional ChaCha candidate fixtures export symmetric::chacha_candidate_cases() for the root build owner to register. Fresh AEAD cases construct and erase their key context inside each timed operation. *-seal-records candidates use one retained key, record-specific input/AAD and the same advancing 96-bit nonce prefixes as their pinned controls; they require distinct=false. Every retained body/tag/status is compared with the independent pinned implementation after timing. Setup-only final-counter probes exercise the actual selected backend before fixture admission, including complete/partial final blocks and unchanged output after rejected overflow. Native tests execute exact 100-record groups at 32/128/512/2048 bytes, all three verdict patterns and four cohort counters. Native SIMD tests cover the scalar fallback; actual SIMD validation requires the installed-Wasm fixtures.

Explicit AES candidates

Aes128GcmPrepared64 and Aes256GcmPrepared64 retain six groups of masked and reversed key words used by the pinned constant-time software POLYVAL multiplication. The prepared hash key occupies 192 bytes, versus the upstream 16-byte hash key; each record borrows it instead of rebuilding its masks/reversals per block. GHashPrepared64 and PolyvalPrepared64 expose the same raw complete-block hashing for attribution. GHASH's RFC 8452 conversion uses two 64-bit words; the existing software64 multiplication already avoids wide-product helpers. No secret-indexed lookup or secret-dependent arithmetic branch is introduced.

Aes128GcmSivBatched and Aes256GcmSivBatched encrypt their four/six per-nonce derivation blocks together through the existing AES backend. Portable wasm64 fixslicing processes four AES blocks per round batch, making this a concrete candidate for reducing derivation work. Per-nonce keys are still derived for every record; a retained master key never authorizes reuse of a derived key under another nonce. Invalid GCM-SIV opens wipe the body directly, avoiding the upstream ciphertext-restoration pass followed by the wrapper's required wipe. These candidates preserve the default wrappers' admission and failure contract. Defaults remain the pinned upstream wrappers pending actual IC measurements.

All AES candidates use four 16-byte counter blocks on the stack, with no heap allocation in the algorithm. GCM-SIV derivation uses six 16-byte blocks, a 16-byte MAC key and a 16/32-byte encryption key, then a 192-byte prepared hash key and the derived AES schedule. These are source-level scratch sizes, not a measured maximum call-stack bound; backend round scratch and compiler temporaries remain additional. Key contexts and temporary key/keystream buffers use explicit erasure. Native AES backend dispatch can differ from wasm64; native timing cannot qualify an IC performance improvement.

The AES-only candidate fixture adds fixed-tuple candidate seal/open cases with fresh or prepared key contexts, matching the existing upstream fixture bytes and restore boundary. Its paired *-reference-records and candidate *-records cases vary plaintext/AAD and advance nonces across driver cohorts. For those cases distinct=false means one shared master key; distinct=true means a prepared master key per record. Record bytes/nonces vary in both populations. Open cases test valid/invalid/mixed tags. Every retained body, tag and status is checked after timing against the upstream implementation. Native exact-cohort tests execute 100 records at each of 32/128/512/2048 bytes. Source identities, licenses and local changes are in provenance/aes-candidates.json.

Authenticated open cohorts

chachapoly_open_cohort is an explicit ChaCha20-Poly1305 candidate for borrowed records. ChaChaKeySet::Distinct supplies one raw key per record; ChaChaKeySet::Shared supplies one raw key decoded within the call. Neither mode carries prepared state from an earlier call. Counts and every record's lengths are checked before any body or verdict changes. Equal-size groups of four use parallel ChaCha block zero and payload blocks. Ragged groups and trailing records use the existing per-record implementation. Authentication checks all 16 tag bytes per record before writing its plaintext. Invalid records are cleared completely, including when another SIMD lane succeeds.

ChaChaCohortBackend::Simd4 isolates the four-record ChaCha candidate with scalar widened Poly1305. Simd4Poly2 additionally hashes two independent records in 64-bit SIMD lanes. This uses the same clamped five 26-bit limbs, lane-local carries and branchless final reduction as the scalar arithmetic. The largest intermediate is below 2^61; the inductive carry bound is recorded beside the implementation and in cohort provenance. All addressable key, keystream and accumulator scratch is erased; compiler temporaries remain outside that guarantee. The algorithm allocates no heap storage.

Cohort fixtures export symmetric::chacha_cohort_cases() for root registration. The six cases compare the current per-record SIMD4 Wide implementation, parallel ChaCha alone, and parallel ChaCha/Poly1305. *-distinct requires distinct=true; *-one-key requires distinct=false and still stages 100 different records/nonces. Each timed call restores all bodies, constructs its key context and temporary borrowed-carrier vector, authenticates/decrypts each admitted record once, and drops that vector. Full bodies and verdicts are checked after timing against independently staged upstream ciphertexts. Opening a staged ciphertext again does not create a new encryption or refresh its nonce.

Native tests execute exact 100-record cohorts at 32/128/256/512/2048 bytes, boundary and ragged lengths, shared/distinct keys, all verdict patterns, RFC 8439 vectors, tails through 500 records and complete failure wiping. The same paired Poly1305 formulas run natively with scalar vector lanes, including all-ones carry cases. Native ChaCha still uses its scalar fallback. The actual SIMD loads/shuffles and whole/body/page costs require installed wasm64 qualification; existing defaults remain unchanged.