author | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2013-04-21 15:54:15 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2013-04-21 15:54:15 (UTC) |
commit | 1906ddfb5d3887edeedaf8e07d14ad89abbd214d (patch) (unidiff) | |
tree | 37df37cfcd6df9931ce92e53ef8d686adc9caa09 /frontend/gamma/js/ClipperzCryptoLibrary/PRNG.js | |
parent | 0608e045f6aa471916829468f48082ea07a453f4 (diff) | |
download | clipperz-1906ddfb5d3887edeedaf8e07d14ad89abbd214d.zip clipperz-1906ddfb5d3887edeedaf8e07d14ad89abbd214d.tar.gz clipperz-1906ddfb5d3887edeedaf8e07d14ad89abbd214d.tar.bz2 |
Aborted attempt to factor out the Crypto library on its own module
Diffstat (limited to 'frontend/gamma/js/ClipperzCryptoLibrary/PRNG.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/gamma/js/ClipperzCryptoLibrary/PRNG.js | 850 |
1 files changed, 0 insertions, 850 deletions
diff --git a/frontend/gamma/js/ClipperzCryptoLibrary/PRNG.js b/frontend/gamma/js/ClipperzCryptoLibrary/PRNG.js deleted file mode 100644 index 18cc260..0000000 --- a/frontend/gamma/js/ClipperzCryptoLibrary/PRNG.js +++ b/dev/null | |||
@@ -1,850 +0,0 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2013 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz, the online password manager. | ||
6 | For further information about its features and functionalities please | ||
7 | refer to http://www.clipperz.com. | ||
8 | |||
9 | * Clipperz is free software: you can redistribute it and/or modify it | ||
10 | under the terms of the GNU Affero General Public License as published | ||
11 | by the Free Software Foundation, either version 3 of the License, or | ||
12 | (at your option) any later version. | ||
13 | |||
14 | * Clipperz is distributed in the hope that it will be useful, but | ||
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
17 | See the GNU Affero General Public License for more details. | ||
18 | |||
19 | * You should have received a copy of the GNU Affero General Public | ||
20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. | ||
21 | |||
22 | */ | ||
23 | |||
24 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { | ||
25 | throw "Clipperz.Crypto.PRNG depends on Clipperz.ByteArray!"; | ||
26 | } | ||
27 | |||
28 | try { if (typeof(Clipperz.Crypto.SHA) == 'undefined') { throw ""; }} catch (e) { | ||
29 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.SHA!"; | ||
30 | } | ||
31 | |||
32 | try { if (typeof(Clipperz.Crypto.AES) == 'undefined') { throw ""; }} catch (e) { | ||
33 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.AES!"; | ||
34 | } | ||
35 | |||
36 | if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { Clipperz.Crypto.PRNG = {}; } | ||
37 | |||
38 | //############################################################################# | ||
39 | |||
40 | Clipperz.Crypto.PRNG.EntropyAccumulator = function(args) { | ||
41 | args = args || {}; | ||
42 | //MochiKit.Base.bindMethods(this); | ||
43 | |||
44 | this._stack = new Clipperz.ByteArray(); | ||
45 | this._maxStackLengthBeforeHashing = args.maxStackLengthBeforeHashing || 256; | ||
46 | return this; | ||
47 | } | ||
48 | |||
49 | Clipperz.Crypto.PRNG.EntropyAccumulator.prototype = MochiKit.Base.update(null, { | ||
50 | |||
51 | 'toString': function() { | ||
52 | return "Clipperz.Crypto.PRNG.EntropyAccumulator"; | ||
53 | }, | ||
54 | |||
55 | //------------------------------------------------------------------------- | ||
56 | |||
57 | 'stack': function() { | ||
58 | return this._stack; | ||
59 | }, | ||
60 | |||
61 | 'setStack': function(aValue) { | ||
62 | this._stack = aValue; | ||
63 | }, | ||
64 | |||
65 | 'resetStack': function() { | ||
66 | this.stack().reset(); | ||
67 | }, | ||
68 | |||
69 | 'maxStackLengthBeforeHashing': function() { | ||
70 | return this._maxStackLengthBeforeHashing; | ||
71 | }, | ||
72 | |||
73 | //------------------------------------------------------------------------- | ||
74 | |||
75 | 'addRandomByte': function(aValue) { | ||
76 | this.stack().appendByte(aValue); | ||
77 | |||
78 | if (this.stack().length() > this.maxStackLengthBeforeHashing()) { | ||
79 | this.setStack(Clipperz.Crypto.SHA.sha_d256(this.stack())); | ||
80 | } | ||
81 | }, | ||
82 | |||
83 | //------------------------------------------------------------------------- | ||
84 | __syntaxFix__: "syntax fix" | ||
85 | }); | ||
86 | |||
87 | //############################################################################# | ||
88 | |||
89 | Clipperz.Crypto.PRNG.RandomnessSource = function(args) { | ||
90 | args = args || {}; | ||
91 | MochiKit.Base.bindMethods(this); | ||
92 | |||
93 | this._generator = args.generator || null; | ||
94 | this._sourceId = args.sourceId || null; | ||
95 | this._boostMode = args.boostMode || false; | ||
96 | |||
97 | this._nextPoolIndex = 0; | ||
98 | |||
99 | return this; | ||
100 | } | ||
101 | |||
102 | Clipperz.Crypto.PRNG.RandomnessSource.prototype = MochiKit.Base.update(null, { | ||
103 | |||
104 | 'generator': function() { | ||
105 | return this._generator; | ||
106 | }, | ||
107 | |||
108 | 'setGenerator': function(aValue) { | ||
109 | this._generator = aValue; | ||
110 | }, | ||
111 | |||
112 | //------------------------------------------------------------------------- | ||
113 | |||
114 | 'boostMode': function() { | ||
115 | return this._boostMode; | ||
116 | }, | ||
117 | |||
118 | 'setBoostMode': function(aValue) { | ||
119 | this._boostMode = aValue; | ||
120 | }, | ||
121 | |||
122 | //------------------------------------------------------------------------- | ||
123 | |||
124 | 'sourceId': function() { | ||
125 | return this._sourceId; | ||
126 | }, | ||
127 | |||
128 | 'setSourceId': function(aValue) { | ||
129 | this._sourceId = aValue; | ||
130 | }, | ||
131 | |||
132 | //------------------------------------------------------------------------- | ||
133 | |||
134 | 'nextPoolIndex': function() { | ||
135 | return this._nextPoolIndex; | ||
136 | }, | ||
137 | |||
138 | 'incrementNextPoolIndex': function() { | ||
139 | this._nextPoolIndex = ((this._nextPoolIndex + 1) % this.generator().numberOfEntropyAccumulators()); | ||
140 | }, | ||
141 | |||
142 | //------------------------------------------------------------------------- | ||
143 | |||
144 | 'updateGeneratorWithValue': function(aRandomValue) { | ||
145 | if (this.generator() != null) { | ||
146 | this.generator().addRandomByte(this.sourceId(), this.nextPoolIndex(), aRandomValue); | ||
147 | this.incrementNextPoolIndex(); | ||
148 | } | ||
149 | }, | ||
150 | |||
151 | //------------------------------------------------------------------------- | ||
152 | __syntaxFix__: "syntax fix" | ||
153 | }); | ||
154 | |||
155 | //############################################################################# | ||
156 | |||
157 | Clipperz.Crypto.PRNG.TimeRandomnessSource = function(args) { | ||
158 | args = args || {}; | ||
159 | //MochiKit.Base.bindMethods(this); | ||
160 | |||
161 | this._intervalTime = args.intervalTime || 1000; | ||
162 | |||
163 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | ||
164 | |||
165 | this.collectEntropy(); | ||
166 | return this; | ||
167 | } | ||
168 | |||
169 | Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | ||
170 | |||
171 | 'intervalTime': function() { | ||
172 | return this._intervalTime; | ||
173 | }, | ||
174 | |||
175 | //------------------------------------------------------------------------- | ||
176 | |||
177 | 'collectEntropy': function() { | ||
178 | varnow; | ||
179 | varentropyByte; | ||
180 | var intervalTime; | ||
181 | now = new Date(); | ||
182 | entropyByte = (now.getTime() & 0xff); | ||
183 | |||
184 | intervalTime = this.intervalTime(); | ||
185 | if (this.boostMode() == true) { | ||
186 | intervalTime = intervalTime / 9; | ||
187 | } | ||
188 | |||
189 | this.updateGeneratorWithValue(entropyByte); | ||
190 | setTimeout(this.collectEntropy, intervalTime); | ||
191 | }, | ||
192 | |||
193 | //------------------------------------------------------------------------- | ||
194 | |||
195 | 'numberOfRandomBits': function() { | ||
196 | return 5; | ||
197 | }, | ||
198 | |||
199 | //------------------------------------------------------------------------- | ||
200 | |||
201 | 'pollingFrequency': function() { | ||
202 | return 10; | ||
203 | }, | ||
204 | |||
205 | //------------------------------------------------------------------------- | ||
206 | __syntaxFix__: "syntax fix" | ||
207 | }); | ||
208 | |||
209 | //***************************************************************************** | ||
210 | |||
211 | Clipperz.Crypto.PRNG.MouseRandomnessSource = function(args) { | ||
212 | args = args || {}; | ||
213 | |||
214 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | ||
215 | |||
216 | this._numberOfBitsToCollectAtEachEvent = 4; | ||
217 | this._randomBitsCollector = 0; | ||
218 | this._numberOfRandomBitsCollected = 0; | ||
219 | |||
220 | MochiKit.Signal.connect(document, 'onmousemove', this, 'collectEntropy'); | ||
221 | |||
222 | return this; | ||
223 | } | ||
224 | |||
225 | Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | ||
226 | |||
227 | //------------------------------------------------------------------------- | ||
228 | |||
229 | 'numberOfBitsToCollectAtEachEvent': function() { | ||
230 | return this._numberOfBitsToCollectAtEachEvent; | ||
231 | }, | ||
232 | |||
233 | //------------------------------------------------------------------------- | ||
234 | |||
235 | 'randomBitsCollector': function() { | ||
236 | return this._randomBitsCollector; | ||
237 | }, | ||
238 | |||
239 | 'setRandomBitsCollector': function(aValue) { | ||
240 | this._randomBitsCollector = aValue; | ||
241 | }, | ||
242 | |||
243 | 'appendRandomBitsToRandomBitsCollector': function(aValue) { | ||
244 | var collectedBits; | ||
245 | var numberOfRandomBitsCollected; | ||
246 | |||
247 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); | ||
248 | collectetBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); | ||
249 | this.setRandomBitsCollector(collectetBits); | ||
250 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); | ||
251 | |||
252 | if (numberOfRandomBitsCollected == 8) { | ||
253 | this.updateGeneratorWithValue(collectetBits); | ||
254 | numberOfRandomBitsCollected = 0; | ||
255 | this.setRandomBitsCollector(0); | ||
256 | } | ||
257 | |||
258 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) | ||
259 | }, | ||
260 | |||
261 | //------------------------------------------------------------------------- | ||
262 | |||
263 | 'numberOfRandomBitsCollected': function() { | ||
264 | return this._numberOfRandomBitsCollected; | ||
265 | }, | ||
266 | |||
267 | 'setNumberOfRandomBitsCollected': function(aValue) { | ||
268 | this._numberOfRandomBitsCollected = aValue; | ||
269 | }, | ||
270 | |||
271 | //------------------------------------------------------------------------- | ||
272 | |||
273 | 'collectEntropy': function(anEvent) { | ||
274 | var mouseLocation; | ||
275 | var randomBit; | ||
276 | var mask; | ||
277 | |||
278 | mask = 0xffffffff >>> (32 - this.numberOfBitsToCollectAtEachEvent()); | ||
279 | |||
280 | mouseLocation = anEvent.mouse().client; | ||
281 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & mask); | ||
282 | this.appendRandomBitsToRandomBitsCollector(randomBit) | ||
283 | }, | ||
284 | |||
285 | //------------------------------------------------------------------------- | ||
286 | |||
287 | 'numberOfRandomBits': function() { | ||
288 | return 1; | ||
289 | }, | ||
290 | |||
291 | //------------------------------------------------------------------------- | ||
292 | |||
293 | 'pollingFrequency': function() { | ||
294 | return 10; | ||
295 | }, | ||
296 | |||
297 | //------------------------------------------------------------------------- | ||
298 | __syntaxFix__: "syntax fix" | ||
299 | }); | ||
300 | |||
301 | //***************************************************************************** | ||
302 | |||
303 | Clipperz.Crypto.PRNG.KeyboardRandomnessSource = function(args) { | ||
304 | args = args || {}; | ||
305 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | ||
306 | |||
307 | this._randomBitsCollector = 0; | ||
308 | this._numberOfRandomBitsCollected = 0; | ||
309 | |||
310 | MochiKit.Signal.connect(document, 'onkeypress', this, 'collectEntropy'); | ||
311 | |||
312 | return this; | ||
313 | } | ||
314 | |||
315 | Clipperz.Crypto.PRNG.KeyboardRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | ||
316 | |||
317 | //------------------------------------------------------------------------- | ||
318 | |||
319 | 'randomBitsCollector': function() { | ||
320 | return this._randomBitsCollector; | ||
321 | }, | ||
322 | |||
323 | 'setRandomBitsCollector': function(aValue) { | ||
324 | this._randomBitsCollector = aValue; | ||
325 | }, | ||
326 | |||
327 | 'appendRandomBitToRandomBitsCollector': function(aValue) { | ||
328 | var collectedBits; | ||
329 | var numberOfRandomBitsCollected; | ||
330 | |||
331 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); | ||
332 | collectetBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); | ||
333 | this.setRandomBitsCollector(collectetBits); | ||
334 | numberOfRandomBitsCollected ++; | ||
335 | |||
336 | if (numberOfRandomBitsCollected == 8) { | ||
337 | this.updateGeneratorWithValue(collectetBits); | ||
338 | numberOfRandomBitsCollected = 0; | ||
339 | this.setRandomBitsCollector(0); | ||
340 | } | ||
341 | |||
342 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) | ||
343 | }, | ||
344 | |||
345 | //------------------------------------------------------------------------- | ||
346 | |||
347 | 'numberOfRandomBitsCollected': function() { | ||
348 | return this._numberOfRandomBitsCollected; | ||
349 | }, | ||
350 | |||
351 | 'setNumberOfRandomBitsCollected': function(aValue) { | ||
352 | this._numberOfRandomBitsCollected = aValue; | ||
353 | }, | ||
354 | |||
355 | //------------------------------------------------------------------------- | ||
356 | |||
357 | 'collectEntropy': function(anEvent) { | ||
358 | /* | ||
359 | var mouseLocation; | ||
360 | var randomBit; | ||
361 | |||
362 | mouseLocation = anEvent.mouse().client; | ||
363 | |||
364 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & 0x1); | ||
365 | this.appendRandomBitToRandomBitsCollector(randomBit); | ||
366 | */ | ||
367 | }, | ||
368 | |||
369 | //------------------------------------------------------------------------- | ||
370 | |||
371 | 'numberOfRandomBits': function() { | ||
372 | return 1; | ||
373 | }, | ||
374 | |||
375 | //------------------------------------------------------------------------- | ||
376 | |||
377 | 'pollingFrequency': function() { | ||
378 | return 10; | ||
379 | }, | ||
380 | |||
381 | //------------------------------------------------------------------------- | ||
382 | __syntaxFix__: "syntax fix" | ||
383 | }); | ||
384 | |||
385 | //############################################################################# | ||
386 | |||
387 | Clipperz.Crypto.PRNG.Fortuna = function(args) { | ||
388 | vari,c; | ||
389 | |||
390 | args = args || {}; | ||
391 | |||
392 | this._key = args.seed || null; | ||
393 | if (this._key == null) { | ||
394 | this._counter = 0; | ||
395 | this._key = new Clipperz.ByteArray(); | ||
396 | } else { | ||
397 | this._counter = 1; | ||
398 | } | ||
399 | |||
400 | this._aesKey = null; | ||
401 | |||
402 | this._firstPoolReseedLevel = args.firstPoolReseedLevel || 32 || 64; | ||
403 | this._numberOfEntropyAccumulators = args.numberOfEntropyAccumulators || 32; | ||
404 | |||
405 | this._accumulators = []; | ||
406 | c = this.numberOfEntropyAccumulators(); | ||
407 | for (i=0; i<c; i++) { | ||
408 | this._accumulators.push(new Clipperz.Crypto.PRNG.EntropyAccumulator()); | ||
409 | } | ||
410 | |||
411 | this._randomnessSources = []; | ||
412 | this._reseedCounter = 0; | ||
413 | |||
414 | return this; | ||
415 | } | ||
416 | |||
417 | Clipperz.Crypto.PRNG.Fortuna.prototype = MochiKit.Base.update(null, { | ||
418 | |||
419 | 'toString': function() { | ||
420 | return "Clipperz.Crypto.PRNG.Fortuna"; | ||
421 | }, | ||
422 | |||
423 | //------------------------------------------------------------------------- | ||
424 | |||
425 | 'key': function() { | ||
426 | return this._key; | ||
427 | }, | ||
428 | |||
429 | 'setKey': function(aValue) { | ||
430 | this._key = aValue; | ||
431 | this._aesKey = null; | ||
432 | }, | ||
433 | |||
434 | 'aesKey': function() { | ||
435 | if (this._aesKey == null) { | ||
436 | this._aesKey = new Clipperz.Crypto.AES.Key({key:this.key()}); | ||
437 | } | ||
438 | |||
439 | return this._aesKey; | ||
440 | }, | ||
441 | |||
442 | 'accumulators': function() { | ||
443 | return this._accumulators; | ||
444 | }, | ||
445 | |||
446 | 'firstPoolReseedLevel': function() { | ||
447 | return this._firstPoolReseedLevel; | ||
448 | }, | ||
449 | |||
450 | //------------------------------------------------------------------------- | ||
451 | |||
452 | 'reseedCounter': function() { | ||
453 | return this._reseedCounter; | ||
454 | }, | ||
455 | |||
456 | 'incrementReseedCounter': function() { | ||
457 | this._reseedCounter = this._reseedCounter +1; | ||
458 | }, | ||
459 | |||
460 | //------------------------------------------------------------------------- | ||
461 | |||
462 | 'reseed': function() { | ||
463 | varnewKeySeed; | ||
464 | var reseedCounter; | ||
465 | varreseedCounterMask; | ||
466 | var i, c; | ||
467 | |||
468 | newKeySeed = this.key(); | ||
469 | this.incrementReseedCounter(); | ||
470 | reseedCounter = this.reseedCounter(); | ||
471 | |||
472 | c = this.numberOfEntropyAccumulators(); | ||
473 | reseedCounterMask = 0xffffffff >>> (32 - c); | ||
474 | for (i=0; i<c; i++) { | ||
475 | if ((i == 0) || ((reseedCounter & (reseedCounterMask >>> (c - i))) == 0)) { | ||
476 | newKeySeed.appendBlock(this.accumulators()[i].stack()); | ||
477 | this.accumulators()[i].resetStack(); | ||
478 | } | ||
479 | } | ||
480 | |||
481 | if (reseedCounter == 1) { | ||
482 | c = this.randomnessSources().length; | ||
483 | for (i=0; i<c; i++) { | ||
484 | this.randomnessSources()[i].setBoostMode(false); | ||
485 | } | ||
486 | } | ||
487 | |||
488 | this.setKey(Clipperz.Crypto.SHA.sha_d256(newKeySeed)); | ||
489 | if (reseedCounter == 1) { | ||
490 | //MochiKit.Logging.logDebug("### PRNG.readyToGenerateRandomBytes"); | ||
491 | Clipperz.log("### PRNG.readyToGenerateRandomBytes"); | ||
492 | MochiKit.Signal.signal(this, 'readyToGenerateRandomBytes'); | ||
493 | } | ||
494 | MochiKit.Signal.signal(this, 'reseeded'); | ||
495 | }, | ||
496 | |||
497 | //------------------------------------------------------------------------- | ||
498 | |||
499 | 'isReadyToGenerateRandomValues': function() { | ||
500 | return this.reseedCounter() != 0; | ||
501 | }, | ||
502 | |||
503 | //------------------------------------------------------------------------- | ||
504 | |||
505 | 'entropyLevel': function() { | ||
506 | return this.accumulators()[0].stack().length() + (this.reseedCounter() * this.firstPoolReseedLevel()); | ||
507 | }, | ||
508 | |||
509 | //------------------------------------------------------------------------- | ||
510 | |||
511 | 'counter': function() { | ||
512 | return this._counter; | ||
513 | }, | ||
514 | |||
515 | 'incrementCounter': function() { | ||
516 | this._counter += 1; | ||
517 | }, | ||
518 | |||
519 | 'counterBlock': function() { | ||
520 | var result; | ||
521 | |||
522 | result = new Clipperz.ByteArray().appendWords(this.counter(), 0, 0, 0); | ||
523 | |||
524 | return result; | ||
525 | }, | ||
526 | |||
527 | //------------------------------------------------------------------------- | ||
528 | |||
529 | 'getRandomBlock': function() { | ||
530 | var result; | ||
531 | |||
532 | result = new Clipperz.ByteArray(Clipperz.Crypto.AES.encryptBlock(this.aesKey(), this.counterBlock().arrayValues())); | ||
533 | this.incrementCounter(); | ||
534 | |||
535 | return result; | ||
536 | }, | ||
537 | |||
538 | //------------------------------------------------------------------------- | ||
539 | |||
540 | 'getRandomBytes': function(aSize) { | ||
541 | var result; | ||
542 | |||
543 | if (this.isReadyToGenerateRandomValues()) { | ||
544 | var i,c; | ||
545 | var newKey; | ||
546 | |||
547 | result = new Clipperz.ByteArray(); | ||
548 | |||
549 | c = Math.ceil(aSize / (128 / 8)); | ||
550 | for (i=0; i<c; i++) { | ||
551 | result.appendBlock(this.getRandomBlock()); | ||
552 | } | ||
553 | |||
554 | if (result.length() != aSize) { | ||
555 | result = result.split(0, aSize); | ||
556 | } | ||
557 | |||
558 | newKey = this.getRandomBlock().appendBlock(this.getRandomBlock()); | ||
559 | this.setKey(newKey); | ||
560 | } else { | ||
561 | MochiKit.Logging.logWarning("Fortuna generator has not enough entropy, yet!"); | ||
562 | throw Clipperz.Crypto.PRNG.exception.NotEnoughEntropy; | ||
563 | } | ||
564 | |||
565 | return result; | ||
566 | }, | ||
567 | |||
568 | //------------------------------------------------------------------------- | ||
569 | |||
570 | 'addRandomByte': function(aSourceId, aPoolId, aRandomValue) { | ||
571 | varselectedAccumulator; | ||
572 | |||
573 | selectedAccumulator = this.accumulators()[aPoolId]; | ||
574 | selectedAccumulator.addRandomByte(aRandomValue); | ||
575 | |||
576 | if (aPoolId == 0) { | ||
577 | MochiKit.Signal.signal(this, 'addedRandomByte') | ||
578 | if (selectedAccumulator.stack().length() > this.firstPoolReseedLevel()) { | ||
579 | this.reseed(); | ||
580 | } | ||
581 | } | ||
582 | }, | ||
583 | |||
584 | //------------------------------------------------------------------------- | ||
585 | |||
586 | 'numberOfEntropyAccumulators': function() { | ||
587 | return this._numberOfEntropyAccumulators; | ||
588 | }, | ||
589 | |||
590 | //------------------------------------------------------------------------- | ||
591 | |||
592 | 'randomnessSources': function() { | ||
593 | return this._randomnessSources; | ||
594 | }, | ||
595 | |||
596 | 'addRandomnessSource': function(aRandomnessSource) { | ||
597 | aRandomnessSource.setGenerator(this); | ||
598 | aRandomnessSource.setSourceId(this.randomnessSources().length); | ||
599 | this.randomnessSources().push(aRandomnessSource); | ||
600 | |||
601 | if (this.isReadyToGenerateRandomValues() == false) { | ||
602 | aRandomnessSource.setBoostMode(true); | ||
603 | } | ||
604 | }, | ||
605 | |||
606 | //------------------------------------------------------------------------- | ||
607 | |||
608 | 'deferredEntropyCollection': function(aValue) { | ||
609 | var result; | ||
610 | |||
611 | //MochiKit.Logging.logDebug(">>> PRNG.deferredEntropyCollection"); | ||
612 | |||
613 | if (this.isReadyToGenerateRandomValues()) { | ||
614 | //MochiKit.Logging.logDebug("--- PRNG.deferredEntropyCollection - 1"); | ||
615 | result = aValue; | ||
616 | } else { | ||
617 | //MochiKit.Logging.logDebug("--- PRNG.deferredEntropyCollection - 2"); | ||
618 | var deferredResult; | ||
619 | |||
620 | // Clipperz.NotificationCenter.notify(this, 'updatedProgressState', 'collectingEntropy', true); | ||
621 | |||
622 | deferredResult = new Clipperz.Async.Deferred("PRNG.deferredEntropyCollection"); | ||
623 | // deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("1.2.1 - PRNG.deferredEntropyCollection - 1: " + res); return res;}); | ||
624 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); | ||
625 | // deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("1.2.2 - PRNG.deferredEntropyCollection - 2: " + res); return res;}); | ||
626 | MochiKit.Signal.connect(this, | ||
627 | 'readyToGenerateRandomBytes', | ||
628 | deferredResult, | ||
629 | 'callback'); | ||
630 | |||
631 | result = deferredResult; | ||
632 | } | ||
633 | //MochiKit.Logging.logDebug("<<< PRNG.deferredEntropyCollection - result: " + result); | ||
634 | |||
635 | return result; | ||
636 | }, | ||
637 | |||
638 | //------------------------------------------------------------------------- | ||
639 | |||
640 | 'fastEntropyAccumulationForTestingPurpose': function() { | ||
641 | while (! this.isReadyToGenerateRandomValues()) { | ||
642 | this.addRandomByte(Math.floor(Math.random() * 32), Math.floor(Math.random() * 32), Math.floor(Math.random() * 256)); | ||
643 | } | ||
644 | }, | ||
645 | |||
646 | //------------------------------------------------------------------------- | ||
647 | |||
648 | 'dump': function(appendToDoc) { | ||
649 | var tbl; | ||
650 | var i,c; | ||
651 | |||
652 | tbl = document.createElement("table"); | ||
653 | tbl.border = 0; | ||
654 | with (tbl.style) { | ||
655 | border = "1px solid lightgrey"; | ||
656 | fontFamily = 'Helvetica, Arial, sans-serif'; | ||
657 | fontSize = '8pt'; | ||
658 | //borderCollapse = "collapse"; | ||
659 | } | ||
660 | var hdr = tbl.createTHead(); | ||
661 | var hdrtr = hdr.insertRow(0); | ||
662 | // document.createElement("tr"); | ||
663 | { | ||
664 | var ntd; | ||
665 | |||
666 | ntd = hdrtr.insertCell(0); | ||
667 | ntd.style.borderBottom = "1px solid lightgrey"; | ||
668 | ntd.style.borderRight = "1px solid lightgrey"; | ||
669 | ntd.appendChild(document.createTextNode("#")); | ||
670 | |||
671 | ntd = hdrtr.insertCell(1); | ||
672 | ntd.style.borderBottom = "1px solid lightgrey"; | ||
673 | ntd.style.borderRight = "1px solid lightgrey"; | ||
674 | ntd.appendChild(document.createTextNode("s")); | ||
675 | |||
676 | ntd = hdrtr.insertCell(2); | ||
677 | ntd.colSpan = this.firstPoolReseedLevel(); | ||
678 | ntd.style.borderBottom = "1px solid lightgrey"; | ||
679 | ntd.style.borderRight = "1px solid lightgrey"; | ||
680 | ntd.appendChild(document.createTextNode("base values")); | ||
681 | |||
682 | ntd = hdrtr.insertCell(3); | ||
683 | ntd.colSpan = 20; | ||
684 | ntd.style.borderBottom = "1px solid lightgrey"; | ||
685 | ntd.appendChild(document.createTextNode("extra values")); | ||
686 | |||
687 | } | ||
688 | |||
689 | c = this.accumulators().length; | ||
690 | for (i=0; i<c ; i++) { | ||
691 | varcurrentAccumulator; | ||
692 | var bdytr; | ||
693 | var bdytd; | ||
694 | var ii, cc; | ||
695 | |||
696 | currentAccumulator = this.accumulators()[i] | ||
697 | |||
698 | bdytr = tbl.insertRow(true); | ||
699 | |||
700 | bdytd = bdytr.insertCell(0); | ||
701 | bdytd.style.borderRight = "1px solid lightgrey"; | ||
702 | bdytd.style.color = "lightgrey"; | ||
703 | bdytd.appendChild(document.createTextNode("" + i)); | ||
704 | |||
705 | bdytd = bdytr.insertCell(1); | ||
706 | bdytd.style.borderRight = "1px solid lightgrey"; | ||
707 | bdytd.style.color = "gray"; | ||
708 | bdytd.appendChild(document.createTextNode("" + currentAccumulator.stack().length())); | ||
709 | |||
710 | |||
711 | cc = Math.max(currentAccumulator.stack().length(), this.firstPoolReseedLevel()); | ||
712 | for (ii=0; ii<cc; ii++) { | ||
713 | var cellText; | ||
714 | |||
715 | bdytd = bdytr.insertCell(ii + 2); | ||
716 | |||
717 | if (ii < currentAccumulator.stack().length()) { | ||
718 | cellText = Clipperz.ByteArray.byteToHex(currentAccumulator.stack().byteAtIndex(ii)); | ||
719 | } else { | ||
720 | cellText = "_"; | ||
721 | } | ||
722 | |||
723 | if (ii == (this.firstPoolReseedLevel() - 1)) { | ||
724 | bdytd.style.borderRight = "1px solid lightgrey"; | ||
725 | } | ||
726 | |||
727 | bdytd.appendChild(document.createTextNode(cellText)); | ||
728 | } | ||
729 | |||
730 | } | ||
731 | |||
732 | |||
733 | if (appendToDoc) { | ||
734 | var ne = document.createElement("div"); | ||
735 | ne.id = "entropyGeneratorStatus"; | ||
736 | with (ne.style) { | ||
737 | fontFamily = "Courier New, monospace"; | ||
738 | fontSize = "12px"; | ||
739 | lineHeight = "16px"; | ||
740 | borderTop = "1px solid black"; | ||
741 | padding = "10px"; | ||
742 | } | ||
743 | if (document.getElementById(ne.id)) { | ||
744 | MochiKit.DOM.swapDOM(ne.id, ne); | ||
745 | } else { | ||
746 | document.body.appendChild(ne); | ||
747 | } | ||
748 | ne.appendChild(tbl); | ||
749 | } | ||
750 | |||
751 | return tbl; | ||
752 | }, | ||
753 | |||
754 | //----------------------------------------------------------------------------- | ||
755 | __syntaxFix__: "syntax fix" | ||
756 | }); | ||
757 | |||
758 | //############################################################################# | ||
759 | |||
760 | Clipperz.Crypto.PRNG.Random = function(args) { | ||
761 | args = args || {}; | ||
762 | //MochiKit.Base.bindMethods(this); | ||
763 | |||
764 | return this; | ||
765 | } | ||
766 | |||
767 | Clipperz.Crypto.PRNG.Random.prototype = MochiKit.Base.update(null, { | ||
768 | |||
769 | 'toString': function() { | ||
770 | return "Clipperz.Crypto.PRNG.Random"; | ||
771 | }, | ||
772 | |||
773 | //------------------------------------------------------------------------- | ||
774 | |||
775 | 'getRandomBytes': function(aSize) { | ||
776 | //Clipperz.Profile.start("Clipperz.Crypto.PRNG.Random.getRandomBytes"); | ||
777 | varresult; | ||
778 | var i,c; | ||
779 | |||
780 | result = new Clipperz.ByteArray() | ||
781 | c = aSize || 1; | ||
782 | for (i=0; i<c; i++) { | ||
783 | result.appendByte((Math.random()*255) & 0xff); | ||
784 | } | ||
785 | |||
786 | //Clipperz.Profile.stop("Clipperz.Crypto.PRNG.Random.getRandomBytes"); | ||
787 | return result; | ||
788 | }, | ||
789 | |||
790 | //------------------------------------------------------------------------- | ||
791 | __syntaxFix__: "syntax fix" | ||
792 | }); | ||
793 | |||
794 | //############################################################################# | ||
795 | |||
796 | _clipperz_crypt_prng_defaultPRNG = null; | ||
797 | |||
798 | Clipperz.Crypto.PRNG.defaultRandomGenerator = function() { | ||
799 | if (_clipperz_crypt_prng_defaultPRNG == null) { | ||
800 | _clipperz_crypt_prng_defaultPRNG = new Clipperz.Crypto.PRNG.Fortuna(); | ||
801 | |||
802 | //............................................................. | ||
803 | // | ||
804 | // TimeRandomnessSource | ||
805 | // | ||
806 | //............................................................. | ||
807 | { | ||
808 | var newRandomnessSource; | ||
809 | |||
810 | newRandomnessSource = new Clipperz.Crypto.PRNG.TimeRandomnessSource({intervalTime:111}); | ||
811 | _clipperz_crypt_prng_defaultPRNG.addRandomnessSource(newRandomnessSource); | ||
812 | } | ||
813 | |||
814 | //............................................................. | ||
815 | // | ||
816 | // MouseRandomnessSource | ||
817 | // | ||
818 | //............................................................. | ||
819 | { | ||
820 | varnewRandomnessSource; | ||
821 | |||
822 | newRandomnessSource = new Clipperz.Crypto.PRNG.MouseRandomnessSource(); | ||
823 | _clipperz_crypt_prng_defaultPRNG.addRandomnessSource(newRandomnessSource); | ||
824 | } | ||
825 | |||
826 | //............................................................. | ||
827 | // | ||
828 | // KeyboardRandomnessSource | ||
829 | // | ||
830 | //............................................................. | ||
831 | { | ||
832 | varnewRandomnessSource; | ||
833 | |||
834 | newRandomnessSource = new Clipperz.Crypto.PRNG.KeyboardRandomnessSource(); | ||
835 | _clipperz_crypt_prng_defaultPRNG.addRandomnessSource(newRandomnessSource); | ||
836 | } | ||
837 | |||
838 | } | ||
839 | |||
840 | return _clipperz_crypt_prng_defaultPRNG; | ||
841 | }; | ||
842 | |||
843 | //############################################################################# | ||
844 | |||
845 | Clipperz.Crypto.PRNG.exception = { | ||
846 | NotEnoughEntropy: new MochiKit.Base.NamedError("Clipperz.Crypto.PRNG.exception.NotEnoughEntropy") | ||
847 | }; | ||
848 | |||
849 | |||
850 | MochiKit.DOM.addLoadEvent(Clipperz.Crypto.PRNG.defaultRandomGenerator); | ||