chacha: readUint32Le generalised for uint8array; aead: standard fix

This commit is contained in:
Christophe Diederichs 2020-06-17 12:50:27 +02:00
parent 272ac68f6b
commit 68bd4b4a68
2 changed files with 17 additions and 7 deletions

View File

@ -89,7 +89,7 @@ function crypto_aead_chacha20poly1305_ietf_decrypt (m, nsec, c, ad, npub, k) {
return c.length - crypto_aead_chacha20poly1305_ietf_ABYTES return c.length - crypto_aead_chacha20poly1305_ietf_ABYTES
} }
return ret; return ret
} }
function crypto_aead_chacha20poly1305_ietf_decrypt_detached (m, nsec, c, mac, ad, npub, k) { function crypto_aead_chacha20poly1305_ietf_decrypt_detached (m, nsec, c, mac, ad, npub, k) {

View File

@ -95,18 +95,18 @@ function Chacha20 (n, k, counter) {
this.state = new Uint32Array(16) this.state = new Uint32Array(16)
for (let i = 0; i < 4; i++) this.state[i] = constant[i] for (let i = 0; i < 4; i++) this.state[i] = constant[i]
for (let i = 0; i < 8; i++) this.state[4 + i] = k.readUInt32LE(4 * i) for (let i = 0; i < 8; i++) this.state[4 + i] = readUInt32LE(k, 4 * i)
this.state[12] = counter & 0xffffffff this.state[12] = counter & 0xffffffff
if (n.byteLength === 8) { if (n.byteLength === 8) {
this.state[13] = (counter && 0xffffffff00000000) >> 32 this.state[13] = (counter && 0xffffffff00000000) >> 32
this.state[14] = n.readUInt32LE(0) this.state[14] = readUInt32LE(n, 0)
this.state[15] = n.readUInt32LE(4) this.state[15] = readUInt32LE(n, 4)
} else { } else {
this.state[13] = n.readUInt32LE(0) this.state[13] = readUInt32LE(n, 0)
this.state[14] = n.readUInt32LE(4) this.state[14] = readUInt32LE(n, 4)
this.state[15] = n.readUInt32LE(8) this.state[15] = readUInt32LE(n, 8)
} }
return this return this
@ -207,3 +207,13 @@ function QR (obj, a, b, c, d) {
obj[b] ^= obj[c] obj[b] ^= obj[c]
obj[b] = rotl(obj[b], 7) obj[b] = rotl(obj[b], 7)
} }
function readUInt32LE (buf, offset) {
if (Buffer.isBuffer(buf)) return buf.readUInt32LE(offset)
else if (buf instanceof Uint8Array) {
var ret = 0
for (let i = 0; i < 4; i++) ret |= buf[offset + i] << (8 * i)
return ret
}
assert(false, 'buf should be a Buffer or a Uint8Array')
}