Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.Mobile.Tls13KeySchedule

NEW UNIT (TLS 1.3).

  This unit implements the TLS 1.3 key schedule of RFC 8446 section 7
  (Key Schedule) and section 7.3 (Traffic Key Calculation).

  It REUSES the existing HKDF primitives already present in stKDF.pas:
      tHKDFExtract  -> HKDF-Extract (RFC 5869)
      tHKDFExpand   -> HKDF-Expand  (RFC 5869)
  and the hash primitives in stSecUtils (MustFindHashClass / THash). No new
  cryptography is implemented here - only the TLS-1.3-specific label framing
  (HKDF-Expand-Label / Derive-Secret) and the Extract/Expand chaining that
  produces the early, handshake and application traffic secrets.

  The unit is deliberately independent of the StreamSec.Mobile.* handshake
  units (it speaks only tBytes / THashClass), so it can be consumed by the
  record layer (StreamSec.Mobile.TlsLayer) without introducing a circular
  dependency.

Feature notes: TLS implementation notes (RFC map)

Classes, interfaces and records

classTTls13KeySchedule

TTls13KeySchedule walks the RFC 8446 sec.7.1 secret ladder:

0
| PSK ->HKDF-Extract = Early Secret
| +--> Derive-Secret(.,"c e traffic", CH) (0-RTT)
| Derive-Secret(.,"derived","")
| (EC)DHE->HKDF-Extract = Handshake Secret
| +--> Derive-Secret(.,"c hs traffic", CH..SH)
| +--> Derive-Secret(.,"s hs traffic", CH..SH)
| Derive-Secret(.,"derived","")
| 0 ->HKDF-Extract = Master Secret
+--> Derive-Secret(.,"c ap traffic", CH..SF)
+--> Derive-Secret(.,"s ap traffic", CH..SF)
+--> Derive-Secret(.,"res master", CH..CF)

The caller supplies the running Transcript-Hash at each stage; the schedule never stores handshake messages itself.

constructor Create(aHashClass: THashClass); destructor Destroy; override; procedure InitEarlySecret(const aPSK: tBytes);
aPSK may be empty
procedure DeriveHandshakeSecret(const aSharedSecret: tBytes);
(EC)DHE output
procedure DeriveMasterSecret; function ClientEarlyTrafficSecret(const aTHash_CH: tBytes): tBytes;
0-RTT
function ClientHandshakeTrafficSecret(const aTHash_SH: tBytes): tBytes;
Delegates to DeriveSecret, forwarding fHashClass, fHandshakeSecret, 'c hs traffic' and aTHash_SH, and returns its result.
function ServerHandshakeTrafficSecret(const aTHash_SH: tBytes): tBytes;
Delegates to DeriveSecret, forwarding fHashClass, fHandshakeSecret, 's hs traffic' and aTHash_SH, and returns its result.
function ClientApplicationTrafficSecret(const aTHash_SF: tBytes): tBytes;
Delegates to DeriveSecret, forwarding fHashClass, fMasterSecret, 'c ap traffic' and aTHash_SF, and returns its result.
function ServerApplicationTrafficSecret(const aTHash_SF: tBytes): tBytes;
Delegates to DeriveSecret, forwarding fHashClass, fMasterSecret, 's ap traffic' and aTHash_SF, and returns its result.
function ResumptionMasterSecret(const aTHash_CF: tBytes): tBytes;
Delegates to DeriveSecret, forwarding fHashClass, fMasterSecret, 'res master' and aTHash_CF, and returns its result.
function ExporterMasterSecret(const aTHash_SF: tBytes): tBytes;
Delegates to DeriveSecret, forwarding fHashClass, fMasterSecret, 'exp master' and aTHash_SF, and returns its result.
function ResumptionBinderKey: tBytes;
PSK resumption (RFC 8446 sec.4.2.11.2): the binder key is derived from the early secret (which must have been seeded from the resumption PSK via InitEarlySecret). The PskBinderEntry is then computed like a Finished MAC keyed by FinishedKey(ResumptionBinderKey) over the truncated ClientHello.
class function ResumptionPSK(aHashClass: THashClass; const aResMaster, aNonce: tBytes): tBytes;
Resumption PSK (RFC 8446 sec.4.6.1): PSK = HKDF-Expand-Label( resumption_master_secret, "resumption", ticket_nonce, Hash.length).
property HashClass: THashClass read fHashClass; class function HkdfExpandLabel(aHashClass: THashClass; const aSecret: tBytes; const aLabel: AnsiString; const aContext: tBytes; aLen: Integer): tBytes;
HKDF-Expand-Label (RFC 8446 sec.7.1).
class function DeriveSecret(aHashClass: THashClass; const aSecret: tBytes; const aLabel: AnsiString; const aTranscriptHash: tBytes): tBytes;
Derive-Secret (RFC 8446 sec.7.1).
class function Extract(aHashClass: THashClass; const aSalt, aIKM: tBytes): tBytes;
HKDF-Extract wrapper (RFC 5869) - tHKDFExtract from stKDF.
class function TranscriptHash(aHashClass: THashClass; const aMessages: tBytes): tBytes;
Transcript-Hash(messages) = Hash(messages) (RFC 8446 sec.4.4.1).
class procedure TrafficKeyIV(aHashClass: THashClass; const aTrafficSecret: tBytes; aKeyLen, aIVLen: Integer; out aKey, aIV: tBytes);
Per-direction record key + iv (RFC 8446 sec.7.3).
class procedure QUICInitialSecrets(aVersion: Cardinal; const aDCID: TBytes; out aClientSecret, aServerSecret: TBytes);
QUIC Initial traffic secrets use SHA-256 and the version's Initial salt (RFC 9001 section 5.2; RFC 9369 section 3.3.1). aVersion is the on-wire version, $00000001 or $6B3343CF. aDCID contains at most 20 bytes.
class procedure QUICPacketKeys(aHash: THashClass; const aTrafficSecret: TBytes; aKeyLen: Integer; aVersion: Cardinal; out aKey, aIV, aHeaderKey: TBytes);
QUIC packet key, 12-byte IV and header protection key (RFC 9001 section 5.1; RFC 9369 section 3.3.2). aTrafficSecret has aHash.DigestSize bytes; aKeyLen is 16 or 32. Outputs are independent of the read-only input.
class function FinishedKey(aHashClass: THashClass; const aBaseKey: tBytes): tBytes;
finished_key = HKDF-Expand-Label(BaseKey,"finished","",Hash.length).
class function UpdateTrafficSecret(aHashClass: THashClass; const aSecret: tBytes): tBytes;
application_traffic_secret_N+1 (KeyUpdate, RFC 8446 sec.7.2).