Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update v0.7.2 for modular sodium-javascript library #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 28 additions & 31 deletions crypto_generichash.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand All @@ -53,27 +50,27 @@ 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')
t.end()
})

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')
Expand All @@ -82,54 +79,54 @@ 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')
t.end()
})

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')
t.end()
})

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')
t.end()
})

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')
Expand Down
33 changes: 15 additions & 18 deletions crypto_kdf.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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) {
Expand Down
48 changes: 23 additions & 25 deletions crypto_secretbox.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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')
Expand Down
14 changes: 6 additions & 8 deletions crypto_shorthash.js
Original file line number Diff line number Diff line change
@@ -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]],
Expand Down Expand Up @@ -177,19 +174,20 @@ module.exports = function (sodium) {
}

assert.ok(sodium.crypto_shorthash_WASM_LOADED)

run(assert)
})

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
Expand Down
Loading