Add blake2b for generichash

This commit is contained in:
Emil Bay 2017-06-06 21:04:36 +02:00
parent f639c1ce6f
commit 5397910bbb
No known key found for this signature in database
GPG Key ID: AF1CF37B90FBF638
3 changed files with 37 additions and 26 deletions

27
crypto_generichash.js Normal file
View File

@ -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)
}

View File

@ -2162,21 +2162,6 @@ function crypto_secretbox_open_easy(msg, box, n, k) {
return true 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, var crypto_secretbox_KEYBYTES = 32,
crypto_secretbox_NONCEBYTES = 24, crypto_secretbox_NONCEBYTES = 24,
crypto_secretbox_ZEROBYTES = 32, 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_detached = crypto_sign_detached
sodium.crypto_sign_verify_detached = crypto_sign_verify_detached sodium.crypto_sign_verify_detached = crypto_sign_verify_detached
forward(require('./crypto_generichash'))
sodium.crypto_stream_KEYBYTES = 32 sodium.crypto_stream_KEYBYTES = 32
sodium.crypto_stream_NONCEBYTES = 24 sodium.crypto_stream_NONCEBYTES = 24
sodium.crypto_stream = crypto_stream_wrap 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_easy = crypto_secretbox_easy
sodium.crypto_secretbox_open_easy = crypto_secretbox_open_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) { function cleanup(arr) {
for (var i = 0; i < arr.length; i++) arr[i] = 0; 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 : '')) 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() { (function() {
// Initialize PRNG if environment provides CSPRNG. // Initialize PRNG if environment provides CSPRNG.
// If not, methods calling randombytes will throw. // If not, methods calling randombytes will throw.

View File

@ -4,10 +4,10 @@
"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": {
"blakejs": "^1.0.1" "blake2b": "^1.2.0"
}, },
"devDependencies": { "devDependencies": {
"sodium-test": "^0.1.0" "sodium-test": "^0.2.0"
}, },
"repository": { "repository": {
"type": "git", "type": "git",