diff --git a/.aegir.js b/.aegir.js index 25fbe20..3faa51b 100644 --- a/.aegir.js +++ b/.aegir.js @@ -1,3 +1,3 @@ module.exports = { - bundlesize: { maxSize: '155kB' } + bundlesize: { maxSize: '124kB' } } diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index fb5e189..0000000 --- a/.eslintignore +++ /dev/null @@ -1 +0,0 @@ -src/keys/keys.proto.js \ No newline at end of file diff --git a/package.json b/package.json index 098d204..e94108e 100644 --- a/package.json +++ b/package.json @@ -37,25 +37,20 @@ ], "license": "MIT", "dependencies": { - "asmcrypto.js": "^2.3.2", - "bn.js": "^5.0.0", - "browserify-aes": "^1.2.0", - "bs58": "^4.0.1", + "buffer": "^5.5.0", "err-code": "^2.0.0", "iso-random-stream": "^1.1.0", "keypair": "^1.0.1", - "libp2p-crypto-secp256k1": "~0.4.0", - "multihashing-async": "~0.8.0", + "libp2p-crypto-secp256k1": "^0.4.2", + "multibase": "^0.6.0", + "multihashing-async": "^0.8.1", "node-forge": "~0.9.1", "pem-jwk": "^2.0.0", "protons": "^1.0.1", - "rsa-pem-to-jwk": "^1.1.3", - "tweetnacl": "^1.0.1", "ursa-optional": "~0.10.1" }, "devDependencies": { - "@types/bn.js": "^4.11.6", - "@types/chai": "^4.2.7", + "@types/chai": "^4.2.11", "@types/chai-string": "^1.4.2", "@types/dirty-chai": "^2.0.2", "@types/mocha": "^7.0.1", diff --git a/src/aes/ciphers-browser.js b/src/aes/ciphers-browser.js index 5aee05a..4e8f160 100644 --- a/src/aes/ciphers-browser.js +++ b/src/aes/ciphers-browser.js @@ -1,8 +1,27 @@ 'use strict' - -const crypto = require('browserify-aes') +const { Buffer } = require('buffer') +require('node-forge/lib/aes') +const forge = require('node-forge/lib/forge') module.exports = { - createCipheriv: crypto.createCipheriv, - createDecipheriv: crypto.createDecipheriv + createCipheriv: (mode, key, iv) => { + const cipher2 = forge.cipher.createCipher('AES-CTR', key.toString('binary')) + cipher2.start({ iv: iv.toString('binary') }) + return { + update: (data) => { + cipher2.update(forge.util.createBuffer(data.toString('binary'))) + return Buffer.from(cipher2.output.getBytes(), 'binary') + } + } + }, + createDecipheriv: (mode, key, iv) => { + const cipher2 = forge.cipher.createDecipher('AES-CTR', key.toString('binary')) + cipher2.start({ iv: iv.toString('binary') }) + return { + update: (data) => { + cipher2.update(forge.util.createBuffer(data.toString('binary'))) + return Buffer.from(cipher2.output.getBytes(), 'binary') + } + } + } } diff --git a/src/aes/index-browser.js b/src/aes/index-browser.js deleted file mode 100644 index a806917..0000000 --- a/src/aes/index-browser.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict' - -const asm = require('asmcrypto.js') -const validateCipherMode = require('./cipher-mode') - -exports.create = async function (key, iv) { // eslint-disable-line require-await - // Throws an error if mode is invalid - validateCipherMode(key) - - const enc = new asm.AES_CTR.Encrypt({ - key: key, - nonce: iv - }) - const dec = new asm.AES_CTR.Decrypt({ - key: key, - nonce: iv - }) - - const res = { - async encrypt (data) { // eslint-disable-line require-await - return Buffer.from( - enc.process(data).result - ) - }, - - async decrypt (data) { // eslint-disable-line require-await - return Buffer.from( - dec.process(data).result - ) - } - } - - return res -} diff --git a/src/hmac/index-browser.js b/src/hmac/index-browser.js index 1eeb7df..cda9da2 100644 --- a/src/hmac/index-browser.js +++ b/src/hmac/index-browser.js @@ -1,5 +1,5 @@ 'use strict' - +const { Buffer } = require('buffer') const webcrypto = require('../webcrypto') const lengths = require('./lengths') diff --git a/src/keys/ed25519-class.js b/src/keys/ed25519-class.js index b9f7106..1cba96f 100644 --- a/src/keys/ed25519-class.js +++ b/src/keys/ed25519-class.js @@ -1,8 +1,9 @@ 'use strict' -const multihashing = require('multihashing-async') +const { Buffer } = require('buffer') +const sha = require('multihashing-async/src/sha') const protobuf = require('protons') -const bs58 = require('bs58') +const multibase = require('multibase') const errcode = require('err-code') const crypto = require('./ed25519') @@ -33,7 +34,7 @@ class Ed25519PublicKey { } async hash () { // eslint-disable-line require-await - return multihashing(this.bytes, 'sha2-256') + return sha.multihashing(this.bytes, 'sha2-256') } } @@ -69,7 +70,7 @@ class Ed25519PrivateKey { } async hash () { // eslint-disable-line require-await - return multihashing(this.bytes, 'sha2-256') + return sha.multihashing(this.bytes, 'sha2-256') } /** @@ -83,7 +84,7 @@ class Ed25519PrivateKey { */ async id () { const hash = await this.public.hash() - return bs58.encode(hash) + return multibase.encode('base58btc', hash).toString().slice(1) } } @@ -100,13 +101,13 @@ function unmarshalEd25519PublicKey (bytes) { } async function generateKeyPair () { - const { secretKey, publicKey } = await crypto.generateKey() - return new Ed25519PrivateKey(secretKey, publicKey) + const { privateKey, publicKey } = await crypto.generateKey() + return new Ed25519PrivateKey(privateKey, publicKey) } async function generateKeyPairFromSeed (seed) { - const { secretKey, publicKey } = await crypto.generateKeyFromSeed(seed) - return new Ed25519PrivateKey(secretKey, publicKey) + const { privateKey, publicKey } = await crypto.generateKeyFromSeed(seed) + return new Ed25519PrivateKey(privateKey, publicKey) } function ensureKey (key, length) { diff --git a/src/keys/ed25519.js b/src/keys/ed25519.js index cbe6c86..140cc14 100644 --- a/src/keys/ed25519.js +++ b/src/keys/ed25519.js @@ -1,23 +1,24 @@ 'use strict' -const nacl = require('tweetnacl') - -exports.publicKeyLength = nacl.sign.publicKeyLength -exports.privateKeyLength = nacl.sign.secretKeyLength +require('node-forge/lib/ed25519') +const forge = require('node-forge/lib/forge') +exports.publicKeyLength = forge.pki.ed25519.constants.PUBLIC_KEY_BYTE_LENGTH +exports.privateKeyLength = forge.pki.ed25519.constants.PRIVATE_KEY_BYTE_LENGTH exports.generateKey = async function () { // eslint-disable-line require-await - return nacl.sign.keyPair() + return forge.pki.ed25519.generateKeyPair() } // seed should be a 32 byte uint8array exports.generateKeyFromSeed = async function (seed) { // eslint-disable-line require-await - return nacl.sign.keyPair.fromSeed(seed) + return forge.pki.ed25519.generateKeyPair({ seed }) } exports.hashAndSign = async function (key, msg) { // eslint-disable-line require-await - return Buffer.from(nacl.sign.detached(msg, key)) + return forge.pki.ed25519.sign({ message: msg, privateKey: key }) + // return Buffer.from(nacl.sign.detached(msg, key)) } exports.hashAndVerify = async function (key, sig, msg) { // eslint-disable-line require-await - return nacl.sign.detached.verify(msg, sig, key) + return forge.pki.ed25519.verify({ signature: sig, message: msg, publicKey: key }) } diff --git a/src/keys/index.js b/src/keys/index.js index 7775c71..a05d8cc 100644 --- a/src/keys/index.js +++ b/src/keys/index.js @@ -1,9 +1,9 @@ 'use strict' +const { Buffer } = require('buffer') const protobuf = require('protons') const keysPBM = protobuf(require('./keys.proto')) require('node-forge/lib/asn1') -require('node-forge/lib/rsa') require('node-forge/lib/pbe') const forge = require('node-forge/lib/forge') const errcode = require('err-code') diff --git a/src/keys/key-stretcher.js b/src/keys/key-stretcher.js index c9c5366..4870620 100644 --- a/src/keys/key-stretcher.js +++ b/src/keys/key-stretcher.js @@ -1,5 +1,5 @@ 'use strict' - +const { Buffer } = require('buffer') const errcode = require('err-code') const hmac = require('../hmac') diff --git a/src/keys/rsa-browser.js b/src/keys/rsa-browser.js index ffdd646..be78717 100644 --- a/src/keys/rsa-browser.js +++ b/src/keys/rsa-browser.js @@ -1,5 +1,6 @@ 'use strict' +const { Buffer } = require('buffer') const webcrypto = require('../webcrypto') const randomBytes = require('../random-bytes') diff --git a/src/keys/rsa-class.js b/src/keys/rsa-class.js index 6a4775c..9cce175 100644 --- a/src/keys/rsa-class.js +++ b/src/keys/rsa-class.js @@ -1,14 +1,14 @@ 'use strict' -const multihashing = require('multihashing-async') +const sha = require('multihashing-async/src/sha') const protobuf = require('protons') -const bs58 = require('bs58') +const multibase = require('multibase') const errcode = require('err-code') const crypto = require('./rsa') const pbm = protobuf(require('./keys.proto')) require('node-forge/lib/sha512') -require('node-forge/lib/pbe') +require('node-forge/lib/ed25519') const forge = require('node-forge/lib/forge') class RsaPublicKey { @@ -40,7 +40,7 @@ class RsaPublicKey { } async hash () { // eslint-disable-line require-await - return multihashing(this.bytes, 'sha2-256') + return sha.multihashing(this.bytes, 'sha2-256') } } @@ -88,7 +88,7 @@ class RsaPrivateKey { } async hash () { // eslint-disable-line require-await - return multihashing(this.bytes, 'sha2-256') + return sha.multihashing(this.bytes, 'sha2-256') } /** @@ -102,7 +102,7 @@ class RsaPrivateKey { */ async id () { const hash = await this.public.hash() - return bs58.encode(hash) + return multibase.encode('base58btc', hash).toString().slice(1) } /** diff --git a/test/aes/aes.spec.js b/test/aes/aes.spec.js index 1c0cdd7..45b4b11 100644 --- a/test/aes/aes.spec.js +++ b/test/aes/aes.spec.js @@ -2,7 +2,7 @@ /* eslint-disable valid-jsdoc */ /* eslint-env mocha */ 'use strict' - +const { Buffer } = require('buffer') const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect diff --git a/test/fixtures/go-elliptic-key.js b/test/fixtures/go-elliptic-key.js index 8e7d00c..21dc93e 100644 --- a/test/fixtures/go-elliptic-key.js +++ b/test/fixtures/go-elliptic-key.js @@ -1,4 +1,5 @@ 'use strict' +const { Buffer } = require('buffer') module.exports = { curve: 'P-256', diff --git a/test/fixtures/go-key-ed25519.js b/test/fixtures/go-key-ed25519.js index 756bf0e..2fc7830 100644 --- a/test/fixtures/go-key-ed25519.js +++ b/test/fixtures/go-key-ed25519.js @@ -1,4 +1,5 @@ 'use strict' +const { Buffer } = require('buffer') module.exports = { // These were generated in a gore (https://github.com/motemen/gore) repl session: diff --git a/test/fixtures/go-key-rsa.js b/test/fixtures/go-key-rsa.js index 0b26092..7c822f4 100644 --- a/test/fixtures/go-key-rsa.js +++ b/test/fixtures/go-key-rsa.js @@ -1,5 +1,5 @@ 'use strict' - +const { Buffer } = require('buffer') module.exports = { private: { hash: Buffer.from([ diff --git a/test/fixtures/go-stretch-key.js b/test/fixtures/go-stretch-key.js index 141e787..8880a8e 100644 --- a/test/fixtures/go-stretch-key.js +++ b/test/fixtures/go-stretch-key.js @@ -1,5 +1,5 @@ 'use strict' - +const { Buffer } = require('buffer') module.exports = [{ cipher: 'AES-256', hash: 'SHA256', diff --git a/test/fixtures/secp256k1.js b/test/fixtures/secp256k1.js index 7851721..21f72f1 100644 --- a/test/fixtures/secp256k1.js +++ b/test/fixtures/secp256k1.js @@ -1,5 +1,6 @@ 'use strict' +const { Buffer } = require('buffer') module.exports = { // protobuf marshaled key pair generated with libp2p-crypto-secp256k1 // and marshaled with libp2p-crypto.marshalPublicKey / marshalPrivateKey diff --git a/test/helpers/test-garbage-error-handling.js b/test/helpers/test-garbage-error-handling.js index 75d8ab2..fbe978f 100644 --- a/test/helpers/test-garbage-error-handling.js +++ b/test/helpers/test-garbage-error-handling.js @@ -1,6 +1,7 @@ /* eslint-env mocha */ 'use strict' +const { Buffer } = require('buffer') const util = require('util') const garbage = [Buffer.from('00010203040506070809', 'hex'), {}, null, false, undefined, true, 1, 0, Buffer.from(''), 'aGVsbG93b3JsZA==', 'helloworld', ''] diff --git a/test/hmac/hmac.spec.js b/test/hmac/hmac.spec.js index 72d5372..75fd997 100644 --- a/test/hmac/hmac.spec.js +++ b/test/hmac/hmac.spec.js @@ -1,7 +1,7 @@ /* eslint max-nested-callbacks: ["error", 8] */ /* eslint-env mocha */ 'use strict' - +const { Buffer } = require('buffer') const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect diff --git a/test/keys/ed25519.spec.js b/test/keys/ed25519.spec.js index 1fc2716..bfedbec 100644 --- a/test/keys/ed25519.spec.js +++ b/test/keys/ed25519.spec.js @@ -1,6 +1,7 @@ /* eslint-env mocha */ 'use strict' +const { Buffer } = require('buffer') const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect diff --git a/test/keys/rsa.spec.js b/test/keys/rsa.spec.js index dc8db15..cc4daec 100644 --- a/test/keys/rsa.spec.js +++ b/test/keys/rsa.spec.js @@ -2,6 +2,7 @@ /* eslint-env mocha */ 'use strict' +const { Buffer } = require('buffer') const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect diff --git a/test/util.spec.js b/test/util.spec.js index 5c362d1..cd7832e 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -3,6 +3,7 @@ 'use strict' const chai = require('chai') +const { Buffer } = require('buffer') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai)