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

Commit

Permalink
fix: switch to protobufjs (#107)
Browse files Browse the repository at this point in the history
rm unsafe-eval
  • Loading branch information
dignifiedquire authored and daviddias committed Sep 5, 2017
1 parent e0b916a commit dc2793f
Show file tree
Hide file tree
Showing 9 changed files with 583 additions and 35 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/keys/keys.proto.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ test/test-data/go-ipfs-repo/LOG.old

# while testing npm5
package-lock.json
yarn.lock
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"scripts": {
"lint": "aegir-lint",
"build": "aegir-build",
"build-proto": "pbjs --wrap commonjs --target static-module src/keys/keys.proto > src/keys/keys.proto.js",
"test": "aegir-test",
"test:node": "aegir-test --env node",
"test:browser": "aegir-test --env browser",
Expand All @@ -37,7 +38,7 @@
"libp2p-crypto-secp256k1": "~0.2.2",
"multihashing-async": "~0.4.6",
"pem-jwk": "^1.5.1",
"protocol-buffers": "^3.2.1",
"protobufjs": "^6.8.0",
"rsa-pem-to-jwk": "^1.1.3",
"safe-buffer": "^5.1.1",
"tweetnacl": "^1.0.0",
Expand Down Expand Up @@ -78,4 +79,4 @@
"greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>",
"nikuda <nikuda@gmail.com>"
]
}
}
55 changes: 55 additions & 0 deletions src/aes/index-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

const asm = require('asmcrypto.js')
const setImmediate = require('async/setImmediate')

exports.create = function (key, iv, callback) {
const done = (err, res) => setImmediate(() => callback(err, res))

if (key.length !== 16 && key.length !== 32) {
return done(new Error('Invalid key length'))
}

const enc = new asm.AES_CTR.Encrypt({
key: key,
nonce: iv
})
const dec = new asm.AES_CTR.Decrypt({
key: key,
nonce: iv
})

const res = {
encrypt (data, cb) {
const done = (err, res) => setImmediate(() => cb(err, res))

let res
try {
res = Buffer.from(
enc.process(data).result
)
} catch (err) {
return done(err)
}

done(null, res)
},

decrypt (data, cb) {
const done = (err, res) => setImmediate(() => cb(err, res))

let res
try {
res = Buffer.from(
dec.process(data).result
)
} catch (err) {
return done(err)
}

done(null, res)
}
}

done(null, res)
}
10 changes: 6 additions & 4 deletions src/keys/ed25519-class.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'

const multihashing = require('multihashing-async')
const protobuf = require('protocol-buffers')
const Buffer = require('safe-buffer').Buffer

const crypto = require('./ed25519')
const pbm = protobuf(require('./keys.proto'))
const pbm = require('./keys.proto.js')

class Ed25519PublicKey {
constructor (key) {
Expand All @@ -22,10 +21,11 @@ class Ed25519PublicKey {
}

get bytes () {
return pbm.PublicKey.encode({
const msg = pbm.PublicKey.create({
Type: pbm.KeyType.Ed25519,
Data: this.marshal()
})
return Buffer.from(pbm.PublicKey.encode(msg).finish())
}

equals (key) {
Expand Down Expand Up @@ -64,10 +64,12 @@ class Ed25519PrivateKey {
}

get bytes () {
return pbm.PrivateKey.encode({
const msg = pbm.PrivateKey.create({
Type: pbm.KeyType.Ed25519,
Data: this.marshal()
})

return Buffer.from(pbm.PrivateKey.encode(msg).finish())
}

equals (key) {
Expand Down
17 changes: 9 additions & 8 deletions src/keys/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const protobuf = require('protocol-buffers')
const keysPBM = protobuf(require('./keys.proto'))
const keysPBM = require('./keys.proto.js')

exports = module.exports

Expand Down Expand Up @@ -50,15 +49,16 @@ exports.generateKeyPairFromSeed = (type, seed, bits, cb) => {
// representative object
exports.unmarshalPublicKey = (buf) => {
const decoded = keysPBM.PublicKey.decode(buf)
const data = Buffer.from(decoded.Data)

switch (decoded.Type) {
case keysPBM.KeyType.RSA:
return supportedKeys.rsa.unmarshalRsaPublicKey(decoded.Data)
return supportedKeys.rsa.unmarshalRsaPublicKey(data)
case keysPBM.KeyType.Ed25519:
return supportedKeys.ed25519.unmarshalEd25519PublicKey(decoded.Data)
return supportedKeys.ed25519.unmarshalEd25519PublicKey(data)
case keysPBM.KeyType.Secp256k1:
if (supportedKeys.secp256k1) {
return supportedKeys.secp256k1.unmarshalSecp256k1PublicKey(decoded.Data)
return supportedKeys.secp256k1.unmarshalSecp256k1PublicKey(data)
} else {
throw new Error('secp256k1 support requires libp2p-crypto-secp256k1 package')
}
Expand All @@ -81,15 +81,16 @@ exports.marshalPublicKey = (key, type) => {
// representative object
exports.unmarshalPrivateKey = (buf, callback) => {
const decoded = keysPBM.PrivateKey.decode(buf)
const data = Buffer.from(decoded.Data)

switch (decoded.Type) {
case keysPBM.KeyType.RSA:
return supportedKeys.rsa.unmarshalRsaPrivateKey(decoded.Data, callback)
return supportedKeys.rsa.unmarshalRsaPrivateKey(data, callback)
case keysPBM.KeyType.Ed25519:
return supportedKeys.ed25519.unmarshalEd25519PrivateKey(decoded.Data, callback)
return supportedKeys.ed25519.unmarshalEd25519PrivateKey(data, callback)
case keysPBM.KeyType.Secp256k1:
if (supportedKeys.secp256k1) {
return supportedKeys.secp256k1.unmarshalSecp256k1PrivateKey(decoded.Data, callback)
return supportedKeys.secp256k1.unmarshalSecp256k1PrivateKey(data, callback)
} else {
return callback(new Error('secp256k1 support requires libp2p-crypto-secp256k1 package'))
}
Expand Down
15 changes: 15 additions & 0 deletions src/keys/keys.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
enum KeyType {
RSA = 0;
Ed25519 = 1;
Secp256k1 = 2;
}

message PublicKey {
required KeyType Type = 1;
required bytes Data = 2;
}

message PrivateKey {
required KeyType Type = 1;
required bytes Data = 2;
}
Loading

0 comments on commit dc2793f

Please sign in to comment.