Debian Codegraph @

crt/aws-c-cal/source/windows/bcrypt_hash.c

raw symbols SCIP
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
8 #include <windows.h>
9
10 #include <bcrypt.h>
11 #include <winerror.h>
12
13 static BCRYPT_ALG_HANDLE s_sha512_alg = NULL;
14 static size_t s_sha512_obj_len = 0;
15 static aws_thread_once s_sha512_once = AWS_THREAD_ONCE_STATIC_INIT;
16
17 static BCRYPT_ALG_HANDLE s_sha256_alg = NULL;
18 static size_t s_sha256_obj_len = 0;
19 static aws_thread_once s_sha256_once = AWS_THREAD_ONCE_STATIC_INIT;
20
21 static BCRYPT_ALG_HANDLE s_sha1_alg = NULL;
22 static size_t s_sha1_obj_len = 0;
23 static aws_thread_once s_sha1_once = AWS_THREAD_ONCE_STATIC_INIT;
24
25 static BCRYPT_ALG_HANDLE s_md5_alg = NULL;
26 static size_t s_md5_obj_len = 0;
27 static aws_thread_once s_md5_once = AWS_THREAD_ONCE_STATIC_INIT;
28
29 static void s_destroy(struct aws_hash *hash);
30 static int s_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash);
31 static int s_finalize(struct aws_hash *hash, struct aws_byte_buf *output);
32
33 static struct aws_hash_vtable s_sha512_vtable = {
34 .destroy = s_destroy,
35 .update = s_update,
36 .finalize = s_finalize,
37 .alg_name = "SHA512",
38 .provider = "Windows CNG",
39 };
40
41 static struct aws_hash_vtable s_sha256_vtable = {
42 .destroy = s_destroy,
43 .update = s_update,
44 .finalize = s_finalize,
45 .alg_name = "SHA256",
46 .provider = "Windows CNG",
47 };
48
49 static struct aws_hash_vtable s_sha1_vtable = {
50 .destroy = s_destroy,
51 .update = s_update,
52 .finalize = s_finalize,
53 .alg_name = "SHA1",
54 .provider = "Windows CNG",
55 };
56
57 static struct aws_hash_vtable s_md5_vtable = {
58 .destroy = s_destroy,
59 .update = s_update,
60 .finalize = s_finalize,
61 .alg_name = "MD5",
62 .provider = "Windows CNG",
63 };
64
65 struct bcrypt_hash_handle {
66 struct aws_hash hash;
67 BCRYPT_HASH_HANDLE hash_handle;
68 uint8_t *hash_obj;
69 };
70
71 static void s_load_sha512_alg_handle(void *user_data) {
72 (void)user_data;
73 /* this function is incredibly slow, LET IT LEAK*/
74 (void)BCryptOpenAlgorithmProvider(&s_sha512_alg, BCRYPT_SHA512_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
75 AWS_ASSERT(s_sha512_alg);
76 DWORD result_length = 0;
77 (void)BCryptGetProperty(
78 s_sha512_alg, BCRYPT_OBJECT_LENGTH, (PBYTE)&s_sha512_obj_len, sizeof(s_sha512_obj_len), &result_length, 0);
79 }
80
81 static void s_load_sha256_alg_handle(void *user_data) {
82 (void)user_data;
83 /* this function is incredibly slow, LET IT LEAK*/
84 (void)BCryptOpenAlgorithmProvider(&s_sha256_alg, BCRYPT_SHA256_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
85 AWS_ASSERT(s_sha256_alg);
86 DWORD result_length = 0;
87 (void)BCryptGetProperty(
88 s_sha256_alg, BCRYPT_OBJECT_LENGTH, (PBYTE)&s_sha256_obj_len, sizeof(s_sha256_obj_len), &result_length, 0);
89 }
90
91 static void s_load_sha1_alg_handle(void *user_data) {
92 (void)user_data;
93 /* this function is incredibly slow, LET IT LEAK*/
94 (void)BCryptOpenAlgorithmProvider(&s_sha1_alg, BCRYPT_SHA1_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
95 AWS_ASSERT(s_sha1_alg);
96 DWORD result_length = 0;
97 (void)BCryptGetProperty(
98 s_sha1_alg, BCRYPT_OBJECT_LENGTH, (PBYTE)&s_sha1_obj_len, sizeof(s_sha1_obj_len), &result_length, 0);
99 }
100
101 static void s_load_md5_alg_handle(void *user_data) {
102 (void)user_data;
103 /* this function is incredibly slow, LET IT LEAK*/
104 (void)BCryptOpenAlgorithmProvider(&s_md5_alg, BCRYPT_MD5_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
105 AWS_ASSERT(s_md5_alg);
106 DWORD result_length = 0;
107 (void)BCryptGetProperty(
108 s_md5_alg, BCRYPT_OBJECT_LENGTH, (PBYTE)&s_md5_obj_len, sizeof(s_md5_obj_len), &result_length, 0);
109 }
110
111 struct aws_hash *aws_sha512_default_new(struct aws_allocator *allocator) {
112 aws_thread_call_once(&s_sha512_once, s_load_sha512_alg_handle, NULL);
113
114 struct bcrypt_hash_handle *bcrypt_hash = NULL;
115 uint8_t *hash_obj = NULL;
116 aws_mem_acquire_many(allocator, 2, &bcrypt_hash, sizeof(struct bcrypt_hash_handle), &hash_obj, s_sha512_obj_len);
117
118 AWS_ZERO_STRUCT(*bcrypt_hash);
119 bcrypt_hash->hash.allocator = allocator;
120 bcrypt_hash->hash.vtable = &s_sha512_vtable;
121 bcrypt_hash->hash.impl = bcrypt_hash;
122 bcrypt_hash->hash.digest_size = AWS_SHA512_LEN;
123 bcrypt_hash->hash.good = true;
124 bcrypt_hash->hash_obj = hash_obj;
125 NTSTATUS status = BCryptCreateHash(
126 s_sha512_alg, &bcrypt_hash->hash_handle, bcrypt_hash->hash_obj, (ULONG)s_sha512_obj_len, NULL, 0, 0);
127
128 if (((NTSTATUS)status) < 0) {
129 aws_mem_release(allocator, bcrypt_hash);
130 return NULL;
131 }
132
133 return &bcrypt_hash->hash;
134 }
135
136 struct aws_hash *aws_sha256_default_new(struct aws_allocator *allocator) {
137 aws_thread_call_once(&s_sha256_once, s_load_sha256_alg_handle, NULL);
138
139 struct bcrypt_hash_handle *bcrypt_hash = NULL;
140 uint8_t *hash_obj = NULL;
141 aws_mem_acquire_many(allocator, 2, &bcrypt_hash, sizeof(struct bcrypt_hash_handle), &hash_obj, s_sha256_obj_len);
142
143 AWS_ZERO_STRUCT(*bcrypt_hash);
144 bcrypt_hash->hash.allocator = allocator;
145 bcrypt_hash->hash.vtable = &s_sha256_vtable;
146 bcrypt_hash->hash.impl = bcrypt_hash;
147 bcrypt_hash->hash.digest_size = AWS_SHA256_LEN;
148 bcrypt_hash->hash.good = true;
149 bcrypt_hash->hash_obj = hash_obj;
150 NTSTATUS status = BCryptCreateHash(
151 s_sha256_alg, &bcrypt_hash->hash_handle, bcrypt_hash->hash_obj, (ULONG)s_sha256_obj_len, NULL, 0, 0);
152
153 if (((NTSTATUS)status) < 0) {
154 aws_mem_release(allocator, bcrypt_hash);
155 return NULL;
156 }
157
158 return &bcrypt_hash->hash;
159 }
160
161 struct aws_hash *aws_sha1_default_new(struct aws_allocator *allocator) {
162 aws_thread_call_once(&s_sha1_once, s_load_sha1_alg_handle, NULL);
163
164 struct bcrypt_hash_handle *bcrypt_hash = NULL;
165 uint8_t *hash_obj = NULL;
166 aws_mem_acquire_many(allocator, 2, &bcrypt_hash, sizeof(struct bcrypt_hash_handle), &hash_obj, s_sha1_obj_len);
167
168 AWS_ZERO_STRUCT(*bcrypt_hash);
169 bcrypt_hash->hash.allocator = allocator;
170 bcrypt_hash->hash.vtable = &s_sha1_vtable;
171 bcrypt_hash->hash.impl = bcrypt_hash;
172 bcrypt_hash->hash.digest_size = AWS_SHA1_LEN;
173 bcrypt_hash->hash.good = true;
174 bcrypt_hash->hash_obj = hash_obj;
175 NTSTATUS status = BCryptCreateHash(
176 s_sha1_alg, &bcrypt_hash->hash_handle, bcrypt_hash->hash_obj, (ULONG)s_sha1_obj_len, NULL, 0, 0);
177
178 if (((NTSTATUS)status) < 0) {
179 aws_mem_release(allocator, bcrypt_hash);
180 return NULL;
181 }
182
183 return &bcrypt_hash->hash;
184 }
185
186 struct aws_hash *aws_md5_default_new(struct aws_allocator *allocator) {
187 aws_thread_call_once(&s_md5_once, s_load_md5_alg_handle, NULL);
188
189 struct bcrypt_hash_handle *bcrypt_hash = NULL;
190 uint8_t *hash_obj = NULL;
191 aws_mem_acquire_many(allocator, 2, &bcrypt_hash, sizeof(struct bcrypt_hash_handle), &hash_obj, s_md5_obj_len);
192
193 AWS_ZERO_STRUCT(*bcrypt_hash);
194 bcrypt_hash->hash.allocator = allocator;
195 bcrypt_hash->hash.vtable = &s_md5_vtable;
196 bcrypt_hash->hash.impl = bcrypt_hash;
197 bcrypt_hash->hash.digest_size = AWS_MD5_LEN;
198 bcrypt_hash->hash.good = true;
199 bcrypt_hash->hash_obj = hash_obj;
200 NTSTATUS status =
201 BCryptCreateHash(s_md5_alg, &bcrypt_hash->hash_handle, bcrypt_hash->hash_obj, (ULONG)s_md5_obj_len, NULL, 0, 0);
202
203 if (((NTSTATUS)status) < 0) {
204 aws_mem_release(allocator, bcrypt_hash);
205 return NULL;
206 }
207
208 return &bcrypt_hash->hash;
209 }
210
211 static void s_destroy(struct aws_hash *hash) {
212 struct bcrypt_hash_handle *ctx = hash->impl;
213 BCryptDestroyHash(ctx->hash_handle);
214 aws_mem_release(hash->allocator, ctx);
215 }
216
217 static int s_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash) {
218 if (!hash->good) {
219 return aws_raise_error(AWS_ERROR_INVALID_STATE);
220 }
221
222 struct bcrypt_hash_handle *ctx = hash->impl;
223 NTSTATUS status = BCryptHashData(ctx->hash_handle, to_hash->ptr, (ULONG)to_hash->len, 0);
224
225 if (((NTSTATUS)status) < 0) {
226 hash->good = false;
227 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
228 }
229
230 return AWS_OP_SUCCESS;
231 }
232
233 static int s_finalize(struct aws_hash *hash, struct aws_byte_buf *output) {
234 if (!hash->good) {
235 return aws_raise_error(AWS_ERROR_INVALID_STATE);
236 }
237
238 struct bcrypt_hash_handle *ctx = hash->impl;
239
240 size_t buffer_len = output->capacity - output->len;
241
242 if (buffer_len < hash->digest_size) {
243 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
244 }
245
246 NTSTATUS status = BCryptFinishHash(ctx->hash_handle, output->buffer + output->len, (ULONG)hash->digest_size, 0);
247
248 hash->good = false;
249 if (((NTSTATUS)status) < 0) {
250 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
251 }
252
253 output->len += hash->digest_size;
254 return AWS_OP_SUCCESS;
255 }