Skip to content

Commit

Permalink
fix: pass tag length for aes decipher
Browse files Browse the repository at this point in the history
  • Loading branch information
shamilovtim committed Mar 29, 2024
1 parent e95c128 commit 870ebef
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/mechs/aes/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Buffer } from "buffer";
import crypto, { CipherGCM, DecipherGCM } from "crypto";
import crypto, { CipherGCMTypes } from "crypto";
import { JsonParser, JsonSerializer } from "@peculiar/json-schema";
import * as core from "webcrypto-core";
import { AesCryptoKey } from "./key";
Expand Down Expand Up @@ -136,9 +136,9 @@ export class AesCrypto {
}

public static async encryptAesGCM(algorithm: AesGcmParams, key: AesCryptoKey, data: Buffer) {
const cipher = crypto.createCipheriv(`aes-${key.algorithm.length}-gcm`, key.data, Buffer.from(algorithm.iv as ArrayBuffer), {
const cipher = crypto.createCipheriv(`aes-${key.algorithm.length}-gcm` as CipherGCMTypes, key.data, Buffer.from(algorithm.iv as ArrayBuffer), {
authTagLength: (algorithm.tagLength || 128) >> 3,
} as any) as CipherGCM; // NodeJs d.ts doesn't support CipherGCMOptions for createCipheriv
}); // NodeJs d.ts doesn't support CipherGCMOptions for createCipheriv
if (algorithm.additionalData) {
cipher.setAAD(Buffer.from(algorithm.additionalData as ArrayBuffer));
}
Expand All @@ -149,8 +149,10 @@ export class AesCrypto {
}

public static async decryptAesGCM(algorithm: AesGcmParams, key: AesCryptoKey, data: Buffer) {
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-gcm`, key.data, new Uint8Array(algorithm.iv as ArrayBuffer)) as DecipherGCM;
const tagLength = (algorithm.tagLength || 128) >> 3;
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-gcm` as CipherGCMTypes, key.data, new Uint8Array(algorithm.iv as ArrayBuffer), {
authTagLength: tagLength,
});
const enc = data.slice(0, data.length - tagLength);
const tag = data.slice(data.length - tagLength);
if (algorithm.additionalData) {
Expand Down

0 comments on commit 870ebef

Please sign in to comment.