summaryrefslogtreecommitdiff
path: root/frontend/beta/js/Clipperz/PM/Crypto.js
Unidiff
Diffstat (limited to 'frontend/beta/js/Clipperz/PM/Crypto.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/beta/js/Clipperz/PM/Crypto.js503
1 files changed, 503 insertions, 0 deletions
diff --git a/frontend/beta/js/Clipperz/PM/Crypto.js b/frontend/beta/js/Clipperz/PM/Crypto.js
new file mode 100644
index 0000000..7636822
--- a/dev/null
+++ b/frontend/beta/js/Clipperz/PM/Crypto.js
@@ -0,0 +1,503 @@
1/*
2
3Copyright 2008-2011 Clipperz Srl
4
5This file is part of Clipperz's Javascript Crypto Library.
6Javascript Crypto Library provides web developers with an extensive
7and efficient set of cryptographic functions. The library aims to
8obtain maximum execution speed while preserving modularity and
9reusability.
10For further information about its features and functionalities please
11refer to http://www.clipperz.com
12
13* Javascript Crypto Library is free software: you can redistribute
14 it and/or modify it under the terms of the GNU Affero General Public
15 License as published by the Free Software Foundation, either version
16 3 of the License, or (at your option) any later version.
17
18* Javascript Crypto Library is distributed in the hope that it will
19 be useful, but WITHOUT ANY WARRANTY; without even the implied
20 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU Affero General Public License for more details.
22
23* You should have received a copy of the GNU Affero General Public
24 License along with Javascript Crypto Library. If not, see
25 <http://www.gnu.org/licenses/>.
26
27*/
28
29if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
30if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; }
31if (typeof(Clipperz.PM.Crypto) == 'undefined') { Clipperz.PM.Crypto = {}; }
32
33Clipperz.PM.Crypto.VERSION = "0.2";
34Clipperz.PM.Crypto.NAME = "Clipperz.PM.Crypto";
35
36MochiKit.Base.update(Clipperz.PM.Crypto, {
37
38 '__repr__': function () {
39 return "[" + this.NAME + " " + this.VERSION + "]";
40 },
41
42 //-------------------------------------------------------------------------
43
44 'toString': function () {
45 return this.__repr__();
46 },
47
48 //-------------------------------------------------------------------------
49
50 'communicationProtocol': {
51 'currentVersion': '0.2',
52 'versions': {
53 '0.1': Clipperz.PM.Connection.SRP['1.0'],//Clipperz.Crypto.SRP.versions['1.0'].Connection,
54 '0.2': Clipperz.PM.Connection.SRP['1.1']//Clipperz.Crypto.SRP.versions['1.1'].Connection,
55 },
56 'fallbackVersions': {
57 'current':'0.1',
58 '0.2': '0.1',
59 '0.1': null
60 }
61 },
62
63 //-------------------------------------------------------------------------
64
65 'encryptingFunctions': {
66 'currentVersion': '0.3',
67 'versions': {
68
69 //#####################################################################
70
71 '0.1': {
72 'encrypt': function(aKey, aValue) {
73 return Clipperz.Crypto.Base.encryptUsingSecretKey(aKey, Clipperz.Base.serializeJSON(aValue));
74 },
75
76 'deferredEncrypt': function(aKey, aValue) {
77 var deferredResult;
78
79 deferredResult = new MochiKit.Async.Deferred();
80 deferredResult.addCallback(Clipperz.PM.Crypto.encryptingFunctions.versions['0.1'].encrypt, aKey, aValue);
81 deferredResult.callback();
82
83 return deferredResult;
84 },
85
86 'decrypt': function(aKey, aValue) {
87 var result;
88
89 if (aValue != null) {
90 result = Clipperz.Base.evalJSON(Clipperz.Crypto.Base.decryptUsingSecretKey(aKey, aValue));
91 } else {
92 result = null;
93 }
94
95 return result;
96 },
97
98 'deferredDecrypt': function(aKey, aValue) {
99 var deferredResult;
100
101 deferredResult = new MochiKit.Async.Deferred();
102 deferredResult.addCallback(Clipperz.PM.Crypto.encryptingFunctions.versions['0.1'].decrypt, aKey, aValue);
103 deferredResult.callback();
104
105 return deferredResult;
106 },
107
108 'hash': function(aValue) {
109 var result;
110 var strngResult;
111
112 stringResult = Clipperz.Crypto.Base.computeHashValue(aValue.asString()); //!!!!!!!
113 result = new Clipperz.ByteArray("0x" + stringResult);
114
115 return result;
116 }
117 },
118
119 //#####################################################################
120
121 '0.2': {
122 'encrypt': function(aKey, aValue, aNonce) {
123 var result;
124 varkey, value;
125 var dataToEncrypt;
126 var encryptedData;
127
128 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
129 value = new Clipperz.ByteArray(Clipperz.Base.serializeJSON(aValue));
130 dataToEncrypt = Clipperz.Crypto.SHA.sha_d256(value).appendBlock(value);
131 encryptedData = Clipperz.Crypto.AES.encrypt(key, dataToEncrypt, aNonce);
132 result = encryptedData.toBase64String();
133
134 return result;
135 },
136
137 'deferredEncrypt': function(aKey, aValue, aNonce) {
138 var deferredResult;
139 varkey, value;
140 var dataToEncrypt;
141 var encryptedData;
142
143 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
144 value = new Clipperz.ByteArray(Clipperz.Base.serializeJSON(aValue));
145 dataToEncrypt = Clipperz.Crypto.SHA.sha_d256(value).appendBlock(value);
146
147 deferredResult = new MochiKit.Async.Deferred()
148 deferredResult.addCallback(Clipperz.Crypto.AES.deferredEncrypt, key, dataToEncrypt, aNonce);
149 deferredResult.addCallback(function(aResult) {
150 return aResult.toBase64String();
151 })
152 deferredResult.callback();
153
154 return deferredResult;
155 },
156
157 'decrypt': function(aKey, aValue) {
158 var result;
159
160 if (aValue != null) {
161 var key, value;
162 var decryptedData;
163 var decryptedData;
164
165 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
166 value = new Clipperz.ByteArray().appendBase64String(aValue);
167
168 decryptedData = Clipperz.Crypto.AES.decrypt(key, value);
169 decryptedData = decryptedData.split((256/8));
170
171 try {
172 result = Clipperz.Base.evalJSON(decryptedData.asString());
173 } catch (exception) {
174 MochiKit.Logging.logError("Error while decrypting data");
175 throw Clipperz.Crypto.Base.exception.CorruptedMessage;
176 }
177 } else {
178 result = null;
179 }
180
181 return result;
182 },
183
184 'deferredDecrypt': function(aKey, aValue) {
185 var result;
186
187 if (aValue != null) {
188 var deferredResult;
189 var key, value;
190 var decryptedData;
191
192 result = new MochiKit.Async.Deferred();
193
194 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
195 value = new Clipperz.ByteArray().appendBase64String(aValue);
196
197
198 deferredResult = new MochiKit.Async.Deferred()
199 deferredResult.addCallback(Clipperz.Crypto.AES.deferredDecrypt, key, value);
200 deferredResult.addCallback(function(aResult) {
201 var result;
202 var decryptedData;
203
204 decryptedData = aResult.split((256/8));
205
206 try {
207 result = Clipperz.Base.evalJSON(decryptedData.asString());
208 } catch (exception) {
209 MochiKit.Logging.logError("Error while decrypting data");
210 throw Clipperz.Crypto.Base.exception.CorruptedMessage;
211 }
212
213 return result;
214 })
215 deferredResult.callback();
216
217 result = deferredResult;
218 } else {
219 result = MochiKit.Async.succeed(null);
220 }
221
222 return result;
223 },
224
225 'hash': Clipperz.Crypto.SHA.sha_d256
226 },
227
228 //#####################################################################
229
230 '0.3': {
231 'encrypt': function(aKey, aValue, aNonce) {
232 var result;
233 varkey, value;
234 var data;
235 var dataToEncrypt;
236 var encryptedData;
237
238 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
239 value = Clipperz.Base.serializeJSON(aValue);
240 data = new Clipperz.ByteArray(value);
241 encryptedData = Clipperz.Crypto.AES.encrypt(key, data, aNonce);
242 result = encryptedData.toBase64String();
243
244 return result;
245 },
246
247 'deferredEncrypt': function(aKey, aValue, aNonce) {
248 var deferredResult;
249 varkey, value;
250 var data;
251 var dataToEncrypt;
252 var encryptedData;
253
254 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
255 value = Clipperz.Base.serializeJSON(aValue);
256 data = new Clipperz.ByteArray(value);
257
258 deferredResult = new MochiKit.Async.Deferred()
259//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Clipperz.PM.Crypto.deferredEncrypt - 1: " + res); return res;});
260 deferredResult.addCallback(Clipperz.Crypto.AES.deferredEncrypt, key, data, aNonce);
261//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Clipperz.PM.Crypto.deferredEncrypt - 2: " + res); return res;});
262 deferredResult.addCallback(function(aResult) {
263 return aResult.toBase64String();
264 })
265//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Clipperz.PM.Crypto.deferredEncrypt - 3: " + res); return res;});
266 deferredResult.callback();
267
268 return deferredResult;
269 },
270
271 'decrypt': function(aKey, aValue) {
272 var result;
273
274 if (aValue != null) {
275 var key, value;
276 var decryptedData;
277 var decryptedValue;
278
279 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
280 value = new Clipperz.ByteArray().appendBase64String(aValue);
281
282 decryptedData = Clipperz.Crypto.AES.decrypt(key, value);
283
284 value = decryptedData.asString();
285 try {
286 result = Clipperz.Base.evalJSON(value);
287 } catch (exception) {
288 MochiKit.Logging.logError("Error while decrypting data");
289 throw Clipperz.Crypto.Base.exception.CorruptedMessage;
290 }
291 } else {
292 result = null;
293 }
294
295 return result;
296 },
297
298 'deferredDecrypt': function(aKey, aValue) {
299 var deferredResult;
300 // var now;
301
302 deferredResult = new MochiKit.Async.Deferred();
303 now = new Date;
304
305//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 1: " + res); return res;});
306 if (aValue != null) {
307 var key, value;
308 var decryptedData;
309 var decryptedValue;
310
311 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
312//MochiKit.Logging.logDebug("[" + (new Date() - now) + "] computed key");
313 value = new Clipperz.ByteArray().appendBase64String(aValue);
314//MochiKit.Logging.logDebug("[" + (new Date() - now) + "] appendedBase64String");
315
316//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 1.1: " /* + res*/); return res;});
317 deferredResult.addCallback(Clipperz.Crypto.AES.deferredDecrypt, key, value);
318//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 2: " /* + res*/); return res;});
319 deferredResult.addCallback(MochiKit.Async.wait, 0.1);
320//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 3: " /* + res*/); return res;});
321 deferredResult.addCallback(function(aResult) {
322 return aResult.asString();
323 });
324//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 4: " /* + res*/); return res;});
325 deferredResult.addCallback(MochiKit.Async.wait, 0.1);
326//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 5: " /* + res*/); return res;});
327 deferredResult.addCallback(Clipperz.Base.evalJSON);
328//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 6: " /* + res*/); return res;});
329 deferredResult.addErrback(function(anError) {
330 MochiKit.Logging.logError("Error while decrypting data");
331 throw Clipperz.Crypto.Base.exception.CorruptedMessage;
332 })
333//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 7: " /* + res*/); return res;});
334 } else {
335 deferredResult.addCallback(function() {
336 return null;
337 });
338 }
339 deferredResult.callback();
340
341 return deferredResult;
342 },
343
344 'hash': Clipperz.Crypto.SHA.sha_d256
345 },
346
347 //#####################################################################
348/*
349 '0.4': {
350 'encrypt': function(aKey, aValue, aNonce) {
351 var result;
352 varkey, value;
353 var data;
354 var dataToEncrypt;
355 var encryptedData;
356
357//MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Clipperz.PM.Crypto.versions[0.3].encrypt");
358 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
359//MochiKit.Logging.logDebug("--- [" + (new Date()).valueOf() + "] Clipperz.PM.Crypto.versions[0.3].encrypt - 1");
360 value = Clipperz.Base.serializeJSON(aValue);
361//MochiKit.Logging.logDebug("--- [" + (new Date()).valueOf() + "] Clipperz.PM.Crypto.versions[0.3].encrypt - 2");
362/ *
363//MochiKit.Logging.logDebug("--> encrypt.fullSize: " + value.length);
364 value = value.replace(/":{"label":"/g, '":{l:"');
365 value = value.replace(/":{"key":"/g, '":{k:"');
366 value = value.replace(/":{"notes":"/g, '":{n:"');
367 value = value.replace(/":{"record":"/g, '":{r:"');
368 value = value.replace(/", "label":"/g, '",l:"');
369 value = value.replace(/", "favicon":"/g,'",f:"');
370//MochiKit.Logging.logDebug("<-- encrypt.compressed: " + value.length);
371* /
372 data = new Clipperz.ByteArray(value);
373//MochiKit.Logging.logDebug("--- [" + (new Date()).valueOf() + "] Clipperz.PM.Crypto.versions[0.3].encrypt - 3");
374 encryptedData = Clipperz.Crypto.AES.encrypt(key, data, aNonce);
375//MochiKit.Logging.logDebug("--- [" + (new Date()).valueOf() + "] Clipperz.PM.Crypto.versions[0.3].encrypt - 4");
376 result = encryptedData.toBase64String();
377//MochiKit.Logging.logDebug("<<< [" + (new Date()).valueOf() + "] Clipperz.PM.Crypto.versions[0.3].encrypt");
378
379 return result;
380 },
381
382 'decrypt': function(aKey, aValue) {
383 var result;
384
385 if (aValue != null) {
386 var key, value;
387 var decryptedData;
388 var decryptedValue;
389
390 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
391 value = new Clipperz.ByteArray().appendBase64String(aValue);
392
393 decryptedData = Clipperz.Crypto.AES.decrypt(key, value);
394
395 value = decryptedData.asString();
396/ *
397 value = value.replace(/":{l:"/g,'":{"label":"');
398 value = value.replace(/":{k:"/g,'":{"key":"');
399 value = value.replace(/":{n:"/g,'":{"notes":"');
400 value = value.replace(/":{r:"/g,'":{"record":"');
401 value = value.replace(/",l:"/g, '", "label":"');
402 value = value.replace(/",f:"/g, '", "favicon":"');
403* /
404 try {
405 result = Clipperz.Base.evalJSON(value);
406 } catch (exception) {
407 MochiKit.Logging.logError("Error while decrypting data");
408 throw Clipperz.Crypto.Base.exception.CorruptedMessage;
409 }
410
411
412 } else {
413 result = null;
414 }
415
416 return result;
417 },
418
419 'hash': Clipperz.Crypto.SHA.sha_d256
420 },
421*/
422 //#####################################################################
423 __syntaxFix__: "syntax fix"
424 }
425 },
426
427 //-------------------------------------------------------------------------
428
429 'encrypt': function(aKey, aValue, aVersion) {
430 return Clipperz.PM.Crypto.encryptingFunctions.versions[aVersion].encrypt(aKey, aValue);
431 },
432
433 'deferredEncrypt': function(aKey, aValue, aVersion) {
434 return Clipperz.PM.Crypto.encryptingFunctions.versions[aVersion].deferredEncrypt(aKey, aValue);
435 },
436
437 'encryptWithCurrentVersion': function(aKey, aValue) {
438 return Clipperz.PM.Crypto.encrypt(aKey, aValue, Clipperz.PM.Crypto.encryptingFunctions.currentVersion);
439 },
440
441 'deferredEncryptWithCurrentVersion': function(aKey, aValue) {
442 return Clipperz.PM.Crypto.deferredEncrypt(aKey, aValue, Clipperz.PM.Crypto.encryptingFunctions.currentVersion);
443 },
444
445 //.........................................................................
446
447 'decrypt': function(aKey, aValue, aVersion) {
448 return Clipperz.PM.Crypto.encryptingFunctions.versions[aVersion].decrypt(aKey, aValue);
449 },
450
451 'deferredDecrypt': function(aKey, aValue, aVersion) {
452 return Clipperz.PM.Crypto.encryptingFunctions.versions[aVersion].deferredDecrypt(aKey, aValue);
453 },
454
455 //-------------------------------------------------------------------------
456
457 'randomKey': function() {
458 return Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(32).toHexString().substring(2);
459 },
460
461 //-------------------------------------------------------------------------
462
463 'passwordEntropy': function(aValue) {
464 var result;
465 varbitPerChar;
466
467 bitPerChar = 4;
468 if (/[a-z]/.test(aValue)) {
469 bitPerChar ++;
470 }
471 if (/[A-Z]/.test(aValue)) {
472 bitPerChar ++;
473 }
474 if (/[^a-zA-Z0-9]/.test(aValue)) {
475 bitPerChar ++;
476 }
477//MochiKit.Logging.logDebug("--- bitPerChar: " + bitPerChar);
478
479 result = aValue.length * bitPerChar;
480
481 return result;
482 },
483
484 //-------------------------------------------------------------------------
485
486 'nullValue': "####",
487
488 //-------------------------------------------------------------------------
489 __syntaxFix__: "syntax fix"
490
491});
492
493//*****************************************************************************
494
495MochiKit.Base.update(Clipperz.PM.Crypto.communicationProtocol.versions, {
496 'current': Clipperz.PM.Crypto.communicationProtocol.versions[Clipperz.PM.Crypto.communicationProtocol.currentVersion]
497});
498
499MochiKit.Base.update(Clipperz.PM.Crypto.encryptingFunctions.versions, {
500 'current': Clipperz.PM.Crypto.encryptingFunctions.versions[Clipperz.PM.Crypto.encryptingFunctions.currentVersion]
501});
502
503//*****************************************************************************