diff --git a/SharpbinV3.Client/src/lib/components/PasteComments.svelte b/SharpbinV3.Client/src/lib/components/PasteComments.svelte index ae31efe..1d0aea9 100644 --- a/SharpbinV3.Client/src/lib/components/PasteComments.svelte +++ b/SharpbinV3.Client/src/lib/components/PasteComments.svelte @@ -237,7 +237,10 @@ } -
+

{comments.length} Comments

diff --git a/SharpbinV3.Client/src/lib/utils/argon2.worker.ts b/SharpbinV3.Client/src/lib/utils/argon2.worker.ts new file mode 100644 index 0000000..4b28062 --- /dev/null +++ b/SharpbinV3.Client/src/lib/utils/argon2.worker.ts @@ -0,0 +1,9 @@ +import { argon2id } from 'hash-wasm'; + +self.onmessage = async (event: MessageEvent[0]>) => { + try { + self.postMessage(await argon2id(event.data)); + } catch { + self.postMessage(null); + } +}; diff --git a/SharpbinV3.Client/src/lib/utils/encryption.ts b/SharpbinV3.Client/src/lib/utils/encryption.ts index ea67bd3..9a551d5 100644 --- a/SharpbinV3.Client/src/lib/utils/encryption.ts +++ b/SharpbinV3.Client/src/lib/utils/encryption.ts @@ -1,5 +1,3 @@ -import { argon2id } from 'hash-wasm'; - const BASE64_CHUNK_SIZE = 0x8000; const SALT_LENGTH = 16; const IV_LENGTH = 12; @@ -9,8 +7,58 @@ const ARGON2_PARALLELISM = 1; const ARGON2_HASH_LENGTH = 32; const AES_KEY_LENGTH = 256; const ENCRYPTION_AAD = 'enc-v4'; -const ENCRYPTED_DATA_OVERHEAD = 60; -const ENCRYPTED_METADATA_OVERHEAD = 150; +const ENCRYPTED_DATA_OVERHEAD = SALT_LENGTH + IV_LENGTH + 16; +const MAX_ENVELOPE_LENGTH = 16 * 1024 * 1024; +const ENCRYPTION_HEADER = { + version: 5, + kdf: 'Argon2id', + memorySize: ARGON2_MEMORY_SIZE, + iterations: ARGON2_ITERATIONS, + parallelism: ARGON2_PARALLELISM, + algorithm: 'AES-GCM', + saltLength: SALT_LENGTH, + ivLength: IV_LENGTH +}; +let derivingKey = false; + +async function argon2id( + options: Parameters[0] +): Promise> { + if (derivingKey) throw new Error('Another encryption operation is already running.'); + derivingKey = true; + try { + return await new Promise((resolve, reject) => { + const worker = new Worker(new URL('./argon2.worker.ts', import.meta.url), { type: 'module' }); + const finish = () => { + clearTimeout(timeout); + worker.terminate(); + }; + const timeout = setTimeout(() => { + finish(); + reject(new Error('Key derivation timed out.')); + }, 60_000); + worker.onmessage = (event: MessageEvent) => { + finish(); + if (event.data instanceof Uint8Array && event.data.length === ARGON2_HASH_LENGTH) { + resolve(new Uint8Array(event.data)); + } else reject(new Error('Key derivation failed. The device may lack memory.')); + }; + worker.onerror = (event) => { + event.preventDefault(); + finish(); + reject(new Error('Unable to run the encryption worker.')); + }; + try { + worker.postMessage(options); + } catch (error) { + finish(); + reject(error); + } + }); + } finally { + derivingKey = false; + } +} function toBase64(bytes: Uint8Array): string { let binary = ''; @@ -29,7 +77,21 @@ function fromBase64(data: string): Uint8Array { return bytes; } +export function encodePasswordFragment(password: string): string { + return `u1.${toBase64(new TextEncoder().encode(password))}`; +} + +export function decodePasswordFragment(fragment: string): string { + return fragment.startsWith('u1.') + ? new TextDecoder('utf-8', { fatal: true }).decode(fromBase64(fragment.slice(3))) + : atob(fragment); +} + export async function encryptAES(content: string, password: string): Promise { + if (!password) throw new Error('Password required for encryption.'); + if (estimateEncryptedSize(content) > MAX_ENVELOPE_LENGTH) { + throw new Error('Encrypted paste exceeds the supported size limit.'); + } const enc = new TextEncoder(); const salt = window.crypto.getRandomValues(new Uint8Array(SALT_LENGTH)); @@ -56,7 +118,7 @@ export async function encryptAES(content: string, password: string): Promise { try { const enc = new TextEncoder(); + if (result.length > MAX_ENVELOPE_LENGTH) { + throw new Error('Encrypted paste exceeds the supported size limit.'); + } const parsed = JSON.parse(result); + if (!parsed || typeof parsed !== 'object' || parsed.algorithm !== 'AES-GCM') { + throw new Error('Unsupported or malformed encrypted paste.'); + } + const legacyPBKDF2 = + parsed.version === 1 && + parsed.kdf === 'PBKDF2' && + parsed.iterations === 100000 && + parsed.hash === 'SHA-256' && + (parsed.saltLength === undefined || parsed.saltLength === SALT_LENGTH) && + (parsed.ivLength === undefined || parsed.ivLength === IV_LENGTH); + const legacyArgon2 = + parsed.version === 4 && parsed.memorySize === 65536 && parsed.saltLength === 32; + const currentArgon2 = + (parsed.version === 4 || parsed.version === 5) && + parsed.memorySize === ARGON2_MEMORY_SIZE && + parsed.saltLength === SALT_LENGTH; + if ( + !legacyPBKDF2 && + !( + parsed.kdf === 'Argon2id' && + parsed.iterations === ARGON2_ITERATIONS && + parsed.parallelism === ARGON2_PARALLELISM && + parsed.ivLength === IV_LENGTH && + (legacyArgon2 || currentArgon2) + ) + ) + throw new Error('Unsupported encryption profile.'); + if ( + typeof parsed.data !== 'string' || + !parsed.data.length || + parsed.data.length % 4 !== 0 || + !/^[A-Za-z0-9+/]*={0,2}$/.test(parsed.data) + ) { + throw new Error('Malformed encrypted-paste encoding.'); + } const combined = fromBase64(parsed.data); - const saltLength = parsed.saltLength || SALT_LENGTH; - const ivLength = parsed.ivLength || IV_LENGTH; + const saltLength = parsed.saltLength ?? SALT_LENGTH; + const ivLength = parsed.ivLength ?? IV_LENGTH; + if (combined.length < saltLength + ivLength + 16) { + throw new Error('Encrypted paste is truncated.'); + } const salt = combined.slice(0, saltLength); const iv = combined.slice(saltLength, saltLength + ivLength); const data = combined.slice(saltLength + ivLength); @@ -146,14 +242,27 @@ export async function decryptAES(result: string, password: string): Promise {data.paste.author.username} @@ -938,7 +954,7 @@