Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(680)

Side by Side Diff: runtime/bin/crypto_fuchsia.cc

Issue 2204523002: Fuchsia: Use low-level prng call, add test, update test runner. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/dart-lang/sdk/@master
Patch Set: Fix bug Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/builtin_impl_sources.gypi ('k') | runtime/bin/crypto_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(TARGET_OS_FUCHSIA) 6 #if defined(TARGET_OS_FUCHSIA)
7 7
8 #include "bin/crypto.h" 8 #include "bin/crypto.h"
9 9
10 #include <magenta/syscalls.h>
11
10 namespace dart { 12 namespace dart {
11 namespace bin { 13 namespace bin {
12 14
13 bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) { 15 bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) {
14 uint32_t num;
15 intptr_t read = 0; 16 intptr_t read = 0;
16 while (read < count) { 17 while (read < count) {
17 if (rand_r(&num) != 0) { 18 const intptr_t remaining = count - read;
19 const intptr_t len =
20 (MX_CPRNG_DRAW_MAX_LEN < remaining) ? MX_CPRNG_DRAW_MAX_LEN
21 : remaining;
22 const mx_ssize_t res = mx_cprng_draw(buffer + read, len);
23 if (res == ERR_INVALID_ARGS) {
18 return false; 24 return false;
19 } 25 }
20 for (int i = 0; i < 4 && read < count; i++) { 26 read += res;
21 buffer[read] = num >> (i * 8);
22 read++;
23 }
24 } 27 }
25 return true; 28 return true;
26 } 29 }
27 30
28 } // namespace bin 31 } // namespace bin
29 } // namespace dart 32 } // namespace dart
30 33
31 #endif // defined(TARGET_OS_FUCHSIA) 34 #endif // defined(TARGET_OS_FUCHSIA)
OLDNEW
« no previous file with comments | « runtime/bin/builtin_impl_sources.gypi ('k') | runtime/bin/crypto_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698