crypto-profile 0.9.0

Named cryptographic profiles over the AWS-LC FIPS module: algorithm types, per-module capabilities and a conformance suite
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
//! The algorithms the crate names: signature keys, TLS signature schemes, key-exchange groups,
//! cipher suites, hashes and TLS versions.
use std::fmt;
use std::str::FromStr;

/// A signature key algorithm: what a key is generated as, and what its CSR is signed with.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SignatureAlgorithm {
    /// ML-DSA-87 (FIPS 204).
    MlDsa87,
    /// ECDSA on P-256 with SHA-256.
    EcdsaP256Sha256,
    /// ECDSA on P-384 with SHA-384.
    EcdsaP384Sha384,
    /// ECDSA on P-521 with SHA-384.
    EcdsaP521Sha384,
    /// ECDSA on P-521 with SHA-512.
    EcdsaP521Sha512,
    /// Ed25519 (FIPS 186-5 EdDSA).
    Ed25519,
    /// A 2048-bit RSA key; its CSR is signed RSASSA-PKCS1-v1_5 with SHA-384.
    Rsa2048Sha384,
    /// A 3072-bit RSA key; its CSR is signed RSASSA-PKCS1-v1_5 with SHA-384.
    Rsa3072Sha384,
    /// A 4096-bit RSA key; its CSR is signed RSASSA-PKCS1-v1_5 with SHA-384.
    Rsa4096Sha384,
}

impl SignatureAlgorithm {
    /// Every signature key algorithm.
    pub const ALL: [SignatureAlgorithm; 9] = [
        SignatureAlgorithm::MlDsa87,
        SignatureAlgorithm::EcdsaP256Sha256,
        SignatureAlgorithm::EcdsaP384Sha384,
        SignatureAlgorithm::EcdsaP521Sha384,
        SignatureAlgorithm::EcdsaP521Sha512,
        SignatureAlgorithm::Ed25519,
        SignatureAlgorithm::Rsa2048Sha384,
        SignatureAlgorithm::Rsa3072Sha384,
        SignatureAlgorithm::Rsa4096Sha384,
    ];

    /// The position of `self` in [`Self::ALL`].
    #[cfg(test)]
    pub(crate) const fn ordinal(self) -> usize {
        match self {
            SignatureAlgorithm::MlDsa87 => 0,
            SignatureAlgorithm::EcdsaP256Sha256 => 1,
            SignatureAlgorithm::EcdsaP384Sha384 => 2,
            SignatureAlgorithm::EcdsaP521Sha384 => 3,
            SignatureAlgorithm::EcdsaP521Sha512 => 4,
            SignatureAlgorithm::Ed25519 => 5,
            SignatureAlgorithm::Rsa2048Sha384 => 6,
            SignatureAlgorithm::Rsa3072Sha384 => 7,
            SignatureAlgorithm::Rsa4096Sha384 => 8,
        }
    }

