Top Description Inners Fields Constructors Methods
at.favre.lib.crypto

public final Class HKDF

extends Object
Class Inheritance
Annotations
@SuppressWarnings:WeakerAccess
Imports
javax.crypto.Mac, .SecretKey, java.nio.ByteBuffer

A standards-compliant implementation of RFC 5869 for HMAC-based Key Derivation Function.

HKDF follows the "extract-then-expand" paradigm, where the KDF logically consists of two modules. The first stage takes the input keying material and "extracts" from it a fixed-length pseudorandom key K. The second stage "expands" the key K into several additional pseudorandom keys (the output of the KDF).

HKDF was first described by Hugo Krawczyk.

This implementation is thread safe without the need for synchronization.

Simple Example:

    byte[] pseudoRandomKey = HKDF.fromHmacSha256().extract(null, lowEntropyInput);
    byte[] outputKeyingMaterial = HKDF.fromHmacSha256().expand(pseudoRandomKey, null, 64);
See Also
RFC 5869, Cryptographic Extraction and Key Derivation: The HKDF Scheme, Wikipedia: HKDF

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
private static HKDF
hkdfHmacSha256

Cache instances

private static HKDF
private final HkdfMacFactory

Constructor Summary

AccessConstructor and Description
private
HKDF(HkdfMacFactory macFactory)

Method Summary

Modifier and TypeMethod and Description
public byte[]

Returns:

new byte array of output keying material (OKM)
expand
(byte[]
a pseudo random key of at least hmac hash length in bytes (usually, the output from the extract step)
pseudoRandomKey
,
byte[]
optional context and application specific information; may be null
info
,
int
length of output keying material in bytes
outLengthBytes
)

Step 2 of RFC 5869 (Section 2.3)

To "expand" the generated output of an already reasonably random input such as an existing shared key into a larger cryptographically independent output, thereby producing multiple keys deterministically from that initial shared key, so that the same process may produce those same secret keys safely on multiple devices, as long as the same inputs are used.

public byte[]

Returns:

new byte array of output keying material (OKM)
expand
(SecretKey
a pseudo random key of at least hmac hash length in bytes (usually, the output from the extract step)
pseudoRandomKey
,
byte[]
optional context and application specific information; may be null
info
,
int
length of output keying material in bytes
outLengthBytes
)

Use this if you require SecretKey types by your security framework.

public byte[]

Returns:

a new byte array pseudo random key (of hash length in bytes) (PRK) which can be used to expand
extract
(byte[]
optional salt value (a non-secret random value) (can be null) if not provided, it is set to an array of hash length of zeros.
salt
,
byte[]
data to be extracted (IKM)
inputKeyingMaterial
)

Step 1 of RFC 5869 (Section 2.2)

The first stage takes the input keying material and "extracts" from it a fixed-length pseudorandom key K.

public byte[]

Returns:

a new byte array pseudo random key (of hash length in bytes) (PRK) which can be used to expand
extract
(SecretKey
optional salt value (a non-secret random value) (can be null)
salt
,
byte[]
data to be extracted (IKM)
inputKeyingMaterial
)

Use this if you require SecretKey types by your security framework.

public byte[]

Returns:

new byte array of output keying material (OKM)
extractAndExpand
(byte[]
optional salt value (a non-secret random value);
saltExtract
,
byte[]
data to be extracted (IKM)
inputKeyingMaterial
,
byte[]
optional context and application specific information; may be null
infoExpand
,
int
length of output keying material in bytes
outLengthByte
)

Convenience method for extract & expand in a single method

public byte[]

Returns:

new byte array of output keying material (OKM)
extractAndExpand
(SecretKey
optional salt value (a non-secret random value);
saltExtract
,
byte[]
data to be extracted (IKM)
inputKeyingMaterial
,
byte[]
optional context and application specific information; may be null
infoExpand
,
int
length of output keying material in bytes
outLengthByte
)

Convenience method for extract & expand in a single method

public static HKDF

Returns:

a new instance of HKDF
from
(HkdfMacFactory
used for HKDF
macFactory
)

Create a new HKDF instance for given macFactory.

