UNPKG

6.71 kBJavaScriptView Raw
1;(function (root, factory, undef) {
2 if (typeof exports === "object") {
3 // CommonJS
4 module.exports = exports = factory(require("./core"), require("./enc-base64"), require("./md5"), require("./evpkdf"), require("./cipher-core"));
5 }
6 else if (typeof define === "function" && define.amd) {
7 // AMD
8 define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory);
9 }
10 else {
11 // Global (browser)
12 factory(root.CryptoJS);
13 }
14}(this, function (CryptoJS) {
15
16 (function () {
17 // Shortcuts
18 var C = CryptoJS;
19 var C_lib = C.lib;
20 var StreamCipher = C_lib.StreamCipher;
21 var C_algo = C.algo;
22
23 // Reusable objects
24 var S = [];
25 var C_ = [];
26 var G = [];
27
28 /**
29 * Rabbit stream cipher algorithm.
30 *
31 * This is a legacy version that neglected to convert the key to little-endian.
32 * This error doesn't affect the cipher's security,
33 * but it does affect its compatibility with other implementations.
34 */
35 var RabbitLegacy = C_algo.RabbitLegacy = StreamCipher.extend({
36 _doReset: function () {
37 // Shortcuts
38 var K = this._key.words;
39 var iv = this.cfg.iv;
40
41 // Generate initial state values
42 var X = this._X = [
43 K[0], (K[3] << 16) | (K[2] >>> 16),
44 K[1], (K[0] << 16) | (K[3] >>> 16),
45 K[2], (K[1] << 16) | (K[0] >>> 16),
46 K[3], (K[2] << 16) | (K[1] >>> 16)
47 ];
48
49 // Generate initial counter values
50 var C = this._C = [
51 (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff),
52 (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff),
53 (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff),
54 (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff)
55 ];
56
57 // Carry bit
58 this._b = 0;
59
60 // Iterate the system four times
61 for (var i = 0; i < 4; i++) {
62 nextState.call(this);
63 }
64
65 // Modify the counters
66 for (var i = 0; i < 8; i++) {
67 C[i] ^= X[(i + 4) & 7];
68 }
69
70 // IV setup
71 if (iv) {
72 // Shortcuts
73 var IV = iv.words;
74 var IV_0 = IV[0];
75 var IV_1 = IV[1];
76
77 // Generate four subvectors
78 var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00);
79 var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00);
80 var i1 = (i0 >>> 16) | (i2 & 0xffff0000);
81 var i3 = (i2 << 16) | (i0 & 0x0000ffff);
82
83 // Modify counter values
84 C[0] ^= i0;
85 C[1] ^= i1;
86 C[2] ^= i2;
87 C[3] ^= i3;
88 C[4] ^= i0;
89 C[5] ^= i1;
90 C[6] ^= i2;
91 C[7] ^= i3;
92
93 // Iterate the system four times
94 for (var i = 0; i < 4; i++) {
95 nextState.call(this);
96 }
97 }
98 },
99
100 _doProcessBlock: function (M, offset) {
101 // Shortcut
102 var X = this._X;
103
104 // Iterate the system
105 nextState.call(this);
106
107 // Generate four keystream words
108 S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16);
109 S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16);
110 S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16);
111 S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16);
112
113 for (var i = 0; i < 4; i++) {
114 // Swap endian
115 S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) |
116 (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00);
117
118 // Encrypt
119 M[offset + i] ^= S[i];
120 }
121 },
122
123 blockSize: 128/32,
124
125 ivSize: 64/32
126 });
127
128 function nextState() {
129 // Shortcuts
130 var X = this._X;
131 var C = this._C;
132
133 // Save old counter values
134 for (var i = 0; i < 8; i++) {
135 C_[i] = C[i];
136 }
137
138 // Calculate new counter values
139 C[0] = (C[0] + 0x4d34d34d + this._b) | 0;
140 C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0;
141 C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0;
142 C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0;
143 C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0;
144 C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0;
145 C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0;
146 C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0;
147 this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0;
148
149 // Calculate the g-values
150 for (var i = 0; i < 8; i++) {
151 var gx = X[i] + C[i];
152
153 // Construct high and low argument for squaring
154 var ga = gx & 0xffff;
155 var gb = gx >>> 16;
156
157 // Calculate high and low result of squaring
158 var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb;
159 var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0);
160
161 // High XOR low
162 G[i] = gh ^ gl;
163 }
164
165 // Calculate new state values
166 X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0;
167 X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0;
168 X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0;
169 X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0;
170 X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0;
171 X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0;
172 X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0;
173 X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0;
174 }
175
176 /**
177 * Shortcut functions to the cipher's object interface.
178 *
179 * @example
180 *
181 * var ciphertext = CryptoJS.RabbitLegacy.encrypt(message, key, cfg);
182 * var plaintext = CryptoJS.RabbitLegacy.decrypt(ciphertext, key, cfg);
183 */
184 C.RabbitLegacy = StreamCipher._createHelper(RabbitLegacy);
185 }());
186
187
188 return CryptoJS.RabbitLegacy;
189
190}));
\No newline at end of file