feat: implement generateEphemeralKeyPair

This commit is contained in:
Friedel Ziegelmayer
2016-05-19 22:33:09 +02:00
parent ca0b5305a2
commit d415fa8007
6 changed files with 68 additions and 15 deletions

30
src/ephemeral-keys.js Normal file
View 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')
}
}

View File

@@ -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
View 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')
}