Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Async Crypto Endeavour #12

Merged
merged 4 commits into from
Nov 3, 2016
Merged

Async Crypto Endeavour #12

merged 4 commits into from
Nov 3, 2016

Conversation

dignifiedquire
Copy link
Member

@dignifiedquire dignifiedquire commented Sep 23, 2016

  • No more node-forge
  • No more elliptic.js

~~Needs https://github.com/multiformats/js-multihashing/pull/12~~ Now multihashing-async

Stats can be found here: https://github.com/libp2p/js-libp2p-crypto/blob/webcrypto/stats.md

@dignifiedquire
Copy link
Member Author

Added PKCS#1 handling to ensure interop with go, which made rsa key generation a lot slower. Need to find a faster way to do this.

@dignifiedquire
Copy link
Member Author

Added PKCS#1 handling to ensure interop with go, which made rsa key generation a lot slower. Need to find a faster way to do this.

Turns out it is not slower, I just misread my stats :) Just a small size increase due to the need to bundle some more JavaScript libraries.

@dignifiedquire dignifiedquire changed the title [WIP] Move to webcrypto as much as possible Replace elliptic.js and node-forge with webcrypto Sep 26, 2016
@dignifiedquire
Copy link
Member Author

dignifiedquire commented Oct 4, 2016

"protocol-buffers": "^3.1.6"
"asn1.js": "^4.8.1",
"async": "^2.0.1",
"multihashing-async": "^0.1.0",
Copy link
Member

Choose a reason for hiding this comment

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

the "Needs" on #12 (comment) is no longer valid, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes

- `buf: Buffer`
- `callback: Function`

Converts a protobuf serialized private key into its representative object.
Copy link
Member

Choose a reason for hiding this comment

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

API should be on the README.md

Copy link
Member Author

Choose a reason for hiding this comment

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

It's too long and was moved out, as we did before with very large APIs

Copy link
Member

Choose a reason for hiding this comment

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

I don't believe that is true, which other modules have an API.md?

- [`marshalPublicKey(key[, type])`](#marshalpublickeykey-type)
- [`unmarshalPublicKey(buf)`](#unmarshalpublickeybuf)
- [`marshalPrivateKey(key[, type])`](#marshalprivatekeykey-type)
- [`unmarshalPrivateKey(buf)`](#unmarshalprivatekeybuf)
Copy link
Member

Choose a reason for hiding this comment

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

why stop linking to things?

Copy link
Member

Choose a reason for hiding this comment

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

Let's continue linking the methods.

- `buf: Buffer`

Converts a protobuf serialized private key into its representative object.
See [API.md](API.md)
Copy link
Member

Choose a reason for hiding this comment

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

The API should be on the README, it is how we do it in every other module.

curves.forEach((curve) => {
suite.add(`ephemeral key with secrect ${curve}`, (d) => {
crypto.generateEphemeralKeyPair('P-256', (err, res) => {
if (err) throw err
Copy link
Member

Choose a reason for hiding this comment

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

Is this using latest aegir? Missing the curly

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

'use strict'

module.exports = function getWebCrypto () {
const WebCrypto = require('node-webcrypto-ossl')
Copy link
Member

Choose a reason for hiding this comment

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

WARNING
At this time this solution should be considered suitable for research and experimentation, further code and security review is needed before utilization in a production application.

This makes the statement that 'using webcrypto is safer' invalid because we know have to trust this shim

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a pretty thin wrapper around openssl, so I wouldn't say that this makes it less safe than the current things we have

if (window.crypto) {
return window.crypto
}
}
Copy link
Member

Choose a reason for hiding this comment

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

wait, so, if in browser, we have to use a "webcrypto-shim"?

Copy link
Member Author

Choose a reason for hiding this comment

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

webcrypto-shim only helps to fix some differences in api in some browsers, it doesn't actually provide a shim for the apis if they are not available

Copy link
Member

@daviddias daviddias Oct 28, 2016

Choose a reason for hiding this comment

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

  • I see, can you add that as a comment?

}
}

throw new Error('Please use an environment with crypto support')
Copy link
Member

Choose a reason for hiding this comment

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

If the shim is there, when would this happen?

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 above, in all browsers that don't have webcrypto this will happen

'use strict'

module.exports = function getWebCrypto () {
if (typeof window !== 'undefined') {
Copy link
Member

Choose a reason for hiding this comment

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

use detect-node

Copy link
Member Author

Choose a reason for hiding this comment

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

No, we don't want to know if we are in node, we explicitly want to know if there is a window object available.

decrypt (bytes) {
return this._privateKey.decrypt(bytes, 'RSAES-PKCS1-V1_5')
decrypt (msg, callback) {
crypto.decrypt(this._key, msg, callback)
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 the difference between bytes and msg? Probably node, why change the name?

Copy link
Member Author

Choose a reason for hiding this comment

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

I found msg to better describe what is actually being passed through.

@daviddias
Copy link
Member

This is a giant PR. I didn't see any direct go-ipfs interop tests with regards to crypto.

@dignifiedquire
Copy link
Member Author

I didn't see any direct go-ipfs interop tests with regards to crypto.

They were there before, you can grep for go interop in the tests folder

@daviddias
Copy link
Member

daviddias commented Oct 12, 2016

Needs:

  • Squashing of commits (plus use of commit convention)
  • Move API.md to README.md
  • Keep the table of contents with links to API calls
  • Do we need: Async Crypto Endeavour #12 (comment) ? When is it used?

expect(err).to.not.exist
if (err) {
return done(err)
}
Copy link
Member

Choose a reason for hiding this comment

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

  • No need for this check

const curveLengths = {
'P-256': 32,
'P-384': 48,
'P-521': 66
Copy link
Member

Choose a reason for hiding this comment

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

These lengths, are not equal to there lenghts:

const lengths = {
'P-256': 65,
'P-384': 97,
'P-521': 133
Copy link
Member

Choose a reason for hiding this comment

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

key = crypto.generateKeyPair('RSA', 2048)
before((done) => {
crypto.generateKeyPair('RSA', 2048, (err, _key) => {
if (err) return done(err)
Copy link
Member

Choose a reason for hiding this comment

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

Weird that aegir didn't fail to lint this one. It is missing the curly

@kumavis
Copy link

kumavis commented Oct 20, 2016

☕️ I am happily awaiting this PR being merged
let me know if I can assist in any way 🙆

@daviddias daviddias changed the title Replace elliptic.js and node-forge with webcrypto Async Crypto Endeavour Oct 30, 2016
@@ -44,43 +58,91 @@ npm install --save libp2p-crypto
```js
const crypto = require('libp2p-crypto')

var keyPair = crypto.generateKeyPair('RSA', 2048)
crypto.generateKeyPair('RSA', 2048, (err, key) => {
})
Copy link
Member

Choose a reason for hiding this comment

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

Is it key or keyPair? Wanna show an example of logging out each?

@JustinDrake
Copy link

@daviddias
Copy link
Member

@JustinDrake check https://github.com/libp2p/js-libp2p-crypto/blob/master/stats.md

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants