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
//! Algorithm types, the per-module capability model, the named profiles and the conformance suite
//! shared by every `crypto-profile` backend. This crate performs no cryptography.
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
/// `Options`, `ProfileSpec`, `Selection` and `AdmittedSelection` are `#[non_exhaustive]`
/// (decision 0012), so a new field is not a breaking change. Outside this crate `Options` is built
/// from its default:
///
/// ```
/// use crypto_profile::Options;
/// let mut o = Options::default();
/// o.pq_key_exchange_required = true;
/// assert_eq!(o.to_string(), "post-quantum key exchange required");
/// ```
///
/// ```compile_fail,E0639
/// use crypto_profile::Options;
/// let mut o = Options { ..Options::default() };
/// o.pq_key_exchange_required = true;
/// assert_eq!(o.to_string(), "post-quantum key exchange required");
/// ```
/// The algorithm enums, `Algorithm` and `Profile` are `#[non_exhaustive]` (decision 0012), so a
/// new variant is not a breaking change. A `match` outside this crate needs a `_` arm:
///
/// ```
/// use crypto_profile::{Algorithm, MacAlgorithm};
/// fn outside_tls(a: Algorithm) -> Option<bool> {
/// match a {
/// Algorithm::Scheme(_) | Algorithm::Kx(_) => Some(false),
/// Algorithm::Cipher(_) | Algorithm::Protocol(_) => Some(false),
/// Algorithm::Mac(_) | Algorithm::Kdf(_) | Algorithm::KeyAgreement(_) => Some(true),
/// Algorithm::Aead(_) | Algorithm::Hash(_) | Algorithm::Signature(_) => Some(true),
/// _ => None,
/// }
/// }
/// assert_eq!(outside_tls(Algorithm::Mac(MacAlgorithm::HmacSha256)), Some(true));
/// ```
///
/// ```compile_fail,E0004
/// use crypto_profile::{Algorithm, MacAlgorithm};
/// fn outside_tls(a: Algorithm) -> Option<bool> {
/// match a {
/// Algorithm::Scheme(_) | Algorithm::Kx(_) => Some(false),
/// Algorithm::Cipher(_) | Algorithm::Protocol(_) => Some(false),
/// Algorithm::Mac(_) | Algorithm::Kdf(_) | Algorithm::KeyAgreement(_) => Some(true),
/// Algorithm::Aead(_) | Algorithm::Hash(_) | Algorithm::Signature(_) => Some(true),
/// }
/// }
/// assert_eq!(outside_tls(Algorithm::Mac(MacAlgorithm::HmacSha256)), Some(true));
/// ```
///
/// ```
/// use crypto_profile::Profile;
/// fn government(p: Profile) -> Option<bool> {
/// match p {
/// Profile::Unrestricted | Profile::Preferred => Some(false),
/// Profile::Cnsa2 | Profile::Cnsa1 | Profile::Bsi => Some(true),
/// Profile::Fips1403 | Profile::Fips1402 => Some(true),
/// _ => None,
/// }
/// }
/// assert_eq!(government(Profile::Bsi), Some(true));
/// ```
///
/// ```compile_fail,E0004
/// use crypto_profile::Profile;
/// fn government(p: Profile) -> Option<bool> {
/// match p {
/// Profile::Unrestricted | Profile::Preferred => Some(false),
/// Profile::Cnsa2 | Profile::Cnsa1 | Profile::Bsi => Some(true),
/// Profile::Fips1403 | Profile::Fips1402 => Some(true),
/// }
/// }
/// assert_eq!(government(Profile::Bsi), Some(true));
/// ```
///
/// ```
/// use crypto_profile::HashAlgorithm;
/// fn sha3(h: HashAlgorithm) -> Option<bool> {
/// match h {
/// HashAlgorithm::Sha256 | HashAlgorithm::Sha384 | HashAlgorithm::Sha512 => Some(false),
/// HashAlgorithm::Sha3_256 | HashAlgorithm::Sha3_384 | HashAlgorithm::Sha3_512 => Some(true),
/// _ => None,
/// }
/// }
/// assert_eq!(sha3(HashAlgorithm::Sha3_384), Some(true));
/// ```
///
/// ```compile_fail,E0004
/// use crypto_profile::HashAlgorithm;
/// fn sha3(h: HashAlgorithm) -> Option<bool> {
/// match h {
/// HashAlgorithm::Sha256 | HashAlgorithm::Sha384 | HashAlgorithm::Sha512 => Some(false),
/// HashAlgorithm::Sha3_256 | HashAlgorithm::Sha3_384 | HashAlgorithm::Sha3_512 => Some(true),
/// }
/// }
/// assert_eq!(sha3(HashAlgorithm::Sha3_384), Some(true));
/// ```
///
/// ```
/// use crypto_profile::SignatureAlgorithm as S;
/// fn pss(a: S) -> Option<bool> {
/// match a {
/// S::Rsa2048Sha384Pss | S::Rsa3072Sha384Pss | S::Rsa4096Sha384Pss => Some(true),
/// S::Rsa2048Sha384 | S::Rsa3072Sha384 | S::Rsa4096Sha384 => Some(false),
/// S::MlDsa87 | S::EcdsaP256Sha256 | S::EcdsaP384Sha384 => Some(false),
/// S::EcdsaP521Sha384 | S::EcdsaP521Sha512 | S::Ed25519 => Some(false),
/// _ => None,
/// }
/// }
/// assert_eq!(pss(S::Rsa3072Sha384Pss), Some(true));
/// ```
///
/// ```compile_fail,E0004
/// use crypto_profile::SignatureAlgorithm as S;
/// fn pss(a: S) -> Option<bool> {
/// match a {
/// S::Rsa2048Sha384Pss | S::Rsa3072Sha384Pss | S::Rsa4096Sha384Pss => Some(true),
/// S::Rsa2048Sha384 | S::Rsa3072Sha384 | S::Rsa4096Sha384 => Some(false),
/// S::MlDsa87 | S::EcdsaP256Sha256 | S::EcdsaP384Sha384 => Some(false),
/// S::EcdsaP521Sha384 | S::EcdsaP521Sha512 | S::Ed25519 => Some(false),
/// }
/// }
/// assert_eq!(pss(S::Rsa3072Sha384Pss), Some(true));
/// ```
/// Every `ALL` is a `&'static [T]` (decision 0012), so a new variant changes its length, not its
/// type:
///
/// ```
/// use crypto_profile::{HashAlgorithm, Profile};
/// let all: &'static [Profile] = Profile::ALL;
/// assert!(all.contains(&Profile::Cnsa2));
/// let mut lens = Vec::new();
/// for &h in HashAlgorithm::ALL {
/// lens.push(h.output_len());
/// }
/// assert_eq!(lens, [32, 48, 64, 32, 48, 64]);
/// ```
include!;
include!;