File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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:
Original file line number Diff line number Diff line change @@ -7,6 +7,12 @@ import { getRandomBytes } from '../../crypto/index.js';
77const ARGON2_TYPE = 0x02 ; // id
88const ARGON2_VERSION = 0x13 ;
99const 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
1117export 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.
You can’t perform that action at this time.
0 commit comments