js-libp2p-crypto/test/keys/key-stretcher.spec.js

67 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

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)
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']
const hashes = ['SHA1', 'SHA256', 'SHA512']
let res
// @ts-check
/**
* @type {Uint8Array}
*/
let secret
before(async () => {
res = await crypto.keys.generateEphemeralKeyPair('P-256')
secret = await res.genSharedKey(res.key)
})
2016-05-20 12:50:16 +02:00
ciphers.forEach((cipher) => {
hashes.forEach((hash) => {
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
})
})
})
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) => {
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
const keys = await crypto.keys.keyStretcher(cipher, hash, secret)
2016-05-20 15:55:19 +02: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
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
})