Compare commits

..
1 Commits
Author SHA1 Message Date
Emil Bay 002969f78d Preliminary kx api
Missing dependent apis and tests
2018-02-15 22:31:13 +01:00
5 changed files with 88 additions and 10 deletions
+2 -2
View File
@@ -6,7 +6,7 @@ module.exports.crypto_kdf_PRIMITIVE = 'blake2b'
module.exports.crypto_kdf_BYTES_MIN = 16 module.exports.crypto_kdf_BYTES_MIN = 16
module.exports.crypto_kdf_BYTES_MAX = 64 module.exports.crypto_kdf_BYTES_MAX = 64
module.exports.crypto_kdf_CONTEXTBYTES = 8 module.exports.crypto_kdf_CONTEXTBYTES = 8
module.exports.crypto_kdf_KEYBYTES = 32 module.exports.crypto_kdf_KEYBYTES = 64
function STORE64_LE(dest, int) { function STORE64_LE(dest, int) {
var mul = 1 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) STORE64_LE(salt, subkey_id)
var outlen = Math.min(subkey.length, module.exports.crypto_kdf_BYTES_MAX) var outlen = Math.min(subkey.length, module.exports.crypto_kdf_BYTES_MAX)
blake2b(outlen, key.subarray(0, module.exports.crypto_kdf_KEYBYTES), salt, ctx_padded, true) blake2b(outlen, key, salt, ctx_padded, true)
.final(subkey) .final(subkey)
} }
+79
View File
@@ -0,0 +1,79 @@
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
View File
@@ -5,8 +5,13 @@ exports.crypto_shorthash_BYTES = siphash.BYTES
exports.crypto_shorthash_KEYBYTES = siphash.KEYBYTES exports.crypto_shorthash_KEYBYTES = siphash.KEYBYTES
exports.crypto_shorthash_WASM_SUPPORTED = siphash.WASM_SUPPORTED exports.crypto_shorthash_WASM_SUPPORTED = siphash.WASM_SUPPORTED
exports.crypto_shorthash_WASM_LOADED = siphash.WASM_LOADED exports.crypto_shorthash_WASM_LOADED = siphash.WASM_LOADED
exports.crypto_shorthash_ready = siphash.ready
exports.crypto_shorthash = shorthash exports.crypto_shorthash = shorthash
siphash.ready(function () {
exports.crypto_shorthash_WASM_LOADED = siphash.WASM_LOADED
})
function shorthash (out, data, key, noAssert) { function shorthash (out, data, key, noAssert) {
siphash(data, key, out, noAssert) siphash(data, key, out, noAssert)
} }
+1 -7
View File
@@ -1,6 +1,6 @@
{ {
"name": "sodium-javascript", "name": "sodium-javascript",
"version": "0.5.5", "version": "0.5.2",
"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": {
@@ -14,12 +14,6 @@
"browserify": "^14.1.0", "browserify": "^14.1.0",
"sodium-test": "^0.7.0" "sodium-test": "^0.7.0"
}, },
"browser": {
"crypto": false
},
"react-native": {
"crypto": "crypto"
},
"scripts": { "scripts": {
"browser": "browserify test.js | browser-run", "browser": "browserify test.js | browser-run",
"browser-manual": "browserify test.js | browser-run -p 1234", "browser-manual": "browserify test.js | browser-run -p 1234",
+1 -1
View File
@@ -21,7 +21,7 @@ var randombytes = (function () {
return browserBytes return browserBytes
} else if (typeof require !== 'undefined') { } else if (typeof require !== 'undefined') {
// Node.js. // Node.js.
crypto = require('crypto') crypto = require('cry' + 'pto');
if (crypto && crypto.randomBytes) { if (crypto && crypto.randomBytes) {
return nodeBytes return nodeBytes
} }