Diffstat (limited to 'frontend/gamma/js/Clipperz/Crypto/AES.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/gamma/js/Clipperz/Crypto/AES.js | 29 |
1 files changed, 11 insertions, 18 deletions
diff --git a/frontend/gamma/js/Clipperz/Crypto/AES.js b/frontend/gamma/js/Clipperz/Crypto/AES.js index c811f1c..cb56f11 100644 --- a/frontend/gamma/js/Clipperz/Crypto/AES.js +++ b/frontend/gamma/js/Clipperz/Crypto/AES.js | |||
@@ -1,54 +1,52 @@ | |||
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.AES depends on Clipperz.ByteArray!"; | 25 | throw "Clipperz.Crypto.AES depends on Clipperz.ByteArray!"; |
28 | } | 26 | } |
29 | 27 | ||
30 | //Dependency commented to avoid a circular reference | 28 | //Dependency commented to avoid a circular reference |
31 | //try { if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { throw ""; }} catch (e) { | 29 | //try { if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { throw ""; }} catch (e) { |
32 | //throw "Clipperz.Crypto.AES depends on Clipperz.Crypto.PRNG!"; | 30 | //throw "Clipperz.Crypto.AES depends on Clipperz.Crypto.PRNG!"; |
33 | //} | 31 | //} |
34 | 32 | ||
35 | if (typeof(Clipperz.Crypto.AES) == 'undefined') { Clipperz.Crypto.AES = {}; } | 33 | if (typeof(Clipperz.Crypto.AES) == 'undefined') { Clipperz.Crypto.AES = {}; } |
36 | 34 | ||
37 | //############################################################################# | 35 | //############################################################################# |
38 | 36 | ||
39 | Clipperz.Crypto.AES.DeferredExecutionContext = function(args) { | 37 | Clipperz.Crypto.AES.DeferredExecutionContext = function(args) { |
40 | args = args || {}; | 38 | args = args || {}; |
41 | 39 | ||
42 | this._key = args.key; | 40 | this._key = args.key; |
43 | this._message = args.message; | 41 | this._message = args.message; |
44 | this._result = args.message.clone(); | 42 | this._result = args.message.clone(); |
45 | this._nonce = args.nonce; | 43 | this._nonce = args.nonce; |
46 | this._messageLength = this._message.length(); | 44 | this._messageLength = this._message.length(); |
47 | 45 | ||
48 | this._messageArray = this._message.arrayValues(); | 46 | this._messageArray = this._message.arrayValues(); |
49 | this._resultArray = this._result.arrayValues(); | 47 | this._resultArray = this._result.arrayValues(); |
50 | this._nonceArray = this._nonce.arrayValues(); | 48 | this._nonceArray = this._nonce.arrayValues(); |
51 | 49 | ||
52 | this._executionStep = 0; | 50 | this._executionStep = 0; |
53 | 51 | ||
54 | // this._elaborationChunkSize = 1024; // 4096; // 16384; //4096; | 52 | // this._elaborationChunkSize = 1024; // 4096; // 16384; //4096; |
@@ -91,89 +89,88 @@ Clipperz.Crypto.AES.DeferredExecutionContext.prototype = MochiKit.Base.update(nu | |||
91 | 'nonceArray': function() { | 89 | 'nonceArray': function() { |
92 | return this._nonceArray; | 90 | return this._nonceArray; |
93 | }, | 91 | }, |
94 | 92 | ||
95 | 'elaborationChunkSize': function() { | 93 | 'elaborationChunkSize': function() { |
96 | // return Clipperz.Crypto.AES.DeferredExecution.chunkSize; | 94 | // return Clipperz.Crypto.AES.DeferredExecution.chunkSize; |
97 | // return this._elaborationChunkSize; | 95 | // return this._elaborationChunkSize; |
98 | return (this._elaborationChunks * 1024); | 96 | return (this._elaborationChunks * 1024); |
99 | }, | 97 | }, |
100 | 98 | ||
101 | 'executionStep': function() { | 99 | 'executionStep': function() { |
102 | return this._executionStep; | 100 | return this._executionStep; |
103 | }, | 101 | }, |
104 | 102 | ||
105 | 'setExecutionStep': function(aValue) { | 103 | 'setExecutionStep': function(aValue) { |
106 | this._executionStep = aValue; | 104 | this._executionStep = aValue; |
107 | }, | 105 | }, |
108 | 106 | ||
109 | 'tuneExecutionParameters': function (anElapsedTime) { | 107 | 'tuneExecutionParameters': function (anElapsedTime) { |
110 | //var originalChunks = this._elaborationChunks; | 108 | //var originalChunks = this._elaborationChunks; |
111 | if (anElapsedTime > 0) { | 109 | if (anElapsedTime > 0) { |
112 | this._elaborationChunks = Math.round(this._elaborationChunks * ((anElapsedTime + 1000)/(anElapsedTime * 2))); | 110 | this._elaborationChunks = Math.round(this._elaborationChunks * ((anElapsedTime + 1000)/(anElapsedTime * 2))); |
113 | } | 111 | } |
114 | //Clipperz.log("tuneExecutionParameters - elapsedTime: " + anElapsedTime + /*originalChunks,*/ " chunks # " + this._elaborationChunks + " [" + this._executionStep + " / " + this._messageLength + "]"); | 112 | //Clipperz.log("tuneExecutionParameters - elapsedTime: " + anElapsedTime + /*originalChunks,*/ " chunks # " + this._elaborationChunks + " [" + this._executionStep + " / " + this._messageLength + "]"); |
115 | }, | 113 | }, |
116 | 114 | ||
117 | 'pause': function(aValue) { | 115 | 'pause': function(aValue) { |
118 | // return MochiKit.Async.wait(Clipperz.Crypto.AES.DeferredExecution.pauseTime, aValue); | 116 | // return MochiKit.Async.wait(Clipperz.Crypto.AES.DeferredExecution.pauseTime, aValue); |
119 | return MochiKit.Async.wait(this._pauseTime, aValue); | 117 | return MochiKit.Async.wait(this._pauseTime, aValue); |
120 | }, | 118 | }, |
121 | 119 | ||
122 | 'isDone': function () { | 120 | 'isDone': function () { |
123 | //console.log("isDone", this.executionStep(), this.messageLength()); | ||
124 | return (this._executionStep >= this._messageLength); | 121 | return (this._executionStep >= this._messageLength); |
125 | }, | 122 | }, |
126 | 123 | ||
127 | //----------------------------------------------------------------------------- | 124 | //----------------------------------------------------------------------------- |
128 | __syntaxFix__: "syntax fix" | 125 | __syntaxFix__: "syntax fix" |
129 | 126 | ||
130 | }); | 127 | }); |
131 | 128 | ||
132 | //############################################################################# | 129 | //############################################################################# |
133 | 130 | ||
134 | Clipperz.Crypto.AES.Key = function(args) { | 131 | Clipperz.Crypto.AES.Key = function(args) { |
135 | args = args || {}; | 132 | args = args || {}; |
136 | 133 | ||
137 | this._key = args.key; | 134 | this._key = args.key; |
138 | this._keySize = args.keySize || this.key().length(); | 135 | this._keySize = args.keySize || this.key().length(); |
139 | 136 | ||
140 | if (this.keySize() == 128/8) { | 137 | if (this.keySize() == 128/8) { |
141 | this._b = 176; | 138 | this._b = 176; |
142 | this._numberOfRounds = 10; | 139 | this._numberOfRounds = 10; |
143 | } else if (this.keySize() == 256/8) { | 140 | } else if (this.keySize() == 256/8) { |
144 | this._b = 240; | 141 | this._b = 240; |
145 | this._numberOfRounds = 14; | 142 | this._numberOfRounds = 14; |
146 | } else { | 143 | } else { |
147 | MochiKit.Logging.logError("AES unsupported key size: " + (this.keySize() * 8) + " bits"); | 144 | Clipperz.logError("AES unsupported key size: " + (this.keySize() * 8) + " bits"); |
148 | throw Clipperz.Crypto.AES.exception.UnsupportedKeySize; | 145 | throw Clipperz.Crypto.AES.exception.UnsupportedKeySize; |
149 | } | 146 | } |
150 | 147 | ||
151 | this._stretchedKey = null; | 148 | this._stretchedKey = null; |
152 | 149 | ||
153 | return this; | 150 | return this; |
154 | } | 151 | } |
155 | 152 | ||
156 | Clipperz.Crypto.AES.Key.prototype = MochiKit.Base.update(null, { | 153 | Clipperz.Crypto.AES.Key.prototype = MochiKit.Base.update(null, { |
157 | 154 | ||
158 | 'asString': function() { | 155 | 'asString': function() { |
159 | return "Clipperz.Crypto.AES.Key (" + this.key().toHexString() + ")"; | 156 | return "Clipperz.Crypto.AES.Key (" + this.key().toHexString() + ")"; |
160 | }, | 157 | }, |
161 | 158 | ||
162 | //----------------------------------------------------------------------------- | 159 | //----------------------------------------------------------------------------- |
163 | 160 | ||
164 | 'key': function() { | 161 | 'key': function() { |
165 | return this._key; | 162 | return this._key; |
166 | }, | 163 | }, |
167 | 164 | ||
168 | 'keySize': function() { | 165 | 'keySize': function() { |
169 | return this._keySize; | 166 | return this._keySize; |
170 | }, | 167 | }, |
171 | 168 | ||
172 | 'b': function() { | 169 | 'b': function() { |
173 | return this._b; | 170 | return this._b; |
174 | }, | 171 | }, |
175 | 172 | ||
176 | 'numberOfRounds': function() { | 173 | 'numberOfRounds': function() { |
177 | return this._numberOfRounds; | 174 | return this._numberOfRounds; |
178 | }, | 175 | }, |
179 | //========================================================================= | 176 | //========================================================================= |
@@ -779,88 +776,84 @@ MochiKit.Base.update(Clipperz.Crypto.AES, { | |||
779 | 776 | ||
780 | 'deferredEncryptBlocks': function(anExecutionContext) { | 777 | 'deferredEncryptBlocks': function(anExecutionContext) { |
781 | vardeferredResult; | 778 | vardeferredResult; |
782 | 779 | ||
783 | if (! anExecutionContext.isDone()) { | 780 | if (! anExecutionContext.isDone()) { |
784 | deferredResult = Clipperz.Async.callbacks("Clipperz.Crypto.AES.deferredEncryptBloks", [ | 781 | deferredResult = Clipperz.Async.callbacks("Clipperz.Crypto.AES.deferredEncryptBloks", [ |
785 | Clipperz.Crypto.AES.deferredEncryptExecutionChunk, | 782 | Clipperz.Crypto.AES.deferredEncryptExecutionChunk, |
786 | MochiKit.Base.method(anExecutionContext, 'pause'), | 783 | MochiKit.Base.method(anExecutionContext, 'pause'), |
787 | Clipperz.Crypto.AES.deferredEncryptBlocks | 784 | Clipperz.Crypto.AES.deferredEncryptBlocks |
788 | ], {trace:false}, anExecutionContext); | 785 | ], {trace:false}, anExecutionContext); |
789 | } else { | 786 | } else { |
790 | deferredResult = MochiKit.Async.succeed(anExecutionContext); | 787 | deferredResult = MochiKit.Async.succeed(anExecutionContext); |
791 | } | 788 | } |
792 | 789 | ||
793 | return deferredResult; | 790 | return deferredResult; |
794 | }, | 791 | }, |
795 | 792 | ||
796 | //----------------------------------------------------------------------------- | 793 | //----------------------------------------------------------------------------- |
797 | 794 | ||
798 | 'deferredEncrypt': function(aKey, someData, aNonce) { | 795 | 'deferredEncrypt': function(aKey, someData, aNonce) { |
799 | var deferredResult; | 796 | var deferredResult; |
800 | varexecutionContext; | 797 | varexecutionContext; |
801 | var result; | 798 | var result; |
802 | var nonce; | 799 | var nonce; |
803 | var key; | 800 | var key; |
804 | 801 | ||
805 | key = new Clipperz.Crypto.AES.Key({key:aKey}); | 802 | key = new Clipperz.Crypto.AES.Key({key:aKey}); |
806 | nonce = aNonce ? aNonce.clone() : Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(128/8); | 803 | nonce = aNonce ? aNonce.clone() : Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(128/8); |
807 | 804 | ||
808 | executionContext = new Clipperz.Crypto.AES.DeferredExecutionContext({key:key, message:someData, nonce:nonce}); | 805 | executionContext = new Clipperz.Crypto.AES.DeferredExecutionContext({key:key, message:someData, nonce:nonce}); |
809 | 806 | ||
810 | deferredResult = new Clipperz.Async.Deferred("AES.deferredEncrypt"); | 807 | deferredResult = new Clipperz.Async.Deferred("AES.deferredEncrypt"); |
811 | //deferredResult.addCallback(function (aValue) { console.log(">>> deferredEncrypt"); return aValue; }); | ||
812 | deferredResult.addCallback(Clipperz.Crypto.AES.deferredEncryptBlocks); | 808 | deferredResult.addCallback(Clipperz.Crypto.AES.deferredEncryptBlocks); |
813 | deferredResult.addCallback(function(anExecutionContext) { | 809 | deferredResult.addCallback(function(anExecutionContext) { |
814 | var result; | 810 | var result; |
815 | 811 | ||
816 | result = anExecutionContext.nonce().clone(); | 812 | result = anExecutionContext.nonce().clone(); |
817 | result.appendBytes(anExecutionContext.resultArray()); | 813 | result.appendBytes(anExecutionContext.resultArray()); |
818 | 814 | ||
819 | return result; | 815 | return result; |
820 | }); | 816 | }); |
821 | //deferredResult.addCallback(function (aValue) { console.log("<<< deferredEncrypt"); return aValue; }); | ||
822 | deferredResult.callback(executionContext) | 817 | deferredResult.callback(executionContext) |
823 | 818 | ||
824 | return deferredResult; | 819 | return deferredResult; |
825 | }, | 820 | }, |
826 | 821 | ||
827 | //----------------------------------------------------------------------------- | 822 | //----------------------------------------------------------------------------- |
828 | 823 | ||
829 | 'deferredDecrypt': function(aKey, someData) { | 824 | 'deferredDecrypt': function(aKey, someData) { |
830 | var deferredResult | 825 | var deferredResult |
831 | var nonce; | 826 | var nonce; |
832 | var message; | 827 | var message; |
833 | var key; | 828 | var key; |
834 | 829 | ||
835 | key = new Clipperz.Crypto.AES.Key({key:aKey}); | 830 | key = new Clipperz.Crypto.AES.Key({key:aKey}); |
836 | nonce = someData.split(0, (128/8)); | 831 | nonce = someData.split(0, (128/8)); |
837 | message = someData.split(128/8); | 832 | message = someData.split(128/8); |
838 | executionContext = new Clipperz.Crypto.AES.DeferredExecutionContext({key:key, message:message, nonce:nonce}); | 833 | executionContext = new Clipperz.Crypto.AES.DeferredExecutionContext({key:key, message:message, nonce:nonce}); |
839 | 834 | ||
840 | deferredResult = new Clipperz.Async.Deferred("AES.deferredDecrypt"); | 835 | deferredResult = new Clipperz.Async.Deferred("AES.deferredDecrypt"); |
841 | //deferredResult.addCallback(function (aValue) { console.log(">>> deferredDecrypt"); return aValue; }); | ||
842 | deferredResult.addCallback(Clipperz.Crypto.AES.deferredEncryptBlocks); | 836 | deferredResult.addCallback(Clipperz.Crypto.AES.deferredEncryptBlocks); |
843 | deferredResult.addCallback(function(anExecutionContext) { | 837 | deferredResult.addCallback(function(anExecutionContext) { |
844 | return anExecutionContext.result(); | 838 | return anExecutionContext.result(); |
845 | }); | 839 | }); |
846 | //deferredResult.addCallback(function (aValue) { console.log("<<< deferredDecrypt"); return aValue; }); | ||
847 | deferredResult.callback(executionContext); | 840 | deferredResult.callback(executionContext); |
848 | 841 | ||
849 | return deferredResult; | 842 | return deferredResult; |
850 | }, | 843 | }, |
851 | 844 | ||
852 | //----------------------------------------------------------------------------- | 845 | //----------------------------------------------------------------------------- |
853 | __syntaxFix__: "syntax fix" | 846 | __syntaxFix__: "syntax fix" |
854 | 847 | ||
855 | }); | 848 | }); |
856 | 849 | ||
857 | //############################################################################# | 850 | //############################################################################# |
858 | 851 | ||
859 | //Clipperz.Crypto.AES.DeferredExecution = { | 852 | //Clipperz.Crypto.AES.DeferredExecution = { |
860 | // 'chunkSize': 16384, // 4096, // 1024 4096 8192 1638432768; | 853 | // 'chunkSize': 16384, // 4096, // 1024 4096 8192 1638432768; |
861 | // 'pauseTime': 0.02 //0.2 | 854 | // 'pauseTime': 0.02 //0.2 |
862 | //} | 855 | //} |
863 | 856 | ||
864 | Clipperz.Crypto.AES.exception = { | 857 | Clipperz.Crypto.AES.exception = { |
865 | 'UnsupportedKeySize': new MochiKit.Base.NamedError("Clipperz.Crypto.AES.exception.UnsupportedKeySize") | 858 | 'UnsupportedKeySize': new MochiKit.Base.NamedError("Clipperz.Crypto.AES.exception.UnsupportedKeySize") |
866 | }; | 859 | }; |