Class Hkdf
HMAC-based key derivation (HKDF), per RFC 5869.
HKDF turns input keying material that is not uniformly random -- the
shared secret of a KeyAgreement, say -- into one or more keys of any
length. It runs in two steps: extract concentrates the input into a
pseudorandom key, and expand stretches that into as many bytes as are
asked for, bound to an info label so that keys derived for different
purposes never coincide.
KeyPair mine = KeyGenerator.ec(256);
KeyPair theirs = KeyGenerator.ec(256);
byte[] shared = KeyAgreement.ecdh(mine.getPrivateKey(), theirs.getPublicKey());
byte[] keys = Hkdf.derive("SHA-256", null, shared, "chat keys".getBytes(), 64);
It is pure Java over Hmac, so it behaves identically on every port and on
the server.
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]Extract then expand in one call.static byte[]The expand step: deriveslengthbytes from a pseudorandom key.static byte[]The extract step:HMAC(salt, ikm).
-
Method Details
-
extract
The extract step:
HMAC(salt, ikm). A null or emptysaltis replaced by a string of zero bytes the length of the hash, as the RFC specifies.Parameters
-
algorithm: the hash, any name [Hmac#create(String, byte[])] accepts -
salt: optional salt -
ikm: the input keying material
-
-
expand
The expand step: derives
lengthbytes from a pseudorandom key.Parameters
-
algorithm: the hash -
prk: the pseudorandom key, usually the result ofextract(String, byte[], byte[]) -
info: context label; may be null -
length: bytes wanted, at most 255 times the hash length
-
-
derive
Extract then expand in one call.
Parameters
-
algorithm: the hash, for example"SHA-256" -
salt: optional salt -
ikm: the input keying material -
info: context label; may be null -
length: bytes wanted
-
-