ktls/tls13

Types

Tls13Suite = enum
  tlsAes128GcmSha256,       ## `TLS_AES_128_GCM_SHA256` (HKDF-SHA-256).
  tlsAes256GcmSha384,       ## `TLS_AES_256_GCM_SHA384` (HKDF-SHA-384).
  tlsChacha20Poly1305Sha256  ## `TLS_CHACHA20_POLY1305_SHA256` (HKDF-SHA-256).

Procs

proc hkdfExpandLabel(secret: openArray[byte]; label: string;
                     context: openArray[byte]; outLen: Natural; hashLen: Natural): seq[
    byte] {....raises: [ValueError], tags: [], forbids: [].}
HKDF-Expand-Label(Secret, Label, Context, Length) with the "tls13 " prefix. hashLen selects SHA-256 (32) or SHA-384 (48).
proc nextTrafficSecret(secret: openArray[byte]; hashLen: Natural): seq[byte] {.
    ...raises: [ValueError], tags: [], forbids: [].}
Advance a traffic secret on KeyUpdate (RFC 8446 ยง7.2).
proc parseTls13Suite(name: string): Tls13Suite {....raises: [ValueError], tags: [],
    forbids: [].}
Parse an OpenSSL-style suite name (TLS_AES_128_GCM_SHA256, โ€ฆ).
proc suiteCipher(s: Tls13Suite): CipherSuite {....raises: [], tags: [], forbids: [].}
proc suiteHashLen(s: Tls13Suite): Natural {....raises: [], tags: [], forbids: [].}
proc toKeyMaterial(suite: Tls13Suite; secret: openArray[byte];
                   recSeq: array[8, byte] = [0'u8, 0, 0, 0, 0, 0, 0, 0]): KeyMaterial {.
    ...raises: [ValueError, KtlsError], tags: [], forbids: [].}
Expand a TLS 1.3 traffic secret into kernel KeyMaterial, splitting the 12-byte IV into the kernel's salt + iv layout (ChaCha20-Poly1305 keeps the full 12-byte IV, empty salt).
proc trafficIv(secret: openArray[byte]; hashLen: Natural): array[12, byte] {.
    ...raises: [ValueError], tags: [], forbids: [].}
Derive the 12-byte iv for a traffic secret.
proc trafficKey(secret: openArray[byte]; hashLen: Natural; keyLen: Natural): seq[
    byte] {....raises: [ValueError], tags: [], forbids: [].}
Derive the key for a traffic secret.