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

Side by Side Diff: runtime/lib/math.cc

Issue 1398453004: Add a 'secure' constructor to Random in dart:math returning a cryptographically (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: address review comments Created 5 years, 2 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 | « no previous file | runtime/lib/math_patch.dart » ('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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 <ctype.h> // isspace. 5 #include <ctype.h> // isspace.
6 6
7 #include "vm/bootstrap_natives.h" 7 #include "vm/bootstrap_natives.h"
8 8
9 #include "vm/exceptions.h" 9 #include "vm/exceptions.h"
10 #include "vm/native_entry.h" 10 #include "vm/native_entry.h"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 ASSERT(!state_field_value.IsNull()); 86 ASSERT(!state_field_value.IsNull());
87 ASSERT(state_field_value.IsTypedData()); 87 ASSERT(state_field_value.IsTypedData());
88 const TypedData& array = TypedData::Cast(state_field_value); 88 const TypedData& array = TypedData::Cast(state_field_value);
89 ASSERT(array.Length() == 2); 89 ASSERT(array.Length() == 2);
90 ASSERT(array.ElementType() == kUint32ArrayElement); 90 ASSERT(array.ElementType() == kUint32ArrayElement);
91 return array.raw(); 91 return array.raw();
92 } 92 }
93 93
94 94
95 // Implements: 95 // Implements:
96 // var state = ((_A * (_state[kSTATE_LO])) + _state[kSTATE_HI]) & _MASK_64; 96 // var state =
97 // _state[kSTATE_LO] = state & _MASK_32; 97 // ((_A * (_state[_kSTATE_LO])) + _state[_kSTATE_HI]) & (1 << 64) - 1);
98 // _state[kSTATE_HI] = state >> 32; 98 // _state[_kSTATE_LO] = state & (1 << 32) - 1);
99 // _state[_kSTATE_HI] = state >> 32;
99 DEFINE_NATIVE_ENTRY(Random_nextState, 1) { 100 DEFINE_NATIVE_ENTRY(Random_nextState, 1) {
100 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0)); 101 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0));
101 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver)); 102 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver));
102 const uint64_t state_lo = array.GetUint32(0); 103 const uint64_t state_lo = array.GetUint32(0);
103 const uint64_t state_hi = array.GetUint32(array.ElementSizeInBytes()); 104 const uint64_t state_hi = array.GetUint32(array.ElementSizeInBytes());
104 const uint64_t A = 0xffffda61; 105 const uint64_t A = 0xffffda61;
105 uint64_t state = (A * state_lo) + state_hi; 106 uint64_t state = (A * state_lo) + state_hi;
106 array.SetUint32(0, static_cast<uint32_t>(state)); 107 array.SetUint32(0, static_cast<uint32_t>(state));
107 array.SetUint32(array.ElementSizeInBytes(), 108 array.SetUint32(array.ElementSizeInBytes(),
108 static_cast<uint32_t>(state >> 32)); 109 static_cast<uint32_t>(state >> 32));
(...skipping 29 matching lines...) Expand all
138 // Implements: 139 // Implements:
139 // uint64_t hash = 0; 140 // uint64_t hash = 0;
140 // do { 141 // do {
141 // hash = hash * 1037 ^ mix64((uint64_t)seed); 142 // hash = hash * 1037 ^ mix64((uint64_t)seed);
142 // seed >>= 64; 143 // seed >>= 64;
143 // } while (seed != 0 && seed != -1); // Limits if seed positive or negative. 144 // } while (seed != 0 && seed != -1); // Limits if seed positive or negative.
144 // if (hash == 0) { 145 // if (hash == 0) {
145 // hash = 0x5A17; 146 // hash = 0x5A17;
146 // } 147 // }
147 // var result = new Uint32List(2); 148 // var result = new Uint32List(2);
148 // result[kSTATE_LO] = seed & _MASK_32; 149 // result[_kSTATE_LO] = seed & ((1 << 32) - 1);
149 // result[kSTATE_HI] = seed >> 32; 150 // result[_kSTATE_HI] = seed >> 32;
150 // return result; 151 // return result;
151 DEFINE_NATIVE_ENTRY(Random_setupSeed, 1) { 152 DEFINE_NATIVE_ENTRY(Random_setupSeed, 1) {
152 GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(0)); 153 GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(0));
153 uint64_t seed = 0; 154 uint64_t seed = 0;
154 if (seed_int.IsBigint()) { 155 if (seed_int.IsBigint()) {
155 Bigint& big_seed = Bigint::Handle(); 156 Bigint& big_seed = Bigint::Handle();
156 big_seed ^= seed_int.raw(); 157 big_seed ^= seed_int.raw();
157 uint64_t negate_mask = 0; 158 uint64_t negate_mask = 0;
158 uint64_t borrow = 0; 159 uint64_t borrow = 0;
159 if (big_seed.IsNegative()) { 160 if (big_seed.IsNegative()) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 } 192 }
192 193
193 194
194 DEFINE_NATIVE_ENTRY(Random_initialSeed, 0) { 195 DEFINE_NATIVE_ENTRY(Random_initialSeed, 0) {
195 Random* rnd = isolate->random(); 196 Random* rnd = isolate->random();
196 uint64_t seed = rnd->NextUInt32(); 197 uint64_t seed = rnd->NextUInt32();
197 seed |= (static_cast<uint64_t>(rnd->NextUInt32()) << 32); 198 seed |= (static_cast<uint64_t>(rnd->NextUInt32()) << 32);
198 return CreateRandomState(zone, seed); 199 return CreateRandomState(zone, seed);
199 } 200 }
200 201
202
203 DEFINE_NATIVE_ENTRY(SecureRandom_getBytes, 1) {
204 GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(0));
205 const intptr_t n = count.Value();
206 ASSERT((n > 0) && (n <= 8));
207 uint8_t buffer[8];
208 Dart_EntropySource entropy_source = isolate->entropy_source_callback();
209 if ((entropy_source == NULL) || !entropy_source(buffer, n)) {
210 const String& error = String::Handle(String::New(
211 "No source of cryptographically secure random numbers available."));
212 const Array& args = Array::Handle(Array::New(1));
213 args.SetAt(0, error);
214 Exceptions::ThrowByType(Exceptions::kUnsupported, args);
215 }
216 uint64_t result = 0;
217 for (intptr_t i = 0; i < n; i++) {
218 result = (result << 8) | buffer[i];
219 }
220 return Integer::New(result);
221 }
222
201 } // namespace dart 223 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/math_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698