hiss 0.4.0

Static, type-level Noise Protocol Framework with pluggable hardware-backed crypto.
Documentation
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
//! Type-level Noise protocol framework.
//!
//! This module implements the [Noise Protocol Framework][noise] entirely
//! at the type level. The protocol descriptor, handshake pattern,
//! message sequence, pre-message requirements, and per-token state
//! transitions are all encoded in Rust's type system — the compiler
//! enforces protocol correctness at build time with zero runtime
//! dispatch.
//!
//! [noise]: https://noiseprotocol.org/noise.html
//!
//! # Architecture
//!
//! The design has four layers, each building on the one below:
//!
//! ## 1. Protocol descriptor — [`Noise<P, Cu, Ci, H>`]
//!
//! A zero-sized struct parameterised over four zero-sized type
//! arguments:
//!
//! | Parameter | Trait    | Example        | What it provides                |
//! |-----------|----------|----------------|---------------------------------|
//! | `P`       | [`Pattern`] | [`IKpsk1`](pattern::IKpsk1) | Token sequences, pre-messages |
//! | `Cu`      | [`Curve`]   | [`P256`]    | Key sizes, DH output length     |
//! | `Ci`      | [`Cipher`]  | [`ChaChaPoly`], [`AesGcm`] | Tag size, AEAD operations    |
//! | `H`       | [`Hash`]    | [`Blake2b`] | Hash length, HMAC, HKDF         |
//!
//! A type alias pins the protocol for an entire application:
//!
//! ```
//! use hiss::noise::*;
//!
//! type Channel = Noise<pattern::IKpsk1, P256, ChaChaPoly, Blake2b>;
//!
//! let proto = Channel::new();
//! assert_eq!(proto.to_string(), "Noise_IKpsk1_P256_ChaChaPoly_BLAKE2b");
//! ```
//!
//! Because every component is a ZST, `Noise` itself is zero-sized and
//! all sizes are available as `const` at compile time:
//!
//! ```
//! # use hiss::noise::*;
//! # type Channel = Noise<pattern::IKpsk1, P256, ChaChaPoly, Blake2b>;
//! assert_eq!(Channel::PUBLIC_KEY_SIZE, 65);   // P-256 SEC1 uncompressed
//! assert_eq!(Channel::TAG_SIZE, 16);           // Poly1305
//! assert_eq!(Channel::HASH_LEN, 64);           // BLAKE2b
//! assert_eq!(std::mem::size_of::<Channel>(), 0);
//! ```
//!
//! ## 2. Token Cons-lists — [`Cons`], [`Nil`], [`Message`]
//!
//! Handshake patterns are expressed as nested type-level linked lists.
//! Each handshake token ([`E`], [`S`], [`Es`], [`Ee`], [`Se`],
//! [`Psk`]) is a ZST. Tokens within a message are chained with
//! [`Cons<Token, Rest>`], terminated by [`Nil`]. Messages carry a
//! direction ([`ToResponder`] or [`ToInitiator`]) and their token
//! list. The full pattern is a Cons-list of Messages.
//!
//! For example, IKpsk1 encodes as:
//!
//! ```text
//! PreMessages:
//!   Cons<Message<ToInitiator, Cons<S, Nil>>, Nil>                          ← s
//!
//! Messages:
//!   Cons<
//!     Message<ToResponder, Cons<E, Cons<Es, Cons<S, Cons<Ss, Cons<Psk, Nil>>>>>>,  → e, es, s, ss, psk
//!     Cons<
//!       Message<ToInitiator, Cons<E, Cons<Ee, Cons<Se, Nil>>>>,                    ← e, ee, se
//!       Nil
//!     >
//!   >
//! ```
//!
//! The compiler monomorphises the handshake state machine over these
//! types. Each token method peels one element from the Cons-list,
//! so the type system tracks exactly where we are in the protocol.
//! Calling tokens out of order, skipping a token, or processing
//! messages in the wrong direction is a compile error.
//!
//! ## 3. Pre-messages
//!
//! A pre-message is a static key both parties already hold when the
//! handshake begins. [`noise!`](crate::noise!) reads the pattern's
//! pre-message lines at expansion time and turns each one into a
//! **parameter of the role's constructor**, after `provider` and
//! `prologue` and in pattern order — so every key the pattern requires
//! up front is supplied in the one call that starts the handshake, and
//! omitting one is a missing argument, not a runtime error. Which key
//! a role supplies depends on whether the pre-message travels in that
//! role's own sending direction:
//!
//! | Pre-message | Role          | Parameter       | What you supply              |
//! |-------------|---------------|-----------------|------------------------------|
//! | `← s`       | [`Initiator`] | `remote_static` | The peer's static public key |
//! | `← s`       | [`Responder`] | `static_key`    | Our own static private key   |
//! | `→ s`       | [`Initiator`] | `static_key`    | Our own static private key   |
//! | `→ s`       | [`Responder`] | `remote_static` | The peer's static public key |
//!
//! A constructor taking a `static_key` returns `Result`, because
//! deriving its public half goes through the provider and can fail; one
//! taking only a `remote_static` — or no pre-message at all — is
//! infallible.
//!
//! The [`Pattern::PreMessages`] Cons-list is the type-level record of
//! the same pre-messages: [`WellFormed`] walks it ahead of the message
//! list to check the pattern against Noise §7.3 at compile time.
//!
//! ## 4. The generated state machine
//!
//! [`noise!`](crate::noise!) turns a pattern into a type-state machine
//! with one method per handshake message. Each `write_message_N` returns
//! the finished message as a `[u8; MSGn_SIZE]`; each `read_message_N`
//! borrows one for the duration of the call. Calling them out of order,
//! or handing a reader a buffer of the wrong length, is a compile error.
//!
//! Nothing here performs I/O. Which message sizes exist, which keys each
//! method consumes, and which states expose `remote_static()` are all
//! decided by the pattern at expansion time — the compiler picks the
//! right one, with no `match`, no `if`, and no runtime check.
//!
//! # Compile-time message sizes
//!
//! Because every component size is a `const` — public key size from
//! the [`Curve`], tag size from the [`Cipher`], hash length from the
//! [`Hash`] — the exact byte size of every handshake message is
//! known at compile time (see [`noise_message_size!`](crate::noise_message_size)).
//! For `Noise_IKpsk1_P256_ChaChaPoly_BLAKE2b`:
//!
//! | Message | Contents                                   | Size (bytes)              |
//! |---------|--------------------------------------------|---------------------------|
//! | msg1    | `e_pub` + `encrypted(s_pub)` + payload tag | 65 + (65 + 16) + 16 = 162 |
//! | msg2    | `e_pub` + payload tag                      | 65 + 16 = 81              |
//!
//! # Pluggable crypto provider
//!
//! The generated state machine is generic over the crypto provider, so
//! the per-token DH and key generation can use any backend:
//!
//! - **Software** (`eccoxide`/`cryptoxide`) — resolves immediately.
//! - **Secure Enclave** (Apple Security framework) — the blocking
//!   Security-framework calls run on the calling thread; may prompt for
//!   biometric authentication.
//!
//! It takes a synchronous
//! [`DhProvider`](crate::provider::DhProvider); the
//! [`DhProviderAsync`](crate::provider::DhProviderAsync) refinement
//! exists for backends whose work is awaitable.
//!
//! # Usage
//!
//! See the [crate-level Quickstart](crate) for a complete worked example,
//! and [`noise!`](crate::noise!) for the DSL and the full generated API.

pub(crate) mod buffers;
pub mod cipher;
pub mod cipher_state;
pub mod curve;
pub mod datagram;
pub mod error;
pub(crate) mod handshake;
pub mod hash;
pub mod pattern;
pub(crate) mod process;
pub mod role;
#[cfg(any(target_os = "macos", target_os = "ios", test))]
pub(crate) mod seal;
pub mod session_id;
#[doc(hidden)]
pub mod support;
pub mod symmetric_state;
pub mod tokens;
pub mod transport;
pub mod well_formed;

pub use self::cipher::{AesGcm, AesGcmKey, ChaChaPoly, ChaChaPolyKey, Cipher};
pub use self::cipher_state::CipherState;
pub use self::curve::{Curve, DhCurve, P256, X448, X25519};
pub use self::datagram::{DatagramRecv, DatagramSend};
pub use self::error::HandshakeError;
// Protocol re-exported from this module (defined below on Noise).
pub use self::hash::{Blake2b, Blake2s, Hash, Sha256, Sha512};
// Pattern markers stay namespaced under `noise::pattern::{N, K, …}`; only the
// `Pattern` trait is re-exported at the root. There are deliberately no
// suite-bound protocol aliases here: `N`, `XX`, … are Noise *patterns*, not
// whole `Noise<P, Cu, Ci, H>` protocols, so callers spell the protocol out (or
// alias it locally) rather than rely on a root name that conflates the two.
pub use self::pattern::Pattern;
pub use self::role::{Initiator, Responder, Role};
pub use self::session_id::SessionId;
pub use self::symmetric_state::SymmetricState;
pub use self::tokens::*;
pub use self::transport::{Transport, TransportRecv, TransportSend};
pub use self::well_formed::WellFormed;

use std::fmt;
use std::marker::PhantomData;

/// A fully parameterised Noise protocol descriptor.
///
/// Zero-sized — carries no runtime data. All properties are derived
/// from the trait bounds on `P`, `Cu`, `Ci`, and `H`.
pub struct Noise<P, Cu, Ci, H> {
    _pattern: PhantomData<fn() -> P>,
    _curve: PhantomData<fn() -> Cu>,
    _cipher: PhantomData<fn() -> Ci>,
    _hash: PhantomData<fn() -> H>,
}

impl<P, Cu, Ci, H> Noise<P, Cu, Ci, H> {
    /// Create a new protocol descriptor.
    pub const fn new() -> Self {
        Self {
            _pattern: PhantomData,
            _curve: PhantomData,
            _cipher: PhantomData,
            _hash: PhantomData,
        }
    }
}

impl<P: Pattern, Cu: DhCurve, Ci: Cipher, H: Hash> Noise<P, Cu, Ci, H> {
    /// DH output length in bytes.
    pub const DHLEN: usize = Cu::DHLEN;

    /// Serialised public key size in bytes.
    pub const PUBLIC_KEY_SIZE: usize = Cu::PUBLIC_KEY_SIZE;

    /// AEAD authentication tag size in bytes.
    pub const TAG_SIZE: usize = Ci::TAG_SIZE;

    /// Hash output length in bytes (also the chaining key size).
    pub const HASH_LEN: usize = H::HASH_LEN;

    /// Number of handshake messages in the pattern.
    pub const NUM_MESSAGES: usize = P::NUM_MESSAGES;
}

impl<P: Pattern, Cu: Curve, Ci: Cipher, H: Hash> fmt::Display for Noise<P, Cu, Ci, H> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Noise_{}_{}_{}_{}", P::NAME, Cu::NAME, Ci::NAME, H::NAME)
    }
}

impl<P, Cu, Ci, H> Default for Noise<P, Cu, Ci, H> {
    fn default() -> Self {
        Self::new()
    }
}

/// A fully specified Noise protocol — pattern, curve, cipher, and hash.
///
/// Implemented by [`Noise<P, Cu, Ci, H>`]. Used as a single type
/// parameter by the generated state machines instead of spreading four
/// separate generic parameters.
pub trait Protocol {
    /// The handshake pattern (e.g. [`IKpsk1`](pattern::IKpsk1)).
    type Pattern: Pattern;
    /// The DH curve (e.g. [`P256`]).
    type Curve: DhCurve;
    /// The AEAD cipher — [`ChaChaPoly`] or [`AesGcm`].
    type Cipher: Cipher;
    /// The hash function (e.g. [`Blake2b`]).
    type Hash: Hash;
}

impl<P: WellFormed, Cu: DhCurve, Ci: Cipher, H: Hash> Protocol for Noise<P, Cu, Ci, H> {
    type Pattern = P;
    type Curve = Cu;
    type Cipher = Ci;
    type Hash = H;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::curve::p256::{P256r1PrivateKey, P256r1PublicKey};
    use crate::noise_message_size;
    use crate::provider::EphemeralOnly;
    use crate::provider::ProviderExt;
    use crate::psk::Psk;
    use rand::rngs::StdRng;

    use std::num::NonZeroU64;

    // The zero-sized protocol descriptors, used by the `descriptor_string`
    // and `sizes` tests below — these exercise `Noise<P, Cu, Ci, H>` itself.
    type Channel = Noise<pattern::IKpsk1, P256, ChaChaPoly, Blake2b>;
    type NoiseSeal = Noise<pattern::N, P256, ChaChaPoly, Blake2b>;
    type NoiseK = Noise<pattern::K, P256, ChaChaPoly, Blake2b>;
    type NoiseKpsk0 = Noise<pattern::Kpsk0, P256, ChaChaPoly, Blake2b>;

