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
// 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. The application's transport layer MUST detect
//! copies, of what it received and of what it sent, keyed on
//! [`message_id`]; see the security note on
//! [`crate::protocol::DeRecProtocol::process`] for the full contract.
use crate::;
use channel;
use ;
use ;
use Message;
use ;
pub use *;
pub use *;
/// 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.
/// The identity of one delivered message, read off its envelope without the
/// channel key. See [`message_id`].
/// Identify an envelope for replay and reflection detection.
///
/// Detecting a message delivered twice is the application's job, since it
/// owns the transport. What to keep, for how long, and that the record must
/// survive a restart is set out on
/// [`process`](crate::protocol::DeRecProtocol::process). `id` is the same for
/// every copy of one message and different for every other message:
///
/// - It covers only the encrypted `message` field. The cleartext fields
/// around it (`sequence`, `traceId`, the protocol version) are not
/// authenticated, and protobuf accepts more than one encoding of the same
/// envelope, so a copy can arrive as different bytes and still be
/// accepted. A hash of the raw envelope therefore does not identify it.
/// - Changing the `message` field fails decryption, and every message is
/// sealed under a fresh random nonce, so no other message, not even one
/// with the same body, has the same `id`.
///
/// `timestamp` is the envelope's cleartext send time. Once
/// [`process`](crate::protocol::DeRecProtocol::process) accepts the message
/// it is proven equal to the authenticated time inside it.
///
/// The plaintext `PrePair` leg of `HashedKeys` pairing is the exception: its
/// `message` field is not encrypted, so a copy re-encoded inside it gets a
/// different `id`. 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, and
/// [`Error::InvalidInput`] when it carries no timestamp, which the protocol
/// never accepts.
/// 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.