Debian Codegraph @

crt/s2n/tests/unit/s2n_tls13_prf_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 <stdio.h>
17 #include <string.h>
18
19 #include "api/s2n.h"
20 #include "crypto/s2n_hkdf.h"
21 #include "s2n_test.h"
22 #include "stuffer/s2n_stuffer.h"
23 #include "testlib/s2n_testlib.h"
24 #include "utils/s2n_safety.h"
25
26 /*
27 * Test vectors from https://datatracker.ietf.org/doc/draft-ietf-tls-tls13-vectors/?include_text=1
28 */
29 int main(int argc, char **argv)
30 {
31 char client_handshake_message_hex_in[] = "010000c003032724c0ba613abd59"
32 "4894f19ff6d59cde8364549555119b96ec1158fb9ac"
33 "ba397000006130113031302010000910000000b0009"
34 "000006736572766572ff01000100000a00140012001"
35 "d001700180019010001010102010301040023000000"
36 "3300260024001d00207e74fe9d31f2bb96f4f553465"
37 "b92ea8210971e71e258d6cf622c3b086db26104002b"
38 "0003027f1c000d0020001e040305030603020308040"
39 "805080604010501060102010402050206020202002d"
40 "00020101001c00024001";
41
42 char server_handshake_message_hex_in[] = "020000560303b9206e3d30c43c8a"
43 "cb1c234f9d004c6a2fecb84c6811ca285c1bbce322"
44 "bed16000130100002e00330024001d0020bba1ffe6"
45 "d10f92d4a8444aa51913fea27c3d2bfdf24489da40"
46 "92dfbfbfd67c53002b00027f1c";
47
48 char expected_secret_hex_in[] = "33ad0a1c607ec03b09e6cd9893680ce210adf300aa1f2660e1b22e10f170f92a";
49 char expected_expanded_hex_in[] = "6f2615a108c702c5678f54fc9dbab69716c076189c48250cebeac3576c3611ba";
50
51 DEFER_CLEANUP(struct s2n_stuffer client_handshake_message_in = { 0 }, s2n_stuffer_free);
52 DEFER_CLEANUP(struct s2n_stuffer server_handshake_message_in = { 0 }, s2n_stuffer_free);
53 DEFER_CLEANUP(struct s2n_stuffer expected_secret_in = { 0 }, s2n_stuffer_free);
54 DEFER_CLEANUP(struct s2n_stuffer expected_expanded_in = { 0 }, s2n_stuffer_free);
55
56 uint8_t client_handshake_message_bytes[sizeof(client_handshake_message_hex_in) / 2] = { 0 };
57 uint8_t server_handshake_message_bytes[sizeof(server_handshake_message_hex_in) / 2] = { 0 };
58 uint8_t expected_secret_bytes[sizeof(expected_secret_hex_in) / 2] = { 0 };
59 uint8_t expected_expanded_bytes[sizeof(expected_expanded_hex_in) / 2] = { 0 };
60
61 uint8_t digest_buf[SHA256_DIGEST_LENGTH];
62 uint8_t secret_buf[SHA256_DIGEST_LENGTH];
63 struct s2n_blob digest = { 0 };
64 struct s2n_blob secret = { 0 };
65
66 struct s2n_hash_state transcript_hash, transcript_hash_snapshot;
67
68 BEGIN_TEST();
69 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
70
71 EXPECT_SUCCESS(s2n_stuffer_alloc_ro_from_string(&client_handshake_message_in, client_handshake_message_hex_in));
72 EXPECT_SUCCESS(s2n_stuffer_alloc_ro_from_string(&server_handshake_message_in, server_handshake_message_hex_in));
73 EXPECT_SUCCESS(s2n_stuffer_alloc_ro_from_string(&expected_secret_in, expected_secret_hex_in));
74 EXPECT_SUCCESS(s2n_stuffer_alloc_ro_from_string(&expected_expanded_in, expected_expanded_hex_in));
75
76 /* Parse the hex */
77 for (size_t i = 0; i < sizeof(client_handshake_message_bytes); i++) {
78 uint8_t c = 0;
79 EXPECT_OK(s2n_stuffer_read_uint8_hex(&client_handshake_message_in, &c));
80 client_handshake_message_bytes[i] = c;
81 }
82
83 for (size_t i = 0; i < sizeof(server_handshake_message_bytes); i++) {
84 uint8_t c = 0;
85 EXPECT_OK(s2n_stuffer_read_uint8_hex(&server_handshake_message_in, &c));
86 server_handshake_message_bytes[i] = c;
87 }
88
89 for (size_t i = 0; i < sizeof(expected_secret_bytes); i++) {
90 uint8_t c = 0;
91 EXPECT_OK(s2n_stuffer_read_uint8_hex(&expected_secret_in, &c));
92 expected_secret_bytes[i] = c;
93 }
94
95 for (size_t i = 0; i < sizeof(expected_expanded_bytes); i++) {
96 uint8_t c = 0;
97 EXPECT_OK(s2n_stuffer_read_uint8_hex(&expected_expanded_in, &c));
98 expected_expanded_bytes[i] = c;
99 }
100
101 EXPECT_SUCCESS(s2n_hash_new(&transcript_hash));
102 EXPECT_SUCCESS(s2n_hash_new(&transcript_hash_snapshot));
103 EXPECT_SUCCESS(s2n_hash_init(&transcript_hash, S2N_HASH_SHA256));
104 EXPECT_SUCCESS(s2n_hash_copy(&transcript_hash_snapshot, &transcript_hash));
105 EXPECT_SUCCESS(s2n_hash_digest(&transcript_hash_snapshot, digest_buf, SHA256_DIGEST_LENGTH));
106
107 uint8_t salt_buf[32] = { 0 };
108 struct s2n_blob salt = { 0 };
109
110 uint8_t ikm_buf[32] = { 0 };
111 struct s2n_blob ikm = { 0 };
112
113 uint8_t output_buf[SHA256_DIGEST_LENGTH] = { 0 };
114 struct s2n_blob output = { 0 };
115
116 EXPECT_SUCCESS(s2n_blob_init(&salt, salt_buf, sizeof(salt_buf)));
117 EXPECT_SUCCESS(s2n_blob_init(&ikm, ikm_buf, sizeof(ikm_buf)));
118 EXPECT_SUCCESS(s2n_blob_init(&digest, digest_buf, sizeof(digest_buf)));
119 EXPECT_SUCCESS(s2n_blob_init(&secret, secret_buf, sizeof(secret_buf)));
120 EXPECT_SUCCESS(s2n_blob_init(&output, output_buf, sizeof(output_buf)));
121
122 struct s2n_hmac_state throwaway;
123 EXPECT_SUCCESS(s2n_hmac_new(&throwaway));
124
125 /* Validate the early secret */
126 EXPECT_SUCCESS(s2n_hkdf_extract(&throwaway, S2N_HMAC_SHA256, &salt, &ikm, &secret));
127 EXPECT_EQUAL(memcmp(secret_buf, expected_secret_bytes, sizeof(secret_buf)), 0);
128
129 /* Validate the derived secret */
130 S2N_BLOB_LABEL(label, "derived");
131
132 struct s2n_hmac_state hmac = { 0 };
133
134 EXPECT_SUCCESS(s2n_hmac_new(&hmac));
135 EXPECT_SUCCESS(s2n_hkdf_expand_label(&hmac, S2N_HMAC_SHA256, &secret, &label, &digest, &output));
136
137 EXPECT_EQUAL(memcmp(output_buf, expected_expanded_bytes, sizeof(output_buf)), 0);
138
139 EXPECT_SUCCESS(s2n_hmac_free(&throwaway));
140 EXPECT_SUCCESS(s2n_hmac_free(&hmac));
141 EXPECT_SUCCESS(s2n_hash_free(&transcript_hash));
142 EXPECT_SUCCESS(s2n_hash_free(&transcript_hash_snapshot));
143
144 END_TEST();
145 }