summaryrefslogtreecommitdiff
path: root/base/rand_util_nacl.cc
blob: b7f2395464ffedc267fb93e34bd5841aa2bd54c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/rand_util.h"

#include <nacl/nacl_random.h>
#include <stddef.h>
#include <stdint.h>

#include "base/check_op.h"
#include "base/containers/span.h"

namespace base {

void RandBytes(span<uint8_t> output) {
  while (!output.empty()) {
    size_t nread;
    const int error = nacl_secure_random(output.data(), output.size(), &nread);
    CHECK_EQ(error, 0);
    CHECK_LE(nread, output.size());
    output = output.subspan(nread);
  }
}

void RandBytes(void* output, size_t output_length) {
  RandBytes(make_span(static_cast<uint8_t*>(output), output_length));
}

}  // namespace base