-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: use webcrypto over tweetnacl #1
Conversation
first issue: |
ok as for slowness two things:
|
ok on 2nd thought yes you do need to probably do need to derive the password on each message since it's for one off communications that have to include the salt with them, so the real speed difference is just that with nacl you could brute force the password to a message very easily while with one you couldn't because it takes longer |
@calvinmetcalf Hey, thanks for this feedback! It really helps. That confirmation that I'm doing things right-ish here is good motive to keep at it. |
Pull Request Test Coverage Report for Build 820605883Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
I've just added some rudimentary cross-platform support along with mochify for testing. Everything seems to be working properly now. Basically, we use webcrypto if we can find it, or we fallback to using NodeJS crypto. The differences get disambiguated during import into an intermediate object |
The major drawback here is that bundling NodeJS crypto adds about 500KB to a browserify bundle. For comparison, using tweetnacl, the bundle size is 50KB. (Both of these values are on a minified bundle.) That's considerable!! I sure hope we can shrink it down. @calvinmetcalf Any ideas here? |
OK, so I was able to get as low as 375KB:
Hm. |
Looks like others have taken issue with the size of crypto-browserify as well. It looks like it's getting a nice refresh but I don't know when to expect bundling optimizations from it. For now I'm content to leave this PR open for further discussion while the main branch retains the use of tweetnacl. Given the status of its audit, I am not inclined to consider it a vulnerability. |
Had a thought and my hand slipped and now the bundle is only 26kb. Essentially you tell Browserify to explicitly not bundle Downstream users will still face this bundling problem until crypto-browserify sees the relevant improvements. |
ok so you have a few choices, one is that almost all the crypto packages are available separately so you can do
that way you will only get the bits you need in node the other option is that you have a file crypto.node.js module.exports = require('crypto').webcrypto; and another one called crypto.browser.js module.exports = global.crypto; // don't use window, you might be in a web worker and then in your package.json file you put in "browser": {
"./crypto.node.js": "./crypto.browser.js"
} that'll swap them out for you at bundle time |
Thanks Calvin, that's great. I'll see what I can do. |
Bundle clocks in now at 3.7kb but flops on NodeJS <16 and any browser that hasn't got subtle crypto. |
there may be a node only polyfill that you could prevent from being bundled, the real question is how bad is browser support and do you want to include a polyfill |
I was able to add support for node 12 and 14 and the bundle only grew to 4.5kb, which is fine. The trick was not compiling |
Build fixed. I think my only remaining worry is that only rather recent browsers are supported by this model -- I had to update my local firefox to get it to work outside of the testing environment -- but I'm otherwise ready to hit merge. |
OK. I've added a note to the readme about not excluding crypto from bundling in order to support older browsers. That's enough compatibility for me! |
0b9ecfd
to
9edc3c8
Compare
OK! Last check. So far we've got:
I'm going to let this sit for comment for about 24hr and then do a squash merge. |
After some time and reflection, I don't believe this is the best way forward right now. Rather than rely on a complex web of unaudited dependencies to facilitate native-ish cryptography, I'm going to stick with the thing that has been audited. |
This PR swaps out TweetNaCl for webcrypto, a browser standard and experimental NodeJS API.
Unfortunately the result is... very slow. And it doesn't appear to work in the browser:
As for the performance...
Each encryption and decryption operation takes about 5ms. This is astonishingly slow relative to TweetNaCl, making me reticent to accept it as an alternative.
Comments welcome.