Skip to content

Commit 54114c5

Browse files
committed
fix(keys): decryptWithKey has to catch verification exception from sodium decryption
1 parent a0bd3ec commit 54114c5

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/keys/utils/symmetric.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,21 @@ function decryptWithKey(
8383
const plainText = Buffer.allocUnsafeSlow(
8484
macAndCipherText.byteLength - macSize,
8585
);
86-
// This returns the number of bytes that has been decrypted
87-
const decrypted = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(
88-
plainText,
89-
null,
90-
macAndCipherText,
91-
additionalData,
92-
nonce,
93-
key,
94-
);
86+
let decrypted: number;
87+
try {
88+
// This returns the number of bytes that has been decrypted
89+
// It will throw if the MAC cannot be authenticated
90+
decrypted = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(
91+
plainText,
92+
null,
93+
macAndCipherText,
94+
additionalData,
95+
nonce,
96+
key,
97+
);
98+
} catch {
99+
return;
100+
}
95101
if (decrypted !== plainText.byteLength) {
96102
return;
97103
}

0 commit comments

Comments
 (0)