Class Hkdf

java.lang.Object
io.github.mahmoudimus.quic.Hkdf

public final class Hkdf extends Object
HKDF (RFC 5869) over an HMAC, and the HKDF-Expand-Label of TLS 1.3 (RFC 8446 section 7.1) that QUIC uses for its secrets and keys (RFC 9001 section 5.1).

The functional result of every operation is produced by the Dafny-verified kernel compiled to io.github.mahmoudimus.quic.internal.verifiedhkdf.VerifiedHkdf; this class preserves the historical Java API and exception surface. The verified kernel, the reviewed Java/JCE bridge, the Dafny compiler/runtime, the JDK cryptographic provider and JVM timing remain trusted. The proof establishes functional equations relative to an abstract HMAC; it makes no claim about HMAC security, provider correctness, constant-time execution or secret erasure.

Instances are immutable and thread-safe; no provider Mac is shared between calls.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Hkdf
    HKDF with HMAC-SHA256, for the SHA-256 cipher suites and QUIC's Initial secrets.
    static final Hkdf
    HKDF with HMAC-SHA384, for TLS_AES_256_GCM_SHA384.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    expand(byte[] prk, byte[] info, int length)
    HKDF-Expand(prk, info, length).
    byte[]
    expandLabel(byte[] secret, String label, byte[] context, int length)
    TLS 1.3's HKDF-Expand-Label(secret, label, context, length), with the "tls13 " prefix added to label.
    byte[]
    extract(byte[] salt, byte[] ikm)
    HKDF-Extract(salt, ikm).
    int
    The length of the hash, which is also the length of extract(byte[], byte[]) output.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SHA256

      public static final Hkdf SHA256
      HKDF with HMAC-SHA256, for the SHA-256 cipher suites and QUIC's Initial secrets.
    • SHA384

      public static final Hkdf SHA384
      HKDF with HMAC-SHA384, for TLS_AES_256_GCM_SHA384.
  • Method Details

    • hashLength

      public int hashLength()
      The length of the hash, which is also the length of extract(byte[], byte[]) output.
      Returns:
      the hash length in bytes
    • extract

      public byte[] extract(byte[] salt, byte[] ikm)
      HKDF-Extract(salt, ikm).

      For compatibility with the previous JCE-backed implementation, a null IKM is treated as an empty IKM, exactly as Mac.doFinal(null) was. A null salt remains a NullPointerException.

      Parameters:
      salt - the salt; empty means a string of hashLength() zeros
      ikm - the input keying material
      Returns:
      the pseudorandom key, hashLength() bytes
    • expand

      public byte[] expand(byte[] prk, byte[] info, int length)
      HKDF-Expand(prk, info, length).

      For compatibility with the previous JCE-backed implementation, a null info is treated as empty (as Mac.update(null) was), while a null PRK retains the historical IllegalArgumentException. Empty PRKs, negative lengths and lengths above 255 * hashLength remain rejected.

      Parameters:
      prk - the pseudorandom key, at least hashLength() bytes
      info - the context
      length - the output length, at most 255 times hashLength()
      Returns:
      the output keying material
    • expandLabel

      public byte[] expandLabel(byte[] secret, String label, byte[] context, int length)
      TLS 1.3's HKDF-Expand-Label(secret, label, context, length), with the "tls13 " prefix added to label.
      Parameters:
      secret - the secret to expand
      label - the label without its prefix, such as "quic key"
      context - the context, often empty or a transcript hash
      length - the output length, at most 65535
      Returns:
      the derived bytes
      Throws:
      IllegalArgumentException - if the label is not ASCII or a length exceeds its encoding