Compare commits

...
5 Commits
Author SHA1 Message Date
Christophe Diederichs 0fba70b6fa add crypto_auth_hmac methods 2020-09-03 12:49:05 +02:00
Mathias Buus a546f3e51d 0.6.2 2020-08-12 15:40:16 +02:00
Emil BayandMathias Buus a338ae9f9d Missing helpers (#24)
* Detach buffers by sending to an empty message channel

* Move helpers out

* fix import

* export helpers

* Try browser testing

* messagechannel check

* xvfb-run --auto-servernum npm run test-browser fails weirdly on ci, removing

Co-authored-by: Mathias Buus <mathiasbuus@gmail.com>
2020-08-12 15:39:14 +02:00
Christophe Diederichs e4693065fd update dependencies 2020-07-13 14:53:38 +02:00
Christophe Diederichs 44e5985630 change to universal hashes 2020-07-13 14:53:38 +02:00
9 changed files with 146 additions and 26 deletions
+15
View File
@@ -1,3 +1,18 @@
language: node_js
node_js:
- lts/*
env:
global:
- MOZ_HEADLESS=1
services:
- xvfb
addons:
firefox: latest
chrome: stable
cache:
npm: false
script:
- npm test
- xvfb-run --auto-servernum npm run test-browser -- --browser chrome
- xvfb-run --auto-servernum npm run test-browser -- --browser firefox
+72
View File
@@ -0,0 +1,72 @@
/* eslint-disable camelcase */
const { crypto_verify_32, crypto_verify_64 } = require('./crypto_verify')
const Sha256 = require('sha256-universal')
const Sha512 = require('sha512-universal')
const assert = require('nanoassert')
const crypto_auth_hmacsha256_BYTES = 32
const crypto_auth_hmacsha256_KEYBYTES = 32
const crypto_auth_hmacsha512_BYTES = 64
const crypto_auth_hmacsha512_KEYBYTES = 32
const crypto_auth_hmacsha512256_BYTES = 32
const crypto_auth_hmacsha512256_KEYBYTES = 32
function crypto_auth_hmacsha256 (out, input, k) {
assert(out.byteLength === crypto_auth_hmacsha256_BYTES, "out should be 'crypto_auth_hmacsha256_BYTES' in length")
const hmac = Sha256.HMAC(key)
hmac.update(input)
return hmac.digest(out)
}
function crypto_auth_hmacsha256_verify (h, input, k) {
const correct = Sha256.HMAC(k).update(input).digest()
return crypto_verify_32(h, 0, correct, 0) | sodium_memcmp(correct, h, 32)
}
function crypto_auth_hmacsha512 (out, input, k) {
assert(out.byteLength === crypto_auth_hmacsha512_BYTES, "out should be 'crypto_auth_hmacsha512_BYTES' in length")
const hmac = Sha512.HMAC(key)
hmac.update(input)
return hmac.digest(out)
}
function crypto_auth_hmacsha512_verify (h, input, k) {
const correct = Sha512.HMAC(k).update(input).digest()
return crypto_verify_64(h, 0, correct, 0) | sodium_memcmp(correct, h, 64)
}
function crypto_auth_hmacsha512256 (out, input, k) {
assert(out.byteLength === crypto_auth_hmacsha512_BYTES, "out should be 'crypto_auth_hmacsha512256_BYTES' in length")
const out0 = Buffer.alloc(64)
const hmac = Sha512.HMAC(key)
hmac.update(input)
hmac.digest(out)
out.set(out0.subarray(0, 32))
}
function crypto_auth_hmacsha512256_verify (h, input, k) {
const correct = Sha512.HMAC(k).update(input).digest()
return crypto_verify_32(h, 0, correct, 0) | sodium_memcmp(correct, h, 32)
}
module.exports = {
crypto_auth_hmacsha256_BYTES,
crypto_auth_hmacsha256_KEYBYTES,
crypto_auth_hmacsha512_BYTES,
crypto_auth_hmacsha512_KEYBYTES,
crypto_auth_hmacsha512256_BYTES,
crypto_auth_hmacsha512256_KEYBYTES,
crypto_auth_hmacsha256,
crypto_auth_hmacsha256_verify,
crypto_auth_hmacsha512,
crypto_auth_hmacsha512_verify,
crypto_auth_hmacsha512256,
crypto_auth_hmacsha512256_verify
}
+1 -1
View File
@@ -1,5 +1,5 @@
/* eslint-disable camelcase */
const sha512 = require('sha512-wasm')
const sha512 = require('sha512-universal')
const assert = require('nanoassert')
if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.')
+1 -1
View File
@@ -1,5 +1,5 @@
/* eslint-disable camelcase */
const sha256 = require('sha256-wasm')
const sha256 = require('sha256-universal')
const assert = require('nanoassert')
if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.')
+8 -15
View File
@@ -1,11 +1,7 @@
/* eslint-disable camelcase */
const assert = require('nanoassert')
module.exports = {
crypto_verify_16,
crypto_verify_32,
sodium_memcmp,
sodium_is_zero
crypto_verify_32
}
function vn (x, xi, y, yi, n) {
@@ -14,6 +10,11 @@ function vn (x, xi, y, yi, n) {
return (1 & ((d - 1) >>> 8)) - 1
}
// Make non enumerable as this is an internal function
Object.defineProperty(module.exports, 'vn', {
value: vn
})
function crypto_verify_16 (x, xi, y, yi) {
return vn(x, xi, y, yi, 16)
}
@@ -22,14 +23,6 @@ function crypto_verify_32 (x, xi, y, yi) {
return vn(x, xi, y, yi, 32)
}
function sodium_memcmp (a, b) {
assert(a.byteLength === b.byteLength, 'buffers must be the same size')
return vn(a, 0, b, 0, a.byteLength) === 0
}
function sodium_is_zero (arr) {
var d = 0
for (let i = 0; i < arr.length; i++) d |= arr[i]
return d === 0
function crypto_verify_64 (x, xi, y, yi) {
return vn(x, xi, y, yi, 64)
}
+31
View File
@@ -0,0 +1,31 @@
/* eslint-disable camelcase */
const assert = require('nanoassert')
const { vn } = require('./crypto_verify')
function sodium_increment (n) {
const nlen = n.byteLength
var c = 1
for (var i = 0; i < nlen; i++) {
c += n[i]
n[i] = c
c >>= 8
}
}
function sodium_memcmp (a, b) {
assert(a.byteLength === b.byteLength, 'buffers must be the same size')
return vn(a, 0, b, 0, a.byteLength) === 0
}
function sodium_is_zero (arr) {
var d = 0
for (let i = 0; i < arr.length; i++) d |= arr[i]
return d === 0
}
module.exports = {
sodium_increment,
sodium_memcmp,
sodium_is_zero
}
+4 -2
View File
@@ -8,7 +8,11 @@
// Implementation derived from TweetNaCl version 20140427.
// See for details: http://tweetnacl.cr.yp.to/
forward(require('./randombytes'))
forward(require('./memory'))
forward(require('./helpers'))
forward(require('./crypto_verify'))
forward(require('./crypto_auth'))
forward(require('./crypto_box'))
forward(require('./crypto_generichash'))
forward(require('./crypto_hash'))
@@ -23,8 +27,6 @@ forward(require('./crypto_shorthash'))
forward(require('./crypto_sign'))
forward(require('./crypto_stream'))
forward(require('./crypto_stream_chacha20'))
forward(require('./crypto_verify'))
forward(require('./randombytes'))
function forward (submodule) {
Object.keys(submodule).forEach(function (prop) {
+9
View File
@@ -1,14 +1,23 @@
/* eslint-disable camelcase */
var MessageChannel = global.MessageChannel
if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads'))
function sodium_malloc (n) {
return new Uint8Array(n)
}
const sink = new MessageChannel()
function sodium_free (n) {
sodium_memzero(n)
sink.port1.postMessage(n.buffer, [n.buffer])
}
function sodium_memzero (arr) {
arr.fill(0)
}
module.exports = {
sodium_malloc,
sodium_free,
sodium_memzero
}
+5 -7
View File
@@ -1,19 +1,18 @@
{
"name": "sodium-javascript",
"version": "0.6.1",
"version": "0.6.2",
"description": "WIP - a pure javascript version of sodium-native",
"main": "index.js",
"dependencies": {
"blake2b": "^2.1.1",
"chacha20-universal": "^1.0.4",
"nanoassert": "^2.0.0",
"sha256-wasm": "^1.3.0",
"sha512-wasm": "^1.2.0",
"sha256-universal": "^1.1.0",
"sha512-universal": "^1.1.0",
"siphash24": "^1.0.1",
"xsalsa20": "^1.0.0"
},
"devDependencies": {
"browser-run": "^7.0.2",
"browserify": "^16.5.1",
"sodium-test": "^0.9.0",
"standard": "^14.3.4",
@@ -31,10 +30,9 @@
"crypto": "crypto"
},
"scripts": {
"browser": "browserify test.js | browser-run",
"browser-manual": "browserify test.js | tape-run",
"pretest": "standard",
"test": "node test.js"
"test": "node test.js",
"test-browser": "browserify test.js | tape-run"
},
"repository": {
"type": "git",