pqbox 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -121,6 +121,42 @@ export interface PasswordHashOptions {
121
121
  }
122
122
  /** Hashes a password with Argon2id; returns a PHC string. Strings are UTF-8 encoded. */
123
123
  export declare function passwordHash(password: string | Uint8Array, options?: PasswordHashOptions): string;
124
+ /** Shortest salt {@link passwordDeriveKey} accepts, in bytes (the length of {@link passwordGenerateSalt}). */
125
+ export declare const PASSWORD_MIN_SALT_LENGTH = 16;
126
+ /** Shortest key {@link passwordDeriveKey} derives, in bytes. */
127
+ export declare const PASSWORD_MIN_KEY_LENGTH = 16;
128
+ /** Longest key {@link passwordDeriveKey} derives, in bytes. */
129
+ export declare const PASSWORD_MAX_KEY_LENGTH = 64;
130
+ /** A fresh random 16-byte salt for {@link passwordDeriveKey}; store it with the data. */
131
+ export declare function passwordGenerateSalt(): Uint8Array;
132
+ /** Options of {@link passwordDeriveKey}. */
133
+ export interface PasswordDeriveKeyOptions {
134
+ /** Key length in bytes: an integer from 16 to 64, 32 by default. */
135
+ length?: number;
136
+ /** Memory in KiB, 65536 (64 MiB) by default. */
137
+ mCost?: number;
138
+ /** Passes, 3 by default. */
139
+ tCost?: number;
140
+ /** Lanes, 1 by default. */
141
+ pCost?: number;
142
+ /** Largest memory cost accepted, {@link PASSWORD_LIMITS} by default; raise only for costs from a trusted source. */
143
+ maxMCost?: number;
144
+ /** Largest number of passes accepted. */
145
+ maxTCost?: number;
146
+ /** Largest number of lanes accepted. */
147
+ maxPCost?: number;
148
+ }
149
+ /**
150
+ * Derives a key from a password and a stored salt with Argon2id (version 0x13, no secret, no
151
+ * associated data): the same inputs always give the same key. Strings are UTF-8 encoded.
152
+ *
153
+ * The salt must be at least 16 bytes ({@link passwordGenerateSalt}); costs default to those of
154
+ * {@link passwordHash} and are checked against the limits before any memory is allocated. Throws
155
+ * `PqboxError` (`InvalidParams`) for anything out of range. Keep keys for different purposes
156
+ * apart with distinct salts, or expand one derived key with {@link hkdfSha256} and one `info`
157
+ * label per purpose.
158
+ */
159
+ export declare function passwordDeriveKey(password: string | Uint8Array, salt: Uint8Array, options?: PasswordDeriveKeyOptions): Uint8Array;
124
160
  /**
125
161
  * Cost limits of {@link passwordVerify}, {@link PASSWORD_LIMITS} by default. Raise them only for
126
162
  * PHC strings from a trusted source: at the limits, one verification uses 256 MiB and about a
package/dist/index.js CHANGED
@@ -193,6 +193,36 @@ export function passwordHash(password, options = {}) {
193
193
  const p = intOption(options.pCost, 1, U32_MAX, "Argon2 p_cost out of range");
194
194
  return call(() => withPassword(password, (pw) => wasm.passwordHash(pw, m, t, p)));
195
195
  }
196
+ /** Shortest salt {@link passwordDeriveKey} accepts, in bytes (the length of {@link passwordGenerateSalt}). */
197
+ export const PASSWORD_MIN_SALT_LENGTH = 16;
198
+ /** Shortest key {@link passwordDeriveKey} derives, in bytes. */
199
+ export const PASSWORD_MIN_KEY_LENGTH = 16;
200
+ /** Longest key {@link passwordDeriveKey} derives, in bytes. */
201
+ export const PASSWORD_MAX_KEY_LENGTH = 64;
202
+ /** A fresh random 16-byte salt for {@link passwordDeriveKey}; store it with the data. */
203
+ export function passwordGenerateSalt() {
204
+ return call(() => wasm.passwordGenerateSalt());
205
+ }
206
+ /**
207
+ * Derives a key from a password and a stored salt with Argon2id (version 0x13, no secret, no
208
+ * associated data): the same inputs always give the same key. Strings are UTF-8 encoded.
209
+ *
210
+ * The salt must be at least 16 bytes ({@link passwordGenerateSalt}); costs default to those of
211
+ * {@link passwordHash} and are checked against the limits before any memory is allocated. Throws
212
+ * `PqboxError` (`InvalidParams`) for anything out of range. Keep keys for different purposes
213
+ * apart with distinct salts, or expand one derived key with {@link hkdfSha256} and one `info`
214
+ * label per purpose.
215
+ */
216
+ export function passwordDeriveKey(password, salt, options = {}) {
217
+ const length = intOption(options.length, 32, PASSWORD_MAX_KEY_LENGTH, "derived key length out of range");
218
+ const m = intOption(options.mCost, 65536, U32_MAX, "Argon2 m_cost out of range");
219
+ const t = intOption(options.tCost, 3, U32_MAX, "Argon2 t_cost out of range");
220
+ const p = intOption(options.pCost, 1, U32_MAX, "Argon2 p_cost out of range");
221
+ const maxM = intOption(options.maxMCost, PASSWORD_LIMITS.maxMCost, U32_MAX, "Argon2 maxMCost out of range");
222
+ const maxT = intOption(options.maxTCost, PASSWORD_LIMITS.maxTCost, U32_MAX, "Argon2 maxTCost out of range");
223
+ const maxP = intOption(options.maxPCost, PASSWORD_LIMITS.maxPCost, U32_MAX, "Argon2 maxPCost out of range");
224
+ return call(() => withPassword(password, (pw) => wasm.passwordDeriveKey(pw, salt, length, m, t, p, maxM, maxT, maxP)));
225
+ }
196
226
  /**
197
227
  * Checks a password against a PHC string: `false` on mismatch, throws `PqboxError`
198
228
  * (`InvalidPasswordHash`) for an invalid string or one whose costs exceed the limits.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pqbox",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "Post-quantum crypto toolkit (ML-KEM, ML-DSA, SLH-DSA, Classic McEliece hybrids, sealed boxes, AEAD, KDF, hashing, Argon2id) compiled from Rust to WebAssembly",
5
5
  "license": "MIT",
6
6
  "author": "maxgfr",