support more memory in argon2

This commit is contained in:
antelle 2017-11-14 22:46:23 +01:00
parent 5e6cb5fc4a
commit 32dcb31085
2 changed files with 19 additions and 5 deletions

View File

@ -10,7 +10,7 @@ const KdbxwebInit = {
argon2(password, salt, memory, iterations, length, parallelism, type, version) { argon2(password, salt, memory, iterations, length, parallelism, type, version) {
const args = { 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(); const ts = logger.ts();
return runtime.hash(args).then(hash => { return runtime.hash(args).then(hash => {
logger.debug('Hash computed', logger.ts(ts)); logger.debug('Hash computed', logger.ts(ts));
@ -19,7 +19,7 @@ const KdbxwebInit = {
}); });
}, },
loadRuntime() { loadRuntime(requiredMemory) {
if (this.runtimeModule) { if (this.runtimeModule) {
return Promise.resolve(this.runtimeModule); return Promise.resolve(this.runtimeModule);
} }
@ -32,15 +32,27 @@ const KdbxwebInit = {
const ts = logger.ts(); const ts = logger.ts();
const argon2LoaderCode = require('argon2'); const argon2LoaderCode = require('argon2');
const wasmBinaryBase64 = require('argon2-wasm'); 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",' + 'wasmJSMethod: "native-wasm",' +
'wasmBinary: Uint8Array.from(atob("' + wasmBinaryBase64 + '"), c => c.charCodeAt(0)),' + 'wasmBinary: Uint8Array.from(atob("' + wasmBinaryBase64 + '"), c => c.charCodeAt(0)),' +
'print(...args) { postMessage({op:"log",args}) },' + 'print(...args) { postMessage({op:"log",args}) },' +
'printErr(...args) { postMessage({op:"log",args}) },' + 'printErr(...args) { postMessage({op:"log",args}) },' +
'postRun:' + this.workerPostRun.toString() + ',' + 'postRun:' + this.workerPostRun.toString() + ',' +
'calcHash:' + this.calcHash.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 blob = new Blob([script], {type: 'application/javascript'});
const objectUrl = URL.createObjectURL(blob); const objectUrl = URL.createObjectURL(blob);
const worker = new Worker(objectUrl); const worker = new Worker(objectUrl);
@ -60,6 +72,8 @@ const KdbxwebInit = {
worker.postMessage(args); worker.postMessage(args);
const onHashMessage = e => { const onHashMessage = e => {
worker.removeEventListener('message', onHashMessage); worker.removeEventListener('message', onHashMessage);
worker.terminate();
KdbxwebInit.runtimeModule = null;
if (!e.data || e.data.error || !e.data.hash) { if (!e.data || e.data.error || !e.data.hash) {
const ex = e.data && e.data.error || 'unexpected error'; const ex = e.data && e.data.error || 'unexpected error';
logger.error('Worker error', ex); logger.error('Worker error', ex);

View File

@ -32,6 +32,6 @@
"FileSaver.js": "eligrey/FileSaver.js", "FileSaver.js": "eligrey/FileSaver.js",
"jquery": "3.1.1", "jquery": "3.1.1",
"jsqrcode": "antelle/jsqrcode#0.1.3", "jsqrcode": "antelle/jsqrcode#0.1.3",
"argon2-browser": "1.0.0" "argon2-browser": "1.2.0"
} }
} }