    /// The algorithm's stable configuration name.
    pub const fn as_str(self) -> &'static str {
        match self {
            SignatureAlgorithm::MlDsa87 => "ml-dsa-87",
            SignatureAlgorithm::EcdsaP256Sha256 => "ecdsa-p256-sha256",
            SignatureAlgorithm::EcdsaP384Sha384 => "ecdsa-p384-sha384",
            SignatureAlgorithm::EcdsaP521Sha384 => "ecdsa-p521-sha384",
            SignatureAlgorithm::EcdsaP521Sha512 => "ecdsa-p521-sha512",
            SignatureAlgorithm::Ed25519 => "ed25519",
            SignatureAlgorithm::Rsa2048Sha384 => "rsa2048-sha384",
            SignatureAlgorithm::Rsa3072Sha384 => "rsa3072-sha384",
            SignatureAlgorithm::Rsa4096Sha384 => "rsa4096-sha384",
        }
    }

    /// The X.509 signature-algorithm OID a CSR or certificate signed by this key carries
    /// (RFC 5758, RFC 8410, RFC 4055, and draft-ietf-lamps-dilithium-certificates for ML-DSA).
    pub const fn signature_oid(self) -> &'static str {
        match self {
            SignatureAlgorithm::MlDsa87 => "2.16.840.1.101.3.4.3.19",
            SignatureAlgorithm::EcdsaP256Sha256 => "1.2.840.10045.4.3.2",
            SignatureAlgorithm::EcdsaP384Sha384 | SignatureAlgorithm::EcdsaP521Sha384 => {
                "1.2.840.10045.4.3.3"
            }
            SignatureAlgorithm::EcdsaP521Sha512 => "1.2.840.10045.4.3.4",
            SignatureAlgorithm::Ed25519 => "1.3.101.112",
            SignatureAlgorithm::Rsa2048Sha384
            | SignatureAlgorithm::Rsa3072Sha384
            | SignatureAlgorithm::Rsa4096Sha384 => "1.2.840.113549.1.1.12",
        }
    }

    /// The TLS signature scheme this crate pairs with a key of this algorithm: the scheme the key
    /// signs a handshake with when the peer offers this scheme alone.
    ///
    /// An ECDSA, Ed25519 or ML-DSA key has no other scheme. An RSA key's scheme comes from the
    /// peer's offer, whatever the key's CSR was signed with: rustls signs with the first of RSA-PSS
    /// with SHA-512, SHA-384 and SHA-256, then PKCS#1 v1.5 with SHA-512, SHA-384 and SHA-256, that
    /// the peer offers (`RSA_SCHEMES` in `crypto-profile/src/provider/sign.rs`, the order rustls
    /// 0.23.46 uses), and TLS 1.3 admits only the RSA-PSS ones (`src/enums.rs:554-579`). So an RSA
    /// key signs with RSA-PSS with SHA-384 only when the peer offers it and does not offer RSA-PSS
    /// with SHA-512.
    pub const fn tls_scheme(self) -> TlsSignatureScheme {
        match self {
            SignatureAlgorithm::MlDsa87 => TlsSignatureScheme::MlDsa87,
            SignatureAlgorithm::EcdsaP256Sha256 => TlsSignatureScheme::EcdsaNistp256Sha256,
            SignatureAlgorithm::EcdsaP384Sha384 => TlsSignatureScheme::EcdsaNistp384Sha384,
            SignatureAlgorithm::EcdsaP521Sha384 | SignatureAlgorithm::EcdsaP521Sha512 => {
                TlsSignatureScheme::EcdsaNistp521Sha512
            }
            SignatureAlgorithm::Ed25519 => TlsSignatureScheme::Ed25519,
            SignatureAlgorithm::Rsa2048Sha384
            | SignatureAlgorithm::Rsa3072Sha384
            | SignatureAlgorithm::Rsa4096Sha384 => TlsSignatureScheme::RsaPssSha384,
        }
    }
}

/// A name that is not one of the crate's algorithms.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseAlgorithmError(pub String);

impl fmt::Display for ParseAlgorithmError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "unknown signature algorithm {:?}", self.0)
    }
}

impl std::error::Error for ParseAlgorithmError {}

impl FromStr for SignatureAlgorithm {
    type Err = ParseAlgorithmError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        SignatureAlgorithm::ALL
            .into_iter()
            .find(|a| a.as_str() == s)
            .ok_or_else(|| ParseAlgorithmError(s.to_string()))
    }
}

impl fmt::Display for SignatureAlgorithm {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A TLS handshake signature scheme.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TlsSignatureScheme {
    /// `ecdsa_secp256r1_sha256`.
    EcdsaNistp256Sha256,
    /// `ecdsa_secp384r1_sha384`.
    EcdsaNistp384Sha384,
    /// `ecdsa_secp521r1_sha512`.
    EcdsaNistp521Sha512,
    /// `ed25519`.
    Ed25519,
    /// `rsa_pss_rsae_sha256`.
    RsaPssSha256,
    /// `rsa_pss_rsae_sha384`.
    RsaPssSha384,
    /// `rsa_pss_rsae_sha512`.
    RsaPssSha512,
    /// `rsa_pkcs1_sha256`.
    RsaPkcs1Sha256,
    /// `rsa_pkcs1_sha384`.
    RsaPkcs1Sha384,
    /// `rsa_pkcs1_sha512`.
    RsaPkcs1Sha512,
    /// `mldsa87`.
    MlDsa87,
}

impl TlsSignatureScheme {
    /// Whether a handshake at `v` can sign with this scheme. RFC 8446 §4.2.3 excludes RSA
    /// PKCS#1 v1.5 from TLS 1.3 handshake signatures; ML-DSA is defined for TLS 1.3 only.
    pub const fn usable_in(self, v: TlsVersion) -> bool {
        match self {
            TlsSignatureScheme::RsaPkcs1Sha256
            | TlsSignatureScheme::RsaPkcs1Sha384
            | TlsSignatureScheme::RsaPkcs1Sha512 => matches!(v, TlsVersion::Tls12),
            TlsSignatureScheme::MlDsa87 => matches!(v, TlsVersion::Tls13),
            TlsSignatureScheme::EcdsaNistp256Sha256
            | TlsSignatureScheme::EcdsaNistp384Sha384
            | TlsSignatureScheme::EcdsaNistp521Sha512
            | TlsSignatureScheme::Ed25519
            | TlsSignatureScheme::RsaPssSha256
            | TlsSignatureScheme::RsaPssSha384
            | TlsSignatureScheme::RsaPssSha512 => true,
        }
    }

