Class HKDF
The HKDF is initialized ("extracted") from a (non-secret) salt and a secret key. From this, any number of secret keys can be derived ("expanded") deterministically.
When multiple keys are to be derived from the same initial keying/salting material,
each separate key should use a distinct "context" in the expand(int, byte[])
call. This ensures that there exists a domain separation between the keys.
Using the same context as another key on a HKDF initialized with the same salt+key
results in the exact same derived key material as that key.
This implementation only offers HMAC-SHA256-based key derivation.
- Author:
- vekterli
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionbyte[]expand(int wantedBytes) Derives a key with a given number of bytes.byte[]expand(int wantedBytes, byte[] context) Derives a key with a given number of bytes for a particular context.static HKDFextractedFrom(byte[] salt, byte[] ikm) Creates and returns a new HKDF instance extracted from the given salt and key.static HKDFofPseudoRandomKey(byte[] prk) byte[]static HKDFunsaltedExtractedFrom(byte[] ikm) Creates and returns a new unsalted HKDF instance extracted from the given key.
-
Field Details
-
MAX_OUTPUT_SIZE
public static final int MAX_OUTPUT_SIZE- See Also:
-
-
Method Details
-
pseudoRandomKey
public byte[] pseudoRandomKey()- Returns:
- the computed pseudo-random key (PRK) used as input for each
expand()call.
-
ofPseudoRandomKey
- Returns:
- a new HKDF instance initially keyed with the given PRK
-
extractedFrom
Creates and returns a new HKDF instance extracted from the given salt and key.Both the salt and input key value may be of arbitrary size, but it is recommended to have both be at least 16 bytes in size.
- Parameters:
salt- a non-secret salt value. Should ideally be high entropy and functionally "as if random". May not be empty, useunsaltedExtractedFrom(byte[])if unsalted extraction is desired (though this is not recommended).ikm- secret initial Input Keying Material value.- Returns:
- a new HKDF instance ready for deriving keys based on the salt and IKM.
-
unsaltedExtractedFrom
Creates and returns a new unsalted HKDF instance extracted from the given key.Prefer using the salted
extractedFrom(byte[], byte[])method if possible.- Parameters:
ikm- secret initial Input Keying Material value.- Returns:
- a new HKDF instance ready for deriving keys based on the IKM and an all-zero salt.
-
expand
public byte[] expand(int wantedBytes, byte[] context) Derives a key with a given number of bytes for a particular context. The returned key is always deterministic for a given unique context and a HKDF initialized with a specific salt+IKM pair.Thread safety: multiple threads can safely call
expand()simultaneously on the same HKDF object.- Parameters:
wantedBytes- Positive number of output bytes. Must be less than or equal toMAX_OUTPUT_SIZEcontext- Context for key derivation. Derivation is deterministic for a given context. Note: this maps to the "info" field in RFC-5869.- Returns:
- A byte buffer of size wantedBytes filled with derived key material
-
expand
public byte[] expand(int wantedBytes) Derives a key with a given number of bytes. The returned key is always deterministic for a HKDF initialized with a specific salt+IKM pair.If more than one key is to be derived, use
expand(int, byte[])Thread safety: multiple threads can safely call
expand()simultaneously on the same HKDF object.- Parameters:
wantedBytes- Positive number of output bytes. Must be less than or equal toMAX_OUTPUT_SIZE- Returns:
- A byte buffer of size wantedBytes filled with derived key material
-