author | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2014-06-21 08:50:00 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2014-06-21 09:20:16 (UTC) |
commit | 6dd16d9359e3a4dc306802588b09acd43947a606 (patch) (unidiff) | |
tree | f3ab7778e037e42cb47c810f9d435d40de5adf15 /frontend/gamma | |
parent | a6852c93138f3c4596fb4df8bce5b7d19ef50478 (diff) | |
download | clipperz-6dd16d9359e3a4dc306802588b09acd43947a606.zip clipperz-6dd16d9359e3a4dc306802588b09acd43947a606.tar.gz clipperz-6dd16d9359e3a4dc306802588b09acd43947a606.tar.bz2 |
Inproved PRNG configuration
-rw-r--r-- | frontend/gamma/js/Clipperz/Crypto/PRNG.js | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/frontend/gamma/js/Clipperz/Crypto/PRNG.js b/frontend/gamma/js/Clipperz/Crypto/PRNG.js index 7885429..80d972f 100644 --- a/frontend/gamma/js/Clipperz/Crypto/PRNG.js +++ b/frontend/gamma/js/Clipperz/Crypto/PRNG.js | |||
@@ -65,515 +65,515 @@ Clipperz.Crypto.PRNG.EntropyAccumulator.prototype = MochiKit.Base.update(null, { | |||
65 | }, | 65 | }, |
66 | 66 | ||
67 | 'resetStack': function() { | 67 | 'resetStack': function() { |
68 | this.stack().reset(); | 68 | this.stack().reset(); |
69 | }, | 69 | }, |
70 | 70 | ||
71 | 'maxStackLengthBeforeHashing': function() { | 71 | 'maxStackLengthBeforeHashing': function() { |
72 | return this._maxStackLengthBeforeHashing; | 72 | return this._maxStackLengthBeforeHashing; |
73 | }, | 73 | }, |
74 | 74 | ||
75 | //------------------------------------------------------------------------- | 75 | //------------------------------------------------------------------------- |
76 | 76 | ||
77 | 'addRandomByte': function(aValue) { | 77 | 'addRandomByte': function(aValue) { |
78 | this.stack().appendByte(aValue); | 78 | this.stack().appendByte(aValue); |
79 | 79 | ||
80 | if (this.stack().length() > this.maxStackLengthBeforeHashing()) { | 80 | if (this.stack().length() > this.maxStackLengthBeforeHashing()) { |
81 | this.setStack(Clipperz.Crypto.SHA.sha_d256(this.stack())); | 81 | this.setStack(Clipperz.Crypto.SHA.sha_d256(this.stack())); |
82 | } | 82 | } |
83 | }, | 83 | }, |
84 | 84 | ||
85 | //------------------------------------------------------------------------- | 85 | //------------------------------------------------------------------------- |
86 | __syntaxFix__: "syntax fix" | 86 | __syntaxFix__: "syntax fix" |
87 | }); | 87 | }); |
88 | 88 | ||
89 | //############################################################################# | 89 | //############################################################################# |
90 | 90 | ||
91 | Clipperz.Crypto.PRNG.RandomnessSource = function(args) { | 91 | Clipperz.Crypto.PRNG.RandomnessSource = function(args) { |
92 | args = args || {}; | 92 | args = args || {}; |
93 | MochiKit.Base.bindMethods(this); | 93 | MochiKit.Base.bindMethods(this); |
94 | 94 | ||
95 | this._generator = args.generator || null; | 95 | this._generator = args.generator || null; |
96 | this._sourceId = args.sourceId || null; | 96 | this._sourceId = args.sourceId || null; |
97 | this._boostMode = args.boostMode || false; | 97 | this._boostMode = args.boostMode || false; |
98 | 98 | ||
99 | this._nextPoolIndex = 0; | 99 | this._nextPoolIndex = 0; |
100 | 100 | ||
101 | return this; | 101 | return this; |
102 | } | 102 | } |
103 | 103 | ||
104 | Clipperz.Crypto.PRNG.RandomnessSource.prototype = MochiKit.Base.update(null, { | 104 | Clipperz.Crypto.PRNG.RandomnessSource.prototype = MochiKit.Base.update(null, { |
105 | 105 | ||
106 | 'generator': function() { | 106 | 'generator': function() { |
107 | return this._generator; | 107 | return this._generator; |
108 | }, | 108 | }, |
109 | 109 | ||
110 | 'setGenerator': function(aValue) { | 110 | 'setGenerator': function(aValue) { |
111 | this._generator = aValue; | 111 | this._generator = aValue; |
112 | }, | 112 | }, |
113 | 113 | ||
114 | //------------------------------------------------------------------------- | 114 | //------------------------------------------------------------------------- |
115 | 115 | ||
116 | 'boostMode': function() { | 116 | 'boostMode': function() { |
117 | return this._boostMode; | 117 | return this._boostMode; |
118 | }, | 118 | }, |
119 | 119 | ||
120 | 'setBoostMode': function(aValue) { | 120 | 'setBoostMode': function(aValue) { |
121 | this._boostMode = aValue; | 121 | this._boostMode = aValue; |
122 | }, | 122 | }, |
123 | 123 | ||
124 | //------------------------------------------------------------------------- | 124 | //------------------------------------------------------------------------- |
125 | 125 | ||
126 | 'sourceId': function() { | 126 | 'sourceId': function() { |
127 | return this._sourceId; | 127 | return this._sourceId; |
128 | }, | 128 | }, |
129 | 129 | ||
130 | 'setSourceId': function(aValue) { | 130 | 'setSourceId': function(aValue) { |
131 | this._sourceId = aValue; | 131 | this._sourceId = aValue; |
132 | }, | 132 | }, |
133 | 133 | ||
134 | //------------------------------------------------------------------------- | 134 | //------------------------------------------------------------------------- |
135 | 135 | ||
136 | 'nextPoolIndex': function() { | 136 | 'nextPoolIndex': function() { |
137 | return this._nextPoolIndex; | 137 | return this._nextPoolIndex; |
138 | }, | 138 | }, |
139 | 139 | ||
140 | 'incrementNextPoolIndex': function() { | 140 | 'incrementNextPoolIndex': function() { |
141 | this._nextPoolIndex = ((this._nextPoolIndex + 1) % this.generator().numberOfEntropyAccumulators()); | 141 | this._nextPoolIndex = ((this._nextPoolIndex + 1) % this.generator().numberOfEntropyAccumulators()); |
142 | }, | 142 | }, |
143 | 143 | ||
144 | //------------------------------------------------------------------------- | 144 | //------------------------------------------------------------------------- |
145 | 145 | ||
146 | 'updateGeneratorWithValue': function(aRandomValue) { | 146 | 'updateGeneratorWithValue': function(aRandomValue) { |
147 | if (this.generator() != null) { | 147 | if (this.generator() != null) { |
148 | this.generator().addRandomByte(this.sourceId(), this.nextPoolIndex(), aRandomValue); | 148 | this.generator().addRandomByte(this.sourceId(), this.nextPoolIndex(), aRandomValue); |
149 | this.incrementNextPoolIndex(); | 149 | this.incrementNextPoolIndex(); |
150 | } | 150 | } |
151 | }, | 151 | }, |
152 | 152 | ||
153 | //------------------------------------------------------------------------- | 153 | //------------------------------------------------------------------------- |
154 | __syntaxFix__: "syntax fix" | 154 | __syntaxFix__: "syntax fix" |
155 | }); | 155 | }); |
156 | 156 | ||
157 | //############################################################################# | 157 | //############################################################################# |
158 | 158 | ||
159 | Clipperz.Crypto.PRNG.TimeRandomnessSource = function(args) { | 159 | Clipperz.Crypto.PRNG.TimeRandomnessSource = function(args) { |
160 | args = args || {}; | 160 | args = args || {}; |
161 | //MochiKit.Base.bindMethods(this); | 161 | //MochiKit.Base.bindMethods(this); |
162 | 162 | ||
163 | this._intervalTime = args.intervalTime || 1000; | 163 | this._intervalTime = args.intervalTime || 1000; |
164 | 164 | ||
165 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 165 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
166 | 166 | ||
167 | this.collectEntropy(); | 167 | this.collectEntropy(); |
168 | return this; | 168 | return this; |
169 | } | 169 | } |
170 | 170 | ||
171 | Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 171 | Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
172 | 172 | ||
173 | 'intervalTime': function() { | 173 | 'intervalTime': function() { |
174 | return this._intervalTime; | 174 | return this._intervalTime; |
175 | }, | 175 | }, |
176 | 176 | ||
177 | //------------------------------------------------------------------------- | 177 | //------------------------------------------------------------------------- |
178 | 178 | ||
179 | 'collectEntropy': function() { | 179 | 'collectEntropy': function() { |
180 | varnow; | 180 | varnow; |
181 | varentropyByte; | 181 | varentropyByte; |
182 | var intervalTime; | 182 | var intervalTime; |
183 | now = new Date(); | 183 | now = new Date(); |
184 | entropyByte = (now.getTime() & 0xff); | 184 | entropyByte = (now.getTime() & 0xff); |
185 | 185 | ||
186 | intervalTime = this.intervalTime(); | 186 | intervalTime = this.intervalTime(); |
187 | if (this.boostMode() == true) { | 187 | if (this.boostMode() == true) { |
188 | intervalTime = intervalTime / 9; | 188 | intervalTime = intervalTime / 9; |
189 | } | 189 | } |
190 | 190 | ||
191 | this.updateGeneratorWithValue(entropyByte); | 191 | this.updateGeneratorWithValue(entropyByte); |
192 | setTimeout(this.collectEntropy, intervalTime); | 192 | setTimeout(this.collectEntropy, intervalTime); |
193 | }, | 193 | }, |
194 | 194 | ||
195 | //------------------------------------------------------------------------- | 195 | //------------------------------------------------------------------------- |
196 | 196 | ||
197 | 'numberOfRandomBits': function() { | 197 | 'numberOfRandomBits': function() { |
198 | return 5; | 198 | return 5; |
199 | }, | 199 | }, |
200 | 200 | ||
201 | //------------------------------------------------------------------------- | 201 | //------------------------------------------------------------------------- |
202 | __syntaxFix__: "syntax fix" | 202 | __syntaxFix__: "syntax fix" |
203 | }); | 203 | }); |
204 | 204 | ||
205 | //***************************************************************************** | 205 | //***************************************************************************** |
206 | 206 | ||
207 | Clipperz.Crypto.PRNG.MouseRandomnessSource = function(args) { | 207 | Clipperz.Crypto.PRNG.MouseRandomnessSource = function(args) { |
208 | args = args || {}; | 208 | args = args || {}; |
209 | 209 | ||
210 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 210 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
211 | 211 | ||
212 | this._numberOfBitsToCollectAtEachEvent = 4; | 212 | this._numberOfBitsToCollectAtEachEvent = 4; |
213 | this._randomBitsCollector = 0; | 213 | this._randomBitsCollector = 0; |
214 | this._numberOfRandomBitsCollected = 0; | 214 | this._numberOfRandomBitsCollected = 0; |
215 | 215 | ||
216 | MochiKit.Signal.connect(document, 'onmousemove', this, 'collectEntropy'); | 216 | MochiKit.Signal.connect(document, 'onmousemove', this, 'collectEntropy'); |
217 | 217 | ||
218 | return this; | 218 | return this; |
219 | } | 219 | } |
220 | 220 | ||
221 | Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 221 | Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
222 | 222 | ||
223 | //------------------------------------------------------------------------- | 223 | //------------------------------------------------------------------------- |
224 | 224 | ||
225 | 'numberOfBitsToCollectAtEachEvent': function() { | 225 | 'numberOfBitsToCollectAtEachEvent': function() { |
226 | return this._numberOfBitsToCollectAtEachEvent; | 226 | return this._numberOfBitsToCollectAtEachEvent; |
227 | }, | 227 | }, |
228 | 228 | ||
229 | //------------------------------------------------------------------------- | 229 | //------------------------------------------------------------------------- |
230 | 230 | ||
231 | 'randomBitsCollector': function() { | 231 | 'randomBitsCollector': function() { |
232 | return this._randomBitsCollector; | 232 | return this._randomBitsCollector; |
233 | }, | 233 | }, |
234 | 234 | ||
235 | 'setRandomBitsCollector': function(aValue) { | 235 | 'setRandomBitsCollector': function(aValue) { |
236 | this._randomBitsCollector = aValue; | 236 | this._randomBitsCollector = aValue; |
237 | }, | 237 | }, |
238 | 238 | ||
239 | 'appendRandomBitsToRandomBitsCollector': function(aValue) { | 239 | 'appendRandomBitsToRandomBitsCollector': function(aValue) { |
240 | var collectedBits; | 240 | var collectedBits; |
241 | var numberOfRandomBitsCollected; | 241 | var numberOfRandomBitsCollected; |
242 | 242 | ||
243 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); | 243 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); |
244 | collectedBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); | 244 | collectedBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); |
245 | this.setRandomBitsCollector(collectedBits); | 245 | this.setRandomBitsCollector(collectedBits); |
246 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); | 246 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); |
247 | 247 | ||
248 | if (numberOfRandomBitsCollected == 8) { | 248 | if (numberOfRandomBitsCollected == 8) { |
249 | this.updateGeneratorWithValue(collectedBits); | 249 | this.updateGeneratorWithValue(collectedBits); |
250 | numberOfRandomBitsCollected = 0; | 250 | numberOfRandomBitsCollected = 0; |
251 | this.setRandomBitsCollector(0); | 251 | this.setRandomBitsCollector(0); |
252 | } | 252 | } |
253 | 253 | ||
254 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) | 254 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) |
255 | }, | 255 | }, |
256 | 256 | ||
257 | //------------------------------------------------------------------------- | 257 | //------------------------------------------------------------------------- |
258 | 258 | ||
259 | 'numberOfRandomBitsCollected': function() { | 259 | 'numberOfRandomBitsCollected': function() { |
260 | return this._numberOfRandomBitsCollected; | 260 | return this._numberOfRandomBitsCollected; |
261 | }, | 261 | }, |
262 | 262 | ||
263 | 'setNumberOfRandomBitsCollected': function(aValue) { | 263 | 'setNumberOfRandomBitsCollected': function(aValue) { |
264 | this._numberOfRandomBitsCollected = aValue; | 264 | this._numberOfRandomBitsCollected = aValue; |
265 | }, | 265 | }, |
266 | 266 | ||
267 | //------------------------------------------------------------------------- | 267 | //------------------------------------------------------------------------- |
268 | 268 | ||
269 | 'collectEntropy': function(anEvent) { | 269 | 'collectEntropy': function(anEvent) { |
270 | var mouseLocation; | 270 | var mouseLocation; |
271 | var randomBit; | 271 | var randomBit; |
272 | var mask; | 272 | var mask; |
273 | 273 | ||
274 | mask = 0xffffffff >>> (32 - this.numberOfBitsToCollectAtEachEvent()); | 274 | mask = 0xffffffff >>> (32 - this.numberOfBitsToCollectAtEachEvent()); |
275 | 275 | ||
276 | mouseLocation = anEvent.mouse().client; | 276 | mouseLocation = anEvent.mouse().client; |
277 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & mask); | 277 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & mask); |
278 | this.appendRandomBitsToRandomBitsCollector(randomBit) | 278 | this.appendRandomBitsToRandomBitsCollector(randomBit) |
279 | }, | 279 | }, |
280 | 280 | ||
281 | //------------------------------------------------------------------------- | 281 | //------------------------------------------------------------------------- |
282 | 282 | ||
283 | 'numberOfRandomBits': function() { | 283 | 'numberOfRandomBits': function() { |
284 | return 1; | 284 | return 1; |
285 | }, | 285 | }, |
286 | 286 | ||
287 | //------------------------------------------------------------------------- | 287 | //------------------------------------------------------------------------- |
288 | __syntaxFix__: "syntax fix" | 288 | __syntaxFix__: "syntax fix" |
289 | }); | 289 | }); |
290 | 290 | ||
291 | //***************************************************************************** | 291 | //***************************************************************************** |
292 | 292 | ||
293 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource = function(args) { | 293 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource = function(args) { |
294 | args = args || {}; | 294 | args = args || {}; |
295 | 295 | ||
296 | this._intervalTime = args.intervalTime || 1000; | 296 | this._intervalTime = args.intervalTime || 1000; |
297 | this._browserCrypto = args.browserCrypto; | 297 | this._browserCrypto = args.browserCrypto; |
298 | 298 | ||
299 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 299 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
300 | 300 | ||
301 | this.collectEntropy(); | 301 | this.collectEntropy(); |
302 | return this; | 302 | return this; |
303 | } | 303 | } |
304 | 304 | ||
305 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 305 | Clipperz.Crypto.PRNG.CryptoRandomRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
306 | 306 | ||
307 | 'intervalTime': function() { | 307 | 'intervalTime': function() { |
308 | return this._intervalTime; | 308 | return this._intervalTime; |
309 | }, | 309 | }, |
310 | 310 | ||
311 | 'browserCrypto': function () { | 311 | 'browserCrypto': function () { |
312 | return this._browserCrypto; | 312 | return this._browserCrypto; |
313 | }, | 313 | }, |
314 | 314 | ||
315 | //------------------------------------------------------------------------- | 315 | //------------------------------------------------------------------------- |
316 | 316 | ||
317 | 'collectEntropy': function() { | 317 | 'collectEntropy': function() { |
318 | varbytesToCollect; | 318 | varbytesToCollect; |
319 | 319 | ||
320 | if (this.boostMode() == true) { | 320 | if (this.boostMode() == true) { |
321 | bytesToCollect = 8; | 321 | bytesToCollect = 64; |
322 | } else { | 322 | } else { |
323 | bytesToCollect = 32; | 323 | bytesToCollect = 8; |
324 | } | 324 | } |
325 | 325 | ||
326 | var randomValuesArray = new Uint8Array(bytesToCollect); | 326 | var randomValuesArray = new Uint8Array(bytesToCollect); |
327 | this.browserCrypto().getRandomValues(randomValuesArray); | 327 | this.browserCrypto().getRandomValues(randomValuesArray); |
328 | for (var i = 0; i < randomValuesArray.length; i++) { | 328 | for (var i = 0; i < randomValuesArray.length; i++) { |
329 | this.updateGeneratorWithValue(randomValuesArray[i]); | 329 | this.updateGeneratorWithValue(randomValuesArray[i]); |
330 | } | 330 | } |
331 | 331 | ||
332 | setTimeout(this.collectEntropy, this.intervalTime()); | 332 | setTimeout(this.collectEntropy, this.intervalTime()); |
333 | }, | 333 | }, |
334 | 334 | ||
335 | //------------------------------------------------------------------------- | 335 | //------------------------------------------------------------------------- |
336 | __syntaxFix__: "syntax fix" | 336 | __syntaxFix__: "syntax fix" |
337 | }); | 337 | }); |
338 | 338 | ||
339 | //############################################################################# | 339 | //############################################################################# |
340 | 340 | ||
341 | Clipperz.Crypto.PRNG.Fortuna = function(args) { | 341 | Clipperz.Crypto.PRNG.Fortuna = function(args) { |
342 | vari,c; | 342 | vari,c; |
343 | 343 | ||
344 | args = args || {}; | 344 | args = args || {}; |
345 | 345 | ||
346 | this._key = args.seed || null; | 346 | this._key = args.seed || null; |
347 | if (this._key == null) { | 347 | if (this._key == null) { |
348 | this._counter = 0; | 348 | this._counter = 0; |
349 | this._key = new Clipperz.ByteArray(); | 349 | this._key = new Clipperz.ByteArray(); |
350 | } else { | 350 | } else { |
351 | this._counter = 1; | 351 | this._counter = 1; |
352 | } | 352 | } |
353 | 353 | ||
354 | this._aesKey = null; | 354 | this._aesKey = null; |
355 | 355 | ||
356 | this._firstPoolReseedLevel = args.firstPoolReseedLevel || 32 || 64; | 356 | this._firstPoolReseedLevel = args.firstPoolReseedLevel || 32 || 64; |
357 | this._numberOfEntropyAccumulators = args.numberOfEntropyAccumulators || 32; | 357 | this._numberOfEntropyAccumulators = args.numberOfEntropyAccumulators || 32; |
358 | 358 | ||
359 | this._accumulators = []; | 359 | this._accumulators = []; |
360 | c = this.numberOfEntropyAccumulators(); | 360 | c = this.numberOfEntropyAccumulators(); |
361 | for (i=0; i<c; i++) { | 361 | for (i=0; i<c; i++) { |
362 | this._accumulators.push(new Clipperz.Crypto.PRNG.EntropyAccumulator()); | 362 | this._accumulators.push(new Clipperz.Crypto.PRNG.EntropyAccumulator()); |
363 | } | 363 | } |
364 | 364 | ||
365 | this._randomnessSources = []; | 365 | this._randomnessSources = []; |
366 | this._reseedCounter = 0; | 366 | this._reseedCounter = 0; |
367 | 367 | ||
368 | return this; | 368 | return this; |
369 | } | 369 | } |
370 | 370 | ||
371 | Clipperz.Crypto.PRNG.Fortuna.prototype = MochiKit.Base.update(null, { | 371 | Clipperz.Crypto.PRNG.Fortuna.prototype = MochiKit.Base.update(null, { |
372 | 372 | ||
373 | 'toString': function() { | 373 | 'toString': function() { |
374 | return "Clipperz.Crypto.PRNG.Fortuna"; | 374 | return "Clipperz.Crypto.PRNG.Fortuna"; |
375 | }, | 375 | }, |
376 | 376 | ||
377 | //------------------------------------------------------------------------- | 377 | //------------------------------------------------------------------------- |
378 | 378 | ||
379 | 'key': function() { | 379 | 'key': function() { |
380 | return this._key; | 380 | return this._key; |
381 | }, | 381 | }, |
382 | 382 | ||
383 | 'setKey': function(aValue) { | 383 | 'setKey': function(aValue) { |
384 | this._key = aValue; | 384 | this._key = aValue; |
385 | this._aesKey = null; | 385 | this._aesKey = null; |
386 | }, | 386 | }, |
387 | 387 | ||
388 | 'aesKey': function() { | 388 | 'aesKey': function() { |
389 | if (this._aesKey == null) { | 389 | if (this._aesKey == null) { |
390 | this._aesKey = new Clipperz.Crypto.AES.Key({key:this.key()}); | 390 | this._aesKey = new Clipperz.Crypto.AES.Key({key:this.key()}); |
391 | } | 391 | } |
392 | 392 | ||
393 | return this._aesKey; | 393 | return this._aesKey; |
394 | }, | 394 | }, |
395 | 395 | ||
396 | 'accumulators': function() { | 396 | 'accumulators': function() { |
397 | return this._accumulators; | 397 | return this._accumulators; |
398 | }, | 398 | }, |
399 | 399 | ||
400 | 'firstPoolReseedLevel': function() { | 400 | 'firstPoolReseedLevel': function() { |
401 | return this._firstPoolReseedLevel; | 401 | return this._firstPoolReseedLevel; |
402 | }, | 402 | }, |
403 | 403 | ||
404 | //------------------------------------------------------------------------- | 404 | //------------------------------------------------------------------------- |
405 | 405 | ||
406 | 'reseedCounter': function() { | 406 | 'reseedCounter': function() { |
407 | return this._reseedCounter; | 407 | return this._reseedCounter; |
408 | }, | 408 | }, |
409 | 409 | ||
410 | 'incrementReseedCounter': function() { | 410 | 'incrementReseedCounter': function() { |
411 | this._reseedCounter = this._reseedCounter +1; | 411 | this._reseedCounter = this._reseedCounter +1; |
412 | }, | 412 | }, |
413 | 413 | ||
414 | //------------------------------------------------------------------------- | 414 | //------------------------------------------------------------------------- |
415 | 415 | ||
416 | 'reseed': function() { | 416 | 'reseed': function() { |
417 | varnewKeySeed; | 417 | varnewKeySeed; |
418 | var reseedCounter; | 418 | var reseedCounter; |
419 | varreseedCounterMask; | 419 | varreseedCounterMask; |
420 | var i, c; | 420 | var i, c; |
421 | 421 | ||
422 | newKeySeed = this.key(); | 422 | newKeySeed = this.key(); |
423 | this.incrementReseedCounter(); | 423 | this.incrementReseedCounter(); |
424 | reseedCounter = this.reseedCounter(); | 424 | reseedCounter = this.reseedCounter(); |
425 | 425 | ||
426 | c = this.numberOfEntropyAccumulators(); | 426 | c = this.numberOfEntropyAccumulators(); |
427 | reseedCounterMask = 0xffffffff >>> (32 - c); | 427 | reseedCounterMask = 0xffffffff >>> (32 - c); |
428 | for (i=0; i<c; i++) { | 428 | for (i=0; i<c; i++) { |
429 | if ((i == 0) || ((reseedCounter & (reseedCounterMask >>> (c - i))) == 0)) { | 429 | if ((i == 0) || ((reseedCounter & (reseedCounterMask >>> (c - i))) == 0)) { |
430 | newKeySeed.appendBlock(this.accumulators()[i].stack()); | 430 | newKeySeed.appendBlock(this.accumulators()[i].stack()); |
431 | this.accumulators()[i].resetStack(); | 431 | this.accumulators()[i].resetStack(); |
432 | } | 432 | } |
433 | } | 433 | } |
434 | 434 | ||
435 | if (reseedCounter == 1) { | 435 | if (reseedCounter == 1) { |
436 | c = this.randomnessSources().length; | 436 | c = this.randomnessSources().length; |
437 | for (i=0; i<c; i++) { | 437 | for (i=0; i<c; i++) { |
438 | this.randomnessSources()[i].setBoostMode(false); | 438 | this.randomnessSources()[i].setBoostMode(false); |
439 | } | 439 | } |
440 | } | 440 | } |
441 | 441 | ||
442 | this.setKey(Clipperz.Crypto.SHA.sha_d256(newKeySeed)); | 442 | this.setKey(Clipperz.Crypto.SHA.sha_d256(newKeySeed)); |
443 | if (reseedCounter == 1) { | 443 | if (reseedCounter == 1) { |
444 | Clipperz.log("### PRNG.readyToGenerateRandomBytes"); | 444 | Clipperz.log("### PRNG.readyToGenerateRandomBytes"); |
445 | MochiKit.Signal.signal(this, 'readyToGenerateRandomBytes'); | 445 | MochiKit.Signal.signal(this, 'readyToGenerateRandomBytes'); |
446 | } | 446 | } |
447 | MochiKit.Signal.signal(this, 'reseeded'); | 447 | MochiKit.Signal.signal(this, 'reseeded'); |
448 | }, | 448 | }, |
449 | 449 | ||
450 | //------------------------------------------------------------------------- | 450 | //------------------------------------------------------------------------- |
451 | 451 | ||
452 | 'isReadyToGenerateRandomValues': function() { | 452 | 'isReadyToGenerateRandomValues': function() { |
453 | return this.reseedCounter() != 0; | 453 | return this.reseedCounter() != 0; |
454 | }, | 454 | }, |
455 | 455 | ||
456 | //------------------------------------------------------------------------- | 456 | //------------------------------------------------------------------------- |
457 | 457 | ||
458 | 'entropyLevel': function() { | 458 | 'entropyLevel': function() { |
459 | return this.accumulators()[0].stack().length() + (this.reseedCounter() * this.firstPoolReseedLevel()); | 459 | return this.accumulators()[0].stack().length() + (this.reseedCounter() * this.firstPoolReseedLevel()); |
460 | }, | 460 | }, |
461 | 461 | ||
462 | //------------------------------------------------------------------------- | 462 | //------------------------------------------------------------------------- |
463 | 463 | ||
464 | 'counter': function() { | 464 | 'counter': function() { |
465 | return this._counter; | 465 | return this._counter; |
466 | }, | 466 | }, |
467 | 467 | ||
468 | 'incrementCounter': function() { | 468 | 'incrementCounter': function() { |
469 | this._counter += 1; | 469 | this._counter += 1; |
470 | }, | 470 | }, |
471 | 471 | ||
472 | 'counterBlock': function() { | 472 | 'counterBlock': function() { |
473 | var result; | 473 | var result; |
474 | 474 | ||
475 | result = new Clipperz.ByteArray().appendWords(this.counter(), 0, 0, 0); | 475 | result = new Clipperz.ByteArray().appendWords(this.counter(), 0, 0, 0); |
476 | 476 | ||
477 | return result; | 477 | return result; |
478 | }, | 478 | }, |
479 | 479 | ||
480 | //------------------------------------------------------------------------- | 480 | //------------------------------------------------------------------------- |
481 | 481 | ||
482 | 'getRandomBlock': function() { | 482 | 'getRandomBlock': function() { |
483 | var result; | 483 | var result; |
484 | 484 | ||
485 | result = new Clipperz.ByteArray(Clipperz.Crypto.AES.encryptBlock(this.aesKey(), this.counterBlock().arrayValues())); | 485 | result = new Clipperz.ByteArray(Clipperz.Crypto.AES.encryptBlock(this.aesKey(), this.counterBlock().arrayValues())); |
486 | this.incrementCounter(); | 486 | this.incrementCounter(); |
487 | 487 | ||
488 | return result; | 488 | return result; |
489 | }, | 489 | }, |
490 | 490 | ||
491 | //------------------------------------------------------------------------- | 491 | //------------------------------------------------------------------------- |
492 | 492 | ||
493 | 'getRandomBytes': function(aSize) { | 493 | 'getRandomBytes': function(aSize) { |
494 | var result; | 494 | var result; |
495 | 495 | ||
496 | if (this.isReadyToGenerateRandomValues()) { | 496 | if (this.isReadyToGenerateRandomValues()) { |
497 | var i,c; | 497 | var i,c; |
498 | var newKey; | 498 | var newKey; |
499 | 499 | ||
500 | result = new Clipperz.ByteArray(); | 500 | result = new Clipperz.ByteArray(); |
501 | 501 | ||
502 | c = Math.ceil(aSize / (128 / 8)); | 502 | c = Math.ceil(aSize / (128 / 8)); |
503 | for (i=0; i<c; i++) { | 503 | for (i=0; i<c; i++) { |
504 | result.appendBlock(this.getRandomBlock()); | 504 | result.appendBlock(this.getRandomBlock()); |
505 | } | 505 | } |
506 | 506 | ||
507 | if (result.length() != aSize) { | 507 | if (result.length() != aSize) { |
508 | result = result.split(0, aSize); | 508 | result = result.split(0, aSize); |
509 | } | 509 | } |
510 | 510 | ||
511 | newKey = this.getRandomBlock().appendBlock(this.getRandomBlock()); | 511 | newKey = this.getRandomBlock().appendBlock(this.getRandomBlock()); |
512 | this.setKey(newKey); | 512 | this.setKey(newKey); |
513 | } else { | 513 | } else { |
514 | Clipperz.logWarning("Fortuna generator has not enough entropy, yet!"); | 514 | Clipperz.logWarning("Fortuna generator has not enough entropy, yet!"); |
515 | throw Clipperz.Crypto.PRNG.exception.NotEnoughEntropy; | 515 | throw Clipperz.Crypto.PRNG.exception.NotEnoughEntropy; |
516 | } | 516 | } |
517 | 517 | ||
518 | return result; | 518 | return result; |
519 | }, | 519 | }, |
520 | 520 | ||
521 | //------------------------------------------------------------------------- | 521 | //------------------------------------------------------------------------- |
522 | 522 | ||
523 | 'addRandomByte': function(aSourceId, aPoolId, aRandomValue) { | 523 | 'addRandomByte': function(aSourceId, aPoolId, aRandomValue) { |
524 | varselectedAccumulator; | 524 | varselectedAccumulator; |
525 | 525 | ||
526 | selectedAccumulator = this.accumulators()[aPoolId]; | 526 | selectedAccumulator = this.accumulators()[aPoolId]; |
527 | selectedAccumulator.addRandomByte(aRandomValue); | 527 | selectedAccumulator.addRandomByte(aRandomValue); |
528 | 528 | ||
529 | if (aPoolId == 0) { | 529 | if (aPoolId == 0) { |
530 | MochiKit.Signal.signal(this, 'addedRandomByte') | 530 | MochiKit.Signal.signal(this, 'addedRandomByte') |
531 | if (selectedAccumulator.stack().length() > this.firstPoolReseedLevel()) { | 531 | if (selectedAccumulator.stack().length() > this.firstPoolReseedLevel()) { |
532 | this.reseed(); | 532 | this.reseed(); |
533 | } | 533 | } |
534 | } | 534 | } |
535 | }, | 535 | }, |
536 | 536 | ||
537 | //------------------------------------------------------------------------- | 537 | //------------------------------------------------------------------------- |
538 | 538 | ||
539 | 'numberOfEntropyAccumulators': function() { | 539 | 'numberOfEntropyAccumulators': function() { |
540 | return this._numberOfEntropyAccumulators; | 540 | return this._numberOfEntropyAccumulators; |
541 | }, | 541 | }, |
542 | 542 | ||
543 | //------------------------------------------------------------------------- | 543 | //------------------------------------------------------------------------- |
544 | 544 | ||
545 | 'randomnessSources': function() { | 545 | 'randomnessSources': function() { |
546 | return this._randomnessSources; | 546 | return this._randomnessSources; |
547 | }, | 547 | }, |
548 | 548 | ||
549 | 'addRandomnessSource': function(aRandomnessSource) { | 549 | 'addRandomnessSource': function(aRandomnessSource) { |
550 | aRandomnessSource.setGenerator(this); | 550 | aRandomnessSource.setGenerator(this); |
551 | aRandomnessSource.setSourceId(this.randomnessSources().length); | 551 | aRandomnessSource.setSourceId(this.randomnessSources().length); |
552 | this.randomnessSources().push(aRandomnessSource); | 552 | this.randomnessSources().push(aRandomnessSource); |
553 | 553 | ||
554 | if (this.isReadyToGenerateRandomValues() == false) { | 554 | if (this.isReadyToGenerateRandomValues() == false) { |
555 | aRandomnessSource.setBoostMode(true); | 555 | aRandomnessSource.setBoostMode(true); |
556 | } | 556 | } |
557 | }, | 557 | }, |
558 | 558 | ||
559 | //------------------------------------------------------------------------- | 559 | //------------------------------------------------------------------------- |
560 | 560 | ||
561 | 'deferredEntropyCollection': function(aValue) { | 561 | 'deferredEntropyCollection': function(aValue) { |
562 | var result; | 562 | var result; |
563 | 563 | ||
564 | 564 | ||
565 | if (this.isReadyToGenerateRandomValues()) { | 565 | if (this.isReadyToGenerateRandomValues()) { |
566 | result = aValue; | 566 | result = aValue; |
567 | } else { | 567 | } else { |
568 | var deferredResult; | 568 | var deferredResult; |
569 | 569 | ||
570 | deferredResult = new Clipperz.Async.Deferred("PRNG.deferredEntropyCollection"); | 570 | deferredResult = new Clipperz.Async.Deferred("PRNG.deferredEntropyCollection"); |
571 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); | 571 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); |
572 | MochiKit.Signal.connect(this, | 572 | MochiKit.Signal.connect(this, |
573 | 'readyToGenerateRandomBytes', | 573 | 'readyToGenerateRandomBytes', |
574 | deferredResult, | 574 | deferredResult, |
575 | 'callback'); | 575 | 'callback'); |
576 | 576 | ||
577 | result = deferredResult; | 577 | result = deferredResult; |
578 | } | 578 | } |
579 | 579 | ||