Skip to content

Pure JS Crypto Implementation #122

@lauckhart

Description

@lauckhart

Feedback appreciated!

Goals:

  • Move remaining matter.js tests from matter-node.js
  • Enable full functionality in non-node environments without proper crypto primitives.

Proposal:

  • Create new matter-crypto.js package parallel to matter.js. Keeps the hair out of matter.js
  • Move Crypto.ts into matter-crypto.ts or (b) make matter-crypto.ts a test-only dependency of matter.ts (which would be a bit circular so could get weird)
  • For completeness, move existing JS crypto dependencies of matter.js (BN and elliptic) to this package as well
  • Implement one of below options for pure-JS crypto. After writing up I lean toward option 2

Option 1: Implement Crypto interface using SJCL

Pros

  • SJCL appears mature
  • AFAICT the only pure-JS library that supports all primitives matter.js requires
  • Could ingest only the subset we need with a small custom build

Cons

  • NPM package includes a subset of functionality that doesn't meet our needs, so custom build would be required
  • Custom build is simple but requires make, java and perl. Not a reasonable dependency for our build, so SCJL.js should probably go in matter.js git as part of matter-crypto.js (along with scripts to recreate)
  • Would require writing new Crypto derivative. Doesn't look terrible but this would be the slow bit
  • Would require ongoing maintenance of two Crypto implementations

Option 2: Shim node's OpenSSL style wrappers

Pros

  • Can use existing implementation: crypto-browserify
  • Fast to implement
  • crypto-browserify is highly modular so we could import only the subset we need
  • Would only need to maintain single implementation
  • Shim can automatically bypass if native crypto is available
  • This implementation could support native crypto in non-Node environments. For example, if I were to create a React Native app I would probably use react-native-fast-crypto which... Emulates node's API by wrapping OpenSSL

Cons

  • Weird dependency for crypto-matter.js on "browserify" component or including subset of crypto-browserify in crypto-matter.js (attributed of course)
  • crypto-browserify doesn't support CCM mode. There's an old PR adding it but we'd need to fork or include patched files in matter.js repo

Option 3: Implement crypto using mix of 3rd party implementations

Pros

  • Could adopt best-in-class implementation of each required feature
  • Keeps dependencies relatively clean

Cons

  • Not clearly superior to previous options as we'd still need to import some of either SJCL or browserify-aes to get CCM mode
  • Some implementations not available in NPM so would require build steps and/or checking into matter.js git
  • Probably slowest in terms of research and implementation

Option 4: New implementation of crypto primitives in matter-crypto.ts

Ha ha, j/k

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions