xref: /aosp_15_r20/external/libchrome/base/rand_util_posix.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/rand_util.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <errno.h>
8*635a8641SAndroid Build Coastguard Worker #include <fcntl.h>
9*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
10*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
11*635a8641SAndroid Build Coastguard Worker #include <unistd.h>
12*635a8641SAndroid Build Coastguard Worker 
13*635a8641SAndroid Build Coastguard Worker #include "base/files/file_util.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/lazy_instance.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/posix/eintr_wrapper.h"
17*635a8641SAndroid Build Coastguard Worker 
18*635a8641SAndroid Build Coastguard Worker namespace {
19*635a8641SAndroid Build Coastguard Worker 
20*635a8641SAndroid Build Coastguard Worker // We keep the file descriptor for /dev/urandom around so we don't need to
21*635a8641SAndroid Build Coastguard Worker // reopen it (which is expensive), and since we may not even be able to reopen
22*635a8641SAndroid Build Coastguard Worker // it if we are later put in a sandbox. This class wraps the file descriptor so
23*635a8641SAndroid Build Coastguard Worker // we can use LazyInstance to handle opening it on the first access.
24*635a8641SAndroid Build Coastguard Worker class URandomFd {
25*635a8641SAndroid Build Coastguard Worker  public:
26*635a8641SAndroid Build Coastguard Worker #if defined(OS_AIX)
27*635a8641SAndroid Build Coastguard Worker   // AIX has no 64-bit support for open falgs such as -
28*635a8641SAndroid Build Coastguard Worker   //  O_CLOEXEC, O_NOFOLLOW and O_TTY_INIT
URandomFd()29*635a8641SAndroid Build Coastguard Worker   URandomFd() : fd_(HANDLE_EINTR(open("/dev/urandom", O_RDONLY))) {
30*635a8641SAndroid Build Coastguard Worker     DCHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno;
31*635a8641SAndroid Build Coastguard Worker   }
32*635a8641SAndroid Build Coastguard Worker #else
33*635a8641SAndroid Build Coastguard Worker   URandomFd() : fd_(HANDLE_EINTR(open("/dev/urandom", O_RDONLY | O_CLOEXEC))) {
34*635a8641SAndroid Build Coastguard Worker     DCHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno;
35*635a8641SAndroid Build Coastguard Worker   }
36*635a8641SAndroid Build Coastguard Worker #endif
37*635a8641SAndroid Build Coastguard Worker 
~URandomFd()38*635a8641SAndroid Build Coastguard Worker   ~URandomFd() { close(fd_); }
39*635a8641SAndroid Build Coastguard Worker 
fd() const40*635a8641SAndroid Build Coastguard Worker   int fd() const { return fd_; }
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker  private:
43*635a8641SAndroid Build Coastguard Worker   const int fd_;
44*635a8641SAndroid Build Coastguard Worker };
45*635a8641SAndroid Build Coastguard Worker 
46*635a8641SAndroid Build Coastguard Worker base::LazyInstance<URandomFd>::Leaky g_urandom_fd = LAZY_INSTANCE_INITIALIZER;
47*635a8641SAndroid Build Coastguard Worker 
48*635a8641SAndroid Build Coastguard Worker }  // namespace
49*635a8641SAndroid Build Coastguard Worker 
50*635a8641SAndroid Build Coastguard Worker namespace base {
51*635a8641SAndroid Build Coastguard Worker 
RandBytes(void * output,size_t output_length)52*635a8641SAndroid Build Coastguard Worker void RandBytes(void* output, size_t output_length) {
53*635a8641SAndroid Build Coastguard Worker   const int urandom_fd = g_urandom_fd.Pointer()->fd();
54*635a8641SAndroid Build Coastguard Worker   const bool success =
55*635a8641SAndroid Build Coastguard Worker       ReadFromFD(urandom_fd, static_cast<char*>(output), output_length);
56*635a8641SAndroid Build Coastguard Worker   CHECK(success);
57*635a8641SAndroid Build Coastguard Worker }
58*635a8641SAndroid Build Coastguard Worker 
GetUrandomFD(void)59*635a8641SAndroid Build Coastguard Worker int GetUrandomFD(void) {
60*635a8641SAndroid Build Coastguard Worker   return g_urandom_fd.Pointer()->fd();
61*635a8641SAndroid Build Coastguard Worker }
62*635a8641SAndroid Build Coastguard Worker 
63*635a8641SAndroid Build Coastguard Worker }  // namespace base
64