crypto-profile 0.9.0

Named cryptographic profiles over the AWS-LC FIPS module: algorithm types, per-module capabilities and a conformance suite
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! The profile golden tables every backend's `resolve` is checked against; literal IANA
//! codepoints and key names. The three rows the option leaves empty (`cnsa1`, `bsi` and `fips1402`
//! with the option) specify the refusal `resolve` gives; their lists are never compared with a
//! resolved profile.
use crate::{
    Algorithm, CryptoError, Family, Module, ModuleLine, Options, Profile, SignatureAlgorithm,
    TlsSignatureScheme,
};

/// One profile's lists, with or without the option.
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct Golden {
    /// The profile.
    pub profile: Profile,
    /// Whether post-quantum key exchange is required.
    pub pq: bool,
    /// Protocol versions.
    pub versions: &'static [u16],
    /// Cipher suites.
    pub suites: &'static [u16],
    /// Key-exchange groups.
    pub groups: &'static [u16],
    /// Handshake signature schemes.
    pub schemes: &'static [u16],
    /// Key algorithms.
    pub keys: &'static [&'static str],
    /// Entries excluded by the module line, in family and preference order.
    pub excluded: &'static [Algorithm],
}

/// Every handshake scheme, in canonical order.
#[doc(hidden)]
pub const ALL_SCHEMES: &[u16] = &[
    0x0906, 0x0503, 0x0403, 0x0603, 0x0807, 0x0805, 0x0806, 0x0804, 0x0501, 0x0601, 0x0401,
];
/// Every handshake scheme but RSA PKCS#1 v1.5, in canonical order.
#[doc(hidden)]
pub const ALL_SCHEMES_BUT_PKCS1: &[u16] = &[
    0x0906, 0x0503, 0x0403, 0x0603, 0x0807, 0x0805, 0x0806, 0x0804,
];
/// Every key algorithm, in preference order.
#[doc(hidden)]
pub const ALL_KEYS: &[&str] = &[
    "ml-dsa-87",
    "ecdsa-p384-sha384",
    "ecdsa-p256-sha256",
    "ecdsa-p521-sha512",
    "ecdsa-p521-sha384",
    "ed25519",
    "rsa3072-sha384",
    "rsa4096-sha384",
    "rsa2048-sha384",
];
/// The cipher suites of `bsi`, `fips1403` and `fips1402`.
#[doc(hidden)]
pub const FIPS_SUITES: &[u16] = &[0x1302, 0x1301, 0xC02C, 0xC030, 0xC02B, 0xC02F];
/// `preferred`'s handshake schemes.
#[doc(hidden)]
pub const PREFERRED_SCHEMES: &[u16] = &[0x0906, 0x0503, 0x0403, 0x0603, 0x0805, 0x0806, 0x0804];
/// `preferred`'s key algorithms.
#[doc(hidden)]
pub const PREFERRED_KEYS: &[&str] = &[
    "ml-dsa-87",
    "ecdsa-p384-sha384",
    "ecdsa-p256-sha256",
    "ecdsa-p521-sha512",
    "ecdsa-p521-sha384",
    "rsa3072-sha384",
    "rsa4096-sha384",
];

