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
3 // found in the LICENSE file.
5 #include "base/rand_util.h"
7 #include "base/basictypes.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "native_client/src/untrusted/irt/irt.h"
18 nacl_interface_query(NACL_IRT_RANDOM_v0_1
, &random_
, sizeof(random_
));
19 CHECK_EQ(result
, sizeof(random_
));
24 void GetRandomBytes(void* output
, size_t num_bytes
) {
25 char* output_ptr
= static_cast<char*>(output
);
26 while (num_bytes
> 0) {
28 const int error
= random_
.get_random_bytes(output_ptr
, num_bytes
, &nread
);
30 CHECK_LE(nread
, num_bytes
);
37 nacl_irt_random random_
;
40 base::LazyInstance
<NaclRandom
>::Leaky g_nacl_random
= LAZY_INSTANCE_INITIALIZER
;
46 // NOTE: This function must be cryptographically secure. http://crbug.com/140076
49 g_nacl_random
.Pointer()->GetRandomBytes(&result
, sizeof(result
));
53 void RandBytes(void* output
, size_t output_length
) {
54 g_nacl_random
.Pointer()->GetRandomBytes(output
, output_length
);