| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/cal/hash.h> |
| 6 |
|
| 7 |
#ifndef BYO_CRYPTO |
| 8 |
extern struct aws_hash *aws_sha512_default_new(struct aws_allocator *allocator); |
| 9 |
extern struct aws_hash *aws_sha256_default_new(struct aws_allocator *allocator); |
| 10 |
extern struct aws_hash *aws_sha1_default_new(struct aws_allocator *allocator); |
| 11 |
extern struct aws_hash *aws_md5_default_new(struct aws_allocator *allocator); |
| 12 |
|
| 13 |
static aws_hash_new_fn *s_sha512_new_fn = aws_sha512_default_new; |
| 14 |
static aws_hash_new_fn *s_sha256_new_fn = aws_sha256_default_new; |
| 15 |
static aws_hash_new_fn *s_sha1_new_fn = aws_sha1_default_new; |
| 16 |
static aws_hash_new_fn *s_md5_new_fn = aws_md5_default_new; |
| 17 |
#else |
| 18 |
static struct aws_hash *aws_hash_new_abort(struct aws_allocator *allocator) { |
| 19 |
(void)allocator; |
| 20 |
abort(); |
| 21 |
} |
| 22 |
|
| 23 |
static aws_hash_new_fn *s_sha512_new_fn = aws_hash_new_abort; |
| 24 |
static aws_hash_new_fn *s_sha256_new_fn = aws_hash_new_abort; |
| 25 |
static aws_hash_new_fn *s_sha1_new_fn = aws_hash_new_abort; |
| 26 |
static aws_hash_new_fn *s_md5_new_fn = aws_hash_new_abort; |
| 27 |
#endif |
| 28 |
|
| 29 |
struct aws_hash *aws_sha1_new(struct aws_allocator *allocator) { |
| 30 |
return s_sha1_new_fn(allocator); |
| 31 |
} |
| 32 |
|
| 33 |
struct aws_hash *aws_sha512_new(struct aws_allocator *allocator) { |
| 34 |
return s_sha512_new_fn(allocator); |
| 35 |
} |
| 36 |
|
| 37 |
struct aws_hash *aws_sha256_new(struct aws_allocator *allocator) { |
| 38 |
return s_sha256_new_fn(allocator); |
| 39 |
} |
| 40 |
|
| 41 |
struct aws_hash *aws_md5_new(struct aws_allocator *allocator) { |
| 42 |
return s_md5_new_fn(allocator); |
| 43 |
} |
| 44 |
|
| 45 |
void aws_set_md5_new_fn(aws_hash_new_fn *fn) { |
| 46 |
s_md5_new_fn = fn; |
| 47 |
} |
| 48 |
|
| 49 |
void aws_set_sha512_new_fn(aws_hash_new_fn *fn) { |
| 50 |
s_sha512_new_fn = fn; |
| 51 |
} |
| 52 |
|
| 53 |
void aws_set_sha256_new_fn(aws_hash_new_fn *fn) { |
| 54 |
s_sha256_new_fn = fn; |
| 55 |
} |
| 56 |
|
| 57 |
void aws_set_sha1_new_fn(aws_hash_new_fn *fn) { |
| 58 |
s_sha1_new_fn = fn; |
| 59 |
} |
| 60 |
|
| 61 |
void aws_hash_destroy(struct aws_hash *hash) { |
| 62 |
hash->vtable->destroy(hash); |
| 63 |
} |
| 64 |
|
| 65 |
int aws_hash_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash) { |
| 66 |
return hash->vtable->update(hash, to_hash); |
| 67 |
} |
| 68 |
|
| 69 |
int aws_hash_finalize(struct aws_hash *hash, struct aws_byte_buf *output, size_t truncate_to) { |
| 70 |
|
| 71 |
if (truncate_to && truncate_to < hash->digest_size) { |
| 72 |
size_t available_buffer = output->capacity - output->len; |
| 73 |
if (available_buffer < truncate_to) { |
| 74 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 75 |
} |
| 76 |
|
| 77 |
uint8_t tmp_output[128] = {0}; |
| 78 |
AWS_ASSERT(sizeof(tmp_output) >= hash->digest_size); |
| 79 |
|
| 80 |
struct aws_byte_buf tmp_out_buf = aws_byte_buf_from_array(tmp_output, sizeof(tmp_output)); |
| 81 |
tmp_out_buf.len = 0; |
| 82 |
|
| 83 |
if (hash->vtable->finalize(hash, &tmp_out_buf)) { |
| 84 |
return AWS_OP_ERR; |
| 85 |
} |
| 86 |
|
| 87 |
memcpy(output->buffer + output->len, tmp_output, truncate_to); |
| 88 |
output->len += truncate_to; |
| 89 |
return AWS_OP_SUCCESS; |
| 90 |
} |
| 91 |
|
| 92 |
return hash->vtable->finalize(hash, output); |
| 93 |
} |
| 94 |
|
| 95 |
static inline int compute_hash( |
| 96 |
struct aws_hash *hash, |
| 97 |
const struct aws_byte_cursor *input, |
| 98 |
struct aws_byte_buf *output, |
| 99 |
size_t truncate_to) { |
| 100 |
if (!hash) { |
| 101 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 102 |
} |
| 103 |
|
| 104 |
if (aws_hash_update(hash, input)) { |
| 105 |
aws_hash_destroy(hash); |
| 106 |
return AWS_OP_ERR; |
| 107 |
} |
| 108 |
|
| 109 |
if (aws_hash_finalize(hash, output, truncate_to)) { |
| 110 |
aws_hash_destroy(hash); |
| 111 |
return AWS_OP_ERR; |
| 112 |
} |
| 113 |
|
| 114 |
aws_hash_destroy(hash); |
| 115 |
return AWS_OP_SUCCESS; |
| 116 |
} |
| 117 |
|
| 118 |
int aws_md5_compute( |
| 119 |
struct aws_allocator *allocator, |
| 120 |
const struct aws_byte_cursor *input, |
| 121 |
struct aws_byte_buf *output, |
| 122 |
size_t truncate_to) { |
| 123 |
return compute_hash(aws_md5_new(allocator), input, output, truncate_to); |
| 124 |
} |
| 125 |
|
| 126 |
int aws_sha512_compute( |
| 127 |
struct aws_allocator *allocator, |
| 128 |
const struct aws_byte_cursor *input, |
| 129 |
struct aws_byte_buf *output, |
| 130 |
size_t truncate_to) { |
| 131 |
return compute_hash(aws_sha512_new(allocator), input, output, truncate_to); |
| 132 |
} |
| 133 |
|
| 134 |
int aws_sha256_compute( |
| 135 |
struct aws_allocator *allocator, |
| 136 |
const struct aws_byte_cursor *input, |
| 137 |
struct aws_byte_buf *output, |
| 138 |
size_t truncate_to) { |
| 139 |
return compute_hash(aws_sha256_new(allocator), input, output, truncate_to); |
| 140 |
} |
| 141 |
|
| 142 |
int aws_sha1_compute( |
| 143 |
struct aws_allocator *allocator, |
| 144 |
const struct aws_byte_cursor *input, |
| 145 |
struct aws_byte_buf *output, |
| 146 |
size_t truncate_to) { |
| 147 |
return compute_hash(aws_sha1_new(allocator), input, output, truncate_to); |
| 148 |
} |