1 | /**
|
---|
2 | * RC2 implementation.
|
---|
3 | *
|
---|
4 | * @author Stefan Siegl
|
---|
5 | *
|
---|
6 | * Copyright (c) 2012 Stefan Siegl <stesie@brokenpipe.de>
|
---|
7 | *
|
---|
8 | * Information on the RC2 cipher is available from RFC #2268,
|
---|
9 | * http://www.ietf.org/rfc/rfc2268.txt
|
---|
10 | */
|
---|
11 | var forge = require('./forge');
|
---|
12 | require('./util');
|
---|
13 |
|
---|
14 | var piTable = [
|
---|
15 | 0xd9, 0x78, 0xf9, 0xc4, 0x19, 0xdd, 0xb5, 0xed, 0x28, 0xe9, 0xfd, 0x79, 0x4a, 0xa0, 0xd8, 0x9d,
|
---|
16 | 0xc6, 0x7e, 0x37, 0x83, 0x2b, 0x76, 0x53, 0x8e, 0x62, 0x4c, 0x64, 0x88, 0x44, 0x8b, 0xfb, 0xa2,
|
---|
17 | 0x17, 0x9a, 0x59, 0xf5, 0x87, 0xb3, 0x4f, 0x13, 0x61, 0x45, 0x6d, 0x8d, 0x09, 0x81, 0x7d, 0x32,
|
---|
18 | 0xbd, 0x8f, 0x40, 0xeb, 0x86, 0xb7, 0x7b, 0x0b, 0xf0, 0x95, 0x21, 0x22, 0x5c, 0x6b, 0x4e, 0x82,
|
---|
19 | 0x54, 0xd6, 0x65, 0x93, 0xce, 0x60, 0xb2, 0x1c, 0x73, 0x56, 0xc0, 0x14, 0xa7, 0x8c, 0xf1, 0xdc,
|
---|
20 | 0x12, 0x75, 0xca, 0x1f, 0x3b, 0xbe, 0xe4, 0xd1, 0x42, 0x3d, 0xd4, 0x30, 0xa3, 0x3c, 0xb6, 0x26,
|
---|
21 | 0x6f, 0xbf, 0x0e, 0xda, 0x46, 0x69, 0x07, 0x57, 0x27, 0xf2, 0x1d, 0x9b, 0xbc, 0x94, 0x43, 0x03,
|
---|
22 | 0xf8, 0x11, 0xc7, 0xf6, 0x90, 0xef, 0x3e, 0xe7, 0x06, 0xc3, 0xd5, 0x2f, 0xc8, 0x66, 0x1e, 0xd7,
|
---|
23 | 0x08, 0xe8, 0xea, 0xde, 0x80, 0x52, 0xee, 0xf7, 0x84, 0xaa, 0x72, 0xac, 0x35, 0x4d, 0x6a, 0x2a,
|
---|
24 | 0x96, 0x1a, 0xd2, 0x71, 0x5a, 0x15, 0x49, 0x74, 0x4b, 0x9f, 0xd0, 0x5e, 0x04, 0x18, 0xa4, 0xec,
|
---|
25 | 0xc2, 0xe0, 0x41, 0x6e, 0x0f, 0x51, 0xcb, 0xcc, 0x24, 0x91, 0xaf, 0x50, 0xa1, 0xf4, 0x70, 0x39,
|
---|
26 | 0x99, 0x7c, 0x3a, 0x85, 0x23, 0xb8, 0xb4, 0x7a, 0xfc, 0x02, 0x36, 0x5b, 0x25, 0x55, 0x97, 0x31,
|
---|
27 | 0x2d, 0x5d, 0xfa, 0x98, 0xe3, 0x8a, 0x92, 0xae, 0x05, 0xdf, 0x29, 0x10, 0x67, 0x6c, 0xba, 0xc9,
|
---|
28 | 0xd3, 0x00, 0xe6, 0xcf, 0xe1, 0x9e, 0xa8, 0x2c, 0x63, 0x16, 0x01, 0x3f, 0x58, 0xe2, 0x89, 0xa9,
|
---|
29 | 0x0d, 0x38, 0x34, 0x1b, 0xab, 0x33, 0xff, 0xb0, 0xbb, 0x48, 0x0c, 0x5f, 0xb9, 0xb1, 0xcd, 0x2e,
|
---|
30 | 0xc5, 0xf3, 0xdb, 0x47, 0xe5, 0xa5, 0x9c, 0x77, 0x0a, 0xa6, 0x20, 0x68, 0xfe, 0x7f, 0xc1, 0xad
|
---|
31 | ];
|
---|
32 |
|
---|
33 | var s = [1, 2, 3, 5];
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Rotate a word left by given number of bits.
|
---|
37 | *
|
---|
38 | * Bits that are shifted out on the left are put back in on the right
|
---|
39 | * hand side.
|
---|
40 | *
|
---|
41 | * @param word The word to shift left.
|
---|
42 | * @param bits The number of bits to shift by.
|
---|
43 | * @return The rotated word.
|
---|
44 | */
|
---|
45 | var rol = function(word, bits) {
|
---|
46 | return ((word << bits) & 0xffff) | ((word & 0xffff) >> (16 - bits));
|
---|
47 | };
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Rotate a word right by given number of bits.
|
---|
51 | *
|
---|
52 | * Bits that are shifted out on the right are put back in on the left
|
---|
53 | * hand side.
|
---|
54 | *
|
---|
55 | * @param word The word to shift right.
|
---|
56 | * @param bits The number of bits to shift by.
|
---|
57 | * @return The rotated word.
|
---|
58 | */
|
---|
59 | var ror = function(word, bits) {
|
---|
60 | return ((word & 0xffff) >> bits) | ((word << (16 - bits)) & 0xffff);
|
---|
61 | };
|
---|
62 |
|
---|
63 | /* RC2 API */
|
---|
64 | module.exports = forge.rc2 = forge.rc2 || {};
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Perform RC2 key expansion as per RFC #2268, section 2.
|
---|
68 | *
|
---|
69 | * @param key variable-length user key (between 1 and 128 bytes)
|
---|
70 | * @param effKeyBits number of effective key bits (default: 128)
|
---|
71 | * @return the expanded RC2 key (ByteBuffer of 128 bytes)
|
---|
72 | */
|
---|
73 | forge.rc2.expandKey = function(key, effKeyBits) {
|
---|
74 | if(typeof key === 'string') {
|
---|
75 | key = forge.util.createBuffer(key);
|
---|
76 | }
|
---|
77 | effKeyBits = effKeyBits || 128;
|
---|
78 |
|
---|
79 | /* introduce variables that match the names used in RFC #2268 */
|
---|
80 | var L = key;
|
---|
81 | var T = key.length();
|
---|
82 | var T1 = effKeyBits;
|
---|
83 | var T8 = Math.ceil(T1 / 8);
|
---|
84 | var TM = 0xff >> (T1 & 0x07);
|
---|
85 | var i;
|
---|
86 |
|
---|
87 | for(i = T; i < 128; i++) {
|
---|
88 | L.putByte(piTable[(L.at(i - 1) + L.at(i - T)) & 0xff]);
|
---|
89 | }
|
---|
90 |
|
---|
91 | L.setAt(128 - T8, piTable[L.at(128 - T8) & TM]);
|
---|
92 |
|
---|
93 | for(i = 127 - T8; i >= 0; i--) {
|
---|
94 | L.setAt(i, piTable[L.at(i + 1) ^ L.at(i + T8)]);
|
---|
95 | }
|
---|
96 |
|
---|
97 | return L;
|
---|
98 | };
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Creates a RC2 cipher object.
|
---|
102 | *
|
---|
103 | * @param key the symmetric key to use (as base for key generation).
|
---|
104 | * @param bits the number of effective key bits.
|
---|
105 | * @param encrypt false for decryption, true for encryption.
|
---|
106 | *
|
---|
107 | * @return the cipher.
|
---|
108 | */
|
---|
109 | var createCipher = function(key, bits, encrypt) {
|
---|
110 | var _finish = false, _input = null, _output = null, _iv = null;
|
---|
111 | var mixRound, mashRound;
|
---|
112 | var i, j, K = [];
|
---|
113 |
|
---|
114 | /* Expand key and fill into K[] Array */
|
---|
115 | key = forge.rc2.expandKey(key, bits);
|
---|
116 | for(i = 0; i < 64; i++) {
|
---|
117 | K.push(key.getInt16Le());
|
---|
118 | }
|
---|
119 |
|
---|
120 | if(encrypt) {
|
---|
121 | /**
|
---|
122 | * Perform one mixing round "in place".
|
---|
123 | *
|
---|
124 | * @param R Array of four words to perform mixing on.
|
---|
125 | */
|
---|
126 | mixRound = function(R) {
|
---|
127 | for(i = 0; i < 4; i++) {
|
---|
128 | R[i] += K[j] + (R[(i + 3) % 4] & R[(i + 2) % 4]) +
|
---|
129 | ((~R[(i + 3) % 4]) & R[(i + 1) % 4]);
|
---|
130 | R[i] = rol(R[i], s[i]);
|
---|
131 | j++;
|
---|
132 | }
|
---|
133 | };
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Perform one mashing round "in place".
|
---|
137 | *
|
---|
138 | * @param R Array of four words to perform mashing on.
|
---|
139 | */
|
---|
140 | mashRound = function(R) {
|
---|
141 | for(i = 0; i < 4; i++) {
|
---|
142 | R[i] += K[R[(i + 3) % 4] & 63];
|
---|
143 | }
|
---|
144 | };
|
---|
145 | } else {
|
---|
146 | /**
|
---|
147 | * Perform one r-mixing round "in place".
|
---|
148 | *
|
---|
149 | * @param R Array of four words to perform mixing on.
|
---|
150 | */
|
---|
151 | mixRound = function(R) {
|
---|
152 | for(i = 3; i >= 0; i--) {
|
---|
153 | R[i] = ror(R[i], s[i]);
|
---|
154 | R[i] -= K[j] + (R[(i + 3) % 4] & R[(i + 2) % 4]) +
|
---|
155 | ((~R[(i + 3) % 4]) & R[(i + 1) % 4]);
|
---|
156 | j--;
|
---|
157 | }
|
---|
158 | };
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Perform one r-mashing round "in place".
|
---|
162 | *
|
---|
163 | * @param R Array of four words to perform mashing on.
|
---|
164 | */
|
---|
165 | mashRound = function(R) {
|
---|
166 | for(i = 3; i >= 0; i--) {
|
---|
167 | R[i] -= K[R[(i + 3) % 4] & 63];
|
---|
168 | }
|
---|
169 | };
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Run the specified cipher execution plan.
|
---|
174 | *
|
---|
175 | * This function takes four words from the input buffer, applies the IV on
|
---|
176 | * it (if requested) and runs the provided execution plan.
|
---|
177 | *
|
---|
178 | * The plan must be put together in form of a array of arrays. Where the
|
---|
179 | * outer one is simply a list of steps to perform and the inner one needs
|
---|
180 | * to have two elements: the first one telling how many rounds to perform,
|
---|
181 | * the second one telling what to do (i.e. the function to call).
|
---|
182 | *
|
---|
183 | * @param {Array} plan The plan to execute.
|
---|
184 | */
|
---|
185 | var runPlan = function(plan) {
|
---|
186 | var R = [];
|
---|
187 |
|
---|
188 | /* Get data from input buffer and fill the four words into R */
|
---|
189 | for(i = 0; i < 4; i++) {
|
---|
190 | var val = _input.getInt16Le();
|
---|
191 |
|
---|
192 | if(_iv !== null) {
|
---|
193 | if(encrypt) {
|
---|
194 | /* We're encrypting, apply the IV first. */
|
---|
195 | val ^= _iv.getInt16Le();
|
---|
196 | } else {
|
---|
197 | /* We're decryption, keep cipher text for next block. */
|
---|
198 | _iv.putInt16Le(val);
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | R.push(val & 0xffff);
|
---|
203 | }
|
---|
204 |
|
---|
205 | /* Reset global "j" variable as per spec. */
|
---|
206 | j = encrypt ? 0 : 63;
|
---|
207 |
|
---|
208 | /* Run execution plan. */
|
---|
209 | for(var ptr = 0; ptr < plan.length; ptr++) {
|
---|
210 | for(var ctr = 0; ctr < plan[ptr][0]; ctr++) {
|
---|
211 | plan[ptr][1](R);
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | /* Write back result to output buffer. */
|
---|
216 | for(i = 0; i < 4; i++) {
|
---|
217 | if(_iv !== null) {
|
---|
218 | if(encrypt) {
|
---|
219 | /* We're encrypting in CBC-mode, feed back encrypted bytes into
|
---|
220 | IV buffer to carry it forward to next block. */
|
---|
221 | _iv.putInt16Le(R[i]);
|
---|
222 | } else {
|
---|
223 | R[i] ^= _iv.getInt16Le();
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | _output.putInt16Le(R[i]);
|
---|
228 | }
|
---|
229 | };
|
---|
230 |
|
---|
231 | /* Create cipher object */
|
---|
232 | var cipher = null;
|
---|
233 | cipher = {
|
---|
234 | /**
|
---|
235 | * Starts or restarts the encryption or decryption process, whichever
|
---|
236 | * was previously configured.
|
---|
237 | *
|
---|
238 | * To use the cipher in CBC mode, iv may be given either as a string
|
---|
239 | * of bytes, or as a byte buffer. For ECB mode, give null as iv.
|
---|
240 | *
|
---|
241 | * @param iv the initialization vector to use, null for ECB mode.
|
---|
242 | * @param output the output the buffer to write to, null to create one.
|
---|
243 | */
|
---|
244 | start: function(iv, output) {
|
---|
245 | if(iv) {
|
---|
246 | /* CBC mode */
|
---|
247 | if(typeof iv === 'string') {
|
---|
248 | iv = forge.util.createBuffer(iv);
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | _finish = false;
|
---|
253 | _input = forge.util.createBuffer();
|
---|
254 | _output = output || new forge.util.createBuffer();
|
---|
255 | _iv = iv;
|
---|
256 |
|
---|
257 | cipher.output = _output;
|
---|
258 | },
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Updates the next block.
|
---|
262 | *
|
---|
263 | * @param input the buffer to read from.
|
---|
264 | */
|
---|
265 | update: function(input) {
|
---|
266 | if(!_finish) {
|
---|
267 | // not finishing, so fill the input buffer with more input
|
---|
268 | _input.putBuffer(input);
|
---|
269 | }
|
---|
270 |
|
---|
271 | while(_input.length() >= 8) {
|
---|
272 | runPlan([
|
---|
273 | [ 5, mixRound ],
|
---|
274 | [ 1, mashRound ],
|
---|
275 | [ 6, mixRound ],
|
---|
276 | [ 1, mashRound ],
|
---|
277 | [ 5, mixRound ]
|
---|
278 | ]);
|
---|
279 | }
|
---|
280 | },
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Finishes encrypting or decrypting.
|
---|
284 | *
|
---|
285 | * @param pad a padding function to use, null for PKCS#7 padding,
|
---|
286 | * signature(blockSize, buffer, decrypt).
|
---|
287 | *
|
---|
288 | * @return true if successful, false on error.
|
---|
289 | */
|
---|
290 | finish: function(pad) {
|
---|
291 | var rval = true;
|
---|
292 |
|
---|
293 | if(encrypt) {
|
---|
294 | if(pad) {
|
---|
295 | rval = pad(8, _input, !encrypt);
|
---|
296 | } else {
|
---|
297 | // add PKCS#7 padding to block (each pad byte is the
|
---|
298 | // value of the number of pad bytes)
|
---|
299 | var padding = (_input.length() === 8) ? 8 : (8 - _input.length());
|
---|
300 | _input.fillWithByte(padding, padding);
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | if(rval) {
|
---|
305 | // do final update
|
---|
306 | _finish = true;
|
---|
307 | cipher.update();
|
---|
308 | }
|
---|
309 |
|
---|
310 | if(!encrypt) {
|
---|
311 | // check for error: input data not a multiple of block size
|
---|
312 | rval = (_input.length() === 0);
|
---|
313 | if(rval) {
|
---|
314 | if(pad) {
|
---|
315 | rval = pad(8, _output, !encrypt);
|
---|
316 | } else {
|
---|
317 | // ensure padding byte count is valid
|
---|
318 | var len = _output.length();
|
---|
319 | var count = _output.at(len - 1);
|
---|
320 |
|
---|
321 | if(count > len) {
|
---|
322 | rval = false;
|
---|
323 | } else {
|
---|
324 | // trim off padding bytes
|
---|
325 | _output.truncate(count);
|
---|
326 | }
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|
330 |
|
---|
331 | return rval;
|
---|
332 | }
|
---|
333 | };
|
---|
334 |
|
---|
335 | return cipher;
|
---|
336 | };
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Creates an RC2 cipher object to encrypt data in ECB or CBC mode using the
|
---|
340 | * given symmetric key. The output will be stored in the 'output' member
|
---|
341 | * of the returned cipher.
|
---|
342 | *
|
---|
343 | * The key and iv may be given as a string of bytes or a byte buffer.
|
---|
344 | * The cipher is initialized to use 128 effective key bits.
|
---|
345 | *
|
---|
346 | * @param key the symmetric key to use.
|
---|
347 | * @param iv the initialization vector to use.
|
---|
348 | * @param output the buffer to write to, null to create one.
|
---|
349 | *
|
---|
350 | * @return the cipher.
|
---|
351 | */
|
---|
352 | forge.rc2.startEncrypting = function(key, iv, output) {
|
---|
353 | var cipher = forge.rc2.createEncryptionCipher(key, 128);
|
---|
354 | cipher.start(iv, output);
|
---|
355 | return cipher;
|
---|
356 | };
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * Creates an RC2 cipher object to encrypt data in ECB or CBC mode using the
|
---|
360 | * given symmetric key.
|
---|
361 | *
|
---|
362 | * The key may be given as a string of bytes or a byte buffer.
|
---|
363 | *
|
---|
364 | * To start encrypting call start() on the cipher with an iv and optional
|
---|
365 | * output buffer.
|
---|
366 | *
|
---|
367 | * @param key the symmetric key to use.
|
---|
368 | *
|
---|
369 | * @return the cipher.
|
---|
370 | */
|
---|
371 | forge.rc2.createEncryptionCipher = function(key, bits) {
|
---|
372 | return createCipher(key, bits, true);
|
---|
373 | };
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * Creates an RC2 cipher object to decrypt data in ECB or CBC mode using the
|
---|
377 | * given symmetric key. The output will be stored in the 'output' member
|
---|
378 | * of the returned cipher.
|
---|
379 | *
|
---|
380 | * The key and iv may be given as a string of bytes or a byte buffer.
|
---|
381 | * The cipher is initialized to use 128 effective key bits.
|
---|
382 | *
|
---|
383 | * @param key the symmetric key to use.
|
---|
384 | * @param iv the initialization vector to use.
|
---|
385 | * @param output the buffer to write to, null to create one.
|
---|
386 | *
|
---|
387 | * @return the cipher.
|
---|
388 | */
|
---|
389 | forge.rc2.startDecrypting = function(key, iv, output) {
|
---|
390 | var cipher = forge.rc2.createDecryptionCipher(key, 128);
|
---|
391 | cipher.start(iv, output);
|
---|
392 | return cipher;
|
---|
393 | };
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * Creates an RC2 cipher object to decrypt data in ECB or CBC mode using the
|
---|
397 | * given symmetric key.
|
---|
398 | *
|
---|
399 | * The key may be given as a string of bytes or a byte buffer.
|
---|
400 | *
|
---|
401 | * To start decrypting call start() on the cipher with an iv and optional
|
---|
402 | * output buffer.
|
---|
403 | *
|
---|
404 | * @param key the symmetric key to use.
|
---|
405 | *
|
---|
406 | * @return the cipher.
|
---|
407 | */
|
---|
408 | forge.rc2.createDecryptionCipher = function(key, bits) {
|
---|
409 | return createCipher(key, bits, false);
|
---|
410 | };
|
---|