Added crypto_kdf

This commit is contained in:
Emil Bay 2017-06-07 22:38:40 +02:00
parent afa3b5fc04
commit 94e3891f67
No known key found for this signature in database
GPG Key ID: AF1CF37B90FBF638
3 changed files with 39 additions and 1 deletions

37
crypto_kdf.js Normal file
View File

@ -0,0 +1,37 @@
var assert = require('assert')
var randombytes_buf = require('.').randombytes_buf
var blake2b = require('blake2b')
module.exports.crypto_kdf_PRIMITIVE = 'blake2b'
module.exports.crypto_kdf_BYTES_MIN = 16
module.exports.crypto_kdf_BYTES_MAX = 64
module.exports.crypto_kdf_CONTEXTBYTES = 8
module.exports.crypto_kdf_KEYBYTES = 64
function STORE64_LE(dest, int) {
var mul = 1
var i = 0
dest[0] = int & 0xFF
while (++i < 8 && (mul *= 0x100)) {
dest[i] = (int / mul) & 0xFF
}
}
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')
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)
}
module.exports.crypto_kdf_keygen = function crypto_kdf_keygen (out) {
assert(out.length >= module.exports.crypto_kdf_KEYBYTES)
randombytes_buf(out, module.exports.crypto_kdf_KEYBYTES)
}

View File

@ -2198,6 +2198,7 @@ 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')) forward(require('./crypto_generichash'))
forward(require('./crypto_kdf'))
sodium.crypto_stream_KEYBYTES = 32 sodium.crypto_stream_KEYBYTES = 32
sodium.crypto_stream_NONCEBYTES = 24 sodium.crypto_stream_NONCEBYTES = 24

View File

@ -7,7 +7,7 @@
"blake2b": "^1.2.0" "blake2b": "^1.2.0"
}, },
"devDependencies": { "devDependencies": {
"sodium-test": "^0.2.0" "sodium-test": "^0.3.0"
}, },
"repository": { "repository": {
"type": "git", "type": "git",