Debian Codegraph @

crt/aws-c-cal/tests/sha512_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/cal/hash.h>
6 #include <aws/common/byte_buf.h>
7 #include <aws/testing/aws_test_harness.h>
8
9 #include "test_case_helper.h"
10 /*
11 * these are the NIST test vectors, as compiled here:
12 * https://www.di-mgt.com.au/sha_testvectors.html
13 */
14
15 static int s_sha512_nist_test_case_1_fn(struct aws_allocator *allocator, void *ctx) {
16 (void)ctx;
17
18 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abc");
19 uint8_t expected[] = {
20 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31, 0x12,
21 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a, 0x21, 0x92,
22 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd, 0x45, 0x4d, 0x44,
23 0x23, 0x64, 0x3c, 0xe8, 0x0e, 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
24
25 };
26 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
27
28 return s_verify_hash_test_case(allocator, &input, &expected_buf, aws_sha512_new);
29 }
30
31 AWS_TEST_CASE(sha512_nist_test_case_1, s_sha512_nist_test_case_1_fn)
32
33 static int s_sha512_nist_test_case_2_fn(struct aws_allocator *allocator, void *ctx) {
34 (void)ctx;
35
36 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("");
37 uint8_t expected[] = {0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6,
38 0x6d, 0x80, 0x07, 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 0x83, 0xf4,
39 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2,
40 0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, 0x63, 0xb9, 0x31, 0xbd,
41 0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e};
42 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
43
44 return s_verify_hash_test_case(allocator, &input, &expected_buf, aws_sha512_new);
45 }
46
47 AWS_TEST_CASE(sha512_nist_test_case_2, s_sha512_nist_test_case_2_fn)
48
49 static int s_sha512_nist_test_case_3_fn(struct aws_allocator *allocator, void *ctx) {
50 (void)ctx;
51
52 struct aws_byte_cursor input =
53 aws_byte_cursor_from_c_str("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
54 uint8_t expected[] = {
55 0x20, 0x4a, 0x8f, 0xc6, 0xdd, 0xa8, 0x2f, 0x0a, 0x0c, 0xed, 0x7b, 0xeb, 0x8e, 0x08, 0xa4, 0x16, 0x57,
56 0xc1, 0x6e, 0xf4, 0x68, 0xb2, 0x28, 0xa8, 0x27, 0x9b, 0xe3, 0x31, 0xa7, 0x03, 0xc3, 0x35, 0x96, 0xfd,
57 0x15, 0xc1, 0x3b, 0x1b, 0x07, 0xf9, 0xaa, 0x1d, 0x3b, 0xea, 0x57, 0x78, 0x9c, 0xa0, 0x31, 0xad, 0x85,
58 0xc7, 0xa7, 0x1d, 0xd7, 0x03, 0x54, 0xec, 0x63, 0x12, 0x38, 0xca, 0x34, 0x45
59
60 };
61 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
62
63 return s_verify_hash_test_case(allocator, &input, &expected_buf, aws_sha512_new);
64 }
65
66 AWS_TEST_CASE(sha512_nist_test_case_3, s_sha512_nist_test_case_3_fn)
67
68 static int s_sha512_nist_test_case_4_fn(struct aws_allocator *allocator, void *ctx) {
69 (void)ctx;
70
71 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij"
72 "klmghijklmnhijklmnoijklmnopjklmnopqklm"
73 "nopqrlmnopqrsmnopqrstnopqrstu");
74 uint8_t expected[] = {0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7, 0x28, 0x14,
75 0xfc, 0x14, 0x3f, 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1, 0x72, 0x99,
76 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7,
77 0xe4, 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a, 0xc7, 0xd3, 0x29, 0xee,
78 0xb6, 0xdd, 0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09};
79 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
80
81 return s_verify_hash_test_case(allocator, &input, &expected_buf, aws_sha512_new);
82 }
83
84 AWS_TEST_CASE(sha512_nist_test_case_4, s_sha512_nist_test_case_4_fn)
85
86 static int s_sha512_nist_test_case_5_fn(struct aws_allocator *allocator, void *ctx) {
87 (void)ctx;
88
89 aws_cal_library_test_init(allocator);
90
91 struct aws_hash *hash = aws_sha512_new(allocator);
92 ASSERT_NOT_NULL(hash);
93 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("a");
94
95 for (size_t i = 0; i < 1000000; ++i) {
96 ASSERT_SUCCESS(aws_hash_update(hash, &input));
97 }
98
99 uint8_t output[AWS_SHA512_LEN] = {0};
100 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
101 output_buf.len = 0;
102 ASSERT_SUCCESS(aws_hash_finalize(hash, &output_buf, 0));
103
104 uint8_t expected[] = {
105 0xe7, 0x18, 0x48, 0x3d, 0x0c, 0xe7, 0x69, 0x64, 0x4e, 0x2e, 0x42, 0xc7, 0xbc, 0x15, 0xb4, 0x63, 0x8e,
106 0x1f, 0x98, 0xb1, 0x3b, 0x20, 0x44, 0x28, 0x56, 0x32, 0xa8, 0x03, 0xaf, 0xa9, 0x73, 0xeb, 0xde, 0x0f,
107 0xf2, 0x44, 0x87, 0x7e, 0xa6, 0x0a, 0x4c, 0xb0, 0x43, 0x2c, 0xe5, 0x77, 0xc3, 0x1b, 0xeb, 0x00, 0x9c,
108 0x5c, 0x2c, 0x49, 0xaa, 0x2e, 0x4e, 0xad, 0xb2, 0x17, 0xad, 0x8c, 0xc0, 0x9b
109
110 };
111 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
112 ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len);
113
114 aws_hash_destroy(hash);
115
116 aws_cal_library_clean_up();
117
118 return AWS_OP_SUCCESS;
119 }
120
121 AWS_TEST_CASE(sha512_nist_test_case_5, s_sha512_nist_test_case_5_fn)
122
123 static int s_sha512_nist_test_case_5_truncated_fn(struct aws_allocator *allocator, void *ctx) {
124 (void)ctx;
125
126 aws_cal_library_test_init(allocator);
127
128 struct aws_hash *hash = aws_sha512_new(allocator);
129 ASSERT_NOT_NULL(hash);
130 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("a");
131
132 for (size_t i = 0; i < 1000000; ++i) {
133 ASSERT_SUCCESS(aws_hash_update(hash, &input));
134 }
135
136 uint8_t expected[] = {
137 0xe7, 0x18, 0x48, 0x3d, 0x0c, 0xe7, 0x69, 0x64, 0x4e, 0x2e, 0x42, 0xc7, 0xbc, 0x15, 0xb4, 0x63,
138 0x8e, 0x1f, 0x98, 0xb1, 0x3b, 0x20, 0x44, 0x28, 0x56, 0x32, 0xa8, 0x03, 0xaf, 0xa9, 0x73, 0xeb,
139 };
140 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
141 uint8_t output[AWS_SHA512_LEN] = {0};
142 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, expected_buf.len);
143 output_buf.len = 0;
144 ASSERT_SUCCESS(aws_hash_finalize(hash, &output_buf, 32));
145
146 ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len);
147
148 aws_hash_destroy(hash);
149
150 aws_cal_library_clean_up();
151
152 return AWS_OP_SUCCESS;
153 }
154
155 AWS_TEST_CASE(sha512_nist_test_case_5_truncated, s_sha512_nist_test_case_5_truncated_fn)
156
157 static int s_sha512_nist_test_case_6_fn(struct aws_allocator *allocator, void *ctx) {
158 (void)ctx;
159
160 aws_cal_library_test_init(allocator);
161
162 struct aws_hash *hash = aws_sha512_new(allocator);
163 ASSERT_NOT_NULL(hash);
164 struct aws_byte_cursor input =
165 aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno");
166
167 for (size_t i = 0; i < 16777216; ++i) {
168 ASSERT_SUCCESS(aws_hash_update(hash, &input));
169 }
170
171 uint8_t output[AWS_SHA512_LEN] = {0};
172 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
173 output_buf.len = 0;
174 ASSERT_SUCCESS(aws_hash_finalize(hash, &output_buf, 0));
175
176 uint8_t expected[] = {0xb4, 0x7c, 0x93, 0x34, 0x21, 0xea, 0x2d, 0xb1, 0x49, 0xad, 0x6e, 0x10, 0xfc,
177 0xe6, 0xc7, 0xf9, 0x3d, 0x07, 0x52, 0x38, 0x01, 0x80, 0xff, 0xd7, 0xf4, 0x62,
178 0x9a, 0x71, 0x21, 0x34, 0x83, 0x1d, 0x77, 0xbe, 0x60, 0x91, 0xb8, 0x19, 0xed,
179 0x35, 0x2c, 0x29, 0x67, 0xa2, 0xe2, 0xd4, 0xfa, 0x50, 0x50, 0x72, 0x3c, 0x96,
180 0x30, 0x69, 0x1f, 0x1a, 0x05, 0xa7, 0x28, 0x1d, 0xbe, 0x6c, 0x10, 0x86};
181
182 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
183 ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len);
184
185 aws_hash_destroy(hash);
186
187 aws_cal_library_clean_up();
188
189 return AWS_OP_SUCCESS;
190 }
191
192 AWS_TEST_CASE(sha512_nist_test_case_6, s_sha512_nist_test_case_6_fn)
193
194 static int s_sha512_test_invalid_buffer_fn(struct aws_allocator *allocator, void *ctx) {
195 (void)ctx;
196
197 aws_cal_library_test_init(allocator);
198
199 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij"
200 "klmghijklmnhijklmnoijklmnopjklmnopqklm"
201 "nopqrlmnopqrsmnopqrstnopqrstu");
202 uint8_t output[AWS_SHA512_LEN] = {0};
203 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
204 output_buf.len = 1;
205
206 ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_sha512_compute(allocator, &input, &output_buf, 0));
207
208 aws_cal_library_clean_up();
209
210 return AWS_OP_SUCCESS;
211 }
212
213 AWS_TEST_CASE(sha512_test_invalid_buffer, s_sha512_test_invalid_buffer_fn)
214
215 static int s_sha512_test_oneshot_fn(struct aws_allocator *allocator, void *ctx) {
216 (void)ctx;
217
218 aws_cal_library_test_init(allocator);
219
220 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij"
221 "klmghijklmnhijklmnoijklmnopjklmnopqklm"
222 "nopqrlmnopqrsmnopqrstnopqrstu");
223 uint8_t expected[] = {0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7, 0x28, 0x14,
224 0xfc, 0x14, 0x3f, 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1, 0x72, 0x99,
225 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7,
226 0xe4, 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a, 0xc7, 0xd3, 0x29, 0xee,
227 0xb6, 0xdd, 0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09};
228
229 uint8_t output[AWS_SHA512_LEN] = {0};
230 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
231 output_buf.len = 0;
232
233 ASSERT_SUCCESS(aws_sha512_compute(allocator, &input, &output_buf, 0));
234 ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), output_buf.buffer, output_buf.len);
235
236 aws_cal_library_clean_up();
237
238 return AWS_OP_SUCCESS;
239 }
240
241 AWS_TEST_CASE(sha512_test_oneshot, s_sha512_test_oneshot_fn)
242
243 static int s_sha512_test_invalid_state_fn(struct aws_allocator *allocator, void *ctx) {
244 (void)ctx;
245
246 aws_cal_library_test_init(allocator);
247
248 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij"
249 "klmghijklmnhijklmnoijklmnopjklmnopqklm"
250 "nopqrlmnopqrsmnopqrstnopqrstu");
251
252 struct aws_hash *hash = aws_sha512_new(allocator);
253 ASSERT_NOT_NULL(hash);
254
255 uint8_t output[AWS_SHA512_LEN] = {0};
256 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
257 output_buf.len = 0;
258
259 ASSERT_SUCCESS(aws_hash_update(hash, &input));
260 ASSERT_SUCCESS(aws_hash_finalize(hash, &output_buf, 0));
261 ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_hash_update(hash, &input));
262 ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_hash_finalize(hash, &output_buf, 0));
263
264 aws_hash_destroy(hash);
265
266 aws_cal_library_clean_up();
267
268 return AWS_OP_SUCCESS;
269 }
270
271 AWS_TEST_CASE(sha512_test_invalid_state, s_sha512_test_invalid_state_fn)
272
273 static int s_sha512_test_extra_buffer_space_fn(struct aws_allocator *allocator, void *ctx) {
274 (void)ctx;
275
276 aws_cal_library_test_init(allocator);
277
278 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("123456789012345678901234567890123456789012345"
279 "67890123456789012345678901234567890");
280
281 struct aws_byte_buf digest_size_buf;
282 struct aws_byte_buf super_size_buf;
283
284 aws_byte_buf_init(&digest_size_buf, allocator, AWS_SHA512_LEN);
285 aws_byte_buf_init(&super_size_buf, allocator, AWS_SHA512_LEN + 100);
286
287 aws_sha512_compute(allocator, &input, &digest_size_buf, 0);
288 aws_sha512_compute(allocator, &input, &super_size_buf, 0);
289
290 ASSERT_TRUE(aws_byte_buf_eq(&digest_size_buf, &super_size_buf));
291 ASSERT_TRUE(super_size_buf.len == AWS_SHA512_LEN);
292
293 aws_byte_buf_clean_up(&digest_size_buf);
294 aws_byte_buf_clean_up(&super_size_buf);
295
296 aws_cal_library_clean_up();
297
298 return AWS_OP_SUCCESS;
299 }
300 AWS_TEST_CASE(sha512_test_extra_buffer_space, s_sha512_test_extra_buffer_space_fn)