From 5397910bbb31c34193a56ba50830d945edfe1362 Mon Sep 17 00:00:00 2001 From: Emil Bay Date: Tue, 6 Jun 2017 21:04:36 +0200 Subject: [PATCH] Add blake2b for generichash --- crypto_generichash.js | 27 +++++++++++++++++++++++++++ index.js | 32 ++++++++------------------------ package.json | 4 ++-- 3 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 crypto_generichash.js diff --git a/crypto_generichash.js b/crypto_generichash.js new file mode 100644 index 0000000..2c3bf89 --- /dev/null +++ b/crypto_generichash.js @@ -0,0 +1,27 @@ +var assert = require('assert') +var blake2b = require('blake2b') + +module.exports.crypto_generichash_PRIMITIVE = 'blake2b' +module.exports.crypto_generichash_BYTES_MIN = blake2b.BYTES_MIN +module.exports.crypto_generichash_BYTES_MAX = blake2b.BYTES_MAX +module.exports.crypto_generichash_BYTES = blake2b.BYTES +module.exports.crypto_generichash_KEYBYTES_MIN = blake2b.KEYBYTES_MIN +module.exports.crypto_generichash_KEYBYTES_MAX = blake2b.KEYBYTES_MAX +module.exports.crypto_generichash_KEYBYTES = blake2b.KEYBYTES + +module.exports.crypto_generichash = function (output, input, key) { + blake2b(output, input, key) +} + +module.exports.crypto_generichash_batch = function (output, inputArray, key) { + var ctx = blake2b.instance(output.length, key) + for (var i = 0; i < inputArray.length; i++) { + ctx.update(inputArray[i]) + } + ctx.final(output) +} + +module.exports.crypto_generichash_instance = function (key, outlen) { + if (outlen == null) outlen = module.exports.crypto_generichash_BYTES + return blake2b.instance(outlen, key) +} diff --git a/index.js b/index.js index 8cc07dc..c9189fe 100644 --- a/index.js +++ b/index.js @@ -2162,21 +2162,6 @@ function crypto_secretbox_open_easy(msg, box, n, k) { return true } -var blake2b = require('blakejs/blake2b') - -function crypto_generichash (out, data, key) { - var tmp = blake2b.blake2b(data, key, out.length) - for (var i = 0; i < tmp.length; i++) out[i] = tmp[i] -} - -function crypto_generichash_batch (out, batch, key) { - var i = 0 - var ctx = blake2b.blake2bInit(out.length, key) - for (i = 0; i < batch.length; i++) blake2b.blake2bUpdate(ctx, batch[i]) - var tmp = blake2b.blake2bFinal(ctx) - for (var i = 0; i < tmp.length; i++) out[i] = tmp[i] -} - var crypto_secretbox_KEYBYTES = 32, crypto_secretbox_NONCEBYTES = 24, crypto_secretbox_ZEROBYTES = 32, @@ -2208,6 +2193,8 @@ sodium.crypto_sign_open = crypto_sign_open sodium.crypto_sign_detached = crypto_sign_detached sodium.crypto_sign_verify_detached = crypto_sign_verify_detached +forward(require('./crypto_generichash')) + sodium.crypto_stream_KEYBYTES = 32 sodium.crypto_stream_NONCEBYTES = 24 sodium.crypto_stream = crypto_stream_wrap @@ -2224,15 +2211,6 @@ sodium.crypto_secretbox_MACBYTES = 16 sodium.crypto_secretbox_easy = crypto_secretbox_easy sodium.crypto_secretbox_open_easy = crypto_secretbox_open_easy -sodium.crypto_generichash_BYTES_MIN = 16 -sodium.crypto_generichash_BYTES_MAX = 64 -sodium.crypto_generichash_BYTES = 32 -sodium.crypto_generichash_KEYBYTES_MIN = 16 -sodium.crypto_generichash_KEYBYTES_MAX = 64 -sodium.crypto_generichash_KEYBYTES = 32 -sodium.crypto_generichash = crypto_generichash -sodium.crypto_generichash_batch = crypto_generichash_batch - function cleanup(arr) { for (var i = 0; i < arr.length; i++) arr[i] = 0; } @@ -2241,6 +2219,12 @@ function check (buf, len) { if (!buf || (len && buf.length < len)) throw new Error('Argument must be a buffer' + (len ? ' of length ' + len : '')) } +function forward (submodule) { + Object.keys(submodule).forEach(function (prop) { + module.exports[prop] = submodule[prop] + }) +} + (function() { // Initialize PRNG if environment provides CSPRNG. // If not, methods calling randombytes will throw. diff --git a/package.json b/package.json index 15fa6a4..e90ef6e 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,10 @@ "description": "WIP - a pure javascript version of sodium-native", "main": "index.js", "dependencies": { - "blakejs": "^1.0.1" + "blake2b": "^1.2.0" }, "devDependencies": { - "sodium-test": "^0.1.0" + "sodium-test": "^0.2.0" }, "repository": { "type": "git",