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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! Your static keypair, and the seam that lets it live in hardware.
//!
//! [`SoftwareIdentity`] is the in-memory default; implementing
//! [`Identity`] yourself puts the key behind a Secure Enclave or an HSM.
//! It is the `I` in §16.4's `core::Endpoint<I: Identity>`.
//!
//! §16.4 parameterises the endpoint core over an identity because "the
//! mid-state map is typed over `I::Provider`": a parked staged chain owns a
//! suspended hiss handshake, and that type carries the provider. So the
//! seam is not "a key" — it is the pair *(provider, static private handle)*
//! a single handshake consumes.
//!
//! # Why [`Identity::open`] is a factory
//!
//! Three facts about hiss 0.3.2 force it:
//!
//! 1. Every handshake instance **consumes a provider by value** —
//! `IK::initiator(provider, …)` and `IK::responder(provider, …)`.
//! 2. Each also needs our static private key **by value** — the responder
//! at construction, the initiator at `write_message_1`.
//! 3. `CryptoKeyProvider::PrivateKey` is deliberately **not** `Clone`
//! ("secret keys should not be silently duplicated").
//!
//! An endpoint runs many handshakes at once — up to `INTRO_QUEUE_CAP`
//! parked mid-states, each of which §17.5 says "holds the endpoint's static
//! provider". One provider and one key cannot serve them, so the seam mints
//! a fresh pair per handshake. `&self`, so it can be called while the
//! introduction queue is borrowed; fallible, because a hardware call can
//! fail and the alternative is a panic on the accept path.
//!
//! Nothing here asks for key *bytes*. That is the whole point: an iOS
//! Secure Enclave identity returns a retained `SecKey` handle — a refcount
//! bump, not a copy — so S21's "key material is non-exportable" survives
//! the seam intact.
//!
//! # No `Send`, deliberately
//!
//! Neither [`Identity`] nor its associated types carry a `Send` bound, and
//! none may be added: an enclave-backed key is not `Send`, and a bound here
//! would exclude the case the seam exists for (S21). `hiss::provider::DhProvider`
//! itself carries no `Send` bound either, so the requirement is
//! satisfiable end to end.
//!
//! # `DhProviderAsync` is excluded on purpose
//!
//! [`Identity::Provider`] is bounded on `DhProvider` — the **synchronous**
//! surface — and `hiss::provider::DhProviderAsync` is deliberately not
//! accommodated. Three independent reasons, so no one of them lapsing
//! reopens the question:
//!
//! 1. **hiss cannot drive it.** The state machines `noise!` generates are
//! bounded `CP: DhProvider<Curve>` at every entry point; an async-only
//! backend cannot run an IK handshake through hiss at all.
//! 2. **§16.4 forbids the shape.** The core's staged verbs are ratified
//! *synchronous*. An async DH would make `read_identity` /
//! `authenticate` / `accept` return futures inside the core, which is a
//! spec change rather than an implementation choice.
//! 3. **It would reintroduce the very bound this seam exists to avoid.**
//! `DhProviderAsync::dh_async` returns `impl Future<Output = …> + Send`
//! — a `Send` requirement written into the trait. Accommodating it would
//! import `Send` into the one seam built to be free of it.
//!
//! Reason 3 is the counter-intuitive one, and it is why "support both
//! surfaces" is the wrong instinct here.
use RefCell;
use fmt;
use Curve;
use ;
use P256;
use ;
use ChaCha20Rng;
use ;
use crate;
/// The DH curve an identity's suite uses.
pub type CurveOf<I> = Curve;
/// The static public key type an identity's suite uses — §2.4's canonical
/// encoding is `AsRef<[u8]>` on it.
pub type PublicKeyOf<I> = PublicKey;
/// The private-key handle an identity's provider mints.
pub type PrivateKeyOf<I> = PrivateKey;
/// The pre-shared key an identity's suite requires — `()` on every
/// [`channel!`](crate::channel) suite, [`hiss::psk::Psk`] on a
/// [`channel_psk!`](crate::channel_psk) one. Ruling 280.
pub type PskOf<I> = Psk;
/// An endpoint's long-term static identity, as a **factory** for the
/// per-handshake `(provider, static private key)` pair hiss consumes.
///
/// See the [module docs](self) for why this is a factory rather than a key
/// holder, and for why the asynchronous DH surface is deliberately absent.
// ═══════════════════════════════════════════════════════════════════════
// The software identity
// ═══════════════════════════════════════════════════════════════════════
/// Why a [`SoftwareIdentity`] could not be built or opened.
/// The crates.io-only default identity: a P-256 static held in memory.
///
/// **P-256 specifically**, not any suite's curve. hiss exposes no generic
/// "import a private key from bytes" seam — `P256r1PrivateKey::from_bytes`
/// is the concrete one — and [`Identity::open`] must hand out a fresh
/// owned key per handshake, which a non-`Clone` handle can only satisfy by
/// re-import. The suite's *cipher* and *hash* stay generic; only the curve
/// is fixed. A backend for another curve implements [`Identity`] directly.
///
/// # The RNG is a seed source, not the handshake RNG
///
/// `open()` draws a **fresh 32-byte sub-seed** from `R` and builds the
/// handshake's `EphemeralOnly` around a `ChaCha20Rng` seeded with it.
/// Cloning `R` into each handshake would be a catastrophe rather than a
/// convenience: two clones of a seeded RNG produce the *same* ephemeral,
/// and §5.5 requires a completely fresh ephemeral on every retransmit.
/// Drawing a sub-seed advances the parent, so every handshake gets a
/// distinct stream and a seeded parent still makes the whole sequence
/// reproducible.
///
/// # `R` must be seeded from OS entropy in production
///
/// **This type takes no OS default, and that is the one thing to know
/// about it.** `R` is not a convenience RNG: it is the source of the
/// static scalar in [`generate`](Self::generate) *and* of every handshake
/// ephemeral this identity ever produces, in both roles, through the
/// sub-seed `open()` draws. Reproducibility is a **testing** property
/// here, exactly as
/// [`EndpointBuilder::rng_seed`](crate::shell::EndpointBuilder::rng_seed) says it
/// is for §16.6's endpoint RNG — but that one defaults to OS entropy when
/// the caller says nothing, and this one has no default to fall back to,
/// because `R` is a constructor argument. The asymmetry is a trap: the
/// less critical RNG is the one that is safe by default.
///
/// What a predictable `R` costs, stated separately because the two
/// constructors lose different things:
///
/// - [`generate`](Self::generate) draws the **static private key** from
/// `R`. Predict `R` and the identity itself is recoverable — total
/// compromise, indefinitely, with no session to expire.
/// - [`from_scalar`](Self::from_scalar) keeps the static safe but still
/// feeds every ephemeral. Predict `R` and an initiator's ephemeral
/// private key follows, so `es = DH(e_i, S_r)` is computable from public
/// data alone — which decrypts msg1's static field and its timestamp.
/// That voids §5.3's stated guarantee that those are *"opaque to any
/// passive observer"*, and forward secrecy with them. (`ss` still blocks
/// a full transcript break, so this is identity and metadata exposure,
/// not immediate session compromise.)
///
/// **The `R: CryptoRng` bound does not carry this.** `ChaCha20Rng` is a
/// CSPRNG *given an unpredictable seed*; the bound describes the
/// algorithm and says nothing about where the seed came from.
///
/// The production shape, which is also what
/// [`EndpointBuilder::build`](crate::shell::EndpointBuilder::build) does for the
/// endpoint RNG:
///
/// ```no_run
/// use rand_chacha::ChaCha20Rng;
/// use rand_chacha::rand_core::SeedableRng;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut seed = [0u8; 32];
/// getrandom::fill(&mut seed)?;
/// let rng = ChaCha20Rng::from_seed(seed);
/// // …then `SoftwareIdentity::<MySuite>::generate(rng)`.
/// # let _ = rng;
/// # Ok(())
/// # }
/// ```
///
/// `rand_core` 0.10 ships no `OsRng` of its own (it moved to `rand` as
/// `SysRng`), so the two lines above — or `rand`'s equivalent — are the
/// whole of it. A seeded `R` belongs in tests, where the `testutil`
/// fabric uses exactly that and is right to.