libsodium api; use let/const; prune old code; include offset/length for ws buffer

This commit is contained in:
Christophe Diederichs 2020-05-05 14:08:11 +02:00
parent 54a049406f
commit fd7372df04

View File

@ -21,7 +21,7 @@ exports.crypto_stream_chacha20_xor = function (c, cpos, m, mpos, clen, n, k) {
assert(k.byteLength === exports.crypto_stream_chacha20_KEYBYTES, assert(k.byteLength === exports.crypto_stream_chacha20_KEYBYTES,
'k should be crypto_stream_chacha20_KEYBYTES') 'k should be crypto_stream_chacha20_KEYBYTES')
var xor = new Chacha20(n, k) const xor = new Chacha20(n, k)
xor.update(c, m) xor.update(c, m)
xor.final() xor.final()
} }
@ -32,7 +32,7 @@ exports.crypto_stream_chacha20_xor_ic = function (c, m, mlen, n, ic, k) {
assert(k.byteLength === exports.crypto_stream_chacha20_KEYBYTES, assert(k.byteLength === exports.crypto_stream_chacha20_KEYBYTES,
'k should be crypto_stream_chacha20_KEYBYTES') 'k should be crypto_stream_chacha20_KEYBYTES')
var xor = new Chacha20(n, k, ic) const xor = new Chacha20(n, k, ic)
xor.update(c, m) xor.update(c, m)
xor.final() xor.final()
} }
@ -57,7 +57,7 @@ exports.crypto_stream_chacha20_ietf_xor = function (c, cpos, m, mpos, clen, n, k
assert(k.byteLength === exports.crypto_stream_chacha20_ietf_KEYBYTES, assert(k.byteLength === exports.crypto_stream_chacha20_ietf_KEYBYTES,
'k should be crypto_stream_chacha20_ietf_KEYBYTES') 'k should be crypto_stream_chacha20_ietf_KEYBYTES')
var xor = new Chacha20(n, k) const xor = new Chacha20(n, k)
xor.update(c, m) xor.update(c, m)
xor.final() xor.final()
} }
@ -68,7 +68,7 @@ exports.crypto_stream_chacha20_ietf_xor_ic = function (c, m, mlen, n, ic, k) {
assert(k.byteLength === exports.crypto_stream_chacha20_ietf_KEYBYTES, assert(k.byteLength === exports.crypto_stream_chacha20_ietf_KEYBYTES,
'k should be crypto_stream_chacha20_ietf_KEYBYTES') 'k should be crypto_stream_chacha20_ietf_KEYBYTES')
var xor = new Chacha20(n, k, ic) const xor = new Chacha20(n, k, ic)
xor.update(c, m) xor.update(c, m)
xor.final() xor.final()
} }
@ -117,14 +117,14 @@ Chacha20.prototype.update = function (output, input) {
assert(output.byteLength >= input.byteLength, assert(output.byteLength >= input.byteLength,
'output cannot be shorter than input.') 'output cannot be shorter than input.')
var len = input.length let len = input.length
var offset = this.pos % 64 let offset = this.pos % 64
this.pos += len this.pos += len
// input position // input position
var j = 0 let j = 0
var keyStream = chacha20_block(this.state) let keyStream = chacha20_block(this.state)
// try to finsih the current block // try to finsih the current block
while (offset > 0 && len > 0) { while (offset > 0 && len > 0) {
@ -162,33 +162,28 @@ Chacha20.prototype.final = function () {
this.finalized = true this.finalized = true
} }
module.exports.keystream = function (output, key, nonce, counter) {
var c = new Chacha20(key, nonce, counter)
c.update(output, Buffer.alloc(output.length))
c.final()
}
function chacha20_block (state) { function chacha20_block (state) {
var workingState = new Uint32Array(16) // working state
for (let i = 16; i--;) workingState[i] = state[i] const ws = new Uint32Array(16)
for (let i = 16; i--;) ws[i] = state[i]
for (let i = 0; i < 20; i += 2) { for (let i = 0; i < 20; i += 2) {
QR(workingState, 0, 4, 8, 12) // column 0 QR(ws, 0, 4, 8, 12) // column 0
QR(workingState, 1, 5, 9, 13) // column 1 QR(ws, 1, 5, 9, 13) // column 1
QR(workingState, 2, 6, 10, 14) // column 2 QR(ws, 2, 6, 10, 14) // column 2
QR(workingState, 3, 7, 11, 15) // column 3 QR(ws, 3, 7, 11, 15) // column 3
QR(workingState, 0, 5, 10, 15) // diagonal 1 (main diagonal) QR(ws, 0, 5, 10, 15) // diagonal 1 (main diagonal)
QR(workingState, 1, 6, 11, 12) // diagonal 2 QR(ws, 1, 6, 11, 12) // diagonal 2
QR(workingState, 2, 7, 8, 13) // diagonal 3 QR(ws, 2, 7, 8, 13) // diagonal 3
QR(workingState, 3, 4, 9, 14) // diagonal 4 QR(ws, 3, 4, 9, 14) // diagonal 4
} }
for (let i = 0; i < 16; i++) { for (let i = 0; i < 16; i++) {
workingState[i] += state[i] ws[i] += state[i]
} }
return Buffer.from(workingState.buffer) return Buffer.from(ws.buffer, ws.byteOffset, ws.byteLength)
} }
function rotl (a, b) { function rotl (a, b) {