Skip to content

Kernel TLS (kTLS) for TLS 1.3; v0.3.0 changelog - #31

Merged
y001j merged 1 commit into
masterfrom
feat/ktls
Nov 22, 2026
Merged

y001j merged 1 commit into
masterfrom
feat/ktls

Conversation

@y001j

@y001j y001j commented Nov 22, 2026

Copy link
Copy Markdown
Owner

kTLS

After a TLS 1.3 handshake (AES-GCM / ChaCha20-Poly1305) the traffic keys go to the kernel: it encrypts/decrypts during the copies it makes anyway, and the connection's data path becomes that of plain TCP (in-place sends included). WithKTLS (default on), Conn.KernelTLS, Stats.KernelTLSConnections.

  • Handshake reads/writes the socket directly from its goroutine (dup'd fd), never past the end of the current TLS record, so data after the handshake stays in the socket for the kernel; the loop arms recv afterwards.
  • Keys from a per-connection KeyLogWriter (user's writer still gets the lines) → HKDF-Expand-Label → TCP_ULP tls, TLS_RX, TLS_TX with record sequence 0. That holds because kTLS servers send no session tickets (Go sends them under the application keys in its first flight) — documented; WithKTLS(false) keeps them.
  • TLS_RX_EXPECT_NO_PAD lets the kernel decrypt straight into the receive buffer (+6–14% at 16–64KB in the benchmark).
  • Control records: recv fails with EIO → recvmsg + TLS_GET_RECORD_TYPE; close_notify = EOF, tickets ignored, KeyUpdate closes. close_notify sent with TLS_SET_RECORD_TYPE.
  • Fallback to user-space crypto/tls without the tls module (unprivileged processes cannot load it), for TLS 1.2, or with WithKTLS(false).

Performance (vs a crypto/tls server, server on 1 core, arm64 VM)

kTLS user-space TLS
128B (1/64/512 conns) 1.00 / 1.03 / 1.13 1.01 / 1.04 / 1.17
16KB 0.98 0.92
64KB 0.90 0.77

perf: copies drop from 18% to 6% of CPU, but this VM's kernel AES-GCM (gcm-aes-ce) is no faster than Go's assembly, which caps the gain here.

Tests

TLS tests run with kTLS on and off and assert kTLS is used when the module is loaded; new: TLS 1.2 stays in user space, data sent right after the handshake is not lost. Pass with the module unloaded, on both backends, 5× each; CI now loads the module.

Also: CHANGELOG v0.3.0 (epoll fallback, TLS, kTLS). After merging, v0.3.0 will be tagged with a GitHub release.

🤖 Generated with Claude Code

After a TLS 1.3 handshake with AES-GCM or ChaCha20-Poly1305 the traffic
keys go to the kernel, which encrypts and decrypts during the copies it
makes anyway; the connection's data path is then that of plain TCP,
in-place sends included. WithKTLS (default on), Conn.KernelTLS and
Stats.KernelTLSConnections.

- The handshake now reads and writes the socket directly from its
  goroutine (on a dup of the fd), never past the end of the current
  TLS record, so everything after it stays in the socket for the
  kernel. The loop arms recv only after the handshake.
- Secrets come from a per-connection KeyLogWriter (the user's writer
  still receives the lines); key and IV via HKDF-Expand-Label; record
  sequence 0, which holds because kTLS servers send no session tickets
  (Go sends them with the application keys in its first flight).
- TLS_RX_EXPECT_NO_PAD lets the kernel decrypt into the receive buffer.
- Non-data records make recv fail with EIO: read with recvmsg and
  TLS_GET_RECORD_TYPE; close_notify ends the connection like EOF,
  session tickets are ignored, KeyUpdate closes. close_notify is sent
  with TLS_SET_RECORD_TYPE before shutdown.
- No tls module (unprivileged processes cannot load it), TLS 1.2, or
  WithKTLS(false): crypto/tls in user space as before.

Measured against a crypto/tls server (server on 1 core, arm64 VM):
kTLS 1.00-1.13x at 128B, 0.98x at 16KB, 0.90x at 64KB, versus
0.92x / 0.77x for user-space TLS at 16KB / 64KB. perf showed the copies
gone (18% -> 6% of CPU) while the kernel's AES-GCM is no faster than
Go's here. Tests run with kTLS on and off, assert that kTLS is used
when the module is loaded, cover TLS 1.2, data sent right after the
handshake, and pass with the module unloaded; CI loads the module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.55906% with 57 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.19%. Comparing base (c8b7ab1) to head (d24a0cc).

Files with missing lines Patch % Lines
tls.go 75.34% 54 Missing ⚠️
connection.go 84.61% 2 Missing ⚠️
conn.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #31      +/-   ##
==========================================
- Coverage   70.22%   70.19%   -0.04%     
==========================================
  Files          25       25              
  Lines        2146     2335     +189     
==========================================
+ Hits         1507     1639     +132     
- Misses        639      696      +57     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@y001j
y001j merged commit 43c3288 into master Nov 22, 2026
6 checks passed
@y001j
y001j deleted the feat/ktls branch November 22, 2026 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant