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 and later).
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, on the CMVP Modules In Process list; not validated.
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 ecdh_outside_tls(l: Linkage) -> &'static str {
162    match l {
163        Linkage::Static => {
164            "AWS-LC FIPS 3.1.0 Security Policy, cert #5314 (static), p. 20: the SP 800-56Ar3 assurances hold only with an application that implements the TLS protocol, for ephemeral keys from the module's key pair generation; \"The module does not support static keys\""
165        }
166        Linkage::Dynamic => {
167            "AWS-LC FIPS 3.1.0 Security Policy, cert #5298 (dynamic), p. 20: the SP 800-56Ar3 assurances hold only with an application that implements the TLS protocol, for ephemeral keys from the module's key pair generation; \"The module does not support static keys\""
168        }
169    }
170}
171
172const fn x25519_absent(l: Linkage) -> &'static str {
173    match l {
174        Linkage::Static => {
175            "AWS-LC FIPS 3.1.0 Security Policy, cert #5314 (static): not listed in Tables 5-8; FIPS 140-3 IG C.K Resolution 5 (p. 160)"
176        }
177        Linkage::Dynamic => {
178            "AWS-LC FIPS 3.1.0 Security Policy, cert #5298 (dynamic): not listed in Tables 5-8; FIPS 140-3 IG C.K Resolution 5 (p. 160)"
179        }
180    }
181}
182
183const fn gcm_scenario_2(l: Linkage) -> &'static str {
184    match l {
185        Linkage::Static => {
186            "AWS-LC FIPS 3.1.0 Security Policy, cert #5314 (static), Table 5, p. 8 (AES-GCM: IV Generation Internal, IV Generation Mode 8.2.2, Key Length 128, 256); §2.7 GCM IV, Scenario 2, pp. 18-19 (EVP_aead_aes_128_gcm_randnonce, EVP_aead_aes_256_gcm_randnonce)"
187        }
188        Linkage::Dynamic => {
189            "AWS-LC FIPS 3.1.0 Security Policy, cert #5298 (dynamic), Table 5, p. 8 (AES-GCM: IV Generation Internal, IV Generation Mode 8.2.2, Key Length 128, 256); §2.7 GCM IV, Scenario 2, p. 19 (EVP_aead_aes_128_gcm_randnonce, EVP_aead_aes_256_gcm_randnonce)"
190        }
191    }
192}
193
194const ED25519_5314: Approval = Approval::NotApproved {
195    source: "AWS-LC FIPS 3.1.0 Security Policy, cert #5314 (static), Table 12 (Approved Services), pp. 24-25: Signature Generation and Signature Verification list RSA and ECDSA, not EdDSA",
196};
197
198const fn ed25519_3x(l: Linkage) -> Approval {
199    match l {
200        Linkage::Static => ED25519_5314,
201        Linkage::Dynamic => Approval::Approved { source: table5(l) },
202    }
203}
204
205/// X25519, in key agreement and as a TLS group, on a module with no security policy: IG C.K
206/// settles it for every module.
207const X25519_NOT_APPROVED: Approval = Approval::NotApproved {
208    source: "FIPS 140-3 IG C.K Resolution 5 (p. 160): X25519 is not an SP 800-56Ar3 key-agreement curve",
209};
210
211const fn three_x(l: Linkage, a: Algorithm) -> Capability {
212    let yes = Approval::Approved { source: table5(l) };
213    let no = Approval::NotApproved { source: absent(l) };
214    let (available, approval) = match a {
215        Algorithm::Signature(s) => match s {
216            SignatureAlgorithm::MlDsa87 => (false, no),
217            SignatureAlgorithm::Ed25519 => (true, ed25519_3x(l)),
218            SignatureAlgorithm::EcdsaP256Sha256
219            | SignatureAlgorithm::EcdsaP384Sha384
220            | SignatureAlgorithm::EcdsaP521Sha384
221            | SignatureAlgorithm::EcdsaP521Sha512
222            | SignatureAlgorithm::Rsa2048Sha384
223            | SignatureAlgorithm::Rsa3072Sha384
224            | SignatureAlgorithm::Rsa4096Sha384
225            | SignatureAlgorithm::Rsa2048Sha384Pss
226            | SignatureAlgorithm::Rsa3072Sha384Pss
227            | SignatureAlgorithm::Rsa4096Sha384Pss => (true, yes),
228        },
229        Algorithm::Scheme(s) => match s {
230            TlsSignatureScheme::MlDsa87 => (false, no),
231            TlsSignatureScheme::Ed25519 => (true, ed25519_3x(l)),
232            TlsSignatureScheme::EcdsaNistp256Sha256
233            | TlsSignatureScheme::EcdsaNistp384Sha384
234            | TlsSignatureScheme::EcdsaNistp521Sha512
235            | TlsSignatureScheme::RsaPssSha256
236            | TlsSignatureScheme::RsaPssSha384
237            | TlsSignatureScheme::RsaPssSha512
238            | TlsSignatureScheme::RsaPkcs1Sha256
239            | TlsSignatureScheme::RsaPkcs1Sha384
240            | TlsSignatureScheme::RsaPkcs1Sha512 => (true, yes),
241        },
242        Algorithm::Kx(k) => match k {
243            KxGroup::MlKem1024 | KxGroup::Secp384r1 | KxGroup::Secp256r1 => (true, yes),
244            KxGroup::X25519 => (
245                true,
246                Approval::NotApproved {
247                    source: x25519_absent(l),
248                },
249            ),
250            KxGroup::X25519MlKem768 | KxGroup::Secp256r1MlKem768 | KxGroup::Secp384r1MlKem1024 => {
251                (true, Approval::Unverified)
252            }
253        },
254        Algorithm::Cipher(c) => match c {
255            CipherSuite::Tls13Aes256GcmSha384
256            | CipherSuite::Tls13Aes128GcmSha256
257            | CipherSuite::Tls12EcdheEcdsaAes256GcmSha384
258            | CipherSuite::Tls12EcdheEcdsaAes128GcmSha256
259            | CipherSuite::Tls12EcdheRsaAes256GcmSha384
260            | CipherSuite::Tls12EcdheRsaAes128GcmSha256 => (true, yes),
261            CipherSuite::Tls13Chacha20Poly1305Sha256
262            | CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256
263            | CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256 => (true, no),
264        },
265        Algorithm::Hash(h) => match h {
266            HashAlgorithm::Sha256
267            | HashAlgorithm::Sha384
268            | HashAlgorithm::Sha512
269            | HashAlgorithm::Sha3_256
270            | HashAlgorithm::Sha3_384
271            | HashAlgorithm::Sha3_512 => (true, yes),
272        },
273        Algorithm::Protocol(v) => match v {
274            TlsVersion::Tls12 => (true, yes),
275            TlsVersion::Tls13 => (true, Approval::Unverified),
276        },
277        Algorithm::Mac(m) => match m {
278            MacAlgorithm::HmacSha256 | MacAlgorithm::HmacSha384 | MacAlgorithm::HmacSha512 => {
279                (true, yes)
280            }
281        },
282        Algorithm::Kdf(k) => match k {
283            KdfAlgorithm::HkdfSha256
284            | KdfAlgorithm::HkdfSha384
285            | KdfAlgorithm::HkdfSha512
286            | KdfAlgorithm::OneStepSha256
287            | KdfAlgorithm::OneStepSha384
288            | KdfAlgorithm::OneStepSha512 => (true, yes),
289        },
290        Algorithm::KeyAgreement(k) => match k {
291            KeyAgreementAlgorithm::EcdhP256
292            | KeyAgreementAlgorithm::EcdhP384
293            | KeyAgreementAlgorithm::EcdhP521 => (
294                true,
295                Approval::NotApproved {
296                    source: ecdh_outside_tls(l),
297                },
298            ),
299            KeyAgreementAlgorithm::X25519 => (
300                true,
301                Approval::NotApproved {
302                    source: x25519_absent(l),
303                },
304            ),
305        },
306        Algorithm::Aead(d) => match d {
307            AeadAlgorithm::Aes128Gcm | AeadAlgorithm::Aes256Gcm => (
308                true,
309                Approval::Approved {
310                    source: gcm_scenario_2(l),
311                },
312            ),
313            AeadAlgorithm::Chacha20Poly1305 => (true, no),
314        },
315    };
316    Capability {
317        available,
318        approval,
319    }
320}
321
322const fn four_x(l: Linkage, a: Algorithm) -> Capability {
323    Capability {
324        available: true,
325        approval: four_x_cell(l, a),
326    }
327}
328
329const fn tested_on(l: Linkage, dynamic: &'static str, static_build: &'static str) -> Approval {
330    Approval::AlgorithmTested {
331        cavp: match l {
332            Linkage::Dynamic => dynamic,
333            Linkage::Static => static_build,
334        },
335    }
336}
337
338/// Each `AlgorithmTested` cell cites the lowest-numbered AWS-LC FIPS 4.0.0 CAVP certificate of its
339/// linkage (A7839-A7876 dynamic build, A7886-A7923 static build) that alone shows every operation
340/// the cell needs, with the parameters the citation names (decision 0004).
341const fn four_x_cell(l: Linkage, a: Algorithm) -> Approval {
342    match a {
343        Algorithm::Signature(s) => match s {
344            SignatureAlgorithm::MlDsa87 => tested_on(
345                l,
346                "A7855 (ML-DSA KeyGen, SigGen, SigVer: ML-DSA-87)",
347                "A7902 (ML-DSA KeyGen, SigGen, SigVer: ML-DSA-87)",
348            ),
349            SignatureAlgorithm::Ed25519 => tested_on(
350                l,
351                "A7855 (EDDSA KeyGen, SigGen, SigVer: ED-25519)",
352                "A7902 (EDDSA KeyGen, SigGen, SigVer: ED-25519)",
353            ),
354            SignatureAlgorithm::Rsa2048Sha384 => tested_on(
355                l,
356                "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 2048, SHA2-384)",
357                "A7912 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 2048, SHA2-384)",
358            ),
359            SignatureAlgorithm::Rsa3072Sha384 => tested_on(
360                l,
361                "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 3072, SHA2-384)",
362                "A7912 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 3072, SHA2-384)",
363            ),
364            SignatureAlgorithm::Rsa4096Sha384 => tested_on(
365                l,
366                "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 4096, SHA2-384)",
367                "A7912 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 4096, SHA2-384)",
368            ),
369            SignatureAlgorithm::Rsa2048Sha384Pss => tested_on(
370                l,
371                "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pss, mgf1, 2048, SHA2-384, salt 48)",
372                "A7912 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pss, mgf1, 2048, SHA2-384, salt 48)",
373            ),
374            SignatureAlgorithm::Rsa3072Sha384Pss => tested_on(
375                l,
376                "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pss, mgf1, 3072, SHA2-384, salt 48)",
377                "A7912 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pss, mgf1, 3072, SHA2-384, salt 48)",
378            ),
379            SignatureAlgorithm::Rsa4096Sha384Pss => tested_on(
380                l,
381                "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pss, mgf1, 4096, SHA2-384, salt 48)",
382                "A7912 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pss, mgf1, 4096, SHA2-384, salt 48)",
383            ),
384            SignatureAlgorithm::EcdsaP256Sha256
385            | SignatureAlgorithm::EcdsaP384Sha384
386            | SignatureAlgorithm::EcdsaP521Sha384
387            | SignatureAlgorithm::EcdsaP521Sha512 => Approval::Unverified,
388        },
389        Algorithm::Scheme(t) => match t {
390            TlsSignatureScheme::EcdsaNistp256Sha256 => tested_on(
391                l,
392                "A7856 (ECDSA SigVer (FIPS186-5): P-256, SHA2-256)",
393                "A7903 (ECDSA SigVer (FIPS186-5): P-256, SHA2-256)",
394            ),
395            TlsSignatureScheme::EcdsaNistp384Sha384 => tested_on(
396                l,
397                "A7856 (ECDSA SigVer (FIPS186-5): P-384, SHA2-384)",
398                "A7903 (ECDSA SigVer (FIPS186-5): P-384, SHA2-384)",
399            ),
400            TlsSignatureScheme::EcdsaNistp521Sha512 => tested_on(
401                l,
402                "A7856 (ECDSA SigVer (FIPS186-5): P-521, SHA2-512)",
403                "A7903 (ECDSA SigVer (FIPS186-5): P-521, SHA2-512)",
404            ),
405            TlsSignatureScheme::Ed25519 => tested_on(
406                l,
407                "A7855 (EDDSA SigVer: ED-25519)",
408                "A7902 (EDDSA SigVer: ED-25519)",
409            ),
410            TlsSignatureScheme::RsaPssSha256 => tested_on(
411                l,
412                "A7856 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-256)",
413                "A7903 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-256)",
414            ),
415            TlsSignatureScheme::RsaPssSha384 => tested_on(
416                l,
417                "A7856 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-384)",
418                "A7903 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-384)",
419            ),
420            TlsSignatureScheme::RsaPssSha512 => tested_on(
421                l,
422                "A7856 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-512)",
423                "A7903 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-512)",
424            ),
425            TlsSignatureScheme::RsaPkcs1Sha256 => tested_on(
426                l,
427                "A7856 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-256)",
428                "A7903 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-256)",
429            ),
430            TlsSignatureScheme::RsaPkcs1Sha384 => tested_on(
431                l,
432                "A7856 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-384)",
433                "A7903 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-384)",
434            ),
435            TlsSignatureScheme::RsaPkcs1Sha512 => tested_on(
436                l,
437                "A7856 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-512)",
438                "A7903 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-512)",
439            ),
440            TlsSignatureScheme::MlDsa87 => tested_on(
441                l,
442                "A7855 (ML-DSA SigVer: ML-DSA-87)",
443                "A7902 (ML-DSA SigVer: ML-DSA-87)",
444            ),
445        },
446        Algorithm::Kx(k) => match k {
447            KxGroup::MlKem1024 => tested_on(
448                l,
449                "A7855 (ML-KEM KeyGen, EncapDecap: ML-KEM-1024)",
450                "A7902 (ML-KEM KeyGen, EncapDecap: ML-KEM-1024)",
451            ),
452            KxGroup::Secp384r1 => tested_on(
453                l,
454                "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, P-384; ECDSA KeyGen (FIPS186-5): P-384)",
455                "A7919 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, P-384; ECDSA KeyGen (FIPS186-5): P-384)",
456            ),
457            KxGroup::Secp256r1 => tested_on(
458                l,
459                "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, P-256; ECDSA KeyGen (FIPS186-5): P-256)",
460                "A7919 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, P-256; ECDSA KeyGen (FIPS186-5): P-256)",
461            ),
462            KxGroup::X25519 => X25519_NOT_APPROVED,
463            KxGroup::X25519MlKem768 | KxGroup::Secp256r1MlKem768 | KxGroup::Secp384r1MlKem1024 => {
464                Approval::Unverified
465            }
466        },
467        Algorithm::Cipher(c) => match c {
468            CipherSuite::Tls13Aes256GcmSha384
469            | CipherSuite::Tls12EcdheEcdsaAes256GcmSha384
470            | CipherSuite::Tls12EcdheRsaAes256GcmSha384
471            | CipherSuite::Tls13Aes128GcmSha256
472            | CipherSuite::Tls12EcdheEcdsaAes128GcmSha256
473            | CipherSuite::Tls12EcdheRsaAes128GcmSha256
474            | CipherSuite::Tls13Chacha20Poly1305Sha256
475            | CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256
476            | CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256 => Approval::Unverified,
477        },
478        Algorithm::Hash(h) => match h {
479            HashAlgorithm::Sha256 => tested_on(l, "A7855 (SHA2-256)", "A7902 (SHA2-256)"),
480            HashAlgorithm::Sha384 => tested_on(l, "A7855 (SHA2-384)", "A7902 (SHA2-384)"),
481            HashAlgorithm::Sha512 => tested_on(l, "A7855 (SHA2-512)", "A7902 (SHA2-512)"),
482            HashAlgorithm::Sha3_256 => tested_on(l, "A7855 (SHA3-256)", "A7902 (SHA3-256)"),
483            HashAlgorithm::Sha3_384 => tested_on(l, "A7855 (SHA3-384)", "A7902 (SHA3-384)"),
484            HashAlgorithm::Sha3_512 => tested_on(l, "A7855 (SHA3-512)", "A7902 (SHA3-512)"),
485        },
486        Algorithm::Protocol(v) => match v {
487            TlsVersion::Tls12 | TlsVersion::Tls13 => Approval::Unverified,
488        },
489        Algorithm::Mac(m) => match m {
490            MacAlgorithm::HmacSha256 => tested_on(
491                l,
492                "A7855 (HMAC-SHA2-256: MAC 256, key 112-524288)",
493                "A7902 (HMAC-SHA2-256: MAC 256, key 112-524288)",
494            ),
495            MacAlgorithm::HmacSha384 => tested_on(
496                l,
497                "A7855 (HMAC-SHA2-384: MAC 384, key 112-524288)",
498                "A7902 (HMAC-SHA2-384: MAC 384, key 112-524288)",
499            ),
500            MacAlgorithm::HmacSha512 => tested_on(
501                l,
502                "A7855 (HMAC-SHA2-512: MAC 512, key 112-524288)",
503                "A7902 (HMAC-SHA2-512: MAC 512, key 112-524288)",
504            ),
505        },
506        Algorithm::Kdf(k) => match k {
507            KdfAlgorithm::HkdfSha256 => tested_on(
508                l,
509                "A7855 (KDA HKDF Sp800-56Cr1: HMAC SHA2-256, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo||l)",
510                "A7902 (KDA HKDF Sp800-56Cr1: HMAC SHA2-256, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo||l)",
511            ),
512            KdfAlgorithm::HkdfSha384 => tested_on(
513                l,
514                "A7855 (KDA HKDF Sp800-56Cr1: HMAC SHA2-384, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo||l)",
515                "A7902 (KDA HKDF Sp800-56Cr1: HMAC SHA2-384, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo||l)",
516            ),
517            KdfAlgorithm::HkdfSha512 => tested_on(
518                l,
519                "A7855 (KDA HKDF Sp800-56Cr1: HMAC SHA2-512, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo||l)",
520                "A7902 (KDA HKDF Sp800-56Cr1: HMAC SHA2-512, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo||l)",
521            ),
522            KdfAlgorithm::OneStepSha256 => tested_on(
523                l,
524                "A7856 (KDA OneStep SP800-56Cr2: SHA2-256, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo)",
525                "A7903 (KDA OneStep SP800-56Cr2: SHA2-256, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo)",
526            ),
527            KdfAlgorithm::OneStepSha384 => tested_on(
528                l,
529                "A7856 (KDA OneStep SP800-56Cr2: SHA2-384, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo)",
530                "A7903 (KDA OneStep SP800-56Cr2: SHA2-384, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo)",
531            ),
532            KdfAlgorithm::OneStepSha512 => tested_on(
533                l,
534                "A7856 (KDA OneStep SP800-56Cr2: SHA2-512, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo)",
535                "A7903 (KDA OneStep SP800-56Cr2: SHA2-512, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo)",
536            ),
537        },
538        Algorithm::KeyAgreement(k) => match k {
539            KeyAgreementAlgorithm::EcdhP256 => tested_on(
540                l,
541                "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, initiator and responder, P-256; ECDSA KeyGen (FIPS186-5): P-256)",
542                "A7919 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, initiator and responder, P-256; ECDSA KeyGen (FIPS186-5): P-256)",
543            ),
544            KeyAgreementAlgorithm::EcdhP384 => tested_on(
545                l,
546                "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, initiator and responder, P-384; ECDSA KeyGen (FIPS186-5): P-384)",
547                "A7919 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, initiator and responder, P-384; ECDSA KeyGen (FIPS186-5): P-384)",
548            ),
549            KeyAgreementAlgorithm::EcdhP521 => tested_on(
550                l,
551                "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, initiator and responder, P-521; ECDSA KeyGen (FIPS186-5): P-521)",
552                "A7919 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, initiator and responder, P-521; ECDSA KeyGen (FIPS186-5): P-521)",
553            ),
554            KeyAgreementAlgorithm::X25519 => X25519_NOT_APPROVED,
555        },
556        Algorithm::Aead(d) => match d {
557            AeadAlgorithm::Aes128Gcm => tested_on(
558                l,
559                "A7840 (AES-GCM: Encrypt, IV Generation Internal, 8.2.2; Decrypt, IV Generation External; key 128, IV 96, tag 128)",
560                "A7887 (AES-GCM: Encrypt, IV Generation Internal, 8.2.2; Decrypt, IV Generation External; key 128, IV 96, tag 128)",
561            ),
562            AeadAlgorithm::Aes256Gcm => tested_on(
563                l,
564                "A7840 (AES-GCM: Encrypt, IV Generation Internal, 8.2.2; Decrypt, IV Generation External; key 256, IV 96, tag 128)",
565                "A7887 (AES-GCM: Encrypt, IV Generation Internal, 8.2.2; Decrypt, IV Generation External; key 256, IV 96, tag 128)",
566            ),
567            AeadAlgorithm::Chacha20Poly1305 => Approval::Unverified,
568        },
569    }
570}
571
572/// What the backend for `m` can do for `a`, and what that module's published evidence says about
573/// it: its security policy, or one CAVP certificate (decision 0004).
574pub const fn capability(m: Module, a: Algorithm) -> Capability {
575    match m.line {
576        ModuleLine::AwsLcFips3 => three_x(m.linkage, a),
577        ModuleLine::AwsLcFips4 => four_x(m.linkage, a),
578    }
579}
580
581/// Refuses an algorithm the module's backend does not implement.
582pub fn ensure_available(m: Module, a: Algorithm) -> Result<(), CryptoError> {
583    if capability(m, a).available {
584        Ok(())
585    } else {
586        Err(CryptoError::Unavailable(a))
587    }
588}
589
590#[cfg(test)]
591mod tests {
592    use super::*;
593
594    const M3S: Module = Module {
595        line: ModuleLine::AwsLcFips3,
596        linkage: Linkage::Static,
597    };
598    const M3D: Module = Module {
599        line: ModuleLine::AwsLcFips3,
600        linkage: Linkage::Dynamic,
601    };
602    const M4S: Module = Module {
603        line: ModuleLine::AwsLcFips4,
604        linkage: Linkage::Static,
605    };
606    const M4D: Module = Module {
607        line: ModuleLine::AwsLcFips4,
608        linkage: Linkage::Dynamic,
609    };
610
611    fn modules() -> [Module; 4] {
612        [M3S, M3D, M4S, M4D]
613    }
614
615    const X25519: Algorithm = Algorithm::KeyAgreement(KeyAgreementAlgorithm::X25519);
616    const KX_X25519: Algorithm = Algorithm::Kx(KxGroup::X25519);
617
618    #[test]
619    fn certificates_name_their_module() {
620        let c = certificate(M3S);
621        assert_eq!((c.number, c.status), (Some(5314), CertStatus::Validated));
622        assert!(c.policy.unwrap().ends_with("140sp5314.pdf"));
623        let c = certificate(M3D);
624        assert_eq!((c.number, c.status), (Some(5298), CertStatus::Validated));
625        assert!(c.policy.unwrap().ends_with("140sp5298.pdf"));
626        for m in [M4S, M4D] {
627            let c = certificate(m);
628            assert_eq!(
629                (c.number, c.status, c.policy),
630                (None, CertStatus::InProcess, None)
631            );
632        }
633    }
634
635    #[test]
636    fn every_sourced_cell_cites_its_own_certificate() {
637        for m in modules() {
638            for a in Algorithm::all() {
639                match capability(m, a).approval {
640                    Approval::Approved { source } | Approval::NotApproved { source } => {
641                        assert!(!source.is_empty(), "{m:?} {a:?}");
642                        let want = match (m.line, m.linkage) {
643                            (ModuleLine::AwsLcFips3, Linkage::Static) => "#5314",
644                            (ModuleLine::AwsLcFips3, Linkage::Dynamic) => "#5298",
645                            (ModuleLine::AwsLcFips4, Linkage::Static | Linkage::Dynamic) => {
646                                assert!(a == X25519 || a == KX_X25519, "{m:?} {a:?}");
647                                "FIPS 140-3 IG C.K Resolution 5"
648                            }
649                        };
650                        assert!(source.contains(want), "{m:?} {a:?} cites {source}");
651                    }
652                    Approval::AlgorithmTested { cavp } => {
653                        let range = match (m.line, m.linkage) {
654                            (ModuleLine::AwsLcFips4, Linkage::Dynamic) => 7839..=7876,
655                            (ModuleLine::AwsLcFips4, Linkage::Static) => 7886..=7923,
656                            (ModuleLine::AwsLcFips3, _) => panic!("{m:?} {a:?} cites {cavp}"),
657                        };
658                        let number: u32 = cavp[1..5].parse().unwrap();
659                        assert!(
660                            cavp.starts_with('A') && range.contains(&number),
661                            "{m:?} {a:?} cites {cavp}"
662                        );
663                        assert_eq!(cavp.matches("A7").count(), 1, "{a:?} cites {cavp}");
664                    }
665                    Approval::Unverified => {}
666                }
667            }
668        }
669    }
670
671    #[test]
672    fn x25519_is_not_approved_on_any_module() {
673        for m in modules() {
674            for a in [X25519, KX_X25519] {
675                let c = capability(m, a);
676                assert!(c.available, "{m:?} {a:?}");
677                match c.approval {
678                    Approval::NotApproved { source } => {
679                        assert!(
680                            source.contains("IG C.K Resolution 5 (p. 160)"),
681                            "{m:?} {a:?}"
682                        )
683                    }
684                    other => panic!("{m:?} {a:?}: {other:?}"),
685                }
686            }
687        }
688    }
689
690    #[test]
691    fn three_x_approves_aes_gcm_outside_tls_on_scenario_2() {
692        for (m, cert, scenario) in [
693            (M3S, "#5314 (static)", "Scenario 2, pp. 18-19"),
694            (M3D, "#5298 (dynamic)", "Scenario 2, p. 19"),
695        ] {
696            for d in [AeadAlgorithm::Aes128Gcm, AeadAlgorithm::Aes256Gcm] {
697                let c = capability(m, Algorithm::Aead(d));
698                assert_eq!(
699                    c,
700                    Capability {
701                        available: true,
702                        approval: Approval::Approved {
703                            source: gcm_scenario_2(m.linkage)
704                        }
705                    },
706                    "{m:?} {d:?}"
707                );
708                for want in [
709                    cert,
710                    "Table 5, p. 8",
711                    "IV Generation Internal, IV Generation Mode 8.2.2",
712                    scenario,
713                    "EVP_aead_aes_128_gcm_randnonce, EVP_aead_aes_256_gcm_randnonce",
714                ] {
715                    assert!(gcm_scenario_2(m.linkage).contains(want), "{m:?} {want}");
716                }
717            }
718            assert_eq!(
719                capability(m, Algorithm::Aead(AeadAlgorithm::Chacha20Poly1305)),
720                Capability {
721                    available: true,
722                    approval: Approval::NotApproved {
723                        source: absent(m.linkage)
724                    }
725                },
726                "{m:?}"
727            );
728        }
729    }
730
731    #[test]
732    fn every_hash_cell_is_pinned_to_its_citation() {
733        for (m, cert) in [(M3S, "#5314 (static)"), (M3D, "#5298 (dynamic)")] {
734            for &h in HashAlgorithm::ALL {
735                assert_eq!(
736                    capability(m, Algorithm::Hash(h)),
737                    Capability {
738                        available: true,
739                        approval: Approval::Approved {
740                            source: table5(m.linkage)
741                        }
742                    },
743                    "{m:?} {h:?}"
744                );
745            }
746            assert!(table5(m.linkage).contains(cert), "{m:?}");
747            assert!(table5(m.linkage).contains("Table 5, pp. 8-11"), "{m:?}");
748        }
749        for (m, cert) in [(M4D, "A7855"), (M4S, "A7902")] {
750            for (h, name) in [
751                (HashAlgorithm::Sha256, "SHA2-256"),
752                (HashAlgorithm::Sha384, "SHA2-384"),
753                (HashAlgorithm::Sha512, "SHA2-512"),
754                (HashAlgorithm::Sha3_256, "SHA3-256"),
755                (HashAlgorithm::Sha3_384, "SHA3-384"),
756                (HashAlgorithm::Sha3_512, "SHA3-512"),
757            ] {
758                let want = format!("{cert} ({name})");
759                match capability(m, Algorithm::Hash(h)) {
760                    Capability {
761                        available: true,
762                        approval: Approval::AlgorithmTested { cavp },
763                    } => assert_eq!(cavp, want, "{m:?} {h:?}"),
764                    other => panic!("{m:?} {h:?}: {other:?}"),
765                }
766            }
767        }
768    }
769
770    #[test]
771    fn three_x_does_not_approve_ecdh_outside_tls() {
772        for (m, cert) in [(M3S, "#5314 (static)"), (M3D, "#5298 (dynamic)")] {
773            for k in [
774                KeyAgreementAlgorithm::EcdhP256,
775                KeyAgreementAlgorithm::EcdhP384,
776                KeyAgreementAlgorithm::EcdhP521,
777            ] {
778                let c = capability(m, Algorithm::KeyAgreement(k));
779                assert_eq!(
780                    c,
781                    Capability {
782                        available: true,
783                        approval: Approval::NotApproved {
784                            source: ecdh_outside_tls(m.linkage)
785                        }
786                    },
787                    "{m:?} {k:?}"
788                );
789                let Approval::NotApproved { source } = c.approval else {
790                    panic!("{m:?} {k:?}")
791                };
792                assert!(source.contains(cert), "{source}");
793                assert!(source.contains("p. 20"), "{source}");
794                assert!(source.contains("does not support static keys"), "{source}");
795            }
796        }
797    }
798
799    #[test]
800    fn three_x_static_does_not_approve_ed25519_and_dynamic_does() {
801        for a in [
802            Algorithm::Signature(SignatureAlgorithm::Ed25519),
803            Algorithm::Scheme(TlsSignatureScheme::Ed25519),
804        ] {
805            assert_eq!(
806                capability(M3S, a),
807                Capability {
808                    available: true,
809                    approval: ED25519_5314
810                },
811                "{a:?}"
812            );
813            assert_eq!(
814                capability(M3D, a),
815                Capability {
816                    available: true,
817                    approval: Approval::Approved {
818                        source: table5(Linkage::Dynamic)
819                    }
820                },
821                "{a:?}"
822            );
823        }
824        let Approval::NotApproved { source } = ED25519_5314 else {
825            panic!("{ED25519_5314:?}")
826        };
827        for want in [
828            "#5314 (static)",
829            "Table 12",
830            "pp. 24-25",
831            "RSA and ECDSA",
832            "not EdDSA",
833        ] {
834            assert!(source.contains(want), "{want}");
835        }
836    }
837
838    #[test]
839    fn every_rsa_signature_cell_is_pinned_to_its_citation() {
840        use SignatureAlgorithm as S;
841        for m in [M3S, M3D] {
842            for a in [
843                S::Rsa2048Sha384,
844                S::Rsa3072Sha384,
845                S::Rsa4096Sha384,
846                S::Rsa2048Sha384Pss,
847                S::Rsa3072Sha384Pss,
848                S::Rsa4096Sha384Pss,
849            ] {
850                assert_eq!(
851                    capability(m, Algorithm::Signature(a)),
852                    Capability {
853                        available: true,
854                        approval: Approval::Approved {
855                            source: table5(m.linkage)
856                        }
857                    },
858                    "{m:?} {a}"
859                );
860            }
861        }
862        for (m, cert) in [(M4D, "A7873"), (M4S, "A7912")] {
863            for (a, padding, bits) in [
864                (S::Rsa2048Sha384, "pkcs1v1.5, pss", 2048),
865                (S::Rsa3072Sha384, "pkcs1v1.5, pss", 3072),
866                (S::Rsa4096Sha384, "pkcs1v1.5, pss", 4096),
867                (S::Rsa2048Sha384Pss, "pss, mgf1", 2048),
868                (S::Rsa3072Sha384Pss, "pss, mgf1", 3072),
869                (S::Rsa4096Sha384Pss, "pss, mgf1", 4096),
870            ] {
871                let salt = if padding.starts_with("pss") {
872                    ", salt 48"
873                } else {
874                    ""
875                };
876                let want = format!(
877                    "{cert} (RSA KeyGen, SigGen, SigVer (FIPS186-5): {padding}, {bits}, SHA2-384{salt})"
878                );
879                match capability(m, Algorithm::Signature(a)) {
880                    Capability {
881                        available: true,
882                        approval: Approval::AlgorithmTested { cavp },
883                    } => assert_eq!(cavp, want, "{m:?} {a}"),
884                    other => panic!("{m:?} {a}: {other:?}"),
885                }
886            }
887        }
888    }
889
890    fn four_x_citations() -> [(Algorithm, Option<(&'static str, &'static str)>); 63] {
891        use Algorithm as A;
892        use CipherSuite as C;
893        use HashAlgorithm as H;
894        use KxGroup as K;
895        use SignatureAlgorithm as S;
896        use TlsSignatureScheme as T;
897        use TlsVersion as V;
898        [
899            (
900                A::Signature(S::MlDsa87),
901                Some((
902                    "A7855 (ML-DSA KeyGen, SigGen, SigVer: ML-DSA-87)",
903                    "A7902 (ML-DSA KeyGen, SigGen, SigVer: ML-DSA-87)",
904                )),
905            ),
906            (A::Signature(S::EcdsaP256Sha256), None),
907            (A::Signature(S::EcdsaP384Sha384), None),
908            (A::Signature(S::EcdsaP521Sha384), None),
909            (A::Signature(S::EcdsaP521Sha512), None),
910            (
911                A::Signature(S::Ed25519),
912                Some((
913                    "A7855 (EDDSA KeyGen, SigGen, SigVer: ED-25519)",
914                    "A7902 (EDDSA KeyGen, SigGen, SigVer: ED-25519)",
915                )),
916            ),
917            (
918                A::Signature(S::Rsa2048Sha384),
919                Some((
920                    "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 2048, SHA2-384)",
921                    "A7912 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 2048, SHA2-384)",
922                )),
923            ),
924            (
925                A::Signature(S::Rsa3072Sha384),
926                Some((
927                    "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 3072, SHA2-384)",
928                    "A7912 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 3072, SHA2-384)",
929                )),
930            ),
931            (
932                A::Signature(S::Rsa4096Sha384),
933                Some((
934                    "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 4096, SHA2-384)",
935                    "A7912 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pkcs1v1.5, pss, 4096, SHA2-384)",
936                )),
937            ),
938            (
939                A::Signature(S::Rsa2048Sha384Pss),
940                Some((
941                    "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pss, mgf1, 2048, SHA2-384, salt 48)",
942                    "A7912 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pss, mgf1, 2048, SHA2-384, salt 48)",
943                )),
944            ),
945            (
946                A::Signature(S::Rsa3072Sha384Pss),
947                Some((
948                    "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pss, mgf1, 3072, SHA2-384, salt 48)",
949                    "A7912 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pss, mgf1, 3072, SHA2-384, salt 48)",
950                )),
951            ),
952            (
953                A::Signature(S::Rsa4096Sha384Pss),
954                Some((
955                    "A7873 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pss, mgf1, 4096, SHA2-384, salt 48)",
956                    "A7912 (RSA KeyGen, SigGen, SigVer (FIPS186-5): pss, mgf1, 4096, SHA2-384, salt 48)",
957                )),
958            ),
959            (
960                A::Scheme(T::EcdsaNistp256Sha256),
961                Some((
962                    "A7856 (ECDSA SigVer (FIPS186-5): P-256, SHA2-256)",
963                    "A7903 (ECDSA SigVer (FIPS186-5): P-256, SHA2-256)",
964                )),
965            ),
966            (
967                A::Scheme(T::EcdsaNistp384Sha384),
968                Some((
969                    "A7856 (ECDSA SigVer (FIPS186-5): P-384, SHA2-384)",
970                    "A7903 (ECDSA SigVer (FIPS186-5): P-384, SHA2-384)",
971                )),
972            ),
973            (
974                A::Scheme(T::EcdsaNistp521Sha512),
975                Some((
976                    "A7856 (ECDSA SigVer (FIPS186-5): P-521, SHA2-512)",
977                    "A7903 (ECDSA SigVer (FIPS186-5): P-521, SHA2-512)",
978                )),
979            ),
980            (
981                A::Scheme(T::Ed25519),
982                Some((
983                    "A7855 (EDDSA SigVer: ED-25519)",
984                    "A7902 (EDDSA SigVer: ED-25519)",
985                )),
986            ),
987            (
988                A::Scheme(T::RsaPssSha256),
989                Some((
990                    "A7856 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-256)",
991                    "A7903 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-256)",
992                )),
993            ),
994            (
995                A::Scheme(T::RsaPssSha384),
996                Some((
997                    "A7856 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-384)",
998                    "A7903 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-384)",
999                )),
1000            ),
1001            (
1002                A::Scheme(T::RsaPssSha512),
1003                Some((
1004                    "A7856 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-512)",
1005                    "A7903 (RSA SigVer (FIPS186-5): pss, 2048, 3072, 4096, SHA2-512)",
1006                )),
1007            ),
1008            (
1009                A::Scheme(T::RsaPkcs1Sha256),
1010                Some((
1011                    "A7856 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-256)",
1012                    "A7903 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-256)",
1013                )),
1014            ),
1015            (
1016                A::Scheme(T::RsaPkcs1Sha384),
1017                Some((
1018                    "A7856 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-384)",
1019                    "A7903 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-384)",
1020                )),
1021            ),
1022            (
1023                A::Scheme(T::RsaPkcs1Sha512),
1024                Some((
1025                    "A7856 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-512)",
1026                    "A7903 (RSA SigVer (FIPS186-5): pkcs1v1.5, 2048, 3072, 4096, SHA2-512)",
1027                )),
1028            ),
1029            (
1030                A::Scheme(T::MlDsa87),
1031                Some((
1032                    "A7855 (ML-DSA SigVer: ML-DSA-87)",
1033                    "A7902 (ML-DSA SigVer: ML-DSA-87)",
1034                )),
1035            ),
1036            (
1037                A::Kx(K::MlKem1024),
1038                Some((
1039                    "A7855 (ML-KEM KeyGen, EncapDecap: ML-KEM-1024)",
1040                    "A7902 (ML-KEM KeyGen, EncapDecap: ML-KEM-1024)",
1041                )),
1042            ),
1043            (A::Kx(K::Secp384r1MlKem1024), None),
1044            (A::Kx(K::X25519MlKem768), None),
1045            (A::Kx(K::Secp256r1MlKem768), None),
1046            (
1047                A::Kx(K::Secp384r1),
1048                Some((
1049                    "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, P-384; ECDSA KeyGen (FIPS186-5): P-384)",
1050                    "A7919 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, P-384; ECDSA KeyGen (FIPS186-5): P-384)",
1051                )),
1052            ),
1053            (
1054                A::Kx(K::Secp256r1),
1055                Some((
1056                    "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, P-256; ECDSA KeyGen (FIPS186-5): P-256)",
1057                    "A7919 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, P-256; ECDSA KeyGen (FIPS186-5): P-256)",
1058                )),
1059            ),
1060            (A::Kx(K::X25519), None),
1061            (A::Cipher(C::Tls13Aes256GcmSha384), None),
1062            (A::Cipher(C::Tls13Aes128GcmSha256), None),
1063            (A::Cipher(C::Tls13Chacha20Poly1305Sha256), None),
1064            (A::Cipher(C::Tls12EcdheEcdsaAes256GcmSha384), None),
1065            (A::Cipher(C::Tls12EcdheEcdsaAes128GcmSha256), None),
1066            (A::Cipher(C::Tls12EcdheEcdsaChacha20Poly1305Sha256), None),
1067            (A::Cipher(C::Tls12EcdheRsaAes256GcmSha384), None),
1068            (A::Cipher(C::Tls12EcdheRsaAes128GcmSha256), None),
1069            (A::Cipher(C::Tls12EcdheRsaChacha20Poly1305Sha256), None),
1070            (
1071                A::Hash(H::Sha256),
1072                Some(("A7855 (SHA2-256)", "A7902 (SHA2-256)")),
1073            ),
1074            (
1075                A::Hash(H::Sha384),
1076                Some(("A7855 (SHA2-384)", "A7902 (SHA2-384)")),
1077            ),
1078            (
1079                A::Hash(H::Sha512),
1080                Some(("A7855 (SHA2-512)", "A7902 (SHA2-512)")),
1081            ),
1082            (
1083                A::Hash(H::Sha3_256),
1084                Some(("A7855 (SHA3-256)", "A7902 (SHA3-256)")),
1085            ),
1086            (
1087                A::Hash(H::Sha3_384),
1088                Some(("A7855 (SHA3-384)", "A7902 (SHA3-384)")),
1089            ),
1090            (
1091                A::Hash(H::Sha3_512),
1092                Some(("A7855 (SHA3-512)", "A7902 (SHA3-512)")),
1093            ),
1094            (A::Protocol(V::Tls12), None),
1095            (A::Protocol(V::Tls13), None),
1096            (
1097                A::Mac(MacAlgorithm::HmacSha256),
1098                Some((
1099                    "A7855 (HMAC-SHA2-256: MAC 256, key 112-524288)",
1100                    "A7902 (HMAC-SHA2-256: MAC 256, key 112-524288)",
1101                )),
1102            ),
1103            (
1104                A::Mac(MacAlgorithm::HmacSha384),
1105                Some((
1106                    "A7855 (HMAC-SHA2-384: MAC 384, key 112-524288)",
1107                    "A7902 (HMAC-SHA2-384: MAC 384, key 112-524288)",
1108                )),
1109            ),
1110            (
1111                A::Mac(MacAlgorithm::HmacSha512),
1112                Some((
1113                    "A7855 (HMAC-SHA2-512: MAC 512, key 112-524288)",
1114                    "A7902 (HMAC-SHA2-512: MAC 512, key 112-524288)",
1115                )),
1116            ),
1117            (
1118                A::Kdf(KdfAlgorithm::HkdfSha256),
1119                Some((
1120                    "A7855 (KDA HKDF Sp800-56Cr1: HMAC SHA2-256, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo||l)",
1121                    "A7902 (KDA HKDF Sp800-56Cr1: HMAC SHA2-256, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo||l)",
1122                )),
1123            ),
1124            (
1125                A::Kdf(KdfAlgorithm::HkdfSha384),
1126                Some((
1127                    "A7855 (KDA HKDF Sp800-56Cr1: HMAC SHA2-384, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo||l)",
1128                    "A7902 (KDA HKDF Sp800-56Cr1: HMAC SHA2-384, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo||l)",
1129                )),
1130            ),
1131            (
1132                A::Kdf(KdfAlgorithm::HkdfSha512),
1133                Some((
1134                    "A7855 (KDA HKDF Sp800-56Cr1: HMAC SHA2-512, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo||l)",
1135                    "A7902 (KDA HKDF Sp800-56Cr1: HMAC SHA2-512, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo||l)",
1136                )),
1137            ),
1138            (
1139                A::Kdf(KdfAlgorithm::OneStepSha256),
1140                Some((
1141                    "A7856 (KDA OneStep SP800-56Cr2: SHA2-256, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo)",
1142                    "A7903 (KDA OneStep SP800-56Cr2: SHA2-256, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo)",
1143                )),
1144            ),
1145            (
1146                A::Kdf(KdfAlgorithm::OneStepSha384),
1147                Some((
1148                    "A7856 (KDA OneStep SP800-56Cr2: SHA2-384, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo)",
1149                    "A7903 (KDA OneStep SP800-56Cr2: SHA2-384, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo)",
1150                )),
1151            ),
1152            (
1153                A::Kdf(KdfAlgorithm::OneStepSha512),
1154                Some((
1155                    "A7856 (KDA OneStep SP800-56Cr2: SHA2-512, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo)",
1156                    "A7903 (KDA OneStep SP800-56Cr2: SHA2-512, shared secret 224-2048, derived key 2048, fixed info uPartyInfo||vPartyInfo)",
1157                )),
1158            ),
1159            (
1160                A::KeyAgreement(KeyAgreementAlgorithm::EcdhP256),
1161                Some((
1162                    "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, initiator and responder, P-256; ECDSA KeyGen (FIPS186-5): P-256)",
1163                    "A7919 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, initiator and responder, P-256; ECDSA KeyGen (FIPS186-5): P-256)",
1164                )),
1165            ),
1166            (
1167                A::KeyAgreement(KeyAgreementAlgorithm::EcdhP384),
1168                Some((
1169                    "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, initiator and responder, P-384; ECDSA KeyGen (FIPS186-5): P-384)",
1170                    "A7919 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, initiator and responder, P-384; ECDSA KeyGen (FIPS186-5): P-384)",
1171                )),
1172            ),
1173            (
1174                A::KeyAgreement(KeyAgreementAlgorithm::EcdhP521),
1175                Some((
1176                    "A7864 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, initiator and responder, P-521; ECDSA KeyGen (FIPS186-5): P-521)",
1177                    "A7919 (KAS-ECC-SSC Sp800-56Ar3: ephemeralUnified, initiator and responder, P-521; ECDSA KeyGen (FIPS186-5): P-521)",
1178                )),
1179            ),
1180            (X25519, None),
1181            (
1182                A::Aead(AeadAlgorithm::Aes128Gcm),
1183                Some((
1184                    "A7840 (AES-GCM: Encrypt, IV Generation Internal, 8.2.2; Decrypt, IV Generation External; key 128, IV 96, tag 128)",
1185                    "A7887 (AES-GCM: Encrypt, IV Generation Internal, 8.2.2; Decrypt, IV Generation External; key 128, IV 96, tag 128)",
1186                )),
1187            ),
1188            (
1189                A::Aead(AeadAlgorithm::Aes256Gcm),
1190                Some((
1191                    "A7840 (AES-GCM: Encrypt, IV Generation Internal, 8.2.2; Decrypt, IV Generation External; key 256, IV 96, tag 128)",
1192                    "A7887 (AES-GCM: Encrypt, IV Generation Internal, 8.2.2; Decrypt, IV Generation External; key 256, IV 96, tag 128)",
1193                )),
1194            ),
1195            (A::Aead(AeadAlgorithm::Chacha20Poly1305), None),
1196        ]
1197    }
1198
1199    fn four_x_cells_are_pinned(m: Module) {
1200        let expected = four_x_citations();
1201        assert_eq!(expected.len(), Algorithm::all().len());
1202        assert!(
1203            Algorithm::all()
1204                .iter()
1205                .all(|a| expected.iter().any(|(b, _)| b == a))
1206        );
1207        assert_eq!(expected.iter().filter(|(_, c)| c.is_some()).count(), 42);
1208        for (a, cavp) in expected {
1209            let approval = match (cavp, m.linkage) {
1210                (Some((cavp, _)), Linkage::Dynamic) | (Some((_, cavp)), Linkage::Static) => {
1211                    Approval::AlgorithmTested { cavp }
1212                }
1213                (None, _) if a == X25519 || a == KX_X25519 => X25519_NOT_APPROVED,
1214                (None, _) => Approval::Unverified,
1215            };
1216            assert_eq!(
1217                capability(m, a),
1218                Capability {
1219                    available: true,
1220                    approval
1221                },
1222                "{m:?} {a:?}"
1223            );
1224            assert!(!approval.is_approved(), "{a:?}");
1225        }
1226    }
1227
1228    #[test]
1229    fn four_x_dynamic_cells_are_pinned_to_their_cavp_certificates() {
1230        four_x_cells_are_pinned(M4D);
1231    }
1232
1233    #[test]
1234    fn four_x_static_cells_are_pinned_to_their_cavp_certificates() {
1235        four_x_cells_are_pinned(M4S);
1236    }
1237
1238    #[test]
1239    fn each_static_certificate_is_the_dynamic_ones_counterpart() {
1240        let pairs = [
1241            ("A7855", "A7902"),
1242            ("A7856", "A7903"),
1243            ("A7873", "A7912"),
1244            ("A7864", "A7919"),
1245            ("A7840", "A7887"),
1246        ];
1247        for (a, cavp) in four_x_citations() {
1248            if let Some((dynamic, static_build)) = cavp {
1249                assert!(
1250                    pairs.contains(&(&dynamic[..5], &static_build[..5])),
1251                    "{a:?}: {dynamic} / {static_build}"
1252                );
1253                assert_eq!(dynamic[5..], static_build[5..], "{a:?}");
1254            }
1255        }
1256    }
1257
1258    #[test]
1259    fn three_x_lacks_only_ml_dsa() {
1260        for m in [M3S, M3D] {
1261            for a in Algorithm::all() {
1262                let ml_dsa = matches!(
1263                    a,
1264                    Algorithm::Signature(SignatureAlgorithm::MlDsa87)
1265                        | Algorithm::Scheme(TlsSignatureScheme::MlDsa87)
1266                );
1267                assert_eq!(capability(m, a).available, !ml_dsa, "{m:?} {a:?}");
1268            }
1269        }
1270    }
1271
1272    #[test]
1273    fn three_x_approval_matches_table_5() {
1274        use Algorithm as A;
1275        let approved = |a| capability(M3S, a).approval.is_approved();
1276        assert!(approved(A::Signature(SignatureAlgorithm::Rsa2048Sha384)));
1277        assert!(approved(A::Signature(SignatureAlgorithm::Rsa4096Sha384Pss)));
1278        assert!(approved(A::Signature(SignatureAlgorithm::EcdsaP521Sha384)));
1279        assert!(approved(A::Kx(KxGroup::MlKem1024)));
1280        assert!(approved(A::Kx(KxGroup::Secp384r1)));
1281        assert!(approved(A::Cipher(CipherSuite::Tls13Aes128GcmSha256)));
1282        assert!(approved(A::Hash(HashAlgorithm::Sha512)));
1283        assert!(approved(A::Hash(HashAlgorithm::Sha3_256)));
1284        assert!(approved(A::Mac(MacAlgorithm::HmacSha384)));
1285        assert!(approved(A::Kdf(KdfAlgorithm::HkdfSha512)));
1286        assert!(approved(A::Kdf(KdfAlgorithm::OneStepSha256)));
1287        assert!(approved(A::Protocol(TlsVersion::Tls12)));
1288        assert!(approved(A::Aead(AeadAlgorithm::Aes128Gcm)));
1289        assert!(approved(A::Aead(AeadAlgorithm::Aes256Gcm)));
1290        for a in [
1291            A::Signature(SignatureAlgorithm::MlDsa87),
1292            A::Scheme(TlsSignatureScheme::MlDsa87),
1293            A::Kx(KxGroup::X25519),
1294            A::Cipher(CipherSuite::Tls13Chacha20Poly1305Sha256),
1295            A::Cipher(CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256),
1296            A::Aead(AeadAlgorithm::Chacha20Poly1305),
1297        ] {
1298            assert!(
1299                matches!(capability(M3S, a).approval, Approval::NotApproved { .. }),
1300                "{a:?}"
1301            );
1302        }
1303        for a in [
1304            A::Kx(KxGroup::X25519MlKem768),
1305            A::Kx(KxGroup::Secp256r1MlKem768),
1306            A::Protocol(TlsVersion::Tls13),
1307        ] {
1308            assert_eq!(capability(M3S, a).approval, Approval::Unverified, "{a:?}");
1309        }
1310    }
1311
1312    #[test]
1313    fn unverified_is_never_approved() {
1314        assert!(!Approval::Unverified.is_approved());
1315        assert!(!Approval::AlgorithmTested { cavp: "A7855" }.is_approved());
1316        assert!(!Approval::NotApproved { source: "x" }.is_approved());
1317        assert!(Approval::Approved { source: "x" }.is_approved());
1318    }
1319
1320    #[test]
1321    fn ensure_available_refuses_ml_dsa_on_3x() {
1322        let a = Algorithm::Signature(SignatureAlgorithm::MlDsa87);
1323        assert_eq!(ensure_available(M3S, a), Err(CryptoError::Unavailable(a)));
1324        assert_eq!(ensure_available(M4S, a), Ok(()));
1325    }
1326
1327    #[test]
1328    fn three_x_table_is_pinned_cell_by_cell() {
1329        use Algorithm as A;
1330        use SignatureAlgorithm as S;
1331        use TlsSignatureScheme as T;
1332        #[derive(Debug, Clone, Copy, PartialEq)]
1333        enum K {
1334            Approved,
1335            NotApproved,
1336            Unverified,
1337            StaticNotApproved,
1338        }
1339        use K::{Approved as Y, NotApproved as N, StaticNotApproved as E, Unverified as U};
1340        let expected: [(Algorithm, bool, K); 63] = [
1341            (A::Signature(S::MlDsa87), false, N),
1342            (A::Signature(S::EcdsaP256Sha256), true, Y),
1343            (A::Signature(S::EcdsaP384Sha384), true, Y),
1344            (A::Signature(S::EcdsaP521Sha384), true, Y),
1345            (A::Signature(S::EcdsaP521Sha512), true, Y),
1346            (A::Signature(S::Ed25519), true, E),
1347            (A::Signature(S::Rsa2048Sha384), true, Y),
1348            (A::Signature(S::Rsa3072Sha384), true, Y),
1349            (A::Signature(S::Rsa4096Sha384), true, Y),
1350            (A::Signature(S::Rsa2048Sha384Pss), true, Y),
1351            (A::Signature(S::Rsa3072Sha384Pss), true, Y),
1352            (A::Signature(S::Rsa4096Sha384Pss), true, Y),
1353            (A::Scheme(T::EcdsaNistp256Sha256), true, Y),
1354            (A::Scheme(T::EcdsaNistp384Sha384), true, Y),
1355            (A::Scheme(T::EcdsaNistp521Sha512), true, Y),
1356            (A::Scheme(T::Ed25519), true, E),
1357            (A::Scheme(T::RsaPssSha256), true, Y),
1358            (A::Scheme(T::RsaPssSha384), true, Y),
1359            (A::Scheme(T::RsaPssSha512), true, Y),
1360            (A::Scheme(T::RsaPkcs1Sha256), true, Y),
1361            (A::Scheme(T::RsaPkcs1Sha384), true, Y),
1362            (A::Scheme(T::RsaPkcs1Sha512), true, Y),
1363            (A::Scheme(T::MlDsa87), false, N),
1364            (A::Kx(KxGroup::MlKem1024), true, Y),
1365            (A::Kx(KxGroup::Secp384r1MlKem1024), true, U),
1366            (A::Kx(KxGroup::X25519MlKem768), true, U),
1367            (A::Kx(KxGroup::Secp256r1MlKem768), true, U),
1368            (A::Kx(KxGroup::Secp384r1), true, Y),
1369            (A::Kx(KxGroup::Secp256r1), true, Y),
1370            (A::Kx(KxGroup::X25519), true, N),
1371            (A::Cipher(CipherSuite::Tls13Aes256GcmSha384), true, Y),
1372            (A::Cipher(CipherSuite::Tls13Aes128GcmSha256), true, Y),
1373            (A::Cipher(CipherSuite::Tls13Chacha20Poly1305Sha256), true, N),
1374            (
1375                A::Cipher(CipherSuite::Tls12EcdheEcdsaAes256GcmSha384),
1376                true,
1377                Y,
1378            ),
1379            (
1380                A::Cipher(CipherSuite::Tls12EcdheEcdsaAes128GcmSha256),
1381                true,
1382                Y,
1383            ),
1384            (
1385                A::Cipher(CipherSuite::Tls12EcdheEcdsaChacha20Poly1305Sha256),
1386                true,
1387                N,
1388            ),
1389            (
1390                A::Cipher(CipherSuite::Tls12EcdheRsaAes256GcmSha384),
1391                true,
1392                Y,
1393            ),
1394            (
1395                A::Cipher(CipherSuite::Tls12EcdheRsaAes128GcmSha256),
1396                true,
1397                Y,
1398            ),
1399            (
1400                A::Cipher(CipherSuite::Tls12EcdheRsaChacha20Poly1305Sha256),
1401                true,
1402                N,
1403            ),
1404            (A::Hash(HashAlgorithm::Sha256), true, Y),
1405            (A::Hash(HashAlgorithm::Sha384), true, Y),
1406            (A::Hash(HashAlgorithm::Sha512), true, Y),
1407            (A::Hash(HashAlgorithm::Sha3_256), true, Y),
1408            (A::Hash(HashAlgorithm::Sha3_384), true, Y),
1409            (A::Hash(HashAlgorithm::Sha3_512), true, Y),
1410            (A::Protocol(TlsVersion::Tls12), true, Y),
1411            (A::Protocol(TlsVersion::Tls13), true, U),
1412            (A::Mac(MacAlgorithm::HmacSha256), true, Y),
1413            (A::Mac(MacAlgorithm::HmacSha384), true, Y),
1414            (A::Mac(MacAlgorithm::HmacSha512), true, Y),
1415            (A::Kdf(KdfAlgorithm::HkdfSha256), true, Y),
1416            (A::Kdf(KdfAlgorithm::HkdfSha384), true, Y),
1417            (A::Kdf(KdfAlgorithm::HkdfSha512), true, Y),
1418            (A::Kdf(KdfAlgorithm::OneStepSha256), true, Y),
1419            (A::Kdf(KdfAlgorithm::OneStepSha384), true, Y),
1420            (A::Kdf(KdfAlgorithm::OneStepSha512), true, Y),
1421            (A::KeyAgreement(KeyAgreementAlgorithm::EcdhP256), true, N),
1422            (A::KeyAgreement(KeyAgreementAlgorithm::EcdhP384), true, N),
1423            (A::KeyAgreement(KeyAgreementAlgorithm::EcdhP521), true, N),
1424            (X25519, true, N),
1425            (A::Aead(AeadAlgorithm::Aes128Gcm), true, Y),
1426            (A::Aead(AeadAlgorithm::Aes256Gcm), true, Y),
1427            (A::Aead(AeadAlgorithm::Chacha20Poly1305), true, N),
1428        ];
1429        assert_eq!(expected.len(), Algorithm::all().len());
1430        for m in [M3S, M3D] {
1431            for (a, available, kind) in &expected {
1432                let c = capability(m, *a);
1433                let got = match c.approval {
1434                    Approval::Approved { .. } => K::Approved,
1435                    Approval::NotApproved { .. } => K::NotApproved,
1436                    Approval::Unverified => K::Unverified,
1437                    Approval::AlgorithmTested { cavp } => panic!("{m:?} {a:?}: 3.x cites {cavp}"),
1438                };
1439                let want = match (kind, m.linkage) {
1440                    (K::StaticNotApproved, Linkage::Static) => K::NotApproved,
1441                    (K::StaticNotApproved, Linkage::Dynamic) => K::Approved,
1442                    (other, _) => *other,
1443                };
1444                assert_eq!((c.available, got), (*available, want), "{m:?} {a:?}");
1445            }
1446        }
1447    }
1448}