@@ -10,229 +10,4 @@ seconds_since_epoch()
10
10
return std::time (nullptr );
11
11
}
12
12
13
- // /
14
- // / CipherSuites and details
15
- // /
16
-
17
- using hpke::AEAD;
18
- using hpke::Digest;
19
- using hpke::HPKE;
20
- using hpke::KDF;
21
- using hpke::KEM;
22
- using hpke::Signature;
23
-
24
- struct CipherAlgorithms
25
- {
26
- KEM::ID kem_id;
27
- KDF::ID kdf_id;
28
- AEAD::ID aead_id;
29
- Digest::ID digest_id;
30
- Signature::ID sig_id;
31
- SignatureScheme scheme;
32
- };
33
-
34
- template <CipherSuite::ID CS>
35
- extern const CipherAlgorithms cipher_algs;
36
-
37
- template <>
38
- const CipherAlgorithms
39
- cipher_algs<CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519>{
40
- KEM::ID::DHKEM_X25519_SHA256, KDF::ID::HKDF_SHA256,
41
- AEAD::ID::AES_128_GCM, Digest::ID::SHA256,
42
- Signature::ID::Ed25519, SignatureScheme::Ed25519,
43
- };
44
-
45
- template <>
46
- const CipherAlgorithms cipher_algs<CipherSuite::ID::P256_AES128GCM_SHA256_P256>{
47
- KEM::ID::DHKEM_P256_SHA256, KDF::ID::HKDF_SHA256,
48
- AEAD::ID::AES_128_GCM, Digest::ID::SHA256,
49
- Signature::ID::P256_SHA256, SignatureScheme::P256_SHA256,
50
- };
51
-
52
- template <>
53
- const CipherAlgorithms
54
- cipher_algs<CipherSuite::ID::X25519_CHACHA20POLY1305_SHA256_Ed25519>{
55
- KEM::ID::DHKEM_X25519_SHA256, KDF::ID::HKDF_SHA256,
56
- AEAD::ID::CHACHA20_POLY1305, Digest::ID::SHA256,
57
- Signature::ID::Ed25519, SignatureScheme::Ed25519,
58
- };
59
-
60
- template <>
61
- const CipherAlgorithms
62
- cipher_algs<CipherSuite::ID::X448_AES256GCM_SHA512_Ed448>{
63
- KEM::ID::DHKEM_X448_SHA512, KDF::ID::HKDF_SHA512, AEAD::ID::AES_256_GCM,
64
- Digest::ID::SHA512, Signature::ID::Ed448, SignatureScheme::Ed448,
65
- };
66
-
67
- template <>
68
- const CipherAlgorithms cipher_algs<CipherSuite::ID::P521_AES256GCM_SHA512_P521>{
69
- KEM::ID::DHKEM_P521_SHA512, KDF::ID::HKDF_SHA512,
70
- AEAD::ID::AES_256_GCM, Digest::ID::SHA512,
71
- Signature::ID::P521_SHA512, SignatureScheme::P521_SHA512,
72
- };
73
-
74
- template <>
75
- const CipherAlgorithms
76
- cipher_algs<CipherSuite::ID::X448_CHACHA20POLY1305_SHA512_Ed448>{
77
- KEM::ID::DHKEM_X448_SHA512, KDF::ID::HKDF_SHA512,
78
- AEAD::ID::CHACHA20_POLY1305, Digest::ID::SHA512,
79
- Signature::ID::Ed448, SignatureScheme::Ed448,
80
- };
81
-
82
- static const CipherAlgorithms&
83
- algs_for_suite (CipherSuite::ID id)
84
- {
85
- switch (id) {
86
- case CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519:
87
- return cipher_algs<CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519>;
88
-
89
- case CipherSuite::ID::P256_AES128GCM_SHA256_P256:
90
- return cipher_algs<CipherSuite::ID::P256_AES128GCM_SHA256_P256>;
91
-
92
- case CipherSuite::ID::X25519_CHACHA20POLY1305_SHA256_Ed25519:
93
- return cipher_algs<
94
- CipherSuite::ID::X25519_CHACHA20POLY1305_SHA256_Ed25519>;
95
-
96
- case CipherSuite::ID::X448_AES256GCM_SHA512_Ed448:
97
- return cipher_algs<CipherSuite::ID::X448_AES256GCM_SHA512_Ed448>;
98
-
99
- case CipherSuite::ID::P521_AES256GCM_SHA512_P521:
100
- return cipher_algs<CipherSuite::ID::P521_AES256GCM_SHA512_P521>;
101
-
102
- case CipherSuite::ID::X448_CHACHA20POLY1305_SHA512_Ed448:
103
- return cipher_algs<CipherSuite::ID::X448_CHACHA20POLY1305_SHA512_Ed448>;
104
-
105
- default :
106
- throw InvalidParameterError (" Unsupported ciphersuite" );
107
- }
108
- }
109
-
110
- static std::unique_ptr<HPKE>
111
- hpke_for_suite (CipherSuite::ID id)
112
- {
113
- const auto & algs = algs_for_suite (id);
114
- return std::make_unique<HPKE>(algs.kem_id , algs.kdf_id , algs.aead_id );
115
- }
116
-
117
- static std::unique_ptr<Digest>
118
- digest_for_suite (CipherSuite::ID id)
119
- {
120
- return Digest::create (algs_for_suite (id).digest_id );
121
- }
122
-
123
- static std::unique_ptr<Signature>
124
- sig_for_suite (CipherSuite::ID id)
125
- {
126
- return Signature::create (algs_for_suite (id).sig_id );
127
- }
128
-
129
- SignatureScheme
130
- scheme_for_suite (CipherSuite::ID id)
131
- {
132
- return algs_for_suite (id).scheme ;
133
- }
134
-
135
- CipherSuite::CipherSuite ()
136
- : id(CipherSuite::ID::unknown)
137
- {}
138
-
139
- CipherSuite::CipherSuite (ID id_in)
140
- : id(id_in)
141
- {
142
- reset (id);
143
- }
144
-
145
- CipherSuite::CipherSuite (const CipherSuite& other)
146
- : id(other.id)
147
- {
148
- reset (id);
149
- }
150
-
151
- CipherSuite::CipherSuite (CipherSuite&& other)
152
- : id(other.id)
153
- , hpke(std::move(other.hpke))
154
- , digest(std::move(other.digest))
155
- , sig(std::move(other.sig))
156
- {}
157
-
158
- CipherSuite&
159
- CipherSuite::operator =(const CipherSuite& other)
160
- {
161
- if (this != &other) {
162
- reset (other.id );
163
- }
164
- return *this ;
165
- }
166
-
167
- struct HKDFLabel
168
- {
169
- uint16_t length;
170
- bytes label;
171
- bytes context;
172
-
173
- TLS_SERIALIZABLE (length, label, context)
174
- TLS_TRAITS (tls::pass, tls::vector<1 >, tls::vector<4 >)
175
- };
176
-
177
- bytes
178
- CipherSuite::expand_with_label (const bytes& secret,
179
- const std::string& label,
180
- const bytes& context,
181
- size_t length) const
182
- {
183
- auto mls_label = to_bytes (std::string (" mls10 " ) + label);
184
- auto length16 = static_cast <uint16_t >(length);
185
- auto label_bytes = tls::marshal (HKDFLabel{ length16, mls_label, context });
186
- return hpke->kdf ->expand (secret, label_bytes, length);
187
- }
188
-
189
- void
190
- CipherSuite::reset (ID id_in)
191
- {
192
- if (id_in == ID::unknown) {
193
- return ;
194
- }
195
-
196
- id = id_in;
197
- hpke = hpke_for_suite (id);
198
- digest = digest_for_suite (id);
199
- sig = sig_for_suite (id);
200
- }
201
-
202
- tls::istream&
203
- operator >>(tls::istream& str, CipherSuite& suite)
204
- {
205
- CipherSuite::ID id;
206
- str >> id;
207
- suite = CipherSuite (id);
208
- return str;
209
- }
210
-
211
- tls::ostream&
212
- operator <<(tls::ostream& str, const CipherSuite& suite)
213
- {
214
- return str << suite.id ;
215
- }
216
-
217
- bool
218
- operator ==(const CipherSuite& lhs, const CipherSuite& rhs)
219
- {
220
- return lhs.id == rhs.id ;
221
- }
222
-
223
- bool
224
- operator !=(const CipherSuite& lhs, const CipherSuite& rhs)
225
- {
226
- return lhs.id != rhs.id ;
227
- }
228
-
229
- const std::array<CipherSuite::ID, 6 > all_supported_suites = {
230
- CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519,
231
- CipherSuite::ID::P256_AES128GCM_SHA256_P256,
232
- CipherSuite::ID::X25519_CHACHA20POLY1305_SHA256_Ed25519,
233
- CipherSuite::ID::X448_AES256GCM_SHA512_Ed448,
234
- CipherSuite::ID::P521_AES256GCM_SHA512_P521,
235
- CipherSuite::ID::X448_CHACHA20POLY1305_SHA512_Ed448,
236
- };
237
-
238
13
} // namespace mls
0 commit comments