| 1 | #ifndef AWS_CAL_HMAC_H_ |
| 2 | #define AWS_CAL_HMAC_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_SHA256_HMAC_LEN 32 |
| 15 | #define AWS_SHA512_HMAC_LEN 64 |
| 16 | |
| 17 | struct aws_hmac; |
| 18 | |
| 19 | struct aws_hmac_vtable { |
| 20 | const char *alg_name; |
| 21 | const char *provider; |
| 22 | void (*destroy)(struct aws_hmac *hmac); |
| 23 | int (*update)(struct aws_hmac *hmac, const struct aws_byte_cursor *buf); |
| 24 | int (*finalize)(struct aws_hmac *hmac, struct aws_byte_buf *out); |
| 25 | }; |
| 26 | |
| 27 | struct aws_hmac { |
| 28 | struct aws_allocator *allocator; |
| 29 | struct aws_hmac_vtable *vtable; |
| 30 | size_t digest_size; |
| 31 | bool good; |
| 32 | void *impl; |
| 33 | }; |
| 34 | |
| 35 | typedef struct aws_hmac *(aws_hmac_new_fn)(struct aws_allocator *allocator, const struct aws_byte_cursor *secret); |
| 36 | |
| 37 | AWS_EXTERN_C_BEGIN |
| 38 | /** |
| 39 | * Allocates and initializes a sha256 hmac instance. Secret is the key to be |
| 40 | * used for the hmac process. |
| 41 | */ |
| 42 | AWS_CAL_API struct aws_hmac *aws_sha256_hmac_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret); |
| 43 | |
| 44 | /** |
| 45 | * Allocates and initializes a sha512 hmac instance. Secret is the key to be |
| 46 | * used for the hmac process. |
| 47 | */ |
| 48 | AWS_CAL_API struct aws_hmac *aws_sha512_hmac_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret); |
| 49 | |
| 50 | /** |
| 51 | * Cleans up and deallocates hmac. |
| 52 | */ |
| 53 | AWS_CAL_API void aws_hmac_destroy(struct aws_hmac *hmac); |
| 54 | |
| 55 | /** |
| 56 | * Updates the running hmac with to_hash. this can be called multiple times. |
| 57 | */ |
| 58 | AWS_CAL_API int aws_hmac_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hmac); |
| 59 | |
| 60 | /** |
| 61 | * Completes the hmac computation and writes the final digest to output. |
| 62 | * Allocation of output is the caller's responsibility. If you specify |
| 63 | * truncate_to to something other than 0, the output will be truncated to that |
| 64 | * number of bytes. For example if you want a SHA256 digest as the first 16 |
| 65 | * bytes, set truncate_to to 16. If you want the full digest size, just set this |
| 66 | * to 0. |
| 67 | */ |
| 68 | AWS_CAL_API int aws_hmac_finalize(struct aws_hmac *hmac, struct aws_byte_buf *output, size_t truncate_to); |
| 69 | |
| 70 | /** |
| 71 | * Computes the sha256 hmac over input and writes the digest output to 'output'. |
| 72 | * Use this if you don't need to stream the data you're hashing and you can load |
| 73 | * the entire input to hash into memory. If you specify truncate_to to something |
| 74 | * other than 0, the output will be truncated to that number of bytes. For |
| 75 | * example if you want a SHA256 HMAC digest as the first 16 bytes, set |
| 76 | * truncate_to to 16. If you want the full digest size, just set this to 0. |
| 77 | */ |
| 78 | AWS_CAL_API int aws_sha256_hmac_compute( |
| 79 | struct aws_allocator *allocator, |
| 80 | const struct aws_byte_cursor *secret, |
| 81 | const struct aws_byte_cursor *to_hmac, |
| 82 | struct aws_byte_buf *output, |
| 83 | size_t truncate_to); |
| 84 | |
| 85 | /** |
| 86 | * Computes the sha512 hmac over input and writes the digest output to 'output'. |
| 87 | * Use this if you don't need to stream the data you're hashing and you can load |
| 88 | * the entire input to hash into memory. If you specify truncate_to to something |
| 89 | * other than 0, the output will be truncated to that number of bytes. For |
| 90 | * example if you want a SHA512 HMAC digest as the first 16 bytes, set |
| 91 | * truncate_to to 16. If you want the full digest size, just set this to 0. |
| 92 | */ |
| 93 | AWS_CAL_API int aws_sha512_hmac_compute( |
| 94 | struct aws_allocator *allocator, |
| 95 | const struct aws_byte_cursor *secret, |
| 96 | const struct aws_byte_cursor *to_hmac, |
| 97 | struct aws_byte_buf *output, |
| 98 | size_t truncate_to); |
| 99 | |
| 100 | /** |
| 101 | * Set the implementation of sha256 hmac to use. If you compiled without |
| 102 | * BYO_CRYPTO, you do not need to call this. However, if use this, we will |
| 103 | * honor it, regardless of compile options. This may be useful for testing |
| 104 | * purposes. If you did set BYO_CRYPTO, and you do not call this function |
| 105 | * you will segfault. |
| 106 | */ |
| 107 | AWS_CAL_API void aws_set_sha256_hmac_new_fn(aws_hmac_new_fn *fn); |
| 108 | |
| 109 | /** |
| 110 | * Set the implementation of sha256 hmac to use. If you compiled without |
| 111 | * BYO_CRYPTO, you do not need to call this. However, if use this, we will |
| 112 | * honor it, regardless of compile options. This may be useful for testing |
| 113 | * purposes. If you did set BYO_CRYPTO, and you do not call this function |
| 114 | * you will segfault. |
| 115 | */ |
| 116 | AWS_CAL_API void aws_set_sha512_hmac_new_fn(aws_hmac_new_fn *fn); |
| 117 | |
| 118 | AWS_EXTERN_C_END |
| 119 | AWS_POP_SANE_WARNING_LEVEL |
| 120 | |
| 121 | #endif /* AWS_CAL_HASH_H_ */ |