lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

commit 123b545d6b0b29a9cb3854f69789afe00c650cec
parent 53f7c6f732e22972fe0c4b24ac1225e6511b7925
Author: triesap <tyson@radroots.org>
Date:   Mon, 12 Oct 2026 20:06:21 +0000

simplex_smp_crypto: Reject lossy padded message lengths

- Check plaintext length before encoding its two-byte padding prefix.
- Retain public lossless boundary and typed rejection regressions.
- Preserve accepted wire bytes, algorithms and public API shapes.
- Verify seventy-two crypto and caller tests plus all scoped Rust checks.

Diffstat:
Mcontracts/architecture/deviations.toml | 28++++++++++++++++++++++++++++
Mcrates/simplex_smp_crypto/README | 5+++++
Mcrates/simplex_smp_crypto/src/message.rs | 8+++++++-
Acrates/simplex_smp_crypto/tests/padding_admission.rs | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 94 insertions(+), 1 deletion(-)

diff --git a/contracts/architecture/deviations.toml b/contracts/architecture/deviations.toml @@ -2,6 +2,34 @@ schema_version = 1 architecture_id = "radroots.crates.release.v1" [[deviation]] +id = "RCRV1-DEV-031" +date = "2026-10-12" +status = "closed" +approval = "The user approved the successor refactor, its scoped execution/recovery amendments and all remaining local verified checkpoints; publication and supported consumer qualification remain separate." +affected_steps = ["010", "040", "070", "130"] +spec_anchors = ["contracts/crates/release_v1/radroots_crates_release_v1.toml#repository_policy"] +source_evidence = [ + "The public padded-message encoder casts unrestricted plaintext length to u16 after checking only padding capacity; its decoder uses that two-byte length to select returned bytes.", + "The current owner and freshly fetched published integration branch retain the identical message helper; no newer published correction was observed.", + "Complete crypto/transport source and caller review identifies the helper in public prelude, ratchet, short-link and live transport paths; source observation alone is not an executed reproduction.", +] +replacement_action = "Bring forward contained lossless padding admission during source reevaluation. Reproduce accepted-byte truncation through the public helper, then reject plaintext lengths unrepresentable by its existing two-byte prefix before allocation or encryption, using the existing typed error. Preserve accepted wire bytes, public API shapes, algorithms, dependencies and feature ownership." +verification = [ + "Validate this standalone deviation before any production edit, then preserve the retained public boundary regression result against unchanged source.", + "Use only fixed disposable key/nonce bytes and bounded local payloads. Prove exact accepted encode/decode at empty, small and maximum representable lengths, and typed rejection above the field limit without network or account operations.", + "Run exact Rust 1.99.0 scoped formatting, all-feature/all-target check, full package tests, no-default-feature library check and warnings-as-errors Clippy; check and test direct owning crypto/transport callers before the local owner commit.", +] +unresolved_risk = "Contained lossless padding admission is locally verified. Aggregate resource limits, ratchet state/custody, independent crypto vectors, actual live transport and supported consumer adoption remain separate; no deployed loss or full platform acceptance is claimed." +normative_architecture_change = false +adr_required = false +closure_evidence = [ + "The retained public boundary suite failed against unchanged production source: a 65536-byte accepted plaintext decoded to zero bytes. Both valid-boundary and insufficient-capacity controls passed.", + "Checked u16 admission now rejects 65536- and 65537-byte plaintexts before padding/encryption; empty, small and 65535-byte accepted payloads preserve exact bytes with both exact and extra padding.", + "Exact Rust 1.99.0 scoped formatting, all-feature/all-target crypto/agent/transport checking, 72 passing package tests with zero ignored, crypto/agent no-default-feature library checking and warnings-as-errors Clippy passed on local macOS arm64.", + "Only checked plaintext-length admission, meaningful public boundary regressions, package guidance and this deviation changed. Accepted wire bytes, public API shapes, cryptographic algorithms, dependencies and feature ownership remain unchanged.", +] + +[[deviation]] id = "RCRV1-DEV-030" date = "2026-10-12" status = "closed" diff --git a/crates/simplex_smp_crypto/README b/crates/simplex_smp_crypto/README @@ -10,6 +10,11 @@ authorization and ratchet cryptography for the `radroots` core libraries. * ratchet header, role, and state types for SimpleX SMP sessions; * X.509 conversion helpers and related nonce and authentication constants. +Padded messages carry a two-byte plaintext length. Payloads larger than +`u16::MAX` are rejected before padding or encryption; accepted payloads retain +every byte. This field limit does not establish application-wide resource +limits or independently qualify ratchet state, custody or live transport. + ## Copyright Except as otherwise noted, all files in the `radroots_simplex_smp_crypto` diff --git a/crates/simplex_smp_crypto/src/message.rs b/crates/simplex_smp_crypto/src/message.rs @@ -158,6 +158,12 @@ pub fn encrypt_padded( plaintext: &[u8], padded_len: usize, ) -> Result<Vec<u8>, RadrootsSimplexSmpCryptoError> { + let plaintext_length = u16::try_from(plaintext.len()).map_err(|_| { + RadrootsSimplexSmpCryptoError::InvalidMessageLength { + actual: plaintext.len(), + padded: padded_len, + } + })?; if plaintext.len().saturating_add(2) > padded_len { return Err(RadrootsSimplexSmpCryptoError::InvalidMessageLength { actual: plaintext.len(), @@ -165,7 +171,7 @@ pub fn encrypt_padded( }); } let mut padded = Vec::with_capacity(padded_len); - padded.extend_from_slice(&(plaintext.len() as u16).to_be_bytes()); + padded.extend_from_slice(&plaintext_length.to_be_bytes()); padded.extend_from_slice(plaintext); padded.resize(padded_len, b'#'); encrypt_no_pad(shared_secret, nonce, &padded) diff --git a/crates/simplex_smp_crypto/tests/padding_admission.rs b/crates/simplex_smp_crypto/tests/padding_admission.rs @@ -0,0 +1,54 @@ +use radroots_simplex_smp_crypto::prelude::{ + RadrootsSimplexSmpCryptoError, decrypt_padded, encrypt_padded, +}; + +const KEY: [u8; 32] = [7; 32]; +const NONCE: [u8; 24] = [9; 24]; + +#[test] +fn accepted_payloads_round_trip_without_losing_bytes() { + for length in [0_usize, 1, 4096, usize::from(u16::MAX)] { + let plaintext: Vec<u8> = (0..length).map(|index| (index % 251) as u8).collect(); + for extra_padding in [0, 3] { + let ciphertext = encrypt_padded(&KEY, &NONCE, &plaintext, length + 2 + extra_padding) + .expect("representable plaintext must fit the padded message"); + assert_eq!( + decrypt_padded(&KEY, &NONCE, &ciphertext).unwrap(), + plaintext, + "accepted plaintext must preserve every byte" + ); + } + } +} + +#[test] +fn unrepresentable_payload_lengths_are_rejected() { + for length in [usize::from(u16::MAX) + 1, usize::from(u16::MAX) + 2] { + let plaintext = vec![0xa5; length]; + let result = encrypt_padded(&KEY, &NONCE, &plaintext, length + 2); + if let Ok(ciphertext) = &result { + let decoded = decrypt_padded(&KEY, &NONCE, ciphertext).unwrap(); + assert_eq!( + decoded.len(), + plaintext.len(), + "an accepted encoding must never lose plaintext bytes" + ); + } + assert!(matches!( + result, + Err(RadrootsSimplexSmpCryptoError::InvalidMessageLength { actual, .. }) + if actual == length + )); + } +} + +#[test] +fn insufficient_padding_capacity_is_rejected() { + assert!(matches!( + encrypt_padded(&KEY, &NONCE, b"abc", 4), + Err(RadrootsSimplexSmpCryptoError::InvalidMessageLength { + actual: 3, + padded: 4 + }) + )); +}