Debian Codegraph @

crt/s2n/tests/testlib/s2n_kem_fuzz_testlib.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 "crypto/s2n_pq.h"
17 #include "s2n_testlib.h"
18 #include "tests/testlib/s2n_nist_kats.h"
19 #include "tls/s2n_kem.h"
20 #include "utils/s2n_safety.h"
21
22 int s2n_kem_recv_ciphertext_fuzz_test_init(const char *kat_file_path, struct s2n_kem_params *kem_params)
23 {
24 POSIX_ENSURE_REF(kat_file_path);
25 POSIX_ENSURE_REF(kem_params);
26 POSIX_ENSURE_REF(kem_params->kem);
27
28 POSIX_GUARD(s2n_alloc(&kem_params->private_key, kem_params->kem->private_key_length));
29 FILE *kat_file = fopen(kat_file_path, "r");
30 POSIX_ENSURE_REF(kat_file);
31 POSIX_GUARD(ReadHex(kat_file, kem_params->private_key.data, kem_params->kem->private_key_length, "sk = "));
32 fclose(kat_file);
33
34 return S2N_SUCCESS;
35 }
36
37 int s2n_kem_recv_ciphertext_fuzz_test(const uint8_t *buf, size_t len, struct s2n_kem_params *kem_params)
38 {
39 POSIX_ENSURE_REF(buf);
40 POSIX_ENSURE_REF(kem_params);
41 POSIX_ENSURE_REF(kem_params->kem);
42
43 DEFER_CLEANUP(struct s2n_stuffer ciphertext = { 0 }, s2n_stuffer_free);
44 POSIX_GUARD(s2n_stuffer_alloc(&ciphertext, len));
45 POSIX_GUARD(s2n_stuffer_write_bytes(&ciphertext, buf, len));
46
47 /* Don't GUARD the call to recv_ciphertext().
48 * recv_ciphertext() parses the would-be ciphertext bytes from the
49 * handshake, then passes them to the KEM's decaps function.
50 * recv_ciphertext() may fail appropriately during parsing if the
51 * ciphertext bytes do not correspond to TLS specification (e.g.
52 * improperly length-encoded).
53 *
54 * All but one of the KEM's decaps functions are written in such
55 * a way that they should never fail, regardless of the input provided
56 * by the fuzzer. If the fuzzer-provided "ciphertext" is not a
57 * valid PQ ciphertext (and it probably won't be), the decaps function
58 * should still succeed and return 0, but the output plaintext will
59 * be garbage. Therefore, if recv_ciphertext() fails for these KEMs,
60 * it should not have been due to S2N_ERR_PQ_CRYPTO.
61 *
62 * If PQ is not enabled, then recv_ciphertext() should always fail. */
63 int recv_ciphertext_ret = s2n_kem_recv_ciphertext(&ciphertext, kem_params);
64
65 if (s2n_pq_is_enabled() && recv_ciphertext_ret != S2N_SUCCESS) {
66 POSIX_ENSURE_NE(s2n_errno, S2N_ERR_PQ_CRYPTO);
67 }
68
69 if (!s2n_pq_is_enabled()) {
70 POSIX_ENSURE_NE(recv_ciphertext_ret, S2N_SUCCESS);
71 }
72
73 /* Shared secret may have been alloc'ed in recv_ciphertext */
74 POSIX_GUARD(s2n_free(&kem_params->shared_secret));
75
76 return S2N_SUCCESS;
77 }
78
79 int s2n_kem_recv_public_key_fuzz_test(const uint8_t *buf, size_t len, struct s2n_kem_params *kem_params)
80 {
81 POSIX_ENSURE_REF(buf);
82 POSIX_ENSURE_REF(kem_params);
83 POSIX_ENSURE_REF(kem_params->kem);
84
85 DEFER_CLEANUP(struct s2n_stuffer public_key = { 0 }, s2n_stuffer_free);
86 POSIX_GUARD(s2n_stuffer_alloc(&public_key, len));
87 POSIX_GUARD(s2n_stuffer_write_bytes(&public_key, buf, len));
88
89 /* s2n_kem_recv_public_key performs only very basic checks, like ensuring
90 * that the public key size is correct. If the received public key passes,
91 * we continue by calling s2n_kem_send_ciphertext to attempt to use the key
92 * for encryption. */
93 if (s2n_kem_recv_public_key(&public_key, kem_params) == S2N_SUCCESS) {
94 DEFER_CLEANUP(struct s2n_stuffer out = { 0 }, s2n_stuffer_free);
95 POSIX_GUARD(s2n_stuffer_growable_alloc(&out, 8192));
96 int send_ct_ret = s2n_kem_send_ciphertext(&out, kem_params);
97
98 /* The KEM encaps functions are written in such a way that
99 * s2n_kem_send_ciphertext() should always succeed as long
100 * as PQ is enabled, even if the previously received public
101 * key is not valid. If PQ is not enabled, send_ciphertext()
102 * should always fail because of a PQ crypto errno. */
103 if (s2n_pq_is_enabled()) {
104 POSIX_ENSURE_EQ(send_ct_ret, S2N_SUCCESS);
105 } else {
106 POSIX_ENSURE_NE(send_ct_ret, S2N_SUCCESS);
107 POSIX_ENSURE_EQ(s2n_errno, S2N_ERR_PQ_CRYPTO);
108 }
109 }
110
111 /* Clean up */
112 POSIX_GUARD(s2n_kem_free(kem_params));
113
114 return S2N_SUCCESS;
115 }