Skip to main content

crypto_profile/
capability.rs

1//! What each AWS-LC FIPS module can do and what the published evidence says about each
2//! algorithm: the module's security policy, cited by table for every sourced cell, or, for a
3//! module with none published, one CAVP certificate, which is never an approval (decision 0004).
4use crate::algorithm::*;
5use crate::error::CryptoError;
6
7/// An AWS-LC FIPS module line.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9#[non_exhaustive]
10pub enum ModuleLine {
11    /// AWS-LC FIPS 3.x (aws-lc-rs 1.17).
12    AwsLcFips3,
13    /// AWS-LC FIPS 4.x (aws-lc-rs 1.18).
14    AwsLcFips4,
15}
16
17/// How the FIPS module is linked; each linkage is a separate module, with its own certificate or
18/// submission.
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20#[non_exhaustive]
21pub enum Linkage {
22    /// Linked into the binary.
23    Static,
24    /// Loaded as a shared library.
25    Dynamic,
26}
27
28/// An AWS-LC FIPS module: a line and a linkage.
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
30pub struct Module {
31    /// The module line.
32    pub line: ModuleLine,
33    /// The linkage.
34    pub linkage: Linkage,
35}
36
37/// A certificate's CMVP status as recorded when this crate version was released.
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39#[non_exhaustive]
40pub enum CertStatus {
41    /// On the CMVP validated list.
42    Validated,
43    /// Submitted and in CMVP review.
44    InProcess,
45    /// Moved to the CMVP historical list.
46    Historical,
47}
48
49/// The certificate a module carries.
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub struct Certificate {
52    /// The module's name, with the release for a 3.x line.
53    pub module_name: &'static str,
54    /// The certificate number, absent while in review.
55    pub number: Option<u32>,
56    /// The status when this crate version was released; the CMVP list is authoritative.
57    pub status: CertStatus,
58    /// The security policy URL, absent while none is published.
59    pub policy: Option<&'static str>,
60}
61
62/// A module's approval status for one algorithm, as its published evidence settles it.
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub enum Approval {
65    /// Listed as approved; `source` cites the table.
66    Approved {
67        /// The security-policy citation.
68        source: &'static str,
69    },
70    /// Not approved; `source` cites where the policy says so or omits it.
71    NotApproved {
72        /// The security-policy citation.
73        source: &'static str,
74    },
75    /// One published CAVP certificate for this module shows every operation the cell needs, with
76    /// the parameters `cavp` names, but no security policy lists it as approved. Never approved.
77    /// It covers only those parameters: rustls verifies a `Scheme` cell's signatures over a wider
78    /// range of keys (decision 0004 ยง5). Tested on that certificate's processor variant; which
79    /// variant a build uses depends on the CPU.
80    AlgorithmTested {
81        /// The certificate, and the capabilities and parameters it shows.
82        cavp: &'static str,
83    },
84    /// No published source settles it: no security policy, and no single CAVP certificate under
85    /// decision 0004. Treated as not approved everywhere.
86    Unverified,
87}
88
89impl Approval {
90    /// Whether the cell is approved. Only `Approved` is; `AlgorithmTested` and `Unverified` are not.
91    pub const fn is_approved(self) -> bool {
92        matches!(self, Approval::Approved { .. })
93    }
94}
95
96/// One cell of the capability table. A cipher suite cell speaks only to the suite's algorithms;
97/// whether its protocol version is approved is the `Protocol` cell.
98#[derive(Debug, Clone, Copy, PartialEq, Eq)]
99pub struct Capability {
100    /// The backend for this module implements the algorithm.
101    pub available: bool,
102    /// The module's approval status for it.
103    pub approval: Approval,
104}
105
106const POLICY_5314: &str = "https://csrc.nist.gov/CSRC/media/projects/cryptographic-module-validation-program/documents/security-policies/140sp5314.pdf";
107const POLICY_5298: &str = "https://csrc.nist.gov/CSRC/media/projects/cryptographic-module-validation-program/documents/security-policies/140sp5298.pdf";
108
109/// The certificate for a module.
110pub const fn certificate(m: Module) -> Certificate {
111    match (m.line, m.linkage) {
112        (ModuleLine::AwsLcFips3, Linkage::Static) => Certificate {
113            module_name: "AWS-LC 3 Cryptographic Module (static), AWS-LC FIPS 3.1.0",
114            number: Some(5314),
115            status: CertStatus::Validated,
116            policy: Some(POLICY_5314),
117        },
118        (ModuleLine::AwsLcFips3, Linkage::Dynamic) => Certificate {
119            module_name: "AWS-LC 3 Cryptographic Module (dynamic), AWS-LC FIPS 3.1.0",
120            number: Some(5298),
121            status: CertStatus::Validated,
122            policy: Some(POLICY_5298),
123        },
124        (ModuleLine::AwsLcFips4, Linkage::Static) => Certificate {
125            module_name: "AWS-LC 4 Cryptographic Module (static)",
126            number: None,
127            status: CertStatus::InProcess,
128            policy: None,
129        },
130        (ModuleLine::AwsLcFips4, Linkage::Dynamic) => Certificate {
131            module_name: "AWS-LC 4 Cryptographic Module (dynamic)",
132            number: None,
133            status: CertStatus::InProcess,
134            policy: None,
135        },
136    }
137}
138
139const fn table5(l: Linkage) -> &'static str {
140    match l {
141        Linkage::Static => {
142            "AWS-LC FIPS 3.1.0 Security Policy, cert #5314 (static), Table 5, pp. 8-11"
143        }
144        Linkage::Dynamic => {
145            "AWS-LC FIPS 3.1.0 Security Policy, cert #5298 (dynamic), Table 5, pp. 8-11"
146        }
147    }
148}
149
150const fn absent(l: Linkage) -> &'static str {
151    match l {
152        Linkage::Static => {
153            "AWS-LC FIPS 3.1.0 Security Policy, cert #5314 (static): not listed in Tables 5-8"
154        }
155        Linkage::Dynamic => {
156            "AWS-LC FIPS 3.1.0 Security Policy, cert #5298 (dynamic): not listed in Tables 5-8"
157        }
158    }
159}
160
161const fn three_x(l: Linkage, a: Algorithm) -> Capability {
162    let yes = Approval::Approved { source: table5(l) };
163    let no = Approval::NotApproved { source: absent(l) };
164    let (available, approval) = match a {
165        Algorithm::Signature(s) => match s {
166            SignatureAlgorithm::MlDsa87 => (false, no),
167            SignatureAlgorithm::EcdsaP256Sha256
168            | SignatureAlgorithm::EcdsaP384Sha384
169            | SignatureAlgorithm::EcdsaP521Sha384
170            | SignatureAlgorithm::EcdsaP521Sha512
171            | SignatureAlgorithm::Ed25519
172            | SignatureAlgorithm::Rsa2048Sha384
173            | SignatureAlgorithm::Rsa3072Sha384
174            | SignatureAlgorithm::Rsa4096Sha384 => (true, yes),
175        },
176        Algorithm::Scheme(s) => match s {
177            TlsSignatureScheme::MlDsa87 => (false, no),
178            TlsSignatureScheme::EcdsaNistp256Sha256
179            | TlsSignatureScheme::EcdsaNistp384Sha384
180            | TlsSignatureScheme::EcdsaNistp521Sha512
181            | TlsSignatureScheme::Ed25519
182            | TlsSignatureScheme::RsaPssSha256
183            | TlsSignatureScheme::RsaPssSha384
184            | TlsSignatureScheme::RsaPssSha512
185            | TlsSignatureScheme::RsaPkcs1Sha256
186            | TlsSignatureScheme::RsaPkcs1Sha384
187            | TlsSignatureScheme::RsaPkcs1Sha512 => (true, yes),
188        },
189        Algorithm::Kx(k) => match k {
190            KxGroup::MlKem1024 | KxGroup::Secp384r1 | KxGroup::Secp256r1 => (true, yes),
191            KxGroup::X25519 => (true, no),
192            KxGroup::X25519MlKem768 | KxGroup::Secp256r1MlKem768 | KxGroup::Secp384r1MlKem1024 => {
193                (true, Approval::Unverified)
194            }
195        },
196        Algorithm::Cipher(c) => match c {
197            CipherSuite::Tls13Aes256GcmSha384
198            | CipherSuite::Tls13Aes128GcmSha256
199            | CipherSuite::Tls12EcdheEcdsaAes256GcmSha384
200            | CipherSuite::Tls12EcdheEcdsaAes128GcmSha256
201            | CipherSuite::Tls12EcdheRsaAes256GcmSha384
202            | CipherSuite::Tls12EcdheRsaAes128GcmSha256 => (true, yes),
203            CipherSuite::Tls13Chacha20Poly1305Sha256
204            | CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256
205            | CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256 => (true, no),
206        },
207        Algorithm::Hash(h) => match h {
208            HashAlgorithm::Sha256 | HashAlgorithm::Sha384 | HashAlgorithm::Sha512 => (true, yes),
209        },
210        Algorithm::Protocol(v) => match v {
211            TlsVersion::Tls12 => (true, yes),
212            TlsVersion::Tls13 => (true, Approval::Unverified),
213        },
214    };
215    Capability {
216        available,
217        approval,
218    }
219}
220
221const fn four_x(l: Linkage, a: Algorithm) -> Capability {
222    Capability {
223        available: true,
224        approval: match l {
225            Linkage::Static => Approval::Unverified,
226            Linkage::Dynamic => four_x_dynamic(a),
227        },
228    }
229}
230
231const fn tested(cavp: &'static str) -> Approval {
232    Approval::AlgorithmTested { cavp }
233}
234
235/// Each `AlgorithmTested` cell cites the lowest-numbered AWS-LC FIPS 4.0.0 CAVP certificate
236/// (A7839-A7876, dynamic build) that alone shows every operation the cell needs, with the
237/// parameters the citation names (decision 0004).
238const fn four_x_dynamic(a: Algorithm) -> Approval {
239    match a {
240        Algorithm::Signature(s) => match s {
241            SignatureAlgorithm::MlDsa87 => {
242                tested("A7855 (ML-DSA KeyGen, SigGen, SigVer: ML-DSA-87)")
243            }
244            SignatureAlgorithm::Ed25519 => tested("A7855 (EDDSA KeyGen, SigGen, SigVer: ED-25519)"),
245            SignatureAlgorithm::Rsa2048Sha384 => tested(
246                "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 2048, SHA2-384)",
247            ),
248            SignatureAlgorithm::Rsa3072Sha384 => tested(
249                "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 3072, SHA2-384)",
250            ),
251            SignatureAlgorithm::Rsa4096Sha384 => tested(
252                "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 4096, SHA2-384)",
253            ),
254            SignatureAlgorithm::EcdsaP256Sha256
255            | SignatureAlgorithm::EcdsaP384Sha384
256            | SignatureAlgorithm::EcdsaP521Sha384
257            | SignatureAlgorithm::EcdsaP521Sha512 => Approval::Unverified,
258        },
259        Algorithm::Scheme(t) => match t {
260            TlsSignatureScheme::EcdsaNistp256Sha256 => {
261                tested("A7856 (ECDSA SigVer (FIPS186-5): P-256, SHA2-256)")
262            }
263            TlsSignatureScheme::EcdsaNistp384Sha384 => {
264                tested("A7856 (ECDSA SigVer (FIPS186-5): P-384, SHA2-384)")
265            }
266            TlsSignatureScheme::EcdsaNistp521Sha512 => {
267                tested("A7856 (ECDSA SigVer (FIPS186-5): P-521, SHA2-512)")
268            }
269            TlsSignatureScheme::Ed25519 => tested("A7855 (EDDSA SigVer: ED-25519)"),
270            TlsSignatureScheme::RsaPssSha256 => {
271                tested("A7856 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-256)")
272            }
273            TlsSignatureScheme::RsaPssSha384 => {
274                tested("A7856 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-384)")
275            }
276            TlsSignatureScheme::RsaPssSha512 => {
277                tested("A7856 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-512)")
278            }
279            TlsSignatureScheme::RsaPkcs1Sha256 => {
280                tested("A7856 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-256)")
281            }
282            TlsSignatureScheme::RsaPkcs1Sha384 => {
283                tested("A7856 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-384)")
284            }
285            TlsSignatureScheme::RsaPkcs1Sha512 => {
286                tested("A7856 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-512)")
287            }
288            TlsSignatureScheme::MlDsa87 => tested("A7855 (ML-DSA SigVer: ML-DSA-87)"),
289        },
290        Algorithm::Kx(k) => match k {
291            KxGroup::MlKem1024 => tested("A7855 (ML-KEM KeyGen, EncapDecap: ML-KEM-1024)"),
292            KxGroup::Secp384r1 => tested(
293                "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, P-384; ECDSA KeyGen (FIPS186-5): P-384)",
294            ),
295            KxGroup::Secp256r1 => tested(
296                "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, P-256; ECDSA KeyGen (FIPS186-5): P-256)",
297            ),
298            KxGroup::X25519MlKem768
299            | KxGroup::Secp256r1MlKem768
300            | KxGroup::Secp384r1MlKem1024
301            | KxGroup::X25519 => Approval::Unverified,
302        },
303        Algorithm::Cipher(c) => match c {
304            CipherSuite::Tls13Aes256GcmSha384
305            | CipherSuite::Tls12EcdheEcdsaAes256GcmSha384
306            | CipherSuite::Tls12EcdheRsaAes256GcmSha384
307            | CipherSuite::Tls13Aes128GcmSha256
308            | CipherSuite::Tls12EcdheEcdsaAes128GcmSha256
309            | CipherSuite::Tls12EcdheRsaAes128GcmSha256
310            | CipherSuite::Tls13Chacha20Poly1305Sha256
311            | CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256
312            | CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256 => Approval::Unverified,
313        },
314        Algorithm::Hash(h) => match h {
315            HashAlgorithm::Sha256 => tested("A7855 (SHA2-256)"),
316            HashAlgorithm::Sha384 => tested("A7855 (SHA2-384)"),
317            HashAlgorithm::Sha512 => tested("A7855 (SHA2-512)"),
318        },
319        Algorithm::Protocol(v) => match v {
320            TlsVersion::Tls12 | TlsVersion::Tls13 => Approval::Unverified,
321        },
322    }
323}
324
325/// What the backend for `m` can do for `a`, and what that module's published evidence says about
326/// it: its security policy, or one CAVP certificate (decision 0004).
327pub const fn capability(m: Module, a: Algorithm) -> Capability {
328    match m.line {
329        ModuleLine::AwsLcFips3 => three_x(m.linkage, a),
330        ModuleLine::AwsLcFips4 => four_x(m.linkage, a),
331    }
332}
333
334/// Refuses an algorithm the module's backend does not implement.
335pub fn ensure_available(m: Module, a: Algorithm) -> Result<(), CryptoError> {
336    if capability(m, a).available {
337        Ok(())
338    } else {
339        Err(CryptoError::Unavailable(a))
340    }
341}
342
343#[cfg(test)]
344mod tests {
345    use super::*;
346
347    const M3S: Module = Module {
348        line: ModuleLine::AwsLcFips3,
349        linkage: Linkage::Static,
350    };
351    const M3D: Module = Module {
352        line: ModuleLine::AwsLcFips3,
353        linkage: Linkage::Dynamic,
354    };
355    const M4S: Module = Module {
356        line: ModuleLine::AwsLcFips4,
357        linkage: Linkage::Static,
358    };
359    const M4D: Module = Module {
360        line: ModuleLine::AwsLcFips4,
361        linkage: Linkage::Dynamic,
362    };
363
364    fn modules() -> [Module; 4] {
365        [M3S, M3D, M4S, M4D]
366    }
367
368    #[test]
369    fn certificates_name_their_module() {
370        let c = certificate(M3S);
371        assert_eq!((c.number, c.status), (Some(5314), CertStatus::Validated));
372        assert!(c.policy.unwrap().ends_with("140sp5314.pdf"));
373        let c = certificate(M3D);
374        assert_eq!((c.number, c.status), (Some(5298), CertStatus::Validated));
375        assert!(c.policy.unwrap().ends_with("140sp5298.pdf"));
376        for m in [M4S, M4D] {
377            let c = certificate(m);
378            assert_eq!(
379                (c.number, c.status, c.policy),
380                (None, CertStatus::InProcess, None)
381            );
382        }
383    }
384
385    #[test]
386    fn every_sourced_cell_cites_its_own_certificate() {
387        for m in modules() {
388            for a in Algorithm::all() {
389                match capability(m, a).approval {
390                    Approval::Approved { source } | Approval::NotApproved { source } => {
391                        assert!(!source.is_empty(), "{m:?} {a:?}");
392                        let want = match m.linkage {
393                            Linkage::Static => "#5314",
394                            Linkage::Dynamic => "#5298",
395                        };
396                        assert!(source.contains(want), "{m:?} {a:?} cites {source}");
397                    }
398                    Approval::AlgorithmTested { cavp } => {
399                        assert_eq!(m, M4D, "{a:?}");
400                        assert!(cavp.starts_with("A78"), "{a:?} cites {cavp}");
401                        assert_eq!(cavp.matches("A78").count(), 1, "{a:?} cites {cavp}");
402                    }
403                    Approval::Unverified => {}
404                }
405            }
406        }
407    }
408
409    #[test]
410    fn four_x_static_is_available_and_unverified_everywhere() {
411        for a in Algorithm::all() {
412            assert_eq!(
413                capability(M4S, a),
414                Capability {
415                    available: true,
416                    approval: Approval::Unverified
417                },
418                "{a:?}"
419            );
420        }
421    }
422
423    #[test]
424    fn four_x_dynamic_cells_are_pinned_to_their_cavp_certificates() {
425        use Algorithm as A;
426        use CipherSuite as C;
427        use HashAlgorithm as H;
428        use KxGroup as K;
429        use SignatureAlgorithm as S;
430        use TlsSignatureScheme as T;
431        use TlsVersion as V;
432        let expected: [(Algorithm, Option<&'static str>); 41] = [
433            (
434                A::Signature(S::MlDsa87),
435                Some("A7855 (ML-DSA KeyGen, SigGen, SigVer: ML-DSA-87)"),
436            ),
437            (A::Signature(S::EcdsaP256Sha256), None),
438            (A::Signature(S::EcdsaP384Sha384), None),
439            (A::Signature(S::EcdsaP521Sha384), None),
440            (A::Signature(S::EcdsaP521Sha512), None),
441            (
442                A::Signature(S::Ed25519),
443                Some("A7855 (EDDSA KeyGen, SigGen, SigVer: ED-25519)"),
444            ),
445            (
446                A::Signature(S::Rsa2048Sha384),
447                Some(
448                    "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 2048, SHA2-384)",
449                ),
450            ),
451            (
452                A::Signature(S::Rsa3072Sha384),
453                Some(
454                    "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 3072, SHA2-384)",
455                ),
456            ),
457            (
458                A::Signature(S::Rsa4096Sha384),
459                Some(
460                    "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 4096, SHA2-384)",
461                ),
462            ),
463            (
464                A::Scheme(T::EcdsaNistp256Sha256),
465                Some("A7856 (ECDSA SigVer (FIPS186-5): P-256, SHA2-256)"),
466            ),
467            (
468                A::Scheme(T::EcdsaNistp384Sha384),
469                Some("A7856 (ECDSA SigVer (FIPS186-5): P-384, SHA2-384)"),
470            ),
471            (
472                A::Scheme(T::EcdsaNistp521Sha512),
473                Some("A7856 (ECDSA SigVer (FIPS186-5): P-521, SHA2-512)"),
474            ),
475            (
476                A::Scheme(T::Ed25519),
477                Some("A7855 (EDDSA SigVer: ED-25519)"),
478            ),
479            (
480                A::Scheme(T::RsaPssSha256),
481                Some("A7856 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-256)"),
482            ),
483            (
484                A::Scheme(T::RsaPssSha384),
485                Some("A7856 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-384)"),
486            ),
487            (
488                A::Scheme(T::RsaPssSha512),
489                Some("A7856 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-512)"),
490            ),
491            (
492                A::Scheme(T::RsaPkcs1Sha256),
493                Some("A7856 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-256)"),
494            ),
495            (
496                A::Scheme(T::RsaPkcs1Sha384),
497                Some("A7856 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-384)"),
498            ),
499            (
500                A::Scheme(T::RsaPkcs1Sha512),
501                Some("A7856 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-512)"),
502            ),
503            (
504                A::Scheme(T::MlDsa87),
505                Some("A7855 (ML-DSA SigVer: ML-DSA-87)"),
506            ),
507            (
508                A::Kx(K::MlKem1024),
509                Some("A7855 (ML-KEM KeyGen, EncapDecap: ML-KEM-1024)"),
510            ),
511            (A::Kx(K::Secp384r1MlKem1024), None),
512            (A::Kx(K::X25519MlKem768), None),
513            (A::Kx(K::Secp256r1MlKem768), None),
514            (
515                A::Kx(K::Secp384r1),
516                Some(
517                    "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, P-384; ECDSA KeyGen (FIPS186-5): P-384)",
518                ),
519            ),
520            (
521                A::Kx(K::Secp256r1),
522                Some(
523                    "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, P-256; ECDSA KeyGen (FIPS186-5): P-256)",
524                ),
525            ),
526            (A::Kx(K::X25519), None),
527            (A::Cipher(C::Tls13Aes256GcmSha384), None),
528            (A::Cipher(C::Tls13Aes128GcmSha256), None),
529            (A::Cipher(C::Tls13Chacha20Poly1305Sha256), None),
530            (A::Cipher(C::Tls12EcdheEcdsaAes256GcmSha384), None),
531            (A::Cipher(C::Tls12EcdheEcdsaAes128GcmSha256), None),
532            (A::Cipher(C::Tls12EcdheEcdsaChacha20Poly1305Sha256), None),
533            (A::Cipher(C::Tls12EcdheRsaAes256GcmSha384), None),
534            (A::Cipher(C::Tls12EcdheRsaAes128GcmSha256), None),
535            (A::Cipher(C::Tls12EcdheRsaChacha20Poly1305Sha256), None),
536            (A::Hash(H::Sha256), Some("A7855 (SHA2-256)")),
537            (A::Hash(H::Sha384), Some("A7855 (SHA2-384)")),
538            (A::Hash(H::Sha512), Some("A7855 (SHA2-512)")),
539            (A::Protocol(V::Tls12), None),
540            (A::Protocol(V::Tls13), None),
541        ];
542        assert!(
543            Algorithm::all()
544                .iter()
545                .all(|a| expected.iter().any(|(b, _)| b == a))
546        );
547        assert_eq!(expected.iter().filter(|(_, c)| c.is_some()).count(), 22);
548        for (a, cavp) in expected {
549            let approval = match cavp {
550                Some(cavp) => Approval::AlgorithmTested { cavp },
551                None => Approval::Unverified,
552            };
553            assert_eq!(
554                capability(M4D, a),
555                Capability {
556                    available: true,
557                    approval
558                },
559                "{a:?}"
560            );
561            assert!(!approval.is_approved(), "{a:?}");
562        }
563    }
564
565    #[test]
566    fn three_x_lacks_ml_dsa_only() {
567        for m in [M3S, M3D] {
568            for a in Algorithm::all() {
569                let ml_dsa = matches!(
570                    a,
571                    Algorithm::Signature(SignatureAlgorithm::MlDsa87)
572                        | Algorithm::Scheme(TlsSignatureScheme::MlDsa87)
573                );
574                assert_eq!(capability(m, a).available, !ml_dsa, "{m:?} {a:?}");
575            }
576        }
577    }
578
579    #[test]
580    fn three_x_approval_matches_table_5() {
581        use Algorithm as A;
582        let approved = |a| capability(M3S, a).approval.is_approved();
583        assert!(approved(A::Signature(SignatureAlgorithm::Ed25519)));
584        assert!(approved(A::Signature(SignatureAlgorithm::Rsa2048Sha384)));
585        assert!(approved(A::Signature(SignatureAlgorithm::EcdsaP521Sha384)));
586        assert!(approved(A::Kx(KxGroup::MlKem1024)));
587        assert!(approved(A::Kx(KxGroup::Secp384r1)));
588        assert!(approved(A::Cipher(CipherSuite::Tls13Aes128GcmSha256)));
589        assert!(approved(A::Hash(HashAlgorithm::Sha512)));
590        assert!(approved(A::Protocol(TlsVersion::Tls12)));
591        for a in [
592            A::Signature(SignatureAlgorithm::MlDsa87),
593            A::Scheme(TlsSignatureScheme::MlDsa87),
594            A::Kx(KxGroup::X25519),
595            A::Cipher(CipherSuite::Tls13Chacha20Poly1305Sha256),
596            A::Cipher(CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256),
597        ] {
598            assert!(
599                matches!(capability(M3S, a).approval, Approval::NotApproved { .. }),
600                "{a:?}"
601            );
602        }
603        for a in [
604            A::Kx(KxGroup::X25519MlKem768),
605            A::Kx(KxGroup::Secp256r1MlKem768),
606            A::Protocol(TlsVersion::Tls13),
607        ] {
608            assert_eq!(capability(M3S, a).approval, Approval::Unverified, "{a:?}");
609        }
610    }
611
612    #[test]
613    fn unverified_is_never_approved() {
614        assert!(!Approval::Unverified.is_approved());
615        assert!(!Approval::AlgorithmTested { cavp: "A7855" }.is_approved());
616        assert!(!Approval::NotApproved { source: "x" }.is_approved());
617        assert!(Approval::Approved { source: "x" }.is_approved());
618    }
619
620    #[test]
621    fn ensure_available_refuses_ml_dsa_on_3x() {
622        let a = Algorithm::Signature(SignatureAlgorithm::MlDsa87);
623        assert_eq!(ensure_available(M3S, a), Err(CryptoError::Unavailable(a)));
624        assert_eq!(ensure_available(M4S, a), Ok(()));
625    }
626
627    #[test]
628    fn three_x_table_is_pinned_cell_by_cell() {
629        use Algorithm as A;
630        use SignatureAlgorithm as S;
631        use TlsSignatureScheme as T;
632        #[derive(Debug, Clone, Copy, PartialEq)]
633        enum K {
634            Approved,
635            NotApproved,
636            Unverified,
637        }
638        use K::{Approved as Y, NotApproved as N, Unverified as U};
639        let expected: [(Algorithm, bool, K); 41] = [
640            (A::Signature(S::MlDsa87), false, N),
641            (A::Signature(S::EcdsaP256Sha256), true, Y),
642            (A::Signature(S::EcdsaP384Sha384), true, Y),
643            (A::Signature(S::EcdsaP521Sha384), true, Y),
644            (A::Signature(S::EcdsaP521Sha512), true, Y),
645            (A::Signature(S::Ed25519), true, Y),
646            (A::Signature(S::Rsa2048Sha384), true, Y),
647            (A::Signature(S::Rsa3072Sha384), true, Y),
648            (A::Signature(S::Rsa4096Sha384), true, Y),
649            (A::Scheme(T::EcdsaNistp256Sha256), true, Y),
650            (A::Scheme(T::EcdsaNistp384Sha384), true, Y),
651            (A::Scheme(T::EcdsaNistp521Sha512), true, Y),
652            (A::Scheme(T::Ed25519), true, Y),
653            (A::Scheme(T::RsaPssSha256), true, Y),
654            (A::Scheme(T::RsaPssSha384), true, Y),
655            (A::Scheme(T::RsaPssSha512), true, Y),
656            (A::Scheme(T::RsaPkcs1Sha256), true, Y),
657            (A::Scheme(T::RsaPkcs1Sha384), true, Y),
658            (A::Scheme(T::RsaPkcs1Sha512), true, Y),
659            (A::Scheme(T::MlDsa87), false, N),
660            (A::Kx(KxGroup::MlKem1024), true, Y),
661            (A::Kx(KxGroup::Secp384r1MlKem1024), true, U),
662            (A::Kx(KxGroup::X25519MlKem768), true, U),
663            (A::Kx(KxGroup::Secp256r1MlKem768), true, U),
664            (A::Kx(KxGroup::Secp384r1), true, Y),
665            (A::Kx(KxGroup::Secp256r1), true, Y),
666            (A::Kx(KxGroup::X25519), true, N),
667            (A::Cipher(CipherSuite::Tls13Aes256GcmSha384), true, Y),
668            (A::Cipher(CipherSuite::Tls13Aes128GcmSha256), true, Y),
669            (A::Cipher(CipherSuite::Tls13Chacha20Poly1305Sha256), true, N),
670            (
671                A::Cipher(CipherSuite::Tls12EcdheEcdsaAes256GcmSha384),
672                true,
673                Y,
674            ),
675            (
676                A::Cipher(CipherSuite::Tls12EcdheEcdsaAes128GcmSha256),
677                true,
678                Y,
679            ),
680            (
681                A::Cipher(CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256),
682                true,
683                N,
684            ),
685            (
686                A::Cipher(CipherSuite::Tls12EcdheRsaAes256GcmSha384),
687                true,
688                Y,
689            ),
690            (
691                A::Cipher(CipherSuite::Tls12EcdheRsaAes128GcmSha256),
692                true,
693                Y,
694            ),
695            (
696                A::Cipher(CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256),
697                true,
698                N,
699            ),
700            (A::Hash(HashAlgorithm::Sha256), true, Y),
701            (A::Hash(HashAlgorithm::Sha384), true, Y),
702            (A::Hash(HashAlgorithm::Sha512), true, Y),
703            (A::Protocol(TlsVersion::Tls12), true, Y),
704            (A::Protocol(TlsVersion::Tls13), true, U),
705        ];
706        assert_eq!(expected.len(), Algorithm::all().len());
707        for m in [M3S, M3D] {
708            for (a, available, kind) in &expected {
709                let c = capability(m, *a);
710                let got = match c.approval {
711                    Approval::Approved { .. } => K::Approved,
712                    Approval::NotApproved { .. } => K::NotApproved,
713                    Approval::Unverified => K::Unverified,
714                    Approval::AlgorithmTested { cavp } => panic!("{m:?} {a:?}: 3.x cites {cavp}"),
715                };
716                assert_eq!((c.available, got), (*available, *kind), "{m:?} {a:?}");
717            }
718        }
719    }
720}