api: close compatibility surface gaps

This commit is contained in:
DeFiDude committed 2026-07-24 18:07:41 -06:00
1 parent 2be764308e
commit ca9bcae376
6 files changed
+66 -25

No files matched your search

+6
View File
@@ -11,10 +11,16 @@ use crate::random::{random_16, random_bytes};
/// IV (16) + HMAC (32). A token is this plus the PKCS7-padded ciphertext.
pub const TOKEN_OVERHEAD: usize = 48;
/// AES block size used by legacy AES-128 tokens.
pub const AES128_BLOCKSIZE: usize = 16;
/// AES block size used by current AES-256 tokens.
pub const AES256_BLOCKSIZE: usize = 16;
/// AES-256 token key size: 32-byte signing key + 32-byte encryption key.
pub const KEY_LENGTH: usize = 64;
/// Legacy AES-128 token key size: 16-byte signing key + 16-byte encryption key.
pub const LEGACY_KEY_LENGTH: usize = 32;
/// Python `Identity.DERIVED_KEY_LENGTH_LEGACY`.
pub const DERIVED_KEY_LENGTH_LEGACY: usize = LEGACY_KEY_LENGTH;
/// Errors surfaced by [`encrypt`] and [`decrypt`].
#[derive(Debug, Error)]
+5
View File
@@ -18,6 +18,8 @@ pub const CURVE: &str = "Curve25519";
pub const KEY_SIZE_BITS: usize = 512;
/// X25519 ratchet key size, in bits.
pub const RATCHET_SIZE_BITS: usize = 256;
/// X25519 ratchet key size, in bytes (Python `Identity.RATCHETSIZE`).
pub const RATCHETSIZE: usize = RATCHET_SIZE_BITS / 8;
/// Addressable identity hash size, in bits.
pub const HASH_LENGTH_BITS: usize = 128;
/// Ratchet identifier size, in bytes.
@@ -115,6 +117,7 @@ impl Identity {
pub const CURVE: &'static str = CURVE;
pub const KEY_SIZE_BITS: usize = KEY_SIZE_BITS;
pub const RATCHET_SIZE_BITS: usize = RATCHET_SIZE_BITS;
pub const RATCHETSIZE: usize = RATCHET_SIZE_BITS / 8;
pub const HASH_LENGTH_BITS: usize = HASH_LENGTH_BITS;
pub fn new() -> Self {
@@ -451,6 +454,8 @@ mod tests {
#[test]
fn test_generate_identity() {
let id = Identity::new();
assert_eq!(Identity::CURVE, "Curve25519");
assert_eq!(Identity::RATCHETSIZE, 32);
assert!(id.has_private_key());
assert_ne!(id.hash, [0u8; 16]);
}
+6 -2
View File
@@ -2,8 +2,12 @@ use rns_crypto::hkdf::hkdf_sha256;
use rns_crypto::sha::sha256;
use thiserror::Error;
// Fixed IFAC HKDF salt. Shared with the Python reference; changing it breaks interop.
const IFAC_SALT: &str = "adf54d882c9a9b80771eb4995d702d4a3e733391b2a0f53f416d9f907e55cff8";
/// Fixed IFAC HKDF salt shared with the Python reference.
///
/// This is public for applications that need to derive or inspect
/// Python-compatible IFAC material outside the runtime helper. Changing it
/// would break interoperability.
pub const IFAC_SALT: &str = "adf54d882c9a9b80771eb4995d702d4a3e733391b2a0f53f416d9f907e55cff8";
#[derive(Debug, Error)]
pub enum IfacError {
+14
View File
@@ -107,6 +107,9 @@ pub struct Link {
/// Truncated SHA-256 of the request's hashable part.
pub link_id: [u8; 16],
pub state: LinkState,
/// Terminal close reason, retained after keys and transient state are
/// purged. Remains `None` until an orderly or timeout close occurs.
pub teardown_reason: Option<CloseReason>,
pub is_initiator: bool,
pub mode: u8,
@@ -216,6 +219,7 @@ impl Link {
let link = Self {
link_id,
state: LinkState::Pending,
teardown_reason: None,
is_initiator: true,
mode: DEFAULT_MODE,
ephemeral_keys: Some(ephemeral_keys),
@@ -476,6 +480,7 @@ impl Link {
let link = Self {
link_id,
state: LinkState::Handshake,
teardown_reason: None,
is_initiator: false,
mode: request.signalling.mode,
ephemeral_keys: Some(responder_keys),
@@ -1298,6 +1303,7 @@ impl Link {
fn close(&mut self, reason: CloseReason) {
tracing::debug!(link_id = ?self.link_id, ?reason, "link state -> Closed");
self.teardown_reason = Some(reason);
for receipt in &mut self.pending_requests {
receipt.fail();
}
@@ -1616,11 +1622,19 @@ mod tests {
let teardown_data = initiator.teardown(CloseReason::InitiatorClosed);
assert_eq!(initiator.state, LinkState::Closed);
assert_eq!(
initiator.teardown_reason,
Some(CloseReason::InitiatorClosed)
);
assert!(teardown_data.is_some());
let accepted = responder.receive_teardown(&teardown_data.unwrap());
assert!(accepted);
assert_eq!(responder.state, LinkState::Closed);
assert_eq!(
responder.teardown_reason,
Some(CloseReason::DestinationClosed)
);
}
#[test]
+15 -7
View File
@@ -306,11 +306,8 @@ impl Channel {
false
}
/// Empty registration list means "accept anything", matching the
/// permissive behaviour of the Python reference before type filters were
/// added.
fn is_type_registered(&self, msg_type: u16) -> bool {
self.registered_types.is_empty() || self.registered_types.contains(&msg_type)
self.registered_types.contains(&msg_type)
}
/// True when the channel has window headroom to accept a new outbound message.
@@ -843,6 +840,7 @@ mod tests {
fn test_channel_send_receive() {
let mut tx_channel = Channel::new(0.1);
let mut rx_channel = Channel::new(0.1);
rx_channel.register_message_type(0x0001).unwrap();
let msg = TestMessage::new(b"hello");
let raw = tx_channel.send(&msg).unwrap();
@@ -857,6 +855,7 @@ mod tests {
fn test_channel_ordering() {
let mut tx = Channel::new(0.1);
let mut rx = Channel::new(0.1);
rx.register_message_type(0x0001).unwrap();
let msg1 = TestMessage::new(b"first");
let msg2 = TestMessage::new(b"second");
@@ -925,6 +924,7 @@ mod tests {
fn test_duplicate_rejection() {
let mut tx = Channel::new(0.1);
let mut rx = Channel::new(0.1);
rx.register_message_type(0x0001).unwrap();
let msg = TestMessage::new(b"once");
let raw = tx.send(&msg).unwrap();
@@ -1094,8 +1094,8 @@ mod tests {
}
#[test]
fn test_no_registered_types_allows_all() {
// With an empty registry the channel must not filter inbound frames.
fn test_no_registered_types_rejects_all() {
// Python requires every inbound user or system type to be registered.
let mut ch = Channel::new(0.1);
let mut raw = Vec::new();
@@ -1105,7 +1105,10 @@ mod tests {
raw.extend_from_slice(b"test");
let result = ch.receive(&raw);
assert!(result.is_ok());
assert!(matches!(
result,
Err(ChannelError::UnknownMessageType(0x1234))
));
}
#[test]
@@ -1152,6 +1155,7 @@ mod tests {
let raw = ch.send(&msg).unwrap();
let mut rx = Channel::new(0.1);
rx.register_message_type(0x0001).unwrap();
let counter2 = counter.clone();
let c4 = counter2.clone();
rx.add_message_handler(Box::new(move |_msg_type, _payload| {
@@ -1183,6 +1187,7 @@ mod tests {
let mut tx = Channel::new(0.1);
let raw = tx.send(&TestMessage::new(b"handler tokens")).unwrap();
let mut rx = Channel::new(0.1);
rx.register_message_type(0x0001).unwrap();
let counter = Arc::new(AtomicU32::new(0));
let first_counter = counter.clone();
@@ -1216,6 +1221,7 @@ mod tests {
let link_id = [0xAA; 16];
let mut tx_lc = LinkChannel::new(link_id, 0.1);
let mut rx_lc = LinkChannel::new(link_id, 0.1);
rx_lc.register_message_type(0x0001).unwrap();
assert_eq!(*tx_lc.link_id(), link_id);
assert!(tx_lc.is_active());
@@ -1235,6 +1241,7 @@ mod tests {
let link_id = [0xBB; 16];
let mut tx_lc = LinkChannel::new(link_id, 0.1);
let mut rx_lc = LinkChannel::new(link_id, 0.1);
rx_lc.register_message_type(0x0001).unwrap();
// Send two messages
let msg1 = TestMessage::new(b"first");
@@ -1338,6 +1345,7 @@ mod tests {
let mut tx_lc = LinkChannel::new_encrypted(link_id, 0.1, keys_a);
let mut rx_lc = LinkChannel::new_encrypted(link_id, 0.1, keys_b);
rx_lc.register_message_type(0x0001).unwrap();
let msg = TestMessage::new(b"encrypted hello");
let raw = tx_lc.prepare_send(&msg).unwrap();
+20 -16
View File
@@ -1179,12 +1179,10 @@ impl LinkManager {
return;
}
if active.channel.is_none()
&& Self::ensure_link_channel(active, link_id).is_none()
{
if active.channel.is_none() {
tracing::debug!(
link_id = hex::encode(link_id),
"channel data received before session keys were available"
"channel data received before the application opened a channel"
);
return;
}
@@ -5150,16 +5148,17 @@ mod tests {
let mut lm = LinkManager::new(transport_tx, event_rx, [0xBB; 16], None);
let (channel_tx, mut channel_rx) = mpsc::channel(4);
lm.set_channel_message_channel(channel_tx);
let mut receiver_channel =
rns_protocol::channel::LinkChannel::new_encrypted(link_id, receiver_rtt, receiver_keys);
receiver_channel
.register_message_type(TEST_CHANNEL_MSG_TYPE)
.unwrap();
lm.active_links.insert(
link_id,
ActiveLink {
link: receiver_link,
_interface_id: 1,
channel: Some(rns_protocol::channel::LinkChannel::new_encrypted(
link_id,
receiver_rtt,
receiver_keys,
)),
channel: Some(receiver_channel),
inbound_resources: HashMap::new(),
outbound_resources: HashMap::new(),
outbound_split_queues: HashMap::new(),
@@ -5234,6 +5233,14 @@ mod tests {
let mut lm_b = LinkManager::new(tx_b, event_rx_b, [0xB1; 16], None);
lm_b.active_links
.insert(link_id, active_link_entry(responder));
lm_a.get_channel(&link_id)
.unwrap()
.register_message_type(TEST_CHANNEL_MSG_TYPE)
.unwrap();
lm_b.get_channel(&link_id)
.unwrap()
.register_message_type(TEST_CHANNEL_MSG_TYPE)
.unwrap();
// A -> B channel data; B proves it back.
lm_a.send_channel_message(&link_id, &TestChannelNoop)
@@ -5345,7 +5352,7 @@ mod tests {
}
#[test]
fn channel_packet_opens_channel_before_proof_and_dispatch() {
fn channel_packet_without_open_channel_is_dropped() {
let (sender_link, receiver_link, _identity_key) = handshaken_link_pair_with_identity();
let link_id = receiver_link.link_id;
let mut sender_channel = rns_protocol::channel::LinkChannel::new_encrypted(
@@ -5392,12 +5399,9 @@ mod tests {
lm.handle_inbound_packet(&raw, 1);
let delivered = channel_rx.try_recv().expect("channel message dispatched");
assert_eq!(delivered.link_id, link_id);
assert_eq!(delivered.msg_type, TEST_CHANNEL_MSG_TYPE);
let outbound = transport_rx.try_recv().expect("channel proof queued");
assert!(matches!(outbound, TransportMessage::Outbound(_)));
assert!(lm.active_links.get(&link_id).unwrap().channel.is_some());
assert!(channel_rx.try_recv().is_err());
assert!(transport_rx.try_recv().is_err());
assert!(lm.active_links.get(&link_id).unwrap().channel.is_none());
}
#[test]