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 <stddef.h> |
| 10 #include <stdint.h> |
9 #include <unistd.h> | 11 #include <unistd.h> |
10 | 12 |
11 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
12 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
13 #include "base/logging.h" | 15 #include "base/logging.h" |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 // We keep the file descriptor for /dev/urandom around so we don't need to | 19 // 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 | 20 // reopen it (which is expensive), and since we may not even be able to reopen |
(...skipping 13 matching lines...) Expand all Loading... |
32 const int fd_; | 34 const int fd_; |
33 }; | 35 }; |
34 | 36 |
35 base::LazyInstance<URandomFd>::Leaky g_urandom_fd = LAZY_INSTANCE_INITIALIZER; | 37 base::LazyInstance<URandomFd>::Leaky g_urandom_fd = LAZY_INSTANCE_INITIALIZER; |
36 | 38 |
37 } // namespace | 39 } // namespace |
38 | 40 |
39 namespace base { | 41 namespace base { |
40 | 42 |
41 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 | 43 // NOTE: This function must be cryptographically secure. http://crbug.com/140076 |
42 uint64 RandUint64() { | 44 uint64_t RandUint64() { |
43 uint64 number; | 45 uint64_t number; |
44 RandBytes(&number, sizeof(number)); | 46 RandBytes(&number, sizeof(number)); |
45 return number; | 47 return number; |
46 } | 48 } |
47 | 49 |
48 void RandBytes(void* output, size_t output_length) { | 50 void RandBytes(void* output, size_t output_length) { |
49 const int urandom_fd = g_urandom_fd.Pointer()->fd(); | 51 const int urandom_fd = g_urandom_fd.Pointer()->fd(); |
50 const bool success = | 52 const bool success = |
51 ReadFromFD(urandom_fd, static_cast<char*>(output), output_length); | 53 ReadFromFD(urandom_fd, static_cast<char*>(output), output_length); |
52 CHECK(success); | 54 CHECK(success); |
53 } | 55 } |
54 | 56 |
55 int GetUrandomFD(void) { | 57 int GetUrandomFD(void) { |
56 return g_urandom_fd.Pointer()->fd(); | 58 return g_urandom_fd.Pointer()->fd(); |
57 } | 59 } |
58 | 60 |
59 } // namespace base | 61 } // namespace base |
OLD | NEW |