import type { BufferSource } from "@sammo/util"; const subtle = globalThis.crypto.subtle; export async function AES_GCM_Encrypt(key: BufferSource | CryptoKey, iv: BufferSource, msg: BufferSource, aad?: BufferSource): Promise { const keyObj = key instanceof CryptoKey ? key : await subtle.importKey("raw", key, { name: "AES-GCM", }, false, ["encrypt"]); const ciphertext = await subtle.encrypt({ name: "AES-GCM", iv, additionalData: aad }, keyObj, msg); return ciphertext; } export async function AES_GCM_Decrypt(key: BufferSource | CryptoKey, iv: BufferSource, ciphertext: BufferSource, aad?: BufferSource): Promise { const keyObj = key instanceof CryptoKey ? key : await subtle.importKey("raw", key, { name: "AES-GCM", }, false, ["decrypt"]); const plaintext = await subtle.decrypt({ name: "AES-GCM", iv: iv, additionalData: aad }, keyObj, ciphertext); //아마도 tag 검증 실패시 예외가 발생할 것으로 예상 return plaintext; } export async function AES_CBC_Encrypt(key: BufferSource | CryptoKey, iv: BufferSource, msg: BufferSource): Promise { const keyObj = key instanceof CryptoKey ? key : await subtle.importKey("raw", key, { name: "AES-CBC", }, false, ["encrypt"]); const ciphertext = await subtle.encrypt({ name: "AES-CBC", iv, }, keyObj, msg); return ciphertext; } export async function AES_CBC_Decrypt(key: BufferSource | CryptoKey, iv: BufferSource, ciphertext: BufferSource): Promise { const keyObj = key instanceof CryptoKey ? key : await subtle.importKey("raw", key, { name: "AES-CBC", }, false, ["decrypt"]); const plaintext = await subtle.decrypt({ name: "AES-CBC", iv: iv, }, keyObj, ciphertext); return plaintext; }