mseaborn@chromium.org | 056521b | 2012-05-30 23:26:13 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [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 | |
deanm@chromium.org | 2f749ea | 2009-06-26 19:00:02 +0900 | [diff] [blame] | 7 | #include <errno.h> |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 8 | #include <fcntl.h> |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 9 | #include <unistd.h> |
| 10 | |
brettw@chromium.org | 01f3da4 | 2014-08-14 05:22:14 +0900 | [diff] [blame^] | 11 | #include "base/files/file_util.h" |
deanm@chromium.org | 2f749ea | 2009-06-26 19:00:02 +0900 | [diff] [blame] | 12 | #include "base/lazy_instance.h" |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 13 | #include "base/logging.h" |
| 14 | |
deanm@chromium.org | 2f749ea | 2009-06-26 19:00:02 +0900 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | // We keep the file descriptor for /dev/urandom around so we don't need to |
| 18 | // reopen it (which is expensive), and since we may not even be able to reopen |
| 19 | // it if we are later put in a sandbox. This class wraps the file descriptor so |
| 20 | // we can use LazyInstance to handle opening it on the first access. |
| 21 | class URandomFd { |
| 22 | public: |
dalecurtis@chromium.org | 4f4a877 | 2014-01-23 11:14:28 +0900 | [diff] [blame] | 23 | URandomFd() : fd_(open("/dev/urandom", O_RDONLY)) { |
brettw@chromium.org | 5faed3c | 2011-10-27 06:48:00 +0900 | [diff] [blame] | 24 | DCHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno; |
deanm@chromium.org | 2f749ea | 2009-06-26 19:00:02 +0900 | [diff] [blame] | 25 | } |
| 26 | |
dalecurtis@chromium.org | 4f4a877 | 2014-01-23 11:14:28 +0900 | [diff] [blame] | 27 | ~URandomFd() { close(fd_); } |
deanm@chromium.org | 2f749ea | 2009-06-26 19:00:02 +0900 | [diff] [blame] | 28 | |
| 29 | int fd() const { return fd_; } |
| 30 | |
| 31 | private: |
dalecurtis@chromium.org | 4f4a877 | 2014-01-23 11:14:28 +0900 | [diff] [blame] | 32 | const int fd_; |
deanm@chromium.org | 2f749ea | 2009-06-26 19:00:02 +0900 | [diff] [blame] | 33 | }; |
| 34 | |
rvargas@chromium.org | 4abdbe8 | 2012-12-21 11:59:50 +0900 | [diff] [blame] | 35 | base::LazyInstance<URandomFd>::Leaky g_urandom_fd = LAZY_INSTANCE_INITIALIZER; |
deanm@chromium.org | 2f749ea | 2009-06-26 19:00:02 +0900 | [diff] [blame] | 36 | |
| 37 | } // namespace |
| 38 | |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 39 | namespace base { |
| 40 | |
mniknami@chromium.org | f4331ae | 2012-08-03 05:22:25 +0900 | [diff] [blame] | 41 | // NOTE: This function must be cryptographically secure. http://crbug.com/140076 |
deanm@chromium.org | 48c40ce | 2008-11-15 08:28:29 +0900 | [diff] [blame] | 42 | uint64 RandUint64() { |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 43 | uint64 number; |
dalecurtis@chromium.org | 4f4a877 | 2014-01-23 11:14:28 +0900 | [diff] [blame] | 44 | RandBytes(&number, sizeof(number)); |
mark@chromium.org | b93c054 | 2008-09-30 07:18:01 +0900 | [diff] [blame] | 45 | return number; |
| 46 | } |
| 47 | |
dalecurtis@chromium.org | 4f4a877 | 2014-01-23 11:14:28 +0900 | [diff] [blame] | 48 | void RandBytes(void* output, size_t output_length) { |
| 49 | const int urandom_fd = g_urandom_fd.Pointer()->fd(); |
| 50 | const bool success = |
| 51 | ReadFromFD(urandom_fd, static_cast<char*>(output), output_length); |
| 52 | CHECK(success); |
| 53 | } |
| 54 | |
agl@chromium.org | ba637ac | 2010-03-05 05:18:55 +0900 | [diff] [blame] | 55 | int GetUrandomFD(void) { |
| 56 | return g_urandom_fd.Pointer()->fd(); |
| 57 | } |
mseaborn@chromium.org | 056521b | 2012-05-30 23:26:13 +0900 | [diff] [blame] | 58 | |
| 59 | } // namespace base |