| 1 |
#include "aws/s3/private/s3_checksums.h" |
| 2 |
#include "aws/s3/private/s3_util.h" |
| 3 |
#include <aws/cal/hash.h> |
| 4 |
#include <aws/checksums/crc.h> |
| 5 |
#include <aws/checksums/xxhash.h> |
| 6 |
#include <aws/http/request_response.h> |
| 7 |
|
| 8 |
#define AWS_CRC32_LEN sizeof(uint32_t) |
| 9 |
#define AWS_CRC32C_LEN sizeof(uint32_t) |
| 10 |
#define AWS_CRC64_LEN sizeof(uint64_t) |
| 11 |
|
| 12 |
enum { |
| 13 |
AWS_XXHASH64_LEN = 8, |
| 14 |
AWS_XXHASH3_64_LEN = 8, |
| 15 |
AWS_XXHASH3_128_LEN = 16, |
| 16 |
}; |
| 17 |
|
| 18 |
static const struct aws_byte_cursor s_crc64nvme_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("CRC64NVME"); |
| 19 |
static const struct aws_byte_cursor s_crc32c_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("CRC32C"); |
| 20 |
static const struct aws_byte_cursor s_crc32_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("CRC32"); |
| 21 |
static const struct aws_byte_cursor s_sha1_algorithm_value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("SHA1"); |
| 22 |
static 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"); |
| 27 |
|
| 28 |
static const struct aws_byte_cursor s_crc64nvme_header_name = |
| 29 |
AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-checksum-crc64nvme"); |
| 30 |
static const struct aws_byte_cursor s_crc32c_header_name = |
| 31 |
AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-checksum-crc32c"); |
| 32 |
static const struct aws_byte_cursor s_crc32_header_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-checksum-crc32"); |
| 33 |
static const struct aws_byte_cursor s_sha1_header_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-checksum-sha1"); |
| 34 |
static const struct aws_byte_cursor s_sha256_header_name = |
| 35 |
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"); |
| 44 |
|
| 45 |
static const struct aws_byte_cursor s_crc64nvme_completed_part_name = |
| 46 |
AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("ChecksumCRC64NVME"); |
| 47 |
static const struct aws_byte_cursor s_crc32c_completed_part_name = |
| 48 |
AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("ChecksumCRC32C"); |
| 49 |
static const struct aws_byte_cursor s_crc32_completed_part_name = |
| 50 |
AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("ChecksumCRC32"); |
| 51 |
static const struct aws_byte_cursor s_sha1_completed_part_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("ChecksumSHA1"); |
| 52 |
static const struct aws_byte_cursor s_sha256_completed_part_name = |
| 53 |
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"); |
| 62 |
static const struct aws_byte_cursor s_empty_cursor = { |
| 63 |
.len = 0, |
| 64 |
.ptr = NULL, |
| 65 |
}; |
| 66 |
|
| 67 |
size_t aws_get_digest_size_from_checksum_algorithm(enum aws_s3_checksum_algorithm algorithm) { |
| 68 |
switch (algorithm) { |
| 69 |
case AWS_SCA_CRC64NVME: |
| 70 |
return AWS_CRC64_LEN; |
| 71 |
case AWS_SCA_CRC32C: |
| 72 |
return AWS_CRC32C_LEN; |
| 73 |
case AWS_SCA_CRC32: |
| 74 |
return AWS_CRC32_LEN; |
| 75 |
case AWS_SCA_SHA1: |
| 76 |
return AWS_SHA1_LEN; |
| 77 |
case AWS_SCA_SHA256: |
| 78 |
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; |
| 87 |
default: |
| 88 |
return 0; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
struct aws_byte_cursor aws_get_http_header_name_from_checksum_algorithm(enum aws_s3_checksum_algorithm algorithm) { |
| 93 |
switch (algorithm) { |
| 94 |
case AWS_SCA_CRC64NVME: |
| 95 |
return s_crc64nvme_header_name; |
| 96 |
case AWS_SCA_CRC32C: |
| 97 |
return s_crc32c_header_name; |
| 98 |
case AWS_SCA_CRC32: |
| 99 |
return s_crc32_header_name; |
| 100 |
case AWS_SCA_SHA1: |
| 101 |
return s_sha1_header_name; |
| 102 |
case AWS_SCA_SHA256: |
| 103 |
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; |
| 112 |
default: |
| 113 |
return s_empty_cursor; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
struct aws_byte_cursor aws_get_checksum_algorithm_name(enum aws_s3_checksum_algorithm algorithm) { |
| 118 |
switch (algorithm) { |
| 119 |
case AWS_SCA_CRC64NVME: |
| 120 |
return s_crc64nvme_algorithm_value; |
| 121 |
case AWS_SCA_CRC32C: |
| 122 |
return s_crc32c_algorithm_value; |
| 123 |
case AWS_SCA_CRC32: |
| 124 |
return s_crc32_algorithm_value; |
| 125 |
case AWS_SCA_SHA1: |
| 126 |
return s_sha1_algorithm_value; |
| 127 |
case AWS_SCA_SHA256: |
| 128 |
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; |
| 137 |
default: |
| 138 |
return s_empty_cursor; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
struct aws_byte_cursor aws_get_completed_part_name_from_checksum_algorithm(enum aws_s3_checksum_algorithm algorithm) { |
| 143 |
switch (algorithm) { |
| 144 |
case AWS_SCA_CRC64NVME: |
| 145 |
return s_crc64nvme_completed_part_name; |
| 146 |
case AWS_SCA_CRC32C: |
| 147 |
return s_crc32c_completed_part_name; |
| 148 |
case AWS_SCA_CRC32: |
| 149 |
return s_crc32_completed_part_name; |
| 150 |
case AWS_SCA_SHA1: |
| 151 |
return s_sha1_completed_part_name; |
| 152 |
case AWS_SCA_SHA256: |
| 153 |
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; |
| 162 |
default: |
| 163 |
return s_empty_cursor; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
static void s_hash_destroy(struct aws_s3_checksum *checksum) { |
| 168 |
struct aws_hash *hash = checksum->impl.hash; |
| 169 |
aws_hash_destroy(hash); |
| 170 |
aws_mem_release(checksum->allocator, checksum); |
| 171 |
} |
| 172 |
|
| 173 |
static int s_hash_update(struct aws_s3_checksum *checksum, const struct aws_byte_cursor *to_checksum) { |
| 174 |
return aws_hash_update(checksum->impl.hash, to_checksum); |
| 175 |
} |
| 176 |
|
| 177 |
static int s_hash_finalize(struct aws_s3_checksum *checksum, struct aws_byte_buf *output) { |
| 178 |
checksum->good = false; |
| 179 |
return aws_hash_finalize(checksum->impl.hash, output, 0); |
| 180 |
} |
| 181 |
|
| 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 |
|
| 197 |
static int s_crc_finalize_helper(struct aws_s3_checksum *checksum, struct aws_byte_buf *out) { |
| 198 |
AWS_PRECONDITION(aws_byte_buf_is_valid(out)); |
| 199 |
|
| 200 |
if (!checksum->good) { |
| 201 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 202 |
} |
| 203 |
checksum->good = false; |
| 204 |
size_t len = checksum->digest_size; |
| 205 |
if (out->capacity - out->len < len) { |
| 206 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 207 |
} |
| 208 |
if (checksum->digest_size == AWS_CRC32_LEN) { |
| 209 |
if (aws_byte_buf_write_be32(out, checksum->impl.crc_val_32bit)) { |
| 210 |
return AWS_OP_SUCCESS; |
| 211 |
} |
| 212 |
} else { |
| 213 |
if (aws_byte_buf_write_be64(out, checksum->impl.crc_val_64bit)) { |
| 214 |
return AWS_OP_SUCCESS; |
| 215 |
} |
| 216 |
} |
| 217 |
return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE); |
| 218 |
} |
| 219 |
|
| 220 |
static int s_crc32_finalize(struct aws_s3_checksum *checksum, struct aws_byte_buf *out) { |
| 221 |
return s_crc_finalize_helper(checksum, out); |
| 222 |
} |
| 223 |
|
| 224 |
static int s_crc64_finalize(struct aws_s3_checksum *checksum, struct aws_byte_buf *out) { |
| 225 |
return s_crc_finalize_helper(checksum, out); |
| 226 |
} |
| 227 |
|
| 228 |
static int s_crc32_checksum_update(struct aws_s3_checksum *checksum, const struct aws_byte_cursor *buf) { |
| 229 |
if (!checksum->good) { |
| 230 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 231 |
} |
| 232 |
checksum->impl.crc_val_32bit = aws_checksums_crc32_ex(buf->ptr, buf->len, checksum->impl.crc_val_32bit); |
| 233 |
return AWS_OP_SUCCESS; |
| 234 |
} |
| 235 |
|
| 236 |
static int s_crc32c_checksum_update(struct aws_s3_checksum *checksum, const struct aws_byte_cursor *buf) { |
| 237 |
if (!checksum->good) { |
| 238 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 239 |
} |
| 240 |
checksum->impl.crc_val_32bit = aws_checksums_crc32c_ex(buf->ptr, buf->len, checksum->impl.crc_val_32bit); |
| 241 |
return AWS_OP_SUCCESS; |
| 242 |
} |
| 243 |
|
| 244 |
static int s_crc64nvme_checksum_update(struct aws_s3_checksum *checksum, const struct aws_byte_cursor *buf) { |
| 245 |
if (!checksum->good) { |
| 246 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 247 |
} |
| 248 |
checksum->impl.crc_val_64bit = aws_checksums_crc64nvme_ex(buf->ptr, buf->len, checksum->impl.crc_val_64bit); |
| 249 |
return AWS_OP_SUCCESS; |
| 250 |
} |
| 251 |
|
| 252 |
static void s_crc_destroy(struct aws_s3_checksum *checksum) { |
| 253 |
aws_mem_release(checksum->allocator, checksum); |
| 254 |
} |
| 255 |
|
| 256 |
static struct aws_checksum_vtable hash_vtable = { |
| 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, |
| 266 |
}; |
| 267 |
|
| 268 |
static struct aws_checksum_vtable crc32_vtable = { |
| 269 |
.update = s_crc32_checksum_update, |
| 270 |
.finalize = s_crc32_finalize, |
| 271 |
.destroy = s_crc_destroy, |
| 272 |
}; |
| 273 |
static struct aws_checksum_vtable crc32c_vtable = { |
| 274 |
.update = s_crc32c_checksum_update, |
| 275 |
.finalize = s_crc32_finalize, |
| 276 |
.destroy = s_crc_destroy, |
| 277 |
}; |
| 278 |
static struct aws_checksum_vtable crc64nvme_vtable = { |
| 279 |
.update = s_crc64nvme_checksum_update, |
| 280 |
.finalize = s_crc64_finalize, |
| 281 |
.destroy = s_crc_destroy, |
| 282 |
}; |
| 283 |
|
| 284 |
struct aws_s3_checksum *aws_hash_new(struct aws_allocator *allocator, aws_hash_new_fn hash_fn) { |
| 285 |
struct aws_s3_checksum *checksum = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_checksum)); |
| 286 |
struct aws_hash *hash = hash_fn(allocator); |
| 287 |
if (!hash) { |
| 288 |
aws_mem_release(allocator, checksum); |
| 289 |
aws_raise_error(aws_last_error_or_unknown()); |
| 290 |
return NULL; |
| 291 |
} |
| 292 |
checksum->impl.hash = hash; |
| 293 |
checksum->allocator = allocator; |
| 294 |
checksum->vtable = &hash_vtable; |
| 295 |
checksum->good = true; |
| 296 |
checksum->digest_size = hash->digest_size; |
| 297 |
return checksum; |
| 298 |
} |
| 299 |
|
| 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 |
|
| 321 |
static struct aws_s3_checksum *s_crc32_checksum_new(struct aws_allocator *allocator) { |
| 322 |
struct aws_s3_checksum *checksum = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_checksum)); |
| 323 |
checksum->vtable = &crc32_vtable; |
| 324 |
checksum->allocator = allocator; |
| 325 |
checksum->impl.crc_val_32bit = 0; |
| 326 |
checksum->good = true; |
| 327 |
checksum->digest_size = AWS_CRC32_LEN; |
| 328 |
|
| 329 |
return checksum; |
| 330 |
} |
| 331 |
|
| 332 |
static struct aws_s3_checksum *s_crc32c_checksum_new(struct aws_allocator *allocator) { |
| 333 |
struct aws_s3_checksum *checksum = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_checksum)); |
| 334 |
checksum->vtable = &crc32c_vtable; |
| 335 |
checksum->allocator = allocator; |
| 336 |
checksum->impl.crc_val_32bit = 0; |
| 337 |
checksum->good = true; |
| 338 |
checksum->digest_size = AWS_CRC32C_LEN; |
| 339 |
return checksum; |
| 340 |
} |
| 341 |
|
| 342 |
static struct aws_s3_checksum *s_crc64nvme_checksum_new(struct aws_allocator *allocator) { |
| 343 |
struct aws_s3_checksum *checksum = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_checksum)); |
| 344 |
checksum->vtable = &crc64nvme_vtable; |
| 345 |
checksum->allocator = allocator; |
| 346 |
checksum->impl.crc_val_64bit = 0; |
| 347 |
checksum->good = true; |
| 348 |
checksum->digest_size = AWS_CRC64_LEN; |
| 349 |
return checksum; |
| 350 |
} |
| 351 |
|
| 352 |
struct aws_s3_checksum *aws_checksum_new(struct aws_allocator *allocator, enum aws_s3_checksum_algorithm algorithm) { |
| 353 |
struct aws_s3_checksum *checksum = NULL; |
| 354 |
switch (algorithm) { |
| 355 |
case AWS_SCA_CRC64NVME: |
| 356 |
checksum = s_crc64nvme_checksum_new(allocator); |
| 357 |
break; |
| 358 |
case AWS_SCA_CRC32C: |
| 359 |
checksum = s_crc32c_checksum_new(allocator); |
| 360 |
break; |
| 361 |
case AWS_SCA_CRC32: |
| 362 |
checksum = s_crc32_checksum_new(allocator); |
| 363 |
break; |
| 364 |
case AWS_SCA_SHA1: |
| 365 |
checksum = aws_hash_new(allocator, aws_sha1_new); |
| 366 |
break; |
| 367 |
case AWS_SCA_SHA256: |
| 368 |
checksum = aws_hash_new(allocator, aws_sha256_new); |
| 369 |
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; |
| 382 |
default: |
| 383 |
return NULL; |
| 384 |
} |
| 385 |
|
| 386 |
if (checksum != NULL) { |
| 387 |
checksum->algorithm = algorithm; |
| 388 |
} |
| 389 |
return checksum; |
| 390 |
} |
| 391 |
|
| 392 |
void aws_checksum_destroy(struct aws_s3_checksum *checksum) { |
| 393 |
if (checksum != NULL) { |
| 394 |
checksum->vtable->destroy(checksum); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
int aws_checksum_update(struct aws_s3_checksum *checksum, const struct aws_byte_cursor *to_checksum) { |
| 399 |
AWS_PRECONDITION(checksum); |
| 400 |
return checksum->vtable->update(checksum, to_checksum); |
| 401 |
} |
| 402 |
|
| 403 |
int aws_checksum_finalize(struct aws_s3_checksum *checksum, struct aws_byte_buf *output) { |
| 404 |
AWS_PRECONDITION(checksum); |
| 405 |
return checksum->vtable->finalize(checksum, output); |
| 406 |
} |
| 407 |
|
| 408 |
static int s_checksum_compute_fn( |
| 409 |
struct aws_allocator *allocator, |
| 410 |
const struct aws_byte_cursor *input, |
| 411 |
struct aws_byte_buf *output, |
| 412 |
struct aws_s3_checksum *(*s_crc_new)(struct aws_allocator *)) { |
| 413 |
struct aws_s3_checksum *checksum = s_crc_new(allocator); |
| 414 |
if (aws_checksum_update(checksum, input)) { |
| 415 |
aws_checksum_destroy(checksum); |
| 416 |
return AWS_OP_ERR; |
| 417 |
} |
| 418 |
if (aws_checksum_finalize(checksum, output)) { |
| 419 |
aws_checksum_destroy(checksum); |
| 420 |
return AWS_OP_ERR; |
| 421 |
} |
| 422 |
aws_checksum_destroy(checksum); |
| 423 |
return AWS_OP_SUCCESS; |
| 424 |
} |
| 425 |
|
| 426 |
int aws_checksum_compute( |
| 427 |
struct aws_allocator *allocator, |
| 428 |
enum aws_s3_checksum_algorithm algorithm, |
| 429 |
const struct aws_byte_cursor *input, |
| 430 |
struct aws_byte_buf *output) { |
| 431 |
|
| 432 |
switch (algorithm) { |
| 433 |
case AWS_SCA_SHA1: |
| 434 |
return aws_sha1_compute(allocator, input, output, 0); |
| 435 |
case AWS_SCA_SHA256: |
| 436 |
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); |
| 445 |
case AWS_SCA_CRC64NVME: |
| 446 |
return s_checksum_compute_fn(allocator, input, output, s_crc64nvme_checksum_new); |
| 447 |
case AWS_SCA_CRC32: |
| 448 |
return s_checksum_compute_fn(allocator, input, output, s_crc32_checksum_new); |
| 449 |
case AWS_SCA_CRC32C: |
| 450 |
return s_checksum_compute_fn(allocator, input, output, s_crc32c_checksum_new); |
| 451 |
default: |
| 452 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
static void s_byte_buf_to_upper(struct aws_byte_buf *buf) { |
| 457 |
AWS_PRECONDITION(buf); |
| 458 |
|
| 459 |
for (size_t i = 0; i < buf->len; ++i) { |
| 460 |
if (buf->buffer[i] >= 'a' && buf->buffer[i] <= 'z') { |
| 461 |
buf->buffer[i] = buf->buffer[i] + ('A' - 'a'); |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
static int s_init_and_verify_checksum_config_from_headers( |
| 467 |
struct aws_s3_meta_request_checksum_config_storage *checksum_config, |
| 468 |
const struct aws_http_message *message, |
| 469 |
const void *log_id) { |
| 470 |
|
| 471 |
struct aws_http_headers *headers = aws_http_message_get_headers(message); |
| 472 |
enum aws_s3_checksum_algorithm header_algo = AWS_SCA_NONE; |
| 473 |
struct aws_byte_cursor header_value; |
| 474 |
AWS_ZERO_STRUCT(header_value); |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
bool has_checksum_value_header = false; |
| 483 |
struct aws_byte_cursor checksum_header_name; |
| 484 |
for (size_t i = 0; i < aws_http_headers_count(headers); ++i) { |
| 485 |
struct aws_http_header header; |
| 486 |
if (aws_http_headers_get_index(headers, i, &header)) { |
| 487 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 488 |
} |
| 489 |
|
| 490 |
if (aws_s3_is_checksum_value_header_name(header.name)) { |
| 491 |
checksum_header_name = header.name; |
| 492 |
has_checksum_value_header = true; |
| 493 |
break; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
if (has_checksum_value_header) { |
| 498 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_checksum_algo_priority_list); i++) { |
| 499 |
enum aws_s3_checksum_algorithm algorithm = s_checksum_algo_priority_list[i]; |
| 500 |
const struct aws_byte_cursor algorithm_header_name = |
| 501 |
aws_get_http_header_name_from_checksum_algorithm(algorithm); |
| 502 |
if (aws_http_headers_get(headers, algorithm_header_name, &header_value) == AWS_OP_SUCCESS) { |
| 503 |
if (header_algo == AWS_SCA_NONE) { |
| 504 |
header_algo = algorithm; |
| 505 |
} else { |
| 506 |
|
| 507 |
AWS_LOGF_ERROR( |
| 508 |
AWS_LS_S3_META_REQUEST, |
| 509 |
"id=%p Could not create auto-ranged-put meta request; multiple checksum headers has been set", |
| 510 |
log_id); |
| 511 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
if (!has_checksum_value_header) { |
| 518 |
|
| 519 |
return AWS_OP_SUCCESS; |
| 520 |
} |
| 521 |
|
| 522 |
if (header_algo == AWS_SCA_NONE) { |
| 523 |
header_algo = AWS_SCA_UNKNOWN; |
| 524 |
} |
| 525 |
|
| 526 |
if (checksum_config->has_full_object_checksum) { |
| 527 |
|
| 528 |
AWS_LOGF_ERROR( |
| 529 |
AWS_LS_S3_META_REQUEST, |
| 530 |
"id=%p: Could not create auto-ranged-put meta request; full object checksum is set from multiple ways.", |
| 531 |
log_id); |
| 532 |
|
| 533 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 534 |
} |
| 535 |
|
| 536 |
AWS_LOGF_DEBUG( |
| 537 |
AWS_LS_S3_META_REQUEST, |
| 538 |
"id=%p Setting the full-object checksum from header; algorithm: " PRInSTR ", value: " PRInSTR ".", |
| 539 |
log_id, |
| 540 |
AWS_BYTE_CURSOR_PRI(aws_get_checksum_algorithm_name(header_algo)), |
| 541 |
AWS_BYTE_CURSOR_PRI(header_value)); |
| 542 |
|
| 543 |
checksum_config->checksum_algorithm = header_algo; |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
checksum_config->location = AWS_SCL_NONE; |
| 550 |
|
| 551 |
if (header_algo == AWS_SCA_UNKNOWN) { |
| 552 |
aws_byte_cursor_advance(&checksum_header_name, sizeof("x-amz-checksum-") - 1); |
| 553 |
aws_byte_buf_init_copy_from_cursor( |
| 554 |
&checksum_config->unknown_checksum_algo, checksum_config->allocator, checksum_header_name); |
| 555 |
s_byte_buf_to_upper(&checksum_config->unknown_checksum_algo); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
aws_byte_buf_init_copy_from_cursor( |
| 560 |
&checksum_config->full_object_checksum, checksum_config->allocator, header_value); |
| 561 |
checksum_config->has_full_object_checksum = true; |
| 562 |
return AWS_OP_SUCCESS; |
| 563 |
} |
| 564 |
|
| 565 |
int aws_s3_meta_request_checksum_config_storage_init( |
| 566 |
struct aws_allocator *allocator, |
| 567 |
struct aws_s3_meta_request_checksum_config_storage *internal_config, |
| 568 |
const struct aws_s3_checksum_config *config, |
| 569 |
const struct aws_http_message *message, |
| 570 |
const void *log_id) { |
| 571 |
AWS_ZERO_STRUCT(*internal_config); |
| 572 |
|
| 573 |
internal_config->allocator = allocator; |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
if (!config) { |
| 578 |
return AWS_OP_SUCCESS; |
| 579 |
} |
| 580 |
|
| 581 |
struct aws_http_headers *headers = aws_http_message_get_headers(message); |
| 582 |
if (config->location == AWS_SCL_TRAILER) { |
| 583 |
struct aws_byte_cursor existing_encoding; |
| 584 |
AWS_ZERO_STRUCT(existing_encoding); |
| 585 |
if (aws_http_headers_get(headers, g_content_encoding_header_name, &existing_encoding) == AWS_OP_SUCCESS) { |
| 586 |
if (aws_byte_cursor_find_exact(&existing_encoding, &g_content_encoding_header_aws_chunked, NULL) == |
| 587 |
AWS_OP_SUCCESS) { |
| 588 |
AWS_LOGF_ERROR( |
| 589 |
AWS_LS_S3_META_REQUEST, |
| 590 |
"id=%p Cannot create meta s3 request; for trailer checksum, the original request cannot be " |
| 591 |
"aws-chunked encoding. The client will encode the request instead.", |
| 592 |
(void *)log_id); |
| 593 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 594 |
return AWS_OP_ERR; |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
if (config->location != AWS_SCL_NONE && config->checksum_algorithm == AWS_SCA_NONE) { |
| 599 |
AWS_LOGF_ERROR( |
| 600 |
AWS_LS_S3_META_REQUEST, |
| 601 |
"id=%p Cannot create meta s3 request; checksum location is set, but no checksum algorithm selected.", |
| 602 |
(void *)log_id); |
| 603 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 604 |
return AWS_OP_ERR; |
| 605 |
} |
| 606 |
|
| 607 |
internal_config->checksum_algorithm = config->checksum_algorithm; |
| 608 |
internal_config->location = config->location; |
| 609 |
internal_config->validate_response_checksum = config->validate_response_checksum; |
| 610 |
|
| 611 |
internal_config->full_object_checksum_callback = config->full_object_checksum_callback; |
| 612 |
internal_config->user_data = config->user_data; |
| 613 |
if (internal_config->full_object_checksum_callback) { |
| 614 |
|
| 615 |
internal_config->has_full_object_checksum = true; |
| 616 |
} |
| 617 |
|
| 618 |
if (config->validate_checksum_algorithms) { |
| 619 |
const size_t count = aws_array_list_length(config->validate_checksum_algorithms); |
| 620 |
for (size_t i = 0; i < count; ++i) { |
| 621 |
enum aws_s3_checksum_algorithm algorithm; |
| 622 |
aws_array_list_get_at(config->validate_checksum_algorithms, &algorithm, i); |
| 623 |
switch (algorithm) { |
| 624 |
case AWS_SCA_CRC64NVME: |
| 625 |
internal_config->response_checksum_algorithms.crc64nvme = true; |
| 626 |
break; |
| 627 |
case AWS_SCA_CRC32C: |
| 628 |
internal_config->response_checksum_algorithms.crc32c = true; |
| 629 |
break; |
| 630 |
case AWS_SCA_CRC32: |
| 631 |
internal_config->response_checksum_algorithms.crc32 = true; |
| 632 |
break; |
| 633 |
case AWS_SCA_SHA1: |
| 634 |
internal_config->response_checksum_algorithms.sha1 = true; |
| 635 |
break; |
| 636 |
case AWS_SCA_SHA256: |
| 637 |
internal_config->response_checksum_algorithms.sha256 = true; |
| 638 |
break; |
| 639 |
case AWS_SCA_SHA512: |
| 640 |
internal_config->response_checksum_algorithms.sha512 = true; |
| 641 |
break; |
| 642 |
case AWS_SCA_XXHASH64: |
| 643 |
internal_config->response_checksum_algorithms.xxhash64 = true; |
| 644 |
break; |
| 645 |
case AWS_SCA_XXHASH3_64: |
| 646 |
internal_config->response_checksum_algorithms.xxhash3_64 = true; |
| 647 |
break; |
| 648 |
case AWS_SCA_XXHASH3_128: |
| 649 |
internal_config->response_checksum_algorithms.xxhash3_128 = true; |
| 650 |
break; |
| 651 |
default: |
| 652 |
break; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
} else if (config->validate_response_checksum) { |
| 657 |
internal_config->response_checksum_algorithms.crc64nvme = true; |
| 658 |
internal_config->response_checksum_algorithms.crc32 = true; |
| 659 |
internal_config->response_checksum_algorithms.crc32c = true; |
| 660 |
internal_config->response_checksum_algorithms.sha1 = true; |
| 661 |
internal_config->response_checksum_algorithms.sha256 = true; |
| 662 |
internal_config->response_checksum_algorithms.sha512 = true; |
| 663 |
internal_config->response_checksum_algorithms.xxhash64 = true; |
| 664 |
internal_config->response_checksum_algorithms.xxhash3_64 = true; |
| 665 |
internal_config->response_checksum_algorithms.xxhash3_128 = true; |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
if (s_init_and_verify_checksum_config_from_headers(internal_config, message, log_id)) { |
| 670 |
return AWS_OP_ERR; |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
return AWS_OP_SUCCESS; |
| 675 |
} |
| 676 |
|
| 677 |
void aws_s3_meta_request_checksum_config_storage_cleanup( |
| 678 |
struct aws_s3_meta_request_checksum_config_storage *internal_config) { |
| 679 |
if (internal_config->has_full_object_checksum) { |
| 680 |
aws_byte_buf_clean_up(&internal_config->full_object_checksum); |
| 681 |
} |
| 682 |
aws_byte_buf_clean_up(&internal_config->unknown_checksum_algo); |
| 683 |
} |