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
//! 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 suite's
//!
//! The spec seals with the AEAD of the connection's negotiated cipher suite. Sendspin defines
//! two suites - `25519_ChaChaPoly_SHA256` and `25519_AESGCM_SHA256` - and this crate offers the
//! first only ([`NOISE_SUITE`](crate::messages::NOISE_SUITE)), so the AEAD here is
//! ChaCha20-Poly1305. Offering `25519_AESGCM_SHA256` would mean selecting the AEAD from the
//! suite rather than fixing it in this module.
//!
//! The module is behind the `pairing-code` feature with [`pake`](super::pake): wrapping exists
//! only for the code flows, and the default build keeps no AEAD of its own.
use ;
use ;
use ;
use ;
/// 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 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.
const NONCE_BYTES: = ;
/// 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.
/// Seals a 32-byte value under `K_wrap`, returning the 48-byte 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.
///
/// # Example
///
/// ```
/// 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];
/// assert_eq!(unwrap(&key, &wrap(&key, &value)).expect("round trip"), value);
/// ```
/// Opens a 48-byte sealed value under `K_wrap`, returning the 32-byte value.
///
/// # Errors
///
/// Fails when the tag does not authenticate: a wrong key - a different label, `sid` or `ISK` -
/// or any changed byte of the ciphertext or tag. The error names no key, value or seal bytes.
/// Builds the cipher for `key`.
///
/// `new_from_slice` only fails on a key of the wrong length, and `key` is exactly 32 bytes, so
/// the failure is unreachable. The panic message carries no key material.
/// Returns the fixed wrapping nonce.