Diffstat (limited to 'frontend/beta/js/Clipperz/Crypto') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/AES.js | 22 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/Base.js | 22 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/BigInt.js | 22 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/BigInt_scoped.js | 22 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/ECC.js | 22 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Curve.js | 22 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/FiniteField.js | 22 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Point.js | 22 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Value.js | 22 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/PRNG.js | 22 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/RSA.js | 22 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/SHA.js | 22 | ||||
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/SRP.js | 22 |
13 files changed, 130 insertions, 156 deletions
diff --git a/frontend/beta/js/Clipperz/Crypto/AES.js b/frontend/beta/js/Clipperz/Crypto/AES.js index 7ddda3e..a5c63fb 100644 --- a/frontend/beta/js/Clipperz/Crypto/AES.js +++ b/frontend/beta/js/Clipperz/Crypto/AES.js | |||
@@ -1,406 +1,404 @@ | |||
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 | return this; | 52 | return this; |
55 | } | 53 | } |
56 | 54 | ||
57 | Clipperz.Crypto.AES.DeferredExecutionContext.prototype = MochiKit.Base.update(null, { | 55 | Clipperz.Crypto.AES.DeferredExecutionContext.prototype = MochiKit.Base.update(null, { |
58 | 56 | ||
59 | 'key': function() { | 57 | 'key': function() { |
60 | return this._key; | 58 | return this._key; |
61 | }, | 59 | }, |
62 | 60 | ||
63 | 'message': function() { | 61 | 'message': function() { |
64 | return this._message; | 62 | return this._message; |
65 | }, | 63 | }, |
66 | 64 | ||
67 | 'messageLength': function() { | 65 | 'messageLength': function() { |
68 | return this._messageLength; | 66 | return this._messageLength; |
69 | }, | 67 | }, |
70 | 68 | ||
71 | 'result': function() { | 69 | 'result': function() { |
72 | return new Clipperz.ByteArray(this.resultArray()); | 70 | return new Clipperz.ByteArray(this.resultArray()); |
73 | }, | 71 | }, |
74 | 72 | ||
75 | 'nonce': function() { | 73 | 'nonce': function() { |
76 | return this._nonce; | 74 | return this._nonce; |
77 | }, | 75 | }, |
78 | 76 | ||
79 | 'messageArray': function() { | 77 | 'messageArray': function() { |
80 | return this._messageArray; | 78 | return this._messageArray; |
81 | }, | 79 | }, |
82 | 80 | ||
83 | 'resultArray': function() { | 81 | 'resultArray': function() { |
84 | return this._resultArray; | 82 | return this._resultArray; |
85 | }, | 83 | }, |
86 | 84 | ||
87 | 'nonceArray': function() { | 85 | 'nonceArray': function() { |
88 | return this._nonceArray; | 86 | return this._nonceArray; |
89 | }, | 87 | }, |
90 | 88 | ||
91 | 'elaborationChunkSize': function() { | 89 | 'elaborationChunkSize': function() { |
92 | return Clipperz.Crypto.AES.DeferredExecution.chunkSize; | 90 | return Clipperz.Crypto.AES.DeferredExecution.chunkSize; |
93 | }, | 91 | }, |
94 | 92 | ||
95 | 'executionStep': function() { | 93 | 'executionStep': function() { |
96 | return this._executionStep; | 94 | return this._executionStep; |
97 | }, | 95 | }, |
98 | 96 | ||
99 | 'setExecutionStep': function(aValue) { | 97 | 'setExecutionStep': function(aValue) { |
100 | this._executionStep = aValue; | 98 | this._executionStep = aValue; |
101 | }, | 99 | }, |
102 | 100 | ||
103 | 'pause': function(aValue) { | 101 | 'pause': function(aValue) { |
104 | return MochiKit.Async.wait(Clipperz.Crypto.AES.DeferredExecution.pauseTime, aValue); | 102 | return MochiKit.Async.wait(Clipperz.Crypto.AES.DeferredExecution.pauseTime, aValue); |
105 | }, | 103 | }, |
106 | 104 | ||
107 | //----------------------------------------------------------------------------- | 105 | //----------------------------------------------------------------------------- |
108 | __syntaxFix__: "syntax fix" | 106 | __syntaxFix__: "syntax fix" |
109 | 107 | ||
110 | }); | 108 | }); |
111 | 109 | ||
112 | //############################################################################# | 110 | //############################################################################# |
113 | 111 | ||
114 | Clipperz.Crypto.AES.Key = function(args) { | 112 | Clipperz.Crypto.AES.Key = function(args) { |
115 | args = args || {}; | 113 | args = args || {}; |
116 | 114 | ||
117 | this._key = args.key; | 115 | this._key = args.key; |
118 | this._keySize = args.keySize || this.key().length(); | 116 | this._keySize = args.keySize || this.key().length(); |
119 | 117 | ||
120 | if (this.keySize() == 128/8) { | 118 | if (this.keySize() == 128/8) { |
121 | this._b = 176; | 119 | this._b = 176; |
122 | this._numberOfRounds = 10; | 120 | this._numberOfRounds = 10; |
123 | } else if (this.keySize() == 256/8) { | 121 | } else if (this.keySize() == 256/8) { |
124 | this._b = 240; | 122 | this._b = 240; |
125 | this._numberOfRounds = 14; | 123 | this._numberOfRounds = 14; |
126 | } else { | 124 | } else { |
127 | MochiKit.Logging.logError("AES unsupported key size: " + (this.keySize() * 8) + " bits"); | 125 | MochiKit.Logging.logError("AES unsupported key size: " + (this.keySize() * 8) + " bits"); |
128 | throw Clipperz.Crypto.AES.exception.UnsupportedKeySize; | 126 | throw Clipperz.Crypto.AES.exception.UnsupportedKeySize; |
129 | } | 127 | } |
130 | 128 | ||
131 | this._stretchedKey = null; | 129 | this._stretchedKey = null; |
132 | 130 | ||
133 | return this; | 131 | return this; |
134 | } | 132 | } |
135 | 133 | ||
136 | Clipperz.Crypto.AES.Key.prototype = MochiKit.Base.update(null, { | 134 | Clipperz.Crypto.AES.Key.prototype = MochiKit.Base.update(null, { |
137 | 135 | ||
138 | 'asString': function() { | 136 | 'asString': function() { |
139 | return "Clipperz.Crypto.AES.Key (" + this.key().toHexString() + ")"; | 137 | return "Clipperz.Crypto.AES.Key (" + this.key().toHexString() + ")"; |
140 | }, | 138 | }, |
141 | 139 | ||
142 | //----------------------------------------------------------------------------- | 140 | //----------------------------------------------------------------------------- |
143 | 141 | ||
144 | 'key': function() { | 142 | 'key': function() { |
145 | return this._key; | 143 | return this._key; |
146 | }, | 144 | }, |
147 | 145 | ||
148 | 'keySize': function() { | 146 | 'keySize': function() { |
149 | return this._keySize; | 147 | return this._keySize; |
150 | }, | 148 | }, |
151 | 149 | ||
152 | 'b': function() { | 150 | 'b': function() { |
153 | return this._b; | 151 | return this._b; |
154 | }, | 152 | }, |
155 | 153 | ||
156 | 'numberOfRounds': function() { | 154 | 'numberOfRounds': function() { |
157 | return this._numberOfRounds; | 155 | return this._numberOfRounds; |
158 | }, | 156 | }, |
159 | //========================================================================= | 157 | //========================================================================= |
160 | 158 | ||
161 | 'keyScheduleCore': function(aWord, aRoundConstantsIndex) { | 159 | 'keyScheduleCore': function(aWord, aRoundConstantsIndex) { |
162 | varresult; | 160 | varresult; |
163 | var sbox; | 161 | var sbox; |
164 | 162 | ||
165 | sbox = Clipperz.Crypto.AES.sbox(); | 163 | sbox = Clipperz.Crypto.AES.sbox(); |
166 | 164 | ||
167 | result = [sbox[aWord[1]] ^ Clipperz.Crypto.AES.roundConstants()[aRoundConstantsIndex], | 165 | result = [sbox[aWord[1]] ^ Clipperz.Crypto.AES.roundConstants()[aRoundConstantsIndex], |
168 | sbox[aWord[2]], | 166 | sbox[aWord[2]], |
169 | sbox[aWord[3]], | 167 | sbox[aWord[3]], |
170 | sbox[aWord[0]]]; | 168 | sbox[aWord[0]]]; |
171 | 169 | ||
172 | return result; | 170 | return result; |
173 | }, | 171 | }, |
174 | 172 | ||
175 | //----------------------------------------------------------------------------- | 173 | //----------------------------------------------------------------------------- |
176 | 174 | ||
177 | 'xorWithPreviousStretchValues': function(aKey, aWord, aPreviousWordIndex) { | 175 | 'xorWithPreviousStretchValues': function(aKey, aWord, aPreviousWordIndex) { |
178 | varresult; | 176 | varresult; |
179 | var i,c; | 177 | var i,c; |
180 | 178 | ||
181 | result = []; | 179 | result = []; |
182 | c = 4; | 180 | c = 4; |
183 | for (i=0; i<c; i++) { | 181 | for (i=0; i<c; i++) { |
184 | result[i] = aWord[i] ^ aKey.byteAtIndex(aPreviousWordIndex + i); | 182 | result[i] = aWord[i] ^ aKey.byteAtIndex(aPreviousWordIndex + i); |
185 | } | 183 | } |
186 | 184 | ||
187 | return result; | 185 | return result; |
188 | }, | 186 | }, |
189 | 187 | ||
190 | //----------------------------------------------------------------------------- | 188 | //----------------------------------------------------------------------------- |
191 | 189 | ||
192 | 'sboxShakeup': function(aWord) { | 190 | 'sboxShakeup': function(aWord) { |
193 | var result; | 191 | var result; |
194 | var sbox; | 192 | var sbox; |
195 | var i,c; | 193 | var i,c; |
196 | 194 | ||
197 | result = []; | 195 | result = []; |
198 | sbox = Clipperz.Crypto.AES.sbox(); | 196 | sbox = Clipperz.Crypto.AES.sbox(); |
199 | c =4; | 197 | c =4; |
200 | for (i=0; i<c; i++) { | 198 | for (i=0; i<c; i++) { |
201 | result[i] = sbox[aWord[i]]; | 199 | result[i] = sbox[aWord[i]]; |
202 | } | 200 | } |
203 | 201 | ||
204 | return result; | 202 | return result; |
205 | }, | 203 | }, |
206 | 204 | ||
207 | //----------------------------------------------------------------------------- | 205 | //----------------------------------------------------------------------------- |
208 | 206 | ||
209 | 'stretchKey': function(aKey) { | 207 | 'stretchKey': function(aKey) { |
210 | varcurrentWord; | 208 | varcurrentWord; |
211 | varkeyLength; | 209 | varkeyLength; |
212 | varpreviousStretchIndex; | 210 | varpreviousStretchIndex; |
213 | var i,c; | 211 | var i,c; |
214 | 212 | ||
215 | keyLength = aKey.length(); | 213 | keyLength = aKey.length(); |
216 | previousStretchIndex = keyLength - this.keySize(); | 214 | previousStretchIndex = keyLength - this.keySize(); |
217 | 215 | ||
218 | currentWord = [aKey.byteAtIndex(keyLength - 4), | 216 | currentWord = [aKey.byteAtIndex(keyLength - 4), |
219 | aKey.byteAtIndex(keyLength - 3), | 217 | aKey.byteAtIndex(keyLength - 3), |
220 | aKey.byteAtIndex(keyLength - 2), | 218 | aKey.byteAtIndex(keyLength - 2), |
221 | aKey.byteAtIndex(keyLength - 1)]; | 219 | aKey.byteAtIndex(keyLength - 1)]; |
222 | currentWord = this.keyScheduleCore(currentWord, keyLength / this.keySize()); | 220 | currentWord = this.keyScheduleCore(currentWord, keyLength / this.keySize()); |
223 | 221 | ||
224 | if (this.keySize() == 256/8) { | 222 | if (this.keySize() == 256/8) { |
225 | c = 8; | 223 | c = 8; |
226 | } else if (this.keySize() == 128/8){ | 224 | } else if (this.keySize() == 128/8){ |
227 | c = 4; | 225 | c = 4; |
228 | } | 226 | } |
229 | 227 | ||
230 | for (i=0; i<c; i++) { | 228 | for (i=0; i<c; i++) { |
231 | if (i == 4) { | 229 | if (i == 4) { |
232 | //fifth streatch word | 230 | //fifth streatch word |
233 | currentWord = this.sboxShakeup(currentWord); | 231 | currentWord = this.sboxShakeup(currentWord); |
234 | } | 232 | } |
235 | 233 | ||
236 | currentWord = this.xorWithPreviousStretchValues(aKey, currentWord, previousStretchIndex + (i*4)); | 234 | currentWord = this.xorWithPreviousStretchValues(aKey, currentWord, previousStretchIndex + (i*4)); |
237 | aKey.appendBytes(currentWord); | 235 | aKey.appendBytes(currentWord); |
238 | } | 236 | } |
239 | 237 | ||
240 | return aKey; | 238 | return aKey; |
241 | }, | 239 | }, |
242 | 240 | ||
243 | //----------------------------------------------------------------------------- | 241 | //----------------------------------------------------------------------------- |
244 | 242 | ||
245 | 'stretchedKey': function() { | 243 | 'stretchedKey': function() { |
246 | if (this._stretchedKey == null) { | 244 | if (this._stretchedKey == null) { |
247 | var stretchedKey; | 245 | var stretchedKey; |
248 | 246 | ||
249 | stretchedKey = this.key().clone(); | 247 | stretchedKey = this.key().clone(); |
250 | 248 | ||
251 | while (stretchedKey.length() < this.keySize()) { | 249 | while (stretchedKey.length() < this.keySize()) { |
252 | stretchedKey.appendByte(0); | 250 | stretchedKey.appendByte(0); |
253 | } | 251 | } |
254 | 252 | ||
255 | while (stretchedKey.length() < this.b()) { | 253 | while (stretchedKey.length() < this.b()) { |
256 | stretchedKey = this.stretchKey(stretchedKey); | 254 | stretchedKey = this.stretchKey(stretchedKey); |
257 | } | 255 | } |
258 | 256 | ||
259 | this._stretchedKey = stretchedKey.split(0, this.b()); | 257 | this._stretchedKey = stretchedKey.split(0, this.b()); |
260 | } | 258 | } |
261 | 259 | ||
262 | return this._stretchedKey; | 260 | return this._stretchedKey; |
263 | }, | 261 | }, |
264 | 262 | ||
265 | //========================================================================= | 263 | //========================================================================= |
266 | __syntaxFix__: "syntax fix" | 264 | __syntaxFix__: "syntax fix" |
267 | }); | 265 | }); |
268 | 266 | ||
269 | //############################################################################# | 267 | //############################################################################# |
270 | 268 | ||
271 | Clipperz.Crypto.AES.State = function(args) { | 269 | Clipperz.Crypto.AES.State = function(args) { |
272 | args = args || {}; | 270 | args = args || {}; |
273 | 271 | ||
274 | this._data = args.block; | 272 | this._data = args.block; |
275 | this._key = args.key; | 273 | this._key = args.key; |
276 | 274 | ||
277 | return this; | 275 | return this; |
278 | } | 276 | } |
279 | 277 | ||
280 | Clipperz.Crypto.AES.State.prototype = MochiKit.Base.update(null, { | 278 | Clipperz.Crypto.AES.State.prototype = MochiKit.Base.update(null, { |
281 | 279 | ||
282 | 'key': function() { | 280 | 'key': function() { |
283 | return this._key; | 281 | return this._key; |
284 | }, | 282 | }, |
285 | 283 | ||
286 | //----------------------------------------------------------------------------- | 284 | //----------------------------------------------------------------------------- |
287 | 285 | ||
288 | 'data': function() { | 286 | 'data': function() { |
289 | return this._data; | 287 | return this._data; |
290 | }, | 288 | }, |
291 | 289 | ||
292 | 'setData': function(aValue) { | 290 | 'setData': function(aValue) { |
293 | this._data = aValue; | 291 | this._data = aValue; |
294 | }, | 292 | }, |
295 | 293 | ||
296 | //========================================================================= | 294 | //========================================================================= |
297 | 295 | ||
298 | 'addRoundKey': function(aRoundNumber) { | 296 | 'addRoundKey': function(aRoundNumber) { |
299 | //each byte of the state is combined with the round key; each round key is derived from the cipher key using a key schedule. | 297 | //each byte of the state is combined with the round key; each round key is derived from the cipher key using a key schedule. |
300 | vardata; | 298 | vardata; |
301 | varstretchedKey; | 299 | varstretchedKey; |
302 | varfirstStretchedKeyIndex; | 300 | varfirstStretchedKeyIndex; |
303 | var i,c; | 301 | var i,c; |
304 | 302 | ||
305 | data = this.data(); | 303 | data = this.data(); |
306 | stretchedKey = this.key().stretchedKey(); | 304 | stretchedKey = this.key().stretchedKey(); |
307 | firstStretchedKeyIndex = aRoundNumber * (128/8); | 305 | firstStretchedKeyIndex = aRoundNumber * (128/8); |
308 | c = 128/8; | 306 | c = 128/8; |
309 | for (i=0; i<c; i++) { | 307 | for (i=0; i<c; i++) { |
310 | data[i] = data[i] ^ stretchedKey.byteAtIndex(firstStretchedKeyIndex + i); | 308 | data[i] = data[i] ^ stretchedKey.byteAtIndex(firstStretchedKeyIndex + i); |
311 | } | 309 | } |
312 | }, | 310 | }, |
313 | 311 | ||
314 | //----------------------------------------------------------------------------- | 312 | //----------------------------------------------------------------------------- |
315 | 313 | ||
316 | 'subBytes': function() { | 314 | 'subBytes': function() { |
317 | // a non-linear substitution step where each byte is replaced with another according to a lookup table. | 315 | // a non-linear substitution step where each byte is replaced with another according to a lookup table. |
318 | var i,c; | 316 | var i,c; |
319 | vardata; | 317 | vardata; |
320 | var sbox; | 318 | var sbox; |
321 | 319 | ||
322 | data = this.data(); | 320 | data = this.data(); |
323 | sbox = Clipperz.Crypto.AES.sbox(); | 321 | sbox = Clipperz.Crypto.AES.sbox(); |
324 | 322 | ||
325 | c = 16; | 323 | c = 16; |
326 | for (i=0; i<c; i++) { | 324 | for (i=0; i<c; i++) { |
327 | data[i] = sbox[data[i]]; | 325 | data[i] = sbox[data[i]]; |
328 | } | 326 | } |
329 | }, | 327 | }, |
330 | 328 | ||
331 | //----------------------------------------------------------------------------- | 329 | //----------------------------------------------------------------------------- |
332 | 330 | ||
333 | 'shiftRows': function() { | 331 | 'shiftRows': function() { |
334 | //a transposition step where each row of the state is shifted cyclically a certain number of steps. | 332 | //a transposition step where each row of the state is shifted cyclically a certain number of steps. |
335 | varnewValue; | 333 | varnewValue; |
336 | vardata; | 334 | vardata; |
337 | varshiftMapping; | 335 | varshiftMapping; |
338 | vari,c; | 336 | vari,c; |
339 | 337 | ||
340 | newValue = new Array(16); | 338 | newValue = new Array(16); |
341 | data = this.data(); | 339 | data = this.data(); |
342 | shiftMapping = Clipperz.Crypto.AES.shiftRowMapping(); | 340 | shiftMapping = Clipperz.Crypto.AES.shiftRowMapping(); |
343 | // [0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11]; | 341 | // [0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11]; |
344 | c = 16; | 342 | c = 16; |
345 | for (i=0; i<c; i++) { | 343 | for (i=0; i<c; i++) { |
346 | newValue[i] = data[shiftMapping[i]]; | 344 | newValue[i] = data[shiftMapping[i]]; |
347 | } | 345 | } |
348 | for (i=0; i<c; i++) { | 346 | for (i=0; i<c; i++) { |
349 | data[i] = newValue[i]; | 347 | data[i] = newValue[i]; |
350 | } | 348 | } |
351 | }, | 349 | }, |
352 | 350 | ||
353 | //----------------------------------------------------------------------------- | 351 | //----------------------------------------------------------------------------- |
354 | /* | 352 | /* |
355 | 'mixColumnsWithValues': function(someValues) { | 353 | 'mixColumnsWithValues': function(someValues) { |
356 | varresult; | 354 | varresult; |
357 | vara; | 355 | vara; |
358 | var i,c; | 356 | var i,c; |
359 | 357 | ||
360 | c = 4; | 358 | c = 4; |
361 | result = []; | 359 | result = []; |
362 | a = []; | 360 | a = []; |
363 | for (i=0; i<c; i++) { | 361 | for (i=0; i<c; i++) { |
364 | a[i] = []; | 362 | a[i] = []; |
365 | a[i][1] = someValues[i] | 363 | a[i][1] = someValues[i] |
366 | if ((a[i][1] & 0x80) == 0x80) { | 364 | if ((a[i][1] & 0x80) == 0x80) { |
367 | a[i][2] = (a[i][1] << 1) ^ 0x11b; | 365 | a[i][2] = (a[i][1] << 1) ^ 0x11b; |
368 | } else { | 366 | } else { |
369 | a[i][2] = a[i][1] << 1; | 367 | a[i][2] = a[i][1] << 1; |
370 | } | 368 | } |
371 | 369 | ||
372 | a[i][3] = a[i][2] ^ a[i][1]; | 370 | a[i][3] = a[i][2] ^ a[i][1]; |
373 | } | 371 | } |
374 | 372 | ||
375 | for (i=0; i<c; i++) { | 373 | for (i=0; i<c; i++) { |
376 | varx; | 374 | varx; |
377 | 375 | ||
378 | x = Clipperz.Crypto.AES.mixColumnsMatrix()[i]; | 376 | x = Clipperz.Crypto.AES.mixColumnsMatrix()[i]; |
379 | result[i] = a[0][x[0]] ^ a[1][x[1]] ^ a[2][x[2]] ^ a[3][x[3]]; | 377 | result[i] = a[0][x[0]] ^ a[1][x[1]] ^ a[2][x[2]] ^ a[3][x[3]]; |
380 | } | 378 | } |
381 | 379 | ||
382 | return result; | 380 | return result; |
383 | }, | 381 | }, |
384 | 382 | ||
385 | 'mixColumns': function() { | 383 | 'mixColumns': function() { |
386 | //a mixing operation which operates on the columns of the state, combining the four bytes in each column using a linear transformation. | 384 | //a mixing operation which operates on the columns of the state, combining the four bytes in each column using a linear transformation. |
387 | var data; | 385 | var data; |
388 | var i, c; | 386 | var i, c; |
389 | 387 | ||
390 | data = this.data(); | 388 | data = this.data(); |
391 | c = 4; | 389 | c = 4; |
392 | for(i=0; i<c; i++) { | 390 | for(i=0; i<c; i++) { |
393 | varblockIndex; | 391 | varblockIndex; |
394 | var mixedValues; | 392 | var mixedValues; |
395 | 393 | ||
396 | blockIndex = i * 4; | 394 | blockIndex = i * 4; |
397 | mixedValues = this.mixColumnsWithValues([data[blockIndex + 0], | 395 | mixedValues = this.mixColumnsWithValues([data[blockIndex + 0], |
398 | data[blockIndex + 1], | 396 | data[blockIndex + 1], |
399 | data[blockIndex + 2], | 397 | data[blockIndex + 2], |
400 | data[blockIndex + 3]]); | 398 | data[blockIndex + 3]]); |
401 | data[blockIndex + 0] = mixedValues[0]; | 399 | data[blockIndex + 0] = mixedValues[0]; |
402 | data[blockIndex + 1] = mixedValues[1]; | 400 | data[blockIndex + 1] = mixedValues[1]; |
403 | data[blockIndex + 2] = mixedValues[2]; | 401 | data[blockIndex + 2] = mixedValues[2]; |
404 | data[blockIndex + 3] = mixedValues[3]; | 402 | data[blockIndex + 3] = mixedValues[3]; |
405 | } | 403 | } |
406 | }, | 404 | }, |
diff --git a/frontend/beta/js/Clipperz/Crypto/Base.js b/frontend/beta/js/Clipperz/Crypto/Base.js index d3a8e36..9acfc49 100644 --- a/frontend/beta/js/Clipperz/Crypto/Base.js +++ b/frontend/beta/js/Clipperz/Crypto/Base.js | |||
@@ -1,406 +1,404 @@ | |||
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.Base) == 'undefined') { throw ""; }} catch (e) { | 24 | try { if (typeof(Clipperz.Base) == 'undefined') { throw ""; }} catch (e) { |
27 | throw "Clipperz.Crypto.Base depends on Clipperz.Base!"; | 25 | throw "Clipperz.Crypto.Base depends on Clipperz.Base!"; |
28 | } | 26 | } |
29 | 27 | ||
30 | if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; } | 28 | if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; } |
31 | if (typeof(Clipperz.Crypto.Base) == 'undefined') { Clipperz.Crypto.Base = {}; } | 29 | if (typeof(Clipperz.Crypto.Base) == 'undefined') { Clipperz.Crypto.Base = {}; } |
32 | 30 | ||
33 | Clipperz.Crypto.Base.VERSION = "0.1"; | 31 | Clipperz.Crypto.Base.VERSION = "0.1"; |
34 | Clipperz.Crypto.Base.NAME = "Clipperz.Crypto.Base"; | 32 | Clipperz.Crypto.Base.NAME = "Clipperz.Crypto.Base"; |
35 | 33 | ||
36 | //############################################################################# | 34 | //############################################################################# |
37 | //Downloaded on March 30, 2006 from http://anmar.eu.org/projects/jssha2/files/jssha2-0.3.zip (jsSha2/sha256.js) | 35 | //Downloaded on March 30, 2006 from http://anmar.eu.org/projects/jssha2/files/jssha2-0.3.zip (jsSha2/sha256.js) |
38 | //############################################################################# | 36 | //############################################################################# |
39 | 37 | ||
40 | /* A JavaScript implementation of the Secure Hash Algorithm, SHA-256 | 38 | /* A JavaScript implementation of the Secure Hash Algorithm, SHA-256 |
41 | * Version 0.3 Copyright Angel Marin 2003-2004 - http://anmar.eu.org/ | 39 | * Version 0.3 Copyright Angel Marin 2003-2004 - http://anmar.eu.org/ |
42 | * Distributed under the BSD License | 40 | * Distributed under the BSD License |
43 | * Some bits taken from Paul Johnston's SHA-1 implementation | 41 | * Some bits taken from Paul Johnston's SHA-1 implementation |
44 | */ | 42 | */ |
45 | var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ | 43 | var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ |
46 | function safe_add (x, y) { | 44 | function safe_add (x, y) { |
47 | var lsw = (x & 0xFFFF) + (y & 0xFFFF); | 45 | var lsw = (x & 0xFFFF) + (y & 0xFFFF); |
48 | var msw = (x >> 16) + (y >> 16) + (lsw >> 16); | 46 | var msw = (x >> 16) + (y >> 16) + (lsw >> 16); |
49 | return (msw << 16) | (lsw & 0xFFFF); | 47 | return (msw << 16) | (lsw & 0xFFFF); |
50 | } | 48 | } |
51 | function S (X, n) {return ( X >>> n ) | (X << (32 - n));} | 49 | function S (X, n) {return ( X >>> n ) | (X << (32 - n));} |
52 | function R (X, n) {return ( X >>> n );} | 50 | function R (X, n) {return ( X >>> n );} |
53 | function Ch(x, y, z) {return ((x & y) ^ ((~x) & z));} | 51 | function Ch(x, y, z) {return ((x & y) ^ ((~x) & z));} |
54 | function Maj(x, y, z) {return ((x & y) ^ (x & z) ^ (y & z));} | 52 | function Maj(x, y, z) {return ((x & y) ^ (x & z) ^ (y & z));} |
55 | function Sigma0256(x) {return (S(x, 2) ^ S(x, 13) ^ S(x, 22));} | 53 | function Sigma0256(x) {return (S(x, 2) ^ S(x, 13) ^ S(x, 22));} |
56 | function Sigma1256(x) {return (S(x, 6) ^ S(x, 11) ^ S(x, 25));} | 54 | function Sigma1256(x) {return (S(x, 6) ^ S(x, 11) ^ S(x, 25));} |
57 | function Gamma0256(x) {return (S(x, 7) ^ S(x, 18) ^ R(x, 3));} | 55 | function Gamma0256(x) {return (S(x, 7) ^ S(x, 18) ^ R(x, 3));} |
58 | function Gamma1256(x) {return (S(x, 17) ^ S(x, 19) ^ R(x, 10));} | 56 | function Gamma1256(x) {return (S(x, 17) ^ S(x, 19) ^ R(x, 10));} |
59 | function core_sha256 (m, l) { | 57 | function core_sha256 (m, l) { |
60 | var K = new Array(0x428A2F98,0x71374491,0xB5C0FBCF,0xE9B5DBA5,0x3956C25B,0x59F111F1,0x923F82A4,0xAB1C5ED5,0xD807AA98,0x12835B01,0x243185BE,0x550C7DC3,0x72BE5D74,0x80DEB1FE,0x9BDC06A7,0xC19BF174,0xE49B69C1,0xEFBE4786,0xFC19DC6,0x240CA1CC,0x2DE92C6F,0x4A7484AA,0x5CB0A9DC,0x76F988DA,0x983E5152,0xA831C66D,0xB00327C8,0xBF597FC7,0xC6E00BF3,0xD5A79147,0x6CA6351,0x14292967,0x27B70A85,0x2E1B2138,0x4D2C6DFC,0x53380D13,0x650A7354,0x766A0ABB,0x81C2C92E,0x92722C85,0xA2BFE8A1,0xA81A664B,0xC24B8B70,0xC76C51A3,0xD192E819,0xD6990624,0xF40E3585,0x106AA070,0x19A4C116,0x1E376C08,0x2748774C,0x34B0BCB5,0x391C0CB3,0x4ED8AA4A,0x5B9CCA4F,0x682E6FF3,0x748F82EE,0x78A5636F,0x84C87814,0x8CC70208,0x90BEFFFA,0xA4506CEB,0xBEF9A3F7,0xC67178F2); | 58 | var K = new Array(0x428A2F98,0x71374491,0xB5C0FBCF,0xE9B5DBA5,0x3956C25B,0x59F111F1,0x923F82A4,0xAB1C5ED5,0xD807AA98,0x12835B01,0x243185BE,0x550C7DC3,0x72BE5D74,0x80DEB1FE,0x9BDC06A7,0xC19BF174,0xE49B69C1,0xEFBE4786,0xFC19DC6,0x240CA1CC,0x2DE92C6F,0x4A7484AA,0x5CB0A9DC,0x76F988DA,0x983E5152,0xA831C66D,0xB00327C8,0xBF597FC7,0xC6E00BF3,0xD5A79147,0x6CA6351,0x14292967,0x27B70A85,0x2E1B2138,0x4D2C6DFC,0x53380D13,0x650A7354,0x766A0ABB,0x81C2C92E,0x92722C85,0xA2BFE8A1,0xA81A664B,0xC24B8B70,0xC76C51A3,0xD192E819,0xD6990624,0xF40E3585,0x106AA070,0x19A4C116,0x1E376C08,0x2748774C,0x34B0BCB5,0x391C0CB3,0x4ED8AA4A,0x5B9CCA4F,0x682E6FF3,0x748F82EE,0x78A5636F,0x84C87814,0x8CC70208,0x90BEFFFA,0xA4506CEB,0xBEF9A3F7,0xC67178F2); |
61 | var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19); | 59 | var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19); |
62 | var W = new Array(64); | 60 | var W = new Array(64); |
63 | var a, b, c, d, e, f, g, h, i, j; | 61 | var a, b, c, d, e, f, g, h, i, j; |
64 | var T1, T2; | 62 | var T1, T2; |
65 | /* append padding */ | 63 | /* append padding */ |
66 | m[l >> 5] |= 0x80 << (24 - l % 32); | 64 | m[l >> 5] |= 0x80 << (24 - l % 32); |
67 | m[((l + 64 >> 9) << 4) + 15] = l; | 65 | m[((l + 64 >> 9) << 4) + 15] = l; |
68 | for ( var i = 0; i<m.length; i+=16 ) { | 66 | for ( var i = 0; i<m.length; i+=16 ) { |
69 | a = HASH[0]; b = HASH[1]; c = HASH[2]; d = HASH[3]; e = HASH[4]; f = HASH[5]; g = HASH[6]; h = HASH[7]; | 67 | a = HASH[0]; b = HASH[1]; c = HASH[2]; d = HASH[3]; e = HASH[4]; f = HASH[5]; g = HASH[6]; h = HASH[7]; |
70 | for ( var j = 0; j<64; j++) { | 68 | for ( var j = 0; j<64; j++) { |
71 | if (j < 16) W[j] = m[j + i]; | 69 | if (j < 16) W[j] = m[j + i]; |
72 | else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]); | 70 | else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]); |
73 | T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]); | 71 | T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]); |
74 | T2 = safe_add(Sigma0256(a), Maj(a, b, c)); | 72 | T2 = safe_add(Sigma0256(a), Maj(a, b, c)); |
75 | h = g; g = f; f = e; e = safe_add(d, T1); d = c; c = b; b = a; a = safe_add(T1, T2); | 73 | h = g; g = f; f = e; e = safe_add(d, T1); d = c; c = b; b = a; a = safe_add(T1, T2); |
76 | } | 74 | } |
77 | HASH[0] = safe_add(a, HASH[0]); HASH[1] = safe_add(b, HASH[1]); HASH[2] = safe_add(c, HASH[2]); HASH[3] = safe_add(d, HASH[3]); HASH[4] = safe_add(e, HASH[4]); HASH[5] = safe_add(f, HASH[5]); HASH[6] = safe_add(g, HASH[6]); HASH[7] = safe_add(h, HASH[7]); | 75 | HASH[0] = safe_add(a, HASH[0]); HASH[1] = safe_add(b, HASH[1]); HASH[2] = safe_add(c, HASH[2]); HASH[3] = safe_add(d, HASH[3]); HASH[4] = safe_add(e, HASH[4]); HASH[5] = safe_add(f, HASH[5]); HASH[6] = safe_add(g, HASH[6]); HASH[7] = safe_add(h, HASH[7]); |
78 | } | 76 | } |
79 | return HASH; | 77 | return HASH; |
80 | } | 78 | } |
81 | function str2binb (str) { | 79 | function str2binb (str) { |
82 | var bin = Array(); | 80 | var bin = Array(); |
83 | var mask = (1 << chrsz) - 1; | 81 | var mask = (1 << chrsz) - 1; |
84 | for(var i = 0; i < str.length * chrsz; i += chrsz) | 82 | for(var i = 0; i < str.length * chrsz; i += chrsz) |
85 | bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i%32); | 83 | bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i%32); |
86 | return bin; | 84 | return bin; |
87 | } | 85 | } |
88 | function binb2hex (binarray) { | 86 | function binb2hex (binarray) { |
89 | var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ | 87 | var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ |
90 | var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; | 88 | var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; |
91 | var str = ""; | 89 | var str = ""; |
92 | for (var i = 0; i < binarray.length * 4; i++) { | 90 | for (var i = 0; i < binarray.length * 4; i++) { |
93 | str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) + hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF); | 91 | str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) + hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF); |
94 | } | 92 | } |
95 | return str; | 93 | return str; |
96 | } | 94 | } |
97 | function hex_sha256(s){return binb2hex(core_sha256(str2binb(s),s.length * chrsz));} | 95 | function hex_sha256(s){return binb2hex(core_sha256(str2binb(s),s.length * chrsz));} |
98 | 96 | ||
99 | 97 | ||
100 | 98 | ||
101 | //############################################################################# | 99 | //############################################################################# |
102 | //Downloaded on March 30, 2006 from http://www.fourmilab.ch/javascrypt/javascrypt.zip (entropy.js) | 100 | //Downloaded on March 30, 2006 from http://www.fourmilab.ch/javascrypt/javascrypt.zip (entropy.js) |
103 | //############################################################################# | 101 | //############################################################################# |
104 | 102 | ||
105 | // Entropy collection utilities | 103 | // Entropy collection utilities |
106 | 104 | ||
107 | /*Start by declaring static storage and initialise | 105 | /*Start by declaring static storage and initialise |
108 | the entropy vector from the time we come through | 106 | the entropy vector from the time we come through |
109 | here. */ | 107 | here. */ |
110 | 108 | ||
111 | var entropyData = new Array(); // Collected entropy data | 109 | var entropyData = new Array(); // Collected entropy data |
112 | var edlen = 0; // Keyboard array data length | 110 | var edlen = 0; // Keyboard array data length |
113 | 111 | ||
114 | addEntropyTime(); // Start entropy collection with page load time | 112 | addEntropyTime(); // Start entropy collection with page load time |
115 | ce(); // Roll milliseconds into initial entropy | 113 | ce(); // Roll milliseconds into initial entropy |
116 | 114 | ||
117 | //Add a byte to the entropy vector | 115 | //Add a byte to the entropy vector |
118 | 116 | ||
119 | function addEntropyByte(b) { | 117 | function addEntropyByte(b) { |
120 | entropyData[edlen++] = b; | 118 | entropyData[edlen++] = b; |
121 | } | 119 | } |
122 | 120 | ||
123 | /*Capture entropy. When the user presses a key or performs | 121 | /*Capture entropy. When the user presses a key or performs |
124 | various other events for which we can request | 122 | various other events for which we can request |
125 | notification, add the time in 255ths of a second to the | 123 | notification, add the time in 255ths of a second to the |
126 | entropyData array. The name of the function is short | 124 | entropyData array. The name of the function is short |
127 | so it doesn't bloat the form object declarations in | 125 | so it doesn't bloat the form object declarations in |
128 | which it appears in various "onXXX" events. */ | 126 | which it appears in various "onXXX" events. */ |
129 | 127 | ||
130 | function ce() { | 128 | function ce() { |
131 | addEntropyByte(Math.floor((((new Date).getMilliseconds()) * 255) / 999)); | 129 | addEntropyByte(Math.floor((((new Date).getMilliseconds()) * 255) / 999)); |
132 | } | 130 | } |
133 | 131 | ||
134 | //Add a 32 bit quantity to the entropy vector | 132 | //Add a 32 bit quantity to the entropy vector |
135 | 133 | ||
136 | function addEntropy32(w) { | 134 | function addEntropy32(w) { |
137 | var i; | 135 | var i; |
138 | 136 | ||
139 | for (i = 0; i < 4; i++) { | 137 | for (i = 0; i < 4; i++) { |
140 | addEntropyByte(w & 0xFF); | 138 | addEntropyByte(w & 0xFF); |
141 | w >>= 8; | 139 | w >>= 8; |
142 | } | 140 | } |
143 | } | 141 | } |
144 | 142 | ||
145 | /*Add the current time and date (milliseconds since the epoch, | 143 | /*Add the current time and date (milliseconds since the epoch, |
146 | truncated to 32 bits) to the entropy vector. */ | 144 | truncated to 32 bits) to the entropy vector. */ |
147 | 145 | ||
148 | function addEntropyTime() { | 146 | function addEntropyTime() { |
149 | addEntropy32((new Date()).getTime()); | 147 | addEntropy32((new Date()).getTime()); |
150 | } | 148 | } |
151 | 149 | ||
152 | /* Start collection of entropy from mouse movements. The | 150 | /* Start collection of entropy from mouse movements. The |
153 | argument specifies the number of entropy items to be | 151 | argument specifies the number of entropy items to be |
154 | obtained from mouse motion, after which mouse motion | 152 | obtained from mouse motion, after which mouse motion |
155 | will be ignored. Note that you can re-enable mouse | 153 | will be ignored. Note that you can re-enable mouse |
156 | motion collection at any time if not already underway. */ | 154 | motion collection at any time if not already underway. */ |
157 | 155 | ||
158 | var mouseMotionCollect = 0; | 156 | var mouseMotionCollect = 0; |
159 | var oldMoveHandler; // For saving and restoring mouse move handler in IE4 | 157 | var oldMoveHandler; // For saving and restoring mouse move handler in IE4 |
160 | 158 | ||
161 | function mouseMotionEntropy(maxsamp) { | 159 | function mouseMotionEntropy(maxsamp) { |
162 | if (mouseMotionCollect <= 0) { | 160 | if (mouseMotionCollect <= 0) { |
163 | mouseMotionCollect = maxsamp; | 161 | mouseMotionCollect = maxsamp; |
164 | if ((document.implementation.hasFeature("Events", "2.0")) && | 162 | if ((document.implementation.hasFeature("Events", "2.0")) && |
165 | document.addEventListener) { | 163 | document.addEventListener) { |
166 | // Browser supports Document Object Model (DOM) 2 events | 164 | // Browser supports Document Object Model (DOM) 2 events |
167 | document.addEventListener("mousemove", mouseMoveEntropy, false); | 165 | document.addEventListener("mousemove", mouseMoveEntropy, false); |
168 | } else { | 166 | } else { |
169 | if (document.attachEvent) { | 167 | if (document.attachEvent) { |
170 | // Internet Explorer 5 and above event model | 168 | // Internet Explorer 5 and above event model |
171 | document.attachEvent("onmousemove", mouseMoveEntropy); | 169 | document.attachEvent("onmousemove", mouseMoveEntropy); |
172 | } else { | 170 | } else { |
173 | //Internet Explorer 4 event model | 171 | //Internet Explorer 4 event model |
174 | oldMoveHandler = document.onmousemove; | 172 | oldMoveHandler = document.onmousemove; |
175 | document.onmousemove = mouseMoveEntropy; | 173 | document.onmousemove = mouseMoveEntropy; |
176 | } | 174 | } |
177 | } | 175 | } |
178 | //dump("Mouse enable", mouseMotionCollect); | 176 | //dump("Mouse enable", mouseMotionCollect); |
179 | } | 177 | } |
180 | } | 178 | } |
181 | 179 | ||
182 | /*Collect entropy from mouse motion events. Note that | 180 | /*Collect entropy from mouse motion events. Note that |
183 | this is craftily coded to work with either DOM2 or Internet | 181 | this is craftily coded to work with either DOM2 or Internet |
184 | Explorer style events. Note that we don't use every successive | 182 | Explorer style events. Note that we don't use every successive |
185 | mouse movement event. Instead, we XOR the three bytes collected | 183 | mouse movement event. Instead, we XOR the three bytes collected |
186 | from the mouse and use that to determine how many subsequent | 184 | from the mouse and use that to determine how many subsequent |
187 | mouse movements we ignore before capturing the next one. */ | 185 | mouse movements we ignore before capturing the next one. */ |
188 | 186 | ||
189 | var mouseEntropyTime = 0; // Delay counter for mouse entropy collection | 187 | var mouseEntropyTime = 0; // Delay counter for mouse entropy collection |
190 | 188 | ||
191 | function mouseMoveEntropy(e) { | 189 | function mouseMoveEntropy(e) { |
192 | if (!e) { | 190 | if (!e) { |
193 | e = window.event; // Internet Explorer event model | 191 | e = window.event; // Internet Explorer event model |
194 | } | 192 | } |
195 | if (mouseMotionCollect > 0) { | 193 | if (mouseMotionCollect > 0) { |
196 | if (mouseEntropyTime-- <= 0) { | 194 | if (mouseEntropyTime-- <= 0) { |
197 | addEntropyByte(e.screenX & 0xFF); | 195 | addEntropyByte(e.screenX & 0xFF); |
198 | addEntropyByte(e.screenY & 0xFF); | 196 | addEntropyByte(e.screenY & 0xFF); |
199 | ce(); | 197 | ce(); |
200 | mouseMotionCollect--; | 198 | mouseMotionCollect--; |
201 | mouseEntropyTime = (entropyData[edlen - 3] ^ entropyData[edlen - 2] ^ | 199 | mouseEntropyTime = (entropyData[edlen - 3] ^ entropyData[edlen - 2] ^ |
202 | entropyData[edlen - 1]) % 19; | 200 | entropyData[edlen - 1]) % 19; |
203 | //dump("Mouse Move", byteArrayToHex(entropyData.slice(-3))); | 201 | //dump("Mouse Move", byteArrayToHex(entropyData.slice(-3))); |
204 | } | 202 | } |
205 | if (mouseMotionCollect <= 0) { | 203 | if (mouseMotionCollect <= 0) { |
206 | if (document.removeEventListener) { | 204 | if (document.removeEventListener) { |
207 | document.removeEventListener("mousemove", mouseMoveEntropy, false); | 205 | document.removeEventListener("mousemove", mouseMoveEntropy, false); |
208 | } else if (document.detachEvent) { | 206 | } else if (document.detachEvent) { |
209 | document.detachEvent("onmousemove", mouseMoveEntropy); | 207 | document.detachEvent("onmousemove", mouseMoveEntropy); |
210 | } else { | 208 | } else { |
211 | document.onmousemove = oldMoveHandler; | 209 | document.onmousemove = oldMoveHandler; |
212 | } | 210 | } |
213 | //dump("Spung!", 0); | 211 | //dump("Spung!", 0); |
214 | } | 212 | } |
215 | } | 213 | } |
216 | } | 214 | } |
217 | 215 | ||
218 | /*Compute a 32 byte key value from the entropy vector. | 216 | /*Compute a 32 byte key value from the entropy vector. |
219 | We compute the value by taking the MD5 sum of the even | 217 | We compute the value by taking the MD5 sum of the even |
220 | and odd bytes respectively of the entropy vector, then | 218 | and odd bytes respectively of the entropy vector, then |
221 | concatenating the two MD5 sums. */ | 219 | concatenating the two MD5 sums. */ |
222 | 220 | ||
223 | function keyFromEntropy() { | 221 | function keyFromEntropy() { |
224 | var i, k = new Array(32); | 222 | var i, k = new Array(32); |
225 | 223 | ||
226 | if (edlen == 0) { | 224 | if (edlen == 0) { |
227 | alert("Blooie! Entropy vector void at call to keyFromEntropy."); | 225 | alert("Blooie! Entropy vector void at call to keyFromEntropy."); |
228 | } | 226 | } |
229 | //dump("Entropy bytes", edlen); | 227 | //dump("Entropy bytes", edlen); |
230 | 228 | ||
231 | md5_init(); | 229 | md5_init(); |
232 | for (i = 0; i < edlen; i += 2) { | 230 | for (i = 0; i < edlen; i += 2) { |
233 | md5_update(entropyData[i]); | 231 | md5_update(entropyData[i]); |
234 | } | 232 | } |
235 | md5_finish(); | 233 | md5_finish(); |
236 | for (i = 0; i < 16; i++) { | 234 | for (i = 0; i < 16; i++) { |
237 | k[i] = digestBits[i]; | 235 | k[i] = digestBits[i]; |
238 | } | 236 | } |
239 | 237 | ||
240 | md5_init(); | 238 | md5_init(); |
241 | for (i = 1; i < edlen; i += 2) { | 239 | for (i = 1; i < edlen; i += 2) { |
242 | md5_update(entropyData[i]); | 240 | md5_update(entropyData[i]); |
243 | } | 241 | } |
244 | md5_finish(); | 242 | md5_finish(); |
245 | for (i = 0; i < 16; i++) { | 243 | for (i = 0; i < 16; i++) { |
246 | k[i + 16] = digestBits[i]; | 244 | k[i + 16] = digestBits[i]; |
247 | } | 245 | } |
248 | 246 | ||
249 | //dump("keyFromEntropy", byteArrayToHex(k)); | 247 | //dump("keyFromEntropy", byteArrayToHex(k)); |
250 | return k; | 248 | return k; |
251 | } | 249 | } |
252 | 250 | ||
253 | //############################################################################# | 251 | //############################################################################# |
254 | //Downloaded on March 30, 2006 from http://www.fourmilab.ch/javascrypt/javascrypt.zip (aesprng.js) | 252 | //Downloaded on March 30, 2006 from http://www.fourmilab.ch/javascrypt/javascrypt.zip (aesprng.js) |
255 | //############################################################################# | 253 | //############################################################################# |
256 | 254 | ||
257 | 255 | ||
258 | // AES based pseudorandom number generator | 256 | // AES based pseudorandom number generator |
259 | 257 | ||
260 | /* Constructor. Called with an array of 32 byte (0-255) values | 258 | /* Constructor. Called with an array of 32 byte (0-255) values |
261 | containing the initial seed. */ | 259 | containing the initial seed. */ |
262 | 260 | ||
263 | function AESprng(seed) { | 261 | function AESprng(seed) { |
264 | this.key = new Array(); | 262 | this.key = new Array(); |
265 | this.key = seed; | 263 | this.key = seed; |
266 | this.itext = hexToByteArray("9F489613248148F9C27945C6AE62EECA3E3367BB14064E4E6DC67A9F28AB3BD1"); | 264 | this.itext = hexToByteArray("9F489613248148F9C27945C6AE62EECA3E3367BB14064E4E6DC67A9F28AB3BD1"); |
267 | this.nbytes = 0; // Bytes left in buffer | 265 | this.nbytes = 0; // Bytes left in buffer |
268 | 266 | ||
269 | this.next = AESprng_next; | 267 | this.next = AESprng_next; |
270 | this.nextbits = AESprng_nextbits; | 268 | this.nextbits = AESprng_nextbits; |
271 | this.nextInt = AESprng_nextInt; | 269 | this.nextInt = AESprng_nextInt; |
272 | this.round = AESprng_round; | 270 | this.round = AESprng_round; |
273 | 271 | ||
274 | /* Encrypt the initial text with the seed key | 272 | /* Encrypt the initial text with the seed key |
275 | three times, feeding the output of the encryption | 273 | three times, feeding the output of the encryption |
276 | back into the key for the next round. */ | 274 | back into the key for the next round. */ |
277 | 275 | ||
278 | bsb = blockSizeInBits; | 276 | bsb = blockSizeInBits; |
279 | blockSizeInBits = 256; | 277 | blockSizeInBits = 256; |
280 | var i, ct; | 278 | var i, ct; |
281 | for (i = 0; i < 3; i++) { | 279 | for (i = 0; i < 3; i++) { |
282 | this.key = rijndaelEncrypt(this.itext, this.key, "ECB"); | 280 | this.key = rijndaelEncrypt(this.itext, this.key, "ECB"); |
283 | } | 281 | } |
284 | 282 | ||
285 | /* Now make between one and four additional | 283 | /* Now make between one and four additional |
286 | key-feedback rounds, with the number determined | 284 | key-feedback rounds, with the number determined |
287 | by bits from the result of the first three | 285 | by bits from the result of the first three |
288 | rounds. */ | 286 | rounds. */ |
289 | 287 | ||
290 | var n = 1 + (this.key[3] & 2) + (this.key[9] & 1); | 288 | var n = 1 + (this.key[3] & 2) + (this.key[9] & 1); |
291 | for (i = 0; i < n; i++) { | 289 | for (i = 0; i < n; i++) { |
292 | this.key = rijndaelEncrypt(this.itext, this.key, "ECB"); | 290 | this.key = rijndaelEncrypt(this.itext, this.key, "ECB"); |
293 | } | 291 | } |
294 | blockSizeInBits = bsb; | 292 | blockSizeInBits = bsb; |
295 | } | 293 | } |
296 | 294 | ||
297 | function AESprng_round() { | 295 | function AESprng_round() { |
298 | bsb = blockSizeInBits; | 296 | bsb = blockSizeInBits; |
299 | blockSizeInBits = 256; | 297 | blockSizeInBits = 256; |
300 | this.key = rijndaelEncrypt(this.itext, this.key, "ECB"); | 298 | this.key = rijndaelEncrypt(this.itext, this.key, "ECB"); |
301 | this.nbytes = 32; | 299 | this.nbytes = 32; |
302 | blockSizeInBits = bsb; | 300 | blockSizeInBits = bsb; |
303 | } | 301 | } |
304 | 302 | ||
305 | //Return next byte from the generator | 303 | //Return next byte from the generator |
306 | 304 | ||
307 | function AESprng_next() { | 305 | function AESprng_next() { |
308 | if (this.nbytes <= 0) { | 306 | if (this.nbytes <= 0) { |
309 | this.round(); | 307 | this.round(); |
310 | } | 308 | } |
311 | return(this.key[--this.nbytes]); | 309 | return(this.key[--this.nbytes]); |
312 | } | 310 | } |
313 | 311 | ||
314 | //Return n bit integer value (up to maximum integer size) | 312 | //Return n bit integer value (up to maximum integer size) |
315 | 313 | ||
316 | function AESprng_nextbits(n) { | 314 | function AESprng_nextbits(n) { |
317 | var i, w = 0, nbytes = Math.floor((n + 7) / 8); | 315 | var i, w = 0, nbytes = Math.floor((n + 7) / 8); |
318 | 316 | ||
319 | for (i = 0; i < nbytes; i++) { | 317 | for (i = 0; i < nbytes; i++) { |
320 | w = (w << 8) | this.next(); | 318 | w = (w << 8) | this.next(); |
321 | } | 319 | } |
322 | return w & ((1 << n) - 1); | 320 | return w & ((1 << n) - 1); |
323 | } | 321 | } |
324 | 322 | ||
325 | // Return integer between 0 and n inclusive | 323 | // Return integer between 0 and n inclusive |
326 | 324 | ||
327 | function AESprng_nextInt(n) { | 325 | function AESprng_nextInt(n) { |
328 | var p = 1, nb = 0; | 326 | var p = 1, nb = 0; |
329 | 327 | ||
330 | // Determine smallest p, 2^p > n | 328 | // Determine smallest p, 2^p > n |
331 | // nb = log_2 p | 329 | // nb = log_2 p |
332 | 330 | ||
333 | while (n >= p) { | 331 | while (n >= p) { |
334 | p <<= 1; | 332 | p <<= 1; |
335 | nb++; | 333 | nb++; |
336 | } | 334 | } |
337 | p--; | 335 | p--; |
338 | 336 | ||
339 | /* Generate values from 0 through n by first generating | 337 | /* Generate values from 0 through n by first generating |
340 | values v from 0 to (2^p)-1, then discarding any results v > n. | 338 | values v from 0 to (2^p)-1, then discarding any results v > n. |
341 | For the rationale behind this (and why taking | 339 | For the rationale behind this (and why taking |
342 | values mod (n + 1) is biased toward smaller values, see | 340 | values mod (n + 1) is biased toward smaller values, see |
343 | Ferguson and Schneier, "Practical Cryptography", | 341 | Ferguson and Schneier, "Practical Cryptography", |
344 | ISBN 0-471-22357-3, section 10.8). */ | 342 | ISBN 0-471-22357-3, section 10.8). */ |
345 | 343 | ||
346 | while (true) { | 344 | while (true) { |
347 | var v = this.nextbits(nb) & p; | 345 | var v = this.nextbits(nb) & p; |
348 | 346 | ||
349 | if (v <= n) { | 347 | if (v <= n) { |
350 | return v; | 348 | return v; |
351 | } | 349 | } |
352 | } | 350 | } |
353 | } | 351 | } |
354 | 352 | ||
355 | //############################################################################# | 353 | //############################################################################# |
356 | //Downloaded on March 30, 2006 from http://www.fourmilab.ch/javascrypt/javascrypt.zip (md5.js) | 354 | //Downloaded on March 30, 2006 from http://www.fourmilab.ch/javascrypt/javascrypt.zip (md5.js) |
357 | //############################################################################# | 355 | //############################################################################# |
358 | 356 | ||
359 | /* | 357 | /* |
360 | * md5.jvs 1.0b 27/06/96 | 358 | * md5.jvs 1.0b 27/06/96 |
361 | * | 359 | * |
362 | * Javascript implementation of the RSA Data Security, Inc. MD5 | 360 | * Javascript implementation of the RSA Data Security, Inc. MD5 |
363 | * Message-Digest Algorithm. | 361 | * Message-Digest Algorithm. |
364 | * | 362 | * |
365 | * Copyright (c) 1996 Henri Torgemane. All Rights Reserved. | 363 | * Copyright (c) 1996 Henri Torgemane. All Rights Reserved. |
366 | * | 364 | * |
367 | * Permission to use, copy, modify, and distribute this software | 365 | * Permission to use, copy, modify, and distribute this software |
368 | * and its documentation for any purposes and without | 366 | * and its documentation for any purposes and without |
369 | * fee is hereby granted provided that this copyright notice | 367 | * fee is hereby granted provided that this copyright notice |
370 | * appears in all copies. | 368 | * appears in all copies. |
371 | * | 369 | * |
372 | * Of course, this soft is provided "as is" without express or implied | 370 | * Of course, this soft is provided "as is" without express or implied |
373 | * warranty of any kind. | 371 | * warranty of any kind. |
374 | 372 | ||
375 | This version contains some trivial reformatting modifications | 373 | This version contains some trivial reformatting modifications |
376 | by John Walker. | 374 | by John Walker. |
377 | 375 | ||
378 | */ | 376 | */ |
379 | 377 | ||
380 | function array(n) { | 378 | function array(n) { |
381 | for (i = 0; i < n; i++) { | 379 | for (i = 0; i < n; i++) { |
382 | this[i] = 0; | 380 | this[i] = 0; |
383 | } | 381 | } |
384 | this.length = n; | 382 | this.length = n; |
385 | } | 383 | } |
386 | 384 | ||
387 | /* Some basic logical functions had to be rewritten because of a bug in | 385 | /* Some basic logical functions had to be rewritten because of a bug in |
388 | * Javascript.. Just try to compute 0xffffffff >> 4 with it.. | 386 | * Javascript.. Just try to compute 0xffffffff >> 4 with it.. |
389 | * Of course, these functions are slower than the original would be, but | 387 | * Of course, these functions are slower than the original would be, but |
390 | * at least, they work! | 388 | * at least, they work! |
391 | */ | 389 | */ |
392 | 390 | ||
393 | function integer(n) { | 391 | function integer(n) { |
394 | return n % (0xffffffff + 1); | 392 | return n % (0xffffffff + 1); |
395 | } | 393 | } |
396 | 394 | ||
397 | function shr(a, b) { | 395 | function shr(a, b) { |
398 | a = integer(a); | 396 | a = integer(a); |
399 | b = integer(b); | 397 | b = integer(b); |
400 | if (a - 0x80000000 >= 0) { | 398 | if (a - 0x80000000 >= 0) { |
401 | a = a % 0x80000000; | 399 | a = a % 0x80000000; |
402 | a >>= b; | 400 | a >>= b; |
403 | a += 0x40000000 >> (b - 1); | 401 | a += 0x40000000 >> (b - 1); |
404 | } else { | 402 | } else { |
405 | a >>= b; | 403 | a >>= b; |
406 | } | 404 | } |
diff --git a/frontend/beta/js/Clipperz/Crypto/BigInt.js b/frontend/beta/js/Clipperz/Crypto/BigInt.js index 41483a3..197cd9a 100644 --- a/frontend/beta/js/Clipperz/Crypto/BigInt.js +++ b/frontend/beta/js/Clipperz/Crypto/BigInt.js | |||
@@ -1,406 +1,404 @@ | |||
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 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | 24 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } |
27 | if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; } | 25 | if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; } |
28 | 26 | ||
29 | //############################################################################# | 27 | //############################################################################# |
30 | //Downloaded on March 05, 2007 from http://www.leemon.com/crypto/BigInt.js | 28 | //Downloaded on March 05, 2007 from http://www.leemon.com/crypto/BigInt.js |
31 | //############################################################################# | 29 | //############################################################################# |
32 | 30 | ||
33 | 31 | ||
34 | //////////////////////////////////////////////////////////////////////////////////////// | 32 | //////////////////////////////////////////////////////////////////////////////////////// |
35 | // Big Integer Library v. 5.0 | 33 | // Big Integer Library v. 5.0 |
36 | // Created 2000, last modified 2006 | 34 | // Created 2000, last modified 2006 |
37 | // Leemon Baird | 35 | // Leemon Baird |
38 | // www.leemon.com | 36 | // www.leemon.com |
39 | // | 37 | // |
40 | // This file is public domain. You can use it for any purpose without restriction. | 38 | // This file is public domain. You can use it for any purpose without restriction. |
41 | // I do not guarantee that it is correct, so use it at your own risk. If you use | 39 | // I do not guarantee that it is correct, so use it at your own risk. If you use |
42 | // it for something interesting, I'd appreciate hearing about it. If you find | 40 | // it for something interesting, I'd appreciate hearing about it. If you find |
43 | // any bugs or make any improvements, I'd appreciate hearing about those too. | 41 | // any bugs or make any improvements, I'd appreciate hearing about those too. |
44 | // It would also be nice if my name and address were left in the comments. | 42 | // It would also be nice if my name and address were left in the comments. |
45 | // But none of that is required. | 43 | // But none of that is required. |
46 | // | 44 | // |
47 | // This code defines a bigInt library for arbitrary-precision integers. | 45 | // This code defines a bigInt library for arbitrary-precision integers. |
48 | // A bigInt is an array of integers storing the value in chunks of bpe bits, | 46 | // A bigInt is an array of integers storing the value in chunks of bpe bits, |
49 | // little endian (buff[0] is the least significant word). | 47 | // little endian (buff[0] is the least significant word). |
50 | // Negative bigInts are stored two's complement. | 48 | // Negative bigInts are stored two's complement. |
51 | // Some functions assume their parameters have at least one leading zero element. | 49 | // Some functions assume their parameters have at least one leading zero element. |
52 | // Functions with an underscore at the end of the name have unpredictable behavior in case of overflow, | 50 | // Functions with an underscore at the end of the name have unpredictable behavior in case of overflow, |
53 | // so the caller must make sure overflow won't happen. | 51 | // so the caller must make sure overflow won't happen. |
54 | // For each function where a parameter is modified, that same | 52 | // For each function where a parameter is modified, that same |
55 | // variable must not be used as another argument too. | 53 | // variable must not be used as another argument too. |
56 | // So, you cannot square x by doing multMod_(x,x,n). | 54 | // So, you cannot square x by doing multMod_(x,x,n). |
57 | // You must use squareMod_(x,n) instead, or do y=dup(x); multMod_(x,y,n). | 55 | // You must use squareMod_(x,n) instead, or do y=dup(x); multMod_(x,y,n). |
58 | // | 56 | // |
59 | // These functions are designed to avoid frequent dynamic memory allocation in the inner loop. | 57 | // These functions are designed to avoid frequent dynamic memory allocation in the inner loop. |
60 | // For most functions, if it needs a BigInt as a local variable it will actually use | 58 | // For most functions, if it needs a BigInt as a local variable it will actually use |
61 | // a global, and will only allocate to it when it's not the right size. This ensures | 59 | // a global, and will only allocate to it when it's not the right size. This ensures |
62 | // that when a function is called repeatedly with same-sized parameters, it only allocates | 60 | // that when a function is called repeatedly with same-sized parameters, it only allocates |
63 | // memory on the first call. | 61 | // memory on the first call. |
64 | // | 62 | // |
65 | // Note that for cryptographic purposes, the calls to Math.random() must | 63 | // Note that for cryptographic purposes, the calls to Math.random() must |
66 | // be replaced with calls to a better pseudorandom number generator. | 64 | // be replaced with calls to a better pseudorandom number generator. |
67 | // | 65 | // |
68 | // In the following, "bigInt" means a bigInt with at least one leading zero element, | 66 | // In the following, "bigInt" means a bigInt with at least one leading zero element, |
69 | // and "integer" means a nonnegative integer less than radix. In some cases, integer | 67 | // and "integer" means a nonnegative integer less than radix. In some cases, integer |
70 | // can be negative. Negative bigInts are 2s complement. | 68 | // can be negative. Negative bigInts are 2s complement. |
71 | // | 69 | // |
72 | // The following functions do not modify their inputs, but dynamically allocate memory every time they are called: | 70 | // The following functions do not modify their inputs, but dynamically allocate memory every time they are called: |
73 | // | 71 | // |
74 | // function bigInt2str(x,base) //convert a bigInt into a string in a given base, from base 2 up to base 95 | 72 | // function bigInt2str(x,base) //convert a bigInt into a string in a given base, from base 2 up to base 95 |
75 | // function dup(x) //returns a copy of bigInt x | 73 | // function dup(x) //returns a copy of bigInt x |
76 | // function findPrimes(n) //return array of all primes less than integer n | 74 | // function findPrimes(n) //return array of all primes less than integer n |
77 | // function int2bigInt(t,n,m) //convert integer t to a bigInt with at least n bits and m array elements | 75 | // function int2bigInt(t,n,m) //convert integer t to a bigInt with at least n bits and m array elements |
78 | // function int2bigInt(s,b,n,m) //convert string s in base b to a bigInt with at least n bits and m array elements | 76 | // function int2bigInt(s,b,n,m) //convert string s in base b to a bigInt with at least n bits and m array elements |
79 | // function trim(x,k) //return a copy of x with exactly k leading zero elements | 77 | // function trim(x,k) //return a copy of x with exactly k leading zero elements |
80 | // | 78 | // |
81 | // The following functions do not modify their inputs, so there is never a problem with the result being too big: | 79 | // The following functions do not modify their inputs, so there is never a problem with the result being too big: |
82 | // | 80 | // |
83 | // function bitSize(x) //returns how many bits long the bigInt x is, not counting leading zeros | 81 | // function bitSize(x) //returns how many bits long the bigInt x is, not counting leading zeros |
84 | // function equals(x,y) //is the bigInt x equal to the bigint y? | 82 | // function equals(x,y) //is the bigInt x equal to the bigint y? |
85 | // function equalsInt(x,y) //is bigint x equal to integer y? | 83 | // function equalsInt(x,y) //is bigint x equal to integer y? |
86 | // function greater(x,y) //is x>y? (x and y are nonnegative bigInts) | 84 | // function greater(x,y) //is x>y? (x and y are nonnegative bigInts) |
87 | // function greaterShift(x,y,shift)//is (x <<(shift*bpe)) > y? | 85 | // function greaterShift(x,y,shift)//is (x <<(shift*bpe)) > y? |
88 | // function isZero(x) //is the bigInt x equal to zero? | 86 | // function isZero(x) //is the bigInt x equal to zero? |
89 | // function millerRabin(x,b) //does one round of Miller-Rabin base integer b say that bigInt x is possibly prime (as opposed to definitely composite)? | 87 | // function millerRabin(x,b) //does one round of Miller-Rabin base integer b say that bigInt x is possibly prime (as opposed to definitely composite)? |
90 | // function modInt(x,n) //return x mod n for bigInt x and integer n. | 88 | // function modInt(x,n) //return x mod n for bigInt x and integer n. |
91 | // function negative(x) //is bigInt x negative? | 89 | // function negative(x) //is bigInt x negative? |
92 | // | 90 | // |
93 | // The following functions do not modify their inputs, but allocate memory and call functions with underscores | 91 | // The following functions do not modify their inputs, but allocate memory and call functions with underscores |
94 | // | 92 | // |
95 | // function add(x,y) //return (x+y) for bigInts x and y. | 93 | // function add(x,y) //return (x+y) for bigInts x and y. |
96 | // function addInt(x,n) //return (x+n) where x is a bigInt and n is an integer. | 94 | // function addInt(x,n) //return (x+n) where x is a bigInt and n is an integer. |
97 | // function expand(x,n) //return a copy of x with at least n elements, adding leading zeros if needed | 95 | // function expand(x,n) //return a copy of x with at least n elements, adding leading zeros if needed |
98 | // function inverseMod(x,n) //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null | 96 | // function inverseMod(x,n) //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null |
99 | // function mod(x,n) //return a new bigInt equal to (x mod n) for bigInts x and n. | 97 | // function mod(x,n) //return a new bigInt equal to (x mod n) for bigInts x and n. |
100 | // function mult(x,y) //return x*y for bigInts x and y. This is faster when y<x. | 98 | // function mult(x,y) //return x*y for bigInts x and y. This is faster when y<x. |
101 | // function multMod(x,y,n) //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. | 99 | // function multMod(x,y,n) //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. |
102 | // function powMod(x,y,n) //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n. | 100 | // function powMod(x,y,n) //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n. |
103 | // function randTruePrime(k) //return a new, random, k-bit, true prime using Maurer's algorithm. | 101 | // function randTruePrime(k) //return a new, random, k-bit, true prime using Maurer's algorithm. |
104 | // function sub(x,y) //return (x-y) for bigInts x and y. Negative answers will be 2s complement | 102 | // function sub(x,y) //return (x-y) for bigInts x and y. Negative answers will be 2s complement |
105 | // | 103 | // |
106 | // The following functions write a bigInt result to one of the parameters, but | 104 | // The following functions write a bigInt result to one of the parameters, but |
107 | // the result is never bigger than the original, so there can't be overflow problems: | 105 | // the result is never bigger than the original, so there can't be overflow problems: |
108 | // | 106 | // |
109 | // function divInt_(x,n) //do x=floor(x/n) for bigInt x and integer n, and return the remainder | 107 | // function divInt_(x,n) //do x=floor(x/n) for bigInt x and integer n, and return the remainder |
110 | // function GCD_(x,y) //set x to the greatest common divisor of bigInts x and y, (y is destroyed). | 108 | // function GCD_(x,y) //set x to the greatest common divisor of bigInts x and y, (y is destroyed). |
111 | // function halve_(x) //do x=floor(|x|/2)*sgn(x) for bigInt x in 2's complement | 109 | // function halve_(x) //do x=floor(|x|/2)*sgn(x) for bigInt x in 2's complement |
112 | // function mod_(x,n) //do x=x mod n for bigInts x and n. | 110 | // function mod_(x,n) //do x=x mod n for bigInts x and n. |
113 | // function rightShift_(x,n) //right shift bigInt x by n bits. 0 <= n < bpe. | 111 | // function rightShift_(x,n) //right shift bigInt x by n bits. 0 <= n < bpe. |
114 | // | 112 | // |
115 | // The following functions write a bigInt result to one of the parameters. The caller is responsible for | 113 | // The following functions write a bigInt result to one of the parameters. The caller is responsible for |
116 | // ensuring it is large enough to hold the result. | 114 | // ensuring it is large enough to hold the result. |
117 | // | 115 | // |
118 | // function addInt_(x,n) //do x=x+n where x is a bigInt and n is an integer | 116 | // function addInt_(x,n) //do x=x+n where x is a bigInt and n is an integer |
119 | // function add_(x,y) //do x=x+y for bigInts x and y | 117 | // function add_(x,y) //do x=x+y for bigInts x and y |
120 | // function addShift_(x,y,ys) //do x=x+(y<<(ys*bpe)) | 118 | // function addShift_(x,y,ys) //do x=x+(y<<(ys*bpe)) |
121 | // function copy_(x,y) //do x=y on bigInts x and y | 119 | // function copy_(x,y) //do x=y on bigInts x and y |
122 | // function copyInt_(x,n) //do x=n on bigInt x and integer n | 120 | // function copyInt_(x,n) //do x=n on bigInt x and integer n |
123 | // function carry_(x) //do carries and borrows so each element of the bigInt x fits in bpe bits. | 121 | // function carry_(x) //do carries and borrows so each element of the bigInt x fits in bpe bits. |
124 | // function divide_(x,y,q,r) //divide_ x by y giving quotient q and remainder r | 122 | // function divide_(x,y,q,r) //divide_ x by y giving quotient q and remainder r |
125 | // function eGCD_(x,y,d,a,b) //sets a,b,d to positive big integers such that d = GCD_(x,y) = a*x-b*y | 123 | // function eGCD_(x,y,d,a,b) //sets a,b,d to positive big integers such that d = GCD_(x,y) = a*x-b*y |
126 | // function inverseMod_(x,n) //do x=x**(-1) mod n, for bigInts x and n. Returns 1 (0) if inverse does (doesn't) exist | 124 | // function inverseMod_(x,n) //do x=x**(-1) mod n, for bigInts x and n. Returns 1 (0) if inverse does (doesn't) exist |
127 | // function inverseModInt_(x,n) //return x**(-1) mod n, for integers x and n. Return 0 if there is no inverse | 125 | // function inverseModInt_(x,n) //return x**(-1) mod n, for integers x and n. Return 0 if there is no inverse |
128 | // function leftShift_(x,n) //left shift bigInt x by n bits. n<bpe. | 126 | // function leftShift_(x,n) //left shift bigInt x by n bits. n<bpe. |
129 | // function linComb_(x,y,a,b) //do x=a*x+b*y for bigInts x and y and integers a and b | 127 | // function linComb_(x,y,a,b) //do x=a*x+b*y for bigInts x and y and integers a and b |
130 | // function linCombShift_(x,y,b,ys) //do x=x+b*(y<<(ys*bpe)) for bigInts x and y, and integers b and ys | 128 | // function linCombShift_(x,y,b,ys) //do x=x+b*(y<<(ys*bpe)) for bigInts x and y, and integers b and ys |
131 | // function mont_(x,y,n,np) //Montgomery multiplication (see comments where the function is defined) | 129 | // function mont_(x,y,n,np) //Montgomery multiplication (see comments where the function is defined) |
132 | // function mult_(x,y) //do x=x*y for bigInts x and y. | 130 | // function mult_(x,y) //do x=x*y for bigInts x and y. |
133 | // function multInt_(x,n) //do x=x*n where x is a bigInt and n is an integer. | 131 | // function multInt_(x,n) //do x=x*n where x is a bigInt and n is an integer. |
134 | // function multMod_(x,y,n) //do x=x*y mod n for bigInts x,y,n. | 132 | // function multMod_(x,y,n) //do x=x*y mod n for bigInts x,y,n. |
135 | // function powMod_(x,y,n) //do x=x**y mod n, where x,y,n are bigInts (n is odd) and ** is exponentiation. 0**0=1. | 133 | // function powMod_(x,y,n) //do x=x**y mod n, where x,y,n are bigInts (n is odd) and ** is exponentiation. 0**0=1. |
136 | // function randBigInt_(b,n,s) //do b = an n-bit random BigInt. if s=1, then nth bit (most significant bit) is set to 1. n>=1. | 134 | // function randBigInt_(b,n,s) //do b = an n-bit random BigInt. if s=1, then nth bit (most significant bit) is set to 1. n>=1. |
137 | // function randTruePrime_(ans,k) //do ans = a random k-bit true random prime (not just probable prime) with 1 in the msb. | 135 | // function randTruePrime_(ans,k) //do ans = a random k-bit true random prime (not just probable prime) with 1 in the msb. |
138 | // function squareMod_(x,n) //do x=x*x mod n for bigInts x,n | 136 | // function squareMod_(x,n) //do x=x*x mod n for bigInts x,n |
139 | // function sub_(x,y) //do x=x-y for bigInts x and y. Negative answers will be 2s complement. | 137 | // function sub_(x,y) //do x=x-y for bigInts x and y. Negative answers will be 2s complement. |
140 | // function subShift_(x,y,ys) //do x=x-(y<<(ys*bpe)). Negative answers will be 2s complement. | 138 | // function subShift_(x,y,ys) //do x=x-(y<<(ys*bpe)). Negative answers will be 2s complement. |
141 | // | 139 | // |
142 | // The following functions are based on algorithms from the _Handbook of Applied Cryptography_ | 140 | // The following functions are based on algorithms from the _Handbook of Applied Cryptography_ |
143 | // powMod_() = algorithm 14.94, Montgomery exponentiation | 141 | // powMod_() = algorithm 14.94, Montgomery exponentiation |
144 | // eGCD_,inverseMod_() = algorithm 14.61, Binary extended GCD_ | 142 | // eGCD_,inverseMod_() = algorithm 14.61, Binary extended GCD_ |
145 | // GCD_() = algorothm 14.57, Lehmer's algorithm | 143 | // GCD_() = algorothm 14.57, Lehmer's algorithm |
146 | // mont_() = algorithm 14.36, Montgomery multiplication | 144 | // mont_() = algorithm 14.36, Montgomery multiplication |
147 | // divide_() = algorithm 14.20 Multiple-precision division | 145 | // divide_() = algorithm 14.20 Multiple-precision division |
148 | // squareMod_() = algorithm 14.16 Multiple-precision squaring | 146 | // squareMod_() = algorithm 14.16 Multiple-precision squaring |
149 | // randTruePrime_() = algorithm 4.62, Maurer's algorithm | 147 | // randTruePrime_() = algorithm 4.62, Maurer's algorithm |
150 | // millerRabin() = algorithm 4.24, Miller-Rabin algorithm | 148 | // millerRabin() = algorithm 4.24, Miller-Rabin algorithm |
151 | // | 149 | // |
152 | // Profiling shows: | 150 | // Profiling shows: |
153 | // randTruePrime_() spends: | 151 | // randTruePrime_() spends: |
154 | // 10% of its time in calls to powMod_() | 152 | // 10% of its time in calls to powMod_() |
155 | // 85% of its time in calls to millerRabin() | 153 | // 85% of its time in calls to millerRabin() |
156 | // millerRabin() spends: | 154 | // millerRabin() spends: |
157 | // 99% of its time in calls to powMod_() (always with a base of 2) | 155 | // 99% of its time in calls to powMod_() (always with a base of 2) |
158 | // powMod_() spends: | 156 | // powMod_() spends: |
159 | // 94% of its time in calls to mont_() (almost always with x==y) | 157 | // 94% of its time in calls to mont_() (almost always with x==y) |
160 | // | 158 | // |
161 | // This suggests there are several ways to speed up this library slightly: | 159 | // This suggests there are several ways to speed up this library slightly: |
162 | // - convert powMod_ to use a Montgomery form of k-ary window (or maybe a Montgomery form of sliding window) | 160 | // - convert powMod_ to use a Montgomery form of k-ary window (or maybe a Montgomery form of sliding window) |
163 | // -- this should especially focus on being fast when raising 2 to a power mod n | 161 | // -- this should especially focus on being fast when raising 2 to a power mod n |
164 | // - convert randTruePrime_() to use a minimum r of 1/3 instead of 1/2 with the appropriate change to the test | 162 | // - convert randTruePrime_() to use a minimum r of 1/3 instead of 1/2 with the appropriate change to the test |
165 | // - tune the parameters in randTruePrime_(), including c, m, and recLimit | 163 | // - tune the parameters in randTruePrime_(), including c, m, and recLimit |
166 | // - speed up the single loop in mont_() that takes 95% of the runtime, perhaps by reducing checking | 164 | // - speed up the single loop in mont_() that takes 95% of the runtime, perhaps by reducing checking |
167 | // within the loop when all the parameters are the same length. | 165 | // within the loop when all the parameters are the same length. |
168 | // | 166 | // |
169 | // There are several ideas that look like they wouldn't help much at all: | 167 | // There are several ideas that look like they wouldn't help much at all: |
170 | // - replacing trial division in randTruePrime_() with a sieve (that speeds up something taking almost no time anyway) | 168 | // - replacing trial division in randTruePrime_() with a sieve (that speeds up something taking almost no time anyway) |
171 | // - increase bpe from 15 to 30 (that would help if we had a 32*32->64 multiplier, but not with JavaScript's 32*32->32) | 169 | // - increase bpe from 15 to 30 (that would help if we had a 32*32->64 multiplier, but not with JavaScript's 32*32->32) |
172 | // - speeding up mont_(x,y,n,np) when x==y by doing a non-modular, non-Montgomery square | 170 | // - speeding up mont_(x,y,n,np) when x==y by doing a non-modular, non-Montgomery square |
173 | // followed by a Montgomery reduction. The intermediate answer will be twice as long as x, so that | 171 | // followed by a Montgomery reduction. The intermediate answer will be twice as long as x, so that |
174 | // method would be slower. This is unfortunate because the code currently spends almost all of its time | 172 | // method would be slower. This is unfortunate because the code currently spends almost all of its time |
175 | // doing mont_(x,x,...), both for randTruePrime_() and powMod_(). A faster method for Montgomery squaring | 173 | // doing mont_(x,x,...), both for randTruePrime_() and powMod_(). A faster method for Montgomery squaring |
176 | // would have a large impact on the speed of randTruePrime_() and powMod_(). HAC has a couple of poorly-worded | 174 | // would have a large impact on the speed of randTruePrime_() and powMod_(). HAC has a couple of poorly-worded |
177 | // sentences that seem to imply it's faster to do a non-modular square followed by a single | 175 | // sentences that seem to imply it's faster to do a non-modular square followed by a single |
178 | // Montgomery reduction, but that's obviously wrong. | 176 | // Montgomery reduction, but that's obviously wrong. |
179 | //////////////////////////////////////////////////////////////////////////////////////// | 177 | //////////////////////////////////////////////////////////////////////////////////////// |
180 | 178 | ||
181 | //globals | 179 | //globals |
182 | bpe=0; //bits stored per array element | 180 | bpe=0; //bits stored per array element |
183 | mask=0; //AND this with an array element to chop it down to bpe bits | 181 | mask=0; //AND this with an array element to chop it down to bpe bits |
184 | radix=mask+1; //equals 2^bpe. A single 1 bit to the left of the last bit of mask. | 182 | radix=mask+1; //equals 2^bpe. A single 1 bit to the left of the last bit of mask. |
185 | 183 | ||
186 | //the digits for converting to different bases | 184 | //the digits for converting to different bases |
187 | digitsStr='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=!@#$%^&*()[]{}|;:,.<>/?`~ \\\'\"+-'; | 185 | digitsStr='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=!@#$%^&*()[]{}|;:,.<>/?`~ \\\'\"+-'; |
188 | 186 | ||
189 | //initialize the global variables | 187 | //initialize the global variables |
190 | for (bpe=0; (1<<(bpe+1)) > (1<<bpe); bpe++); //bpe=number of bits in the mantissa on this platform | 188 | for (bpe=0; (1<<(bpe+1)) > (1<<bpe); bpe++); //bpe=number of bits in the mantissa on this platform |
191 | bpe>>=1; //bpe=number of bits in one element of the array representing the bigInt | 189 | bpe>>=1; //bpe=number of bits in one element of the array representing the bigInt |
192 | mask=(1<<bpe)-1; //AND the mask with an integer to get its bpe least significant bits | 190 | mask=(1<<bpe)-1; //AND the mask with an integer to get its bpe least significant bits |
193 | radix=mask+1; //2^bpe. a single 1 bit to the left of the first bit of mask | 191 | radix=mask+1; //2^bpe. a single 1 bit to the left of the first bit of mask |
194 | one=int2bigInt(1,1,1); //constant used in powMod_() | 192 | one=int2bigInt(1,1,1); //constant used in powMod_() |
195 | 193 | ||
196 | //the following global variables are scratchpad memory to | 194 | //the following global variables are scratchpad memory to |
197 | //reduce dynamic memory allocation in the inner loop | 195 | //reduce dynamic memory allocation in the inner loop |
198 | t=new Array(0); | 196 | t=new Array(0); |
199 | ss=t; //used in mult_() | 197 | ss=t; //used in mult_() |
200 | s0=t; //used in multMod_(), squareMod_() | 198 | s0=t; //used in multMod_(), squareMod_() |
201 | s1=t; //used in powMod_(), multMod_(), squareMod_() | 199 | s1=t; //used in powMod_(), multMod_(), squareMod_() |
202 | s2=t; //used in powMod_(), multMod_() | 200 | s2=t; //used in powMod_(), multMod_() |
203 | s3=t; //used in powMod_() | 201 | s3=t; //used in powMod_() |
204 | s4=t; s5=t; //used in mod_() | 202 | s4=t; s5=t; //used in mod_() |
205 | s6=t; //used in bigInt2str() | 203 | s6=t; //used in bigInt2str() |
206 | s7=t; //used in powMod_() | 204 | s7=t; //used in powMod_() |
207 | T=t; //used in GCD_() | 205 | T=t; //used in GCD_() |
208 | sa=t; //used in mont_() | 206 | sa=t; //used in mont_() |
209 | mr_x1=t; mr_r=t; mr_a=t; //used in millerRabin() | 207 | mr_x1=t; mr_r=t; mr_a=t; //used in millerRabin() |
210 | eg_v=t; eg_u=t; eg_A=t; eg_B=t; eg_C=t; eg_D=t; //used in eGCD_(), inverseMod_() | 208 | eg_v=t; eg_u=t; eg_A=t; eg_B=t; eg_C=t; eg_D=t; //used in eGCD_(), inverseMod_() |
211 | md_q1=t; md_q2=t; md_q3=t; md_r=t; md_r1=t; md_r2=t; md_tt=t; //used in mod_() | 209 | md_q1=t; md_q2=t; md_q3=t; md_r=t; md_r1=t; md_r2=t; md_tt=t; //used in mod_() |
212 | 210 | ||
213 | primes=t; pows=t; s_i=t; s_i2=t; s_R=t; s_rm=t; s_q=t; s_n1=t; | 211 | primes=t; pows=t; s_i=t; s_i2=t; s_R=t; s_rm=t; s_q=t; s_n1=t; |
214 | s_a=t; s_r2=t; s_n=t; s_b=t; s_d=t; s_x1=t; s_x2=t, s_aa=t; //used in randTruePrime_() | 212 | s_a=t; s_r2=t; s_n=t; s_b=t; s_d=t; s_x1=t; s_x2=t, s_aa=t; //used in randTruePrime_() |
215 | 213 | ||
216 | //////////////////////////////////////////////////////////////////////////////////////// | 214 | //////////////////////////////////////////////////////////////////////////////////////// |
217 | 215 | ||
218 | //return array of all primes less than integer n | 216 | //return array of all primes less than integer n |
219 | function findPrimes(n) { | 217 | function findPrimes(n) { |
220 | var i,s,p,ans; | 218 | var i,s,p,ans; |
221 | s=new Array(n); | 219 | s=new Array(n); |
222 | for (i=0;i<n;i++) | 220 | for (i=0;i<n;i++) |
223 | s[i]=0; | 221 | s[i]=0; |
224 | s[0]=2; | 222 | s[0]=2; |
225 | p=0; //first p elements of s are primes, the rest are a sieve | 223 | p=0; //first p elements of s are primes, the rest are a sieve |
226 | for(;s[p]<n;) { //s[p] is the pth prime | 224 | for(;s[p]<n;) { //s[p] is the pth prime |
227 | for(i=s[p]*s[p]; i<n; i+=s[p]) //mark multiples of s[p] | 225 | for(i=s[p]*s[p]; i<n; i+=s[p]) //mark multiples of s[p] |
228 | s[i]=1; | 226 | s[i]=1; |
229 | p++; | 227 | p++; |
230 | s[p]=s[p-1]+1; | 228 | s[p]=s[p-1]+1; |
231 | for(; s[p]<n && s[s[p]]; s[p]++); //find next prime (where s[p]==0) | 229 | for(; s[p]<n && s[s[p]]; s[p]++); //find next prime (where s[p]==0) |
232 | } | 230 | } |
233 | ans=new Array(p); | 231 | ans=new Array(p); |
234 | for(i=0;i<p;i++) | 232 | for(i=0;i<p;i++) |
235 | ans[i]=s[i]; | 233 | ans[i]=s[i]; |
236 | return ans; | 234 | return ans; |
237 | } | 235 | } |
238 | 236 | ||
239 | //does a single round of Miller-Rabin base b consider x to be a possible prime? | 237 | //does a single round of Miller-Rabin base b consider x to be a possible prime? |
240 | //x is a bigInt, and b is an integer | 238 | //x is a bigInt, and b is an integer |
241 | function millerRabin(x,b) { | 239 | function millerRabin(x,b) { |
242 | var i,j,k,s; | 240 | var i,j,k,s; |
243 | 241 | ||
244 | if (mr_x1.length!=x.length) { | 242 | if (mr_x1.length!=x.length) { |
245 | mr_x1=dup(x); | 243 | mr_x1=dup(x); |
246 | mr_r=dup(x); | 244 | mr_r=dup(x); |
247 | mr_a=dup(x); | 245 | mr_a=dup(x); |
248 | } | 246 | } |
249 | 247 | ||
250 | copyInt_(mr_a,b); | 248 | copyInt_(mr_a,b); |
251 | copy_(mr_r,x); | 249 | copy_(mr_r,x); |
252 | copy_(mr_x1,x); | 250 | copy_(mr_x1,x); |
253 | 251 | ||
254 | addInt_(mr_r,-1); | 252 | addInt_(mr_r,-1); |
255 | addInt_(mr_x1,-1); | 253 | addInt_(mr_x1,-1); |
256 | 254 | ||
257 | //s=the highest power of two that divides mr_r | 255 | //s=the highest power of two that divides mr_r |
258 | k=0; | 256 | k=0; |
259 | for (i=0;i<mr_r.length;i++) | 257 | for (i=0;i<mr_r.length;i++) |
260 | for (j=1;j<mask;j<<=1) | 258 | for (j=1;j<mask;j<<=1) |
261 | if (x[i] & j) { | 259 | if (x[i] & j) { |
262 | s=(k<mr_r.length+bpe ? k : 0); | 260 | s=(k<mr_r.length+bpe ? k : 0); |
263 | i=mr_r.length; | 261 | i=mr_r.length; |
264 | j=mask; | 262 | j=mask; |
265 | } else | 263 | } else |
266 | k++; | 264 | k++; |
267 | 265 | ||
268 | if (s) | 266 | if (s) |
269 | rightShift_(mr_r,s); | 267 | rightShift_(mr_r,s); |
270 | 268 | ||
271 | powMod_(mr_a,mr_r,x); | 269 | powMod_(mr_a,mr_r,x); |
272 | 270 | ||
273 | if (!equalsInt(mr_a,1) && !equals(mr_a,mr_x1)) { | 271 | if (!equalsInt(mr_a,1) && !equals(mr_a,mr_x1)) { |
274 | j=1; | 272 | j=1; |
275 | while (j<=s-1 && !equals(mr_a,mr_x1)) { | 273 | while (j<=s-1 && !equals(mr_a,mr_x1)) { |
276 | squareMod_(mr_a,x); | 274 | squareMod_(mr_a,x); |
277 | if (equalsInt(mr_a,1)) { | 275 | if (equalsInt(mr_a,1)) { |
278 | return 0; | 276 | return 0; |
279 | } | 277 | } |
280 | j++; | 278 | j++; |
281 | } | 279 | } |
282 | if (!equals(mr_a,mr_x1)) { | 280 | if (!equals(mr_a,mr_x1)) { |
283 | return 0; | 281 | return 0; |
284 | } | 282 | } |
285 | } | 283 | } |
286 | return 1; | 284 | return 1; |
287 | } | 285 | } |
288 | 286 | ||
289 | //returns how many bits long the bigInt is, not counting leading zeros. | 287 | //returns how many bits long the bigInt is, not counting leading zeros. |
290 | function bitSize(x) { | 288 | function bitSize(x) { |
291 | var j,z,w; | 289 | var j,z,w; |
292 | for (j=x.length-1; (x[j]==0) && (j>0); j--); | 290 | for (j=x.length-1; (x[j]==0) && (j>0); j--); |
293 | for (z=0,w=x[j]; w; (w>>=1),z++); | 291 | for (z=0,w=x[j]; w; (w>>=1),z++); |
294 | z+=bpe*j; | 292 | z+=bpe*j; |
295 | return z; | 293 | return z; |
296 | } | 294 | } |
297 | 295 | ||
298 | //return a copy of x with at least n elements, adding leading zeros if needed | 296 | //return a copy of x with at least n elements, adding leading zeros if needed |
299 | function expand(x,n) { | 297 | function expand(x,n) { |
300 | var ans=int2bigInt(0,(x.length>n ? x.length : n)*bpe,0); | 298 | var ans=int2bigInt(0,(x.length>n ? x.length : n)*bpe,0); |
301 | copy_(ans,x); | 299 | copy_(ans,x); |
302 | return ans; | 300 | return ans; |
303 | } | 301 | } |
304 | 302 | ||
305 | //return a k-bit true random prime using Maurer's algorithm. | 303 | //return a k-bit true random prime using Maurer's algorithm. |
306 | function randTruePrime(k) { | 304 | function randTruePrime(k) { |
307 | var ans=int2bigInt(0,k,0); | 305 | var ans=int2bigInt(0,k,0); |
308 | randTruePrime_(ans,k); | 306 | randTruePrime_(ans,k); |
309 | return trim(ans,1); | 307 | return trim(ans,1); |
310 | } | 308 | } |
311 | 309 | ||
312 | //return a new bigInt equal to (x mod n) for bigInts x and n. | 310 | //return a new bigInt equal to (x mod n) for bigInts x and n. |
313 | function mod(x,n) { | 311 | function mod(x,n) { |
314 | var ans=dup(x); | 312 | var ans=dup(x); |
315 | mod_(ans,n); | 313 | mod_(ans,n); |
316 | return trim(ans,1); | 314 | return trim(ans,1); |
317 | } | 315 | } |
318 | 316 | ||
319 | //return (x+n) where x is a bigInt and n is an integer. | 317 | //return (x+n) where x is a bigInt and n is an integer. |
320 | function addInt(x,n) { | 318 | function addInt(x,n) { |
321 | var ans=expand(x,x.length+1); | 319 | var ans=expand(x,x.length+1); |
322 | addInt_(ans,n); | 320 | addInt_(ans,n); |
323 | return trim(ans,1); | 321 | return trim(ans,1); |
324 | } | 322 | } |
325 | 323 | ||
326 | //return x*y for bigInts x and y. This is faster when y<x. | 324 | //return x*y for bigInts x and y. This is faster when y<x. |
327 | function mult(x,y) { | 325 | function mult(x,y) { |
328 | var ans=expand(x,x.length+y.length); | 326 | var ans=expand(x,x.length+y.length); |
329 | mult_(ans,y); | 327 | mult_(ans,y); |
330 | return trim(ans,1); | 328 | return trim(ans,1); |
331 | } | 329 | } |
332 | 330 | ||
333 | //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n. | 331 | //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n. |
334 | function powMod(x,y,n) { | 332 | function powMod(x,y,n) { |
335 | var ans=expand(x,n.length); | 333 | var ans=expand(x,n.length); |
336 | powMod_(ans,trim(y,2),trim(n,2),0); //this should work without the trim, but doesn't | 334 | powMod_(ans,trim(y,2),trim(n,2),0); //this should work without the trim, but doesn't |
337 | return trim(ans,1); | 335 | return trim(ans,1); |
338 | } | 336 | } |
339 | 337 | ||
340 | //return (x-y) for bigInts x and y. Negative answers will be 2s complement | 338 | //return (x-y) for bigInts x and y. Negative answers will be 2s complement |
341 | function sub(x,y) { | 339 | function sub(x,y) { |
342 | var ans=expand(x,(x.length>y.length ? x.length+1 : y.length+1)); | 340 | var ans=expand(x,(x.length>y.length ? x.length+1 : y.length+1)); |
343 | sub_(ans,y); | 341 | sub_(ans,y); |
344 | return trim(ans,1); | 342 | return trim(ans,1); |
345 | } | 343 | } |
346 | 344 | ||
347 | //return (x+y) for bigInts x and y. | 345 | //return (x+y) for bigInts x and y. |
348 | function add(x,y) { | 346 | function add(x,y) { |
349 | var ans=expand(x,(x.length>y.length ? x.length+1 : y.length+1)); | 347 | var ans=expand(x,(x.length>y.length ? x.length+1 : y.length+1)); |
350 | add_(ans,y); | 348 | add_(ans,y); |
351 | return trim(ans,1); | 349 | return trim(ans,1); |
352 | } | 350 | } |
353 | 351 | ||
354 | //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null | 352 | //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null |
355 | function inverseMod(x,n) { | 353 | function inverseMod(x,n) { |
356 | var ans=expand(x,n.length); | 354 | var ans=expand(x,n.length); |
357 | var s; | 355 | var s; |
358 | s=inverseMod_(ans,n); | 356 | s=inverseMod_(ans,n); |
359 | return s ? trim(ans,1) : null; | 357 | return s ? trim(ans,1) : null; |
360 | } | 358 | } |
361 | 359 | ||
362 | //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. | 360 | //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. |
363 | function multMod(x,y,n) { | 361 | function multMod(x,y,n) { |
364 | var ans=expand(x,n.length); | 362 | var ans=expand(x,n.length); |
365 | multMod_(ans,y,n); | 363 | multMod_(ans,y,n); |
366 | return trim(ans,1); | 364 | return trim(ans,1); |
367 | } | 365 | } |
368 | 366 | ||
369 | //generate a k-bit true random prime using Maurer's algorithm, | 367 | //generate a k-bit true random prime using Maurer's algorithm, |
370 | //and put it into ans. The bigInt ans must be large enough to hold it. | 368 | //and put it into ans. The bigInt ans must be large enough to hold it. |
371 | function randTruePrime_(ans,k) { | 369 | function randTruePrime_(ans,k) { |
372 | var c,m,pm,dd,j,r,B,divisible,z,zz,recSize; | 370 | var c,m,pm,dd,j,r,B,divisible,z,zz,recSize; |
373 | 371 | ||
374 | if (primes.length==0) | 372 | if (primes.length==0) |
375 | primes=findPrimes(30000); //check for divisibility by primes <=30000 | 373 | primes=findPrimes(30000); //check for divisibility by primes <=30000 |
376 | 374 | ||
377 | if (pows.length==0) { | 375 | if (pows.length==0) { |
378 | pows=new Array(512); | 376 | pows=new Array(512); |
379 | for (j=0;j<512;j++) { | 377 | for (j=0;j<512;j++) { |
380 | pows[j]=Math.pow(2,j/511.-1.); | 378 | pows[j]=Math.pow(2,j/511.-1.); |
381 | } | 379 | } |
382 | } | 380 | } |
383 | 381 | ||
384 | //c and m should be tuned for a particular machine and value of k, to maximize speed | 382 | //c and m should be tuned for a particular machine and value of k, to maximize speed |
385 | //this was: c=primes[primes.length-1]/k/k; //check using all the small primes. (c=0.1 in HAC) | 383 | //this was: c=primes[primes.length-1]/k/k; //check using all the small primes. (c=0.1 in HAC) |
386 | c=0.1; | 384 | c=0.1; |
387 | m=20; //generate this k-bit number by first recursively generating a number that has between k/2 and k-m bits | 385 | m=20; //generate this k-bit number by first recursively generating a number that has between k/2 and k-m bits |
388 | recLimit=20; /*must be at least 2 (was 29)*/ //stop recursion when k <=recLimit | 386 | recLimit=20; /*must be at least 2 (was 29)*/ //stop recursion when k <=recLimit |
389 | 387 | ||
390 | if (s_i2.length!=ans.length) { | 388 | if (s_i2.length!=ans.length) { |
391 | s_i2=dup(ans); | 389 | s_i2=dup(ans); |
392 | s_R =dup(ans); | 390 | s_R =dup(ans); |
393 | s_n1=dup(ans); | 391 | s_n1=dup(ans); |
394 | s_r2=dup(ans); | 392 | s_r2=dup(ans); |
395 | s_d =dup(ans); | 393 | s_d =dup(ans); |
396 | s_x1=dup(ans); | 394 | s_x1=dup(ans); |
397 | s_x2=dup(ans); | 395 | s_x2=dup(ans); |
398 | s_b =dup(ans); | 396 | s_b =dup(ans); |
399 | s_n =dup(ans); | 397 | s_n =dup(ans); |
400 | s_i =dup(ans); | 398 | s_i =dup(ans); |
401 | s_rm=dup(ans); | 399 | s_rm=dup(ans); |
402 | s_q =dup(ans); | 400 | s_q =dup(ans); |
403 | s_a =dup(ans); | 401 | s_a =dup(ans); |
404 | s_aa=dup(ans); | 402 | s_aa=dup(ans); |
405 | } | 403 | } |
406 | 404 | ||
diff --git a/frontend/beta/js/Clipperz/Crypto/BigInt_scoped.js b/frontend/beta/js/Clipperz/Crypto/BigInt_scoped.js index f91c7e9..bc60330 100644 --- a/frontend/beta/js/Clipperz/Crypto/BigInt_scoped.js +++ b/frontend/beta/js/Clipperz/Crypto/BigInt_scoped.js | |||
@@ -1,406 +1,404 @@ | |||
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 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | 24 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } |
27 | if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; } | 25 | if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; } |
28 | 26 | ||
29 | if (typeof(Leemon) == 'undefined') { Leemon = {}; } | 27 | if (typeof(Leemon) == 'undefined') { Leemon = {}; } |
30 | if (typeof(Baird.Crypto) == 'undefined') { Baird.Crypto = {}; } | 28 | if (typeof(Baird.Crypto) == 'undefined') { Baird.Crypto = {}; } |
31 | if (typeof(Baird.Crypto.BigInt) == 'undefined') { Baird.Crypto.BigInt = {}; } | 29 | if (typeof(Baird.Crypto.BigInt) == 'undefined') { Baird.Crypto.BigInt = {}; } |
32 | 30 | ||
33 | 31 | ||
34 | //############################################################################# | 32 | //############################################################################# |
35 | //Downloaded on March 05, 2007 from http://www.leemon.com/crypto/BigInt.js | 33 | //Downloaded on March 05, 2007 from http://www.leemon.com/crypto/BigInt.js |
36 | //############################################################################# | 34 | //############################################################################# |
37 | 35 | ||
38 | //////////////////////////////////////////////////////////////////////////////////////// | 36 | //////////////////////////////////////////////////////////////////////////////////////// |
39 | // Big Integer Library v. 5.0 | 37 | // Big Integer Library v. 5.0 |
40 | // Created 2000, last modified 2006 | 38 | // Created 2000, last modified 2006 |
41 | // Leemon Baird | 39 | // Leemon Baird |
42 | // www.leemon.com | 40 | // www.leemon.com |
43 | // | 41 | // |
44 | // This file is public domain. You can use it for any purpose without restriction. | 42 | // This file is public domain. You can use it for any purpose without restriction. |
45 | // I do not guarantee that it is correct, so use it at your own risk. If you use | 43 | // I do not guarantee that it is correct, so use it at your own risk. If you use |
46 | // it for something interesting, I'd appreciate hearing about it. If you find | 44 | // it for something interesting, I'd appreciate hearing about it. If you find |
47 | // any bugs or make any improvements, I'd appreciate hearing about those too. | 45 | // any bugs or make any improvements, I'd appreciate hearing about those too. |
48 | // It would also be nice if my name and address were left in the comments. | 46 | // It would also be nice if my name and address were left in the comments. |
49 | // But none of that is required. | 47 | // But none of that is required. |
50 | // | 48 | // |
51 | // This code defines a bigInt library for arbitrary-precision integers. | 49 | // This code defines a bigInt library for arbitrary-precision integers. |
52 | // A bigInt is an array of integers storing the value in chunks of bpe bits, | 50 | // A bigInt is an array of integers storing the value in chunks of bpe bits, |
53 | // little endian (buff[0] is the least significant word). | 51 | // little endian (buff[0] is the least significant word). |
54 | // Negative bigInts are stored two's complement. | 52 | // Negative bigInts are stored two's complement. |
55 | // Some functions assume their parameters have at least one leading zero element. | 53 | // Some functions assume their parameters have at least one leading zero element. |
56 | // Functions with an underscore at the end of the name have unpredictable behavior in case of overflow, | 54 | // Functions with an underscore at the end of the name have unpredictable behavior in case of overflow, |
57 | // so the caller must make sure overflow won't happen. | 55 | // so the caller must make sure overflow won't happen. |
58 | // For each function where a parameter is modified, that same | 56 | // For each function where a parameter is modified, that same |
59 | // variable must not be used as another argument too. | 57 | // variable must not be used as another argument too. |
60 | // So, you cannot square x by doing multMod_(x,x,n). | 58 | // So, you cannot square x by doing multMod_(x,x,n). |
61 | // You must use squareMod_(x,n) instead, or do y=dup(x); multMod_(x,y,n). | 59 | // You must use squareMod_(x,n) instead, or do y=dup(x); multMod_(x,y,n). |
62 | // | 60 | // |
63 | // These functions are designed to avoid frequent dynamic memory allocation in the inner loop. | 61 | // These functions are designed to avoid frequent dynamic memory allocation in the inner loop. |
64 | // For most functions, if it needs a BigInt as a local variable it will actually use | 62 | // For most functions, if it needs a BigInt as a local variable it will actually use |
65 | // a global, and will only allocate to it when it's not the right size. This ensures | 63 | // a global, and will only allocate to it when it's not the right size. This ensures |
66 | // that when a function is called repeatedly with same-sized parameters, it only allocates | 64 | // that when a function is called repeatedly with same-sized parameters, it only allocates |
67 | // memory on the first call. | 65 | // memory on the first call. |
68 | // | 66 | // |
69 | // Note that for cryptographic purposes, the calls to Math.random() must | 67 | // Note that for cryptographic purposes, the calls to Math.random() must |
70 | // be replaced with calls to a better pseudorandom number generator. | 68 | // be replaced with calls to a better pseudorandom number generator. |
71 | // | 69 | // |
72 | // In the following, "bigInt" means a bigInt with at least one leading zero element, | 70 | // In the following, "bigInt" means a bigInt with at least one leading zero element, |
73 | // and "integer" means a nonnegative integer less than radix. In some cases, integer | 71 | // and "integer" means a nonnegative integer less than radix. In some cases, integer |
74 | // can be negative. Negative bigInts are 2s complement. | 72 | // can be negative. Negative bigInts are 2s complement. |
75 | // | 73 | // |
76 | // The following functions do not modify their inputs, but dynamically allocate memory every time they are called: | 74 | // The following functions do not modify their inputs, but dynamically allocate memory every time they are called: |
77 | // | 75 | // |
78 | // function bigInt2str(x,base) //convert a bigInt into a string in a given base, from base 2 up to base 95 | 76 | // function bigInt2str(x,base) //convert a bigInt into a string in a given base, from base 2 up to base 95 |
79 | // function dup(x) //returns a copy of bigInt x | 77 | // function dup(x) //returns a copy of bigInt x |
80 | // function findPrimes(n) //return array of all primes less than integer n | 78 | // function findPrimes(n) //return array of all primes less than integer n |
81 | // function int2bigInt(t,n,m) //convert integer t to a bigInt with at least n bits and m array elements | 79 | // function int2bigInt(t,n,m) //convert integer t to a bigInt with at least n bits and m array elements |
82 | // function str2bigInt(s,b,n,m) //convert string s in base b to a bigInt with at least n bits and m array elements | 80 | // function str2bigInt(s,b,n,m) //convert string s in base b to a bigInt with at least n bits and m array elements |
83 | // function trim(x,k) //return a copy of x with exactly k leading zero elements | 81 | // function trim(x,k) //return a copy of x with exactly k leading zero elements |
84 | // | 82 | // |
85 | // The following functions do not modify their inputs, so there is never a problem with the result being too big: | 83 | // The following functions do not modify their inputs, so there is never a problem with the result being too big: |
86 | // | 84 | // |
87 | // function bitSize(x) //returns how many bits long the bigInt x is, not counting leading zeros | 85 | // function bitSize(x) //returns how many bits long the bigInt x is, not counting leading zeros |
88 | // function equals(x,y) //is the bigInt x equal to the bigint y? | 86 | // function equals(x,y) //is the bigInt x equal to the bigint y? |
89 | // function equalsInt(x,y) //is bigint x equal to integer y? | 87 | // function equalsInt(x,y) //is bigint x equal to integer y? |
90 | // function greater(x,y) //is x>y? (x and y are nonnegative bigInts) | 88 | // function greater(x,y) //is x>y? (x and y are nonnegative bigInts) |
91 | // function greaterShift(x,y,shift)//is (x <<(shift*bpe)) > y? | 89 | // function greaterShift(x,y,shift)//is (x <<(shift*bpe)) > y? |
92 | // function isZero(x) //is the bigInt x equal to zero? | 90 | // function isZero(x) //is the bigInt x equal to zero? |
93 | // function millerRabin(x,b) //does one round of Miller-Rabin base integer b say that bigInt x is possibly prime (as opposed to definitely composite)? | 91 | // function millerRabin(x,b) //does one round of Miller-Rabin base integer b say that bigInt x is possibly prime (as opposed to definitely composite)? |
94 | // function modInt(x,n) //return x mod n for bigInt x and integer n. | 92 | // function modInt(x,n) //return x mod n for bigInt x and integer n. |
95 | // function negative(x) //is bigInt x negative? | 93 | // function negative(x) //is bigInt x negative? |
96 | // | 94 | // |
97 | // The following functions do not modify their inputs, but allocate memory and call functions with underscores | 95 | // The following functions do not modify their inputs, but allocate memory and call functions with underscores |
98 | // | 96 | // |
99 | // function add(x,y) //return (x+y) for bigInts x and y. | 97 | // function add(x,y) //return (x+y) for bigInts x and y. |
100 | // function addInt(x,n) //return (x+n) where x is a bigInt and n is an integer. | 98 | // function addInt(x,n) //return (x+n) where x is a bigInt and n is an integer. |
101 | // function expand(x,n) //return a copy of x with at least n elements, adding leading zeros if needed | 99 | // function expand(x,n) //return a copy of x with at least n elements, adding leading zeros if needed |
102 | // function inverseMod(x,n) //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null | 100 | // function inverseMod(x,n) //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null |
103 | // function mod(x,n) //return a new bigInt equal to (x mod n) for bigInts x and n. | 101 | // function mod(x,n) //return a new bigInt equal to (x mod n) for bigInts x and n. |
104 | // function mult(x,y) //return x*y for bigInts x and y. This is faster when y<x. | 102 | // function mult(x,y) //return x*y for bigInts x and y. This is faster when y<x. |
105 | // function multMod(x,y,n) //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. | 103 | // function multMod(x,y,n) //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. |
106 | // function powMod(x,y,n) //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n. | 104 | // function powMod(x,y,n) //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n. |
107 | // function randTruePrime(k) //return a new, random, k-bit, true prime using Maurer's algorithm. | 105 | // function randTruePrime(k) //return a new, random, k-bit, true prime using Maurer's algorithm. |
108 | // function sub(x,y) //return (x-y) for bigInts x and y. Negative answers will be 2s complement | 106 | // function sub(x,y) //return (x-y) for bigInts x and y. Negative answers will be 2s complement |
109 | // | 107 | // |
110 | // The following functions write a bigInt result to one of the parameters, but | 108 | // The following functions write a bigInt result to one of the parameters, but |
111 | // the result is never bigger than the original, so there can't be overflow problems: | 109 | // the result is never bigger than the original, so there can't be overflow problems: |
112 | // | 110 | // |
113 | // function divInt_(x,n) //do x=floor(x/n) for bigInt x and integer n, and return the remainder | 111 | // function divInt_(x,n) //do x=floor(x/n) for bigInt x and integer n, and return the remainder |
114 | // function GCD_(x,y) //set x to the greatest common divisor of bigInts x and y, (y is destroyed). | 112 | // function GCD_(x,y) //set x to the greatest common divisor of bigInts x and y, (y is destroyed). |
115 | // function halve_(x) //do x=floor(|x|/2)*sgn(x) for bigInt x in 2's complement | 113 | // function halve_(x) //do x=floor(|x|/2)*sgn(x) for bigInt x in 2's complement |
116 | // function mod_(x,n) //do x=x mod n for bigInts x and n. | 114 | // function mod_(x,n) //do x=x mod n for bigInts x and n. |
117 | // function rightShift_(x,n) //right shift bigInt x by n bits. 0 <= n < bpe. | 115 | // function rightShift_(x,n) //right shift bigInt x by n bits. 0 <= n < bpe. |
118 | // | 116 | // |
119 | // The following functions write a bigInt result to one of the parameters. The caller is responsible for | 117 | // The following functions write a bigInt result to one of the parameters. The caller is responsible for |
120 | // ensuring it is large enough to hold the result. | 118 | // ensuring it is large enough to hold the result. |
121 | // | 119 | // |
122 | // function addInt_(x,n) //do x=x+n where x is a bigInt and n is an integer | 120 | // function addInt_(x,n) //do x=x+n where x is a bigInt and n is an integer |
123 | // function add_(x,y) //do x=x+y for bigInts x and y | 121 | // function add_(x,y) //do x=x+y for bigInts x and y |
124 | // function addShift_(x,y,ys) //do x=x+(y<<(ys*bpe)) | 122 | // function addShift_(x,y,ys) //do x=x+(y<<(ys*bpe)) |
125 | // function copy_(x,y) //do x=y on bigInts x and y | 123 | // function copy_(x,y) //do x=y on bigInts x and y |
126 | // function copyInt_(x,n) //do x=n on bigInt x and integer n | 124 | // function copyInt_(x,n) //do x=n on bigInt x and integer n |
127 | // function carry_(x) //do carries and borrows so each element of the bigInt x fits in bpe bits. | 125 | // function carry_(x) //do carries and borrows so each element of the bigInt x fits in bpe bits. |
128 | // function divide_(x,y,q,r) //divide_ x by y giving quotient q and remainder r | 126 | // function divide_(x,y,q,r) //divide_ x by y giving quotient q and remainder r |
129 | // function eGCD_(x,y,d,a,b) //sets a,b,d to positive big integers such that d = GCD_(x,y) = a*x-b*y | 127 | // function eGCD_(x,y,d,a,b) //sets a,b,d to positive big integers such that d = GCD_(x,y) = a*x-b*y |
130 | // function inverseMod_(x,n) //do x=x**(-1) mod n, for bigInts x and n. Returns 1 (0) if inverse does (doesn't) exist | 128 | // function inverseMod_(x,n) //do x=x**(-1) mod n, for bigInts x and n. Returns 1 (0) if inverse does (doesn't) exist |
131 | // function inverseModInt_(x,n) //return x**(-1) mod n, for integers x and n. Return 0 if there is no inverse | 129 | // function inverseModInt_(x,n) //return x**(-1) mod n, for integers x and n. Return 0 if there is no inverse |
132 | // function leftShift_(x,n) //left shift bigInt x by n bits. n<bpe. | 130 | // function leftShift_(x,n) //left shift bigInt x by n bits. n<bpe. |
133 | // function linComb_(x,y,a,b) //do x=a*x+b*y for bigInts x and y and integers a and b | 131 | // function linComb_(x,y,a,b) //do x=a*x+b*y for bigInts x and y and integers a and b |
134 | // function linCombShift_(x,y,b,ys) //do x=x+b*(y<<(ys*bpe)) for bigInts x and y, and integers b and ys | 132 | // function linCombShift_(x,y,b,ys) //do x=x+b*(y<<(ys*bpe)) for bigInts x and y, and integers b and ys |
135 | // function mont_(x,y,n,np) //Montgomery multiplication (see comments where the function is defined) | 133 | // function mont_(x,y,n,np) //Montgomery multiplication (see comments where the function is defined) |
136 | // function mult_(x,y) //do x=x*y for bigInts x and y. | 134 | // function mult_(x,y) //do x=x*y for bigInts x and y. |
137 | // function multInt_(x,n) //do x=x*n where x is a bigInt and n is an integer. | 135 | // function multInt_(x,n) //do x=x*n where x is a bigInt and n is an integer. |
138 | // function multMod_(x,y,n) //do x=x*y mod n for bigInts x,y,n. | 136 | // function multMod_(x,y,n) //do x=x*y mod n for bigInts x,y,n. |
139 | // function powMod_(x,y,n) //do x=x**y mod n, where x,y,n are bigInts (n is odd) and ** is exponentiation. 0**0=1. | 137 | // function powMod_(x,y,n) //do x=x**y mod n, where x,y,n are bigInts (n is odd) and ** is exponentiation. 0**0=1. |
140 | // function randBigInt_(b,n,s) //do b = an n-bit random BigInt. if s=1, then nth bit (most significant bit) is set to 1. n>=1. | 138 | // function randBigInt_(b,n,s) //do b = an n-bit random BigInt. if s=1, then nth bit (most significant bit) is set to 1. n>=1. |
141 | // function randTruePrime_(ans,k) //do ans = a random k-bit true random prime (not just probable prime) with 1 in the msb. | 139 | // function randTruePrime_(ans,k) //do ans = a random k-bit true random prime (not just probable prime) with 1 in the msb. |
142 | // function squareMod_(x,n) //do x=x*x mod n for bigInts x,n | 140 | // function squareMod_(x,n) //do x=x*x mod n for bigInts x,n |
143 | // function sub_(x,y) //do x=x-y for bigInts x and y. Negative answers will be 2s complement. | 141 | // function sub_(x,y) //do x=x-y for bigInts x and y. Negative answers will be 2s complement. |
144 | // function subShift_(x,y,ys) //do x=x-(y<<(ys*bpe)). Negative answers will be 2s complement. | 142 | // function subShift_(x,y,ys) //do x=x-(y<<(ys*bpe)). Negative answers will be 2s complement. |
145 | // | 143 | // |
146 | // The following functions are based on algorithms from the _Handbook of Applied Cryptography_ | 144 | // The following functions are based on algorithms from the _Handbook of Applied Cryptography_ |
147 | // powMod_() = algorithm 14.94, Montgomery exponentiation | 145 | // powMod_() = algorithm 14.94, Montgomery exponentiation |
148 | // eGCD_,inverseMod_() = algorithm 14.61, Binary extended GCD_ | 146 | // eGCD_,inverseMod_() = algorithm 14.61, Binary extended GCD_ |
149 | // GCD_() = algorothm 14.57, Lehmer's algorithm | 147 | // GCD_() = algorothm 14.57, Lehmer's algorithm |
150 | // mont_() = algorithm 14.36, Montgomery multiplication | 148 | // mont_() = algorithm 14.36, Montgomery multiplication |
151 | // divide_() = algorithm 14.20 Multiple-precision division | 149 | // divide_() = algorithm 14.20 Multiple-precision division |
152 | // squareMod_() = algorithm 14.16 Multiple-precision squaring | 150 | // squareMod_() = algorithm 14.16 Multiple-precision squaring |
153 | // randTruePrime_() = algorithm 4.62, Maurer's algorithm | 151 | // randTruePrime_() = algorithm 4.62, Maurer's algorithm |
154 | // millerRabin() = algorithm 4.24, Miller-Rabin algorithm | 152 | // millerRabin() = algorithm 4.24, Miller-Rabin algorithm |
155 | // | 153 | // |
156 | // Profiling shows: | 154 | // Profiling shows: |
157 | // randTruePrime_() spends: | 155 | // randTruePrime_() spends: |
158 | // 10% of its time in calls to powMod_() | 156 | // 10% of its time in calls to powMod_() |
159 | // 85% of its time in calls to millerRabin() | 157 | // 85% of its time in calls to millerRabin() |
160 | // millerRabin() spends: | 158 | // millerRabin() spends: |
161 | // 99% of its time in calls to powMod_() (always with a base of 2) | 159 | // 99% of its time in calls to powMod_() (always with a base of 2) |
162 | // powMod_() spends: | 160 | // powMod_() spends: |
163 | // 94% of its time in calls to mont_() (almost always with x==y) | 161 | // 94% of its time in calls to mont_() (almost always with x==y) |
164 | // | 162 | // |
165 | // This suggests there are several ways to speed up this library slightly: | 163 | // This suggests there are several ways to speed up this library slightly: |
166 | // - convert powMod_ to use a Montgomery form of k-ary window (or maybe a Montgomery form of sliding window) | 164 | // - convert powMod_ to use a Montgomery form of k-ary window (or maybe a Montgomery form of sliding window) |
167 | // -- this should especially focus on being fast when raising 2 to a power mod n | 165 | // -- this should especially focus on being fast when raising 2 to a power mod n |
168 | // - convert randTruePrime_() to use a minimum r of 1/3 instead of 1/2 with the appropriate change to the test | 166 | // - convert randTruePrime_() to use a minimum r of 1/3 instead of 1/2 with the appropriate change to the test |
169 | // - tune the parameters in randTruePrime_(), including c, m, and recLimit | 167 | // - tune the parameters in randTruePrime_(), including c, m, and recLimit |
170 | // - speed up the single loop in mont_() that takes 95% of the runtime, perhaps by reducing checking | 168 | // - speed up the single loop in mont_() that takes 95% of the runtime, perhaps by reducing checking |
171 | // within the loop when all the parameters are the same length. | 169 | // within the loop when all the parameters are the same length. |
172 | // | 170 | // |
173 | // There are several ideas that look like they wouldn't help much at all: | 171 | // There are several ideas that look like they wouldn't help much at all: |
174 | // - replacing trial division in randTruePrime_() with a sieve (that speeds up something taking almost no time anyway) | 172 | // - replacing trial division in randTruePrime_() with a sieve (that speeds up something taking almost no time anyway) |
175 | // - increase bpe from 15 to 30 (that would help if we had a 32*32->64 multiplier, but not with JavaScript's 32*32->32) | 173 | // - increase bpe from 15 to 30 (that would help if we had a 32*32->64 multiplier, but not with JavaScript's 32*32->32) |
176 | // - speeding up mont_(x,y,n,np) when x==y by doing a non-modular, non-Montgomery square | 174 | // - speeding up mont_(x,y,n,np) when x==y by doing a non-modular, non-Montgomery square |
177 | // followed by a Montgomery reduction. The intermediate answer will be twice as long as x, so that | 175 | // followed by a Montgomery reduction. The intermediate answer will be twice as long as x, so that |
178 | // method would be slower. This is unfortunate because the code currently spends almost all of its time | 176 | // method would be slower. This is unfortunate because the code currently spends almost all of its time |
179 | // doing mont_(x,x,...), both for randTruePrime_() and powMod_(). A faster method for Montgomery squaring | 177 | // doing mont_(x,x,...), both for randTruePrime_() and powMod_(). A faster method for Montgomery squaring |
180 | // would have a large impact on the speed of randTruePrime_() and powMod_(). HAC has a couple of poorly-worded | 178 | // would have a large impact on the speed of randTruePrime_() and powMod_(). HAC has a couple of poorly-worded |
181 | // sentences that seem to imply it's faster to do a non-modular square followed by a single | 179 | // sentences that seem to imply it's faster to do a non-modular square followed by a single |
182 | // Montgomery reduction, but that's obviously wrong. | 180 | // Montgomery reduction, but that's obviously wrong. |
183 | //////////////////////////////////////////////////////////////////////////////////////// | 181 | //////////////////////////////////////////////////////////////////////////////////////// |
184 | 182 | ||
185 | // | 183 | // |
186 | //The whole library has been moved into the Baird.Crypto.BigInt scope by Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 184 | //The whole library has been moved into the Baird.Crypto.BigInt scope by Giulio Cesare Solaroli <giulio.cesare@clipperz.com> |
187 | // | 185 | // |
188 | Baird.Crypto.BigInt.VERSION = "5.0"; | 186 | Baird.Crypto.BigInt.VERSION = "5.0"; |
189 | Baird.Crypto.BigInt.NAME = "Baird.Crypto.BigInt"; | 187 | Baird.Crypto.BigInt.NAME = "Baird.Crypto.BigInt"; |
190 | 188 | ||
191 | MochiKit.Base.update(Baird.Crypto.BigInt, { | 189 | MochiKit.Base.update(Baird.Crypto.BigInt, { |
192 | //globals | 190 | //globals |
193 | 'bpe': 0, //bits stored per array element | 191 | 'bpe': 0, //bits stored per array element |
194 | 'mask': 0, //AND this with an array element to chop it down to bpe bits | 192 | 'mask': 0, //AND this with an array element to chop it down to bpe bits |
195 | 'radix': Baird.Crypto.BigInt.mask + 1,//equals 2^bpe. A single 1 bit to the left of the last bit of mask. | 193 | 'radix': Baird.Crypto.BigInt.mask + 1,//equals 2^bpe. A single 1 bit to the left of the last bit of mask. |
196 | 194 | ||
197 | //the digits for converting to different bases | 195 | //the digits for converting to different bases |
198 | 'digitsStr': '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=!@#$%^&*()[]{}|;:,.<>/?`~ \\\'\"+-', | 196 | 'digitsStr': '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=!@#$%^&*()[]{}|;:,.<>/?`~ \\\'\"+-', |
199 | 197 | ||
200 | //initialize the global variables | 198 | //initialize the global variables |
201 | for (bpe=0; (1<<(bpe+1)) > (1<<bpe); bpe++); //bpe=number of bits in the mantissa on this platform | 199 | for (bpe=0; (1<<(bpe+1)) > (1<<bpe); bpe++); //bpe=number of bits in the mantissa on this platform |
202 | bpe>>=1; //bpe=number of bits in one element of the array representing the bigInt | 200 | bpe>>=1; //bpe=number of bits in one element of the array representing the bigInt |
203 | mask=(1<<bpe)-1; //AND the mask with an integer to get its bpe least significant bits | 201 | mask=(1<<bpe)-1; //AND the mask with an integer to get its bpe least significant bits |
204 | radix=mask+1; //2^bpe. a single 1 bit to the left of the first bit of mask | 202 | radix=mask+1; //2^bpe. a single 1 bit to the left of the first bit of mask |
205 | one=int2bigInt(1,1,1); //constant used in powMod_() | 203 | one=int2bigInt(1,1,1); //constant used in powMod_() |
206 | 204 | ||
207 | //the following global variables are scratchpad memory to | 205 | //the following global variables are scratchpad memory to |
208 | //reduce dynamic memory allocation in the inner loop | 206 | //reduce dynamic memory allocation in the inner loop |
209 | t=new Array(0); | 207 | t=new Array(0); |
210 | ss=t; //used in mult_() | 208 | ss=t; //used in mult_() |
211 | s0=t; //used in multMod_(), squareMod_() | 209 | s0=t; //used in multMod_(), squareMod_() |
212 | s1=t; //used in powMod_(), multMod_(), squareMod_() | 210 | s1=t; //used in powMod_(), multMod_(), squareMod_() |
213 | s2=t; //used in powMod_(), multMod_() | 211 | s2=t; //used in powMod_(), multMod_() |
214 | s3=t; //used in powMod_() | 212 | s3=t; //used in powMod_() |
215 | s4=t; s5=t; //used in mod_() | 213 | s4=t; s5=t; //used in mod_() |
216 | s6=t; //used in bigInt2str() | 214 | s6=t; //used in bigInt2str() |
217 | s7=t; //used in powMod_() | 215 | s7=t; //used in powMod_() |
218 | T=t; //used in GCD_() | 216 | T=t; //used in GCD_() |
219 | sa=t; //used in mont_() | 217 | sa=t; //used in mont_() |
220 | mr_x1=t; mr_r=t; mr_a=t; //used in millerRabin() | 218 | mr_x1=t; mr_r=t; mr_a=t; //used in millerRabin() |
221 | eg_v=t; eg_u=t; eg_A=t; eg_B=t; eg_C=t; eg_D=t; //used in eGCD_(), inverseMod_() | 219 | eg_v=t; eg_u=t; eg_A=t; eg_B=t; eg_C=t; eg_D=t; //used in eGCD_(), inverseMod_() |
222 | md_q1=t; md_q2=t; md_q3=t; md_r=t; md_r1=t; md_r2=t; md_tt=t; //used in mod_() | 220 | md_q1=t; md_q2=t; md_q3=t; md_r=t; md_r1=t; md_r2=t; md_tt=t; //used in mod_() |
223 | 221 | ||
224 | primes=t; pows=t; s_i=t; s_i2=t; s_R=t; s_rm=t; s_q=t; s_n1=t; | 222 | primes=t; pows=t; s_i=t; s_i2=t; s_R=t; s_rm=t; s_q=t; s_n1=t; |
225 | s_a=t; s_r2=t; s_n=t; s_b=t; s_d=t; s_x1=t; s_x2=t, s_aa=t; //used in randTruePrime_() | 223 | s_a=t; s_r2=t; s_n=t; s_b=t; s_d=t; s_x1=t; s_x2=t, s_aa=t; //used in randTruePrime_() |
226 | 224 | ||
227 | //////////////////////////////////////////////////////////////////////////////////////// | 225 | //////////////////////////////////////////////////////////////////////////////////////// |
228 | 226 | ||
229 | //return array of all primes less than integer n | 227 | //return array of all primes less than integer n |
230 | 'findPrimes': function(n) { | 228 | 'findPrimes': function(n) { |
231 | var i,s,p,ans; | 229 | var i,s,p,ans; |
232 | s=new Array(n); | 230 | s=new Array(n); |
233 | for (i=0;i<n;i++) | 231 | for (i=0;i<n;i++) |
234 | s[i]=0; | 232 | s[i]=0; |
235 | s[0]=2; | 233 | s[0]=2; |
236 | p=0; //first p elements of s are primes, the rest are a sieve | 234 | p=0; //first p elements of s are primes, the rest are a sieve |
237 | for(;s[p]<n;) { //s[p] is the pth prime | 235 | for(;s[p]<n;) { //s[p] is the pth prime |
238 | for(i=s[p]*s[p]; i<n; i+=s[p]) //mark multiples of s[p] | 236 | for(i=s[p]*s[p]; i<n; i+=s[p]) //mark multiples of s[p] |
239 | s[i]=1; | 237 | s[i]=1; |
240 | p++; | 238 | p++; |
241 | s[p]=s[p-1]+1; | 239 | s[p]=s[p-1]+1; |
242 | for(; s[p]<n && s[s[p]]; s[p]++); //find next prime (where s[p]==0) | 240 | for(; s[p]<n && s[s[p]]; s[p]++); //find next prime (where s[p]==0) |
243 | } | 241 | } |
244 | ans=new Array(p); | 242 | ans=new Array(p); |
245 | for(i=0;i<p;i++) | 243 | for(i=0;i<p;i++) |
246 | ans[i]=s[i]; | 244 | ans[i]=s[i]; |
247 | return ans; | 245 | return ans; |
248 | }, | 246 | }, |
249 | 247 | ||
250 | //does a single round of Miller-Rabin base b consider x to be a possible prime? | 248 | //does a single round of Miller-Rabin base b consider x to be a possible prime? |
251 | //x is a bigInt, and b is an integer | 249 | //x is a bigInt, and b is an integer |
252 | 'millerRabin': function(x,b) { | 250 | 'millerRabin': function(x,b) { |
253 | var i,j,k,s; | 251 | var i,j,k,s; |
254 | 252 | ||
255 | if (mr_x1.length!=x.length) { | 253 | if (mr_x1.length!=x.length) { |
256 | mr_x1=dup(x); | 254 | mr_x1=dup(x); |
257 | mr_r=dup(x); | 255 | mr_r=dup(x); |
258 | mr_a=dup(x); | 256 | mr_a=dup(x); |
259 | } | 257 | } |
260 | 258 | ||
261 | copyInt_(mr_a,b); | 259 | copyInt_(mr_a,b); |
262 | copy_(mr_r,x); | 260 | copy_(mr_r,x); |
263 | copy_(mr_x1,x); | 261 | copy_(mr_x1,x); |
264 | 262 | ||
265 | addInt_(mr_r,-1); | 263 | addInt_(mr_r,-1); |
266 | addInt_(mr_x1,-1); | 264 | addInt_(mr_x1,-1); |
267 | 265 | ||
268 | //s=the highest power of two that divides mr_r | 266 | //s=the highest power of two that divides mr_r |
269 | k=0; | 267 | k=0; |
270 | for (i=0;i<mr_r.length;i++) | 268 | for (i=0;i<mr_r.length;i++) |
271 | for (j=1;j<mask;j<<=1) | 269 | for (j=1;j<mask;j<<=1) |
272 | if (x[i] & j) { | 270 | if (x[i] & j) { |
273 | s=(k<mr_r.length+bpe ? k : 0); | 271 | s=(k<mr_r.length+bpe ? k : 0); |
274 | i=mr_r.length; | 272 | i=mr_r.length; |
275 | j=mask; | 273 | j=mask; |
276 | } else | 274 | } else |
277 | k++; | 275 | k++; |
278 | 276 | ||
279 | if (s) | 277 | if (s) |
280 | rightShift_(mr_r,s); | 278 | rightShift_(mr_r,s); |
281 | 279 | ||
282 | powMod_(mr_a,mr_r,x); | 280 | powMod_(mr_a,mr_r,x); |
283 | 281 | ||
284 | if (!equalsInt(mr_a,1) && !equals(mr_a,mr_x1)) { | 282 | if (!equalsInt(mr_a,1) && !equals(mr_a,mr_x1)) { |
285 | j=1; | 283 | j=1; |
286 | while (j<=s-1 && !equals(mr_a,mr_x1)) { | 284 | while (j<=s-1 && !equals(mr_a,mr_x1)) { |
287 | squareMod_(mr_a,x); | 285 | squareMod_(mr_a,x); |
288 | if (equalsInt(mr_a,1)) { | 286 | if (equalsInt(mr_a,1)) { |
289 | return 0; | 287 | return 0; |
290 | } | 288 | } |
291 | j++; | 289 | j++; |
292 | } | 290 | } |
293 | if (!equals(mr_a,mr_x1)) { | 291 | if (!equals(mr_a,mr_x1)) { |
294 | return 0; | 292 | return 0; |
295 | } | 293 | } |
296 | } | 294 | } |
297 | 295 | ||
298 | return 1; | 296 | return 1; |
299 | }, | 297 | }, |
300 | 298 | ||
301 | //returns how many bits long the bigInt is, not counting leading zeros. | 299 | //returns how many bits long the bigInt is, not counting leading zeros. |
302 | 'bitSize': function(x) { | 300 | 'bitSize': function(x) { |
303 | var j,z,w; | 301 | var j,z,w; |
304 | for (j=x.length-1; (x[j]==0) && (j>0); j--); | 302 | for (j=x.length-1; (x[j]==0) && (j>0); j--); |
305 | for (z=0,w=x[j]; w; (w>>=1),z++); | 303 | for (z=0,w=x[j]; w; (w>>=1),z++); |
306 | z+=bpe*j; | 304 | z+=bpe*j; |
307 | return z; | 305 | return z; |
308 | }, | 306 | }, |
309 | 307 | ||
310 | //return a copy of x with at least n elements, adding leading zeros if needed | 308 | //return a copy of x with at least n elements, adding leading zeros if needed |
311 | 'expand': function(x,n) { | 309 | 'expand': function(x,n) { |
312 | var ans=int2bigInt(0,(x.length>n ? x.length : n)*bpe,0); | 310 | var ans=int2bigInt(0,(x.length>n ? x.length : n)*bpe,0); |
313 | copy_(ans,x); | 311 | copy_(ans,x); |
314 | return ans; | 312 | return ans; |
315 | }, | 313 | }, |
316 | 314 | ||
317 | //return a k-bit true random prime using Maurer's algorithm. | 315 | //return a k-bit true random prime using Maurer's algorithm. |
318 | 'randTruePrime': function(k) { | 316 | 'randTruePrime': function(k) { |
319 | var ans=int2bigInt(0,k,0); | 317 | var ans=int2bigInt(0,k,0); |
320 | randTruePrime_(ans,k); | 318 | randTruePrime_(ans,k); |
321 | return trim(ans,1); | 319 | return trim(ans,1); |
322 | }, | 320 | }, |
323 | 321 | ||
324 | //return a new bigInt equal to (x mod n) for bigInts x and n. | 322 | //return a new bigInt equal to (x mod n) for bigInts x and n. |
325 | 'mod': function(x,n) { | 323 | 'mod': function(x,n) { |
326 | var ans=dup(x); | 324 | var ans=dup(x); |
327 | mod_(ans,n); | 325 | mod_(ans,n); |
328 | return trim(ans,1); | 326 | return trim(ans,1); |
329 | }, | 327 | }, |
330 | 328 | ||
331 | //return (x+n) where x is a bigInt and n is an integer. | 329 | //return (x+n) where x is a bigInt and n is an integer. |
332 | 'addInt': function(x,n) { | 330 | 'addInt': function(x,n) { |
333 | var ans=expand(x,x.length+1); | 331 | var ans=expand(x,x.length+1); |
334 | addInt_(ans,n); | 332 | addInt_(ans,n); |
335 | return trim(ans,1); | 333 | return trim(ans,1); |
336 | }, | 334 | }, |
337 | 335 | ||
338 | //return x*y for bigInts x and y. This is faster when y<x. | 336 | //return x*y for bigInts x and y. This is faster when y<x. |
339 | 'mult': function(x,y) { | 337 | 'mult': function(x,y) { |
340 | var ans=expand(x,x.length+y.length); | 338 | var ans=expand(x,x.length+y.length); |
341 | mult_(ans,y); | 339 | mult_(ans,y); |
342 | return trim(ans,1); | 340 | return trim(ans,1); |
343 | }, | 341 | }, |
344 | 342 | ||
345 | //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n. | 343 | //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n. |
346 | 'powMod': function(x,y,n) { | 344 | 'powMod': function(x,y,n) { |
347 | var ans=expand(x,n.length); | 345 | var ans=expand(x,n.length); |
348 | powMod_(ans,trim(y,2),trim(n,2),0); //this should work without the trim, but doesn't | 346 | powMod_(ans,trim(y,2),trim(n,2),0); //this should work without the trim, but doesn't |
349 | return trim(ans,1); | 347 | return trim(ans,1); |
350 | }, | 348 | }, |
351 | 349 | ||
352 | //return (x-y) for bigInts x and y. Negative answers will be 2s complement | 350 | //return (x-y) for bigInts x and y. Negative answers will be 2s complement |
353 | 'sub': function(x,y) { | 351 | 'sub': function(x,y) { |
354 | var ans=expand(x,(x.length>y.length ? x.length+1 : y.length+1)); | 352 | var ans=expand(x,(x.length>y.length ? x.length+1 : y.length+1)); |
355 | sub_(ans,y); | 353 | sub_(ans,y); |
356 | return trim(ans,1); | 354 | return trim(ans,1); |
357 | }, | 355 | }, |
358 | 356 | ||
359 | //return (x+y) for bigInts x and y. | 357 | //return (x+y) for bigInts x and y. |
360 | 'add': function(x,y) { | 358 | 'add': function(x,y) { |
361 | var ans=expand(x,(x.length>y.length ? x.length+1 : y.length+1)); | 359 | var ans=expand(x,(x.length>y.length ? x.length+1 : y.length+1)); |
362 | add_(ans,y); | 360 | add_(ans,y); |
363 | return trim(ans,1); | 361 | return trim(ans,1); |
364 | }, | 362 | }, |
365 | 363 | ||
366 | //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null | 364 | //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null |
367 | 'inverseMod': function(x,n) { | 365 | 'inverseMod': function(x,n) { |
368 | var ans=expand(x,n.length); | 366 | var ans=expand(x,n.length); |
369 | var s; | 367 | var s; |
370 | s=inverseMod_(ans,n); | 368 | s=inverseMod_(ans,n); |
371 | return s ? trim(ans,1) : null; | 369 | return s ? trim(ans,1) : null; |
372 | }, | 370 | }, |
373 | 371 | ||
374 | //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. | 372 | //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. |
375 | 'multMod': function(x,y,n) { | 373 | 'multMod': function(x,y,n) { |
376 | var ans=expand(x,n.length); | 374 | var ans=expand(x,n.length); |
377 | multMod_(ans,y,n); | 375 | multMod_(ans,y,n); |
378 | return trim(ans,1); | 376 | return trim(ans,1); |
379 | }, | 377 | }, |
380 | 378 | ||
381 | //generate a k-bit true random prime using Maurer's algorithm, | 379 | //generate a k-bit true random prime using Maurer's algorithm, |
382 | //and put it into ans. The bigInt ans must be large enough to hold it. | 380 | //and put it into ans. The bigInt ans must be large enough to hold it. |
383 | 'randTruePrime_': function(ans,k) { | 381 | 'randTruePrime_': function(ans,k) { |
384 | var c,m,pm,dd,j,r,B,divisible,z,zz,recSize; | 382 | var c,m,pm,dd,j,r,B,divisible,z,zz,recSize; |
385 | 383 | ||
386 | if (primes.length==0) | 384 | if (primes.length==0) |
387 | primes=findPrimes(30000); //check for divisibility by primes <=30000 | 385 | primes=findPrimes(30000); //check for divisibility by primes <=30000 |
388 | 386 | ||
389 | if (pows.length==0) { | 387 | if (pows.length==0) { |
390 | pows=new Array(512); | 388 | pows=new Array(512); |
391 | for (j=0;j<512;j++) { | 389 | for (j=0;j<512;j++) { |
392 | pows[j]=Math.pow(2,j/511.-1.); | 390 | pows[j]=Math.pow(2,j/511.-1.); |
393 | } | 391 | } |
394 | } | 392 | } |
395 | 393 | ||
396 | //c and m should be tuned for a particular machine and value of k, to maximize speed | 394 | //c and m should be tuned for a particular machine and value of k, to maximize speed |
397 | //this was: c=primes[primes.length-1]/k/k; //check using all the small primes. (c=0.1 in HAC) | 395 | //this was: c=primes[primes.length-1]/k/k; //check using all the small primes. (c=0.1 in HAC) |
398 | c=0.1; | 396 | c=0.1; |
399 | m=20; //generate this k-bit number by first recursively generating a number that has between k/2 and k-m bits | 397 | m=20; //generate this k-bit number by first recursively generating a number that has between k/2 and k-m bits |
400 | recLimit=20; /*must be at least 2 (was 29)*/ //stop recursion when k <=recLimit | 398 | recLimit=20; /*must be at least 2 (was 29)*/ //stop recursion when k <=recLimit |
401 | 399 | ||
402 | if (s_i2.length!=ans.length) { | 400 | if (s_i2.length!=ans.length) { |
403 | s_i2=dup(ans); | 401 | s_i2=dup(ans); |
404 | s_R =dup(ans); | 402 | s_R =dup(ans); |
405 | s_n1=dup(ans); | 403 | s_n1=dup(ans); |
406 | s_r2=dup(ans); | 404 | s_r2=dup(ans); |
diff --git a/frontend/beta/js/Clipperz/Crypto/ECC.js b/frontend/beta/js/Clipperz/Crypto/ECC.js index bdfd9be..74eb02f 100644 --- a/frontend/beta/js/Clipperz/Crypto/ECC.js +++ b/frontend/beta/js/Clipperz/Crypto/ECC.js | |||
@@ -1,406 +1,404 @@ | |||
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 | /* | 24 | /* |
27 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { | 25 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { |
28 | throw "Clipperz.Crypto.ECC depends on Clipperz.ByteArray!"; | 26 | throw "Clipperz.Crypto.ECC depends on Clipperz.ByteArray!"; |
29 | } | 27 | } |
30 | 28 | ||
31 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } | 29 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } |
32 | 30 | ||
33 | 31 | ||
34 | //############################################################################# | 32 | //############################################################################# |
35 | 33 | ||
36 | Clipperz.Crypto.ECC.BinaryField = {}; | 34 | Clipperz.Crypto.ECC.BinaryField = {}; |
37 | 35 | ||
38 | //############################################################################# | 36 | //############################################################################# |
39 | 37 | ||
40 | Clipperz.Crypto.ECC.BinaryField.AbstractValue = function(aValue, aBase) { | 38 | Clipperz.Crypto.ECC.BinaryField.AbstractValue = function(aValue, aBase) { |
41 | return this; | 39 | return this; |
42 | } | 40 | } |
43 | 41 | ||
44 | Clipperz.Crypto.ECC.BinaryField.AbstractValue.prototype = MochiKit.Base.update(null, { | 42 | Clipperz.Crypto.ECC.BinaryField.AbstractValue.prototype = MochiKit.Base.update(null, { |
45 | 43 | ||
46 | 'asString': function(aBase) { | 44 | 'asString': function(aBase) { |
47 | throw Clipperz.Base.exception.AbstractMethod; | 45 | throw Clipperz.Base.exception.AbstractMethod; |
48 | }, | 46 | }, |
49 | 47 | ||
50 | 'isZero': function() { | 48 | 'isZero': function() { |
51 | throw Clipperz.Base.exception.AbstractMethod; | 49 | throw Clipperz.Base.exception.AbstractMethod; |
52 | }, | 50 | }, |
53 | 51 | ||
54 | 'shiftLeft': function(aNumberOfBitsToShift) { | 52 | 'shiftLeft': function(aNumberOfBitsToShift) { |
55 | throw Clipperz.Base.exception.AbstractMethod; | 53 | throw Clipperz.Base.exception.AbstractMethod; |
56 | }, | 54 | }, |
57 | 55 | ||
58 | 'bitSize': function() { | 56 | 'bitSize': function() { |
59 | throw Clipperz.Base.exception.AbstractMethod; | 57 | throw Clipperz.Base.exception.AbstractMethod; |
60 | }, | 58 | }, |
61 | 59 | ||
62 | 'isBitSet': function(aBitPosition) { | 60 | 'isBitSet': function(aBitPosition) { |
63 | throw Clipperz.Base.exception.AbstractMethod; | 61 | throw Clipperz.Base.exception.AbstractMethod; |
64 | }, | 62 | }, |
65 | 63 | ||
66 | 'xor': function(aValue) { | 64 | 'xor': function(aValue) { |
67 | throw Clipperz.Base.exception.AbstractMethod; | 65 | throw Clipperz.Base.exception.AbstractMethod; |
68 | }, | 66 | }, |
69 | 67 | ||
70 | 'compare': function(aValue) { | 68 | 'compare': function(aValue) { |
71 | throw Clipperz.Base.exception.AbstractMethod; | 69 | throw Clipperz.Base.exception.AbstractMethod; |
72 | }, | 70 | }, |
73 | 71 | ||
74 | //----------------------------------------------------------------------------- | 72 | //----------------------------------------------------------------------------- |
75 | __syntaxFix__: "syntax fix" | 73 | __syntaxFix__: "syntax fix" |
76 | }); | 74 | }); |
77 | 75 | ||
78 | //***************************************************************************** | 76 | //***************************************************************************** |
79 | / * | 77 | / * |
80 | Clipperz.Crypto.ECC.BinaryField.BigIntValue = function(aValue, aBase) { | 78 | Clipperz.Crypto.ECC.BinaryField.BigIntValue = function(aValue, aBase) { |
81 | this._value = new Clipperz.Crypto.BigInt(aValue, aBase); | 79 | this._value = new Clipperz.Crypto.BigInt(aValue, aBase); |
82 | return this; | 80 | return this; |
83 | } | 81 | } |
84 | 82 | ||
85 | Clipperz.Crypto.ECC.BinaryField.BigIntValue.prototype = MochiKit.Base.update(new Clipperz.Crypto.ECC.BinaryField.AbstractValue(), { | 83 | Clipperz.Crypto.ECC.BinaryField.BigIntValue.prototype = MochiKit.Base.update(new Clipperz.Crypto.ECC.BinaryField.AbstractValue(), { |
86 | 84 | ||
87 | 'value': function() { | 85 | 'value': function() { |
88 | return this._value; | 86 | return this._value; |
89 | }, | 87 | }, |
90 | 88 | ||
91 | //----------------------------------------------------------------------------- | 89 | //----------------------------------------------------------------------------- |
92 | 90 | ||
93 | 'isZero': function() { | 91 | 'isZero': function() { |
94 | return (this.value().compare(Clipperz.Crypto.ECC.BinaryField.BigIntValue.O) == 0); | 92 | return (this.value().compare(Clipperz.Crypto.ECC.BinaryField.BigIntValue.O) == 0); |
95 | }, | 93 | }, |
96 | 94 | ||
97 | //----------------------------------------------------------------------------- | 95 | //----------------------------------------------------------------------------- |
98 | 96 | ||
99 | 'asString': function(aBase) { | 97 | 'asString': function(aBase) { |
100 | return this.value().asString(aBase); | 98 | return this.value().asString(aBase); |
101 | }, | 99 | }, |
102 | 100 | ||
103 | //----------------------------------------------------------------------------- | 101 | //----------------------------------------------------------------------------- |
104 | 102 | ||
105 | 'shiftLeft': function(aNumberOfBitsToShift) { | 103 | 'shiftLeft': function(aNumberOfBitsToShift) { |
106 | return new Clipperz.Crypto.ECC.BinaryField.BigIntValue(this.value().shiftLeft(aNumberOfBitsToShift)); | 104 | return new Clipperz.Crypto.ECC.BinaryField.BigIntValue(this.value().shiftLeft(aNumberOfBitsToShift)); |
107 | }, | 105 | }, |
108 | 106 | ||
109 | //----------------------------------------------------------------------------- | 107 | //----------------------------------------------------------------------------- |
110 | 108 | ||
111 | 'bitSize': function() { | 109 | 'bitSize': function() { |
112 | return this.value().bitSize(); | 110 | return this.value().bitSize(); |
113 | }, | 111 | }, |
114 | 112 | ||
115 | //----------------------------------------------------------------------------- | 113 | //----------------------------------------------------------------------------- |
116 | 114 | ||
117 | 'isBitSet': function(aBitPosition) { | 115 | 'isBitSet': function(aBitPosition) { |
118 | return this.value().isBitSet(aBitPosition); | 116 | return this.value().isBitSet(aBitPosition); |
119 | }, | 117 | }, |
120 | 118 | ||
121 | //----------------------------------------------------------------------------- | 119 | //----------------------------------------------------------------------------- |
122 | 120 | ||
123 | 'xor': function(aValue) { | 121 | 'xor': function(aValue) { |
124 | return new Clipperz.Crypto.ECC.BinaryField.BigIntValue(this.value().xor(aValue.value())); | 122 | return new Clipperz.Crypto.ECC.BinaryField.BigIntValue(this.value().xor(aValue.value())); |
125 | }, | 123 | }, |
126 | 124 | ||
127 | //----------------------------------------------------------------------------- | 125 | //----------------------------------------------------------------------------- |
128 | 126 | ||
129 | 'compare': function(aValue) { | 127 | 'compare': function(aValue) { |
130 | return this.value().compare(aValue.value()); | 128 | return this.value().compare(aValue.value()); |
131 | }, | 129 | }, |
132 | 130 | ||
133 | //----------------------------------------------------------------------------- | 131 | //----------------------------------------------------------------------------- |
134 | __syntaxFix__: "syntax fix" | 132 | __syntaxFix__: "syntax fix" |
135 | }); | 133 | }); |
136 | 134 | ||
137 | Clipperz.Crypto.ECC.BinaryField.BigIntValue.O = new Clipperz.Crypto.BigInt(0); | 135 | Clipperz.Crypto.ECC.BinaryField.BigIntValue.O = new Clipperz.Crypto.BigInt(0); |
138 | Clipperz.Crypto.ECC.BinaryField.BigIntValue.I = new Clipperz.Crypto.BigInt(1); | 136 | Clipperz.Crypto.ECC.BinaryField.BigIntValue.I = new Clipperz.Crypto.BigInt(1); |
139 | * / | 137 | * / |
140 | //***************************************************************************** | 138 | //***************************************************************************** |
141 | 139 | ||
142 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue = function(aValue, aBase) { | 140 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue = function(aValue, aBase) { |
143 | if (aValue.constructor == String) { | 141 | if (aValue.constructor == String) { |
144 | varvalue; | 142 | varvalue; |
145 | varstringLength; | 143 | varstringLength; |
146 | var numberOfWords; | 144 | var numberOfWords; |
147 | vari,c; | 145 | vari,c; |
148 | 146 | ||
149 | if (aBase != 16) { | 147 | if (aBase != 16) { |
150 | throw Clipperz.Crypto.ECC.BinaryField.WordArrayValue.exception.UnsupportedBase; | 148 | throw Clipperz.Crypto.ECC.BinaryField.WordArrayValue.exception.UnsupportedBase; |
151 | } | 149 | } |
152 | 150 | ||
153 | value = aValue.replace(/ /g, ''); | 151 | value = aValue.replace(/ /g, ''); |
154 | stringLength = value.length; | 152 | stringLength = value.length; |
155 | numberOfWords = Math.ceil(stringLength / 8); | 153 | numberOfWords = Math.ceil(stringLength / 8); |
156 | this._value = new Array(numberOfWords); | 154 | this._value = new Array(numberOfWords); |
157 | 155 | ||
158 | c = numberOfWords; | 156 | c = numberOfWords; |
159 | for (i=0; i<c; i++) { | 157 | for (i=0; i<c; i++) { |
160 | varword; | 158 | varword; |
161 | 159 | ||
162 | if (i < (c-1)) { | 160 | if (i < (c-1)) { |
163 | word = parseInt(value.substr(stringLength-((i+1)*8), 8), 16); | 161 | word = parseInt(value.substr(stringLength-((i+1)*8), 8), 16); |
164 | } else { | 162 | } else { |
165 | word = parseInt(value.substr(0, stringLength-(i*8)), 16); | 163 | word = parseInt(value.substr(0, stringLength-(i*8)), 16); |
166 | } | 164 | } |
167 | 165 | ||
168 | this._value[i] = word; | 166 | this._value[i] = word; |
169 | } | 167 | } |
170 | } else if (aValue.constructor == Array) { | 168 | } else if (aValue.constructor == Array) { |
171 | var itemsToCopy; | 169 | var itemsToCopy; |
172 | 170 | ||
173 | itemsToCopy = aValue.length; | 171 | itemsToCopy = aValue.length; |
174 | while (aValue[itemsToCopy - 1] == 0) { | 172 | while (aValue[itemsToCopy - 1] == 0) { |
175 | itemsToCopy --; | 173 | itemsToCopy --; |
176 | } | 174 | } |
177 | 175 | ||
178 | this._value = aValue.slice(0, itemsToCopy); | 176 | this._value = aValue.slice(0, itemsToCopy); |
179 | } else if (aValue.constructor == Number) { | 177 | } else if (aValue.constructor == Number) { |
180 | this._value = [aValue]; | 178 | this._value = [aValue]; |
181 | } else { | 179 | } else { |
182 | // throw Clipperz.Crypto.ECC.BinaryField.WordArrayValue.exception.UnsupportedConstructorValueType; | 180 | // throw Clipperz.Crypto.ECC.BinaryField.WordArrayValue.exception.UnsupportedConstructorValueType; |
183 | } | 181 | } |
184 | 182 | ||
185 | return this; | 183 | return this; |
186 | } | 184 | } |
187 | 185 | ||
188 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.prototype = MochiKit.Base.update(new Clipperz.Crypto.ECC.BinaryField.AbstractValue(), { | 186 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.prototype = MochiKit.Base.update(new Clipperz.Crypto.ECC.BinaryField.AbstractValue(), { |
189 | 187 | ||
190 | 'value': function() { | 188 | 'value': function() { |
191 | return this._value; | 189 | return this._value; |
192 | }, | 190 | }, |
193 | 191 | ||
194 | //----------------------------------------------------------------------------- | 192 | //----------------------------------------------------------------------------- |
195 | 193 | ||
196 | 'wordSize': function() { | 194 | 'wordSize': function() { |
197 | return this._value.length | 195 | return this._value.length |
198 | }, | 196 | }, |
199 | 197 | ||
200 | //----------------------------------------------------------------------------- | 198 | //----------------------------------------------------------------------------- |
201 | 199 | ||
202 | 'clone': function() { | 200 | 'clone': function() { |
203 | return new Clipperz.Crypto.ECC.BinaryField.WordArrayValue(this._value.slice(0)); | 201 | return new Clipperz.Crypto.ECC.BinaryField.WordArrayValue(this._value.slice(0)); |
204 | }, | 202 | }, |
205 | 203 | ||
206 | //----------------------------------------------------------------------------- | 204 | //----------------------------------------------------------------------------- |
207 | 205 | ||
208 | 'isZero': function() { | 206 | 'isZero': function() { |
209 | return (this.compare(Clipperz.Crypto.ECC.BinaryField.WordArrayValue.O) == 0); | 207 | return (this.compare(Clipperz.Crypto.ECC.BinaryField.WordArrayValue.O) == 0); |
210 | }, | 208 | }, |
211 | 209 | ||
212 | //----------------------------------------------------------------------------- | 210 | //----------------------------------------------------------------------------- |
213 | 211 | ||
214 | 'asString': function(aBase) { | 212 | 'asString': function(aBase) { |
215 | varresult; | 213 | varresult; |
216 | var i,c; | 214 | var i,c; |
217 | 215 | ||
218 | if (aBase != 16) { | 216 | if (aBase != 16) { |
219 | throw Clipperz.Crypto.ECC.BinaryField.WordArrayValue.exception.UnsupportedBase; | 217 | throw Clipperz.Crypto.ECC.BinaryField.WordArrayValue.exception.UnsupportedBase; |
220 | } | 218 | } |
221 | 219 | ||
222 | result = ""; | 220 | result = ""; |
223 | c = this.wordSize(); | 221 | c = this.wordSize(); |
224 | for (i=0; i<c; i++) { | 222 | for (i=0; i<c; i++) { |
225 | varwordAsString; | 223 | varwordAsString; |
226 | 224 | ||
227 | // wordAsString = ("00000000" + this.value()[i].toString(16)); | 225 | // wordAsString = ("00000000" + this.value()[i].toString(16)); |
228 | wordAsString = ("00000000" + this._value[i].toString(16)); | 226 | wordAsString = ("00000000" + this._value[i].toString(16)); |
229 | wordAsString = wordAsString.substring(wordAsString.length - 8); | 227 | wordAsString = wordAsString.substring(wordAsString.length - 8); |
230 | result = wordAsString + result; | 228 | result = wordAsString + result; |
231 | } | 229 | } |
232 | 230 | ||
233 | result = result.replace(/^(00)* SPACEs THAT SHOULD BE REMOVED TO FIX THIS REGEX /, ""); | 231 | result = result.replace(/^(00)* SPACEs THAT SHOULD BE REMOVED TO FIX THIS REGEX /, ""); |
234 | 232 | ||
235 | if (result == "") { | 233 | if (result == "") { |
236 | result = "0"; | 234 | result = "0"; |
237 | } | 235 | } |
238 | 236 | ||
239 | return result; | 237 | return result; |
240 | }, | 238 | }, |
241 | 239 | ||
242 | //----------------------------------------------------------------------------- | 240 | //----------------------------------------------------------------------------- |
243 | 241 | ||
244 | 'shiftLeft': function(aNumberOfBitsToShift) { | 242 | 'shiftLeft': function(aNumberOfBitsToShift) { |
245 | return new Clipperz.Crypto.ECC.BinaryField.WordArrayValue(Clipperz.Crypto.ECC.BinaryField.WordArrayValue.shiftLeft(this._value, aNumberOfBitsToShift)); | 243 | return new Clipperz.Crypto.ECC.BinaryField.WordArrayValue(Clipperz.Crypto.ECC.BinaryField.WordArrayValue.shiftLeft(this._value, aNumberOfBitsToShift)); |
246 | }, | 244 | }, |
247 | 245 | ||
248 | //----------------------------------------------------------------------------- | 246 | //----------------------------------------------------------------------------- |
249 | 247 | ||
250 | 'bitSize': function() { | 248 | 'bitSize': function() { |
251 | return Clipperz.Crypto.ECC.BinaryField.WordArrayValue.bitSize(this._value); | 249 | return Clipperz.Crypto.ECC.BinaryField.WordArrayValue.bitSize(this._value); |
252 | }, | 250 | }, |
253 | 251 | ||
254 | //----------------------------------------------------------------------------- | 252 | //----------------------------------------------------------------------------- |
255 | 253 | ||
256 | 'isBitSet': function(aBitPosition) { | 254 | 'isBitSet': function(aBitPosition) { |
257 | return Clipperz.Crypto.ECC.BinaryField.WordArrayValue.isBitSet(this._value, aBitPosition); | 255 | return Clipperz.Crypto.ECC.BinaryField.WordArrayValue.isBitSet(this._value, aBitPosition); |
258 | }, | 256 | }, |
259 | 257 | ||
260 | //----------------------------------------------------------------------------- | 258 | //----------------------------------------------------------------------------- |
261 | 259 | ||
262 | 'xor': function(aValue) { | 260 | 'xor': function(aValue) { |
263 | return new Clipperz.Crypto.ECC.BinaryField.WordArrayValue(Clipperz.Crypto.ECC.BinaryField.WordArrayValue.xor(this._value, aValue._value)); | 261 | return new Clipperz.Crypto.ECC.BinaryField.WordArrayValue(Clipperz.Crypto.ECC.BinaryField.WordArrayValue.xor(this._value, aValue._value)); |
264 | }, | 262 | }, |
265 | 263 | ||
266 | //----------------------------------------------------------------------------- | 264 | //----------------------------------------------------------------------------- |
267 | 265 | ||
268 | 'compare': function(aValue) { | 266 | 'compare': function(aValue) { |
269 | return Clipperz.Crypto.ECC.BinaryField.WordArrayValue.compare(this._value, aValue._value); | 267 | return Clipperz.Crypto.ECC.BinaryField.WordArrayValue.compare(this._value, aValue._value); |
270 | }, | 268 | }, |
271 | 269 | ||
272 | //----------------------------------------------------------------------------- | 270 | //----------------------------------------------------------------------------- |
273 | __syntaxFix__: "syntax fix" | 271 | __syntaxFix__: "syntax fix" |
274 | }); | 272 | }); |
275 | 273 | ||
276 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.O = new Clipperz.Crypto.ECC.BinaryField.WordArrayValue('0', 16); | 274 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.O = new Clipperz.Crypto.ECC.BinaryField.WordArrayValue('0', 16); |
277 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.I = new Clipperz.Crypto.ECC.BinaryField.WordArrayValue('1', 16); | 275 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.I = new Clipperz.Crypto.ECC.BinaryField.WordArrayValue('1', 16); |
278 | 276 | ||
279 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.xor = function(a, b) { | 277 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.xor = function(a, b) { |
280 | var result; | 278 | var result; |
281 | var resultSize; | 279 | var resultSize; |
282 | var i,c; | 280 | var i,c; |
283 | 281 | ||
284 | resultSize = Math.max(a.length, b.length); | 282 | resultSize = Math.max(a.length, b.length); |
285 | 283 | ||
286 | result = new Array(resultSize); | 284 | result = new Array(resultSize); |
287 | c = resultSize; | 285 | c = resultSize; |
288 | for (i=0; i<c; i++) { | 286 | for (i=0; i<c; i++) { |
289 | // resultValue[i] = (((this.value()[i] || 0) ^ (aValue.value()[i] || 0)) >>> 0); | 287 | // resultValue[i] = (((this.value()[i] || 0) ^ (aValue.value()[i] || 0)) >>> 0); |
290 | result[i] = (((a[i] || 0) ^ (b[i] || 0)) >>> 0); | 288 | result[i] = (((a[i] || 0) ^ (b[i] || 0)) >>> 0); |
291 | } | 289 | } |
292 | 290 | ||
293 | return result; | 291 | return result; |
294 | }; | 292 | }; |
295 | 293 | ||
296 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.shiftLeft = function(aWordArray, aNumberOfBitsToShift) { | 294 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.shiftLeft = function(aWordArray, aNumberOfBitsToShift) { |
297 | var numberOfWordsToShift; | 295 | var numberOfWordsToShift; |
298 | varnumberOfBitsToShift; | 296 | varnumberOfBitsToShift; |
299 | var result; | 297 | var result; |
300 | varoverflowValue; | 298 | varoverflowValue; |
301 | vari,c; | 299 | vari,c; |
302 | 300 | ||
303 | numberOfWordsToShift = Math.floor(aNumberOfBitsToShift / 32); | 301 | numberOfWordsToShift = Math.floor(aNumberOfBitsToShift / 32); |
304 | numberOfBitsToShift = aNumberOfBitsToShift % 32; | 302 | numberOfBitsToShift = aNumberOfBitsToShift % 32; |
305 | 303 | ||
306 | result = new Array(aWordArray.length + numberOfWordsToShift); | 304 | result = new Array(aWordArray.length + numberOfWordsToShift); |
307 | 305 | ||
308 | c = numberOfWordsToShift; | 306 | c = numberOfWordsToShift; |
309 | for (i=0; i<c; i++) { | 307 | for (i=0; i<c; i++) { |
310 | result[i] = 0; | 308 | result[i] = 0; |
311 | } | 309 | } |
312 | 310 | ||
313 | overflowValue = 0; | 311 | overflowValue = 0; |
314 | nextOverflowValue = 0; | 312 | nextOverflowValue = 0; |
315 | 313 | ||
316 | c = aWordArray.length; | 314 | c = aWordArray.length; |
317 | for (i=0; i<c; i++) { | 315 | for (i=0; i<c; i++) { |
318 | varvalue; | 316 | varvalue; |
319 | varresultWord; | 317 | varresultWord; |
320 | 318 | ||
321 | // value = this.value()[i]; | 319 | // value = this.value()[i]; |
322 | value = aWordArray[i]; | 320 | value = aWordArray[i]; |
323 | 321 | ||
324 | if (numberOfBitsToShift > 0) { | 322 | if (numberOfBitsToShift > 0) { |
325 | var nextOverflowValue; | 323 | var nextOverflowValue; |
326 | 324 | ||
327 | nextOverflowValue = (value >>> (32 - numberOfBitsToShift)); | 325 | nextOverflowValue = (value >>> (32 - numberOfBitsToShift)); |
328 | value = value & (0xffffffff >>> numberOfBitsToShift); | 326 | value = value & (0xffffffff >>> numberOfBitsToShift); |
329 | resultWord = (((value << numberOfBitsToShift) | overflowValue) >>> 0); | 327 | resultWord = (((value << numberOfBitsToShift) | overflowValue) >>> 0); |
330 | } else { | 328 | } else { |
331 | resultWord = value; | 329 | resultWord = value; |
332 | } | 330 | } |
333 | 331 | ||
334 | result[i+numberOfWordsToShift] = resultWord; | 332 | result[i+numberOfWordsToShift] = resultWord; |
335 | overflowValue = nextOverflowValue; | 333 | overflowValue = nextOverflowValue; |
336 | } | 334 | } |
337 | 335 | ||
338 | if (overflowValue != 0) { | 336 | if (overflowValue != 0) { |
339 | result[aWordArray.length + numberOfWordsToShift] = overflowValue; | 337 | result[aWordArray.length + numberOfWordsToShift] = overflowValue; |
340 | } | 338 | } |
341 | 339 | ||
342 | return result; | 340 | return result; |
343 | }; | 341 | }; |
344 | 342 | ||
345 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.bitSize = function(aWordArray) { | 343 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.bitSize = function(aWordArray) { |
346 | varresult; | 344 | varresult; |
347 | varnotNullElements; | 345 | varnotNullElements; |
348 | var mostValuableWord; | 346 | var mostValuableWord; |
349 | var matchingBitsInMostImportantWord; | 347 | var matchingBitsInMostImportantWord; |
350 | var mask; | 348 | var mask; |
351 | var i,c; | 349 | var i,c; |
352 | 350 | ||
353 | notNullElements = aWordArray.length; | 351 | notNullElements = aWordArray.length; |
354 | 352 | ||
355 | if ((aWordArray.length == 1) && (aWordArray[0] == 0)) { | 353 | if ((aWordArray.length == 1) && (aWordArray[0] == 0)) { |
356 | result = 0; | 354 | result = 0; |
357 | } else { | 355 | } else { |
358 | while((aWordArray[notNullElements - 1] == 0) && (notNullElements > 0)) { | 356 | while((aWordArray[notNullElements - 1] == 0) && (notNullElements > 0)) { |
359 | notNullElements --; | 357 | notNullElements --; |
360 | } | 358 | } |
361 | 359 | ||
362 | result = (notNullElements - 1) * 32; | 360 | result = (notNullElements - 1) * 32; |
363 | mostValuableWord = aWordArray[notNullElements - 1]; | 361 | mostValuableWord = aWordArray[notNullElements - 1]; |
364 | 362 | ||
365 | matchingBits = 32; | 363 | matchingBits = 32; |
366 | mask = 0x80000000; | 364 | mask = 0x80000000; |
367 | 365 | ||
368 | while ((matchingBits > 0) && ((mostValuableWord & mask) == 0)) { | 366 | while ((matchingBits > 0) && ((mostValuableWord & mask) == 0)) { |
369 | matchingBits --; | 367 | matchingBits --; |
370 | mask >>>= 1; | 368 | mask >>>= 1; |
371 | } | 369 | } |
372 | 370 | ||
373 | result += matchingBits; | 371 | result += matchingBits; |
374 | } | 372 | } |
375 | 373 | ||
376 | return result; | 374 | return result; |
377 | }; | 375 | }; |
378 | 376 | ||
379 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.isBitSet = function(aWordArray, aBitPosition) { | 377 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.isBitSet = function(aWordArray, aBitPosition) { |
380 | var result; | 378 | var result; |
381 | varbyteIndex; | 379 | varbyteIndex; |
382 | var bitIndexInSelectedByte; | 380 | var bitIndexInSelectedByte; |
383 | 381 | ||
384 | byteIndex = Math.floor(aBitPosition / 32); | 382 | byteIndex = Math.floor(aBitPosition / 32); |
385 | bitIndexInSelectedByte = aBitPosition % 32; | 383 | bitIndexInSelectedByte = aBitPosition % 32; |
386 | 384 | ||
387 | if (byteIndex <= aWordArray.length) { | 385 | if (byteIndex <= aWordArray.length) { |
388 | result = ((aWordArray[byteIndex] & (1 << bitIndexInSelectedByte)) != 0); | 386 | result = ((aWordArray[byteIndex] & (1 << bitIndexInSelectedByte)) != 0); |
389 | } else { | 387 | } else { |
390 | result = false; | 388 | result = false; |
391 | } | 389 | } |
392 | 390 | ||
393 | return result; | 391 | return result; |
394 | }; | 392 | }; |
395 | 393 | ||
396 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.compare = function(a,b) { | 394 | Clipperz.Crypto.ECC.BinaryField.WordArrayValue.compare = function(a,b) { |
397 | varresult; | 395 | varresult; |
398 | var i,c; | 396 | var i,c; |
399 | 397 | ||
400 | result = MochiKit.Base.compare(a.length, b.length); | 398 | result = MochiKit.Base.compare(a.length, b.length); |
401 | 399 | ||
402 | c = a.length; | 400 | c = a.length; |
403 | for (i=0; (i<c) && (result==0); i++) { | 401 | for (i=0; (i<c) && (result==0); i++) { |
404 | //console.log("compare[" + c + " - " + i + " - 1] " + this.value()[c-i-1] + ", " + aValue.value()[c-i-1]); | 402 | //console.log("compare[" + c + " - " + i + " - 1] " + this.value()[c-i-1] + ", " + aValue.value()[c-i-1]); |
405 | // result = MochiKit.Base.compare(this.value()[c-i-1], aValue.value()[c-i-1]); | 403 | // result = MochiKit.Base.compare(this.value()[c-i-1], aValue.value()[c-i-1]); |
406 | result = MochiKit.Base.compare(a[c-i-1], b[c-i-1]); | 404 | result = MochiKit.Base.compare(a[c-i-1], b[c-i-1]); |
diff --git a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Curve.js b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Curve.js index 01127c3..c39a075 100644 --- a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Curve.js +++ b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Curve.js | |||
@@ -1,406 +1,404 @@ | |||
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.ECC depends on Clipperz.ByteArray!"; | 25 | throw "Clipperz.Crypto.ECC depends on Clipperz.ByteArray!"; |
28 | } | 26 | } |
29 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } | 27 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } |
30 | if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; } | 28 | if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; } |
31 | 29 | ||
32 | Clipperz.Crypto.ECC.BinaryField.Curve = function(args) { | 30 | Clipperz.Crypto.ECC.BinaryField.Curve = function(args) { |
33 | args = args || {}; | 31 | args = args || {}; |
34 | 32 | ||
35 | this._modulus = args.modulus; | 33 | this._modulus = args.modulus; |
36 | 34 | ||
37 | this._a = args.a; | 35 | this._a = args.a; |
38 | this._b = args.b; | 36 | this._b = args.b; |
39 | this._G = args.G; | 37 | this._G = args.G; |
40 | this._r = args.r; | 38 | this._r = args.r; |
41 | this._h = args.h; | 39 | this._h = args.h; |
42 | 40 | ||
43 | this._finiteField = null; | 41 | this._finiteField = null; |
44 | 42 | ||
45 | return this; | 43 | return this; |
46 | } | 44 | } |
47 | 45 | ||
48 | Clipperz.Crypto.ECC.BinaryField.Curve.prototype = MochiKit.Base.update(null, { | 46 | Clipperz.Crypto.ECC.BinaryField.Curve.prototype = MochiKit.Base.update(null, { |
49 | 47 | ||
50 | 'asString': function() { | 48 | 'asString': function() { |
51 | return "Clipperz.Crypto.ECC.BinaryField.Curve"; | 49 | return "Clipperz.Crypto.ECC.BinaryField.Curve"; |
52 | }, | 50 | }, |
53 | 51 | ||
54 | //----------------------------------------------------------------------------- | 52 | //----------------------------------------------------------------------------- |
55 | 53 | ||
56 | 'modulus': function() { | 54 | 'modulus': function() { |
57 | return this._modulus; | 55 | return this._modulus; |
58 | }, | 56 | }, |
59 | 57 | ||
60 | 'a': function() { | 58 | 'a': function() { |
61 | return this._a; | 59 | return this._a; |
62 | }, | 60 | }, |
63 | 61 | ||
64 | 'b': function() { | 62 | 'b': function() { |
65 | return this._b; | 63 | return this._b; |
66 | }, | 64 | }, |
67 | 65 | ||
68 | 'G': function() { | 66 | 'G': function() { |
69 | return this._G; | 67 | return this._G; |
70 | }, | 68 | }, |
71 | 69 | ||
72 | 'r': function() { | 70 | 'r': function() { |
73 | return this._r; | 71 | return this._r; |
74 | }, | 72 | }, |
75 | 73 | ||
76 | 'h': function() { | 74 | 'h': function() { |
77 | return this._h; | 75 | return this._h; |
78 | }, | 76 | }, |
79 | 77 | ||
80 | //----------------------------------------------------------------------------- | 78 | //----------------------------------------------------------------------------- |
81 | 79 | ||
82 | 'finiteField': function() { | 80 | 'finiteField': function() { |
83 | if (this._finiteField == null) { | 81 | if (this._finiteField == null) { |
84 | this._finiteField = new Clipperz.Crypto.ECC.BinaryField.FiniteField({modulus:this.modulus()}) | 82 | this._finiteField = new Clipperz.Crypto.ECC.BinaryField.FiniteField({modulus:this.modulus()}) |
85 | } | 83 | } |
86 | 84 | ||
87 | return this._finiteField; | 85 | return this._finiteField; |
88 | }, | 86 | }, |
89 | 87 | ||
90 | //----------------------------------------------------------------------------- | 88 | //----------------------------------------------------------------------------- |
91 | 89 | ||
92 | 'negate': function(aPointA) { | 90 | 'negate': function(aPointA) { |
93 | var result; | 91 | var result; |
94 | 92 | ||
95 | result = new Clipperz.Crypto.ECC.Point({x:aPointA.x(), y:this.finiteField().add(aPointA.y(), aPointA.x())}) | 93 | result = new Clipperz.Crypto.ECC.Point({x:aPointA.x(), y:this.finiteField().add(aPointA.y(), aPointA.x())}) |
96 | 94 | ||
97 | return result; | 95 | return result; |
98 | }, | 96 | }, |
99 | 97 | ||
100 | //----------------------------------------------------------------------------- | 98 | //----------------------------------------------------------------------------- |
101 | 99 | ||
102 | 'add': function(aPointA, aPointB) { | 100 | 'add': function(aPointA, aPointB) { |
103 | var result; | 101 | var result; |
104 | 102 | ||
105 | //console.log(">>> ECC.BinaryField.Curve.add"); | 103 | //console.log(">>> ECC.BinaryField.Curve.add"); |
106 | if (aPointA.isZero()) { | 104 | if (aPointA.isZero()) { |
107 | //console.log("--- pointA == zero"); | 105 | //console.log("--- pointA == zero"); |
108 | result = aPointB; | 106 | result = aPointB; |
109 | } else if (aPointB.isZero()) { | 107 | } else if (aPointB.isZero()) { |
110 | //console.log("--- pointB == zero"); | 108 | //console.log("--- pointB == zero"); |
111 | result = aPointA; | 109 | result = aPointA; |
112 | } else if ((aPointA.x().compare(aPointB.x()) == 0) && ((aPointA.y().compare(aPointB.y()) != 0) || aPointB.x().isZero())) { | 110 | } else if ((aPointA.x().compare(aPointB.x()) == 0) && ((aPointA.y().compare(aPointB.y()) != 0) || aPointB.x().isZero())) { |
113 | //console.log("compare A.x - B.x: ", aPointA.x().compare(aPointB.x())); | 111 | //console.log("compare A.x - B.x: ", aPointA.x().compare(aPointB.x())); |
114 | //console.log("compare A.y - B.y: ", (aPointA.y().compare(aPointB.y()) != 0)); | 112 | //console.log("compare A.y - B.y: ", (aPointA.y().compare(aPointB.y()) != 0)); |
115 | //console.log("compare B.x.isZero(): ", aPointB.x().isZero()); | 113 | //console.log("compare B.x.isZero(): ", aPointB.x().isZero()); |
116 | 114 | ||
117 | //console.log("--- result = zero"); | 115 | //console.log("--- result = zero"); |
118 | result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O}); | 116 | result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O}); |
119 | } else { | 117 | } else { |
120 | //console.log("--- result = ELSE"); | 118 | //console.log("--- result = ELSE"); |
121 | varf2m; | 119 | varf2m; |
122 | var x, y; | 120 | var x, y; |
123 | var lambda; | 121 | var lambda; |
124 | var aX, aY, bX, bY; | 122 | var aX, aY, bX, bY; |
125 | 123 | ||
126 | aX = aPointA.x()._value; | 124 | aX = aPointA.x()._value; |
127 | aY = aPointA.y()._value; | 125 | aY = aPointA.y()._value; |
128 | bX = aPointB.x()._value; | 126 | bX = aPointB.x()._value; |
129 | bY = aPointB.y()._value; | 127 | bY = aPointB.y()._value; |
130 | 128 | ||
131 | f2m = this.finiteField(); | 129 | f2m = this.finiteField(); |
132 | 130 | ||
133 | if (aPointA.x().compare(aPointB.x()) != 0) { | 131 | if (aPointA.x().compare(aPointB.x()) != 0) { |
134 | //console.log(" a.x != b.x"); | 132 | //console.log(" a.x != b.x"); |
135 | lambda =f2m._fastMultiply( | 133 | lambda =f2m._fastMultiply( |
136 | f2m._add(aY, bY), | 134 | f2m._add(aY, bY), |
137 | f2m._inverse(f2m._add(aX, bX)) | 135 | f2m._inverse(f2m._add(aX, bX)) |
138 | ); | 136 | ); |
139 | x = f2m._add(this.a()._value, f2m._square(lambda)); | 137 | x = f2m._add(this.a()._value, f2m._square(lambda)); |
140 | f2m._overwriteAdd(x, lambda); | 138 | f2m._overwriteAdd(x, lambda); |
141 | f2m._overwriteAdd(x, aX); | 139 | f2m._overwriteAdd(x, aX); |
142 | f2m._overwriteAdd(x, bX); | 140 | f2m._overwriteAdd(x, bX); |
143 | } else { | 141 | } else { |
144 | //console.log(" a.x == b.x"); | 142 | //console.log(" a.x == b.x"); |
145 | lambda = f2m._add(bX, f2m._fastMultiply(bY, f2m._inverse(bX))); | 143 | lambda = f2m._add(bX, f2m._fastMultiply(bY, f2m._inverse(bX))); |
146 | //console.log(" lambda: " + lambda.asString(16)); | 144 | //console.log(" lambda: " + lambda.asString(16)); |
147 | x = f2m._add(this.a()._value, f2m._square(lambda)); | 145 | x = f2m._add(this.a()._value, f2m._square(lambda)); |
148 | //console.log(" x (step 1): " + x.asString(16)); | 146 | //console.log(" x (step 1): " + x.asString(16)); |
149 | f2m._overwriteAdd(x, lambda); | 147 | f2m._overwriteAdd(x, lambda); |
150 | //console.log(" x (step 2): " + x.asString(16)); | 148 | //console.log(" x (step 2): " + x.asString(16)); |
151 | } | 149 | } |
152 | 150 | ||
153 | y = f2m._fastMultiply(f2m._add(bX, x), lambda); | 151 | y = f2m._fastMultiply(f2m._add(bX, x), lambda); |
154 | //console.log(" y (step 1): " + y.asString(16)); | 152 | //console.log(" y (step 1): " + y.asString(16)); |
155 | f2m._overwriteAdd(y, x); | 153 | f2m._overwriteAdd(y, x); |
156 | //console.log(" y (step 2): " + y.asString(16)); | 154 | //console.log(" y (step 2): " + y.asString(16)); |
157 | f2m._overwriteAdd(y, bY); | 155 | f2m._overwriteAdd(y, bY); |
158 | //console.log(" y (step 3): " + y.asString(16)); | 156 | //console.log(" y (step 3): " + y.asString(16)); |
159 | 157 | ||
160 | result = new Clipperz.Crypto.ECC.BinaryField.Point({x:new Clipperz.Crypto.ECC.BinaryField.Value(x), y:new Clipperz.Crypto.ECC.BinaryField.Value(y)}) | 158 | result = new Clipperz.Crypto.ECC.BinaryField.Point({x:new Clipperz.Crypto.ECC.BinaryField.Value(x), y:new Clipperz.Crypto.ECC.BinaryField.Value(y)}) |
161 | } | 159 | } |
162 | //console.log("<<< ECC.BinaryField.Curve.add"); | 160 | //console.log("<<< ECC.BinaryField.Curve.add"); |
163 | 161 | ||
164 | return result; | 162 | return result; |
165 | }, | 163 | }, |
166 | 164 | ||
167 | //----------------------------------------------------------------------------- | 165 | //----------------------------------------------------------------------------- |
168 | 166 | ||
169 | 'overwriteAdd': function(aPointA, aPointB) { | 167 | 'overwriteAdd': function(aPointA, aPointB) { |
170 | if (aPointA.isZero()) { | 168 | if (aPointA.isZero()) { |
171 | // result = aPointB; | 169 | // result = aPointB; |
172 | aPointA._x._value = aPointB._x._value; | 170 | aPointA._x._value = aPointB._x._value; |
173 | aPointA._y._value = aPointB._y._value; | 171 | aPointA._y._value = aPointB._y._value; |
174 | } else if (aPointB.isZero()) { | 172 | } else if (aPointB.isZero()) { |
175 | // result = aPointA; | 173 | // result = aPointA; |
176 | } else if ((aPointA.x().compare(aPointB.x()) == 0) && ((aPointA.y().compare(aPointB.y()) != 0) || aPointB.x().isZero())) { | 174 | } else if ((aPointA.x().compare(aPointB.x()) == 0) && ((aPointA.y().compare(aPointB.y()) != 0) || aPointB.x().isZero())) { |
177 | // result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O}); | 175 | // result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O}); |
178 | aPointA._x = Clipperz.Crypto.ECC.BinaryField.Value.O; | 176 | aPointA._x = Clipperz.Crypto.ECC.BinaryField.Value.O; |
179 | aPointA._y = Clipperz.Crypto.ECC.BinaryField.Value.O; | 177 | aPointA._y = Clipperz.Crypto.ECC.BinaryField.Value.O; |
180 | } else { | 178 | } else { |
181 | varf2m; | 179 | varf2m; |
182 | var x, y; | 180 | var x, y; |
183 | var lambda; | 181 | var lambda; |
184 | var aX, aY, bX, bY; | 182 | var aX, aY, bX, bY; |
185 | 183 | ||
186 | aX = aPointA.x()._value; | 184 | aX = aPointA.x()._value; |
187 | aY = aPointA.y()._value; | 185 | aY = aPointA.y()._value; |
188 | bX = aPointB.x()._value; | 186 | bX = aPointB.x()._value; |
189 | bY = aPointB.y()._value; | 187 | bY = aPointB.y()._value; |
190 | 188 | ||
191 | f2m = this.finiteField(); | 189 | f2m = this.finiteField(); |
192 | 190 | ||
193 | if (aPointA.x().compare(aPointB.x()) != 0) { | 191 | if (aPointA.x().compare(aPointB.x()) != 0) { |
194 | //console.log(" a.x != b.x"); | 192 | //console.log(" a.x != b.x"); |
195 | lambda =f2m._fastMultiply( | 193 | lambda =f2m._fastMultiply( |
196 | f2m._add(aY, bY), | 194 | f2m._add(aY, bY), |
197 | f2m._inverse(f2m._add(aX, bX)) | 195 | f2m._inverse(f2m._add(aX, bX)) |
198 | ); | 196 | ); |
199 | x = f2m._add(this.a()._value, f2m._square(lambda)); | 197 | x = f2m._add(this.a()._value, f2m._square(lambda)); |
200 | f2m._overwriteAdd(x, lambda); | 198 | f2m._overwriteAdd(x, lambda); |
201 | f2m._overwriteAdd(x, aX); | 199 | f2m._overwriteAdd(x, aX); |
202 | f2m._overwriteAdd(x, bX); | 200 | f2m._overwriteAdd(x, bX); |
203 | } else { | 201 | } else { |
204 | //console.log(" a.x == b.x"); | 202 | //console.log(" a.x == b.x"); |
205 | lambda = f2m._add(bX, f2m._fastMultiply(bY, f2m._inverse(bX))); | 203 | lambda = f2m._add(bX, f2m._fastMultiply(bY, f2m._inverse(bX))); |
206 | //console.log(" lambda: " + lambda.asString(16)); | 204 | //console.log(" lambda: " + lambda.asString(16)); |
207 | x = f2m._add(this.a()._value, f2m._square(lambda)); | 205 | x = f2m._add(this.a()._value, f2m._square(lambda)); |
208 | //console.log(" x (step 1): " + x.asString(16)); | 206 | //console.log(" x (step 1): " + x.asString(16)); |
209 | f2m._overwriteAdd(x, lambda); | 207 | f2m._overwriteAdd(x, lambda); |
210 | //console.log(" x (step 2): " + x.asString(16)); | 208 | //console.log(" x (step 2): " + x.asString(16)); |
211 | } | 209 | } |
212 | 210 | ||
213 | y = f2m._fastMultiply(f2m._add(bX, x), lambda); | 211 | y = f2m._fastMultiply(f2m._add(bX, x), lambda); |
214 | //console.log(" y (step 1): " + y.asString(16)); | 212 | //console.log(" y (step 1): " + y.asString(16)); |
215 | f2m._overwriteAdd(y, x); | 213 | f2m._overwriteAdd(y, x); |
216 | //console.log(" y (step 2): " + y.asString(16)); | 214 | //console.log(" y (step 2): " + y.asString(16)); |
217 | f2m._overwriteAdd(y, bY); | 215 | f2m._overwriteAdd(y, bY); |
218 | //console.log(" y (step 3): " + y.asString(16)); | 216 | //console.log(" y (step 3): " + y.asString(16)); |
219 | 217 | ||
220 | // result = new Clipperz.Crypto.ECC.BinaryField.Point({x:new Clipperz.Crypto.ECC.BinaryField.Value(x), y:new Clipperz.Crypto.ECC.BinaryField.Value(y)}) | 218 | // result = new Clipperz.Crypto.ECC.BinaryField.Point({x:new Clipperz.Crypto.ECC.BinaryField.Value(x), y:new Clipperz.Crypto.ECC.BinaryField.Value(y)}) |
221 | aPointA._x._value = x; | 219 | aPointA._x._value = x; |
222 | aPointA._y._value = y; | 220 | aPointA._y._value = y; |
223 | 221 | ||
224 | } | 222 | } |
225 | //console.log("<<< ECC.BinaryField.Curve.add"); | 223 | //console.log("<<< ECC.BinaryField.Curve.add"); |
226 | 224 | ||
227 | return result; | 225 | return result; |
228 | }, | 226 | }, |
229 | 227 | ||
230 | //----------------------------------------------------------------------------- | 228 | //----------------------------------------------------------------------------- |
231 | 229 | ||
232 | 'multiply': function(aValue, aPoint) { | 230 | 'multiply': function(aValue, aPoint) { |
233 | var result; | 231 | var result; |
234 | 232 | ||
235 | //console.profile(); | 233 | //console.profile(); |
236 | result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O}); | 234 | result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O}); |
237 | 235 | ||
238 | if (aValue.isZero() == false) { | 236 | if (aValue.isZero() == false) { |
239 | var k, Q; | 237 | var k, Q; |
240 | var i; | 238 | var i; |
241 | var countIndex; countIndex = 0; | 239 | var countIndex; countIndex = 0; |
242 | 240 | ||
243 | if (aValue.compare(Clipperz.Crypto.ECC.BinaryField.Value.O) > 0) { | 241 | if (aValue.compare(Clipperz.Crypto.ECC.BinaryField.Value.O) > 0) { |
244 | k = aValue; | 242 | k = aValue; |
245 | Q = aPoint; | 243 | Q = aPoint; |
246 | } else { | 244 | } else { |
247 | MochiKit.Logging.logError("The Clipperz.Crypto.ECC.BinaryFields.Value does not work with negative values!!!!"); | 245 | MochiKit.Logging.logError("The Clipperz.Crypto.ECC.BinaryFields.Value does not work with negative values!!!!"); |
248 | k = aValue.negate(); | 246 | k = aValue.negate(); |
249 | Q = this.negate(aPoint); | 247 | Q = this.negate(aPoint); |
250 | } | 248 | } |
251 | 249 | ||
252 | //console.log("k: " + k.toString(16)); | 250 | //console.log("k: " + k.toString(16)); |
253 | //console.log("k.bitSize: " + k.bitSize()); | 251 | //console.log("k.bitSize: " + k.bitSize()); |
254 | for (i=k.bitSize()-1; i>=0; i--) { | 252 | for (i=k.bitSize()-1; i>=0; i--) { |
255 | result = this.add(result, result); | 253 | result = this.add(result, result); |
256 | // this.overwriteAdd(result, result); | 254 | // this.overwriteAdd(result, result); |
257 | if (k.isBitSet(i)) { | 255 | if (k.isBitSet(i)) { |
258 | result = this.add(result, Q); | 256 | result = this.add(result, Q); |
259 | // this.overwriteAdd(result, Q); | 257 | // this.overwriteAdd(result, Q); |
260 | } | 258 | } |
261 | 259 | ||
262 | // if (countIndex==100) {console.log("multiply.break"); break;} else countIndex++; | 260 | // if (countIndex==100) {console.log("multiply.break"); break;} else countIndex++; |
263 | } | 261 | } |
264 | } | 262 | } |
265 | //console.profileEnd(); | 263 | //console.profileEnd(); |
266 | 264 | ||
267 | return result; | 265 | return result; |
268 | }, | 266 | }, |
269 | 267 | ||
270 | //----------------------------------------------------------------------------- | 268 | //----------------------------------------------------------------------------- |
271 | __syntaxFix__: "syntax fix" | 269 | __syntaxFix__: "syntax fix" |
272 | }); | 270 | }); |
273 | 271 | ||
274 | 272 | ||
275 | //############################################################################# | 273 | //############################################################################# |
276 | 274 | ||
277 | Clipperz.Crypto.ECC.StandardCurves = {}; | 275 | Clipperz.Crypto.ECC.StandardCurves = {}; |
278 | 276 | ||
279 | MochiKit.Base.update(Clipperz.Crypto.ECC.StandardCurves, { | 277 | MochiKit.Base.update(Clipperz.Crypto.ECC.StandardCurves, { |
280 | /* | 278 | /* |
281 | '_K571': null, | 279 | '_K571': null, |
282 | 'K571': function() { | 280 | 'K571': function() { |
283 | if (Clipperz.Crypto.ECC.StandardCurves._K571 == null) { | 281 | if (Clipperz.Crypto.ECC.StandardCurves._K571 == null) { |
284 | Clipperz.Crypto.ECC.StandardCurves._K571 = new Clipperz.Crypto.ECC.Curve.Koblitz({ | 282 | Clipperz.Crypto.ECC.StandardCurves._K571 = new Clipperz.Crypto.ECC.Curve.Koblitz({ |
285 | exadecimalForm: '80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425', | 283 | exadecimalForm: '80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425', |
286 | a: new Clipperz.Crypto.BigInt(0), | 284 | a: new Clipperz.Crypto.BigInt(0), |
287 | G: new Clipperz.Crypto.ECC.Point({ | 285 | G: new Clipperz.Crypto.ECC.Point({ |
288 | x: new Clipperz.Crypto.BigInt('26eb7a859923fbc82189631f8103fe4ac9ca2970012d5d46024804801841ca44370958493b205e647da304db4ceb08cbbd1ba39494776fb988b47174dca88c7e2945283a01c8972', 16), | 286 | x: new Clipperz.Crypto.BigInt('26eb7a859923fbc82189631f8103fe4ac9ca2970012d5d46024804801841ca44370958493b205e647da304db4ceb08cbbd1ba39494776fb988b47174dca88c7e2945283a01c8972', 16), |
289 | y: new Clipperz.Crypto.BigInt('349dc807f4fbf374f4aeade3bca95314dd58cec9f307a54ffc61efc006d8a2c9d4979c0ac44aea74fbebbb9f772aedcb620b01a7ba7af1b320430c8591984f601cd4c143ef1c7a3', 16) | 287 | y: new Clipperz.Crypto.BigInt('349dc807f4fbf374f4aeade3bca95314dd58cec9f307a54ffc61efc006d8a2c9d4979c0ac44aea74fbebbb9f772aedcb620b01a7ba7af1b320430c8591984f601cd4c143ef1c7a3', 16) |
290 | }), | 288 | }), |
291 | n: new Clipperz.Crypto.BigInt('1932268761508629172347675945465993672149463664853217499328617625725759571144780212268133978522706711834706712800825351461273674974066617311929682421617092503555733685276673', 16), | 289 | n: new Clipperz.Crypto.BigInt('1932268761508629172347675945465993672149463664853217499328617625725759571144780212268133978522706711834706712800825351461273674974066617311929682421617092503555733685276673', 16), |
292 | h: new Clipperz.Crypto.BigInt(4) | 290 | h: new Clipperz.Crypto.BigInt(4) |
293 | }); | 291 | }); |
294 | } | 292 | } |
295 | 293 | ||
296 | return Clipperz.Crypto.ECC.StandardCurves._K571; | 294 | return Clipperz.Crypto.ECC.StandardCurves._K571; |
297 | }, | 295 | }, |
298 | */ | 296 | */ |
299 | //----------------------------------------------------------------------------- | 297 | //----------------------------------------------------------------------------- |
300 | 298 | ||
301 | '_B571': null, | 299 | '_B571': null, |
302 | 'B571': function() { //f(z) = z^571 + z^10 + z^5 + z^2 + 1 | 300 | 'B571': function() { //f(z) = z^571 + z^10 + z^5 + z^2 + 1 |
303 | if (Clipperz.Crypto.ECC.StandardCurves._B571 == null) { | 301 | if (Clipperz.Crypto.ECC.StandardCurves._B571 == null) { |
304 | Clipperz.Crypto.ECC.StandardCurves._B571 = new Clipperz.Crypto.ECC.BinaryField.Curve({ | 302 | Clipperz.Crypto.ECC.StandardCurves._B571 = new Clipperz.Crypto.ECC.BinaryField.Curve({ |
305 | modulus: new Clipperz.Crypto.ECC.BinaryField.Value('80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425', 16), | 303 | modulus: new Clipperz.Crypto.ECC.BinaryField.Value('80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425', 16), |
306 | a: new Clipperz.Crypto.ECC.BinaryField.Value('1', 16), | 304 | a: new Clipperz.Crypto.ECC.BinaryField.Value('1', 16), |
307 | b: new Clipperz.Crypto.ECC.BinaryField.Value('02f40e7e2221f295de297117b7f3d62f5c6a97ffcb8ceff1cd6ba8ce4a9a18ad84ffabbd8efa59332be7ad6756a66e294afd185a78ff12aa520e4de739baca0c7ffeff7f2955727a', 16), | 305 | b: new Clipperz.Crypto.ECC.BinaryField.Value('02f40e7e2221f295de297117b7f3d62f5c6a97ffcb8ceff1cd6ba8ce4a9a18ad84ffabbd8efa59332be7ad6756a66e294afd185a78ff12aa520e4de739baca0c7ffeff7f2955727a', 16), |
308 | G: new Clipperz.Crypto.ECC.BinaryField.Point({ | 306 | G: new Clipperz.Crypto.ECC.BinaryField.Point({ |
309 | x: new Clipperz.Crypto.ECC.BinaryField.Value('0303001d 34b85629 6c16c0d4 0d3cd775 0a93d1d2 955fa80a a5f40fc8 db7b2abd bde53950 f4c0d293 cdd711a3 5b67fb14 99ae6003 8614f139 4abfa3b4 c850d927 e1e7769c 8eec2d19', 16), | 307 | x: new Clipperz.Crypto.ECC.BinaryField.Value('0303001d 34b85629 6c16c0d4 0d3cd775 0a93d1d2 955fa80a a5f40fc8 db7b2abd bde53950 f4c0d293 cdd711a3 5b67fb14 99ae6003 8614f139 4abfa3b4 c850d927 e1e7769c 8eec2d19', 16), |
310 | y: new Clipperz.Crypto.ECC.BinaryField.Value('037bf273 42da639b 6dccfffe b73d69d7 8c6c27a6 009cbbca 1980f853 3921e8a6 84423e43 bab08a57 6291af8f 461bb2a8 b3531d2f 0485c19b 16e2f151 6e23dd3c 1a4827af 1b8ac15b', 16) | 308 | y: new Clipperz.Crypto.ECC.BinaryField.Value('037bf273 42da639b 6dccfffe b73d69d7 8c6c27a6 009cbbca 1980f853 3921e8a6 84423e43 bab08a57 6291af8f 461bb2a8 b3531d2f 0485c19b 16e2f151 6e23dd3c 1a4827af 1b8ac15b', 16) |
311 | }), | 309 | }), |
312 | r: new Clipperz.Crypto.ECC.BinaryField.Value('03ffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff e661ce18 ff559873 08059b18 6823851e c7dd9ca1 161de93d 5174d66e 8382e9bb 2fe84e47', 16), | 310 | r: new Clipperz.Crypto.ECC.BinaryField.Value('03ffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff e661ce18 ff559873 08059b18 6823851e c7dd9ca1 161de93d 5174d66e 8382e9bb 2fe84e47', 16), |
313 | h: new Clipperz.Crypto.ECC.BinaryField.Value('2', 16) | 311 | h: new Clipperz.Crypto.ECC.BinaryField.Value('2', 16) |
314 | 312 | ||
315 | // S: new Clipperz.Crypto.ECC.BinaryField.Value('2aa058f73a0e33ab486b0f610410c53a7f132310', 10), | 313 | // S: new Clipperz.Crypto.ECC.BinaryField.Value('2aa058f73a0e33ab486b0f610410c53a7f132310', 10), |
316 | // n: new Clipperz.Crypto.ECC.BinaryField.Value('03ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe661ce18ff55987308059b186823851ec7dd9ca1161de93d5174d66e8382e9bb2fe84e47', 16), | 314 | // n: new Clipperz.Crypto.ECC.BinaryField.Value('03ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe661ce18ff55987308059b186823851ec7dd9ca1161de93d5174d66e8382e9bb2fe84e47', 16), |
317 | }); | 315 | }); |
318 | 316 | ||
319 | //----------------------------------------------------------------------------- | 317 | //----------------------------------------------------------------------------- |
320 | // | 318 | // |
321 | //Guide to Elliptic Curve Cryptography | 319 | //Guide to Elliptic Curve Cryptography |
322 | //Darrel Hankerson, Alfred Menezes, Scott Vanstone | 320 | //Darrel Hankerson, Alfred Menezes, Scott Vanstone |
323 | //- Pag: 56, Alorithm 2.45 (with a typo!!!) | 321 | //- Pag: 56, Alorithm 2.45 (with a typo!!!) |
324 | // | 322 | // |
325 | //----------------------------------------------------------------------------- | 323 | //----------------------------------------------------------------------------- |
326 | // | 324 | // |
327 | // http://www.milw0rm.com/papers/136 | 325 | // http://www.milw0rm.com/papers/136 |
328 | // | 326 | // |
329 | // ------------------------------------------------------------------------- | 327 | // ------------------------------------------------------------------------- |
330 | // Polynomial Reduction Algorithm Modulo f571 | 328 | // Polynomial Reduction Algorithm Modulo f571 |
331 | // ------------------------------------------------------------------------- | 329 | // ------------------------------------------------------------------------- |
332 | // | 330 | // |
333 | // Input: Polynomial p(x) of degree 1140 or less, stored as | 331 | // Input: Polynomial p(x) of degree 1140 or less, stored as |
334 | // an array of 2T machinewords. | 332 | // an array of 2T machinewords. |
335 | // Output: p(x) mod f571(x) | 333 | // Output: p(x) mod f571(x) |
336 | // | 334 | // |
337 | // FOR i = T-1, ..., 0 DO | 335 | // FOR i = T-1, ..., 0 DO |
338 | // SET X := P[i+T] | 336 | // SET X := P[i+T] |
339 | // P[i] := P[i] ^ (X<<5) ^ (X<<7) ^ (X<<10) ^ (X<<15) | 337 | // P[i] := P[i] ^ (X<<5) ^ (X<<7) ^ (X<<10) ^ (X<<15) |
340 | // P[i+1] := P[i+1] ^ (X>>17) ^ (X>>22) ^ (X>>25) ^ (X>>27) | 338 | // P[i+1] := P[i+1] ^ (X>>17) ^ (X>>22) ^ (X>>25) ^ (X>>27) |
341 | // | 339 | // |
342 | // SET X := P[T-1] >> 27 | 340 | // SET X := P[T-1] >> 27 |
343 | // P[0] := P[0] ^ X ^ (X<<2) ^ (X<<5) ^ (X<<10) | 341 | // P[0] := P[0] ^ X ^ (X<<2) ^ (X<<5) ^ (X<<10) |
344 | // P[T-1] := P[T-1] & 0x07ffffff | 342 | // P[T-1] := P[T-1] & 0x07ffffff |
345 | // | 343 | // |
346 | // RETURN P[T-1],...,P[0] | 344 | // RETURN P[T-1],...,P[0] |
347 | // | 345 | // |
348 | // ------------------------------------------------------------------------- | 346 | // ------------------------------------------------------------------------- |
349 | // | 347 | // |
350 | Clipperz.Crypto.ECC.StandardCurves._B571.finiteField().slowModule = Clipperz.Crypto.ECC.StandardCurves._B571.finiteField().module; | 348 | Clipperz.Crypto.ECC.StandardCurves._B571.finiteField().slowModule = Clipperz.Crypto.ECC.StandardCurves._B571.finiteField().module; |
351 | Clipperz.Crypto.ECC.StandardCurves._B571.finiteField().module = function(aValue) { | 349 | Clipperz.Crypto.ECC.StandardCurves._B571.finiteField().module = function(aValue) { |
352 | varresult; | 350 | varresult; |
353 | 351 | ||
354 | if (aValue.bitSize() > 1140) { | 352 | if (aValue.bitSize() > 1140) { |
355 | MochiKit.Logging.logWarning("ECC.StandarCurves.B571.finiteField().module: falling back to default implementation"); | 353 | MochiKit.Logging.logWarning("ECC.StandarCurves.B571.finiteField().module: falling back to default implementation"); |
356 | result = Clipperz.Crypto.ECC.StandardCurves._B571.finiteField().slowModule(aValue); | 354 | result = Clipperz.Crypto.ECC.StandardCurves._B571.finiteField().slowModule(aValue); |
357 | } else { | 355 | } else { |
358 | varC, T; | 356 | varC, T; |
359 | var i; | 357 | var i; |
360 | 358 | ||
361 | //console.log(">>> binaryField.finiteField.(improved)module"); | 359 | //console.log(">>> binaryField.finiteField.(improved)module"); |
362 | // C = aValue.value().slice(0); | 360 | // C = aValue.value().slice(0); |
363 | C = aValue._value.slice(0); | 361 | C = aValue._value.slice(0); |
364 | for (i=35; i>=18; i--) { | 362 | for (i=35; i>=18; i--) { |
365 | T = C[i]; | 363 | T = C[i]; |
366 | C[i-18] = (((C[i-18] ^ (T<<5) ^ (T<<7) ^ (T<<10) ^ (T<<15)) & 0xffffffff) >>> 0); | 364 | C[i-18] = (((C[i-18] ^ (T<<5) ^ (T<<7) ^ (T<<10) ^ (T<<15)) & 0xffffffff) >>> 0); |
367 | C[i-17] = ((C[i-17] ^ (T>>>27) ^ (T>>>25) ^ (T>>>22) ^ (T>>>17)) >>> 0); | 365 | C[i-17] = ((C[i-17] ^ (T>>>27) ^ (T>>>25) ^ (T>>>22) ^ (T>>>17)) >>> 0); |
368 | } | 366 | } |
369 | T = (C[17] >>> 27); | 367 | T = (C[17] >>> 27); |
370 | C[0] = ((C[0] ^ T ^ ((T<<2) ^ (T<<5) ^ (T<<10)) & 0xffffffff) >>> 0); | 368 | C[0] = ((C[0] ^ T ^ ((T<<2) ^ (T<<5) ^ (T<<10)) & 0xffffffff) >>> 0); |
371 | C[17] = (C[17] & 0x07ffffff); | 369 | C[17] = (C[17] & 0x07ffffff); |
372 | 370 | ||
373 | for(i=18; i<=35; i++) { | 371 | for(i=18; i<=35; i++) { |
374 | C[i] = 0; | 372 | C[i] = 0; |
375 | } | 373 | } |
376 | 374 | ||
377 | result = new Clipperz.Crypto.ECC.BinaryField.Value(C); | 375 | result = new Clipperz.Crypto.ECC.BinaryField.Value(C); |
378 | //console.log("<<< binaryField.finiteField.(improved)module"); | 376 | //console.log("<<< binaryField.finiteField.(improved)module"); |
379 | } | 377 | } |
380 | 378 | ||
381 | return result; | 379 | return result; |
382 | }; | 380 | }; |
383 | } | 381 | } |
384 | 382 | ||
385 | return Clipperz.Crypto.ECC.StandardCurves._B571; | 383 | return Clipperz.Crypto.ECC.StandardCurves._B571; |
386 | }, | 384 | }, |
387 | 385 | ||
388 | //----------------------------------------------------------------------------- | 386 | //----------------------------------------------------------------------------- |
389 | 387 | ||
390 | '_B283': null, | 388 | '_B283': null, |
391 | 'B283': function() { //f(z) = z^283 + z^12 + z^7 + z^5 + 1 | 389 | 'B283': function() { //f(z) = z^283 + z^12 + z^7 + z^5 + 1 |
392 | if (Clipperz.Crypto.ECC.StandardCurves._B283 == null) { | 390 | if (Clipperz.Crypto.ECC.StandardCurves._B283 == null) { |
393 | Clipperz.Crypto.ECC.StandardCurves._B283 = new Clipperz.Crypto.ECC.BinaryField.Curve({ | 391 | Clipperz.Crypto.ECC.StandardCurves._B283 = new Clipperz.Crypto.ECC.BinaryField.Curve({ |
394 | // modulus: new Clipperz.Crypto.ECC.BinaryField.Value('10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 000010a1', 16), | 392 | // modulus: new Clipperz.Crypto.ECC.BinaryField.Value('10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 000010a1', 16), |
395 | modulus: new Clipperz.Crypto.ECC.BinaryField.Value('08000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 000010a1', 16), | 393 | modulus: new Clipperz.Crypto.ECC.BinaryField.Value('08000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 000010a1', 16), |
396 | a: new Clipperz.Crypto.ECC.BinaryField.Value('1', 16), | 394 | a: new Clipperz.Crypto.ECC.BinaryField.Value('1', 16), |
397 | b: new Clipperz.Crypto.ECC.BinaryField.Value('027b680a c8b8596d a5a4af8a 19a0303f ca97fd76 45309fa2 a581485a f6263e31 3b79a2f5', 16), | 395 | b: new Clipperz.Crypto.ECC.BinaryField.Value('027b680a c8b8596d a5a4af8a 19a0303f ca97fd76 45309fa2 a581485a f6263e31 3b79a2f5', 16), |
398 | G: new Clipperz.Crypto.ECC.BinaryField.Point({ | 396 | G: new Clipperz.Crypto.ECC.BinaryField.Point({ |
399 | x: new Clipperz.Crypto.ECC.BinaryField.Value('05f93925 8db7dd90 e1934f8c 70b0dfec 2eed25b8 557eac9c 80e2e198 f8cdbecd 86b12053', 16), | 397 | x: new Clipperz.Crypto.ECC.BinaryField.Value('05f93925 8db7dd90 e1934f8c 70b0dfec 2eed25b8 557eac9c 80e2e198 f8cdbecd 86b12053', 16), |
400 | y: new Clipperz.Crypto.ECC.BinaryField.Value('03676854 fe24141c b98fe6d4 b20d02b4 516ff702 350eddb0 826779c8 13f0df45 be8112f4', 16) | 398 | y: new Clipperz.Crypto.ECC.BinaryField.Value('03676854 fe24141c b98fe6d4 b20d02b4 516ff702 350eddb0 826779c8 13f0df45 be8112f4', 16) |
401 | }), | 399 | }), |
402 | r: new Clipperz.Crypto.ECC.BinaryField.Value('03ffffff ffffffff ffffffff ffffffff ffffef90 399660fc 938a9016 5b042a7c efadb307', 16), | 400 | r: new Clipperz.Crypto.ECC.BinaryField.Value('03ffffff ffffffff ffffffff ffffffff ffffef90 399660fc 938a9016 5b042a7c efadb307', 16), |
403 | h: new Clipperz.Crypto.ECC.BinaryField.Value('2', 16) | 401 | h: new Clipperz.Crypto.ECC.BinaryField.Value('2', 16) |
404 | 402 | ||
405 | // S: new Clipperz.Crypto.ECC.BinaryField.Value('2aa058f73a0e33ab486b0f610410c53a7f132310', 10), | 403 | // S: new Clipperz.Crypto.ECC.BinaryField.Value('2aa058f73a0e33ab486b0f610410c53a7f132310', 10), |
406 | // n: new Clipperz.Crypto.ECC.BinaryField.Value('03ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe661ce18ff55987308059b186823851ec7dd9ca1161de93d5174d66e8382e9bb2fe84e47', 16), | 404 | // n: new Clipperz.Crypto.ECC.BinaryField.Value('03ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe661ce18ff55987308059b186823851ec7dd9ca1161de93d5174d66e8382e9bb2fe84e47', 16), |
diff --git a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/FiniteField.js b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/FiniteField.js index 650b479..de1e6a8 100644 --- a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/FiniteField.js +++ b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/FiniteField.js | |||
@@ -1,406 +1,404 @@ | |||
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.ECC depends on Clipperz.ByteArray!"; | 25 | throw "Clipperz.Crypto.ECC depends on Clipperz.ByteArray!"; |
28 | } | 26 | } |
29 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } | 27 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } |
30 | if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; } | 28 | if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; } |
31 | 29 | ||
32 | Clipperz.Crypto.ECC.BinaryField.FiniteField = function(args) { | 30 | Clipperz.Crypto.ECC.BinaryField.FiniteField = function(args) { |
33 | args = args || {}; | 31 | args = args || {}; |
34 | this._modulus = args.modulus; | 32 | this._modulus = args.modulus; |
35 | 33 | ||
36 | return this; | 34 | return this; |
37 | } | 35 | } |
38 | 36 | ||
39 | Clipperz.Crypto.ECC.BinaryField.FiniteField.prototype = MochiKit.Base.update(null, { | 37 | Clipperz.Crypto.ECC.BinaryField.FiniteField.prototype = MochiKit.Base.update(null, { |
40 | 38 | ||
41 | 'asString': function() { | 39 | 'asString': function() { |
42 | return "Clipperz.Crypto.ECC.BinaryField.FiniteField (" + this.modulus().asString() + ")"; | 40 | return "Clipperz.Crypto.ECC.BinaryField.FiniteField (" + this.modulus().asString() + ")"; |
43 | }, | 41 | }, |
44 | 42 | ||
45 | //----------------------------------------------------------------------------- | 43 | //----------------------------------------------------------------------------- |
46 | 44 | ||
47 | 'modulus': function() { | 45 | 'modulus': function() { |
48 | return this._modulus; | 46 | return this._modulus; |
49 | }, | 47 | }, |
50 | 48 | ||
51 | //----------------------------------------------------------------------------- | 49 | //----------------------------------------------------------------------------- |
52 | 50 | ||
53 | '_module': function(aValue) { | 51 | '_module': function(aValue) { |
54 | varresult; | 52 | varresult; |
55 | var modulusComparison; | 53 | var modulusComparison; |
56 | //console.log(">>> binaryField.finiteField.(standard)module"); | 54 | //console.log(">>> binaryField.finiteField.(standard)module"); |
57 | 55 | ||
58 | modulusComparison = Clipperz.Crypto.ECC.BinaryField.Value._compare(aValue, this.modulus()._value); | 56 | modulusComparison = Clipperz.Crypto.ECC.BinaryField.Value._compare(aValue, this.modulus()._value); |
59 | 57 | ||
60 | if (modulusComparison < 0) { | 58 | if (modulusComparison < 0) { |
61 | result = aValue; | 59 | result = aValue; |
62 | } else if (modulusComparison == 0) { | 60 | } else if (modulusComparison == 0) { |
63 | result = [0]; | 61 | result = [0]; |
64 | } else { | 62 | } else { |
65 | var modulusBitSize; | 63 | var modulusBitSize; |
66 | var resultBitSize; | 64 | var resultBitSize; |
67 | 65 | ||
68 | result = aValue; | 66 | result = aValue; |
69 | 67 | ||
70 | modulusBitSize = this.modulus().bitSize(); | 68 | modulusBitSize = this.modulus().bitSize(); |
71 | resultBitSize = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(result); | 69 | resultBitSize = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(result); |
72 | while (resultBitSize >= modulusBitSize) { | 70 | while (resultBitSize >= modulusBitSize) { |
73 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(result, Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(this.modulus()._value, resultBitSize - modulusBitSize)); | 71 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(result, Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(this.modulus()._value, resultBitSize - modulusBitSize)); |
74 | resultBitSize = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(result); | 72 | resultBitSize = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(result); |
75 | } | 73 | } |
76 | } | 74 | } |
77 | //console.log("<<< binaryField.finiteField.(standard)module"); | 75 | //console.log("<<< binaryField.finiteField.(standard)module"); |
78 | 76 | ||
79 | return result; | 77 | return result; |
80 | }, | 78 | }, |
81 | 79 | ||
82 | 'module': function(aValue) { | 80 | 'module': function(aValue) { |
83 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._module(aValue._value.slice(0))); | 81 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._module(aValue._value.slice(0))); |
84 | }, | 82 | }, |
85 | 83 | ||
86 | //----------------------------------------------------------------------------- | 84 | //----------------------------------------------------------------------------- |
87 | 85 | ||
88 | '_add': function(a, b) { | 86 | '_add': function(a, b) { |
89 | return Clipperz.Crypto.ECC.BinaryField.Value._xor(a, b); | 87 | return Clipperz.Crypto.ECC.BinaryField.Value._xor(a, b); |
90 | }, | 88 | }, |
91 | 89 | ||
92 | '_overwriteAdd': function(a, b) { | 90 | '_overwriteAdd': function(a, b) { |
93 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(a, b); | 91 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(a, b); |
94 | }, | 92 | }, |
95 | 93 | ||
96 | 'add': function(a, b) { | 94 | 'add': function(a, b) { |
97 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._add(a._value, b._value)); | 95 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._add(a._value, b._value)); |
98 | }, | 96 | }, |
99 | 97 | ||
100 | //----------------------------------------------------------------------------- | 98 | //----------------------------------------------------------------------------- |
101 | 99 | ||
102 | 'negate': function(aValue) { | 100 | 'negate': function(aValue) { |
103 | return aValue.clone(); | 101 | return aValue.clone(); |
104 | }, | 102 | }, |
105 | 103 | ||
106 | //----------------------------------------------------------------------------- | 104 | //----------------------------------------------------------------------------- |
107 | 105 | ||
108 | '_multiply': function(a, b) { | 106 | '_multiply': function(a, b) { |
109 | var result; | 107 | var result; |
110 | var valueToXor; | 108 | var valueToXor; |
111 | var i,c; | 109 | var i,c; |
112 | 110 | ||
113 | result = [0]; | 111 | result = [0]; |
114 | valueToXor = b; | 112 | valueToXor = b; |
115 | c = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(a); | 113 | c = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(a); |
116 | for (i=0; i<c; i++) { | 114 | for (i=0; i<c; i++) { |
117 | if (Clipperz.Crypto.ECC.BinaryField.Value._isBitSet(a, i) === true) { | 115 | if (Clipperz.Crypto.ECC.BinaryField.Value._isBitSet(a, i) === true) { |
118 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(result, valueToXor); | 116 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(result, valueToXor); |
119 | } | 117 | } |
120 | valueToXor = Clipperz.Crypto.ECC.BinaryField.Value._overwriteShiftLeft(valueToXor, 1); | 118 | valueToXor = Clipperz.Crypto.ECC.BinaryField.Value._overwriteShiftLeft(valueToXor, 1); |
121 | } | 119 | } |
122 | result = this._module(result); | 120 | result = this._module(result); |
123 | 121 | ||
124 | return result; | 122 | return result; |
125 | }, | 123 | }, |
126 | 124 | ||
127 | 'multiply': function(a, b) { | 125 | 'multiply': function(a, b) { |
128 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._multiply(a._value, b._value)); | 126 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._multiply(a._value, b._value)); |
129 | }, | 127 | }, |
130 | 128 | ||
131 | //----------------------------------------------------------------------------- | 129 | //----------------------------------------------------------------------------- |
132 | 130 | ||
133 | '_fastMultiply': function(a, b) { | 131 | '_fastMultiply': function(a, b) { |
134 | var result; | 132 | var result; |
135 | var B; | 133 | var B; |
136 | var i,c; | 134 | var i,c; |
137 | 135 | ||
138 | result = [0]; | 136 | result = [0]; |
139 | B = b.slice(0); //Is this array copy avoidable? | 137 | B = b.slice(0); //Is this array copy avoidable? |
140 | c = 32; | 138 | c = 32; |
141 | for (i=0; i<c; i++) { | 139 | for (i=0; i<c; i++) { |
142 | var ii, cc; | 140 | var ii, cc; |
143 | 141 | ||
144 | cc = a.length; | 142 | cc = a.length; |
145 | for (ii=0; ii<cc; ii++) { | 143 | for (ii=0; ii<cc; ii++) { |
146 | if (((a[ii] >>> i) & 0x01) == 1) { | 144 | if (((a[ii] >>> i) & 0x01) == 1) { |
147 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(result, B, ii); | 145 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(result, B, ii); |
148 | } | 146 | } |
149 | } | 147 | } |
150 | 148 | ||
151 | if (i < (c-1)) { | 149 | if (i < (c-1)) { |
152 | B = Clipperz.Crypto.ECC.BinaryField.Value._overwriteShiftLeft(B, 1); | 150 | B = Clipperz.Crypto.ECC.BinaryField.Value._overwriteShiftLeft(B, 1); |
153 | } | 151 | } |
154 | } | 152 | } |
155 | result = this._module(result); | 153 | result = this._module(result); |
156 | 154 | ||
157 | return result; | 155 | return result; |
158 | }, | 156 | }, |
159 | 157 | ||
160 | 'fastMultiply': function(a, b) { | 158 | 'fastMultiply': function(a, b) { |
161 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._fastMultiply(a._value, b._value)); | 159 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._fastMultiply(a._value, b._value)); |
162 | }, | 160 | }, |
163 | 161 | ||
164 | //----------------------------------------------------------------------------- | 162 | //----------------------------------------------------------------------------- |
165 | // | 163 | // |
166 | //Guide to Elliptic Curve Cryptography | 164 | //Guide to Elliptic Curve Cryptography |
167 | //Darrel Hankerson, Alfred Menezes, Scott Vanstone | 165 | //Darrel Hankerson, Alfred Menezes, Scott Vanstone |
168 | //- Pag: 49, Alorithm 2.34 | 166 | //- Pag: 49, Alorithm 2.34 |
169 | // | 167 | // |
170 | //----------------------------------------------------------------------------- | 168 | //----------------------------------------------------------------------------- |
171 | 169 | ||
172 | '_square': function(aValue) { | 170 | '_square': function(aValue) { |
173 | var result; | 171 | var result; |
174 | var value; | 172 | var value; |
175 | var c,i; | 173 | var c,i; |
176 | var precomputedValues; | 174 | var precomputedValues; |
177 | 175 | ||
178 | value = aValue; | 176 | value = aValue; |
179 | result = new Array(value.length * 2); | 177 | result = new Array(value.length * 2); |
180 | precomputedValues = Clipperz.Crypto.ECC.BinaryField.FiniteField.squarePrecomputedBytes; | 178 | precomputedValues = Clipperz.Crypto.ECC.BinaryField.FiniteField.squarePrecomputedBytes; |
181 | 179 | ||
182 | c = value.length; | 180 | c = value.length; |
183 | for (i=0; i<c; i++) { | 181 | for (i=0; i<c; i++) { |
184 | result[i*2] = precomputedValues[(value[i] & 0x000000ff)]; | 182 | result[i*2] = precomputedValues[(value[i] & 0x000000ff)]; |
185 | result[i*2] |= ((precomputedValues[(value[i] & 0x0000ff00) >>> 8]) << 16); | 183 | result[i*2] |= ((precomputedValues[(value[i] & 0x0000ff00) >>> 8]) << 16); |
186 | 184 | ||
187 | result[i*2 + 1] = precomputedValues[(value[i] & 0x00ff0000) >>> 16]; | 185 | result[i*2 + 1] = precomputedValues[(value[i] & 0x00ff0000) >>> 16]; |
188 | result[i*2 + 1] |= ((precomputedValues[(value[i] & 0xff000000) >>> 24]) << 16); | 186 | result[i*2 + 1] |= ((precomputedValues[(value[i] & 0xff000000) >>> 24]) << 16); |
189 | } | 187 | } |
190 | 188 | ||
191 | return this._module(result); | 189 | return this._module(result); |
192 | }, | 190 | }, |
193 | 191 | ||
194 | 'square': function(aValue) { | 192 | 'square': function(aValue) { |
195 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._square(aValue._value)); | 193 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._square(aValue._value)); |
196 | }, | 194 | }, |
197 | 195 | ||
198 | //----------------------------------------------------------------------------- | 196 | //----------------------------------------------------------------------------- |
199 | 197 | ||
200 | '_inverse': function(aValue) { | 198 | '_inverse': function(aValue) { |
201 | varresult; | 199 | varresult; |
202 | var b, c; | 200 | var b, c; |
203 | var u, v; | 201 | var u, v; |
204 | 202 | ||
205 | // b = Clipperz.Crypto.ECC.BinaryField.Value.I._value; | 203 | // b = Clipperz.Crypto.ECC.BinaryField.Value.I._value; |
206 | b = [1]; | 204 | b = [1]; |
207 | // c = Clipperz.Crypto.ECC.BinaryField.Value.O._value; | 205 | // c = Clipperz.Crypto.ECC.BinaryField.Value.O._value; |
208 | c = [0]; | 206 | c = [0]; |
209 | u = this._module(aValue); | 207 | u = this._module(aValue); |
210 | v = this.modulus()._value.slice(0); | 208 | v = this.modulus()._value.slice(0); |
211 | 209 | ||
212 | while (Clipperz.Crypto.ECC.BinaryField.Value._bitSize(u) > 1) { | 210 | while (Clipperz.Crypto.ECC.BinaryField.Value._bitSize(u) > 1) { |
213 | varbitDifferenceSize; | 211 | varbitDifferenceSize; |
214 | 212 | ||
215 | bitDifferenceSize = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(u) - Clipperz.Crypto.ECC.BinaryField.Value._bitSize(v); | 213 | bitDifferenceSize = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(u) - Clipperz.Crypto.ECC.BinaryField.Value._bitSize(v); |
216 | if (bitDifferenceSize < 0) { | 214 | if (bitDifferenceSize < 0) { |
217 | var swap; | 215 | var swap; |
218 | 216 | ||
219 | swap = u; | 217 | swap = u; |
220 | u = v; | 218 | u = v; |
221 | v = swap; | 219 | v = swap; |
222 | 220 | ||
223 | swap = c; | 221 | swap = c; |
224 | c = b; | 222 | c = b; |
225 | b = swap; | 223 | b = swap; |
226 | 224 | ||
227 | bitDifferenceSize = -bitDifferenceSize; | 225 | bitDifferenceSize = -bitDifferenceSize; |
228 | } | 226 | } |
229 | 227 | ||
230 | u = this._add(u, Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(v, bitDifferenceSize)); | 228 | u = this._add(u, Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(v, bitDifferenceSize)); |
231 | b = this._add(b, Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(c, bitDifferenceSize)); | 229 | b = this._add(b, Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(c, bitDifferenceSize)); |
232 | // this._overwriteAdd(u, Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(v, bitDifferenceSize)); | 230 | // this._overwriteAdd(u, Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(v, bitDifferenceSize)); |
233 | // this._overwriteAdd(b, Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(c, bitDifferenceSize)); | 231 | // this._overwriteAdd(b, Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(c, bitDifferenceSize)); |
234 | } | 232 | } |
235 | 233 | ||
236 | result = this._module(b); | 234 | result = this._module(b); |
237 | 235 | ||
238 | return result; | 236 | return result; |
239 | }, | 237 | }, |
240 | 238 | ||
241 | 'inverse': function(aValue) { | 239 | 'inverse': function(aValue) { |
242 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._inverse(aValue._value)); | 240 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._inverse(aValue._value)); |
243 | }, | 241 | }, |
244 | 242 | ||
245 | //----------------------------------------------------------------------------- | 243 | //----------------------------------------------------------------------------- |
246 | __syntaxFix__: "syntax fix" | 244 | __syntaxFix__: "syntax fix" |
247 | }); | 245 | }); |
248 | 246 | ||
249 | 247 | ||
250 | Clipperz.Crypto.ECC.BinaryField.FiniteField.squarePrecomputedBytes = [ | 248 | Clipperz.Crypto.ECC.BinaryField.FiniteField.squarePrecomputedBytes = [ |
251 | 0x0000, // 0 = 0000 0000 -> 0000 0000 0000 0000 | 249 | 0x0000, // 0 = 0000 0000 -> 0000 0000 0000 0000 |
252 | 0x0001, // 1 = 0000 0001 -> 0000 0000 0000 0001 | 250 | 0x0001, // 1 = 0000 0001 -> 0000 0000 0000 0001 |
253 | 0x0004, // 2 = 0000 0010 -> 0000 0000 0000 0100 | 251 | 0x0004, // 2 = 0000 0010 -> 0000 0000 0000 0100 |
254 | 0x0005, // 3 = 0000 0011 -> 0000 0000 0000 0101 | 252 | 0x0005, // 3 = 0000 0011 -> 0000 0000 0000 0101 |
255 | 0x0010, // 4 = 0000 0100 -> 0000 0000 0001 0000 | 253 | 0x0010, // 4 = 0000 0100 -> 0000 0000 0001 0000 |
256 | 0x0011, // 5 = 0000 0101 -> 0000 0000 0001 0001 | 254 | 0x0011, // 5 = 0000 0101 -> 0000 0000 0001 0001 |
257 | 0x0014, // 6 = 0000 0110 -> 0000 0000 0001 0100 | 255 | 0x0014, // 6 = 0000 0110 -> 0000 0000 0001 0100 |
258 | 0x0015, // 7 = 0000 0111 -> 0000 0000 0001 0101 | 256 | 0x0015, // 7 = 0000 0111 -> 0000 0000 0001 0101 |
259 | 0x0040, // 8 = 0000 1000 -> 0000 0000 0100 0000 | 257 | 0x0040, // 8 = 0000 1000 -> 0000 0000 0100 0000 |
260 | 0x0041, // 9 = 0000 1001 -> 0000 0000 0100 0001 | 258 | 0x0041, // 9 = 0000 1001 -> 0000 0000 0100 0001 |
261 | 0x0044, // 10 = 0000 1010 -> 0000 0000 0100 0100 | 259 | 0x0044, // 10 = 0000 1010 -> 0000 0000 0100 0100 |
262 | 0x0045, // 11 = 0000 1011 -> 0000 0000 0100 0101 | 260 | 0x0045, // 11 = 0000 1011 -> 0000 0000 0100 0101 |
263 | 0x0050, // 12 = 0000 1100 -> 0000 0000 0101 0000 | 261 | 0x0050, // 12 = 0000 1100 -> 0000 0000 0101 0000 |
264 | 0x0051, // 13 = 0000 1101 -> 0000 0000 0101 0001 | 262 | 0x0051, // 13 = 0000 1101 -> 0000 0000 0101 0001 |
265 | 0x0054, // 14 = 0000 1110 -> 0000 0000 0101 0100 | 263 | 0x0054, // 14 = 0000 1110 -> 0000 0000 0101 0100 |
266 | 0x0055, // 15 = 0000 1111 -> 0000 0000 0101 0101 | 264 | 0x0055, // 15 = 0000 1111 -> 0000 0000 0101 0101 |
267 | 265 | ||
268 | 0x0100, // 16 = 0001 0000 -> 0000 0001 0000 0000 | 266 | 0x0100, // 16 = 0001 0000 -> 0000 0001 0000 0000 |
269 | 0x0101, // 17 = 0001 0001 -> 0000 0001 0000 0001 | 267 | 0x0101, // 17 = 0001 0001 -> 0000 0001 0000 0001 |
270 | 0x0104, // 18 = 0001 0010 -> 0000 0001 0000 0100 | 268 | 0x0104, // 18 = 0001 0010 -> 0000 0001 0000 0100 |
271 | 0x0105, // 19 = 0001 0011 -> 0000 0001 0000 0101 | 269 | 0x0105, // 19 = 0001 0011 -> 0000 0001 0000 0101 |
272 | 0x0110, // 20 = 0001 0100 -> 0000 0001 0001 0000 | 270 | 0x0110, // 20 = 0001 0100 -> 0000 0001 0001 0000 |
273 | 0x0111, // 21 = 0001 0101 -> 0000 0001 0001 0001 | 271 | 0x0111, // 21 = 0001 0101 -> 0000 0001 0001 0001 |
274 | 0x0114, // 22 = 0001 0110 -> 0000 0001 0001 0100 | 272 | 0x0114, // 22 = 0001 0110 -> 0000 0001 0001 0100 |
275 | 0x0115, // 23 = 0001 0111 -> 0000 0001 0001 0101 | 273 | 0x0115, // 23 = 0001 0111 -> 0000 0001 0001 0101 |
276 | 0x0140, // 24 = 0001 1000 -> 0000 0001 0100 0000 | 274 | 0x0140, // 24 = 0001 1000 -> 0000 0001 0100 0000 |
277 | 0x0141, // 25 = 0001 1001 -> 0000 0001 0100 0001 | 275 | 0x0141, // 25 = 0001 1001 -> 0000 0001 0100 0001 |
278 | 0x0144, // 26 = 0001 1010 -> 0000 0001 0100 0100 | 276 | 0x0144, // 26 = 0001 1010 -> 0000 0001 0100 0100 |
279 | 0x0145, // 27 = 0001 1011 -> 0000 0001 0100 0101 | 277 | 0x0145, // 27 = 0001 1011 -> 0000 0001 0100 0101 |
280 | 0x0150, // 28 = 0001 1100 -> 0000 0001 0101 0000 | 278 | 0x0150, // 28 = 0001 1100 -> 0000 0001 0101 0000 |
281 | 0x0151, // 28 = 0001 1101 -> 0000 0001 0101 0001 | 279 | 0x0151, // 28 = 0001 1101 -> 0000 0001 0101 0001 |
282 | 0x0154, // 30 = 0001 1110 -> 0000 0001 0101 0100 | 280 | 0x0154, // 30 = 0001 1110 -> 0000 0001 0101 0100 |
283 | 0x0155, // 31 = 0001 1111 -> 0000 0001 0101 0101 | 281 | 0x0155, // 31 = 0001 1111 -> 0000 0001 0101 0101 |
284 | 282 | ||
285 | 0x0400, // 32 = 0010 0000 -> 0000 0100 0000 0000 | 283 | 0x0400, // 32 = 0010 0000 -> 0000 0100 0000 0000 |
286 | 0x0401, // 33 = 0010 0001 -> 0000 0100 0000 0001 | 284 | 0x0401, // 33 = 0010 0001 -> 0000 0100 0000 0001 |
287 | 0x0404, // 34 = 0010 0010 -> 0000 0100 0000 0100 | 285 | 0x0404, // 34 = 0010 0010 -> 0000 0100 0000 0100 |
288 | 0x0405, // 35 = 0010 0011 -> 0000 0100 0000 0101 | 286 | 0x0405, // 35 = 0010 0011 -> 0000 0100 0000 0101 |
289 | 0x0410, // 36 = 0010 0100 -> 0000 0100 0001 0000 | 287 | 0x0410, // 36 = 0010 0100 -> 0000 0100 0001 0000 |
290 | 0x0411, // 37 = 0010 0101 -> 0000 0100 0001 0001 | 288 | 0x0411, // 37 = 0010 0101 -> 0000 0100 0001 0001 |
291 | 0x0414, // 38 = 0010 0110 -> 0000 0100 0001 0100 | 289 | 0x0414, // 38 = 0010 0110 -> 0000 0100 0001 0100 |
292 | 0x0415, // 39 = 0010 0111 -> 0000 0100 0001 0101 | 290 | 0x0415, // 39 = 0010 0111 -> 0000 0100 0001 0101 |
293 | 0x0440, // 40 = 0010 1000 -> 0000 0100 0100 0000 | 291 | 0x0440, // 40 = 0010 1000 -> 0000 0100 0100 0000 |
294 | 0x0441, // 41 = 0010 1001 -> 0000 0100 0100 0001 | 292 | 0x0441, // 41 = 0010 1001 -> 0000 0100 0100 0001 |
295 | 0x0444, // 42 = 0010 1010 -> 0000 0100 0100 0100 | 293 | 0x0444, // 42 = 0010 1010 -> 0000 0100 0100 0100 |
296 | 0x0445, // 43 = 0010 1011 -> 0000 0100 0100 0101 | 294 | 0x0445, // 43 = 0010 1011 -> 0000 0100 0100 0101 |
297 | 0x0450, // 44 = 0010 1100 -> 0000 0100 0101 0000 | 295 | 0x0450, // 44 = 0010 1100 -> 0000 0100 0101 0000 |
298 | 0x0451, // 45 = 0010 1101 -> 0000 0100 0101 0001 | 296 | 0x0451, // 45 = 0010 1101 -> 0000 0100 0101 0001 |
299 | 0x0454, // 46 = 0010 1110 -> 0000 0100 0101 0100 | 297 | 0x0454, // 46 = 0010 1110 -> 0000 0100 0101 0100 |
300 | 0x0455, // 47 = 0010 1111 -> 0000 0100 0101 0101 | 298 | 0x0455, // 47 = 0010 1111 -> 0000 0100 0101 0101 |
301 | 299 | ||
302 | 0x0500, // 48 = 0011 0000 -> 0000 0101 0000 0000 | 300 | 0x0500, // 48 = 0011 0000 -> 0000 0101 0000 0000 |
303 | 0x0501, // 49 = 0011 0001 -> 0000 0101 0000 0001 | 301 | 0x0501, // 49 = 0011 0001 -> 0000 0101 0000 0001 |
304 | 0x0504, // 50 = 0011 0010 -> 0000 0101 0000 0100 | 302 | 0x0504, // 50 = 0011 0010 -> 0000 0101 0000 0100 |
305 | 0x0505, // 51 = 0011 0011 -> 0000 0101 0000 0101 | 303 | 0x0505, // 51 = 0011 0011 -> 0000 0101 0000 0101 |
306 | 0x0510, // 52 = 0011 0100 -> 0000 0101 0001 0000 | 304 | 0x0510, // 52 = 0011 0100 -> 0000 0101 0001 0000 |
307 | 0x0511, // 53 = 0011 0101 -> 0000 0101 0001 0001 | 305 | 0x0511, // 53 = 0011 0101 -> 0000 0101 0001 0001 |
308 | 0x0514, // 54 = 0011 0110 -> 0000 0101 0001 0100 | 306 | 0x0514, // 54 = 0011 0110 -> 0000 0101 0001 0100 |
309 | 0x0515, // 55 = 0011 0111 -> 0000 0101 0001 0101 | 307 | 0x0515, // 55 = 0011 0111 -> 0000 0101 0001 0101 |
310 | 0x0540, // 56 = 0011 1000 -> 0000 0101 0100 0000 | 308 | 0x0540, // 56 = 0011 1000 -> 0000 0101 0100 0000 |
311 | 0x0541, // 57 = 0011 1001 -> 0000 0101 0100 0001 | 309 | 0x0541, // 57 = 0011 1001 -> 0000 0101 0100 0001 |
312 | 0x0544, // 58 = 0011 1010 -> 0000 0101 0100 0100 | 310 | 0x0544, // 58 = 0011 1010 -> 0000 0101 0100 0100 |
313 | 0x0545, // 59 = 0011 1011 -> 0000 0101 0100 0101 | 311 | 0x0545, // 59 = 0011 1011 -> 0000 0101 0100 0101 |
314 | 0x0550, // 60 = 0011 1100 -> 0000 0101 0101 0000 | 312 | 0x0550, // 60 = 0011 1100 -> 0000 0101 0101 0000 |
315 | 0x0551, // 61 = 0011 1101 -> 0000 0101 0101 0001 | 313 | 0x0551, // 61 = 0011 1101 -> 0000 0101 0101 0001 |
316 | 0x0554, // 62 = 0011 1110 -> 0000 0101 0101 0100 | 314 | 0x0554, // 62 = 0011 1110 -> 0000 0101 0101 0100 |
317 | 0x0555, // 63 = 0011 1111 -> 0000 0101 0101 0101 | 315 | 0x0555, // 63 = 0011 1111 -> 0000 0101 0101 0101 |
318 | 316 | ||
319 | 0x1000, // 64 = 0100 0000 -> 0001 0000 0000 0000 | 317 | 0x1000, // 64 = 0100 0000 -> 0001 0000 0000 0000 |
320 | 0x1001, // 65 = 0100 0001 -> 0001 0000 0000 0001 | 318 | 0x1001, // 65 = 0100 0001 -> 0001 0000 0000 0001 |
321 | 0x1004, // 66 = 0100 0010 -> 0001 0000 0000 0100 | 319 | 0x1004, // 66 = 0100 0010 -> 0001 0000 0000 0100 |
322 | 0x1005, // 67 = 0100 0011 -> 0001 0000 0000 0101 | 320 | 0x1005, // 67 = 0100 0011 -> 0001 0000 0000 0101 |
323 | 0x1010, // 68 = 0100 0100 -> 0001 0000 0001 0000 | 321 | 0x1010, // 68 = 0100 0100 -> 0001 0000 0001 0000 |
324 | 0x1011, // 69 = 0100 0101 -> 0001 0000 0001 0001 | 322 | 0x1011, // 69 = 0100 0101 -> 0001 0000 0001 0001 |
325 | 0x1014, // 70 = 0100 0110 -> 0001 0000 0001 0100 | 323 | 0x1014, // 70 = 0100 0110 -> 0001 0000 0001 0100 |
326 | 0x1015, // 71 = 0100 0111 -> 0001 0000 0001 0101 | 324 | 0x1015, // 71 = 0100 0111 -> 0001 0000 0001 0101 |
327 | 0x1040, // 72 = 0100 1000 -> 0001 0000 0100 0000 | 325 | 0x1040, // 72 = 0100 1000 -> 0001 0000 0100 0000 |
328 | 0x1041, // 73 = 0100 1001 -> 0001 0000 0100 0001 | 326 | 0x1041, // 73 = 0100 1001 -> 0001 0000 0100 0001 |
329 | 0x1044, // 74 = 0100 1010 -> 0001 0000 0100 0100 | 327 | 0x1044, // 74 = 0100 1010 -> 0001 0000 0100 0100 |
330 | 0x1045, // 75 = 0100 1011 -> 0001 0000 0100 0101 | 328 | 0x1045, // 75 = 0100 1011 -> 0001 0000 0100 0101 |
331 | 0x1050, // 76 = 0100 1100 -> 0001 0000 0101 0000 | 329 | 0x1050, // 76 = 0100 1100 -> 0001 0000 0101 0000 |
332 | 0x1051, // 77 = 0100 1101 -> 0001 0000 0101 0001 | 330 | 0x1051, // 77 = 0100 1101 -> 0001 0000 0101 0001 |
333 | 0x1054, // 78 = 0100 1110 -> 0001 0000 0101 0100 | 331 | 0x1054, // 78 = 0100 1110 -> 0001 0000 0101 0100 |
334 | 0x1055, // 79 = 0100 1111 -> 0001 0000 0101 0101 | 332 | 0x1055, // 79 = 0100 1111 -> 0001 0000 0101 0101 |
335 | 333 | ||
336 | 0x1100, // 80 = 0101 0000 -> 0001 0001 0000 0000 | 334 | 0x1100, // 80 = 0101 0000 -> 0001 0001 0000 0000 |
337 | 0x1101, // 81 = 0101 0001 -> 0001 0001 0000 0001 | 335 | 0x1101, // 81 = 0101 0001 -> 0001 0001 0000 0001 |
338 | 0x1104, // 82 = 0101 0010 -> 0001 0001 0000 0100 | 336 | 0x1104, // 82 = 0101 0010 -> 0001 0001 0000 0100 |
339 | 0x1105, // 83 = 0101 0011 -> 0001 0001 0000 0101 | 337 | 0x1105, // 83 = 0101 0011 -> 0001 0001 0000 0101 |
340 | 0x1110, // 84 = 0101 0100 -> 0001 0001 0001 0000 | 338 | 0x1110, // 84 = 0101 0100 -> 0001 0001 0001 0000 |
341 | 0x1111, // 85 = 0101 0101 -> 0001 0001 0001 0001 | 339 | 0x1111, // 85 = 0101 0101 -> 0001 0001 0001 0001 |
342 | 0x1114, // 86 = 0101 0110 -> 0001 0001 0001 0100 | 340 | 0x1114, // 86 = 0101 0110 -> 0001 0001 0001 0100 |
343 | 0x1115, // 87 = 0101 0111 -> 0001 0001 0001 0101 | 341 | 0x1115, // 87 = 0101 0111 -> 0001 0001 0001 0101 |
344 | 0x1140, // 88 = 0101 1000 -> 0001 0001 0100 0000 | 342 | 0x1140, // 88 = 0101 1000 -> 0001 0001 0100 0000 |
345 | 0x1141, // 89 = 0101 1001 -> 0001 0001 0100 0001 | 343 | 0x1141, // 89 = 0101 1001 -> 0001 0001 0100 0001 |
346 | 0x1144, // 90 = 0101 1010 -> 0001 0001 0100 0100 | 344 | 0x1144, // 90 = 0101 1010 -> 0001 0001 0100 0100 |
347 | 0x1145, // 91 = 0101 1011 -> 0001 0001 0100 0101 | 345 | 0x1145, // 91 = 0101 1011 -> 0001 0001 0100 0101 |
348 | 0x1150, // 92 = 0101 1100 -> 0001 0001 0101 0000 | 346 | 0x1150, // 92 = 0101 1100 -> 0001 0001 0101 0000 |
349 | 0x1151, // 93 = 0101 1101 -> 0001 0001 0101 0001 | 347 | 0x1151, // 93 = 0101 1101 -> 0001 0001 0101 0001 |
350 | 0x1154, // 94 = 0101 1110 -> 0001 0001 0101 0100 | 348 | 0x1154, // 94 = 0101 1110 -> 0001 0001 0101 0100 |
351 | 0x1155, // 95 = 0101 1111 -> 0001 0001 0101 0101 | 349 | 0x1155, // 95 = 0101 1111 -> 0001 0001 0101 0101 |
352 | 350 | ||
353 | 0x1400, // 96 = 0110 0000 -> 0001 0100 0000 0000 | 351 | 0x1400, // 96 = 0110 0000 -> 0001 0100 0000 0000 |
354 | 0x1401, // 97 = 0110 0001 -> 0001 0100 0000 0001 | 352 | 0x1401, // 97 = 0110 0001 -> 0001 0100 0000 0001 |
355 | 0x1404, // 98 = 0110 0010 -> 0001 0100 0000 0100 | 353 | 0x1404, // 98 = 0110 0010 -> 0001 0100 0000 0100 |
356 | 0x1405, // 99 = 0110 0011 -> 0001 0100 0000 0101 | 354 | 0x1405, // 99 = 0110 0011 -> 0001 0100 0000 0101 |
357 | 0x1410, //100 = 0110 0100 -> 0001 0100 0001 0000 | 355 | 0x1410, //100 = 0110 0100 -> 0001 0100 0001 0000 |
358 | 0x1411, //101 = 0110 0101 -> 0001 0100 0001 0001 | 356 | 0x1411, //101 = 0110 0101 -> 0001 0100 0001 0001 |
359 | 0x1414, //102 = 0110 0110 -> 0001 0100 0001 0100 | 357 | 0x1414, //102 = 0110 0110 -> 0001 0100 0001 0100 |
360 | 0x1415, //103 = 0110 0111 -> 0001 0100 0001 0101 | 358 | 0x1415, //103 = 0110 0111 -> 0001 0100 0001 0101 |
361 | 0x1440, //104 = 0110 1000 -> 0001 0100 0100 0000 | 359 | 0x1440, //104 = 0110 1000 -> 0001 0100 0100 0000 |
362 | 0x1441, //105 = 0110 1001 -> 0001 0100 0100 0001 | 360 | 0x1441, //105 = 0110 1001 -> 0001 0100 0100 0001 |
363 | 0x1444, //106 = 0110 1010 -> 0001 0100 0100 0100 | 361 | 0x1444, //106 = 0110 1010 -> 0001 0100 0100 0100 |
364 | 0x1445, //107 = 0110 1011 -> 0001 0100 0100 0101 | 362 | 0x1445, //107 = 0110 1011 -> 0001 0100 0100 0101 |
365 | 0x1450, //108 = 0110 1100 -> 0001 0100 0101 0000 | 363 | 0x1450, //108 = 0110 1100 -> 0001 0100 0101 0000 |
366 | 0x1451, //109 = 0110 1101 -> 0001 0100 0101 0001 | 364 | 0x1451, //109 = 0110 1101 -> 0001 0100 0101 0001 |
367 | 0x1454, //110 = 0110 1110 -> 0001 0100 0101 0100 | 365 | 0x1454, //110 = 0110 1110 -> 0001 0100 0101 0100 |
368 | 0x1455, //111 = 0110 1111 -> 0001 0100 0101 0101 | 366 | 0x1455, //111 = 0110 1111 -> 0001 0100 0101 0101 |
369 | 367 | ||
370 | 0x1500, //112 = 0111 0000 -> 0001 0101 0000 0000 | 368 | 0x1500, //112 = 0111 0000 -> 0001 0101 0000 0000 |
371 | 0x1501, //113 = 0111 0001 -> 0001 0101 0000 0001 | 369 | 0x1501, //113 = 0111 0001 -> 0001 0101 0000 0001 |
372 | 0x1504, //114 = 0111 0010 -> 0001 0101 0000 0100 | 370 | 0x1504, //114 = 0111 0010 -> 0001 0101 0000 0100 |
373 | 0x1505, //115 = 0111 0011 -> 0001 0101 0000 0101 | 371 | 0x1505, //115 = 0111 0011 -> 0001 0101 0000 0101 |
374 | 0x1510, //116 = 0111 0100 -> 0001 0101 0001 0000 | 372 | 0x1510, //116 = 0111 0100 -> 0001 0101 0001 0000 |
375 | 0x1511, //117 = 0111 0101 -> 0001 0101 0001 0001 | 373 | 0x1511, //117 = 0111 0101 -> 0001 0101 0001 0001 |
376 | 0x1514, //118 = 0111 0110 -> 0001 0101 0001 0100 | 374 | 0x1514, //118 = 0111 0110 -> 0001 0101 0001 0100 |
377 | 0x1515, //119 = 0111 0111 -> 0001 0101 0001 0101 | 375 | 0x1515, //119 = 0111 0111 -> 0001 0101 0001 0101 |
378 | 0x1540, //120 = 0111 1000 -> 0001 0101 0100 0000 | 376 | 0x1540, //120 = 0111 1000 -> 0001 0101 0100 0000 |
379 | 0x1541, //121 = 0111 1001 -> 0001 0101 0100 0001 | 377 | 0x1541, //121 = 0111 1001 -> 0001 0101 0100 0001 |
380 | 0x1544, //122 = 0111 1010 -> 0001 0101 0100 0100 | 378 | 0x1544, //122 = 0111 1010 -> 0001 0101 0100 0100 |
381 | 0x1545, //123 = 0111 1011 -> 0001 0101 0100 0101 | 379 | 0x1545, //123 = 0111 1011 -> 0001 0101 0100 0101 |
382 | 0x1550, //124 = 0111 1100 -> 0001 0101 0101 0000 | 380 | 0x1550, //124 = 0111 1100 -> 0001 0101 0101 0000 |
383 | 0x1551, //125 = 0111 1101 -> 0001 0101 0101 0001 | 381 | 0x1551, //125 = 0111 1101 -> 0001 0101 0101 0001 |
384 | 0x1554, //126 = 0111 1110 -> 0001 0101 0101 0100 | 382 | 0x1554, //126 = 0111 1110 -> 0001 0101 0101 0100 |
385 | 0x1555, //127 = 0111 1111 -> 0001 0101 0101 0101 | 383 | 0x1555, //127 = 0111 1111 -> 0001 0101 0101 0101 |
386 | 384 | ||
387 | 0x4000, //128 = 1000 0000 -> 0100 0000 0000 0000 | 385 | 0x4000, //128 = 1000 0000 -> 0100 0000 0000 0000 |
388 | 0x4001, //129 = 1000 0001 -> 0100 0000 0000 0001 | 386 | 0x4001, //129 = 1000 0001 -> 0100 0000 0000 0001 |
389 | 0x4004, //130 = 1000 0010 -> 0100 0000 0000 0100 | 387 | 0x4004, //130 = 1000 0010 -> 0100 0000 0000 0100 |
390 | 0x4005, //131 = 1000 0011 -> 0100 0000 0000 0101 | 388 | 0x4005, //131 = 1000 0011 -> 0100 0000 0000 0101 |
391 | 0x4010, //132 = 1000 0100 -> 0100 0000 0001 0000 | 389 | 0x4010, //132 = 1000 0100 -> 0100 0000 0001 0000 |
392 | 0x4011, //133 = 1000 0101 -> 0100 0000 0001 0001 | 390 | 0x4011, //133 = 1000 0101 -> 0100 0000 0001 0001 |
393 | 0x4014, //134 = 1000 0110 -> 0100 0000 0001 0100 | 391 | 0x4014, //134 = 1000 0110 -> 0100 0000 0001 0100 |
394 | 0x4015, //135 = 1000 0111 -> 0100 0000 0001 0101 | 392 | 0x4015, //135 = 1000 0111 -> 0100 0000 0001 0101 |
395 | 0x4040, //136 = 1000 1000 -> 0100 0000 0100 0000 | 393 | 0x4040, //136 = 1000 1000 -> 0100 0000 0100 0000 |
396 | 0x4041, //137 = 1000 1001 -> 0100 0000 0100 0001 | 394 | 0x4041, //137 = 1000 1001 -> 0100 0000 0100 0001 |
397 | 0x4044, //138 = 1000 1010 -> 0100 0000 0100 0100 | 395 | 0x4044, //138 = 1000 1010 -> 0100 0000 0100 0100 |
398 | 0x4045, //139 = 1000 1011 -> 0100 0000 0100 0101 | 396 | 0x4045, //139 = 1000 1011 -> 0100 0000 0100 0101 |
399 | 0x4050, //140 = 1000 1100 -> 0100 0000 0101 0000 | 397 | 0x4050, //140 = 1000 1100 -> 0100 0000 0101 0000 |
400 | 0x4051, //141 = 1000 1101 -> 0100 0000 0101 0001 | 398 | 0x4051, //141 = 1000 1101 -> 0100 0000 0101 0001 |
401 | 0x4054, //142 = 1000 1110 -> 0100 0000 0101 0100 | 399 | 0x4054, //142 = 1000 1110 -> 0100 0000 0101 0100 |
402 | 0x4055, //143 = 1000 1111 -> 0100 0000 0101 0101 | 400 | 0x4055, //143 = 1000 1111 -> 0100 0000 0101 0101 |
403 | 401 | ||
404 | 0x4100, //144 = 1001 0000 -> 0100 0001 0000 0000 | 402 | 0x4100, //144 = 1001 0000 -> 0100 0001 0000 0000 |
405 | 0x4101, //145 = 1001 0001 -> 0100 0001 0000 0001 | 403 | 0x4101, //145 = 1001 0001 -> 0100 0001 0000 0001 |
406 | 0x4104, //146 = 1001 0010 -> 0100 0001 0000 0100 | 404 | 0x4104, //146 = 1001 0010 -> 0100 0001 0000 0100 |
diff --git a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Point.js b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Point.js index 6661839..c5db6c6 100644 --- a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Point.js +++ b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Point.js | |||
@@ -1,64 +1,62 @@ | |||
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.ECC depends on Clipperz.ByteArray!"; | 25 | throw "Clipperz.Crypto.ECC depends on Clipperz.ByteArray!"; |
28 | } | 26 | } |
29 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } | 27 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } |
30 | if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; } | 28 | if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; } |
31 | 29 | ||
32 | Clipperz.Crypto.ECC.BinaryField.Point = function(args) { | 30 | Clipperz.Crypto.ECC.BinaryField.Point = function(args) { |
33 | args = args || {}; | 31 | args = args || {}; |
34 | this._x = args.x; | 32 | this._x = args.x; |
35 | this._y = args.y; | 33 | this._y = args.y; |
36 | 34 | ||
37 | return this; | 35 | return this; |
38 | } | 36 | } |
39 | 37 | ||
40 | Clipperz.Crypto.ECC.BinaryField.Point.prototype = MochiKit.Base.update(null, { | 38 | Clipperz.Crypto.ECC.BinaryField.Point.prototype = MochiKit.Base.update(null, { |
41 | 39 | ||
42 | 'asString': function() { | 40 | 'asString': function() { |
43 | return "Clipperz.Crypto.ECC.BinaryField.Point (" + this.x() + ", " + this.y() + ")"; | 41 | return "Clipperz.Crypto.ECC.BinaryField.Point (" + this.x() + ", " + this.y() + ")"; |
44 | }, | 42 | }, |
45 | 43 | ||
46 | //----------------------------------------------------------------------------- | 44 | //----------------------------------------------------------------------------- |
47 | 45 | ||
48 | 'x': function() { | 46 | 'x': function() { |
49 | return this._x; | 47 | return this._x; |
50 | }, | 48 | }, |
51 | 49 | ||
52 | 'y': function() { | 50 | 'y': function() { |
53 | return this._y; | 51 | return this._y; |
54 | }, | 52 | }, |
55 | 53 | ||
56 | //----------------------------------------------------------------------------- | 54 | //----------------------------------------------------------------------------- |
57 | 55 | ||
58 | 'isZero': function() { | 56 | 'isZero': function() { |
59 | return (this.x().isZero() && this.y().isZero()) | 57 | return (this.x().isZero() && this.y().isZero()) |
60 | }, | 58 | }, |
61 | 59 | ||
62 | //----------------------------------------------------------------------------- | 60 | //----------------------------------------------------------------------------- |
63 | __syntaxFix__: "syntax fix" | 61 | __syntaxFix__: "syntax fix" |
64 | }); | 62 | }); |
diff --git a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Value.js b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Value.js index b5beafa..278c299 100644 --- a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Value.js +++ b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Value.js | |||
@@ -1,374 +1,372 @@ | |||
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.ECC depends on Clipperz.ByteArray!"; | 25 | throw "Clipperz.Crypto.ECC depends on Clipperz.ByteArray!"; |
28 | } | 26 | } |
29 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } | 27 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } |
30 | if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; } | 28 | if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; } |
31 | 29 | ||
32 | Clipperz.Crypto.ECC.BinaryField.Value = function(aValue, aBase) { | 30 | Clipperz.Crypto.ECC.BinaryField.Value = function(aValue, aBase) { |
33 | if (aValue.constructor == String) { | 31 | if (aValue.constructor == String) { |
34 | varvalue; | 32 | varvalue; |
35 | varstringLength; | 33 | varstringLength; |
36 | var numberOfWords; | 34 | var numberOfWords; |
37 | vari,c; | 35 | vari,c; |
38 | 36 | ||
39 | if (aBase != 16) { | 37 | if (aBase != 16) { |
40 | throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase; | 38 | throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase; |
41 | } | 39 | } |
42 | 40 | ||
43 | value = aValue.replace(/ /g, ''); | 41 | value = aValue.replace(/ /g, ''); |
44 | stringLength = value.length; | 42 | stringLength = value.length; |
45 | numberOfWords = Math.ceil(stringLength / 8); | 43 | numberOfWords = Math.ceil(stringLength / 8); |
46 | this._value = new Array(numberOfWords); | 44 | this._value = new Array(numberOfWords); |
47 | 45 | ||
48 | c = numberOfWords; | 46 | c = numberOfWords; |
49 | for (i=0; i<c; i++) { | 47 | for (i=0; i<c; i++) { |
50 | varword; | 48 | varword; |
51 | 49 | ||
52 | if (i < (c-1)) { | 50 | if (i < (c-1)) { |
53 | word = parseInt(value.substr(stringLength-((i+1)*8), 8), 16); | 51 | word = parseInt(value.substr(stringLength-((i+1)*8), 8), 16); |
54 | } else { | 52 | } else { |
55 | word = parseInt(value.substr(0, stringLength-(i*8)), 16); | 53 | word = parseInt(value.substr(0, stringLength-(i*8)), 16); |
56 | } | 54 | } |
57 | 55 | ||
58 | this._value[i] = word; | 56 | this._value[i] = word; |
59 | } | 57 | } |
60 | } else if (aValue.constructor == Array) { | 58 | } else if (aValue.constructor == Array) { |
61 | var itemsToCopy; | 59 | var itemsToCopy; |
62 | 60 | ||
63 | itemsToCopy = aValue.length; | 61 | itemsToCopy = aValue.length; |
64 | while (aValue[itemsToCopy - 1] == 0) { | 62 | while (aValue[itemsToCopy - 1] == 0) { |
65 | itemsToCopy --; | 63 | itemsToCopy --; |
66 | } | 64 | } |
67 | 65 | ||
68 | this._value = aValue.slice(0, itemsToCopy); | 66 | this._value = aValue.slice(0, itemsToCopy); |
69 | } else if (aValue.constructor == Number) { | 67 | } else if (aValue.constructor == Number) { |
70 | this._value = [aValue]; | 68 | this._value = [aValue]; |
71 | } else { | 69 | } else { |
72 | // throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedConstructorValueType; | 70 | // throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedConstructorValueType; |
73 | } | 71 | } |
74 | 72 | ||
75 | return this; | 73 | return this; |
76 | } | 74 | } |
77 | 75 | ||
78 | Clipperz.Crypto.ECC.BinaryField.Value.prototype = MochiKit.Base.update(null, { | 76 | Clipperz.Crypto.ECC.BinaryField.Value.prototype = MochiKit.Base.update(null, { |
79 | 77 | ||
80 | 'value': function() { | 78 | 'value': function() { |
81 | return this._value; | 79 | return this._value; |
82 | }, | 80 | }, |
83 | 81 | ||
84 | //----------------------------------------------------------------------------- | 82 | //----------------------------------------------------------------------------- |
85 | 83 | ||
86 | 'wordSize': function() { | 84 | 'wordSize': function() { |
87 | return this._value.length | 85 | return this._value.length |
88 | }, | 86 | }, |
89 | 87 | ||
90 | //----------------------------------------------------------------------------- | 88 | //----------------------------------------------------------------------------- |
91 | 89 | ||
92 | 'clone': function() { | 90 | 'clone': function() { |
93 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._value.slice(0)); | 91 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._value.slice(0)); |
94 | }, | 92 | }, |
95 | 93 | ||
96 | //----------------------------------------------------------------------------- | 94 | //----------------------------------------------------------------------------- |
97 | 95 | ||
98 | 'isZero': function() { | 96 | 'isZero': function() { |
99 | return (this.compare(Clipperz.Crypto.ECC.BinaryField.Value.O) == 0); | 97 | return (this.compare(Clipperz.Crypto.ECC.BinaryField.Value.O) == 0); |
100 | }, | 98 | }, |
101 | 99 | ||
102 | //----------------------------------------------------------------------------- | 100 | //----------------------------------------------------------------------------- |
103 | 101 | ||
104 | 'asString': function(aBase) { | 102 | 'asString': function(aBase) { |
105 | varresult; | 103 | varresult; |
106 | var i,c; | 104 | var i,c; |
107 | 105 | ||
108 | if (aBase != 16) { | 106 | if (aBase != 16) { |
109 | throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase; | 107 | throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase; |
110 | } | 108 | } |
111 | 109 | ||
112 | result = ""; | 110 | result = ""; |
113 | c = this.wordSize(); | 111 | c = this.wordSize(); |
114 | for (i=0; i<c; i++) { | 112 | for (i=0; i<c; i++) { |
115 | varwordAsString; | 113 | varwordAsString; |
116 | 114 | ||
117 | // wordAsString = ("00000000" + this.value()[i].toString(16)); | 115 | // wordAsString = ("00000000" + this.value()[i].toString(16)); |
118 | wordAsString = ("00000000" + this._value[i].toString(16)); | 116 | wordAsString = ("00000000" + this._value[i].toString(16)); |
119 | wordAsString = wordAsString.substring(wordAsString.length - 8); | 117 | wordAsString = wordAsString.substring(wordAsString.length - 8); |
120 | result = wordAsString + result; | 118 | result = wordAsString + result; |
121 | } | 119 | } |
122 | 120 | ||
123 | result = result.replace(/^(00)*/, ""); | 121 | result = result.replace(/^(00)*/, ""); |
124 | 122 | ||
125 | if (result == "") { | 123 | if (result == "") { |
126 | result = "0"; | 124 | result = "0"; |
127 | } | 125 | } |
128 | 126 | ||
129 | return result; | 127 | return result; |
130 | }, | 128 | }, |
131 | 129 | ||
132 | //----------------------------------------------------------------------------- | 130 | //----------------------------------------------------------------------------- |
133 | 131 | ||
134 | 'shiftLeft': function(aNumberOfBitsToShift) { | 132 | 'shiftLeft': function(aNumberOfBitsToShift) { |
135 | return new Clipperz.Crypto.ECC.BinaryField.Value(Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(this._value, aNumberOfBitsToShift)); | 133 | return new Clipperz.Crypto.ECC.BinaryField.Value(Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(this._value, aNumberOfBitsToShift)); |
136 | }, | 134 | }, |
137 | 135 | ||
138 | //----------------------------------------------------------------------------- | 136 | //----------------------------------------------------------------------------- |
139 | 137 | ||
140 | 'bitSize': function() { | 138 | 'bitSize': function() { |
141 | return Clipperz.Crypto.ECC.BinaryField.Value._bitSize(this._value); | 139 | return Clipperz.Crypto.ECC.BinaryField.Value._bitSize(this._value); |
142 | }, | 140 | }, |
143 | 141 | ||
144 | //----------------------------------------------------------------------------- | 142 | //----------------------------------------------------------------------------- |
145 | 143 | ||
146 | 'isBitSet': function(aBitPosition) { | 144 | 'isBitSet': function(aBitPosition) { |
147 | return Clipperz.Crypto.ECC.BinaryField.Value._isBitSet(this._value, aBitPosition); | 145 | return Clipperz.Crypto.ECC.BinaryField.Value._isBitSet(this._value, aBitPosition); |
148 | }, | 146 | }, |
149 | 147 | ||
150 | //----------------------------------------------------------------------------- | 148 | //----------------------------------------------------------------------------- |
151 | 149 | ||
152 | 'xor': function(aValue) { | 150 | 'xor': function(aValue) { |
153 | return new Clipperz.Crypto.ECC.BinaryField.Value(Clipperz.Crypto.ECC.BinaryField.Value._xor(this._value, aValue._value)); | 151 | return new Clipperz.Crypto.ECC.BinaryField.Value(Clipperz.Crypto.ECC.BinaryField.Value._xor(this._value, aValue._value)); |
154 | }, | 152 | }, |
155 | 153 | ||
156 | //----------------------------------------------------------------------------- | 154 | //----------------------------------------------------------------------------- |
157 | 155 | ||
158 | 'compare': function(aValue) { | 156 | 'compare': function(aValue) { |
159 | return Clipperz.Crypto.ECC.BinaryField.Value._compare(this._value, aValue._value); | 157 | return Clipperz.Crypto.ECC.BinaryField.Value._compare(this._value, aValue._value); |
160 | }, | 158 | }, |
161 | 159 | ||
162 | //----------------------------------------------------------------------------- | 160 | //----------------------------------------------------------------------------- |
163 | __syntaxFix__: "syntax fix" | 161 | __syntaxFix__: "syntax fix" |
164 | }); | 162 | }); |
165 | 163 | ||
166 | Clipperz.Crypto.ECC.BinaryField.Value.O = new Clipperz.Crypto.ECC.BinaryField.Value('0', 16); | 164 | Clipperz.Crypto.ECC.BinaryField.Value.O = new Clipperz.Crypto.ECC.BinaryField.Value('0', 16); |
167 | Clipperz.Crypto.ECC.BinaryField.Value.I = new Clipperz.Crypto.ECC.BinaryField.Value('1', 16); | 165 | Clipperz.Crypto.ECC.BinaryField.Value.I = new Clipperz.Crypto.ECC.BinaryField.Value('1', 16); |
168 | 166 | ||
169 | Clipperz.Crypto.ECC.BinaryField.Value._xor = function(a, b, aFirstItemOffset) { | 167 | Clipperz.Crypto.ECC.BinaryField.Value._xor = function(a, b, aFirstItemOffset) { |
170 | var result; | 168 | var result; |
171 | var resultSize; | 169 | var resultSize; |
172 | var i,c; | 170 | var i,c; |
173 | var firstItemOffset; | 171 | var firstItemOffset; |
174 | 172 | ||
175 | firstItemOffset = aFirstItemOffset || 0; | 173 | firstItemOffset = aFirstItemOffset || 0; |
176 | resultSize = Math.max((a.length - firstItemOffset), b.length) + firstItemOffset; | 174 | resultSize = Math.max((a.length - firstItemOffset), b.length) + firstItemOffset; |
177 | 175 | ||
178 | result = new Array(resultSize); | 176 | result = new Array(resultSize); |
179 | 177 | ||
180 | c = firstItemOffset; | 178 | c = firstItemOffset; |
181 | for (i=0; i<c; i++) { | 179 | for (i=0; i<c; i++) { |
182 | result[i] = a[i]; | 180 | result[i] = a[i]; |
183 | } | 181 | } |
184 | 182 | ||
185 | c = resultSize; | 183 | c = resultSize; |
186 | for (i=firstItemOffset; i<c; i++) { | 184 | for (i=firstItemOffset; i<c; i++) { |
187 | result[i] = (((a[i] || 0) ^ (b[i - firstItemOffset] || 0)) >>> 0); | 185 | result[i] = (((a[i] || 0) ^ (b[i - firstItemOffset] || 0)) >>> 0); |
188 | } | 186 | } |
189 | 187 | ||
190 | return result; | 188 | return result; |
191 | }; | 189 | }; |
192 | 190 | ||
193 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor = function(a, b, aFirstItemOffset) { | 191 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor = function(a, b, aFirstItemOffset) { |
194 | var i,c; | 192 | var i,c; |
195 | var firstItemOffset; | 193 | var firstItemOffset; |
196 | 194 | ||
197 | firstItemOffset = aFirstItemOffset || 0; | 195 | firstItemOffset = aFirstItemOffset || 0; |
198 | 196 | ||
199 | c = Math.max((a.length - firstItemOffset), b.length) + firstItemOffset; | 197 | c = Math.max((a.length - firstItemOffset), b.length) + firstItemOffset; |
200 | for (i=firstItemOffset; i<c; i++) { | 198 | for (i=firstItemOffset; i<c; i++) { |
201 | a[i] = (((a[i] || 0) ^ (b[i - firstItemOffset] || 0)) >>> 0); | 199 | a[i] = (((a[i] || 0) ^ (b[i - firstItemOffset] || 0)) >>> 0); |
202 | } | 200 | } |
203 | }; | 201 | }; |
204 | 202 | ||
205 | Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft = function(aWordArray, aNumberOfBitsToShift) { | 203 | Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft = function(aWordArray, aNumberOfBitsToShift) { |
206 | var numberOfWordsToShift; | 204 | var numberOfWordsToShift; |
207 | varnumberOfBitsToShift; | 205 | varnumberOfBitsToShift; |
208 | var result; | 206 | var result; |
209 | varoverflowValue; | 207 | varoverflowValue; |
210 | vari,c; | 208 | vari,c; |
211 | 209 | ||
212 | numberOfWordsToShift = Math.floor(aNumberOfBitsToShift / 32); | 210 | numberOfWordsToShift = Math.floor(aNumberOfBitsToShift / 32); |
213 | numberOfBitsToShift = aNumberOfBitsToShift % 32; | 211 | numberOfBitsToShift = aNumberOfBitsToShift % 32; |
214 | 212 | ||
215 | result = new Array(aWordArray.length + numberOfWordsToShift); | 213 | result = new Array(aWordArray.length + numberOfWordsToShift); |
216 | 214 | ||
217 | c = numberOfWordsToShift; | 215 | c = numberOfWordsToShift; |
218 | for (i=0; i<c; i++) { | 216 | for (i=0; i<c; i++) { |
219 | result[i] = 0; | 217 | result[i] = 0; |
220 | } | 218 | } |
221 | 219 | ||
222 | overflowValue = 0; | 220 | overflowValue = 0; |
223 | nextOverflowValue = 0; | 221 | nextOverflowValue = 0; |
224 | 222 | ||
225 | c = aWordArray.length; | 223 | c = aWordArray.length; |
226 | for (i=0; i<c; i++) { | 224 | for (i=0; i<c; i++) { |
227 | varvalue; | 225 | varvalue; |
228 | varresultWord; | 226 | varresultWord; |
229 | 227 | ||
230 | // value = this.value()[i]; | 228 | // value = this.value()[i]; |
231 | value = aWordArray[i]; | 229 | value = aWordArray[i]; |
232 | 230 | ||
233 | if (numberOfBitsToShift > 0) { | 231 | if (numberOfBitsToShift > 0) { |
234 | var nextOverflowValue; | 232 | var nextOverflowValue; |
235 | 233 | ||
236 | nextOverflowValue = (value >>> (32 - numberOfBitsToShift)); | 234 | nextOverflowValue = (value >>> (32 - numberOfBitsToShift)); |
237 | value = value & (0xffffffff >>> numberOfBitsToShift); | 235 | value = value & (0xffffffff >>> numberOfBitsToShift); |
238 | resultWord = (((value << numberOfBitsToShift) | overflowValue) >>> 0); | 236 | resultWord = (((value << numberOfBitsToShift) | overflowValue) >>> 0); |
239 | } else { | 237 | } else { |
240 | resultWord = value; | 238 | resultWord = value; |
241 | } | 239 | } |
242 | 240 | ||
243 | result[i+numberOfWordsToShift] = resultWord; | 241 | result[i+numberOfWordsToShift] = resultWord; |
244 | overflowValue = nextOverflowValue; | 242 | overflowValue = nextOverflowValue; |
245 | } | 243 | } |
246 | 244 | ||
247 | if (overflowValue != 0) { | 245 | if (overflowValue != 0) { |
248 | result[aWordArray.length + numberOfWordsToShift] = overflowValue; | 246 | result[aWordArray.length + numberOfWordsToShift] = overflowValue; |
249 | } | 247 | } |
250 | 248 | ||
251 | return result; | 249 | return result; |
252 | }; | 250 | }; |
253 | 251 | ||
254 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteShiftLeft = function(aWordArray, aNumberOfBitsToShift) { | 252 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteShiftLeft = function(aWordArray, aNumberOfBitsToShift) { |
255 | var numberOfWordsToShift; | 253 | var numberOfWordsToShift; |
256 | varnumberOfBitsToShift; | 254 | varnumberOfBitsToShift; |
257 | var result; | 255 | var result; |
258 | varoverflowValue; | 256 | varoverflowValue; |
259 | vari,c; | 257 | vari,c; |
260 | 258 | ||
261 | numberOfWordsToShift = Math.floor(aNumberOfBitsToShift / 32); | 259 | numberOfWordsToShift = Math.floor(aNumberOfBitsToShift / 32); |
262 | numberOfBitsToShift = aNumberOfBitsToShift % 32; | 260 | numberOfBitsToShift = aNumberOfBitsToShift % 32; |
263 | 261 | ||
264 | result = new Array(aWordArray.length + numberOfWordsToShift); | 262 | result = new Array(aWordArray.length + numberOfWordsToShift); |
265 | 263 | ||
266 | c = numberOfWordsToShift; | 264 | c = numberOfWordsToShift; |
267 | for (i=0; i<c; i++) { | 265 | for (i=0; i<c; i++) { |
268 | result[i] = 0; | 266 | result[i] = 0; |
269 | } | 267 | } |
270 | 268 | ||
271 | overflowValue = 0; | 269 | overflowValue = 0; |
272 | nextOverflowValue = 0; | 270 | nextOverflowValue = 0; |
273 | 271 | ||
274 | c = aWordArray.length; | 272 | c = aWordArray.length; |
275 | for (i=0; i<c; i++) { | 273 | for (i=0; i<c; i++) { |
276 | varvalue; | 274 | varvalue; |
277 | varresultWord; | 275 | varresultWord; |
278 | 276 | ||
279 | // value = this.value()[i]; | 277 | // value = this.value()[i]; |
280 | value = aWordArray[i]; | 278 | value = aWordArray[i]; |
281 | 279 | ||
282 | if (numberOfBitsToShift > 0) { | 280 | if (numberOfBitsToShift > 0) { |
283 | var nextOverflowValue; | 281 | var nextOverflowValue; |
284 | 282 | ||
285 | nextOverflowValue = (value >>> (32 - numberOfBitsToShift)); | 283 | nextOverflowValue = (value >>> (32 - numberOfBitsToShift)); |
286 | value = value & (0xffffffff >>> numberOfBitsToShift); | 284 | value = value & (0xffffffff >>> numberOfBitsToShift); |
287 | resultWord = (((value << numberOfBitsToShift) | overflowValue) >>> 0); | 285 | resultWord = (((value << numberOfBitsToShift) | overflowValue) >>> 0); |
288 | } else { | 286 | } else { |
289 | resultWord = value; | 287 | resultWord = value; |
290 | } | 288 | } |
291 | 289 | ||
292 | result[i+numberOfWordsToShift] = resultWord; | 290 | result[i+numberOfWordsToShift] = resultWord; |
293 | overflowValue = nextOverflowValue; | 291 | overflowValue = nextOverflowValue; |
294 | } | 292 | } |
295 | 293 | ||
296 | if (overflowValue != 0) { | 294 | if (overflowValue != 0) { |
297 | result[aWordArray.length + numberOfWordsToShift] = overflowValue; | 295 | result[aWordArray.length + numberOfWordsToShift] = overflowValue; |
298 | } | 296 | } |
299 | 297 | ||
300 | return result; | 298 | return result; |
301 | }; | 299 | }; |
302 | 300 | ||
303 | Clipperz.Crypto.ECC.BinaryField.Value._bitSize = function(aWordArray) { | 301 | Clipperz.Crypto.ECC.BinaryField.Value._bitSize = function(aWordArray) { |
304 | varresult; | 302 | varresult; |
305 | varnotNullElements; | 303 | varnotNullElements; |
306 | var mostValuableWord; | 304 | var mostValuableWord; |
307 | var matchingBitsInMostImportantWord; | 305 | var matchingBitsInMostImportantWord; |
308 | var mask; | 306 | var mask; |
309 | var i,c; | 307 | var i,c; |
310 | 308 | ||
311 | notNullElements = aWordArray.length; | 309 | notNullElements = aWordArray.length; |
312 | 310 | ||
313 | if ((aWordArray.length == 1) && (aWordArray[0] == 0)) { | 311 | if ((aWordArray.length == 1) && (aWordArray[0] == 0)) { |
314 | result = 0; | 312 | result = 0; |
315 | } else { | 313 | } else { |
316 | while((aWordArray[notNullElements - 1] == 0) && (notNullElements > 0)) { | 314 | while((aWordArray[notNullElements - 1] == 0) && (notNullElements > 0)) { |
317 | notNullElements --; | 315 | notNullElements --; |
318 | } | 316 | } |
319 | 317 | ||
320 | result = (notNullElements - 1) * 32; | 318 | result = (notNullElements - 1) * 32; |
321 | mostValuableWord = aWordArray[notNullElements - 1]; | 319 | mostValuableWord = aWordArray[notNullElements - 1]; |
322 | 320 | ||
323 | matchingBits = 32; | 321 | matchingBits = 32; |
324 | mask = 0x80000000; | 322 | mask = 0x80000000; |
325 | 323 | ||
326 | while ((matchingBits > 0) && ((mostValuableWord & mask) == 0)) { | 324 | while ((matchingBits > 0) && ((mostValuableWord & mask) == 0)) { |
327 | matchingBits --; | 325 | matchingBits --; |
328 | mask >>>= 1; | 326 | mask >>>= 1; |
329 | } | 327 | } |
330 | 328 | ||
331 | result += matchingBits; | 329 | result += matchingBits; |
332 | } | 330 | } |
333 | 331 | ||
334 | return result; | 332 | return result; |
335 | }; | 333 | }; |
336 | 334 | ||
337 | Clipperz.Crypto.ECC.BinaryField.Value._isBitSet = function(aWordArray, aBitPosition) { | 335 | Clipperz.Crypto.ECC.BinaryField.Value._isBitSet = function(aWordArray, aBitPosition) { |
338 | var result; | 336 | var result; |
339 | varbyteIndex; | 337 | varbyteIndex; |
340 | var bitIndexInSelectedByte; | 338 | var bitIndexInSelectedByte; |
341 | 339 | ||
342 | byteIndex = Math.floor(aBitPosition / 32); | 340 | byteIndex = Math.floor(aBitPosition / 32); |
343 | bitIndexInSelectedByte = aBitPosition % 32; | 341 | bitIndexInSelectedByte = aBitPosition % 32; |
344 | 342 | ||
345 | if (byteIndex <= aWordArray.length) { | 343 | if (byteIndex <= aWordArray.length) { |
346 | result = ((aWordArray[byteIndex] & (1 << bitIndexInSelectedByte)) != 0); | 344 | result = ((aWordArray[byteIndex] & (1 << bitIndexInSelectedByte)) != 0); |
347 | } else { | 345 | } else { |
348 | result = false; | 346 | result = false; |
349 | } | 347 | } |
350 | 348 | ||
351 | return result; | 349 | return result; |
352 | }; | 350 | }; |
353 | 351 | ||
354 | Clipperz.Crypto.ECC.BinaryField.Value._compare = function(a,b) { | 352 | Clipperz.Crypto.ECC.BinaryField.Value._compare = function(a,b) { |
355 | varresult; | 353 | varresult; |
356 | var i,c; | 354 | var i,c; |
357 | 355 | ||
358 | result = MochiKit.Base.compare(a.length, b.length); | 356 | result = MochiKit.Base.compare(a.length, b.length); |
359 | 357 | ||
360 | c = a.length; | 358 | c = a.length; |
361 | for (i=0; (i<c) && (result==0); i++) { | 359 | for (i=0; (i<c) && (result==0); i++) { |
362 | //console.log("compare[" + c + " - " + i + " - 1] " + this.value()[c-i-1] + ", " + aValue.value()[c-i-1]); | 360 | //console.log("compare[" + c + " - " + i + " - 1] " + this.value()[c-i-1] + ", " + aValue.value()[c-i-1]); |
363 | // result = MochiKit.Base.compare(this.value()[c-i-1], aValue.value()[c-i-1]); | 361 | // result = MochiKit.Base.compare(this.value()[c-i-1], aValue.value()[c-i-1]); |
364 | result = MochiKit.Base.compare(a[c-i-1], b[c-i-1]); | 362 | result = MochiKit.Base.compare(a[c-i-1], b[c-i-1]); |
365 | } | 363 | } |
366 | 364 | ||
367 | return result; | 365 | return result; |
368 | }; | 366 | }; |
369 | 367 | ||
370 | 368 | ||
371 | Clipperz.Crypto.ECC.BinaryField.Value['exception']= { | 369 | Clipperz.Crypto.ECC.BinaryField.Value['exception']= { |
372 | 'UnsupportedBase': new MochiKit.Base.NamedError("Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase"), | 370 | 'UnsupportedBase': new MochiKit.Base.NamedError("Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase"), |
373 | 'UnsupportedConstructorValueType':new MochiKit.Base.NamedError("Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedConstructorValueType") | 371 | 'UnsupportedConstructorValueType':new MochiKit.Base.NamedError("Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedConstructorValueType") |
374 | }; | 372 | }; |
diff --git a/frontend/beta/js/Clipperz/Crypto/PRNG.js b/frontend/beta/js/Clipperz/Crypto/PRNG.js index 39d0045..b5c3f8a 100644 --- a/frontend/beta/js/Clipperz/Crypto/PRNG.js +++ b/frontend/beta/js/Clipperz/Crypto/PRNG.js | |||
@@ -1,406 +1,404 @@ | |||
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(); |
47 | this._maxStackLengthBeforeHashing = args.maxStackLengthBeforeHashing || 256; | 45 | this._maxStackLengthBeforeHashing = args.maxStackLengthBeforeHashing || 256; |
48 | return this; | 46 | return this; |
49 | } | 47 | } |
50 | 48 | ||
51 | Clipperz.Crypto.PRNG.EntropyAccumulator.prototype = MochiKit.Base.update(null, { | 49 | Clipperz.Crypto.PRNG.EntropyAccumulator.prototype = MochiKit.Base.update(null, { |
52 | 50 | ||
53 | 'toString': function() { | 51 | 'toString': function() { |
54 | return "Clipperz.Crypto.PRNG.EntropyAccumulator"; | 52 | return "Clipperz.Crypto.PRNG.EntropyAccumulator"; |
55 | }, | 53 | }, |
56 | 54 | ||
57 | //------------------------------------------------------------------------- | 55 | //------------------------------------------------------------------------- |
58 | 56 | ||
59 | 'stack': function() { | 57 | 'stack': function() { |
60 | return this._stack; | 58 | return this._stack; |
61 | }, | 59 | }, |
62 | 60 | ||
63 | 'setStack': function(aValue) { | 61 | 'setStack': function(aValue) { |
64 | this._stack = aValue; | 62 | this._stack = aValue; |
65 | }, | 63 | }, |
66 | 64 | ||
67 | 'resetStack': function() { | 65 | 'resetStack': function() { |
68 | this.stack().reset(); | 66 | this.stack().reset(); |
69 | }, | 67 | }, |
70 | 68 | ||
71 | 'maxStackLengthBeforeHashing': function() { | 69 | 'maxStackLengthBeforeHashing': function() { |
72 | return this._maxStackLengthBeforeHashing; | 70 | return this._maxStackLengthBeforeHashing; |
73 | }, | 71 | }, |
74 | 72 | ||
75 | //------------------------------------------------------------------------- | 73 | //------------------------------------------------------------------------- |
76 | 74 | ||
77 | 'addRandomByte': function(aValue) { | 75 | 'addRandomByte': function(aValue) { |
78 | this.stack().appendByte(aValue); | 76 | this.stack().appendByte(aValue); |
79 | 77 | ||
80 | if (this.stack().length() > this.maxStackLengthBeforeHashing()) { | 78 | if (this.stack().length() > this.maxStackLengthBeforeHashing()) { |
81 | this.setStack(Clipperz.Crypto.SHA.sha_d256(this.stack())); | 79 | this.setStack(Clipperz.Crypto.SHA.sha_d256(this.stack())); |
82 | } | 80 | } |
83 | }, | 81 | }, |
84 | 82 | ||
85 | //------------------------------------------------------------------------- | 83 | //------------------------------------------------------------------------- |
86 | __syntaxFix__: "syntax fix" | 84 | __syntaxFix__: "syntax fix" |
87 | }); | 85 | }); |
88 | 86 | ||
89 | //############################################################################# | 87 | //############################################################################# |
90 | 88 | ||
91 | Clipperz.Crypto.PRNG.RandomnessSource = function(args) { | 89 | Clipperz.Crypto.PRNG.RandomnessSource = function(args) { |
92 | args = args || {}; | 90 | args = args || {}; |
93 | MochiKit.Base.bindMethods(this); | 91 | MochiKit.Base.bindMethods(this); |
94 | 92 | ||
95 | this._generator = args.generator || null; | 93 | this._generator = args.generator || null; |
96 | this._sourceId = args.sourceId || null; | 94 | this._sourceId = args.sourceId || null; |
97 | this._boostMode = args.boostMode || false; | 95 | this._boostMode = args.boostMode || false; |
98 | 96 | ||
99 | this._nextPoolIndex = 0; | 97 | this._nextPoolIndex = 0; |
100 | 98 | ||
101 | return this; | 99 | return this; |
102 | } | 100 | } |
103 | 101 | ||
104 | Clipperz.Crypto.PRNG.RandomnessSource.prototype = MochiKit.Base.update(null, { | 102 | Clipperz.Crypto.PRNG.RandomnessSource.prototype = MochiKit.Base.update(null, { |
105 | 103 | ||
106 | 'generator': function() { | 104 | 'generator': function() { |
107 | return this._generator; | 105 | return this._generator; |
108 | }, | 106 | }, |
109 | 107 | ||
110 | 'setGenerator': function(aValue) { | 108 | 'setGenerator': function(aValue) { |
111 | this._generator = aValue; | 109 | this._generator = aValue; |
112 | }, | 110 | }, |
113 | 111 | ||
114 | //------------------------------------------------------------------------- | 112 | //------------------------------------------------------------------------- |
115 | 113 | ||
116 | 'boostMode': function() { | 114 | 'boostMode': function() { |
117 | return this._boostMode; | 115 | return this._boostMode; |
118 | }, | 116 | }, |
119 | 117 | ||
120 | 'setBoostMode': function(aValue) { | 118 | 'setBoostMode': function(aValue) { |
121 | this._boostMode = aValue; | 119 | this._boostMode = aValue; |
122 | }, | 120 | }, |
123 | 121 | ||
124 | //------------------------------------------------------------------------- | 122 | //------------------------------------------------------------------------- |
125 | 123 | ||
126 | 'sourceId': function() { | 124 | 'sourceId': function() { |
127 | return this._sourceId; | 125 | return this._sourceId; |
128 | }, | 126 | }, |
129 | 127 | ||
130 | 'setSourceId': function(aValue) { | 128 | 'setSourceId': function(aValue) { |
131 | this._sourceId = aValue; | 129 | this._sourceId = aValue; |
132 | }, | 130 | }, |
133 | 131 | ||
134 | //------------------------------------------------------------------------- | 132 | //------------------------------------------------------------------------- |
135 | 133 | ||
136 | 'nextPoolIndex': function() { | 134 | 'nextPoolIndex': function() { |
137 | return this._nextPoolIndex; | 135 | return this._nextPoolIndex; |
138 | }, | 136 | }, |
139 | 137 | ||
140 | 'incrementNextPoolIndex': function() { | 138 | 'incrementNextPoolIndex': function() { |
141 | this._nextPoolIndex = ((this._nextPoolIndex + 1) % this.generator().numberOfEntropyAccumulators()); | 139 | this._nextPoolIndex = ((this._nextPoolIndex + 1) % this.generator().numberOfEntropyAccumulators()); |
142 | }, | 140 | }, |
143 | 141 | ||
144 | //------------------------------------------------------------------------- | 142 | //------------------------------------------------------------------------- |
145 | 143 | ||
146 | 'updateGeneratorWithValue': function(aRandomValue) { | 144 | 'updateGeneratorWithValue': function(aRandomValue) { |
147 | if (this.generator() != null) { | 145 | if (this.generator() != null) { |
148 | this.generator().addRandomByte(this.sourceId(), this.nextPoolIndex(), aRandomValue); | 146 | this.generator().addRandomByte(this.sourceId(), this.nextPoolIndex(), aRandomValue); |
149 | this.incrementNextPoolIndex(); | 147 | this.incrementNextPoolIndex(); |
150 | } | 148 | } |
151 | }, | 149 | }, |
152 | 150 | ||
153 | //------------------------------------------------------------------------- | 151 | //------------------------------------------------------------------------- |
154 | __syntaxFix__: "syntax fix" | 152 | __syntaxFix__: "syntax fix" |
155 | }); | 153 | }); |
156 | 154 | ||
157 | //############################################################################# | 155 | //############################################################################# |
158 | 156 | ||
159 | Clipperz.Crypto.PRNG.TimeRandomnessSource = function(args) { | 157 | Clipperz.Crypto.PRNG.TimeRandomnessSource = function(args) { |
160 | args = args || {}; | 158 | args = args || {}; |
161 | //MochiKit.Base.bindMethods(this); | 159 | //MochiKit.Base.bindMethods(this); |
162 | 160 | ||
163 | this._intervalTime = args.intervalTime || 1000; | 161 | this._intervalTime = args.intervalTime || 1000; |
164 | 162 | ||
165 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 163 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
166 | 164 | ||
167 | this.collectEntropy(); | 165 | this.collectEntropy(); |
168 | return this; | 166 | return this; |
169 | } | 167 | } |
170 | 168 | ||
171 | Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 169 | Clipperz.Crypto.PRNG.TimeRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
172 | 170 | ||
173 | 'intervalTime': function() { | 171 | 'intervalTime': function() { |
174 | return this._intervalTime; | 172 | return this._intervalTime; |
175 | }, | 173 | }, |
176 | 174 | ||
177 | //------------------------------------------------------------------------- | 175 | //------------------------------------------------------------------------- |
178 | 176 | ||
179 | 'collectEntropy': function() { | 177 | 'collectEntropy': function() { |
180 | varnow; | 178 | varnow; |
181 | varentropyByte; | 179 | varentropyByte; |
182 | var intervalTime; | 180 | var intervalTime; |
183 | now = new Date(); | 181 | now = new Date(); |
184 | entropyByte = (now.getTime() & 0xff); | 182 | entropyByte = (now.getTime() & 0xff); |
185 | 183 | ||
186 | intervalTime = this.intervalTime(); | 184 | intervalTime = this.intervalTime(); |
187 | if (this.boostMode() == true) { | 185 | if (this.boostMode() == true) { |
188 | intervalTime = intervalTime / 9; | 186 | intervalTime = intervalTime / 9; |
189 | } | 187 | } |
190 | 188 | ||
191 | this.updateGeneratorWithValue(entropyByte); | 189 | this.updateGeneratorWithValue(entropyByte); |
192 | setTimeout(this.collectEntropy, intervalTime); | 190 | setTimeout(this.collectEntropy, intervalTime); |
193 | }, | 191 | }, |
194 | 192 | ||
195 | //------------------------------------------------------------------------- | 193 | //------------------------------------------------------------------------- |
196 | 194 | ||
197 | 'numberOfRandomBits': function() { | 195 | 'numberOfRandomBits': function() { |
198 | return 5; | 196 | return 5; |
199 | }, | 197 | }, |
200 | 198 | ||
201 | //------------------------------------------------------------------------- | 199 | //------------------------------------------------------------------------- |
202 | 200 | ||
203 | 'pollingFrequency': function() { | 201 | 'pollingFrequency': function() { |
204 | return 10; | 202 | return 10; |
205 | }, | 203 | }, |
206 | 204 | ||
207 | //------------------------------------------------------------------------- | 205 | //------------------------------------------------------------------------- |
208 | __syntaxFix__: "syntax fix" | 206 | __syntaxFix__: "syntax fix" |
209 | }); | 207 | }); |
210 | 208 | ||
211 | //***************************************************************************** | 209 | //***************************************************************************** |
212 | 210 | ||
213 | Clipperz.Crypto.PRNG.MouseRandomnessSource = function(args) { | 211 | Clipperz.Crypto.PRNG.MouseRandomnessSource = function(args) { |
214 | args = args || {}; | 212 | args = args || {}; |
215 | 213 | ||
216 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 214 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
217 | 215 | ||
218 | this._numberOfBitsToCollectAtEachEvent = 4; | 216 | this._numberOfBitsToCollectAtEachEvent = 4; |
219 | this._randomBitsCollector = 0; | 217 | this._randomBitsCollector = 0; |
220 | this._numberOfRandomBitsCollected = 0; | 218 | this._numberOfRandomBitsCollected = 0; |
221 | 219 | ||
222 | MochiKit.Signal.connect(document, 'onmousemove', this, 'collectEntropy'); | 220 | MochiKit.Signal.connect(document, 'onmousemove', this, 'collectEntropy'); |
223 | 221 | ||
224 | return this; | 222 | return this; |
225 | } | 223 | } |
226 | 224 | ||
227 | Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 225 | Clipperz.Crypto.PRNG.MouseRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
228 | 226 | ||
229 | //------------------------------------------------------------------------- | 227 | //------------------------------------------------------------------------- |
230 | 228 | ||
231 | 'numberOfBitsToCollectAtEachEvent': function() { | 229 | 'numberOfBitsToCollectAtEachEvent': function() { |
232 | return this._numberOfBitsToCollectAtEachEvent; | 230 | return this._numberOfBitsToCollectAtEachEvent; |
233 | }, | 231 | }, |
234 | 232 | ||
235 | //------------------------------------------------------------------------- | 233 | //------------------------------------------------------------------------- |
236 | 234 | ||
237 | 'randomBitsCollector': function() { | 235 | 'randomBitsCollector': function() { |
238 | return this._randomBitsCollector; | 236 | return this._randomBitsCollector; |
239 | }, | 237 | }, |
240 | 238 | ||
241 | 'setRandomBitsCollector': function(aValue) { | 239 | 'setRandomBitsCollector': function(aValue) { |
242 | this._randomBitsCollector = aValue; | 240 | this._randomBitsCollector = aValue; |
243 | }, | 241 | }, |
244 | 242 | ||
245 | 'appendRandomBitsToRandomBitsCollector': function(aValue) { | 243 | 'appendRandomBitsToRandomBitsCollector': function(aValue) { |
246 | var collectedBits; | 244 | var collectedBits; |
247 | var numberOfRandomBitsCollected; | 245 | var numberOfRandomBitsCollected; |
248 | 246 | ||
249 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); | 247 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); |
250 | collectetBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); | 248 | collectetBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); |
251 | this.setRandomBitsCollector(collectetBits); | 249 | this.setRandomBitsCollector(collectetBits); |
252 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); | 250 | numberOfRandomBitsCollected += this.numberOfBitsToCollectAtEachEvent(); |
253 | 251 | ||
254 | if (numberOfRandomBitsCollected == 8) { | 252 | if (numberOfRandomBitsCollected == 8) { |
255 | this.updateGeneratorWithValue(collectetBits); | 253 | this.updateGeneratorWithValue(collectetBits); |
256 | numberOfRandomBitsCollected = 0; | 254 | numberOfRandomBitsCollected = 0; |
257 | this.setRandomBitsCollector(0); | 255 | this.setRandomBitsCollector(0); |
258 | } | 256 | } |
259 | 257 | ||
260 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) | 258 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) |
261 | }, | 259 | }, |
262 | 260 | ||
263 | //------------------------------------------------------------------------- | 261 | //------------------------------------------------------------------------- |
264 | 262 | ||
265 | 'numberOfRandomBitsCollected': function() { | 263 | 'numberOfRandomBitsCollected': function() { |
266 | return this._numberOfRandomBitsCollected; | 264 | return this._numberOfRandomBitsCollected; |
267 | }, | 265 | }, |
268 | 266 | ||
269 | 'setNumberOfRandomBitsCollected': function(aValue) { | 267 | 'setNumberOfRandomBitsCollected': function(aValue) { |
270 | this._numberOfRandomBitsCollected = aValue; | 268 | this._numberOfRandomBitsCollected = aValue; |
271 | }, | 269 | }, |
272 | 270 | ||
273 | //------------------------------------------------------------------------- | 271 | //------------------------------------------------------------------------- |
274 | 272 | ||
275 | 'collectEntropy': function(anEvent) { | 273 | 'collectEntropy': function(anEvent) { |
276 | var mouseLocation; | 274 | var mouseLocation; |
277 | var randomBit; | 275 | var randomBit; |
278 | var mask; | 276 | var mask; |
279 | 277 | ||
280 | mask = 0xffffffff >>> (32 - this.numberOfBitsToCollectAtEachEvent()); | 278 | mask = 0xffffffff >>> (32 - this.numberOfBitsToCollectAtEachEvent()); |
281 | 279 | ||
282 | mouseLocation = anEvent.mouse().client; | 280 | mouseLocation = anEvent.mouse().client; |
283 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & mask); | 281 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & mask); |
284 | this.appendRandomBitsToRandomBitsCollector(randomBit) | 282 | this.appendRandomBitsToRandomBitsCollector(randomBit) |
285 | }, | 283 | }, |
286 | 284 | ||
287 | //------------------------------------------------------------------------- | 285 | //------------------------------------------------------------------------- |
288 | 286 | ||
289 | 'numberOfRandomBits': function() { | 287 | 'numberOfRandomBits': function() { |
290 | return 1; | 288 | return 1; |
291 | }, | 289 | }, |
292 | 290 | ||
293 | //------------------------------------------------------------------------- | 291 | //------------------------------------------------------------------------- |
294 | 292 | ||
295 | 'pollingFrequency': function() { | 293 | 'pollingFrequency': function() { |
296 | return 10; | 294 | return 10; |
297 | }, | 295 | }, |
298 | 296 | ||
299 | //------------------------------------------------------------------------- | 297 | //------------------------------------------------------------------------- |
300 | __syntaxFix__: "syntax fix" | 298 | __syntaxFix__: "syntax fix" |
301 | }); | 299 | }); |
302 | 300 | ||
303 | //***************************************************************************** | 301 | //***************************************************************************** |
304 | 302 | ||
305 | Clipperz.Crypto.PRNG.KeyboardRandomnessSource = function(args) { | 303 | Clipperz.Crypto.PRNG.KeyboardRandomnessSource = function(args) { |
306 | args = args || {}; | 304 | args = args || {}; |
307 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); | 305 | Clipperz.Crypto.PRNG.RandomnessSource.call(this, args); |
308 | 306 | ||
309 | this._randomBitsCollector = 0; | 307 | this._randomBitsCollector = 0; |
310 | this._numberOfRandomBitsCollected = 0; | 308 | this._numberOfRandomBitsCollected = 0; |
311 | 309 | ||
312 | MochiKit.Signal.connect(document, 'onkeypress', this, 'collectEntropy'); | 310 | MochiKit.Signal.connect(document, 'onkeypress', this, 'collectEntropy'); |
313 | 311 | ||
314 | return this; | 312 | return this; |
315 | } | 313 | } |
316 | 314 | ||
317 | Clipperz.Crypto.PRNG.KeyboardRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { | 315 | Clipperz.Crypto.PRNG.KeyboardRandomnessSource.prototype = MochiKit.Base.update(new Clipperz.Crypto.PRNG.RandomnessSource, { |
318 | 316 | ||
319 | //------------------------------------------------------------------------- | 317 | //------------------------------------------------------------------------- |
320 | 318 | ||
321 | 'randomBitsCollector': function() { | 319 | 'randomBitsCollector': function() { |
322 | return this._randomBitsCollector; | 320 | return this._randomBitsCollector; |
323 | }, | 321 | }, |
324 | 322 | ||
325 | 'setRandomBitsCollector': function(aValue) { | 323 | 'setRandomBitsCollector': function(aValue) { |
326 | this._randomBitsCollector = aValue; | 324 | this._randomBitsCollector = aValue; |
327 | }, | 325 | }, |
328 | 326 | ||
329 | 'appendRandomBitToRandomBitsCollector': function(aValue) { | 327 | 'appendRandomBitToRandomBitsCollector': function(aValue) { |
330 | var collectedBits; | 328 | var collectedBits; |
331 | var numberOfRandomBitsCollected; | 329 | var numberOfRandomBitsCollected; |
332 | 330 | ||
333 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); | 331 | numberOfRandomBitsCollected = this.numberOfRandomBitsCollected(); |
334 | collectetBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); | 332 | collectetBits = this.randomBitsCollector() | (aValue << numberOfRandomBitsCollected); |
335 | this.setRandomBitsCollector(collectetBits); | 333 | this.setRandomBitsCollector(collectetBits); |
336 | numberOfRandomBitsCollected ++; | 334 | numberOfRandomBitsCollected ++; |
337 | 335 | ||
338 | if (numberOfRandomBitsCollected == 8) { | 336 | if (numberOfRandomBitsCollected == 8) { |
339 | this.updateGeneratorWithValue(collectetBits); | 337 | this.updateGeneratorWithValue(collectetBits); |
340 | numberOfRandomBitsCollected = 0; | 338 | numberOfRandomBitsCollected = 0; |
341 | this.setRandomBitsCollector(0); | 339 | this.setRandomBitsCollector(0); |
342 | } | 340 | } |
343 | 341 | ||
344 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) | 342 | this.setNumberOfRandomBitsCollected(numberOfRandomBitsCollected) |
345 | }, | 343 | }, |
346 | 344 | ||
347 | //------------------------------------------------------------------------- | 345 | //------------------------------------------------------------------------- |
348 | 346 | ||
349 | 'numberOfRandomBitsCollected': function() { | 347 | 'numberOfRandomBitsCollected': function() { |
350 | return this._numberOfRandomBitsCollected; | 348 | return this._numberOfRandomBitsCollected; |
351 | }, | 349 | }, |
352 | 350 | ||
353 | 'setNumberOfRandomBitsCollected': function(aValue) { | 351 | 'setNumberOfRandomBitsCollected': function(aValue) { |
354 | this._numberOfRandomBitsCollected = aValue; | 352 | this._numberOfRandomBitsCollected = aValue; |
355 | }, | 353 | }, |
356 | 354 | ||
357 | //------------------------------------------------------------------------- | 355 | //------------------------------------------------------------------------- |
358 | 356 | ||
359 | 'collectEntropy': function(anEvent) { | 357 | 'collectEntropy': function(anEvent) { |
360 | /* | 358 | /* |
361 | var mouseLocation; | 359 | var mouseLocation; |
362 | var randomBit; | 360 | var randomBit; |
363 | 361 | ||
364 | mouseLocation = anEvent.mouse().client; | 362 | mouseLocation = anEvent.mouse().client; |
365 | 363 | ||
366 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & 0x1); | 364 | randomBit = ((mouseLocation.x ^ mouseLocation.y) & 0x1); |
367 | this.appendRandomBitToRandomBitsCollector(randomBit); | 365 | this.appendRandomBitToRandomBitsCollector(randomBit); |
368 | */ | 366 | */ |
369 | }, | 367 | }, |
370 | 368 | ||
371 | //------------------------------------------------------------------------- | 369 | //------------------------------------------------------------------------- |
372 | 370 | ||
373 | 'numberOfRandomBits': function() { | 371 | 'numberOfRandomBits': function() { |
374 | return 1; | 372 | return 1; |
375 | }, | 373 | }, |
376 | 374 | ||
377 | //------------------------------------------------------------------------- | 375 | //------------------------------------------------------------------------- |
378 | 376 | ||
379 | 'pollingFrequency': function() { | 377 | 'pollingFrequency': function() { |
380 | return 10; | 378 | return 10; |
381 | }, | 379 | }, |
382 | 380 | ||
383 | //------------------------------------------------------------------------- | 381 | //------------------------------------------------------------------------- |
384 | __syntaxFix__: "syntax fix" | 382 | __syntaxFix__: "syntax fix" |
385 | }); | 383 | }); |
386 | 384 | ||
387 | //############################################################################# | 385 | //############################################################################# |
388 | 386 | ||
389 | Clipperz.Crypto.PRNG.Fortuna = function(args) { | 387 | Clipperz.Crypto.PRNG.Fortuna = function(args) { |
390 | vari,c; | 388 | vari,c; |
391 | 389 | ||
392 | args = args || {}; | 390 | args = args || {}; |
393 | 391 | ||
394 | this._key = args.seed || null; | 392 | this._key = args.seed || null; |
395 | if (this._key == null) { | 393 | if (this._key == null) { |
396 | this._counter = 0; | 394 | this._counter = 0; |
397 | this._key = new Clipperz.ByteArray(); | 395 | this._key = new Clipperz.ByteArray(); |
398 | } else { | 396 | } else { |
399 | this._counter = 1; | 397 | this._counter = 1; |
400 | } | 398 | } |
401 | 399 | ||
402 | this._aesKey = null; | 400 | this._aesKey = null; |
403 | 401 | ||
404 | this._firstPoolReseedLevel = args.firstPoolReseedLevel || 32 || 64; | 402 | this._firstPoolReseedLevel = args.firstPoolReseedLevel || 32 || 64; |
405 | this._numberOfEntropyAccumulators = args.numberOfEntropyAccumulators || 32; | 403 | this._numberOfEntropyAccumulators = args.numberOfEntropyAccumulators || 32; |
406 | 404 | ||
diff --git a/frontend/beta/js/Clipperz/Crypto/RSA.js b/frontend/beta/js/Clipperz/Crypto/RSA.js index 6844dba..5a480f1 100644 --- a/frontend/beta/js/Clipperz/Crypto/RSA.js +++ b/frontend/beta/js/Clipperz/Crypto/RSA.js | |||
@@ -1,148 +1,146 @@ | |||
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.Crypto.BigInt) == 'undefined') { throw ""; }} catch (e) { | 24 | try { if (typeof(Clipperz.Crypto.BigInt) == 'undefined') { throw ""; }} catch (e) { |
27 | throw "Clipperz.Crypto.RSA depends on Clipperz.Crypto.BigInt!"; | 25 | throw "Clipperz.Crypto.RSA depends on Clipperz.Crypto.BigInt!"; |
28 | } | 26 | } |
29 | 27 | ||
30 | if (typeof(Clipperz.Crypto.RSA) == 'undefined') { Clipperz.Crypto.RSA = {}; } | 28 | if (typeof(Clipperz.Crypto.RSA) == 'undefined') { Clipperz.Crypto.RSA = {}; } |
31 | 29 | ||
32 | Clipperz.Crypto.RSA.VERSION = "0.1"; | 30 | Clipperz.Crypto.RSA.VERSION = "0.1"; |
33 | Clipperz.Crypto.RSA.NAME = "Clipperz.RSA"; | 31 | Clipperz.Crypto.RSA.NAME = "Clipperz.RSA"; |
34 | 32 | ||
35 | //############################################################################# | 33 | //############################################################################# |
36 | 34 | ||
37 | MochiKit.Base.update(Clipperz.Crypto.RSA, { | 35 | MochiKit.Base.update(Clipperz.Crypto.RSA, { |
38 | 36 | ||
39 | //------------------------------------------------------------------------- | 37 | //------------------------------------------------------------------------- |
40 | 38 | ||
41 | 'publicKeyWithValues': function (e, d, n) { | 39 | 'publicKeyWithValues': function (e, d, n) { |
42 | varresult; | 40 | varresult; |
43 | 41 | ||
44 | result = {}; | 42 | result = {}; |
45 | 43 | ||
46 | if (e.isBigInt) { | 44 | if (e.isBigInt) { |
47 | result.e = e; | 45 | result.e = e; |
48 | } else { | 46 | } else { |
49 | result.e = new Clipperz.Crypto.BigInt(e, 16); | 47 | result.e = new Clipperz.Crypto.BigInt(e, 16); |
50 | } | 48 | } |
51 | 49 | ||
52 | if (d.isBigInt) { | 50 | if (d.isBigInt) { |
53 | result.d = d; | 51 | result.d = d; |
54 | } else { | 52 | } else { |
55 | result.d = new Clipperz.Crypto.BigInt(d, 16); | 53 | result.d = new Clipperz.Crypto.BigInt(d, 16); |
56 | } | 54 | } |
57 | 55 | ||
58 | if (n.isBigInt) { | 56 | if (n.isBigInt) { |
59 | result.n = n; | 57 | result.n = n; |
60 | } else { | 58 | } else { |
61 | result.n = new Clipperz.Crypto.BigInt(n, 16); | 59 | result.n = new Clipperz.Crypto.BigInt(n, 16); |
62 | } | 60 | } |
63 | 61 | ||
64 | return result; | 62 | return result; |
65 | }, | 63 | }, |
66 | 64 | ||
67 | 'privateKeyWithValues': function(e, d, n) { | 65 | 'privateKeyWithValues': function(e, d, n) { |
68 | return Clipperz.Crypto.RSA.publicKeyWithValues(e, d, n); | 66 | return Clipperz.Crypto.RSA.publicKeyWithValues(e, d, n); |
69 | }, | 67 | }, |
70 | 68 | ||
71 | //----------------------------------------------------------------------------- | 69 | //----------------------------------------------------------------------------- |
72 | 70 | ||
73 | 'encryptUsingPublicKey': function (aKey, aMessage) { | 71 | 'encryptUsingPublicKey': function (aKey, aMessage) { |
74 | varmessageValue; | 72 | varmessageValue; |
75 | varresult; | 73 | varresult; |
76 | 74 | ||
77 | messageValue = new Clipperz.Crypto.BigInt(aMessage, 16); | 75 | messageValue = new Clipperz.Crypto.BigInt(aMessage, 16); |
78 | result = messageValue.powerModule(aKey.e, aKey.n); | 76 | result = messageValue.powerModule(aKey.e, aKey.n); |
79 | 77 | ||
80 | return result.asString(16); | 78 | return result.asString(16); |
81 | }, | 79 | }, |
82 | 80 | ||
83 | //............................................................................. | 81 | //............................................................................. |
84 | 82 | ||
85 | 'decryptUsingPublicKey': function (aKey, aMessage) { | 83 | 'decryptUsingPublicKey': function (aKey, aMessage) { |
86 | return Clipperz.Crypto.RSA.encryptUsingPublicKey(aKey, aMessage); | 84 | return Clipperz.Crypto.RSA.encryptUsingPublicKey(aKey, aMessage); |
87 | }, | 85 | }, |
88 | 86 | ||
89 | //----------------------------------------------------------------------------- | 87 | //----------------------------------------------------------------------------- |
90 | 88 | ||
91 | 'encryptUsingPrivateKey': function (aKey, aMessage) { | 89 | 'encryptUsingPrivateKey': function (aKey, aMessage) { |
92 | varmessageValue; | 90 | varmessageValue; |
93 | varresult; | 91 | varresult; |
94 | 92 | ||
95 | messageValue = new Clipperz.Crypto.BigInt(aMessage, 16); | 93 | messageValue = new Clipperz.Crypto.BigInt(aMessage, 16); |
96 | result = messageValue.powerModule(aKey.d, aKey.n); | 94 | result = messageValue.powerModule(aKey.d, aKey.n); |
97 | 95 | ||
98 | return result.asString(16); | 96 | return result.asString(16); |
99 | }, | 97 | }, |
100 | 98 | ||
101 | //............................................................................. | 99 | //............................................................................. |
102 | 100 | ||
103 | 'decryptUsingPrivateKey': function (aKey, aMessage) { | 101 | 'decryptUsingPrivateKey': function (aKey, aMessage) { |
104 | return Clipperz.Crypto.RSA.encryptUsingPrivateKey(aKey, aMessage); | 102 | return Clipperz.Crypto.RSA.encryptUsingPrivateKey(aKey, aMessage); |
105 | }, | 103 | }, |
106 | 104 | ||
107 | //----------------------------------------------------------------------------- | 105 | //----------------------------------------------------------------------------- |
108 | 106 | ||
109 | 'generatePublicKey': function(aNumberOfBits) { | 107 | 'generatePublicKey': function(aNumberOfBits) { |
110 | varresult; | 108 | varresult; |
111 | vare; | 109 | vare; |
112 | vard; | 110 | vard; |
113 | varn; | 111 | varn; |
114 | 112 | ||
115 | e = new Clipperz.Crypto.BigInt("10001", 16); | 113 | e = new Clipperz.Crypto.BigInt("10001", 16); |
116 | 114 | ||
117 | { | 115 | { |
118 | var p, q; | 116 | var p, q; |
119 | varphi; | 117 | varphi; |
120 | 118 | ||
121 | do { | 119 | do { |
122 | p = Clipperz.Crypto.BigInt.randomPrime(aNumberOfBits); | 120 | p = Clipperz.Crypto.BigInt.randomPrime(aNumberOfBits); |
123 | } while (p.module(e).equals(1)); | 121 | } while (p.module(e).equals(1)); |
124 | 122 | ||
125 | do { | 123 | do { |
126 | q = Clipperz.Crypto.BigInt.randomPrime(aNumberOfBits); | 124 | q = Clipperz.Crypto.BigInt.randomPrime(aNumberOfBits); |
127 | } while ((q.equals(p)) || (q.module(e).equals(1))); | 125 | } while ((q.equals(p)) || (q.module(e).equals(1))); |
128 | 126 | ||
129 | n = p.multiply(q); | 127 | n = p.multiply(q); |
130 | phi = (p.subtract(1).multiply(q.subtract(1))); | 128 | phi = (p.subtract(1).multiply(q.subtract(1))); |
131 | d = e.powerModule(-1, phi); | 129 | d = e.powerModule(-1, phi); |
132 | } | 130 | } |
133 | 131 | ||
134 | result = Clipperz.Crypto.RSA.publicKeyWithValues(e, d, n); | 132 | result = Clipperz.Crypto.RSA.publicKeyWithValues(e, d, n); |
135 | 133 | ||
136 | return result; | 134 | return result; |
137 | }, | 135 | }, |
138 | 136 | ||
139 | //------------------------------------------------------------------------- | 137 | //------------------------------------------------------------------------- |
140 | 138 | ||
141 | __syntaxFix__: "syntax fix" | 139 | __syntaxFix__: "syntax fix" |
142 | 140 | ||
143 | //------------------------------------------------------------------------- | 141 | //------------------------------------------------------------------------- |
144 | 142 | ||
145 | }); | 143 | }); |
146 | 144 | ||
147 | //############################################################################# | 145 | //############################################################################# |
148 | 146 | ||
diff --git a/frontend/beta/js/Clipperz/Crypto/SHA.js b/frontend/beta/js/Clipperz/Crypto/SHA.js index 635eb90..9605d1c 100644 --- a/frontend/beta/js/Clipperz/Crypto/SHA.js +++ b/frontend/beta/js/Clipperz/Crypto/SHA.js | |||
@@ -1,293 +1,291 @@ | |||
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 | if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; } | 28 | if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; } |
31 | if (typeof(Clipperz.Crypto.SHA) == 'undefined') { Clipperz.Crypto.SHA = {}; } | 29 | if (typeof(Clipperz.Crypto.SHA) == 'undefined') { Clipperz.Crypto.SHA = {}; } |
32 | 30 | ||
33 | Clipperz.Crypto.SHA.VERSION = "0.3"; | 31 | Clipperz.Crypto.SHA.VERSION = "0.3"; |
34 | Clipperz.Crypto.SHA.NAME = "Clipperz.Crypto.SHA"; | 32 | Clipperz.Crypto.SHA.NAME = "Clipperz.Crypto.SHA"; |
35 | 33 | ||
36 | MochiKit.Base.update(Clipperz.Crypto.SHA, { | 34 | MochiKit.Base.update(Clipperz.Crypto.SHA, { |
37 | 35 | ||
38 | '__repr__': function () { | 36 | '__repr__': function () { |
39 | return "[" + this.NAME + " " + this.VERSION + "]"; | 37 | return "[" + this.NAME + " " + this.VERSION + "]"; |
40 | }, | 38 | }, |
41 | 39 | ||
42 | 'toString': function () { | 40 | 'toString': function () { |
43 | return this.__repr__(); | 41 | return this.__repr__(); |
44 | }, | 42 | }, |
45 | 43 | ||
46 | //----------------------------------------------------------------------------- | 44 | //----------------------------------------------------------------------------- |
47 | 45 | ||
48 | 'rotateRight': function(aValue, aNumberOfBits) { | 46 | 'rotateRight': function(aValue, aNumberOfBits) { |
49 | //Clipperz.Profile.start("Clipperz.Crypto.SHA.rotateRight"); | 47 | //Clipperz.Profile.start("Clipperz.Crypto.SHA.rotateRight"); |
50 | var result; | 48 | var result; |
51 | 49 | ||
52 | result = (aValue >>> aNumberOfBits) | (aValue << (32 - aNumberOfBits)); | 50 | result = (aValue >>> aNumberOfBits) | (aValue << (32 - aNumberOfBits)); |
53 | 51 | ||
54 | //Clipperz.Profile.stop("Clipperz.Crypto.SHA.rotateRight"); | 52 | //Clipperz.Profile.stop("Clipperz.Crypto.SHA.rotateRight"); |
55 | return result; | 53 | return result; |
56 | }, | 54 | }, |
57 | 55 | ||
58 | 'shiftRight': function(aValue, aNumberOfBits) { | 56 | 'shiftRight': function(aValue, aNumberOfBits) { |
59 | //Clipperz.Profile.start("Clipperz.Crypto.SHA.shiftRight"); | 57 | //Clipperz.Profile.start("Clipperz.Crypto.SHA.shiftRight"); |
60 | var result; | 58 | var result; |
61 | 59 | ||
62 | result = aValue >>> aNumberOfBits; | 60 | result = aValue >>> aNumberOfBits; |
63 | 61 | ||
64 | //Clipperz.Profile.stop("Clipperz.Crypto.SHA.shiftRight"); | 62 | //Clipperz.Profile.stop("Clipperz.Crypto.SHA.shiftRight"); |
65 | return result; | 63 | return result; |
66 | }, | 64 | }, |
67 | 65 | ||
68 | //----------------------------------------------------------------------------- | 66 | //----------------------------------------------------------------------------- |
69 | 67 | ||
70 | 'safeAdd': function() { | 68 | 'safeAdd': function() { |
71 | //Clipperz.Profile.start("Clipperz.Crypto.SHA.safeAdd"); | 69 | //Clipperz.Profile.start("Clipperz.Crypto.SHA.safeAdd"); |
72 | varresult; | 70 | varresult; |
73 | vari, c; | 71 | vari, c; |
74 | 72 | ||
75 | result = arguments[0]; | 73 | result = arguments[0]; |
76 | c = arguments.length; | 74 | c = arguments.length; |
77 | for (i=1; i<c; i++) { | 75 | for (i=1; i<c; i++) { |
78 | varlowerBytesSum; | 76 | varlowerBytesSum; |
79 | 77 | ||
80 | lowerBytesSum = (result & 0xffff) + (arguments[i] & 0xffff); | 78 | lowerBytesSum = (result & 0xffff) + (arguments[i] & 0xffff); |
81 | result = (((result >> 16) + (arguments[i] >> 16) + (lowerBytesSum >> 16)) << 16) | (lowerBytesSum & 0xffff); | 79 | result = (((result >> 16) + (arguments[i] >> 16) + (lowerBytesSum >> 16)) << 16) | (lowerBytesSum & 0xffff); |
82 | } | 80 | } |
83 | 81 | ||
84 | //Clipperz.Profile.stop("Clipperz.Crypto.SHA.safeAdd"); | 82 | //Clipperz.Profile.stop("Clipperz.Crypto.SHA.safeAdd"); |
85 | return result; | 83 | return result; |
86 | }, | 84 | }, |
87 | 85 | ||
88 | //----------------------------------------------------------------------------- | 86 | //----------------------------------------------------------------------------- |
89 | 87 | ||
90 | 'sha256_array': function(aValue) { | 88 | 'sha256_array': function(aValue) { |
91 | //Clipperz.Profile.start("Clipperz.Crypto.SHA.sha256_array"); | 89 | //Clipperz.Profile.start("Clipperz.Crypto.SHA.sha256_array"); |
92 | varresult; | 90 | varresult; |
93 | varmessage; | 91 | varmessage; |
94 | var h0, h1, h2, h3, h4, h5, h6, h7; | 92 | var h0, h1, h2, h3, h4, h5, h6, h7; |
95 | vark; | 93 | vark; |
96 | varmessageLength; | 94 | varmessageLength; |
97 | varmessageLengthInBits; | 95 | varmessageLengthInBits; |
98 | var_i, _c; | 96 | var_i, _c; |
99 | var charBits; | 97 | var charBits; |
100 | var rotateRight; | 98 | var rotateRight; |
101 | var shiftRight; | 99 | var shiftRight; |
102 | var safeAdd; | 100 | var safeAdd; |
103 | varbytesPerBlock; | 101 | varbytesPerBlock; |
104 | var currentMessageIndex; | 102 | var currentMessageIndex; |
105 | 103 | ||
106 | bytesPerBlock = 512/8; | 104 | bytesPerBlock = 512/8; |
107 | rotateRight = Clipperz.Crypto.SHA.rotateRight; | 105 | rotateRight = Clipperz.Crypto.SHA.rotateRight; |
108 | shiftRight = Clipperz.Crypto.SHA.shiftRight; | 106 | shiftRight = Clipperz.Crypto.SHA.shiftRight; |
109 | safeAdd = Clipperz.Crypto.SHA.safeAdd; | 107 | safeAdd = Clipperz.Crypto.SHA.safeAdd; |
110 | 108 | ||
111 | charBits = 8; | 109 | charBits = 8; |
112 | 110 | ||
113 | h0 = 0x6a09e667; | 111 | h0 = 0x6a09e667; |
114 | h1 = 0xbb67ae85; | 112 | h1 = 0xbb67ae85; |
115 | h2 = 0x3c6ef372; | 113 | h2 = 0x3c6ef372; |
116 | h3 = 0xa54ff53a; | 114 | h3 = 0xa54ff53a; |
117 | h4 = 0x510e527f; | 115 | h4 = 0x510e527f; |
118 | h5 = 0x9b05688c; | 116 | h5 = 0x9b05688c; |
119 | h6 = 0x1f83d9ab; | 117 | h6 = 0x1f83d9ab; |
120 | h7 = 0x5be0cd19; | 118 | h7 = 0x5be0cd19; |
121 | 119 | ||
122 | k = [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, | 120 | k = [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
123 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, | 121 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
124 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, | 122 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
125 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, | 123 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
126 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, | 124 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
127 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, | 125 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
128 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, | 126 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
129 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]; | 127 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]; |
130 | 128 | ||
131 | message = aValue; | 129 | message = aValue; |
132 | messageLength = message.length; | 130 | messageLength = message.length; |
133 | 131 | ||
134 | //Pre-processing: | 132 | //Pre-processing: |
135 | message.push(0x80); //append a single "1" bit to message | 133 | message.push(0x80); //append a single "1" bit to message |
136 | 134 | ||
137 | _c = (512 - (((messageLength + 1) * charBits) % 512) - 64) / charBits; | 135 | _c = (512 - (((messageLength + 1) * charBits) % 512) - 64) / charBits; |
138 | for (_i=0; _i<_c; _i++) { | 136 | for (_i=0; _i<_c; _i++) { |
139 | message.push(0x00); //append "0" bits until message length ≡ 448 ≡ -64 (mod 512) | 137 | message.push(0x00); //append "0" bits until message length ≡ 448 ≡ -64 (mod 512) |
140 | } | 138 | } |
141 | messageLengthInBits = messageLength * charBits; | 139 | messageLengthInBits = messageLength * charBits; |
142 | message.push(0x00); //the 4 most high byte are alway 0 as message length is represented with a 32bit value; | 140 | message.push(0x00); //the 4 most high byte are alway 0 as message length is represented with a 32bit value; |
143 | message.push(0x00); | 141 | message.push(0x00); |
144 | message.push(0x00); | 142 | message.push(0x00); |
145 | message.push(0x00); | 143 | message.push(0x00); |
146 | message.push((messageLengthInBits >> 24)& 0xff); | 144 | message.push((messageLengthInBits >> 24)& 0xff); |
147 | message.push((messageLengthInBits >> 16)& 0xff); | 145 | message.push((messageLengthInBits >> 16)& 0xff); |
148 | message.push((messageLengthInBits >> 8) & 0xff); | 146 | message.push((messageLengthInBits >> 8) & 0xff); |
149 | message.push( messageLengthInBits & 0xff); | 147 | message.push( messageLengthInBits & 0xff); |
150 | 148 | ||
151 | currentMessageIndex = 0; | 149 | currentMessageIndex = 0; |
152 | while(currentMessageIndex < message.length) { | 150 | while(currentMessageIndex < message.length) { |
153 | varw; | 151 | varw; |
154 | vara, b, c, d, e, f, g, h; | 152 | vara, b, c, d, e, f, g, h; |
155 | 153 | ||
156 | w = Array(64); | 154 | w = Array(64); |
157 | 155 | ||
158 | _c = 16; | 156 | _c = 16; |
159 | for (_i=0; _i<_c; _i++) { | 157 | for (_i=0; _i<_c; _i++) { |
160 | var _j; | 158 | var _j; |
161 | 159 | ||
162 | _j = currentMessageIndex + _i*4; | 160 | _j = currentMessageIndex + _i*4; |
163 | w[_i] = (message[_j] << 24) | (message[_j + 1] << 16) | (message[_j + 2] << 8) | (message[_j + 3] << 0); | 161 | w[_i] = (message[_j] << 24) | (message[_j + 1] << 16) | (message[_j + 2] << 8) | (message[_j + 3] << 0); |
164 | } | 162 | } |
165 | 163 | ||
166 | _c = 64; | 164 | _c = 64; |
167 | for (_i=16; _i<_c; _i++) { | 165 | for (_i=16; _i<_c; _i++) { |
168 | vars0, s1; | 166 | vars0, s1; |
169 | 167 | ||
170 | s0 = (rotateRight(w[_i-15], 7)) ^ (rotateRight(w[_i-15], 18)) ^ (shiftRight(w[_i-15], 3)); | 168 | s0 = (rotateRight(w[_i-15], 7)) ^ (rotateRight(w[_i-15], 18)) ^ (shiftRight(w[_i-15], 3)); |
171 | s1 = (rotateRight(w[_i-2], 17)) ^ (rotateRight(w[_i-2], 19)) ^ (shiftRight(w[_i-2], 10)); | 169 | s1 = (rotateRight(w[_i-2], 17)) ^ (rotateRight(w[_i-2], 19)) ^ (shiftRight(w[_i-2], 10)); |
172 | w[_i] = safeAdd(w[_i-16], s0, w[_i-7], s1); | 170 | w[_i] = safeAdd(w[_i-16], s0, w[_i-7], s1); |
173 | } | 171 | } |
174 | 172 | ||
175 | a=h0; b=h1; c=h2; d=h3; e=h4; f=h5; g=h6; h=h7; | 173 | a=h0; b=h1; c=h2; d=h3; e=h4; f=h5; g=h6; h=h7; |
176 | 174 | ||
177 | _c = 64; | 175 | _c = 64; |
178 | for (_i=0; _i<_c; _i++) { | 176 | for (_i=0; _i<_c; _i++) { |
179 | var s0, s1, ch, maj, t1, t2; | 177 | var s0, s1, ch, maj, t1, t2; |
180 | 178 | ||
181 | s0 = (rotateRight(a, 2)) ^ (rotateRight(a, 13)) ^ (rotateRight(a, 22)); | 179 | s0 = (rotateRight(a, 2)) ^ (rotateRight(a, 13)) ^ (rotateRight(a, 22)); |
182 | maj = (a & b) ^ (a & c) ^ (b & c); | 180 | maj = (a & b) ^ (a & c) ^ (b & c); |
183 | t2 = safeAdd(s0, maj); | 181 | t2 = safeAdd(s0, maj); |
184 | s1 = (rotateRight(e, 6)) ^ (rotateRight(e, 11)) ^ (rotateRight(e, 25)); | 182 | s1 = (rotateRight(e, 6)) ^ (rotateRight(e, 11)) ^ (rotateRight(e, 25)); |
185 | ch = (e & f) ^ ((~e) & g); | 183 | ch = (e & f) ^ ((~e) & g); |
186 | t1 = safeAdd(h, s1, ch, k[_i], w[_i]); | 184 | t1 = safeAdd(h, s1, ch, k[_i], w[_i]); |
187 | 185 | ||
188 | h = g; | 186 | h = g; |
189 | g = f; | 187 | g = f; |
190 | f = e; | 188 | f = e; |
191 | e = safeAdd(d, t1); | 189 | e = safeAdd(d, t1); |
192 | d = c; | 190 | d = c; |
193 | c = b; | 191 | c = b; |
194 | b = a; | 192 | b = a; |
195 | a = safeAdd(t1, t2); | 193 | a = safeAdd(t1, t2); |
196 | } | 194 | } |
197 | 195 | ||
198 | h0 = safeAdd(h0, a); | 196 | h0 = safeAdd(h0, a); |
199 | h1 = safeAdd(h1, b); | 197 | h1 = safeAdd(h1, b); |
200 | h2 = safeAdd(h2, c); | 198 | h2 = safeAdd(h2, c); |
201 | h3 = safeAdd(h3, d); | 199 | h3 = safeAdd(h3, d); |
202 | h4 = safeAdd(h4, e); | 200 | h4 = safeAdd(h4, e); |
203 | h5 = safeAdd(h5, f); | 201 | h5 = safeAdd(h5, f); |
204 | h6 = safeAdd(h6, g); | 202 | h6 = safeAdd(h6, g); |
205 | h7 = safeAdd(h7, h); | 203 | h7 = safeAdd(h7, h); |
206 | 204 | ||
207 | currentMessageIndex += bytesPerBlock; | 205 | currentMessageIndex += bytesPerBlock; |
208 | } | 206 | } |
209 | 207 | ||
210 | result = new Array(256/8); | 208 | result = new Array(256/8); |
211 | result[0] = (h0 >> 24)& 0xff; | 209 | result[0] = (h0 >> 24)& 0xff; |
212 | result[1] = (h0 >> 16)& 0xff; | 210 | result[1] = (h0 >> 16)& 0xff; |
213 | result[2] = (h0 >> 8)& 0xff; | 211 | result[2] = (h0 >> 8)& 0xff; |
214 | result[3] = h0 & 0xff; | 212 | result[3] = h0 & 0xff; |
215 | 213 | ||
216 | result[4] = (h1 >> 24)& 0xff; | 214 | result[4] = (h1 >> 24)& 0xff; |
217 | result[5] = (h1 >> 16)& 0xff; | 215 | result[5] = (h1 >> 16)& 0xff; |
218 | result[6] = (h1 >> 8)& 0xff; | 216 | result[6] = (h1 >> 8)& 0xff; |
219 | result[7] = h1 & 0xff; | 217 | result[7] = h1 & 0xff; |
220 | 218 | ||
221 | result[8] = (h2 >> 24)& 0xff; | 219 | result[8] = (h2 >> 24)& 0xff; |
222 | result[9] = (h2 >> 16)& 0xff; | 220 | result[9] = (h2 >> 16)& 0xff; |
223 | result[10] = (h2 >> 8)& 0xff; | 221 | result[10] = (h2 >> 8)& 0xff; |
224 | result[11] = h2 & 0xff; | 222 | result[11] = h2 & 0xff; |
225 | 223 | ||
226 | result[12] = (h3 >> 24)& 0xff; | 224 | result[12] = (h3 >> 24)& 0xff; |
227 | result[13] = (h3 >> 16)& 0xff; | 225 | result[13] = (h3 >> 16)& 0xff; |
228 | result[14] = (h3 >> 8)& 0xff; | 226 | result[14] = (h3 >> 8)& 0xff; |
229 | result[15] = h3 & 0xff; | 227 | result[15] = h3 & 0xff; |
230 | 228 | ||
231 | result[16] = (h4 >> 24)& 0xff; | 229 | result[16] = (h4 >> 24)& 0xff; |
232 | result[17] = (h4 >> 16)& 0xff; | 230 | result[17] = (h4 >> 16)& 0xff; |
233 | result[18] = (h4 >> 8)& 0xff; | 231 | result[18] = (h4 >> 8)& 0xff; |
234 | result[19] = h4 & 0xff; | 232 | result[19] = h4 & 0xff; |
235 | 233 | ||
236 | result[20] = (h5 >> 24)& 0xff; | 234 | result[20] = (h5 >> 24)& 0xff; |
237 | result[21] = (h5 >> 16)& 0xff; | 235 | result[21] = (h5 >> 16)& 0xff; |
238 | result[22] = (h5 >> 8)& 0xff; | 236 | result[22] = (h5 >> 8)& 0xff; |
239 | result[23] = h5 & 0xff; | 237 | result[23] = h5 & 0xff; |
240 | 238 | ||
241 | result[24] = (h6 >> 24)& 0xff; | 239 | result[24] = (h6 >> 24)& 0xff; |
242 | result[25] = (h6 >> 16)& 0xff; | 240 | result[25] = (h6 >> 16)& 0xff; |
243 | result[26] = (h6 >> 8)& 0xff; | 241 | result[26] = (h6 >> 8)& 0xff; |
244 | result[27] = h6 & 0xff; | 242 | result[27] = h6 & 0xff; |
245 | 243 | ||
246 | result[28] = (h7 >> 24)& 0xff; | 244 | result[28] = (h7 >> 24)& 0xff; |
247 | result[29] = (h7 >> 16)& 0xff; | 245 | result[29] = (h7 >> 16)& 0xff; |
248 | result[30] = (h7 >> 8)& 0xff; | 246 | result[30] = (h7 >> 8)& 0xff; |
249 | result[31] = h7 & 0xff; | 247 | result[31] = h7 & 0xff; |
250 | 248 | ||
251 | //Clipperz.Profile.stop("Clipperz.Crypto.SHA.sha256_array"); | 249 | //Clipperz.Profile.stop("Clipperz.Crypto.SHA.sha256_array"); |
252 | return result; | 250 | return result; |
253 | }, | 251 | }, |
254 | 252 | ||
255 | //----------------------------------------------------------------------------- | 253 | //----------------------------------------------------------------------------- |
256 | 254 | ||
257 | 'sha256': function(aValue) { | 255 | 'sha256': function(aValue) { |
258 | //Clipperz.Profile.start("Clipperz.Crypto.SHA.sha256"); | 256 | //Clipperz.Profile.start("Clipperz.Crypto.SHA.sha256"); |
259 | var result; | 257 | var result; |
260 | var resultArray; | 258 | var resultArray; |
261 | varvalueArray; | 259 | varvalueArray; |
262 | 260 | ||
263 | valueArray = aValue.arrayValues(); | 261 | valueArray = aValue.arrayValues(); |
264 | resultArray = Clipperz.Crypto.SHA.sha256_array(valueArray); | 262 | resultArray = Clipperz.Crypto.SHA.sha256_array(valueArray); |
265 | 263 | ||
266 | result = new Clipperz.ByteArray(resultArray); | 264 | result = new Clipperz.ByteArray(resultArray); |
267 | 265 | ||
268 | //Clipperz.Profile.stop("Clipperz.Crypto.SHA.sha256"); | 266 | //Clipperz.Profile.stop("Clipperz.Crypto.SHA.sha256"); |
269 | return result; | 267 | return result; |
270 | }, | 268 | }, |
271 | 269 | ||
272 | //----------------------------------------------------------------------------- | 270 | //----------------------------------------------------------------------------- |
273 | 271 | ||
274 | 'sha_d256': function(aValue) { | 272 | 'sha_d256': function(aValue) { |
275 | //Clipperz.Profile.start("Clipperz.Crypto.SHA.sha_d256"); | 273 | //Clipperz.Profile.start("Clipperz.Crypto.SHA.sha_d256"); |
276 | var result; | 274 | var result; |
277 | var resultArray; | 275 | var resultArray; |
278 | varvalueArray; | 276 | varvalueArray; |
279 | 277 | ||
280 | valueArray = aValue.arrayValues(); | 278 | valueArray = aValue.arrayValues(); |
281 | resultArray = Clipperz.Crypto.SHA.sha256_array(valueArray); | 279 | resultArray = Clipperz.Crypto.SHA.sha256_array(valueArray); |
282 | resultArray = Clipperz.Crypto.SHA.sha256_array(resultArray); | 280 | resultArray = Clipperz.Crypto.SHA.sha256_array(resultArray); |
283 | 281 | ||
284 | result = new Clipperz.ByteArray(resultArray); | 282 | result = new Clipperz.ByteArray(resultArray); |
285 | 283 | ||
286 | //Clipperz.Profile.stop("Clipperz.Crypto.SHA.sha256"); | 284 | //Clipperz.Profile.stop("Clipperz.Crypto.SHA.sha256"); |
287 | return result; | 285 | return result; |
288 | }, | 286 | }, |
289 | 287 | ||
290 | //----------------------------------------------------------------------------- | 288 | //----------------------------------------------------------------------------- |
291 | __syntaxFix__: "syntax fix" | 289 | __syntaxFix__: "syntax fix" |
292 | 290 | ||
293 | }); | 291 | }); |
diff --git a/frontend/beta/js/Clipperz/Crypto/SRP.js b/frontend/beta/js/Clipperz/Crypto/SRP.js index 3b25275..8cc80ba 100644 --- a/frontend/beta/js/Clipperz/Crypto/SRP.js +++ b/frontend/beta/js/Clipperz/Crypto/SRP.js | |||
@@ -1,328 +1,326 @@ | |||
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.BigInt) == 'undefined') { throw ""; }} catch (e) { | 28 | try { if (typeof(Clipperz.Crypto.BigInt) == 'undefined') { throw ""; }} catch (e) { |
31 | throw "Clipperz.Crypto.SRP depends on Clipperz.Crypto.BigInt!"; | 29 | throw "Clipperz.Crypto.SRP depends on Clipperz.Crypto.BigInt!"; |
32 | } | 30 | } |
33 | 31 | ||
34 | try { if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { throw ""; }} catch (e) { | 32 | try { if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { throw ""; }} catch (e) { |
35 | throw "Clipperz.Crypto.SRP depends on Clipperz.Crypto.PRNG!"; | 33 | throw "Clipperz.Crypto.SRP depends on Clipperz.Crypto.PRNG!"; |
36 | } | 34 | } |
37 | 35 | ||
38 | if (typeof(Clipperz.Crypto.SRP) == 'undefined') { Clipperz.Crypto.SRP = {}; } | 36 | if (typeof(Clipperz.Crypto.SRP) == 'undefined') { Clipperz.Crypto.SRP = {}; } |
39 | 37 | ||
40 | Clipperz.Crypto.SRP.VERSION = "0.1"; | 38 | Clipperz.Crypto.SRP.VERSION = "0.1"; |
41 | Clipperz.Crypto.SRP.NAME = "Clipperz.Crypto.SRP"; | 39 | Clipperz.Crypto.SRP.NAME = "Clipperz.Crypto.SRP"; |
42 | 40 | ||
43 | //############################################################################# | 41 | //############################################################################# |
44 | 42 | ||
45 | MochiKit.Base.update(Clipperz.Crypto.SRP, { | 43 | MochiKit.Base.update(Clipperz.Crypto.SRP, { |
46 | 44 | ||
47 | '_n': null, | 45 | '_n': null, |
48 | '_g': null, | 46 | '_g': null, |
49 | //------------------------------------------------------------------------- | 47 | //------------------------------------------------------------------------- |
50 | 48 | ||
51 | 'n': function() { | 49 | 'n': function() { |
52 | if (Clipperz.Crypto.SRP._n == null) { | 50 | if (Clipperz.Crypto.SRP._n == null) { |
53 | Clipperz.Crypto.SRP._n = new Clipperz.Crypto.BigInt("115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3", 16); | 51 | Clipperz.Crypto.SRP._n = new Clipperz.Crypto.BigInt("115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3", 16); |
54 | } | 52 | } |
55 | 53 | ||
56 | return Clipperz.Crypto.SRP._n; | 54 | return Clipperz.Crypto.SRP._n; |
57 | }, | 55 | }, |
58 | 56 | ||
59 | //------------------------------------------------------------------------- | 57 | //------------------------------------------------------------------------- |
60 | 58 | ||
61 | 'g': function() { | 59 | 'g': function() { |
62 | if (Clipperz.Crypto.SRP._g == null) { | 60 | if (Clipperz.Crypto.SRP._g == null) { |
63 | Clipperz.Crypto.SRP._g = new Clipperz.Crypto.BigInt(2); //eventually 5 (as suggested on the Diffi-Helmann documentation) | 61 | Clipperz.Crypto.SRP._g = new Clipperz.Crypto.BigInt(2); //eventually 5 (as suggested on the Diffi-Helmann documentation) |
64 | } | 62 | } |
65 | 63 | ||
66 | return Clipperz.Crypto.SRP._g; | 64 | return Clipperz.Crypto.SRP._g; |
67 | }, | 65 | }, |
68 | 66 | ||
69 | //----------------------------------------------------------------------------- | 67 | //----------------------------------------------------------------------------- |
70 | 68 | ||
71 | 'exception': { | 69 | 'exception': { |
72 | 'InvalidValue': new MochiKit.Base.NamedError("Clipperz.Crypto.SRP.exception.InvalidValue") | 70 | 'InvalidValue': new MochiKit.Base.NamedError("Clipperz.Crypto.SRP.exception.InvalidValue") |
73 | }, | 71 | }, |
74 | 72 | ||
75 | //------------------------------------------------------------------------- | 73 | //------------------------------------------------------------------------- |
76 | __syntaxFix__: "syntax fix" | 74 | __syntaxFix__: "syntax fix" |
77 | 75 | ||
78 | }); | 76 | }); |
79 | 77 | ||
80 | //############################################################################# | 78 | //############################################################################# |
81 | // | 79 | // |
82 | // S R P C o n n e c t i o n version 1.0 | 80 | // S R P C o n n e c t i o n version 1.0 |
83 | // | 81 | // |
84 | //============================================================================= | 82 | //============================================================================= |
85 | Clipperz.Crypto.SRP.Connection = function (args) { | 83 | Clipperz.Crypto.SRP.Connection = function (args) { |
86 | args = args || {}; | 84 | args = args || {}; |
87 | 85 | ||
88 | this._C = args.C; | 86 | this._C = args.C; |
89 | this._P = args.P; | 87 | this._P = args.P; |
90 | this.hash = args.hash; | 88 | this.hash = args.hash; |
91 | 89 | ||
92 | this._a = null; | 90 | this._a = null; |
93 | this._A = null; | 91 | this._A = null; |
94 | 92 | ||
95 | this._s = null; | 93 | this._s = null; |
96 | this._B = null; | 94 | this._B = null; |
97 | 95 | ||
98 | this._x = null; | 96 | this._x = null; |
99 | 97 | ||
100 | this._u = null; | 98 | this._u = null; |
101 | this._K = null; | 99 | this._K = null; |
102 | this._M1 = null; | 100 | this._M1 = null; |
103 | this._M2 = null; | 101 | this._M2 = null; |
104 | 102 | ||
105 | this._sessionKey = null; | 103 | this._sessionKey = null; |
106 | 104 | ||
107 | return this; | 105 | return this; |
108 | } | 106 | } |
109 | 107 | ||
110 | Clipperz.Crypto.SRP.Connection.prototype = MochiKit.Base.update(null, { | 108 | Clipperz.Crypto.SRP.Connection.prototype = MochiKit.Base.update(null, { |
111 | 109 | ||
112 | 'toString': function () { | 110 | 'toString': function () { |
113 | return "Clipperz.Crypto.SRP.Connection (username: " + this.username() + "). Status: " + this.statusDescription(); | 111 | return "Clipperz.Crypto.SRP.Connection (username: " + this.username() + "). Status: " + this.statusDescription(); |
114 | }, | 112 | }, |
115 | 113 | ||
116 | //------------------------------------------------------------------------- | 114 | //------------------------------------------------------------------------- |
117 | 115 | ||
118 | 'C': function () { | 116 | 'C': function () { |
119 | return this._C; | 117 | return this._C; |
120 | }, | 118 | }, |
121 | 119 | ||
122 | //------------------------------------------------------------------------- | 120 | //------------------------------------------------------------------------- |
123 | 121 | ||
124 | 'P': function () { | 122 | 'P': function () { |
125 | return this._P; | 123 | return this._P; |
126 | }, | 124 | }, |
127 | 125 | ||
128 | //------------------------------------------------------------------------- | 126 | //------------------------------------------------------------------------- |
129 | 127 | ||
130 | 'a': function () { | 128 | 'a': function () { |
131 | if (this._a == null) { | 129 | if (this._a == null) { |
132 | this._a = new Clipperz.Crypto.BigInt(Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32).toHexString().substring(2), 16); | 130 | this._a = new Clipperz.Crypto.BigInt(Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32).toHexString().substring(2), 16); |
133 | // this._a = new Clipperz.Crypto.BigInt("37532428169486597638072888476611365392249575518156687476805936694442691012367", 10); | 131 | // this._a = new Clipperz.Crypto.BigInt("37532428169486597638072888476611365392249575518156687476805936694442691012367", 10); |
134 | //MochiKit.Logging.logDebug("SRP a: " + this._a); | 132 | //MochiKit.Logging.logDebug("SRP a: " + this._a); |
135 | } | 133 | } |
136 | 134 | ||
137 | return this._a; | 135 | return this._a; |
138 | }, | 136 | }, |
139 | 137 | ||
140 | //------------------------------------------------------------------------- | 138 | //------------------------------------------------------------------------- |
141 | 139 | ||
142 | 'A': function () { | 140 | 'A': function () { |
143 | if (this._A == null) { | 141 | if (this._A == null) { |
144 | //Warning: this value should be strictly greater than zero: how should we perform this check? | 142 | //Warning: this value should be strictly greater than zero: how should we perform this check? |
145 | this._A = Clipperz.Crypto.SRP.g().powerModule(this.a(), Clipperz.Crypto.SRP.n()); | 143 | this._A = Clipperz.Crypto.SRP.g().powerModule(this.a(), Clipperz.Crypto.SRP.n()); |
146 | 144 | ||
147 | if (this._A.equals(0)) { | 145 | if (this._A.equals(0)) { |
148 | MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'A' to 0."); | 146 | MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'A' to 0."); |
149 | throw Clipperz.Crypto.SRP.exception.InvalidValue; | 147 | throw Clipperz.Crypto.SRP.exception.InvalidValue; |
150 | } | 148 | } |
151 | //MochiKit.Logging.logDebug("SRP A: " + this._A); | 149 | //MochiKit.Logging.logDebug("SRP A: " + this._A); |
152 | } | 150 | } |
153 | 151 | ||
154 | return this._A; | 152 | return this._A; |
155 | }, | 153 | }, |
156 | 154 | ||
157 | //------------------------------------------------------------------------- | 155 | //------------------------------------------------------------------------- |
158 | 156 | ||
159 | 's': function () { | 157 | 's': function () { |
160 | return this._s; | 158 | return this._s; |
161 | //MochiKit.Logging.logDebug("SRP s: " + this._S); | 159 | //MochiKit.Logging.logDebug("SRP s: " + this._S); |
162 | }, | 160 | }, |
163 | 161 | ||
164 | 'set_s': function(aValue) { | 162 | 'set_s': function(aValue) { |
165 | this._s = aValue; | 163 | this._s = aValue; |
166 | }, | 164 | }, |
167 | 165 | ||
168 | //------------------------------------------------------------------------- | 166 | //------------------------------------------------------------------------- |
169 | 167 | ||
170 | 'B': function () { | 168 | 'B': function () { |
171 | return this._B; | 169 | return this._B; |
172 | }, | 170 | }, |
173 | 171 | ||
174 | 'set_B': function(aValue) { | 172 | 'set_B': function(aValue) { |
175 | //Warning: this value should be strictly greater than zero: how should we perform this check? | 173 | //Warning: this value should be strictly greater than zero: how should we perform this check? |
176 | if (! aValue.equals(0)) { | 174 | if (! aValue.equals(0)) { |
177 | this._B = aValue; | 175 | this._B = aValue; |
178 | //MochiKit.Logging.logDebug("SRP B: " + this._B); | 176 | //MochiKit.Logging.logDebug("SRP B: " + this._B); |
179 | } else { | 177 | } else { |
180 | MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'B' to 0."); | 178 | MochiKit.Logging.logError("Clipperz.Crypto.SRP.Connection: trying to set 'B' to 0."); |
181 | throw Clipperz.Crypto.SRP.exception.InvalidValue; | 179 | throw Clipperz.Crypto.SRP.exception.InvalidValue; |
182 | } | 180 | } |
183 | }, | 181 | }, |
184 | 182 | ||
185 | //------------------------------------------------------------------------- | 183 | //------------------------------------------------------------------------- |
186 | 184 | ||
187 | 'x': function () { | 185 | 'x': function () { |
188 | if (this._x == null) { | 186 | if (this._x == null) { |
189 | this._x = new Clipperz.Crypto.BigInt(this.stringHash(this.s().asString(16, 64) + this.P()), 16); | 187 | this._x = new Clipperz.Crypto.BigInt(this.stringHash(this.s().asString(16, 64) + this.P()), 16); |
190 | //MochiKit.Logging.logDebug("SRP x: " + this._x); | 188 | //MochiKit.Logging.logDebug("SRP x: " + this._x); |
191 | } | 189 | } |
192 | 190 | ||
193 | return this._x; | 191 | return this._x; |
194 | }, | 192 | }, |
195 | 193 | ||
196 | //------------------------------------------------------------------------- | 194 | //------------------------------------------------------------------------- |
197 | 195 | ||
198 | 'u': function () { | 196 | 'u': function () { |
199 | if (this._u == null) { | 197 | if (this._u == null) { |
200 | this._u = new Clipperz.Crypto.BigInt(this.stringHash(this.B().asString()), 16); | 198 | this._u = new Clipperz.Crypto.BigInt(this.stringHash(this.B().asString()), 16); |
201 | //MochiKit.Logging.logDebug("SRP u: " + this._u); | 199 | //MochiKit.Logging.logDebug("SRP u: " + this._u); |
202 | } | 200 | } |
203 | 201 | ||
204 | return this._u; | 202 | return this._u; |
205 | }, | 203 | }, |
206 | 204 | ||
207 | //------------------------------------------------------------------------- | 205 | //------------------------------------------------------------------------- |
208 | 206 | ||
209 | 'S': function () { | 207 | 'S': function () { |
210 | if (this._S == null) { | 208 | if (this._S == null) { |
211 | var bigint; | 209 | var bigint; |
212 | varsrp; | 210 | varsrp; |
213 | 211 | ||
214 | bigint = Clipperz.Crypto.BigInt; | 212 | bigint = Clipperz.Crypto.BigInt; |
215 | srp = Clipperz.Crypto.SRP; | 213 | srp = Clipperz.Crypto.SRP; |
216 | 214 | ||
217 | this._S =bigint.powerModule( | 215 | this._S =bigint.powerModule( |
218 | bigint.subtract(this.B(), bigint.powerModule(srp.g(), this.x(), srp.n())), | 216 | bigint.subtract(this.B(), bigint.powerModule(srp.g(), this.x(), srp.n())), |
219 | bigint.add(this.a(), bigint.multiply(this.u(), this.x())), | 217 | bigint.add(this.a(), bigint.multiply(this.u(), this.x())), |
220 | srp.n() | 218 | srp.n() |
221 | ) | 219 | ) |
222 | //MochiKit.Logging.logDebug("SRP S: " + this._S); | 220 | //MochiKit.Logging.logDebug("SRP S: " + this._S); |
223 | } | 221 | } |
224 | 222 | ||
225 | return this._S; | 223 | return this._S; |
226 | }, | 224 | }, |
227 | 225 | ||
228 | //------------------------------------------------------------------------- | 226 | //------------------------------------------------------------------------- |
229 | 227 | ||
230 | 'K': function () { | 228 | 'K': function () { |
231 | if (this._K == null) { | 229 | if (this._K == null) { |
232 | this._K = this.stringHash(this.S().asString()); | 230 | this._K = this.stringHash(this.S().asString()); |
233 | //MochiKit.Logging.logDebug("SRP K: " + this._K); | 231 | //MochiKit.Logging.logDebug("SRP K: " + this._K); |
234 | } | 232 | } |
235 | 233 | ||
236 | return this._K; | 234 | return this._K; |
237 | }, | 235 | }, |
238 | 236 | ||
239 | //------------------------------------------------------------------------- | 237 | //------------------------------------------------------------------------- |
240 | 238 | ||
241 | 'M1': function () { | 239 | 'M1': function () { |
242 | if (this._M1 == null) { | 240 | if (this._M1 == null) { |
243 | this._M1 = this.stringHash(this.A().asString(10) + this.B().asString(10) + this.K()); | 241 | this._M1 = this.stringHash(this.A().asString(10) + this.B().asString(10) + this.K()); |
244 | //MochiKit.Logging.logDebug("SRP M1: " + this._M1); | 242 | //MochiKit.Logging.logDebug("SRP M1: " + this._M1); |
245 | } | 243 | } |
246 | 244 | ||
247 | return this._M1; | 245 | return this._M1; |
248 | }, | 246 | }, |
249 | 247 | ||
250 | //------------------------------------------------------------------------- | 248 | //------------------------------------------------------------------------- |
251 | 249 | ||
252 | 'M2': function () { | 250 | 'M2': function () { |
253 | if (this._M2 == null) { | 251 | if (this._M2 == null) { |
254 | this._M2 = this.stringHash(this.A().asString(10) + this.M1() + this.K()); | 252 | this._M2 = this.stringHash(this.A().asString(10) + this.M1() + this.K()); |
255 | //MochiKit.Logging.logDebug("SRP M2: " + this._M2); | 253 | //MochiKit.Logging.logDebug("SRP M2: " + this._M2); |
256 | } | 254 | } |
257 | 255 | ||
258 | return this._M2; | 256 | return this._M2; |
259 | }, | 257 | }, |
260 | 258 | ||
261 | //========================================================================= | 259 | //========================================================================= |
262 | 260 | ||
263 | 'serverSideCredentialsWithSalt': function(aSalt) { | 261 | 'serverSideCredentialsWithSalt': function(aSalt) { |
264 | var result; | 262 | var result; |
265 | var s, x, v; | 263 | var s, x, v; |
266 | 264 | ||
267 | s = aSalt; | 265 | s = aSalt; |
268 | x = this.stringHash(s + this.P()); | 266 | x = this.stringHash(s + this.P()); |
269 | v = Clipperz.Crypto.SRP.g().powerModule(new Clipperz.Crypto.BigInt(x, 16), Clipperz.Crypto.SRP.n()); | 267 | v = Clipperz.Crypto.SRP.g().powerModule(new Clipperz.Crypto.BigInt(x, 16), Clipperz.Crypto.SRP.n()); |
270 | 268 | ||
271 | result = {}; | 269 | result = {}; |
272 | result['C'] = this.C(); | 270 | result['C'] = this.C(); |
273 | result['s'] = s; | 271 | result['s'] = s; |
274 | result['v'] = v.asString(16); | 272 | result['v'] = v.asString(16); |
275 | 273 | ||
276 | return result; | 274 | return result; |
277 | }, | 275 | }, |
278 | 276 | ||
279 | 'serverSideCredentials': function() { | 277 | 'serverSideCredentials': function() { |
280 | var result; | 278 | var result; |
281 | var s; | 279 | var s; |
282 | 280 | ||
283 | s = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32).toHexString().substring(2); | 281 | s = Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32).toHexString().substring(2); |
284 | 282 | ||
285 | result = this.serverSideCredentialsWithSalt(s); | 283 | result = this.serverSideCredentialsWithSalt(s); |
286 | 284 | ||
287 | return result; | 285 | return result; |
288 | }, | 286 | }, |
289 | 287 | ||
290 | //========================================================================= | 288 | //========================================================================= |
291 | /* | 289 | /* |
292 | 'computeServerSide_S': function(b) { | 290 | 'computeServerSide_S': function(b) { |
293 | var result; | 291 | var result; |
294 | var v; | 292 | var v; |
295 | var bigint; | 293 | var bigint; |
296 | varsrp; | 294 | varsrp; |
297 | 295 | ||
298 | bigint = Clipperz.Crypto.BigInt; | 296 | bigint = Clipperz.Crypto.BigInt; |
299 | srp = Clipperz.Crypto.SRP; | 297 | srp = Clipperz.Crypto.SRP; |
300 | 298 | ||
301 | v = new Clipperz.Crypto.BigInt(srpConnection.serverSideCredentialsWithSalt(this.s().asString(16, 64)).v, 16); | 299 | v = new Clipperz.Crypto.BigInt(srpConnection.serverSideCredentialsWithSalt(this.s().asString(16, 64)).v, 16); |
302 | // _S = (this.A().multiply(this.v().modPow(this.u(), this.n()))).modPow(this.b(), this.n()); | 300 | // _S = (this.A().multiply(this.v().modPow(this.u(), this.n()))).modPow(this.b(), this.n()); |
303 | result = bigint.powerModule( | 301 | result = bigint.powerModule( |
304 | bigint.multiply( | 302 | bigint.multiply( |
305 | this.A(), | 303 | this.A(), |
306 | bigint.powerModule(v, this.u(), srp.n()) | 304 | bigint.powerModule(v, this.u(), srp.n()) |
307 | ), new Clipperz.Crypto.BigInt(b, 10), srp.n() | 305 | ), new Clipperz.Crypto.BigInt(b, 10), srp.n() |
308 | ); | 306 | ); |
309 | 307 | ||
310 | return result; | 308 | return result; |
311 | }, | 309 | }, |
312 | */ | 310 | */ |
313 | //========================================================================= | 311 | //========================================================================= |
314 | 312 | ||
315 | 'stringHash': function(aValue) { | 313 | 'stringHash': function(aValue) { |
316 | varresult; | 314 | varresult; |
317 | 315 | ||
318 | result = this.hash(new Clipperz.ByteArray(aValue)).toHexString().substring(2); | 316 | result = this.hash(new Clipperz.ByteArray(aValue)).toHexString().substring(2); |
319 | 317 | ||
320 | return result; | 318 | return result; |
321 | }, | 319 | }, |
322 | 320 | ||
323 | //========================================================================= | 321 | //========================================================================= |
324 | __syntaxFix__: "syntax fix" | 322 | __syntaxFix__: "syntax fix" |
325 | 323 | ||
326 | }); | 324 | }); |
327 | 325 | ||
328 | //############################################################################# | 326 | //############################################################################# |