1 | var test = require('tape');
|
2 | var fixtures = require('./fixtures.json');
|
3 | var _crypto = require('crypto');
|
4 | var crypto = require('../');
|
5 | var modes = require('../modes');
|
6 | var types = Object.keys(modes);
|
7 | var ebtk = require('../EVP_BytesToKey');
|
8 | function isGCM(cipher) {
|
9 | return modes[cipher].mode === 'GCM';
|
10 | }
|
11 | function isNode10() {
|
12 | return process.version && process.version.split('.').length === 3 && parseInt(process.version.split('.')[1], 10) <= 10;
|
13 | }
|
14 | fixtures.forEach(function (fixture, i) {
|
15 |
|
16 | types.forEach(function (cipher) {
|
17 | if (isGCM(cipher)) {
|
18 | return;
|
19 | }
|
20 | test('fixture ' + i + ' ' + cipher, function (t) {
|
21 | t.plan(1);
|
22 | var suite = crypto.createCipher(cipher, new Buffer(fixture.password));
|
23 | var buf = new Buffer('');
|
24 | suite.on('data', function (d) {
|
25 | buf = Buffer.concat([buf, d]);
|
26 | });
|
27 | suite.on('error', function (e) {
|
28 | console.log(e);
|
29 | });
|
30 | suite.on("end", function () {
|
31 |
|
32 |
|
33 |
|
34 | t.equals(buf.toString('hex'), fixture.results.ciphers[cipher]);
|
35 | });
|
36 | suite.write(new Buffer(fixture.text));
|
37 | suite.end();
|
38 | });
|
39 | test('fixture ' + i + ' ' + cipher + '-legacy', function (t) {
|
40 | t.plan(3);
|
41 | var suite = crypto.createCipher(cipher, new Buffer(fixture.password));
|
42 | var buf = new Buffer('');
|
43 | var suite2 = _crypto.createCipher(cipher, new Buffer(fixture.password));
|
44 | var buf2 = new Buffer('');
|
45 | var inbuf = new Buffer(fixture.text);
|
46 | var mid = ~~(inbuf.length/2);
|
47 | buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]);
|
48 | buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]);
|
49 | t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate');
|
50 | buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]);
|
51 | buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]);
|
52 | t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate 2');
|
53 | buf = Buffer.concat([buf, suite.final()]);
|
54 | buf2 = Buffer.concat([buf2, suite2.final()]);
|
55 | t.equals(buf.toString('hex'), buf2.toString('hex'), 'final');
|
56 | });
|
57 | test('fixture ' + i + ' ' + cipher + '-decrypt', function (t) {
|
58 | t.plan(1);
|
59 | var suite = crypto.createDecipher(cipher, new Buffer(fixture.password));
|
60 | var buf = new Buffer('');
|
61 | suite.on('data', function (d) {
|
62 | buf = Buffer.concat([buf, d]);
|
63 | });
|
64 | suite.on('error', function (e) {
|
65 | console.log(e);
|
66 | });
|
67 | suite.on("end", function () {
|
68 |
|
69 |
|
70 |
|
71 | t.equals(buf.toString('utf8'), fixture.text);
|
72 | });
|
73 | suite.write(new Buffer(fixture.results.ciphers[cipher], 'hex'));
|
74 | suite.end();
|
75 | });
|
76 | test('fixture ' + i + ' ' + cipher + '-decrypt-legacy', function (t) {
|
77 | t.plan(4);
|
78 | var suite = crypto.createDecipher(cipher, new Buffer(fixture.password));
|
79 | var buf = new Buffer('');
|
80 | var suite2 = _crypto.createDecipher(cipher, new Buffer(fixture.password));
|
81 | var buf2 = new Buffer('');
|
82 | var inbuf = new Buffer(fixture.results.ciphers[cipher], 'hex');
|
83 | var mid = ~~(inbuf.length/2);
|
84 | buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]);
|
85 | buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]);
|
86 | t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate');
|
87 | buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]);
|
88 | buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]);
|
89 | t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate 2');
|
90 | buf = Buffer.concat([buf, suite.final()]);
|
91 | buf2 = Buffer.concat([buf2, suite2.final()]);
|
92 | t.equals(buf.toString('utf8'), fixture.text);
|
93 | t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'final');
|
94 | });
|
95 |
|
96 |
|
97 | types.forEach(function (cipher) {
|
98 | if (modes[cipher].mode === 'ECB') {
|
99 | return;
|
100 | }
|
101 | if (isGCM(cipher) && isNode10()) {
|
102 | return;
|
103 | }
|
104 | test('fixture ' + i + ' ' + cipher + '-iv', function (t) {
|
105 | if (isGCM(cipher)) {
|
106 | t.plan(4);
|
107 | } else {
|
108 | t.plan(2);
|
109 | }
|
110 | var suite = crypto.createCipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
|
111 | var suite2 = _crypto.createCipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
|
112 | var buf = new Buffer('');
|
113 | var buf2 = new Buffer('');
|
114 | suite.on('data', function (d) {
|
115 | buf = Buffer.concat([buf, d]);
|
116 | });
|
117 | suite.on('error', function (e) {
|
118 | console.log(e);
|
119 | });
|
120 | suite2.on('data', function (d) {
|
121 | buf2 = Buffer.concat([buf2, d]);
|
122 | });
|
123 | suite2.on('error', function (e) {
|
124 | console.log(e);
|
125 | });
|
126 | suite.on("end", function () {
|
127 | t.equals(buf.toString('hex'), fixture.results.cipherivs[cipher], 'vs fixture');
|
128 | t.equals(buf.toString('hex'), buf2.toString('hex'), 'vs node');
|
129 | if (isGCM(cipher)) {
|
130 | t.equals(suite.getAuthTag().toString('hex'), fixture.authtag[cipher], 'authtag vs fixture');
|
131 | t.equals(suite.getAuthTag().toString('hex'), suite2.getAuthTag().toString('hex'), 'authtag vs node');
|
132 | }
|
133 | });
|
134 | if (isGCM(cipher)) {
|
135 | suite.setAAD(new Buffer(fixture.aad, 'hex'));
|
136 | suite2.setAAD(new Buffer(fixture.aad, 'hex'));
|
137 | }
|
138 | suite2.write(new Buffer(fixture.text));
|
139 | suite2.end();
|
140 | suite.write(new Buffer(fixture.text));
|
141 | suite.end();
|
142 | });
|
143 |
|
144 | test('fixture ' + i + ' ' + cipher + '-legacy-iv', function (t) {
|
145 | if (isGCM(cipher)) {
|
146 | t.plan(6);
|
147 | } else {
|
148 | t.plan(4);
|
149 | }
|
150 | var suite = crypto.createCipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
|
151 | var suite2 = _crypto.createCipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
|
152 | var buf = new Buffer('');
|
153 | var buf2 = new Buffer('');
|
154 | var inbuf = new Buffer(fixture.text);
|
155 | var mid = ~~(inbuf.length/2);
|
156 | if (isGCM(cipher)) {
|
157 | suite.setAAD(new Buffer(fixture.aad, 'hex'));
|
158 | suite2.setAAD(new Buffer(fixture.aad, 'hex'));
|
159 | }
|
160 | buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]);
|
161 | buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]);
|
162 | t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate');
|
163 | buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]);
|
164 | buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]);
|
165 | t.equals(buf.toString('hex'), buf2.toString('hex'), 'intermediate 2');
|
166 | buf = Buffer.concat([buf, suite.final()]);
|
167 | buf2 = Buffer.concat([buf2, suite2.final()]);
|
168 | t.equals(buf.toString('hex'), fixture.results.cipherivs[cipher]);
|
169 | t.equals(buf.toString('hex'), buf2.toString('hex'), 'final');
|
170 | if (isGCM(cipher)) {
|
171 | t.equals(suite.getAuthTag().toString('hex'), fixture.authtag[cipher], 'authtag vs fixture');
|
172 | t.equals(suite.getAuthTag().toString('hex'), suite2.getAuthTag().toString('hex'), 'authtag vs node');
|
173 | }
|
174 | });
|
175 | test('fixture ' + i + ' ' + cipher + '-iv-decrypt', function (t) {
|
176 | t.plan(2);
|
177 | var suite = crypto.createDecipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
|
178 | var buf = new Buffer('');
|
179 | var suite2 = _crypto.createDecipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
|
180 | var buf2 = new Buffer('');
|
181 | suite.on('data', function (d) {
|
182 | buf = Buffer.concat([buf, d]);
|
183 | });
|
184 | suite.on('error', function (e) {
|
185 | t.notOk(e);
|
186 | });
|
187 | suite2.on('data', function (d) {
|
188 | buf2 = Buffer.concat([buf2, d]);
|
189 | });
|
190 | suite2.on('error', function (e) {
|
191 | t.notOk(e);
|
192 | });
|
193 | suite.on("end", function () {
|
194 | t.equals(buf.toString('utf8'), fixture.text, 'correct text vs fixture');
|
195 | t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'correct text vs node');
|
196 | });
|
197 | if (isGCM(cipher)) {
|
198 | suite.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'));
|
199 | suite2.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'));
|
200 | suite.setAAD(new Buffer(fixture.aad, 'hex'));
|
201 | suite2.setAAD(new Buffer(fixture.aad, 'hex'));
|
202 | }
|
203 |
|
204 | suite2.write(new Buffer(fixture.results.cipherivs[cipher], 'hex'));
|
205 | suite.write(new Buffer(fixture.results.cipherivs[cipher], 'hex'));
|
206 | suite2.end();
|
207 | suite.end();
|
208 | });
|
209 | test('fixture ' + i + ' ' + cipher + '-decrypt-legacy', function (t) {
|
210 | t.plan(4);
|
211 | var suite = crypto.createDecipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
|
212 | var buf = new Buffer('');
|
213 | var suite2 = _crypto.createDecipheriv(cipher, ebtk(_crypto, fixture.password, modes[cipher].key).key, isGCM(cipher) ? (new Buffer(fixture.iv, 'hex').slice(0, 12)) : (new Buffer(fixture.iv, 'hex')));
|
214 | var buf2 = new Buffer('');
|
215 | var inbuf = new Buffer(fixture.results.cipherivs[cipher], 'hex');
|
216 | var mid = ~~(inbuf.length/2);
|
217 | if (isGCM(cipher)) {
|
218 | suite.setAAD(new Buffer(fixture.aad, 'hex'));
|
219 | suite2.setAAD(new Buffer(fixture.aad, 'hex'));
|
220 | suite.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'));
|
221 | suite2.setAuthTag(new Buffer(fixture.authtag[cipher], 'hex'));
|
222 | }
|
223 | buf = Buffer.concat([buf, suite.update(inbuf.slice(0, mid))]);
|
224 | buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(0, mid))]);
|
225 |
|
226 | t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate');
|
227 | buf = Buffer.concat([buf, suite.update(inbuf.slice(mid))]);
|
228 | buf2 = Buffer.concat([buf2, suite2.update(inbuf.slice(mid))]);
|
229 | t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'intermediate 2');
|
230 | buf = Buffer.concat([buf, suite.final()]);
|
231 | buf2 = Buffer.concat([buf2, suite2.final()]);
|
232 | t.equals(buf.toString('utf8'), fixture.text);
|
233 | t.equals(buf.toString('utf8'), buf2.toString('utf8'), 'final');
|
234 | });
|
235 | });
|
236 | });
|
237 | });
|
238 |
|
239 | if (!isNode10()) {
|
240 | test('node tests', function (t) {
|
241 | var TEST_CASES = [
|
242 | { algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
|
243 | iv: '583673497131313748307652', plain: 'Hello World!',
|
244 | ct: '4BE13896F64DFA2C2D0F2C76',
|
245 | tag: '272B422F62EB545EAA15B5FF84092447', tampered: false },
|
246 | { algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
|
247 | iv: '583673497131313748307652', plain: 'Hello World!',
|
248 | ct: '4BE13896F64DFA2C2D0F2C76', aad: '000000FF',
|
249 | tag: 'BA2479F66275665A88CB7B15F43EB005', tampered: false },
|
250 | { algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
|
251 | iv: '583673497131313748307652', plain: 'Hello World!',
|
252 | ct: '4BE13596F64DFA2C2D0FAC76',
|
253 | tag: '272B422F62EB545EAA15B5FF84092447', tampered: true },
|
254 | { algo: 'aes-256-gcm', key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
|
255 | iv: '36306950306836764a6f4561', plain: 'Hello node.js world!',
|
256 | ct: '58E62CFE7B1D274111A82267EBB93866E72B6C2A',
|
257 | tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: false },
|
258 | { algo: 'aes-256-gcm', key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
|
259 | iv: '36306950306836764a6f4561', plain: 'Hello node.js world!',
|
260 | ct: '58E62CFF7B1D274011A82267EBB93866E72B6C2B',
|
261 | tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: true },
|
262 | { algo: 'aes-192-gcm', key: '1ed2233fa2223ef5d7df08546049406c7305220bca40d4c9',
|
263 | iv: '0e1791e9db3bd21a9122c416', plain: 'Hello node.js world!',
|
264 | password: 'very bad password', aad: '63616c76696e',
|
265 | ct: 'DDA53A4059AA17B88756984995F7BBA3C636CC44',
|
266 | tag: 'D2A35E5C611E5E3D2258360241C5B045', tampered: false }
|
267 | ];
|
268 |
|
269 | var ciphers = Object.keys(modes);
|
270 | function testIt(i) {
|
271 | t.test('test case ' + i, function (t) {
|
272 | var test = TEST_CASES[i];
|
273 |
|
274 | if (ciphers.indexOf(test.algo) == -1) {
|
275 | console.log('skipping unsupported ' + test.algo + ' test');
|
276 | return;
|
277 | }
|
278 |
|
279 | (function() {
|
280 | var encrypt = crypto.createCipheriv(test.algo,
|
281 | new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
|
282 | if (test.aad)
|
283 | encrypt.setAAD(new Buffer(test.aad, 'hex'));
|
284 | var hex = encrypt.update(test.plain, 'ascii', 'hex');
|
285 | hex += encrypt.final('hex');
|
286 | var auth_tag = encrypt.getAuthTag();
|
287 |
|
288 | if (!test.tampered) {
|
289 | t.equal(hex.toUpperCase(), test.ct);
|
290 | t.equal(auth_tag.toString('hex').toUpperCase(), test.tag);
|
291 | }
|
292 | })();
|
293 |
|
294 | (function() {
|
295 | var decrypt = crypto.createDecipheriv(test.algo,
|
296 | new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
|
297 | decrypt.setAuthTag(new Buffer(test.tag, 'hex'));
|
298 | if (test.aad)
|
299 | decrypt.setAAD(new Buffer(test.aad, 'hex'));
|
300 | var msg = decrypt.update(test.ct, 'hex', 'ascii');
|
301 | if (!test.tampered) {
|
302 | msg += decrypt.final('ascii');
|
303 | t.equal(msg, test.plain);
|
304 | } else {
|
305 |
|
306 | t.throws(function() { decrypt.final('ascii'); }, / auth/);
|
307 | }
|
308 | })();
|
309 |
|
310 | (function() {
|
311 | if (!test.password) return;
|
312 | var encrypt = crypto.createCipher(test.algo, test.password);
|
313 | if (test.aad)
|
314 | encrypt.setAAD(new Buffer(test.aad, 'hex'));
|
315 | var hex = encrypt.update(test.plain, 'ascii', 'hex');
|
316 | hex += encrypt.final('hex');
|
317 | var auth_tag = encrypt.getAuthTag();
|
318 |
|
319 | if (!test.tampered) {
|
320 | t.equal(hex.toUpperCase(), test.ct);
|
321 | t.equal(auth_tag.toString('hex').toUpperCase(), test.tag);
|
322 | }
|
323 | })();
|
324 |
|
325 | (function() {
|
326 | if (!test.password) return;
|
327 | var decrypt = crypto.createDecipher(test.algo, test.password);
|
328 | decrypt.setAuthTag(new Buffer(test.tag, 'hex'));
|
329 | if (test.aad)
|
330 | decrypt.setAAD(new Buffer(test.aad, 'hex'));
|
331 | var msg = decrypt.update(test.ct, 'hex', 'ascii');
|
332 | if (!test.tampered) {
|
333 | msg += decrypt.final('ascii');
|
334 | t.equal(msg, test.plain);
|
335 | } else {
|
336 |
|
337 | t.throws(function() { decrypt.final('ascii'); }, / auth/);
|
338 | }
|
339 | })();
|
340 |
|
341 |
|
342 |
|
343 |
|
344 | if (i > 0) {
|
345 | t.end();
|
346 | return;
|
347 | }
|
348 |
|
349 | (function() {
|
350 |
|
351 | var encrypt = crypto.createCipheriv('aes-128-cbc',
|
352 | 'ipxp9a6i1Mb4USb4', '6fKjEjR3Vl30EUYC');
|
353 | encrypt.update('blah', 'ascii');
|
354 | encrypt.final();
|
355 | t.throws(function() { encrypt.getAuthTag(); });
|
356 | t.throws(function() {
|
357 | encrypt.setAAD(new Buffer('123', 'ascii')); });
|
358 | })();
|
359 |
|
360 | (function() {
|
361 |
|
362 | var encrypt = crypto.createCipheriv(test.algo,
|
363 | new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
|
364 | encrypt.update('blah', 'ascii');
|
365 | t.throws(function() { encrypt.getAuthTag(); }, / state/);
|
366 | })();
|
367 |
|
368 | (function() {
|
369 |
|
370 | var encrypt = crypto.createCipheriv(test.algo,
|
371 | new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
|
372 | t.throws(function() {
|
373 | encrypt.setAuthTag(new Buffer(test.tag, 'hex')); }, / state/);
|
374 | })();
|
375 |
|
376 | (function() {
|
377 |
|
378 | var decrypt = crypto.createDecipheriv(test.algo,
|
379 | new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
|
380 | t.throws(function() { decrypt.getAuthTag(); }, / state/);
|
381 | })();
|
382 | t.end();
|
383 | });
|
384 | }
|
385 | for (var i in TEST_CASES) {
|
386 | testIt(i);
|
387 |
|
388 | }
|
389 | });
|
390 | }
|
391 | function corectPaddingWords(padding, result) {
|
392 | test('correct padding ' + padding.toString('hex'), function (t) {
|
393 | t.plan(1);
|
394 | var block1 = new Buffer(16);
|
395 | block1.fill(4);
|
396 | result = block1.toString('hex') + result.toString('hex');
|
397 | var cipher = _crypto.createCipher('aes128', new Buffer('password'));
|
398 | cipher.setAutoPadding(false);
|
399 | var decipher = crypto.createDecipher('aes128', new Buffer('password'));
|
400 | var out = new Buffer('');
|
401 | out = Buffer.concat([out, cipher.update(block1)]);
|
402 | out = Buffer.concat([out, cipher.update(padding)]);
|
403 | var deciphered = decipher.update(out);
|
404 | deciphered = Buffer.concat([deciphered, decipher.final()]);
|
405 | t.equals(deciphered.toString('hex'), result);
|
406 | });
|
407 | }
|
408 |
|
409 | var sixteens = new Buffer(16);
|
410 | sixteens.fill(16);
|
411 | corectPaddingWords(sixteens, new Buffer(''));
|
412 | var fifteens = new Buffer(16);
|
413 | fifteens.fill(15);
|
414 | fifteens[0] = 5;
|
415 | corectPaddingWords(fifteens, new Buffer([5]));
|
416 | var one = _crypto.randomBytes(16);
|
417 | one[15] = 1;
|
418 | corectPaddingWords(one, one.slice(0, -1));
|
419 | function incorectPaddingthrows(padding) {
|
420 | test('incorrect padding ' + padding.toString('hex'), function (t) {
|
421 | t.plan(2);
|
422 | var block1 = new Buffer(16);
|
423 | block1.fill(4);
|
424 | var cipher = crypto.createCipher('aes128', new Buffer('password'));
|
425 | cipher.setAutoPadding(false);
|
426 | var decipher = crypto.createDecipher('aes128', new Buffer('password'));
|
427 | var decipher2 = _crypto.createDecipher('aes128', new Buffer('password'));
|
428 | var out = new Buffer('');
|
429 | out = Buffer.concat([out, cipher.update(block1)]);
|
430 | out = Buffer.concat([out, cipher.update(padding)]);
|
431 | decipher.update(out);
|
432 | decipher2.update(out);
|
433 | t.throws(function () {
|
434 | decipher.final();
|
435 | }, 'mine');
|
436 | t.throws(function () {
|
437 | decipher2.final();
|
438 | }, 'node');
|
439 | });
|
440 | }
|
441 | function incorectPaddingDoesNotThrow(padding) {
|
442 | test('stream incorrect padding ' + padding.toString('hex'), function (t) {
|
443 | t.plan(2);
|
444 | var block1 = new Buffer(16);
|
445 | block1.fill(4);
|
446 | var cipher = crypto.createCipher('aes128', new Buffer('password'));
|
447 | cipher.setAutoPadding(false);
|
448 | var decipher = crypto.createDecipher('aes128', new Buffer('password'));
|
449 | var decipher2 = _crypto.createDecipher('aes128', new Buffer('password'));
|
450 | cipher.pipe(decipher);
|
451 | cipher.pipe(decipher2);
|
452 | cipher.write(block1);
|
453 | cipher.write(padding);
|
454 | decipher.on('error', function (e) {
|
455 | t.ok(e, 'mine');
|
456 | });
|
457 | decipher2.on('error', function (e) {
|
458 | t.ok(e, 'node');
|
459 | });
|
460 | cipher.end();
|
461 | });
|
462 | }
|
463 | var sixteens2 = new Buffer(16);
|
464 | sixteens2.fill(16);
|
465 | sixteens2[3] = 5;
|
466 | incorectPaddingthrows(sixteens2);
|
467 | incorectPaddingDoesNotThrow(sixteens2);
|
468 | var fifteens2 = new Buffer(16);
|
469 | fifteens2.fill(15);
|
470 | fifteens2[0] = 5;
|
471 | fifteens2[1] = 6;
|
472 | incorectPaddingthrows(fifteens2);
|
473 | incorectPaddingDoesNotThrow(fifteens2);
|
474 | var two = _crypto.randomBytes(16);
|
475 | two[15] = 2;
|
476 | two[14] = 1;
|
477 | incorectPaddingthrows(two);
|
478 | incorectPaddingDoesNotThrow(two);
|
479 | test('autopadding false decipher', function (t) {
|
480 | t.plan(2);
|
481 | var mycipher = crypto.createCipher('AES-128-ECB', new Buffer('password'));
|
482 | var nodecipher = _crypto.createCipher('AES-128-ECB', new Buffer('password'));
|
483 | var myEnc = mycipher.final();
|
484 | var nodeEnc = nodecipher.final();
|
485 | t.equals(myEnc.toString('hex'), nodeEnc.toString('hex'), 'same encryption');
|
486 | var decipher = crypto.createDecipher('aes-128-ecb', new Buffer('password'));
|
487 | decipher.setAutoPadding(false);
|
488 | var decipher2 = _crypto.createDecipher('aes-128-ecb', new Buffer('password'));
|
489 | decipher2.setAutoPadding(false);
|
490 | t.equals(decipher.update(myEnc).toString('hex'), decipher2.update(nodeEnc).toString('hex'), 'same decryption');
|
491 | });
|
492 |
|
493 | test('autopadding false cipher throws', function (t) {
|
494 | t.plan(2);
|
495 | var mycipher = crypto.createCipher('aes-128-ecb', new Buffer('password'));
|
496 | mycipher.setAutoPadding(false);
|
497 | var nodecipher = _crypto.createCipher('aes-128-ecb', new Buffer('password'));
|
498 | nodecipher.setAutoPadding(false);
|
499 | mycipher.update('foo');
|
500 | nodecipher.update('foo');
|
501 | t.throws(function () {
|
502 | mycipher.final();
|
503 | }, 'mine');
|
504 | t.throws(function () {
|
505 | nodecipher.final();
|
506 | }, 'node');
|
507 | });
|