Diffstat (limited to 'frontend/gamma/js/Clipperz/Crypto/PRNG.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/gamma/js/Clipperz/Crypto/PRNG.js | 33 |
1 files changed, 11 insertions, 22 deletions
diff --git a/frontend/gamma/js/Clipperz/Crypto/PRNG.js b/frontend/gamma/js/Clipperz/Crypto/PRNG.js index adfdb16..c539f06 100644 --- a/frontend/gamma/js/Clipperz/Crypto/PRNG.js +++ b/frontend/gamma/js/Clipperz/Crypto/PRNG.js | |||
@@ -1,46 +1,44 @@ | |||
1 | /* | 1 | /* |
2 | 2 | ||
3 | Copyright 2008-2011 Clipperz Srl | 3 | Copyright 2008-2013 Clipperz Srl |
4 | 4 | ||
5 | This file is part of Clipperz Community Edition. | 5 | This file is part of Clipperz, the online password manager. |
6 | Clipperz Community Edition is an online password manager. | ||
7 | For further information about its features and functionalities please | 6 | For further information about its features and functionalities please |
8 | refer to http://www.clipperz.com. | 7 | refer to http://www.clipperz.com. |
9 | 8 | ||
10 | * Clipperz Community Edition is free software: you can redistribute | 9 | * Clipperz is free software: you can redistribute it and/or modify it |
11 | it and/or modify it under the terms of the GNU Affero General Public | 10 | under the terms of the GNU Affero General Public License as published |
12 | License as published by the Free Software Foundation, either version | 11 | by the Free Software Foundation, either version 3 of the License, or |
13 | 3 of the License, or (at your option) any later version. | 12 | (at your option) any later version. |
14 | 13 | ||
15 | * Clipperz Community Edition is distributed in the hope that it will | 14 | * Clipperz is distributed in the hope that it will be useful, but |
16 | be useful, but WITHOUT ANY WARRANTY; without even the implied | 15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. | 17 | See the GNU Affero General Public License for more details. |
19 | 18 | ||
20 | * You should have received a copy of the GNU Affero General Public | 19 | * You should have received a copy of the GNU Affero General Public |
21 | License along with Clipperz Community Edition. If not, see | 20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. |
22 | <http://www.gnu.org/licenses/>. | ||
23 | 21 | ||
24 | */ | 22 | */ |
25 | 23 | ||
26 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { | 24 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { |
27 | throw "Clipperz.Crypto.PRNG depends on Clipperz.ByteArray!"; | 25 | throw "Clipperz.Crypto.PRNG depends on Clipperz.ByteArray!"; |
28 | } | 26 | } |
29 | 27 | ||
30 | try { if (typeof(Clipperz.Crypto.SHA) == 'undefined') { throw ""; }} catch (e) { | 28 | try { if (typeof(Clipperz.Crypto.SHA) == 'undefined') { throw ""; }} catch (e) { |
31 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.SHA!"; | 29 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.SHA!"; |
32 | } | 30 | } |
33 | 31 | ||
34 | try { if (typeof(Clipperz.Crypto.AES) == 'undefined') { throw ""; }} catch (e) { | 32 | try { if (typeof(Clipperz.Crypto.AES) == 'undefined') { throw ""; }} catch (e) { |
35 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.AES!"; | 33 | throw "Clipperz.Crypto.PRNG depends on Clipperz.Crypto.AES!"; |
36 | } | 34 | } |
37 | 35 | ||
38 | if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { Clipperz.Crypto.PRNG = {}; } | 36 | if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { Clipperz.Crypto.PRNG = {}; } |
39 | 37 | ||
40 | //############################################################################# | 38 | //############################################################################# |
41 | 39 | ||
42 | Clipperz.Crypto.PRNG.EntropyAccumulator = function(args) { | 40 | Clipperz.Crypto.PRNG.EntropyAccumulator = function(args) { |
43 | args = args || {}; | 41 | args = args || {}; |
44 | //MochiKit.Base.bindMethods(this); | 42 | //MochiKit.Base.bindMethods(this); |
45 | 43 | ||
46 | this._stack = new Clipperz.ByteArray(); | 44 | this._stack = new Clipperz.ByteArray(); |
@@ -468,49 +466,48 @@ Clipperz.Crypto.PRNG.Fortuna.prototype = MochiKit.Base.update(null, { | |||
468 | var i, c; | 466 | var i, c; |
469 | 467 | ||
470 | newKeySeed = this.key(); | 468 | newKeySeed = this.key(); |
471 | this.incrementReseedCounter(); | 469 | this.incrementReseedCounter(); |
472 | reseedCounter = this.reseedCounter(); | 470 | reseedCounter = this.reseedCounter(); |
473 | 471 | ||
474 | c = this.numberOfEntropyAccumulators(); | 472 | c = this.numberOfEntropyAccumulators(); |
475 | reseedCounterMask = 0xffffffff >>> (32 - c); | 473 | reseedCounterMask = 0xffffffff >>> (32 - c); |
476 | for (i=0; i<c; i++) { | 474 | for (i=0; i<c; i++) { |
477 | if ((i == 0) || ((reseedCounter & (reseedCounterMask >>> (c - i))) == 0)) { | 475 | if ((i == 0) || ((reseedCounter & (reseedCounterMask >>> (c - i))) == 0)) { |
478 | newKeySeed.appendBlock(this.accumulators()[i].stack()); | 476 | newKeySeed.appendBlock(this.accumulators()[i].stack()); |
479 | this.accumulators()[i].resetStack(); | 477 | this.accumulators()[i].resetStack(); |
480 | } | 478 | } |
481 | } | 479 | } |
482 | 480 | ||
483 | if (reseedCounter == 1) { | 481 | if (reseedCounter == 1) { |
484 | c = this.randomnessSources().length; | 482 | c = this.randomnessSources().length; |
485 | for (i=0; i<c; i++) { | 483 | for (i=0; i<c; i++) { |
486 | this.randomnessSources()[i].setBoostMode(false); | 484 | this.randomnessSources()[i].setBoostMode(false); |
487 | } | 485 | } |
488 | } | 486 | } |
489 | 487 | ||
490 | this.setKey(Clipperz.Crypto.SHA.sha_d256(newKeySeed)); | 488 | this.setKey(Clipperz.Crypto.SHA.sha_d256(newKeySeed)); |
491 | if (reseedCounter == 1) { | 489 | if (reseedCounter == 1) { |
492 | //MochiKit.Logging.logDebug("### PRNG.readyToGenerateRandomBytes"); | ||
493 | Clipperz.log("### PRNG.readyToGenerateRandomBytes"); | 490 | Clipperz.log("### PRNG.readyToGenerateRandomBytes"); |
494 | MochiKit.Signal.signal(this, 'readyToGenerateRandomBytes'); | 491 | MochiKit.Signal.signal(this, 'readyToGenerateRandomBytes'); |
495 | } | 492 | } |
496 | MochiKit.Signal.signal(this, 'reseeded'); | 493 | MochiKit.Signal.signal(this, 'reseeded'); |
497 | }, | 494 | }, |
498 | 495 | ||
499 | //------------------------------------------------------------------------- | 496 | //------------------------------------------------------------------------- |
500 | 497 | ||
501 | 'isReadyToGenerateRandomValues': function() { | 498 | 'isReadyToGenerateRandomValues': function() { |
502 | return this.reseedCounter() != 0; | 499 | return this.reseedCounter() != 0; |
503 | }, | 500 | }, |
504 | 501 | ||
505 | //------------------------------------------------------------------------- | 502 | //------------------------------------------------------------------------- |
506 | 503 | ||
507 | 'entropyLevel': function() { | 504 | 'entropyLevel': function() { |
508 | return this.accumulators()[0].stack().length() + (this.reseedCounter() * this.firstPoolReseedLevel()); | 505 | return this.accumulators()[0].stack().length() + (this.reseedCounter() * this.firstPoolReseedLevel()); |
509 | }, | 506 | }, |
510 | 507 | ||
511 | //------------------------------------------------------------------------- | 508 | //------------------------------------------------------------------------- |
512 | 509 | ||
513 | 'counter': function() { | 510 | 'counter': function() { |
514 | return this._counter; | 511 | return this._counter; |
515 | }, | 512 | }, |
516 | 513 | ||
@@ -539,49 +536,49 @@ Clipperz.log("### PRNG.readyToGenerateRandomBytes"); | |||
539 | 536 | ||
540 | //------------------------------------------------------------------------- | 537 | //------------------------------------------------------------------------- |
541 | 538 | ||
542 | 'getRandomBytes': function(aSize) { | 539 | 'getRandomBytes': function(aSize) { |
543 | var result; | 540 | var result; |
544 | 541 | ||
545 | if (this.isReadyToGenerateRandomValues()) { | 542 | if (this.isReadyToGenerateRandomValues()) { |
546 | var i,c; | 543 | var i,c; |
547 | var newKey; | 544 | var newKey; |
548 | 545 | ||
549 | result = new Clipperz.ByteArray(); | 546 | result = new Clipperz.ByteArray(); |
550 | 547 | ||
551 | c = Math.ceil(aSize / (128 / 8)); | 548 | c = Math.ceil(aSize / (128 / 8)); |
552 | for (i=0; i<c; i++) { | 549 | for (i=0; i<c; i++) { |
553 | result.appendBlock(this.getRandomBlock()); | 550 | result.appendBlock(this.getRandomBlock()); |
554 | } | 551 | } |
555 | 552 | ||
556 | if (result.length() != aSize) { | 553 | if (result.length() != aSize) { |
557 | result = result.split(0, aSize); | 554 | result = result.split(0, aSize); |
558 | } | 555 | } |
559 | 556 | ||
560 | newKey = this.getRandomBlock().appendBlock(this.getRandomBlock()); | 557 | newKey = this.getRandomBlock().appendBlock(this.getRandomBlock()); |
561 | this.setKey(newKey); | 558 | this.setKey(newKey); |
562 | } else { | 559 | } else { |
563 | MochiKit.Logging.logWarning("Fortuna generator has not enough entropy, yet!"); | 560 | Clipperz.logWarning("Fortuna generator has not enough entropy, yet!"); |
564 | throw Clipperz.Crypto.PRNG.exception.NotEnoughEntropy; | 561 | throw Clipperz.Crypto.PRNG.exception.NotEnoughEntropy; |
565 | } | 562 | } |
566 | 563 | ||
567 | return result; | 564 | return result; |
568 | }, | 565 | }, |
569 | 566 | ||
570 | //------------------------------------------------------------------------- | 567 | //------------------------------------------------------------------------- |
571 | 568 | ||
572 | 'addRandomByte': function(aSourceId, aPoolId, aRandomValue) { | 569 | 'addRandomByte': function(aSourceId, aPoolId, aRandomValue) { |
573 | varselectedAccumulator; | 570 | varselectedAccumulator; |
574 | 571 | ||
575 | selectedAccumulator = this.accumulators()[aPoolId]; | 572 | selectedAccumulator = this.accumulators()[aPoolId]; |
576 | selectedAccumulator.addRandomByte(aRandomValue); | 573 | selectedAccumulator.addRandomByte(aRandomValue); |
577 | 574 | ||
578 | if (aPoolId == 0) { | 575 | if (aPoolId == 0) { |
579 | MochiKit.Signal.signal(this, 'addedRandomByte') | 576 | MochiKit.Signal.signal(this, 'addedRandomByte') |
580 | if (selectedAccumulator.stack().length() > this.firstPoolReseedLevel()) { | 577 | if (selectedAccumulator.stack().length() > this.firstPoolReseedLevel()) { |
581 | this.reseed(); | 578 | this.reseed(); |
582 | } | 579 | } |
583 | } | 580 | } |
584 | }, | 581 | }, |
585 | 582 | ||
586 | //------------------------------------------------------------------------- | 583 | //------------------------------------------------------------------------- |
587 | 584 | ||
@@ -589,71 +586,63 @@ MochiKit.Logging.logWarning("Fortuna generator has not enough entropy, yet!"); | |||
589 | return this._numberOfEntropyAccumulators; | 586 | return this._numberOfEntropyAccumulators; |
590 | }, | 587 | }, |
591 | 588 | ||
592 | //------------------------------------------------------------------------- | 589 | //------------------------------------------------------------------------- |
593 | 590 | ||
594 | 'randomnessSources': function() { | 591 | 'randomnessSources': function() { |
595 | return this._randomnessSources; | 592 | return this._randomnessSources; |
596 | }, | 593 | }, |
597 | 594 | ||
598 | 'addRandomnessSource': function(aRandomnessSource) { | 595 | 'addRandomnessSource': function(aRandomnessSource) { |
599 | aRandomnessSource.setGenerator(this); | 596 | aRandomnessSource.setGenerator(this); |
600 | aRandomnessSource.setSourceId(this.randomnessSources().length); | 597 | aRandomnessSource.setSourceId(this.randomnessSources().length); |
601 | this.randomnessSources().push(aRandomnessSource); | 598 | this.randomnessSources().push(aRandomnessSource); |
602 | 599 | ||
603 | if (this.isReadyToGenerateRandomValues() == false) { | 600 | if (this.isReadyToGenerateRandomValues() == false) { |
604 | aRandomnessSource.setBoostMode(true); | 601 | aRandomnessSource.setBoostMode(true); |
605 | } | 602 | } |
606 | }, | 603 | }, |
607 | 604 | ||
608 | //------------------------------------------------------------------------- | 605 | //------------------------------------------------------------------------- |
609 | 606 | ||
610 | 'deferredEntropyCollection': function(aValue) { | 607 | 'deferredEntropyCollection': function(aValue) { |
611 | var result; | 608 | var result; |
612 | 609 | ||
613 | //MochiKit.Logging.logDebug(">>> PRNG.deferredEntropyCollection"); | ||
614 | 610 | ||
615 | if (this.isReadyToGenerateRandomValues()) { | 611 | if (this.isReadyToGenerateRandomValues()) { |
616 | //MochiKit.Logging.logDebug("--- PRNG.deferredEntropyCollection - 1"); | ||
617 | result = aValue; | 612 | result = aValue; |
618 | } else { | 613 | } else { |
619 | //MochiKit.Logging.logDebug("--- PRNG.deferredEntropyCollection - 2"); | ||
620 | var deferredResult; | 614 | var deferredResult; |
621 | 615 | ||
622 | // Clipperz.NotificationCenter.notify(this, 'updatedProgressState', 'collectingEntropy', true); | ||
623 | |||
624 | deferredResult = new Clipperz.Async.Deferred("PRNG.deferredEntropyCollection"); | 616 | deferredResult = new Clipperz.Async.Deferred("PRNG.deferredEntropyCollection"); |
625 | // deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("1.2.1 - PRNG.deferredEntropyCollection - 1: " + res); return res;}); | ||
626 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); | 617 | deferredResult.addCallback(MochiKit.Base.partial(MochiKit.Async.succeed, aValue)); |
627 | // deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("1.2.2 - PRNG.deferredEntropyCollection - 2: " + res); return res;}); | ||
628 | MochiKit.Signal.connect(this, | 618 | MochiKit.Signal.connect(this, |
629 | 'readyToGenerateRandomBytes', | 619 | 'readyToGenerateRandomBytes', |
630 | deferredResult, | 620 | deferredResult, |
631 | 'callback'); | 621 | 'callback'); |
632 | 622 | ||
633 | result = deferredResult; | 623 | result = deferredResult; |
634 | } | 624 | } |
635 | //MochiKit.Logging.logDebug("<<< PRNG.deferredEntropyCollection - result: " + result); | ||
636 | 625 | ||
637 | return result; | 626 | return result; |
638 | }, | 627 | }, |
639 | 628 | ||
640 | //------------------------------------------------------------------------- | 629 | //------------------------------------------------------------------------- |
641 | 630 | ||
642 | 'fastEntropyAccumulationForTestingPurpose': function() { | 631 | 'fastEntropyAccumulationForTestingPurpose': function() { |
643 | while (! this.isReadyToGenerateRandomValues()) { | 632 | while (! this.isReadyToGenerateRandomValues()) { |
644 | this.addRandomByte(Math.floor(Math.random() * 32), Math.floor(Math.random() * 32), Math.floor(Math.random() * 256)); | 633 | this.addRandomByte(Math.floor(Math.random() * 32), Math.floor(Math.random() * 32), Math.floor(Math.random() * 256)); |
645 | } | 634 | } |
646 | }, | 635 | }, |
647 | 636 | ||
648 | //------------------------------------------------------------------------- | 637 | //------------------------------------------------------------------------- |
649 | 638 | ||
650 | 'dump': function(appendToDoc) { | 639 | 'dump': function(appendToDoc) { |
651 | var tbl; | 640 | var tbl; |
652 | var i,c; | 641 | var i,c; |
653 | 642 | ||
654 | tbl = document.createElement("table"); | 643 | tbl = document.createElement("table"); |
655 | tbl.border = 0; | 644 | tbl.border = 0; |
656 | with (tbl.style) { | 645 | with (tbl.style) { |
657 | border = "1px solid lightgrey"; | 646 | border = "1px solid lightgrey"; |
658 | fontFamily = 'Helvetica, Arial, sans-serif'; | 647 | fontFamily = 'Helvetica, Arial, sans-serif'; |
659 | fontSize = '8pt'; | 648 | fontSize = '8pt'; |