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
|
|
|
|
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
|
2020-01-17 03:04:52 -08:00
|
|
|
// @ts-check
|
|
|
|
/**
|
|
|
|
* @type {Record<string, number>}
|
|
|
|
*/
|
2016-10-03 23:15:21 +11:00
|
|
|
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) => {
|
2019-07-10 17:15:26 +01:00
|
|
|
it(`generate and shared key ${curve}`, async () => {
|
|
|
|
const keys = await Promise.all([
|
|
|
|
crypto.keys.generateEphemeralKeyPair(curve),
|
|
|
|
crypto.keys.generateEphemeralKeyPair(curve)
|
|
|
|
])
|
2016-10-03 23:15:21 +11:00
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
expect(keys[0].key).to.have.length(lengths[curve])
|
|
|
|
expect(keys[1].key).to.have.length(lengths[curve])
|
|
|
|
|
|
|
|
const shared = await keys[0].genSharedKey(keys[1].key)
|
|
|
|
expect(shared).to.have.length(secretLengths[curve])
|
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', () => {
|
2019-07-10 17:15:26 +01:00
|
|
|
it('generates a shared secret', async () => {
|
2016-05-20 15:13:56 +02:00
|
|
|
const curve = fixtures.curve
|
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
const keys = await Promise.all([
|
|
|
|
crypto.keys.generateEphemeralKeyPair(curve),
|
|
|
|
crypto.keys.generateEphemeralKeyPair(curve)
|
|
|
|
])
|
2016-10-03 23:15:21 +11:00
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
const alice = keys[0]
|
|
|
|
const bob = keys[1]
|
|
|
|
bob.key = fixtures.bob.public
|
2016-11-29 16:36:56 +01:00
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
const secrets = await Promise.all([
|
|
|
|
alice.genSharedKey(bob.key),
|
|
|
|
bob.genSharedKey(alice.key, fixtures.bob)
|
|
|
|
])
|
2016-11-29 16:36:56 +01:00
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
expect(secrets[0]).to.eql(secrets[1])
|
|
|
|
expect(secrets[0]).to.have.length(32)
|
2016-05-20 15:13:56 +02:00
|
|
|
})
|
|
|
|
})
|
2019-07-22 06:16:02 -04:00
|
|
|
|
2019-10-24 18:16:41 +02:00
|
|
|
it('handles bad curve name', async () => {
|
2019-07-22 06:16:02 -04:00
|
|
|
try {
|
|
|
|
await crypto.keys.generateEphemeralKeyPair('bad name')
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.code).equals('ERR_INVALID_CURVE')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
expect.fail('Did not throw error')
|
|
|
|
})
|
2016-05-19 22:33:09 +02:00
|
|
|
})
|