| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/cal/cal.h> |
| 6 |
#include <aws/cal/hmac.h> |
| 7 |
|
| 8 |
#include <aws/common/thread.h> |
| 9 |
|
| 10 |
#include <windows.h> |
| 11 |
|
| 12 |
#include <bcrypt.h> |
| 13 |
#include <winerror.h> |
| 14 |
|
| 15 |
static BCRYPT_ALG_HANDLE s_sha256_hmac_alg = NULL; |
| 16 |
static BCRYPT_ALG_HANDLE s_sha512_hmac_alg = NULL; |
| 17 |
static size_t s_sha256_hmac_obj_len = 0; |
| 18 |
static size_t s_sha512_hmac_obj_len = 0; |
| 19 |
|
| 20 |
static aws_thread_once s_sha256_hmac_once = AWS_THREAD_ONCE_STATIC_INIT; |
| 21 |
static aws_thread_once s_sha512_hmac_once = AWS_THREAD_ONCE_STATIC_INIT; |
| 22 |
|
| 23 |
static void s_destroy(struct aws_hmac *hash); |
| 24 |
static int s_update(struct aws_hmac *hash, const struct aws_byte_cursor *to_hash); |
| 25 |
static int s_finalize(struct aws_hmac *hash, struct aws_byte_buf *output); |
| 26 |
|
| 27 |
static struct aws_hmac_vtable s_sha256_hmac_vtable = { |
| 28 |
.destroy = s_destroy, |
| 29 |
.update = s_update, |
| 30 |
.finalize = s_finalize, |
| 31 |
.alg_name = "SHA256 HMAC", |
| 32 |
.provider = "Windows CNG", |
| 33 |
}; |
| 34 |
|
| 35 |
static struct aws_hmac_vtable s_sha512_hmac_vtable = { |
| 36 |
.destroy = s_destroy, |
| 37 |
.update = s_update, |
| 38 |
.finalize = s_finalize, |
| 39 |
.alg_name = "SHA512 HMAC", |
| 40 |
.provider = "Windows CNG", |
| 41 |
}; |
| 42 |
|
| 43 |
struct bcrypt_hmac_handle { |
| 44 |
struct aws_hmac hmac; |
| 45 |
BCRYPT_HASH_HANDLE hash_handle; |
| 46 |
uint8_t *hash_obj; |
| 47 |
}; |
| 48 |
|
| 49 |
static void s_load_alg_handle_sha256(void *user_data) { |
| 50 |
(void)user_data; |
| 51 |
|
| 52 |
BCryptOpenAlgorithmProvider( |
| 53 |
&s_sha256_hmac_alg, BCRYPT_SHA256_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG); |
| 54 |
AWS_ASSERT(s_sha256_hmac_alg); |
| 55 |
DWORD result_length = 0; |
| 56 |
BCryptGetProperty( |
| 57 |
s_sha256_hmac_alg, |
| 58 |
BCRYPT_OBJECT_LENGTH, |
| 59 |
(PBYTE)&s_sha256_hmac_obj_len, |
| 60 |
sizeof(s_sha256_hmac_obj_len), |
| 61 |
&result_length, |
| 62 |
0); |
| 63 |
} |
| 64 |
|
| 65 |
static void s_load_alg_handle_sha512(void *user_data) { |
| 66 |
(void)user_data; |
| 67 |
|
| 68 |
BCryptOpenAlgorithmProvider( |
| 69 |
&s_sha512_hmac_alg, BCRYPT_SHA512_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG); |
| 70 |
AWS_ASSERT(s_sha512_hmac_alg); |
| 71 |
DWORD result_length = 0; |
| 72 |
BCryptGetProperty( |
| 73 |
s_sha512_hmac_alg, |
| 74 |
BCRYPT_OBJECT_LENGTH, |
| 75 |
(PBYTE)&s_sha512_hmac_obj_len, |
| 76 |
sizeof(s_sha512_hmac_obj_len), |
| 77 |
&result_length, |
| 78 |
0); |
| 79 |
} |
| 80 |
|
| 81 |
struct aws_hmac *aws_sha256_hmac_default_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) { |
| 82 |
aws_thread_call_once(&s_sha256_hmac_once, s_load_alg_handle_sha256, NULL); |
| 83 |
|
| 84 |
struct bcrypt_hmac_handle *bcrypt_hmac; |
| 85 |
uint8_t *hash_obj; |
| 86 |
aws_mem_acquire_many( |
| 87 |
allocator, 2, &bcrypt_hmac, sizeof(struct bcrypt_hmac_handle), &hash_obj, s_sha256_hmac_obj_len); |
| 88 |
|
| 89 |
AWS_ZERO_STRUCT(*bcrypt_hmac); |
| 90 |
bcrypt_hmac->hmac.allocator = allocator; |
| 91 |
bcrypt_hmac->hmac.vtable = &s_sha256_hmac_vtable; |
| 92 |
bcrypt_hmac->hmac.impl = bcrypt_hmac; |
| 93 |
bcrypt_hmac->hmac.digest_size = AWS_SHA256_HMAC_LEN; |
| 94 |
bcrypt_hmac->hmac.good = true; |
| 95 |
bcrypt_hmac->hash_obj = hash_obj; |
| 96 |
NTSTATUS status = BCryptCreateHash( |
| 97 |
s_sha256_hmac_alg, |
| 98 |
&bcrypt_hmac->hash_handle, |
| 99 |
bcrypt_hmac->hash_obj, |
| 100 |
(ULONG)s_sha256_hmac_obj_len, |
| 101 |
secret->ptr, |
| 102 |
(ULONG)secret->len, |
| 103 |
0); |
| 104 |
|
| 105 |
if (((NTSTATUS)status) < 0) { |
| 106 |
aws_mem_release(allocator, bcrypt_hmac); |
| 107 |
aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED); |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
return &bcrypt_hmac->hmac; |
| 112 |
} |
| 113 |
|
| 114 |
struct aws_hmac *aws_sha512_hmac_default_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) { |
| 115 |
aws_thread_call_once(&s_sha512_hmac_once, s_load_alg_handle_sha512, NULL); |
| 116 |
|
| 117 |
struct bcrypt_hmac_handle *bcrypt_hmac; |
| 118 |
uint8_t *hash_obj; |
| 119 |
aws_mem_acquire_many( |
| 120 |
allocator, 2, &bcrypt_hmac, sizeof(struct bcrypt_hmac_handle), &hash_obj, s_sha512_hmac_obj_len); |
| 121 |
|
| 122 |
AWS_ZERO_STRUCT(*bcrypt_hmac); |
| 123 |
bcrypt_hmac->hmac.allocator = allocator; |
| 124 |
bcrypt_hmac->hmac.vtable = &s_sha512_hmac_vtable; |
| 125 |
bcrypt_hmac->hmac.impl = bcrypt_hmac; |
| 126 |
bcrypt_hmac->hmac.digest_size = AWS_SHA512_HMAC_LEN; |
| 127 |
bcrypt_hmac->hmac.good = true; |
| 128 |
bcrypt_hmac->hash_obj = hash_obj; |
| 129 |
NTSTATUS status = BCryptCreateHash( |
| 130 |
s_sha512_hmac_alg, |
| 131 |
&bcrypt_hmac->hash_handle, |
| 132 |
bcrypt_hmac->hash_obj, |
| 133 |
(ULONG)s_sha512_hmac_obj_len, |
| 134 |
secret->ptr, |
| 135 |
(ULONG)secret->len, |
| 136 |
0); |
| 137 |
|
| 138 |
if (((NTSTATUS)status) < 0) { |
| 139 |
aws_mem_release(allocator, bcrypt_hmac); |
| 140 |
aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED); |
| 141 |
return NULL; |
| 142 |
} |
| 143 |
|
| 144 |
return &bcrypt_hmac->hmac; |
| 145 |
} |
| 146 |
|
| 147 |
static void s_destroy(struct aws_hmac *hmac) { |
| 148 |
struct bcrypt_hmac_handle *ctx = hmac->impl; |
| 149 |
BCryptDestroyHash(ctx->hash_handle); |
| 150 |
aws_mem_release(hmac->allocator, ctx); |
| 151 |
} |
| 152 |
|
| 153 |
static int s_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hash) { |
| 154 |
if (!hmac->good) { |
| 155 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 156 |
} |
| 157 |
|
| 158 |
struct bcrypt_hmac_handle *ctx = hmac->impl; |
| 159 |
NTSTATUS status = BCryptHashData(ctx->hash_handle, to_hash->ptr, (ULONG)to_hash->len, 0); |
| 160 |
|
| 161 |
if (((NTSTATUS)status) < 0) { |
| 162 |
hmac->good = false; |
| 163 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 164 |
} |
| 165 |
|
| 166 |
return AWS_OP_SUCCESS; |
| 167 |
} |
| 168 |
|
| 169 |
static int s_finalize(struct aws_hmac *hmac, struct aws_byte_buf *output) { |
| 170 |
if (!hmac->good) { |
| 171 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 172 |
} |
| 173 |
|
| 174 |
struct bcrypt_hmac_handle *ctx = hmac->impl; |
| 175 |
|
| 176 |
size_t buffer_len = output->capacity - output->len; |
| 177 |
|
| 178 |
if (buffer_len < hmac->digest_size) { |
| 179 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 180 |
} |
| 181 |
|
| 182 |
NTSTATUS status = BCryptFinishHash(ctx->hash_handle, output->buffer + output->len, (ULONG)hmac->digest_size, 0); |
| 183 |
|
| 184 |
hmac->good = false; |
| 185 |
if (((NTSTATUS)status) < 0) { |
| 186 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 187 |
} |
| 188 |
|
| 189 |
output->len += hmac->digest_size; |
| 190 |
return AWS_OP_SUCCESS; |
| 191 |
} |