APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.crypto

public final Class KDF

extends Object
Class Inheritance
Imports
sun.security.jca.GetInstance, .GetInstance.Instance, sun.security.util.Debug, java.security.InvalidAlgorithmParameterException, .NoSuchAlgorithmException, .NoSuchProviderException, .Security, .Provider, .Provider.Service, java.security.spec.AlgorithmParameterSpec, java.util.Iterator, .Objects

This class provides the functionality of a Key Derivation Function (KDF), which is a cryptographic algorithm for deriving additional keys from input keying material (IKM) and (optionally) other data.

KDF objects are instantiated with the getInstance family of methods.

The class has two derive methods, deriveKey and deriveData. The deriveKey method accepts an algorithm name and returns a SecretKey object with the specified algorithm. The deriveData method returns a byte array of raw data.

API Usage Example:

KDF kdfHkdf = KDF.getInstance("HKDF-SHA256"); AlgorithmParameterSpec derivationSpec = HKDFParameterSpec.ofExtract() .addIKM(ikm) .addSalt(salt).thenExpand(info, 32); SecretKey sKey = kdfHkdf.deriveKey("AES", derivationSpec);
KDF kdfHkdf = KDF.getInstance("HKDF-SHA256");

AlgorithmParameterSpec derivationSpec =
         HKDFParameterSpec.ofExtract()
                          .addIKM(ikm)
                          .addSalt(salt).thenExpand(info, 32);

SecretKey sKey = kdfHkdf.deriveKey("AES", derivationSpec);

Concurrent Access

Unless otherwise documented by an implementation, the methods defined in this class are not thread-safe. Multiple threads that need to access a single object concurrently should synchronize amongst themselves and provide the necessary locking. Multiple threads each manipulating separate objects need not synchronize.

Delayed Provider Selection

If a provider is not specified when calling one of the getInstance methods, the implementation delays the selection of the provider until the deriveKey or deriveData method is called. This is called delayed provider selection. The primary reason this is done is to ensure that the selected provider can handle the key material that is passed to those methods - for example, the key material may reside on a hardware device that only a specific KDF provider can utilize. The getInstance method returns a KDF object as long as there exists at least one registered security provider that implements the algorithm and supports the optional parameters. The delayed provider selection process traverses the list of registered security providers, starting with the most preferred Provider. The first provider that supports the specified algorithm, optional parameters, and key material is selected.

If the getProviderName or getParameters method is called before the deriveKey or deriveData methods, the first provider supporting the KDF algorithm and optional KDFParameters is chosen. This provider may not support the key material that is subsequently passed to the deriveKey or deriveData methods. Therefore, it is recommended not to call the getProviderName or getParameters methods until after a key derivation operation. Once a provider is selected, it cannot be changed.

Since
25
See Also
KDFParameters, SecretKey

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static record

Field Summary

Modifier and TypeField and Description
private final String
private final KDF.Delegate
private final KDFParameters
private final Object
private static final Debug
private final Iterator<Provider.Service>
private static final boolean
private KDF.Delegate

Constructor Summary

AccessConstructor and Description
private
KDF(KDF.Delegate delegate, String algorithm)

private
KDF(KDF.Delegate firstPairOfSpiAndProv, Iterator<Provider.Service> t, String algorithm, KDFParameters kdfParameters)

Method Summary

Modifier and TypeMethod and Description
private static boolean
private Object
chooseProvider(String algorithm, AlgorithmParameterSpec derivationSpec)

Selects the provider which supports the passed algorithm and derivationSpec values, and assigns the global spi and provider variables if they have not been assigned yet.

public byte[]

Returns:

the derived key in its raw bytes
deriveData
(AlgorithmParameterSpec
the object describing the inputs to the derivation function
derivationSpec
)

Derives a key, returns raw data as a byte array.

public SecretKey

Returns:

the derived key
deriveKey
(String
the algorithm of the resultant SecretKey object. See the SecretKey Algorithms section in the Java Security Standard Algorithm Names Specification for information about standard secret key algorithm names.
alg
,
AlgorithmParameterSpec
the object describing the inputs to the derivation function
derivationSpec
)

Derives a key, returned as a SecretKey object.

public String

Returns:

the algorithm name of this KDF object
getAlgorithm
()

Returns the algorithm name of this KDF object.

public static KDF

Returns:

a KDF object
getInstance
(String
the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.
algorithm
)

Returns a KDF object that implements the specified algorithm.

public static KDF

Returns:

a KDF object
getInstance
(String
the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.
algorithm
,
String
the provider to use for this key derivation
provider
)

Returns a KDF object that implements the specified algorithm from the specified security provider.

public static KDF

Returns:

a KDF object
getInstance
(String
the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.
algorithm
,
Provider
the provider to use for this key derivation
provider
)

Returns a KDF object that implements the specified algorithm from the specified security provider.

public static KDF

Returns:

a KDF object
getInstance
(String
the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.
algorithm
,
KDFParameters
the KDFParameters used to configure the derivation algorithm or null if no parameters are provided
kdfParameters
)

Returns a KDF object that implements the specified algorithm and is initialized with the specified parameters.

