1/*
2 * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15#include <aws/common/device_random.h>
16
17#include <aws/common/byte_buf.h>
18#include <aws/common/thread.h>
19
20#include <fcntl.h>
21#include <unistd.h>
22
23static int s_rand_fd = -1;
24static aws_thread_once s_rand_init = AWS_THREAD_ONCE_STATIC_INIT;
25
26#ifdef O_CLOEXEC
27# define OPEN_FLAGS (O_RDONLY | O_CLOEXEC)
28#else
29# define OPEN_FLAGS (O_RDONLY)
30#endif
31static void s_init_rand(void *user_data) {
32 (void)user_data;
33 s_rand_fd = open("/dev/urandom", OPEN_FLAGS);
34
35 if (s_rand_fd == -1) {
36 s_rand_fd = open("/dev/urandom", O_RDONLY);
37
38 if (s_rand_fd == -1) {
39 abort();
40 }
41 }
42
43 if (-1 == fcntl(s_rand_fd, F_SETFD, FD_CLOEXEC)) {
44 abort();
45 }
46}
47
48static int s_fallback_device_random_buffer(struct aws_byte_buf *output) {
49
50 aws_thread_call_once(&s_rand_init, s_init_rand, NULL);
51
52 size_t diff = output->capacity - output->len;
53
54 ssize_t amount_read = read(s_rand_fd, output->buffer + output->len, diff);
55
56 if (amount_read != diff) {
57 return aws_raise_error(AWS_ERROR_RANDOM_GEN_FAILED);
58 }
59
60 output->len += diff;
61
62 return AWS_OP_SUCCESS;
63}
64
65int aws_device_random_buffer(struct aws_byte_buf *output) {
66 return s_fallback_device_random_buffer(output);
67}
68