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
  • Loading branch information
larabr committed Apr 11, 2024
1 parent ad4d580 commit 5c98c7f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions openpgp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ export namespace enums {
}

export declare class Argon2S2K {
static reloadWasmModule(): void;
constructor(config: Config);
salt: Uint8Array;
/** @throws Argon2OutOfMemoryError */
Expand Down
12 changes: 9 additions & 3 deletions src/type/s2k/argon2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ let argon2Promise;
const ARGON2_WASM_MEMORY_THRESHOLD_RELOAD = 2 << 19;

class Argon2S2K {
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 @@ -114,9 +122,7 @@ 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(() => {});
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 5c98c7f

Please sign in to comment.