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

View File

@ -18,7 +18,6 @@ needed for libp2p. This is based on this [go implementation](https://github.com/
## API ## API
### `generateKeyPair(type, bits, cb)` ### `generateKeyPair(type, bits, cb)`
- `type: String`, only `'RSA'` is currently supported - `type: String`, only `'RSA'` is currently supported
@ -27,6 +26,16 @@ needed for libp2p. This is based on this [go implementation](https://github.com/
Generates a keypair of the given type and bitsize. Generates a keypair of the given type and bitsize.
### `generateEphemeralKeyPair(curve)`
- `curve: String`, one of `'P-256'`, `'P-384'`, `'P-521'` is currently supported
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.
Returns a `Buffer`.
### `marshalPublicKey(key[, type])` ### `marshalPublicKey(key[, type])`
- `key: crypto.rsa.RsaPublicKey` - `key: crypto.rsa.RsaPublicKey`

View File

@ -25,6 +25,7 @@
"author": "Friedel Ziegelmayer <dignifiedqurie@gmail.com>", "author": "Friedel Ziegelmayer <dignifiedqurie@gmail.com>",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"elliptic": "^6.2.3",
"multihashing": "^0.2.1", "multihashing": "^0.2.1",
"node-forge": "^0.6.39", "node-forge": "^0.6.39",
"protocol-buffers": "^3.1.6" "protocol-buffers": "^3.1.6"

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') exports.utils = require('./utils')
const keys = exports.keys = require('./keys') 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 // Generates a keypair of the given type and bitsize
exports.generateKeyPair = (type, bits, cb) => { exports.generateKeyPair = (type, bits, cb) => {
let key = keys[type.toLowerCase()] let key = keys[type.toLowerCase()]
@ -18,20 +21,6 @@ exports.generateKeyPair = (type, bits, cb) => {
key.generateKeyPair(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 // Converts a protobuf serialized public key into its
// representative object // representative object
exports.unmarshalPublicKey = (buf) => { 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')
}

View File

@ -0,0 +1,17 @@
/* eslint-env mocha */
'use strict'
const expect = require('chai').expect
const crypto = require('../src')
describe('generateEphemeralKeyPair', () => {
it('returns a function that generates a shared secret', () => {
const maker = crypto.generateEphemeralKeyPair('P-256')
const ourPublic = '044374add0df35706db7dade25f3959fc051d2ef5166f8a6a0aa632d0ab41cdb4d30e1a064e121ac56155235a6b8d4c5d8fe35e019f507f4e2ff1445e229d7af43'
expect(
maker(ourPublic)
).to.have.length(32)
})
})