Key Derivation Function API in Java 25
A key derivation function (KDF) turns secret input, such as a shared secret from a key exchange, into one or more strong keys. Java 25 (JEP 510) added the standard javax.crypto.KDF API with an HKDF implementation, after a preview in Java 24.
This tutorial shows how to configure HKDF, how to verify it against the official test vector, and how to derive an AES key after a key agreement.
1. Why Was a KDF API Needed?
Protocols such as TLS 1.3 and Hybrid Public Key Encryption, and quantum-resistant schemes, derive keys with KDFs. Before Java 25 there was no standard API for this, so developers wrote HKDF on top of Mac or used third-party libraries. A shared API lets security providers supply implementations and keeps application code independent of them.
2. Syntax
KDF hkdf = KDF.getInstance("HKDF-SHA256");
AlgorithmParameterSpec params = HKDFParameterSpec.ofExtract()
.addIKM(inputKeyMaterial) // secret input
.addSalt(salt) // optional, recommended
.thenExpand(info, 32); // context info and output length
byte[] bytes = hkdf.deriveData(params); // raw bytes
SecretKey key = hkdf.deriveKey("AES", params); // ready-to-use key
Available algorithms include HKDF-SHA256, HKDF-SHA384 and HKDF-SHA512.
3. Practical Examples
Example 1: Matching the RFC 5869 test vector
Using the official inputs from RFC 5869 test case 1 proves the implementation produces exactly the expected output keying material.
import java.util.HexFormat;
import javax.crypto.KDF;
import javax.crypto.spec.HKDFParameterSpec;
public class HkdfTestVector {
public static void main(String[] args) throws Exception {
HexFormat hex = HexFormat.of();
// Input values from RFC 5869, Appendix A.1 (test case 1)
byte[] ikm = hex.parseHex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
byte[] salt = hex.parseHex("000102030405060708090a0b0c");
byte[] info = hex.parseHex("f0f1f2f3f4f5f6f7f8f9");
KDF hkdf = KDF.getInstance("HKDF-SHA256");
HKDFParameterSpec params = HKDFParameterSpec.ofExtract()
.addIKM(ikm)
.addSalt(salt)
.thenExpand(info, 42); // 42 bytes of output keying material
byte[] okm = hkdf.deriveData(params);
String expected = "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865";
System.out.println("Algorithm : " + hkdf.getAlgorithm());
System.out.println("OKM : " + hex.formatHex(okm));
System.out.println("Matches RFC 5869: " + hex.formatHex(okm).equals(expected));
}
}
Algorithm : HKDF-SHA256 OKM : 3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865 Matches RFC 5869: true
Example 2: Key agreement followed by HKDF
Alice and Bob agree on a shared secret with X25519, each derive the same AES-256 key with HKDF, and Bob decrypts Alice's message.
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import javax.crypto.Cipher;
import javax.crypto.KDF;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.HKDFParameterSpec;
public class DeriveAesKey {
public static void main(String[] args) throws Exception {
// 1. Two parties agree on a shared secret with X25519
KeyPair alice = KeyPairGenerator.getInstance("X25519").generateKeyPair();
KeyPair bob = KeyPairGenerator.getInstance("X25519").generateKeyPair();
byte[] sharedA = agree(alice, bob);
byte[] sharedB = agree(bob, alice);
// 2. Each side derives the same AES key from the shared secret with HKDF
SecretKey keyA = derive(sharedA);
SecretKey keyB = derive(sharedB);
System.out.println("Derived key: " + keyA.getAlgorithm() + ", " + keyA.getEncoded().length * 8 + " bits");
// 3. Alice encrypts, Bob decrypts with his own derived key
byte[] iv = new byte[12]; // fixed IV only for this demo; never reuse in real code
Cipher enc = Cipher.getInstance("AES/GCM/NoPadding");
enc.init(Cipher.ENCRYPT_MODE, keyA, new GCMParameterSpec(128, iv));
byte[] ciphertext = enc.doFinal("Transfer 5000 to account 42".getBytes(StandardCharsets.UTF_8));
Cipher dec = Cipher.getInstance("AES/GCM/NoPadding");
dec.init(Cipher.DECRYPT_MODE, keyB, new GCMParameterSpec(128, iv));
System.out.println("Bob reads : " + new String(dec.doFinal(ciphertext), StandardCharsets.UTF_8));
}
static byte[] agree(KeyPair own, KeyPair other) throws Exception {
KeyAgreement ka = KeyAgreement.getInstance("X25519");
ka.init(own.getPrivate());
ka.doPhase(other.getPublic(), true);
return ka.generateSecret();
}
static SecretKey derive(byte[] shared) throws Exception {
KDF hkdf = KDF.getInstance("HKDF-SHA256");
return hkdf.deriveKey("AES", HKDFParameterSpec.ofExtract()
.addIKM(shared)
.addSalt("chat-app-v1".getBytes(StandardCharsets.UTF_8))
.thenExpand("message-key".getBytes(StandardCharsets.UTF_8), 32));
}
}
Derived key: AES, 256 bits Bob reads : Transfer 5000 to account 42
4. Extract and Expand
5. Important Points and Best Practices
- Never use a raw shared secret directly as an encryption key; derive keys with a KDF.
- Use a different
infovalue for each purpose, for example one key for encryption and one for authentication. - HKDF is not a password hashing function; use a slow algorithm such as PBKDF2 for passwords.
- The fixed IV in Example 2 is only for a deterministic demo; real AES-GCM code must use a unique IV for every message.
6. Summary
- Java 25 (JEP 510) added the standard
javax.crypto.KDFAPI. - HKDF is configured with
HKDFParameterSpec: extract, then expand. deriveDatareturns bytes;deriveKeyreturns aSecretKey.- KDFs turn shared secrets into purpose-specific keys.
Related Tutorials
- Base64 API (Java 8)
- Java 25 Features
- Stable Values (Preview)
- Enhanced Pseudo-Random Number Generators (Java 17)
7. References
Frequently Asked Questions (FAQ)
Q1. What is a key derivation function?
An algorithm that derives one or more cryptographic keys from secret input material, optionally with a salt and context information.
Q2. Which KDF algorithms does Java 25 provide?
The default provider supports HKDF with SHA-256, SHA-384 and SHA-512.
Q3. What is the difference between deriveData and deriveKey?
deriveData returns raw bytes; deriveKey returns a SecretKey for a named algorithm such as AES.
Q4. Can HKDF be used for passwords?
No. HKDF is fast and designed for high-entropy input. For passwords use a deliberately slow function such as PBKDF2.