Compare commits

..
6 Commits
Author SHA1 Message Date
Christophe Diederichs ce5ac41ecd 0.6.3 2020-09-15 17:39:54 +02:00
Christophe Diederichs a82160d51b crypto_verify return booleans & add crypto_verify_64 (#33)
* crypto_verify return booleans

* can now return crypto_verify result directly

* remove redundant return values

* unpackneg check returns boolean
2020-09-15 17:27:39 +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
12 changed files with 83 additions and 38 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
+1 -1
View File
@@ -131,7 +131,7 @@ function crypto_aead_chacha20poly1305_ietf_decrypt_detached (m, nsec, c, mac, ad
computed_mac.fill(0)
slen.fill(0)
if (ret !== 0) {
if (!ret) {
m.fill(0)
throw new Error('could not verify data')
}
+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.')
+1 -2
View File
@@ -23,7 +23,6 @@ function crypto_onetimeauth (mac, msg, key) {
var s = new Poly1305(key)
s.update(msg, 0, msg.byteLength)
s.finish(mac, 0)
return true
}
function crypto_onetimeauth_verify (mac, msg, key) {
@@ -33,5 +32,5 @@ function crypto_onetimeauth_verify (mac, msg, key) {
var tmp = new Uint8Array(16)
crypto_onetimeauth(tmp, msg, key)
return crypto_verify_16(mac, 0, tmp, 0) === 0
return crypto_verify_16(mac, 0, tmp, 0)
}
+1 -3
View File
@@ -37,7 +37,6 @@ function crypto_secretbox (c, m, n, k) {
c.subarray(0, crypto_onetimeauth_KEYBYTES)
)
c.fill(0, 0, crypto_secretbox_BOXZEROBYTES)
return 0
}
function crypto_secretbox_open (m, c, n, k) {
@@ -94,9 +93,8 @@ function crypto_secretbox_easy (o, msg, n, k) {
const m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.byteLength)
const c = new Uint8Array(m.byteLength)
m.set(msg, crypto_secretbox_ZEROBYTES)
if (crypto_secretbox(c, m, n, k) === false) return false
crypto_secretbox(c, m, n, k)
o.set(c.subarray(crypto_secretbox_BOXZEROBYTES))
return true
}
function crypto_secretbox_open_easy (msg, box, n, k) {
+5 -5
View File
@@ -242,16 +242,16 @@ function unpackneg (r, p) {
S(chk, r[0])
M(chk, chk, den)
if (neq25519(chk, num)) M(r[0], r[0], I)
if (!neq25519(chk, num)) M(r[0], r[0], I)
S(chk, r[0])
M(chk, chk, den)
if (neq25519(chk, num)) return -1
if (!neq25519(chk, num)) return false
if (par25519(r[0]) === (p[31] >> 7)) Z(r[0], gf0, r[0])
M(r[3], r[0], r[1])
return 0
return true
}
/* eslint-disable no-unused-vars */
@@ -270,7 +270,7 @@ function crypto_sign_open (msg, sm, pk) {
mlen = -1
if (n < 64) return false
if (unpackneg(q, pk)) return false
if (!unpackneg(q, pk)) return false
for (i = 0; i < n; i++) m[i] = sm[i]
for (i = 0; i < 32; i++) m[i + 32] = pk[i]
@@ -283,7 +283,7 @@ function crypto_sign_open (msg, sm, pk) {
pack(t, p)
n -= 64
if (crypto_verify_32(sm, 0, t, 0)) {
if (!crypto_verify_32(sm, 0, t, 0)) {
for (i = 0; i < n; i++) m[i] = 0
return false
// throw new Error('crypto_sign_open failed')
+10 -16
View File
@@ -1,11 +1,8 @@
/* eslint-disable camelcase */
const assert = require('nanoassert')
module.exports = {
crypto_verify_16,
crypto_verify_32,
sodium_memcmp,
sodium_is_zero
crypto_verify_64
}
function vn (x, xi, y, yi, n) {
@@ -14,22 +11,19 @@ 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)
return vn(x, xi, y, yi, 16) === 0
}
function crypto_verify_32 (x, xi, y, yi) {
return vn(x, xi, y, yi, 32)
return vn(x, xi, y, yi, 32) === 0
}
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) === 0
}
+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
}
+3 -2
View File
@@ -8,7 +8,10 @@
// 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_box'))
forward(require('./crypto_generichash'))
forward(require('./crypto_hash'))
@@ -23,8 +26,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.3",
"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.0.1",
"sha512-universal": "^1.0.1",
"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",