[email protected] | ead8c1f | 2012-05-30 14:26:13 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #include "base/rand_util.h" | ||||
6 | |||||
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 7 | #include <errno.h> |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 8 | #include <fcntl.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 9 | #include <stddef.h> |
10 | #include <stdint.h> | ||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 11 | #include <unistd.h> |
12 | |||||
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 13 | #include "base/files/file_util.h" |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 14 | #include "base/logging.h" |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame^] | 15 | #include "base/no_destructor.h" |
mark | 4cec494 | 2017-02-28 23:56:00 | [diff] [blame] | 16 | #include "base/posix/eintr_wrapper.h" |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 17 | |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 18 | namespace { |
19 | |||||
20 | // We keep the file descriptor for /dev/urandom around so we don't need to | ||||
21 | // reopen it (which is expensive), and since we may not even be able to reopen | ||||
22 | // it if we are later put in a sandbox. This class wraps the file descriptor so | ||||
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame^] | 23 | // we can use a static-local variable to handle opening it on the first access. |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 24 | class URandomFd { |
25 | public: | ||||
rayb | 0088ee5 | 2017-04-26 22:35:08 | [diff] [blame] | 26 | #if defined(OS_AIX) |
27 | // AIX has no 64-bit support for open falgs such as - | ||||
28 | // O_CLOEXEC, O_NOFOLLOW and O_TTY_INIT | ||||
29 | URandomFd() : fd_(HANDLE_EINTR(open("/dev/urandom", O_RDONLY))) { | ||||
Wez | eba2149 | 2018-12-05 18:24:17 | [diff] [blame] | 30 | DPCHECK(fd_ >= 0) << "Cannot open /dev/urandom"; |
rayb | 0088ee5 | 2017-04-26 22:35:08 | [diff] [blame] | 31 | } |
32 | #else | ||||
mark | 4cec494 | 2017-02-28 23:56:00 | [diff] [blame] | 33 | URandomFd() : fd_(HANDLE_EINTR(open("/dev/urandom", O_RDONLY | O_CLOEXEC))) { |
Wez | eba2149 | 2018-12-05 18:24:17 | [diff] [blame] | 34 | DPCHECK(fd_ >= 0) << "Cannot open /dev/urandom"; |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 35 | } |
rayb | 0088ee5 | 2017-04-26 22:35:08 | [diff] [blame] | 36 | #endif |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 37 | |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 38 | ~URandomFd() { close(fd_); } |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 39 | |
40 | int fd() const { return fd_; } | ||||
41 | |||||
42 | private: | ||||
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 43 | const int fd_; |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 44 | }; |
45 | |||||
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 46 | } // namespace |
47 | |||||
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 48 | namespace base { |
49 | |||||
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 50 | void RandBytes(void* output, size_t output_length) { |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame^] | 51 | const int urandom_fd = GetUrandomFD(); |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 52 | const bool success = |
53 | ReadFromFD(urandom_fd, static_cast<char*>(output), output_length); | ||||
54 | CHECK(success); | ||||
55 | } | ||||
56 | |||||
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame^] | 57 | int GetUrandomFD() { |
58 | static NoDestructor<URandomFd> urandom_fd; | ||||
59 | return urandom_fd->fd(); | ||||
[email protected] | 1d87fad | 2010-03-04 20:18:55 | [diff] [blame] | 60 | } |
[email protected] | ead8c1f | 2012-05-30 14:26:13 | [diff] [blame] | 61 | |
62 | } // namespace base |