    /// The IANA TLS SignatureScheme codepoint.
    pub const fn iana(self) -> u16 {
        match self {
            TlsSignatureScheme::EcdsaNistp256Sha256 => 0x0403,
            TlsSignatureScheme::EcdsaNistp384Sha384 => 0x0503,
            TlsSignatureScheme::EcdsaNistp521Sha512 => 0x0603,
            TlsSignatureScheme::Ed25519 => 0x0807,
            TlsSignatureScheme::RsaPssSha256 => 0x0804,
            TlsSignatureScheme::RsaPssSha384 => 0x0805,
            TlsSignatureScheme::RsaPssSha512 => 0x0806,
            TlsSignatureScheme::RsaPkcs1Sha256 => 0x0401,
            TlsSignatureScheme::RsaPkcs1Sha384 => 0x0501,
            TlsSignatureScheme::RsaPkcs1Sha512 => 0x0601,
            TlsSignatureScheme::MlDsa87 => 0x0906,
        }
    }

    /// The scheme's rank in the canonical order every profile lists its schemes in, and in which a
    /// backend carries a policy's schemes: 0 first, one rank per scheme.
    pub const fn preference(self) -> u8 {
        match self {
            TlsSignatureScheme::MlDsa87 => 0,
            TlsSignatureScheme::EcdsaNistp384Sha384 => 1,
            TlsSignatureScheme::EcdsaNistp256Sha256 => 2,
            TlsSignatureScheme::EcdsaNistp521Sha512 => 3,
            TlsSignatureScheme::Ed25519 => 4,
            TlsSignatureScheme::RsaPssSha384 => 5,
            TlsSignatureScheme::RsaPssSha512 => 6,
            TlsSignatureScheme::RsaPssSha256 => 7,
            TlsSignatureScheme::RsaPkcs1Sha384 => 8,
            TlsSignatureScheme::RsaPkcs1Sha512 => 9,
            TlsSignatureScheme::RsaPkcs1Sha256 => 10,
        }
    }

    /// Every TLS signature scheme.
    pub const ALL: [TlsSignatureScheme; 11] = [
        TlsSignatureScheme::EcdsaNistp256Sha256,
        TlsSignatureScheme::EcdsaNistp384Sha384,
        TlsSignatureScheme::EcdsaNistp521Sha512,
        TlsSignatureScheme::Ed25519,
        TlsSignatureScheme::RsaPssSha256,
        TlsSignatureScheme::RsaPssSha384,
        TlsSignatureScheme::RsaPssSha512,
        TlsSignatureScheme::RsaPkcs1Sha256,
        TlsSignatureScheme::RsaPkcs1Sha384,
        TlsSignatureScheme::RsaPkcs1Sha512,
        TlsSignatureScheme::MlDsa87,
    ];

