Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
51f8fbc2d3 | ||
|
|
f790621e60 | ||
|
|
4e42b942a5 | ||
|
|
49587c3d7f | ||
|
|
92aee452df | ||
|
|
e1ca83f1bd | ||
|
|
c81ef709d1 | ||
|
|
4e1c69ba13 | ||
|
|
5159d68fa9 | ||
|
|
5ccdcdee17 | ||
|
|
48081c6896 | ||
|
|
b1741bfdae | ||
|
|
f7de366eec | ||
|
|
51e11143e5 |
+2
-2
@@ -6,7 +6,7 @@ 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
|
||||
module.exports.crypto_kdf_KEYBYTES = 32
|
||||
|
||||
function STORE64_LE(dest, int) {
|
||||
var mul = 1
|
||||
@@ -29,7 +29,7 @@ module.exports.crypto_kdf_derive_from_key = function crypto_kdf_derive_from_key
|
||||
STORE64_LE(salt, subkey_id)
|
||||
|
||||
var outlen = Math.min(subkey.length, module.exports.crypto_kdf_BYTES_MAX)
|
||||
blake2b(outlen, key, salt, ctx_padded, true)
|
||||
blake2b(outlen, key.subarray(0, module.exports.crypto_kdf_KEYBYTES), salt, ctx_padded, true)
|
||||
.final(subkey)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
var assert = require('nanoassert')
|
||||
var randombytes_buf = require('./randombytes').randombytes_buf
|
||||
var generichash = require('./crypto_generichash')
|
||||
var crypto_scalarmult_base = require('./crypto_scalarmult').crypto_scalarmult_base
|
||||
|
||||
exports.crypto_kx_PUBLICKEYBYTES = 32
|
||||
exports.crypto_kx_SECRETKEYBYTES = 32
|
||||
exports.crypto_kx_SEEDBYTES = 32
|
||||
exports.crypto_kx_SESSIONKEYBYTES = 32
|
||||
exports.crypto_kx_PRIMITIVE = ''
|
||||
|
||||
exports.crypto_kx_keypair = function (pk, sk) {
|
||||
assert(sk.byteLength >= exports.crypto_kx_SECRETKEYBYTES, 'sk must be at least crypto_kx_SECRETKEYBYTES')
|
||||
assert(pk.byteLength >= exports.crypto_kx_PUBLICKEYBYTES, 'pk must be at least crypto_kx_PUBLICKEYBYTES')
|
||||
|
||||
randombytes_buf(sk.subarray(0, exports.crypto_kx_SECRETKEYBYTES))
|
||||
|
||||
crypto_scalarmult_base(
|
||||
pk.subarray(0, exports.crypto_kx_PUBLICKEYBYTES),
|
||||
sk.subarray(0, exports.crypto_kx_SECRETKEYBYTES)
|
||||
)
|
||||
}
|
||||
|
||||
exports.crypto_kx_seed_keypair = function (pk, sk, seed) {
|
||||
assert(sk.byteLength >= exports.crypto_kx_SECRETKEYBYTES, 'sk must be at least crypto_kx_SECRETKEYBYTES')
|
||||
assert(pk.byteLength >= exports.crypto_kx_PUBLICKEYBYTES, 'pk must be at least crypto_kx_PUBLICKEYBYTES')
|
||||
assert(seed.byteLength >= exports.crypto_kx_SEEDBYTES, 'seed must be at least crypto_kx_SEEDBYTES')
|
||||
|
||||
generichash.crypto_generichash(
|
||||
sk.subarray(0, exports.crypto_kx_SECRETKEYBYTES),
|
||||
seed.subarray(0, exports.crypto_kx_SEEDBYTES)
|
||||
)
|
||||
|
||||
crypto_scalarmult_base(
|
||||
pk.subarray(0, exports.crypto_kx_PUBLICKEYBYTES),
|
||||
sk.subarray(0, exports.crypto_kx_SECRETKEYBYTES)
|
||||
)
|
||||
}
|
||||
|
||||
exports.crypto_kx_client_session_keys = calc.bind(null, false)
|
||||
|
||||
exports.crypto_kx_server_session_keys = calc.bind(null, true)
|
||||
|
||||
|
||||
function calc (server, rx, tx, pk, sk, remote_pk) {
|
||||
if (rx == null) rx = tx
|
||||
if (tx == null) tx = rx
|
||||
|
||||
assert(rx >= exports.crypto_kx_SESSIONKEYBYTES, 'rx must be at least crypto_kx_SESSIONKEYBYTES')
|
||||
assert(tx >= exports.crypto_kx_SESSIONKEYBYTES, 'tx should be at least crypto_kx_SESSIONKEYBYTES')
|
||||
assert(sk.byteLength >= exports.crypto_kx_SECRETKEYBYTES, 'sk must be at least crypto_kx_SECRETKEYBYTES')
|
||||
assert(pk.byteLength >= exports.crypto_kx_PUBLICKEYBYTES, 'pk must be at least crypto_kx_PUBLICKEYBYTES')
|
||||
assert(remote_pk.byteLength >= exports.crypto_kx_PUBLICKEYBYTES, 'remote_pk must be at least crypto_kx_PUBLICKEYBYTES')
|
||||
|
||||
rx = rx.subarray(0, exports.crypto_kx_SESSIONKEYBYTES)
|
||||
tx = tx.subarray(0, exports.crypto_kx_SESSIONKEYBYTES)
|
||||
|
||||
sk = sk.subarray(0, exports.crypto_kx_SECRETKEYBYTES)
|
||||
pk = pk.subarray(0, exports.crypto_kx_PUBLICKEYBYTES)
|
||||
remote_pk = remote_pk.subarray(0, exports.crypto_kx_PUBLICKEYBYTES)
|
||||
|
||||
var keys = new Uint8Array(2 * exports.crypto_kx_SESSIONKEYBYTES)
|
||||
var hash = generichash.crypto_generichash_instance(null, keys.byteLength)
|
||||
var q = new Uint8Array(scalarmult.crypto_scalarmult_BYTES)
|
||||
|
||||
scalarmult.crypto_scalamult(q, sk, remote_pk)
|
||||
|
||||
hash.update(q)
|
||||
q.fill(0)
|
||||
// if server, then remote_pk == client_pk
|
||||
hash.update(server ? remote_pk : pk)
|
||||
hash.update(server ? pk : remote_pk)
|
||||
hash.final(keys)
|
||||
|
||||
// TODO clear out hash
|
||||
|
||||
rx.set(keys.subarray(0, exports.crypto_kx_SESSIONKEYBYTES))
|
||||
tx.set(keys.subarray(exports.crypto_kx_SESSIONKEYBYTES))
|
||||
}
|
||||
@@ -5,13 +5,8 @@ exports.crypto_shorthash_BYTES = siphash.BYTES
|
||||
exports.crypto_shorthash_KEYBYTES = siphash.KEYBYTES
|
||||
exports.crypto_shorthash_WASM_SUPPORTED = siphash.WASM_SUPPORTED
|
||||
exports.crypto_shorthash_WASM_LOADED = siphash.WASM_LOADED
|
||||
exports.crypto_shorthash_ready = siphash.ready
|
||||
exports.crypto_shorthash = shorthash
|
||||
|
||||
siphash.ready(function () {
|
||||
exports.crypto_shorthash_WASM_LOADED = siphash.WASM_LOADED
|
||||
})
|
||||
|
||||
function shorthash (out, data, key, noAssert) {
|
||||
siphash(data, key, out, noAssert)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
var xsalsa20 = require('xsalsa20')
|
||||
|
||||
// Based on https://github.com/dchest/tweetnacl-js/blob/6dcbcaf5f5cbfd313f2dcfe763db35c828c8ff5b/nacl-fast.js.
|
||||
|
||||
var sodium = module.exports
|
||||
@@ -1736,6 +1738,57 @@ function crypto_secretbox_open_easy(msg, box, n, k) {
|
||||
return true
|
||||
}
|
||||
|
||||
function crypto_box_keypair(pk, sk) {
|
||||
check(pk, crypto_box_PUBLICKEYBYTES)
|
||||
check(sk, crypto_box_SECRETKEYBYTES)
|
||||
randombytes(sk, 32)
|
||||
return crypto_scalarmult_base(pk, sk)
|
||||
}
|
||||
|
||||
function crypto_box_seal(c, m, pk) {
|
||||
check(c, crypto_box_SEALBYTES + m.length)
|
||||
check(pk, crypto_box_PUBLICKEYBYTES)
|
||||
|
||||
var epk = c.subarray(0, crypto_box_PUBLICKEYBYTES)
|
||||
var esk = new Uint8Array(crypto_box_SECRETKEYBYTES)
|
||||
crypto_box_keypair(epk, esk)
|
||||
|
||||
var n = new Uint8Array(crypto_box_NONCEBYTES)
|
||||
sodium.crypto_generichash_batch(n, [ epk, pk ])
|
||||
|
||||
var s = new Uint8Array(crypto_box_PUBLICKEYBYTES)
|
||||
crypto_scalarmult(s, esk, pk)
|
||||
|
||||
var k = new Uint8Array(crypto_box_BEFORENMBYTES)
|
||||
var zero = new Uint8Array(16)
|
||||
xsalsa20.core_hsalsa20(k, zero, s, xsalsa20.SIGMA)
|
||||
|
||||
crypto_secretbox_easy(c.subarray(epk.length), m, n, k)
|
||||
|
||||
cleanup(esk)
|
||||
}
|
||||
|
||||
function crypto_box_seal_open(m, c, pk, sk) {
|
||||
check(c, crypto_box_SEALBYTES)
|
||||
check(m, c.length - crypto_box_SEALBYTES)
|
||||
check(pk, crypto_box_PUBLICKEYBYTES)
|
||||
check(sk, crypto_box_SECRETKEYBYTES)
|
||||
|
||||
var epk = c.subarray(0, crypto_box_PUBLICKEYBYTES)
|
||||
|
||||
var n = new Uint8Array(crypto_box_NONCEBYTES)
|
||||
sodium.crypto_generichash_batch(n, [ epk, pk ])
|
||||
|
||||
var s = new Uint8Array(crypto_box_PUBLICKEYBYTES)
|
||||
crypto_scalarmult(s, sk, epk)
|
||||
|
||||
var k = new Uint8Array(crypto_box_BEFORENMBYTES)
|
||||
var zero = new Uint8Array(16)
|
||||
xsalsa20.core_hsalsa20(k, zero, s, xsalsa20.SIGMA)
|
||||
|
||||
return crypto_secretbox_open_easy(m, c.subarray(epk.length), n, k)
|
||||
}
|
||||
|
||||
var crypto_secretbox_KEYBYTES = 32,
|
||||
crypto_secretbox_NONCEBYTES = 24,
|
||||
crypto_secretbox_ZEROBYTES = 32,
|
||||
@@ -1748,6 +1801,8 @@ var crypto_secretbox_KEYBYTES = 32,
|
||||
crypto_box_NONCEBYTES = crypto_secretbox_NONCEBYTES,
|
||||
crypto_box_ZEROBYTES = crypto_secretbox_ZEROBYTES,
|
||||
crypto_box_BOXZEROBYTES = crypto_secretbox_BOXZEROBYTES,
|
||||
crypto_box_SEALBYTES = 48,
|
||||
crypto_box_BEFORENMBYTES = 32,
|
||||
crypto_sign_BYTES = 64,
|
||||
crypto_sign_PUBLICKEYBYTES = 32,
|
||||
crypto_sign_SECRETKEYBYTES = 64,
|
||||
@@ -1788,6 +1843,18 @@ sodium.crypto_secretbox_open_easy = crypto_secretbox_open_easy
|
||||
sodium.crypto_secretbox_detached = crypto_secretbox_detached
|
||||
sodium.crypto_secretbox_open_detached = crypto_secretbox_open_detached
|
||||
|
||||
sodium.crypto_box_PUBLICKEYBYTES = crypto_box_PUBLICKEYBYTES
|
||||
sodium.crypto_box_SECRETKEYBYTES = crypto_box_SECRETKEYBYTES
|
||||
sodium.crypto_box_SEALBYTES = crypto_box_SEALBYTES
|
||||
sodium.crypto_box_BEFORENMBYTES = crypto_box_BEFORENMBYTES
|
||||
sodium.crypto_box_keypair = crypto_box_keypair
|
||||
sodium.crypto_box_seal = crypto_box_seal
|
||||
sodium.crypto_box_seal_open = crypto_box_seal_open
|
||||
|
||||
sodium.sodium_malloc = function (n) {
|
||||
return new Uint8Array(n)
|
||||
}
|
||||
|
||||
function cleanup(arr) {
|
||||
for (var i = 0; i < arr.length; i++) arr[i] = 0;
|
||||
}
|
||||
|
||||
+7
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sodium-javascript",
|
||||
"version": "0.5.2",
|
||||
"version": "0.5.6",
|
||||
"description": "WIP - a pure javascript version of sodium-native",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
@@ -14,6 +14,12 @@
|
||||
"browserify": "^14.1.0",
|
||||
"sodium-test": "^0.7.0"
|
||||
},
|
||||
"browser": {
|
||||
"crypto": false
|
||||
},
|
||||
"react-native": {
|
||||
"crypto": "crypto"
|
||||
},
|
||||
"scripts": {
|
||||
"browser": "browserify test.js | browser-run",
|
||||
"browser-manual": "browserify test.js | browser-run -p 1234",
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ var randombytes = (function () {
|
||||
return browserBytes
|
||||
} else if (typeof require !== 'undefined') {
|
||||
// Node.js.
|
||||
crypto = require('cry' + 'pto');
|
||||
crypto = require('crypto')
|
||||
if (crypto && crypto.randomBytes) {
|
||||
return nodeBytes
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user