UNPKG

2.33 kBJavaScriptView Raw
1;(function (root, factory, undef) {
2 if (typeof exports === "object") {
3 // CommonJS
4 module.exports = exports = factory(require("./core"), require("./cipher-core"));
5 }
6 else if (typeof define === "function" && define.amd) {
7 // AMD
8 define(["./core", "./cipher-core"], factory);
9 }
10 else {
11 // Global (browser)
12 factory(root.CryptoJS);
13 }
14}(this, function (CryptoJS) {
15
16 /** @preserve
17 * Counter block mode compatible with Dr Brian Gladman fileenc.c
18 * derived from CryptoJS.mode.CTR
19 * Jan Hruby jhruby.web@gmail.com
20 */
21 CryptoJS.mode.CTRGladman = (function () {
22 var CTRGladman = CryptoJS.lib.BlockCipherMode.extend();
23
24 function incWord(word)
25 {
26 if (((word >> 24) & 0xff) === 0xff) { //overflow
27 var b1 = (word >> 16)&0xff;
28 var b2 = (word >> 8)&0xff;
29 var b3 = word & 0xff;
30
31 if (b1 === 0xff) // overflow b1
32 {
33 b1 = 0;
34 if (b2 === 0xff)
35 {
36 b2 = 0;
37 if (b3 === 0xff)
38 {
39 b3 = 0;
40 }
41 else
42 {
43 ++b3;
44 }
45 }
46 else
47 {
48 ++b2;
49 }
50 }
51 else
52 {
53 ++b1;
54 }
55
56 word = 0;
57 word += (b1 << 16);
58 word += (b2 << 8);
59 word += b3;
60 }
61 else
62 {
63 word += (0x01 << 24);
64 }
65 return word;
66 }
67
68 function incCounter(counter)
69 {
70 if ((counter[0] = incWord(counter[0])) === 0)
71 {
72 // encr_data in fileenc.c from Dr Brian Gladman's counts only with DWORD j < 8
73 counter[1] = incWord(counter[1]);
74 }
75 return counter;
76 }
77
78 var Encryptor = CTRGladman.Encryptor = CTRGladman.extend({
79 processBlock: function (words, offset) {
80 // Shortcuts
81 var cipher = this._cipher
82 var blockSize = cipher.blockSize;
83 var iv = this._iv;
84 var counter = this._counter;
85
86 // Generate keystream
87 if (iv) {
88 counter = this._counter = iv.slice(0);
89
90 // Remove IV for subsequent blocks
91 this._iv = undefined;
92 }
93
94 incCounter(counter);
95
96 var keystream = counter.slice(0);
97 cipher.encryptBlock(keystream, 0);
98
99 // Encrypt
100 for (var i = 0; i < blockSize; i++) {
101 words[offset + i] ^= keystream[i];
102 }
103 }
104 });
105
106 CTRGladman.Decryptor = Encryptor;
107
108 return CTRGladman;
109 }());
110
111
112
113
114 return CryptoJS.mode.CTRGladman;
115
116}));
\No newline at end of file