Debian Codegraph @

crt/aws-c-cal/tests/test_case_helper.h

raw symbols SCIP
1 #ifndef AWS_CAL_TEST_CASE_HELPER_H
2 #define AWS_CAL_TEST_CASE_HELPER_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7 #include <aws/cal/cal.h>
8 #include <aws/cal/hash.h>
9 #include <aws/cal/hmac.h>
10 #include <aws/common/environment.h>
11 #include <aws/common/string.h>
12
13 #if !defined(BYO_CRYPTO) && !defined(AWS_OS_APPLE) && !defined(AWS_OS_WINDOWS)
14 # include <openssl/crypto.h>
15 #endif
16
17 /**
18 * If $AWS_TEST_FIPS env-var is set, turn on FIPS mode.
19 * Then do normal aws_cal_library_init()
20 */
21 static inline void aws_cal_library_test_init(struct aws_allocator *allocator) {
22 struct aws_string *key_AWS_TEST_FIPS = aws_string_new_from_c_str(allocator, "AWS_TEST_FIPS");
23 struct aws_string *val_AWS_TEST_FIPS = NULL;
24 aws_get_environment_value(allocator, key_AWS_TEST_FIPS, &val_AWS_TEST_FIPS);
25 bool is_fips_desired = val_AWS_TEST_FIPS != NULL;
26 aws_string_destroy(key_AWS_TEST_FIPS);
27 aws_string_destroy(val_AWS_TEST_FIPS);
28
29 if (is_fips_desired) {
30 #if defined(OPENSSL_IS_AWSLC)
31 AWS_FATAL_ASSERT(FIPS_mode_set(1) == 1 && "FIPS_mode_set(1) must succeed");
32 #else
33 AWS_FATAL_ASSERT(!is_fips_desired && "AWS_TEST_FIPS is currently only supported with AWS-LC");
34 #endif
35 }
36
37 aws_cal_library_init(allocator);
38 }
39
40 static inline int s_verify_hmac_test_case(
41 struct aws_allocator *allocator,
42 struct aws_byte_cursor *input,
43 struct aws_byte_cursor *secret,
44 struct aws_byte_cursor *expected,
45 aws_hmac_new_fn *new_fn) {
46
47 aws_cal_library_test_init(allocator);
48
49 /* test all possible segmentation lengths from 1 byte at a time to the entire
50 * input. Using a do-while so that we still do 1 pass on 0-length input */
51 size_t advance_i = 1;
52 do {
53 uint8_t output[128] = {0};
54 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, AWS_ARRAY_SIZE(output));
55
56 struct aws_hmac *hmac = new_fn(allocator, secret);
57 ASSERT_NOT_NULL(hmac);
58
59 struct aws_byte_cursor input_cpy = *input;
60
61 while (input_cpy.len) {
62 size_t max_advance = aws_min_size(input_cpy.len, advance_i);
63 struct aws_byte_cursor segment = aws_byte_cursor_from_array(input_cpy.ptr, max_advance);
64 ASSERT_SUCCESS(aws_hmac_update(hmac, &segment));
65 aws_byte_cursor_advance(&input_cpy, max_advance);
66 }
67
68 size_t truncation_size = expected->len;
69
70 ASSERT_SUCCESS(aws_hmac_finalize(hmac, &output_buf, truncation_size));
71 ASSERT_BIN_ARRAYS_EQUALS(expected->ptr, expected->len, output_buf.buffer, output_buf.len);
72
73 aws_hmac_destroy(hmac);
74 } while (++advance_i <= input->len);
75
76 aws_cal_library_clean_up();
77
78 return AWS_OP_SUCCESS;
79 }
80
81 static inline int s_verify_hash_test_case(
82 struct aws_allocator *allocator,
83 struct aws_byte_cursor *input,
84 struct aws_byte_cursor *expected,
85 aws_hash_new_fn *new_fn) {
86
87 aws_cal_library_test_init(allocator);
88
89 /* test all possible segmentation lengths from 1 byte at a time to the entire
90 * input. Using a do-while so that we still do 1 pass on 0-length input */
91 size_t advance_i = 1;
92 do {
93 uint8_t output[128] = {0};
94 struct aws_byte_buf output_buf = aws_byte_buf_from_empty_array(output, AWS_ARRAY_SIZE(output));
95
96 struct aws_hash *hash = new_fn(allocator);
97 ASSERT_NOT_NULL(hash);
98
99 struct aws_byte_cursor input_cpy = *input;
100
101 while (input_cpy.len) {
102 size_t max_advance = aws_min_size(input_cpy.len, advance_i);
103 struct aws_byte_cursor segment = aws_byte_cursor_from_array(input_cpy.ptr, max_advance);
104 ASSERT_SUCCESS(aws_hash_update(hash, &segment));
105 aws_byte_cursor_advance(&input_cpy, max_advance);
106 }
107
108 size_t truncation_size = expected->len;
109
110 ASSERT_SUCCESS(aws_hash_finalize(hash, &output_buf, truncation_size));
111 ASSERT_BIN_ARRAYS_EQUALS(expected->ptr, expected->len, output_buf.buffer, output_buf.len);
112
113 aws_hash_destroy(hash);
114 } while (++advance_i <= input->len);
115
116 aws_cal_library_clean_up();
117
118 return AWS_OP_SUCCESS;
119 }
120
121 #endif /*AWS_CAL_TEST_CASE_HELPER_H*/