blob: 6a6e05ada814d2758a46f82b2097016665179c67 [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"
16
deanm@chromium.org2f749ea2009-06-26 19:00:02 +090017namespace {
18
19// We keep the file descriptor for /dev/urandom around so we don't need to
20// reopen it (which is expensive), and since we may not even be able to reopen
21// it if we are later put in a sandbox. This class wraps the file descriptor so
22// we can use LazyInstance to handle opening it on the first access.
23class URandomFd {
24 public:
dalecurtis@chromium.org4f4a8772014-01-23 11:14:28 +090025 URandomFd() : fd_(open("/dev/urandom", O_RDONLY)) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +090026 DCHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno;
deanm@chromium.org2f749ea2009-06-26 19:00:02 +090027 }
28
dalecurtis@chromium.org4f4a8772014-01-23 11:14:28 +090029 ~URandomFd() { close(fd_); }
deanm@chromium.org2f749ea2009-06-26 19:00:02 +090030
31 int fd() const { return fd_; }
32
33 private:
dalecurtis@chromium.org4f4a8772014-01-23 11:14:28 +090034 const int fd_;
deanm@chromium.org2f749ea2009-06-26 19:00:02 +090035};
36
rvargas@chromium.org4abdbe82012-12-21 11:59:50 +090037base::LazyInstance<URandomFd>::Leaky g_urandom_fd = LAZY_INSTANCE_INITIALIZER;
deanm@chromium.org2f749ea2009-06-26 19:00:02 +090038
39} // namespace
40
mark@chromium.orgb93c0542008-09-30 07:18:01 +090041namespace base {
42
mniknami@chromium.orgf4331ae2012-08-03 05:22:25 +090043// NOTE: This function must be cryptographically secure. http://crbug.com/140076
avia6a6a682015-12-27 07:15:14 +090044uint64_t RandUint64() {
45 uint64_t number;
dalecurtis@chromium.org4f4a8772014-01-23 11:14:28 +090046 RandBytes(&number, sizeof(number));
mark@chromium.orgb93c0542008-09-30 07:18:01 +090047 return number;
48}
49
dalecurtis@chromium.org4f4a8772014-01-23 11:14:28 +090050void RandBytes(void* output, size_t output_length) {
51 const int urandom_fd = g_urandom_fd.Pointer()->fd();
52 const bool success =
53 ReadFromFD(urandom_fd, static_cast<char*>(output), output_length);
54 CHECK(success);
55}
56
agl@chromium.orgba637ac2010-03-05 05:18:55 +090057int GetUrandomFD(void) {
58 return g_urandom_fd.Pointer()->fd();
59}
mseaborn@chromium.org056521b2012-05-30 23:26:13 +090060
61} // namespace base