    /// The position of `self` in [`Self::ALL`].
    #[cfg(test)]
    pub(crate) const fn ordinal(self) -> usize {
        match self {
            TlsSignatureScheme::EcdsaNistp256Sha256 => 0,
            TlsSignatureScheme::EcdsaNistp384Sha384 => 1,
            TlsSignatureScheme::EcdsaNistp521Sha512 => 2,
            TlsSignatureScheme::Ed25519 => 3,
            TlsSignatureScheme::RsaPssSha256 => 4,
            TlsSignatureScheme::RsaPssSha384 => 5,
            TlsSignatureScheme::RsaPssSha512 => 6,
            TlsSignatureScheme::RsaPkcs1Sha256 => 7,
            TlsSignatureScheme::RsaPkcs1Sha384 => 8,
            TlsSignatureScheme::RsaPkcs1Sha512 => 9,
            TlsSignatureScheme::MlDsa87 => 10,
        }
    }
}

/// A TLS cipher suite.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CipherSuite {
    /// `TLS_AES_256_GCM_SHA384`.
    Tls13Aes256GcmSha384,
    /// `TLS_AES_128_GCM_SHA256`.
    Tls13Aes128GcmSha256,
    /// `TLS_CHACHA20_POLY1305_SHA256`.
    Tls13Chacha20Poly1305Sha256,
    /// `TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384`.
    Tls12EcdheEcdsaAes256GcmSha384,
    /// `TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256`.
    Tls12EcdheEcdsaAes128GcmSha256,
    /// `TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256`.
    Tls12EcdheEcdsaChacha20Poly1305Sha256,
    /// `TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384`.
    Tls12EcdheRsaAes256GcmSha384,
    /// `TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256`.
    Tls12EcdheRsaAes128GcmSha256,
    /// `TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256`.
    Tls12EcdheRsaChacha20Poly1305Sha256,
}

impl CipherSuite {
    /// Whether a handshake under this suite can sign with `t`. A TLS 1.2 suite fixes the
    /// authentication algorithm (ECDSA or EdDSA for `EcdheEcdsa`, RSA for `EcdheRsa`); a TLS 1.3
    /// suite does not.
    pub const fn signs_with(self, t: TlsSignatureScheme) -> bool {
        use TlsSignatureScheme as T;
        match self {
            CipherSuite::Tls13Aes256GcmSha384
            | CipherSuite::Tls13Aes128GcmSha256
            | CipherSuite::Tls13Chacha20Poly1305Sha256 => t.usable_in(TlsVersion::Tls13),
            CipherSuite::Tls12EcdheEcdsaAes256GcmSha384
            | CipherSuite::Tls12EcdheEcdsaAes128GcmSha256
            | CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256 => matches!(
                t,
                T::EcdsaNistp256Sha256
                    | T::EcdsaNistp384Sha384
                    | T::EcdsaNistp521Sha512
                    | T::Ed25519
            ),
            CipherSuite::Tls12EcdheRsaAes256GcmSha384
            | CipherSuite::Tls12EcdheRsaAes128GcmSha256
            | CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256 => matches!(
                t,
                T::RsaPssSha256
                    | T::RsaPssSha384
                    | T::RsaPssSha512
                    | T::RsaPkcs1Sha256
                    | T::RsaPkcs1Sha384
                    | T::RsaPkcs1Sha512
            ),
        }
    }

    /// Every cipher suite.
    pub const ALL: [CipherSuite; 9] = [
        CipherSuite::Tls13Aes256GcmSha384,
        CipherSuite::Tls13Aes128GcmSha256,
        CipherSuite::Tls13Chacha20Poly1305Sha256,
        CipherSuite::Tls12EcdheEcdsaAes256GcmSha384,
        CipherSuite::Tls12EcdheEcdsaAes128GcmSha256,
        CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256,
        CipherSuite::Tls12EcdheRsaAes256GcmSha384,
        CipherSuite::Tls12EcdheRsaAes128GcmSha256,
        CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256,
    ];

    /// The position of `self` in [`Self::ALL`].
    #[cfg(test)]
    pub(crate) const fn ordinal(self) -> usize {
        match self {
            CipherSuite::Tls13Aes256GcmSha384 => 0,
            CipherSuite::Tls13Aes128GcmSha256 => 1,
            CipherSuite::Tls13Chacha20Poly1305Sha256 => 2,
            CipherSuite::Tls12EcdheEcdsaAes256GcmSha384 => 3,
            CipherSuite::Tls12EcdheEcdsaAes128GcmSha256 => 4,
            CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256 => 5,
            CipherSuite::Tls12EcdheRsaAes256GcmSha384 => 6,
            CipherSuite::Tls12EcdheRsaAes128GcmSha256 => 7,
            CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256 => 8,
        }
    }