public static KDF

Returns:

a KDF object
getInstance
(String
the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.
algorithm
,
KDFParameters
the KDFParameters used to configure the derivation algorithm or null if no parameters are provided
kdfParameters
,
String
the provider to use for this key derivation
provider
)

Returns a KDF object that implements the specified algorithm from the specified provider and is initialized with the specified parameters.

public static KDF

Returns:

a KDF object
getInstance
(String
the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.
algorithm
,
KDFParameters
the KDFParameters used to configure the derivation algorithm or null if no parameters are provided
kdfParameters
,
Provider
the provider to use for this key derivation
provider
)

Returns a KDF object that implements the specified algorithm from the specified provider and is initialized with the specified parameters.

private static KDF.Delegate
getNext(Iterator<Provider.Service> serviceIter, KDFParameters kdfParameters)

public KDFParameters

Returns:

the parameters used with this KDF object, or null
getParameters
()

Returns the KDFParameters used with this KDF object.

public String

Returns:

the name of the provider
getProviderName
()

Returns the name of the provider.

private static KDF
private void
private void
useFirstSpi()

Use the firstSpi as the chosen KDFSpi and set the fields accordingly

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

algorithmback to summary
private final String algorithm
candidateback to summary
private final KDF.Delegate candidate
kdfParametersback to summary
private final KDFParameters kdfParameters
lockback to summary
private final Object lock
pdebugback to summary
private static final Debug pdebug
serviceIteratorback to summary
private final Iterator<Provider.Service> serviceIterator
skipDebugback to summary
private static final boolean skipDebug
theOneback to summary
private KDF.Delegate theOne

Constructor Detail

KDFback to summary
private KDF(KDF.Delegate delegate, String algorithm)
KDFback to summary
private KDF(KDF.Delegate firstPairOfSpiAndProv, Iterator<Provider.Service> t, String algorithm, KDFParameters kdfParameters)

Method Detail

checkSpiNonNullback to summary
private static boolean checkSpiNonNull(KDF.Delegate d)
chooseProviderback to summary
private Object chooseProvider(String algorithm, AlgorithmParameterSpec derivationSpec) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException

Selects the provider which supports the passed algorithm and derivationSpec values, and assigns the global spi and provider variables if they have not been assigned yet.

If the spi has already been set, it will just return the result.

deriveDataback to summary
public byte[] deriveData(AlgorithmParameterSpec derivationSpec) throws InvalidAlgorithmParameterException

Derives a key, returns raw data as a byte array.

Parameters
derivationSpec:AlgorithmParameterSpec

the object describing the inputs to the derivation function

Returns:byte[]

the derived key in its raw bytes

Exceptions
InvalidAlgorithmParameterException:
if the information contained within the derivationSpec is invalid
UnsupportedOperationException:
if the derived keying material is not extractable
NullPointerException:
if derivationSpec is null
See Also
Delayed Provider Selection
deriveKeyback to summary
public SecretKey deriveKey(String alg, AlgorithmParameterSpec derivationSpec) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException

Derives a key, returned as a SecretKey object.

Parameters
alg:String

the algorithm of the resultant SecretKey object. See the SecretKey Algorithms section in the Java Security Standard Algorithm Names Specification for information about standard secret key algorithm names.

derivationSpec:AlgorithmParameterSpec

the object describing the inputs to the derivation function

Returns:SecretKey

the derived key

Exceptions
InvalidAlgorithmParameterException:
if the information contained within the derivationSpec is invalid or if the combination of alg and the derivationSpec results in something invalid
NoSuchAlgorithmException:
if alg is empty or invalid
NullPointerException:
if alg or derivationSpec is null
External Specification
Java Security Standard Algorithm Names
See Also
Delayed Provider Selection
getAlgorithmback to summary
public String getAlgorithm()

Returns the algorithm name of this KDF object.

Returns:String

the algorithm name of this KDF object

getInstanceback to summary
public static KDF getInstance(String algorithm) throws NoSuchAlgorithmException

Returns a KDF object that implements the specified algorithm.

Implementation Note

The JDK Reference Implementation additionally uses the jdk.security.provider.preferred Security property to determine the preferred provider order for the specified algorithm. This may be different than the order of providers returned by Security.getProviders().

Parameters
algorithm:String

the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.

Returns:KDF

a KDF object

Exceptions
NoSuchAlgorithmException:
if no Provider supports a KDF implementation for the specified algorithm
NullPointerException:
if algorithm is null
External Specification
Java Security Standard Algorithm Names
See Also
Delayed Provider Selection
getInstanceback to summary
public static KDF getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException

Returns a KDF object that implements the specified algorithm from the specified security provider. The specified provider must be registered in the security provider list.

Parameters
algorithm:String

the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.

provider:String

the provider to use for this key derivation

Returns:KDF

a KDF object

Exceptions
NoSuchAlgorithmException:
if the specified provider does not support the specified KDF algorithm
NoSuchProviderException:
if the specified provider is not registered in the security provider list
NullPointerException:
if algorithm or provider is null
External Specification
Java Security Standard Algorithm Names
getInstanceback to summary
public static KDF getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException

