Browse Source

block cipher should support "encrypt" and "decrypt"

patch-2
Ryan X. Charles 11 years ago
parent
commit
a09cac4ba6
  1. 18
      lib/expmt/cbc.js
  2. 20
      test/test.cbc.js

18
lib/expmt/cbc.js

@ -1,11 +1,11 @@
var Random = require('../random'); var Random = require('../random');
// http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher-block_chaining_.28CBC.29 // http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher-block_chaining_.28CBC.29
var CBC = function CBC(blockcipherf, keybuf, ivbuf) { var CBC = function CBC(blockcipher, keybuf, ivbuf) {
if (!(this instanceof CBC)) if (!(this instanceof CBC))
return new CBC(blockcipherf, keybuf, ivbuf); return new CBC(blockcipher, keybuf, ivbuf);
this.blockcipherf = blockcipherf; this.blockcipher = blockcipher;
this.keybuf = keybuf; this.keybuf = keybuf;
this.ivbuf = ivbuf; this.ivbuf = ivbuf;
}; };
@ -26,26 +26,26 @@ CBC.buf2blockbufs = function(buf, blocksize) {
return blockbufs; return blockbufs;
}; };
CBC.encrypt = function(messagebuf, ivbuf, blockcipherf, keybuf) { CBC.encrypt = function(messagebuf, ivbuf, blockcipher, keybuf) {
var blocksize = ivbuf.length * 8; var blocksize = ivbuf.length * 8;
var blockbufs = CBC.buf2blockbufs(messagebuf, blocksize); var blockbufs = CBC.buf2blockbufs(messagebuf, blocksize);
var encbufs = CBC.encryptblocks(blockbufs, ivbuf, blockcipherf, keybuf); var encbufs = CBC.encryptblocks(blockbufs, ivbuf, blockcipher, keybuf);
var enc = Buffer.concat(encbufs); var enc = Buffer.concat(encbufs);
return enc; return enc;
}; };
CBC.encryptblock = function(blockbuf, ivbuf, blockcipherf, keybuf) { CBC.encryptblock = function(blockbuf, ivbuf, blockcipher, keybuf) {
var xorbuf = CBC.xorbufs(blockbuf, ivbuf); var xorbuf = CBC.xorbufs(blockbuf, ivbuf);
var encbuf = blockcipherf(xorbuf, keybuf); var encbuf = blockcipher.encrypt(xorbuf, keybuf);
return encbuf; return encbuf;
}; };
CBC.encryptblocks = function(blockbufs, ivbuf, blockcipherf, keybuf) { CBC.encryptblocks = function(blockbufs, ivbuf, blockcipher, keybuf) {
var encbufs = []; var encbufs = [];
for (var i = 0; i < blockbufs.length; i++) { for (var i = 0; i < blockbufs.length; i++) {
var blockbuf = blockbufs[i]; var blockbuf = blockbufs[i];
var encbuf = CBC.encryptblock(blockbuf, ivbuf, blockcipherf, keybuf); var encbuf = CBC.encryptblock(blockbuf, ivbuf, blockcipher, keybuf);
encbufs.push(encbuf); encbufs.push(encbuf);

20
test/test.cbc.js

@ -46,10 +46,11 @@ describe('CBC', function() {
ivbuf.fill(0x10); ivbuf.fill(0x10);
var keybuf = new Buffer(128 / 8); var keybuf = new Buffer(128 / 8);
keybuf.fill(0); keybuf.fill(0);
var blockcipherf = function(messagebuf, keybuf) { var blockcipher = {};
blockcipher.encrypt = function(messagebuf, keybuf) {
return messagebuf; return messagebuf;
}; };
var encbuf = CBC.encrypt(messagebuf, ivbuf, blockcipherf, keybuf); var encbuf = CBC.encrypt(messagebuf, ivbuf, blockcipher, keybuf);
encbuf.toString('hex').should.equal('101010101010101010101010101010100000000000000000000000000000000010101010101010101010101010101010'); encbuf.toString('hex').should.equal('101010101010101010101010101010100000000000000000000000000000000010101010101010101010101010101010');
}); });
@ -64,10 +65,11 @@ describe('CBC', function() {
ivbuf.fill(0x10); ivbuf.fill(0x10);
var keybuf = new Buffer(128 / 8); var keybuf = new Buffer(128 / 8);
keybuf.fill(0); keybuf.fill(0);
var blockcipherf = function(messagebuf, keybuf) { var blockcipher = {};
blockcipher.encrypt = function(messagebuf, keybuf) {
return messagebuf; return messagebuf;
}; };
var enc = CBC.encryptblock(messagebuf, ivbuf, blockcipherf, keybuf); var enc = CBC.encryptblock(messagebuf, ivbuf, blockcipher, keybuf);
enc.toString('hex').should.equal(ivbuf.toString('hex')); enc.toString('hex').should.equal(ivbuf.toString('hex'));
}); });
@ -78,10 +80,11 @@ describe('CBC', function() {
ivbuf.fill(0x10); ivbuf.fill(0x10);
var keybuf = new Buffer(128 / 8); var keybuf = new Buffer(128 / 8);
keybuf.fill(0); keybuf.fill(0);
var blockcipherf = function(messagebuf, keybuf) { var blockcipher = {};
blockcipher.encrypt = function(messagebuf, keybuf) {
return messagebuf; return messagebuf;
}; };
var enc = CBC.encryptblock(messagebuf, ivbuf, blockcipherf, keybuf); var enc = CBC.encryptblock(messagebuf, ivbuf, blockcipher, keybuf);
enc.toString('hex').should.equal('00000000000000000000000000000000'); enc.toString('hex').should.equal('00000000000000000000000000000000');
}); });
@ -98,10 +101,11 @@ describe('CBC', function() {
ivbuf.fill(0x10); ivbuf.fill(0x10);
var keybuf = new Buffer(128 / 8); var keybuf = new Buffer(128 / 8);
keybuf.fill(0); keybuf.fill(0);
var blockcipherf = function(messagebuf, keybuf) { var blockcipher = {}
blockcipher.encrypt = function(messagebuf, keybuf) {
return messagebuf; return messagebuf;
}; };
var encbufs = CBC.encryptblocks([messagebuf1, messagebuf2], ivbuf, blockcipherf, keybuf); var encbufs = CBC.encryptblocks([messagebuf1, messagebuf2], ivbuf, blockcipher, keybuf);
encbufs[0].toString('hex').should.equal('10101010101010101010101010101010'); encbufs[0].toString('hex').should.equal('10101010101010101010101010101010');
encbufs[1].toString('hex').should.equal('00000000000000000000000000000000'); encbufs[1].toString('hex').should.equal('00000000000000000000000000000000');
}); });

Loading…
Cancel
Save