Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #29 from libp2p/aes-interop
Browse files Browse the repository at this point in the history
AES Interop
  • Loading branch information
daviddias authored Nov 11, 2016
2 parents 3cc26d1 + 9994023 commit 22e95bc
Show file tree
Hide file tree
Showing 18 changed files with 2,276 additions and 66 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -66,4 +67,4 @@
"greenkeeperio-bot <[email protected]>",
"nikuda <[email protected]>"
]
}
}
52 changes: 0 additions & 52 deletions src/crypto/aes-browser.js

This file was deleted.

12 changes: 6 additions & 6 deletions src/crypto/aes.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions src/crypto/ciphers-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

const crypto = require('browserify-aes')

module.exports = {
createCipheriv: crypto.createCipheriv,
createDecipheriv: crypto.createDecipheriv
}
8 changes: 8 additions & 0 deletions src/crypto/ciphers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

const crypto = require('crypto')

module.exports = {
createCipheriv: crypto.createCipheriv,
createDecipheriv: crypto.createDecipheriv
}
90 changes: 84 additions & 6 deletions test/aes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
'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',
Expand All @@ -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()
})
})
}
}
27 changes: 27 additions & 0 deletions test/fixtures/aes.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading

0 comments on commit 22e95bc

Please sign in to comment.