2016-09-08 15:28:50 +02:00
|
|
|
/* eslint max-nested-callbacks: ["error", 8] */
|
2016-05-20 12:50:16 +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)
|
2019-07-22 06:16:02 -04:00
|
|
|
const { expectErrCode } = require('../util')
|
2017-07-22 10:57:27 -07:00
|
|
|
const crypto = require('../../src')
|
|
|
|
const fixtures = require('../fixtures/go-stretch-key')
|
2016-05-20 12:50:16 +02:00
|
|
|
|
|
|
|
describe('keyStretcher', () => {
|
|
|
|
describe('generate', () => {
|
|
|
|
const ciphers = ['AES-128', 'AES-256', 'Blowfish']
|
2016-05-20 16:27:11 +02:00
|
|
|
const hashes = ['SHA1', 'SHA256', 'SHA512']
|
2016-09-13 13:23:11 +02:00
|
|
|
let res
|
2020-01-17 03:04:52 -08:00
|
|
|
// @ts-check
|
|
|
|
/**
|
2020-08-07 15:23:02 +01:00
|
|
|
* @type {Uint8Array}
|
2020-01-17 03:04:52 -08:00
|
|
|
*/
|
2016-09-13 13:23:11 +02:00
|
|
|
let secret
|
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
before(async () => {
|
|
|
|
res = await crypto.keys.generateEphemeralKeyPair('P-256')
|
|
|
|
secret = await res.genSharedKey(res.key)
|
2016-09-13 13:23:11 +02:00
|
|
|
})
|
2016-05-20 12:50:16 +02:00
|
|
|
|
|
|
|
ciphers.forEach((cipher) => {
|
|
|
|
hashes.forEach((hash) => {
|
2019-07-10 17:15:26 +01:00
|
|
|
it(`${cipher} - ${hash}`, async () => {
|
|
|
|
const keys = await crypto.keys.keyStretcher(cipher, hash, secret)
|
|
|
|
expect(keys.k1).to.exist()
|
|
|
|
expect(keys.k2).to.exist()
|
2016-05-20 12:50:16 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-07-22 06:16:02 -04:00
|
|
|
|
|
|
|
it('handles invalid cipher type', () => {
|
|
|
|
return expectErrCode(crypto.keys.keyStretcher('invalid-cipher', 'SHA256', 'secret'), 'ERR_INVALID_CIPHER_TYPE')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handles missing hash type', () => {
|
|
|
|
return expectErrCode(crypto.keys.keyStretcher('AES-128', '', 'secret'), 'ERR_MISSING_HASH_TYPE')
|
|
|
|
})
|
2016-05-20 12:50:16 +02:00
|
|
|
})
|
2016-05-20 15:55:19 +02:00
|
|
|
|
|
|
|
describe('go interop', () => {
|
|
|
|
fixtures.forEach((test) => {
|
2019-07-10 17:15:26 +01:00
|
|
|
it(`${test.cipher} - ${test.hash}`, async () => {
|
2016-05-20 15:55:19 +02:00
|
|
|
const cipher = test.cipher
|
|
|
|
const hash = test.hash
|
|
|
|
const secret = test.secret
|
2019-07-10 17:15:26 +01:00
|
|
|
const keys = await crypto.keys.keyStretcher(cipher, hash, secret)
|
2016-05-20 15:55:19 +02:00
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
expect(keys.k1.iv).to.be.eql(test.k1.iv)
|
|
|
|
expect(keys.k1.cipherKey).to.be.eql(test.k1.cipherKey)
|
|
|
|
expect(keys.k1.macKey).to.be.eql(test.k1.macKey)
|
2016-05-20 15:55:19 +02:00
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
expect(keys.k2.iv).to.be.eql(test.k2.iv)
|
|
|
|
expect(keys.k2.cipherKey).to.be.eql(test.k2.cipherKey)
|
|
|
|
expect(keys.k2.macKey).to.be.eql(test.k2.macKey)
|
2016-05-20 15:55:19 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-05-20 12:50:16 +02:00
|
|
|
})
|