Debian Codegraph @

crt/aws-c-s3/tests/s3_checksums_sha512_tests.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/s3/private/s3_checksums.h"
6 #include <aws/cal/hash.h>
7 #include <aws/common/byte_buf.h>
8 #include <aws/testing/aws_test_harness.h>
9
10 #include <s3_checksums_test_case_helper.h>
11 /*
12 * these are the NIST test vectors, as compiled here:
13 * https://www.di-mgt.com.au/sha_testvectors.html
14 */
15
16 static int s_sha512_nist_test_case_1_fn(struct aws_allocator *allocator, void *ctx) {
17 (void)ctx;
18
19 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abc");
20 uint8_t expected[] = {0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73, 0x49, 0xae,
21 0x20, 0x41, 0x31, 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 0x0a, 0x9e,
22 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a, 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1,
23 0xa8, 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd, 0x45, 0x4d, 0x44, 0x23,
24 0x64, 0x3c, 0xe8, 0x0e, 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f};
25 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
26
27 return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_SHA512);
28 }
29
30 AWS_TEST_CASE(sha512_nist_test_case_1, s_sha512_nist_test_case_1_fn)
31
32 static int s_sha512_nist_test_case_2_fn(struct aws_allocator *allocator, void *ctx) {
33 (void)ctx;
34
35 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("");
36 uint8_t expected[] = {
37 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07,
38 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce,
39 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f,
40 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e,
41 };
42 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
43
44 return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_SHA512);
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,
56 0x57, 0xc1, 0x6e, 0xf4, 0x68, 0xb2, 0x28, 0xa8, 0x27, 0x9b, 0xe3, 0x31, 0xa7, 0x03, 0xc3, 0x35,
57 0x96, 0xfd, 0x15, 0xc1, 0x3b, 0x1b, 0x07, 0xf9, 0xaa, 0x1d, 0x3b, 0xea, 0x57, 0x78, 0x9c, 0xa0,
58 0x31, 0xad, 0x85, 0xc7, 0xa7, 0x1d, 0xd7, 0x03, 0x54, 0xec, 0x63, 0x12, 0x38, 0xca, 0x34, 0x45,
59 };
60 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
61
62 return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_SHA512);
63 }
64
65 AWS_TEST_CASE(sha512_nist_test_case_3, s_sha512_nist_test_case_3_fn)
66
67 static int s_sha512_nist_test_case_4_fn(struct aws_allocator *allocator, void *ctx) {
68 (void)ctx;
69
70 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij"
71 "klmghijklmnhijklmnoijklmnopjklmnopqklm"
72 "nopqrlmnopqrsmnopqrstnopqrstu");
73 uint8_t expected[] = {
74 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f,
75 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1, 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18,
76 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4, 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
77 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09,
78 };
79 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
80
81 return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_SHA512);
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_s3_library_init(allocator);
90
91 struct aws_s3_checksum *checksum = aws_checksum_new(allocator, AWS_SCA_SHA512);
92 ASSERT_NOT_NULL(checksum);
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_checksum_update(checksum, &input));
97 }
98
99 uint8_t output[AWS_SHA512_LEN] = {0};
100 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output));
101 ASSERT_SUCCESS(aws_checksum_finalize(checksum, &output_buf));
102
103 uint8_t expected[] = {
104 0xe7, 0x18, 0x48, 0x3d, 0x0c, 0xe7, 0x69, 0x64, 0x4e, 0x2e, 0x42, 0xc7, 0xbc, 0x15, 0xb4, 0x63,
105 0x8e, 0x1f, 0x98, 0xb1, 0x3b, 0x20, 0x44, 0x28, 0x56, 0x32, 0xa8, 0x03, 0xaf, 0xa9, 0x73, 0xeb,
106 0xde, 0x0f, 0xf2, 0x44, 0x87, 0x7e, 0xa6, 0x0a, 0x4c, 0xb0, 0x43, 0x2c, 0xe5, 0x77, 0xc3, 0x1b,
107 0xeb, 0x00, 0x9c, 0x5c, 0x2c, 0x49, 0xaa, 0x2e, 0x4e, 0xad, 0xb2, 0x17, 0xad, 0x8c, 0xc0, 0x9b,
108 };
109 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
110 ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len);
111
112 aws_checksum_destroy(checksum);
113
114 aws_s3_library_clean_up();
115
116 return AWS_OP_SUCCESS;
117 }
118
119 AWS_TEST_CASE(sha512_nist_test_case_5, s_sha512_nist_test_case_5_fn)
120
121 static int s_sha512_nist_test_case_6_fn(struct aws_allocator *allocator, void *ctx) {
122 (void)ctx;
123
124 aws_s3_library_init(allocator);
125
126 struct aws_s3_checksum *checksum = aws_checksum_new(allocator, AWS_SCA_SHA512);
127 ASSERT_NOT_NULL(checksum);
128 struct aws_byte_cursor input =
129 aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno");
130
131 for (size_t i = 0; i < 16777216; ++i) {
132 ASSERT_SUCCESS(aws_checksum_update(checksum, &input));
133 }
134
135 uint8_t output[AWS_SHA512_LEN] = {0};
136 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output));
137 ASSERT_SUCCESS(aws_checksum_finalize(checksum, &output_buf));
138
139 uint8_t expected[] = {
140 0xb4, 0x7c, 0x93, 0x34, 0x21, 0xea, 0x2d, 0xb1, 0x49, 0xad, 0x6e, 0x10, 0xfc, 0xe6, 0xc7, 0xf9,
141 0x3d, 0x07, 0x52, 0x38, 0x01, 0x80, 0xff, 0xd7, 0xf4, 0x62, 0x9a, 0x71, 0x21, 0x34, 0x83, 0x1d,
142 0x77, 0xbe, 0x60, 0x91, 0xb8, 0x19, 0xed, 0x35, 0x2c, 0x29, 0x67, 0xa2, 0xe2, 0xd4, 0xfa, 0x50,
143 0x50, 0x72, 0x3c, 0x96, 0x30, 0x69, 0x1f, 0x1a, 0x05, 0xa7, 0x28, 0x1d, 0xbe, 0x6c, 0x10, 0x86,
144 };
145
146 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
147 ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len);
148
149 aws_checksum_destroy(checksum);
150
151 aws_s3_library_clean_up();
152
153 return AWS_OP_SUCCESS;
154 }
155
156 AWS_TEST_CASE(sha512_nist_test_case_6, s_sha512_nist_test_case_6_fn)
157
158 static int s_sha512_test_invalid_buffer_fn(struct aws_allocator *allocator, void *ctx) {
159 (void)ctx;
160
161 aws_s3_library_init(allocator);
162
163 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij"
164 "klmghijklmnhijklmnoijklmnopjklmnopqklm"
165 "nopqrlmnopqrsmnopqrstnopqrstu");
166 uint8_t output[AWS_SHA512_LEN] = {0};
167 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
168 output_buf.len = 1;
169
170 ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_checksum_compute(allocator, AWS_SCA_SHA512, &input, &output_buf));
171
172 aws_s3_library_clean_up();
173
174 return AWS_OP_SUCCESS;
175 }
176
177 AWS_TEST_CASE(sha512_test_invalid_buffer, s_sha512_test_invalid_buffer_fn)
178
179 static int s_sha512_test_oneshot_fn(struct aws_allocator *allocator, void *ctx) {
180 (void)ctx;
181
182 aws_s3_library_init(allocator);
183
184 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij"
185 "klmghijklmnhijklmnoijklmnopjklmnopqklm"
186 "nopqrlmnopqrsmnopqrstnopqrstu");
187 uint8_t expected[] = {
188 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f,
189 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1, 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18,
190 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4, 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
191 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09,
192 };
193
194 uint8_t output[AWS_SHA512_LEN] = {0};
195 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
196 output_buf.len = 0;
197
198 ASSERT_SUCCESS(aws_checksum_compute(allocator, AWS_SCA_SHA512, &input, &output_buf));
199 ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), output_buf.buffer, output_buf.len);
200
201 aws_s3_library_clean_up();
202
203 return AWS_OP_SUCCESS;
204 }
205
206 AWS_TEST_CASE(sha512_test_oneshot, s_sha512_test_oneshot_fn)
207
208 static int s_sha512_test_invalid_state_fn(struct aws_allocator *allocator, void *ctx) {
209 (void)ctx;
210
211 aws_s3_library_init(allocator);
212
213 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij"
214 "klmghijklmnhijklmnoijklmnopjklmnopqklm"
215 "nopqrlmnopqrsmnopqrstnopqrstu");
216
217 struct aws_s3_checksum *checksum = aws_checksum_new(allocator, AWS_SCA_SHA512);
218 ASSERT_NOT_NULL(checksum);
219
220 uint8_t output[AWS_SHA512_LEN] = {0};
221 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, sizeof(output));
222
223 ASSERT_SUCCESS(aws_checksum_update(checksum, &input));
224 ASSERT_SUCCESS(aws_checksum_finalize(checksum, &output_buf));
225 ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_checksum_update(checksum, &input));
226 ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_checksum_finalize(checksum, &output_buf));
227
228 aws_checksum_destroy(checksum);
229
230 aws_s3_library_clean_up();
231
232 return AWS_OP_SUCCESS;
233 }
234
235 AWS_TEST_CASE(sha512_test_invalid_state, s_sha512_test_invalid_state_fn)