Skip to content
This repository was archived by the owner on Feb 24, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions .aegir.js

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ build
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

lib
dist
20 changes: 11 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
sudo: false
language: node_js
node_js:
- 4
- 5
- stable
matrix:
include:
- node_js: 4
env: CXX=g++-4.8
- node_js: 6
env:
- SAUCE=true
- CXX=g++-4.8
- node_js: stable
env: CXX=g++-4.8

# Make sure we have new NPM.
before_install:
Expand All @@ -14,21 +20,17 @@ script:
- npm test
- npm run coverage


before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

after_success:
- npm run coverage-publish

env:
- CXX=g++-4.8

addons:
firefox: 'latest'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
- g++-4.8
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
[![Travis CI](https://travis-ci.org/libp2p/js-libp2p-secio.svg?branch=master)](https://travis-ci.org/libp2p/js-libp2p-secio)
[![Circle CI](https://circleci.com/gh/libp2p/js-libp2p-secio.svg?style=svg)](https://circleci.com/gh/libp2p/js-libp2p-secio)
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-secio.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-secio) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)

[![Sauce Test Status](https://saucelabs.com/browser-matrix/libp2p-js-secio.svg)](https://saucelabs.com/u/libp2p-js-secio)

> Secio implementation in JavaScript

Expand Down Expand Up @@ -60,7 +64,7 @@ You can learn more about pull-streams at:

#### Converting `pull-streams` to Node.js Streams

If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/dominictarr/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module [`pull-stream-to-stream`](https://github.com/pull-stream/pull-stream-to-stream), giving you an instance of a Node.js stream that is linked to the pull-stream. For example:

```js
const pullToStream = require('pull-stream-to-stream')
Expand All @@ -75,7 +79,7 @@ To learn more about this utility, visit https://pull-stream.github.io/#pull-stre

## Contribute

Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/js-libp2p-secio/issues)!
Feel free to join in. All welcome. Open an [issue](https://github.com/libp2p/js-libp2p-secio/issues)!

This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).

Expand Down
87 changes: 87 additions & 0 deletions benchmarks/send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use strict'

const Benchmark = require('benchmark')
const pull = require('pull-stream')
const pair = require('pull-pair/duplex')
const PeerId = require('peer-id')
const crypto = require('libp2p-crypto')

const secio = require('../src')

const suite = new Benchmark.Suite('secio')
const ids = []

suite.add('createKey', function (d) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is d?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the other PR or https://benchmarkjs.com/docs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, can it have a better and intuitive name?

crypto.generateKeyPair('RSA', 2048, (err, key) => {
if (err) {
throw err
}
PeerId.createFromPrivKey(key.bytes, (err, id) => {
if (err) {
throw err
}
ids.push(id)
d.resolve()
})
})
}, {
defer: true
})
.add('send', function (deferred) {
const p = pair()

createSession(p[0], (err, local) => {
if (err) {
throw err
}
createSession(p[1], (err, remote) => {
if (err) {
throw err
}
sendMessages(local, remote)
})
})

function sendMessages (local, remote) {
pull(
pull.infinite(),
pull.take(100),
pull.map((val) => Buffer(val.toString())),
local
)

pull(
remote,
pull.take(100),
pull.collect((err, chunks) => {
if (err) throw err
if (chunks.length !== 100) throw new Error('Did not receive enough chunks')
deferred.resolve()
})
)
}
}, {
defer: true
})
.on('cycle', (event) => {
console.log(String(event.target))
})
// run async
.run({
async: true
})

function createSession (insecure, cb) {
crypto.generateKeyPair('RSA', 2048, (err, key) => {
if (err) {
return cb(err)
}
PeerId.createFromPrivKey(key.bytes, (err, id) => {
if (err) {
return cb(err)
}

cb(null, secio.encrypt(id, key, insecure))
})
})
}
33 changes: 16 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"name": "libp2p-secio",
"version": "0.5.0",
"description": "Secio implementation in JavaScript",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
"main": "src/index.js",
"scripts": {
"lint": "aegir-lint",
"build": "aegir-build",
Expand All @@ -14,45 +13,45 @@
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
"coverage-publish": "aegir-coverage publish",
"bench": "node benchmarks/send.js"
},
"keywords": [
"IPFS",
"libp2p",
"crypto",
"rsa"
],
"author": "Friedel Ziegelmayer <dignifiedqurie@gmail.com>",
"author": "Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"license": "MIT",
"dependencies": {
"async": "^2.1.2",
"debug": "^2.2.0",
"interface-connection": "^0.2.1",
"libp2p-crypto": "^0.6.1",
"multihashing": "^0.2.1",
"node-forge": "^0.6.42",
"peer-id": "^0.7.0",
"interface-connection": "^0.3.0",
"libp2p-crypto": "^0.7.0",
"multihashing-async": "^0.2.0",
"peer-id": "^0.8.0",
"protocol-buffers": "^3.1.6",
"pull-defer": "^0.2.2",
"pull-handshake": "^1.1.4",
"pull-length-prefixed": "^1.2.0",
"pull-stream": "^3.4.3",
"pull-through": "^1.0.18",
"run-series": "^1.1.4"
"pull-stream": "^3.5.0"
},
"devDependencies": {
"aegir": "^8.0.0",
"aegir": "^9.0.1",
"benchmark": "^2.1.2",
"chai": "^3.5.0",
"multistream-select": "^0.11.0",
"multistream-select": "^0.11.1",
"pre-commit": "^1.1.3",
"pull-pair": "^1.1.0",
"run-parallel": "^1.1.6"
"pull-pushable": "^2.0.1"
},
"pre-commit": [
"lint",
"test"
],
"engines": {
"node": "^4.0.0"
"node": ">=4.0.0"
},
"repository": {
"type": "git",
Expand All @@ -68,4 +67,4 @@
"Richard Littauer <[email protected]>",
"greenkeeperio-bot <[email protected]>"
]
}
}
98 changes: 46 additions & 52 deletions src/etm.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,67 @@
'use strict'

const through = require('pull-through')
const pull = require('pull-stream')
const lp = require('pull-length-prefixed')

const toForgeBuffer = require('./support').toForgeBuffer

const lpOpts = {
fixed: true,
bytes: 4
}

exports.createBoxStream = (cipher, mac) => {
const pt = through(function (chunk) {
cipher.update(toForgeBuffer(chunk))

if (cipher.output.length() > 0) {
const data = new Buffer(cipher.output.getBytes(), 'binary')
mac.update(data.toString('binary'))
const macBuffer = new Buffer(mac.digest().getBytes(), 'binary')

this.queue(Buffer.concat([data, macBuffer]))
// reset hmac
mac.start(null, null)
}
})

return pull(
pt,
pull.asyncMap((chunk, cb) => {
cipher.encrypt(chunk, (err, data) => {
if (err) {
return cb(err)
}

mac.digest(data, (err, digest) => {
if (err) {
return cb(err)
}

cb(null, Buffer.concat([data, digest]))
})
})
}),
lp.encode(lpOpts)
)
}

exports.createUnboxStream = (decipher, mac) => {
const pt = through(function (chunk) {
const l = chunk.length
const macSize = mac.getMac().length()

if (l < macSize) {
return this.emit('error', new Error(`buffer (${l}) shorter than MAC size (${macSize})`))
}

const mark = l - macSize
const data = chunk.slice(0, mark)
const macd = chunk.slice(mark)

// Clear out any previous data
mac.start(null, null)

mac.update(data.toString('binary'))
const expected = new Buffer(mac.getMac().getBytes(), 'binary')

// reset hmac
mac.start(null, null)
if (!macd.equals(expected)) {
return this.emit('error', new Error(`MAC Invalid: ${macd.toString('hex')} != ${expected.toString('hex')}`))
}

// all good, decrypt
decipher.update(toForgeBuffer(data))

if (decipher.output.length() > 0) {
const data = new Buffer(decipher.output.getBytes(), 'binary')
this.queue(data)
}
})

return pull(
lp.decode(lpOpts),
pt
pull.asyncMap((chunk, cb) => {
const l = chunk.length
const macSize = mac.length

if (l < macSize) {
return cb(new Error(`buffer (${l}) shorter than MAC size (${macSize})`))
}

const mark = l - macSize
const data = chunk.slice(0, mark)
const macd = chunk.slice(mark)

mac.digest(data, (err, expected) => {
if (err) {
return cb(err)
}

if (!macd.equals(expected)) {
return cb(new Error(`MAC Invalid: ${macd.toString('hex')} != ${expected.toString('hex')}`))
}

// all good, decrypt
decipher.decrypt(data, (err, decrypted) => {
if (err) {
return cb(err)
}

cb(null, decrypted)
})
})
})
)
}
Loading