Update to new blake2b

This commit is contained in:
Emil Bay 2017-06-11 20:23:06 +02:00
parent 776ffea91c
commit 1967024766
No known key found for this signature in database
GPG Key ID: AF1CF37B90FBF638
3 changed files with 16 additions and 10 deletions

View File

@ -10,11 +10,11 @@ module.exports.crypto_generichash_KEYBYTES_MAX = blake2b.KEYBYTES_MAX
module.exports.crypto_generichash_KEYBYTES = blake2b.KEYBYTES module.exports.crypto_generichash_KEYBYTES = blake2b.KEYBYTES
module.exports.crypto_generichash = function (output, input, key) { 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) { 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++) { for (var i = 0; i < inputArray.length; i++) {
ctx.update(inputArray[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) { module.exports.crypto_generichash_instance = function (key, outlen) {
if (outlen == null) outlen = module.exports.crypto_generichash_BYTES if (outlen == null) outlen = module.exports.crypto_generichash_BYTES
return blake2b.instance(outlen, key) return blake2b(outlen, key)
} }

View File

@ -1,4 +1,4 @@
var assert = require('assert') var assert = require('nanoassert')
var randombytes_buf = require('.').randombytes_buf var randombytes_buf = require('.').randombytes_buf
var blake2b = require('blake2b') 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) { 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(subkey.length >= module.exports.crypto_kdf_BYTES_MIN, 'subkey must be at least crypto_kdf_BYTES_MIN')
assert(ctx.length >= module.exports.crypto_kdf_CONTEXTBYTES, 'context must be') 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 ctx_padded = new Uint8Array(blake2b.PERSONALBYTES)
var salt = new Uint8Array(blake2b.SALTBYTES) var salt = new Uint8Array(blake2b.SALTBYTES)
ctx_padded.set(ctx, 0, module.exports.crypto_kdf_CONTEXTBYTES) ctx_padded.set(ctx, 0, module.exports.crypto_kdf_CONTEXTBYTES)
STORE64_LE(salt, subkey_id) 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) { 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)) randombytes_buf(out.subarray(0, module.exports.crypto_kdf_KEYBYTES))
} }

View File

@ -4,11 +4,15 @@
"description": "WIP - a pure javascript version of sodium-native", "description": "WIP - a pure javascript version of sodium-native",
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {
"blake2b": "^1.2.0" "blake2b": "^2.1.1",
"nanoassert": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {
"sodium-test": "^0.3.0" "sodium-test": "^0.3.0"
}, },
"scripts": {
"test": " node test.js"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/mafintosh/sodium-javascript.git" "url": "https://github.com/mafintosh/sodium-javascript.git"