    /// The IANA TLS Cipher Suite codepoint.
    pub const fn iana(self) -> u16 {
        match self {
            CipherSuite::Tls13Aes256GcmSha384 => 0x1302,
            CipherSuite::Tls13Aes128GcmSha256 => 0x1301,
            CipherSuite::Tls13Chacha20Poly1305Sha256 => 0x1303,
            CipherSuite::Tls12EcdheEcdsaAes256GcmSha384 => 0xC02C,
            CipherSuite::Tls12EcdheEcdsaAes128GcmSha256 => 0xC02B,
            CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256 => 0xCCA9,
            CipherSuite::Tls12EcdheRsaAes256GcmSha384 => 0xC030,
            CipherSuite::Tls12EcdheRsaAes128GcmSha256 => 0xC02F,
            CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256 => 0xCCA8,
        }
    }

    /// The TLS version the suite belongs to.
    pub const fn version(self) -> TlsVersion {
        match self {
            CipherSuite::Tls13Aes256GcmSha384
            | CipherSuite::Tls13Aes128GcmSha256
            | CipherSuite::Tls13Chacha20Poly1305Sha256 => TlsVersion::Tls13,
            CipherSuite::Tls12EcdheEcdsaAes256GcmSha384
            | CipherSuite::Tls12EcdheEcdsaAes128GcmSha256
            | CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256
            | CipherSuite::Tls12EcdheRsaAes256GcmSha384
            | CipherSuite::Tls12EcdheRsaAes128GcmSha256
            | CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256 => TlsVersion::Tls12,
        }
    }
}

/// A TLS key-exchange group.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum KxGroup {
    /// ML-KEM-1024 (FIPS 203).
    MlKem1024,
    /// The P-384 + ML-KEM-1024 hybrid (RFC 10024 §4.1-4.3; codepoint §7.3).
    Secp384r1MlKem1024,
    /// The X25519 + ML-KEM-768 hybrid.
    X25519MlKem768,
    /// The P-256 + ML-KEM-768 hybrid.
    Secp256r1MlKem768,
    /// ECDH on P-384.
    Secp384r1,
    /// ECDH on P-256.
    Secp256r1,
    /// X25519.
    X25519,
}

impl KxGroup {
    /// Whether a handshake at `v` can use this group. TLS 1.2 has no ML-KEM or hybrid key
    /// exchange.
    pub const fn usable_in(self, v: TlsVersion) -> bool {
        match v {
            TlsVersion::Tls13 => true,
            TlsVersion::Tls12 => match self {
                KxGroup::Secp384r1 | KxGroup::Secp256r1 | KxGroup::X25519 => true,
                KxGroup::MlKem1024
                | KxGroup::Secp384r1MlKem1024
                | KxGroup::X25519MlKem768
                | KxGroup::Secp256r1MlKem768 => false,
            },
        }
    }

    /// Whether the group's key exchange includes ML-KEM: pure ML-KEM or a hybrid.
    pub const fn is_post_quantum(self) -> bool {
        match self {
            KxGroup::MlKem1024
            | KxGroup::Secp384r1MlKem1024
            | KxGroup::X25519MlKem768
            | KxGroup::Secp256r1MlKem768 => true,
            KxGroup::Secp384r1 | KxGroup::Secp256r1 | KxGroup::X25519 => false,
        }
    }

    /// The IANA TLS Supported Groups codepoint.
    pub const fn iana(self) -> u16 {
        match self {
            KxGroup::MlKem1024 => 0x0202,
            KxGroup::Secp384r1MlKem1024 => 0x11ED,
            KxGroup::X25519MlKem768 => 0x11EC,
            KxGroup::Secp256r1MlKem768 => 0x11EB,
            KxGroup::Secp384r1 => 0x0018,
            KxGroup::Secp256r1 => 0x0017,
            KxGroup::X25519 => 0x001D,
        }
    }

