mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-07-31 06:12:16 +00:00
feat: implement generateEphemeralKeyPair
This commit is contained in:
30
src/ephemeral-keys.js
Normal file
30
src/ephemeral-keys.js
Normal file
@@ -0,0 +1,30 @@
|
||||
'use strict'
|
||||
|
||||
const EC = require('elliptic').ec
|
||||
|
||||
const curveMap = {
|
||||
'P-256': 'p256',
|
||||
'P-384': 'p384',
|
||||
'P-521': 'p521'
|
||||
}
|
||||
|
||||
// Generates an ephemeral public key and returns a function that will compute
|
||||
// the shared secret key.
|
||||
//
|
||||
// Focuses only on ECDH now, but can be made more general in the future.
|
||||
module.exports = (curveName) => {
|
||||
const curve = curveMap[curveName]
|
||||
if (!curve) {
|
||||
throw new Error('unsupported curve passed')
|
||||
}
|
||||
|
||||
const ec = new EC(curve)
|
||||
|
||||
const priv = ec.genKeyPair()
|
||||
|
||||
return (theirPub) => {
|
||||
const pub = ec.keyFromPublic(theirPub, 'hex')
|
||||
|
||||
return priv.derive(pub.getPublic()).toBuffer('le')
|
||||
}
|
||||
}
|
17
src/index.js
17
src/index.js
@@ -8,6 +8,9 @@ const pbm = protobuf(fs.readFileSync(path.join(__dirname, './crypto.proto')))
|
||||
exports.utils = require('./utils')
|
||||
const keys = exports.keys = require('./keys')
|
||||
|
||||
exports.keyStretcher = require('./key-stretcher')
|
||||
exports.generateEphemeralKeyPair = require('./ephemeral-keys')
|
||||
|
||||
// Generates a keypair of the given type and bitsize
|
||||
exports.generateKeyPair = (type, bits, cb) => {
|
||||
let key = keys[type.toLowerCase()]
|
||||
@@ -18,20 +21,6 @@ exports.generateKeyPair = (type, bits, cb) => {
|
||||
key.generateKeyPair(bits, cb)
|
||||
}
|
||||
|
||||
// Generates an ephemeral public key and returns a function that will compute
|
||||
// the shared secret key.
|
||||
//
|
||||
// Focuses only on ECDH now, but can be made more general in the future.
|
||||
exports.generateEphemeralKeyPair = (curveName, cb) => {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
|
||||
// Generates a set of keys for each party by stretching the shared key.
|
||||
// (myIV, theirIV, myCipherKey, theirCipherKey, myMACKey, theirMACKey)
|
||||
exports.keyStretcher = (cipherType, hashType, secret) => {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
|
||||
// Converts a protobuf serialized public key into its
|
||||
// representative object
|
||||
exports.unmarshalPublicKey = (buf) => {
|
||||
|
7
src/key-stretcher.js
Normal file
7
src/key-stretcher.js
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
// Generates a set of keys for each party by stretching the shared key.
|
||||
// (myIV, theirIV, myCipherKey, theirCipherKey, myMACKey, theirMACKey)
|
||||
module.exports = (cipherType, hashType, secret) => {
|
||||
throw new Error('Not implemented')
|
||||
}
|
Reference in New Issue
Block a user