Debian Codegraph @

crt/s2n/tests/unit/s2n_handshake_hashes_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 "tls/s2n_handshake_hashes.h"
17
18 #include "s2n_test.h"
19 #include "tls/s2n_connection.h"
20
21 /* Needed for s2n_handshake_get_hash_state_ptr */
22 #include "tls/s2n_handshake.c"
23
24 int main(int argc, char **argv)
25 {
26 BEGIN_TEST();
27
28 /* Not all hash algorithms are used in the handshake hashes */
29 const bool skip_handshake_hash[S2N_HASH_ALGS_COUNT] = {
30 [S2N_HASH_NONE] = true,
31 [S2N_HASH_SHAKE256_64] = true,
32 };
33
34 /* Test s2n_handshake_hashes_new */
35 {
36 /* Safety */
37 EXPECT_ERROR_WITH_ERRNO(s2n_handshake_hashes_new(NULL), S2N_ERR_NULL);
38
39 /* Allocates a new s2n_handshake_hashes struct */
40 {
41 /* Allocates the struct */
42 struct s2n_connection conn = { 0 };
43 EXPECT_NULL(conn.handshake.hashes);
44 EXPECT_OK(s2n_handshake_hashes_new(&conn.handshake.hashes));
45 EXPECT_NOT_NULL(conn.handshake.hashes);
46
47 uint8_t data[100] = { 0 };
48
49 /* Allocates all hashes */
50 for (s2n_hash_algorithm alg = 0; alg < S2N_HASH_ALGS_COUNT; alg++) {
51 if (skip_handshake_hash[alg]) {
52 continue;
53 }
54
55 uint8_t size = 0;
56 EXPECT_SUCCESS(s2n_hash_digest_size(alg, &size));
57 EXPECT_TRUE(size < sizeof(data));
58
59 struct s2n_hash_state *hash_state = NULL;
60 EXPECT_SUCCESS(s2n_handshake_get_hash_state_ptr(&conn, alg, &hash_state));
61
62 /* Hash is setup / useable */
63 EXPECT_SUCCESS(s2n_hash_digest(hash_state, data, size));
64 }
65
66 s2n_handshake_hashes_free(&conn.handshake.hashes);
67 };
68 };
69
70 /* Test s2n_handshake_hashes_wipe */
71 {
72 /* Safety */
73 EXPECT_ERROR_WITH_ERRNO(s2n_handshake_hashes_wipe(NULL), S2N_ERR_NULL);
74
75 /* Resets all hashes */
76 {
77 struct s2n_connection conn = { 0 };
78 EXPECT_OK(s2n_handshake_hashes_new(&conn.handshake.hashes));
79 EXPECT_NOT_NULL(conn.handshake.hashes);
80
81 uint8_t data[100] = { 0 };
82
83 for (s2n_hash_algorithm alg = 0; alg < S2N_HASH_ALGS_COUNT; alg++) {
84 if (skip_handshake_hash[alg]) {
85 continue;
86 }
87
88 uint8_t size = 0;
89 EXPECT_SUCCESS(s2n_hash_digest_size(alg, &size));
90 EXPECT_TRUE(size < sizeof(data));
91
92 struct s2n_hash_state *hash_state = NULL;
93 EXPECT_SUCCESS(s2n_handshake_get_hash_state_ptr(&conn, alg, &hash_state));
94 EXPECT_SUCCESS(s2n_hash_digest(hash_state, data, size));
95
96 /* Can't calculate the digest again: only one digest allowed per hash */
97 EXPECT_FAILURE_WITH_ERRNO(s2n_hash_digest(hash_state, data, size), S2N_ERR_HASH_NOT_READY);
98
99 /* Wiping the hashes allows them to be successfully reused */
100 EXPECT_OK(s2n_handshake_hashes_wipe(conn.handshake.hashes));
101 EXPECT_SUCCESS(s2n_hash_digest(hash_state, data, size));
102 }
103
104 s2n_handshake_hashes_free(&conn.handshake.hashes);
105 };
106 };
107
108 /* Test s2n_handshake_hashes_free */
109 {
110 /* Safety */
111 EXPECT_ERROR_WITH_ERRNO(s2n_handshake_hashes_free(NULL), S2N_ERR_NULL);
112
113 /* Frees the hashes with no memory leaks */
114 {
115 struct s2n_handshake_hashes *hashes = NULL;
116 EXPECT_OK(s2n_handshake_hashes_new(&hashes));
117 EXPECT_NOT_NULL(hashes);
118
119 EXPECT_OK(s2n_handshake_hashes_free(&hashes));
120 EXPECT_NULL(hashes);
121 };
122 };
123
124 /* Test s2n_handshake_hashes connection lifecycle */
125 {
126 uint8_t digest[SHA256_DIGEST_LENGTH] = { 0 };
127
128 /* A new connection's hashes are properly set up and can be used. */
129 struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
130 EXPECT_NOT_NULL(conn);
131
132 struct s2n_hash_state *hash_state = NULL;
133 EXPECT_SUCCESS(s2n_handshake_get_hash_state_ptr(conn, S2N_HASH_SHA256, &hash_state));
134 EXPECT_NOT_NULL(hash_state);
135 EXPECT_SUCCESS(s2n_hash_digest(hash_state, digest, SHA256_DIGEST_LENGTH));
136 EXPECT_FAILURE_WITH_ERRNO(s2n_hash_digest(hash_state, digest, SHA256_DIGEST_LENGTH), S2N_ERR_HASH_NOT_READY);
137
138 /* A wiped connection's hashes can be reused. */
139 EXPECT_SUCCESS(s2n_connection_wipe(conn));
140 EXPECT_SUCCESS(s2n_handshake_get_hash_state_ptr(conn, S2N_HASH_SHA256, &hash_state));
141 EXPECT_NOT_NULL(hash_state);
142 EXPECT_SUCCESS(s2n_hash_digest(hash_state, digest, SHA256_DIGEST_LENGTH));
143
144 /* Freeing the handshake frees the hashes */
145 EXPECT_SUCCESS(s2n_connection_free_handshake(conn));
146 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_get_hash_state_ptr(conn, S2N_HASH_SHA256, &hash_state), S2N_ERR_NULL);
147 EXPECT_NULL(conn->handshake.hashes);
148
149 /* Wiping the connection should restore the freed hashes */
150 EXPECT_SUCCESS(s2n_connection_wipe(conn));
151 EXPECT_SUCCESS(s2n_handshake_get_hash_state_ptr(conn, S2N_HASH_SHA256, &hash_state));
152 EXPECT_NOT_NULL(hash_state);
153 EXPECT_SUCCESS(s2n_hash_digest(hash_state, digest, SHA256_DIGEST_LENGTH));
154
155 /* Freeing the connection should free the hashes */
156 EXPECT_SUCCESS(s2n_connection_free(conn));
157 };
158
159 END_TEST();
160 }