Returns a KDF object that implements the specified algorithm from the specified security provider.

Parameters
algorithm:String

the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.

provider:Provider

the provider to use for this key derivation

Returns:KDF

a KDF object

Exceptions
NoSuchAlgorithmException:
if the specified provider does not support the specified KDF algorithm
NullPointerException:
if algorithm or provider is null
External Specification
Java Security Standard Algorithm Names
getInstanceback to summary
public static KDF getInstance(String algorithm, KDFParameters kdfParameters) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException

Returns a KDF object that implements the specified algorithm and is initialized with the specified parameters.

Implementation Note

The JDK Reference Implementation additionally uses the jdk.security.provider.preferred Security property to determine the preferred provider order for the specified algorithm. This may be different than the order of providers returned by Security.getProviders().

Parameters
algorithm:String

the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.

kdfParameters:KDFParameters

the KDFParameters used to configure the derivation algorithm or null if no parameters are provided

Returns:KDF

a KDF object

Exceptions
NoSuchAlgorithmException:
if no Provider supports a KDF implementation for the specified algorithm
InvalidAlgorithmParameterException:
if at least one Provider supports a KDF implementation for the specified algorithm but none of them support the specified parameters
NullPointerException:
if algorithm is null
External Specification
Java Security Standard Algorithm Names
See Also
Delayed Provider Selection
getInstanceback to summary
public static KDF getInstance(String algorithm, KDFParameters kdfParameters, String provider) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException

Returns a KDF object that implements the specified algorithm from the specified provider and is initialized with the specified parameters. The specified provider must be registered in the security provider list.

Parameters
algorithm:String

the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.

kdfParameters:KDFParameters

the KDFParameters used to configure the derivation algorithm or null if no parameters are provided

provider:String

the provider to use for this key derivation

Returns:KDF

a KDF object

Exceptions
NoSuchAlgorithmException:
if the specified provider does not support the specified KDF algorithm
NoSuchProviderException:
if the specified provider is not registered in the security provider list
InvalidAlgorithmParameterException:
if the specified provider supports the specified KDF algorithm but does not support the specified parameters
NullPointerException:
if algorithm or provider is null
External Specification
Java Security Standard Algorithm Names
getInstanceback to summary
public static KDF getInstance(String algorithm, KDFParameters kdfParameters, Provider provider) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException

Returns a KDF object that implements the specified algorithm from the specified provider and is initialized with the specified parameters.

Parameters
algorithm:String

the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.

kdfParameters:KDFParameters

the KDFParameters used to configure the derivation algorithm or null if no parameters are provided

provider:Provider

the provider to use for this key derivation

Returns:KDF

a KDF object

Exceptions
NoSuchAlgorithmException:
if the specified provider does not support the specified KDF algorithm
InvalidAlgorithmParameterException:
if the specified provider supports the specified KDF algorithm but does not support the specified parameters
NullPointerException:
if algorithm or provider is null
External Specification
Java Security Standard Algorithm Names
getNextback to summary
private static KDF.Delegate getNext(Iterator<Provider.Service> serviceIter, KDFParameters kdfParameters) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException
getParametersback to summary
public KDFParameters getParameters()

Returns the KDFParameters used with this KDF object.

The returned parameters may be the same that were used to initialize this KDF object, or may contain additional default or random parameter values used by the underlying KDF algorithm. If the required parameters were not supplied and can be generated by the KDF object, the generated parameters are returned; otherwise null is returned.

Returns:KDFParameters

the parameters used with this KDF object, or null

See Also
Delayed Provider Selection
getProviderNameback to summary
public String getProviderName()

Returns the name of the provider.

Returns:String

the name of the provider

See Also
Delayed Provider Selection
handleExceptionback to summary
private static KDF handleException(NoSuchAlgorithmException e) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException
rethrowback to summary
private void rethrow(InvalidAlgorithmParameterException e) throws InvalidAlgorithmParameterException
useFirstSpiback to summary
private void useFirstSpi()

Use the firstSpi as the chosen KDFSpi and set the fields accordingly

javax.crypto back to summary

private final Record KDF.Delegate

extends Record
Class Inheritance
Record Components
spi:KDFSpi 
provider:Provider 

Field Summary

Modifier and TypeField and Description
private final Provider
provider

Record Component accessed by provider().

private final KDFSpi
spi

Record Component accessed by spi().

Constructor Summary

AccessConstructor and Description
private
Delegate(KDFSpi spi, Provider provider)

Method Summary

Modifier and TypeMethod and Description
public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public Provider
provider()

Record Component getter of provider.

public KDFSpi
spi()

Record Component getter of spi.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

providerback to summary
private final Provider provider

Record Component accessed by provider().

spiback to summary
private final KDFSpi spi

Record Component accessed by spi().

Constructor Detail

Delegateback to summary
private Delegate(KDFSpi spi, Provider provider)

Method Detail

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

providerback to summary
public Provider provider()

Record Component getter of provider.

spiback to summary
public KDFSpi spi()

Record Component getter of spi.

toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.