diff --git a/crypto_stream_chacha20.js b/crypto_stream_chacha20.js index 0b73d9a..a22ef4f 100644 --- a/crypto_stream_chacha20.js +++ b/crypto_stream_chacha20.js @@ -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, 'k should be crypto_stream_chacha20_KEYBYTES') - var xor = new Chacha20(n, k) + const xor = new Chacha20(n, k) xor.update(c, m) 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, '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.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, '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.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, '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.final() } @@ -117,14 +117,14 @@ Chacha20.prototype.update = function (output, input) { assert(output.byteLength >= input.byteLength, 'output cannot be shorter than input.') - var len = input.length - var offset = this.pos % 64 + let len = input.length + let offset = this.pos % 64 this.pos += len // 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 while (offset > 0 && len > 0) { @@ -162,33 +162,28 @@ Chacha20.prototype.final = function () { 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) { - var workingState = new Uint32Array(16) - for (let i = 16; i--;) workingState[i] = state[i] + // working state + const ws = new Uint32Array(16) + for (let i = 16; i--;) ws[i] = state[i] for (let i = 0; i < 20; i += 2) { - QR(workingState, 0, 4, 8, 12) // column 0 - QR(workingState, 1, 5, 9, 13) // column 1 - QR(workingState, 2, 6, 10, 14) // column 2 - QR(workingState, 3, 7, 11, 15) // column 3 + QR(ws, 0, 4, 8, 12) // column 0 + QR(ws, 1, 5, 9, 13) // column 1 + QR(ws, 2, 6, 10, 14) // column 2 + QR(ws, 3, 7, 11, 15) // column 3 - QR(workingState, 0, 5, 10, 15) // diagonal 1 (main diagonal) - QR(workingState, 1, 6, 11, 12) // diagonal 2 - QR(workingState, 2, 7, 8, 13) // diagonal 3 - QR(workingState, 3, 4, 9, 14) // diagonal 4 + QR(ws, 0, 5, 10, 15) // diagonal 1 (main diagonal) + QR(ws, 1, 6, 11, 12) // diagonal 2 + QR(ws, 2, 7, 8, 13) // diagonal 3 + QR(ws, 3, 4, 9, 14) // diagonal 4 } 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) {