Skip to content

Commit

Permalink
Avoiding allocating a handle in the Windows RNG. (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
teo-tsirpanis authored Jul 29, 2024
1 parent 622853c commit c9ead75
Showing 1 changed file with 2 additions and 18 deletions.
20 changes: 2 additions & 18 deletions source/windows/device_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,18 @@
#include <aws/common/device_random.h>

#include <aws/common/byte_buf.h>
#include <aws/common/thread.h>

#include <windows.h>

#include <bcrypt.h>

static BCRYPT_ALG_HANDLE s_alg_handle = NULL;
static aws_thread_once s_rand_init = AWS_THREAD_ONCE_STATIC_INIT;

static void s_init_rand(void *user_data) {
(void)user_data;
NTSTATUS status = 0;

status = BCryptOpenAlgorithmProvider(&s_alg_handle, BCRYPT_RNG_ALGORITHM, NULL, 0);

if (!BCRYPT_SUCCESS(status)) {
abort();
}
}

int aws_device_random_buffer(struct aws_byte_buf *output) {
return aws_device_random_buffer_append(output, output->capacity - output->len);
}

int aws_device_random_buffer_append(struct aws_byte_buf *output, size_t n) {
AWS_PRECONDITION(aws_byte_buf_is_valid(output));

aws_thread_call_once(&s_rand_init, s_init_rand, NULL);

size_t space_available = output->capacity - output->len;
if (space_available < n) {
AWS_POSTCONDITION(aws_byte_buf_is_valid(output));
Expand All @@ -47,7 +30,8 @@ int aws_device_random_buffer_append(struct aws_byte_buf *output, size_t n) {
while (n > 0) {
uint32_t capped_n = (uint32_t)aws_min_size(n, UINT32_MAX);

NTSTATUS status = BCryptGenRandom(s_alg_handle, output->buffer + output->len, capped_n, 0 /*flags*/);
NTSTATUS status =
BCryptGenRandom(NULL, output->buffer + output->len, capped_n, BCRYPT_USE_SYSTEM_PREFERRED_RNG);

if (!BCRYPT_SUCCESS(status)) {
output->len = original_len;
Expand Down

0 comments on commit c9ead75

Please sign in to comment.