/// One row per profile and option value.
#[doc(hidden)]
pub const GOLDEN: &[Golden] = &[
    Golden {
        excluded: &[],
        profile: Profile::Unrestricted,
        pq: false,
        versions: &[0x0304, 0x0303],
        suites: &[
            0x1302, 0x1301, 0x1303, 0xC02C, 0xC030, 0xC02B, 0xC02F, 0xCCA9, 0xCCA8,
        ],
        groups: &[0x0202, 0x11ED, 0x11EC, 0x11EB, 0x0018, 0x0017, 0x001D],
        schemes: ALL_SCHEMES,
        keys: ALL_KEYS,
    },
    Golden {
        excluded: &[],
        profile: Profile::Unrestricted,
        pq: true,
        versions: &[0x0304],
        suites: &[0x1302, 0x1301, 0x1303],
        groups: &[0x0202, 0x11ED, 0x11EC, 0x11EB],
        schemes: ALL_SCHEMES_BUT_PKCS1,
        keys: ALL_KEYS,
    },
    Golden {
        excluded: &[],
        profile: Profile::Preferred,
        pq: false,
        versions: &[0x0304],
        suites: &[0x1302, 0x1301],
        groups: &[0x0202, 0x11ED, 0x11EC, 0x11EB, 0x0018, 0x0017],
        schemes: PREFERRED_SCHEMES,
        keys: PREFERRED_KEYS,
    },
    Golden {
        excluded: &[],
        profile: Profile::Preferred,
        pq: true,
        versions: &[0x0304],
        suites: &[0x1302, 0x1301],
        groups: &[0x0202, 0x11ED, 0x11EC, 0x11EB],
        schemes: PREFERRED_SCHEMES,
        keys: PREFERRED_KEYS,
    },
    Golden {
        excluded: &[],
        profile: Profile::Cnsa2,
        pq: false,
        versions: &[0x0304],
        suites: &[0x1302],
        groups: &[0x0202],
        schemes: &[0x0906],
        keys: &["ml-dsa-87"],
    },
    Golden {
        excluded: &[],
        profile: Profile::Cnsa2,
        pq: true,
        versions: &[0x0304],
        suites: &[0x1302],
        groups: &[0x0202],
        schemes: &[0x0906],
        keys: &["ml-dsa-87"],
    },
    Golden {
        excluded: &[],
        profile: Profile::Cnsa1,
        pq: false,
        versions: &[0x0304, 0x0303],
        suites: &[0x1302, 0xC02C, 0xC030],
        groups: &[0x0018],
        schemes: &[0x0503, 0x0805, 0x0501],
        keys: &["ecdsa-p384-sha384", "rsa3072-sha384", "rsa4096-sha384"],
    },
    Golden {
        excluded: &[],
        profile: Profile::Bsi,
        pq: false,
        versions: &[0x0304, 0x0303],
        suites: FIPS_SUITES,
        groups: &[0x0018, 0x0017],
        schemes: &[0x0503, 0x0403, 0x0603, 0x0805, 0x0806, 0x0804],
        keys: &[
            "ecdsa-p384-sha384",
            "ecdsa-p256-sha256",
            "ecdsa-p521-sha512",
        ],
    },
    Golden {
        excluded: &[],
        profile: Profile::Fips1403,
        pq: false,
        versions: &[0x0304, 0x0303],
        suites: FIPS_SUITES,
        groups: &[0x0202, 0x11ED, 0x11EC, 0x11EB, 0x0018, 0x0017],
        schemes: ALL_SCHEMES,
        keys: ALL_KEYS,
    },
    Golden {
        excluded: &[],
        profile: Profile::Fips1403,
        pq: true,
        versions: &[0x0304],
        suites: &[0x1302, 0x1301],
        groups: &[0x0202, 0x11ED, 0x11EC, 0x11EB],
        schemes: ALL_SCHEMES_BUT_PKCS1,
        keys: ALL_KEYS,
    },
    Golden {
        excluded: &[],
        profile: Profile::Fips1402,
        pq: false,
        versions: &[0x0304, 0x0303],
        suites: FIPS_SUITES,
        groups: &[0x0018, 0x0017],
        schemes: &[
            0x0503, 0x0403, 0x0603, 0x0805, 0x0806, 0x0804, 0x0501, 0x0601, 0x0401,
        ],
        keys: &[
            "ecdsa-p384-sha384",
            "ecdsa-p256-sha256",
            "ecdsa-p521-sha512",
            "ecdsa-p521-sha384",
            "rsa3072-sha384",
            "rsa4096-sha384",
            "rsa2048-sha384",
        ],
    },
    Golden {
        excluded: &[],
        profile: Profile::Cnsa1,
        pq: true,
        versions: &[0x0304],
        suites: &[0x1302],
        groups: &[],
        schemes: &[0x0503, 0x0805],
        keys: &["ecdsa-p384-sha384", "rsa3072-sha384", "rsa4096-sha384"],
    },
    Golden {
        excluded: &[],
        profile: Profile::Bsi,
        pq: true,
        versions: &[0x0304],
        suites: &[0x1302, 0x1301],
        groups: &[],
        schemes: &[0x0503, 0x0403, 0x0603, 0x0805, 0x0806, 0x0804],
        keys: &[
            "ecdsa-p384-sha384",
            "ecdsa-p256-sha256",
            "ecdsa-p521-sha512",
        ],
    },
    Golden {
        excluded: &[],
        profile: Profile::Fips1402,
        pq: true,
        versions: &[0x0304],
        suites: &[0x1302, 0x1301],
        groups: &[],
        schemes: &[0x0503, 0x0403, 0x0603, 0x0805, 0x0806, 0x0804],
        keys: &[
            "ecdsa-p384-sha384",
            "ecdsa-p256-sha256",
            "ecdsa-p521-sha512",
            "ecdsa-p521-sha384",
            "rsa3072-sha384",
            "rsa4096-sha384",
            "rsa2048-sha384",
        ],
    },
];

