From 51997be612619e48cf55b3ccce0f40ff537ce42f Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Sat, 2 May 2020 14:44:35 +0200 Subject: [PATCH 1/2] remove buffer-alloc, buffer-from, buffer-equals deps --- crypto_generichash.js | 59 ++++++++++++++++++++----------------------- crypto_kdf.js | 33 +++++++++++------------- crypto_secretbox.js | 48 +++++++++++++++++------------------ crypto_shorthash.js | 13 ++++------ crypto_sign.js | 35 ++++++++++++------------- crypto_stream.js | 50 ++++++++++++++++++------------------ randombytes_buf.js | 12 ++++----- 7 files changed, 116 insertions(+), 134 deletions(-) diff --git a/crypto_generichash.js b/crypto_generichash.js index 5c144e8..c324999 100644 --- a/crypto_generichash.js +++ b/crypto_generichash.js @@ -1,23 +1,20 @@ var tape = require('tape') -var alloc = require('buffer-alloc') -var fill = require('buffer-fill') -var toBuffer = require('buffer-from') module.exports = function (sodium) { tape('crypto_generichash', function (t) { - var buf = toBuffer('Hello, World!') + var buf = Buffer.from('Hello, World!') - var out = alloc(sodium.crypto_generichash_BYTES) + var out = Buffer.alloc(sodium.crypto_generichash_BYTES) sodium.crypto_generichash(out, buf) t.same(out.toString('hex'), '511bc81dde11180838c562c82bb35f3223f46061ebde4a955c27b3f489cf1e03', 'hashed buffer') - var min = alloc(sodium.crypto_generichash_BYTES_MIN) + var min = Buffer.alloc(sodium.crypto_generichash_BYTES_MIN) sodium.crypto_generichash(min, buf) t.same(min.toString('hex'), '3895c59e4aeb0903396b5be3fbec69fe', 'hashed buffer min') - var max = alloc(sodium.crypto_generichash_BYTES_MAX) + var max = Buffer.alloc(sodium.crypto_generichash_BYTES_MAX) sodium.crypto_generichash(max, buf) var res = '7dfdb888af71eae0e6a6b751e8e3413d767ef4fa52a7993daa9ef097f7aa3d949199c113caa37c94f80cf3b22f7d9d6e4f5def4ff927830cffe4857c34be3d89' @@ -27,22 +24,22 @@ module.exports = function (sodium) { }) tape('crypto_generichash with key', function (t) { - var buf = toBuffer('Hello, World!') - var key = alloc(sodium.crypto_generichash_KEYBYTES) + var buf = Buffer.from('Hello, World!') + var key = Buffer.alloc(sodium.crypto_generichash_KEYBYTES) - fill(key, 'lo') + key.fill('lo') - var out = alloc(sodium.crypto_generichash_BYTES) + var out = Buffer.alloc(sodium.crypto_generichash_BYTES) sodium.crypto_generichash(out, buf, key) t.same(out.toString('hex'), 'f4113fe33d43c24c54627d40efa1a78838d4a6d689fd6e83c213848904fffa8c', 'hashed buffer') - var min = alloc(sodium.crypto_generichash_BYTES_MIN) + var min = Buffer.alloc(sodium.crypto_generichash_BYTES_MIN) sodium.crypto_generichash(min, buf, key) t.same(min.toString('hex'), 'c8226257b0d1c3dcf4bbc3ef79574689', 'hashed buffer min') - var max = alloc(sodium.crypto_generichash_BYTES_MAX) + var max = Buffer.alloc(sodium.crypto_generichash_BYTES_MAX) sodium.crypto_generichash(max, buf, key) var res = '763eda46f4c6c61abb4310eb8a488950e9e0667b2fca03c463dc7489e94f065b7af6063fe86b0441c3eb9052800121d55730412abb2cbe0761b1d66f9b047c1c' @@ -53,11 +50,11 @@ module.exports = function (sodium) { tape('crypto_generichash_instance', function (t) { var isntance = sodium.crypto_generichash_instance() - var buf = toBuffer('Hej, Verden') + var buf = Buffer.from('Hej, Verden') for (var i = 0; i < 10; i++) isntance.update(buf) - var out = alloc(sodium.crypto_generichash_BYTES) + var out = Buffer.alloc(sodium.crypto_generichash_BYTES) isntance.final(out) t.same(out.toString('hex'), 'cbc20f347f5dfe37dc13231cbf7eaa4ec48e585ec055a96839b213f62bd8ce00', 'streaming hash') @@ -65,15 +62,15 @@ module.exports = function (sodium) { }) tape('crypto_generichash_instance with key', function (t) { - var key = alloc(sodium.crypto_generichash_KEYBYTES) - fill(key, 'lo') + var key = Buffer.alloc(sodium.crypto_generichash_KEYBYTES) + key.fill('lo') var isntance = sodium.crypto_generichash_instance(key) - var buf = toBuffer('Hej, Verden') + var buf = Buffer.from('Hej, Verden') for (var i = 0; i < 10; i++) isntance.update(buf) - var out = alloc(sodium.crypto_generichash_BYTES) + var out = Buffer.alloc(sodium.crypto_generichash_BYTES) isntance.final(out) t.same(out.toString('hex'), '405f14acbeeb30396b8030f78e6a84bab0acf08cb1376aa200a500f669f675dc', 'streaming keyed hash') @@ -82,11 +79,11 @@ module.exports = function (sodium) { tape('crypto_generichash_instance with hash length', function (t) { var isntance = sodium.crypto_generichash_instance(null, sodium.crypto_generichash_BYTES_MIN) - var buf = toBuffer('Hej, Verden') + var buf = Buffer.from('Hej, Verden') for (var i = 0; i < 10; i++) isntance.update(buf) - var out = alloc(sodium.crypto_generichash_BYTES_MIN) + var out = Buffer.alloc(sodium.crypto_generichash_BYTES_MIN) isntance.final(out) t.same(out.toString('hex'), 'decacdcc3c61948c79d9f8dee5b6aa99', 'streaming short hash') @@ -94,15 +91,15 @@ module.exports = function (sodium) { }) tape('crypto_generichash_instance with key and hash length', function (t) { - var key = alloc(sodium.crypto_generichash_KEYBYTES) - fill(key, 'lo') + var key = Buffer.alloc(sodium.crypto_generichash_KEYBYTES) + key.fill('lo') var isntance = sodium.crypto_generichash_instance(key, sodium.crypto_generichash_BYTES_MIN) - var buf = toBuffer('Hej, Verden') + var buf = Buffer.from('Hej, Verden') for (var i = 0; i < 10; i++) isntance.update(buf) - var out = alloc(sodium.crypto_generichash_BYTES_MIN) + var out = Buffer.alloc(sodium.crypto_generichash_BYTES_MIN) isntance.final(out) t.same(out.toString('hex'), 'fb43f0ab6872cbfd39ec4f8a1bc6fb37', 'streaming short keyed hash') @@ -110,11 +107,11 @@ module.exports = function (sodium) { }) tape('crypto_generichash_batch', function (t) { - var buf = toBuffer('Hej, Verden') + var buf = Buffer.from('Hej, Verden') var batch = [] for (var i = 0; i < 10; i++) batch.push(buf) - var out = alloc(sodium.crypto_generichash_BYTES) + var out = Buffer.alloc(sodium.crypto_generichash_BYTES) sodium.crypto_generichash_batch(out, batch) t.same(out.toString('hex'), 'cbc20f347f5dfe37dc13231cbf7eaa4ec48e585ec055a96839b213f62bd8ce00', 'batch hash') @@ -122,14 +119,14 @@ module.exports = function (sodium) { }) tape('crypto_generichash_batch with key', function (t) { - var key = alloc(sodium.crypto_generichash_KEYBYTES) - fill(key, 'lo') + var key = Buffer.alloc(sodium.crypto_generichash_KEYBYTES) + key.fill('lo') - var buf = toBuffer('Hej, Verden') + var buf = Buffer.from('Hej, Verden') var batch = [] for (var i = 0; i < 10; i++) batch.push(buf) - var out = alloc(sodium.crypto_generichash_BYTES) + var out = Buffer.alloc(sodium.crypto_generichash_BYTES) sodium.crypto_generichash_batch(out, batch, key) t.same(out.toString('hex'), '405f14acbeeb30396b8030f78e6a84bab0acf08cb1376aa200a500f669f675dc', 'batch keyed hash') diff --git a/crypto_kdf.js b/crypto_kdf.js index dfbf289..ecee9a5 100644 --- a/crypto_kdf.js +++ b/crypto_kdf.js @@ -1,40 +1,37 @@ var tape = require('tape') -var alloc = require('buffer-alloc') -var toBuffer = require('buffer-from') -var equals = require('buffer-equals') module.exports = function (sodium) { tape('crypto_kdf_keygen', function (t) { - var key = alloc(sodium.crypto_kdf_KEYBYTES + 1) + var key = Buffer.alloc(sodium.crypto_kdf_KEYBYTES + 1) key[sodium.crypto_kdf_KEYBYTES] = 0xdb t.throws(function () { - sodium.crypto_kdf_keygen(alloc(1)) + sodium.crypto_kdf_keygen(Buffer.alloc(1)) }) sodium.crypto_kdf_keygen(key) - t.notEqual(key, alloc(key.length)) + t.notEqual(key, Buffer.alloc(key.length)) t.equal(key[sodium.crypto_kdf_KEYBYTES], 0xdb) t.end() }) tape('crypto_kdf_derive_from_key', function (t) { - var key = alloc(sodium.crypto_kdf_KEYBYTES) + var key = Buffer.alloc(sodium.crypto_kdf_KEYBYTES) sodium.crypto_kdf_keygen(key) - var subkey = alloc(sodium.crypto_kdf_BYTES_MIN) + var subkey = Buffer.alloc(sodium.crypto_kdf_BYTES_MIN) - sodium.crypto_kdf_derive_from_key(subkey, 0, toBuffer('context_'), key) - t.notEqual(subkey, alloc(subkey.length)) + sodium.crypto_kdf_derive_from_key(subkey, 0, Buffer.from('context_'), key) + t.notEqual(subkey, Buffer.alloc(subkey.length)) - var subkey2 = alloc(sodium.crypto_kdf_BYTES_MIN) + var subkey2 = Buffer.alloc(sodium.crypto_kdf_BYTES_MIN) - sodium.crypto_kdf_derive_from_key(subkey2, 1, toBuffer('context_'), key) + sodium.crypto_kdf_derive_from_key(subkey2, 1, Buffer.from('context_'), key) t.notEqual(subkey, subkey2) - sodium.crypto_kdf_derive_from_key(subkey2, 0, toBuffer('context_'), key) + sodium.crypto_kdf_derive_from_key(subkey2, 0, Buffer.from('context_'), key) t.same(subkey, subkey2) t.end() @@ -44,19 +41,19 @@ module.exports = function (sodium) { var fixtures = require('./fixtures/crypto_kdf.json') for (var i = 0; i < fixtures.length; i++) { - var key = toBuffer(fixtures[i].key, 'hex') + var key = Buffer.from(fixtures[i].key, 'hex') var subkeyLen = fixtures[i].subkey_len var id = fixtures[i].id - var context = toBuffer(fixtures[i].context, 'hex') + var context = Buffer.from(fixtures[i].context, 'hex') var shouldError = fixtures[i].error - var actual = alloc(subkeyLen) + var actual = Buffer.alloc(subkeyLen) try { sodium.crypto_kdf_derive_from_key(actual, id, context, key) - var expected = toBuffer(fixtures[i].subkey, 'hex') - if (equals(actual, expected) === false) { + var expected = Buffer.from(fixtures[i].subkey, 'hex') + if (Buffer.compare(actual, expected) !== 0) { assert.fail('Failed on fixture #' + i) } } catch (ex) { diff --git a/crypto_secretbox.js b/crypto_secretbox.js index 6514017..ae72fa4 100644 --- a/crypto_secretbox.js +++ b/crypto_secretbox.js @@ -1,31 +1,29 @@ var tape = require('tape') -var alloc = require('buffer-alloc') -var toBuffer = require('buffer-from') module.exports = function (sodium) { tape('crypto_secretbox_easy', function (t) { - var message = toBuffer('Hej, Verden!') - var output = alloc(message.length + sodium.crypto_secretbox_MACBYTES) + var message = Buffer.from('Hej, Verden!') + var output = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES) - var key = alloc(sodium.crypto_secretbox_KEYBYTES) + var key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES) sodium.randombytes_buf(key) - var nonce = alloc(sodium.crypto_secretbox_NONCEBYTES) + var nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES) sodium.randombytes_buf(nonce) t.throws(function () { - sodium.crypto_secretbox_easy(alloc(0), message, nonce, key) + sodium.crypto_secretbox_easy(Buffer.alloc(0), message, nonce, key) }, 'throws if output is too small') t.throws(function () { - sodium.crypto_secretbox_easy(alloc(message.length), message, nonce, key) + sodium.crypto_secretbox_easy(Buffer.alloc(message.length), message, nonce, key) }, 'throws if output is too small') sodium.crypto_secretbox_easy(output, message, nonce, key) - t.notEqual(output, alloc(output.length)) + t.notEqual(output, Buffer.alloc(output.length)) - var result = alloc(output.length - sodium.crypto_secretbox_MACBYTES) - t.notOk(sodium.crypto_secretbox_open_easy(result, output, alloc(sodium.crypto_secretbox_NONCEBYTES), key), 'could not decrypt') + var result = Buffer.alloc(output.length - sodium.crypto_secretbox_MACBYTES) + t.notOk(sodium.crypto_secretbox_open_easy(result, output, Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES), key), 'could not decrypt') t.ok(sodium.crypto_secretbox_open_easy(result, output, nonce, key), 'could decrypt') t.same(result, message, 'decrypted message is correct') @@ -34,43 +32,43 @@ module.exports = function (sodium) { }) tape('crypto_secretbox_easy overwrite buffer', function (t) { - var output = alloc(Buffer.byteLength('Hej, Verden!') + sodium.crypto_secretbox_MACBYTES) + var output = Buffer.alloc(Buffer.byteLength('Hej, Verden!') + sodium.crypto_secretbox_MACBYTES) output.write('Hej, Verden!', sodium.crypto_secretbox_MACBYTES) - var key = alloc(sodium.crypto_secretbox_KEYBYTES) + var key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES) sodium.randombytes_buf(key) - var nonce = alloc(sodium.crypto_secretbox_NONCEBYTES) + var nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES) sodium.randombytes_buf(nonce) sodium.crypto_secretbox_easy(output, output.slice(sodium.crypto_secretbox_MACBYTES), nonce, key) - t.notEqual(output, alloc(output.length)) + t.notEqual(output, Buffer.alloc(output.length)) t.ok(sodium.crypto_secretbox_open_easy(output.slice(sodium.crypto_secretbox_MACBYTES), output, nonce, key), 'could decrypt') - t.same(output.slice(sodium.crypto_secretbox_MACBYTES), toBuffer('Hej, Verden!'), 'decrypted message is correct') + t.same(output.slice(sodium.crypto_secretbox_MACBYTES), Buffer.from('Hej, Verden!'), 'decrypted message is correct') t.end() }) tape('crypto_secretbox_detached', function (t) { - var message = toBuffer('Hej, Verden!') - var output = alloc(message.length) - var mac = alloc(sodium.crypto_secretbox_MACBYTES) + var message = Buffer.from('Hej, Verden!') + var output = Buffer.alloc(message.length) + var mac = Buffer.alloc(sodium.crypto_secretbox_MACBYTES) - var key = alloc(sodium.crypto_secretbox_KEYBYTES) + var key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES) sodium.randombytes_buf(key) - var nonce = alloc(sodium.crypto_secretbox_NONCEBYTES) + var nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES) sodium.randombytes_buf(nonce) sodium.crypto_secretbox_detached(output, mac, message, nonce, key) - t.notEqual(mac, alloc(mac.length), 'mac not blank') - t.notEqual(output, alloc(output.length), 'output not blank') + t.notEqual(mac, Buffer.alloc(mac.length), 'mac not blank') + t.notEqual(output, Buffer.alloc(output.length), 'output not blank') - var result = alloc(output.length) + var result = Buffer.alloc(output.length) - t.notOk(sodium.crypto_secretbox_open_detached(result, output, mac, nonce, alloc(key.length)), 'could not decrypt') + t.notOk(sodium.crypto_secretbox_open_detached(result, output, mac, nonce, Buffer.alloc(key.length)), 'could not decrypt') t.ok(sodium.crypto_secretbox_open_detached(result, output, mac, nonce, key), 'could decrypt') t.same(result, message, 'decrypted message is correct') diff --git a/crypto_shorthash.js b/crypto_shorthash.js index 5a42a0e..42db350 100644 --- a/crypto_shorthash.js +++ b/crypto_shorthash.js @@ -1,7 +1,4 @@ var test = require('tape') -var from = require('buffer-from') -var equals = require('buffer-equals') -var alloc = require('buffer-alloc') var vectors = [ // generated from https://github.com/jedisct1/siphash-js/blob/master/test/index.js ['aON1dHrq90SbG8Hx', 'v7LyiwuCrB7EgAibPve6Yg2gLmggxE6j7ocR37EudrH_P9XX2rQK', [147, 73, 50, 63, 71, 98, 203, 42]], @@ -183,13 +180,13 @@ module.exports = function (sodium) { function run (assert) { for (var i = 0; i < vectors.length; i++) { var v = vectors[i] - var key = from(v[0]) - var message = from(v[1]) - var expected = from(v[2]) - var out = alloc(sodium.crypto_shorthash_BYTES) + var key = Buffer.from(v[0]) + var message = Buffer.from(v[1]) + var expected = Buffer.from(v[2]) + var out = Buffer.alloc(sodium.crypto_shorthash_BYTES) sodium.crypto_shorthash(out, message, key) - if (equals(out, expected) === false) { + if (Buffer.compare(out, expected) !== 0) { assert.fail('Failed on fixture #' + i) assert.end() return diff --git a/crypto_sign.js b/crypto_sign.js index 57fb78a..7408234 100644 --- a/crypto_sign.js +++ b/crypto_sign.js @@ -1,16 +1,13 @@ var test = require('tape') -var alloc = require('buffer-alloc') -var equals = require('buffer-equals') -var toBuffer = require('buffer-from') module.exports = function (sodium) { test('crypto_sign_open fixtures', function (assert) { var fixtures = require('./fixtures/crypto_sign.json') for (var i = 0; i < fixtures.length; i++) { - var publicKey = toBuffer(fixtures[i][1]) - var message = toBuffer(fixtures[i][3]) - var signed = toBuffer([].concat(fixtures[i][2], fixtures[i][3])) + var publicKey = Buffer.from(fixtures[i][1]) + var message = Buffer.from(fixtures[i][3]) + var signed = Buffer.from([].concat(fixtures[i][2], fixtures[i][3])) if (!sodium.crypto_sign_open(message, signed, publicKey)) { assert.fail('Failed on fixture #' + i) @@ -27,15 +24,15 @@ module.exports = function (sodium) { var fixtures = require('./fixtures/crypto_sign.json') for (var i = 0; i < fixtures.length; i++) { - var secretKey = toBuffer([].concat(fixtures[i][0], fixtures[i][1])) - var message = toBuffer(fixtures[i][3]) + var secretKey = Buffer.from([].concat(fixtures[i][0], fixtures[i][1])) + var message = Buffer.from(fixtures[i][3]) - var expected = toBuffer([].concat(fixtures[i][2], fixtures[i][3])) - var actual = alloc(sodium.crypto_sign_BYTES + message.length) + var expected = Buffer.from([].concat(fixtures[i][2], fixtures[i][3])) + var actual = Buffer.alloc(sodium.crypto_sign_BYTES + message.length) sodium.crypto_sign(actual, message, secretKey) - if (equals(actual, expected) === false) { + if (Buffer.compare(actual, expected) !== 0) { assert.fail('Failed on fixture #' + i) assert.end() return @@ -50,9 +47,9 @@ module.exports = function (sodium) { var fixtures = require('./fixtures/crypto_sign.json') for (var i = 0; i < fixtures.length; i++) { - var publicKey = toBuffer(fixtures[i][1]) - var message = toBuffer(fixtures[i][3]) - var signature = toBuffer(fixtures[i][2]) + var publicKey = Buffer.from(fixtures[i][1]) + var message = Buffer.from(fixtures[i][3]) + var signature = Buffer.from(fixtures[i][2]) if (!sodium.crypto_sign_verify_detached(signature, message, publicKey)) { assert.fail('Failed on fixture #' + i) @@ -69,15 +66,15 @@ module.exports = function (sodium) { var fixtures = require('./fixtures/crypto_sign.json') for (var i = 0; i < fixtures.length; i++) { - var secretKey = toBuffer([].concat(fixtures[i][0], fixtures[i][1])) - var message = toBuffer(fixtures[i][3]) + var secretKey = Buffer.from([].concat(fixtures[i][0], fixtures[i][1])) + var message = Buffer.from(fixtures[i][3]) - var expected = toBuffer(fixtures[i][2]) - var actual = alloc(sodium.crypto_sign_BYTES) + var expected = Buffer.from(fixtures[i][2]) + var actual = Buffer.alloc(sodium.crypto_sign_BYTES) sodium.crypto_sign_detached(actual, message, secretKey) - if (equals(actual, expected) === false) { + if (Buffer.compare(actual, expected) !== 0) { assert.fail('Failed on fixture #' + i) assert.end() return diff --git a/crypto_stream.js b/crypto_stream.js index a7af37d..f6770a2 100644 --- a/crypto_stream.js +++ b/crypto_stream.js @@ -1,17 +1,15 @@ var tape = require('tape') -var alloc = require('buffer-alloc') -var toBuffer = require('buffer-from') module.exports = function (sodium) { tape('crypto_stream', function (t) { - var buf = alloc(50) + var buf = Buffer.alloc(50) var nonce = random(sodium.crypto_stream_NONCEBYTES) var key = random(sodium.crypto_stream_KEYBYTES) sodium.crypto_stream(buf, nonce, key) - t.notEquals(buf, alloc(50), 'contains noise now') - var copy = toBuffer(buf.toString('hex'), 'hex') + t.notEquals(buf, Buffer.alloc(50), 'contains noise now') + var copy = Buffer.from(buf.toString('hex'), 'hex') sodium.crypto_stream(buf, nonce, key) t.same(buf, copy, 'predictable from nonce, key') @@ -20,27 +18,27 @@ module.exports = function (sodium) { }) tape('crypto_stream_xor', function (t) { - var message = toBuffer('Hello, World!') + var message = Buffer.from('Hello, World!') var nonce = random(sodium.crypto_stream_NONCEBYTES) var key = random(sodium.crypto_stream_KEYBYTES) sodium.crypto_stream_xor(message, message, nonce, key) - t.notEquals(message, toBuffer('Hello, World!'), 'encrypted') + t.notEquals(message, Buffer.from('Hello, World!'), 'encrypted') sodium.crypto_stream_xor(message, message, nonce, key) - t.same(message, toBuffer('Hello, World!'), 'decrypted') + t.same(message, Buffer.from('Hello, World!'), 'decrypted') t.end() }) tape('crypto_stream_xor_instance', function (t) { - var message = toBuffer('Hello, world!') + var message = Buffer.from('Hello, world!') var nonce = random(sodium.crypto_stream_NONCEBYTES) var key = random(sodium.crypto_stream_KEYBYTES) - var out = alloc(message.length) + var out = Buffer.alloc(message.length) var inst = sodium.crypto_stream_xor_instance(nonce, key) @@ -54,19 +52,19 @@ module.exports = function (sodium) { }) tape('crypto_stream_xor_instance with empty buffers', function (t) { - var message = toBuffer('Hello, world!') + var message = Buffer.from('Hello, world!') var nonce = random(sodium.crypto_stream_NONCEBYTES) var key = random(sodium.crypto_stream_KEYBYTES) - var out = alloc(message.length) + var out = Buffer.alloc(message.length) var inst = sodium.crypto_stream_xor_instance(nonce, key) - inst.update(alloc(0), alloc(0)) + inst.update(Buffer.alloc(0), Buffer.alloc(0)) for (var i = 0; i < message.length; i++) { inst.update(out.slice(i), message.slice(i, i + 1)) - inst.update(alloc(0), alloc(0)) + inst.update(Buffer.alloc(0), Buffer.alloc(0)) } sodium.crypto_stream_xor(out, out, nonce, key) @@ -88,16 +86,16 @@ module.exports = function (sodium) { var next = random(61) plain.push(next) - var enc = alloc(61) + var enc = Buffer.alloc(61) encrypt.update(enc, next) encrypted.push(enc) - var dec = alloc(61) + var dec = Buffer.alloc(61) decrypt.update(dec, enc) decrypted.push(dec) } - var enc2 = alloc(1000 * 61) + var enc2 = Buffer.alloc(1000 * 61) sodium.crypto_stream_xor(enc2, Buffer.concat(plain), nonce, key) t.same(Buffer.concat(encrypted), enc2, 'same as encrypting all at once') @@ -120,16 +118,16 @@ module.exports = function (sodium) { var next = random(len) plain.push(next) - var enc = alloc(len) + var enc = Buffer.alloc(len) encrypt.update(enc, next) encrypted.push(enc) - var dec = alloc(len) + var dec = Buffer.alloc(len) decrypt.update(dec, enc) decrypted.push(dec) } - var enc2 = alloc(Buffer.concat(plain).length) + var enc2 = Buffer.alloc(Buffer.concat(plain).length) sodium.crypto_stream_xor(enc2, Buffer.concat(plain), nonce, key) t.same(Buffer.concat(encrypted), enc2, 'same as encrypting all at once') @@ -152,19 +150,19 @@ module.exports = function (sodium) { var next = random(len) plain.push(next) - encrypt.update(alloc(0), alloc(0)) + encrypt.update(Buffer.alloc(0), Buffer.alloc(0)) - var enc = alloc(len) + var enc = Buffer.alloc(len) encrypt.update(enc, next) encrypted.push(enc) - var dec = alloc(len) + var dec = Buffer.alloc(len) decrypt.update(dec, enc) decrypted.push(dec) - decrypt.update(alloc(0), alloc(0)) + decrypt.update(Buffer.alloc(0), Buffer.alloc(0)) } - var enc2 = alloc(Buffer.concat(plain).length) + var enc2 = Buffer.alloc(Buffer.concat(plain).length) sodium.crypto_stream_xor(enc2, Buffer.concat(plain), nonce, key) t.same(Buffer.concat(encrypted), enc2, 'same as encrypting all at once') @@ -173,7 +171,7 @@ module.exports = function (sodium) { }) function random (n) { - var buf = alloc(n) + var buf = Buffer.alloc(n) sodium.randombytes_buf(buf) return buf } diff --git a/randombytes_buf.js b/randombytes_buf.js index be88321..933aa54 100644 --- a/randombytes_buf.js +++ b/randombytes_buf.js @@ -1,11 +1,9 @@ var test = require('tape') -var alloc = require('buffer-alloc') -var equals = require('buffer-equals') var freq = require('buffer-byte-frequency') module.exports = function (sodium) { test.skip('Various test cases', function (assert) { - sodium.randombytes_buf(alloc(0)) + sodium.randombytes_buf(Buffer.alloc(0)) sodium.randombytes_buf(new Uint8Array(16)) assert.throws(function () { @@ -16,13 +14,13 @@ module.exports = function (sodium) { }) test('Generates random bytes', function (assert) { - var bufConst = alloc(64) + var bufConst = Buffer.alloc(64) sodium.randombytes_buf(bufConst) - var buf1 = alloc(64) + var buf1 = Buffer.alloc(64) for (var i = 0; i < 1e4; i++) { sodium.randombytes_buf(buf1) - if (equals(buf1, bufConst) === true) { + if (Buffer.compare(buf1, bufConst) === 0) { assert.fail('Constant buffer should not be equal') assert.end() return @@ -34,7 +32,7 @@ module.exports = function (sodium) { }) test('Exceed quota', function (assert) { - var buf = alloc(1 << 17) + var buf = Buffer.alloc(1 << 17) sodium.randombytes_buf(buf) freq(buf) From c4ec5423710b13bf7be1dfacfea5c979b0839f85 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Mon, 4 May 2020 21:37:53 +0200 Subject: [PATCH 2/2] update tests for modular sodium-javascript --- crypto_shorthash.js | 1 + crypto_stream.js | 18 +++++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/crypto_shorthash.js b/crypto_shorthash.js index 42db350..5bf242e 100644 --- a/crypto_shorthash.js +++ b/crypto_shorthash.js @@ -174,6 +174,7 @@ module.exports = function (sodium) { } assert.ok(sodium.crypto_shorthash_WASM_LOADED) + run(assert) }) diff --git a/crypto_stream.js b/crypto_stream.js index f6770a2..984cef4 100644 --- a/crypto_stream.js +++ b/crypto_stream.js @@ -6,12 +6,12 @@ module.exports = function (sodium) { var nonce = random(sodium.crypto_stream_NONCEBYTES) var key = random(sodium.crypto_stream_KEYBYTES) - sodium.crypto_stream(buf, nonce, key) + sodium.crypto_stream(buf, 0, 0, nonce, key) t.notEquals(buf, Buffer.alloc(50), 'contains noise now') var copy = Buffer.from(buf.toString('hex'), 'hex') - sodium.crypto_stream(buf, nonce, key) + sodium.crypto_stream(buf, 0, 0, nonce, key) t.same(buf, copy, 'predictable from nonce, key') t.end() @@ -22,11 +22,11 @@ module.exports = function (sodium) { var nonce = random(sodium.crypto_stream_NONCEBYTES) var key = random(sodium.crypto_stream_KEYBYTES) - sodium.crypto_stream_xor(message, message, nonce, key) + sodium.crypto_stream_xor(message, 0, message, 0, 0, nonce, key) t.notEquals(message, Buffer.from('Hello, World!'), 'encrypted') - sodium.crypto_stream_xor(message, message, nonce, key) + sodium.crypto_stream_xor(message, 0, message, 0, 0, nonce, key) t.same(message, Buffer.from('Hello, World!'), 'decrypted') @@ -46,7 +46,7 @@ module.exports = function (sodium) { inst.update(out.slice(i), message.slice(i, i + 1)) } - sodium.crypto_stream_xor(out, out, nonce, key) + sodium.crypto_stream_xor(out, 0, out, 0, 0, nonce, key) t.same(out, message, 'decrypted') t.end() }) @@ -67,7 +67,7 @@ module.exports = function (sodium) { inst.update(Buffer.alloc(0), Buffer.alloc(0)) } - sodium.crypto_stream_xor(out, out, nonce, key) + sodium.crypto_stream_xor(out, 0, out, 0, 0, nonce, key) t.same(out, message, 'decrypted') t.end() }) @@ -96,7 +96,7 @@ module.exports = function (sodium) { } var enc2 = Buffer.alloc(1000 * 61) - sodium.crypto_stream_xor(enc2, Buffer.concat(plain), nonce, key) + sodium.crypto_stream_xor(enc2, 0, Buffer.concat(plain), 0, 0, nonce, key) t.same(Buffer.concat(encrypted), enc2, 'same as encrypting all at once') t.same(Buffer.concat(decrypted), Buffer.concat(plain), 'decrypts') @@ -128,7 +128,7 @@ module.exports = function (sodium) { } var enc2 = Buffer.alloc(Buffer.concat(plain).length) - sodium.crypto_stream_xor(enc2, Buffer.concat(plain), nonce, key) + sodium.crypto_stream_xor(enc2, 0, Buffer.concat(plain), 0, 0, nonce, key) t.same(Buffer.concat(encrypted), enc2, 'same as encrypting all at once') t.same(Buffer.concat(decrypted), Buffer.concat(plain), 'decrypts') @@ -163,7 +163,7 @@ module.exports = function (sodium) { } var enc2 = Buffer.alloc(Buffer.concat(plain).length) - sodium.crypto_stream_xor(enc2, Buffer.concat(plain), nonce, key) + sodium.crypto_stream_xor(enc2, 0, Buffer.concat(plain), 0, 0, nonce, key) t.same(Buffer.concat(encrypted), enc2, 'same as encrypting all at once') t.same(Buffer.concat(decrypted), Buffer.concat(plain), 'decrypts')