OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "base/rand_util.h" | |
6 | |
7 #include "base/lazy_instance.h" | |
8 #include "base/logging.h" | |
9 #include "native_client/src/untrusted/irt/irt.h" | |
10 | |
11 namespace { | |
12 | |
13 class NaclRandom { | |
14 public: | |
15 NaclRandom() { | |
16 int success = nacl_interface_query(NACL_IRT_RANDOM_v0_1, | |
17 &random_, sizeof(random_)); | |
18 if (!success) | |
19 LOG(FATAL) << "Failed to initialize RNG."; | |
willchan no longer on Chromium
2012/07/25 17:52:55
Can you split this out into DLOG(ERROR) with the m
Sergey Ulanov
2012/07/25 19:43:23
Replaced with CHECK()
| |
20 } | |
21 | |
22 ~NaclRandom() { | |
23 } | |
24 | |
25 void GetRandomBytes(char* buffer, uint32_t num_bytes) { | |
26 while (num_bytes > 0) { | |
27 size_t nread; | |
28 int error = random_.get_random_bytes(buffer, num_bytes, &nread); | |
29 CHECK(error != 0) << "Failed to get random bytes."; | |
willchan no longer on Chromium
2012/07/25 17:52:55
Ditto here on the CHECKs with messages. Ditch the
Sergey Ulanov
2012/07/25 19:43:23
Done.
| |
30 CHECK_LE(nread, num_bytes); | |
31 buffer += nread; | |
32 num_bytes -= nread; | |
33 } | |
34 } | |
35 | |
36 private: | |
37 nacl_irt_random random_; | |
38 }; | |
39 | |
40 base::LazyInstance<NaclRandom> g_nacl_random = LAZY_INSTANCE_INITIALIZER; | |
willchan no longer on Chromium
2012/07/25 17:52:55
Should this be leaky? I don't know anything about
Sergey Ulanov
2012/07/25 19:43:23
Done.
| |
41 | |
42 } // namespace | |
43 | |
44 namespace base { | |
45 | |
46 uint64 RandUint64() { | |
47 uint64 result; | |
willchan no longer on Chromium
2012/07/25 17:52:55
I think you may need basictypes.h for this type.
Sergey Ulanov
2012/07/25 19:43:23
Done.
| |
48 g_nacl_random.Pointer()->GetRandomBytes( | |
49 reinterpret_cast<char*>(&result), sizeof(result)); | |
50 return result; | |
51 } | |
52 | |
53 } // namespace base | |
OLD | NEW |