bcrypt_hmac.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/common/thread.h>
  7. #include <windows.h>
  8. #include <bcrypt.h>
  9. #include <winerror.h>
  10. static BCRYPT_ALG_HANDLE s_sha256_hmac_alg = NULL;
  11. static size_t s_sha256_hmac_obj_len = 0;
  12. static aws_thread_once s_sha256_hmac_once = AWS_THREAD_ONCE_STATIC_INIT;
  13. static void s_destroy(struct aws_hmac *hash);
  14. static int s_update(struct aws_hmac *hash, const struct aws_byte_cursor *to_hash);
  15. static int s_finalize(struct aws_hmac *hash, struct aws_byte_buf *output);
  16. static struct aws_hmac_vtable s_sha256_hmac_vtable = {
  17. .destroy = s_destroy,
  18. .update = s_update,
  19. .finalize = s_finalize,
  20. .alg_name = "SHA256 HMAC",
  21. .provider = "Windows CNG",
  22. };
  23. struct bcrypt_hmac_handle {
  24. struct aws_hmac hmac;
  25. BCRYPT_HASH_HANDLE hash_handle;
  26. uint8_t *hash_obj;
  27. };
  28. static void s_load_alg_handle(void *user_data) {
  29. (void)user_data;
  30. /* this function is incredibly slow, LET IT LEAK*/
  31. BCryptOpenAlgorithmProvider(
  32. &s_sha256_hmac_alg, BCRYPT_SHA256_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
  33. AWS_ASSERT(s_sha256_hmac_alg);
  34. DWORD result_length = 0;
  35. BCryptGetProperty(
  36. s_sha256_hmac_alg,
  37. BCRYPT_OBJECT_LENGTH,
  38. (PBYTE)&s_sha256_hmac_obj_len,
  39. sizeof(s_sha256_hmac_obj_len),
  40. &result_length,
  41. 0);
  42. }
  43. struct aws_hmac *aws_sha256_hmac_default_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) {
  44. aws_thread_call_once(&s_sha256_hmac_once, s_load_alg_handle, NULL);
  45. struct bcrypt_hmac_handle *bcrypt_hmac;
  46. uint8_t *hash_obj;
  47. aws_mem_acquire_many(
  48. allocator, 2, &bcrypt_hmac, sizeof(struct bcrypt_hmac_handle), &hash_obj, s_sha256_hmac_obj_len);
  49. if (!bcrypt_hmac) {
  50. return NULL;
  51. }
  52. AWS_ZERO_STRUCT(*bcrypt_hmac);
  53. bcrypt_hmac->hmac.allocator = allocator;
  54. bcrypt_hmac->hmac.vtable = &s_sha256_hmac_vtable;
  55. bcrypt_hmac->hmac.impl = bcrypt_hmac;
  56. bcrypt_hmac->hmac.digest_size = AWS_SHA256_HMAC_LEN;
  57. bcrypt_hmac->hmac.good = true;
  58. bcrypt_hmac->hash_obj = hash_obj;
  59. NTSTATUS status = BCryptCreateHash(
  60. s_sha256_hmac_alg,
  61. &bcrypt_hmac->hash_handle,
  62. bcrypt_hmac->hash_obj,
  63. (ULONG)s_sha256_hmac_obj_len,
  64. secret->ptr,
  65. (ULONG)secret->len,
  66. 0);
  67. if (((NTSTATUS)status) < 0) {
  68. aws_mem_release(allocator, bcrypt_hmac);
  69. return NULL;
  70. }
  71. return &bcrypt_hmac->hmac;
  72. }
  73. static void s_destroy(struct aws_hmac *hmac) {
  74. struct bcrypt_hmac_handle *ctx = hmac->impl;
  75. BCryptDestroyHash(ctx->hash_handle);
  76. aws_mem_release(hmac->allocator, ctx);
  77. }
  78. static int s_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hash) {
  79. if (!hmac->good) {
  80. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  81. }
  82. struct bcrypt_hmac_handle *ctx = hmac->impl;
  83. NTSTATUS status = BCryptHashData(ctx->hash_handle, to_hash->ptr, (ULONG)to_hash->len, 0);
  84. if (((NTSTATUS)status) < 0) {
  85. hmac->good = false;
  86. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  87. }
  88. return AWS_OP_SUCCESS;
  89. }
  90. static int s_finalize(struct aws_hmac *hmac, struct aws_byte_buf *output) {
  91. if (!hmac->good) {
  92. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  93. }
  94. struct bcrypt_hmac_handle *ctx = hmac->impl;
  95. size_t buffer_len = output->capacity - output->len;
  96. if (buffer_len < hmac->digest_size) {
  97. return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
  98. }
  99. NTSTATUS status = BCryptFinishHash(ctx->hash_handle, output->buffer + output->len, (ULONG)hmac->digest_size, 0);
  100. hmac->good = false;
  101. if (((NTSTATUS)status) < 0) {
  102. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  103. }
  104. output->len += hmac->digest_size;
  105. return AWS_OP_SUCCESS;
  106. }