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
//! Wrapping a pairing payload under the CPace output.
//!
//! The code-based flows never send two values in clear: the new long-term PSK and, in the
//! Dynamic Pairing Code Flow, the commitment opening `nonce_B`. Each is sealed under the PAKE's
//! intermediate session key with a key derived per field:
//!
//! ```text
//! K_wrap = SHA-256(label || sid || ISK)
//! sealed = AEAD(K_wrap, nonce = 12 zero bytes, aad = empty, value)
//! ```
//!
//! The concatenation is raw - no separator, no length prefix, no NUL terminator - and `ISK` is
//! the 64-byte CPace intermediate session key. The label separates the two fields, so a
//! `wrapped_nonce_B` cannot be replayed as a `wrapped_psk`; the `sid` binds the seal to one CPace
//! session, so a man in the middle that relays between two handshakes, whose differing `h` gives
//! each leg a different `sid`, derives a key on neither leg.
//!
//! A value is 32 bytes and its sealed form 48: the ciphertext plus a 16-byte authentication tag.
//! A payload that fails to authenticate is an error, never a zero or partially decrypted value,
//! and neither [`Debug`] output nor an error string ever carries the key, the value or the seal.
//!
//! # The AEAD is the connection's suite
//!
//! The spec seals with the AEAD of the connection's negotiated cipher suite, so every function
//! here takes a [`Suite`]: `25519_ChaChaPoly_SHA256` seals with ChaCha20-Poly1305 and
//! `25519_AESGCM_SHA256` with AES-256-GCM. Both are 32-byte-key, 12-byte-nonce, 16-byte-tag
//! ciphers, so `K_wrap` and the sealed length are the same under either; only the ciphertext
//! differs, and a value sealed under one suite does not open under the other.
//!
//! The ciphers come from the same resolver `snow` uses for the handshake, which is where the
//! dependency already sits. A build whose resolver lacks the suite's cipher returns an error and
//! never falls back to the other suite.
//!
//! The module is behind the `pairing-code` feature with [`pake`](super::pake): wrapping exists
//! only for the code flows.
use ;
use ;
use CipherChoice;
use ;
use Cipher;
use crateSuite;
/// The label that derives the key for `wrapped_psk`.
pub const PSK_LABEL: & = b"sendspin-pair-psk-wrap-v1";
/// The label that derives the key for `wrapped_nonce_B`.
pub const NONCE_LABEL: & = b"sendspin-pair-nonce-wrap-v1";
/// The length of a wrapped value: 32 bytes.
const VALUE_LEN: usize = 32;
/// The length of the AEAD's authentication tag: 16 bytes.
const TAG_LEN: usize = 16;
/// The length of a sealed value: ciphertext plus tag, the spec's 48-byte field.
const SEALED_LEN: usize = VALUE_LEN + TAG_LEN;
/// The nonce number every wrapping uses. A key is derived per field per session, so it seals
/// exactly one value and the zero nonce never repeats under the same key. Zero is the spec's
/// twelve zero bytes under either cipher: both render a `u64` into the last eight bytes, and
/// this value is zero in either byte order.
const WRAP_NONCE: u64 = 0;
/// Derives `K_wrap = SHA-256(label || sid || ISK)`.
///
/// `label` is [`PSK_LABEL`] or [`NONCE_LABEL`], `sid` the CPace session id exactly as the PAKE
/// used it, and `isk` the 64-byte CPace intermediate session key. The three are concatenated
/// raw; their lengths are fixed by the protocol, so no delimiter is needed. The suite does not
/// enter the derivation: both of Sendspin's suites use the same key with a different AEAD.
/// Seals a 32-byte value under `K_wrap` with the AEAD of `suite`, returning ciphertext and tag.
///
/// The nonce is twelve zero bytes and the associated data is empty, as the spec requires. A
/// key is used for one value only, which is what makes the fixed nonce safe.
///
/// # Errors
///
/// Fails when the build's cipher resolver has no AEAD for `suite`. That is the honest refusal:
/// sealing under the other suite's cipher would produce a field the peer cannot open.
///
/// # Example
///
/// ```
/// use syncspin_core::noise::Suite;
/// use syncspin_core::pairing::wrap::{unwrap, wrap};
///
/// // The key comes from `k_wrap` in a real flow; any 32 bytes will do here.
/// let key = [0x42; 32];
/// let value = [0x24; 32];
/// let sealed = wrap(Suite::ChaChaPoly, &key, &value).expect("the cipher is available");
/// assert_eq!(unwrap(Suite::ChaChaPoly, &key, &sealed).expect("round trip"), value);
/// ```
/// Opens a 48-byte sealed value under `K_wrap` with the AEAD of `suite`, returning the value.
///
/// # Errors
///
/// Fails when the tag does not authenticate: a wrong key - a different label, `sid` or `ISK` - a
/// seal made under the other suite, or any changed byte of the ciphertext or tag. It also fails
/// when the build's cipher resolver has no AEAD for `suite`. The error names no key, value or
/// seal bytes.
/// Builds the suite's AEAD for `key`.
///
/// The resolver is `snow`'s default one, the same one the handshake uses, so a build that has a
/// suite for Noise has it here as well. A missing cipher is an error and not a reason to seal
/// under the other suite. The panic messages and error strings carry no key material: `set`
/// cannot fail on a 32-byte key.