( bundle: Uint8Array, dataKey: Uint8Array, )
| 161 | } |
| 162 | |
| 163 | export async function decryptWithDataKey( |
| 164 | bundle: Uint8Array, |
| 165 | dataKey: Uint8Array, |
| 166 | ): Promise<unknown | null> { |
| 167 | if (bundle.length < 1 || bundle[0] !== 0) return null; |
| 168 | if (bundle.length < 13 + 16) return null; |
| 169 | |
| 170 | const nonce = bundle.slice(1, 13); |
| 171 | // SubtleCrypto expects authTag appended to ciphertext |
| 172 | const ciphertextWithTag = bundle.slice(13); |
| 173 | |
| 174 | try { |
| 175 | const key = await getAesKey(dataKey); |
| 176 | const decrypted = await crypto.subtle.decrypt( |
| 177 | { name: 'AES-GCM', iv: toArrayBuffer(nonce) }, |
| 178 | key, |
| 179 | toArrayBuffer(ciphertextWithTag), |
| 180 | ); |
| 181 | return JSON.parse(new TextDecoder().decode(decrypted)); |
| 182 | } catch { |
| 183 | return null; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // --- Unified encrypt/decrypt --- |
| 188 |
no test coverage detected