Compare commits
10 Commits
3c18d11279
...
a4f4a5f85e
Author | SHA1 | Date | |
---|---|---|---|
|
a4f4a5f85e | ||
|
3fdf60f032 | ||
|
ba83828004 | ||
|
a2fd85dc50 | ||
|
53bde13131 | ||
|
39166a8f0d | ||
|
13f0a45537 | ||
|
3c259701d1 | ||
|
bd98514832 | ||
|
57111936e7 |
117
README.md
117
README.md
|
@ -1,5 +1,116 @@
|
||||||
# eccrypto-torus
|
# eccrypto [![Build Status](https://travis-ci.org/bitchan/eccrypto.svg?branch=master)](https://travis-ci.org/bitchan/eccrypto)
|
||||||
|
|
||||||
Fork of [![NPM](https://nodei.co/npm/eccrypto.png)](https://www.npmjs.com/package/eccrypto)
|
[![NPM](https://nodei.co/npm/eccrypto.png)](https://www.npmjs.com/package/eccrypto)
|
||||||
|
|
||||||
Fixes https://github.com/bitchan/eccrypto/pull/64
|
JavaScript Elliptic curve cryptography library for both browserify and node.
|
||||||
|
|
||||||
|
## Motivation
|
||||||
|
|
||||||
|
There is currently not any isomorphic ECC library which provides ECDSA, ECDH and ECIES for both Node.js and Browser and uses the fastest implementation available (e.g. [secp256k1-node](https://github.com/wanderer/secp256k1-node) is much faster than other libraries but can be used only on Node.js). So `eccrypto` is an attempt to create one.
|
||||||
|
|
||||||
|
## Implementation details
|
||||||
|
|
||||||
|
With the help of browserify `eccrypto` provides different implementations for Browser and Node.js with the same API. Because WebCryptoAPI defines asynchronous promise-driven API, the implementation for Node needs to use promises too.
|
||||||
|
|
||||||
|
* Use Node.js crypto module/library bindings where possible
|
||||||
|
* Use WebCryptoAPI where possible
|
||||||
|
* Promise-driven API
|
||||||
|
* Only secp256k1 curve, only SHA-512 (KDF), HMAC-SHA-256 (HMAC) and AES-256-CBC for ECIES
|
||||||
|
* Compressed key support
|
||||||
|
|
||||||
|
### Native crypto API limitations
|
||||||
|
|
||||||
|
#### crypto
|
||||||
|
|
||||||
|
ECDH only works in Node 0.11+ (see https://github.com/joyent/node/pull/5854), ECDSA only supports keys in PEM format (see https://github.com/joyent/node/issues/6904) and ECIES is not supported at all.
|
||||||
|
|
||||||
|
#### WebCryptoAPI
|
||||||
|
|
||||||
|
ECDSA and ECDH are supported in Chrome [only on Windows](https://sites.google.com/a/chromium.org/dev/blink/webcrypto#TOC-Supported-algorithms-as-of-Chrome-41-) (see also [bug 338883](https://code.google.com/p/chromium/issues/detail?id=338883)), aren't supported by Firefox (fixed only in 36.0+, see [bug 1034854](https://bugzilla.mozilla.org/show_bug.cgi?id=1034854); see also [feature matrix](https://docs.google.com/spreadsheet/ccc?key=0AiAcidBZRLxndE9LWEs2R1oxZ0xidUVoU3FQbFFobkE#gid=1)) and ECIES is not defined at all in WebCryptoAPI draft. Also WebCryptoAPI [currently defines](http://www.w3.org/TR/WebCryptoAPI/#EcKeyGenParams-dictionary) only curves recommended by NIST meaning that secp256k1 (K-256) curve is not supported (see also: [[1]](http://lists.w3.org/Archives/Public/public-webcrypto-comments/2013Dec/0001.html), [[2]](https://bugzilla.mozilla.org/show_bug.cgi?id=1051509)).
|
||||||
|
|
||||||
|
So we use [seck256k1](https://www.npmjs.com/package/secp256k1) library in Node for ECDSA, [elliptic](https://www.npmjs.com/package/elliptic) in Browser for ECDSA and ECDH and implement ECIES manually with the help of native crypto API.
|
||||||
|
|
||||||
|
## Possible future goals
|
||||||
|
|
||||||
|
* Support other curves/KDF/MAC/symmetric encryption schemes
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### ECDSA
|
||||||
|
|
||||||
|
```js
|
||||||
|
var crypto = require("crypto");
|
||||||
|
var eccrypto = require("eccrypto");
|
||||||
|
|
||||||
|
// A new random 32-byte private key.
|
||||||
|
var privateKey = eccrypto.generatePrivate();
|
||||||
|
// Corresponding uncompressed (65-byte) public key.
|
||||||
|
var publicKey = eccrypto.getPublic(privateKey);
|
||||||
|
|
||||||
|
var str = "message to sign";
|
||||||
|
// Always hash you message to sign!
|
||||||
|
var msg = crypto.createHash("sha256").update(str).digest();
|
||||||
|
|
||||||
|
eccrypto.sign(privateKey, msg).then(function(sig) {
|
||||||
|
console.log("Signature in DER format:", sig);
|
||||||
|
eccrypto.verify(publicKey, msg, sig).then(function() {
|
||||||
|
console.log("Signature is OK");
|
||||||
|
}).catch(function() {
|
||||||
|
console.log("Signature is BAD");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### ECDH
|
||||||
|
|
||||||
|
```js
|
||||||
|
var eccrypto = require("eccrypto");
|
||||||
|
|
||||||
|
var privateKeyA = eccrypto.generatePrivate();
|
||||||
|
var publicKeyA = eccrypto.getPublic(privateKeyA);
|
||||||
|
var privateKeyB = eccrypto.generatePrivate();
|
||||||
|
var publicKeyB = eccrypto.getPublic(privateKeyB);
|
||||||
|
|
||||||
|
eccrypto.derive(privateKeyA, publicKeyB).then(function(sharedKey1) {
|
||||||
|
eccrypto.derive(privateKeyB, publicKeyA).then(function(sharedKey2) {
|
||||||
|
console.log("Both shared keys are equal:", sharedKey1, sharedKey2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### ECIES
|
||||||
|
|
||||||
|
```js
|
||||||
|
var eccrypto = require("eccrypto");
|
||||||
|
|
||||||
|
var privateKeyA = eccrypto.generatePrivate();
|
||||||
|
var publicKeyA = eccrypto.getPublic(privateKeyA);
|
||||||
|
var privateKeyB = eccrypto.generatePrivate();
|
||||||
|
var publicKeyB = eccrypto.getPublic(privateKeyB);
|
||||||
|
|
||||||
|
// Encrypting the message for B.
|
||||||
|
eccrypto.encrypt(publicKeyB, Buffer.from("msg to b")).then(function(encrypted) {
|
||||||
|
// B decrypting the message.
|
||||||
|
eccrypto.decrypt(privateKeyB, encrypted).then(function(plaintext) {
|
||||||
|
console.log("Message to part B:", plaintext.toString());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Encrypting the message for A.
|
||||||
|
eccrypto.encrypt(publicKeyA, Buffer.from("msg to a")).then(function(encrypted) {
|
||||||
|
// A decrypting the message.
|
||||||
|
eccrypto.decrypt(privateKeyA, encrypted).then(function(plaintext) {
|
||||||
|
console.log("Message to part A:", plaintext.toString());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
eccrypto - JavaScript Elliptic curve cryptography library
|
||||||
|
|
||||||
|
Written in 2014-2015 by Kagami Hiiragi <kagami@genshiken.org>
|
||||||
|
|
||||||
|
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
|
||||||
|
|
||||||
|
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
||||||
|
|
74
package-lock.json
generated
74
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "eccrypto",
|
"name": "eccrypto",
|
||||||
"version": "1.1.3",
|
"version": "1.1.5",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -42,9 +42,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"acorn": {
|
"acorn": {
|
||||||
"version": "7.1.0",
|
"version": "7.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-7.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/acorn/-/acorn-7.1.1.tgz",
|
||||||
"integrity": "sha512-kL5CuoXA/dgxlBbVrflsflzQ3PAas7RYZB52NOm/6839iVYJgKMJ3cQJD+t2i5+qFa8h3MDpEOJiS64E8JLnSQ=="
|
"integrity": "sha512-add7dgA5ppRPxCFJoAGfMDi7PIBXq1RtGo7BhbLaxwrXPOmw8gq48Y9ozT01hUKy9byMjlR20EJhu5zlkErEkg=="
|
||||||
},
|
},
|
||||||
"acorn-node": {
|
"acorn-node": {
|
||||||
"version": "1.8.2",
|
"version": "1.8.2",
|
||||||
|
@ -176,7 +176,7 @@
|
||||||
},
|
},
|
||||||
"async": {
|
"async": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "http://registry.npmjs.org/async/-/async-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/async/-/async-1.0.0.tgz",
|
||||||
"integrity": "sha1-+PwEyjoTeErenhZBr5hXjPvWR6k=",
|
"integrity": "sha1-+PwEyjoTeErenhZBr5hXjPvWR6k=",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"optional": true
|
"optional": true
|
||||||
|
@ -802,7 +802,7 @@
|
||||||
},
|
},
|
||||||
"readable-stream": {
|
"readable-stream": {
|
||||||
"version": "2.3.6",
|
"version": "2.3.6",
|
||||||
"resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz",
|
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz",
|
||||||
"integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==",
|
"integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
|
@ -1216,17 +1216,29 @@
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"elliptic": {
|
"elliptic": {
|
||||||
"version": "6.5.1",
|
"version": "6.5.4",
|
||||||
"resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.1.tgz",
|
"resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.4.tgz",
|
||||||
"integrity": "sha512-xvJINNLbTeWQjrl6X+7eQCrIy/YPv5XCpKW6kB5mKvtnGILoLDcySuwomfdzt0BMdLNVnuRNTuzKNHj0bva1Cg==",
|
"integrity": "sha512-iLhC6ULemrljPZb+QutR5TQGB+pdW6KGD5RSegS+8sorOZT+rdQFbsQFJgvN3eRqNALqJer4oQ16YvJHlU8hzQ==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"bn.js": "^4.4.0",
|
"bn.js": "^4.11.9",
|
||||||
"brorand": "^1.0.1",
|
"brorand": "^1.1.0",
|
||||||
"hash.js": "^1.0.0",
|
"hash.js": "^1.0.0",
|
||||||
"hmac-drbg": "^1.0.0",
|
"hmac-drbg": "^1.0.1",
|
||||||
"inherits": "^2.0.1",
|
"inherits": "^2.0.4",
|
||||||
"minimalistic-assert": "^1.0.0",
|
"minimalistic-assert": "^1.0.1",
|
||||||
"minimalistic-crypto-utils": "^1.0.0"
|
"minimalistic-crypto-utils": "^1.0.1"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"bn.js": {
|
||||||
|
"version": "4.12.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.12.0.tgz",
|
||||||
|
"integrity": "sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA=="
|
||||||
|
},
|
||||||
|
"inherits": {
|
||||||
|
"version": "2.0.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
|
||||||
|
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"encodeurl": {
|
"encodeurl": {
|
||||||
|
@ -1964,7 +1976,7 @@
|
||||||
},
|
},
|
||||||
"jsonfile": {
|
"jsonfile": {
|
||||||
"version": "2.4.0",
|
"version": "2.4.0",
|
||||||
"resolved": "http://registry.npmjs.org/jsonfile/-/jsonfile-2.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-2.4.0.tgz",
|
||||||
"integrity": "sha1-NzaitCi4e72gzIO1P6PWM6NcKug=",
|
"integrity": "sha1-NzaitCi4e72gzIO1P6PWM6NcKug=",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"optional": true,
|
"optional": true,
|
||||||
|
@ -2038,12 +2050,6 @@
|
||||||
"integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==",
|
"integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"lodash": {
|
|
||||||
"version": "4.17.15",
|
|
||||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
|
|
||||||
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==",
|
|
||||||
"dev": true
|
|
||||||
},
|
|
||||||
"source-map": {
|
"source-map": {
|
||||||
"version": "0.6.1",
|
"version": "0.6.1",
|
||||||
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
|
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
|
||||||
|
@ -2064,14 +2070,6 @@
|
||||||
"lodash": "^4.17.14",
|
"lodash": "^4.17.14",
|
||||||
"minimatch": "^3.0.0",
|
"minimatch": "^3.0.0",
|
||||||
"os-shim": "^0.1.3"
|
"os-shim": "^0.1.3"
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"lodash": {
|
|
||||||
"version": "4.17.15",
|
|
||||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
|
|
||||||
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==",
|
|
||||||
"dev": true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"karma-chrome-launcher": {
|
"karma-chrome-launcher": {
|
||||||
|
@ -2157,9 +2155,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"lodash": {
|
"lodash": {
|
||||||
"version": "4.17.11",
|
"version": "4.17.19",
|
||||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz",
|
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz",
|
||||||
"integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==",
|
"integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"lodash.memoize": {
|
"lodash.memoize": {
|
||||||
|
@ -2287,7 +2285,7 @@
|
||||||
},
|
},
|
||||||
"minimist": {
|
"minimist": {
|
||||||
"version": "0.0.8",
|
"version": "0.0.8",
|
||||||
"resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
|
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
|
||||||
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
|
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
@ -2832,7 +2830,7 @@
|
||||||
},
|
},
|
||||||
"readable-stream": {
|
"readable-stream": {
|
||||||
"version": "1.1.14",
|
"version": "1.1.14",
|
||||||
"resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz",
|
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz",
|
||||||
"integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=",
|
"integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
|
@ -3355,12 +3353,6 @@
|
||||||
"graceful-fs": "^4.1.6"
|
"graceful-fs": "^4.1.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"lodash": {
|
|
||||||
"version": "4.17.15",
|
|
||||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
|
|
||||||
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==",
|
|
||||||
"dev": true
|
|
||||||
},
|
|
||||||
"ms": {
|
"ms": {
|
||||||
"version": "2.1.2",
|
"version": "2.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
|
||||||
|
|
12
package.json
12
package.json
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "eccrypto-torus",
|
"name": "eccrypto",
|
||||||
"version": "1.1.3",
|
"version": "1.1.6",
|
||||||
"description": "JavaScript Elliptic curve cryptography library, includes fix to browser.js so that encrypt/decrypt works",
|
"description": "JavaScript Elliptic curve cryptography library",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"browser": "browser.js",
|
"browser": "browser.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -29,7 +29,7 @@
|
||||||
"elliptic",
|
"elliptic",
|
||||||
"curve"
|
"curve"
|
||||||
],
|
],
|
||||||
"author": "Zhen Yu Yong",
|
"author": "Kagami Hiiragi",
|
||||||
"license": "CC0-1.0",
|
"license": "CC0-1.0",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/bitchan/eccrypto/issues"
|
"url": "https://github.com/bitchan/eccrypto/issues"
|
||||||
|
@ -50,8 +50,8 @@
|
||||||
"mocha": "*"
|
"mocha": "*"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"acorn": "7.1.0",
|
"acorn": "7.1.1",
|
||||||
"elliptic": "6.5.1",
|
"elliptic": "6.5.4",
|
||||||
"es6-promise": "4.2.8",
|
"es6-promise": "4.2.8",
|
||||||
"nan": "2.14.0"
|
"nan": "2.14.0"
|
||||||
},
|
},
|
||||||
|
|
8
test.js
8
test.js
|
@ -217,14 +217,6 @@ describe("ECIES", function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should encrypt and decrypt", function() {
|
|
||||||
return eccrypto.encrypt(publicKeyA, Buffer.from("to a")).then(function(enc) {
|
|
||||||
return eccrypto.decrypt(privateKeyA, enc);
|
|
||||||
}).then(function(msg) {
|
|
||||||
expect(msg.toString()).to.equal("to a");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should encrypt and decrypt with message size > 15", function() {
|
it("should encrypt and decrypt with message size > 15", function() {
|
||||||
return eccrypto.encrypt(publicKeyA, Buffer.from("message size that is greater than 15 for sure =)")).then(function(enc) {
|
return eccrypto.encrypt(publicKeyA, Buffer.from("message size that is greater than 15 for sure =)")).then(function(enc) {
|
||||||
return eccrypto.decrypt(privateKeyA, enc);
|
return eccrypto.decrypt(privateKeyA, enc);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user