Debian Codegraph @

crt/aws-c-s3/include/aws/s3/private/s3_checksums.h

raw symbols SCIP
1 #ifndef AWS_S3_CHECKSUMS_H
2 #define AWS_S3_CHECKSUMS_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7 #include "aws/s3/s3_client.h"
8
9 /* TODO: consider moving the aws_checksum_stream to aws-c-checksum, and the rest about checksum headers and trailer to
10 * aws-c-sdkutil. */
11
12 struct aws_s3_checksum;
13 struct aws_s3_upload_request_checksum_context;
14
15 /* List to check the checksum algorithm to use based on the priority. */
16 static const enum aws_s3_checksum_algorithm s_checksum_algo_priority_list[] = {
17 AWS_SCA_XXHASH3_128,
18 AWS_SCA_XXHASH3_64,
19 AWS_SCA_CRC64NVME,
20 AWS_SCA_CRC32C,
21 AWS_SCA_CRC32,
22 AWS_SCA_XXHASH64,
23 AWS_SCA_SHA512,
24 AWS_SCA_SHA256,
25 AWS_SCA_SHA1,
26 };
27 AWS_STATIC_ASSERT(AWS_ARRAY_SIZE(s_checksum_algo_priority_list) == (AWS_SCA_END - AWS_SCA_INIT + 1));
28
29 struct aws_checksum_vtable {
30 void (*destroy)(struct aws_s3_checksum *checksum);
31 int (*update)(struct aws_s3_checksum *checksum, const struct aws_byte_cursor *buf);
32 int (*finalize)(struct aws_s3_checksum *checksum, struct aws_byte_buf *out);
33 };
34
35 struct aws_s3_checksum {
36 struct aws_allocator *allocator;
37 struct aws_checksum_vtable *vtable;
38 size_t digest_size;
39 enum aws_s3_checksum_algorithm algorithm;
40 bool good;
41 union {
42 struct aws_hash *hash;
43 struct aws_xxhash *xxhash;
44 uint32_t crc_val_32bit;
45 uint64_t crc_val_64bit;
46 } impl;
47 };
48
49 struct aws_s3_meta_request_checksum_config_storage {
50 struct aws_allocator *allocator;
51 struct aws_byte_buf full_object_checksum;
52 bool has_full_object_checksum;
53
54 aws_s3_meta_request_full_object_checksum_fn *full_object_checksum_callback;
55 void *user_data;
56
57 enum aws_s3_checksum_location location;
58 enum aws_s3_checksum_algorithm checksum_algorithm;
59 struct aws_byte_buf unknown_checksum_algo; /* if checksum algo is unknown this will have checksum name */
60 bool validate_response_checksum;
61 struct {
62 bool crc64nvme;
63 bool crc32c;
64 bool crc32;
65 bool sha1;
66 bool sha256;
67 bool sha512;
68 bool xxhash64;
69 bool xxhash3_64;
70 bool xxhash3_128;
71 } response_checksum_algorithms;
72 };
73
74 /**
75 * Helper stream that takes in a stream and the checksum context to help finalize the checksum from the underlying
76 * stream.
77 * The context will be only finalized when the checksum stream has read to the end of stream.
78 *
79 * Note: seek this stream will immediately fail, as it would prevent an accurate calculation of the
80 * checksum.
81 *
82 * @param allocator
83 * @param existing_stream The real content to read from. Destroying the checksum stream destroys the existing stream.
84 * outputs the checksum of existing stream to checksum_output upon destruction. Will be kept
85 * alive by the checksum stream
86 * @param context Checksum context to keep and get checksum requirements from.
87 */
88 AWS_S3_API
89 struct aws_input_stream *aws_checksum_stream_new_with_context(
90 struct aws_allocator *allocator,
91 struct aws_input_stream *existing_stream,
92 struct aws_s3_upload_request_checksum_context *context);
93
94 /**
95 * Helper stream that takes in a stream to keep track of the checksum of the underlying stream during read.
96 * Invoke `aws_checksum_stream_finalize_checksum` to get the checksum of the data has been read so far.
97 *
98 * Note: seek this stream will immediately fail, as it would prevent an accurate calculation of the
99 * checksum.
100 *
101 * @param allocator
102 * @param existing_stream The real content to read from. Destroying the checksum stream destroys the existing stream.
103 * outputs the checksum of existing stream to checksum_output upon destruction. Will be kept
104 * alive by the checksum stream
105 * @param algorithm Checksum algorithm to use.
106 */
107 AWS_S3_API
108 struct aws_input_stream *aws_checksum_stream_new(
109 struct aws_allocator *allocator,
110 struct aws_input_stream *existing_stream,
111 enum aws_s3_checksum_algorithm algorithm);
112
113 /**
114 * Finalize the checksum has read so far to the output checksum buf with base64 encoding.
115 * Not thread safe.
116 */
117 AWS_S3_API
118 int aws_checksum_stream_finalize_checksum(struct aws_input_stream *checksum_stream, struct aws_byte_buf *checksum_buf);
119
120 /**
121 * TODO: properly support chunked encoding.
122 * Creates a chunked encoding stream that wraps an existing stream and adds checksum trailers.
123 *
124 * This function creates a stream that:
125 * 1. Encodes the input stream wraps the existing_stream with aws-chunked encoded.
126 * 2. Calculates a checksum of the stream content (if not already calculated)
127 * 3. Appends the checksum as a trailer at the end of the aws-chunked stream
128 *
129 * Note: This stream does not support seeking operations, as seeking would prevent
130 * accurate checksum calculation and corrupt the chunked encoding format.
131 *
132 * @param allocator Memory allocator to use for stream creation and internal buffers
133 * @param existing_stream The input stream to be chunked and checksummed. This stream
134 * will be acquired by the chunk stream and released when the
135 * chunk stream is destroyed. Must not be NULL.
136 * @param checksum_context Context containing checksum configuration and state. Must not be NULL.
137 * The context contains:
138 * - algorithm: The checksum algorithm to use (CRC32, CRC32C, etc.)
139 * - base64_checksum: Buffer for the calculated checksum result
140 * - checksum_calculated: Whether checksum is pre-calculated or needs calculation
141 * - encoded_checksum_size: Expected size of the base64-encoded checksum
142 *
143 * If checksum_calculated is false, the stream will wrap existing_stream
144 * with a checksum stream to calculate the checksum during reading.
145 * If checksum_calculated is true, the existing checksum value will be used.
146 *
147 * @return A new input stream that provides chunked encoding with checksum trailers,
148 * or NULL if creation fails. The returned stream must be released with
149 * aws_input_stream_release() when no longer needed.
150 *
151 * @note The total length of the returned stream includes:
152 * - Chunk size header (hex representation + \r\n)
153 * - Original stream content
154 * - Final chunk marker (0\r\n or \r\n0\r\n)
155 * - Checksum trailer (header name + : + base64 checksum + \r\n\r\n)
156 */
157 AWS_S3_API
158 struct aws_input_stream *aws_chunk_stream_new(
159 struct aws_allocator *allocator,
160 struct aws_input_stream *existing_stream,
161 struct aws_s3_upload_request_checksum_context *context);
162
163 /**
164 * Get the size of the checksum output corresponding to the aws_s3_checksum_algorithm enum value.
165 */
166 AWS_S3_API
167 size_t aws_get_digest_size_from_checksum_algorithm(enum aws_s3_checksum_algorithm algorithm);
168
169 /**
170 * Get header name to use for algorithm (e.g. "x-amz-checksum-crc32")
171 */
172 AWS_S3_API
173 struct aws_byte_cursor aws_get_http_header_name_from_checksum_algorithm(enum aws_s3_checksum_algorithm algorithm);
174
175 /**
176 * Get algorithm's name (e.g. "CRC32"), to be used as the value of headers like `x-amz-checksum-algorithm`
177 */
178 AWS_S3_API
179 struct aws_byte_cursor aws_get_checksum_algorithm_name(enum aws_s3_checksum_algorithm algorithm);
180
181 /**
182 * Get the name of checksum algorithm to be used as the details of the parts were uploaded. Referring to
183 * https://docs.aws.amazon.com/AmazonS3/latest/API/API_CompletedPart.html#AmazonS3-Type-CompletedPart
184 */
185 AWS_S3_API
186 struct aws_byte_cursor aws_get_completed_part_name_from_checksum_algorithm(enum aws_s3_checksum_algorithm algorithm);
187
188 /**
189 * create a new aws_checksum corresponding to the aws_s3_checksum_algorithm enum value.
190 */
191 AWS_S3_API
192 struct aws_s3_checksum *aws_checksum_new(struct aws_allocator *allocator, enum aws_s3_checksum_algorithm algorithm);
193
194 /**
195 * Compute an aws_checksum corresponding to the provided enum, passing a function pointer around instead of using a
196 * conditional would be faster, but would be a negligible improvement compared to the cost of processing data twice
197 * which would be the only time this function would be used, and would be harder to follow.
198 */
199 AWS_S3_API
200 int aws_checksum_compute(
201 struct aws_allocator *allocator,
202 enum aws_s3_checksum_algorithm algorithm,
203 const struct aws_byte_cursor *input,
204 struct aws_byte_buf *output);
205
206 /**
207 * Cleans up and deallocates checksum.
208 */
209 AWS_S3_API
210 void aws_checksum_destroy(struct aws_s3_checksum *checksum);
211
212 /**
213 * Updates the running checksum with to_checksum. this can be called multiple times.
214 */
215 AWS_S3_API
216 int aws_checksum_update(struct aws_s3_checksum *checksum, const struct aws_byte_cursor *to_checksum);
217
218 /**
219 * Completes the checksum computation and writes the final digest to output.
220 * Allocation of output is the caller's responsibility.
221 */
222 AWS_S3_API
223 int aws_checksum_finalize(struct aws_s3_checksum *checksum, struct aws_byte_buf *output);
224
225 AWS_S3_API
226 int aws_s3_meta_request_checksum_config_storage_init(
227 struct aws_allocator *allocator,
228 struct aws_s3_meta_request_checksum_config_storage *internal_config,
229 const struct aws_s3_checksum_config *config,
230 const struct aws_http_message *message,
231 const void *log_id);
232
233 AWS_S3_API
234 void aws_s3_meta_request_checksum_config_storage_cleanup(
235 struct aws_s3_meta_request_checksum_config_storage *internal_config);
236
237 #endif /* AWS_S3_CHECKSUMS_H */