Fix MAC location

This commit is contained in:
2021-07-30 18:32:25 -05:00
parent 7fe1420876
commit d428445f00
4 changed files with 15 additions and 22 deletions

View File

@@ -17,15 +17,13 @@ import {
ui8_array_to_ui8,
ui8_to_ui8_array,
} from '../utils/byte_order';
import {XChaCha20Poly1305} from '@stablelib/xchacha20poly1305';
import { XChaCha20Poly1305 } from '@stablelib/xchacha20poly1305';
export default class packet {
constructor(token) {
this.token = token;
}
static HEADER = new TextEncoder().encode(this.HEADER_STRING);
buffer = null;
decode_offset = 0;
token;
@@ -163,7 +161,7 @@ export default class packet {
const key = Uint8Array.from(hash.digest());
const nonce = this.buffer.slice(0, 24);
const mac = this.buffer.slice(24, 16);
const mac = this.buffer.slice(24, 16 + 24);
const customEncryption = getCustomEncryption();
if (customEncryption) {
@@ -177,9 +175,13 @@ export default class packet {
'base64'
);
} else {
const aad = ui32_to_be_ui8_array(this.buffer.length);
const aad = ui32_to_be_ui8_array(this.buffer.length);
this.buffer = new Uint8Array([
...this.buffer.slice(nonce.length + mac.length),
...this.buffer.slice(nonce.length, nonce.length + mac.length),
]);
this.buffer = Buffer.from(
new XChaCha20Poly1305(key).open(nonce, this.buffer.slice(24), aad),
new XChaCha20Poly1305(key).open(nonce, this.buffer, aad)
);
}
@@ -299,8 +301,12 @@ export default class packet {
)
);
} else {
const aad = ui32_to_be_ui8_array(this.buffer.length + 40)
const aad = ui32_to_be_ui8_array(this.buffer.length + 40);
this.buffer = new XChaCha20Poly1305(key).seal(nonce, this.buffer, aad);
this.buffer = new Uint8Array([
...this.buffer.slice(this.buffer.length - 16),
...this.buffer.slice(0, this.buffer.length - 16),
]);
this.push_buffer(nonce);
}