    /// Every key-exchange group.
    pub const ALL: [KxGroup; 7] = [
        KxGroup::MlKem1024,
        KxGroup::Secp384r1MlKem1024,
        KxGroup::X25519MlKem768,
        KxGroup::Secp256r1MlKem768,
        KxGroup::Secp384r1,
        KxGroup::Secp256r1,
        KxGroup::X25519,
    ];

    /// The position of `self` in [`Self::ALL`].
    #[cfg(test)]
    pub(crate) const fn ordinal(self) -> usize {
        match self {
            KxGroup::MlKem1024 => 0,
            KxGroup::Secp384r1MlKem1024 => 1,
            KxGroup::X25519MlKem768 => 2,
            KxGroup::Secp256r1MlKem768 => 3,
            KxGroup::Secp384r1 => 4,
            KxGroup::Secp256r1 => 5,
            KxGroup::X25519 => 6,
        }
    }
}

/// A hash function.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HashAlgorithm {
    /// SHA-256.
    Sha256,
    /// SHA-384.
    Sha384,
    /// SHA-512.
    Sha512,
}

impl HashAlgorithm {
    /// Every hash function.
    pub const ALL: [HashAlgorithm; 3] = [
        HashAlgorithm::Sha256,
        HashAlgorithm::Sha384,
        HashAlgorithm::Sha512,
    ];

    /// The position of `self` in [`Self::ALL`].
    #[cfg(test)]
    pub(crate) const fn ordinal(self) -> usize {
        match self {
            HashAlgorithm::Sha256 => 0,
            HashAlgorithm::Sha384 => 1,
            HashAlgorithm::Sha512 => 2,
        }
    }

    /// The digest length in bytes.
    pub const fn output_len(self) -> usize {
        match self {
            HashAlgorithm::Sha256 => 32,
            HashAlgorithm::Sha384 => 48,
            HashAlgorithm::Sha512 => 64,
        }
    }
}

/// A TLS protocol version.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TlsVersion {
    /// TLS 1.2.
    Tls12,
    /// TLS 1.3.
    Tls13,
}

impl TlsVersion {
    /// The TLS ProtocolVersion codepoint.
    pub const fn iana(self) -> u16 {
        match self {
            TlsVersion::Tls12 => 0x0303,
            TlsVersion::Tls13 => 0x0304,
        }
    }

    /// Every TLS version.
    pub const ALL: [TlsVersion; 2] = [TlsVersion::Tls12, TlsVersion::Tls13];

    /// The position of `self` in [`Self::ALL`].
    #[cfg(test)]
    pub(crate) const fn ordinal(self) -> usize {
        match self {
            TlsVersion::Tls12 => 0,
            TlsVersion::Tls13 => 1,
        }
    }
}

/// Any algorithm the capability table has a cell for.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Algorithm {
    /// A signature key algorithm.
    Signature(SignatureAlgorithm),
    /// A TLS signature scheme.
    Scheme(TlsSignatureScheme),
    /// A key-exchange group.
    Kx(KxGroup),
    /// A cipher suite.
    Cipher(CipherSuite),
    /// A hash function.
    Hash(HashAlgorithm),
    /// A TLS version.
    Protocol(TlsVersion),
}

