-
Notifications
You must be signed in to change notification settings - Fork 54
/
gostCipher.js
2273 lines (2067 loc) · 83.5 KB
/
gostCipher.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
/**
* @file GOST 28147-89/GOST R 34.12-2015/GOST R 32.13-2015 Encryption Algorithm
* @version 1.76
* @copyright 2014-2016, Rudolf Nickolaev. All rights reserved.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
(function (root, factory) {
/*
* Module imports and exports
*
*/ // <editor-fold defaultstate="collapsed">
if (typeof define === 'function' && define.amd) {
define(['gostRandom'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostRandom'));
} else {
root.GostCipher = factory(root.GostRandom);
}
// </editor-fold>
}(this, function (GostRandom) {
/*
* Initial parameters and common algortithms of GOST 28147-89
*
* http://tools.ietf.org/html/rfc5830
*
*/ // <editor-fold defaultstate="collapsed">
var root = this;
var rootCrypto = root.crypto || root.msCrypto;
var CryptoOperationData = root.ArrayBuffer;
var SyntaxError = root.SyntaxError || root.Error,
DataError = root.DataError || root.Error,
NotSupportedError = root.NotSupportedError || root.Error;
/*
* Check supported
* This implementation support only Little Endian arhitecture
*/
var littleEndian = (function () {
var buffer = new CryptoOperationData(2);
new DataView(buffer).setInt16(0, 256, true);
return new Int16Array(buffer)[0] === 256;
})();
// Default initial vector
var defaultIV = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]);
// Predefined sBox collection
var sBoxes = {
'E-TEST': [
0x4, 0x2, 0xF, 0x5, 0x9, 0x1, 0x0, 0x8, 0xE, 0x3, 0xB, 0xC, 0xD, 0x7, 0xA, 0x6,
0xC, 0x9, 0xF, 0xE, 0x8, 0x1, 0x3, 0xA, 0x2, 0x7, 0x4, 0xD, 0x6, 0x0, 0xB, 0x5,
0xD, 0x8, 0xE, 0xC, 0x7, 0x3, 0x9, 0xA, 0x1, 0x5, 0x2, 0x4, 0x6, 0xF, 0x0, 0xB,
0xE, 0x9, 0xB, 0x2, 0x5, 0xF, 0x7, 0x1, 0x0, 0xD, 0xC, 0x6, 0xA, 0x4, 0x3, 0x8,
0x3, 0xE, 0x5, 0x9, 0x6, 0x8, 0x0, 0xD, 0xA, 0xB, 0x7, 0xC, 0x2, 0x1, 0xF, 0x4,
0x8, 0xF, 0x6, 0xB, 0x1, 0x9, 0xC, 0x5, 0xD, 0x3, 0x7, 0xA, 0x0, 0xE, 0x2, 0x4,
0x9, 0xB, 0xC, 0x0, 0x3, 0x6, 0x7, 0x5, 0x4, 0x8, 0xE, 0xF, 0x1, 0xA, 0x2, 0xD,
0xC, 0x6, 0x5, 0x2, 0xB, 0x0, 0x9, 0xD, 0x3, 0xE, 0x7, 0xA, 0xF, 0x4, 0x1, 0x8
],
'E-A': [
0x9, 0x6, 0x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, 0xA, 0x9, 0x5, 0xB, 0x4, 0xF, 0x8, 0x6, 0x7, 0xE, 0xD, 0x0, 0x2, 0xC
],
'D-SC': [
0xb, 0xd, 0x7, 0x0, 0x5, 0x4, 0x1, 0xf, 0x9, 0xe, 0x6, 0xa, 0x3, 0xc, 0x8, 0x2,
0x1, 0x2, 0x7, 0x9, 0xd, 0xb, 0xf, 0x8, 0xe, 0xc, 0x4, 0x0, 0x5, 0x6, 0xa, 0x3,
0x5, 0x1, 0xd, 0x3, 0xf, 0x6, 0xc, 0x7, 0x9, 0x8, 0xb, 0x2, 0x4, 0xe, 0x0, 0xa,
0xd, 0x1, 0xb, 0x4, 0x9, 0xc, 0xe, 0x0, 0x7, 0x5, 0x8, 0xf, 0x6, 0x2, 0xa, 0x3,
0x2, 0xd, 0xa, 0xf, 0x9, 0xb, 0x3, 0x7, 0x8, 0xc, 0x5, 0xe, 0x6, 0x0, 0x1, 0x4,
0x0, 0x4, 0x6, 0xc, 0x5, 0x3, 0x8, 0xd, 0xa, 0xb, 0xf, 0x2, 0x1, 0x9, 0x7, 0xe,
0x1, 0x3, 0xc, 0x8, 0xa, 0x6, 0xb, 0x0, 0x2, 0xe, 0x7, 0x9, 0xf, 0x4, 0x5, 0xd,
0xa, 0xb, 0x6, 0x0, 0x1, 0x3, 0x4, 0x7, 0xe, 0xd, 0x5, 0xf, 0x8, 0x2, 0x9, 0xc
]
};
var C = new Uint8Array([
0x69, 0x00, 0x72, 0x22, 0x64, 0xC9, 0x04, 0x23,
0x8D, 0x3A, 0xDB, 0x96, 0x46, 0xE9, 0x2A, 0xC4,
0x18, 0xFE, 0xAC, 0x94, 0x00, 0xED, 0x07, 0x12,
0xC0, 0x86, 0xDC, 0xC2, 0xEF, 0x4C, 0xA9, 0x2B
]);
function signed(x) {
return x >= 0x80000000 ? x - 0x100000000 : x;
}
function unsigned(x) {
return x < 0 ? x + 0x100000000 : x;
}
// Set random values into Uint8Arry
// Random generator
function randomSeed(e) {
GostRandom = GostRandom || root.GostRandom;
var randomSource = GostRandom ? new (GostRandom || root.GostRandom) : rootCrypto;
if (randomSource.getRandomValues)
randomSource.getRandomValues(e);
else
throw new NotSupportedError('Random generator not found');
}
// Get buffer
function buffer(d) {
if (d instanceof CryptoOperationData)
return d;
else if (d && d.buffer && d.buffer instanceof CryptoOperationData)
return d.byteOffset === 0 && d.byteLength === d.buffer.byteLength ?
d.buffer : new Uint8Array(new Uint8Array(d, d.byteOffset, d.byteLength)).buffer;
else
throw new DataError('CryptoOperationData required');
}
// Get byte array
function byteArray(d) {
return new Uint8Array(buffer(d));
}
// Clone byte array
function cloneArray(d) {
return new Uint8Array(byteArray(d));
}
// Get int32 array
function intArray(d) {
return new Int32Array(buffer(d));
}
// Swap bytes for version 2015
function swap32(b) {
return ((b & 0xff) << 24)
| ((b & 0xff00) << 8)
| ((b >> 8) & 0xff00)
| ((b >> 24) & 0xff);
}
// </editor-fold>
/*
* Initial parameters and common algortithms of GOST R 34.12-15
* Algorithm "Kuznechik" 128bit
*
*/ // <editor-fold defaultstate="collapsed">
// Default initial vector
var defaultIV128 = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
// Mult table for R function
var multTable = (function () {
// Multiply two numbers in the GF(2^8) finite field defined
// by the polynomial x^8 + x^7 + x^6 + x + 1 = 0 */
function gmul(a, b) {
var p = 0, counter, carry;
for (counter = 0; counter < 8; counter++) {
if (b & 1)
p ^= a;
carry = a & 0x80; // detect if x^8 term is about to be generated
a = (a << 1) & 0xff;
if (carry)
a ^= 0xc3; // replace x^8 with x^7 + x^6 + x + 1
b >>= 1;
}
return p & 0xff;
}
// It is required only this values for R function
// 0 1 2 3 4 5 6 7
var x = [1, 16, 32, 133, 148, 192, 194, 251];
var m = [];
for (var i = 0; i < 8; i++) {
m[i] = [];
for (var j = 0; j < 256; j++)
m[i][j] = gmul(x[i], j);
}
return m;
})();
// 148, 32, 133, 16, 194, 192, 1, 251, 1, 192, 194, 16, 133, 32, 148, 1
var kB = [4, 2, 3, 1, 6, 5, 0, 7, 0, 5, 6, 1, 3, 2, 4, 0];
// R - function
function funcR(d) {
var sum = 0;
for (var i = 0; i < 16; i++)
sum ^= multTable[kB[i]][d[i]];
for (var i = 16; i > 0; --i)
d[i] = d[i - 1];
d[0] = sum;
}
function funcReverseR(d) {
var tmp = d[0];
for (var i = 0; i < 15; i++)
d[i] = d[i + 1];
d[15] = tmp;
var sum = 0;
for (i = 0; i < 16; i++)
sum ^= multTable[kB[i]][d[i]];
d[15] = sum;
}
// Nonlinear transformation
var kPi = [
252, 238, 221, 17, 207, 110, 49, 22, 251, 196, 250, 218, 35, 197, 4, 77,
233, 119, 240, 219, 147, 46, 153, 186, 23, 54, 241, 187, 20, 205, 95, 193,
249, 24, 101, 90, 226, 92, 239, 33, 129, 28, 60, 66, 139, 1, 142, 79,
5, 132, 2, 174, 227, 106, 143, 160, 6, 11, 237, 152, 127, 212, 211, 31,
235, 52, 44, 81, 234, 200, 72, 171, 242, 42, 104, 162, 253, 58, 206, 204,
181, 112, 14, 86, 8, 12, 118, 18, 191, 114, 19, 71, 156, 183, 93, 135,
21, 161, 150, 41, 16, 123, 154, 199, 243, 145, 120, 111, 157, 158, 178, 177,
50, 117, 25, 61, 255, 53, 138, 126, 109, 84, 198, 128, 195, 189, 13, 87,
223, 245, 36, 169, 62, 168, 67, 201, 215, 121, 214, 246, 124, 34, 185, 3,
224, 15, 236, 222, 122, 148, 176, 188, 220, 232, 40, 80, 78, 51, 10, 74,
167, 151, 96, 115, 30, 0, 98, 68, 26, 184, 56, 130, 100, 159, 38, 65,
173, 69, 70, 146, 39, 94, 85, 47, 140, 163, 165, 125, 105, 213, 149, 59,
7, 88, 179, 64, 134, 172, 29, 247, 48, 55, 107, 228, 136, 217, 231, 137,
225, 27, 131, 73, 76, 63, 248, 254, 141, 83, 170, 144, 202, 216, 133, 97,
32, 113, 103, 164, 45, 43, 9, 91, 203, 155, 37, 208, 190, 229, 108, 82,
89, 166, 116, 210, 230, 244, 180, 192, 209, 102, 175, 194, 57, 75, 99, 182
];
var kReversePi = (function () {
var m = [];
for (var i = 0, n = kPi.length; i < n; i++)
m[kPi[i]] = i;
return m;
})();
function funcS(d) {
for (var i = 0; i < 16; ++i)
d[i] = kPi[d[i]];
}
function funcReverseS(d) {
for (var i = 0; i < 16; ++i)
d[i] = kReversePi[d[i]];
}
function funcX(a, b) {
for (var i = 0; i < 16; ++i)
a[i] ^= b[i];
}
function funcL(d) {
for (var i = 0; i < 16; ++i)
funcR(d);
}
function funcReverseL(d) {
for (var i = 0; i < 16; ++i)
funcReverseR(d);
}
function funcLSX(a, b) {
funcX(a, b);
funcS(a);
funcL(a);
}
function funcReverseLSX(a, b) {
funcX(a, b);
funcReverseL(a);
funcReverseS(a);
}
function funcF(inputKey, inputKeySecond, iterationConst) {
var tmp = new Uint8Array(inputKey);
funcLSX(inputKey, iterationConst);
funcX(inputKey, inputKeySecond);
inputKeySecond.set(tmp);
}
function funcC(number, d) {
for (var i = 0; i < 15; i++)
d[i] = 0;
d[15] = number;
funcL(d);
}
// </editor-fold>
/**
* Key schedule for GOST R 34.12-15 128bits
*
* @memberOf GostCipher
* @private
* @instance
* @method keySchedule
* @param {type} k
* @returns {Uint8Array}
*/
function keySchedule128(k) // <editor-fold defaultstate="collapsed">
{
var keys = new Uint8Array(160), c = new Uint8Array(16);
keys.set(byteArray(k));
for (var j = 0; j < 4; j++) {
var j0 = 32 * j, j1 = 32 * (j + 1);
keys.set(new Uint8Array(keys.buffer, j0, 32), j1);
for (var i = 1; i < 9; i++) {
funcC(j * 8 + i, c);
funcF(new Uint8Array(keys.buffer, j1, 16),
new Uint8Array(keys.buffer, j1 + 16, 16), c);
}
}
return keys;
} // </editor-fold>
/**
* GOST R 34.12-15 128 bits encrypt/decrypt process
*
* @memberOf GostCipher
* @private
* @instance
* @method round
* @param {Uint8Array} k Scheduled key
* @param {Uint8Array} d Data
* @param {number} ofs Offsec
* @param {number} e true - decrypt
*/
function process128(k, d, ofs, e) // <editor-fold defaultstate="collapsed">
{
ofs = ofs || d.byteOffset;
var r = new Uint8Array(d.buffer, ofs, 16);
if (e) {
for (var i = 0; i < 9; i++)
funcReverseLSX(r, new Uint8Array(k.buffer, (9 - i) * 16, 16));
funcX(r, new Uint8Array(k.buffer, 0, 16));
} else {
for (var i = 0; i < 9; i++)
funcLSX(r, new Uint8Array(k.buffer, 16 * i, 16));
funcX(r, new Uint8Array(k.buffer, 16 * 9, 16));
}
} // </editor-fold>
/**
* One GOST encryption round
*
* @memberOf GostCipher
* @private
* @instance
* @method round
* @param {Int8Array} S sBox
* @param {Int32Array} m 2x32 bits cipher block
* @param {Int32Array} k 32 bits key[i]
*/
function round(S, m, k) // <editor-fold defaultstate="collapsed">
{
var cm = (m[0] + k) & 0xffffffff;
var om = S[ 0 + ((cm >> (0 * 4)) & 0xF)] << (0 * 4);
om |= S[ 16 + ((cm >> (1 * 4)) & 0xF)] << (1 * 4);
om |= S[ 32 + ((cm >> (2 * 4)) & 0xF)] << (2 * 4);
om |= S[ 48 + ((cm >> (3 * 4)) & 0xF)] << (3 * 4);
om |= S[ 64 + ((cm >> (4 * 4)) & 0xF)] << (4 * 4);
om |= S[ 80 + ((cm >> (5 * 4)) & 0xF)] << (5 * 4);
om |= S[ 96 + ((cm >> (6 * 4)) & 0xF)] << (6 * 4);
om |= S[112 + ((cm >> (7 * 4)) & 0xF)] << (7 * 4);
cm = om << 11 | om >>> (32 - 11);
cm ^= m[1];
m[1] = m[0];
m[0] = cm;
} // </editor-fold>
/**
* Process encrypt/decrypt block with key K using GOST 28147-89
*
* @memberOf GostCipher
* @private
* @instance
* @method process
* @param k {Int32Array} 8x32 bits key
* @param d {Int32Array} 8x8 bits cipher block
* @param ofs {number} offset
*/
function process89(k, d, ofs) // <editor-fold defaultstate="collapsed">
{
ofs = ofs || d.byteOffset;
var s = this.sBox,
m = new Int32Array(d.buffer, ofs, 2);
for (var i = 0; i < 32; i++)
round(s, m, k[i]);
var r = m[0];
m[0] = m[1];
m[1] = r;
} // </editor-fold>
/**
* Process encrypt/decrypt block with key K using GOST R 34.12-15 64bit block
*
* @memberOf GostCipher
* @private
* @instance
* @method process
* @param k {Int32Array} 8x32 bits key
* @param d {Int32Array} 8x8 bits cipher block
* @param ofs {number} offset
*/
function process15(k, d, ofs) // <editor-fold defaultstate="collapsed">
{
ofs = ofs || d.byteOffset;
var s = this.sBox,
m = new Int32Array(d.buffer, ofs, 2),
r = swap32(m[0]);
m[0] = swap32(m[1]);
m[1] = r;
for (var i = 0; i < 32; i++)
round(s, m, k[i]);
m[0] = swap32(m[0]);
m[1] = swap32(m[1]);
} // </editor-fold>
/**
* Key keySchedule algorithm for GOST 28147-89 64bit cipher
*
* @memberOf GostCipher
* @private
* @instance
* @method process
* @param k {Uint8Array} 8 bit key array
* @param e {boolean} true - decrypt
* @returns {Int32Array} keyScheduled 32-bit key
*/
function keySchedule89(k, e) // <editor-fold defaultstate="collapsed">
{
var sch = new Int32Array(32),
key = new Int32Array(buffer(k));
for (var i = 0; i < 8; i++)
sch[i] = key[i];
if (e) {
for (var i = 0; i < 8; i++)
sch[i + 8] = sch[7 - i];
for (var i = 0; i < 8; i++)
sch[i + 16] = sch[7 - i];
} else {
for (var i = 0; i < 8; i++)
sch[i + 8] = sch[i];
for (var i = 0; i < 8; i++)
sch[i + 16] = sch[i];
}
for (var i = 0; i < 8; i++)
sch[i + 24] = sch[7 - i];
return sch;
} // </editor-fold>
/**
* Key keySchedule algorithm for GOST R 34.12-15 64bit cipher
*
* @memberOf GostCipher
* @private
* @instance
* @method process
* @param k {Uint8Array} 8 bit key array
* @param e {boolean} true - decrypt
* @returns {Int32Array} keyScheduled 32-bit key
*/
function keySchedule15(k, e) // <editor-fold defaultstate="collapsed">
{
var sch = new Int32Array(32),
key = new Int32Array(buffer(k));
for (var i = 0; i < 8; i++)
sch[i] = swap32(key[i]);
if (e) {
for (var i = 0; i < 8; i++)
sch[i + 8] = sch[7 - i];
for (var i = 0; i < 8; i++)
sch[i + 16] = sch[7 - i];
} else {
for (var i = 0; i < 8; i++)
sch[i + 8] = sch[i];
for (var i = 0; i < 8; i++)
sch[i + 16] = sch[i];
}
for (var i = 0; i < 8; i++)
sch[i + 24] = sch[7 - i];
return sch;
} // </editor-fold>
/**
* Key schedule for RC2
*
* https://tools.ietf.org/html/rfc2268
*
* @memberOf GostCipher
* @private
* @instance
* @method keySchedule
* @param {Uint8Array} k
* @returns {Uint16Array}
*/
var keyScheduleRC2 = (function () // <editor-fold defaultstate="collapsed">
{
// an array of "random" bytes based on the digits of PI = 3.14159...
var PITABLE = new Uint8Array([
0xd9, 0x78, 0xf9, 0xc4, 0x19, 0xdd, 0xb5, 0xed, 0x28, 0xe9, 0xfd, 0x79, 0x4a, 0xa0, 0xd8, 0x9d,
0xc6, 0x7e, 0x37, 0x83, 0x2b, 0x76, 0x53, 0x8e, 0x62, 0x4c, 0x64, 0x88, 0x44, 0x8b, 0xfb, 0xa2,
0x17, 0x9a, 0x59, 0xf5, 0x87, 0xb3, 0x4f, 0x13, 0x61, 0x45, 0x6d, 0x8d, 0x09, 0x81, 0x7d, 0x32,
0xbd, 0x8f, 0x40, 0xeb, 0x86, 0xb7, 0x7b, 0x0b, 0xf0, 0x95, 0x21, 0x22, 0x5c, 0x6b, 0x4e, 0x82,
0x54, 0xd6, 0x65, 0x93, 0xce, 0x60, 0xb2, 0x1c, 0x73, 0x56, 0xc0, 0x14, 0xa7, 0x8c, 0xf1, 0xdc,
0x12, 0x75, 0xca, 0x1f, 0x3b, 0xbe, 0xe4, 0xd1, 0x42, 0x3d, 0xd4, 0x30, 0xa3, 0x3c, 0xb6, 0x26,
0x6f, 0xbf, 0x0e, 0xda, 0x46, 0x69, 0x07, 0x57, 0x27, 0xf2, 0x1d, 0x9b, 0xbc, 0x94, 0x43, 0x03,
0xf8, 0x11, 0xc7, 0xf6, 0x90, 0xef, 0x3e, 0xe7, 0x06, 0xc3, 0xd5, 0x2f, 0xc8, 0x66, 0x1e, 0xd7,
0x08, 0xe8, 0xea, 0xde, 0x80, 0x52, 0xee, 0xf7, 0x84, 0xaa, 0x72, 0xac, 0x35, 0x4d, 0x6a, 0x2a,
0x96, 0x1a, 0xd2, 0x71, 0x5a, 0x15, 0x49, 0x74, 0x4b, 0x9f, 0xd0, 0x5e, 0x04, 0x18, 0xa4, 0xec,
0xc2, 0xe0, 0x41, 0x6e, 0x0f, 0x51, 0xcb, 0xcc, 0x24, 0x91, 0xaf, 0x50, 0xa1, 0xf4, 0x70, 0x39,
0x99, 0x7c, 0x3a, 0x85, 0x23, 0xb8, 0xb4, 0x7a, 0xfc, 0x02, 0x36, 0x5b, 0x25, 0x55, 0x97, 0x31,
0x2d, 0x5d, 0xfa, 0x98, 0xe3, 0x8a, 0x92, 0xae, 0x05, 0xdf, 0x29, 0x10, 0x67, 0x6c, 0xba, 0xc9,
0xd3, 0x00, 0xe6, 0xcf, 0xe1, 0x9e, 0xa8, 0x2c, 0x63, 0x16, 0x01, 0x3f, 0x58, 0xe2, 0x89, 0xa9,
0x0d, 0x38, 0x34, 0x1b, 0xab, 0x33, 0xff, 0xb0, 0xbb, 0x48, 0x0c, 0x5f, 0xb9, 0xb1, 0xcd, 0x2e,
0xc5, 0xf3, 0xdb, 0x47, 0xe5, 0xa5, 0x9c, 0x77, 0x0a, 0xa6, 0x20, 0x68, 0xfe, 0x7f, 0xc1, 0xad
]);
return function (k)
{
var key = new Uint8Array(buffer(k)),
T = Math.min(key.length, 128),
T1 = this.effectiveLength,
T8 = Math.floor((T1 + 7) / 8),
TM = 0xff % Math.pow(2, 8 + T1 - 8 * T8);
var L = new Uint8Array(128), K = new Uint16Array(L.buffer);
for (var i = 0; i < T; i++)
L[i] = key[i];
for (var i = T; i < 128; i++)
L[i] = PITABLE[(L[i - 1] + L[i - T]) % 256];
L[128 - T8] = PITABLE[L[128 - T8] & TM];
for (var i = 127 - T8; i >= 0; --i)
L[i] = PITABLE[L[i + 1] ^ L[i + T8]];
return K;
};
} // </editor-fold>
)();
/**
* RC2 encrypt/decrypt process
*
* https://tools.ietf.org/html/rfc2268
*
* @memberOf GostCipher
* @private
* @instance
* @method round
* @param {CryptoOperationData} k Scheduled key
* @param {CryptoOperationData} d Data
* @param {number} ofs Offsec
* @param {number} e true - decrypt
*/
var processRC2 = (function () // <editor-fold defaultstate="collapsed">
{
var K, j, R = new Uint16Array(4),
s = new Uint16Array([1, 2, 3, 5]), reverse;
function rol(R, s) {
return (R << s | R >>> (16 - s)) & 0xffff;
}
function ror(R, s) {
return (R >>> s | R << (16 - s)) & 0xffff;
}
function mix(i) {
if (reverse) {
R[i] = ror(R[i], s[i]);
R[i] = R[i] - K[j] - (R[(i + 3) % 4] & R[(i + 2) % 4]) - ((~R[(i + 3) % 4]) & R[(i + 1) % 4]);
j = j - 1;
} else {
R[i] = R[i] + K[j] + (R[(i + 3) % 4] & R[(i + 2) % 4]) + ((~R[(i + 3) % 4]) & R[(i + 1) % 4]);
j = j + 1;
R[i] = rol(R[i], s[i]);
}
}
function mash(i) {
if (reverse) {
R[i] = R[i] - K[R[(i + 3) % 4] & 63];
} else {
R[i] = R[i] + K[R[(i + 3) % 4] & 63];
}
}
function perform(method, count) {
count = count || 1;
for (var j = 0; j < count; j++) {
if (reverse) {
for (var i = 3; i >= 0; --i)
method(i);
} else {
for (var i = 0; i < 4; i++)
method(i);
}
}
}
return function (k, d, ofs, e) {
reverse = e;
// 1. Initialize words R[0], ..., R[3] to contain the 64-bit
// ciphertext value.
R = new Uint16Array(d.buffer, ofs || d.byteOffset, 4);
// 2. Expand the key, so that words K[0], ..., K[63] become
// defined.
K = k;
// 3. Initialize j to zero (enc) j to 63 (dec).
j = e ? 63 : 0;
// 4. Perform five mixing rounds.
perform(mix, 5);
// 5. Perform one mashing round.
perform(mash);
// 6. Perform six mixing rounds.
perform(mix, 6);
// 7. Perform one mashing round.
perform(mash);
// 8. Perform five mixing rounds.
perform(mix, 5);
};
} // </editor-fold>
)();
/**
* Algorithm name GOST 28147-ECB<br><br>
*
* encryptECB (K, D) is D, encrypted with key k using GOST 28147/GOST R 34.13 in
* "prostaya zamena" (Electronic Codebook, ECB) mode.
* @memberOf GostCipher
* @method encrypt
* @instance
* @param k {CryptoOperationData} 8x32 bit key
* @param d {CryptoOperationData} 8 bits message
* @return {CryptoOperationData} result
*/
function encryptECB(k, d) // <editor-fold defaultstate="collapsed">
{
var p = this.pad(byteArray(d)),
n = this.blockSize,
b = p.byteLength / n,
key = this.keySchedule(k);
for (var i = 0; i < b; i++)
this.process(key, p, n * i);
return p.buffer;
} // </editor-fold>
/**
* Algorithm name GOST 28147-ECB<br><br>
*
* decryptECB (K, D) is D, decrypted with key K using GOST 28147/GOST R 34.13 in
* "prostaya zamena" (Electronic Codebook, ECB) mode.
*
* @memberOf GostCipher
* @method decrypt
* @instance
* @param k {CryptoOperationData} 8x32 bits key
* @param d {CryptoOperationData} 8 bits message
* @return {CryptoOperationData} result
*/
function decryptECB(k, d) // <editor-fold defaultstate="collapsed">
{
var p = cloneArray(d),
n = this.blockSize,
b = p.byteLength / n,
key = this.keySchedule(k, 1);
for (var i = 0; i < b; i++)
this.process(key, p, n * i, 1);
return this.unpad(p).buffer;
} // </editor-fold>
/**
* Algorithm name GOST 28147-CFB<br><br>
*
* encryptCFB (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
* in "gammirovanie s obratnoj svyaziyu" (Cipher Feedback, CFB) mode, and IV is
* used as the initialization vector.
*
* @memberOf GostCipher
* @method encrypt
* @instance
* @param {CryptoOperationData} k 8x32 bits key
* @param {CryptoOperationData} d 8 bits array with data
* @param {CryptoOperationData} iv initial vector
* @return {CryptoOperationData} result
*/
function encryptCFB(k, d, iv) // <editor-fold defaultstate="collapsed">
{
var s = new Uint8Array(iv || this.iv),
c = cloneArray(d),
m = s.length,
t = new Uint8Array(m),
b = this.shiftBits >> 3,
cb = c.length, r = cb % b, q = (cb - r) / b,
key = this.keySchedule(k);
for (var i = 0; i < q; i++) {
for (var j = 0; j < m; j++)
t[j] = s[j];
this.process(key, s);
for (var j = 0; j < b; j++)
c[i * b + j] ^= s[j];
for (var j = 0; j < m - b; j++)
s[j] = t[b + j];
for (var j = 0; j < b; j++)
s[m - b + j] = c[i * b + j];
k = this.keyMeshing(k, s, i, key);
}
if (r > 0) {
this.process(key, s);
for (var i = 0; i < r; i++)
c[q * b + i] ^= s[i];
}
return c.buffer;
} // </editor-fold>
/**
* Algorithm name GOST 28147-CFB<br><br>
*
* decryptCFB (IV, K, D) is D, decrypted with key K using GOST 28147/GOST R 34.13
* in "gammirovanie s obratnoj svyaziyu po shifrotekstu" (Cipher Feedback, CFB) mode, and IV is
* used as the initialization vector.
*
* @memberOf GostCipher
* @method decrypt
* @instance
* @param {CryptoOperationData} k 8x32 bits key
* @param {CryptoOperationData} d 8 bits array with data
* @param {CryptoOperationData} iv initial vector
* @return {CryptoOperationData} result
*/
function decryptCFB(k, d, iv) // <editor-fold defaultstate="collapsed">
{
var s = new Uint8Array(iv || this.iv),
c = cloneArray(d),
m = s.length,
t = new Uint8Array(m),
b = this.shiftBits >> 3,
cb = c.length, r = cb % b, q = (cb - r) / b,
key = this.keySchedule(k);
for (var i = 0; i < q; i++) {
for (var j = 0; j < m; j++)
t[j] = s[j];
this.process(key, s);
for (var j = 0; j < b; j++) {
t[j] = c[i * b + j];
c[i * b + j] ^= s[j];
}
for (var j = 0; j < m - b; j++)
s[j] = t[b + j];
for (var j = 0; j < b; j++)
s[m - b + j] = t[j];
k = this.keyMeshing(k, s, i, key);
}
if (r > 0) {
this.process(key, s);
for (var i = 0; i < r; i++)
c[q * b + i] ^= s[i];
}
return c.buffer;
} // </editor-fold>
/**
* Algorithm name GOST 28147-OFB<br><br>
*
* encryptOFB/decryptOFB (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
* in "gammirovanie s obratnoj svyaziyu po vyhodu" (Output Feedback, OFB) mode, and IV is
* used as the initialization vector.
*
* @memberOf GostCipher
* @method encrypt
* @instance
* @param {CryptoOperationData} k 8x32 bits key
* @param {CryptoOperationData} d 8 bits array with data
* @param {CryptoOperationData} iv 8x8 optional bits initial vector
* @return {CryptoOperationData} result
*/
/**
* Algorithm name GOST 28147-OFB<br><br>
*
* encryptOFB/decryptOFB (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
* in "gammirovanie s obratnoj svyaziyu po vyhodu" (Output Feedback, OFB) mode, and IV is
* used as the initialization vector.
*
* @memberOf GostCipher
* @method decrypt
* @instance
* @param {CryptoOperationData} k 8x32 bits key
* @param {CryptoOperationData} d 8 bits array with data
* @param {CryptoOperationData} iv initial vector
* @return {CryptoOperationData} result
*/
function processOFB(k, d, iv) // <editor-fold defaultstate="collapsed">
{
var s = new Uint8Array(iv || this.iv),
c = cloneArray(d),
m = s.length,
t = new Uint8Array(m),
b = this.shiftBits >> 3,
p = new Uint8Array(b),
cb = c.length, r = cb % b, q = (cb - r) / b,
key = this.keySchedule(k);
for (var i = 0; i < q; i++) {
for (var j = 0; j < m; j++)
t[j] = s[j];
this.process(key, s);
for (var j = 0; j < b; j++)
p[j] = s[j];
for (var j = 0; j < b; j++)
c[i * b + j] ^= s[j];
for (var j = 0; j < m - b; j++)
s[j] = t[b + j];
for (var j = 0; j < b; j++)
s[m - b + j] = p[j];
k = this.keyMeshing(k, s, i, key);
}
if (r > 0) {
this.process(key, s);
for (var i = 0; i < r; i++)
c[q * b + i] ^= s[i];
}
return c.buffer;
} // </editor-fold>
/**
* Algorithm name GOST 28147-CTR<br><br>
*
* encryptCTR/decryptCTR (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
* in "gammirovanie" (Counter Mode-CTR) mode, and IV is used as the
* initialization vector.
* @memberOf GostCipher
* @method encrypt
* @instance
* @param {CryptoOperationData} k 8x32 bits key
* @param {CryptoOperationData} d 8 bits array with data
* @param {CryptoOperationData} iv 8x8 optional bits initial vector
* @return {CryptoOperationData} result
*/
/**
* Algorithm name GOST 28147-CTR<br><br>
*
* encryptCTR/decryptCTR (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
* in "gammirovanie" (Counter Mode-CTR) mode, and IV is used as the
* initialization vector.
* @memberOf GostCipher
* @method decrypt
* @instance
* @param {CryptoOperationData} k 8x32 bits key