Compare commits

...

17 Commits

Author SHA1 Message Date
David Dias
df23d634c5 chore: release version v0.15.0 2019-01-03 16:15:45 +00:00
David Dias
88e1bcf75f chore: update contributors 2019-01-03 16:15:45 +00:00
Jacob Heun
c54ea206f0 feat: nextTick instead of setImmediate, and fix sync in async (#136)
* fix: avoid sync callback in async function

* chore: fix linting

* chore: remove non jenkins ci

* refactor: use nextTick over setImmediate

* refactor: async/nextTick for better browser support
2019-01-03 16:13:07 +00:00
David Dias
857d2bd902 chore: release version v0.14.1 2018-11-05 18:29:00 +00:00
David Dias
200110cb9d chore: update contributors 2018-11-05 18:29:00 +00:00
Jacob Heun
9e5778694c fix: dont setimmediate when its not needed 2018-11-05 19:26:45 +01:00
Jacob Heun
87e8f1c86f chore: remove lint warnings 2018-11-05 19:26:45 +01:00
Jacob Heun
df75980a88 chore: update deps 2018-11-05 19:26:45 +01:00
David Dias
934390acd3 chore: release version v0.14.0 2018-09-17 15:35:20 -07:00
David Dias
8b80b46667 chore: update contributors 2018-09-17 15:35:20 -07:00
David Dias
e8efad546f chore: update deps 2018-09-17 15:33:43 -07:00
Maciej Krüger
e8cbf13d85 fix(lint): use ~ for ursa-optional version 2018-09-17 15:32:13 -07:00
Maciej Krüger
c7e0409c1c fix: windows build 2018-09-17 15:32:13 -07:00
Maciej Krüger
f4c00893ad test: add test for different rsa crypto libs 2018-09-17 15:32:13 -07:00
Maciej Krüger
b05e77f375 feat: use ursa-optional for lightning fast key generation
The difference between ursa and ursa-optional is that ursa-optional does not cause any problems if it fails to compile
2018-09-17 15:32:13 -07:00
Joao Santos
ad478454d8 docs: adds usage examples to AES and HMAC 2018-05-29 14:23:29 +01:00
David Dias
8c69ffb20f docs: Lead maintainer (#119)
* add lead maintainer

* chore: update deps

* Update package.json
2018-05-29 14:22:48 +01:00
19 changed files with 238 additions and 128 deletions

View File

@@ -1,32 +0,0 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
sudo: false
language: node_js
matrix:
include:
- node_js: 6
env: CXX=g++-4.8
- node_js: 8
env: CXX=g++-4.8
# - node_js: stable
# env: CXX=g++-4.8
script:
- npm run lint
- npm run test
- npm run coverage
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
after_success:
- npm run coverage-publish
addons:
firefox: 'latest'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8

View File

@@ -1,3 +1,39 @@
<a name="0.15.0"></a>
# [0.15.0](https://github.com/libp2p/js-libp2p-crypto/compare/v0.14.1...v0.15.0) (2019-01-03)
### Features
* nextTick instead of setImmediate, and fix sync in async ([#136](https://github.com/libp2p/js-libp2p-crypto/issues/136)) ([c54ea20](https://github.com/libp2p/js-libp2p-crypto/commit/c54ea20))
<a name="0.14.1"></a>
## [0.14.1](https://github.com/libp2p/js-libp2p-crypto/compare/v0.14.0...v0.14.1) (2018-11-05)
### Bug Fixes
* dont setimmediate when its not needed ([9e57786](https://github.com/libp2p/js-libp2p-crypto/commit/9e57786))
<a name="0.14.0"></a>
# [0.14.0](https://github.com/libp2p/js-libp2p-crypto/compare/v0.13.0...v0.14.0) (2018-09-17)
### Bug Fixes
* windows build ([c7e0409](https://github.com/libp2p/js-libp2p-crypto/commit/c7e0409))
* **lint:** use ~ for ursa-optional version ([e8cbf13](https://github.com/libp2p/js-libp2p-crypto/commit/e8cbf13))
### Features
* use ursa-optional for lightning fast key generation ([b05e77f](https://github.com/libp2p/js-libp2p-crypto/commit/b05e77f))
<a name="0.13.0"></a>
# [0.13.0](https://github.com/libp2p/js-libp2p-crypto/compare/v0.12.1...v0.13.0) (2018-04-05)

View File

@@ -16,6 +16,10 @@
This repo contains the JavaScript implementation of the crypto primitives needed for libp2p. This is based on this [go implementation](https://github.com/libp2p/go-libp2p-crypto).
## Lead Maintainer
[Friedel Ziegelmayer](https://github.com/dignifiedquire/)
## Table of Contents
- [Install](#install)
@@ -70,8 +74,51 @@ This uses `CTR` mode.
- `data: Buffer`
- `callback: Function`
```
TODO: Example of using aes
```js
var crypto = require('libp2p-crypto')
// Setting up Key and IV
// A 16 bytes array, 128 Bits, AES-128 is chosen
var key128 = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
// A 16 bytes array, 128 Bits,
var IV = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
async function main () {
let decryptedMessage = 'Hello, world!'
let encryptedMessage
// Encrypting
await crypto.aes.create(key128, IV, (err, cipher) => {
if (!err) {
cipher.encrypt(Buffer.from(decryptedMessage), (err, encryptedBuffer) => {
if (!err) {
console.log(encryptedBuffer)
// prints: <Buffer 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
encryptedMessage = encryptedBuffer
}
})
}
})
// Decrypting
await crypto.aes.create(key128, IV, (err, cipher) => {
if (!err) {
cipher.decrypt(encryptedMessage, (err, decryptedBuffer) => {
if (!err) {
console.log(decryptedBuffer)
// prints: <Buffer 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
console.log(decryptedBuffer.toString('utf-8'))
// prints: Hello, world!
}
})
}
})
}
main()
```
### `crypto.hmac`
@@ -91,8 +138,20 @@ Exposes an interface to the Keyed-Hash Message Authentication Code (HMAC) as def
Example:
```
TODO: Example of using hmac
```js
var crypto = require('libp2p-crypto')
let hash = 'SHA1' // 'SHA256' || 'SHA512'
crypto.hmac.create(hash, Buffer.from('secret'), (err, hmac) => {
if (!err) {
hmac.digest(Buffer.from('hello world'), (err, sig) => {
if (!err) {
console.log(sig)
}
})
}
})
```
### `crypto.keys`

View File

@@ -1,29 +0,0 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
version: "{build}"
environment:
matrix:
- nodejs_version: "6"
- nodejs_version: "8"
matrix:
fast_finish: true
install:
# Install Node.js
- ps: Install-Product node $env:nodejs_version
# Upgrade npm
- npm install -g npm
# Output our current versions for debugging
- node --version
- npm --version
# Install our package dependencies
- npm install
test_script:
- npm run test:node
build: off

View File

@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'
const Benchmark = require('benchmark')
@@ -24,4 +25,4 @@ curves.forEach((curve) => {
suite
.on('cycle', (event) => console.log(String(event.target)))
.run({async: true})
.run({ async: true })

View File

@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'
const Benchmark = require('benchmark')
@@ -24,7 +25,7 @@ async.waterfall([
suite
.on('cycle', (event) => console.log(String(event.target)))
.run({async: true})
.run({ async: true })
})
function setup (cipher, hash, secret) {

View File

@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'
const Benchmark = require('benchmark')
@@ -39,4 +40,4 @@ suite.add('sign and verify', (d) => {
suite
.on('cycle', (event) => console.log(String(event.target)))
.run({async: true})
.run({ async: true })

View File

@@ -1,15 +0,0 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
machine:
node:
version: stable
dependencies:
pre:
- google-chrome --version
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo dpkg -i google-chrome.deb || true
- sudo apt-get update
- sudo apt-get install -f
- sudo apt-get install --only-upgrade lsb-base
- sudo dpkg -i google-chrome.deb
- google-chrome --version

View File

@@ -1,8 +1,9 @@
{
"name": "libp2p-crypto",
"version": "0.13.0",
"version": "0.15.0",
"description": "Crypto primitives for libp2p",
"main": "src/index.js",
"leadMaintainer": "Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"browser": {
"./src/hmac/index.js": "./src/hmac/index-browser.js",
"./src/keys/ecdh.js": "./src/keys/ecdh-browser.js",
@@ -27,35 +28,30 @@
"crypto",
"rsa"
],
"author": "Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"license": "MIT",
"dependencies": {
"asn1.js": "^5.0.0",
"async": "^2.6.0",
"asn1.js": "^5.0.1",
"async": "^2.6.1",
"browserify-aes": "^1.2.0",
"bs58": "^4.0.1",
"keypair": "^1.0.1",
"libp2p-crypto-secp256k1": "~0.2.2",
"multihashing-async": "~0.4.8",
"node-forge": "^0.7.5",
"multihashing-async": "~0.5.1",
"node-forge": "~0.7.6",
"pem-jwk": "^1.5.1",
"protons": "^1.0.1",
"rsa-pem-to-jwk": "^1.1.3",
"tweetnacl": "^1.0.0",
"ursa-optional": "~0.9.9",
"webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master"
},
"devDependencies": {
"aegir": "^13.0.6",
"aegir": "^17.1.1",
"benchmark": "^2.1.4",
"chai": "^4.1.2",
"chai-string": "^1.4.0",
"dirty-chai": "^2.0.1",
"pre-commit": "^1.2.2"
"chai": "^4.2.0",
"chai-string": "^1.5.0",
"dirty-chai": "^2.0.1"
},
"pre-commit": [
"lint",
"test"
],
"engines": {
"node": ">=6.0.0",
"npm": ">=3.0.0"
@@ -74,6 +70,8 @@
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Greenkeeper <support@greenkeeper.io>",
"Jack Kleeman <jackkleeman@gmail.com>",
"Jacob Heun <jacobheun@gmail.com>",
"Joao Santos <jrmsantos15@gmail.com>",
"Maciej Krüger <mkg20001@gmail.com>",
"Richard Littauer <richard.littauer@gmail.com>",
"Richard Schneider <makaretu@gmail.com>",

View File

@@ -1,10 +1,10 @@
'use strict'
const asm = require('asmcrypto.js')
const setImmediate = require('async/setImmediate')
const nextTick = require('async/nextTick')
exports.create = function (key, iv, callback) {
const done = (err, res) => setImmediate(() => callback(err, res))
const done = (err, res) => nextTick(() => callback(err, res))
if (key.length !== 16 && key.length !== 32) {
return done(new Error('Invalid key length'))
@@ -21,7 +21,7 @@ exports.create = function (key, iv, callback) {
const res = {
encrypt (data, cb) {
const done = (err, res) => setImmediate(() => cb(err, res))
const done = (err, res) => nextTick(() => cb(err, res))
let res
try {
@@ -36,7 +36,7 @@ exports.create = function (key, iv, callback) {
},
decrypt (data, cb) {
const done = (err, res) => setImmediate(() => cb(err, res))
const done = (err, res) => nextTick(() => cb(err, res))
let res
try {

View File

@@ -12,7 +12,7 @@ const hashTypes = {
}
const sign = (key, data, cb) => {
nodeify(crypto.subtle.sign({name: 'HMAC'}, key, data)
nodeify(crypto.subtle.sign({ name: 'HMAC' }, key, data)
.then((raw) => Buffer.from(raw)), cb)
}
@@ -24,7 +24,7 @@ exports.create = function (hashType, secret, callback) {
secret,
{
name: 'HMAC',
hash: {name: hash}
hash: { name: hash }
},
false,
['sign']

View File

@@ -2,6 +2,7 @@
const crypto = require('crypto')
const lengths = require('./lengths')
const nextTick = require('async/nextTick')
exports.create = function (hash, secret, callback) {
const res = {
@@ -10,7 +11,7 @@ exports.create = function (hash, secret, callback) {
hmac.update(data)
setImmediate(() => {
nextTick(() => {
cb(null, hmac.digest())
})
},

View File

@@ -1,7 +1,7 @@
'use strict'
const crypto = require('crypto')
const setImmediate = require('async/setImmediate')
const nextTick = require('async/nextTick')
const curves = {
'P-256': 'prime256v1',
@@ -16,7 +16,7 @@ exports.generateEphmeralKeyPair = function (curve, callback) {
const ecdh = crypto.createECDH(curves[curve])
ecdh.generateKeys()
setImmediate(() => callback(null, {
nextTick(() => callback(null, {
key: ecdh.getPublicKey(),
genSharedKey (theirPub, forcePrivate, cb) {
if (typeof forcePrivate === 'function') {
@@ -35,7 +35,7 @@ exports.generateEphmeralKeyPair = function (curve, callback) {
return cb(err)
}
setImmediate(() => cb(null, secret))
nextTick(() => cb(null, secret))
}
}))
}

View File

@@ -1,13 +1,13 @@
'use strict'
const nacl = require('tweetnacl')
const setImmediate = require('async/setImmediate')
const nextTick = require('async/nextTick')
exports.publicKeyLength = nacl.sign.publicKeyLength
exports.privateKeyLength = nacl.sign.secretKeyLength
exports.generateKey = function (callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
result = nacl.sign.keyPair()
@@ -20,7 +20,7 @@ exports.generateKey = function (callback) {
// seed should be a 32 byte uint8array
exports.generateKeyFromSeed = function (seed, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
result = nacl.sign.keyPair.fromSeed(seed)
@@ -32,13 +32,13 @@ exports.generateKeyFromSeed = function (seed, callback) {
}
exports.hashAndSign = function (key, msg, callback) {
setImmediate(() => {
nextTick(() => {
callback(null, Buffer.from(nacl.sign.detached(msg, key)))
})
}
exports.hashAndVerify = function (key, sig, msg, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
result = nacl.sign.detached.verify(msg, sig, key)

View File

@@ -12,7 +12,7 @@ exports.generateKey = function (bits, callback) {
name: 'RSASSA-PKCS1-v1_5',
modulusLength: bits,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
true,
['sign', 'verify']
@@ -31,7 +31,7 @@ exports.unmarshalPrivateKey = function (key, callback) {
key,
{
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
true,
['sign']
@@ -59,13 +59,13 @@ exports.hashAndSign = function (key, msg, callback) {
key,
{
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
false,
['sign']
).then((privateKey) => {
return webcrypto.subtle.sign(
{name: 'RSASSA-PKCS1-v1_5'},
{ name: 'RSASSA-PKCS1-v1_5' },
privateKey,
Uint8Array.from(msg)
)
@@ -78,13 +78,13 @@ exports.hashAndVerify = function (key, sig, msg, callback) {
key,
{
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
false,
['verify']
).then((publicKey) => {
return webcrypto.subtle.verify(
{name: 'RSASSA-PKCS1-v1_5'},
{ name: 'RSASSA-PKCS1-v1_5' },
publicKey,
sig,
msg
@@ -109,7 +109,7 @@ function derivePublicFromPrivate (jwKey) {
},
{
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
true,
['verify']

View File

@@ -3,11 +3,11 @@
const multihashing = require('multihashing-async')
const protobuf = require('protons')
const bs58 = require('bs58')
const nextTick = require('async/nextTick')
const crypto = require('./rsa')
const pbm = protobuf(require('./keys.proto'))
const forge = require('node-forge')
const setImmediate = require('async/setImmediate')
class RsaPublicKey {
constructor (key) {
@@ -129,7 +129,7 @@ class RsaPrivateKey {
ensure(callback)
setImmediate(() => {
nextTick(() => {
let err = null
let pem = null
try {

View File

@@ -1,15 +1,36 @@
'use strict'
const crypto = require('crypto')
const keypair = require('keypair')
const setImmediate = require('async/setImmediate')
const nextTick = require('async/nextTick')
let keypair
try {
if (process.env.LP2P_FORCE_CRYPTO_LIB === 'keypair') {
throw new Error('Force keypair usage')
}
const ursa = require('ursa-optional') // throws if not compiled
keypair = ({ bits }) => {
const key = ursa.generatePrivateKey(bits)
return {
private: key.toPrivatePem(),
public: key.toPublicPem()
}
}
} catch (e) {
if (process.env.LP2P_FORCE_CRYPTO_LIB === 'ursa') {
throw e
}
keypair = require('keypair')
}
const pemToJwk = require('pem-jwk').pem2jwk
const jwkToPem = require('pem-jwk').jwk2pem
exports.utils = require('./rsa-utils')
exports.generateKey = function (bits, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
const key = keypair({ bits: bits })
@@ -27,7 +48,7 @@ exports.generateKey = function (bits, callback) {
// Takes a jwk key
exports.unmarshalPrivateKey = function (key, callback) {
setImmediate(() => {
nextTick(() => {
if (!key) {
return callback(new Error('Key is invalid'))
}
@@ -47,7 +68,7 @@ exports.getRandomValues = function (arr) {
}
exports.hashAndSign = function (key, msg, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
const sign = crypto.createSign('RSA-SHA256')
@@ -63,7 +84,7 @@ exports.hashAndSign = function (key, msg, callback) {
}
exports.hashAndVerify = function (key, sig, msg, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
const verify = crypto.createVerify('RSA-SHA256')

View File

@@ -0,0 +1,65 @@
'use strict'
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
chai.use(require('chai-string'))
const LIBS = ['ursa', 'keypair']
describe('RSA crypto libs', function () {
this.timeout(20 * 1000)
LIBS.forEach(lib => {
describe(lib, () => {
let crypto
let rsa
before(() => {
process.env.LP2P_FORCE_CRYPTO_LIB = lib
for (const path in require.cache) { // clear module cache
if (path.endsWith('.js')) {
delete require.cache[path]
}
}
crypto = require('../../src')
rsa = crypto.keys.supportedKeys.rsa
})
it('generates a valid key', (done) => {
crypto.keys.generateKeyPair('RSA', 512, (err, key) => {
if (err) {
return done(err)
}
expect(key).to.be.an.instanceof(rsa.RsaPrivateKey)
key.hash((err, digest) => {
if (err) {
return done(err)
}
expect(digest).to.have.length(34)
done()
})
})
})
after(() => {
for (const path in require.cache) { // clear module cache
if (path.endsWith('.js')) {
delete require.cache[path]
}
}
delete process.env.LP2P_FORCE_CRYPTO_LIB
})
})
})
})

3
test/node.js Normal file
View File

@@ -0,0 +1,3 @@
'use strict'
require('./keys/rsa-crypto-libs')