summaryrefslogtreecommitdiff
path: root/frontend/beta/js/Clipperz/Crypto/AES_2.js
Unidiff
Diffstat (limited to 'frontend/beta/js/Clipperz/Crypto/AES_2.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/beta/js/Clipperz/Crypto/AES_2.js829
1 files changed, 829 insertions, 0 deletions
diff --git a/frontend/beta/js/Clipperz/Crypto/AES_2.js b/frontend/beta/js/Clipperz/Crypto/AES_2.js
new file mode 100644
index 0000000..9735d17
--- a/dev/null
+++ b/frontend/beta/js/Clipperz/Crypto/AES_2.js
@@ -0,0 +1,829 @@
1/*
2
3Copyright 2008-2013 Clipperz Srl
4
5This file is part of Clipperz, the online password manager.
6For further information about its features and functionalities please
7refer to http://www.clipperz.com.
8
9* Clipperz is free software: you can redistribute it and/or modify it
10 under the terms of the GNU Affero General Public License as published
11 by the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14* Clipperz is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 See the GNU Affero General Public License for more details.
18
19* You should have received a copy of the GNU Affero General Public
20 License along with Clipperz. If not, see http://www.gnu.org/licenses/.
21
22*/
23
24try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) {
25 throw "Clipperz.Crypto.AES_2 depends on Clipperz.ByteArray!";
26}
27
28 //Dependency commented to avoid a circular reference
29//try { if (typeof(Clipperz.Crypto.PRNG) == 'undefined') { throw ""; }} catch (e) {
30 //throw "Clipperz.Crypto.AES_2 depends on Clipperz.Crypto.PRNG!";
31//}
32
33if (typeof(Clipperz.Crypto.AES_2) == 'undefined') { Clipperz.Crypto.AES_2 = {}; }
34
35//#############################################################################
36
37Clipperz.Crypto.AES_2.DeferredExecutionContext = function(args) {
38 args = args || {};
39
40 this._key = args.key;
41 this._message = args.message;
42 this._result = args.message.clone();
43 this._nonce = args.nonce;
44 this._messageLength = this._message.length();
45
46 this._messageArray = this._message.arrayValues();
47 this._resultArray = this._result.arrayValues();
48 this._nonceArray = this._nonce.arrayValues();
49
50 this._executionStep = 0;
51
52 return this;
53}
54
55Clipperz.Crypto.AES_2.DeferredExecutionContext.prototype = MochiKit.Base.update(null, {
56
57 'key': function() {
58 return this._key;
59 },
60
61 'message': function() {
62 return this._message;
63 },
64
65 'messageLength': function() {
66 return this._messageLength;
67 },
68
69 'result': function() {
70 return new Clipperz.ByteArray(this.resultArray());
71 },
72
73 'nonce': function() {
74 return this._nonce;
75 },
76
77 'messageArray': function() {
78 return this._messageArray;
79 },
80
81 'resultArray': function() {
82 return this._resultArray;
83 },
84
85 'nonceArray': function() {
86 return this._nonceArray;
87 },
88
89 'elaborationChunkSize': function() {
90 return Clipperz.Crypto.AES_2.DeferredExecution.chunkSize;
91 },
92
93 'executionStep': function() {
94 return this._executionStep;
95 },
96
97 'setExecutionStep': function(aValue) {
98 this._executionStep = aValue;
99 },
100
101 'pause': function(aValue) {
102 return MochiKit.Async.wait(Clipperz.Crypto.AES_2.DeferredExecution.pauseTime, aValue);
103 },
104
105 //-----------------------------------------------------------------------------
106 __syntaxFix__: "syntax fix"
107
108});
109
110//#############################################################################
111
112Clipperz.Crypto.AES_2.Key = function(args) {
113 args = args || {};
114
115 this._key = args.key;
116 this._keySize = args.keySize || this.key().length();
117
118 if (this.keySize() == 128/8) {
119 this._b = 176;
120 this._numberOfRounds = 10;
121 } else if (this.keySize() == 256/8) {
122 this._b = 240;
123 this._numberOfRounds = 14;
124 } else {
125 MochiKit.Logging.logError("AES unsupported key size: " + (this.keySize() * 8) + " bits");
126 throw Clipperz.Crypto.AES_2.exception.UnsupportedKeySize;
127 }
128
129 this._stretchedKey = null;
130
131 return this;
132}
133
134Clipperz.Crypto.AES_2.Key.prototype = MochiKit.Base.update(null, {
135
136 'asString': function() {
137 return "Clipperz.Crypto.AES_2.Key (" + this.key().toHexString() + ")";
138 },
139
140 //-----------------------------------------------------------------------------
141
142 'key': function() {
143 return this._key;
144 },
145
146 'keySize': function() {
147 return this._keySize;
148 },
149
150 'b': function() {
151 return this._b;
152 },
153
154 'numberOfRounds': function() {
155 return this._numberOfRounds;
156 },
157 //=========================================================================
158
159 'keyScheduleCore': function(aWord, aRoundConstantsIndex) {
160 varresult;
161 var sbox;
162
163 sbox = Clipperz.Crypto.AES_2.sbox();
164
165 result = [sbox[aWord[1]] ^ Clipperz.Crypto.AES_2.roundConstants()[aRoundConstantsIndex],
166 sbox[aWord[2]],
167 sbox[aWord[3]],
168 sbox[aWord[0]]];
169
170 return result;
171 },
172
173 //-----------------------------------------------------------------------------
174
175 'xorWithPreviousStretchValues': function(aKey, aWord, aPreviousWordIndex) {
176 varresult;
177 var i,c;
178
179 result = [];
180 c = 4;
181 for (i=0; i<c; i++) {
182 result[i] = aWord[i] ^ aKey.byteAtIndex(aPreviousWordIndex + i);
183 }
184
185 return result;
186 },
187
188 //-----------------------------------------------------------------------------
189
190 'sboxShakeup': function(aWord) {
191 var result;
192 var sbox;
193 var i,c;
194
195 result = [];
196 sbox = Clipperz.Crypto.AES_2.sbox();
197 c =4;
198 for (i=0; i<c; i++) {
199 result[i] = sbox[aWord[i]];
200 }
201
202 return result;
203 },
204
205 //-----------------------------------------------------------------------------
206
207 'stretchKey': function(aKey) {
208 varcurrentWord;
209 varkeyLength;
210 varpreviousStretchIndex;
211 var i,c;
212
213 keyLength = aKey.length();
214 previousStretchIndex = keyLength - this.keySize();
215
216 currentWord = [aKey.byteAtIndex(keyLength - 4),
217 aKey.byteAtIndex(keyLength - 3),
218 aKey.byteAtIndex(keyLength - 2),
219 aKey.byteAtIndex(keyLength - 1)];
220 currentWord = this.keyScheduleCore(currentWord, keyLength / this.keySize());
221
222 if (this.keySize() == 256/8) {
223 c = 8;
224 } else if (this.keySize() == 128/8){
225 c = 4;
226 }
227
228 for (i=0; i<c; i++) {
229 if (i == 4) {
230 //fifth streatch word
231 currentWord = this.sboxShakeup(currentWord);
232 }
233
234 currentWord = this.xorWithPreviousStretchValues(aKey, currentWord, previousStretchIndex + (i*4));
235 aKey.appendBytes(currentWord);
236 }
237
238 return aKey;
239 },
240
241 //-----------------------------------------------------------------------------
242
243 'stretchedKey': function() {
244 if (this._stretchedKey == null) {
245 var stretchedKey;
246
247 stretchedKey = this.key().clone();
248
249 while (stretchedKey.length() < this.keySize()) {
250 stretchedKey.appendByte(0);
251 }
252
253 while (stretchedKey.length() < this.b()) {
254 stretchedKey = this.stretchKey(stretchedKey);
255 }
256
257 this._stretchedKey = stretchedKey.split(0, this.b());
258 }
259
260 return this._stretchedKey;
261 },
262
263 //=========================================================================
264 __syntaxFix__: "syntax fix"
265});
266
267//#############################################################################
268
269Clipperz.Crypto.AES_2.State = function(args) {
270 args = args || {};
271
272 this._data = args.block.slice(0);
273 this._key = args.key;
274
275 return this;
276}
277
278Clipperz.Crypto.AES_2.State.prototype = MochiKit.Base.update(null, {
279
280 'key': function() {
281 return this._key;
282 },
283
284 //-----------------------------------------------------------------------------
285
286 'data': function() {
287 return this._data;
288 },
289
290 'setData': function(aValue) {
291 this._data = aValue;
292 },
293
294 //=========================================================================
295
296 'addRoundKey': function(aRoundNumber) {
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.
298 vardata;
299 varstretchedKey;
300 varfirstStretchedKeyIndex;
301 var i,c;
302
303 data = this.data();
304 stretchedKey = this.key().stretchedKey();
305 firstStretchedKeyIndex = aRoundNumber * (128/8);
306 c = 128/8;
307 for (i=0; i<c; i++) {
308 data[i] = data[i] ^ stretchedKey.byteAtIndex(firstStretchedKeyIndex + i);
309 }
310 },
311
312 //-----------------------------------------------------------------------------
313
314 'subBytes': function() {
315 // a non-linear substitution step where each byte is replaced with another according to a lookup table.
316 var i,c;
317 vardata;
318 var sbox;
319
320 data = this.data();
321 sbox = Clipperz.Crypto.AES_2.sbox();
322
323 c = 16;
324 for (i=0; i<c; i++) {
325 data[i] = sbox[data[i]];
326 }
327 },
328
329 //-----------------------------------------------------------------------------
330
331 'shiftRows': function() {
332 //a transposition step where each row of the state is shifted cyclically a certain number of steps.
333 varnewValue;
334 vardata;
335 varshiftMapping;
336 vari,c;
337
338 newValue = new Array(16);
339 data = this.data();
340 shiftMapping = Clipperz.Crypto.AES_2.shiftRowMapping();
341 // [0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11];
342 c = 16;
343 for (i=0; i<c; i++) {
344 newValue[i] = data[shiftMapping[i]];
345 }
346 for (i=0; i<c; i++) {
347 data[i] = newValue[i];
348 }
349 },
350
351 //-----------------------------------------------------------------------------
352/*
353 'mixColumnsWithValues': function(someValues) {
354 varresult;
355 vara;
356 var i,c;
357
358 c = 4;
359 result = [];
360 a = [];
361 for (i=0; i<c; i++) {
362 a[i] = [];
363 a[i][1] = someValues[i]
364 if ((a[i][1] & 0x80) == 0x80) {
365 a[i][2] = (a[i][1] << 1) ^ 0x11b;
366 } else {
367 a[i][2] = a[i][1] << 1;
368 }
369
370 a[i][3] = a[i][2] ^ a[i][1];
371 }
372
373 for (i=0; i<c; i++) {
374 varx;
375
376 x = Clipperz.Crypto.AES_2.mixColumnsMatrix()[i];
377 result[i] = a[0][x[0]] ^ a[1][x[1]] ^ a[2][x[2]] ^ a[3][x[3]];
378 }
379
380 return result;
381 },
382
383 'mixColumns': function() {
384 //a mixing operation which operates on the columns of the state, combining the four bytes in each column using a linear transformation.
385 var data;
386 var i, c;
387
388 data = this.data();
389 c = 4;
390 for(i=0; i<c; i++) {
391 varblockIndex;
392 var mixedValues;
393
394 blockIndex = i * 4;
395 mixedValues = this.mixColumnsWithValues([data[blockIndex + 0],
396 data[blockIndex + 1],
397 data[blockIndex + 2],
398 data[blockIndex + 3]]);
399 data[blockIndex + 0] = mixedValues[0];
400 data[blockIndex + 1] = mixedValues[1];
401 data[blockIndex + 2] = mixedValues[2];
402 data[blockIndex + 3] = mixedValues[3];
403 }
404 },
405*/
406
407 'mixColumns': function() {
408 //a mixing operation which operates on the columns of the state, combining the four bytes in each column using a linear transformation.
409 var data;
410 var i, c;
411 var a_1;
412 var a_2;
413
414 a_1 = new Array(4);
415 a_2 = new Array(4);
416
417 data = this.data();
418 c = 4;
419 for(i=0; i<c; i++) {
420 varblockIndex;
421 var ii, cc;
422
423 blockIndex = i * 4;
424
425 cc = 4;
426 for (ii=0; ii<cc; ii++) {
427 var value;
428
429 value = data[blockIndex + ii];
430 a_1[ii] = value;
431 a_2[ii] = (value & 0x80) ? ((value << 1) ^ 0x011b) : (value << 1);
432 }
433
434 data[blockIndex + 0] = a_2[0] ^ a_1[1] ^ a_2[1] ^ a_1[2] ^ a_1[3];
435 data[blockIndex + 1] = a_1[0] ^ a_2[1] ^ a_1[2] ^ a_2[2] ^ a_1[3];
436 data[blockIndex + 2] = a_1[0] ^ a_1[1] ^ a_2[2] ^ a_1[3] ^ a_2[3];
437 data[blockIndex + 3] = a_1[0] ^ a_2[0] ^ a_1[1] ^ a_1[2] ^ a_2[3];
438 }
439 },
440
441 //=========================================================================
442
443 'spinRound': function(aRoundNumber) {
444 this.addRoundKey(aRoundNumber);
445 this.subBytes();
446 this.shiftRows();
447 this.mixColumns();
448 },
449
450 'spinLastRound': function() {
451 this.addRoundKey(this.key().numberOfRounds() - 1);
452 this.subBytes();
453 this.shiftRows();
454 this.addRoundKey(this.key().numberOfRounds());
455 },
456
457 //=========================================================================
458
459 'encrypt': function() {
460 vari,c;
461
462 c = this.key().numberOfRounds() - 1;
463 for (i=0; i<c; i++) {
464 this.spinRound(i);
465 }
466
467 this.spinLastRound();
468 },
469
470 //=========================================================================
471 __syntaxFix__: "syntax fix"
472});
473
474//#############################################################################
475
476Clipperz.Crypto.AES_2.VERSION = "0.1";
477Clipperz.Crypto.AES_2.NAME = "Clipperz.Crypto.AES_2";
478
479MochiKit.Base.update(Clipperz.Crypto.AES_2, {
480
481 //http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-AES.html
482 //http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
483 //http://en.wikipedia.org/wiki/Rijndael_key_schedule
484 //http://en.wikipedia.org/wiki/Rijndael_S-box
485
486 '__repr__': function () {
487 return "[" + this.NAME + " " + this.VERSION + "]";
488 },
489
490 'toString': function () {
491 return this.__repr__();
492 },
493
494 //=============================================================================
495
496 '_sbox': null,
497 'sbox': function() {
498 if (Clipperz.Crypto.AES_2._sbox == null) {
499 Clipperz.Crypto.AES_2._sbox = [
5000x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
5010xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
5020xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
5030x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
5040x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
5050x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
5060xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
5070x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
5080xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
5090x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
5100xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
5110xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
5120xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
5130x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
5140xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
5150x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
516 ];
517 }
518
519 return Clipperz.Crypto.AES_2._sbox;
520 },
521
522 //-----------------------------------------------------------------------------
523 //
524 // 0 4 8 12 0 4 812
525 // 1 5 9 13 => 5 9 131
526 // 2 6 10 14 10 14 26
527 // 3 7 11 15 15 3 711
528 //
529 '_shiftRowMapping': null,
530 'shiftRowMapping': function() {
531 if (Clipperz.Crypto.AES_2._shiftRowMapping == null) {
532 Clipperz.Crypto.AES_2._shiftRowMapping = [0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11];
533 }
534
535 return Clipperz.Crypto.AES_2._shiftRowMapping;
536 },
537
538 //-----------------------------------------------------------------------------
539
540 '_mixColumnsMatrix': null,
541 'mixColumnsMatrix': function() {
542 if (Clipperz.Crypto.AES_2._mixColumnsMatrix == null) {
543 Clipperz.Crypto.AES_2._mixColumnsMatrix = [[2, 3, 1 ,1],
544 [1, 2, 3, 1],
545 [1, 1, 2, 3],
546 [3, 1, 1, 2] ];
547 }
548
549 return Clipperz.Crypto.AES_2._mixColumnsMatrix;
550 },
551
552 '_roundConstants': null,
553 'roundConstants': function() {
554 if (Clipperz.Crypto.AES_2._roundConstants == null) {
555 Clipperz.Crypto.AES_2._roundConstants = [ , 1, 2, 4, 8, 16, 32, 64, 128, 27, 54, 108, 216, 171, 77, 154];
556 // Clipperz.Crypto.AES_2._roundConstants = [ , 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a];
557 }
558
559 return Clipperz.Crypto.AES_2._roundConstants;
560 },
561
562 //=============================================================================
563
564 'incrementNonce': function(nonce) {
565 var i;
566 var done;
567
568 done = false;
569 i = nonce.length - 1;
570
571 while ((i>=0) && (done == false)) {
572 var currentByteValue;
573
574 currentByteValue = nonce[i];
575
576 if (currentByteValue == 0xff) {
577 nonce[i] = 0;
578 if (i>= 0) {
579 i --;
580 } else {
581 done = true;
582 }
583 } else {
584 nonce[i] = currentByteValue + 1;
585 done = true;
586 }
587 }
588 },
589
590 //-----------------------------------------------------------------------------
591
592 'encryptBlock': function(aKey, aBlock) {
593 varresult;
594 varstate;
595
596 state = new Clipperz.Crypto.AES_2.State({block:aBlock, key:aKey});
597//is(state.data(), 'before');
598 state.encrypt();
599 result = state.data();
600
601 return result;
602 },
603
604 //-----------------------------------------------------------------------------
605
606 'encryptBlocks': function(aKey, aMessage, aNonce) {
607 varresult;
608 var nonce;
609 var self;
610 varmessageIndex;
611 varmessageLength;
612 var blockSize;
613
614 self = Clipperz.Crypto.AES_2;
615 blockSize = 128/8;
616 messageLength = aMessage.length;
617 nonce = aNonce;
618
619 result = aMessage;
620 messageIndex = 0;
621 while (messageIndex < messageLength) {
622 var encryptedBlock;
623 var i,c;
624
625 encryptedBlock = self.encryptBlock(aKey, nonce);
626
627 if ((messageLength - messageIndex) > blockSize) {
628 c = blockSize;
629 } else {
630 c = messageLength - messageIndex;
631 }
632
633 for (i=0; i<c; i++) {
634 result[messageIndex + i] = result[messageIndex + i] ^ encryptedBlock[i];
635 }
636
637 messageIndex += blockSize;
638 self.incrementNonce(nonce);
639 }
640
641 return result;
642 },
643
644 //-----------------------------------------------------------------------------
645
646 'encrypt': function(aKey, someData, aNonce) {
647 var result;
648 var nonce;
649 varencryptedData;
650 var key;
651
652 key = new Clipperz.Crypto.AES_2.Key({key:aKey});
653 nonce = aNonce ? aNonce.clone() : Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(128/8);
654
655 encryptedData = Clipperz.Crypto.AES_2.encryptBlocks(key, someData.arrayValues(), nonce.arrayValues());
656
657 result = nonce.appendBytes(encryptedData);
658
659 return result;
660 },
661
662 //-----------------------------------------------------------------------------
663
664 'decrypt': function(aKey, someData) {
665 var result;
666 var nonce;
667 var encryptedData;
668 var decryptedData;
669 vardataIterator;
670 var key;
671
672 key = new Clipperz.Crypto.AES_2.Key({key:aKey});
673
674 encryptedData = someData.arrayValues();
675 nonce = encryptedData.slice(0, (128/8));
676 encryptedData = encryptedData.slice(128/8);
677 decryptedData = Clipperz.Crypto.AES_2.encryptBlocks(key, encryptedData, nonce);
678
679 result = new Clipperz.ByteArray(decryptedData);
680
681 return result;
682 },
683
684 //=============================================================================
685
686 'deferredEncryptExecutionChunk': function(anExecutionContext) {
687 varresult;
688 var nonce;
689 var self;
690 varmessageIndex;
691 varmessageLength;
692 var blockSize;
693 var executionLimit;
694
695 self = Clipperz.Crypto.AES_2;
696 blockSize = 128/8;
697 messageLength = anExecutionContext.messageArray().length;
698 nonce = anExecutionContext.nonceArray();
699 result = anExecutionContext.resultArray();
700
701 messageIndex = anExecutionContext.executionStep();
702 executionLimit = messageIndex + anExecutionContext.elaborationChunkSize();
703 executionLimit = Math.min(executionLimit, messageLength);
704
705 while (messageIndex < executionLimit) {
706 var encryptedBlock;
707 var i,c;
708
709 encryptedBlock = self.encryptBlock(anExecutionContext.key(), nonce);
710
711 if ((executionLimit - messageIndex) > blockSize) {
712 c = blockSize;
713 } else {
714 c = executionLimit - messageIndex;
715 }
716
717 for (i=0; i<c; i++) {
718 result[messageIndex + i] = result[messageIndex + i] ^ encryptedBlock[i];
719 }
720
721 messageIndex += blockSize;
722 self.incrementNonce(nonce);
723 }
724 anExecutionContext.setExecutionStep(messageIndex);
725
726 return anExecutionContext;
727 },
728
729 //-----------------------------------------------------------------------------
730
731 'deferredEncryptBlocks': function(anExecutionContext) {
732 vardeferredResult;
733 varmessageSize;
734 var i,c;
735 var now;
736
737 messageSize = anExecutionContext.messageLength();
738
739 deferredResult = new MochiKit.Async.Deferred();
740//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Clipperz.Crypto.AES_2.deferredEncryptBlocks - START: " + res); return res;});
741 // deferredResult.addCallback(MochiKit.Base.method(anExecutionContext, 'pause'));
742
743 c = Math.ceil(messageSize / anExecutionContext.elaborationChunkSize());
744 for (i=0; i<c; i++) {
745//deferredResult.addBoth(function(res) {now = new Date(); return res;});
746//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Clipperz.Crypto.AES_2.deferredEncryptBlocks - : (" + i + ") - " + res); return res;});
747 deferredResult.addCallback(Clipperz.Crypto.AES_2.deferredEncryptExecutionChunk);
748//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "]Clipperz.Crypto.AES_2.deferredEncryptBlocks"); return res;});
749//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Clipperz.Crypto.AES_2.deferredEncryptBlocks - : (" + i + ") -- " + res); return res;});
750 deferredResult.addCallback(MochiKit.Base.method(anExecutionContext, 'pause'));
751//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Clipperz.Crypto.AES_2.deferredEncryptBlocks - : (" + i + ") --- " + res); return res;});
752 }
753//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Clipperz.Crypto.AES_2.deferredEncryptBlocks - END: " + res); return res;});
754
755 deferredResult.callback(anExecutionContext);
756
757 return deferredResult;
758 },
759
760 //-----------------------------------------------------------------------------
761
762 'deferredEncrypt': function(aKey, someData, aNonce) {
763 var deferredResult;
764 varexecutionContext;
765 var result;
766 var nonce;
767 var key;
768
769 key = new Clipperz.Crypto.AES_2.Key({key:aKey});
770 nonce = aNonce ? aNonce.clone() : Clipperz.Crypto.PRNG.defaultRandomGenerator().getRandomBytes(128/8);
771
772 executionContext = new Clipperz.Crypto.AES_2.DeferredExecutionContext({key:key, message:someData, nonce:nonce});
773
774 deferredResult = new MochiKit.Async.Deferred();
775//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Clipperz.Crypto.AES_2.deferredEncrypt - 1: " + res); return res;});
776 deferredResult.addCallback(Clipperz.Crypto.AES_2.deferredEncryptBlocks);
777//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Clipperz.Crypto.AES_2.deferredEncrypt - 2: " + res); return res;});
778 deferredResult.addCallback(function(anExecutionContext) {
779 var result;
780
781 result = anExecutionContext.nonce().clone();
782 result.appendBytes(anExecutionContext.resultArray());
783
784 return result;
785 });
786//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Clipperz.Crypto.AES_2.deferredEncrypt - 3: " + res); return res;});
787 deferredResult.callback(executionContext)
788
789 return deferredResult;
790 },
791
792 //-----------------------------------------------------------------------------
793
794 'deferredDecrypt': function(aKey, someData) {
795 var deferredResult
796 var nonce;
797 var message;
798 var key;
799
800 key = new Clipperz.Crypto.AES_2.Key({key:aKey});
801 nonce = someData.split(0, (128/8));
802 message = someData.split(128/8);
803 executionContext = new Clipperz.Crypto.AES_2.DeferredExecutionContext({key:key, message:message, nonce:nonce});
804
805 deferredResult = new MochiKit.Async.Deferred();
806 deferredResult.addCallback(Clipperz.Crypto.AES_2.deferredEncryptBlocks);
807 deferredResult.addCallback(function(anExecutionContext) {
808 return anExecutionContext.result();
809 });
810 deferredResult.callback(executionContext);
811
812 return deferredResult;
813 },
814
815 //-----------------------------------------------------------------------------
816 __syntaxFix__: "syntax fix"
817
818});
819
820//#############################################################################
821
822Clipperz.Crypto.AES_2.DeferredExecution = {
823 'chunkSize': 4096, // 1024 4096 8192 1638432768;
824 'pauseTime': 0.2
825}
826
827Clipperz.Crypto.AES_2.exception = {
828 'UnsupportedKeySize': new MochiKit.Base.NamedError("Clipperz.Crypto.AES_2.exception.UnsupportedKeySize")
829};