Debian Codegraph @

crt/s2n/tests/unit/s2n_hash_all_algs_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 "crypto/s2n_evp.h"
17 #include "crypto/s2n_fips.h"
18 #include "crypto/s2n_hash.h"
19 #include "s2n_test.h"
20 #include "testlib/s2n_testlib.h"
21
22 #define INPUT_DATA_SIZE 100
23 #define OUTPUT_DATA_SIZE 100
24
25 const uint8_t input_data[INPUT_DATA_SIZE] = "hello hash";
26
27 /* These values were generated using the low level s2n_hash implementation.
28 * They are useful to validate that the results of the low level implementation
29 * never change and match the results of the EVP implementation.
30 */
31 const char *expected_result_hex[S2N_HASH_ALGS_COUNT] = {
32 [S2N_HASH_NONE] = "",
33 [S2N_HASH_MD5] = "f5d589043253ca6ae54124c31be43701",
34 [S2N_HASH_SHA1] = "ccf8abd6b03ef5054a4f257e7c712e17f965272d",
35 [S2N_HASH_SHA224] = "dae80554ab74bf098b1a39e48c85c58e4af4628d2a357ee5cf6b1b85",
36 [S2N_HASH_SHA256] = "a8a7fb9d2d3ff62eee5bed1bfcc7b2e17ffebf00c3c77fdf43259d690022041f",
37 [S2N_HASH_SHA384] = "d7131b24ea0985fc9f6462139969decff21f24967f6df17e31ce2410fda6534a5c"
38 "f85cb4be737961eddce0c201c0dac0",
39 [S2N_HASH_SHA512] = "b11305336d6071d8cbab6709fc1019f874961e13a04611f8e7d4c1f9164a2c923f"
40 "7b3da0a37001cef5fdb71584a0f92020a45f23a6fc06cc3ab42ceaa0467a34",
41 [S2N_HASH_MD5_SHA1] = "f5d589043253ca6ae54124c31be43701ccf8abd6b03ef5054a4f257e7c712e17f965272d",
42 [S2N_HASH_SHAKE256_64] = "b3b5bd9ed95e6699e691ff443bc285967c57bec042ea60c88184b"
43 "c02233c1df51c2adb174ea09ea281edb42da9cf0cff09b4bc555b3258b29a953a9bdb0a84c7",
44 };
45
46 S2N_RESULT s2n_hash_test_state(struct s2n_hash_state *hash_state, s2n_hash_algorithm hash_alg, struct s2n_blob *digest)
47 {
48 /* Test s2n_hash_update */
49 {
50 /* Break the test data into some arbitrarily sized chunks. */
51 size_t chunk_sizes[] = { 1, 0, 10, 17, 31 };
52
53 size_t offset = 0;
54 for (size_t i = 0; i < s2n_array_len(chunk_sizes); i++) {
55 RESULT_GUARD_POSIX(s2n_hash_update(hash_state, input_data + offset, chunk_sizes[i]));
56 offset += chunk_sizes[i];
57 RESULT_ENSURE_EQ(hash_state->currently_in_hash, offset);
58 }
59
60 /* Add the rest */
61 RESULT_GUARD_POSIX(s2n_hash_update(hash_state, input_data + offset, (INPUT_DATA_SIZE - offset)));
62 RESULT_ENSURE_EQ(hash_state->currently_in_hash, INPUT_DATA_SIZE);
63 };
64
65 /* Test s2n_hash_copy */
66 struct s2n_hash_state hash_copy = { 0 };
67 {
68 struct s2n_blob result = { 0 };
69 uint8_t result_data[OUTPUT_DATA_SIZE] = { 0 };
70 RESULT_GUARD_POSIX(s2n_blob_init(&result, result_data, OUTPUT_DATA_SIZE));
71
72 RESULT_GUARD_POSIX(s2n_hash_new(&hash_copy));
73 RESULT_GUARD_POSIX(s2n_hash_copy(&hash_copy, hash_state));
74 RESULT_ENSURE_EQ(hash_copy.currently_in_hash, hash_state->currently_in_hash);
75 RESULT_ENSURE_EQ(hash_copy.is_ready_for_input, hash_state->is_ready_for_input);
76 };
77
78 /* Test s2n_hash_digest */
79 {
80 uint8_t digest_size = 0;
81 RESULT_GUARD_POSIX(s2n_hash_digest_size(hash_alg, &digest_size));
82 digest->size = digest_size;
83
84 RESULT_GUARD_POSIX(s2n_hash_digest(hash_state, digest->data, digest_size));
85 RESULT_ENSURE_EQ(hash_state->currently_in_hash, 0);
86 RESULT_ENSURE_EQ(hash_state->is_ready_for_input, false);
87
88 uint8_t copy_result[OUTPUT_DATA_SIZE] = { 0 };
89 RESULT_GUARD_POSIX(s2n_hash_digest(&hash_copy, copy_result, digest_size));
90 RESULT_ENSURE_EQ(hash_state->currently_in_hash, 0);
91 RESULT_ENSURE_EQ(hash_state->is_ready_for_input, false);
92 RESULT_ENSURE_EQ(memcmp(digest->data, copy_result, digest_size), 0);
93 };
94
95 RESULT_GUARD_POSIX(s2n_hash_free(&hash_copy));
96 return S2N_RESULT_OK;
97 }
98
99 S2N_RESULT s2n_hash_test(s2n_hash_algorithm hash_alg, struct s2n_blob *digest)
100 {
101 struct s2n_hash_state hash_state = { 0 };
102
103 /* Test s2n_hash_new + s2n_hash_init */
104 {
105 RESULT_GUARD_POSIX(s2n_hash_new(&hash_state));
106 RESULT_ENSURE_EQ(hash_state.currently_in_hash, 0);
107 RESULT_ENSURE_EQ(hash_state.is_ready_for_input, false);
108
109 RESULT_GUARD_POSIX(s2n_hash_init(&hash_state, hash_alg));
110 RESULT_ENSURE_EQ(hash_state.currently_in_hash, 0);
111 RESULT_ENSURE_EQ(hash_state.is_ready_for_input, true);
112
113 RESULT_GUARD(s2n_hash_test_state(&hash_state, hash_alg, digest));
114 };
115
116 /* Test s2n_hash_reset */
117 {
118 struct s2n_blob result = { 0 };
119 uint8_t result_data[OUTPUT_DATA_SIZE] = { 0 };
120 RESULT_GUARD_POSIX(s2n_blob_init(&result, result_data, OUTPUT_DATA_SIZE));
121
122 RESULT_GUARD_POSIX(s2n_hash_reset(&hash_state));
123 RESULT_ENSURE_EQ(hash_state.currently_in_hash, 0);
124 RESULT_ENSURE_EQ(hash_state.is_ready_for_input, true);
125
126 RESULT_GUARD(s2n_hash_test_state(&hash_state, hash_alg, &result));
127 RESULT_ENSURE_EQ(digest->size, result.size);
128 RESULT_ENSURE_EQ(memcmp(digest->data, result.data, result.size), 0);
129 };
130
131 RESULT_GUARD_POSIX(s2n_hash_free(&hash_state));
132 return S2N_RESULT_OK;
133 }
134
135 int main(int argc, char **argv)
136 {
137 BEGIN_TEST();
138
139 for (s2n_hash_algorithm hash_alg = 0; hash_alg < S2N_HASH_ALGS_COUNT; hash_alg++) {
140 if (!s2n_hash_is_available(hash_alg)) {
141 continue;
142 }
143
144 struct s2n_blob actual_result = { 0 };
145 uint8_t actual_result_data[OUTPUT_DATA_SIZE] = { 0 };
146 EXPECT_SUCCESS(s2n_blob_init(&actual_result, actual_result_data, OUTPUT_DATA_SIZE));
147
148 S2N_CHECKED_BLOB_FROM_HEX(expected_result, EXPECT_OK, expected_result_hex[hash_alg]);
149
150 EXPECT_OK(s2n_hash_test(hash_alg, &actual_result));
151 EXPECT_EQUAL(expected_result.size, actual_result.size);
152 EXPECT_BYTEARRAY_EQUAL(expected_result.data, actual_result.data, actual_result.size);
153 }
154
155 END_TEST();
156 }