mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-03-15 15:21:07 +00:00
feat: implement generateEphemeralKeyPair
This commit is contained in:
parent
ca0b5305a2
commit
d415fa8007
11
README.md
11
README.md
@ -18,7 +18,6 @@ needed for libp2p. This is based on this [go implementation](https://github.com/
|
||||
|
||||
## API
|
||||
|
||||
|
||||
### `generateKeyPair(type, bits, cb)`
|
||||
|
||||
- `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.
|
||||
|
||||
### `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])`
|
||||
|
||||
- `key: crypto.rsa.RsaPublicKey`
|
||||
|
@ -25,6 +25,7 @@
|
||||
"author": "Friedel Ziegelmayer <dignifiedqurie@gmail.com>",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"elliptic": "^6.2.3",
|
||||
"multihashing": "^0.2.1",
|
||||
"node-forge": "^0.6.39",
|
||||
"protocol-buffers": "^3.1.6"
|
||||
|
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')
|
||||
}
|
17
test/ephemeral-keys.spec.js
Normal file
17
test/ephemeral-keys.spec.js
Normal 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)
|
||||
})
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user