1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.
//! Outer-envelope (`DeRecMessage`) parsing, building, and trace-id plumbing.
//!
//! # Security: no upper bound on inbound *byte size*
//!
//! This section is **only about how many bytes the parser will accept**.
//! Semantic validation of decoded content — transport-protocol scheme
//! consistency, contact-mode/field invariants, timestamp pairing,
//! `(nonce, secret_id, version)` binding for verification responses,
//! etc. — runs at the primitive `extract` layer and at the FFI/WASM
//! seams regardless of how big or small the envelope is. Don't read
//! this note as "the library skips validation"; it skips one specific
//! thing: a maximum-byte-length check.
//!
//! Every function in this module that ingests peer wire bytes —
//! [`extract_inner_message`], [`extract_inner_pairing_message`],
//! [`extract_inner_plaintext_message`], [`apply_trace_id`], [`read_trace_id`] —
//! parses whatever it's handed. No caller-side size cap is enforced anywhere
//! in the library, by design: legitimate envelopes span tens of bytes (acks)
//! through many MB (replica-secret sync), so any cap tight enough to be useful
//! against DoS would risk silently truncating a legitimate share or secret and
//! making the secret unrecoverable.
//!
//! The application's transport layer MUST bound inbound message size at a
//! ceiling consistent with its deployment's max secret size, helper count,
//! and replica fan-out before handing bytes to the library. See the security
//! note on [`crate::protocol::DeRecProtocol::process`] for the canonical
//! statement of this contract.
//!
//! Malformed bytes surface as [`crate::Error::ProtobufDecode`]; recursion
//! depth is bounded by `prost`'s decoder, and the DeRec schema is shallow
//! enough that no additional caller-side recursion limit is needed.
//!
//! # Security: replay detection belongs to the transport
//!
//! The library processes a copy of a message as often as it is delivered
//! within the inbound window. Detecting copies, of what it received and of
//! what it sent, is the application's job; see the security note on
//! [`crate::protocol::DeRecProtocol::process`]. [`encrypted_payload`] reads
//! the part of an envelope that is safe to key that detection on.
use crate::;
use channel;
use ;
use ;
use Message;
pub use *;
pub use *;
/// Decrypt and decode a channel envelope's payload under `shared_key`, and
/// check that the protocol version it carries equals the envelope's.
pub
/// Decode a plaintext `PrePair` envelope's payload, and check that the
/// protocol version it carries equals the envelope's.
pub
/// Decrypt a channel envelope's `message` under `shared_key`, without
/// decoding the plaintext.
///
/// Success authenticates the sender as the holder of the channel key, which
/// is what separates an undecryptable message (no one to answer) from an
/// authenticated one whose body cannot be decoded (answered with an
/// `ErrorResponseMessage`).
pub
/// Re-stamp the `trace_id` field on an already-produced DeRecMessage envelope.
///
/// The envelope's outer layer is plaintext, so this just decodes the
/// `DeRecMessage` protobuf, overwrites the `trace_id` field, and re-encodes.
/// The inner encrypted `message` payload is untouched — no crypto work.
///
/// Useful for consumers using primitives directly: the `*::request::produce`
/// functions emit envelopes with `trace_id = 0` (the protobuf default), so
/// callers who want correlation can produce + then [`apply_trace_id`] to set
/// their own. The orchestrator (`DeRecProtocol`) already does this
/// automatically on every outbound request.
/// Draw a fresh correlation token for an outbound request envelope.
///
/// Mirrors [`DeRecMessageBuilder::auto_trace_id`] but for the case where the
/// envelope was built by a primitive and is then re-stamped via
/// [`apply_trace_id`]. A `0` return is indistinguishable from "unset", so
/// there is a 2^-64 chance of drawing a token downstream code might read as
/// "no correlation requested" — not worth coding around.
pub
/// Read the `trace_id` field off an inbound DeRecMessage envelope without
/// touching the encrypted inner payload.
///
/// Pair with [`apply_trace_id`] for request/response correlation when
/// driving the protocol through primitives directly.
/// Read the encrypted `message` field off an envelope, without the channel
/// key.
///
/// This is the part of an envelope an application can key duplicate
/// detection on. It cannot be changed without failing decryption, and every
/// message is sealed under a fresh random nonce, so it is the same for every
/// copy of one message and different for every other message, even one with
/// the same body. The cleartext fields around it (`sequence`, `traceId`)
/// are not authenticated, and protobuf accepts more than one encoding of the
/// same envelope, so a copy can arrive as different bytes: a hash of the raw
/// envelope does not identify a message. How to key on these bytes (as they
/// are, or hashed) and for how long is the application's choice; see the
/// security note on [`crate::protocol::DeRecProtocol::process`].
///
/// The plaintext `PrePair` leg of `HashedKeys` pairing is the exception: its
/// `message` field is not encrypted, so a copy re-encoded inside it reads
/// differently. That leg is not authenticated at all; the contact's nonce is
/// what binds it.
///
/// # Errors
///
/// [`Error::ProtobufDecode`] when the bytes are not an envelope.
/// Decrypt and decode a pairing envelope's payload, returning the protocol
/// version it carries alongside it for the caller to check against the
/// envelope's.
pub
/// Decodes the inner [`MessageBody`] from a plaintext envelope.
///
/// Used by the `HashedKeys` pre-pair leg, where the `message` field of the
/// outer [`DeRecMessage`] envelope carries a serialized `MessageBody`
/// directly — no encryption, because no shared or asymmetric key exists
/// yet at that point in the protocol.
///
/// Counterpart to the plaintext PrePair envelope builder path —
/// see [`crate::primitives::pairing::request::produce_pre_pair_request`]
/// and [`crate::primitives::pairing::response::produce_pre_pair`] for the
/// producers that emit the bytes this function decodes.