diff --git a/lib/expmt/aes.js b/lib/expmt/aes.js new file mode 100644 index 0000000..4117ed4 --- /dev/null +++ b/lib/expmt/aes.js @@ -0,0 +1,47 @@ +var aes = require('aes'); + +var AES = function AES() { +}; + +AES.encrypt = function(messagebuf, keybuf) { + var key = AES.buf2words(keybuf); + var message = AES.buf2words(messagebuf); + var a = new aes(key); + var enc = a.encrypt(message); + var encbuf = AES.words2buf(enc); + return encbuf; +}; + +AES.decrypt = function(encbuf, keybuf) { + var enc = AES.buf2words(encbuf); + var key = AES.buf2words(keybuf); + var a = new aes(key); + var message = a.decrypt(enc); + var messagebuf = AES.words2buf(message); + return messagebuf; +}; + +AES.buf2words = function(buf) { + if (buf.length % 4) + throw new Error('buf length must be a multiple of 4'); + + var words = []; + + for (var i = 0; i < buf.length / 4; i++) { + words.push(buf.readUInt32BE(i * 4)); + }; + + return words; +}; + +AES.words2buf = function(words) { + var buf = new Buffer(words.length * 4); + + for (var i = 0; i < words.length; i++) { + buf.writeUInt32BE(words[i], i * 4); + }; + + return buf; +}; + +module.exports = AES; diff --git a/package.json b/package.json index 71685fc..758d541 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "multisig" ], "dependencies": { + "aes": "=0.1.0", "bn.js": "=0.13.3", "bs58": "=1.2.1", "elliptic": "=0.15.7", diff --git a/test/test.aes.js b/test/test.aes.js new file mode 100644 index 0000000..be09a4c --- /dev/null +++ b/test/test.aes.js @@ -0,0 +1,85 @@ +var should = require('chai').should(); +var Hash = require('../lib/hash'); +var AES = require('../lib/expmt/aes'); + +describe('AES', function() { + var m128 = Hash.sha256(new Buffer('test1')).slice(0, 128 / 8); + + var k128 = Hash.sha256(new Buffer('test2')).slice(0, 128 / 8); + var k192 = Hash.sha256(new Buffer('test2')).slice(0, 192 / 8); + var k256 = Hash.sha256(new Buffer('test2')).slice(0, 256 / 8); + + var e128 = new Buffer('3477e13884125038f4dc24e9d2cfbbc7', 'hex'); + var e192 = new Buffer('b670954c0e2da1aaa5f9063de04eb961', 'hex'); + var e256 = new Buffer('dd2ce24581183a4a7c0b1068f8bc79f0', 'hex'); + + + describe('@encrypt', function() { + + it('should encrypt with a 128 bit key', function() { + var encbuf = AES.encrypt(m128, k128); + encbuf.toString('hex').should.equal(e128.toString('hex')); + }); + + it('should encrypt with a 192 bit key', function() { + var encbuf = AES.encrypt(m128, k192); + encbuf.toString('hex').should.equal(e192.toString('hex')); + }); + + it('should encrypt with a 256 bit key', function() { + var encbuf = AES.encrypt(m128, k256); + encbuf.toString('hex').should.equal(e256.toString('hex')); + }); + + }); + + describe('@decrypt', function() { + + it('should encrypt/decrypt with a 128 bit key', function() { + var encbuf = AES.encrypt(m128, k128); + var m = AES.decrypt(encbuf, k128); + m.toString('hex').should.equal(m128.toString('hex')); + }); + + it('should encrypt/decrypt with a 192 bit key', function() { + var encbuf = AES.encrypt(m128, k192); + var m = AES.decrypt(encbuf, k192); + m.toString('hex').should.equal(m128.toString('hex')); + }); + + it('should encrypt/decrypt with a 256 bit key', function() { + var encbuf = AES.encrypt(m128, k256); + var m = AES.decrypt(encbuf, k256); + m.toString('hex').should.equal(m128.toString('hex')); + }); + + }); + + describe('@buf2words', function() { + + it('should convert this 4 length buffer into an array', function() { + var buf = new Buffer([0, 0, 0, 0]); + var words = AES.buf2words(buf); + words.length.should.equal(1); + }); + + it('should throw an error on this 5 length buffer', function() { + var buf = new Buffer([0, 0, 0, 0, 0]); + (function() { + var words = AES.buf2words(buf); + }).should.throw(); + }); + + }); + + describe('@words2buf', function() { + + it('should convert this array into a buffer', function() { + var a = [100, 0]; + var buf = AES.words2buf(a); + buf.length.should.equal(8); + }); + + }); + +});