2016-09-13 13:23:11 +02:00
|
|
|
/* eslint max-nested-callbacks: ["error", 8] */
|
2016-05-19 18:47:48 +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-05-19 18:47:48 +02:00
|
|
|
const crypto = require('../src')
|
2016-05-20 14:41:25 +02:00
|
|
|
const fixtures = require('./fixtures/go-key-rsa')
|
2019-07-22 06:16:02 -04:00
|
|
|
const { expectErrCode } = require('./util')
|
2020-08-07 15:23:02 +01:00
|
|
|
const uint8ArrayEquals = require('uint8arrays/equals')
|
2016-05-19 18:47:48 +02:00
|
|
|
|
2020-01-17 03:04:52 -08:00
|
|
|
/** @typedef {import("libp2p-crypto").PrivateKey} PrivateKey */
|
|
|
|
|
2017-12-01 09:36:29 +01:00
|
|
|
describe('libp2p-crypto', function () {
|
|
|
|
this.timeout(20 * 1000)
|
2020-01-17 03:04:52 -08:00
|
|
|
// @ts-check
|
|
|
|
/**
|
|
|
|
* @type {PrivateKey}
|
|
|
|
*/
|
2016-05-19 21:45:43 +02:00
|
|
|
let key
|
2019-07-10 17:15:26 +01:00
|
|
|
before(async () => {
|
|
|
|
key = await crypto.keys.generateKeyPair('RSA', 512)
|
2016-05-19 21:45:43 +02:00
|
|
|
})
|
2016-05-19 20:18:31 +02:00
|
|
|
|
2016-05-19 21:45:43 +02:00
|
|
|
it('marshalPublicKey and unmarshalPublicKey', () => {
|
2017-07-22 10:57:27 -07:00
|
|
|
const key2 = crypto.keys.unmarshalPublicKey(
|
|
|
|
crypto.keys.marshalPublicKey(key.public))
|
2016-05-19 20:18:31 +02:00
|
|
|
|
2016-05-19 21:45:43 +02:00
|
|
|
expect(key2.equals(key.public)).to.be.eql(true)
|
2017-02-04 04:23:38 -05:00
|
|
|
|
|
|
|
expect(() => {
|
2017-07-22 10:57:27 -07:00
|
|
|
crypto.keys.marshalPublicKey(key.public, 'invalid-key-type')
|
2017-02-04 04:23:38 -05:00
|
|
|
}).to.throw()
|
2016-05-19 21:45:43 +02:00
|
|
|
})
|
2016-05-19 20:18:31 +02:00
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
it('marshalPrivateKey and unmarshalPrivateKey', async () => {
|
2017-02-04 04:23:38 -05:00
|
|
|
expect(() => {
|
2017-07-22 10:57:27 -07:00
|
|
|
crypto.keys.marshalPrivateKey(key, 'invalid-key-type')
|
2017-02-04 04:23:38 -05:00
|
|
|
}).to.throw()
|
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
const key2 = await crypto.keys.unmarshalPrivateKey(crypto.keys.marshalPrivateKey(key))
|
2016-05-19 18:47:48 +02:00
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
expect(key2.equals(key)).to.be.eql(true)
|
|
|
|
expect(key2.public.equals(key.public)).to.be.eql(true)
|
2016-05-19 18:47:48 +02:00
|
|
|
})
|
2016-05-20 14:41:25 +02:00
|
|
|
|
2019-07-22 06:16:02 -04:00
|
|
|
it('generateKeyPair', () => {
|
|
|
|
return expectErrCode(crypto.keys.generateKeyPair('invalid-key-type', 512), 'ERR_UNSUPPORTED_KEY_TYPE')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('generateKeyPairFromSeed', () => {
|
|
|
|
var seed = crypto.randomBytes(32)
|
|
|
|
return expectErrCode(crypto.keys.generateKeyPairFromSeed('invalid-key-type', seed, 512), 'ERR_UNSUPPORTED_KEY_TYPE')
|
|
|
|
})
|
|
|
|
|
2016-09-13 13:23:11 +02:00
|
|
|
// marshalled keys seem to be slightly different
|
|
|
|
// unsure as to if this is just a difference in encoding
|
|
|
|
// or a bug
|
2016-05-20 14:41:25 +02:00
|
|
|
describe('go interop', () => {
|
2019-07-10 17:15:26 +01:00
|
|
|
it('unmarshals private key', async () => {
|
|
|
|
const key = await crypto.keys.unmarshalPrivateKey(fixtures.private.key)
|
|
|
|
const hash = fixtures.private.hash
|
|
|
|
expect(fixtures.private.key).to.eql(key.bytes)
|
|
|
|
const digest = await key.hash()
|
|
|
|
expect(digest).to.eql(hash)
|
2016-05-20 14:41:25 +02:00
|
|
|
})
|
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
it('unmarshals public key', async () => {
|
2017-07-22 10:57:27 -07:00
|
|
|
const key = crypto.keys.unmarshalPublicKey(fixtures.public.key)
|
2016-05-20 14:41:25 +02:00
|
|
|
const hash = fixtures.public.hash
|
2017-07-22 10:57:27 -07:00
|
|
|
expect(crypto.keys.marshalPublicKey(key)).to.eql(fixtures.public.key)
|
2019-07-10 17:15:26 +01:00
|
|
|
const digest = await key.hash()
|
|
|
|
expect(digest).to.eql(hash)
|
2016-05-20 14:41:25 +02:00
|
|
|
})
|
|
|
|
|
2019-07-10 17:15:26 +01:00
|
|
|
it('unmarshal -> marshal, private key', async () => {
|
|
|
|
const key = await crypto.keys.unmarshalPrivateKey(fixtures.private.key)
|
|
|
|
const marshalled = crypto.keys.marshalPrivateKey(key)
|
|
|
|
expect(marshalled).to.eql(fixtures.private.key)
|
2016-05-20 14:41:25 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('unmarshal -> marshal, public key', () => {
|
2017-07-22 10:57:27 -07:00
|
|
|
const key = crypto.keys.unmarshalPublicKey(fixtures.public.key)
|
|
|
|
const marshalled = crypto.keys.marshalPublicKey(key)
|
2020-08-07 15:23:02 +01:00
|
|
|
expect(uint8ArrayEquals(fixtures.public.key, marshalled)).to.eql(true)
|
2016-05-20 14:41:25 +02:00
|
|
|
})
|
2017-01-16 05:17:50 +01:00
|
|
|
})
|
2016-12-01 11:42:19 +00:00
|
|
|
|
2017-12-20 21:11:47 +13:00
|
|
|
describe('pbkdf2', () => {
|
|
|
|
it('generates a derived password using sha1', () => {
|
|
|
|
const p1 = crypto.pbkdf2('password', 'at least 16 character salt', 500, 512 / 8, 'sha1')
|
|
|
|
expect(p1).to.exist()
|
|
|
|
expect(p1).to.be.a('string')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('generates a derived password using sha2-512', () => {
|
|
|
|
const p1 = crypto.pbkdf2('password', 'at least 16 character salt', 500, 512 / 8, 'sha2-512')
|
|
|
|
expect(p1).to.exist()
|
|
|
|
expect(p1).to.be.a('string')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('generates the same derived password with the same options', () => {
|
|
|
|
const p1 = crypto.pbkdf2('password', 'at least 16 character salt', 10, 512 / 8, 'sha1')
|
|
|
|
const p2 = crypto.pbkdf2('password', 'at least 16 character salt', 10, 512 / 8, 'sha1')
|
|
|
|
const p3 = crypto.pbkdf2('password', 'at least 16 character salt', 11, 512 / 8, 'sha1')
|
|
|
|
expect(p2).to.equal(p1)
|
|
|
|
expect(p3).to.not.equal(p2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws on invalid hash name', () => {
|
2019-07-22 06:16:02 -04:00
|
|
|
const fn = () => crypto.pbkdf2('password', 'at least 16 character salt', 500, 512 / 8, 'shaX-xxx')
|
|
|
|
expect(fn).to.throw().with.property('code', 'ERR_UNSUPPORTED_HASH_TYPE')
|
2017-12-20 21:11:47 +13:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-01-16 05:17:50 +01:00
|
|
|
describe('randomBytes', () => {
|
2020-01-17 03:04:52 -08:00
|
|
|
it('throws with invalid number passed', () => {
|
2016-12-01 11:42:19 +00:00
|
|
|
expect(() => {
|
2020-01-17 03:04:52 -08:00
|
|
|
crypto.randomBytes(-1)
|
2017-02-04 04:23:38 -05:00
|
|
|
}).to.throw()
|
2016-12-01 11:42:19 +00:00
|
|
|
})
|
|
|
|
|
2017-01-16 05:17:50 +01:00
|
|
|
it('generates different random things', () => {
|
2016-12-01 11:42:19 +00:00
|
|
|
const buf1 = crypto.randomBytes(10)
|
|
|
|
expect(buf1.length).to.equal(10)
|
|
|
|
const buf2 = crypto.randomBytes(10)
|
|
|
|
expect(buf1).to.not.eql(buf2)
|
|
|
|
})
|
2016-05-20 14:41:25 +02:00
|
|
|
})
|
2016-05-19 18:47:48 +02:00
|
|
|
})
|