Debian Codegraph @

crt/aws-c-cal/tests/md5_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/testing/aws_test_harness.h>
7
8 #include "test_case_helper.h"
9
10 /*
11 * these are the rfc1321 test vectors
12 */
13
14 static int s_md5_rfc1321_test_case_1_fn(struct aws_allocator *allocator, void *ctx) {
15 (void)ctx;
16
17 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("");
18 uint8_t expected[] = {
19 0xd4,
20 0x1d,
21 0x8c,
22 0xd9,
23 0x8f,
24 0x00,
25 0xb2,
26 0x04,
27 0xe9,
28 0x80,
29 0x09,
30 0x98,
31 0xec,
32 0xf8,
33 0x42,
34 0x7e,
35 };
36 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
37
38 return s_verify_hash_test_case(allocator, &input, &expected_buf, aws_md5_new);
39 }
40
41 AWS_TEST_CASE(md5_rfc1321_test_case_1, s_md5_rfc1321_test_case_1_fn)
42
43 static int s_md5_rfc1321_test_case_2_fn(struct aws_allocator *allocator, void *ctx) {
44 (void)ctx;
45
46 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("a");
47 uint8_t expected[] = {
48 0x0c,
49 0xc1,
50 0x75,
51 0xb9,
52 0xc0,
53 0xf1,
54 0xb6,
55 0xa8,
56 0x31,
57 0xc3,
58 0x99,
59 0xe2,
60 0x69,
61 0x77,
62 0x26,
63 0x61,
64 };
65 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
66
67 return s_verify_hash_test_case(allocator, &input, &expected_buf, aws_md5_new);
68 }
69
70 AWS_TEST_CASE(md5_rfc1321_test_case_2, s_md5_rfc1321_test_case_2_fn)
71
72 static int s_md5_rfc1321_test_case_3_fn(struct aws_allocator *allocator, void *ctx) {
73 (void)ctx;
74
75 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abc");
76 uint8_t expected[] = {
77 0x90,
78 0x01,
79 0x50,
80 0x98,
81 0x3c,
82 0xd2,
83 0x4f,
84 0xb0,
85 0xd6,
86 0x96,
87 0x3f,
88 0x7d,
89 0x28,
90 0xe1,
91 0x7f,
92 0x72,
93 };
94 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
95
96 return s_verify_hash_test_case(allocator, &input, &expected_buf, aws_md5_new);
97 }
98
99 AWS_TEST_CASE(md5_rfc1321_test_case_3, s_md5_rfc1321_test_case_3_fn)
100
101 static int s_md5_rfc1321_test_case_4_fn(struct aws_allocator *allocator, void *ctx) {
102 (void)ctx;
103
104 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("message digest");
105 uint8_t expected[] = {
106 0xf9,
107 0x6b,
108 0x69,
109 0x7d,
110 0x7c,
111 0xb7,
112 0x93,
113 0x8d,
114 0x52,
115 0x5a,
116 0x2f,
117 0x31,
118 0xaa,
119 0xf1,
120 0x61,
121 0xd0,
122 };
123 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
124
125 return s_verify_hash_test_case(allocator, &input, &expected_buf, aws_md5_new);
126 }
127
128 AWS_TEST_CASE(md5_rfc1321_test_case_4, s_md5_rfc1321_test_case_4_fn)
129
130 static int s_md5_rfc1321_test_case_5_fn(struct aws_allocator *allocator, void *ctx) {
131 (void)ctx;
132
133 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("abcdefghijklmnopqrstuvwxyz");
134 uint8_t expected[] = {
135 0xc3,
136 0xfc,
137 0xd3,
138 0xd7,
139 0x61,
140 0x92,
141 0xe4,
142 0x00,
143 0x7d,
144 0xfb,
145 0x49,
146 0x6c,
147 0xca,
148 0x67,
149 0xe1,
150 0x3b,
151 };
152 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
153
154 return s_verify_hash_test_case(allocator, &input, &expected_buf, aws_md5_new);
155 }
156
157 AWS_TEST_CASE(md5_rfc1321_test_case_5, s_md5_rfc1321_test_case_5_fn)
158
159 static int s_md5_rfc1321_test_case_6_fn(struct aws_allocator *allocator, void *ctx) {
160 (void)ctx;
161
162 struct aws_byte_cursor input =
163 aws_byte_cursor_from_c_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
164 uint8_t expected[] = {
165 0xd1,
166 0x74,
167 0xab,
168 0x98,
169 0xd2,
170 0x77,
171 0xd9,
172 0xf5,
173 0xa5,
174 0x61,
175 0x1c,
176 0x2c,
177 0x9f,
178 0x41,
179 0x9d,
180 0x9f,
181 };
182 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
183
184 return s_verify_hash_test_case(allocator, &input, &expected_buf, aws_md5_new);
185 }
186
187 AWS_TEST_CASE(md5_rfc1321_test_case_6, s_md5_rfc1321_test_case_6_fn)
188
189 static int s_md5_rfc1321_test_case_7_fn(struct aws_allocator *allocator, void *ctx) {
190 (void)ctx;
191
192 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("123456789012345678901234567890123456789012345"
193 "67890123456789012345678901234567890");
194 uint8_t expected[] = {
195 0x57,
196 0xed,
197 0xf4,
198 0xa2,
199 0x2b,
200 0xe3,
201 0xc9,
202 0x55,
203 0xac,
204 0x49,
205 0xda,
206 0x2e,
207 0x21,
208 0x07,
209 0xb6,
210 0x7a,
211 };
212 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
213
214 return s_verify_hash_test_case(allocator, &input, &expected_buf, aws_md5_new);
215 }
216
217 AWS_TEST_CASE(md5_rfc1321_test_case_7, s_md5_rfc1321_test_case_7_fn)
218
219 static int s_md5_rfc1321_test_case_7_truncated_fn(struct aws_allocator *allocator, void *ctx) {
220 (void)ctx;
221
222 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("123456789012345678901234567890123456789012345"
223 "67890123456789012345678901234567890");
224 uint8_t expected[] = {
225 0x57,
226 0xed,
227 0xf4,
228 0xa2,
229 0x2b,
230 0xe3,
231 0xc9,
232 0x55,
233 };
234 struct aws_byte_cursor expected_buf = aws_byte_cursor_from_array(expected, sizeof(expected));
235
236 return s_verify_hash_test_case(allocator, &input, &expected_buf, aws_md5_new);
237 }
238
239 AWS_TEST_CASE(md5_rfc1321_test_case_7_truncated, s_md5_rfc1321_test_case_7_truncated_fn)
240
241 static int s_md5_verify_known_collision_fn(struct aws_allocator *allocator, void *ctx) {
242 (void)ctx;
243
244 aws_cal_library_test_init(allocator);
245
246 uint8_t message_1[] = {
247 0xd1, 0x31, 0xdd, 0x02, 0xc5, 0xe6, 0xee, 0xc4, 0x69, 0x3d, 0x9a, 0x06, 0x98, 0xaf, 0xf9, 0x5c,
248 0x2f, 0xca, 0xb5, 0x87, 0x12, 0x46, 0x7e, 0xab, 0x40, 0x04, 0x58, 0x3e, 0xb8, 0xfb, 0x7f, 0x89,
249 0x55, 0xad, 0x34, 0x06, 0x09, 0xf4, 0xb3, 0x02, 0x83, 0xe4, 0x88, 0x83, 0x25, 0x71, 0x41, 0x5a,
250 0x08, 0x51, 0x25, 0xe8, 0xf7, 0xcd, 0xc9, 0x9f, 0xd9, 0x1d, 0xbd, 0xf2, 0x80, 0x37, 0x3c, 0x5b,
251 0xd8, 0x82, 0x3e, 0x31, 0x56, 0x34, 0x8f, 0x5b, 0xae, 0x6d, 0xac, 0xd4, 0x36, 0xc9, 0x19, 0xc6,
252 0xdd, 0x53, 0xe2, 0xb4, 0x87, 0xda, 0x03, 0xfd, 0x02, 0x39, 0x63, 0x06, 0xd2, 0x48, 0xcd, 0xa0,
253 0xe9, 0x9f, 0x33, 0x42, 0x0f, 0x57, 0x7e, 0xe8, 0xce, 0x54, 0xb6, 0x70, 0x80, 0xa8, 0x0d, 0x1e,
254 0xc6, 0x98, 0x21, 0xbc, 0xb6, 0xa8, 0x83, 0x93, 0x96, 0xf9, 0x65, 0x2b, 0x6f, 0xf7, 0x2a, 0x70,
255 };
256
257 uint8_t message_2[] = {
258 0xd1, 0x31, 0xdd, 0x02, 0xc5, 0xe6, 0xee, 0xc4, 0x69, 0x3d, 0x9a, 0x06, 0x98, 0xaf, 0xf9, 0x5c,
259 0x2f, 0xca, 0xb5, 0x07, 0x12, 0x46, 0x7e, 0xab, 0x40, 0x04, 0x58, 0x3e, 0xb8, 0xfb, 0x7f, 0x89,
260 0x55, 0xad, 0x34, 0x06, 0x09, 0xf4, 0xb3, 0x02, 0x83, 0xe4, 0x88, 0x83, 0x25, 0xf1, 0x41, 0x5a,
261 0x08, 0x51, 0x25, 0xe8, 0xf7, 0xcd, 0xc9, 0x9f, 0xd9, 0x1d, 0xbd, 0x72, 0x80, 0x37, 0x3c, 0x5b,
262 0xd8, 0x82, 0x3e, 0x31, 0x56, 0x34, 0x8f, 0x5b, 0xae, 0x6d, 0xac, 0xd4, 0x36, 0xc9, 0x19, 0xc6,
263 0xdd, 0x53, 0xe2, 0x34, 0x87, 0xda, 0x03, 0xfd, 0x02, 0x39, 0x63, 0x06, 0xd2, 0x48, 0xcd, 0xa0,
264 0xe9, 0x9f, 0x33, 0x42, 0x0f, 0x57, 0x7e, 0xe8, 0xce, 0x54, 0xb6, 0x70, 0x80, 0x28, 0x0d, 0x1e,
265 0xc6, 0x98, 0x21, 0xbc, 0xb6, 0xa8, 0x83, 0x93, 0x96, 0xf9, 0x65, 0xab, 0x6f, 0xf7, 0x2a, 0x70,
266 };
267
268 uint8_t collision_result[] = {
269 0x79,
270 0x05,
271 0x40,
272 0x25,
273 0x25,
274 0x5f,
275 0xb1,
276 0xa2,
277 0x6e,
278 0x4b,
279 0xc4,
280 0x22,
281 0xae,
282 0xf5,
283 0x4e,
284 0xb4,
285 };
286
287 uint8_t output1[AWS_MD5_LEN] = {0};
288 struct aws_byte_buf output1_buf = aws_byte_buf_from_array(output1, sizeof(output1));
289 output1_buf.len = 0;
290
291 struct aws_byte_cursor message_1_buf = aws_byte_cursor_from_array(message_1, sizeof(message_1));
292
293 ASSERT_SUCCESS(aws_md5_compute(allocator, &message_1_buf, &output1_buf, 0));
294 ASSERT_BIN_ARRAYS_EQUALS(collision_result, sizeof(collision_result), output1, sizeof(output1));
295
296 uint8_t output2[AWS_MD5_LEN] = {0};
297 struct aws_byte_buf output2_buf = aws_byte_buf_from_array(output2, sizeof(output2));
298 output2_buf.len = 0;
299
300 struct aws_byte_cursor message_2_buf = aws_byte_cursor_from_array(message_2, sizeof(message_2));
301
302 ASSERT_SUCCESS(aws_md5_compute(allocator, &message_2_buf, &output2_buf, 0));
303 ASSERT_BIN_ARRAYS_EQUALS(collision_result, sizeof(collision_result), output2, sizeof(output2));
304
305 aws_cal_library_clean_up();
306
307 return AWS_OP_SUCCESS;
308 }
309
310 AWS_TEST_CASE(md5_verify_known_collision, s_md5_verify_known_collision_fn)
311
312 static int s_md5_invalid_buffer_size_fn(struct aws_allocator *allocator, void *ctx) {
313 (void)ctx;
314
315 aws_cal_library_test_init(allocator);
316
317 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("123456789012345678901234567890123456789012345"
318 "67890123456789012345678901234567890");
319
320 uint8_t output[AWS_MD5_LEN] = {0};
321 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
322 output_buf.len = 1;
323
324 ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_md5_compute(allocator, &input, &output_buf, 0));
325
326 aws_cal_library_clean_up();
327
328 return AWS_OP_SUCCESS;
329 }
330
331 AWS_TEST_CASE(md5_invalid_buffer_size, s_md5_invalid_buffer_size_fn)
332
333 static int s_md5_test_invalid_state_fn(struct aws_allocator *allocator, void *ctx) {
334 (void)ctx;
335
336 aws_cal_library_test_init(allocator);
337
338 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("123456789012345678901234567890123456789012345"
339 "67890123456789012345678901234567890");
340
341 struct aws_hash *hash = aws_md5_new(allocator);
342 ASSERT_NOT_NULL(hash);
343
344 uint8_t output[AWS_MD5_LEN] = {0};
345 struct aws_byte_buf output_buf = aws_byte_buf_from_array(output, sizeof(output));
346 output_buf.len = 0;
347
348 ASSERT_SUCCESS(aws_hash_update(hash, &input));
349 ASSERT_SUCCESS(aws_hash_finalize(hash, &output_buf, 0));
350 ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_hash_update(hash, &input));
351 ASSERT_ERROR(AWS_ERROR_INVALID_STATE, aws_hash_finalize(hash, &output_buf, 0));
352
353 aws_hash_destroy(hash);
354
355 aws_cal_library_clean_up();
356
357 return AWS_OP_SUCCESS;
358 }
359 AWS_TEST_CASE(md5_test_invalid_state, s_md5_test_invalid_state_fn)
360
361 static int s_md5_test_extra_buffer_space_fn(struct aws_allocator *allocator, void *ctx) {
362 (void)ctx;
363
364 aws_cal_library_test_init(allocator);
365
366 struct aws_byte_cursor input = aws_byte_cursor_from_c_str("123456789012345678901234567890123456789012345"
367 "67890123456789012345678901234567890");
368
369 struct aws_byte_buf digest_size_buf;
370 struct aws_byte_buf super_size_buf;
371
372 aws_byte_buf_init(&digest_size_buf, allocator, AWS_MD5_LEN);
373 aws_byte_buf_init(&super_size_buf, allocator, AWS_MD5_LEN + 100);
374
375 aws_md5_compute(allocator, &input, &digest_size_buf, 0);
376 aws_md5_compute(allocator, &input, &super_size_buf, 0);
377
378 ASSERT_TRUE(aws_byte_buf_eq(&digest_size_buf, &super_size_buf));
379 ASSERT_TRUE(super_size_buf.len == AWS_MD5_LEN);
380
381 aws_byte_buf_clean_up(&digest_size_buf);
382 aws_byte_buf_clean_up(&super_size_buf);
383
384 aws_cal_library_clean_up();
385
386 return AWS_OP_SUCCESS;
387 }
388 AWS_TEST_CASE(md5_test_extra_buffer_space, s_md5_test_extra_buffer_space_fn)