xref: /aosp_15_r20/external/cronet/base/rand_util_nacl.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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 
7 #include <nacl/nacl_random.h>
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include "base/check_op.h"
12 #include "base/containers/span.h"
13 
14 namespace base {
15 
RandBytes(span<uint8_t> output)16 void RandBytes(span<uint8_t> output) {
17   while (!output.empty()) {
18     size_t nread;
19     const int error = nacl_secure_random(output.data(), output.size(), &nread);
20     CHECK_EQ(error, 0);
21     CHECK_LE(nread, output.size());
22     output = output.subspan(nread);
23   }
24 }
25 
RandBytes(void * output,size_t output_length)26 void RandBytes(void* output, size_t output_length) {
27   RandBytes(make_span(static_cast<uint8_t*>(output), output_length));
28 }
29 
30 }  // namespace base
31