Debian Codegraph @

crt/s2n/tests/unit/s2n_kem_test.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 #include "tls/s2n_kem.h"
16
17 #include "crypto/s2n_ecc_evp.h"
18 #include "crypto/s2n_pq.h"
19 #include "tests/s2n_test.h"
20 #include "tls/extensions/s2n_key_share.h"
21 #include "utils/s2n_safety.h"
22
23 #define TEST_PUBLIC_KEY_LENGTH 2
24 const uint8_t TEST_PUBLIC_KEY[] = { 2, 2 };
25 #define TEST_PRIVATE_KEY_LENGTH 3
26 const uint8_t TEST_PRIVATE_KEY[] = { 3, 3, 3 };
27 #define TEST_SHARED_SECRET_LENGTH 4
28 const uint8_t TEST_SHARED_SECRET[] = { 4, 4, 4, 4 };
29 #define TEST_CIPHERTEXT_LENGTH 5
30 const uint8_t TEST_CIPHERTEXT[] = { 5, 5, 5, 5, 5 };
31
32 int alloc_test_kem_params(struct s2n_kem_params *kem_params)
33 {
34 POSIX_GUARD(s2n_alloc(&(kem_params->private_key), TEST_PRIVATE_KEY_LENGTH));
35 struct s2n_stuffer private_key_stuffer = { 0 };
36 POSIX_GUARD(s2n_stuffer_init(&private_key_stuffer, &(kem_params->private_key)));
37 POSIX_GUARD(s2n_stuffer_write_bytes(&private_key_stuffer, TEST_PRIVATE_KEY, TEST_PRIVATE_KEY_LENGTH));
38
39 POSIX_GUARD(s2n_alloc(&(kem_params->public_key), TEST_PUBLIC_KEY_LENGTH));
40 struct s2n_stuffer public_key_stuffer = { 0 };
41 POSIX_GUARD(s2n_stuffer_init(&public_key_stuffer, &(kem_params->public_key)));
42 POSIX_GUARD(s2n_stuffer_write_bytes(&public_key_stuffer, TEST_PUBLIC_KEY, TEST_PUBLIC_KEY_LENGTH));
43
44 POSIX_GUARD(s2n_alloc(&(kem_params->shared_secret), TEST_SHARED_SECRET_LENGTH));
45 struct s2n_stuffer shared_secret_stuffer = { 0 };
46 POSIX_GUARD(s2n_stuffer_init(&shared_secret_stuffer, &(kem_params->shared_secret)));
47 POSIX_GUARD(s2n_stuffer_write_bytes(&shared_secret_stuffer, TEST_SHARED_SECRET, TEST_SHARED_SECRET_LENGTH));
48
49 POSIX_ENSURE_NE(0, kem_params->private_key.allocated);
50 POSIX_ENSURE_NE(0, kem_params->public_key.allocated);
51 POSIX_ENSURE_NE(0, kem_params->shared_secret.allocated);
52
53 return S2N_SUCCESS;
54 }
55
56 int assert_kem_params_free(struct s2n_kem_params *kem_params)
57 {
58 POSIX_ENSURE_EQ(NULL, kem_params->private_key.data);
59 POSIX_ENSURE_EQ(0, kem_params->private_key.size);
60 POSIX_ENSURE_EQ(0, kem_params->private_key.allocated);
61
62 POSIX_ENSURE_EQ(NULL, kem_params->public_key.data);
63 POSIX_ENSURE_EQ(0, kem_params->public_key.size);
64 POSIX_ENSURE_EQ(0, kem_params->public_key.allocated);
65
66 POSIX_ENSURE_EQ(NULL, kem_params->shared_secret.data);
67 POSIX_ENSURE_EQ(0, kem_params->shared_secret.size);
68 POSIX_ENSURE_EQ(0, kem_params->shared_secret.allocated);
69
70 return S2N_SUCCESS;
71 }
72
73 int s2n_test_generate_keypair(const struct s2n_kem *kem, unsigned char *public_key, unsigned char *private_key)
74 {
75 memset(public_key, kem->public_key_length, kem->public_key_length);
76 memset(private_key, kem->private_key_length, kem->private_key_length);
77 return 0;
78 }
79
80 int s2n_test_encrypt(const struct s2n_kem *kem, unsigned char *ciphertext, unsigned char *shared_secret, const unsigned char *public_key)
81 {
82 POSIX_GUARD(memcmp(public_key, TEST_PUBLIC_KEY, TEST_PUBLIC_KEY_LENGTH));
83 memset(ciphertext, kem->ciphertext_length, kem->ciphertext_length);
84 memset(shared_secret, kem->shared_secret_key_length, kem->shared_secret_key_length);
85 return 0;
86 }
87
88 int s2n_test_decrypt(const struct s2n_kem *kem, unsigned char *shared_secret, const unsigned char *ciphertext, const unsigned char *private_key)
89 {
90 POSIX_GUARD(memcmp(ciphertext, TEST_CIPHERTEXT, TEST_CIPHERTEXT_LENGTH));
91 POSIX_GUARD(memcmp(private_key, TEST_PRIVATE_KEY, TEST_PRIVATE_KEY_LENGTH));
92 memset(shared_secret, kem->shared_secret_key_length, kem->shared_secret_key_length);
93 return 0;
94 }
95
96 const struct s2n_kem s2n_test_kem = {
97 .public_key_length = TEST_PUBLIC_KEY_LENGTH,
98 .private_key_length = TEST_PRIVATE_KEY_LENGTH,
99 .shared_secret_key_length = TEST_SHARED_SECRET_LENGTH,
100 .ciphertext_length = TEST_CIPHERTEXT_LENGTH,
101 .generate_keypair = &s2n_test_generate_keypair,
102 .encapsulate = &s2n_test_encrypt,
103 .decapsulate = &s2n_test_decrypt,
104 };
105
106 int main(int argc, char **argv)
107 {
108 BEGIN_TEST();
109 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
110
111 /* Run KEM tests that don't depend on the value of len_prefix */
112 {
113 /* Regression test for network parsing data of expected sizes */
114 EXPECT_EQUAL(sizeof(kem_extension_size), 2);
115 EXPECT_EQUAL(sizeof(kem_public_key_size), 2);
116 EXPECT_EQUAL(sizeof(kem_private_key_size), 2);
117 EXPECT_EQUAL(sizeof(kem_shared_secret_size), 2);
118 EXPECT_EQUAL(sizeof(kem_ciphertext_key_size), 2);
119 };
120 {
121 /* Tests for s2n_kem_free() */
122 EXPECT_SUCCESS(s2n_kem_free(NULL));
123
124 struct s2n_kem_params kem_params = { 0 };
125 EXPECT_SUCCESS(s2n_kem_free(&kem_params));
126
127 /* Fill kem_params with secrets and ensure that they have been freed */
128 EXPECT_SUCCESS(alloc_test_kem_params(&kem_params));
129 EXPECT_SUCCESS(s2n_kem_free(&kem_params));
130 EXPECT_SUCCESS(assert_kem_params_free(&kem_params));
131 };
132 {
133 /* Tests for s2n_kem_group_free() */
134 EXPECT_SUCCESS(s2n_kem_group_free(NULL));
135
136 struct s2n_kem_group_params kem_group_params = { 0 };
137 EXPECT_SUCCESS(s2n_kem_group_free(&kem_group_params));
138
139 /* Fill the kem_group_params with secrets */
140 EXPECT_SUCCESS(alloc_test_kem_params(&kem_group_params.kem_params));
141 struct s2n_stuffer wire = { 0 };
142 POSIX_GUARD(s2n_stuffer_growable_alloc(&wire, 1024));
143 kem_group_params.ecc_params.negotiated_curve = &s2n_ecc_curve_secp256r1;
144 POSIX_GUARD(s2n_ecdhe_parameters_send(&kem_group_params.ecc_params, &wire));
145 POSIX_GUARD(s2n_stuffer_free(&wire));
146 EXPECT_NOT_NULL(kem_group_params.ecc_params.evp_pkey);
147
148 /* Ensure that secrets have been freed */
149 EXPECT_SUCCESS(s2n_kem_group_free(&kem_group_params));
150 EXPECT_SUCCESS(assert_kem_params_free(&kem_group_params.kem_params));
151 EXPECT_NULL(kem_group_params.ecc_params.evp_pkey);
152 };
153 {
154 /* Failure cases for s2n_get_kem_from_extension_id() */
155 const struct s2n_kem *returned_kem = NULL;
156 kem_extension_size non_existent_kem_id = 65535;
157 EXPECT_FAILURE_WITH_ERRNO(s2n_get_kem_from_extension_id(non_existent_kem_id, &returned_kem), S2N_ERR_KEM_UNSUPPORTED_PARAMS);
158 };
159
160 /* If KEM tests depend on len_prefix, test with both possible values */
161 for (int len_prefixed = 0; len_prefixed < 2; len_prefixed++) {
162 {
163 struct s2n_kem_params server_kem_params = { 0 };
164 server_kem_params.kem = &s2n_test_kem;
165 server_kem_params.len_prefixed = len_prefixed;
166 EXPECT_SUCCESS(s2n_alloc(&server_kem_params.public_key, TEST_PUBLIC_KEY_LENGTH));
167 EXPECT_OK(s2n_kem_generate_keypair(&server_kem_params));
168 EXPECT_EQUAL(TEST_PUBLIC_KEY_LENGTH, server_kem_params.public_key.size);
169 EXPECT_EQUAL(TEST_PRIVATE_KEY_LENGTH, server_kem_params.private_key.size);
170 EXPECT_BYTEARRAY_EQUAL(TEST_PUBLIC_KEY, server_kem_params.public_key.data, TEST_PUBLIC_KEY_LENGTH);
171 EXPECT_BYTEARRAY_EQUAL(TEST_PRIVATE_KEY, server_kem_params.private_key.data, TEST_PRIVATE_KEY_LENGTH);
172 /* KeyGen shouldn't modify the shared secret */
173 EXPECT_EQUAL(0, server_kem_params.shared_secret.size);
174 EXPECT_EQUAL(0, server_kem_params.shared_secret.allocated);
175 EXPECT_NULL(server_kem_params.shared_secret.data);
176
177 struct s2n_kem_params client_kem_params = { 0 };
178 client_kem_params.kem = &s2n_test_kem;
179 client_kem_params.len_prefixed = len_prefixed;
180 /* This would be handled by client/server key exchange methods which isn't being tested */
181 POSIX_GUARD(s2n_alloc(&client_kem_params.public_key, TEST_PUBLIC_KEY_LENGTH));
182 memset(client_kem_params.public_key.data, TEST_PUBLIC_KEY_LENGTH, TEST_PUBLIC_KEY_LENGTH);
183
184 DEFER_CLEANUP(struct s2n_blob ciphertext = { 0 }, s2n_free);
185 POSIX_GUARD(s2n_alloc(&ciphertext, TEST_CIPHERTEXT_LENGTH));
186
187 EXPECT_OK(s2n_kem_encapsulate(&client_kem_params, &ciphertext));
188 EXPECT_EQUAL(TEST_SHARED_SECRET_LENGTH, client_kem_params.shared_secret.size);
189 EXPECT_EQUAL(TEST_CIPHERTEXT_LENGTH, ciphertext.size);
190 EXPECT_BYTEARRAY_EQUAL(TEST_SHARED_SECRET, client_kem_params.shared_secret.data, TEST_SHARED_SECRET_LENGTH);
191 EXPECT_BYTEARRAY_EQUAL(TEST_CIPHERTEXT, ciphertext.data, TEST_CIPHERTEXT_LENGTH);
192 /* Encaps shouldn't modify the public or private keys */
193 EXPECT_EQUAL(TEST_PUBLIC_KEY_LENGTH, client_kem_params.public_key.size);
194 EXPECT_BYTEARRAY_EQUAL(TEST_PUBLIC_KEY, client_kem_params.public_key.data, TEST_PUBLIC_KEY_LENGTH);
195 EXPECT_EQUAL(0, client_kem_params.private_key.size);
196 EXPECT_EQUAL(0, client_kem_params.private_key.allocated);
197 EXPECT_NULL(client_kem_params.private_key.data);
198
199 EXPECT_OK(s2n_kem_decapsulate(&server_kem_params, &ciphertext));
200 EXPECT_EQUAL(TEST_SHARED_SECRET_LENGTH, server_kem_params.shared_secret.size);
201 EXPECT_BYTEARRAY_EQUAL(TEST_SHARED_SECRET, server_kem_params.shared_secret.data, TEST_SHARED_SECRET_LENGTH);
202 /* Decaps shouldn't modify the public or private keys */
203 EXPECT_EQUAL(TEST_PUBLIC_KEY_LENGTH, server_kem_params.public_key.size);
204 EXPECT_BYTEARRAY_EQUAL(TEST_PUBLIC_KEY, server_kem_params.public_key.data, TEST_PUBLIC_KEY_LENGTH);
205 EXPECT_EQUAL(TEST_PRIVATE_KEY_LENGTH, server_kem_params.private_key.size);
206 EXPECT_BYTEARRAY_EQUAL(TEST_PRIVATE_KEY, server_kem_params.private_key.data, TEST_PRIVATE_KEY_LENGTH);
207
208 EXPECT_SUCCESS(s2n_kem_free(&server_kem_params));
209 EXPECT_SUCCESS(s2n_kem_free(&client_kem_params));
210 };
211 {
212 /* Happy case for s2n_kem_send_public_key() */
213 struct s2n_kem_params kem_params = { .kem = &s2n_test_kem, .len_prefixed = len_prefixed };
214
215 DEFER_CLEANUP(struct s2n_blob io_blob = { 0 }, s2n_free);
216 EXPECT_SUCCESS(s2n_alloc(&io_blob, TEST_PUBLIC_KEY_LENGTH + 2));
217 struct s2n_stuffer io_stuffer = { 0 };
218 EXPECT_SUCCESS(s2n_stuffer_init(&io_stuffer, &io_blob));
219
220 EXPECT_SUCCESS(s2n_kem_send_public_key(&io_stuffer, &kem_params));
221
222 /* {0, 2} = length of public key to follow
223 * {2, 2} = test public key */
224 uint8_t prefixed_output[] = { 0, 2, 2, 2 };
225 uint8_t unprefixed_output[] = { 2, 2 };
226
227 uint8_t *output = unprefixed_output;
228 uint16_t output_len = TEST_PUBLIC_KEY_LENGTH;
229
230 if (len_prefixed) {
231 output = prefixed_output;
232 output_len = TEST_PUBLIC_KEY_LENGTH + 2;
233 }
234
235 EXPECT_BYTEARRAY_EQUAL(io_stuffer.blob.data, output, output_len);
236
237 EXPECT_EQUAL(kem_params.private_key.size, TEST_PRIVATE_KEY_LENGTH);
238 EXPECT_BYTEARRAY_EQUAL(kem_params.private_key.data, TEST_PRIVATE_KEY, TEST_PRIVATE_KEY_LENGTH);
239 EXPECT_EQUAL(kem_params.public_key.size, 0);
240 EXPECT_NULL(kem_params.public_key.data);
241 EXPECT_EQUAL(kem_params.shared_secret.size, 0);
242 EXPECT_NULL(kem_params.shared_secret.data);
243
244 /* The private key gets alloc'ed in s2n_kem_generate_keypair().
245 * Nothing else should have been alloc'ed. */
246 EXPECT_EQUAL(0, kem_params.public_key.allocated);
247 EXPECT_EQUAL(0, kem_params.shared_secret.allocated);
248 EXPECT_NOT_EQUAL(0, kem_params.private_key.allocated);
249 EXPECT_SUCCESS(s2n_kem_free(&kem_params));
250 };
251 {
252 /* Failure cases for s2n_kem_send_public_key() */
253 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_send_public_key(NULL, NULL), S2N_ERR_NULL);
254
255 DEFER_CLEANUP(struct s2n_blob io_blob = { 0 }, s2n_free);
256 EXPECT_SUCCESS(s2n_alloc(&io_blob, 1));
257 struct s2n_stuffer io_stuffer = { 0 };
258 EXPECT_SUCCESS(s2n_stuffer_init(&io_stuffer, &io_blob));
259
260 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_send_public_key(&io_stuffer, NULL), S2N_ERR_NULL);
261
262 struct s2n_kem_params kem_params = { 0 };
263 kem_params.len_prefixed = len_prefixed;
264 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_send_public_key(&io_stuffer, &kem_params), S2N_ERR_NULL);
265 };
266 {
267 /* Happy case for s2n_kem_send_ciphertext() */
268 struct s2n_kem_params kem_params = { .kem = &s2n_test_kem, .len_prefixed = len_prefixed };
269
270 DEFER_CLEANUP(struct s2n_blob io_blob = { 0 }, s2n_free);
271 EXPECT_SUCCESS(s2n_alloc(&io_blob, TEST_CIPHERTEXT_LENGTH + 2));
272 struct s2n_stuffer io_stuffer = { 0 };
273 EXPECT_SUCCESS(s2n_stuffer_init(&io_stuffer, &io_blob));
274
275 EXPECT_SUCCESS(s2n_alloc(&(kem_params.public_key), TEST_PUBLIC_KEY_LENGTH));
276 POSIX_CHECKED_MEMCPY(kem_params.public_key.data, TEST_PUBLIC_KEY, TEST_PUBLIC_KEY_LENGTH);
277
278 EXPECT_SUCCESS(s2n_kem_send_ciphertext(&io_stuffer, &kem_params));
279
280 /* {0, 5} = length of ciphertext to follow
281 * {5, 5, 5, 5, 5} = test ciphertext */
282 uint8_t prefixed_output[] = { 0, 5, 5, 5, 5, 5, 5 };
283 uint8_t unprefixed_output[] = { 5, 5, 5, 5, 5 };
284
285 uint8_t *output = unprefixed_output;
286 uint16_t output_len = TEST_CIPHERTEXT_LENGTH;
287
288 if (len_prefixed) {
289 output = prefixed_output;
290 output_len = TEST_CIPHERTEXT_LENGTH + 2;
291 }
292
293 EXPECT_BYTEARRAY_EQUAL(io_stuffer.blob.data, output, output_len);
294
295 EXPECT_EQUAL(kem_params.shared_secret.size, TEST_SHARED_SECRET_LENGTH);
296 EXPECT_BYTEARRAY_EQUAL(kem_params.shared_secret.data, TEST_SHARED_SECRET, TEST_SHARED_SECRET_LENGTH);
297 EXPECT_EQUAL(kem_params.public_key.size, TEST_PUBLIC_KEY_LENGTH);
298 EXPECT_BYTEARRAY_EQUAL(kem_params.public_key.data, TEST_PUBLIC_KEY, TEST_PUBLIC_KEY_LENGTH);
299 EXPECT_EQUAL(kem_params.private_key.size, 0);
300 EXPECT_NULL(kem_params.private_key.data);
301
302 /* We alloc'ed the public key previously in the test; the shared secret was
303 * alloc'ed in Encaps; the private key should not have been alloc'ed */
304 EXPECT_EQUAL(0, kem_params.private_key.allocated);
305 EXPECT_NOT_EQUAL(0, kem_params.public_key.allocated);
306 EXPECT_NOT_EQUAL(0, kem_params.public_key.allocated);
307 EXPECT_SUCCESS(s2n_kem_free(&kem_params));
308 };
309 {
310 /* Failure cases for s2n_kem_send_ciphertext() */
311 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_send_ciphertext(NULL, NULL), S2N_ERR_NULL);
312
313 DEFER_CLEANUP(struct s2n_blob io_blob = { 0 }, s2n_free);
314 EXPECT_SUCCESS(s2n_alloc(&io_blob, 1));
315 struct s2n_stuffer io_stuffer = { 0 };
316 EXPECT_SUCCESS(s2n_stuffer_init(&io_stuffer, &io_blob));
317
318 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_send_ciphertext(&io_stuffer, NULL), S2N_ERR_NULL);
319
320 struct s2n_kem_params kem_params = { 0 };
321 kem_params.len_prefixed = len_prefixed;
322 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_send_ciphertext(&io_stuffer, &kem_params), S2N_ERR_NULL);
323
324 kem_params.kem = &s2n_test_kem;
325 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_send_ciphertext(&io_stuffer, &kem_params), S2N_ERR_NULL);
326 };
327 {
328 /* Happy case for s2n_kem_recv_ciphertext() */
329 struct s2n_kem_params kem_params = { .kem = &s2n_test_kem, .len_prefixed = len_prefixed };
330
331 DEFER_CLEANUP(struct s2n_blob io_blob = { 0 }, s2n_free);
332 EXPECT_SUCCESS(s2n_alloc(&io_blob, TEST_CIPHERTEXT_LENGTH + 2));
333 struct s2n_stuffer io_stuffer = { 0 };
334 EXPECT_SUCCESS(s2n_stuffer_init(&io_stuffer, &io_blob));
335
336 EXPECT_SUCCESS(s2n_alloc(&(kem_params.private_key), TEST_PRIVATE_KEY_LENGTH));
337 POSIX_CHECKED_MEMCPY(kem_params.private_key.data, TEST_PRIVATE_KEY, TEST_PRIVATE_KEY_LENGTH);
338
339 /* {0, 5} = length of ciphertext to follow
340 * {5, 5, 5, 5, 5} = test ciphertext */
341 uint8_t prefixed_input[] = { 0, 5, 5, 5, 5, 5, 5 };
342 uint8_t unprefixed_input[] = { 5, 5, 5, 5, 5 };
343
344 uint8_t *input = unprefixed_input;
345 uint16_t input_len = TEST_CIPHERTEXT_LENGTH;
346 if (len_prefixed) {
347 input = prefixed_input;
348 input_len = TEST_CIPHERTEXT_LENGTH + 2;
349 }
350
351 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&io_stuffer, input, input_len));
352 EXPECT_SUCCESS(s2n_stuffer_reread(&io_stuffer));
353
354 EXPECT_SUCCESS(s2n_kem_recv_ciphertext(&io_stuffer, &kem_params));
355
356 EXPECT_EQUAL(kem_params.shared_secret.size, TEST_SHARED_SECRET_LENGTH);
357 EXPECT_BYTEARRAY_EQUAL(kem_params.shared_secret.data, TEST_SHARED_SECRET, TEST_SHARED_SECRET_LENGTH);
358 EXPECT_EQUAL(0, kem_params.public_key.size);
359 EXPECT_NULL(kem_params.public_key.data);
360
361 /* We alloc'ed the private key previously in the test; the shared secret was
362 * alloc'ed in Decaps; the public key should not have been alloc'ed */
363 EXPECT_EQUAL(0, kem_params.public_key.allocated);
364 EXPECT_NOT_EQUAL(0, kem_params.private_key.allocated);
365 EXPECT_NOT_EQUAL(0, kem_params.shared_secret.allocated);
366 EXPECT_SUCCESS(s2n_kem_free(&kem_params));
367 };
368 {
369 /* Failure cases for s2n_kem_recv_ciphertext() */
370 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_recv_ciphertext(NULL, NULL), S2N_ERR_NULL);
371
372 DEFER_CLEANUP(struct s2n_blob io_blob = { 0 }, s2n_free);
373 EXPECT_SUCCESS(s2n_alloc(&io_blob, 1));
374 struct s2n_stuffer io_stuffer = { 0 };
375 EXPECT_SUCCESS(s2n_stuffer_init(&io_stuffer, &io_blob));
376
377 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_recv_ciphertext(&io_stuffer, NULL), S2N_ERR_NULL);
378
379 struct s2n_kem_params kem_params = { 0 };
380 kem_params.len_prefixed = len_prefixed;
381 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_recv_ciphertext(&io_stuffer, &kem_params), S2N_ERR_NULL);
382
383 kem_params.kem = &s2n_test_kem;
384 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_recv_ciphertext(&io_stuffer, &kem_params), S2N_ERR_NULL);
385
386 /* The given ciphertext length doesn't match the KEM's actual ciphertext length */
387 EXPECT_SUCCESS(s2n_alloc(&(kem_params.private_key), TEST_PRIVATE_KEY_LENGTH));
388 POSIX_CHECKED_MEMCPY(kem_params.private_key.data, TEST_PRIVATE_KEY, TEST_PRIVATE_KEY_LENGTH);
389 DEFER_CLEANUP(struct s2n_blob io_blob_3 = { 0 }, s2n_free);
390 EXPECT_SUCCESS(s2n_alloc(&io_blob_3, TEST_CIPHERTEXT_LENGTH + 2));
391 struct s2n_stuffer io_stuffer_3 = { 0 };
392 EXPECT_SUCCESS(s2n_stuffer_init(&io_stuffer_3, &io_blob_3));
393 uint8_t bad_ct_input_3[] = { 0, 2, 2, 2 };
394 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&io_stuffer_3, bad_ct_input_3, 4));
395 EXPECT_SUCCESS(s2n_stuffer_reread(&io_stuffer_3));
396
397 if (len_prefixed) {
398 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_recv_ciphertext(&io_stuffer_3, &kem_params), S2N_ERR_BAD_MESSAGE);
399 }
400
401 /* We alloc'ed the private key previously in the test; our failure cases for
402 * s2n_kem_recv_ciphertext() never reached a point where we alloc'ed anything else */
403 EXPECT_NOT_EQUAL(0, kem_params.private_key.allocated);
404 EXPECT_EQUAL(0, kem_params.public_key.allocated);
405 EXPECT_EQUAL(0, kem_params.shared_secret.allocated);
406 EXPECT_SUCCESS(s2n_kem_free(&kem_params));
407 };
408 {
409 /* Happy case for s2n_kem_recv_public_key() */
410 struct s2n_kem_params kem_params = { .kem = &s2n_test_kem, .len_prefixed = len_prefixed };
411
412 DEFER_CLEANUP(struct s2n_blob io_blob = { 0 }, s2n_free);
413 EXPECT_SUCCESS(s2n_alloc(&io_blob, TEST_PUBLIC_KEY_LENGTH + 2));
414 struct s2n_stuffer io_stuffer = { 0 };
415 EXPECT_SUCCESS(s2n_stuffer_init(&io_stuffer, &io_blob));
416
417 /* {0, 2} = length of public key to follow
418 * {2, 2} = test public key */
419 uint8_t prefixed_input[] = { 0, 2, 2, 2 };
420 uint8_t unprefixed_input[] = { 2, 2 };
421
422 uint8_t *input = unprefixed_input;
423 uint16_t input_len = TEST_PUBLIC_KEY_LENGTH;
424
425 if (len_prefixed) {
426 input = prefixed_input;
427 input_len = TEST_PUBLIC_KEY_LENGTH + 2;
428 }
429
430 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&io_stuffer, input, input_len));
431 EXPECT_SUCCESS(s2n_stuffer_reread(&io_stuffer));
432
433 EXPECT_SUCCESS(s2n_kem_recv_public_key(&io_stuffer, &kem_params));
434
435 /* s2n_kem_recv_public_key() should alloc kem_params->public_key and nothing else */
436 EXPECT_EQUAL(kem_params.public_key.size, TEST_PUBLIC_KEY_LENGTH);
437 EXPECT_NOT_EQUAL(0, kem_params.public_key.allocated);
438 EXPECT_BYTEARRAY_EQUAL(kem_params.public_key.data, TEST_PUBLIC_KEY, TEST_PUBLIC_KEY_LENGTH);
439 EXPECT_EQUAL(0, kem_params.shared_secret.allocated);
440 EXPECT_EQUAL(0, kem_params.private_key.allocated);
441 EXPECT_SUCCESS(s2n_kem_free(&kem_params));
442 };
443 {
444 /* Failure cases for s2n_kem_recv_public_key() */
445 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_recv_public_key(NULL, NULL), S2N_ERR_NULL);
446
447 DEFER_CLEANUP(struct s2n_blob io_blob = { 0 }, s2n_free);
448 EXPECT_SUCCESS(s2n_alloc(&io_blob, 1));
449 struct s2n_stuffer io_stuffer = { 0 };
450 EXPECT_SUCCESS(s2n_stuffer_init(&io_stuffer, &io_blob));
451
452 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_recv_public_key(&io_stuffer, NULL), S2N_ERR_NULL);
453
454 struct s2n_kem_params kem_params = { 0 };
455 kem_params.len_prefixed = len_prefixed;
456 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_recv_public_key(&io_stuffer, &kem_params), S2N_ERR_NULL);
457
458 kem_params.kem = &s2n_test_kem;
459
460 /* The given public key length doesn't match the KEM's actual public key length */
461 DEFER_CLEANUP(struct s2n_blob io_blob_3 = { 0 }, s2n_free);
462 EXPECT_SUCCESS(s2n_alloc(&io_blob_3, 5));
463 struct s2n_stuffer io_stuffer_3 = { 0 };
464 EXPECT_SUCCESS(s2n_stuffer_init(&io_stuffer_3, &io_blob_3));
465 uint8_t bad_pk_input_3[] = { 0, 3, 3, 3, 3 };
466 EXPECT_SUCCESS(s2n_stuffer_write_bytes(&io_stuffer_3, bad_pk_input_3, 5));
467 EXPECT_SUCCESS(s2n_stuffer_reread(&io_stuffer_3));
468 if (len_prefixed) {
469 EXPECT_FAILURE_WITH_ERRNO(s2n_kem_recv_public_key(&io_stuffer_3, &kem_params), S2N_ERR_BAD_MESSAGE);
470 }
471 };
472 }
473
474 END_TEST();
475 }