From 196702476682bd04e2979868f766a85f2714d7da Mon Sep 17 00:00:00 2001 From: Emil Bay Date: Sun, 11 Jun 2017 20:23:06 +0200 Subject: [PATCH] Update to new blake2b --- crypto_generichash.js | 6 +++--- crypto_kdf.js | 14 ++++++++------ package.json | 6 +++++- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/crypto_generichash.js b/crypto_generichash.js index 2c3bf89..ed699a2 100644 --- a/crypto_generichash.js +++ b/crypto_generichash.js @@ -10,11 +10,11 @@ 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) + blake2b(output.length, key).update(input).final(output) } module.exports.crypto_generichash_batch = function (output, inputArray, key) { - var ctx = blake2b.instance(output.length, key) + var ctx = blake2b(output.length, key) for (var i = 0; i < inputArray.length; i++) { ctx.update(inputArray[i]) } @@ -23,5 +23,5 @@ module.exports.crypto_generichash_batch = function (output, inputArray, key) { module.exports.crypto_generichash_instance = function (key, outlen) { if (outlen == null) outlen = module.exports.crypto_generichash_BYTES - return blake2b.instance(outlen, key) + return blake2b(outlen, key) } diff --git a/crypto_kdf.js b/crypto_kdf.js index 1fef3ea..4aa945b 100644 --- a/crypto_kdf.js +++ b/crypto_kdf.js @@ -1,4 +1,4 @@ -var assert = require('assert') +var assert = require('nanoassert') var randombytes_buf = require('.').randombytes_buf var blake2b = require('blake2b') @@ -18,20 +18,22 @@ function STORE64_LE(dest, int) { } module.exports.crypto_kdf_derive_from_key = function crypto_kdf_derive_from_key (subkey, subkey_id, ctx, key) { - assert(subkey.length >= module.exports.crypto_kdf_BYTES_MIN, 'subkey must be') - assert(ctx.length >= module.exports.crypto_kdf_CONTEXTBYTES, 'context must be') + assert(subkey.length >= module.exports.crypto_kdf_BYTES_MIN, 'subkey must be at least crypto_kdf_BYTES_MIN') + assert(subkey_id >= 0 && subkey_id <= 0x1fffffffffffff, 'subkey_id must be safe integer') + assert(ctx.length >= module.exports.crypto_kdf_CONTEXTBYTES, 'context must be at least crypto_kdf_CONTEXTBYTES') var ctx_padded = new Uint8Array(blake2b.PERSONALBYTES) var salt = new Uint8Array(blake2b.SALTBYTES) ctx_padded.set(ctx, 0, module.exports.crypto_kdf_CONTEXTBYTES) - STORE64_LE(salt, subkey_id) - blake2b(subkey.slice(0, Math.min(subkey.length, module.exports.crypto_kdf_BYTES_MAX)), [], key, salt, ctx_padded, true) + var outlen = Math.min(subkey.length, module.exports.crypto_kdf_BYTES_MAX) + blake2b(outlen, key, salt, ctx_padded, true) + .final(subkey) } module.exports.crypto_kdf_keygen = function crypto_kdf_keygen (out) { - assert(out.length >= module.exports.crypto_kdf_KEYBYTES) + assert(out.length >= module.exports.crypto_kdf_KEYBYTES, 'out.length must be crypto_kdf_KEYBYTES') randombytes_buf(out.subarray(0, module.exports.crypto_kdf_KEYBYTES)) } diff --git a/package.json b/package.json index 647277f..a286be0 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,15 @@ "description": "WIP - a pure javascript version of sodium-native", "main": "index.js", "dependencies": { - "blake2b": "^1.2.0" + "blake2b": "^2.1.1", + "nanoassert": "^1.0.0" }, "devDependencies": { "sodium-test": "^0.3.0" }, + "scripts": { + "test": " node test.js" + }, "repository": { "type": "git", "url": "https://github.com/mafintosh/sodium-javascript.git"