diff --git a/.travis.yml b/.travis.yml index c159f6a..48bd1a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,18 @@ language: node_js node_js: - lts/* +env: + global: + - MOZ_HEADLESS=1 +services: + - xvfb +addons: + firefox: latest + chrome: stable +cache: + npm: false + +script: + - npm test + - xvfb-run --auto-servernum npm run test-browser -- --browser chrome + - xvfb-run --auto-servernum npm run test-browser -- --browser firefox diff --git a/crypto_verify.js b/crypto_verify.js index 1dfe154..f23ff3d 100644 --- a/crypto_verify.js +++ b/crypto_verify.js @@ -1,11 +1,7 @@ /* eslint-disable camelcase */ -const assert = require('nanoassert') - module.exports = { crypto_verify_16, - crypto_verify_32, - sodium_memcmp, - sodium_is_zero + crypto_verify_32 } function vn (x, xi, y, yi, n) { @@ -14,6 +10,11 @@ function vn (x, xi, y, yi, n) { return (1 & ((d - 1) >>> 8)) - 1 } +// Make non enumerable as this is an internal function +Object.defineProperty(module.exports, 'vn', { + value: vn +}) + function crypto_verify_16 (x, xi, y, yi) { return vn(x, xi, y, yi, 16) } @@ -21,15 +22,3 @@ function crypto_verify_16 (x, xi, y, yi) { function crypto_verify_32 (x, xi, y, yi) { return vn(x, xi, y, yi, 32) } - -function sodium_memcmp (a, b) { - assert(a.byteLength === b.byteLength, 'buffers must be the same size') - - return vn(a, 0, b, 0, a.byteLength) === 0 -} - -function sodium_is_zero (arr) { - var d = 0 - for (let i = 0; i < arr.length; i++) d |= arr[i] - return d === 0 -} diff --git a/helpers.js b/helpers.js new file mode 100644 index 0000000..389f5c1 --- /dev/null +++ b/helpers.js @@ -0,0 +1,31 @@ +/* eslint-disable camelcase */ +const assert = require('nanoassert') +const { vn } = require('./crypto_verify') + +function sodium_increment (n) { + const nlen = n.byteLength + var c = 1 + for (var i = 0; i < nlen; i++) { + c += n[i] + n[i] = c + c >>= 8 + } +} + +function sodium_memcmp (a, b) { + assert(a.byteLength === b.byteLength, 'buffers must be the same size') + + return vn(a, 0, b, 0, a.byteLength) === 0 +} + +function sodium_is_zero (arr) { + var d = 0 + for (let i = 0; i < arr.length; i++) d |= arr[i] + return d === 0 +} + +module.exports = { + sodium_increment, + sodium_memcmp, + sodium_is_zero +} diff --git a/index.js b/index.js index 717ce28..48a88bb 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,10 @@ // Implementation derived from TweetNaCl version 20140427. // See for details: http://tweetnacl.cr.yp.to/ +forward(require('./randombytes')) forward(require('./memory')) +forward(require('./helpers')) +forward(require('./crypto_verify')) forward(require('./crypto_box')) forward(require('./crypto_generichash')) forward(require('./crypto_hash')) @@ -23,8 +26,6 @@ forward(require('./crypto_shorthash')) forward(require('./crypto_sign')) forward(require('./crypto_stream')) forward(require('./crypto_stream_chacha20')) -forward(require('./crypto_verify')) -forward(require('./randombytes')) function forward (submodule) { Object.keys(submodule).forEach(function (prop) { diff --git a/memory.js b/memory.js index e641d6b..4d8aa4f 100644 --- a/memory.js +++ b/memory.js @@ -1,14 +1,23 @@ /* eslint-disable camelcase */ +var MessageChannel = global.MessageChannel +if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads')) function sodium_malloc (n) { return new Uint8Array(n) } +const sink = new MessageChannel() +function sodium_free (n) { + sodium_memzero(n) + sink.port1.postMessage(n.buffer, [n.buffer]) +} + function sodium_memzero (arr) { arr.fill(0) } module.exports = { sodium_malloc, + sodium_free, sodium_memzero } diff --git a/package.json b/package.json index b009ca4..8f812da 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ "xsalsa20": "^1.0.0" }, "devDependencies": { - "browser-run": "^7.0.2", "browserify": "^16.5.1", "sodium-test": "^0.9.0", "standard": "^14.3.4", @@ -31,10 +30,9 @@ "crypto": "crypto" }, "scripts": { - "browser": "browserify test.js | browser-run", - "browser-manual": "browserify test.js | tape-run", "pretest": "standard", - "test": "node test.js" + "test": "node test.js", + "test-browser": "browserify test.js | tape-run" }, "repository": { "type": "git",