public static HKDF

Returns:

HKDF instance
fromHmacSha256
()

Return a shared instance using HMAC with Sha256.

public static HKDF

Returns:

HKDF instance
fromHmacSha512
()

Return a shared instance using HMAC with Sha512.

pack-priv HkdfMacFactory

Returns:

factory
getMacFactory
()

Get the used mac factory

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

hkdfHmacSha256back to summary
private static HKDF hkdfHmacSha256

Cache instances

hkdfHmacSha512back to summary
private static HKDF hkdfHmacSha512
macFactoryback to summary
private final HkdfMacFactory macFactory

Constructor Detail

HKDFback to summary
private HKDF(HkdfMacFactory macFactory)

Method Detail

expandback to summary
public byte[] expand(byte[] pseudoRandomKey, byte[] info, int outLengthBytes)

Step 2 of RFC 5869 (Section 2.3)

To "expand" the generated output of an already reasonably random input such as an existing shared key into a larger cryptographically independent output, thereby producing multiple keys deterministically from that initial shared key, so that the same process may produce those same secret keys safely on multiple devices, as long as the same inputs are used.

About Info (from RFC 5869):

While the 'info' value is optional in the definition of HKDF, it is often of great importance in applications. Its main objective is to bind the derived key material to application- and context-specific information. For example, 'info' may contain a protocol number, algorithm identifiers, user identities, etc. In particular, it may prevent the derivation of the same keying material for different contexts (when the same input key material (IKM) is used in such different contexts).
Parameters
pseudoRandomKey:byte[]

a pseudo random key of at least hmac hash length in bytes (usually, the output from the extract step)

info:byte[]

optional context and application specific information; may be null

outLengthBytes:int

length of output keying material in bytes

Returns:byte[]

new byte array of output keying material (OKM)

See Also
RFC 5869 Section 2.3
expandback to summary
public byte[] expand(SecretKey pseudoRandomKey, byte[] info, int outLengthBytes)

Use this if you require SecretKey types by your security framework. See also HkdfMacFactory#createSecretKey(byte[]).

See expand(byte[], byte[], int) for description.

Parameters
pseudoRandomKey:SecretKey

a pseudo random key of at least hmac hash length in bytes (usually, the output from the extract step)

info:byte[]

optional context and application specific information; may be null

outLengthBytes:int

length of output keying material in bytes

Returns:byte[]

new byte array of output keying material (OKM)

extractback to summary
public byte[] extract(byte[] salt, byte[] inputKeyingMaterial)

Step 1 of RFC 5869 (Section 2.2)

The first stage takes the input keying material and "extracts" from it a fixed-length pseudorandom key K. The goal of the "extract" stage is to "concentrate" and provide a more uniformly unbiased and higher entropy but smaller output. This is done by utilising the diffusion properties of cryptographic MACs.

About Salts (from RFC 5869):

HKDF is defined to operate with and without random salt. This is done to accommodate applications where a salt value is not available. We stress, however, that the use of salt adds significantly to the strength of HKDF, ensuring independence between different uses of the hash function, supporting "source-independent" extraction, and strengthening the analytical results that back the HKDF design.
Parameters
salt:byte[]

optional salt value (a non-secret random value) (can be null) if not provided, it is set to an array of hash length of zeros.

inputKeyingMaterial:byte[]

data to be extracted (IKM)

Returns:byte[]

a new byte array pseudo random key (of hash length in bytes) (PRK) which can be used to expand

See Also
RFC 5869 Section 2.2
extractback to summary
public byte[] extract(SecretKey salt, byte[] inputKeyingMaterial)

Use this if you require SecretKey types by your security framework. See also HkdfMacFactory#createSecretKey(byte[]).

See extract(byte[], byte[]) for description.

Parameters
salt:SecretKey

optional salt value (a non-secret random value) (can be null)

inputKeyingMaterial:byte[]

data to be extracted (IKM)

Returns:byte[]

a new byte array pseudo random key (of hash length in bytes) (PRK) which can be used to expand

