Skip to content

Commit 28aabef

Browse files
committed
Added multiple block processing to CBC and ECB.
1 parent f387521 commit 28aabef

File tree

9 files changed

+1189
-1146
lines changed

9 files changed

+1189
-1146
lines changed

generate-tests.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ def default(o, encoder=json.JSONEncoder()):
5959

6060
if mode == 'CBC':
6161
iv = os.urandom(16)
62-
plaintext = [ os.urandom(16) for x in xrange(0, test) ]
62+
63+
text_length = [None, 16, 16, 16, 32, 48, 64, 64, 64][test]
64+
if test == 1:
65+
plaintext = [ '' ]
66+
else:
67+
plaintext = [ os.urandom(text_length) for x in xrange(0, test) ]
6368

6469
kaes = KAES.new(key, KAES.MODE_CBC, IV = iv)
6570
kaes2 = KAES.new(key, KAES.MODE_CBC, IV = iv)
@@ -74,7 +79,11 @@ def default(o, encoder=json.JSONEncoder()):
7479
segment_size = test
7580

7681
elif mode == 'ECB':
77-
plaintext = [ os.urandom(16) for x in xrange(0, test) ]
82+
text_length = [None, 16, 16, 16, 32, 48, 64, 64, 64][test]
83+
if test == 1:
84+
plaintext = [ '' ]
85+
else:
86+
plaintext = [ os.urandom(text_length) for x in xrange(0, test) ]
7887

7988
kaes = KAES.new(key, KAES.MODE_ECB)
8089
kaes2 = KAES.new(key, KAES.MODE_ECB)

index.js

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,37 @@
379379
}
380380

381381
ModeOfOperationECB.prototype.encrypt = function(plaintext) {
382-
return this._aes.encrypt(plaintext);
382+
if ((plaintext.length % 16) !== 0) {
383+
throw new Error('invalid plaintext size (must be multiple of 16 bytes)');
384+
}
385+
386+
var ciphertext = createBuffer(plaintext.length);
387+
var block = createBuffer(16);
388+
389+
for (var i = 0; i < plaintext.length; i += 16) {
390+
copyBuffer(plaintext, block, 0, i, i + 16);
391+
block = this._aes.encrypt(block);
392+
copyBuffer(block, ciphertext, i, 0, 16);
393+
}
394+
395+
return ciphertext;
383396
}
384397

385-
ModeOfOperationECB.prototype.decrypt = function(ciphertext, encoding) {
386-
return this._aes.decrypt(ciphertext);
398+
ModeOfOperationECB.prototype.decrypt = function(ciphertext) {
399+
if ((ciphertext.length % 16) !== 0) {
400+
throw new Error('invalid ciphertext size (must be multiple of 16 bytes)');
401+
}
402+
403+
var plaintext = createBuffer(ciphertext.length);
404+
var block = createBuffer(16);
405+
406+
for (var i = 0; i < ciphertext.length; i += 16) {
407+
copyBuffer(ciphertext, block, 0, i, i + 16);
408+
block = this._aes.decrypt(block);
409+
copyBuffer(block, plaintext, i, 0, 16);
410+
}
411+
412+
return plaintext;
387413
}
388414

389415

@@ -411,31 +437,45 @@
411437
}
412438

