From 32dcb3108566c64b120b56f79b069534eb276c13 Mon Sep 17 00:00:00 2001 From: antelle Date: Tue, 14 Nov 2017 22:46:23 +0100 Subject: [PATCH] support more memory in argon2 --- app/scripts/util/kdbxweb-init.js | 22 ++++++++++++++++++---- bower.json | 2 +- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/scripts/util/kdbxweb-init.js b/app/scripts/util/kdbxweb-init.js index 2cce80f1..3f70f7cc 100644 --- a/app/scripts/util/kdbxweb-init.js +++ b/app/scripts/util/kdbxweb-init.js @@ -10,7 +10,7 @@ const KdbxwebInit = { argon2(password, salt, memory, iterations, length, parallelism, type, version) { const args = { password, salt, memory, iterations, length, parallelism, type, version }; - return this.loadRuntime().then(runtime => { + return this.loadRuntime(memory).then(runtime => { const ts = logger.ts(); return runtime.hash(args).then(hash => { logger.debug('Hash computed', logger.ts(ts)); @@ -19,7 +19,7 @@ const KdbxwebInit = { }); }, - loadRuntime() { + loadRuntime(requiredMemory) { if (this.runtimeModule) { return Promise.resolve(this.runtimeModule); } @@ -32,15 +32,27 @@ const KdbxwebInit = { const ts = logger.ts(); const argon2LoaderCode = require('argon2'); const wasmBinaryBase64 = require('argon2-wasm'); - const moduleDecl = '{' + + + const KB = 1024 * 1024; + const MB = 1024 * KB; + const GB = 1024 * MB; + const WASM_PAGE_SIZE = 64 * 1024; + const totalMemory = (2 * GB - 64 * KB) / 1024 / WASM_PAGE_SIZE; + const initialMemory = Math.min(Math.max(Math.ceil(requiredMemory * 1024 / WASM_PAGE_SIZE), 256) + 256, totalMemory); + + const memoryDecl = `var wasmMemory=new WebAssembly.Memory({initial:${initialMemory},maximum:${totalMemory}});`; + const moduleDecl = 'var Module={' + 'wasmJSMethod: "native-wasm",' + 'wasmBinary: Uint8Array.from(atob("' + wasmBinaryBase64 + '"), c => c.charCodeAt(0)),' + 'print(...args) { postMessage({op:"log",args}) },' + 'printErr(...args) { postMessage({op:"log",args}) },' + 'postRun:' + this.workerPostRun.toString() + ',' + 'calcHash:' + this.calcHash.toString() + ',' + + 'wasmMemory:wasmMemory,' + + 'buffer:wasmMemory.buffer,' + + 'TOTAL_MEMORY:' + initialMemory * WASM_PAGE_SIZE + '}'; - const script = argon2LoaderCode.replace('var Module', 'var Module=' + moduleDecl); + const script = argon2LoaderCode.replace('var Module', memoryDecl + moduleDecl); const blob = new Blob([script], {type: 'application/javascript'}); const objectUrl = URL.createObjectURL(blob); const worker = new Worker(objectUrl); @@ -60,6 +72,8 @@ const KdbxwebInit = { worker.postMessage(args); const onHashMessage = e => { worker.removeEventListener('message', onHashMessage); + worker.terminate(); + KdbxwebInit.runtimeModule = null; if (!e.data || e.data.error || !e.data.hash) { const ex = e.data && e.data.error || 'unexpected error'; logger.error('Worker error', ex); diff --git a/bower.json b/bower.json index 6b7a24a6..3ce201d2 100644 --- a/bower.json +++ b/bower.json @@ -32,6 +32,6 @@ "FileSaver.js": "eligrey/FileSaver.js", "jquery": "3.1.1", "jsqrcode": "antelle/jsqrcode#0.1.3", - "argon2-browser": "1.0.0" + "argon2-browser": "1.2.0" } }