extractAndExpandback to summary
public byte[] extractAndExpand(byte[] saltExtract, byte[] inputKeyingMaterial, byte[] infoExpand, int outLengthByte)

Convenience method for extract & expand in a single method

Parameters
saltExtract:byte[]

optional salt value (a non-secret random value);

inputKeyingMaterial:byte[]

data to be extracted (IKM)

infoExpand:byte[]

optional context and application specific information; may be null

outLengthByte:int

length of output keying material in bytes

Returns:byte[]

new byte array of output keying material (OKM)

extractAndExpandback to summary
public byte[] extractAndExpand(SecretKey saltExtract, byte[] inputKeyingMaterial, byte[] infoExpand, int outLengthByte)

Convenience method for extract & expand in a single method

Parameters
saltExtract:SecretKey

optional salt value (a non-secret random value);

inputKeyingMaterial:byte[]

data to be extracted (IKM)

infoExpand:byte[]

optional context and application specific information; may be null

outLengthByte:int

length of output keying material in bytes

Returns:byte[]

new byte array of output keying material (OKM)

fromback to summary
public static HKDF from(HkdfMacFactory macFactory)

Create a new HKDF instance for given macFactory.

Parameters
macFactory:HkdfMacFactory

used for HKDF

Returns:HKDF

a new instance of HKDF

fromHmacSha256back to summary
public static HKDF fromHmacSha256()

Return a shared instance using HMAC with Sha256. Even though shared, this instance is thread-safe.

Returns:HKDF

HKDF instance

fromHmacSha512back to summary
public static HKDF fromHmacSha512()

Return a shared instance using HMAC with Sha512. Even though shared, this instance is thread-safe.

Returns:HKDF

HKDF instance

getMacFactoryback to summary
pack-priv HkdfMacFactory getMacFactory()

Get the used mac factory

Returns:HkdfMacFactory

factory

at.favre.lib.crypto back to summary

pack-priv final Class HKDF.Expander

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final HkdfMacFactory

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv byte[]

Returns:

new byte array of output keying material (OKM)
execute
(SecretKey
a pseudorandom key of at least hmac hash length in bytes (usually, the output from the extract step)
pseudoRandomKey
,
byte[]
optional context and application specific information; may be null
info
,
int
length of output keying material in bytes (must be <= 255 * mac hash length)
outLengthBytes
)

Step 2 of RFC 5869.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

macFactoryback to summary
private final HkdfMacFactory macFactory

Constructor Detail

Expanderback to summary
pack-priv Expander(HkdfMacFactory macFactory)

Method Detail

executeback to summary
pack-priv byte[] execute(SecretKey pseudoRandomKey, byte[] info, int outLengthBytes)

Step 2 of RFC 5869.

Parameters
pseudoRandomKey:SecretKey

a pseudorandom key of at least hmac hash length in bytes (usually, the output from the extract step)

info:byte[]

optional context and application specific information; may be null

outLengthBytes:int

length of output keying material in bytes (must be <= 255 * mac hash length)

Returns:byte[]

new byte array of output keying material (OKM)

at.favre.lib.crypto back to summary

pack-priv final Class HKDF.Extractor

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final HkdfMacFactory

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv byte[]

Returns:

a new byte array pseudorandom key (of hash length in bytes) (PRK) which can be used to expand
execute
(SecretKey
optional salt value (a non-secret random value); if not provided, it is set to an array of hash length of zeros.
salt
,
byte[]
data to be extracted (IKM)
inputKeyingMaterial
)

Step 1 of RFC 5869

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

macFactoryback to summary
private final HkdfMacFactory macFactory

Constructor Detail

Extractorback to summary
pack-priv Extractor(HkdfMacFactory macFactory)

Method Detail

executeback to summary
pack-priv byte[] execute(SecretKey salt, byte[] inputKeyingMaterial)

Step 1 of RFC 5869

Parameters
salt:SecretKey

optional salt value (a non-secret random value); if not provided, it is set to an array of hash length of zeros.

inputKeyingMaterial:byte[]

data to be extracted (IKM)

Returns:byte[]

a new byte array pseudorandom key (of hash length in bytes) (PRK) which can be used to expand