Skip to content

Commit

Permalink
Add Argon2S2K.reloadWasmModule() for manually triggering memory dea…
Browse files Browse the repository at this point in the history
…llocation (#14)

Also, make `ARGON2_WASM_MEMORY_THRESHOLD_RELOAD`
a static class property, to be able to change its value.
  • Loading branch information
larabr authored Apr 12, 2024
1 parent ad4d580 commit 0c70aa6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
'extends': 'airbnb-base',
'parserOptions': {
'ecmaVersion': 11,
'ecmaVersion': 2022,
'sourceType': 'module'
},

Expand Down
2 changes: 2 additions & 0 deletions openpgp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,8 @@ export namespace enums {
}

export declare class Argon2S2K {
static reloadWasmModule(): void;
static ARGON2_WASM_MEMORY_THRESHOLD_RELOAD: number;
constructor(config: Config);
salt: Uint8Array;
/** @throws Argon2OutOfMemoryError */
Expand Down
25 changes: 20 additions & 5 deletions src/type/s2k/argon2.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,26 @@ export class Argon2OutOfMemoryError extends Error {
let loadArgonWasmModule;
let argon2Promise;
// reload wasm module above this treshold, to deallocated used memory
const ARGON2_WASM_MEMORY_THRESHOLD_RELOAD = 2 << 19;
// (cannot be declared as a simple `static` field as its not supported by Safari 14)
let ARGON2_WASM_MEMORY_THRESHOLD_RELOAD = 2 << 19;

class Argon2S2K {
static get ARGON2_WASM_MEMORY_THRESHOLD_RELOAD() {
return ARGON2_WASM_MEMORY_THRESHOLD_RELOAD;
}

static set ARGON2_WASM_MEMORY_THRESHOLD_RELOAD(memoryThreshold) {
ARGON2_WASM_MEMORY_THRESHOLD_RELOAD = memoryThreshold;
}

static reloadWasmModule() {
if (!loadArgonWasmModule) return;

// it will be awaited if needed at the next `produceKey` invocation
argon2Promise = loadArgonWasmModule();
argon2Promise.catch(() => {});
}

/**
* @param {Object} [config] - Full configuration, defaults to openpgp.config
*/
Expand Down Expand Up @@ -113,10 +130,8 @@ class Argon2S2K {
});

// a lot of memory was used, reload to deallocate
if (decodedM > ARGON2_WASM_MEMORY_THRESHOLD_RELOAD) {
// it will be awaited if needed at the next `produceKey` invocation
argon2Promise = loadArgonWasmModule();
argon2Promise.catch(() => {});
if (decodedM > Argon2S2K.ARGON2_WASM_MEMORY_THRESHOLD_RELOAD) {
Argon2S2K.reloadWasmModule();
}
return hash;
} catch (e) {
Expand Down
14 changes: 14 additions & 0 deletions test/benchmarks/memory_usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,20 @@ class MemoryBenchamrkSuite {
});
});

suite.add('openpgp.encrypt/decryptSessionKeys (argon2)', async () => {
const config = { s2kType: openpgp.enums.s2k.argon2 };
const passwords = 'password';
const sessionKey = {
algorithm: 'aes128',
data: require('crypto').getRandomValues(new Uint8Array(16))
};
const encrypted = await openpgp.encryptSessionKey({ ...sessionKey, passwords, config, format: 'object' });
assert(encrypted.packets.length === 1);
const skesk = encrypted.packets[0];
assert(skesk.s2k.type === 'argon2');
await openpgp.decryptSessionKeys({ message: encrypted, passwords });
});

const stats = await suite.run();
// Print JSON stats to stdout
console.log(JSON.stringify(stats, null, 4));
Expand Down

0 comments on commit 0c70aa6

Please sign in to comment.