OLD | NEW |
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> | 10 #include <magenta/syscalls.h> |
11 | 11 |
12 namespace dart { | 12 namespace dart { |
13 namespace bin { | 13 namespace bin { |
14 | 14 |
15 bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) { | 15 bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) { |
16 intptr_t read = 0; | 16 intptr_t read = 0; |
17 while (read < count) { | 17 while (read < count) { |
18 const intptr_t remaining = count - read; | 18 const intptr_t remaining = count - read; |
19 const intptr_t len = | 19 const intptr_t len = |
20 (MX_CPRNG_DRAW_MAX_LEN < remaining) ? MX_CPRNG_DRAW_MAX_LEN | 20 (MX_CPRNG_DRAW_MAX_LEN < remaining) ? MX_CPRNG_DRAW_MAX_LEN : remaining; |
21 : remaining; | |
22 mx_size_t res = 0; | 21 mx_size_t res = 0; |
23 const mx_status_t status = mx_cprng_draw(buffer + read, len, &res); | 22 const mx_status_t status = mx_cprng_draw(buffer + read, len, &res); |
24 if (status != NO_ERROR) { | 23 if (status != NO_ERROR) { |
25 return false; | 24 return false; |
26 } | 25 } |
27 read += res; | 26 read += res; |
28 } | 27 } |
29 return true; | 28 return true; |
30 } | 29 } |
31 | 30 |
32 } // namespace bin | 31 } // namespace bin |
33 } // namespace dart | 32 } // namespace dart |
34 | 33 |
35 #endif // defined(TARGET_OS_FUCHSIA) | 34 #endif // defined(TARGET_OS_FUCHSIA) |
OLD | NEW |