    // The generated state machines the handshake tests drive. Named for
    // their patterns: the identifier becomes `Pattern::NAME` and reaches the
    // protocol name that seeds the initial handshake hash.
    hiss::noise! { pub N<P256, ChaChaPoly, Blake2b>      { <- s ... -> e, es } }
    hiss::noise! { pub K<P256, ChaChaPoly, Blake2b>      { -> s <- s ... -> e, es, ss } }
    hiss::noise! { pub Kpsk0<P256, ChaChaPoly, Blake2b>  { -> s <- s ... -> psk, e, es, ss } }
    hiss::noise! {
        pub IKpsk1<P256, ChaChaPoly, Blake2b> { <- s ... -> e, es, s, ss, psk <- e, ee, se }
    }

    // An AES-GCM suite, for the auto-trait assertion below: `AesGcmKey` is
    // the one `Cipher::Key` with a foreign type inside it, so it is where a
    // lost `Send`/`Sync` would show up first.
    hiss::noise! {
        pub IkAes<P256, AesGcm, Sha256> { <- s ... -> e, es, s, ss <- e, ee, se }
    }

    /// Naming a cipher must never be what stops a session type from crossing
    /// a thread boundary — the examples move handshake and transport state
    /// into `tokio::spawn` and `std::thread::spawn`, so this is load-bearing
    /// rather than hypothetical.
    ///
    /// Two things could take `Send`/`Sync` away and both are closed here:
    /// the associated `Cipher::Key`, which the trait bounds
    /// `Send + Sync + 'static`, and the `PhantomData` marker, which is
    /// `PhantomData<fn() -> Ci>` so that a `!Send` *cipher marker* cannot
    /// poison a state either. Compile-time only; the body runs nothing.
    #[test]
    fn session_types_are_send_and_sync() {
        fn assert_send_sync<T: Send + Sync>() {}

        assert_send_sync::<Transport<IkAes>>();
        assert_send_sync::<DatagramRecv<IkAes>>();
        assert_send_sync::<DatagramSend<IkAes>>();
        assert_send_sync::<CipherState<AesGcm>>();

        assert_send_sync::<Transport<IKpsk1>>();
        assert_send_sync::<DatagramRecv<IKpsk1>>();
        assert_send_sync::<DatagramSend<IKpsk1>>();
        assert_send_sync::<CipherState<ChaChaPoly>>();
    }

    /// Complete an IKpsk1 handshake hiss↔hiss and hand back both
    /// transports.
    ///
    /// The whole exchange is two calls a side, so the tests that only need
    /// a live channel say that rather than restating the token sequence.
    fn complete_ikpsk1(psk: &Psk) -> (Transport<IKpsk1>, Transport<IKpsk1>) {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());
        let initiator_static = provider.generate::<P256>().unwrap();
        let responder_static = provider.generate::<P256>().unwrap();
        let responder_pub = provider.public(&responder_static).unwrap();

