bcrypt_hash.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/cal/hash.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_alg = NULL;
  11. static size_t s_sha256_obj_len = 0;
  12. static aws_thread_once s_sha256_once = AWS_THREAD_ONCE_STATIC_INIT;
  13. static BCRYPT_ALG_HANDLE s_sha1_alg = NULL;
  14. static size_t s_sha1_obj_len = 0;
  15. static aws_thread_once s_sha1_once = AWS_THREAD_ONCE_STATIC_INIT;
  16. static BCRYPT_ALG_HANDLE s_md5_alg = NULL;
  17. static size_t s_md5_obj_len = 0;
  18. static aws_thread_once s_md5_once = AWS_THREAD_ONCE_STATIC_INIT;
  19. static void s_destroy(struct aws_hash *hash);
  20. static int s_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash);
  21. static int s_finalize(struct aws_hash *hash, struct aws_byte_buf *output);
  22. static struct aws_hash_vtable s_sha256_vtable = {
  23. .destroy = s_destroy,
  24. .update = s_update,
  25. .finalize = s_finalize,
  26. .alg_name = "SHA256",
  27. .provider = "Windows CNG",
  28. };
  29. static struct aws_hash_vtable s_sha1_vtable = {
  30. .destroy = s_destroy,
  31. .update = s_update,
  32. .finalize = s_finalize,
  33. .alg_name = "SHA1",
  34. .provider = "Windows CNG",
  35. };
  36. static struct aws_hash_vtable s_md5_vtable = {
  37. .destroy = s_destroy,
  38. .update = s_update,
  39. .finalize = s_finalize,
  40. .alg_name = "MD5",
  41. .provider = "Windows CNG",
  42. };
  43. struct bcrypt_hash_handle {
  44. struct aws_hash hash;
  45. BCRYPT_HASH_HANDLE hash_handle;
  46. uint8_t *hash_obj;
  47. };
  48. static void s_load_sha256_alg_handle(void *user_data) {
  49. (void)user_data;
  50. /* this function is incredibly slow, LET IT LEAK*/
  51. (void)BCryptOpenAlgorithmProvider(&s_sha256_alg, BCRYPT_SHA256_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
  52. AWS_ASSERT(s_sha256_alg);
  53. DWORD result_length = 0;
  54. (void)BCryptGetProperty(
  55. s_sha256_alg, BCRYPT_OBJECT_LENGTH, (PBYTE)&s_sha256_obj_len, sizeof(s_sha256_obj_len), &result_length, 0);
  56. }
  57. static void s_load_sha1_alg_handle(void *user_data) {
  58. (void)user_data;
  59. /* this function is incredibly slow, LET IT LEAK*/
  60. (void)BCryptOpenAlgorithmProvider(&s_sha1_alg, BCRYPT_SHA1_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
  61. AWS_ASSERT(s_sha1_alg);
  62. DWORD result_length = 0;
  63. (void)BCryptGetProperty(
  64. s_sha1_alg, BCRYPT_OBJECT_LENGTH, (PBYTE)&s_sha1_obj_len, sizeof(s_sha1_obj_len), &result_length, 0);
  65. }
  66. static void s_load_md5_alg_handle(void *user_data) {
  67. (void)user_data;
  68. /* this function is incredibly slow, LET IT LEAK*/
  69. (void)BCryptOpenAlgorithmProvider(&s_md5_alg, BCRYPT_MD5_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
  70. AWS_ASSERT(s_md5_alg);
  71. DWORD result_length = 0;
  72. (void)BCryptGetProperty(
  73. s_md5_alg, BCRYPT_OBJECT_LENGTH, (PBYTE)&s_md5_obj_len, sizeof(s_md5_obj_len), &result_length, 0);
  74. }
  75. struct aws_hash *aws_sha256_default_new(struct aws_allocator *allocator) {
  76. aws_thread_call_once(&s_sha256_once, s_load_sha256_alg_handle, NULL);
  77. struct bcrypt_hash_handle *bcrypt_hash = NULL;
  78. uint8_t *hash_obj = NULL;
  79. aws_mem_acquire_many(allocator, 2, &bcrypt_hash, sizeof(struct bcrypt_hash_handle), &hash_obj, s_sha256_obj_len);
  80. if (!bcrypt_hash) {
  81. return NULL;
  82. }
  83. AWS_ZERO_STRUCT(*bcrypt_hash);
  84. bcrypt_hash->hash.allocator = allocator;
  85. bcrypt_hash->hash.vtable = &s_sha256_vtable;
  86. bcrypt_hash->hash.impl = bcrypt_hash;
  87. bcrypt_hash->hash.digest_size = AWS_SHA256_LEN;
  88. bcrypt_hash->hash.good = true;
  89. bcrypt_hash->hash_obj = hash_obj;
  90. NTSTATUS status = BCryptCreateHash(
  91. s_sha256_alg, &bcrypt_hash->hash_handle, bcrypt_hash->hash_obj, (ULONG)s_sha256_obj_len, NULL, 0, 0);
  92. if (((NTSTATUS)status) < 0) {
  93. aws_mem_release(allocator, bcrypt_hash);
  94. return NULL;
  95. }
  96. return &bcrypt_hash->hash;
  97. }
  98. struct aws_hash *aws_sha1_default_new(struct aws_allocator *allocator) {
  99. aws_thread_call_once(&s_sha1_once, s_load_sha1_alg_handle, NULL);
  100. struct bcrypt_hash_handle *bcrypt_hash = NULL;
  101. uint8_t *hash_obj = NULL;
  102. aws_mem_acquire_many(allocator, 2, &bcrypt_hash, sizeof(struct bcrypt_hash_handle), &hash_obj, s_sha1_obj_len);
  103. if (!bcrypt_hash) {
  104. return NULL;
  105. }
  106. AWS_ZERO_STRUCT(*bcrypt_hash);
  107. bcrypt_hash->hash.allocator = allocator;
  108. bcrypt_hash->hash.vtable = &s_sha1_vtable;
  109. bcrypt_hash->hash.impl = bcrypt_hash;
  110. bcrypt_hash->hash.digest_size = AWS_SHA1_LEN;
  111. bcrypt_hash->hash.good = true;
  112. bcrypt_hash->hash_obj = hash_obj;
  113. NTSTATUS status = BCryptCreateHash(
  114. s_sha1_alg, &bcrypt_hash->hash_handle, bcrypt_hash->hash_obj, (ULONG)s_sha1_obj_len, NULL, 0, 0);
  115. if (((NTSTATUS)status) < 0) {
  116. aws_mem_release(allocator, bcrypt_hash);
  117. return NULL;
  118. }
  119. return &bcrypt_hash->hash;
  120. }
  121. struct aws_hash *aws_md5_default_new(struct aws_allocator *allocator) {
  122. aws_thread_call_once(&s_md5_once, s_load_md5_alg_handle, NULL);
  123. struct bcrypt_hash_handle *bcrypt_hash = NULL;
  124. uint8_t *hash_obj = NULL;
  125. aws_mem_acquire_many(allocator, 2, &bcrypt_hash, sizeof(struct bcrypt_hash_handle), &hash_obj, s_md5_obj_len);
  126. if (!bcrypt_hash) {
  127. return NULL;
  128. }
  129. AWS_ZERO_STRUCT(*bcrypt_hash);
  130. bcrypt_hash->hash.allocator = allocator;
  131. bcrypt_hash->hash.vtable = &s_md5_vtable;
  132. bcrypt_hash->hash.impl = bcrypt_hash;
  133. bcrypt_hash->hash.digest_size = AWS_MD5_LEN;
  134. bcrypt_hash->hash.good = true;
  135. bcrypt_hash->hash_obj = hash_obj;
  136. NTSTATUS status =
  137. BCryptCreateHash(s_md5_alg, &bcrypt_hash->hash_handle, bcrypt_hash->hash_obj, (ULONG)s_md5_obj_len, NULL, 0, 0);
  138. if (((NTSTATUS)status) < 0) {
  139. aws_mem_release(allocator, bcrypt_hash);
  140. return NULL;
  141. }
  142. return &bcrypt_hash->hash;
  143. }
  144. static void s_destroy(struct aws_hash *hash) {
  145. struct bcrypt_hash_handle *ctx = hash->impl;
  146. BCryptDestroyHash(ctx->hash_handle);
  147. aws_mem_release(hash->allocator, ctx);
  148. }
  149. static int s_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash) {
  150. if (!hash->good) {
  151. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  152. }
  153. struct bcrypt_hash_handle *ctx = hash->impl;
  154. NTSTATUS status = BCryptHashData(ctx->hash_handle, to_hash->ptr, (ULONG)to_hash->len, 0);
  155. if (((NTSTATUS)status) < 0) {
  156. hash->good = false;
  157. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  158. }
  159. return AWS_OP_SUCCESS;
  160. }
  161. static int s_finalize(struct aws_hash *hash, struct aws_byte_buf *output) {
  162. if (!hash->good) {
  163. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  164. }
  165. struct bcrypt_hash_handle *ctx = hash->impl;
  166. size_t buffer_len = output->capacity - output->len;
  167. if (buffer_len < hash->digest_size) {
  168. return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
  169. }
  170. NTSTATUS status = BCryptFinishHash(ctx->hash_handle, output->buffer + output->len, (ULONG)hash->digest_size, 0);
  171. hash->good = false;
  172. if (((NTSTATUS)status) < 0) {
  173. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  174. }
  175. output->len += hash->digest_size;
  176. return AWS_OP_SUCCESS;
  177. }