[go: nahoru, domu]

blob: 5f991d5cc053db910b02ccc268345db677bb775f [file] [log] [blame]
Scott Graham4ffd63b2017-06-01 18:03:331// Copyright 2017 The Chromium Authors. All rights reserved.
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
Scott Grahamfe0e9f462017-09-18 21:25:047#include <zircon/syscalls.h>
Scott Graham4ffd63b2017-06-01 18:03:338
9#include <algorithm>
10
11#include "base/logging.h"
12
13namespace base {
14
Scott Graham4ffd63b2017-06-01 18:03:3315void RandBytes(void* output, size_t output_length) {
16 size_t remaining = output_length;
17 unsigned char* cur = reinterpret_cast<unsigned char*>(output);
Sergey Ulanovfdc62f8e2017-08-01 19:51:0018 while (remaining > 0) {
Scott Graham4ffd63b2017-06-01 18:03:3319 // The syscall has a maximum number of bytes that can be read at once.
20 size_t read_len =
Scott Grahamfe0e9f462017-09-18 21:25:0421 std::min(remaining, static_cast<size_t>(ZX_CPRNG_DRAW_MAX_LEN));
Scott Graham4ffd63b2017-06-01 18:03:3322
23 size_t actual;
Scott Grahamfe0e9f462017-09-18 21:25:0424 zx_status_t status = zx_cprng_draw(cur, read_len, &actual);
25 CHECK(status == ZX_OK && read_len == actual);
Scott Graham4ffd63b2017-06-01 18:03:3326
27 CHECK(remaining >= actual);
28 remaining -= actual;
29 cur += actual;
Sergey Ulanovfdc62f8e2017-08-01 19:51:0030 }
Scott Graham4ffd63b2017-06-01 18:03:3331}
32
33} // namespace base