/// The independent per-line golden row, or the first empty-family refusal.
#[doc(hidden)]
pub fn expected(module: Module, profile: Profile, options: Options) -> Result<Golden, CryptoError> {
    let mut g = GOLDEN
        .iter()
        .find(|g| g.profile == profile && g.pq == options.pq_key_exchange_required)
        .unwrap_or_else(|| panic!("no golden row for {profile} {options}"))
        .clone();
    match module.line {
        ModuleLine::AwsLcFips4 => {}
        ModuleLine::AwsLcFips3 => {
            match profile {
                Profile::Unrestricted | Profile::Fips1403 => {
                    g.schemes = if options.pq_key_exchange_required {
                        &[0x0503, 0x0403, 0x0603, 0x0807, 0x0805, 0x0806, 0x0804]
                    } else {
                        &[
                            0x0503, 0x0403, 0x0603, 0x0807, 0x0805, 0x0806, 0x0804, 0x0501, 0x0601,
                            0x0401,
                        ]
                    };
                    g.keys = &[
                        "ecdsa-p384-sha384",
                        "ecdsa-p256-sha256",
                        "ecdsa-p521-sha512",
                        "ecdsa-p521-sha384",
                        "ed25519",
                        "rsa3072-sha384",
                        "rsa4096-sha384",
                        "rsa2048-sha384",
                    ];
                }
                Profile::Preferred => {
                    g.schemes = &[0x0503, 0x0403, 0x0603, 0x0805, 0x0806, 0x0804];
                    g.keys = &[
                        "ecdsa-p384-sha384",
                        "ecdsa-p256-sha256",
                        "ecdsa-p521-sha512",
                        "ecdsa-p521-sha384",
                        "rsa3072-sha384",
                        "rsa4096-sha384",
                    ];
                }
                Profile::Cnsa2 => {
                    g.schemes = &[];
                    g.keys = &[];
                }
                Profile::Cnsa1 | Profile::Bsi | Profile::Fips1402 => {}
            }
            match profile {
                Profile::Unrestricted | Profile::Preferred | Profile::Fips1403 | Profile::Cnsa2 => {
                    g.excluded = &[
                        Algorithm::Scheme(TlsSignatureScheme::MlDsa87),
                        Algorithm::Signature(SignatureAlgorithm::MlDsa87),
                    ];
                }
                Profile::Cnsa1 | Profile::Bsi | Profile::Fips1402 => {}
            }
        }
    }
    for (empty, category) in [
        (g.versions.is_empty(), Family::Version),
        (g.suites.is_empty(), Family::CipherSuite),
        (g.groups.is_empty(), Family::KxGroup),
        (g.schemes.is_empty(), Family::SignatureScheme),
        (g.keys.is_empty(), Family::KeyAlgorithm),
    ] {
        if empty {
            return Err(CryptoError::EmptyProfile {
                profile,
                options,
                category,
            });
        }
    }
    Ok(g)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::capability::{Linkage, ModuleLine};
    use crate::error::Family;
    use crate::profile::select;
    use crate::{Algorithm, TlsSignatureScheme};

    const M3S: Module = Module {
        line: ModuleLine::AwsLcFips3,
        linkage: Linkage::Static,
    };
    const M3D: Module = Module {
        line: ModuleLine::AwsLcFips3,
        linkage: Linkage::Dynamic,
    };
    const M4S: Module = Module {
        line: ModuleLine::AwsLcFips4,
        linkage: Linkage::Static,
    };
    const M4D: Module = Module {
        line: ModuleLine::AwsLcFips4,
        linkage: Linkage::Dynamic,
    };
    const PQ: Options = Options {
        peer_certificates_match_profile: false,
        pq_key_exchange_required: true,
    };

    type Lists = (
        Vec<u16>,
        Vec<u16>,
        Vec<u16>,
        Vec<u16>,
        Vec<&'static str>,
        Vec<Algorithm>,
    );

    #[test]
    fn every_profile_and_option_has_exactly_one_golden_row() {
        assert_eq!(GOLDEN.len(), 14);
        for p in Profile::ALL {
            for pq in [false, true] {
                let n = GOLDEN
                    .iter()
                    .filter(|g| g.profile == p && g.pq == pq)
                    .count();
                assert_eq!(n, 1, "{p} {pq}");
            }
        }
    }

    #[test]
    fn select_agrees_with_the_golden_rows_on_every_module() {
        for m in [M3S, M3D, M4S, M4D] {
            let mut successes = 0;
            for p in Profile::ALL {
                for pq in [false, true] {
                    let o = Options {
                        peer_certificates_match_profile: false,
                        pq_key_exchange_required: pq,
                    };
                    let got: Result<Lists, CryptoError> = select(p, o, m).map(|s| {
                        (
                            s.versions.iter().map(|x| x.iana()).collect(),
                            s.cipher_suites.iter().map(|x| x.iana()).collect(),
                            s.kx_groups.iter().map(|x| x.iana()).collect(),
                            s.signature_schemes.iter().map(|x| x.iana()).collect(),
                            s.key_algorithms.iter().map(|x| x.as_str()).collect(),
                            s.excluded,
                        )
                    });
                    let want: Result<Lists, CryptoError> = expected(m, p, o).map(|g| {
                        (
                            g.versions.to_vec(),
                            g.suites.to_vec(),
                            g.groups.to_vec(),
                            g.schemes.to_vec(),
                            g.keys.to_vec(),
                            g.excluded.to_vec(),
                        )
                    });
                    assert_eq!(got, want, "{m:?} {p} {o}");
                    successes += usize::from(got.is_ok());
                }
            }
            assert_eq!(
                successes,
                match m.line {
                    ModuleLine::AwsLcFips3 => 9,
                    ModuleLine::AwsLcFips4 => 11,
                }
            );
        }
    }

    #[test]
    fn expected_records_the_3x_refusals() {
        let d = Options::default();
        assert_eq!(
            expected(M3S, Profile::Cnsa2, d).map(|_| ()),
            Err(CryptoError::EmptyProfile {
                profile: Profile::Cnsa2,
                options: d,
                category: Family::SignatureScheme,
            })
        );
        assert_eq!(
            expected(M3S, Profile::Preferred, PQ).unwrap().excluded,
            &[
                Algorithm::Scheme(TlsSignatureScheme::MlDsa87),
                Algorithm::Signature(SignatureAlgorithm::MlDsa87)
            ]
        );
        assert!(expected(M3S, Profile::Cnsa1, d).is_ok());
        assert_eq!(
            expected(M3S, Profile::Fips1402, PQ).map(|_| ()),
            Err(CryptoError::EmptyProfile {
                profile: Profile::Fips1402,
                options: PQ,
                category: Family::KxGroup,
            })
        );
        assert!(expected(M4S, Profile::Cnsa2, d).is_ok());
    }
}