diff --git a/server/util/aes.ts b/server/util/aes.ts index 340d974..b0fc740 100644 --- a/server/util/aes.ts +++ b/server/util/aes.ts @@ -1,15 +1,9 @@ import { webcrypto } from "crypto"; const subtle = webcrypto.subtle; +type BufferSource = ArrayBufferView | ArrayBuffer; -type EncryptedData = { - iv: ArrayBuffer; - ciphertext: ArrayBuffer; //tag 포함 -} - - -export async function AES_GCM_Encrypt(key: ArrayBuffer, msg: ArrayBuffer, aad?: ArrayBuffer): Promise { - const iv = webcrypto.getRandomValues(new Uint8Array(12)); +export async function AES_GCM_Encrypt(key: BufferSource, iv: BufferSource, msg: BufferSource, aad?: BufferSource): Promise { const keyObj = await subtle.importKey("raw", key, { name: "AES-GCM", }, false, ["encrypt"]); @@ -20,23 +14,20 @@ export async function AES_GCM_Encrypt(key: ArrayBuffer, msg: ArrayBuffer, aad?: additionalData: aad }, keyObj, msg); - return { - iv, - ciphertext - } + return ciphertext; } -export async function AES_GCM_Decrypt(key: ArrayBuffer, data: EncryptedData, aad?: ArrayBuffer): Promise { +export async function AES_GCM_Decrypt(key: BufferSource, iv: BufferSource, ciphertext: BufferSource, aad?: BufferSource): Promise { const keyObj = await subtle.importKey("raw", key, { name: "AES-GCM", }, false, ["decrypt"]); const plaintext = await subtle.decrypt({ name: "AES-GCM", - iv: data.iv, + iv: iv, additionalData: aad - }, keyObj, data.ciphertext); + }, keyObj, ciphertext); //아마도 tag 검증 실패시 예외가 발생할 것으로 예상 return plaintext;