Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
sergeyu@chromium.org | 1968d977 | 2012-07-26 22:53:13 | [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 | |
penghuang@chromium.org | 4a88783 | 2014-06-17 20:28:47 | [diff] [blame] | 7 | #include <nacl/nacl_random.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
penghuang@chromium.org | 4a88783 | 2014-06-17 20:28:47 | [diff] [blame] | 10 | |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 11 | #include "base/check_op.h" |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 12 | #include "base/containers/span.h" |
sergeyu@chromium.org | 1968d977 | 2012-07-26 22:53:13 | [diff] [blame] | 13 | |
sergeyu@chromium.org | 1968d977 | 2012-07-26 22:53:13 | [diff] [blame] | 14 | namespace base { |
| 15 | |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 16 | void RandBytes(span<uint8_t> output) { |
| 17 | while (!output.empty()) { |
John Mellor | afab972d | 2017-09-26 16:28:19 | [diff] [blame] | 18 | size_t nread; |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 19 | const int error = nacl_secure_random(output.data(), output.size(), &nread); |
John Mellor | afab972d | 2017-09-26 16:28:19 | [diff] [blame] | 20 | CHECK_EQ(error, 0); |
Austin Sullivan | a41f7f6 | 2024-01-09 20:11:50 | [diff] [blame] | 21 | CHECK_LE(nread, output.size()); |
| 22 | output = output.subspan(nread); |
John Mellor | afab972d | 2017-09-26 16:28:19 | [diff] [blame] | 23 | } |
dalecurtis@chromium.org | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 24 | } |
| 25 | |
sergeyu@chromium.org | 1968d977 | 2012-07-26 22:53:13 | [diff] [blame] | 26 | } // namespace base |