|
|
|
var should = require('chai').should();
|
|
|
|
var CBC = require('../lib/expmt/cbc');
|
|
|
|
|
|
|
|
describe('CBC', function() {
|
|
|
|
|
|
|
|
it('should return a new CBC', function() {
|
|
|
|
var cbc = new CBC();
|
|
|
|
should.exist(cbc);
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return a new CBC when called without "new"', function() {
|
|
|
|
var cbc = new CBC();
|
|
|
|
should.exist(cbc);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('@buf2blockbufs', function() {
|
|
|
|
|
|
|
|
it('should convert this buffer into one block', function() {
|
|
|
|
var buf = new Buffer(16 - 1);
|
|
|
|
buf.fill(0);
|
|
|
|
var blockbufs = CBC.buf2blockbufs(buf, 16 * 8);
|
|
|
|
blockbufs.length.should.equal(1);
|
|
|
|
blockbufs[0].toString('hex').should.equal('00000000000000000000000000000001');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should convert this buffer into two blocks', function() {
|
|
|
|
var buf = new Buffer(16);
|
|
|
|
buf.fill(0);
|
|
|
|
var blockbufs = CBC.buf2blockbufs(buf, 16 * 8);
|
|
|
|
blockbufs.length.should.equal(2);
|
|
|
|
blockbufs[0].toString('hex').should.equal('00000000000000000000000000000000');
|
|
|
|
blockbufs[1].toString('hex').should.equal('10101010101010101010101010101010');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('@encrypt', function() {
|
|
|
|
|
|
|
|
it('should return this known value', function() {
|
|
|
|
var messagebuf1 = new Buffer(128 / 8);
|
|
|
|
messagebuf1.fill(0);
|
|
|
|
var messagebuf2 = new Buffer(128 / 8);
|
|
|
|
messagebuf2.fill(0x10);
|
|
|
|
var messagebuf = Buffer.concat([messagebuf1, messagebuf2]);
|
|
|
|
var ivbuf = new Buffer(128 / 8);
|
|
|
|
ivbuf.fill(0x10);
|
|
|
|
var keybuf = new Buffer(128 / 8);
|
|
|
|
keybuf.fill(0);
|
|
|
|
var blockcipher = {};
|
|
|
|
blockcipher.encrypt = function(messagebuf, keybuf) {
|
|
|
|
return messagebuf;
|
|
|
|
};
|
|
|
|
var encbuf = CBC.encrypt(messagebuf, ivbuf, blockcipher, keybuf);
|
|
|
|
encbuf.toString('hex').should.equal('101010101010101010101010101010100000000000000000000000000000000010101010101010101010101010101010');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('@encryptblock', function() {
|
|
|
|
|
|
|
|
it('should return this known value', function() {
|
|
|
|
var messagebuf = new Buffer(128 / 8);
|
|
|
|
messagebuf.fill(0);
|
|
|
|
var ivbuf = new Buffer(128 / 8);
|
|
|
|
ivbuf.fill(0x10);
|
|
|
|
var keybuf = new Buffer(128 / 8);
|
|
|
|
keybuf.fill(0);
|
|
|
|
var blockcipher = {};
|
|
|
|
blockcipher.encrypt = function(messagebuf, keybuf) {
|
|
|
|
return messagebuf;
|
|
|
|
};
|
|
|
|
var enc = CBC.encryptblock(messagebuf, ivbuf, blockcipher, keybuf);
|
|
|
|
enc.toString('hex').should.equal(ivbuf.toString('hex'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return this other known value', function() {
|
|
|
|
var messagebuf = new Buffer(128 / 8);
|
|
|
|
messagebuf.fill(0x10);
|
|
|
|
var ivbuf = new Buffer(128 / 8);
|
|
|
|
ivbuf.fill(0x10);
|
|
|
|
var keybuf = new Buffer(128 / 8);
|
|
|
|
keybuf.fill(0);
|
|
|
|
var blockcipher = {};
|
|
|
|
blockcipher.encrypt = function(messagebuf, keybuf) {
|
|
|
|
return messagebuf;
|
|
|
|
};
|
|
|
|
var enc = CBC.encryptblock(messagebuf, ivbuf, blockcipher, keybuf);
|
|
|
|
enc.toString('hex').should.equal('00000000000000000000000000000000');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('@encryptblocks', function() {
|
|
|
|
|
|
|
|
it('should return this known value', function() {
|
|
|
|
var messagebuf1 = new Buffer(128 / 8);
|
|
|
|
messagebuf1.fill(0);
|
|
|
|
var messagebuf2 = new Buffer(128 / 8);
|
|
|
|
messagebuf2.fill(0x10);
|
|
|
|
var ivbuf = new Buffer(128 / 8);
|
|
|
|
ivbuf.fill(0x10);
|
|
|
|
var keybuf = new Buffer(128 / 8);
|
|
|
|
keybuf.fill(0);
|
|
|
|
var blockcipher = {}
|
|
|
|
blockcipher.encrypt = function(messagebuf, keybuf) {
|
|
|
|
return messagebuf;
|
|
|
|
};
|
|
|
|
var encbufs = CBC.encryptblocks([messagebuf1, messagebuf2], ivbuf, blockcipher, keybuf);
|
|
|
|
encbufs[0].toString('hex').should.equal('10101010101010101010101010101010');
|
|
|
|
encbufs[1].toString('hex').should.equal('00000000000000000000000000000000');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('@pkcs7pad', function() {
|
|
|
|
|
|
|
|
it('should pad this 32 bit buffer to 128 bits with the number 128/8 - 32/8', function() {
|
|
|
|
var buf = new Buffer(32 / 8);
|
|
|
|
buf.fill(0);
|
|
|
|
var padbuf = CBC.pkcs7pad(buf, 128);
|
|
|
|
padbuf.length.should.equal(128 / 8);
|
|
|
|
padbuf[32 / 8].should.equal(128 / 8 - 32 / 8);
|
|
|
|
padbuf[32 / 8 + 1].should.equal(128 / 8 - 32 / 8);
|
|
|
|
// ...
|
|
|
|
padbuf[32 / 8 + 128 / 8 - 32 / 8 - 1].should.equal(128 / 8 - 32 / 8);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('@xorbufs', function() {
|
|
|
|
|
|
|
|
it('should xor 1 and 0', function() {
|
|
|
|
var buf1 = new Buffer([1]);
|
|
|
|
var buf2 = new Buffer([0]);
|
|
|
|
var buf = CBC.xorbufs(buf1, buf2);
|
|
|
|
buf[0].should.equal(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should xor 1 and 1', function() {
|
|
|
|
var buf1 = new Buffer([1]);
|
|
|
|
var buf2 = new Buffer([1]);
|
|
|
|
var buf = CBC.xorbufs(buf1, buf2);
|
|
|
|
buf[0].should.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|