device_random.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/device_random.h>
  6. #include <aws/common/byte_buf.h>
  7. #include <aws/common/thread.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. static int s_rand_fd = -1;
  11. static aws_thread_once s_rand_init = AWS_THREAD_ONCE_STATIC_INIT;
  12. #ifdef O_CLOEXEC
  13. # define OPEN_FLAGS (O_RDONLY | O_CLOEXEC)
  14. #else
  15. # define OPEN_FLAGS (O_RDONLY)
  16. #endif
  17. static void s_init_rand(void *user_data) {
  18. (void)user_data;
  19. s_rand_fd = open("/dev/urandom", OPEN_FLAGS);
  20. if (s_rand_fd == -1) {
  21. s_rand_fd = open("/dev/urandom", O_RDONLY);
  22. if (s_rand_fd == -1) {
  23. abort();
  24. }
  25. }
  26. if (-1 == fcntl(s_rand_fd, F_SETFD, FD_CLOEXEC)) {
  27. abort();
  28. }
  29. }
  30. int aws_device_random_buffer_append(struct aws_byte_buf *output, size_t n) {
  31. AWS_PRECONDITION(aws_byte_buf_is_valid(output));
  32. aws_thread_call_once(&s_rand_init, s_init_rand, NULL);
  33. size_t space_available = output->capacity - output->len;
  34. if (space_available < n) {
  35. AWS_POSTCONDITION(aws_byte_buf_is_valid(output));
  36. return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
  37. }
  38. size_t original_len = output->len;
  39. /* read() can fail if N is too large (e.g. x64 macos fails if N > INT32_MAX),
  40. * so work in reasonably sized chunks. */
  41. while (n > 0) {
  42. size_t capped_n = aws_min_size(n, 1024 * 1024 * 1024 * 1 /* 1GiB */);
  43. ssize_t amount_read = read(s_rand_fd, output->buffer + output->len, capped_n);
  44. if (amount_read <= 0) {
  45. output->len = original_len;
  46. AWS_POSTCONDITION(aws_byte_buf_is_valid(output));
  47. return aws_raise_error(AWS_ERROR_RANDOM_GEN_FAILED);
  48. }
  49. output->len += amount_read;
  50. n -= amount_read;
  51. }
  52. AWS_POSTCONDITION(aws_byte_buf_is_valid(output));
  53. return AWS_OP_SUCCESS;
  54. }
  55. int aws_device_random_buffer(struct aws_byte_buf *output) {
  56. return aws_device_random_buffer_append(output, output->capacity - output->len);
  57. }