Compare commits

..

26 Commits

Author SHA1 Message Date
David Dias
7d63f698c0 chore: release version v0.8.5 2017-02-10 18:54:16 -08:00
David Dias
a49df7786c chore: update contributors 2017-02-10 18:54:16 -08:00
David Dias
c73adb00cc fix: add libp2p-crypto-secp256k1 2017-02-10 18:53:36 -08:00
David Dias
8a95de4700 chore: release version v0.8.4 2017-02-10 17:54:47 -08:00
David Dias
c35a65133f chore: update contributors 2017-02-10 17:54:47 -08:00
David Dias
4eb4aa05de fix: rm browserify-optional and transforms 2017-02-10 17:54:11 -08:00
David Dias
21f96a4eb7 chore: release version v0.8.3 2017-02-10 16:16:49 -08:00
David Dias
39b5124526 chore: update contributors 2017-02-10 16:16:49 -08:00
David Dias
93e50dde10 fix: browserify-optional needs to be dep 2017-02-10 16:16:11 -08:00
David Dias
e383da5d44 chore: release version v0.8.2 2017-02-10 10:28:44 -08:00
David Dias
9b687f380c chore: update contributors 2017-02-10 10:28:44 -08:00
Friedel Ziegelmayer
82abede64a fix: use browserify-optional (#72)
This works around the issue that browserify throws on missing modules.
2017-02-10 10:21:56 -08:00
David Dias
a56ab406e4 chore: release version v0.8.1 2017-02-09 08:32:12 -08:00
David Dias
d753941c6e chore: update contributors 2017-02-09 08:32:12 -08:00
David Dias
6022eb0838 chore: ^ to ~ 2017-02-09 08:29:55 -08:00
Dmitriy Ryajov
1c6d0912cd chore: removing --webworker from build steps 2017-02-09 12:14:26 +01:00
Friedel Ziegelmayer
e18804c31e chore: release version v0.8.0 2017-02-07 21:03:56 +01:00
Friedel Ziegelmayer
133a2c8833 chore: update contributors 2017-02-07 21:03:55 +01:00
Friedel Ziegelmayer
fad0865b2b chore: drop webworker flag 2017-02-07 20:46:58 +01:00
Friedel Ziegelmayer
4edef07182 Merge pull request #67 from libp2p/greenkeeper-aegir-10.0.0
Update aegir to version 10.0.0 🚀
2017-02-07 20:45:13 +01:00
Friedel Ziegelmayer
19c6ce7c06 Merge pull request #65 from dryajov/master
feat: change window to self for webworker support
2017-02-07 20:45:01 +01:00
Friedel Ziegelmayer
3fa76a874f Merge pull request #66 from libp2p/expose-proto
feat: expose protobuf
2017-02-07 20:44:36 +01:00
greenkeeperio-bot
6e0fd6f257 chore(package): update aegir to version 10.0.0
https://greenkeeper.io/
2017-02-07 18:45:52 +01:00
Friedel Ziegelmayer
c91d9b61c8 feat: expose protobuf
this allows other modules to reuse the protobuf definition
2017-02-07 17:51:43 +01:00
Yusef Napora
76eeb5aa18 feat: add support for secp256k1 keys through the libp2p-crypto-secp256k1 module 2017-02-04 10:23:38 +01:00
dryajov
843b5e33d6 WIP: use self instead of window for WebWorker compatibility 2017-01-31 02:09:46 -08:00
9 changed files with 184 additions and 28 deletions

View File

@@ -107,9 +107,21 @@ Depending on the environment this is either an instance of [node-webcrypto-ossl]
### `keys`
#### Supported Key Types
The [`generateKeyPair`](#generatekeypairtype-bits-callback), [`marshalPublicKey`](#marshalpublickeykey-type-callback), and
[`marshalPrivateKey`](#marshalprivatekeykey-type) functions accept a string `type` argument.
Currently the `'RSA'` and `'ed25519'` types are supported, although ed25519 keys support only signing and
verification of messages. For encryption / decryption support, RSA keys should be used.
Installing the [libp2p-crypto-secp256k1](https://github.com/libp2p/js-libp2p-crypto-secp256k1) module adds support for the
`'secp256k1'` type, which supports ECDSA signatures using the secp256k1 elliptic curve popularized by Bitcoin. This module
is not installed by default, and should be explicitly depended on if your project requires secp256k1 support.
### `generateKeyPair(type, bits, callback)`
- `type: String`, only `'RSA'` is currently supported
- `type: String`, see [Supported Key Types](#supported-key-types) above.
- `bits: Number` Minimum of 1024
- `callback: Function`
@@ -160,8 +172,8 @@ Calls back with an object of the form
### `marshalPublicKey(key[, type], callback)`
- `key: crypto.rsa.RsaPublicKey`
- `type: String`, only `'RSA'` is currently supported
- `key: keys.rsa.RsaPublicKey | keys.ed25519.Ed25519PublicKey | require('libp2p-crypto-secp256k1').Secp256k1PublicKey`
- `type: String`, see [Supported Key Types](#supported-key-types) above.
Converts a public key object into a protobuf serialized public key.
@@ -173,8 +185,8 @@ Converts a protobuf serialized public key into its representative object.
### `marshalPrivateKey(key[, type])`
- `key: crypto.rsa.RsaPrivateKey`
- `type: String`, only `'RSA'` is currently supported
- `key: keys.rsa.RsaPrivateKey | keys.ed25519.Ed25519PrivateKey | require('libp2p-crypto-secp256k1').Secp256k1PrivateKey`
- `type: String`, see [Supported Key Types](#supported-key-types) above.
Converts a private key object into a protobuf serialized private key.

View File

@@ -1,6 +1,6 @@
{
"name": "libp2p-crypto",
"version": "0.7.7",
"version": "0.8.5",
"description": "Crypto primitives for libp2p",
"main": "src/index.js",
"browser": {
@@ -17,10 +17,10 @@
"test": "npm run test:node && npm run test:no-webcrypto && npm run test:browser",
"test:node": "aegir-test --env node",
"test:no-webcrypto": "NO_WEBCRYPTO=true aegir-test --env node",
"test:browser": "aegir-test --env browser --webworker",
"release": "aegir-release --webworker",
"release-minor": "aegir-release --type minor --webworker",
"release-major": "aegir-release --type major --webworker",
"test:browser": "aegir-test --env browser",
"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
},
@@ -37,7 +37,8 @@
"async": "^2.1.4",
"browserify-aes": "^1.0.6",
"keypair": "^1.0.1",
"multihashing-async": "^0.4.0",
"libp2p-crypto-secp256k1": "^0.1.3",
"multihashing-async": "~0.4.2",
"nodeify": "^1.0.0",
"pem-jwk": "^1.5.1",
"protocol-buffers": "^3.2.1",
@@ -47,37 +48,38 @@
"webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master"
},
"devDependencies": {
"aegir": "^9.4.0",
"aegir": "^10.0.0",
"benchmark": "^2.1.3",
"chai": "^3.5.0",
"pre-commit": "^1.2.2"
},
"optionalDependencies": {
"node-webcrypto-ossl": "^1.0.16"
"node-webcrypto-ossl": "^1.0.17"
},
"pre-commit": [
"lint",
"test"
],
"engines": {
"node": ">=4.0.0"
"node": ">=4.0.0",
"npm": ">=3.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/ipfs/js-libp2p-crypto.git"
"url": "https://github.com/libp2p/js-libp2p-crypto.git"
},
"bugs": {
"url": "https://github.com/ipfs/js-libp2p-crypto/issues"
"url": "https://github.com/libp2p/js-libp2p-crypto/issues"
},
"homepage": "https://github.com/ipfs/js-libp2p-crypto",
"homepage": "https://github.com/libp2p/js-libp2p-crypto",
"contributors": [
"David Dias <daviddias.p@gmail.com>",
"Dmitriy Ryajov <dryajov@gmail.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Greenkeeper <support@greenkeeper.io>",
"Richard Littauer <richard.littauer@gmail.com>",
"Tom Swindell <t.swindell@rubyx.co.uk>",
"Yusef Napora <yusef@napora.org>",
"dryajov <dryajov@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>",
"nikuda <nikuda@gmail.com>"
]
}

View File

@@ -3,6 +3,7 @@
module.exports = `enum KeyType {
RSA = 0;
Ed25519 = 1;
Secp256k1 = 2;
}
message PublicKey {

View File

@@ -3,12 +3,12 @@
'use strict'
module.exports = function getWebCrypto () {
if (typeof window !== 'undefined') {
if (typeof self !== 'undefined') {
// This is only a shim for interfaces, not for functionality
require('webcrypto-shim')(window)
require('webcrypto-shim')(self)
if (window.crypto) {
return window.crypto
if (self.crypto) {
return self.crypto
}
}

View File

@@ -5,12 +5,17 @@ const protobuf = require('protocol-buffers')
const pbm = protobuf(require('./crypto.proto'))
const c = require('./crypto')
exports.protobuf = pbm
exports.hmac = c.hmac
exports.aes = c.aes
exports.webcrypto = c.webcrypto
const keys = exports.keys = require('./keys')
const KEY_TYPES = ['rsa', 'ed25519']
function isValidKeyType (keyType) {
const key = keys[keyType.toLowerCase()]
return key !== undefined
}
exports.keyStretcher = require('./key-stretcher')
exports.generateEphemeralKeyPair = require('./ephemeral-keys')
@@ -35,6 +40,12 @@ exports.unmarshalPublicKey = (buf) => {
return keys.rsa.unmarshalRsaPublicKey(decoded.Data)
case pbm.KeyType.Ed25519:
return keys.ed25519.unmarshalEd25519PublicKey(decoded.Data)
case pbm.KeyType.Secp256k1:
if (keys.secp256k1) {
return keys.secp256k1.unmarshalSecp256k1PublicKey(decoded.Data)
} else {
throw new Error('secp256k1 support requires libp2p-crypto-secp256k1 package')
}
default:
throw new Error('invalid or unsupported key type')
}
@@ -43,7 +54,7 @@ exports.unmarshalPublicKey = (buf) => {
// Converts a public key object into a protobuf serialized public key
exports.marshalPublicKey = (key, type) => {
type = (type || 'rsa').toLowerCase()
if (KEY_TYPES.indexOf(type) < 0) {
if (!isValidKeyType(type)) {
throw new Error('invalid or unsupported key type')
}
@@ -60,6 +71,12 @@ exports.unmarshalPrivateKey = (buf, callback) => {
return keys.rsa.unmarshalRsaPrivateKey(decoded.Data, callback)
case pbm.KeyType.Ed25519:
return keys.ed25519.unmarshalEd25519PrivateKey(decoded.Data, callback)
case pbm.KeyType.Secp256k1:
if (keys.secp256k1) {
return keys.secp256k1.unmarshalSecp256k1PrivateKey(decoded.Data, callback)
} else {
return callback(new Error('secp256k1 support requires libp2p-crypto-secp256k1 package'))
}
default:
callback(new Error('invalid or unsupported key type'))
}
@@ -68,7 +85,7 @@ exports.unmarshalPrivateKey = (buf, callback) => {
// Converts a private key object into a protobuf serialized private key
exports.marshalPrivateKey = (key, type) => {
type = (type || 'rsa').toLowerCase()
if (KEY_TYPES.indexOf(type) < 0) {
if (!isValidKeyType(type)) {
throw new Error('invalid or unsupported key type')
}

View File

@@ -2,5 +2,6 @@
module.exports = {
rsa: require('./rsa'),
ed25519: require('./ed25519')
ed25519: require('./ed25519'),
secp256k1: require('libp2p-crypto-secp256k1')
}

11
test/fixtures/secp256k1.js vendored Normal file
View File

@@ -0,0 +1,11 @@
'use strict'
const Buffer = require('safe-buffer').Buffer
module.exports = {
// protobuf marshaled key pair generated with libp2p-crypto-secp256k1
// and marshaled with libp2p-crypto.marshalPublicKey / marshalPrivateKey
pbmPrivateKey: Buffer.from('08021220e0600103010000000100000000000000be1dc82c2e000000e8d6030301000000', 'hex'),
pbmPublicKey: Buffer.from('0802122103a9a7272a726fa083abf31ba44037f8347fbc5e5d3113d62a7c6bc26752fd8ee1', 'hex')
}

View File

@@ -23,9 +23,17 @@ describe('libp2p-crypto', () => {
const key2 = crypto.unmarshalPublicKey(crypto.marshalPublicKey(key.public))
expect(key2.equals(key.public)).to.be.eql(true)
expect(() => {
crypto.marshalPublicKey(key.public, 'invalid-key-type')
}).to.throw()
})
it('marshalPrivateKey and unmarshalPrivateKey', (done) => {
expect(() => {
crypto.marshalPrivateKey(key, 'invalid-key-type')
}).to.throw()
crypto.unmarshalPrivateKey(crypto.marshalPrivateKey(key), (err, key2) => {
if (err) {
return done(err)
@@ -103,7 +111,7 @@ describe('libp2p-crypto', () => {
it('throws with no number passed', () => {
expect(() => {
crypto.randomBytes()
}).to.throw
}).to.throw()
})
it('generates different random things', () => {

104
test/secp256k1.spec.js Normal file
View File

@@ -0,0 +1,104 @@
/* eslint-env mocha */
'use strict'
const expect = require('chai').expect
const fixtures = require('./fixtures/secp256k1')
const crypto = require('../src')
const mockPublicKey = {
bytes: fixtures.pbmPublicKey
}
const mockPrivateKey = {
bytes: fixtures.pbmPrivateKey,
public: mockPublicKey
}
const mockSecp256k1Module = {
generateKeyPair (bits, callback) {
callback(null, mockPrivateKey)
},
unmarshalSecp256k1PrivateKey (buf, callback) {
callback(null, mockPrivateKey)
},
unmarshalSecp256k1PublicKey (buf) {
return mockPublicKey
}
}
describe('with libp2p-crypto-secp256k1 module present', () => {
let key
before((done) => {
crypto.keys['secp256k1'] = mockSecp256k1Module
crypto.generateKeyPair('secp256k1', 256, (err, _key) => {
if (err) return done(err)
key = _key
done()
})
})
after((done) => {
delete crypto.keys['secp256k1']
done()
})
it('generates a valid key', (done) => {
expect(
key
).to.exist
done()
})
it('protobuf encoding', (done) => {
const keyMarshal = crypto.marshalPrivateKey(key)
crypto.unmarshalPrivateKey(keyMarshal, (err, key2) => {
if (err) return done(err)
const keyMarshal2 = crypto.marshalPrivateKey(key2)
expect(
keyMarshal
).to.be.eql(
keyMarshal2
)
const pk = key.public
const pkMarshal = crypto.marshalPublicKey(pk)
const pk2 = crypto.unmarshalPublicKey(pkMarshal)
const pkMarshal2 = crypto.marshalPublicKey(pk2)
expect(
pkMarshal
).to.be.eql(
pkMarshal2
)
done()
})
})
})
describe('without libp2p-crypto-secp256k1 module present', () => {
it('fails to generate a secp256k1 key', (done) => {
crypto.generateKeyPair('secp256k1', 256, (err, key) => {
expect(err).to.exist
expect(key).to.not.exist
done()
})
})
it('fails to unmarshal a secp256k1 private key', (done) => {
crypto.unmarshalPrivateKey(fixtures.pbmPrivateKey, (err, key) => {
expect(err).to.exist
expect(key).to.not.exist
done()
})
})
it('fails to unmarshal a secp256k1 public key', () => {
expect(() => {
crypto.unmarshalPublicKey(fixtures.pbmPublicKey)
}).to.throw(Error)
})
})