Skip to content

Commit 3c6abc9

Browse files
authored
Argon2: set hard limit for config.maxArgon2MemoryExponent to cap memory at 1 TiB (#2014)
While the argon2 lib in principle supports a larger value, this new limit is still unrealistically large, and it enables the safe use of bitwise operations. Thanks to Shirsendu Mondal (@Shirshaw64p) for the report.
1 parent cba2904 commit 3c6abc9

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@ const config: Config = {
173173
},
174174
/**
175175
* Max memory exponent allowed for Argon2 memory allocation (e.g. `maxArgon2MemoryExponent: 20` corresponds
176-
* to a memory limit of 2**20 = 1GiB).
176+
* to a memory limit of 2**20 KiB = 1GiB).
177177
* This limit is applied both on encryption (if `config.s2kType` is set to `enums.s2k.argon2`)
178178
* and decryption.
179179
* If the input memory exponent exceeds this value, the library will not attempt the argon2 key derivation
180180
* and instead directly throw an `Argon2OutOfMemoryError` error.
181181
* NB: on encryption, if `s2kArgon2Params.memoryExponent` is larger than `maxArgon2MemoryExponent`,
182182
* the operation will fail.
183183
*/
184-
maxArgon2MemoryExponent: Infinity,
184+
maxArgon2MemoryExponent: 30,
185185
/**
186186
* Allow decryption of messages without integrity protection.
187187
* This is an **insecure** setting:

src/type/s2k/argon2.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import { getRandomBytes } from '../../crypto/index.js';
77
const ARGON2_TYPE = 0x02; // id
88
const ARGON2_VERSION = 0x13;
99
const ARGON2_SALT_SIZE = 16;
10+
/**
11+
* Max exponent supported, that applies regardless of `config.maxArgon2MemoryExponent`;
12+
* the argon2 lib in principle supports a larger value, but this is already unrealistically large,
13+
* and it enables us to use bitwise operations.
14+
*/
15+
const ARGON2_MAX_ENCODEDM = 30;
1016

1117
export class Argon2OutOfMemoryError extends Error {
1218
constructor(...params) {
@@ -104,10 +110,13 @@ class Argon2S2K {
104110
* @async
105111
*/
106112
async produceKey(passphrase, keySize, config) {
113+
if (config.maxArgon2MemoryExponent > ARGON2_MAX_ENCODEDM) {
114+
throw new Argon2OutOfMemoryError(`'config.maxArgon2MemoryExponent' exceeds the max allowed value of ${ARGON2_MAX_ENCODEDM}`);
115+
}
107116
if (this.encodedM > config.maxArgon2MemoryExponent) {
108117
throw new Argon2OutOfMemoryError('Argon2 required memory exceeds `config.maxArgon2MemoryExponent`');
109118
}
110-
const decodedM = 2 << (this.encodedM - 1);
119+
const decodedM = 1 << this.encodedM;
111120

112121
try {
113122
// on first load, the argon2 lib is imported and the WASM module is initialized.

0 commit comments

Comments
 (0)