1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Password-based key management (`PBES2-HS256+A128KW`, `PBES2-HS384+A192KW`,
//! `PBES2-HS512+A256KW`, RFC 7518 §4.8) — the input type and the work budget.
//!
//! PBES2 is the one key-management family in RFC 7518 whose key material is not a
//! key: it is a *password*, an human-chosen secret with none of the properties a JWK
//! is assumed to have. That difference is why this module exists separately rather
//! than as another `&[u8]` accepted by the existing entry points.
//!
//! ## Why a distinct type instead of a string or an `oct` JWK
//!
//! The existing paths take `&[u8]` or [`crate::jwk::Jwk`], and both would *work*
//! mechanically: a password is bytes, and `JWK::from_password` is a thing other
//! libraries have shipped. Both would also be wrong, in the same way — they let a
//! caller hand a password to code whose job is to use a key, and the two are not
//! interchangeable in either direction:
//!
//! * a key is high-entropy and is used directly; **a password is low-entropy and is
//! only ever an input to a key derivation function**. Passing one where a key is
//! expected means someone, somewhere, uses `k` as a KEK without deriving anything,
//! which is exactly the mistake PBES2's parameter names exist to prevent;
//! * a JWK carries policy that a password does not have — `alg`, `use`, `key_ops`,
//! `kid`. A `Jwk` standing in for a password either leaves those empty (so the
//! pinning checks that protect every other path silently stop applying) or fills
//! them with something that pretends to be a key policy;
//! * and an `oct` JWK whose `k` holds a password would be *accepted* by the `A128KW`
//! and `dir` paths too, so the same value would be usable both as a password and as
//! a raw key — one secret with two completely different security arguments, which
//! is a confusion, not a convenience.
//!
//! So a password arrives as [`Password`], and only the PBES2 paths accept it. The
//! algorithms that want a key refuse it, and vice versa: the type decides which
//! family a secret can enter, rather than a runtime check hoping the caller meant
//! what the header says.
//!
//! ## Zeroization
//!
//! A password outlives its derived key by design: the caller keeps typing it, and
//! the library derives, wraps and forgets. [`Password`] therefore owns its bytes in
//! [`zeroize::Zeroizing`], and so does every derived key-encryption key this module produces.
//! The guarantee is deliberately modest — a password in a terminal or a config file
//! is already not secret at rest — but the derived KEK is the value an attacker
//! actually wants, and nothing here leaves it in a plain `Vec`.
//!
//! ## The work budget
//!
//! PBKDF2's iteration count is chosen by the **sender** and paid for by the
//! **recipient**: `p2c` says how much CPU the recipient must spend before it learns
//! whether the password was right. That is an odd asymmetry, and it needs a policy on
//! both ends, which is what the constants below are. [`crate::jwe::JweEncryptOptions`]
//! and [`crate::jwe::JweDecryptOptions`] expose the knobs.
//!
//! The numbers were chosen from measurements, not from precedent; see
//! `crate::jwe::internal::tests::pbes2_work_budget_benchmark`, which is the test that
//! produced them and can re-derive them on other hardware.
/// The lowest PBKDF2 iteration count this crate will emit or accept.
///
/// RFC 7518 §4.8.1.2 states "A minimum iteration count of 1000 is RECOMMENDED", so
/// refusing anything below it is the RFC's own floor rather than this crate's
/// opinion, and it costs nothing in interoperability: the peers this crate is tested
/// against emit 2048 (panva) and 8192 (jwcrypto).
///
/// The cryptographic floor is not the point. A count of 1 makes the salt the only
/// thing separating two messages under the same password, which turns PBES2 into
/// "AES-KW with a low-entropy key" — a different, much weaker construction that some
/// peers' history shows them producing when a caller overrides the default.
pub const PBES2_MIN_ITERATIONS: u32 = 1_000;
/// The iteration count used when the caller does not choose one.
///
/// 8 192, and the number is bounded from both sides in a way PBES2 is unusual about:
/// the **sender** chooses it and the **recipient** pays it, so a count is only
/// meaningful if the recipient is willing to do the work.
///
/// On the upper side, real verifiers cap what they will compute rather than let a
/// stranger bill them. panva refuses a `p2c` above 10 000 by default (`maxPBES2Count`,
/// which its source makes configurable), and jwcrypto's `default_max_pbkdf2_iterations`
/// is 16 384. A default of 100 000 — which is what a pure cost-based rule ("about a
/// tenth of a second of recipient work": 31 ms / 105 ms / 155 ms for the three
/// algorithms on the measured machine) would have produced — makes messages that
/// **both** peers refuse to read, which is worse than a slightly cheaper default: the
/// failure is not a refusal to interoperate on a corner case, it is every message this
/// crate emits being undecryptable. So the emit-side default lives inside the
/// intersection of what verifiers accept, 1 000 … 10 000, and does not pretend
/// otherwise.
///
/// Inside that window, 8 192 is four times panva's 2 048 default, equal to jwcrypto's
/// own, eight times the RFC's recommended floor, and costs the recipient at most about
/// 17 ms on the measured machine (2.9 ms / 7.9 ms / 16.6 ms for the three algorithms) —
/// cheap enough that nobody works around it, which is the real failure mode of
/// password-based crypto: an unlock that takes ten seconds gets replaced by a sticky
/// note.
///
/// A caller who controls both ends and wants more work per message can raise it with
/// [`crate::jwe::JweEncryptOptions::with_pbes2_iterations`], and should know from this
/// comment that they are spending someone's CPU.
pub const PBES2_DEFAULT_ITERATIONS: u32 = 8_192;
/// The highest PBKDF2 iteration count accepted by default when *reading*.
///
/// The sender picks this number and the recipient pays it, so an unbounded accepted
/// count is a one-message denial-of-service: a peer can name 100 000 000 iterations
/// (the backend's own ceiling) and make a receiver burn minutes of CPU per message
/// before the AES-KW integrity check says the password was wrong anyway.
///
/// The default is a per-message budget of about a second on the measured machine
/// (163 ms / 983 ms / 909 ms for the three algorithms) — enough headroom for a sender
/// who chose a count well above ours, tight enough that the worst case a peer can
/// impose is a pause rather than a stall.
///
/// It is deliberately **far above** [`PBES2_DEFAULT_ITERATIONS`], and the asymmetry is
/// the point: what we emit is limited by what other implementations are willing to
/// compute, while what we accept is limited only by our own patience. Refusing a
/// message because its sender was more careful than our default would be self-harm. Lower it with
/// [`crate::jwe::JweDecryptOptions::with_pbes2_max_iterations`] where latency matters;
/// raising it is a decision about how much CPU to let a stranger bill to this process,
/// which is why it takes an explicit call rather than a config default.
///
/// A caller that tries several passwords in a loop pays this per attempt. This crate
/// reads exactly one recipient element per PBES2 message, so a message cannot
/// multiply the cost; a keyring search can, and the caller bounds that loop.
pub const PBES2_DEFAULT_MAX_ITERATIONS: u32 = 1_000_000;
pub use cratePassword;