From 393fa1751214894af04de0a7261f57b06d083ca4 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Thu, 10 Nov 2016 12:55:49 +0100 Subject: [PATCH 1/2] test(aes): add failing interop tests --- test/aes.spec.js | 92 +++++++++++++++-- test/fixtures/aes.js | 27 +++++ test/fixtures/fix1.json | 212 +++++++++++++++++++++++++++++++++++++++ test/fixtures/fix10.json | 212 +++++++++++++++++++++++++++++++++++++++ test/fixtures/fix2.json | 212 +++++++++++++++++++++++++++++++++++++++ test/fixtures/fix3.json | 212 +++++++++++++++++++++++++++++++++++++++ test/fixtures/fix4.json | 212 +++++++++++++++++++++++++++++++++++++++ test/fixtures/fix5.json | 212 +++++++++++++++++++++++++++++++++++++++ test/fixtures/fix6.json | 212 +++++++++++++++++++++++++++++++++++++++ test/fixtures/fix7.json | 212 +++++++++++++++++++++++++++++++++++++++ test/fixtures/fix8.json | 212 +++++++++++++++++++++++++++++++++++++++ test/fixtures/fix9.json | 212 +++++++++++++++++++++++++++++++++++++++ test/fixtures/go-aes.js | 20 ++++ 13 files changed, 2252 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/aes.js create mode 100644 test/fixtures/fix1.json create mode 100644 test/fixtures/fix10.json create mode 100644 test/fixtures/fix2.json create mode 100644 test/fixtures/fix3.json create mode 100644 test/fixtures/fix4.json create mode 100644 test/fixtures/fix5.json create mode 100644 test/fixtures/fix6.json create mode 100644 test/fixtures/fix7.json create mode 100644 test/fixtures/fix8.json create mode 100644 test/fixtures/fix9.json create mode 100644 test/fixtures/go-aes.js diff --git a/test/aes.spec.js b/test/aes.spec.js index eeaec72e..3579a680 100644 --- a/test/aes.spec.js +++ b/test/aes.spec.js @@ -3,14 +3,18 @@ 'use strict' const expect = require('chai').expect +const series = require('async/series') + const crypto = require('../src') +const fixtures = require('./fixtures/aes') +const goFixtures = require('./fixtures/go-aes') const bytes = { 16: 'AES-128', 32: 'AES-256' } -describe('AES-CTR', () => { +describe.only('AES-CTR', () => { Object.keys(bytes).forEach((byte) => { it(`${bytes[byte]} - encrypt and decrypt`, (done) => { const key = new Buffer(parseInt(byte, 10)) @@ -22,16 +26,90 @@ describe('AES-CTR', () => { crypto.aes.create(key, iv, (err, cipher) => { expect(err).to.not.exist - cipher.encrypt(new Buffer('hello'), (err, res) => { - expect(err).to.not.exist + series([ + encryptAndDecrypt(cipher), + encryptAndDecrypt(cipher), + encryptAndDecrypt(cipher), + encryptAndDecrypt(cipher), + encryptAndDecrypt(cipher) + ], done) + }) + }) + }) + Object.keys(bytes).forEach((byte) => { + it(`${bytes[byte]} - fixed - encrypt and decrypt`, (done) => { + const key = new Buffer(parseInt(byte, 10)) + key.fill(5) + + const iv = new Buffer(16) + iv.fill(1) + + crypto.aes.create(key, iv, (err, cipher) => { + expect(err).to.not.exist - cipher.decrypt(res, (err, res) => { + series(fixtures[byte].inputs.map((rawIn, i) => (cb) => { + const input = new Buffer(rawIn) + const output = new Buffer(fixtures[byte].outputs[i]) + cipher.encrypt(input, (err, res) => { expect(err).to.not.exist - expect(res).to.be.eql(new Buffer('hello')) - done() + expect(res).to.have.length(output.length) + expect(res).to.be.eql(output) + cipher.decrypt(res, (err, res) => { + expect(err).to.not.exist + expect(res).to.be.eql(input) + cb() + }) }) - }) + }), done) + }) + }) + }) + + Object.keys(bytes).forEach((byte) => { + if (!goFixtures[byte]) { + return + } + + it(`${bytes[byte]} - go interop - encrypt and decrypt`, (done) => { + const key = new Buffer(parseInt(byte, 10)) + key.fill(5) + + const iv = new Buffer(16) + iv.fill(1) + + crypto.aes.create(key, iv, (err, cipher) => { + expect(err).to.not.exist + + series(goFixtures[byte].inputs.map((rawIn, i) => (cb) => { + const input = new Buffer(rawIn) + const output = new Buffer(goFixtures[byte].outputs[i]) + cipher.encrypt(input, (err, res) => { + expect(err).to.not.exist + expect(res).to.have.length(output.length) + expect(res).to.be.eql(output) + cipher.decrypt(res, (err, res) => { + expect(err).to.not.exist + expect(res).to.be.eql(input) + cb() + }) + }) + }), done) }) }) }) }) + +function encryptAndDecrypt (cipher) { + const data = new Buffer(100) + data.fill(Math.ceil(Math.random() * 100)) + return (cb) => { + cipher.encrypt(data, (err, res) => { + expect(err).to.not.exist + cipher.decrypt(res, (err, res) => { + expect(err).to.not.exist + expect(res).to.be.eql(data) + cb() + }) + }) + } +} diff --git a/test/fixtures/aes.js b/test/fixtures/aes.js new file mode 100644 index 00000000..55b09f42 --- /dev/null +++ b/test/fixtures/aes.js @@ -0,0 +1,27 @@ +'use strict' + +const fixes16 = [ + require('./fix1.json'), + require('./fix2.json'), + require('./fix3.json'), + require('./fix4.json'), + require('./fix5.json') +] +const fixes32 = [ + require('./fix6.json'), + require('./fix7.json'), + require('./fix8.json'), + require('./fix9.json'), + require('./fix10.json') +] + +module.exports = { + 16: { + inputs: fixes16.map((f) => f.input), + outputs: fixes16.map((f) => f.output) + }, + 32: { + inputs: fixes32.map((f) => f.input), + outputs: fixes32.map((f) => f.output) + } +} diff --git a/test/fixtures/fix1.json b/test/fixtures/fix1.json new file mode 100644 index 00000000..a69a25b9 --- /dev/null +++ b/test/fixtures/fix1.json @@ -0,0 +1,212 @@ +{ + "input": { + "type": "Buffer", + "data": [ + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47, + 47 + ] + }, + "output": { + "type": "Buffer", + "data": [ + 121, + 104, + 0, + 151, + 57, + 137, + 22, + 239, + 234, + 151, + 58, + 15, + 100, + 22, + 228, + 110, + 85, + 248, + 249, + 15, + 145, + 128, + 223, + 25, + 192, + 175, + 132, + 169, + 98, + 203, + 231, + 106, + 224, + 102, + 206, + 244, + 29, + 213, + 36, + 2, + 26, + 213, + 94, + 29, + 134, + 219, + 136, + 73, + 212, + 176, + 33, + 95, + 198, + 91, + 148, + 139, + 132, + 252, + 182, + 115, + 116, + 160, + 146, + 194, + 0, + 97, + 181, + 0, + 193, + 149, + 21, + 51, + 248, + 97, + 32, + 62, + 86, + 153, + 238, + 67, + 38, + 34, + 55, + 143, + 70, + 193, + 99, + 107, + 31, + 67, + 90, + 97, + 55, + 63, + 69, + 203, + 33, + 233, + 74, + 237 + ] + } +} \ No newline at end of file diff --git a/test/fixtures/fix10.json b/test/fixtures/fix10.json new file mode 100644 index 00000000..eeb516f3 --- /dev/null +++ b/test/fixtures/fix10.json @@ -0,0 +1,212 @@ +{ + "input": { + "type": "Buffer", + "data": [ + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72, + 72 + ] + }, + "output": { + "type": "Buffer", + "data": [ + 201, + 224, + 56, + 64, + 167, + 224, + 124, + 49, + 19, + 51, + 208, + 226, + 216, + 50, + 12, + 73, + 15, + 255, + 156, + 108, + 98, + 179, + 144, + 110, + 33, + 151, + 43, + 64, + 227, + 153, + 120, + 39, + 21, + 151, + 41, + 61, + 245, + 20, + 31, + 23, + 109, + 213, + 109, + 55, + 35, + 40, + 122, + 109, + 41, + 10, + 32, + 176, + 25, + 184, + 91, + 176, + 177, + 134, + 138, + 252, + 160, + 2, + 108, + 43, + 222, + 239, + 174, + 2, + 145, + 74, + 34, + 131, + 237, + 214, + 235, + 102, + 26, + 204, + 124, + 64, + 101, + 134, + 186, + 45, + 55, + 29, + 52, + 55, + 171, + 95, + 172, + 86, + 242, + 101, + 141, + 153, + 222, + 161, + 128, + 83 + ] + } +} \ No newline at end of file diff --git a/test/fixtures/fix2.json b/test/fixtures/fix2.json new file mode 100644 index 00000000..725591d0 --- /dev/null +++ b/test/fixtures/fix2.json @@ -0,0 +1,212 @@ +{ + "input": { + "type": "Buffer", + "data": [ + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24, + 24 + ] + }, + "output": { + "type": "Buffer", + "data": [ + 109, + 172, + 127, + 179, + 110, + 222, + 79, + 219, + 87, + 76, + 70, + 204, + 166, + 110, + 184, + 229, + 90, + 49, + 160, + 252, + 78, + 58, + 73, + 51, + 152, + 218, + 3, + 200, + 10, + 124, + 152, + 117, + 137, + 40, + 23, + 127, + 56, + 57, + 203, + 31, + 101, + 227, + 31, + 198, + 223, + 86, + 98, + 120, + 100, + 86, + 116, + 144, + 142, + 127, + 68, + 175, + 249, + 232, + 22, + 83, + 22, + 68, + 60, + 230, + 146, + 22, + 153, + 193, + 67, + 5, + 51, + 253, + 239, + 210, + 80, + 31, + 254, + 103, + 185, + 145, + 123, + 99, + 205, + 175, + 156, + 144, + 191, + 67, + 31, + 236, + 43, + 98, + 197, + 235, + 31, + 50, + 69, + 228, + 100, + 64 + ] + } +} \ No newline at end of file diff --git a/test/fixtures/fix3.json b/test/fixtures/fix3.json new file mode 100644 index 00000000..e0022d4d --- /dev/null +++ b/test/fixtures/fix3.json @@ -0,0 +1,212 @@ +{ + "input": { + "type": "Buffer", + "data": [ + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + 7 + ] + }, + "output": { + "type": "Buffer", + "data": [ + 191, + 93, + 126, + 191, + 180, + 42, + 118, + 144, + 28, + 188, + 211, + 191, + 211, + 130, + 170, + 153, + 134, + 240, + 179, + 83, + 75, + 23, + 42, + 68, + 158, + 200, + 123, + 155, + 57, + 169, + 152, + 133, + 33, + 114, + 90, + 29, + 131, + 91, + 70, + 105, + 83, + 45, + 40, + 47, + 77, + 96, + 97, + 8, + 40, + 1, + 110, + 245, + 106, + 172, + 152, + 146, + 5, + 114, + 132, + 0, + 179, + 31, + 44, + 78, + 19, + 109, + 92, + 199, + 226, + 36, + 12, + 74, + 180, + 241, + 224, + 107, + 83, + 13, + 167, + 27, + 251, + 101, + 193, + 98, + 49, + 90, + 225, + 197, + 75, + 213, + 144, + 52, + 235, + 130, + 92, + 247, + 219, + 139, + 209, + 132 + ] + } +} \ No newline at end of file diff --git a/test/fixtures/fix4.json b/test/fixtures/fix4.json new file mode 100644 index 00000000..03534e35 --- /dev/null +++ b/test/fixtures/fix4.json @@ -0,0 +1,212 @@ +{ + "input": { + "type": "Buffer", + "data": [ + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25, + 25 + ] + }, + "output": { + "type": "Buffer", + "data": [ + 191, + 110, + 53, + 190, + 250, + 44, + 190, + 136, + 22, + 209, + 131, + 200, + 112, + 31, + 61, + 183, + 247, + 95, + 4, + 249, + 69, + 147, + 238, + 74, + 18, + 71, + 197, + 115, + 141, + 226, + 102, + 92, + 91, + 128, + 181, + 172, + 67, + 109, + 17, + 165, + 52, + 10, + 55, + 31, + 55, + 225, + 253, + 140, + 154, + 35, + 104, + 62, + 119, + 103, + 197, + 152, + 125, + 134, + 140, + 181, + 170, + 76, + 75, + 114, + 195, + 188, + 68, + 197, + 28, + 47, + 116, + 82, + 34, + 128, + 232, + 122, + 14, + 229, + 122, + 161, + 36, + 212, + 161, + 164, + 145, + 86, + 215, + 233, + 222, + 50, + 143, + 89, + 131, + 32, + 130, + 196, + 109, + 36, + 204, + 254 + ] + } +} \ No newline at end of file diff --git a/test/fixtures/fix5.json b/test/fixtures/fix5.json new file mode 100644 index 00000000..d76ed7a9 --- /dev/null +++ b/test/fixtures/fix5.json @@ -0,0 +1,212 @@ +{ + "input": { + "type": "Buffer", + "data": [ + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48, + 48 + ] + }, + "output": { + "type": "Buffer", + "data": [ + 97, + 142, + 8, + 37, + 69, + 173, + 124, + 180, + 97, + 70, + 45, + 91, + 196, + 126, + 184, + 135, + 213, + 104, + 171, + 89, + 231, + 63, + 43, + 42, + 7, + 245, + 75, + 165, + 205, + 182, + 24, + 50, + 170, + 217, + 128, + 112, + 114, + 215, + 209, + 145, + 135, + 235, + 179, + 212, + 5, + 81, + 142, + 199, + 53, + 221, + 39, + 239, + 167, + 21, + 237, + 168, + 145, + 249, + 250, + 108, + 2, + 247, + 89, + 73, + 228, + 227, + 255, + 155, + 121, + 157, + 205, + 96, + 43, + 32, + 112, + 209, + 173, + 96, + 143, + 43, + 220, + 140, + 26, + 205, + 34, + 34, + 53, + 157, + 41, + 167, + 125, + 235, + 243, + 85, + 13, + 14, + 93, + 109, + 233, + 186 + ] + } +} \ No newline at end of file diff --git a/test/fixtures/fix6.json b/test/fixtures/fix6.json new file mode 100644 index 00000000..8b58e2de --- /dev/null +++ b/test/fixtures/fix6.json @@ -0,0 +1,212 @@ +{ + "input": { + "type": "Buffer", + "data": [ + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68, + 68 + ] + }, + "output": { + "type": "Buffer", + "data": [ + 198, + 59, + 91, + 107, + 222, + 214, + 31, + 230, + 112, + 1, + 199, + 174, + 17, + 224, + 194, + 225, + 138, + 65, + 58, + 160, + 31, + 249, + 102, + 16, + 74, + 46, + 85, + 30, + 232, + 249, + 120, + 108, + 38, + 165, + 105, + 27, + 12, + 98, + 98, + 108, + 130, + 163, + 16, + 137, + 116, + 104, + 153, + 188, + 5, + 251, + 163, + 244, + 0, + 58, + 1, + 207, + 25, + 78, + 188, + 210, + 205, + 221, + 48, + 132, + 72, + 209, + 11, + 67, + 14, + 42, + 218, + 71, + 148, + 252, + 126, + 183, + 60, + 32, + 93, + 36, + 125, + 103, + 191, + 117, + 82, + 241, + 190, + 176, + 1, + 129, + 118, + 112, + 139, + 153, + 178, + 56, + 61, + 91, + 52, + 198 + ] + } +} \ No newline at end of file diff --git a/test/fixtures/fix7.json b/test/fixtures/fix7.json new file mode 100644 index 00000000..1a052c5a --- /dev/null +++ b/test/fixtures/fix7.json @@ -0,0 +1,212 @@ +{ + "input": { + "type": "Buffer", + "data": [ + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10 + ] + }, + "output": { + "type": "Buffer", + "data": [ + 206, + 241, + 99, + 120, + 248, + 163, + 57, + 30, + 216, + 86, + 255, + 192, + 89, + 193, + 176, + 17, + 78, + 62, + 80, + 149, + 189, + 235, + 27, + 157, + 175, + 248, + 7, + 19, + 222, + 64, + 111, + 199, + 204, + 163, + 26, + 16, + 95, + 221, + 70, + 32, + 239, + 4, + 58, + 162, + 253, + 237, + 8, + 51, + 94, + 3, + 165, + 186, + 223, + 210, + 116, + 101, + 228, + 82, + 103, + 76, + 74, + 44, + 51, + 117, + 189, + 140, + 132, + 230, + 188, + 243, + 24, + 158, + 149, + 93, + 147, + 226, + 113, + 195, + 31, + 24, + 9, + 19, + 27, + 132, + 180, + 152, + 26, + 65, + 125, + 144, + 86, + 167, + 41, + 144, + 158, + 204, + 76, + 46, + 181, + 110 + ] + } +} \ No newline at end of file diff --git a/test/fixtures/fix8.json b/test/fixtures/fix8.json new file mode 100644 index 00000000..d87f819b --- /dev/null +++ b/test/fixtures/fix8.json @@ -0,0 +1,212 @@ +{ + "input": { + "type": "Buffer", + "data": [ + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75, + 75 + ] + }, + "output": { + "type": "Buffer", + "data": [ + 153, + 161, + 201, + 66, + 87, + 46, + 242, + 87, + 136, + 238, + 151, + 198, + 129, + 122, + 62, + 205, + 142, + 252, + 109, + 187, + 238, + 183, + 63, + 95, + 240, + 97, + 220, + 209, + 37, + 144, + 237, + 84, + 251, + 149, + 152, + 222, + 89, + 200, + 208, + 119, + 213, + 38, + 65, + 19, + 2, + 252, + 193, + 125, + 94, + 76, + 182, + 198, + 243, + 121, + 253, + 16, + 45, + 254, + 82, + 47, + 146, + 206, + 41, + 105, + 254, + 237, + 236, + 141, + 118, + 214, + 197, + 228, + 59, + 125, + 45, + 200, + 61, + 24, + 110, + 26, + 235, + 92, + 175, + 255, + 85, + 119, + 57, + 160, + 145, + 107, + 206, + 28, + 62, + 10, + 166, + 89, + 7, + 20, + 246, + 243 + ] + } +} \ No newline at end of file diff --git a/test/fixtures/fix9.json b/test/fixtures/fix9.json new file mode 100644 index 00000000..cb9a9be0 --- /dev/null +++ b/test/fixtures/fix9.json @@ -0,0 +1,212 @@ +{ + "input": { + "type": "Buffer", + "data": [ + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35, + 35 + ] + }, + "output": { + "type": "Buffer", + "data": [ + 79, + 215, + 22, + 29, + 132, + 130, + 214, + 248, + 237, + 124, + 234, + 54, + 215, + 78, + 0, + 211, + 246, + 222, + 54, + 91, + 53, + 2, + 49, + 40, + 0, + 202, + 188, + 25, + 184, + 121, + 164, + 235, + 189, + 97, + 14, + 0, + 83, + 166, + 88, + 62, + 55, + 49, + 79, + 153, + 136, + 193, + 133, + 206, + 172, + 99, + 207, + 73, + 246, + 216, + 192, + 107, + 50, + 206, + 167, + 242, + 25, + 180, + 63, + 184, + 201, + 61, + 90, + 242, + 223, + 192, + 13, + 140, + 31, + 240, + 112, + 157, + 250, + 90, + 142, + 3, + 40, + 12, + 106, + 63, + 73, + 42, + 79, + 82, + 20, + 41, + 187, + 173, + 23, + 85, + 59, + 253, + 212, + 191, + 109, + 46 + ] + } +} \ No newline at end of file diff --git a/test/fixtures/go-aes.js b/test/fixtures/go-aes.js new file mode 100644 index 00000000..2970be29 --- /dev/null +++ b/test/fixtures/go-aes.js @@ -0,0 +1,20 @@ +'use strict' + +module.exports = { + 16: { + inputs: [ + [47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47], + [24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24], + [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], + [25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25], + [48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48] + ], + outputs: [ + [121, 104, 0, 151, 57, 137, 22, 239, 234, 151, 58, 15, 100, 22, 228, 110, 85, 248, 249, 15, 145, 128, 223, 25, 192, 175, 132, 169, 98, 203, 231, 106, 224, 102, 206, 244, 29, 213, 36, 2, 26, 213, 94, 29, 134, 219, 136, 73, 212, 176, 33, 95, 198, 91, 148, 139, 132, 252, 182, 115, 116, 160, 146, 194, 0, 97, 181, 0, 193, 149, 21, 51, 248, 97, 32, 62, 86, 153, 238, 67, 38, 34, 55, 143, 70, 193, 99, 107, 31, 67, 90, 97, 55, 63, 69, 203, 33, 233, 74, 237], + [109, 172, 127, 179, 110, 222, 79, 219, 87, 76, 70, 204, 166, 110, 184, 229, 90, 49, 160, 252, 78, 58, 73, 51, 152, 218, 3, 200, 10, 124, 152, 117, 137, 40, 23, 127, 56, 57, 203, 31, 101, 227, 31, 198, 223, 86, 98, 120, 100, 86, 116, 144, 142, 127, 68, 175, 249, 232, 22, 83, 22, 68, 60, 230, 146, 22, 153, 193, 67, 5, 51, 253, 239, 210, 80, 31, 254, 103, 185, 145, 123, 99, 205, 175, 156, 144, 191, 67, 31, 236, 43, 98, 197, 235, 31, 50, 69, 228, 100, 64], + [191, 93, 126, 191, 180, 42, 118, 144, 28, 188, 211, 191, 211, 130, 170, 153, 134, 240, 179, 83, 75, 23, 42, 68, 158, 200, 123, 155, 57, 169, 152, 133, 33, 114, 90, 29, 131, 91, 70, 105, 83, 45, 40, 47, 77, 96, 97, 8, 40, 1, 110, 245, 106, 172, 152, 146, 5, 114, 132, 0, 179, 31, 44, 78, 19, 109, 92, 199, 226, 36, 12, 74, 180, 241, 224, 107, 83, 13, 167, 27, 251, 101, 193, 98, 49, 90, 225, 197, 75, 213, 144, 52, 235, 130, 92, 247, 219, 139, 209, 132], + [191, 110, 53, 190, 250, 44, 190, 136, 22, 209, 131, 200, 112, 31, 61, 183, 247, 95, 4, 249, 69, 147, 238, 74, 18, 71, 197, 115, 141, 226, 102, 92, 91, 128, 181, 172, 67, 109, 17, 165, 52, 10, 55, 31, 55, 225, 253, 140, 154, 35, 104, 62, 119, 103, 197, 152, 125, 134, 140, 181, 170, 76, 75, 114, 195, 188, 68, 197, 28, 47, 116, 82, 34, 128, 232, 122, 14, 229, 122, 161, 36, 212, 161, 164, 145, 86, 215, 233, 222, 50, 143, 89, 131, 32, 130, 196, 109, 36, 204, 254], + [97, 142, 8, 37, 69, 173, 124, 180, 97, 70, 45, 91, 196, 126, 184, 135, 213, 104, 171, 89, 231, 63, 43, 42, 7, 245, 75, 165, 205, 182, 24, 50, 170, 217, 128, 112, 114, 215, 209, 145, 135, 235, 179, 212, 5, 81, 142, 199, 53, 221, 39, 239, 167, 21, 237, 168, 145, 249, 250, 108, 2, 247, 89, 73, 228, 227, 255, 155, 121, 157, 205, 96, 43, 32, 112, 209, 173, 96, 143, 43, 220, 140, 26, 205, 34, 34, 53, 157, 41, 167, 125, 235, 243, 85, 13, 14, 93, 109, 233, 186] + ] + } +} From 99940234909aaa87b6d52aec2b9981e841af47ab Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Thu, 10 Nov 2016 17:19:45 +0100 Subject: [PATCH 2/2] fix(aes): replace subtle.crypto with browserify-aes Due to the design of `AES-CTR` in the webcrypto spec, there is no streaming mode provided. This results in the counter not being reused between subsequent calls to `encrypt` or `decrypt`. As both the node.js and the go implementation rely on this webcrypto had to be replaced. --- package.json | 5 ++-- src/crypto/aes-browser.js | 52 ----------------------------------- src/crypto/aes.js | 12 ++++---- src/crypto/ciphers-browser.js | 8 ++++++ src/crypto/ciphers.js | 8 ++++++ test/aes.spec.js | 2 +- 6 files changed, 26 insertions(+), 61 deletions(-) delete mode 100644 src/crypto/aes-browser.js create mode 100644 src/crypto/ciphers-browser.js create mode 100644 src/crypto/ciphers.js diff --git a/package.json b/package.json index ada18b72..c4feee24 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "node-webcrypto-ossl": false, "./src/crypto/webcrypto.js": "./src/crypto/webcrypto-browser.js", "./src/crypto/hmac.js": "./src/crypto/hmac-browser.js", - "./src/crypto/aes.js": "./src/crypto/aes-browser.js" + "./src/crypto/ciphers.js": "./src/crypto/ciphers-browser.js" }, "scripts": { "lint": "aegir-lint", @@ -32,6 +32,7 @@ "dependencies": { "asn1.js": "^4.8.1", "async": "^2.1.2", + "browserify-aes": "^1.0.6", "multihashing-async": "^0.2.0", "node-webcrypto-ossl": "^1.0.7", "nodeify": "^1.0.0", @@ -66,4 +67,4 @@ "greenkeeperio-bot ", "nikuda " ] -} \ No newline at end of file +} diff --git a/src/crypto/aes-browser.js b/src/crypto/aes-browser.js deleted file mode 100644 index 22de0cde..00000000 --- a/src/crypto/aes-browser.js +++ /dev/null @@ -1,52 +0,0 @@ -'use strict' - -const nodeify = require('nodeify') - -const crypto = require('./webcrypto')() - -exports.create = function (key, iv, callback) { - nodeify(crypto.subtle.importKey( - 'raw', - key, - { - name: 'AES-CTR' - }, - false, - ['encrypt', 'decrypt'] - ).then((key) => { - const counter = copy(iv) - - return { - encrypt (data, cb) { - nodeify(crypto.subtle.encrypt( - { - name: 'AES-CTR', - counter: counter, - length: 128 - }, - key, - data - ).then((raw) => Buffer.from(raw)), cb) - }, - - decrypt (data, cb) { - nodeify(crypto.subtle.decrypt( - { - name: 'AES-CTR', - counter: counter, - length: 128 - }, - key, - data - ).then((raw) => Buffer.from(raw)), cb) - } - } - }), callback) -} - -function copy (buf) { - const fresh = new Buffer(buf.length) - buf.copy(fresh) - - return fresh -} diff --git a/src/crypto/aes.js b/src/crypto/aes.js index 7326acfc..54005ff8 100644 --- a/src/crypto/aes.js +++ b/src/crypto/aes.js @@ -1,20 +1,20 @@ 'use strict' -const crypto = require('crypto') +const ciphers = require('./ciphers') -const ciphers = { +const CIPHER_MODES = { 16: 'aes-128-ctr', 32: 'aes-256-ctr' } exports.create = function (key, iv, callback) { - const name = ciphers[key.length] - if (!name) { + const mode = CIPHER_MODES[key.length] + if (!mode) { return callback(new Error('Invalid key length')) } - const cipher = crypto.createCipheriv(name, key, iv) - const decipher = crypto.createDecipheriv(name, key, iv) + const cipher = ciphers.createCipheriv(mode, key, iv) + const decipher = ciphers.createDecipheriv(mode, key, iv) const res = { encrypt (data, cb) { diff --git a/src/crypto/ciphers-browser.js b/src/crypto/ciphers-browser.js new file mode 100644 index 00000000..5aee05ae --- /dev/null +++ b/src/crypto/ciphers-browser.js @@ -0,0 +1,8 @@ +'use strict' + +const crypto = require('browserify-aes') + +module.exports = { + createCipheriv: crypto.createCipheriv, + createDecipheriv: crypto.createDecipheriv +} diff --git a/src/crypto/ciphers.js b/src/crypto/ciphers.js new file mode 100644 index 00000000..efcd70f5 --- /dev/null +++ b/src/crypto/ciphers.js @@ -0,0 +1,8 @@ +'use strict' + +const crypto = require('crypto') + +module.exports = { + createCipheriv: crypto.createCipheriv, + createDecipheriv: crypto.createDecipheriv +} diff --git a/test/aes.spec.js b/test/aes.spec.js index 3579a680..6adc1197 100644 --- a/test/aes.spec.js +++ b/test/aes.spec.js @@ -14,7 +14,7 @@ const bytes = { 32: 'AES-256' } -describe.only('AES-CTR', () => { +describe('AES-CTR', () => { Object.keys(bytes).forEach((byte) => { it(`${bytes[byte]} - encrypt and decrypt`, (done) => { const key = new Buffer(parseInt(byte, 10))