Skip to content

Commit 160bb51

Browse files
committed
test: add AAD to tampered ciphertext/tag tests for consistency
1 parent ed317b0 commit 160bb51

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

example/src/tests/cipher/cipher_tests.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,12 @@ test(SUITE, 'GCM wrong key throws error (issue #798)', () => {
167167

168168
test(SUITE, 'GCM tampered ciphertext throws error', () => {
169169
const testKey = Buffer.from(randomFillSync(new Uint8Array(32)));
170-
const testIv = Buffer.from(randomFillSync(new Uint8Array(12)));
170+
const testIv = randomFillSync(new Uint8Array(12));
171171
const testPlaintext = Buffer.from('test data');
172+
const testAad = Buffer.from('additional data');
172173

173-
const cipher = createCipheriv('aes-256-gcm', testKey, testIv);
174+
const cipher = createCipheriv('aes-256-gcm', testKey, Buffer.from(testIv));
175+
cipher.setAAD(testAad);
174176
const encrypted = Buffer.concat([
175177
cipher.update(testPlaintext),
176178
cipher.final(),
@@ -180,7 +182,12 @@ test(SUITE, 'GCM tampered ciphertext throws error', () => {
180182
// Tamper with ciphertext
181183
encrypted[0] = encrypted[0]! ^ 1;
182184

183-
const decipher = createDecipheriv('aes-256-gcm', testKey, testIv);
185+
const decipher = createDecipheriv(
186+
'aes-256-gcm',
187+
testKey,
188+
Buffer.from(testIv),
189+
);
190+
decipher.setAAD(testAad);
184191
decipher.setAuthTag(authTag);
185192
decipher.update(encrypted);
186193

@@ -189,10 +196,12 @@ test(SUITE, 'GCM tampered ciphertext throws error', () => {
189196

190197
test(SUITE, 'GCM tampered auth tag throws error', () => {
191198
const testKey = Buffer.from(randomFillSync(new Uint8Array(32)));
192-
const testIv = Buffer.from(randomFillSync(new Uint8Array(12)));
199+
const testIv = randomFillSync(new Uint8Array(12));
193200
const testPlaintext = Buffer.from('test data');
201+
const testAad = Buffer.from('additional data');
194202

195-
const cipher = createCipheriv('aes-256-gcm', testKey, testIv);
203+
const cipher = createCipheriv('aes-256-gcm', testKey, Buffer.from(testIv));
204+
cipher.setAAD(testAad);
196205
const encrypted = Buffer.concat([
197206
cipher.update(testPlaintext),
198207
cipher.final(),
@@ -202,7 +211,12 @@ test(SUITE, 'GCM tampered auth tag throws error', () => {
202211
// Tamper with auth tag
203212
authTag[0] = authTag[0]! ^ 1;
204213

205-
const decipher = createDecipheriv('aes-256-gcm', testKey, testIv);
214+
const decipher = createDecipheriv(
215+
'aes-256-gcm',
216+
testKey,
217+
Buffer.from(testIv),
218+
);
219+
decipher.setAAD(testAad);
206220
decipher.setAuthTag(authTag);
207221
decipher.update(encrypted);
208222

0 commit comments

Comments
 (0)