Skip to content

Commit 2187175

Browse files
authored
kdf: Kdf trait documentation improvements (#2211)
Expands documentation and renames the second parameter (not counting `self`) to the KDF from `salt` => `non_secret`. When impl'ing this trait for `hkdf` it became clear that, using the existing structs, `info` was the only parameter that made sense (something @newpavlov hinted at), as `salt` is the only one that can be statefully configured and accessed via `self`. So this renames it to a much more nonspecific `non_secret` and notes in the documentation that such a parameter goes by many different names that all generally serve the same function: customizing the outputs in a non-correlated/indistinguishable manner. It suggests consulting the algorithm-specific documentation for what parameter it actually maps to, except for `Kdf`s marked `Pbkdf` where it says it always maps to the salt.
1 parent 16deb21 commit 2187175

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

kdf/src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,25 @@ use core::fmt;
1313
/// Key Derivation Function.
1414
///
1515
/// These functions deterministically produce uniformly random outputs suitable as key material.
16+
///
17+
/// It is recommended for types which implement this trait to also implement [`Default`] whenever
18+
/// possible.
1619
pub trait Kdf {
17-
/// Fills `out` with uniformly random data suitable as key material, derived from the input
18-
/// `secret` and `salt` values, which map to algorithm-specific inputs.
19-
fn derive_key(&self, secret: &[u8], salt: &[u8], out: &mut [u8]) -> Result<()>;
20+
/// Writes uniformly random data suitable as key material into the entire length of `out`,
21+
/// derived from the following inputs:
22+
///
23+
/// - `secret`: this is typically a high-entropy input with at least 128-bits of symmetric
24+
/// strength/randomness, but does not have to be uniformly random (e.g. can be the output of
25+
/// a Diffie-Hellman exchange). For KDFs marked [`Pbkdf`], this parameter is allowed to be a
26+
/// password with a lower entropy, but consumers of these traits MUST bound on [`Pbkdf`]
27+
/// whenever they are expecting the input to be a password.
28+
/// - `non_secret`: this value customizes/personalizes the output and can be used to generate
29+
/// multiple unrelated outputs from the same input. Its secrecy is irrelevant, and it can be
30+
/// published to the world if desired. It maps to an algorithm specific parameter which
31+
/// accomplishes this purpose, sometimes called "salt", "info", "context", or "customization".
32+
/// See algorithm-specific documentation for the specific input this maps to for the specific
33+
/// impls for a given algorithm. For KDFs marked [`Pbkdf`] this is always the salt.
34+
fn derive_key(&self, secret: &[u8], non_secret: &[u8], out: &mut [u8]) -> Result<()>;
2035
}
2136

2237
/// Password-Based Key Derivation Functions: KDFs where it's suitable for the input `secret` to be

0 commit comments

Comments
 (0)