blob: 2c1653d322f7cc725c46bd39324a7806b3f2956b [file] [log] [blame]
mseaborn@chromium.org056521b2012-05-30 23:26:13 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
mark@chromium.orgb93c0542008-09-30 07:18:01 +09002// 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
deanm@chromium.org2f749ea2009-06-26 19:00:02 +09007#include <errno.h>
mark@chromium.orgb93c0542008-09-30 07:18:01 +09008#include <fcntl.h>
avia6a6a682015-12-27 07:15:14 +09009#include <stddef.h>
10#include <stdint.h>
mark@chromium.orgb93c0542008-09-30 07:18:01 +090011#include <unistd.h>
12
brettw@chromium.org01f3da42014-08-14 05:22:14 +090013#include "base/files/file_util.h"
deanm@chromium.org2f749ea2009-06-26 19:00:02 +090014#include "base/lazy_instance.h"
mark@chromium.orgb93c0542008-09-30 07:18:01 +090015#include "base/logging.h"
mark1849d742017-03-01 08:56:00 +090016#include "base/posix/eintr_wrapper.h"
mark@chromium.orgb93c0542008-09-30 07:18:01 +090017
deanm@chromium.org2f749ea2009-06-26 19:00:02 +090018namespace {
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
23// we can use LazyInstance to handle opening it on the first access.
24class URandomFd {
25 public:
rayb157aae32017-04-27 07:35:08 +090026#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))) {
30 DCHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno;
31 }
32#else
mark1849d742017-03-01 08:56:00 +090033 URandomFd() : fd_(HANDLE_EINTR(open("/dev/urandom", O_RDONLY | O_CLOEXEC))) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +090034 DCHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno;
deanm@chromium.org2f749ea2009-06-26 19:00:02 +090035 }
rayb157aae32017-04-27 07:35:08 +090036#endif
deanm@chromium.org2f749ea2009-06-26 19:00:02 +090037
dalecurtis@chromium.org4f4a8772014-01-23 11:14:28 +090038 ~URandomFd() { close(fd_); }
deanm@chromium.org2f749ea2009-06-26 19:00:02 +090039
40 int fd() const { return fd_; }
41
42 private:
dalecurtis@chromium.org4f4a8772014-01-23 11:14:28 +090043 const int fd_;
deanm@chromium.org2f749ea2009-06-26 19:00:02 +090044};
45
rvargas@chromium.org4abdbe82012-12-21 11:59:50 +090046base::LazyInstance<URandomFd>::Leaky g_urandom_fd = LAZY_INSTANCE_INITIALIZER;
deanm@chromium.org2f749ea2009-06-26 19:00:02 +090047
48} // namespace
49
mark@chromium.orgb93c0542008-09-30 07:18:01 +090050namespace base {
51
dalecurtis@chromium.org4f4a8772014-01-23 11:14:28 +090052void RandBytes(void* output, size_t output_length) {
53 const int urandom_fd = g_urandom_fd.Pointer()->fd();
54 const bool success =
55 ReadFromFD(urandom_fd, static_cast<char*>(output), output_length);
56 CHECK(success);
57}
58
agl@chromium.orgba637ac2010-03-05 05:18:55 +090059int GetUrandomFD(void) {
60 return g_urandom_fd.Pointer()->fd();
61}
mseaborn@chromium.org056521b2012-05-30 23:26:13 +090062
63} // namespace base