impl Algorithm {
    /// Every algorithm, in family order.
    pub fn all() -> Vec<Algorithm> {
        let mut v = Vec::new();
        v.extend(SignatureAlgorithm::ALL.map(Algorithm::Signature));
        v.extend(TlsSignatureScheme::ALL.map(Algorithm::Scheme));
        v.extend(KxGroup::ALL.map(Algorithm::Kx));
        v.extend(CipherSuite::ALL.map(Algorithm::Cipher));
        v.extend(HashAlgorithm::ALL.map(Algorithm::Hash));
        v.extend(TlsVersion::ALL.map(Algorithm::Protocol));
        v
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn usability_helpers_decide_per_version() {
        use TlsSignatureScheme as T;
        assert!(KxGroup::Secp384r1.usable_in(TlsVersion::Tls12));
        assert!(!KxGroup::MlKem1024.usable_in(TlsVersion::Tls12));
        assert!(KxGroup::MlKem1024.usable_in(TlsVersion::Tls13));
        assert!(T::RsaPkcs1Sha256.usable_in(TlsVersion::Tls12));
        assert!(!T::RsaPkcs1Sha256.usable_in(TlsVersion::Tls13));
        assert!(T::MlDsa87.usable_in(TlsVersion::Tls13));
        assert!(!T::MlDsa87.usable_in(TlsVersion::Tls12));
        assert!(T::Ed25519.usable_in(TlsVersion::Tls12));
        let c = CipherSuite::Tls12EcdheEcdsaAes256GcmSha384;
        assert!(c.signs_with(T::EcdsaNistp384Sha384));
        assert!(!c.signs_with(T::RsaPssSha384));
        let r = CipherSuite::Tls12EcdheRsaAes256GcmSha384;
        assert!(r.signs_with(T::RsaPkcs1Sha384));
        assert!(!r.signs_with(T::Ed25519));
        let t13 = CipherSuite::Tls13Aes256GcmSha384;
        assert!(t13.signs_with(T::MlDsa87));
        assert!(!t13.signs_with(T::RsaPkcs1Sha384));
    }

    #[test]
    fn preference_is_the_canonical_order() {
        let mut all = TlsSignatureScheme::ALL;
        all.sort_by_key(|t| t.preference());
        assert_eq!(
            all.map(TlsSignatureScheme::iana),
            [
                0x0906, 0x0503, 0x0403, 0x0603, 0x0807, 0x0805, 0x0806, 0x0804, 0x0501, 0x0601,
                0x0401
            ]
        );
        let mut ranks = TlsSignatureScheme::ALL.map(TlsSignatureScheme::preference);
        ranks.sort_unstable();
        assert_eq!(ranks, std::array::from_fn::<u8, 11, _>(|i| i as u8));
    }

    #[test]
    fn post_quantum_groups_are_the_ml_kem_ones() {
        assert_eq!(
            KxGroup::ALL.map(KxGroup::is_post_quantum),
            [true, true, true, true, false, false, false]
        );
    }

    #[test]
    fn signature_names_are_stable_and_round_trip() {
        assert_eq!(
            SignatureAlgorithm::ALL.map(SignatureAlgorithm::as_str),
            [
                "ml-dsa-87",
                "ecdsa-p256-sha256",
                "ecdsa-p384-sha384",
                "ecdsa-p521-sha384",
                "ecdsa-p521-sha512",
                "ed25519",
                "rsa2048-sha384",
                "rsa3072-sha384",
                "rsa4096-sha384",
            ]
        );
        for a in SignatureAlgorithm::ALL {
            assert_eq!(a.as_str().parse::<SignatureAlgorithm>().unwrap(), a);
            assert_eq!(a.to_string(), a.as_str());
        }
        assert_eq!(
            ParseAlgorithmError("p384".to_string()).to_string(),
            "unknown signature algorithm \"p384\""
        );
        for bad in [
            "",
            "p384",
            "ML-DSA-87",
            "ml-dsa-65",
            "rsa-2048",
            "ecdsa-p256-sha384",
        ] {
            assert_eq!(
                bad.parse::<SignatureAlgorithm>(),
                Err(ParseAlgorithmError(bad.to_string())),
                "{bad}"
            );
        }
    }

    #[test]
    fn every_key_algorithm_has_its_tls_scheme() {
        use SignatureAlgorithm as S;
        use TlsSignatureScheme as T;
        let table = [
            (S::MlDsa87, T::MlDsa87),
            (S::EcdsaP256Sha256, T::EcdsaNistp256Sha256),
            (S::EcdsaP384Sha384, T::EcdsaNistp384Sha384),
            (S::EcdsaP521Sha384, T::EcdsaNistp521Sha512),
            (S::EcdsaP521Sha512, T::EcdsaNistp521Sha512),
            (S::Ed25519, T::Ed25519),
            (S::Rsa2048Sha384, T::RsaPssSha384),
            (S::Rsa3072Sha384, T::RsaPssSha384),
            (S::Rsa4096Sha384, T::RsaPssSha384),
        ];
        assert_eq!(table.len(), SignatureAlgorithm::ALL.len());
        for (s, t) in table {
            assert_eq!(s.tls_scheme(), t, "{s}");
        }
    }

    #[test]
    fn cipher_suites_know_their_version() {
        use TlsVersion::{Tls12, Tls13};
        assert_eq!(
            CipherSuite::ALL.map(CipherSuite::version),
            [
                Tls13, Tls13, Tls13, Tls12, Tls12, Tls12, Tls12, Tls12, Tls12
            ]
        );
    }

    #[test]
    fn iana_codepoints_are_the_registry_values() {
        use CipherSuite as C;
        use KxGroup as K;
        use TlsSignatureScheme as T;
        assert_eq!(
            C::ALL.map(C::iana),
            [
                0x1302, 0x1301, 0x1303, 0xC02C, 0xC02B, 0xCCA9, 0xC030, 0xC02F, 0xCCA8
            ]
        );
        assert_eq!(
            K::ALL.map(K::iana),
            [0x0202, 0x11ED, 0x11EC, 0x11EB, 0x0018, 0x0017, 0x001D]
        );
        assert_eq!(
            T::ALL.map(T::iana),
            [
                0x0403, 0x0503, 0x0603, 0x0807, 0x0804, 0x0805, 0x0806, 0x0401, 0x0501, 0x0601,
                0x0906
            ]
        );
        assert_eq!(TlsVersion::ALL.map(TlsVersion::iana), [0x0303, 0x0304]);
    }

    #[test]
    fn signature_oids_are_the_registry_values() {
        use SignatureAlgorithm as S;
        assert_eq!(
            S::ALL.map(S::signature_oid),
            [
                "2.16.840.1.101.3.4.3.19",
                "1.2.840.10045.4.3.2",
                "1.2.840.10045.4.3.3",
                "1.2.840.10045.4.3.3",
                "1.2.840.10045.4.3.4",
                "1.3.101.112",
                "1.2.840.113549.1.1.12",
                "1.2.840.113549.1.1.12",
                "1.2.840.113549.1.1.12",
            ]
        );
    }

    #[test]
    fn hash_lengths() {
        assert_eq!(
            HashAlgorithm::ALL.map(HashAlgorithm::output_len),
            [32, 48, 64]
        );
    }

    #[test]
    fn algorithm_all_is_every_variant_once() {
        let all = Algorithm::all();
        let expected = SignatureAlgorithm::ALL.len()
            + TlsSignatureScheme::ALL.len()
            + KxGroup::ALL.len()
            + CipherSuite::ALL.len()
            + HashAlgorithm::ALL.len()
            + TlsVersion::ALL.len();
        assert_eq!(all.len(), expected);
        assert_eq!(expected, 9 + 11 + 7 + 9 + 3 + 2);
        for (i, a) in all.iter().enumerate() {
            assert!(!all[i + 1..].contains(a), "{a:?} twice");
        }
    }

    #[test]
    fn signature_algorithm_all_lists_every_variant_in_ordinal_order() {
        assert_eq!(
            SignatureAlgorithm::ALL.map(SignatureAlgorithm::ordinal),
            std::array::from_fn::<usize, 9, _>(|i| i)
        );
    }

    #[test]
    fn tls_signature_scheme_all_lists_every_variant_in_ordinal_order() {
        assert_eq!(
            TlsSignatureScheme::ALL.map(TlsSignatureScheme::ordinal),
            std::array::from_fn::<usize, 11, _>(|i| i)
        );
    }

    #[test]
    fn cipher_suite_all_lists_every_variant_in_ordinal_order() {
        assert_eq!(
            CipherSuite::ALL.map(CipherSuite::ordinal),
            std::array::from_fn::<usize, 9, _>(|i| i)
        );
    }

    #[test]
    fn kx_group_all_lists_every_variant_in_ordinal_order() {
        assert_eq!(
            KxGroup::ALL.map(KxGroup::ordinal),
            std::array::from_fn::<usize, 7, _>(|i| i)
        );
    }

    #[test]
    fn hash_algorithm_all_lists_every_variant_in_ordinal_order() {
        assert_eq!(
            HashAlgorithm::ALL.map(HashAlgorithm::ordinal),
            std::array::from_fn::<usize, 3, _>(|i| i)
        );
    }

    #[test]
    fn tls_version_all_lists_every_variant_in_ordinal_order() {
        assert_eq!(
            TlsVersion::ALL.map(TlsVersion::ordinal),
            std::array::from_fn::<usize, 2, _>(|i| i)
        );
    }
}