Debian Codegraph @

crt/s2n/tests/unit/s2n_pq_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
16 #include <openssl/crypto.h>
17
18 #include "crypto/s2n_fips.h"
19 #include "crypto/s2n_openssl.h"
20 #include "crypto/s2n_pq.h"
21 #include "s2n_test.h"
22 #include "tests/testlib/s2n_testlib.h"
23 #include "tls/s2n_kem.h"
24 #include "utils/s2n_safety.h"
25
26 static const struct s2n_kem *test_vectors[] = {
27 &s2n_mlkem_768,
28 &s2n_mlkem_1024,
29 };
30
31 /* EXPECT_SUCCESS checks explicitly function_call != -1; the PQ KEM functions may return
32 * any non-zero int to indicate failure.*/
33 #define EXPECT_PQ_KEM_SUCCESS(function_call) EXPECT_EQUAL((function_call), 0)
34 #define EXPECT_PQ_KEM_FAILURE(function_call) EXPECT_NOT_EQUAL((function_call), 0)
35
36 int main()
37 {
38 BEGIN_TEST();
39
40 #if defined(OPENSSL_IS_AWSLC) && defined(AWSLC_API_VERSION)
41 /* If using non-FIPS AWS-LC >= v1.6 (API vers. 21), expect EVP_KEM from AWS-LC */
42 if (!s2n_is_in_fips_mode() && AWSLC_API_VERSION >= 21) {
43 EXPECT_TRUE(s2n_libcrypto_supports_evp_kem());
44 }
45 #endif
46
47 for (size_t i = 0; i < s2n_array_len(test_vectors); i++) {
48 const struct s2n_kem *kem = test_vectors[i];
49
50 DEFER_CLEANUP(struct s2n_blob public_key = { 0 }, s2n_free);
51 EXPECT_SUCCESS(s2n_alloc(&public_key, kem->public_key_length));
52
53 DEFER_CLEANUP(struct s2n_blob private_key = { 0 }, s2n_free);
54 EXPECT_SUCCESS(s2n_alloc(&private_key, kem->private_key_length));
55
56 DEFER_CLEANUP(struct s2n_blob client_shared_secret = { 0 }, s2n_free);
57 EXPECT_SUCCESS(s2n_alloc(&client_shared_secret, kem->shared_secret_key_length));
58
59 DEFER_CLEANUP(struct s2n_blob server_shared_secret = { 0 }, s2n_free);
60 EXPECT_SUCCESS(s2n_alloc(&server_shared_secret, kem->shared_secret_key_length));
61
62 DEFER_CLEANUP(struct s2n_blob ciphertext = { 0 }, s2n_free);
63 EXPECT_SUCCESS(s2n_alloc(&ciphertext, kem->ciphertext_length));
64
65 if (!s2n_kem_is_available(kem)) {
66 EXPECT_FAILURE_WITH_ERRNO(kem->generate_keypair(kem, public_key.data, private_key.data), S2N_ERR_UNIMPLEMENTED);
67 EXPECT_FAILURE_WITH_ERRNO(kem->encapsulate(kem, ciphertext.data, client_shared_secret.data, public_key.data), S2N_ERR_UNIMPLEMENTED);
68 EXPECT_FAILURE_WITH_ERRNO(kem->decapsulate(kem, server_shared_secret.data, ciphertext.data, private_key.data), S2N_ERR_UNIMPLEMENTED);
69 continue;
70 }
71
72 /* Test a successful round-trip: keygen->enc->dec */
73 EXPECT_PQ_KEM_SUCCESS(kem->generate_keypair(kem, public_key.data, private_key.data));
74 EXPECT_PQ_KEM_SUCCESS(kem->encapsulate(kem, ciphertext.data, client_shared_secret.data, public_key.data));
75 EXPECT_PQ_KEM_SUCCESS(kem->decapsulate(kem, server_shared_secret.data, ciphertext.data, private_key.data));
76 EXPECT_BYTEARRAY_EQUAL(server_shared_secret.data, client_shared_secret.data, kem->shared_secret_key_length);
77
78 /* By design, if an invalid private key + ciphertext pair is provided to decapsulate(),
79 * the function should still succeed (return S2N_SUCCESS); however, the shared secret
80 * that was "decapsulated" will be a garbage random value. */
81 ciphertext.data[0] ^= 1; /* Flip a bit to invalidate the ciphertext */
82
83 EXPECT_PQ_KEM_SUCCESS(kem->decapsulate(kem, server_shared_secret.data, ciphertext.data, private_key.data));
84 EXPECT_BYTEARRAY_NOT_EQUAL(server_shared_secret.data, client_shared_secret.data, kem->shared_secret_key_length);
85 }
86
87 END_TEST();
88 }