forked from meganz/webclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsaasm.js
executable file
·6718 lines (5488 loc) · 236 KB
/
rsaasm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This RSA code from asmCrypto.js was created by doing:
* git clone https://github.com/vibornoff/asmcrypto.js.git
* git checkout v0.0.11
* npm install
* grunt --with="common,utils,exports,globals,rng,rng-exports,rng-globals,bn,bn-exports,rsa,rsa-raw,rsa-pkcs1,rsa-keygen-exports,rsa-raw-exports,aes-cbc" devel
*/
/*! asmCrypto v0.0.11, (c) 2013 Artem S Vybornov, opensource.org/licenses/MIT */
(function ( exports, global ) {
function IllegalStateError () { var err = Error.apply( this, arguments ); this.message = err.message, this.stack = err.stack; }
IllegalStateError.prototype = Object.create( Error.prototype, { name: { value: 'IllegalStateError' } } );
function IllegalArgumentError () { var err = Error.apply( this, arguments ); this.message = err.message, this.stack = err.stack; }
IllegalArgumentError.prototype = Object.create( Error.prototype, { name: { value: 'IllegalArgumentError' } } );
function SecurityError () { var err = Error.apply( this, arguments ); this.message = err.message, this.stack = err.stack; }
SecurityError.prototype = Object.create( Error.prototype, { name: { value: 'SecurityError' } } );
var FloatArray = global.Float64Array || global.Float32Array; // make PhantomJS happy
function string_to_bytes ( str, utf8 ) {
utf8 = !!utf8;
var len = str.length,
bytes = new Uint8Array( utf8 ? 4*len : len );
for ( var i = 0, j = 0; i < len; i++ ) {
var c = str.charCodeAt(i);
if ( utf8 && 0xd800 <= c && c <= 0xdbff ) {
if ( ++i >= len ) throw new Error( "Malformed string, low surrogate expected at position " + i );
c = ( (c ^ 0xd800) << 10 ) | 0x10000 | ( str.charCodeAt(i) ^ 0xdc00 );
}
else if ( !utf8 && c >>> 8 ) {
throw new Error("Wide characters are not allowed.");
}
if ( !utf8 || c <= 0x7f ) {
bytes[j++] = c;
}
else if ( c <= 0x7ff ) {
bytes[j++] = 0xc0 | (c >> 6);
bytes[j++] = 0x80 | (c & 0x3f);
}
else if ( c <= 0xffff ) {
bytes[j++] = 0xe0 | (c >> 12);
bytes[j++] = 0x80 | (c >> 6 & 0x3f);
bytes[j++] = 0x80 | (c & 0x3f);
}
else {
bytes[j++] = 0xf0 | (c >> 18);
bytes[j++] = 0x80 | (c >> 12 & 0x3f);
bytes[j++] = 0x80 | (c >> 6 & 0x3f);
bytes[j++] = 0x80 | (c & 0x3f);
}
}
return bytes.subarray(0, j);
}
function hex_to_bytes ( str ) {
var len = str.length;
if ( len & 1 ) {
str = '0'+str;
len++;
}
var bytes = new Uint8Array(len>>1);
for ( var i = 0; i < len; i += 2 ) {
bytes[i>>1] = parseInt( str.substr( i, 2), 16 );
}
return bytes;
}
function base64_to_bytes ( str ) {
return string_to_bytes( atob( str ) );
}
function bytes_to_string ( bytes, utf8 ) {
utf8 = !!utf8;
var len = bytes.length,
chars = new Array(len);
for ( var i = 0, j = 0; i < len; i++ ) {
var b = bytes[i];
if ( !utf8 || b < 128 ) {
chars[j++] = b;
}
else if ( b >= 192 && b < 224 && i+1 < len ) {
chars[j++] = ( (b & 0x1f) << 6 ) | (bytes[++i] & 0x3f);
}
else if ( b >= 224 && b < 240 && i+2 < len ) {
chars[j++] = ( (b & 0xf) << 12 ) | ( (bytes[++i] & 0x3f) << 6 ) | (bytes[++i] & 0x3f);
}
else if ( b >= 240 && b < 248 && i+3 < len ) {
var c = ( (b & 7) << 18 ) | ( (bytes[++i] & 0x3f) << 12 ) | ( (bytes[++i] & 0x3f) << 6 ) | (bytes[++i] & 0x3f);
if ( c <= 0xffff ) {
chars[j++] = c;
}
else {
c ^= 0x10000;
chars[j++] = 0xd800 | (c >> 10);
chars[j++] = 0xdc00 | (c & 0x3ff);
}
}
else {
throw new Error("Malformed UTF8 character at byte offset " + i);
}
}
var str = '',
bs = 16384;
for ( var i = 0; i < j; i += bs ) {
str += String.fromCharCode.apply( String, chars.slice( i, i+bs <= j ? i+bs : j ) );
}
return str;
}
function bytes_to_hex ( arr ) {
var str = '';
for ( var i = 0; i < arr.length; i++ ) {
var h = ( arr[i] & 0xff ).toString(16);
if ( h.length < 2 ) str += '0';
str += h;
}
return str;
}
function bytes_to_base64 ( arr ) {
return btoa( bytes_to_string(arr) );
}
function pow2_ceil ( a ) {
a -= 1;
a |= a >>> 1;
a |= a >>> 2;
a |= a >>> 4;
a |= a >>> 8;
a |= a >>> 16;
a += 1;
return a;
}
function is_number ( a ) {
return ( typeof a === 'number' );
}
function is_string ( a ) {
return ( typeof a === 'string' );
}
function is_buffer ( a ) {
return ( a instanceof ArrayBuffer );
}
function is_bytes ( a ) {
return ( a instanceof Uint8Array );
}
function is_typed_array ( a ) {
return ( a instanceof Int8Array ) || ( a instanceof Uint8Array )
|| ( a instanceof Int16Array ) || ( a instanceof Uint16Array )
|| ( a instanceof Int32Array ) || ( a instanceof Uint32Array )
|| ( a instanceof Float32Array )
|| ( a instanceof Float64Array );
}
function _heap_init ( constructor, options ) {
var heap = options.heap,
size = heap ? heap.byteLength : options.heapSize || 65536;
if ( size & 0xfff || size <= 0 )
throw new Error("heap size must be a positive integer and a multiple of 4096");
heap = heap || new constructor( new ArrayBuffer(size) );
return heap;
}
function _heap_write ( heap, hpos, data, dpos, dlen ) {
var hlen = heap.length - hpos,
wlen = ( hlen < dlen ) ? hlen : dlen;
heap.set( data.subarray( dpos, dpos+wlen ), hpos );
return wlen;
}
/**
* Util exports
*/
exports.string_to_bytes = string_to_bytes;
exports.hex_to_bytes = hex_to_bytes;
exports.base64_to_bytes = base64_to_bytes;
exports.bytes_to_string = bytes_to_string;
exports.bytes_to_hex = bytes_to_hex;
exports.bytes_to_base64 = bytes_to_base64;
/**
* Error definitions
*/
global.IllegalStateError = IllegalStateError;
global.IllegalArgumentError = IllegalArgumentError;
global.SecurityError = SecurityError;
/**
* @file {@link http://asmjs.org Asm.js} implementation of the {@link https://en.wikipedia.org/wiki/Advanced_Encryption_Standard Advanced Encryption Standard}.
* @author Artem S Vybornov <[email protected]>
* @license MIT
*/
var AES_asm = function () {
"use strict";
/**
* Galois Field stuff init flag
*/
var ginit_done = false;
/**
* Galois Field exponentiation and logarithm tables for 3 (the generator)
*/
var gexp3, glog3;
/**
* Init Galois Field tables
*/
function ginit () {
gexp3 = [],
glog3 = [];
var a = 1, c, d;
for ( c = 0; c < 255; c++ ) {
gexp3[c] = a;
// Multiply by three
d = a & 0x80, a <<= 1, a &= 255;
if ( d === 0x80 ) a ^= 0x1b;
a ^= gexp3[c];
// Set the log table value
glog3[gexp3[c]] = c;
}
gexp3[255] = gexp3[0];
glog3[0] = 0;
ginit_done = true;
}
/**
* Galois Field multiplication
* @param {int} a
* @param {int} b
* @return {int}
*/
function gmul ( a, b ) {
var c = gexp3[ ( glog3[a] + glog3[b] ) % 255 ];
if ( a === 0 || b === 0 ) c = 0;
return c;
}
/**
* Galois Field reciprocal
* @param {int} a
* @return {int}
*/
function ginv ( a ) {
var i = gexp3[ 255 - glog3[a] ];
if ( a === 0 ) i = 0;
return i;
}
/**
* AES stuff init flag
*/
var aes_init_done = false;
/**
* Encryption, Decryption, S-Box and KeyTransform tables
*/
var aes_sbox, aes_sinv, aes_enc, aes_dec;
/**
* Init AES tables
*/
function aes_init () {
if ( !ginit_done ) ginit();
// Calculates AES S-Box value
function _s ( a ) {
var c, s, x;
s = x = ginv(a);
for ( c = 0; c < 4; c++ ) {
s = ( (s << 1) | (s >>> 7) ) & 255;
x ^= s;
}
x ^= 99;
return x;
}
// Tables
aes_sbox = [],
aes_sinv = [],
aes_enc = [ [], [], [], [] ],
aes_dec = [ [], [], [], [] ];
for ( var i = 0; i < 256; i++ ) {
var s = _s(i);
// S-Box and its inverse
aes_sbox[i] = s;
aes_sinv[s] = i;
// Ecryption and Decryption tables
aes_enc[0][i] = ( gmul( 2, s ) << 24 ) | ( s << 16 ) | ( s << 8 ) | gmul( 3, s );
aes_dec[0][s] = ( gmul( 14, i ) << 24 ) | ( gmul( 9, i ) << 16 ) | ( gmul( 13, i ) << 8 ) | gmul( 11, i );
// Rotate tables
for ( var t = 1; t < 4; t++ ) {
aes_enc[t][i] = ( aes_enc[t-1][i] >>> 8 ) | ( aes_enc[t-1][i] << 24 );
aes_dec[t][s] = ( aes_dec[t-1][s] >>> 8 ) | ( aes_dec[t-1][s] << 24 );
}
}
}
/**
* Asm.js module constructor.
*
* <p>
* Heap buffer layout by offset:
* <pre>
* 0x0000 encryption key schedule
* 0x0400 decryption key schedule
* 0x0800 sbox
* 0x0c00 inv sbox
* 0x1000 encryption tables
* 0x2000 decryption tables
* 0x3000 reserved (future GCM multiplication lookup table)
* 0x4000 data
* </pre>
* Don't touch anything before <code>0x400</code>.
* </p>
*
* @alias AES_asm
* @class
* @param {GlobalScope} stdlib - global scope object (e.g. <code>window</code>)
* @param {Object} foreign - <i>ignored</i>
* @param {ArrayBuffer} buffer - heap buffer to link with
*/
var wrapper = function ( stdlib, foreign, buffer ) {
// Init AES stuff for the first time
if ( !aes_init_done ) aes_init();
// Fill up AES tables
var heap = new Uint32Array(buffer);
heap.set( aes_sbox, 0x0800>>2 );
heap.set( aes_sinv, 0x0c00>>2 );
for ( var i = 0; i < 4; i++ ) {
heap.set( aes_enc[i], ( 0x1000 + 0x400 * i )>>2 );
heap.set( aes_dec[i], ( 0x2000 + 0x400 * i )>>2 );
}
/**
* Calculate AES key schedules.
* @instance
* @memberof AES_asm
* @param {int} ks - key size, 4/6/8 (for 128/192/256-bit key correspondingly)
* @param {int} k0..k7 - key vector components
*/
function set_key ( ks, k0, k1, k2, k3, k4, k5, k6, k7 ) {
var ekeys = heap.subarray( 0x000, 60 ),
dkeys = heap.subarray( 0x100, 0x100+60 );
// Encryption key schedule
ekeys.set( [ k0, k1, k2, k3, k4, k5, k6, k7 ] );
for ( var i = ks, rcon = 1; i < 4*ks+28; i++ ) {
var k = ekeys[i-1];
if ( ( i % ks === 0 ) || ( ks === 8 && i % ks === 4 ) ) {
k = aes_sbox[k>>>24]<<24 ^ aes_sbox[k>>>16&255]<<16 ^ aes_sbox[k>>>8&255]<<8 ^ aes_sbox[k&255];
}
if ( i % ks === 0 ) {
k = (k << 8) ^ (k >>> 24) ^ (rcon << 24);
rcon = (rcon << 1) ^ ( (rcon & 0x80) ? 0x1b : 0 );
}
ekeys[i] = ekeys[i-ks] ^ k;
}
// Decryption key schedule
for ( var j = 0; j < i; j += 4 ) {
for ( var jj = 0; jj < 4; jj++ ) {
var k = ekeys[i-(4+j)+(4-jj)%4];
if ( j < 4 || j >= i-4 ) {
dkeys[j+jj] = k;
} else {
dkeys[j+jj] = aes_dec[0][aes_sbox[k>>>24]]
^ aes_dec[1][aes_sbox[k>>>16&255]]
^ aes_dec[2][aes_sbox[k>>>8&255]]
^ aes_dec[3][aes_sbox[k&255]];
}
}
}
// Set rounds number
asm.set_rounds( ks + 5 );
}
var asm = function ( stdlib, foreign, buffer ) {
"use asm";
var S0 = 0, S1 = 0, S2 = 0, S3 = 0,
I0 = 0, I1 = 0, I2 = 0, I3 = 0,
N0 = 0, N1 = 0, N2 = 0, N3 = 0,
M0 = 0, M1 = 0, M2 = 0, M3 = 0,
H0 = 0, H1 = 0, H2 = 0, H3 = 0,
R = 0;
var HEAP = new stdlib.Uint32Array(buffer),
DATA = new stdlib.Uint8Array(buffer);
/**
* AES core
* @param {int} k - precomputed key schedule offset
* @param {int} s - precomputed sbox table offset
* @param {int} t - precomputed round table offset
* @param {int} r - number of inner rounds to perform
* @param {int} x0..x3 - 128-bit input block vector
*/
function _core ( k, s, t, r, x0, x1, x2, x3 ) {
k = k|0;
s = s|0;
t = t|0;
r = r|0;
x0 = x0|0;
x1 = x1|0;
x2 = x2|0;
x3 = x3|0;
var t1 = 0, t2 = 0, t3 = 0,
y0 = 0, y1 = 0, y2 = 0, y3 = 0,
i = 0;
t1 = t|0x400, t2 = t|0x800, t3 = t|0xc00;
// round 0
x0 = x0 ^ HEAP[(k|0)>>2],
x1 = x1 ^ HEAP[(k|4)>>2],
x2 = x2 ^ HEAP[(k|8)>>2],
x3 = x3 ^ HEAP[(k|12)>>2];
// round 1..r
for ( i = 16; (i|0) <= (r<<4); i = (i+16)|0 ) {
y0 = HEAP[(t|x0>>22&1020)>>2] ^ HEAP[(t1|x1>>14&1020)>>2] ^ HEAP[(t2|x2>>6&1020)>>2] ^ HEAP[(t3|x3<<2&1020)>>2] ^ HEAP[(k|i|0)>>2],
y1 = HEAP[(t|x1>>22&1020)>>2] ^ HEAP[(t1|x2>>14&1020)>>2] ^ HEAP[(t2|x3>>6&1020)>>2] ^ HEAP[(t3|x0<<2&1020)>>2] ^ HEAP[(k|i|4)>>2],
y2 = HEAP[(t|x2>>22&1020)>>2] ^ HEAP[(t1|x3>>14&1020)>>2] ^ HEAP[(t2|x0>>6&1020)>>2] ^ HEAP[(t3|x1<<2&1020)>>2] ^ HEAP[(k|i|8)>>2],
y3 = HEAP[(t|x3>>22&1020)>>2] ^ HEAP[(t1|x0>>14&1020)>>2] ^ HEAP[(t2|x1>>6&1020)>>2] ^ HEAP[(t3|x2<<2&1020)>>2] ^ HEAP[(k|i|12)>>2];
x0 = y0, x1 = y1, x2 = y2, x3 = y3;
}
// final round
S0 = HEAP[(s|x0>>22&1020)>>2]<<24 ^ HEAP[(s|x1>>14&1020)>>2]<<16 ^ HEAP[(s|x2>>6&1020)>>2]<<8 ^ HEAP[(s|x3<<2&1020)>>2] ^ HEAP[(k|i|0)>>2],
S1 = HEAP[(s|x1>>22&1020)>>2]<<24 ^ HEAP[(s|x2>>14&1020)>>2]<<16 ^ HEAP[(s|x3>>6&1020)>>2]<<8 ^ HEAP[(s|x0<<2&1020)>>2] ^ HEAP[(k|i|4)>>2],
S2 = HEAP[(s|x2>>22&1020)>>2]<<24 ^ HEAP[(s|x3>>14&1020)>>2]<<16 ^ HEAP[(s|x0>>6&1020)>>2]<<8 ^ HEAP[(s|x1<<2&1020)>>2] ^ HEAP[(k|i|8)>>2],
S3 = HEAP[(s|x3>>22&1020)>>2]<<24 ^ HEAP[(s|x0>>14&1020)>>2]<<16 ^ HEAP[(s|x1>>6&1020)>>2]<<8 ^ HEAP[(s|x2<<2&1020)>>2] ^ HEAP[(k|i|12)>>2];
}
/**
* ECB mode encryption
* @param {int} x0..x3 - 128-bit input block vector
*/
function _ecb_enc ( x0, x1, x2, x3 ) {
x0 = x0|0;
x1 = x1|0;
x2 = x2|0;
x3 = x3|0;
_core(
0x0000, 0x0800, 0x1000,
R,
x0,
x1,
x2,
x3
);
}
/**
* ECB mode decryption
* @param {int} x0..x3 - 128-bit input block vector
*/
function _ecb_dec ( x0, x1, x2, x3 ) {
x0 = x0|0;
x1 = x1|0;
x2 = x2|0;
x3 = x3|0;
var t = 0;
_core(
0x0400, 0x0c00, 0x2000,
R,
x0,
x3,
x2,
x1
);
t = S1, S1 = S3, S3 = t;
}
/**
* CBC mode encryption
* @param {int} x0..x3 - 128-bit input block vector
*/
function _cbc_enc ( x0, x1, x2, x3 ) {
x0 = x0|0;
x1 = x1|0;
x2 = x2|0;
x3 = x3|0;
_core(
0x0000, 0x0800, 0x1000,
R,
I0 ^ x0,
I1 ^ x1,
I2 ^ x2,
I3 ^ x3
);
I0 = S0,
I1 = S1,
I2 = S2,
I3 = S3;
}
/**
* CBC mode decryption
* @param {int} x0..x3 - 128-bit input block vector
*/
function _cbc_dec ( x0, x1, x2, x3 ) {
x0 = x0|0;
x1 = x1|0;
x2 = x2|0;
x3 = x3|0;
var t = 0;
_core(
0x0400, 0x0c00, 0x2000,
R,
x0,
x3,
x2,
x1
);
t = S1, S1 = S3, S3 = t;
S0 = S0 ^ I0,
S1 = S1 ^ I1,
S2 = S2 ^ I2,
S3 = S3 ^ I3;
I0 = x0,
I1 = x1,
I2 = x2,
I3 = x3;
}
/**
* CFB mode encryption
* @param {int} x0..x3 - 128-bit input block vector
*/
function _cfb_enc ( x0, x1, x2, x3 ) {
x0 = x0|0;
x1 = x1|0;
x2 = x2|0;
x3 = x3|0;
_core(
0x0000, 0x0800, 0x1000,
R,
I0,
I1,
I2,
I3
);
I0 = S0 = S0 ^ x0,
I1 = S1 = S1 ^ x1,
I2 = S2 = S2 ^ x2,
I3 = S3 = S3 ^ x3;
}
/**
* CFB mode decryption
* @param {int} x0..x3 - 128-bit input block vector
*/
function _cfb_dec ( x0, x1, x2, x3 ) {
x0 = x0|0;
x1 = x1|0;
x2 = x2|0;
x3 = x3|0;
_core(
0x0000, 0x0800, 0x1000,
R,
I0,
I1,
I2,
I3
);
S0 = S0 ^ x0,
S1 = S1 ^ x1,
S2 = S2 ^ x2,
S3 = S3 ^ x3;
I0 = x0,
I1 = x1,
I2 = x2,
I3 = x3;
}
/**
* OFB mode encryption / decryption
* @param {int} x0..x3 - 128-bit input block vector
*/
function _ofb ( x0, x1, x2, x3 ) {
x0 = x0|0;
x1 = x1|0;
x2 = x2|0;
x3 = x3|0;
_core(
0x0000, 0x0800, 0x1000,
R,
I0,
I1,
I2,
I3
);
I0 = S0,
I1 = S1,
I2 = S2,
I3 = S3;
S0 = S0 ^ x0,
S1 = S1 ^ x1,
S2 = S2 ^ x2,
S3 = S3 ^ x3;
}
/**
* CTR mode encryption / decryption
* @param {int} x0..x3 - 128-bit input block vector
*/
function _ctr ( x0, x1, x2, x3 ) {
x0 = x0|0;
x1 = x1|0;
x2 = x2|0;
x3 = x3|0;
_core(
0x0000, 0x0800, 0x1000,
R,
N0,
N1,
N2,
N3
);
N3 = ( ~M3 & N3 ) | M3 & ( N3 + 1 ),
N2 = ( ~M2 & N2 ) | M2 & ( N2 + ( (N3|0) == 0 ) ),
N1 = ( ~M1 & N1 ) | M1 & ( N1 + ( (N2|0) == 0 ) ),
N0 = ( ~M0 & N0 ) | M0 & ( N0 + ( (N1|0) == 0 ) );
S0 = S0 ^ x0,
S1 = S1 ^ x1,
S2 = S2 ^ x2,
S3 = S3 ^ x3;
}
/**
* GCM mode MAC calculation
* @param {int} x0..x3 - 128-bit input block vector
*/
function _gcm_mac ( x0, x1, x2, x3 ) {
x0 = x0|0;
x1 = x1|0;
x2 = x2|0;
x3 = x3|0;
var y0 = 0, y1 = 0, y2 = 0, y3 = 0,
z0 = 0, z1 = 0, z2 = 0, z3 = 0,
i = 0, c = 0;
x0 = x0 ^ I0,
x1 = x1 ^ I1,
x2 = x2 ^ I2,
x3 = x3 ^ I3;
y0 = H0|0,
y1 = H1|0,
y2 = H2|0,
y3 = H3|0;
for ( ; (i|0) < 128; i = (i + 1)|0 ) {
if ( y0 >>> 31 ) {
z0 = z0 ^ x0,
z1 = z1 ^ x1,
z2 = z2 ^ x2,
z3 = z3 ^ x3;
}
y0 = (y0 << 1) | (y1 >>> 31),
y1 = (y1 << 1) | (y2 >>> 31),
y2 = (y2 << 1) | (y3 >>> 31),
y3 = (y3 << 1);
c = x3 & 1;
x3 = (x3 >>> 1) | (x2 << 31),
x2 = (x2 >>> 1) | (x1 << 31),
x1 = (x1 >>> 1) | (x0 << 31),
x0 = (x0 >>> 1);
if ( c ) x0 = x0 ^ 0xe1000000;
}
I0 = z0,
I1 = z1,
I2 = z2,
I3 = z3;
}
/**
* Set the internal rounds number.
* @instance
* @memberof AES_asm
* @param {int} r - number if inner AES rounds
*/
function set_rounds ( r ) {
r = r|0;
R = r;
}
/**
* Populate the internal state of the module.
* @instance
* @memberof AES_asm
* @param {int} s0...s3 - state vector
*/
function set_state ( s0, s1, s2, s3 ) {
s0 = s0|0;
s1 = s1|0;
s2 = s2|0;
s3 = s3|0;
S0 = s0,
S1 = s1,
S2 = s2,
S3 = s3;
}
/**
* Populate the internal iv of the module.
* @instance
* @memberof AES_asm
* @param {int} i0...i3 - iv vector
*/
function set_iv ( i0, i1, i2, i3 ) {
i0 = i0|0;
i1 = i1|0;
i2 = i2|0;
i3 = i3|0;
I0 = i0,
I1 = i1,
I2 = i2,
I3 = i3;
}
/**
* Set nonce for CTR-family modes.
* @instance
* @memberof AES_asm
* @param {int} n0..n3 - nonce vector
*/
function set_nonce ( n0, n1, n2, n3 ) {
n0 = n0|0;
n1 = n1|0;
n2 = n2|0;
n3 = n3|0;
N0 = n0,
N1 = n1,
N2 = n2,
N3 = n3;
}
/**
* Set counter mask for CTR-family modes.
* @instance
* @memberof AES_asm
* @param {int} m0...m3 - counter mask vector
*/
function set_mask ( m0, m1, m2, m3 ) {
m0 = m0|0;
m1 = m1|0;
m2 = m2|0;
m3 = m3|0;
M0 = m0,
M1 = m1,
M2 = m2,
M3 = m3;
}
/**
* Set counter for CTR-family modes.
* @instance
* @memberof AES_asm
* @param {int} c0...c3 - counter vector
*/
function set_counter ( c0, c1, c2, c3 ) {
c0 = c0|0;
c1 = c1|0;
c2 = c2|0;
c3 = c3|0;
N3 = ( ~M3 & N3 ) | M3 & c3,
N2 = ( ~M2 & N2 ) | M2 & c2,
N1 = ( ~M1 & N1 ) | M1 & c1,
N0 = ( ~M0 & N0 ) | M0 & c0;
}
/**
* Store the internal state vector into the heap.
* @instance
* @memberof AES_asm
* @param {int} pos - offset where to put the data
* @return {int} The number of bytes have been written into the heap, always 16.
*/
function get_state ( pos ) {
pos = pos|0;
if ( pos & 15 ) return -1;
DATA[pos|0] = S0>>>24,
DATA[pos|1] = S0>>>16&255,
DATA[pos|2] = S0>>>8&255,
DATA[pos|3] = S0&255,
DATA[pos|4] = S1>>>24,
DATA[pos|5] = S1>>>16&255,
DATA[pos|6] = S1>>>8&255,
DATA[pos|7] = S1&255,
DATA[pos|8] = S2>>>24,
DATA[pos|9] = S2>>>16&255,
DATA[pos|10] = S2>>>8&255,
DATA[pos|11] = S2&255,
DATA[pos|12] = S3>>>24,
DATA[pos|13] = S3>>>16&255,
DATA[pos|14] = S3>>>8&255,
DATA[pos|15] = S3&255;
return 16;
}
/**
* Store the internal iv vector into the heap.
* @instance
* @memberof AES_asm
* @param {int} pos - offset where to put the data
* @return {int} The number of bytes have been written into the heap, always 16.
*/
function get_iv ( pos ) {
pos = pos|0;
if ( pos & 15 ) return -1;
DATA[pos|0] = I0>>>24,
DATA[pos|1] = I0>>>16&255,
DATA[pos|2] = I0>>>8&255,
DATA[pos|3] = I0&255,
DATA[pos|4] = I1>>>24,
DATA[pos|5] = I1>>>16&255,
DATA[pos|6] = I1>>>8&255,
DATA[pos|7] = I1&255,
DATA[pos|8] = I2>>>24,
DATA[pos|9] = I2>>>16&255,
DATA[pos|10] = I2>>>8&255,
DATA[pos|11] = I2&255,
DATA[pos|12] = I3>>>24,
DATA[pos|13] = I3>>>16&255,
DATA[pos|14] = I3>>>8&255,
DATA[pos|15] = I3&255;
return 16;
}
/**
* GCM initialization.
* @instance
* @memberof AES_asm
*/
function gcm_init ( ) {
_ecb_enc( 0, 0, 0, 0 );
H0 = S0,
H1 = S1,
H2 = S2,
H3 = S3;
}
/**
* Perform ciphering operation on the supplied data.
* @instance
* @memberof AES_asm
* @param {int} mode - block cipher mode (see {@link AES_asm} mode constants)
* @param {int} pos - offset of the data being processed
* @param {int} len - length of the data being processed
* @return {int} Actual amount of data have been processed.
*/
function cipher ( mode, pos, len ) {
mode = mode|0;
pos = pos|0;
len = len|0;
var ret = 0;
if ( pos & 15 ) return -1;
while ( (len|0) >= 16 ) {
_cipher_modes[mode&7](
DATA[pos|0]<<24 | DATA[pos|1]<<16 | DATA[pos|2]<<8 | DATA[pos|3],
DATA[pos|4]<<24 | DATA[pos|5]<<16 | DATA[pos|6]<<8 | DATA[pos|7],
DATA[pos|8]<<24 | DATA[pos|9]<<16 | DATA[pos|10]<<8 | DATA[pos|11],
DATA[pos|12]<<24 | DATA[pos|13]<<16 | DATA[pos|14]<<8 | DATA[pos|15]
);
DATA[pos|0] = S0>>>24,
DATA[pos|1] = S0>>>16&255,
DATA[pos|2] = S0>>>8&255,
DATA[pos|3] = S0&255,
DATA[pos|4] = S1>>>24,
DATA[pos|5] = S1>>>16&255,
DATA[pos|6] = S1>>>8&255,
DATA[pos|7] = S1&255,
DATA[pos|8] = S2>>>24,
DATA[pos|9] = S2>>>16&255,
DATA[pos|10] = S2>>>8&255,
DATA[pos|11] = S2&255,
DATA[pos|12] = S3>>>24,
DATA[pos|13] = S3>>>16&255,
DATA[pos|14] = S3>>>8&255,
DATA[pos|15] = S3&255;
ret = (ret + 16)|0,
pos = (pos + 16)|0,
len = (len - 16)|0;
}
return ret|0;
}
/**
* Calculates MAC of the supplied data.
* @instance
* @memberof AES_asm
* @param {int} mode - block cipher mode (see {@link AES_asm} mode constants)
* @param {int} pos - offset of the data being processed
* @param {int} len - length of the data being processed
* @return {int} Actual amount of data have been processed.
*/
function mac ( mode, pos, len ) {
mode = mode|0;
pos = pos|0;
len = len|0;
var ret = 0;
if ( pos & 15 ) return -1;
while ( (len|0) >= 16 ) {
_mac_modes[mode&1](
DATA[pos|0]<<24 | DATA[pos|1]<<16 | DATA[pos|2]<<8 | DATA[pos|3],
DATA[pos|4]<<24 | DATA[pos|5]<<16 | DATA[pos|6]<<8 | DATA[pos|7],
DATA[pos|8]<<24 | DATA[pos|9]<<16 | DATA[pos|10]<<8 | DATA[pos|11],
DATA[pos|12]<<24 | DATA[pos|13]<<16 | DATA[pos|14]<<8 | DATA[pos|15]
);
ret = (ret + 16)|0,
pos = (pos + 16)|0,
len = (len - 16)|0;
}