UNPKG

6.68 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 var Rabbit = C_algo.Rabbit = StreamCipher.extend({
32 _doReset: function () {
33 // Shortcuts
34 var K = this._key.words;
35 var iv = this.cfg.iv;
36
37 // Swap endian
38 for (var i = 0; i < 4; i++) {
39 K[i] = (((K[i] << 8) | (K[i] >>> 24)) & 0x00ff00ff) |
40 (((K[i] << 24) | (K[i] >>> 8)) & 0xff00ff00);
41 }
42
43 // Generate initial state values
44 var X = this._X = [
45 K[0], (K[3] << 16) | (K[2] >>> 16),
46 K[1], (K[0] << 16) | (K[3] >>> 16),
47 K[2], (K[1] << 16) | (K[0] >>> 16),
48 K[3], (K[2] << 16) | (K[1] >>> 16)
49 ];
50
51 // Generate initial counter values
52 var C = this._C = [
53 (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff),
54 (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff),
55 (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff),
56 (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff)
57 ];
58
59 // Carry bit
60 this._b = 0;
61
62 // Iterate the system four times
63 for (var i = 0; i < 4; i++) {
64 nextState.call(this);
65 }
66
67 // Modify the counters
68 for (var i = 0; i < 8; i++) {
69 C[i] ^= X[(i + 4) & 7];
70 }
71
72 // IV setup
73 if (iv) {
74 // Shortcuts
75 var IV = iv.words;
76 var IV_0 = IV[0];
77 var IV_1 = IV[1];
78
79 // Generate four subvectors
80 var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00);
81 var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00);
82 var i1 = (i0 >>> 16) | (i2 & 0xffff0000);
83 var i3 = (i2 << 16) | (i0 & 0x0000ffff);
84
85 // Modify counter values
86 C[0] ^= i0;
87 C[1] ^= i1;
88 C[2] ^= i2;
89 C[3] ^= i3;
90 C[4] ^= i0;
91 C[5] ^= i1;
92 C[6] ^= i2;
93 C[7] ^= i3;
94
95 // Iterate the system four times
96 for (var i = 0; i < 4; i++) {
97 nextState.call(this);
98 }
99 }
100 },
101
102 _doProcessBlock: function (M, offset) {
103 // Shortcut
104 var X = this._X;
105
106 // Iterate the system
107 nextState.call(this);
108
109 // Generate four keystream words
110 S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16);
111 S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16);
112 S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16);
113 S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16);
114
115 for (var i = 0; i < 4; i++) {
116 // Swap endian
117 S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) |
118 (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00);
119
120 // Encrypt
121 M[offset + i] ^= S[i];
122 }
123 },
124
125 blockSize: 128/32,
126
127 ivSize: 64/32
128 });
129
130 function nextState() {
131 // Shortcuts
132 var X = this._X;
133 var C = this._C;
134
135 // Save old counter values
136 for (var i = 0; i < 8; i++) {
137 C_[i] = C[i];
138 }
139
140 // Calculate new counter values
141 C[0] = (C[0] + 0x4d34d34d + this._b) | 0;
142 C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0;
143 C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0;
144 C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0;
145 C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0;
146 C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0;
147 C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0;
148 C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0;
149 this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0;
150
151 // Calculate the g-values
152 for (var i = 0; i < 8; i++) {
153 var gx = X[i] + C[i];
154
155 // Construct high and low argument for squaring
156 var ga = gx & 0xffff;
157 var gb = gx >>> 16;
158
159 // Calculate high and low result of squaring
160 var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb;
161 var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0);
162
163 // High XOR low
164 G[i] = gh ^ gl;
165 }
166
167 // Calculate new state values
168 X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0;
169 X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0;
170 X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0;
171 X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0;
172 X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0;
173 X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0;
174 X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0;
175 X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0;
176 }
177
178 /**
179 * Shortcut functions to the cipher's object interface.
180 *
181 * @example
182 *
183 * var ciphertext = CryptoJS.Rabbit.encrypt(message, key, cfg);
184 * var plaintext = CryptoJS.Rabbit.decrypt(ciphertext, key, cfg);
185 */
186 C.Rabbit = StreamCipher._createHelper(Rabbit);
187 }());
188
189
190 return CryptoJS.Rabbit;
191
192}));
\No newline at end of file