| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/cal/hmac.h> |
| 6 |
|
| 7 |
#include <CommonCrypto/CommonHMAC.h> |
| 8 |
|
| 9 |
static void s_destroy(struct aws_hmac *hmac); |
| 10 |
static int s_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hmac); |
| 11 |
static int s_finalize(struct aws_hmac *hmac, struct aws_byte_buf *output); |
| 12 |
|
| 13 |
static struct aws_hmac_vtable s_sha256_hmac_vtable = { |
| 14 |
.destroy = s_destroy, |
| 15 |
.update = s_update, |
| 16 |
.finalize = s_finalize, |
| 17 |
.alg_name = "SHA256 HMAC", |
| 18 |
.provider = "CommonCrypto", |
| 19 |
}; |
| 20 |
|
| 21 |
static struct aws_hmac_vtable s_sha512_hmac_vtable = { |
| 22 |
.destroy = s_destroy, |
| 23 |
.update = s_update, |
| 24 |
.finalize = s_finalize, |
| 25 |
.alg_name = "SHA512 HMAC", |
| 26 |
.provider = "CommonCrypto", |
| 27 |
}; |
| 28 |
|
| 29 |
struct cc_hmac { |
| 30 |
struct aws_hmac hmac; |
| 31 |
CCHmacContext cc_hmac_ctx; |
| 32 |
}; |
| 33 |
|
| 34 |
struct aws_hmac *aws_sha256_hmac_default_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) { |
| 35 |
AWS_ASSERT(secret->ptr); |
| 36 |
|
| 37 |
struct cc_hmac *cc_hmac = aws_mem_acquire(allocator, sizeof(struct cc_hmac)); |
| 38 |
|
| 39 |
cc_hmac->hmac.allocator = allocator; |
| 40 |
cc_hmac->hmac.vtable = &s_sha256_hmac_vtable; |
| 41 |
cc_hmac->hmac.impl = cc_hmac; |
| 42 |
cc_hmac->hmac.digest_size = AWS_SHA256_HMAC_LEN; |
| 43 |
cc_hmac->hmac.good = true; |
| 44 |
|
| 45 |
CCHmacInit(&cc_hmac->cc_hmac_ctx, kCCHmacAlgSHA256, secret->ptr, (CC_LONG)secret->len); |
| 46 |
|
| 47 |
return &cc_hmac->hmac; |
| 48 |
} |
| 49 |
|
| 50 |
struct aws_hmac *aws_sha512_hmac_default_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) { |
| 51 |
AWS_ASSERT(secret->ptr); |
| 52 |
|
| 53 |
struct cc_hmac *cc_hmac = aws_mem_acquire(allocator, sizeof(struct cc_hmac)); |
| 54 |
|
| 55 |
cc_hmac->hmac.allocator = allocator; |
| 56 |
cc_hmac->hmac.vtable = &s_sha512_hmac_vtable; |
| 57 |
cc_hmac->hmac.impl = cc_hmac; |
| 58 |
cc_hmac->hmac.digest_size = AWS_SHA512_HMAC_LEN; |
| 59 |
cc_hmac->hmac.good = true; |
| 60 |
|
| 61 |
CCHmacInit(&cc_hmac->cc_hmac_ctx, kCCHmacAlgSHA512, secret->ptr, (CC_LONG)secret->len); |
| 62 |
|
| 63 |
return &cc_hmac->hmac; |
| 64 |
} |
| 65 |
|
| 66 |
static void s_destroy(struct aws_hmac *hmac) { |
| 67 |
struct cc_hmac *ctx = hmac->impl; |
| 68 |
aws_mem_release(hmac->allocator, ctx); |
| 69 |
} |
| 70 |
|
| 71 |
static int s_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hmac) { |
| 72 |
if (!hmac->good) { |
| 73 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 74 |
} |
| 75 |
|
| 76 |
struct cc_hmac *ctx = hmac->impl; |
| 77 |
|
| 78 |
CCHmacUpdate(&ctx->cc_hmac_ctx, to_hmac->ptr, (CC_LONG)to_hmac->len); |
| 79 |
return AWS_OP_SUCCESS; |
| 80 |
} |
| 81 |
|
| 82 |
static int s_finalize(struct aws_hmac *hmac, struct aws_byte_buf *output) { |
| 83 |
if (!hmac->good) { |
| 84 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 85 |
} |
| 86 |
|
| 87 |
struct cc_hmac *ctx = hmac->impl; |
| 88 |
|
| 89 |
size_t buffer_len = output->capacity - output->len; |
| 90 |
|
| 91 |
if (buffer_len < hmac->digest_size) { |
| 92 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 93 |
} |
| 94 |
|
| 95 |
CCHmacFinal(&ctx->cc_hmac_ctx, output->buffer + output->len); |
| 96 |
hmac->good = false; |
| 97 |
output->len += hmac->digest_size; |
| 98 |
return AWS_OP_SUCCESS; |
| 99 |
} |