JFLib

netio/tls/keysched.rs#

Source: lib/netio/tls/keysched.rs

keysched -- the TLS 1.3 key schedule (RFC 8446 7.1) with the hash of the cipher suite (SHA-256
or SHA-384): HKDF-Expand-Label, Derive-Secret, traffic keys and finished keys; the TLS 1.2 PRF
(RFC 5246 5) with SHA-256 or SHA-384; the running transcript hash of a handshake

Every function computes with the hash it is given (HMAC of any HashAlg); the TLS suites use
SHA-256 and SHA-384, and the Transcript keeps those two only
  • type Secret = Vec<u8> a secret of the schedule (as long as the hash: 32 or 48 bytes)

  • fn expand_label(h: HashAlg, secret: &[u8], label: &str, context: &[u8], out: &mut [u8]) HKDF-Expand-Label(secret, label, context, len); out at most 255 blocks of the hash (panics beyond)

  • fn derive(h: HashAlg, secret: &[u8], label: &str, hash: &[u8]) -> Secret Derive-Secret(secret, label, transcript hash)

  • fn extract(h: HashAlg, salt: &[u8], ikm: &[u8]) -> Secret HKDF-Extract (RFC 5869 2.2): HMAC(salt, ikm); an empty salt is the same as zeros of the hash's length (HMAC pads its key with zeros)

  • fn empty_hash(h: HashAlg) -> Vec<u8> the hash of no messages

  • fn early_secret(h: HashAlg, psk: &[u8]) -> Secret the early secret of a pre-shared key (RFC 8446 7.1): HKDF-Extract(0, PSK)

  • fn binder_key(h: HashAlg, early: &[u8]) -> Secret the binder key of a resumption PSK ("res binder", RFC 8446 4.2.11.2) from its early secret

  • struct Schedule the secrets of one handshake

    • hash: HashAlg
    • early: Secret -- the early secret (of the PSK, or of zeros)
    • handshake: Secret
    • master: Secret

impl Schedule

  • fn new(h: HashAlg, psk: Option<&[u8]>, shared: &[u8]) -> Schedule from the pre-shared key (None: zeros) and the (EC)DHE / hybrid shared secret

  • fn binder_key(&self, external: bool) -> Secret the binder key of a resumption PSK ("res binder") or an external one ("ext binder")

  • fn traffic_keys_len(h: HashAlg, secret: &[u8], key_len: usize) -> (Vec<u8>, [u8; 12]) the AEAD key (of key_len bytes) and IV of a traffic secret

  • fn prf12(h: HashAlg, secret: &[u8], label: &str, seed: &[u8], out: &mut [u8]) TLS 1.2: PRF(secret, label, seed) with P_SHA256, or P_SHA384 for the SHA-384 suites

  • fn finished(h: HashAlg, base: &[u8], transcript: &[u8]) -> Vec<u8> verify_data of a Finished message (and a PSK binder: the binder key as base)

  • fn update(h: HashAlg, secret: &[u8]) -> Secret the next traffic secret (KeyUpdate)

  • struct Transcript the transcript hash of a handshake: both hashes until the cipher suite picks one

impl Transcript

  • fn new() -> Transcript
  • fn update(&mut self, m: &[u8])
  • fn hash(&self, h: HashAlg) -> Vec<u8> the hash so far with h: SHA-256 or SHA-384 while it is kept, else empty
  • fn keep(&mut self, h: HashAlg) keep only h from here on (the suite is chosen); a hash other than these two keeps none

Source: docs/API.md in the JFLib repository.