|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const pull = require('pull-stream') |
| 4 | +const debug = require('debug') |
| 5 | +const Errors = require('./errors') |
| 6 | +const xsalsa20 = require('xsalsa20') |
| 7 | +const KEY_LENGTH = require('./key-generator').KEY_LENGTH |
| 8 | + |
| 9 | +const log = debug('libp2p:pnet') |
| 10 | +log.trace = debug('libp2p:pnet:trace') |
| 11 | +log.err = debug('libp2p:pnet:err') |
| 12 | + |
| 13 | +/** |
| 14 | + * Creates a pull stream to encrypt messages in a private network |
| 15 | + * |
| 16 | + * @param {Buffer} nonce The nonce to use in encryption |
| 17 | + * @param {Buffer} psk The private shared key to use in encryption |
| 18 | + * @returns {PullStream} a through stream |
| 19 | + */ |
| 20 | +module.exports.createBoxStream = (nonce, psk) => { |
| 21 | + const xor = xsalsa20(nonce, psk) |
| 22 | + return pull( |
| 23 | + ensureBuffer(), |
| 24 | + pull.map((chunk) => { |
| 25 | + return xor.update(chunk, chunk) |
| 26 | + }) |
| 27 | + ) |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Creates a pull stream to decrypt messages in a private network |
| 32 | + * |
| 33 | + * @param {Object} remote Holds the nonce of the peer |
| 34 | + * @param {Buffer} psk The private shared key to use in decryption |
| 35 | + * @returns {PullStream} a through stream |
| 36 | + */ |
| 37 | +module.exports.createUnboxStream = (remote, psk) => { |
| 38 | + let xor |
| 39 | + return pull( |
| 40 | + ensureBuffer(), |
| 41 | + pull.map((chunk) => { |
| 42 | + if (!xor) { |
| 43 | + xor = xsalsa20(remote.nonce, psk) |
| 44 | + log.trace('Decryption enabled') |
| 45 | + } |
| 46 | + |
| 47 | + return xor.update(chunk, chunk) |
| 48 | + }) |
| 49 | + ) |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Decode the version 1 psk from the given Buffer |
| 54 | + * |
| 55 | + * @param {Buffer} pskBuffer |
| 56 | + * @throws {INVALID_PSK} |
| 57 | + * @returns {Object} The PSK metadata (tag, codecName, psk) |
| 58 | + */ |
| 59 | +module.exports.decodeV1PSK = (pskBuffer) => { |
| 60 | + try { |
| 61 | + // This should pull from multibase/multicodec to allow for |
| 62 | + // more encoding flexibility. Ideally we'd consume the codecs |
| 63 | + // from the buffer line by line to evaluate the next line |
| 64 | + // programatically instead of making assumptions about the |
| 65 | + // encodings of each line. |
| 66 | + const metadata = pskBuffer.toString().split(/(?:\r\n|\r|\n)/g) |
| 67 | + const pskTag = metadata.shift() |
| 68 | + const codec = metadata.shift() |
| 69 | + const psk = Buffer.from(metadata.shift(), 'hex') |
| 70 | + |
| 71 | + if (psk.byteLength !== KEY_LENGTH) { |
| 72 | + throw new Error(Errors.INVALID_PSK) |
| 73 | + } |
| 74 | + |
| 75 | + return { |
| 76 | + tag: pskTag, |
| 77 | + codecName: codec, |
| 78 | + psk: psk |
| 79 | + } |
| 80 | + } catch (err) { |
| 81 | + throw new Error(Errors.INVALID_PSK) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Returns a through pull-stream that ensures the passed chunks |
| 87 | + * are buffers instead of strings |
| 88 | + * @returns {PullStream} a through stream |
| 89 | + */ |
| 90 | +function ensureBuffer () { |
| 91 | + return pull.map((chunk) => { |
| 92 | + if (typeof chunk === 'string') { |
| 93 | + return Buffer.from(chunk, 'utf-8') |
| 94 | + } |
| 95 | + |
| 96 | + return chunk |
| 97 | + }) |
| 98 | +} |
0 commit comments