summaryrefslogtreecommitdiff
path: root/frontend/gamma/tests/tests/Clipperz/Crypto/JSCrypto_vs_Clipperz.test.js
Unidiff
Diffstat (limited to 'frontend/gamma/tests/tests/Clipperz/Crypto/JSCrypto_vs_Clipperz.test.js') (more/less context) (show whitespace changes)
-rw-r--r--frontend/gamma/tests/tests/Clipperz/Crypto/JSCrypto_vs_Clipperz.test.js418
1 files changed, 418 insertions, 0 deletions
diff --git a/frontend/gamma/tests/tests/Clipperz/Crypto/JSCrypto_vs_Clipperz.test.js b/frontend/gamma/tests/tests/Clipperz/Crypto/JSCrypto_vs_Clipperz.test.js
new file mode 100644
index 0000000..7dc688c
--- a/dev/null
+++ b/frontend/gamma/tests/tests/Clipperz/Crypto/JSCrypto_vs_Clipperz.test.js
@@ -0,0 +1,418 @@
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
29//function logElapsedTime(aDescription, aStartTime, anEndTime) {
30 //MochiKit.Logging.logDebug(aDescription + " - took " + (anEndTime - aStartTime) + "ms");
31 //SimpleTest.ok(true, aDescription + " - took " + (anEndTime - aStartTime) + "ms");
32//}
33
34 var asciiTestString = longAsciiText;
35//asciiTestString = asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString;
36//asciiTestString = asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString;
37//asciiTestString = asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString;
38//asciiTestString = asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString + asciiTestString;
39//asciiTestString = asciiTestString + asciiTestString;
40//asciiTestString = asciiTestString + asciiTestString;
41
42 var isoLatin1TestString= longIsoLatin1Text;
43//isoLatin1TestString = isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString;
44//isoLatin1TestString = isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString;
45//isoLatin1TestString = isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString + isoLatin1TestString;
46//isoLatin1TestString = isoLatin1TestString + isoLatin1TestString;
47//isoLatin1TestString = isoLatin1TestString + isoLatin1TestString;
48
49 var utf8TestString = longUtf8Text;
50//utf8TestString = utf8TestString + utf8TestString + utf8TestString + utf8TestString + utf8TestString + utf8TestString + utf8TestString + utf8TestString + utf8TestString + utf8TestString;
51//utf8TestString = utf8TestString + utf8TestString + utf8TestString + utf8TestString + utf8TestString + utf8TestString + utf8TestString + utf8TestString + utf8TestString + utf8TestString;
52//utf8TestString = utf8TestString + utf8TestString;
53//utf8TestString = utf8TestString + utf8TestString;
54
55var times = {
56 'Clipperz': {},
57 'JSCrypto': {}
58};
59
60function appendResults (aDescription, aTimer) {
61 MochiKit.DOM.appendChildNodes(MochiKit.DOM.getElement('timerTBODY'),
62 MochiKit.DOM.TR(null,
63 MochiKit.DOM.TH({align:'left'}, aDescription),
64 MochiKit.DOM.TH(null, aTimer['ascii']['encrypt'] + ' - ' + aTimer['ascii']['decrypt'])//,
65 // MochiKit.DOM.TH(null, aTimer['isoLatin1']['encrypt']+ ' - ' + aTimer['isoLatin1']['decrypt']),
66 // MochiKit.DOM.TH(null, aTimer['utf8']['encrypt'] + ' - ' + aTimer['utf8']['decrypt'])
67 )
68 );
69
70}
71
72//=============================================================================
73
74function timeRegularFunction (aDescription, aString, anEncryptFunction, aDecryptFunction, aTimer, aKey) {
75 var start;
76 var end;
77 var encryptTime;
78 var decryptTime;
79 var ciphertext;
80 var plaintext;
81
82
83 start = new Date();
84 ciphertext = anEncryptFunction('trustno1', aString);
85 end = new Date();
86 encryptTime = end - start;
87
88 start = new Date();
89 plaintext = aDecryptFunction('trustno1', ciphertext);
90 end = new Date();
91 decryptTime = end - start;
92 aTimer[aKey] = { 'encrypt': encryptTime, 'decrypt': decryptTime };
93 SimpleTest.is(aString, plaintext, aDescription);
94}
95/*
96function timeRegularFunction (anEncryptFunction, aDecryptFunction, aTimer) {
97 var start;
98 var end;
99 var encryptTime;
100 var decryptTime;
101 var ciphertext;
102 var plaintext;
103
104
105 start = new Date();
106 ciphertext = anEncryptFunction('trustno1', asciiTestString);
107 end = new Date();
108 encryptTime = end - start;
109
110 start = new Date();
111 plaintext = aDecryptFunction('trustno1', ciphertext);
112 end = new Date();
113 decryptTime = end - start;
114 aTimer['ascii'] = { 'encrypt': encryptTime, 'decrypt': decryptTime };
115 SimpleTest.is(asciiTestString, plaintext, "Encrypt/decrypt the ASCII text");
116
117
118 start = new Date();
119 ciphertext = anEncryptFunction('trustno1', isoLatin1TestString);
120 end = new Date();
121 encryptTime = end - start;
122
123 start = new Date();
124 plaintext = aDecryptFunction('trustno1', ciphertext);
125 end = new Date();
126 decryptTime = end - start;
127 aTimer['isoLatin1'] = { 'encrypt': encryptTime, 'decrypt': decryptTime };
128 SimpleTest.is(isoLatin1TestString, plaintext, "Encrypt/decrypt the ISO-Latin 1 text");
129
130
131 start = new Date();
132 ciphertext = anEncryptFunction('trustno1', utf8TestString);
133 end = new Date();
134 encryptTime = end - start;
135
136 start = new Date();
137 plaintext = aDecryptFunction('trustno1', ciphertext);
138 end = new Date();
139 decryptTime = end - start;
140 aTimer['utf8'] = { 'encrypt': encryptTime, 'decrypt': decryptTime };
141 SimpleTest.is(utf8TestString, plaintext, "Encrypt/decrypt the UTF-8 text");
142}
143*/
144function timeDeferredFunction (aDescription, aString, anEncryptFunction, aDecryptFunction, aTimer, aKey, someTestArgs) {
145 var start;
146 var end;
147
148 var deferredResult;
149
150 aTimer[aKey] = {};
151
152 deferredResult = new Clipperz.Async.Deferred("timeDeferredFunction", someTestArgs);
153 deferredResult.addCallback(function (aValue) { start = new Date(); return aValue});
154 deferredResult.addCallback(anEncryptFunction, 'trustno1', aString);
155 deferredResult.addCallback(function (aValue) {
156 end = new Date();
157 aTimer[aKey]['encrypt'] = end-start;
158 return aValue;
159 });
160 deferredResult.addCallback(function (aValue) { start = new Date(); return aValue});
161 deferredResult.addCallback(aDecryptFunction, 'trustno1');
162 deferredResult.addCallback(function (aValue) {
163 end = new Date();
164 aTimer[aKey]['decrypt'] = end-start;
165 return aValue;
166 });
167 deferredResult.addCallback(function (aValue) {
168 SimpleTest.is(aString, aValue, aDescription);
169 });
170
171 deferredResult.callback();
172
173 return deferredResult;
174}
175
176//=============================================================================
177
178function encryptUsingJSCrypto (aKey, aValue) {
179 var salt;
180 var key;
181 var cipher;
182 var iv;
183 var plaintext;
184 var ciphertext;
185 var tag;
186 var adata;
187
188 salt = [1,2,3,4,5,6,7,8];
189 key = generateKey(aKey, salt);
190
191 cipher = new aes(key, CCM);
192 iv = Random.random_words(4);
193
194 plaintext = aValue;
195 ciphertext = [];
196 tag = [];
197 adata = "";
198
199 cipher.encrypt(iv, plaintext, ciphertext, adata, tag);
200
201 return ciphertext;
202}
203
204//-----------------------------------------------------------------------------
205
206function decryptUsingJSCrypto (aKey, aValue) {
207 var salt;
208 var key;
209 var cipher;
210 var ciphertext;
211 var plaintext;
212 var tag;
213 var adata;
214
215 salt = [1,2,3,4,5,6,7,8];
216 key = generateKey(aKey, salt);
217 tag = [];
218 adata = "";
219
220 cipher = new aes(key, CCM);
221 ciphertext = aValue;
222 plaintext = cipher.decrypt(ciphertext, adata, tag);
223
224 return plaintext;
225}
226
227//=============================================================================
228
229function encryptUsingClipperz (aKey, aValue) {
230 var key;
231 var value;
232 var data;
233 var encryptedData;
234
235 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
236 value = aValue;
237 data = new Clipperz.ByteArray(value);
238 encryptedData = Clipperz.Crypto.AES.encrypt(key, data);
239 return encryptedData.toBase64String();
240}
241
242//-----------------------------------------------------------------------------
243
244function decryptUsingClipperz (aKey, aValue) {
245 var key;
246 var value;
247 var decryptedData;
248
249 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
250 value = new Clipperz.ByteArray().appendBase64String(aValue);
251
252 decryptedData = Clipperz.Crypto.AES.decrypt(key, value).asString();
253
254 return decryptedData;
255}
256
257//=============================================================================
258
259function encryptUsingClipperzAndJSON (aKey, aValue) {
260 var key;
261 var value;
262 var data;
263 var encryptedData;
264
265 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
266 value = Clipperz.Base.serializeJSON(aValue);
267 data = new Clipperz.ByteArray(value);
268 encryptedData = Clipperz.Crypto.AES.encrypt(key, data);
269 return encryptedData.toBase64String();
270}
271
272//-----------------------------------------------------------------------------
273
274function decryptUsingClipperzAndJSON (aKey, aValue) {
275 var key;
276 var value;
277 var decryptedData;
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).asString();
283
284 return Clipperz.Base.evalJSON(decryptedData);
285}
286
287//=============================================================================
288
289function deferredEncryptUsingClipperz (aKey, aValue) {
290 var deferredResult;
291 varkey;
292 var data;
293
294 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
295 data = new Clipperz.ByteArray(aValue);
296
297 deferredResult = new Clipperz.Async.Deferred("Clipperz.deferredEncrypt", {trace:false});
298 deferredResult.addCallback(Clipperz.Crypto.AES.deferredEncrypt, key, data);
299 deferredResult.addCallback(function(aResult) {
300 return aResult.toBase64String();
301 })
302 deferredResult.callback();
303
304 return deferredResult;
305}
306
307//-----------------------------------------------------------------------------
308
309function deferredDecryptUsingClipperz (aKey, aValue) {
310 var key, value;
311
312 key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey));
313 value = new Clipperz.ByteArray().appendBase64String(aValue);
314
315 deferredResult = new Clipperz.Async.Deferred("Clipperz.deferredDecrypt", {trace:false});
316 deferredResult.addCallback(Clipperz.Crypto.AES.deferredDecrypt, key, value);
317 deferredResult.addCallback(function(aResult) {
318 return aResult.asString();
319 });
320 deferredResult.callback();
321
322 return deferredResult;
323}
324
325//=============================================================================
326
327var tests = {
328
329 //-------------------------------------------------------------------------
330
331 'encryptMultipleStringsUsingClipperzFunctions': function (someTestArgs) {
332 // timeRegularFunction(encryptUsingClipperz, decryptUsingClipperz, times['Clipperz']['Regular']);
333 times['Clipperz']['NO JSON'] = {};
334
335 timeRegularFunction("Clipperz - NO JSON - Ascii", asciiTestString, encryptUsingClipperz, decryptUsingClipperz, times['Clipperz']['NO JSON'], 'ascii');
336 timeRegularFunction("Clipperz - NO JSON - ISO Latin 1", isoLatin1TestString,encryptUsingClipperz, decryptUsingClipperz, times['Clipperz']['NO JSON'], 'isoLatin1');
337 timeRegularFunction("Clipperz - NO JSON - UTF-8", utf8TestString, encryptUsingClipperz, decryptUsingClipperz, times['Clipperz']['NO JSON'], 'utf8');
338
339 appendResults("Clipperz - NO JSON", times['Clipperz']['NO JSON']);
340 },
341
342 //-------------------------------------------------------------------------
343
344 'encryptMultipleStringsUsingClipperzAndJSONFunctions': function (someTestArgs) {
345 // timeRegularFunction(encryptUsingClipperzAndJSON, decryptUsingClipperzAndJSON, times['Clipperz']['JSON']);
346 times['Clipperz']['JSON'] = {};
347
348 timeRegularFunction("Clipperz - JSON - Ascii", asciiTestString, encryptUsingClipperzAndJSON, decryptUsingClipperzAndJSON, times['Clipperz']['JSON'], 'ascii');
349 timeRegularFunction("Clipperz - JSON - ISO Latin 1", isoLatin1TestString,encryptUsingClipperzAndJSON, decryptUsingClipperzAndJSON, times['Clipperz']['JSON'], 'isoLatin1');
350 timeRegularFunction("Clipperz - JSON - UTF-8", utf8TestString, encryptUsingClipperzAndJSON, decryptUsingClipperzAndJSON, times['Clipperz']['JSON'], 'utf8');
351
352 appendResults("Clipperz - JSON", times['Clipperz']['JSON']);
353 },
354
355 //-------------------------------------------------------------------------
356
357 'encryptMultipleStringsUsingClipperzDeferredFunctions': function (someTestArgs) {
358 times['Clipperz']['Deferred'] = {};
359 times['Clipperz']['Deferred [NO JSON]'] = {};
360
361 return Clipperz.Async.callbacks("encryptMultipleStringsUsingClipperzDeferredFunctions", [
362 MochiKit.Base.partial(timeDeferredFunction, "Deferred Ascii", asciiTestString, deferredEncryptUsingClipperz, deferredDecryptUsingClipperz, times['Clipperz']['Deferred [NO JSON]'], 'ascii', someTestArgs),
363 // MochiKit.Base.partial(timeDeferredFunction, "Deferred IsoLatin1", isoLatin1TestString, deferredEncryptUsingClipperz, deferredDecryptUsingClipperz, times['Clipperz']['Deferred [NO JSON]'], 'isoLatin1',someTestArgs),
364 // MochiKit.Base.partial(timeDeferredFunction, "Deferred UTF-8", utf8TestString, deferredEncryptUsingClipperz, deferredDecryptUsingClipperz, times['Clipperz']['Deferred [NO JSON]'], 'utf8', someTestArgs),
365
366 MochiKit.Base.partial(appendResults, "Clipperz - deferred [NO JSON]", times['Clipperz']['Deferred [NO JSON]']),
367
368 MochiKit.Base.partial(timeDeferredFunction, "Deferred Ascii", asciiTestString, Clipperz.PM.Crypto.encryptingFunctions.versions['0.3'].deferredEncrypt, Clipperz.PM.Crypto.encryptingFunctions.versions['0.3'].deferredDecrypt, times['Clipperz']['Deferred'], 'ascii', someTestArgs),
369 // MochiKit.Base.partial(timeDeferredFunction, "Deferred IsoLatin1", isoLatin1TestString, Clipperz.PM.Crypto.encryptingFunctions.versions['0.3'].deferredEncrypt, Clipperz.PM.Crypto.encryptingFunctions.versions['0.3'].deferredDecrypt, times['Clipperz']['Deferred'], 'isoLatin1',someTestArgs),
370 // MochiKit.Base.partial(timeDeferredFunction, "Deferred UTF-8", utf8TestString, Clipperz.PM.Crypto.encryptingFunctions.versions['0.3'].deferredEncrypt, Clipperz.PM.Crypto.encryptingFunctions.versions['0.3'].deferredDecrypt, times['Clipperz']['Deferred'], 'utf8', someTestArgs),
371
372 MochiKit.Base.partial(appendResults, "Clipperz - PM", times['Clipperz']['Deferred'])
373 ], someTestArgs);
374 },
375
376 //-------------------------------------------------------------------------
377
378 'encryptMultipleStringsUsingJSCryptoFunctions': function (someTestArgs) {
379 timeRegularFunction("JSCrypto - Ascii", asciiTestString, encryptUsingJSCrypto, decryptUsingJSCrypto, times['JSCrypto'], 'ascii');
380 timeRegularFunction("JSCrypto - ISO Latin 1", isoLatin1TestString,encryptUsingJSCrypto, decryptUsingJSCrypto, times['JSCrypto'], 'isoLatin1');
381 timeRegularFunction("JSCrypto - UTF-8", utf8TestString, encryptUsingJSCrypto, decryptUsingJSCrypto, times['JSCrypto'], 'utf8');
382
383 appendResults("JSCrypto", times['JSCrypto']);
384 },
385
386 //-------------------------------------------------------------------------
387 'syntaxFix': MochiKit.Base.noop
388};
389
390//#############################################################################
391
392SimpleTest.waitForExplicitFinish();
393Clipperz.Crypto.PRNG.defaultRandomGenerator().fastEntropyAccumulationForTestingPurpose();
394Random.set_default_paranoia(0);
395
396//Random.addEventListener("seeded", MochiKit.Base.partial(SimpleTest.runDeferredTests, "Clipperz.Crypto.JSCrypto_vs_Clipperz", tests, {trace:false}));
397//Random.addEventListener("seeded", function () { console.log("SEEDED!")});
398
399
400MochiKit.DOM.appendChildNodes(MochiKit.DOM.currentDocument().body,
401 MochiKit.DOM.TABLE({border:'1', cellpadding:'4', cellspacing:'0'},
402 MochiKit.DOM.THEAD(),
403 MochiKit.DOM.TBODY({id:'timerTBODY'},
404 MochiKit.DOM.TR(null,
405 MochiKit.DOM.TH(null, "algorithm"),
406 MochiKit.DOM.TH(null, "ascii [" + asciiTestString.length + "]"),
407 MochiKit.DOM.TH(null, "ISO Latin 1 [" + isoLatin1TestString.length * 2 + "]"),
408 MochiKit.DOM.TH(null, "UTF 8 [" + utf8TestString.length * 3 + "]")
409 )
410 ),
411 MochiKit.DOM.TFOOT()
412 )//,
413 //MochiKit.DOM.H4(null, "AES chunkSize: " + Clipperz.Crypto.AES.DeferredExecution.chunkSize),
414 //MochiKit.DOM.H4(null, "AES pauseTime: " + Clipperz.Crypto.AES.DeferredExecution.pauseTime)
415);
416
417
418SimpleTest.runDeferredTests("Clipperz.Crypto.JSCrypto_vs_Clipperz", tests, {trace:false}); \ No newline at end of file