22#include "aws/s3/private/s3_util.h"
33#include <aws/cal/hash.h>
44#include <aws/checksums/crc.h>
5+ #include <aws/checksums/xxhash.h>
56#include <aws/http/request_response.h>
67
78#define AWS_CRC32_LEN sizeof(uint32_t)
89#define AWS_CRC32C_LEN sizeof(uint32_t)
910#define AWS_CRC64_LEN sizeof(uint64_t)
1011
12+ enum {
13+ AWS_XXHASH64_LEN = 8 ,
14+ AWS_XXHASH3_64_LEN = 8 ,
15+ AWS_XXHASH3_128_LEN = 16 ,
16+ };
17+
1118static const struct aws_byte_cursor s_crc64nvme_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("CRC64NVME" );
1219static const struct aws_byte_cursor s_crc32c_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("CRC32C" );
1320static const struct aws_byte_cursor s_crc32_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("CRC32" );
1421static const struct aws_byte_cursor s_sha1_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("SHA1" );
1522static const struct aws_byte_cursor s_sha256_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("SHA256" );
23+ static const struct aws_byte_cursor s_sha512_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("SHA512" );
24+ static const struct aws_byte_cursor s_xxhash64_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("XXHASH64" );
25+ static const struct aws_byte_cursor s_xxhash3_64_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("XXHASH3" );
26+ static const struct aws_byte_cursor s_xxhash3_128_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("XXHASH128" );
1627
1728static const struct aws_byte_cursor s_crc64nvme_header_name =
1829 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("x-amz-checksum-crc64nvme" );
@@ -22,6 +33,14 @@ static const struct aws_byte_cursor s_crc32_header_name = AWS_BYTE_CUR_INIT_FROM
2233static const struct aws_byte_cursor s_sha1_header_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("x-amz-checksum-sha1" );
2334static const struct aws_byte_cursor s_sha256_header_name =
2435 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("x-amz-checksum-sha256" );
36+ static const struct aws_byte_cursor s_sha512_header_name =
37+ AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("x-amz-checksum-sha512" );
38+ static const struct aws_byte_cursor s_xxhash64_header_name =
39+ AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("x-amz-checksum-xxhash64" );
40+ static const struct aws_byte_cursor s_xxhash3_64_header_name =
41+ AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("x-amz-checksum-xxhash3" );
42+ static const struct aws_byte_cursor s_xxhash3_128_header_name =
43+ AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("x-amz-checksum-xxhash128" );
2544
2645static const struct aws_byte_cursor s_crc64nvme_completed_part_name =
2746 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("ChecksumCRC64NVME" );
@@ -32,6 +51,14 @@ static const struct aws_byte_cursor s_crc32_completed_part_name =
3251static const struct aws_byte_cursor s_sha1_completed_part_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("ChecksumSHA1" );
3352static const struct aws_byte_cursor s_sha256_completed_part_name =
3453 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("ChecksumSHA256" );
54+ static const struct aws_byte_cursor s_sha512_completed_part_name =
55+ AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("ChecksumSHA512" );
56+ static const struct aws_byte_cursor s_xxhash64_completed_part_name =
57+ AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("ChecksumXXHASH64" );
58+ static const struct aws_byte_cursor s_xxhash3_64_completed_part_name =
59+ AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("ChecksumXXHASH3" );
60+ static const struct aws_byte_cursor s_xxhash3_128_completed_part_name =
61+ AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL ("ChecksumXXHASH128" );
3562static const struct aws_byte_cursor s_empty_cursor = {
3663 .len = 0 ,
3764 .ptr = NULL ,
@@ -49,6 +76,14 @@ size_t aws_get_digest_size_from_checksum_algorithm(enum aws_s3_checksum_algorith
4976 return AWS_SHA1_LEN ;
5077 case AWS_SCA_SHA256 :
5178 return AWS_SHA256_LEN ;
79+ case AWS_SCA_SHA512 :
80+ return AWS_SHA512_LEN ;
81+ case AWS_SCA_XXHASH64 :
82+ return AWS_XXHASH64_LEN ;
83+ case AWS_SCA_XXHASH3_64 :
84+ return AWS_XXHASH3_64_LEN ;
85+ case AWS_SCA_XXHASH3_128 :
86+ return AWS_XXHASH3_128_LEN ;
5287 default :
5388 return 0 ;
5489 }
@@ -66,6 +101,14 @@ struct aws_byte_cursor aws_get_http_header_name_from_checksum_algorithm(enum aws
66101 return s_sha1_header_name ;
67102 case AWS_SCA_SHA256 :
68103 return s_sha256_header_name ;
104+ case AWS_SCA_SHA512 :
105+ return s_sha512_header_name ;
106+ case AWS_SCA_XXHASH64 :
107+ return s_xxhash64_header_name ;
108+ case AWS_SCA_XXHASH3_64 :
109+ return s_xxhash3_64_header_name ;
110+ case AWS_SCA_XXHASH3_128 :
111+ return s_xxhash3_128_header_name ;
69112 default :
70113 return s_empty_cursor ;
71114 }
@@ -83,6 +126,14 @@ struct aws_byte_cursor aws_get_checksum_algorithm_name(enum aws_s3_checksum_algo
83126 return s_sha1_algorithm_value ;
84127 case AWS_SCA_SHA256 :
85128 return s_sha256_algorithm_value ;
129+ case AWS_SCA_SHA512 :
130+ return s_sha512_algorithm_value ;
131+ case AWS_SCA_XXHASH64 :
132+ return s_xxhash64_algorithm_value ;
133+ case AWS_SCA_XXHASH3_64 :
134+ return s_xxhash3_64_algorithm_value ;
135+ case AWS_SCA_XXHASH3_128 :
136+ return s_xxhash3_128_algorithm_value ;
86137 default :
87138 return s_empty_cursor ;
88139 }
@@ -100,26 +151,49 @@ struct aws_byte_cursor aws_get_completed_part_name_from_checksum_algorithm(enum
100151 return s_sha1_completed_part_name ;
101152 case AWS_SCA_SHA256 :
102153 return s_sha256_completed_part_name ;
154+ case AWS_SCA_SHA512 :
155+ return s_sha512_completed_part_name ;
156+ case AWS_SCA_XXHASH64 :
157+ return s_xxhash64_completed_part_name ;
158+ case AWS_SCA_XXHASH3_64 :
159+ return s_xxhash3_64_completed_part_name ;
160+ case AWS_SCA_XXHASH3_128 :
161+ return s_xxhash3_128_completed_part_name ;
103162 default :
104163 return s_empty_cursor ;
105164 }
106165}
107166
108- void s3_hash_destroy (struct aws_s3_checksum * checksum ) {
167+ static void s_hash_destroy (struct aws_s3_checksum * checksum ) {
109168 struct aws_hash * hash = checksum -> impl .hash ;
110169 aws_hash_destroy (hash );
111170 aws_mem_release (checksum -> allocator , checksum );
112171}
113172
114- int s3_hash_update (struct aws_s3_checksum * checksum , const struct aws_byte_cursor * to_checksum ) {
173+ static int s_hash_update (struct aws_s3_checksum * checksum , const struct aws_byte_cursor * to_checksum ) {
115174 return aws_hash_update (checksum -> impl .hash , to_checksum );
116175}
117176
118- int s3_hash_finalize (struct aws_s3_checksum * checksum , struct aws_byte_buf * output ) {
177+ static int s_hash_finalize (struct aws_s3_checksum * checksum , struct aws_byte_buf * output ) {
119178 checksum -> good = false;
120179 return aws_hash_finalize (checksum -> impl .hash , output , 0 );
121180}
122181
182+ static void s_xxhash_destroy (struct aws_s3_checksum * checksum ) {
183+ struct aws_xxhash * hash = checksum -> impl .xxhash ;
184+ aws_xxhash_destroy (hash );
185+ aws_mem_release (checksum -> allocator , checksum );
186+ }
187+
188+ static int s_xxhash_update (struct aws_s3_checksum * checksum , const struct aws_byte_cursor * to_checksum ) {
189+ return aws_xxhash_update (checksum -> impl .xxhash , * to_checksum );
190+ }
191+
192+ static int s_xxhash_finalize (struct aws_s3_checksum * checksum , struct aws_byte_buf * output ) {
193+ checksum -> good = false;
194+ return aws_xxhash_finalize (checksum -> impl .xxhash , output );
195+ }
196+
123197static int s_crc_finalize_helper (struct aws_s3_checksum * checksum , struct aws_byte_buf * out ) {
124198 AWS_PRECONDITION (aws_byte_buf_is_valid (out ));
125199
@@ -180,9 +254,15 @@ static void s_crc_destroy(struct aws_s3_checksum *checksum) {
180254}
181255
182256static struct aws_checksum_vtable hash_vtable = {
183- .update = s3_hash_update ,
184- .finalize = s3_hash_finalize ,
185- .destroy = s3_hash_destroy ,
257+ .update = s_hash_update ,
258+ .finalize = s_hash_finalize ,
259+ .destroy = s_hash_destroy ,
260+ };
261+
262+ static struct aws_checksum_vtable s_xxhash_vtable = {
263+ .update = s_xxhash_update ,
264+ .finalize = s_xxhash_finalize ,
265+ .destroy = s_xxhash_destroy ,
186266};
187267
188268static struct aws_checksum_vtable crc32_vtable = {
@@ -217,6 +297,27 @@ struct aws_s3_checksum *aws_hash_new(struct aws_allocator *allocator, aws_hash_n
217297 return checksum ;
218298}
219299
300+ typedef struct aws_xxhash * (aws_xxhash_new_fn )(struct aws_allocator * allocator , uint64_t seed );
301+
302+ struct aws_s3_checksum * s_aws_xxhash_new (
303+ struct aws_allocator * allocator ,
304+ aws_xxhash_new_fn hash_fn ,
305+ size_t digest_size ) {
306+ struct aws_s3_checksum * checksum = aws_mem_calloc (allocator , 1 , sizeof (struct aws_s3_checksum ));
307+ struct aws_xxhash * hash = hash_fn (allocator , 0 );
308+ if (!hash ) {
309+ aws_mem_release (allocator , checksum );
310+ aws_raise_error (aws_last_error_or_unknown ());
311+ return NULL ;
312+ }
313+ checksum -> impl .xxhash = hash ;
314+ checksum -> allocator = allocator ;
315+ checksum -> vtable = & s_xxhash_vtable ;
316+ checksum -> good = true;
317+ checksum -> digest_size = digest_size ;
318+ return checksum ;
319+ }
320+
220321static struct aws_s3_checksum * s_crc32_checksum_new (struct aws_allocator * allocator ) {
221322 struct aws_s3_checksum * checksum = aws_mem_calloc (allocator , 1 , sizeof (struct aws_s3_checksum ));
222323 checksum -> vtable = & crc32_vtable ;
@@ -266,9 +367,22 @@ struct aws_s3_checksum *aws_checksum_new(struct aws_allocator *allocator, enum a
266367 case AWS_SCA_SHA256 :
267368 checksum = aws_hash_new (allocator , aws_sha256_new );
268369 break ;
370+ case AWS_SCA_SHA512 :
371+ checksum = aws_hash_new (allocator , aws_sha512_new );
372+ break ;
373+ case AWS_SCA_XXHASH64 :
374+ checksum = s_aws_xxhash_new (allocator , aws_xxhash64_new , AWS_XXHASH64_LEN );
375+ break ;
376+ case AWS_SCA_XXHASH3_64 :
377+ checksum = s_aws_xxhash_new (allocator , aws_xxhash3_64_new , AWS_XXHASH3_64_LEN );
378+ break ;
379+ case AWS_SCA_XXHASH3_128 :
380+ checksum = s_aws_xxhash_new (allocator , aws_xxhash3_128_new , AWS_XXHASH3_128_LEN );
381+ break ;
269382 default :
270383 return NULL ;
271384 }
385+
272386 if (checksum != NULL ) {
273387 checksum -> algorithm = algorithm ;
274388 }
@@ -320,6 +434,14 @@ int aws_checksum_compute(
320434 return aws_sha1_compute (allocator , input , output , 0 );
321435 case AWS_SCA_SHA256 :
322436 return aws_sha256_compute (allocator , input , output , 0 );
437+ case AWS_SCA_SHA512 :
438+ return aws_sha512_compute (allocator , input , output , 0 );
439+ case AWS_SCA_XXHASH64 :
440+ return aws_xxhash64_compute (0 , * input , output );
441+ case AWS_SCA_XXHASH3_64 :
442+ return aws_xxhash3_64_compute (0 , * input , output );
443+ case AWS_SCA_XXHASH3_128 :
444+ return aws_xxhash3_128_compute (0 , * input , output );
323445 case AWS_SCA_CRC64NVME :
324446 return s_checksum_compute_fn (allocator , input , output , s_crc64nvme_checksum_new );
325447 case AWS_SCA_CRC32 :
@@ -466,6 +588,18 @@ int aws_s3_meta_request_checksum_config_storage_init(
466588 case AWS_SCA_SHA256 :
467589 internal_config -> response_checksum_algorithms .sha256 = true;
468590 break ;
591+ case AWS_SCA_SHA512 :
592+ internal_config -> response_checksum_algorithms .sha512 = true;
593+ break ;
594+ case AWS_SCA_XXHASH64 :
595+ internal_config -> response_checksum_algorithms .xxhash64 = true;
596+ break ;
597+ case AWS_SCA_XXHASH3_64 :
598+ internal_config -> response_checksum_algorithms .xxhash3_64 = true;
599+ break ;
600+ case AWS_SCA_XXHASH3_128 :
601+ internal_config -> response_checksum_algorithms .xxhash3_128 = true;
602+ break ;
469603 default :
470604 break ;
471605 }
@@ -477,6 +611,10 @@ int aws_s3_meta_request_checksum_config_storage_init(
477611 internal_config -> response_checksum_algorithms .crc32c = true;
478612 internal_config -> response_checksum_algorithms .sha1 = true;
479613 internal_config -> response_checksum_algorithms .sha256 = true;
614+ internal_config -> response_checksum_algorithms .sha512 = true;
615+ internal_config -> response_checksum_algorithms .xxhash64 = true;
616+ internal_config -> response_checksum_algorithms .xxhash3_64 = true;
617+ internal_config -> response_checksum_algorithms .xxhash3_128 = true;
480618 }
481619
482620 /* After applying settings from config, check the message header to override the corresponding settings. */
0 commit comments