opensslcrypto_hmac.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/cal/hmac.h>
  6. #include <aws/cal/private/opensslcrypto_common.h>
  7. #include <openssl/evp.h>
  8. #include <openssl/hmac.h>
  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. static struct aws_hmac_vtable s_sha256_hmac_vtable = {
  13. .destroy = s_destroy,
  14. .update = s_update,
  15. .finalize = s_finalize,
  16. .alg_name = "SHA256 HMAC",
  17. .provider = "OpenSSL Compatible libcrypto",
  18. };
  19. static void s_destroy(struct aws_hmac *hmac) {
  20. if (hmac == NULL) {
  21. return;
  22. }
  23. HMAC_CTX *ctx = hmac->impl;
  24. if (ctx != NULL) {
  25. g_aws_openssl_hmac_ctx_table->free_fn(ctx);
  26. }
  27. aws_mem_release(hmac->allocator, hmac);
  28. }
  29. /*
  30. typedef struct hmac_ctx_st {
  31. const EVP_MD *md;
  32. EVP_MD_CTX md_ctx;
  33. EVP_MD_CTX i_ctx;
  34. EVP_MD_CTX o_ctx;
  35. unsigned int key_length;
  36. unsigned char key[HMAC_MAX_MD_CBLOCK];
  37. } HMAC_CTX;
  38. */
  39. #define SIZEOF_OPENSSL_HMAC_CTX 300 /* <= 288 on 64 bit systems with openssl 1.0.* */
  40. struct aws_hmac *aws_sha256_hmac_default_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) {
  41. AWS_ASSERT(secret->ptr);
  42. struct aws_hmac *hmac = aws_mem_acquire(allocator, sizeof(struct aws_hmac));
  43. if (!hmac) {
  44. return NULL;
  45. }
  46. hmac->allocator = allocator;
  47. hmac->vtable = &s_sha256_hmac_vtable;
  48. hmac->digest_size = AWS_SHA256_HMAC_LEN;
  49. HMAC_CTX *ctx = NULL;
  50. ctx = g_aws_openssl_hmac_ctx_table->new_fn();
  51. if (!ctx) {
  52. aws_raise_error(AWS_ERROR_OOM);
  53. aws_mem_release(allocator, hmac);
  54. return NULL;
  55. }
  56. g_aws_openssl_hmac_ctx_table->init_fn(ctx);
  57. hmac->impl = ctx;
  58. hmac->good = true;
  59. if (!g_aws_openssl_hmac_ctx_table->init_ex_fn(ctx, secret->ptr, (int)secret->len, EVP_sha256(), NULL)) {
  60. s_destroy(hmac);
  61. aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  62. return NULL;
  63. }
  64. return hmac;
  65. }
  66. static int s_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hmac) {
  67. if (!hmac->good) {
  68. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  69. }
  70. HMAC_CTX *ctx = hmac->impl;
  71. if (AWS_LIKELY(g_aws_openssl_hmac_ctx_table->update_fn(ctx, to_hmac->ptr, to_hmac->len))) {
  72. return AWS_OP_SUCCESS;
  73. }
  74. hmac->good = false;
  75. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  76. }
  77. static int s_finalize(struct aws_hmac *hmac, struct aws_byte_buf *output) {
  78. if (!hmac->good) {
  79. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  80. }
  81. HMAC_CTX *ctx = hmac->impl;
  82. size_t buffer_len = output->capacity - output->len;
  83. if (buffer_len < hmac->digest_size) {
  84. return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
  85. }
  86. if (AWS_LIKELY(
  87. g_aws_openssl_hmac_ctx_table->final_fn(ctx, output->buffer + output->len, (unsigned int *)&buffer_len))) {
  88. hmac->good = false;
  89. output->len += hmac->digest_size;
  90. return AWS_OP_SUCCESS;
  91. }
  92. hmac->good = false;
  93. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  94. }