| 1 | #ifndef AWS_CAL_HASH_H_ |
| 2 | #define AWS_CAL_HASH_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/exports.h> |
| 8 | |
| 9 | #include <aws/common/byte_buf.h> |
| 10 | #include <aws/common/common.h> |
| 11 | |
| 12 | AWS_PUSH_SANE_WARNING_LEVEL |
| 13 | |
| 14 | #define AWS_SHA512_LEN 64 |
| 15 | #define AWS_SHA256_LEN 32 |
| 16 | #define AWS_SHA1_LEN 20 |
| 17 | #define AWS_MD5_LEN 16 |
| 18 | |
| 19 | struct aws_hash; |
| 20 | |
| 21 | struct aws_hash_vtable { |
| 22 | const char *alg_name; |
| 23 | const char *provider; |
| 24 | void (*destroy)(struct aws_hash *hash); |
| 25 | int (*update)(struct aws_hash *hash, const struct aws_byte_cursor *buf); |
| 26 | int (*finalize)(struct aws_hash *hash, struct aws_byte_buf *out); |
| 27 | }; |
| 28 | |
| 29 | struct aws_hash { |
| 30 | struct aws_allocator *allocator; |
| 31 | struct aws_hash_vtable *vtable; |
| 32 | size_t digest_size; |
| 33 | bool good; |
| 34 | void *impl; |
| 35 | }; |
| 36 | |
| 37 | typedef struct aws_hash *(aws_hash_new_fn)(struct aws_allocator *allocator); |
| 38 | |
| 39 | AWS_EXTERN_C_BEGIN |
| 40 | /** |
| 41 | * Allocates and initializes a sha512 hash instance. |
| 42 | */ |
| 43 | AWS_CAL_API struct aws_hash *aws_sha512_new(struct aws_allocator *allocator); |
| 44 | |
| 45 | /** |
| 46 | * Allocates and initializes a sha256 hash instance. |
| 47 | */ |
| 48 | AWS_CAL_API struct aws_hash *aws_sha256_new(struct aws_allocator *allocator); |
| 49 | /** |
| 50 | * Allocates and initializes a sha1 hash instance. |
| 51 | */ |
| 52 | AWS_CAL_API struct aws_hash *aws_sha1_new(struct aws_allocator *allocator); |
| 53 | /** |
| 54 | * Allocates and initializes an md5 hash instance. |
| 55 | */ |
| 56 | AWS_CAL_API struct aws_hash *aws_md5_new(struct aws_allocator *allocator); |
| 57 | |
| 58 | /** |
| 59 | * Cleans up and deallocates hash. |
| 60 | */ |
| 61 | AWS_CAL_API void aws_hash_destroy(struct aws_hash *hash); |
| 62 | /** |
| 63 | * Updates the running hash with to_hash. this can be called multiple times. |
| 64 | */ |
| 65 | AWS_CAL_API int aws_hash_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash); |
| 66 | /** |
| 67 | * Completes the hash computation and writes the final digest to output. |
| 68 | * Allocation of output is the caller's responsibility. If you specify |
| 69 | * truncate_to to something other than 0, the output will be truncated to that |
| 70 | * number of bytes. For example, if you want a SHA256 digest as the first 16 |
| 71 | * bytes, set truncate_to to 16. If you want the full digest size, just set this |
| 72 | * to 0. |
| 73 | */ |
| 74 | AWS_CAL_API int aws_hash_finalize(struct aws_hash *hash, struct aws_byte_buf *output, size_t truncate_to); |
| 75 | |
| 76 | /** |
| 77 | * Computes the md5 hash over input and writes the digest output to 'output'. |
| 78 | * Use this if you don't need to stream the data you're hashing and you can load |
| 79 | * the entire input to hash into memory. |
| 80 | */ |
| 81 | AWS_CAL_API int aws_md5_compute( |
| 82 | struct aws_allocator *allocator, |
| 83 | const struct aws_byte_cursor *input, |
| 84 | struct aws_byte_buf *output, |
| 85 | size_t truncate_to); |
| 86 | |
| 87 | /** |
| 88 | * Computes the sha512 hash over input and writes the digest output to 'output'. |
| 89 | * Use this if you don't need to stream the data you're hashing and you can load |
| 90 | * the entire input to hash into memory. If you specify truncate_to to something |
| 91 | * other than 0, the output will be truncated to that number of bytes. For |
| 92 | * example, if you want a SHA512 digest as the first 16 bytes, set truncate_to |
| 93 | * to 16. If you want the full digest size, just set this to 0. |
| 94 | */ |
| 95 | AWS_CAL_API int aws_sha512_compute( |
| 96 | struct aws_allocator *allocator, |
| 97 | const struct aws_byte_cursor *input, |
| 98 | struct aws_byte_buf *output, |
| 99 | size_t truncate_to); |
| 100 | |
| 101 | /** |
| 102 | * Computes the sha256 hash over input and writes the digest output to 'output'. |
| 103 | * Use this if you don't need to stream the data you're hashing and you can load |
| 104 | * the entire input to hash into memory. If you specify truncate_to to something |
| 105 | * other than 0, the output will be truncated to that number of bytes. For |
| 106 | * example, if you want a SHA256 digest as the first 16 bytes, set truncate_to |
| 107 | * to 16. If you want the full digest size, just set this to 0. |
| 108 | */ |
| 109 | AWS_CAL_API int aws_sha256_compute( |
| 110 | struct aws_allocator *allocator, |
| 111 | const struct aws_byte_cursor *input, |
| 112 | struct aws_byte_buf *output, |
| 113 | size_t truncate_to); |
| 114 | |
| 115 | /** |
| 116 | * Computes the sha1 hash over input and writes the digest output to 'output'. |
| 117 | * Use this if you don't need to stream the data you're hashing and you can load |
| 118 | * the entire input to hash into memory. If you specify truncate_to to something |
| 119 | * other than 0, the output will be truncated to that number of bytes. For |
| 120 | * example, if you want a SHA1 digest as the first 16 bytes, set truncate_to |
| 121 | * to 16. If you want the full digest size, just set this to 0. |
| 122 | */ |
| 123 | AWS_CAL_API int aws_sha1_compute( |
| 124 | struct aws_allocator *allocator, |
| 125 | const struct aws_byte_cursor *input, |
| 126 | struct aws_byte_buf *output, |
| 127 | size_t truncate_to); |
| 128 | |
| 129 | /** |
| 130 | * Set the implementation of md5 to use. If you compiled without BYO_CRYPTO, |
| 131 | * you do not need to call this. However, if use this, we will honor it, |
| 132 | * regardless of compile options. This may be useful for testing purposes. If |
| 133 | * you did set BYO_CRYPTO, and you do not call this function you will |
| 134 | * segfault. |
| 135 | */ |
| 136 | AWS_CAL_API void aws_set_md5_new_fn(aws_hash_new_fn *fn); |
| 137 | |
| 138 | /** |
| 139 | * Set the implementation of sha512 to use. If you compiled without |
| 140 | * BYO_CRYPTO, you do not need to call this. However, if use this, we will |
| 141 | * honor it, regardless of compile options. This may be useful for testing |
| 142 | * purposes. If you did set BYO_CRYPTO, and you do not call this function |
| 143 | * you will segfault. |
| 144 | */ |
| 145 | AWS_CAL_API void aws_set_sha512_new_fn(aws_hash_new_fn *fn); |
| 146 | |
| 147 | /** |
| 148 | * Set the implementation of sha256 to use. If you compiled without |
| 149 | * BYO_CRYPTO, you do not need to call this. However, if use this, we will |
| 150 | * honor it, regardless of compile options. This may be useful for testing |
| 151 | * purposes. If you did set BYO_CRYPTO, and you do not call this function |
| 152 | * you will segfault. |
| 153 | */ |
| 154 | AWS_CAL_API void aws_set_sha256_new_fn(aws_hash_new_fn *fn); |
| 155 | |
| 156 | /** |
| 157 | * Set the implementation of sha1 to use. If you compiled without |
| 158 | * BYO_CRYPTO, you do not need to call this. However, if use this, we will |
| 159 | * honor it, regardless of compile options. This may be useful for testing |
| 160 | * purposes. If you did set BYO_CRYPTO, and you do not call this function |
| 161 | * you will segfault. |
| 162 | */ |
| 163 | AWS_CAL_API void aws_set_sha1_new_fn(aws_hash_new_fn *fn); |
| 164 | |
| 165 | AWS_EXTERN_C_END |
| 166 | AWS_POP_SANE_WARNING_LEVEL |
| 167 | |
| 168 | #endif /* AWS_CAL_HASH_H_ */ |