Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56913733b9 | ||
|
|
94e3891f67 | ||
|
|
afa3b5fc04 | ||
|
|
226ecd8778 | ||
|
|
5397910bbb | ||
|
|
f639c1ce6f | ||
|
|
92bea299c6 |
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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,
|
||||
@@ -2195,6 +2180,10 @@ var crypto_secretbox_KEYBYTES = 32,
|
||||
crypto_sign_SEEDBYTES = 32,
|
||||
crypto_hash_BYTES = 64;
|
||||
|
||||
sodium.memzero = function (len, offset) {
|
||||
for (var i = offset; i < len; i++) arr[i] = 0;
|
||||
}
|
||||
|
||||
sodium.randombytes_buf = randombytes_buf
|
||||
|
||||
sodium.crypto_sign_BYTES = crypto_sign_BYTES
|
||||
@@ -2208,6 +2197,9 @@ 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'))
|
||||
forward(require('./crypto_kdf'))
|
||||
|
||||
sodium.crypto_stream_KEYBYTES = 32
|
||||
sodium.crypto_stream_NONCEBYTES = 24
|
||||
sodium.crypto_stream = crypto_stream_wrap
|
||||
@@ -2224,15 +2216,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 +2224,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.
|
||||
|
||||
+5
-3
@@ -1,12 +1,14 @@
|
||||
{
|
||||
"name": "sodium-javascript",
|
||||
"version": "0.0.1",
|
||||
"version": "0.2.0",
|
||||
"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.3.0"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mafintosh/sodium-javascript.git"
|
||||
|
||||
Reference in New Issue
Block a user