blob: 767497ca7aa799db84699705a2e6d9b6cb52ce4d [file] [log] [blame]
[email protected]ead8c1f2012-05-30 14:26:131// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]05f9b682008-09-29 22:18:012// 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]09e5f47a2009-06-26 10:00:027#include <errno.h>
[email protected]05f9b682008-09-29 22:18:018#include <fcntl.h>
avi9b6f42932015-12-26 22:15:149#include <stddef.h>
10#include <stdint.h>
[email protected]05f9b682008-09-29 22:18:0111#include <unistd.h>
12
[email protected]e3177dd52014-08-13 20:22:1413#include "base/files/file_util.h"
[email protected]05f9b682008-09-29 22:18:0114#include "base/logging.h"
Lei Zhange3e126d782020-01-07 21:36:0015#include "base/no_destructor.h"
mark4cec4942017-02-28 23:56:0016#include "base/posix/eintr_wrapper.h"
[email protected]05f9b682008-09-29 22:18:0117
[email protected]09e5f47a2009-06-26 10:00:0218namespace {
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 Zhange3e126d782020-01-07 21:36:0023// we can use a static-local variable to handle opening it on the first access.
[email protected]09e5f47a2009-06-26 10:00:0224class URandomFd {
25 public:
rayb0088ee52017-04-26 22:35:0826#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))) {
Wezeba21492018-12-05 18:24:1730 DPCHECK(fd_ >= 0) << "Cannot open /dev/urandom";
rayb0088ee52017-04-26 22:35:0831 }
32#else
mark4cec4942017-02-28 23:56:0033 URandomFd() : fd_(HANDLE_EINTR(open("/dev/urandom", O_RDONLY | O_CLOEXEC))) {
Wezeba21492018-12-05 18:24:1734 DPCHECK(fd_ >= 0) << "Cannot open /dev/urandom";
[email protected]09e5f47a2009-06-26 10:00:0235 }
rayb0088ee52017-04-26 22:35:0836#endif
[email protected]09e5f47a2009-06-26 10:00:0237
[email protected]c910c5a2014-01-23 02:14:2838 ~URandomFd() { close(fd_); }
[email protected]09e5f47a2009-06-26 10:00:0239
40 int fd() const { return fd_; }
41
42 private:
[email protected]c910c5a2014-01-23 02:14:2843 const int fd_;
[email protected]09e5f47a2009-06-26 10:00:0244};
45
[email protected]09e5f47a2009-06-26 10:00:0246} // namespace
47
[email protected]05f9b682008-09-29 22:18:0148namespace base {
49
[email protected]c910c5a2014-01-23 02:14:2850void RandBytes(void* output, size_t output_length) {
Lei Zhange3e126d782020-01-07 21:36:0051 const int urandom_fd = GetUrandomFD();
[email protected]c910c5a2014-01-23 02:14:2852 const bool success =
53 ReadFromFD(urandom_fd, static_cast<char*>(output), output_length);
54 CHECK(success);
55}
56
Lei Zhange3e126d782020-01-07 21:36:0057int GetUrandomFD() {
58 static NoDestructor<URandomFd> urandom_fd;
59 return urandom_fd->fd();
[email protected]1d87fad2010-03-04 20:18:5560}
[email protected]ead8c1f2012-05-30 14:26:1361
62} // namespace base