110 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-05-19 18:47:48 +02:00
'use strict'
2017-07-22 10:57:27 -07:00
const protobuf = require('protocol-buffers')
2017-07-22 13:25:15 -07:00
const keysPBM = protobuf(require('./keys.proto'))
2017-07-22 10:57:27 -07:00
exports = module.exports
2017-07-22 13:25:15 -07:00
const supportedKeys = {
rsa: require('./rsa-class'),
ed25519: require('./ed25519-class'),
secp256k1: require('libp2p-crypto-secp256k1')(keysPBM, require('../random-bytes'))
}
exports.supportedKeys = supportedKeys
exports.keysPBM = keysPBM
2017-07-22 10:57:27 -07:00
function isValidKeyType (keyType) {
2017-07-22 13:25:15 -07:00
const key = supportedKeys[keyType.toLowerCase()]
2017-07-22 10:57:27 -07:00
return key !== undefined
}
exports.keyStretcher = require('./key-stretcher')
exports.generateEphemeralKeyPair = require('./ephemeral-keys')
// Generates a keypair of the given type and bitsize
exports.generateKeyPair = (type, bits, cb) => {
2017-07-22 13:25:15 -07:00
let key = supportedKeys[type.toLowerCase()]
2017-07-22 10:57:27 -07:00
if (!key) {
return cb(new Error('invalid or unsupported key type'))
}
key.generateKeyPair(bits, cb)
}
// Generates a keypair of the given type and bitsize
// seed is a 32 byte uint8array
exports.generateKeyPairFromSeed = (type, seed, bits, cb) => {
2017-07-22 13:25:15 -07:00
let key = supportedKeys[type.toLowerCase()]
2017-07-22 10:57:27 -07:00
if (!key) {
return cb(new Error('invalid or unsupported key type'))
}
if (type.toLowerCase() !== 'ed25519') {
return cb(new Error('Seed key derivation is unimplemented for RSA or secp256k1'))
}
key.generateKeyPairFromSeed(seed, bits, cb)
}
// Converts a protobuf serialized public key into its
// representative object
exports.unmarshalPublicKey = (buf) => {
2017-07-22 13:25:15 -07:00
const decoded = keysPBM.PublicKey.decode(buf)
2017-07-22 10:57:27 -07:00
switch (decoded.Type) {
2017-07-22 13:25:15 -07:00
case keysPBM.KeyType.RSA:
return supportedKeys.rsa.unmarshalRsaPublicKey(decoded.Data)
case keysPBM.KeyType.Ed25519:
return supportedKeys.ed25519.unmarshalEd25519PublicKey(decoded.Data)
case keysPBM.KeyType.Secp256k1:
if (supportedKeys.secp256k1) {
return supportedKeys.secp256k1.unmarshalSecp256k1PublicKey(decoded.Data)
2017-07-22 10:57:27 -07:00
} else {
throw new Error('secp256k1 support requires libp2p-crypto-secp256k1 package')
}
default:
throw new Error('invalid or unsupported key type')
}
}
// Converts a public key object into a protobuf serialized public key
exports.marshalPublicKey = (key, type) => {
type = (type || 'rsa').toLowerCase()
if (!isValidKeyType(type)) {
throw new Error('invalid or unsupported key type')
}
return key.bytes
}
// Converts a protobuf serialized private key into its
// representative object
exports.unmarshalPrivateKey = (buf, callback) => {
2017-07-22 13:25:15 -07:00
const decoded = keysPBM.PrivateKey.decode(buf)
2017-07-22 10:57:27 -07:00
switch (decoded.Type) {
2017-07-22 13:25:15 -07:00
case keysPBM.KeyType.RSA:
return supportedKeys.rsa.unmarshalRsaPrivateKey(decoded.Data, callback)
case keysPBM.KeyType.Ed25519:
return supportedKeys.ed25519.unmarshalEd25519PrivateKey(decoded.Data, callback)
case keysPBM.KeyType.Secp256k1:
if (supportedKeys.secp256k1) {
return supportedKeys.secp256k1.unmarshalSecp256k1PrivateKey(decoded.Data, callback)
2017-07-22 10:57:27 -07:00
} else {
return callback(new Error('secp256k1 support requires libp2p-crypto-secp256k1 package'))
}
default:
callback(new Error('invalid or unsupported key type'))
}
}
// Converts a private key object into a protobuf serialized private key
exports.marshalPrivateKey = (key, type) => {
type = (type || 'rsa').toLowerCase()
if (!isValidKeyType(type)) {
throw new Error('invalid or unsupported key type')
}
return key.bytes
}