413439
ModeOfOperationCBC.prototype.encrypt = function(plaintext) {
414-
if (plaintext.length != 16) {
415-
throw new Error('invalid plaintext size (must be 16 bytes)');
440+
if ((plaintext.length % 16) !== 0) {
441+
throw new Error('invalid plaintext size (must be multiple of 16 bytes)');
416442
}
417443

418-
var precipherblock = createBuffer(plaintext);
419-
for (var i = 0; i < 16; i++) {
420-
precipherblock[i] ^= this._lastCipherblock[i];
421-
}
444+
var ciphertext = createBuffer(plaintext.length);
445+
var block = createBuffer(16);
446+
447+
for (var i = 0; i < plaintext.length; i += 16) {
448+
copyBuffer(plaintext, block, 0, i, i + 16);
449+
450+
for (var j = 0; j < 16; j++) {
451+
block[j] ^= this._lastCipherblock[j];
452+
}
422453

423-
this._lastCipherblock = this._aes.encrypt(precipherblock);
454+
this._lastCipherblock = this._aes.encrypt(block);
455+
copyBuffer(this._lastCipherblock, ciphertext, i, 0, 16);
456+
}
424457

425-
return this._lastCipherblock;
458+
return ciphertext;
426459
}
427460

428461
ModeOfOperationCBC.prototype.decrypt = function(ciphertext) {
429-
if (ciphertext.length != 16) {
430-
throw new Error('invalid ciphertext size (must be 16 bytes)');
462+
if ((ciphertext.length % 16) !== 0) {
463+
throw new Error('invalid ciphertext size (must be multiple of 16 bytes)');
431464
}
432465

433-
var plaintext = this._aes.decrypt(ciphertext);
434-
for (var i = 0; i < 16; i++) {
435-
plaintext[i] ^= this._lastCipherblock[i];
436-
}
466+
var plaintext = createBuffer(ciphertext.length);
467+
var block = createBuffer(16);
468+
469+
for (var i = 0; i < ciphertext.length; i += 16) {
470+
copyBuffer(ciphertext, block, 0, i, i + 16);
471+
block = this._aes.decrypt(block);
437472

438-
copyBuffer(ciphertext, this._lastCipherblock);
473+
for (var j = 0; j < 16; j++) {
474+
plaintext[i + j] = block[j] ^ this._lastCipherblock[j];
475+
}
476+
477+
copyBuffer(ciphertext, this._lastCipherblock, 0, i, i + 16);
478+
}
439479

440480
return plaintext;
441481
}
@@ -647,7 +687,7 @@
647687
ModeOfOperationCTR.prototype.decrypt = ModeOfOperationCTR.prototype.encrypt;
648688

649689

650-
// The bsic modes of operation as a map
690+
// The basic modes of operation as a map
651691
var ModeOfOperation = {
652692
ecb: ModeOfOperationECB,
653693
cbc: ModeOfOperationCBC,

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aes-js",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"bugs": {
55
"url": "http://github.com/ricmoo/aes-js/issues",
66
"email": "github@ricmoo.com"
@@ -11,7 +11,7 @@
1111
},
1212
"main": "index.js",
1313
"scripts": {
14-
"test": "node test/test-aes.js && node test/test-counter.js && node test/test-buffer.js && node test/test-errors.js"
14+
"test": "./node_modules/.bin/nodeunit test/index.js"
1515
},
1616
"repository": {
1717
"type": "git",

test/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
function populateTests(tests) {
3+
for (var key in tests) {
4+
module.exports[key] = tests[key];
5+
}
6+
}
7+
populateTests(require('./test-aes.js'));
8+
populateTests(require('./test-counter.js'));
9+
populateTests(require('./test-buffer.js'));
10+
populateTests(require('./test-errors.js'));

test/test-aes.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ function makeTest(options) {
7272
var testVectors = require('./test-vectors.json');
7373

7474
var Tests = {};
75-
75+
var counts = {}
7676
for (var i = 0; i < testVectors.length; i++) {
7777
var test = testVectors[i];
78-
name = 'test-' + test.modeOfOperation + '-' + test.key.length;
79-
if (!Tests[name]) { Tests[name] = {}; }
80-
Tests[name]['test-' + Object.keys(Tests[name]).length] = makeTest(test);
78+
name = test.modeOfOperation + '-' + test.key.length;
79+
counts[name] = (counts[name] || 0) + 1;
80+
Tests['test-' + name + '-' + counts[name]] = makeTest(test);
8181
}
8282

83-
nodeunit.reporters.default.run(Tests);
83+
module.exports = Tests;

test/test-buffer.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,18 @@ function buffersEqual(a, b) {
2727
return true;
2828
}
2929

30-
nodeunit.reporters.default.run({
31-
"test-buffer": {
32-
"slowCreate": function(test) {
33-
//var result = new AES(testArray).key;
34-
var result = slowCreateBuffer(testArray);
35-
test.ok(buffersEqual(testArray, result), 'bufferCreate failed to match input array');
30+
module.exports = {
31+
"test-slowCreate": function(test) {
32+
//var result = new AES(testArray).key;
33+
var result = slowCreateBuffer(testArray);
34+
test.ok(buffersEqual(testArray, result), 'bufferCreate failed to match input array');
3635

37-
result = slowCreateBuffer(testBuffer);
38-
test.ok(buffersEqual(testBuffer, result), 'bufferCreate failed to match input array');
36+
result = slowCreateBuffer(testBuffer);
37+
test.ok(buffersEqual(testBuffer, result), 'bufferCreate failed to match input array');
3938

40-
result = slowCreateBuffer(new WeirdBuffer(testArray));
41-
test.ok(buffersEqual(testBuffer, result), 'bufferCreate failed to match input array');
39+
result = slowCreateBuffer(new WeirdBuffer(testArray));
40+
test.ok(buffersEqual(testBuffer, result), 'bufferCreate failed to match input array');
4241

43-
test.done();
44-
},
42+
test.done();
4543
},
46-
});
44+
};

test/test-counter.js

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,17 @@ function makeTest (options) {
5757
};
5858
}
5959

60-
var Tests = {
61-
'test-counter-nullish': {
62-
'test-null': makeTest({nullish: null, incrementResult: "00000000000000000000000000000002"}),
63-
'test-undefined': makeTest({nullish: undefined, incrementResult: "00000000000000000000000000000002"}),
64-
},
65-
'test-counter-number': {
66-
'test-0': makeTest({number: 0, incrementResult: "00000000000000000000000000000001"}),
67-
'test-1': makeTest({number: 1, incrementResult: "00000000000000000000000000000002"}),
68-
'test-254': makeTest({number: 254, incrementResult: "000000000000000000000000000000ff"}),
69-
'test-255': makeTest({number: 255, incrementResult: "00000000000000000000000000000100"}),
70-
'test-256': makeTest({number: 256, incrementResult: "00000000000000000000000000000101"}),
71-
},
72-
'test-counter-bytes': {
73-
'test-0000': makeTest({bytes: "00000000000000000000000000000000", incrementResult: "00000000000000000000000000000001"}),
74-
'test-00ff': makeTest({bytes: "000000000000000000000000000000ff", incrementResult: "00000000000000000000000000000100"}),
75-
'test-ffff': makeTest({bytes: "ffffffffffffffffffffffffffffffff", incrementResult: "00000000000000000000000000000000"}),
76-
'test-dead': makeTest({bytes: "deadbeefdeadbeefdeadbeefdeadbeef", incrementResult: "deadbeefdeadbeefdeadbeefdeadbef0"}),
77-
},
60+
module.exports = {
61+
'test-counter-nullish-null': makeTest({nullish: null, incrementResult: "00000000000000000000000000000002"}),
62+
'test-counter-nullish-undefined': makeTest({nullish: undefined, incrementResult: "00000000000000000000000000000002"}),
63+
'test-counter-number-0': makeTest({number: 0, incrementResult: "00000000000000000000000000000001"}),
64+
'test-counter-number-1': makeTest({number: 1, incrementResult: "00000000000000000000000000000002"}),
65+
'test-counter-number-254': makeTest({number: 254, incrementResult: "000000000000000000000000000000ff"}),
66+
'test-counter-number-255': makeTest({number: 255, incrementResult: "00000000000000000000000000000100"}),
67+
'test-counter-number-256': makeTest({number: 256, incrementResult: "00000000000000000000000000000101"}),
68+
'test-counter-bytes-0000': makeTest({bytes: "00000000000000000000000000000000", incrementResult: "00000000000000000000000000000001"}),
69+
'test-counter-bytes-00ff': makeTest({bytes: "000000000000000000000000000000ff", incrementResult: "00000000000000000000000000000100"}),
70+
'test-counter-bytes-ffff': makeTest({bytes: "ffffffffffffffffffffffffffffffff", incrementResult: "00000000000000000000000000000000"}),
71+
'test-counter-bytes-dead': makeTest({bytes: "deadbeefdeadbeefdeadbeefdeadbeef", incrementResult: "deadbeefdeadbeefdeadbeefdeadbef0"}),
7872
};
7973

80-
/*
81-
for (var i = 0; i < testVectors.length; i++) {
82-
var test = testVectors[i];
83-
name = 'test-' + test.modeOfOperation + '-' + test.key.length;
84-
if (!Tests[name]) { Tests[name] = {}; }
85-
Tests[name]['test-' + Object.keys(Tests[name]).length] = makeTest(test);
86-
}
87-
*/
88-
nodeunit.reporters.default.run(Tests);
89-

0 commit comments

Comments
 (0)