        let (msg1, i_hs) = IKpsk1::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_pub,
        )
        .write_message_1(initiator_static, psk)
        .unwrap();

        let r_hs = IKpsk1::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_static,
        )
        .unwrap()
        .read_message_1(&msg1, psk)
        .unwrap();

        let (msg2, r_transport) = r_hs.write_message_2().unwrap();
        let i_transport = i_hs.read_message_2(&msg2).unwrap();
        (i_transport, r_transport)
    }

    #[test]
    fn descriptor_string() {
        let proto = Channel::new();
        assert_eq!(proto.to_string(), "Noise_IKpsk1_P256_ChaChaPoly_BLAKE2b");
    }

    #[test]
    fn n_descriptor_string() {
        let proto = NoiseSeal::new();
        assert_eq!(proto.to_string(), "Noise_N_P256_ChaChaPoly_BLAKE2b");
    }

    #[test]
    fn sizes() {
        assert_eq!(Channel::DHLEN, 32);
        assert_eq!(Channel::PUBLIC_KEY_SIZE, 65);
        assert_eq!(Channel::TAG_SIZE, 16);
        assert_eq!(Channel::HASH_LEN, 64);
        assert_eq!(Channel::NUM_MESSAGES, 2);
    }

    #[test]
    fn n_sizes() {
        assert_eq!(NoiseSeal::NUM_MESSAGES, 1);
        assert_eq!(size_of::<NoiseSeal>(), 0);
    }

    #[test]
    fn zero_sized() {
        assert_eq!(size_of::<Channel>(), 0);
    }

    // ── Noise N seal/open test ────────────────────────────────────

    /// Noise N one-way seal: encrypt data to a known public key,
    /// then open it with the corresponding private key.
    #[test]
    fn noise_n_seal_open() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        // The "recipient" — in practice, the device's own Secure Enclave key.
        let recipient_static = provider.generate::<P256>().unwrap();
        let recipient_pub = provider.public(&recipient_static).unwrap();

        let psk_to_seal = Psk::from_bytes([0x42; 32]);

        // ── Seal (initiator side) ─────────────────────────────────
        // `-> e, es` is N's only message and also its last, so writing it
        // hands back the finished message and the transport together.
        let (msg, mut transport) = N::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            recipient_pub,
        )
        .write_message_1()
        .unwrap();

        // msg = ephemeral public key (65) + payload tag (16) = 81 bytes
        assert_eq!(N::MSG1_SIZE, 81);

        // Encrypt the PSK as a transport payload.
        let mut sealed = [0u8; 64]; // 32 + 16 tag
        let sealed_len = transport.send(psk_to_seal.as_bytes(), &mut sealed).unwrap();

        // ── Open (responder side) ─────────────────────────────────
        let mut transport = N::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            recipient_static,
        )
        .unwrap()
        .read_message_1(&msg)
        .unwrap();

        let mut opened = [0u8; 32];
        let opened_len = transport
            .receive(&sealed[..sealed_len], &mut opened)
            .unwrap();

        assert_eq!(opened_len, 32);
        assert_eq!(opened, *psk_to_seal.as_bytes());
    }

    // ── Noise N tampered handshake ─────────────────────────────────

    #[test]
    fn noise_n_tampered_ephemeral_rejected() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        let recipient_static = provider.generate::<P256>().unwrap();
        let recipient_pub = provider.public(&recipient_static).unwrap();

        let (mut tampered, _transport) = N::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            recipient_pub,
        )
        .write_message_1()
        .unwrap();
        // Flip a byte in the ephemeral public key.
        tampered[1] ^= 0xFF;

        // The tampered ephemeral either fails to decode as a curve point or
        // yields the wrong shared secret, which fails the payload tag.
        // Either way the read rejects it; there is no partial state to
        // inspect, since the message is processed as a whole.
        let outcome = N::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            recipient_static,
        )
        .unwrap()
        .read_message_1(&tampered);
        assert!(outcome.is_err());
    }

    #[test]
    fn noise_n_tampered_tag_rejected() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        let recipient_static = provider.generate::<P256>().unwrap();
        let recipient_pub = provider.public(&recipient_static).unwrap();

        let (mut tampered, _transport) = N::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            recipient_pub,
        )
        .write_message_1()
        .unwrap();
        // Flip a byte in the payload tag (last 16 bytes).
        tampered[N::MSG1_SIZE - 1] ^= 0xFF;

        // The tag is corrupted, so the read must fail at DecryptAndHash.
        let outcome = N::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            recipient_static,
        )
        .unwrap()
        .read_message_1(&tampered);
        assert!(matches!(outcome, Err(HandshakeError::DecryptionFailed)));
    }

    // ── Noise K / Kpsk0 tests ─────────────────────────────────────

    #[test]
    fn k_descriptor_string() {
        let proto = NoiseK::new();
        assert_eq!(proto.to_string(), "Noise_K_P256_ChaChaPoly_BLAKE2b");
    }

    #[test]
    fn kpsk0_descriptor_string() {
        let proto = NoiseKpsk0::new();
        assert_eq!(proto.to_string(), "Noise_Kpsk0_P256_ChaChaPoly_BLAKE2b");
    }

    #[test]
    fn k_sizes() {
        assert_eq!(NoiseK::NUM_MESSAGES, 1);
        assert_eq!(size_of::<NoiseK>(), 0);
    }

    #[test]
    fn kpsk0_sizes() {
        assert_eq!(NoiseKpsk0::NUM_MESSAGES, 1);
        assert_eq!(size_of::<NoiseKpsk0>(), 0);
    }

    /// Noise K authenticated seal: encrypt data from Alice to Bob,
    /// where both static keys are known. Open with Bob's key.
    #[test]
    fn noise_k_seal_open() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        // Alice (sender) and Bob (recipient) each have static keys.
        let alice_static = provider.generate::<P256>().unwrap();
        let alice_pub = provider.public(&alice_static).unwrap();

        let bob_static = provider.generate::<P256>().unwrap();
        let bob_pub = provider.public(&bob_static).unwrap();

        let payload: [u8; 32] = [0x42; 32];

        // ── Seal (Alice → Bob) ──────────────────────────────────
        // Pre-messages `-> s` (Alice) and `<- s` (Bob) are constructor
        // arguments, in pattern order.
        let (msg, mut transport) = K::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            alice_static,
            bob_pub,
        )
        .unwrap()
        .write_message_1()
        .unwrap();

        // msg = ephemeral public key (65) + payload tag (16) = 81 bytes
        assert_eq!(K::MSG1_SIZE, 81);

        let mut sealed = [0u8; 64]; // 32 + 16 tag
        let sealed_len = transport.send(&payload, &mut sealed).unwrap();

        // ── Open (Bob) ──────────────────────────────────────────
        // Same two pre-messages, mirrored: the peer's public first.
        let mut transport = K::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            alice_pub,
            bob_static,
        )
        .unwrap()
        .read_message_1(&msg)
        .unwrap();

        let mut opened = [0u8; 32];
        let opened_len = transport
            .receive(&sealed[..sealed_len], &mut opened)
            .unwrap();

        assert_eq!(opened_len, 32);
        assert_eq!(opened, payload);
    }

    /// Noise Kpsk0 authenticated seal with PSK binding.
    #[test]
    fn noise_kpsk0_seal_open() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        let alice_static = provider.generate::<P256>().unwrap();
        let alice_pub = provider.public(&alice_static).unwrap();

        let bob_static = provider.generate::<P256>().unwrap();
        let bob_pub = provider.public(&bob_static).unwrap();

        let psk = Psk::from_bytes([0xBB; 32]);
        let payload: [u8; 32] = [0x42; 32];

        // ── Seal (Alice → Bob) ──────────────────────────────────
        // `-> psk, e, es, ss`: the psk is a message token, so it is a
        // writer argument rather than a constructor one.
        let (msg, mut transport) = Kpsk0::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            alice_static,
            bob_pub,
        )
        .unwrap()
        .write_message_1(&psk)
        .unwrap();

        assert_eq!(Kpsk0::MSG1_SIZE, 81);

        let mut sealed = [0u8; 64];
        let sealed_len = transport.send(&payload, &mut sealed).unwrap();

        // ── Open (Bob) ──────────────────────────────────────────
        let mut transport = Kpsk0::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            alice_pub,
            bob_static,
        )
        .unwrap()
        .read_message_1(&msg, &psk)
        .unwrap();

        let mut opened = [0u8; 32];
        let opened_len = transport
            .receive(&sealed[..sealed_len], &mut opened)
            .unwrap();

        assert_eq!(opened_len, 32);
        assert_eq!(opened, payload);
    }

    /// Kpsk0 with wrong PSK fails to decrypt.
    #[test]
    fn noise_kpsk0_wrong_psk_fails() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        let alice_static = provider.generate::<P256>().unwrap();
        let alice_pub = provider.public(&alice_static).unwrap();

        let bob_static = provider.generate::<P256>().unwrap();
        let bob_pub = provider.public(&bob_static).unwrap();

        let psk = Psk::from_bytes([0xBB; 32]);
        let wrong_psk = Psk::from_bytes([0xCC; 32]);
        let payload: [u8; 32] = [0x42; 32];

        // Seal with correct PSK
        let (msg, mut transport) = Kpsk0::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            alice_static,
            bob_pub,
        )
        .unwrap()
        .write_message_1(&psk)
        .unwrap();

        let mut sealed = [0u8; 64];
        let _sealed_len = transport.send(&payload, &mut sealed).unwrap();

        // Open with wrong PSK — the wrong PSK produces different derived
        // keys, so the payload tag closing the message fails to verify.
        let result = Kpsk0::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            alice_pub,
            bob_static,
        )
        .unwrap()
        .read_message_1(&msg, &wrong_psk);
        assert!(result.is_err());
    }

    // ── IKpsk1 handshake flow tests ───────────────────────────────
    //
    // Full round-trip test that runs both initiator and responder
    // against each other with real keys and real crypto operations.

    /// IKpsk1 full round-trip handshake.
    ///
    /// ```text
    /// IKpsk1:
    ///   <- s                         (pre-message: responder's static known)
    ///   ...
    ///   -> e, es, s, ss, psk         (msg1: initiator → responder)
    ///   <- e, ee, se                 (msg2: responder → initiator)
    /// ```
    #[test]
    fn ikpsk1_round_trip() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        // ── Key generation ──────────────────────────────────────
        let initiator_static = provider.generate::<P256>().unwrap();
        let initiator_pub = provider.public(&initiator_static).unwrap();

        let responder_static = provider.generate::<P256>().unwrap();
        let responder_pub = provider.public(&responder_static).unwrap();

        // PSK established during the QR ceremony.
        let psk = Psk::from_bytes([0xAA; 32]);

        // ── Message 1: -> e, es, s, ss, psk (initiator sends) ──
        let (msg1, i_hs) = IKpsk1::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_pub,
        )
        .write_message_1(initiator_static, &psk)
        .unwrap();

        // msg1 = ephemeral (65) + encrypted static (65+16) + payload tag (16) = 162 bytes
        assert_eq!(IKpsk1::MSG1_SIZE, 162);

        // The first 65 bytes of msg1 are the initiator's ephemeral
        // public key (SEC1 uncompressed P-256).
        let initiator_e_from_wire =
            P256r1PublicKey::from_bytes(&msg1[..65]).expect("valid ephemeral in msg1");

        // ── Message 1 (responder receives) ──────────────────────
        let r_hs = IKpsk1::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_static,
        )
        .unwrap()
        .read_message_1(&msg1, &psk)
        .unwrap();

        // What the read recovered matches what was on the wire, and the
        // responder now knows the initiator's static key.
        assert_eq!(*r_hs.remote_ephemeral(), initiator_e_from_wire);
        assert_eq!(*r_hs.remote_static(), initiator_pub);

        // ── Message 2: <- e, ee, se (responder sends) ──────────
        let (msg2, mut r_transport) = r_hs.write_message_2().unwrap();

        // msg2 = ephemeral (65) + payload tag (16) = 81 bytes
        assert_eq!(IKpsk1::MSG2_SIZE, 81);

        // The first 65 bytes of msg2 are the responder's ephemeral
        // public key (SEC1 uncompressed P-256).
        P256r1PublicKey::from_bytes(&msg2[..65]).expect("valid ephemeral in msg2");

        // ── Message 2 (initiator receives) ──────────────────────
        let mut i_transport = i_hs.read_message_2(&msg2).unwrap();

        // ── Verify: both sides derived the same handshake hash ──
        assert_eq!(i_transport.session_id(), r_transport.session_id());

        // ── Verify: transport encryption works bidirectionally ──
        let plaintext = b"hello from initiator";
        let mut ct_buf = [0u8; 256];
        let ct_len = i_transport.send(plaintext, &mut ct_buf).unwrap();
        let mut pt_buf = [0u8; 256];
        let pt_len = r_transport.receive(&ct_buf[..ct_len], &mut pt_buf).unwrap();
        assert_eq!(&pt_buf[..pt_len], plaintext);

        let plaintext = b"hello from responder";
        let ct_len = r_transport.send(plaintext, &mut ct_buf).unwrap();
        let pt_len = i_transport.receive(&ct_buf[..ct_len], &mut pt_buf).unwrap();
        assert_eq!(&pt_buf[..pt_len], plaintext);
    }

    // ── ChaChaPoly AEAD unit tests ────────────────────────────────

    #[test]
    fn chacha_encrypt_decrypt_round_trip() {
        let key = [0x42u8; 32];
        let plaintext = b"the quick brown fox";
        let ad = b"associated data";

        let mut ct = [0u8; 128];
        let ct_len =
            ChaChaPoly::encrypt(&ChaChaPoly::key(&key), 0, ad, plaintext, &mut ct).unwrap();
        assert_eq!(ct_len, plaintext.len() + 16);

        let mut pt = [0u8; 128];
        let pt_len =
            ChaChaPoly::decrypt(&ChaChaPoly::key(&key), 0, ad, &ct[..ct_len], &mut pt).unwrap();
        assert_eq!(&pt[..pt_len], plaintext);
    }

    #[test]
    fn chacha_decrypt_corrupted_tag() {
        let key = [0x42u8; 32];
        let plaintext = b"test data";

        let mut ct = [0u8; 64];
        let ct_len =
            ChaChaPoly::encrypt(&ChaChaPoly::key(&key), 0, &[], plaintext, &mut ct).unwrap();

        // Corrupt the last byte (part of the tag).
        ct[ct_len - 1] ^= 0xFF;

        let mut pt = [0u8; 64];
        let err = ChaChaPoly::decrypt(&ChaChaPoly::key(&key), 0, &[], &ct[..ct_len], &mut pt)
            .unwrap_err();
        assert!(
            matches!(err, error::HandshakeError::DecryptionFailed),
            "expected DecryptionFailed, got {err:?}"
        );
    }

    #[test]
    fn chacha_decrypt_too_short() {
        let key = [0u8; 32];
        let mut pt = [0u8; 64];
        // Less than TAG_SIZE bytes — must fail.
        let err =
            ChaChaPoly::decrypt(&ChaChaPoly::key(&key), 0, &[], &[0u8; 15], &mut pt).unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));
    }

    #[test]
    fn chacha_wrong_nonce_fails() {
        let key = [0x42u8; 32];
        let plaintext = b"nonce matters";

        let mut ct = [0u8; 64];
        let ct_len =
            ChaChaPoly::encrypt(&ChaChaPoly::key(&key), 0, &[], plaintext, &mut ct).unwrap();

        // Decrypt with nonce 1 instead of 0.
        let mut pt = [0u8; 64];
        let err = ChaChaPoly::decrypt(&ChaChaPoly::key(&key), 1, &[], &ct[..ct_len], &mut pt)
            .unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));
    }

    #[test]
    fn chacha_wrong_key_fails() {
        let key = [0x42u8; 32];
        let wrong_key = [0x43u8; 32];
        let plaintext = b"key matters";

        let mut ct = [0u8; 64];
        let ct_len =
            ChaChaPoly::encrypt(&ChaChaPoly::key(&key), 0, &[], plaintext, &mut ct).unwrap();

        let mut pt = [0u8; 64];
        let err = ChaChaPoly::decrypt(&ChaChaPoly::key(&wrong_key), 0, &[], &ct[..ct_len], &mut pt)
            .unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));
    }

    #[test]
    fn chacha_wrong_ad_fails() {
        let key = [0x42u8; 32];
        let plaintext = b"ad matters";

        let mut ct = [0u8; 64];
        let ct_len =
            ChaChaPoly::encrypt(&ChaChaPoly::key(&key), 0, b"correct", plaintext, &mut ct).unwrap();

        let mut pt = [0u8; 64];
        let err = ChaChaPoly::decrypt(&ChaChaPoly::key(&key), 0, b"wrong", &ct[..ct_len], &mut pt)
            .unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));
    }

    #[test]
    fn chacha_empty_plaintext() {
        let key = [0x42u8; 32];
        let mut ct = [0u8; 16]; // tag only
        let ct_len = ChaChaPoly::encrypt(&ChaChaPoly::key(&key), 0, &[], &[], &mut ct).unwrap();
        assert_eq!(ct_len, 16);

        let mut pt = [0u8; 0];
        let pt_len =
            ChaChaPoly::decrypt(&ChaChaPoly::key(&key), 0, &[], &ct[..ct_len], &mut pt).unwrap();
        assert_eq!(pt_len, 0);
    }

    // ── CipherState unit tests ────────────────────────────────────

    #[test]
    fn cipher_state_unkeyed_passthrough() {
        let mut cs = cipher_state::CipherState::<ChaChaPoly>::empty();
        assert!(!cs.has_key());

        let plaintext = b"plaintext passthrough";
        let mut out = [0u8; 64];
        let len = cs.encrypt_with_ad(b"ad", plaintext, &mut out).unwrap();
        assert_eq!(&out[..len], plaintext);

        let mut pt = [0u8; 64];
        let len = cs.decrypt_with_ad(b"ad", &out[..len], &mut pt).unwrap();
        assert_eq!(&pt[..len], plaintext);
    }

    // ── Hash trait unit tests ─────────────────────────────────────

    #[test]
    fn blake2b_hash_deterministic() {
        let a = Blake2b::hash(b"test data");
        let b = Blake2b::hash(b"test data");
        assert_eq!(a, b);
        assert_eq!(a.len(), 64);
    }

    #[test]
    fn blake2b_hash_different_inputs() {
        let a = Blake2b::hash(b"input one");
        let b = Blake2b::hash(b"input two");
        assert_ne!(a, b);
    }

    #[test]
    fn blake2b_hash_two_equals_concat() {
        // hash_two(a, b) should equal hash(a || b)
        let a = b"first part";
        let b = b"second part";
        let h1 = Blake2b::hash_two(a, b);
        let mut concat = a.to_vec();
        concat.extend_from_slice(b);
        let h2 = Blake2b::hash(&concat);
        assert_eq!(h1, h2);
    }

    #[test]
    fn blake2b_hmac_deterministic() {
        let a = Blake2b::hmac(b"key", b"data");
        let b = Blake2b::hmac(b"key", b"data");
        assert_eq!(a, b);
        assert_eq!(a.len(), 64);
    }

    #[test]
    fn blake2b_hmac_different_keys() {
        let a = Blake2b::hmac(b"key1", b"data");
        let b = Blake2b::hmac(b"key2", b"data");
        assert_ne!(a, b);
    }

    /// HMAC-BLAKE2b over RFC 4231's *inputs* — but the values below are
    /// **not** standards-body vectors, and must not be presented as such.
    ///
    /// No standards body publishes HMAC-BLAKE2 vectors: RFC 7693 defines no
    /// HMAC, Wycheproof ships no HMAC-BLAKE2 file, and macOS's LibreSSL has
    /// no BLAKE2 digest at all. So these are cross-generated, and pinned only
    /// because two implementations independent of `cryptoxide` — Python's
    /// `hmac` over `hashlib.blake2b`, and RustCrypto's `hmac::SimpleHmac`
    /// over `blake2::Blake2b512` — agree on every one of them. The recipe was
    /// validated first against this file's pinned BLAKE2s case-6 value, which
    /// it reproduced exactly.
    ///
    /// These matter more than their BLAKE2s counterparts, because since the
    /// move to `cryptoxide` 0.6 the RFC 2104 key schedule under them is
    /// *hiss's own* (`noise::hash`'s private `hmac_blake2` module) rather
    /// than the library's. `tests/primitive_diag.rs` checks the same code
    /// against a hand-rolled ipad/opad oracle, but that oracle shares an
    /// author with the implementation; these do not, so a shared misreading
    /// of RFC 2104 fails here and passes there.
    ///
    /// The broader check on this code path is elsewhere: `mix_key` runs
    /// `Blake2b::hmac` on every one of the 85 BLAKE2b handshake replays — 68
    /// in `tests/noise_cacophony.rs` (34 vectors, each in both roles) and 17
    /// in `tests/noise_kat.rs` — against implementations neither hiss nor
    /// `snow` wrote.
    ///
    /// Cases 1, 2, 3 and 6 of RFC 4231 §4. Case 6's 131-byte key is the only
    /// one of the four longer than the 128-byte block, so it is the only one
    /// that reaches the hash-the-key branch of the key schedule — a branch no
    /// handshake can reach, since `mix_key` always keys with a 64-byte
    /// chaining key. It is reachable only through the public `Hash` trait.
    #[test]
    fn blake2b_hmac_cross_checked() {
        assert_eq!(
            hex::encode(Blake2b::hmac(&[0x0b; 20], b"Hi There")),
            "358a6a184924894fc34bee5680eedf57d84a37bb38832f288e3b27dc63a98cc8\
             c91e76da476b508bc6b2d408a248857452906e4a20b48c6b4b55d2df0fe1dd24"
        );
        assert_eq!(
            hex::encode(Blake2b::hmac(b"Jefe", b"what do ya want for nothing?")),
            "6ff884f8ddc2a6586b3c98a4cd6ebdf14ec10204b6710073eb5865ade37a2643\
             b8807c1335d107ecdb9ffeaeb6828c4625ba172c66379efcd222c2de11727ab4"
        );
        assert_eq!(
            hex::encode(Blake2b::hmac(&[0xaa; 20], &[0xdd; 50])),
            "f43bc62c7a99353c3b2c60e8ef24fbbd42e9547866dc9c5be4edc6f4a7d4bc0a\
             c620c2c60034d040f0dbaf86f9e9cd7891a095595eed55e2a996215f0c15c018"
        );
        assert_eq!(
            hex::encode(Blake2b::hmac(
                &[0xaa; 131],
                b"Test Using Larger Than Block-Size Key - Hash Key First"
            )),
            "a54b2943b2a20227d41ca46c0945af09bc1faefb2f49894c23aebc557fb79c48\
             89dca74408dc865086667aedee4a3185c53a49c80b814c4c5813ea0c8b38a8f8"
        );
    }

    /// FIPS 180-4 Appendix B short-message digests — a standards-body
    /// oracle, which the BLAKE2b tests above have no equivalent of.
    #[test]
    fn sha256_matches_nist_vectors() {
        assert_eq!(
            hex::encode(Sha256::hash(b"")),
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
        );
        assert_eq!(
            hex::encode(Sha256::hash(b"abc")),
            "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
        );
        // The 448-bit two-block message.
        assert_eq!(
            hex::encode(Sha256::hash(
                b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
            )),
            "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
        );
        assert_eq!(Sha256::hash(b"abc").len(), 32);
    }

    #[test]
    fn sha256_hash_two_equals_concat() {
        // hash_two(a, b) should equal hash(a || b)
        let a = b"first part";
        let b = b"second part";
        let h1 = Sha256::hash_two(a, b);
        let mut concat = a.to_vec();
        concat.extend_from_slice(b);
        let h2 = Sha256::hash(&concat);
        assert_eq!(h1, h2);
    }

    /// RFC 4231 §4 HMAC-SHA-256 cases 1, 2, 3 and 6. Case 6's key is
    /// longer than the 64-byte block, which is the only one of the four
    /// that reaches the hash-the-key path inside `Hmac`.
    #[test]
    fn sha256_hmac_rfc4231() {
        assert_eq!(
            hex::encode(Sha256::hmac(&[0x0b; 20], b"Hi There")),
            "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"
        );
        assert_eq!(
            hex::encode(Sha256::hmac(b"Jefe", b"what do ya want for nothing?")),
            "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"
        );
        assert_eq!(
            hex::encode(Sha256::hmac(&[0xaa; 20], &[0xdd; 50])),
            "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe"
        );
        assert_eq!(
            hex::encode(Sha256::hmac(
                &[0xaa; 131],
                b"Test Using Larger Than Block-Size Key - Hash Key First"
            )),
            "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"
        );
    }

    #[test]
    fn sha256_hmac_different_keys() {
        let a = Sha256::hmac(b"key1", b"data");
        let b = Sha256::hmac(b"key2", b"data");
        assert_ne!(a, b);
    }

    /// RFC 7693 Appendix B, "Example of BLAKE2s Computation" — the one
    /// standards-body digest that exists for this hash. `snow` vendors the
    /// same value from `draft-saarinen-blake2-06`
    /// (`resolvers/default.rs`, `test_blake2s`), and Python's
    /// `hashlib.blake2s` reproduces it — three independent sources.
    #[test]
    fn blake2s_matches_rfc7693() {
        assert_eq!(
            hex::encode(Blake2s::hash(b"abc")),
            "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"
        );
        assert_eq!(Blake2s::hash(b"abc").len(), 32);
    }

    #[test]
    fn blake2s_hash_two_equals_concat() {
        // hash_two(a, b) should equal hash(a || b)
        let a = b"first part";
        let b = b"second part";
        let h1 = Blake2s::hash_two(a, b);
        let mut concat = a.to_vec();
        concat.extend_from_slice(b);
        let h2 = Blake2s::hash(&concat);
        assert_eq!(h1, h2);
    }

    /// HMAC-BLAKE2s over RFC 4231's *inputs* — but the values below are
    /// **not** standards-body vectors, and must not be presented as such.
    ///
    /// No standards body publishes HMAC-BLAKE2 vectors: RFC 7693 defines no
    /// HMAC, Wycheproof ships no HMAC-BLAKE2 file, and macOS's LibreSSL has
    /// no BLAKE2 digest at all. So these are cross-generated, and pinned only
    /// because two implementations independent of `cryptoxide` — Python's
    /// `hmac` over `hashlib.blake2s`, and RustCrypto's `hmac::SimpleHmac`
    /// over `blake2::Blake2s256` — agree on every one of them.
    ///
    /// The stronger check on this code path is elsewhere: `mix_key` runs
    /// `Blake2s::hmac` on every one of the 22 BLAKE2s handshakes in
    /// `tests/noise_cacophony.rs`, against an implementation neither hiss
    /// nor `snow` wrote.
    ///
    /// Cases 1, 2, 3 and 6 of RFC 4231 §4. Case 6's 131-byte key is the only
    /// one of the four longer than the 64-byte block, so it is the only one
    /// that reaches the hash-the-key path inside `Hmac`.
    #[test]
    fn blake2s_hmac_cross_checked() {
        assert_eq!(
            hex::encode(Blake2s::hmac(&[0x0b; 20], b"Hi There")),
            "65a8b7c5cc9136d424e82c37e2707e74e913c0655b99c75f40edf387453a3260"
        );
        assert_eq!(
            hex::encode(Blake2s::hmac(b"Jefe", b"what do ya want for nothing?")),
            "90b6281e2f3038c9056af0b4a7e763cae6fe5d9eb4386a0ec95237890c104ff0"
        );
        assert_eq!(
            hex::encode(Blake2s::hmac(&[0xaa; 20], &[0xdd; 50])),
            "fcc4f59529502e34c3d8da3ffdab82966a2cb637ff5e9bd701135c2e9469e790"
        );
        assert_eq!(
            hex::encode(Blake2s::hmac(
                &[0xaa; 131],
                b"Test Using Larger Than Block-Size Key - Hash Key First"
            )),
            "d23d79394f53d536a096e6514447eeaabb05ded01be32c1937da6a8f7103bc4e"
        );
    }

    #[test]
    fn blake2s_hmac_different_keys() {
        let a = Blake2s::hmac(b"key1", b"data");
        let b = Blake2s::hmac(b"key2", b"data");
        assert_ne!(a, b);
    }

    /// FIPS 180-4 Appendix C short-message digests, plus the 896-bit
    /// two-block message — cross-checked against `openssl dgst -sha512`.
    #[test]
    fn sha512_matches_nist_vectors() {
        assert_eq!(
            hex::encode(Sha512::hash(b"")),
            "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce\
             47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
        );
        assert_eq!(
            hex::encode(Sha512::hash(b"abc")),
            "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a\
             2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
        );
        // The 896-bit two-block message.
        assert_eq!(
            hex::encode(Sha512::hash(
                b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn\
                  hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
            )),
            "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018\
             501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"
        );
        assert_eq!(Sha512::hash(b"abc").len(), 64);
    }

    #[test]
    fn sha512_hash_two_equals_concat() {
        // hash_two(a, b) should equal hash(a || b)
        let a = b"first part";
        let b = b"second part";
        let h1 = Sha512::hash_two(a, b);
        let mut concat = a.to_vec();
        concat.extend_from_slice(b);
        let h2 = Sha512::hash(&concat);
        assert_eq!(h1, h2);
    }

    /// RFC 4231 §4 HMAC-SHA-512 cases 1, 2, 3 and 6 — standards-body
    /// vectors, unlike BLAKE2s, which has none. Case 3 additionally matches
    /// `snow`'s own vendored test verbatim. Case 6's key is longer than the
    /// 128-byte block, the only one of the four that reaches the
    /// hash-the-key path.
    #[test]
    fn sha512_hmac_rfc4231() {
        assert_eq!(
            hex::encode(Sha512::hmac(&[0x0b; 20], b"Hi There")),
            "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde\
             daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854"
        );
        assert_eq!(
            hex::encode(Sha512::hmac(b"Jefe", b"what do ya want for nothing?")),
            "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554\
             9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737"
        );
        assert_eq!(
            hex::encode(Sha512::hmac(&[0xaa; 20], &[0xdd; 50])),
            "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39\
             bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb"
        );
        assert_eq!(
            hex::encode(Sha512::hmac(
                &[0xaa; 131],
                b"Test Using Larger Than Block-Size Key - Hash Key First"
            )),
            "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f352\
             6b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598"
        );
    }

    #[test]
    fn sha512_hmac_different_keys() {
        let a = Sha512::hmac(b"key1", b"data");
        let b = Sha512::hmac(b"key2", b"data");
        assert_ne!(a, b);
    }

    // ── Handshake error path tests ────────────────────────────────

    #[test]
    fn generated_message_size_matches_the_size_macro() {
        // This test used to feed a 64-byte buffer to a responder expecting
        // 162 and assert that a token's `read_exact` ran out of bytes.
        // `read_message_1` now takes `&[u8; IKpsk1::MSG1_SIZE]`, so a
        // wrong-length buffer cannot reach it at all: the check moved from
        // run time into the type system, where it is pinned by a
        // `compile_fail` doctest on `hiss::noise!`.
        //
        // What is still worth asserting is that the two *independent* size
        // computations agree — the const the macro generates, and the
        // `noise_message_size!` arithmetic.
        assert_eq!(
            IKpsk1::MSG1_SIZE,
            noise_message_size!(curve: P256, cipher: ChaChaPoly, has_psk: true, keyed: false, tokens: [E, Es, S, Ss, Psk],),
        );
    }

    #[test]
    fn expected_message_size_reports_correctly() {
        // There is no runtime `expected_message_size` query any more; the
        // same value is available at compile time from the message-size
        // macro. msg1 (-> e, es, s, ss, psk):
        // 65 (ephemeral) + 65 (encrypted static) + 16 (tag) + 16 (payload tag) = 162
        assert_eq!(
            noise_message_size!(curve: P256, cipher: ChaChaPoly, has_psk: true, keyed: false, tokens: [E, Es, S, Ss, Psk],),
            162
        );
    }

    #[test]
    fn corrupted_encrypted_static_in_msg1_rejected() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        let initiator_static = provider.generate::<P256>().unwrap();
        let responder_static = provider.generate::<P256>().unwrap();
        let responder_pub = provider.public(&responder_static).unwrap();

        let psk = Psk::from_bytes([0xBB; 32]);

        // Initiator constructs msg1 normally.
        let (mut corrupted, _i_hs) = IKpsk1::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_pub,
        )
        .write_message_1(initiator_static, &psk)
        .unwrap();

        // Corrupt a byte in the encrypted static key area (after the
        // 65-byte ephemeral).
        corrupted[70] ^= 0xFF;

        // The `s` token decrypts the static key — corruption fails its tag,
        // so the read rejects the message.
        let outcome = IKpsk1::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_static,
        )
        .unwrap()
        .read_message_1(&corrupted, &psk);
        assert!(matches!(outcome, Err(HandshakeError::DecryptionFailed)));
    }

    #[test]
    fn mismatched_psk_fails() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        let initiator_static = provider.generate::<P256>().unwrap();
        let responder_static = provider.generate::<P256>().unwrap();
        let responder_pub = provider.public(&responder_static).unwrap();

        let i_psk = Psk::from_bytes([0xAA; 32]);
        let r_psk = Psk::from_bytes([0xBB; 32]); // different!

        // Initiator sends msg1 with i_psk.
        let (msg1, _i_hs) = IKpsk1::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_pub,
        )
        .write_message_1(initiator_static, &i_psk)
        .unwrap();

        // Responder reads msg1 with r_psk — mismatch. The psk token is the
        // last in msg1, so the payload tag closing the message catches the
        // divergence.
        let result = IKpsk1::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_static,
        )
        .unwrap()
        .read_message_1(&msg1, &r_psk);
        assert!(result.is_err());
    }

    #[test]
    fn transport_corrupted_ciphertext_rejected() {
        let psk = Psk::from_bytes([0xCC; 32]);
        let (mut i_transport, mut r_transport) = complete_ikpsk1(&psk);

        // Encrypt a message, then corrupt it.
        let mut ct_buf = [0u8; 256];
        let ct_len = i_transport.send(b"secret", &mut ct_buf).unwrap();
        ct_buf[0] ^= 0xFF;

        let mut pt_buf = [0u8; 256];
        let err = r_transport
            .receive(&ct_buf[..ct_len], &mut pt_buf)
            .unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));
    }

    #[test]
    fn transport_multiple_messages_nonce_advances() {
        let psk = Psk::from_bytes([0xDD; 32]);
        let (mut i_transport, mut r_transport) = complete_ikpsk1(&psk);

        // Send 10 messages in each direction — nonce must advance correctly.
        let mut ct_buf = [0u8; 256];
        let mut pt_buf = [0u8; 256];
        for i in 0u32..10 {
            let msg = format!("message {i}");
            let ct_len = i_transport.send(msg.as_bytes(), &mut ct_buf).unwrap();
            let pt_len = r_transport.receive(&ct_buf[..ct_len], &mut pt_buf).unwrap();
            assert_eq!(&pt_buf[..pt_len], msg.as_bytes());

            let reply = format!("reply {i}");
            let ct_len = r_transport.send(reply.as_bytes(), &mut ct_buf).unwrap();
            let pt_len = i_transport.receive(&ct_buf[..ct_len], &mut pt_buf).unwrap();
            assert_eq!(&pt_buf[..pt_len], reply.as_bytes());
        }
    }

    // ── SymmetricState protocol name hashing ──────────────────────

    #[test]
    fn symmetric_state_long_protocol_name() {
        // A protocol name longer than HASHLEN (64) should be hashed.
        let long_name = "A".repeat(100);
        let ss = symmetric_state::SymmetricState::<ChaChaPoly, Blake2b>::initialize(&long_name);
        // Just verify it doesn't panic and the hash is 64 bytes.
        assert_eq!(ss.handshake_hash().len(), 64);
    }

    #[test]
    fn symmetric_state_short_protocol_name_sha256() {
        // 31 bytes, so at HASHLEN 32 this takes the padding branch. The
        // hashing branch is the one `Noise_IKpsk1_P256_ChaChaPoly_SHA256`
        // (35 bytes) reaches, pinned against snow in `tests/noise_kat.rs`.
        let name = "Noise_XX_P256_ChaChaPoly_SHA256";
        let ss = symmetric_state::SymmetricState::<ChaChaPoly, Sha256>::initialize(name);
        let mut want = vec![0u8; 32];
        want[..name.len()].copy_from_slice(name.as_bytes());
        assert_eq!(ss.handshake_hash(), want.as_slice());
    }

    // ── Wrong responder static key ──────────────────────────────────

    #[test]
    fn ikpsk1_wrong_responder_key_rejected() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        let initiator_static = provider.generate::<P256>().unwrap();
        let responder_static = provider.generate::<P256>().unwrap();
        let wrong_static = provider.generate::<P256>().unwrap();
        let wrong_pub = provider.public(&wrong_static).unwrap();
        let psk = Psk::from_bytes([0xCC; 32]);

        // Initiator targets the wrong responder public key, so its `es` DH
        // is against a key the real responder does not hold.
        let (msg1, _i_hs) = IKpsk1::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            wrong_pub,
        )
        .write_message_1(initiator_static, &psk)
        .unwrap();

        // The actual responder holds a different static key, so `es`
        // produces a different shared secret. The token itself succeeds —
        // it only mixes the DH result in — but the derived cipher key is
        // wrong, so decrypting the initiator's static at the `s` token
        // fails and the read rejects the message.
        let result = IKpsk1::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_static,
        )
        .unwrap()
        .read_message_1(&msg1, &psk);
        assert!(result.is_err());
    }

    // ── Transport direction isolation ───────────────────────────────

    #[test]
    fn transport_keys_are_directional() {
        let psk = Psk::from_bytes([0xFF; 32]);
        let (mut i_transport, mut r_transport) = complete_ikpsk1(&psk);

        // Initiator sends a message.
        let mut ct_buf = [0u8; 256];
        let ct_len = i_transport.send(b"hello", &mut ct_buf).unwrap();

        // Trying to decrypt with the initiator's own receive channel
        // must fail — transport keys are directional.
        let mut pt_buf = [0u8; 256];
        let err = i_transport
            .receive(&ct_buf[..ct_len], &mut pt_buf)
            .unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));

        // But the responder can decrypt it.
        let pt_len = r_transport.receive(&ct_buf[..ct_len], &mut pt_buf).unwrap();
        assert_eq!(&pt_buf[..pt_len], b"hello");
    }

    // ── Session uniqueness (different ephemeral keys) ───────────────

    #[test]
    fn two_sessions_produce_different_handshake_hashes() {
        // Use a fixed responder key so both sessions share the same
        // responder identity — only ephemeral keys differ.
        let responder_bytes = [0xBB_u8; 32];
        let psk = Psk::from_bytes([0xAA; 32]);

        let mut hashes = Vec::new();

        for _ in 0..2 {
            let responder_static =
                P256r1PrivateKey::from_bytes(responder_bytes).expect("valid test scalar");
            let responder_pub = responder_static.public();

            let initiator_static = EphemeralOnly::new(rand::make_rng::<StdRng>())
                .generate::<P256>()
                .unwrap();

            let (msg1, i_hs) = IKpsk1::initiator(
                EphemeralOnly::new(rand::make_rng::<StdRng>()),
                &[],
                responder_pub,
            )
            .write_message_1(initiator_static, &psk)
            .unwrap();

            let r_hs = IKpsk1::responder(
                EphemeralOnly::new(rand::make_rng::<StdRng>()),
                &[],
                responder_static,
            )
            .unwrap()
            .read_message_1(&msg1, &psk)
            .unwrap();

            let (msg2, r_transport) = r_hs.write_message_2().unwrap();
            let i_transport = i_hs.read_message_2(&msg2).unwrap();

            assert_eq!(i_transport.session_id(), r_transport.session_id());
            hashes.push(i_transport.session_id().as_ref().to_vec());
        }

        // Two sessions with different ephemeral keys must produce
        // different handshake hashes.
        assert_ne!(hashes[0], hashes[1]);
    }

    // ── IKpsk1: wrong initiator static key in msg1 ──────────────────

    #[tokio::test]
    async fn ikpsk1_wrong_initiator_static_in_msg1() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        let initiator_static = provider.generate::<P256>().unwrap();
        let initiator_pub = provider.public(&initiator_static).unwrap();
        let responder_static = provider.generate::<P256>().unwrap();
        let responder_pub = provider.public(&responder_static).unwrap();
        let psk = Psk::from_bytes([0xCC; 32]);

        // Initiator sends msg1 with the WRONG static key.
        // In IKpsk1 msg1 (-> e, es, s, ss, psk), the `ss` token is
        // DH(wrong_s, responder_s). Since the responder decrypts the
        // wrong key from the `s` token and computes the same ECDH result,
        // the handshake completes — but the responder sees a different
        // initiator identity. The application layer must verify the
        // revealed static key matches the expected peer.
        let wrong_static = provider.generate::<P256>().unwrap();

        let (msg1, i_hs) = IKpsk1::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_pub,
        )
        .write_message_1(wrong_static, &psk)
        .unwrap();

        let r_hs = IKpsk1::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_static,
        )
        .unwrap()
        .read_message_1(&msg1, &psk)
        .unwrap();

        // The responder decrypted the wrong initiator static key.
        assert_ne!(*r_hs.remote_static(), initiator_pub);

        // Complete the handshake — msg2 still works because the `se`
        // token uses DH(wrong_s, responder_e), which both sides compute
        // consistently.
        let (msg2, r_transport) = r_hs.write_message_2().unwrap();
        let i_transport = i_hs.read_message_2(&msg2).unwrap();

        // Transport works — keys are derived consistently from the wrong
        // static key. The responder must reject this identity at the
        // application layer. The handshake hash also differs from what
        // the real initiator would produce, so channel binding catches it.
        assert_eq!(i_transport.session_id(), r_transport.session_id());
        drop(i_transport);
        drop(r_transport);
    }

    // ── Corrupted msg1 (tampered ephemeral in IKpsk1) ─────────────────

    #[test]
    fn ikpsk1_corrupted_msg1_rejected() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        let initiator_static = provider.generate::<P256>().unwrap();
        let responder_static = provider.generate::<P256>().unwrap();
        let responder_pub = provider.public(&responder_static).unwrap();
        let psk = Psk::from_bytes([0xDD; 32]);

        let (mut corrupted, _i_hs) = IKpsk1::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_pub,
        )
        .write_message_1(initiator_static, &psk)
        .unwrap();
        // Corrupt a byte in the ephemeral public key.
        corrupted[5] ^= 0xFF;

        // Corruption yields either an invalid curve point or a valid but
        // wrong one, whose `es` DH diverges the key and fails the payload
        // tag. Either way the read rejects it and no state comes back.
        let outcome = IKpsk1::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_static,
        )
        .unwrap()
        .read_message_1(&corrupted, &psk);
        assert!(outcome.is_err());
    }

    // ── Corrupted msg2 (tampered ephemeral in IKpsk1) ─────────────────

    #[test]
    fn ikpsk1_corrupted_msg2_rejected() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        let initiator_static = provider.generate::<P256>().unwrap();
        let responder_static = provider.generate::<P256>().unwrap();
        let responder_pub = provider.public(&responder_static).unwrap();
        let psk = Psk::from_bytes([0xDD; 32]);

        // msg1 flows through cleanly so the responder can produce msg2.
        let (msg1, i_hs) = IKpsk1::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_pub,
        )
        .write_message_1(initiator_static, &psk)
        .unwrap();
        let r_hs = IKpsk1::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            &[],
            responder_static,
        )
        .unwrap()
        .read_message_1(&msg1, &psk)
        .unwrap();

        let (mut corrupted, _r_transport) = r_hs.write_message_2().unwrap();
        // Corrupt a byte in the responder's ephemeral public key.
        corrupted[3] ^= 0xFF;

        // Either an invalid curve point or a valid but wrong one whose `ee`
        // DH fails the payload tag — the initiator's read rejects it.
        assert!(i_hs.read_message_2(&corrupted).is_err());
    }

    // ── Transport replay detection (nonce desync) ─────────────────────

    #[test]
    fn transport_replayed_message_rejected() {
        let psk = Psk::from_bytes([0xEE; 32]);
        let (mut i_transport, mut r_transport) = complete_ikpsk1(&psk);

        // Send a message and receive it normally.
        let mut ct_buf = [0u8; 256];
        let ct_len = i_transport.send(b"first message", &mut ct_buf).unwrap();
        let captured = ct_buf[..ct_len].to_vec();

        let mut pt_buf = [0u8; 256];
        let pt_len = r_transport.receive(&captured, &mut pt_buf).unwrap();
        assert_eq!(&pt_buf[..pt_len], b"first message");

        // Replay the same ciphertext — nonce has advanced, so it must fail.
        let err = r_transport.receive(&captured, &mut pt_buf).unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));
    }

    // ── Large payload transport ───────────────────────────────────────

    #[test]
    fn transport_enforces_max_message_length() {
        let psk = Psk::from_bytes([0xFF; 32]);
        let (mut i_transport, mut r_transport) = complete_ikpsk1(&psk);

        // Largest payload whose on-wire message (ciphertext + 16-byte tag)
        // still fits the 65535-byte Noise cap: 65535 - 16 = 65519.
        let max_payload: Vec<u8> = (0..65519).map(|i| (i % 256) as u8).collect();
        let mut ct_buf = vec![0u8; max_payload.len() + 16];
        let ct_len = i_transport.send(&max_payload, &mut ct_buf).unwrap();
        assert_eq!(ct_len, 65535); // exactly the cap

        let mut pt_buf = vec![0u8; max_payload.len()];
        let pt_len = r_transport.receive(&ct_buf[..ct_len], &mut pt_buf).unwrap();
        assert_eq!(&pt_buf[..pt_len], &max_payload[..]);

        // One byte more overflows the cap (message would be 65536) and must
        // be rejected, not emitted as a non-conformant message.
        let over_payload = vec![0u8; 65520];
        let mut ct_buf = vec![0u8; over_payload.len() + 16];
        let err = i_transport.send(&over_payload, &mut ct_buf).unwrap_err();
        assert!(matches!(
            err,
            error::HandshakeError::MessageTooLong { len: 65536 }
        ));

        // The receive side likewise rejects an over-cap incoming message
        // before attempting any decryption.
        let oversize = vec![0u8; 65536];
        let mut pt_buf = vec![0u8; oversize.len()];
        let err = r_transport.receive(&oversize, &mut pt_buf).unwrap_err();
        assert!(matches!(
            err,
            error::HandshakeError::MessageTooLong { len: 65536 }
        ));
    }

    // ── Rekey tests ────────────────────────────────────────────────

    #[test]
    fn transport_rekey_then_communicate() {
        let psk = Psk::from_bytes([0x11; 32]);
        let (mut i_transport, mut r_transport) = complete_ikpsk1(&psk);

        // Send a message before rekey.
        let mut ct = [0u8; 256];
        let mut pt = [0u8; 256];
        let ct_len = i_transport.send(b"before rekey", &mut ct).unwrap();
        let pt_len = r_transport.receive(&ct[..ct_len], &mut pt).unwrap();
        assert_eq!(&pt[..pt_len], b"before rekey");

        // Rekey both sides.
        i_transport.rekey().unwrap();
        r_transport.rekey().unwrap();

        // Communication must still work after rekey.
        let ct_len = i_transport.send(b"after rekey", &mut ct).unwrap();
        let pt_len = r_transport.receive(&ct[..ct_len], &mut pt).unwrap();
        assert_eq!(&pt[..pt_len], b"after rekey");

        // Reverse direction also works.
        let ct_len = r_transport.send(b"reply after rekey", &mut ct).unwrap();
        let pt_len = i_transport.receive(&ct[..ct_len], &mut pt).unwrap();
        assert_eq!(&pt[..pt_len], b"reply after rekey");
    }

    /// Exercises the split-transport API: `Transport::split` and the
    /// resulting `TransportSend`/`TransportRecv` halves (encrypt/decrypt/
    /// rekey/session_id/ephemeral accessors), plus the `Transport`-level
    /// ephemeral accessors. IKpsk1 is interactive, so both sides hold a
    /// local *and* a remote ephemeral.
    #[test]
    fn transport_split_round_trip() {
        let psk = Psk::from_bytes([0x5A; 32]);
        let (i_transport, r_transport) = complete_ikpsk1(&psk);

        // `Transport`-level ephemeral accessors: interactive pattern ⇒
        // both ephemerals present on each side. Both peers agree on the
        // session id (it is derived from the shared handshake hash).
        assert!(i_transport.local_ephemeral().is_some());
        assert!(i_transport.remote_ephemeral().is_some());
        assert!(r_transport.local_ephemeral().is_some());
        assert!(r_transport.remote_ephemeral().is_some());
        // `SessionId` is `PartialEq` but not `Debug`, so compare with `==`.
        assert!(i_transport.session_id() == r_transport.session_id());

        // Split each peer into independent send / receive halves.
        let (mut i_send, mut i_recv) = i_transport.split();
        let (mut r_send, mut r_recv) = r_transport.split();

        // Each half carries a clone of the session id and ephemerals.
        assert!(i_send.session_id() == i_recv.session_id());
        assert!(i_send.session_id() == r_recv.session_id());
        assert!(i_send.local_ephemeral().is_some());
        assert!(i_send.remote_ephemeral().is_some());
        assert!(r_recv.local_ephemeral().is_some());
        assert!(r_recv.remote_ephemeral().is_some());

        let mut ct = [0u8; 256];
        let mut pt = [0u8; 256];

        // initiator → responder across the split halves.
        let ct_len = i_send.encrypt(b"ping", &mut ct).unwrap();
        assert_eq!(ct_len, b"ping".len() + TransportSend::<Channel>::OVERHEAD);
        let pt_len = r_recv.decrypt(&ct[..ct_len], &mut pt).unwrap();
        assert_eq!(&pt[..pt_len], b"ping");

        // responder → initiator.
        let ct_len = r_send.encrypt(b"pong", &mut ct).unwrap();
        let pt_len = i_recv.decrypt(&ct[..ct_len], &mut pt).unwrap();
        assert_eq!(&pt[..pt_len], b"pong");

        // Rekey every half, then communication must still work both ways.
        i_send.rekey().unwrap();
        r_recv.rekey().unwrap();
        r_send.rekey().unwrap();
        i_recv.rekey().unwrap();

        let ct_len = i_send.encrypt(b"after rekey", &mut ct).unwrap();
        let pt_len = r_recv.decrypt(&ct[..ct_len], &mut pt).unwrap();
        assert_eq!(&pt[..pt_len], b"after rekey");

        let ct_len = r_send.encrypt(b"reply after rekey", &mut ct).unwrap();
        let pt_len = i_recv.decrypt(&ct[..ct_len], &mut pt).unwrap();
        assert_eq!(&pt[..pt_len], b"reply after rekey");
    }

    // ── Datagram-mode transport (into_datagram) ───────────────────────
    //
    // These exercise the out-of-order pair from `Transport::into_datagram`:
    // an explicit send counter that `hiss` owns, and a stateless receive
    // half that opens whatever counter it is handed. Each test runs a real
    // IKpsk1 handshake through the shared helper so the datagram pair is
    // backed by genuine, matched keys.

    /// Run a full IKpsk1 handshake and return the two completed transports
    /// `(initiator, responder)`. The datagram tests each need a real,
    /// matched transport pair.
    fn ikpsk1_transport_pair() -> (Transport<IKpsk1>, Transport<IKpsk1>) {
        complete_ikpsk1(&Psk::from_bytes([0x7A; 32]))
    }

    /// Seal several datagrams, shuffle them, and open each at its stated
    /// counter — all must decrypt. Also pins that `encrypt_next` hands out a
    /// strictly monotonic counter and that both halves share a session id.
    #[test]
    fn datagram_shuffled_delivery_all_open() {
        let (i_transport, r_transport) = ikpsk1_transport_pair();
        let (mut i_send, _i_recv) = i_transport.into_datagram();
        let (_r_send, mut r_recv) = r_transport.into_datagram();

        assert!(i_send.session_id() == r_recv.session_id());

        // Seal N messages, remembering each (counter, ciphertext).
        let mut ct = [0u8; 256];
        let mut sealed: Vec<(u64, Vec<u8>)> = Vec::new();
        for i in 0..8u64 {
            let msg = format!("datagram {i}");
            let (counter, ct_len) = i_send.encrypt_next(&[], msg.as_bytes(), &mut ct).unwrap();
            assert_eq!(counter, i); // hiss owns a strictly monotonic counter
            sealed.push((counter, ct[..ct_len].to_vec()));
        }

        // A fixed, non-trivial permutation keeps the shuffle deterministic.
        sealed.swap(0, 7);
        sealed.swap(1, 4);
        sealed.swap(2, 6);
        sealed.swap(3, 5);

        let mut pt = [0u8; 256];
        for (counter, packet) in &sealed {
            let pt_len = r_recv.decrypt_at(*counter, &[], packet, &mut pt).unwrap();
            assert_eq!(&pt[..pt_len], format!("datagram {counter}").as_bytes());
        }
    }

    /// A gap in the counter sequence (a dropped packet) does not stop later
    /// counters opening — the receive half is stateless.
    #[test]
    fn datagram_gap_later_counters_still_open() {
        let (i_transport, r_transport) = ikpsk1_transport_pair();
        let (mut i_send, _) = i_transport.into_datagram();
        let (_, mut r_recv) = r_transport.into_datagram();

        let mut ct = [0u8; 256];
        let mut sealed: Vec<(u64, Vec<u8>)> = Vec::new();
        for i in 0..5u64 {
            let msg = format!("packet {i}");
            let (counter, ct_len) = i_send.encrypt_next(&[], msg.as_bytes(), &mut ct).unwrap();
            sealed.push((counter, ct[..ct_len].to_vec()));
        }

        // Drop counter 2 entirely, as a lossy wire would — the rest still open.
        let mut pt = [0u8; 256];
        for (counter, packet) in sealed.iter().filter(|(c, _)| *c != 2) {
            let pt_len = r_recv.decrypt_at(*counter, &[], packet, &mut pt).unwrap();
            assert_eq!(&pt[..pt_len], format!("packet {counter}").as_bytes());
        }
    }

    /// The same counter can be opened twice (replay rejection is the
    /// caller's duty, documented on `decrypt_at`), while `encrypt_next`
    /// never re-issues a counter.
    #[test]
    fn datagram_replay_opens_and_counter_is_monotonic() {
        let (i_transport, r_transport) = ikpsk1_transport_pair();
        let (mut i_send, _) = i_transport.into_datagram();
        let (_, mut r_recv) = r_transport.into_datagram();

        // Counters handed out are strictly monotonic and never repeat.
        let mut ct = [0u8; 256];
        let mut seen: Vec<u64> = Vec::new();
        for _ in 0..4 {
            let (counter, _) = i_send.encrypt_next(&[], b"tick", &mut ct).unwrap();
            assert!(seen.iter().all(|c| *c != counter));
            seen.push(counter);
        }
        assert_eq!(seen, vec![0, 1, 2, 3]);

        // Seal one packet, then open the SAME counter twice — both succeed,
        // because the receiver keeps no state to reject a replay.
        let (counter, ct_len) = i_send.encrypt_next(&[], b"payload", &mut ct).unwrap();
        let mut pt = [0u8; 256];
        let first = r_recv
            .decrypt_at(counter, &[], &ct[..ct_len], &mut pt)
            .unwrap();
        assert_eq!(&pt[..first], b"payload");
        let second = r_recv
            .decrypt_at(counter, &[], &ct[..ct_len], &mut pt)
            .unwrap();
        assert_eq!(&pt[..second], b"payload");
    }

    /// Tampered ciphertext, the wrong associated data, and the wrong counter
    /// all error; a subsequent honest decrypt still succeeds, because
    /// `decrypt_at` is `&self` and cannot poison any state.
    #[test]
    fn datagram_bad_inputs_error_without_poisoning_state() {
        let (i_transport, r_transport) = ikpsk1_transport_pair();
        let (mut i_send, _) = i_transport.into_datagram();
        let (_, mut r_recv) = r_transport.into_datagram();

        let mut ct = [0u8; 256];
        let (counter, ct_len) = i_send.encrypt_next(b"ad", b"honest", &mut ct).unwrap();
        let good = ct[..ct_len].to_vec();
        let mut pt = [0u8; 256];

        // Tampered ciphertext → DecryptionFailed.
        let mut tampered = good.clone();
        tampered[0] ^= 0xFF;
        let err = r_recv
            .decrypt_at(counter, b"ad", &tampered, &mut pt)
            .unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));

        // Wrong associated data → DecryptionFailed.
        let err = r_recv
            .decrypt_at(counter, b"other", &good, &mut pt)
            .unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));

        // Wrong counter → DecryptionFailed.
        let err = r_recv
            .decrypt_at(counter + 1, b"ad", &good, &mut pt)
            .unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));

        // The honest decrypt at the true counter still works.
        let pt_len = r_recv.decrypt_at(counter, b"ad", &good, &mut pt).unwrap();
        assert_eq!(&pt[..pt_len], b"honest");
    }

    /// The send counter refuses to wrap: driven to `u64::MAX`, `encrypt_next`
    /// errors with `NonceOverflow` and writes nothing.
    #[test]
    fn datagram_encrypt_next_guards_nonce_exhaustion() {
        let (i_transport, _r_transport) = ikpsk1_transport_pair();
        let (mut i_send, _) = i_transport.into_datagram();

        // One short of the cap: this call succeeds and reports u64::MAX - 1.
        i_send.set_counter_for_test(u64::MAX - 1);
        let mut ct = [0u8; 64];
        let (counter, _) = i_send.encrypt_next(&[], b"x", &mut ct).unwrap();
        assert_eq!(counter, u64::MAX - 1);

        // The counter is now u64::MAX: the next seal must refuse to reuse the
        // nonce, and must leave the output untouched.
        let mut ct2 = [0xABu8; 64];
        let err = i_send.encrypt_next(&[], b"x", &mut ct2).unwrap_err();
        assert!(matches!(err, error::HandshakeError::NonceOverflow));
        assert_eq!(ct2, [0xABu8; 64]);
    }

    /// `next_counter` promises the counter of the next successful seal: it
    /// agrees with what `encrypt_next` then returns across many seals, is
    /// left unchanged by a failed (oversize) seal, and still reads
    /// `u64::MAX` at exhaustion — where the seal itself refuses.
    #[test]
    fn datagram_next_counter_matches_next_seal() {
        let (i_transport, _) = ikpsk1_transport_pair();
        let (mut i_send, _) = i_transport.into_datagram();

        // The promise holds seal after seal.
        let mut ct = [0u8; 256];
        for _ in 0..8 {
            let promised = i_send.next_counter();
            let (counter, _) = i_send.encrypt_next(&[], b"payload", &mut ct).unwrap();
            assert_eq!(counter, promised);
        }

        // A failed seal advances nothing: a payload whose on-wire message
        // would exceed the Noise length cap errors, and the promise is
        // unchanged.
        let before = i_send.next_counter();
        let oversize = vec![0u8; 65535];
        let mut big_out = vec![0u8; 65535 + 64];
        let err = i_send
            .encrypt_next(&[], &oversize, &mut big_out)
            .unwrap_err();
        assert!(matches!(err, error::HandshakeError::MessageTooLong { .. }));
        assert_eq!(i_send.next_counter(), before);

        // At exhaustion the accessor reads u64::MAX — the counter that will
        // never be used — while the seal itself refuses.
        i_send.set_counter_for_test(u64::MAX);
        assert_eq!(i_send.next_counter(), u64::MAX);
        let err = i_send.encrypt_next(&[], b"x", &mut ct).unwrap_err();
        assert!(matches!(err, error::HandshakeError::NonceOverflow));
        assert_eq!(i_send.next_counter(), u64::MAX);
    }

    /// A datagram half and a stream half from the *same* handshake transcript
    /// interoperate for the in-order counter sequence 0, 1, 2, …: the
    /// explicit-nonce datagram path computes exactly the bytes the implicit-
    /// nonce stream path expects. This is the security argument that
    /// `into_datagram` invents no new cryptography — it only surfaces the
    /// nonce Noise already uses.
    #[test]
    fn datagram_and_stream_interoperate_in_order() {
        let (i_transport, r_transport) = ikpsk1_transport_pair();
        let (mut i_dg_send, mut i_dg_recv) = i_transport.into_datagram();
        let (mut r_send, mut r_recv) = r_transport.split();

        let mut ct = [0u8; 256];
        let mut pt = [0u8; 256];

        // Datagram sender → stream receiver: the stream half decrypts with
        // its implicit counter 0, 1, 2, …, proving the datagram bytes at
        // counter k are byte-for-byte the stream record k.
        for i in 0..4u64 {
            let msg = format!("dg->stream {i}");
            let (counter, ct_len) = i_dg_send
                .encrypt_next(&[], msg.as_bytes(), &mut ct)
                .unwrap();
            assert_eq!(counter, i);
            let pt_len = r_recv.decrypt(&ct[..ct_len], &mut pt).unwrap();
            assert_eq!(&pt[..pt_len], msg.as_bytes());
        }

        // Stream sender → datagram receiver: `decrypt_at` opens each stream
        // record at its explicit counter.
        for i in 0..4u64 {
            let msg = format!("stream->dg {i}");
            let ct_len = r_send.encrypt(msg.as_bytes(), &mut ct).unwrap();
            let pt_len = i_dg_recv
                .decrypt_at(i, &[], &ct[..ct_len], &mut pt)
                .unwrap();
            assert_eq!(&pt[..pt_len], msg.as_bytes());
        }

        // Both halves agree on the session id with their stream counterparts.
        assert!(i_dg_send.session_id() == r_recv.session_id());
        assert!(i_dg_recv.session_id() == r_send.session_id());
    }

    // ── Epoch-ratcheting datagram transport (into_datagram_with_epoch) ─
    //
    // These exercise the counter-derived Rekey ratchet. Most use a
    // deterministic fixed-key transport (`fixed_epoch_transport`) so a test
    // can seal known bytes and open them on as many independent, matched
    // receivers as it needs — a real handshake yields fresh random keys and
    // only one receiver per pair.

    /// A shorthand for a non-zero epoch size.
    fn epoch(n: u64) -> NonZeroU64 {
        NonZeroU64::new(n).expect("epoch size must be non-zero")
    }

    /// A transport built from a fixed, known key in both directions, so
    /// datagram bytes it seals are deterministic and open on any receiver
    /// built the same way. Not a real handshake — a test fixture.
    fn fixed_epoch_transport() -> Transport<IKpsk1> {
        let key = [0x24u8; 32];
        Transport::<IKpsk1>::new(
            CipherState::from_key(key),
            CipherState::from_key(key),
            SessionId::from(vec![0xEE; 8]),
            None,
            None,
            None,
        )
    }

    /// The Noise §11.3 REKEY known-answer test. Pins the next key derived
    /// from an all-zero key, cross-checking the raw definition path
    /// (`ENCRYPT(k, 2^64−1, "", zeros)[..32]`), the `rekey_key` helper, and
    /// `CipherState::rekey` against one another and against the pinned hex.
    ///
    /// The pinned hex is unchanged from when `rekey_key` returned raw bytes;
    /// what changed is how the other two paths are checked against it. A
    /// [`Cipher::Key`] is opaque — no `PartialEq`, no `Debug`, no way back to
    /// bytes — so the derived keys are compared **behaviourally**: seal the
    /// same probe under each and require identical ciphertext and tag. Two
    /// different ChaCha20-Poly1305 keys agreeing on 47 bytes at a fixed nonce
    /// and AD is not something that happens by accident, so the pin is as
    /// tight as the byte comparison it replaces.
    #[test]
    fn rekey_kat() {
        use super::cipher_state::rekey_key;

        const PROBE: &[u8] = b"probe payload for the rekey KAT";
        const PROBE_AD: &[u8] = b"rekey-kat";

        /// Seal the probe under `key`: a fingerprint of the key bytes.
        fn seal(key: &<ChaChaPoly as Cipher>::Key) -> Vec<u8> {
            let mut out = [0u8; 64];
            let n = ChaChaPoly::encrypt(key, 7, PROBE_AD, PROBE, &mut out).unwrap();
            out[..n].to_vec()
        }

        let k = [0u8; 32];

        // Definition path: encrypt 32 zeros at nonce 2^64−1 with empty ad,
        // take the first 32 ciphertext bytes (the 16-byte tag is discarded).
        let mut scratch = [0u8; 48];
        ChaChaPoly::encrypt(
            &ChaChaPoly::key(&k),
            u64::MAX,
            &[],
            &[0u8; 32],
            &mut scratch,
        )
        .unwrap();
        let mut definition = [0u8; 32];
        definition.copy_from_slice(&scratch[..32]);

        // The pinned answer (computed once, cross-checked against the
        // definition above).
        assert_eq!(
            hex::encode(definition),
            "25ce5d37df19f3783185f2ffd5ab17fa3397c212f02d62fb1733e0b875b74c58",
            "REKEY next-key KAT drifted"
        );

        // The helper path must agree with the definition.
        let expected = seal(&ChaChaPoly::key(&definition));
        let derived = rekey_key::<ChaChaPoly>(&ChaChaPoly::key(&k)).unwrap();
        assert_eq!(
            seal(&derived),
            expected,
            "rekey_key does not derive the REKEY definition"
        );

        // `CipherState::rekey` installs exactly that next key: a rekeyed
        // state seals at nonce 0 exactly as a state freshly keyed with the
        // definition does.
        let mut cs = CipherState::<ChaChaPoly>::from_key(k);
        cs.rekey().unwrap();
        let mut after = [0u8; 64];
        let n = cs.encrypt_with_ad(PROBE_AD, PROBE, &mut after).unwrap();

        let mut reference = CipherState::<ChaChaPoly>::from_key(definition);
        let mut want = [0u8; 64];
        let m = reference
            .encrypt_with_ad(PROBE_AD, PROBE, &mut want)
            .unwrap();

        assert_eq!(
            &after[..n],
            &want[..m],
            "CipherState::rekey installed a key other than the REKEY definition"
        );
    }

    /// The no-ratchet path is byte-identical to a plain half at epoch 0, and
    /// the ratchet demonstrably fires at the first boundary. Uses a fixed key
    /// so the two senders' output can be compared byte for byte.
    #[test]
    fn datagram_no_ratchet_byte_identity() {
        let size = epoch(4);
        let (mut plain_send, _) = fixed_epoch_transport().into_datagram();
        let (mut epoch_send, _) = fixed_epoch_transport().into_datagram_with_epoch(size);

        let mut a = [0u8; 64];
        let mut b = [0u8; 64];

        // Epoch 0 (counters 0..4): the ratcheting sender has not rekeyed, so
        // its output is byte-for-byte the plain sender's.
        for _ in 0..4u64 {
            let (ca, na) = plain_send
                .encrypt_next(b"ad", b"identical payload", &mut a)
                .unwrap();
            let (cb, nb) = epoch_send
                .encrypt_next(b"ad", b"identical payload", &mut b)
                .unwrap();
            assert_eq!(ca, cb);
            assert_eq!(a[..na], b[..nb]);
        }

        // Counter 4 crosses into epoch 1: the ratcheting sender rekeys, so its
        // bytes now diverge from the plain sender's, and a plain receiver
        // (still on the handshake key) cannot open the epoch-1 packet though
        // it opens the plain one.
        let (cp, np) = plain_send.encrypt_next(b"ad", b"x", &mut a).unwrap();
        let (ce, ne) = epoch_send.encrypt_next(b"ad", b"x", &mut b).unwrap();
        assert_eq!(cp, 4);
        assert_eq!(ce, 4);
        assert_ne!(a[..np], b[..ne]);

        let (_, mut plain_recv) = fixed_epoch_transport().into_datagram();
        let mut pt = [0u8; 64];
        assert!(plain_recv.decrypt_at(4, b"ad", &a[..np], &mut pt).is_ok());
        let err = plain_recv
            .decrypt_at(4, b"ad", &b[..ne], &mut pt)
            .unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));
    }

    /// Seal across an epoch boundary and open both in order and reordered.
    /// The reordered pass proves a straggler from the previous epoch opens
    /// under the retained previous key, and that a forward commit happens on
    /// the first future-epoch packet regardless of arrival order.
    #[test]
    fn datagram_epoch_boundary_roundtrip_and_reorder() {
        let size = epoch(4);
        let (mut send, _) = fixed_epoch_transport().into_datagram_with_epoch(size);

        // Seal counters 0..=5; the boundary N = 4 opens epoch 1.
        let mut ctbuf = [0u8; 64];
        let mut sealed: Vec<Vec<u8>> = Vec::new();
        for i in 0..6u64 {
            let (c, n) = send
                .encrypt_next(&[], format!("m{i}").as_bytes(), &mut ctbuf)
                .unwrap();
            assert_eq!(c, i);
            sealed.push(ctbuf[..n].to_vec());
        }

        let mut pt = [0u8; 64];

        // In order on a fresh receiver: N−1 (epoch 0), N (epoch 1 commit),
        // N+1 (epoch 1).
        let (_, mut ra) = fixed_epoch_transport().into_datagram_with_epoch(size);
        for i in [3u64, 4, 5] {
            let n = ra.decrypt_at(i, &[], &sealed[i as usize], &mut pt).unwrap();
            assert_eq!(&pt[..n], format!("m{i}").as_bytes());
        }

        // Reordered on another fresh receiver: N+1 first (commits epoch 1,
        // keeps epoch 0 as prev), then the N−1 straggler (opens under prev),
        // then N.
        let (_, mut rb) = fixed_epoch_transport().into_datagram_with_epoch(size);
        for i in [5u64, 3, 4] {
            let n = rb.decrypt_at(i, &[], &sealed[i as usize], &mut pt).unwrap();
            assert_eq!(&pt[..n], format!("m{i}").as_bytes());
        }
    }

    /// `next_counter` is oblivious to the epoch ratchet: across the epoch
    /// boundary the promise still names exactly the counter each successful
    /// seal returns — the ratchet changes the key, never the counter.
    #[test]
    fn datagram_next_counter_spans_epoch_boundary() {
        let size = epoch(4);
        let (mut send, _) = fixed_epoch_transport().into_datagram_with_epoch(size);

        // Counters 0..=5 cross the N = 4 boundary into epoch 1.
        let mut ct = [0u8; 64];
        for i in 0..6u64 {
            assert_eq!(send.next_counter(), i);
            let (c, _) = send.encrypt_next(&[], b"m", &mut ct).unwrap();
            assert_eq!(c, i);
        }
        assert_eq!(send.next_counter(), 6);
    }

    /// A datagram from two epochs back — older than the retained previous
    /// key — is refused with the ordinary decrypt error, while a straggler
    /// from the immediately-preceding epoch still opens.
    #[test]
    fn datagram_old_epoch_refused() {
        let size = epoch(4);
        let (mut send, _) = fixed_epoch_transport().into_datagram_with_epoch(size);

        let mut ctbuf = [0u8; 64];
        let mut sealed: Vec<Vec<u8>> = Vec::new();
        for i in 0..9u64 {
            let (c, n) = send
                .encrypt_next(&[], format!("m{i}").as_bytes(), &mut ctbuf)
                .unwrap();
            assert_eq!(c, i);
            sealed.push(ctbuf[..n].to_vec());
        }

        let (_, mut recv) = fixed_epoch_transport().into_datagram_with_epoch(size);
        let mut pt = [0u8; 64];

        // Jump straight to epoch 2 (steps = MAX_EPOCH_JUMP, allowed): commits
        // current = epoch 2, prev = epoch 1.
        let n = recv.decrypt_at(8, &[], &sealed[8], &mut pt).unwrap();
        assert_eq!(&pt[..n], b"m8");

        // A straggler from epoch 1 (counter 4) still opens — it is prev.
        let n = recv.decrypt_at(4, &[], &sealed[4], &mut pt).unwrap();
        assert_eq!(&pt[..n], b"m4");

        // Epoch 0 (counter 0) is now two epochs back; its key is gone.
        let err = recv.decrypt_at(0, &[], &sealed[0], &mut pt).unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));
    }

    /// A datagram more than `MAX_EPOCH_JUMP` epochs ahead is refused before
    /// any key is derived, even when genuine, and the committed state is left
    /// untouched so reachable epochs still open.
    #[test]
    fn datagram_forward_jump_cap_refuses_without_derivation() {
        let size = epoch(4);
        let (mut send, _) = fixed_epoch_transport().into_datagram_with_epoch(size);

        let mut ctbuf = [0u8; 64];
        let mut sealed: Vec<Vec<u8>> = Vec::new();
        for i in 0..13u64 {
            let (_, n) = send
                .encrypt_next(&[], format!("m{i}").as_bytes(), &mut ctbuf)
                .unwrap();
            sealed.push(ctbuf[..n].to_vec());
        }

        let (_, mut recv) = fixed_epoch_transport().into_datagram_with_epoch(size);
        let mut pt = [0u8; 64];

        // Counter 12 is epoch 3 = MAX_EPOCH_JUMP + 1 beyond the committed
        // epoch 0. It is refused WITHOUT deriving a key (the cap check returns
        // before any Rekey chain), even though the packet is genuine.
        let err = recv.decrypt_at(12, &[], &sealed[12], &mut pt).unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));

        // Committed state is untouched: an epoch-0 packet still opens as
        // current, and the receiver can still advance to a reachable epoch.
        let n = recv.decrypt_at(0, &[], &sealed[0], &mut pt).unwrap();
        assert_eq!(&pt[..n], b"m0");
        let n = recv.decrypt_at(8, &[], &sealed[8], &mut pt).unwrap();
        assert_eq!(&pt[..n], b"m8");
    }

    /// A forged packet claiming a valid future epoch (within the cap) opens
    /// under a candidate key, fails the AEAD tag, and does NOT advance the
    /// committed state — the one-packet-desync guard.
    #[test]
    fn datagram_forged_future_tag_does_not_advance() {
        let size = epoch(4);
        let (mut send, _) = fixed_epoch_transport().into_datagram_with_epoch(size);

        let mut ctbuf = [0u8; 64];
        let mut sealed: Vec<Vec<u8>> = Vec::new();
        for i in 0..6u64 {
            let (_, n) = send
                .encrypt_next(&[], format!("m{i}").as_bytes(), &mut ctbuf)
                .unwrap();
            sealed.push(ctbuf[..n].to_vec());
        }

        let (_, mut recv) = fixed_epoch_transport().into_datagram_with_epoch(size);
        let mut pt = [0u8; 64];

        // Forge an epoch-1 packet (counter 4, within the cap) with a corrupt
        // ciphertext. The candidate derivation runs, the tag fails, and the
        // committed keys must stay at epoch 0.
        let mut forged = sealed[4].clone();
        forged[0] ^= 0xFF;
        let err = recv.decrypt_at(4, &[], &forged, &mut pt).unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));

        // Proof of no advance: an epoch-0 packet still opens as current, and a
        // genuine epoch-1 packet still triggers a fresh, successful commit.
        let n = recv.decrypt_at(0, &[], &sealed[0], &mut pt).unwrap();
        assert_eq!(&pt[..n], b"m0");
        let n = recv.decrypt_at(4, &[], &sealed[4], &mut pt).unwrap();
        assert_eq!(&pt[..n], b"m4");
        let n = recv.decrypt_at(5, &[], &sealed[5], &mut pt).unwrap();
        assert_eq!(&pt[..n], b"m5");
    }

    /// An epoch-ratcheting sender still refuses to seal at counter 2^64 − 1.
    /// A huge epoch size keeps the ratchet target trivial so the shared
    /// exhaustion guard — not the ratchet loop — is what fires.
    #[test]
    fn datagram_epoch_seal_refuses_nonce_exhaustion() {
        let big = NonZeroU64::new(u64::MAX).unwrap();
        let (mut send, _) = fixed_epoch_transport().into_datagram_with_epoch(big);

        // One short of the cap: succeeds, reports u64::MAX − 1.
        send.set_counter_for_test(u64::MAX - 1);
        let mut ct = [0u8; 64];
        let (c, _) = send.encrypt_next(&[], b"x", &mut ct).unwrap();
        assert_eq!(c, u64::MAX - 1);

        // Counter is now u64::MAX: the next seal must refuse and write nothing.
        let mut ct2 = [0xABu8; 64];
        let err = send.encrypt_next(&[], b"x", &mut ct2).unwrap_err();
        assert!(matches!(err, error::HandshakeError::NonceOverflow));
        assert_eq!(ct2, [0xABu8; 64]);
    }

    #[test]
    fn transport_rekey_desync_rejected() {
        let psk = Psk::from_bytes([0x22; 32]);
        let (mut i_transport, mut r_transport) = complete_ikpsk1(&psk);

        // Only initiator rekeys — responder does not.
        i_transport.rekey().unwrap();

        // Messages encrypted with the new key cannot be decrypted
        // by the responder still using the old key.
        let mut ct = [0u8; 256];
        let ct_len = i_transport.send(b"desynced", &mut ct).unwrap();

        let mut pt = [0u8; 256];
        let err = r_transport.receive(&ct[..ct_len], &mut pt).unwrap_err();
        assert!(matches!(err, error::HandshakeError::DecryptionFailed));
    }

    #[test]
    fn rekey_without_key_fails() {
        let mut cs = cipher_state::CipherState::<ChaChaPoly>::empty();
        let err = cs.rekey().unwrap_err();
        assert!(matches!(err, error::HandshakeError::RekeyWithoutKey));
    }

    // ── Prologue tests ────────────────────────────────────────────────

    #[test]
    fn matching_prologue_succeeds() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        let responder_static = provider.generate::<P256>().unwrap();
        let responder_pub = provider.public(&responder_static).unwrap();

        let prologue = b"hiss/v1";

        let (msg, mut i_transport) = N::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            prologue,
            responder_pub,
        )
        .write_message_1()
        .unwrap();

        let mut r_transport = N::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            prologue,
            responder_static,
        )
        .unwrap()
        .read_message_1(&msg)
        .unwrap();

        // Transport works with matching prologue.
        let mut ct = [0u8; 64];
        let mut pt = [0u8; 64];
        let ct_len = i_transport.send(b"hello", &mut ct).unwrap();
        let pt_len = r_transport.receive(&ct[..ct_len], &mut pt).unwrap();
        assert_eq!(&pt[..pt_len], b"hello");
    }

    #[test]
    fn mismatched_prologue_rejected() {
        let mut provider = EphemeralOnly::new(rand::make_rng::<StdRng>());

        let responder_static = provider.generate::<P256>().unwrap();
        let responder_pub = provider.public(&responder_static).unwrap();

        // Initiator uses prologue "v1", responder uses "v2".
        let (msg, _i_transport) = N::initiator(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            b"v1",
            responder_pub,
        )
        .write_message_1()
        .unwrap();

        // The read fails because the handshake hashes diverge on the
        // differing prologues — the payload AEAD tag will not match.
        let outcome = N::responder(
            EphemeralOnly::new(rand::make_rng::<StdRng>()),
            b"v2",
            responder_static,
        )
        .unwrap()
        .read_message_1(&msg);
        assert!(matches!(outcome, Err(HandshakeError::DecryptionFailed)));
    }

    // ── Output buffer too small ─────────────────────────────────────

    #[test]
    fn chacha_encrypt_output_buffer_too_small() {
        let key = [0u8; 32];
        let plaintext = b"hello world";
        let mut output = [0u8; 10]; // needs 11 + 16 = 27
        let err = ChaChaPoly::encrypt(&ChaChaPoly::key(&key), 0, &[], plaintext, &mut output)
            .unwrap_err();
        assert!(matches!(
            err,
            error::HandshakeError::OutputBufferTooSmall { .. }
        ));
    }

    #[test]
    fn chacha_decrypt_output_buffer_too_small() {
        let key = [0u8; 32];
        // First encrypt something.
        let plaintext = b"hello world";
        let mut ct = [0u8; 64];
        let ct_len =
            ChaChaPoly::encrypt(&ChaChaPoly::key(&key), 0, &[], plaintext, &mut ct).unwrap();

        // Try to decrypt into a too-small buffer.
        let mut output = [0u8; 5]; // needs 11 bytes
        let err = ChaChaPoly::decrypt(&ChaChaPoly::key(&key), 0, &[], &ct[..ct_len], &mut output)
            .unwrap_err();
        assert!(matches!(
            err,
            error::HandshakeError::OutputBufferTooSmall { .. }
        ));
    }

    // ── Property-based tests ──────────────────────────────────────────

    mod prop {
        use super::*;
        use crate::psk::Psk;
        use proptest::prelude::*;

        /// Complete an IKpsk1 handshake with the given keys and PSK,
        /// returning both transport states.
        fn full_ikpsk1_handshake(
            initiator_static: P256r1PrivateKey,
            responder_static: P256r1PrivateKey,
            psk: Psk,
        ) -> (transport::Transport<IKpsk1>, transport::Transport<IKpsk1>) {
            let provider = EphemeralOnly::new(rand::make_rng::<StdRng>());
            let responder_pub = provider.public(&responder_static).unwrap();

            let (msg1, i_hs) = IKpsk1::initiator(
                EphemeralOnly::new(rand::make_rng::<StdRng>()),
                &[],
                responder_pub,
            )
            .write_message_1(initiator_static, &psk)
            .unwrap();

            let r_hs = IKpsk1::responder(
                EphemeralOnly::new(rand::make_rng::<StdRng>()),
                &[],
                responder_static,
            )
            .unwrap()
            .read_message_1(&msg1, &psk)
            .unwrap();

            let (msg2, r_transport) = r_hs.write_message_2().unwrap();
            let i_transport = i_hs.read_message_2(&msg2).unwrap();

            (i_transport, r_transport)
        }

        proptest! {
            /// Any plaintext (up to 4 KiB) survives an IKpsk1 round-trip.
            #[test]
            fn transport_any_payload(
                plaintext in proptest::collection::vec(any::<u8>(), 0..4096),
                psk in any::<[u8; 32]>().prop_map(Psk::from_bytes),
            ) {
                let i_sk = EphemeralOnly::new(rand::make_rng::<StdRng>()).generate::<P256>().unwrap();
                let r_sk = EphemeralOnly::new(rand::make_rng::<StdRng>()).generate::<P256>().unwrap();

                let (mut i_t, mut r_t) = full_ikpsk1_handshake(i_sk, r_sk, psk);

                // Initiator → responder.
                let mut ct = vec![0u8; plaintext.len() + 16];
                let ct_len = i_t.send(&plaintext, &mut ct).unwrap();
                let mut pt = vec![0u8; plaintext.len()];
                let pt_len = r_t.receive(&ct[..ct_len], &mut pt).unwrap();
                prop_assert_eq!(&pt[..pt_len], &plaintext[..]);

                // Responder → initiator.
                let ct_len = r_t.send(&plaintext, &mut ct).unwrap();
                let pt_len = i_t.receive(&ct[..ct_len], &mut pt).unwrap();
                prop_assert_eq!(&pt[..pt_len], &plaintext[..]);
            }

            /// Corrupting any single byte in a transport ciphertext causes
            /// decryption failure.
            #[test]
            fn transport_any_corruption_detected(
                plaintext in proptest::collection::vec(any::<u8>(), 1..512),
                psk in any::<[u8; 32]>().prop_map(Psk::from_bytes),
                corrupt_pos_seed in any::<usize>(),
            ) {
                let i_sk = EphemeralOnly::new(rand::make_rng::<StdRng>()).generate::<P256>().unwrap();
                let r_sk = EphemeralOnly::new(rand::make_rng::<StdRng>()).generate::<P256>().unwrap();

                let (mut i_t, mut r_t) = full_ikpsk1_handshake(i_sk, r_sk, psk);

                let mut ct = vec![0u8; plaintext.len() + 16];
                let ct_len = i_t.send(&plaintext, &mut ct).unwrap();

                // Corrupt a single byte at a random position.
                let pos = corrupt_pos_seed % ct_len;
                ct[pos] ^= 0x01;

                let mut pt = vec![0u8; plaintext.len()];
                let result = r_t.receive(&ct[..ct_len], &mut pt);
                prop_assert!(result.is_err());
            }

            /// Random bytes fed as a handshake msg1 are always rejected.
            #[test]
            fn random_msg1_rejected(
                garbage in proptest::collection::vec(any::<u8>(), 162..163),
            ) {
                let r_sk = EphemeralOnly::new(rand::make_rng::<StdRng>()).generate::<P256>().unwrap();
                let garbage: [u8; IKpsk1::MSG1_SIZE] =
                    garbage.try_into().expect("generated at the wire size");

                // Random bytes of the correct length — rejected either as
                // an invalid curve point at the `e` token, or by the AEAD
                // tag on the encrypted static at `s`. Either way the read
                // fails and no handshake state comes back.
                let outcome = IKpsk1::responder(
                    EphemeralOnly::new(rand::make_rng::<StdRng>()),
                    &[],
                    r_sk,
                )
                .unwrap()
                .read_message_1(&garbage, &Psk::from_bytes([0u8; 32]));
                prop_assert!(outcome.is_err());
            }

            /// AEAD: encrypt then decrypt with random key, nonce, AD, and
            /// plaintext always round-trips.
            #[test]
            fn chacha_round_trip_any(
                key in any::<[u8; 32]>(),
                nonce in 0..1000u64,
                ad in proptest::collection::vec(any::<u8>(), 0..128),
                plaintext in proptest::collection::vec(any::<u8>(), 0..4096),
            ) {
                let mut ct = vec![0u8; plaintext.len() + 16];
                let ct_len = ChaChaPoly::encrypt(&ChaChaPoly::key(&key), nonce, &ad, &plaintext, &mut ct).unwrap();

                let mut pt = vec![0u8; plaintext.len()];
                let pt_len = ChaChaPoly::decrypt(&ChaChaPoly::key(&key), nonce, &ad, &ct[..ct_len], &mut pt).unwrap();
                prop_assert_eq!(&pt[..pt_len], &plaintext[..]);
            }

            /// AEAD: flipping any single bit in ciphertext causes decryption
            /// failure.
            #[test]
            fn chacha_any_bit_flip_detected(
                key in any::<[u8; 32]>(),
                plaintext in proptest::collection::vec(any::<u8>(), 1..256),
                flip_pos_seed in any::<usize>(),
                flip_bit in 0u8..8,
            ) {
                let mut ct = vec![0u8; plaintext.len() + 16];
                let ct_len = ChaChaPoly::encrypt(&ChaChaPoly::key(&key), 0, &[], &plaintext, &mut ct).unwrap();

                let pos = flip_pos_seed % ct_len;
                ct[pos] ^= 1 << flip_bit;

                let mut pt = vec![0u8; plaintext.len()];
                let result = ChaChaPoly::decrypt(&ChaChaPoly::key(&key), 0, &[], &ct[..ct_len], &mut pt);
                prop_assert!(result.is_err());
            }
        }
    }
}