2016-09-13 13:23:11 +02:00
|
|
|
/* eslint max-nested-callbacks: ["error", 8] */
|
2016-05-19 22:33:09 +02:00
|
|
|
/* eslint-env mocha */
|
|
|
|
'use strict'
|
|
|
|
|
2017-03-21 15:05:22 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
|
|
|
const expect = chai.expect
|
|
|
|
chai.use(dirtyChai)
|
2016-10-03 23:15:21 +11:00
|
|
|
const parallel = require('async/parallel')
|
|
|
|
|
2017-07-22 10:57:27 -07:00
|
|
|
const fixtures = require('../fixtures/go-elliptic-key')
|
|
|
|
const crypto = require('../../src')
|
2016-05-19 22:33:09 +02:00
|
|
|
|
2016-10-03 23:15:21 +11:00
|
|
|
const curves = ['P-256', 'P-384'] // 'P-521' fails in tests :( no clue why
|
|
|
|
const lengths = {
|
|
|
|
'P-256': 65,
|
|
|
|
'P-384': 97,
|
|
|
|
'P-521': 133
|
|
|
|
}
|
2016-11-29 16:36:56 +01:00
|
|
|
const secretLengths = {
|
|
|
|
'P-256': 32,
|
|
|
|
'P-384': 48,
|
|
|
|
'P-521': 66
|
|
|
|
}
|
2016-09-13 13:23:11 +02:00
|
|
|
|
2016-05-19 22:33:09 +02:00
|
|
|
describe('generateEphemeralKeyPair', () => {
|
2016-09-13 13:23:11 +02:00
|
|
|
curves.forEach((curve) => {
|
|
|
|
it(`generate and shared key ${curve}`, (done) => {
|
2016-10-03 23:15:21 +11:00
|
|
|
parallel([
|
2017-07-22 10:57:27 -07:00
|
|
|
(cb) => crypto.keys.generateEphemeralKeyPair(curve, cb),
|
|
|
|
(cb) => crypto.keys.generateEphemeralKeyPair(curve, cb)
|
2016-10-03 23:15:21 +11:00
|
|
|
], (err, keys) => {
|
2017-03-21 15:05:22 +00:00
|
|
|
expect(err).to.not.exist()
|
2016-10-03 23:15:21 +11:00
|
|
|
expect(keys[0].key).to.have.length(lengths[curve])
|
|
|
|
expect(keys[1].key).to.have.length(lengths[curve])
|
|
|
|
|
|
|
|
keys[0].genSharedKey(keys[1].key, (err, shared) => {
|
2017-03-21 15:05:22 +00:00
|
|
|
expect(err).to.not.exist()
|
2016-11-29 16:36:56 +01:00
|
|
|
expect(shared).to.have.length(secretLengths[curve])
|
2016-10-03 23:15:21 +11:00
|
|
|
done()
|
2016-09-13 13:23:11 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-05-19 22:33:09 +02:00
|
|
|
})
|
2016-05-20 15:13:56 +02:00
|
|
|
|
2016-10-03 23:15:21 +11:00
|
|
|
describe('go interop', () => {
|
2016-09-13 13:23:11 +02:00
|
|
|
it('generates a shared secret', (done) => {
|
2016-05-20 15:13:56 +02:00
|
|
|
const curve = fixtures.curve
|
|
|
|
|
2016-11-29 16:36:56 +01:00
|
|
|
parallel([
|
2017-07-22 10:57:27 -07:00
|
|
|
(cb) => crypto.keys.generateEphemeralKeyPair(curve, cb),
|
|
|
|
(cb) => crypto.keys.generateEphemeralKeyPair(curve, cb)
|
2016-11-29 16:36:56 +01:00
|
|
|
], (err, res) => {
|
2017-03-21 15:05:22 +00:00
|
|
|
expect(err).to.not.exist()
|
2016-11-29 16:36:56 +01:00
|
|
|
const alice = res[0]
|
|
|
|
const bob = res[1]
|
|
|
|
bob.key = fixtures.bob.public
|
2016-10-03 23:15:21 +11:00
|
|
|
|
2016-11-29 16:36:56 +01:00
|
|
|
parallel([
|
|
|
|
(cb) => alice.genSharedKey(bob.key, cb),
|
|
|
|
(cb) => bob.genSharedKey(alice.key, fixtures.bob, cb)
|
|
|
|
], (err, secrets) => {
|
2017-03-21 15:05:22 +00:00
|
|
|
expect(err).to.not.exist()
|
2016-11-29 16:36:56 +01:00
|
|
|
|
2017-07-22 10:57:27 -07:00
|
|
|
expect(secrets[0]).to.eql(secrets[1])
|
2016-11-29 16:36:56 +01:00
|
|
|
expect(secrets[0]).to.have.length(32)
|
|
|
|
|
2016-10-03 23:15:21 +11:00
|
|
|
done()
|
2016-09-13 13:23:11 +02:00
|
|
|
})
|
2016-10-03 23:15:21 +11:00
|
|
|
})
|
2016-05-20 15:13:56 +02:00
|
|
|
})
|
|
|
|
})
|
2016-05-19 22:33:09 +02:00
|
|
|
})
|