Debian Codegraph @

crt/s2n/tls/s2n_kem.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "tls/s2n_kem.h"
17
18 #include "crypto/s2n_evp_kem.h"
19 #include "crypto/s2n_pq.h"
20 #include "stuffer/s2n_stuffer.h"
21 #include "tls/extensions/s2n_key_share.h"
22 #include "tls/s2n_tls_parameters.h"
23 #include "utils/s2n_mem.h"
24 #include "utils/s2n_safety.h"
25
26 const struct s2n_kem s2n_mlkem_768 = {
27 .name = "mlkem768",
28 .kem_nid = S2N_NID_MLKEM768,
29 .kem_extension_id = 0, /* This is not used in TLS 1.2's KEM extension */
30 .public_key_length = S2N_MLKEM_768_PUBLIC_KEY_BYTES,
31 .private_key_length = S2N_MLKEM_768_SECRET_KEY_BYTES,
32 .shared_secret_key_length = S2N_MLKEM_768_SHARED_SECRET_BYTES,
33 .ciphertext_length = S2N_MLKEM_768_CIPHERTEXT_BYTES,
34 .generate_keypair = &s2n_evp_kem_generate_keypair,
35 .encapsulate = &s2n_evp_kem_encapsulate,
36 .decapsulate = &s2n_evp_kem_decapsulate,
37 };
38
39 const struct s2n_kem s2n_mlkem_1024 = {
40 .name = "mlkem1024",
41 .kem_nid = S2N_NID_MLKEM1024,
42 .kem_extension_id = 0, /* This is not used in TLS 1.2's KEM extension */
43 .public_key_length = S2N_MLKEM_1024_PUBLIC_KEY_BYTES,
44 .private_key_length = S2N_MLKEM_1024_SECRET_KEY_BYTES,
45 .shared_secret_key_length = S2N_MLKEM_1024_SHARED_SECRET_BYTES,
46 .ciphertext_length = S2N_MLKEM_1024_CIPHERTEXT_BYTES,
47 .generate_keypair = &s2n_evp_kem_generate_keypair,
48 .encapsulate = &s2n_evp_kem_encapsulate,
49 .decapsulate = &s2n_evp_kem_decapsulate,
50 };
51
52 const struct s2n_iana_to_kem kem_mapping[1] = {
53 {
54 .iana_value = { TLS_NULL_WITH_NULL_NULL },
55 .kems = NULL,
56 .kem_count = 0,
57 },
58 };
59
60 /*
61 * ML-KEM based pure PQ KEMs as specified by IETF and registered in IANA.
62 *
63 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
64 * https://datatracker.ietf.org/doc/draft-connolly-tls-mlkem-key-agreement/05/
65 */
66 const struct s2n_kem_group s2n_pure_mlkem_1024 = {
67 .name = "MLKEM1024",
68 .iana_id = TLS_PQ_KEM_GROUP_ID_MLKEM_1024,
69 .curve = &s2n_ecc_curve_none,
70 .kem = &s2n_mlkem_1024,
71 .send_kem_first = 0,
72 };
73
74 /*
75 * ML-KEM based hybrid KEMs as specified by IETF and registered in IANA.
76 *
77 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
78 * https://datatracker.ietf.org/doc/draft-kwiatkowski-tls-ecdhe-mlkem/
79 */
80 const struct s2n_kem_group s2n_secp256r1_mlkem_768 = {
81 .name = "SecP256r1MLKEM768",
82 .iana_id = TLS_PQ_KEM_GROUP_ID_SECP256R1_MLKEM_768,
83 .curve = &s2n_ecc_curve_secp256r1,
84 .kem = &s2n_mlkem_768,
85 .send_kem_first = 0,
86 };
87
88 const struct s2n_kem_group s2n_x25519_mlkem_768 = {
89 .name = "X25519MLKEM768",
90 .iana_id = TLS_PQ_KEM_GROUP_ID_X25519_MLKEM_768,
91 .curve = &s2n_ecc_curve_x25519,
92 .kem = &s2n_mlkem_768,
93 /* ML-KEM KeyShare should always be sent first for X25519MLKEM768.
94 * https://datatracker.ietf.org/doc/html/draft-kwiatkowski-tls-ecdhe-mlkem-02#name-negotiated-groups */
95 .send_kem_first = 1,
96 };
97
98 const struct s2n_kem_group s2n_secp384r1_mlkem_1024 = {
99 .name = "SecP384r1MLKEM1024",
100 .iana_id = TLS_PQ_KEM_GROUP_ID_SECP384R1_MLKEM_1024,
101 .curve = &s2n_ecc_curve_secp384r1,
102 .kem = &s2n_mlkem_1024,
103 .send_kem_first = 0,
104 };
105
106 const struct s2n_kem_group *ALL_SUPPORTED_KEM_GROUPS[] = {
107 &s2n_x25519_mlkem_768,
108 &s2n_secp256r1_mlkem_768,
109 &s2n_secp384r1_mlkem_1024,
110 &s2n_pure_mlkem_1024,
111 };
112
113 /* Helper safety macro to call the NIST PQ KEM functions. The NIST
114 * functions may return any non-zero value to indicate failure. */
115 #define GUARD_PQ_AS_RESULT(x) RESULT_ENSURE((x) == 0, S2N_ERR_PQ_CRYPTO)
116
117 S2N_RESULT s2n_kem_generate_keypair(struct s2n_kem_params *kem_params)
118 {
119 RESULT_ENSURE_REF(kem_params);
120 RESULT_ENSURE_REF(kem_params->kem);
121 const struct s2n_kem *kem = kem_params->kem;
122 RESULT_ENSURE_REF(kem->generate_keypair);
123
124 RESULT_ENSURE_REF(kem_params->public_key.data);
125 RESULT_ENSURE(kem_params->public_key.size == kem->public_key_length, S2N_ERR_SAFETY);
126
127 /* Need to save the private key for decapsulation */
128 RESULT_GUARD_POSIX(s2n_realloc(&kem_params->private_key, kem->private_key_length));
129
130 GUARD_PQ_AS_RESULT(kem->generate_keypair(kem, kem_params->public_key.data, kem_params->private_key.data));
131 return S2N_RESULT_OK;
132 }
133
134 S2N_RESULT s2n_kem_encapsulate(struct s2n_kem_params *kem_params, struct s2n_blob *ciphertext)
135 {
136 RESULT_ENSURE_REF(kem_params);
137 RESULT_ENSURE_REF(kem_params->kem);
138 const struct s2n_kem *kem = kem_params->kem;
139 RESULT_ENSURE_REF(kem->encapsulate);
140
141 RESULT_ENSURE(kem_params->public_key.size == kem->public_key_length, S2N_ERR_SAFETY);
142 RESULT_ENSURE_REF(kem_params->public_key.data);
143
144 RESULT_ENSURE_REF(ciphertext);
145 RESULT_ENSURE_REF(ciphertext->data);
146 RESULT_ENSURE(ciphertext->size == kem->ciphertext_length, S2N_ERR_SAFETY);
147
148 /* Need to save the shared secret for key derivation */
149 RESULT_GUARD_POSIX(s2n_alloc(&(kem_params->shared_secret), kem->shared_secret_key_length));
150
151 GUARD_PQ_AS_RESULT(kem->encapsulate(kem, ciphertext->data, kem_params->shared_secret.data, kem_params->public_key.data));
152 return S2N_RESULT_OK;
153 }
154
155 S2N_RESULT s2n_kem_decapsulate(struct s2n_kem_params *kem_params, const struct s2n_blob *ciphertext)
156 {
157 RESULT_ENSURE_REF(kem_params);
158 RESULT_ENSURE_REF(kem_params->kem);
159 const struct s2n_kem *kem = kem_params->kem;
160 RESULT_ENSURE_REF(kem->decapsulate);
161
162 RESULT_ENSURE(kem_params->private_key.size == kem->private_key_length, S2N_ERR_SAFETY);
163 RESULT_ENSURE_REF(kem_params->private_key.data);
164
165 RESULT_ENSURE_REF(ciphertext);
166 RESULT_ENSURE_REF(ciphertext->data);
167 RESULT_ENSURE(ciphertext->size == kem->ciphertext_length, S2N_ERR_SAFETY);
168
169 /* Need to save the shared secret for key derivation */
170 RESULT_GUARD_POSIX(s2n_alloc(&(kem_params->shared_secret), kem->shared_secret_key_length));
171
172 GUARD_PQ_AS_RESULT(kem->decapsulate(kem, kem_params->shared_secret.data, ciphertext->data, kem_params->private_key.data));
173 return S2N_RESULT_OK;
174 }
175
176 static int s2n_kem_check_kem_compatibility(const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN], const struct s2n_kem *candidate_kem,
177 uint8_t *kem_is_compatible)
178 {
179 const struct s2n_iana_to_kem *compatible_kems = NULL;
180 POSIX_GUARD(s2n_cipher_suite_to_kem(iana_value, &compatible_kems));
181
182 for (uint8_t i = 0; i < compatible_kems->kem_count; i++) {
183 if (candidate_kem->kem_extension_id == compatible_kems->kems[i]->kem_extension_id) {
184 *kem_is_compatible = 1;
185 return S2N_SUCCESS;
186 }
187 }
188
189 *kem_is_compatible = 0;
190 return S2N_SUCCESS;
191 }
192
193 int s2n_choose_kem_with_peer_pref_list(const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN], struct s2n_blob *client_kem_ids,
194 const struct s2n_kem *server_kem_pref_list[], const uint8_t num_server_supported_kems, const struct s2n_kem **chosen_kem)
195 {
196 struct s2n_stuffer client_kem_ids_stuffer = { 0 };
197 POSIX_GUARD(s2n_stuffer_init(&client_kem_ids_stuffer, client_kem_ids));
198 POSIX_GUARD(s2n_stuffer_write(&client_kem_ids_stuffer, client_kem_ids));
199
200 /* Each KEM ID is 2 bytes */
201 uint8_t num_client_candidate_kems = client_kem_ids->size / 2;
202
203 for (uint8_t i = 0; i < num_server_supported_kems; i++) {
204 const struct s2n_kem *candidate_server_kem = (server_kem_pref_list[i]);
205
206 uint8_t server_kem_is_compatible = 0;
207 POSIX_GUARD(s2n_kem_check_kem_compatibility(iana_value, candidate_server_kem, &server_kem_is_compatible));
208
209 if (!server_kem_is_compatible) {
210 continue;
211 }
212
213 for (uint8_t j = 0; j < num_client_candidate_kems; j++) {
214 kem_extension_size candidate_client_kem_id = 0;
215 POSIX_GUARD(s2n_stuffer_read_uint16(&client_kem_ids_stuffer, &candidate_client_kem_id));
216
217 if (candidate_server_kem->kem_extension_id == candidate_client_kem_id) {
218 *chosen_kem = candidate_server_kem;
219 return S2N_SUCCESS;
220 }
221 }
222 POSIX_GUARD(s2n_stuffer_reread(&client_kem_ids_stuffer));
223 }
224
225 /* Client and server did not propose any mutually supported KEMs compatible with the ciphersuite */
226 POSIX_BAIL(S2N_ERR_KEM_UNSUPPORTED_PARAMS);
227 }
228
229 int s2n_choose_kem_without_peer_pref_list(const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN], const struct s2n_kem *server_kem_pref_list[],
230 const uint8_t num_server_supported_kems, const struct s2n_kem **chosen_kem)
231 {
232 for (uint8_t i = 0; i < num_server_supported_kems; i++) {
233 uint8_t kem_is_compatible = 0;
234 POSIX_GUARD(s2n_kem_check_kem_compatibility(iana_value, server_kem_pref_list[i], &kem_is_compatible));
235 if (kem_is_compatible) {
236 *chosen_kem = server_kem_pref_list[i];
237 return S2N_SUCCESS;
238 }
239 }
240
241 /* The server preference list did not contain any KEM extensions compatible with the ciphersuite */
242 POSIX_BAIL(S2N_ERR_KEM_UNSUPPORTED_PARAMS);
243 }
244
245 int s2n_kem_free(struct s2n_kem_params *kem_params)
246 {
247 if (kem_params != NULL) {
248 POSIX_GUARD(s2n_free_or_wipe(&kem_params->private_key));
249 POSIX_GUARD(s2n_free_or_wipe(&kem_params->public_key));
250 POSIX_GUARD(s2n_free_or_wipe(&kem_params->shared_secret));
251 }
252 return S2N_SUCCESS;
253 }
254
255 int s2n_kem_group_free(struct s2n_kem_group_params *kem_group_params)
256 {
257 if (kem_group_params != NULL) {
258 POSIX_GUARD(s2n_kem_free(&kem_group_params->kem_params));
259 POSIX_GUARD(s2n_ecc_evp_params_free(&kem_group_params->ecc_params));
260 }
261 return S2N_SUCCESS;
262 }
263
264 int s2n_cipher_suite_to_kem(const uint8_t iana_value[S2N_TLS_CIPHER_SUITE_LEN], const struct s2n_iana_to_kem **compatible_params)
265 {
266 for (size_t i = 0; i < s2n_array_len(kem_mapping); i++) {
267 const struct s2n_iana_to_kem *candidate = &kem_mapping[i];
268 if (s2n_constant_time_equals(iana_value, candidate->iana_value, S2N_TLS_CIPHER_SUITE_LEN)) {
269 *compatible_params = candidate;
270 return S2N_SUCCESS;
271 }
272 }
273 POSIX_BAIL(S2N_ERR_KEM_UNSUPPORTED_PARAMS);
274 }
275
276 int s2n_get_kem_from_extension_id(kem_extension_size kem_id, const struct s2n_kem **kem)
277 {
278 for (size_t i = 0; i < s2n_array_len(kem_mapping); i++) {
279 const struct s2n_iana_to_kem *iana_to_kem = &kem_mapping[i];
280
281 for (int j = 0; j < iana_to_kem->kem_count; j++) {
282 const struct s2n_kem *candidate_kem = iana_to_kem->kems[j];
283 if (candidate_kem->kem_extension_id == kem_id) {
284 *kem = candidate_kem;
285 return S2N_SUCCESS;
286 }
287 }
288 }
289
290 POSIX_BAIL(S2N_ERR_KEM_UNSUPPORTED_PARAMS);
291 }
292
293 int s2n_kem_send_public_key(struct s2n_stuffer *out, struct s2n_kem_params *kem_params)
294 {
295 POSIX_ENSURE_REF(out);
296 POSIX_ENSURE_REF(kem_params);
297 POSIX_ENSURE_REF(kem_params->kem);
298
299 const struct s2n_kem *kem = kem_params->kem;
300
301 if (kem_params->len_prefixed) {
302 POSIX_GUARD(s2n_stuffer_write_uint16(out, kem->public_key_length));
303 }
304
305 /* We don't need to store the public key after sending it.
306 * We write it directly to *out. */
307 kem_params->public_key.data = s2n_stuffer_raw_write(out, kem->public_key_length);
308 POSIX_ENSURE_REF(kem_params->public_key.data);
309 kem_params->public_key.size = kem->public_key_length;
310
311 /* Saves the private key in kem_params */
312 s2n_result keypair_result = s2n_kem_generate_keypair(kem_params);
313
314 /* After using s2n_stuffer_raw_write() above to write the public
315 * key to the stuffer, we want to ensure that kem_params->public_key.data
316 * does not continue to point at *out, else we may unexpectedly
317 * overwrite part of the stuffer when s2n_kem_free() is called. */
318 kem_params->public_key.data = NULL;
319 kem_params->public_key.size = 0;
320
321 /* only error after we have cleaned up public_key to no longer reference *out */
322 POSIX_GUARD_RESULT(keypair_result);
323
324 return S2N_SUCCESS;
325 }
326
327 int s2n_kem_recv_public_key(struct s2n_stuffer *in, struct s2n_kem_params *kem_params)
328 {
329 POSIX_ENSURE_REF(in);
330 POSIX_ENSURE_REF(kem_params);
331 POSIX_ENSURE_REF(kem_params->kem);
332
333 const struct s2n_kem *kem = kem_params->kem;
334
335 if (kem_params->len_prefixed) {
336 kem_public_key_size public_key_length = 0;
337 POSIX_GUARD(s2n_stuffer_read_uint16(in, &public_key_length));
338 POSIX_ENSURE(public_key_length == kem->public_key_length, S2N_ERR_BAD_MESSAGE);
339 }
340
341 /* Alloc memory for the public key; the peer receiving it will need it
342 * later during the handshake to encapsulate the shared secret. */
343 POSIX_GUARD(s2n_alloc(&(kem_params->public_key), kem->public_key_length));
344 POSIX_GUARD(s2n_stuffer_read_bytes(in, kem_params->public_key.data, kem->public_key_length));
345
346 return S2N_SUCCESS;
347 }
348
349 int s2n_kem_send_ciphertext(struct s2n_stuffer *out, struct s2n_kem_params *kem_params)
350 {
351 POSIX_ENSURE_REF(out);
352 POSIX_ENSURE_REF(kem_params);
353 POSIX_ENSURE_REF(kem_params->kem);
354 POSIX_ENSURE_REF(kem_params->public_key.data);
355
356 const struct s2n_kem *kem = kem_params->kem;
357
358 if (kem_params->len_prefixed) {
359 POSIX_GUARD(s2n_stuffer_write_uint16(out, kem->ciphertext_length));
360 }
361
362 /* Ciphertext will get written to *out */
363 struct s2n_blob ciphertext = { 0 };
364 POSIX_GUARD(s2n_blob_init(&ciphertext, s2n_stuffer_raw_write(out, kem->ciphertext_length), kem->ciphertext_length));
365 POSIX_ENSURE_REF(ciphertext.data);
366
367 /* Saves the shared secret in kem_params */
368 POSIX_GUARD_RESULT(s2n_kem_encapsulate(kem_params, &ciphertext));
369
370 return S2N_SUCCESS;
371 }
372
373 int s2n_kem_recv_ciphertext(struct s2n_stuffer *in, struct s2n_kem_params *kem_params)
374 {
375 POSIX_ENSURE_REF(in);
376 POSIX_ENSURE_REF(kem_params);
377 POSIX_ENSURE_REF(kem_params->kem);
378 POSIX_ENSURE_REF(kem_params->private_key.data);
379
380 const struct s2n_kem *kem = kem_params->kem;
381
382 if (kem_params->len_prefixed) {
383 kem_ciphertext_key_size ciphertext_length = 0;
384 POSIX_GUARD(s2n_stuffer_read_uint16(in, &ciphertext_length));
385 POSIX_ENSURE(ciphertext_length == kem->ciphertext_length, S2N_ERR_BAD_MESSAGE);
386 }
387
388 const struct s2n_blob ciphertext = { .data = s2n_stuffer_raw_read(in, kem->ciphertext_length), .size = kem->ciphertext_length };
389 POSIX_ENSURE_REF(ciphertext.data);
390
391 /* Saves the shared secret in kem_params */
392 POSIX_GUARD_RESULT(s2n_kem_decapsulate(kem_params, &ciphertext));
393
394 return S2N_SUCCESS;
395 }
396
397 bool s2n_kem_is_available(const struct s2n_kem *kem)
398 {
399 if (kem == NULL || kem->kem_nid == NID_undef) {
400 return false;
401 }
402
403 return s2n_libcrypto_supports_evp_kem();
404 }
405
406 bool s2n_kem_group_is_available(const struct s2n_kem_group *kem_group)
407 {
408 /* Check for values that might be undefined when compiling for older libcrypto's */
409 if (kem_group == NULL || kem_group->curve == NULL || kem_group->kem == NULL) {
410 return false;
411 }
412
413 bool available = s2n_kem_is_available(kem_group->kem);
414
415 /* x25519 based tls13_kem_groups require EVP_APIS_SUPPORTED */
416 if (kem_group->curve == &s2n_ecc_curve_x25519) {
417 available &= s2n_is_evp_apis_supported();
418 }
419
420 return available;
421 }
422
423 int s2n_find_kem_group_from_iana_id(uint16_t iana_id, const struct s2n_kem_group **out, bool *found)
424 {
425 POSIX_ENSURE_REF(out);
426 POSIX_ENSURE_REF(found);
427 *found = false;
428
429 for (size_t i = 0; i < S2N_KEM_GROUPS_COUNT; i++) {
430 const struct s2n_kem_group *kem_group = ALL_SUPPORTED_KEM_GROUPS[i];
431 POSIX_ENSURE_REF(kem_group);
432 if (kem_group->iana_id == iana_id) {
433 *out = kem_group;
434 *found = true;
435 return S2N_SUCCESS;
436 }
437 }
438 return S2N_SUCCESS;
439 }