Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/rand_util.h" | 5 #include "base/rand_util.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // We keep the file descriptor for /dev/urandom around so we don't need to | 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 | 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 | 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. | 20 // we can use LazyInstance to handle opening it on the first access. |
| 21 class URandomFd { | 21 class URandomFd { |
| 22 public: | 22 public: |
| 23 URandomFd() { | 23 URandomFd() : fd_(open("/dev/urandom", O_RDONLY)) { |
| 24 fd_ = open("/dev/urandom", O_RDONLY); | |
| 25 DCHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno; | 24 DCHECK_GE(fd_, 0) << "Cannot open /dev/urandom: " << errno; |
| 26 } | 25 } |
| 27 | 26 |
| 28 ~URandomFd() { | 27 ~URandomFd() { close(fd_); } |
| 29 close(fd_); | |
| 30 } | |
| 31 | 28 |
| 32 int fd() const { return fd_; } | 29 int fd() const { return fd_; } |
| 33 | 30 |
| 34 private: | 31 private: |
| 35 int fd_; | 32 const int fd_; |
| 36 }; | 33 }; |
| 37 | 34 |
| 38 base::LazyInstance<URandomFd>::Leaky g_urandom_fd = LAZY_INSTANCE_INITIALIZER; | 35 base::LazyInstance<URandomFd>::Leaky g_urandom_fd = LAZY_INSTANCE_INITIALIZER; |
| 39 | 36 |
| 40 } // namespace | 37 } // namespace |
| 41 | 38 |
| 42 namespace base { | 39 namespace base { |
| 43 | 40 |
| 44 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 | 41 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 |
|
wtc
2014/01/22 22:29:09
This comment should be in front of RandUint64(). I
DaleCurtis
2014/01/22 22:54:14
Done.
| |
| 42 void RandBytes(void* output, size_t output_length) { | |
|
wtc
2014/01/22 22:29:09
Define RandBytes after RandUint64, as the Style Gu
DaleCurtis
2014/01/22 22:54:14
Done.
| |
| 43 const int urandom_fd = g_urandom_fd.Pointer()->fd(); | |
| 44 const bool success = | |
| 45 ReadFromFD(urandom_fd, static_cast<char*>(output), output_length); | |
| 46 CHECK(success); | |
| 47 } | |
| 48 | |
| 45 uint64 RandUint64() { | 49 uint64 RandUint64() { |
| 46 uint64 number; | 50 uint64 number; |
| 47 | 51 RandBytes(&number, sizeof(number)); |
| 48 int urandom_fd = g_urandom_fd.Pointer()->fd(); | |
| 49 bool success = ReadFromFD(urandom_fd, reinterpret_cast<char*>(&number), | |
| 50 sizeof(number)); | |
| 51 CHECK(success); | |
| 52 | |
| 53 return number; | 52 return number; |
| 54 } | 53 } |
| 55 | 54 |
| 56 int GetUrandomFD(void) { | 55 int GetUrandomFD(void) { |
| 57 return g_urandom_fd.Pointer()->fd(); | 56 return g_urandom_fd.Pointer()->fd(); |
| 58 } | 57 } |
| 59 | 58 |
| 60 } // namespace base | 59 } // namespace base |
| OLD | NEW |