| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/cal/hmac.h> |
| 6 |
#include <aws/cal/private/opensslcrypto_common.h> |
| 7 |
|
| 8 |
#include <openssl/evp.h> |
| 9 |
#include <openssl/hmac.h> |
| 10 |
|
| 11 |
static void s_destroy(struct aws_hmac *hmac); |
| 12 |
static int s_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hmac); |
| 13 |
static int s_finalize(struct aws_hmac *hmac, struct aws_byte_buf *output); |
| 14 |
|
| 15 |
static struct aws_hmac_vtable s_sha256_hmac_vtable = { |
| 16 |
.destroy = s_destroy, |
| 17 |
.update = s_update, |
| 18 |
.finalize = s_finalize, |
| 19 |
.alg_name = "SHA256 HMAC", |
| 20 |
.provider = "OpenSSL Compatible libcrypto", |
| 21 |
}; |
| 22 |
|
| 23 |
static struct aws_hmac_vtable s_sha512_hmac_vtable = { |
| 24 |
.destroy = s_destroy, |
| 25 |
.update = s_update, |
| 26 |
.finalize = s_finalize, |
| 27 |
.alg_name = "SHA512 HMAC", |
| 28 |
.provider = "OpenSSL Compatible libcrypto", |
| 29 |
}; |
| 30 |
|
| 31 |
static void s_destroy(struct aws_hmac *hmac) { |
| 32 |
if (hmac == NULL) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
HMAC_CTX *ctx = hmac->impl; |
| 37 |
if (ctx != NULL) { |
| 38 |
g_aws_openssl_hmac_ctx_table->free_fn(ctx); |
| 39 |
} |
| 40 |
|
| 41 |
aws_mem_release(hmac->allocator, hmac); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#define SIZEOF_OPENSSL_HMAC_CTX 300 |
| 57 |
|
| 58 |
struct aws_hmac *aws_sha256_hmac_default_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) { |
| 59 |
AWS_ASSERT(secret->ptr); |
| 60 |
|
| 61 |
struct aws_hmac *hmac = aws_mem_acquire(allocator, sizeof(struct aws_hmac)); |
| 62 |
|
| 63 |
hmac->allocator = allocator; |
| 64 |
hmac->vtable = &s_sha256_hmac_vtable; |
| 65 |
hmac->digest_size = AWS_SHA256_HMAC_LEN; |
| 66 |
HMAC_CTX *ctx = NULL; |
| 67 |
ctx = g_aws_openssl_hmac_ctx_table->new_fn(); |
| 68 |
|
| 69 |
if (!ctx) { |
| 70 |
aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED); |
| 71 |
aws_mem_release(allocator, hmac); |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
g_aws_openssl_hmac_ctx_table->init_fn(ctx); |
| 76 |
|
| 77 |
hmac->impl = ctx; |
| 78 |
hmac->good = true; |
| 79 |
|
| 80 |
if (!g_aws_openssl_hmac_ctx_table->init_ex_fn(ctx, secret->ptr, secret->len, EVP_sha256(), NULL)) { |
| 81 |
s_destroy(hmac); |
| 82 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 83 |
return NULL; |
| 84 |
} |
| 85 |
|
| 86 |
return hmac; |
| 87 |
} |
| 88 |
|
| 89 |
struct aws_hmac *aws_sha512_hmac_default_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) { |
| 90 |
AWS_ASSERT(secret->ptr); |
| 91 |
|
| 92 |
struct aws_hmac *hmac = aws_mem_acquire(allocator, sizeof(struct aws_hmac)); |
| 93 |
|
| 94 |
hmac->allocator = allocator; |
| 95 |
hmac->vtable = &s_sha512_hmac_vtable; |
| 96 |
hmac->digest_size = AWS_SHA512_HMAC_LEN; |
| 97 |
HMAC_CTX *ctx = NULL; |
| 98 |
ctx = g_aws_openssl_hmac_ctx_table->new_fn(); |
| 99 |
|
| 100 |
if (!ctx) { |
| 101 |
aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED); |
| 102 |
aws_mem_release(allocator, hmac); |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
g_aws_openssl_hmac_ctx_table->init_fn(ctx); |
| 107 |
|
| 108 |
hmac->impl = ctx; |
| 109 |
hmac->good = true; |
| 110 |
|
| 111 |
if (!g_aws_openssl_hmac_ctx_table->init_ex_fn(ctx, secret->ptr, secret->len, EVP_sha512(), NULL)) { |
| 112 |
s_destroy(hmac); |
| 113 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 114 |
return NULL; |
| 115 |
} |
| 116 |
|
| 117 |
return hmac; |
| 118 |
} |
| 119 |
|
| 120 |
static int s_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hmac) { |
| 121 |
if (!hmac->good) { |
| 122 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 123 |
} |
| 124 |
|
| 125 |
HMAC_CTX *ctx = hmac->impl; |
| 126 |
|
| 127 |
if (AWS_LIKELY(g_aws_openssl_hmac_ctx_table->update_fn(ctx, to_hmac->ptr, to_hmac->len))) { |
| 128 |
return AWS_OP_SUCCESS; |
| 129 |
} |
| 130 |
|
| 131 |
hmac->good = false; |
| 132 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 133 |
} |
| 134 |
|
| 135 |
static int s_finalize(struct aws_hmac *hmac, struct aws_byte_buf *output) { |
| 136 |
if (!hmac->good) { |
| 137 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 138 |
} |
| 139 |
|
| 140 |
HMAC_CTX *ctx = hmac->impl; |
| 141 |
|
| 142 |
size_t buffer_len = output->capacity - output->len; |
| 143 |
|
| 144 |
if (buffer_len < hmac->digest_size) { |
| 145 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 146 |
} |
| 147 |
|
| 148 |
if (AWS_LIKELY( |
| 149 |
g_aws_openssl_hmac_ctx_table->final_fn(ctx, output->buffer + output->len, (unsigned int *)&buffer_len))) { |
| 150 |
hmac->good = false; |
| 151 |
output->len += hmac->digest_size; |
| 152 |
return AWS_OP_SUCCESS; |
| 153 |
} |
| 154 |
|
| 155 |
hmac->good = false; |
| 156 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 157 |
} |