Debian Codegraph @

crt/aws-c-s3/tests/s3_checksums_sha1_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_sha1_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[] = {
21 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
22 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d,
23 };
24 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
25
26 return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_SHA1);
27 }
28
29 AWS_TEST_CASE(sha1_nist_test_case_1, s_sha1_nist_test_case_1_fn)
30
31 static int s_sha1_nist_test_case_2_fn(struct aws_allocator *allocator, void *ctx) {
32 (void)ctx;
33
34 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("");
35 uint8_t expected[] = {
36 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55,
37 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09,
38 };
39 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
40
41 return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_SHA1);
42 }
43
44 AWS_TEST_CASE(sha1_nist_test_case_2, s_sha1_nist_test_case_2_fn)
45
46 static int s_sha1_nist_test_case_3_fn(struct aws_allocator *allocator, void *ctx) {
47 (void)ctx;
48
49 struct aws_byte_cursor input =
50 aws_byte_cursor_from_c_str("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
51 uint8_t expected[] = {
52 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae,
53 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1,
54 };
55 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
56
57 return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_SHA1);
58 }
59
60 AWS_TEST_CASE(sha1_nist_test_case_3, s_sha1_nist_test_case_3_fn)
61
62 static int s_sha1_nist_test_case_4_fn(struct aws_allocator *allocator, void *ctx) {
63 (void)ctx;
64
65 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij"
66 "klmghijklmnhijklmnoijklmnopjklmnopqklm"
67 "nopqrlmnopqrsmnopqrstnopqrstu");
68 uint8_t expected[] = {
69 0xa4, 0x9b, 0x24, 0x46, 0xa0, 0x2c, 0x64, 0x5b, 0xf4, 0x19,
70 0xf9, 0x95, 0xb6, 0x70, 0x91, 0x25, 0x3a, 0x04, 0xa2, 0x59,
71 };
72 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
73
74 return s_verify_checksum_test_case(allocator, &input, &expected_buf, aws_checksum_new, AWS_SCA_SHA1);
75 }
76
77 AWS_TEST_CASE(sha1_nist_test_case_4, s_sha1_nist_test_case_4_fn)
78
79 static int s_sha1_nist_test_case_5_fn(struct aws_allocator *allocator, void *ctx) {
80 (void)ctx;
81
82 aws_s3_library_init(allocator);
83
84 struct aws_s3_checksum *checksum = aws_checksum_new(allocator, AWS_SCA_SHA1);
85 ASSERT_NOT_NULL(checksum);
86 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("a");
87
88 for (size_t i = 0; i < 1000000; ++i) {
89 ASSERT_SUCCESS(aws_checksum_update(checksum, &input));
90 }
91
92 uint8_t output[AWS_SHA1_LEN] = {0};
93 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
94 output_buf.len = 0;
95 ASSERT_SUCCESS(aws_checksum_finalize(checksum, &output_buf));
96
97 uint8_t expected[] = {
98 0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e,
99 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f,
100 };
101 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
102 ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len);
103
104 aws_checksum_destroy(checksum);
105
106 aws_s3_library_clean_up();
107
108 return AWS_OP_SUCCESS;
109 }
110
111 AWS_TEST_CASE(sha1_nist_test_case_5, s_sha1_nist_test_case_5_fn)
112
113 static int s_sha1_nist_test_case_6_fn(struct aws_allocator *allocator, void *ctx) {
114 (void)ctx;
115
116 aws_s3_library_init(allocator);
117
118 struct aws_s3_checksum *checksum = aws_checksum_new(allocator, AWS_SCA_SHA1);
119 ASSERT_NOT_NULL(checksum);
120 struct aws_byte_cursor input =
121 aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno");
122
123 for (size_t i = 0; i < 16777216; ++i) {
124 ASSERT_SUCCESS(aws_checksum_update(checksum, &input));
125 }
126
127 uint8_t output[AWS_SHA1_LEN] = {0};
128 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
129 output_buf.len = 0;
130 ASSERT_SUCCESS(aws_checksum_finalize(checksum, &output_buf));
131
132 uint8_t expected[] = {
133 0x77, 0x89, 0xf0, 0xc9, 0xef, 0x7b, 0xfc, 0x40, 0xd9, 0x33,
134 0x11, 0x14, 0x3d, 0xfb, 0xe6, 0x9e, 0x20, 0x17, 0xf5, 0x92,
135 };
136
137 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
138 ASSERT_BIN_ARRAYS_EQUALS(expected_buf.ptr, expected_buf.len, output_buf.buffer, output_buf.len);
139
140 aws_checksum_destroy(checksum);
141
142 aws_s3_library_clean_up();
143
144 return AWS_OP_SUCCESS;
145 }
146
147 AWS_TEST_CASE(sha1_nist_test_case_6, s_sha1_nist_test_case_6_fn)
148
149 static int s_sha1_test_invalid_buffer_fn(struct aws_allocator *allocator, void *ctx) {
150 (void)ctx;
151
152 aws_s3_library_init(allocator);
153
154 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij"
155 "klmghijklmnhijklmnoijklmnopjklmnopqklm"
156 "nopqrlmnopqrsmnopqrstnopqrstu");
157 uint8_t output[AWS_SHA1_LEN] = {0};
158 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
159 output_buf.len = 1;
160
161 ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_checksum_compute(allocator, AWS_SCA_SHA1, &input, &output_buf));
162
163 aws_s3_library_clean_up();
164
165 return AWS_OP_SUCCESS;
166 }
167
168 AWS_TEST_CASE(sha1_test_invalid_buffer, s_sha1_test_invalid_buffer_fn)
169
170 static int s_sha1_test_oneshot_fn(struct aws_allocator *allocator, void *ctx) {
171 (void)ctx;
172
173 aws_s3_library_init(allocator);
174
175 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij"
176 "klmghijklmnhijklmnoijklmnopjklmnopqklm"
177 "nopqrlmnopqrsmnopqrstnopqrstu");
178 uint8_t expected[] = {
179 0xa4, 0x9b, 0x24, 0x46, 0xa0, 0x2c, 0x64, 0x5b, 0xf4, 0x19,
180 0xf9, 0x95, 0xb6, 0x70, 0x91, 0x25, 0x3a, 0x04, 0xa2, 0x59,
181 };
182
183 uint8_t output[AWS_SHA1_LEN] = {0};
184 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
185 output_buf.len = 0;
186
187 ASSERT_SUCCESS(aws_checksum_compute(allocator, AWS_SCA_SHA1, &input, &output_buf));
188 ASSERT_BIN_ARRAYS_EQUALS(expected, sizeof(expected), output_buf.buffer, output_buf.len);
189
190 aws_s3_library_clean_up();
191
192 return AWS_OP_SUCCESS;
193 }
194
195 AWS_TEST_CASE(sha1_test_oneshot, s_sha1_test_oneshot_fn)
196
197 static int s_sha1_test_invalid_state_fn(struct aws_allocator *allocator, void *ctx) {
198 (void)ctx;
199
200 aws_s3_library_init(allocator);
201
202 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghbcdefghicdefghijdefghijkefghijklfghij"
203 "klmghijklmnhijklmnoijklmnopjklmnopqklm"
204 "nopqrlmnopqrsmnopqrstnopqrstu");
205
206 struct aws_s3_checksum *checksum = aws_checksum_new(allocator, AWS_SCA_SHA1);
207 ASSERT_NOT_NULL(checksum);
208
209 uint8_t output[AWS_SHA1_LEN] = {0};
210 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
211 output_buf.len = 0;
212
213 ASSERT_SUCCESS(aws_checksum_update(checksum, &input));
214 ASSERT_SUCCESS(aws_checksum_finalize(checksum, &output_buf));
215 ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_checksum_update(checksum, &input));
216 ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_checksum_finalize(checksum, &output_buf));
217
218 aws_checksum_destroy(checksum);
219
220 aws_s3_library_clean_up();
221
222 return AWS_OP_SUCCESS;
223 }
224
225 AWS_TEST_CASE(sha1_test_invalid_state, s_sha1_test_invalid_state_fn)