pqbox 1.0.0 → 1.1.0

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/README.md CHANGED
@@ -5,8 +5,9 @@ hybrid key encapsulation, sealed boxes, composite signatures, authenticated encr
5
5
  derivation, hashing and password hashing.
6
6
 
7
7
  pqbox implements no cryptographic primitive itself. Every algorithm comes from a pinned crate,
8
- mostly from [RustCrypto](https://github.com/RustCrypto), plus `classic-mceliece-rust` for Classic
9
- McEliece. pqbox only adds the hybrid combiners, the composite signature framing and a versioned
8
+ mostly from [RustCrypto](https://github.com/RustCrypto), plus Cryspen's formally verified
9
+ [libcrux](https://github.com/cryspen/libcrux) for ML-KEM-1024 and `classic-mceliece-rust` for
10
+ Classic McEliece. pqbox only adds the hybrid combiners, the composite signature framing and a versioned
10
11
  wire format.
11
12
 
12
13
  - **Rust:** crate [`pqbox`](crates/pqbox-core) (sans-IO, `#![forbid(unsafe_code)]`) and the CLI [`pqbox-cli`](crates/pqbox-cli).
@@ -87,7 +88,8 @@ order (signing normalizes it; the malleable `n - s` twin is refused).
87
88
 
88
89
  | Purpose | Algorithm | Crate |
89
90
  |---|---|---|
90
- | KEM | ML-KEM-768 / ML-KEM-1024 (FIPS 203) | `ml-kem` 0.3.4 |
91
+ | KEM | ML-KEM-1024 (FIPS 203), in `pqbox-max` and `pqbox-paranoid` | `libcrux-ml-kem` 0.0.12 (portable build) |
92
+ | KEM | ML-KEM-768 (FIPS 203), inside X-Wing | `ml-kem` 0.3.4 |
91
93
  | KEM | X-Wing (draft-connolly-cfrg-xwing-kem) | `x-wing` 0.1.1 |
92
94
  | KEM | Classic McEliece 8192128 | `classic-mceliece-rust` 3.1.0 |
93
95
  | KEM (classical half) | ECDH P-384, X25519 | `p384` 0.14.0, `x25519-dalek` 3.0.0 |
@@ -98,7 +100,7 @@ order (signing normalizes it; the malleable `n - s` twin is refused).
98
100
  | AEAD | XChaCha20-Poly1305 (default), AES-256-GCM | `chacha20poly1305` 0.11.0, `aes-gcm` 0.11.1 |
99
101
  | KDF / MAC | HKDF-SHA-256, HMAC-SHA-256 | `hkdf` 0.13.0, `hmac` 0.13.0 |
100
102
  | Hash | SHA-256, SHA3-256, BLAKE3 | `sha2` 0.11.1, `sha3` 0.12.0, `blake3` 1.8.9 |
101
- | Password | Argon2id (64 MiB, t=3, p=1 by default; verification capped at 256 MiB, t=10, p=8), PHC strings | `argon2` 0.6.0 |
103
+ | Password | Argon2id (64 MiB, t=3, p=1 by default; verification and key derivation capped at 256 MiB, t=10, p=8): PHC strings, and keys derived from a password and a stored salt | `argon2` 0.6.0 |
102
104
 
103
105
  Symmetric keys are always 256-bit.
104
106
 
@@ -117,7 +119,7 @@ show orders of magnitude, not guarantees.
117
119
  | Suite | KEM keygen | Encapsulate | Decapsulate | Seal 1 KiB | Open 1 KiB | Sign keygen | Sign | Verify |
118
120
  |---|---:|---:|---:|---:|---:|---:|---:|---:|
119
121
  | `xwing` | 53 µs | 50 µs | 80 µs | 54 µs | 83 µs | 264 µs | 378 µs | 61 µs |
120
- | `pqbox-max` | 401 µs | 733 µs | 759 µs | 738 µs | 763 µs | 730 µs | 775 µs | 446 µs |
122
+ | `pqbox-max` | 311 µs | 607 µs | 642 µs | 595 µs | 601 µs | 730 µs | 775 µs | 446 µs |
121
123
  | `pqbox-paranoid` | 966.99 ms | 1.13 ms | 62.03 ms | 2.37 ms | 62.98 ms | 37.48 ms | 435.01 ms | 1.06 ms |
122
124
 
123
125
  Paranoid KEM key generation varies a lot from run to run (0.74 s to 1.20 s here), because Classic
@@ -143,7 +145,7 @@ and `pqbox selftest` checks the installed binary offline.
143
145
  ### Rust
144
146
 
145
147
  ```rust
146
- use pqbox::{Suite, SuitePolicy, kem, seal, sign};
148
+ use pqbox::{Suite, SuitePolicy, aead, kem, password, seal, sign};
147
149
  use rand_core::UnwrapErr;
148
150
 
149
151
  let mut rng = UnwrapErr(getrandom::SysRng); // getrandom 0.4 with the `sys_rng` feature
@@ -168,6 +170,12 @@ let bytes = legacy.public_key().to_bytes();
168
170
  assert!(kem::PublicKey::from_bytes(&bytes).is_err()); // SuiteNotAllowed
169
171
  let policy = SuitePolicy::DEFAULT.allow(Suite::XWing);
170
172
  let pk = kem::PublicKey::from_bytes_with_policy(&bytes, policy)?;
173
+
174
+ // A key derived from a password: store the salt (and the costs) with the data, and derive the
175
+ // same key again later. Argon2id, 64 MiB, 3 passes, 1 lane by default; the key is wiped on drop.
176
+ let salt = password::generate_salt(&mut rng);
177
+ let key = password::derive_key(b"correct horse battery staple", &salt, password::Params::default(), 32)?;
178
+ let ct = aead::encrypt(aead::Algorithm::XChaCha20Poly1305, key.as_bytes(), b"data", b"header", &mut rng)?;
171
179
  # Ok::<(), pqbox::Error>(())
172
180
  ```
173
181
 
@@ -195,12 +203,17 @@ assert pqbox.decrypt(key, ct, aad=b"header") == b"data"
195
203
 
196
204
  phc = pqbox.password_hash("correct horse battery staple")
197
205
  assert pqbox.password_verify("correct horse battery staple", phc)
206
+
207
+ # A key derived from a password and a stored salt (Argon2id, same defaults as password_hash).
208
+ salt = pqbox.password_generate_salt() # 16 random bytes: store them with the data
209
+ key = pqbox.password_derive_key("correct horse battery staple", salt) # 32 bytes
210
+ assert pqbox.password_derive_key("correct horse battery staple", salt) == key
198
211
  ```
199
212
 
200
213
  ### JavaScript / TypeScript
201
214
 
202
215
  ```ts
203
- import { kemKeygen, seal, open, signKeygen, sign, verify, blake3 } from "pqbox";
216
+ import { kemKeygen, seal, open, signKeygen, sign, verify, blake3, passwordDeriveKey, passwordGenerateSalt } from "pqbox";
204
217
 
205
218
  const { publicKey, secretKey } = kemKeygen(); // "pqbox-max"
206
219
  const box = seal(publicKey, new TextEncoder().encode("hello"));
@@ -217,6 +230,10 @@ console.log(blake3(msg));
217
230
  const legacy = kemKeygen("xwing");
218
231
  const box2 = seal(legacy.publicKey, msg, { allowSuites: ["xwing"] });
219
232
  open(legacy.secretKey, box2, { allowSuites: ["xwing"] });
233
+
234
+ // A key derived from a password and a stored salt (Argon2id, same defaults as passwordHash).
235
+ const salt = passwordGenerateSalt(); // 16 random bytes: store them with the data
236
+ const key = passwordDeriveKey("correct horse battery staple", salt); // 32 bytes
220
237
  ```
221
238
 
222
239
  Errors are thrown as `PqboxError` (in Python, raised as `pqbox.PqboxError`, a `ValueError`). They
@@ -290,7 +307,10 @@ is refused. The body layouts are documented in [`format.rs`](crates/pqbox-core/s
290
307
 
291
308
  - **The post-quantum crates are not independently audited.** `ml-kem`, `ml-dsa`, `x-wing` and
292
309
  `slh-dsa` (a release candidate) come from RustCrypto, which has not had them audited, and
293
- `classic-mceliece-rust` is a community crate. pqbox itself has not been audited either.
310
+ `classic-mceliece-rust` is a community crate. `libcrux-ml-kem` (ML-KEM-1024) is partly formally
311
+ verified (panic freedom and functional correctness of most of its code, not constant time; see
312
+ [docs/BACKENDS.md](docs/BACKENDS.md)) but has no public audit either. pqbox itself has not been
313
+ audited.
294
314
  - **Why bother now: harvest now, decrypt later.** No practical break of RSA is publicly known, and
295
315
  no cryptographically relevant quantum computer is known to exist. Data recorded today, however,
296
316
  can be decrypted whenever one appears. Hybrids protect it against that while keeping a classical
@@ -302,6 +322,16 @@ is refused. The body layouts are documented in [`format.rs`](crates/pqbox-core/s
302
322
  more expensive hashes from a trusted source, raise the limits explicitly:
303
323
  `verify_password_with_limits` in Rust, `max_m_cost`/`max_t_cost`/`max_p_cost` in Python,
304
324
  `{ maxMCost, maxTCost, maxPCost }` in JavaScript.
325
+ - **Keys derived from passwords.** `password::derive_key` (Python `password_derive_key`,
326
+ JavaScript `passwordDeriveKey`) is plain Argon2id (version 0x13, no secret, no associated
327
+ data), so it matches other Argon2 libraries given the same inputs. It needs a salt of at least
328
+ 16 bytes (`generate_salt` makes one; store it, and the costs, next to the data) and derives 16
329
+ to 64 bytes, 32 by default. The costs default to 64 MiB, 3 passes and 1 lane, and are held to
330
+ the same limits as verification, with the same opt-in to raise them. Choose the highest memory
331
+ cost your slowest target can afford (RFC 9106 suggests 64 MiB with 3 passes when 2 GiB is too
332
+ much), and never lower the costs of existing data. There is no context input: keep keys for
333
+ different purposes apart with distinct salts, or derive one key and expand it with HKDF-SHA-256
334
+ using one `info` label per purpose.
305
335
  - **AES-256-GCM nonce limit.** `encrypt` draws a random nonce per message. AES-256-GCM's nonce is
306
336
  96 bits, so encrypt at most about 2^32 messages under one key (NIST SP 800-38D) and rotate it
307
337
  after that. XChaCha20-Poly1305, the default, has a 192-bit nonce and no practical limit: prefer
@@ -317,13 +347,15 @@ pqbox stands on these projects. The versions are pinned in the `Cargo.toml` file
317
347
  `=`, for every cryptographic crate); Dependabot proposes updates, and a weekly
318
348
  [upstream watch](.github/workflows/upstream-watch.yml) reports advisories, new releases and
319
349
  alternatives ([`scripts/watch/`](scripts/watch)). [`docs/BACKENDS.md`](docs/BACKENDS.md) compares
320
- the RustCrypto backends with libcrux and AWS-LC.
350
+ the backends with the alternatives (RustCrypto, libcrux and AWS-LC), and explains the move of
351
+ ML-KEM-1024 to libcrux.
321
352
 
322
353
  ### Cryptography (crate `pqbox`)
323
354
 
324
355
  | Crate | Purpose | Repository | crates.io | Implements |
325
356
  |---|---|---|---|---|
326
- | `ml-kem` | ML-KEM-768 / ML-KEM-1024 | [RustCrypto/KEMs](https://github.com/RustCrypto/KEMs/tree/master/ml-kem) | [ml-kem](https://crates.io/crates/ml-kem) | [FIPS 203](https://csrc.nist.gov/pubs/fips/203/final) |
357
+ | `libcrux-ml-kem` | ML-KEM-1024 (`pqbox-max`, `pqbox-paranoid`), portable build; formally verified with [hax](https://github.com/cryspen/hax) and F* ([verification status](https://github.com/cryspen/libcrux/blob/libcrux-ml-kem-v0.0.12/libcrux-ml-kem/proofs/verification_status.md)) | [cryspen/libcrux](https://github.com/cryspen/libcrux/tree/main/libcrux-ml-kem) | [libcrux-ml-kem](https://crates.io/crates/libcrux-ml-kem) | [FIPS 203](https://csrc.nist.gov/pubs/fips/203/final) |
358
+ | `ml-kem` | ML-KEM-768 (inside X-Wing) | [RustCrypto/KEMs](https://github.com/RustCrypto/KEMs/tree/master/ml-kem) | [ml-kem](https://crates.io/crates/ml-kem) | [FIPS 203](https://csrc.nist.gov/pubs/fips/203/final) |
327
359
  | `x-wing` | X-Wing KEM (`xwing` suite) | [RustCrypto/KEMs](https://github.com/RustCrypto/KEMs/tree/master/x-wing) | [x-wing](https://crates.io/crates/x-wing) | [draft-connolly-cfrg-xwing-kem](https://datatracker.ietf.org/doc/draft-connolly-cfrg-xwing-kem/) |
328
360
  | `classic-mceliece-rust` | Classic McEliece 8192128 (`paranoid`) | [Colfenor/classic-mceliece-rust](https://github.com/Colfenor/classic-mceliece-rust) | [classic-mceliece-rust](https://crates.io/crates/classic-mceliece-rust) | [Classic McEliece NIST submission](https://classic.mceliece.org/nist.html) |
329
361
  | `ml-dsa` | ML-DSA-65 / ML-DSA-87 | [RustCrypto/signatures](https://github.com/RustCrypto/signatures/tree/master/ml-dsa) | [ml-dsa](https://crates.io/crates/ml-dsa) | [FIPS 204](https://csrc.nist.gov/pubs/fips/204/final) |
@@ -88,6 +88,21 @@ export function kemKeygen(suite_name: string): object;
88
88
  */
89
89
  export function open(secret_key: Uint8Array, sealed: Uint8Array, allow_suites: string[]): Uint8Array;
90
90
 
91
+ /**
92
+ * Derives a key from a password and a stored salt with Argon2id (version 0x13, no secret, no
93
+ * associated data).
94
+ *
95
+ * # Errors
96
+ * `InvalidParams` for a salt shorter than 16 bytes, a length outside `16..=64`, or costs that
97
+ * are out of range or above the given limits (checked before any memory is allocated).
98
+ */
99
+ export function passwordDeriveKey(password: Uint8Array, salt: Uint8Array, length: number, m_cost: number, t_cost: number, p_cost: number, max_m_cost: number, max_t_cost: number, max_p_cost: number): Uint8Array;
100
+
101
+ /**
102
+ * A fresh random 16-byte salt for [`password_derive_key`].
103
+ */
104
+ export function passwordGenerateSalt(): Uint8Array;
105
+
91
106
  /**
92
107
  * Argon2id password hash as a PHC string.
93
108
  *
@@ -174,6 +189,8 @@ export interface InitOutput {
174
189
  readonly kemEncapsulate: (a: number, b: number, c: number, d: number, e: number) => void;
175
190
  readonly kemKeygen: (a: number, b: number, c: number) => void;
176
191
  readonly open: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
192
+ readonly passwordDeriveKey: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number) => void;
193
+ readonly passwordGenerateSalt: (a: number) => void;
177
194
  readonly passwordHash: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
178
195
  readonly passwordVerify: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
179
196
  readonly seal: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
@@ -334,6 +334,62 @@ export function open(secret_key, sealed, allow_suites) {
334
334
  }
335
335
  }
336
336
 
337
+ /**
338
+ * Derives a key from a password and a stored salt with Argon2id (version 0x13, no secret, no
339
+ * associated data).
340
+ *
341
+ * # Errors
342
+ * `InvalidParams` for a salt shorter than 16 bytes, a length outside `16..=64`, or costs that
343
+ * are out of range or above the given limits (checked before any memory is allocated).
344
+ * @param {Uint8Array} password
345
+ * @param {Uint8Array} salt
346
+ * @param {number} length
347
+ * @param {number} m_cost
348
+ * @param {number} t_cost
349
+ * @param {number} p_cost
350
+ * @param {number} max_m_cost
351
+ * @param {number} max_t_cost
352
+ * @param {number} max_p_cost
353
+ * @returns {Uint8Array}
354
+ */
355
+ export function passwordDeriveKey(password, salt, length, m_cost, t_cost, p_cost, max_m_cost, max_t_cost, max_p_cost) {
356
+ try {
357
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
358
+ const ptr0 = passArray8ToWasm0(password, wasm.__wbindgen_export);
359
+ const len0 = WASM_VECTOR_LEN;
360
+ const ptr1 = passArray8ToWasm0(salt, wasm.__wbindgen_export);
361
+ const len1 = WASM_VECTOR_LEN;
362
+ wasm.passwordDeriveKey(retptr, ptr0, len0, ptr1, len1, length, m_cost, t_cost, p_cost, max_m_cost, max_t_cost, max_p_cost);
363
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
364
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
365
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
366
+ if (r2) {
367
+ throw takeObject(r1);
368
+ }
369
+ return takeObject(r0);
370
+ } finally {
371
+ wasm.__wbindgen_add_to_stack_pointer(16);
372
+ }
373
+ }
374
+
375
+ /**
376
+ * A fresh random 16-byte salt for [`password_derive_key`].
377
+ * @returns {Uint8Array}
378
+ */
379
+ export function passwordGenerateSalt() {
380
+ try {
381
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
382
+ wasm.passwordGenerateSalt(retptr);
383
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
384
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
385
+ var v1 = getArrayU8FromWasm0(r0, r1).slice();
386
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
387
+ return v1;
388
+ } finally {
389
+ wasm.__wbindgen_add_to_stack_pointer(16);
390
+ }
391
+ }
392
+
337
393
  /**
338
394
  * Argon2id password hash as a PHC string.
339
395
  *