Skip to content

etemenanki-reality overview

Source files: 10 · checked against Etemenanki 46c12e4
  • Etemenanki/Cargo.toml
  • Etemenanki/reality/Cargo.toml
  • Etemenanki/reality/src/lib.rs
  • Etemenanki/reality/src/keys.rs
  • Etemenanki/reality/src/kx.rs
  • Etemenanki/reality/src/auth.rs
  • Etemenanki/protocols/Cargo.toml
  • Etemenanki/reality/tests/unit/keys.rs
  • Etemenanki/reality/tests/unit/kx.rs
  • Etemenanki/reality/tests/unit/auth.rs

etemenanki-reality implements REALITY: a TLS 1.3 server that passes itself off as another site, called dest. The server forwards every ClientHello to dest. A client that sealed the right key into the ClientHello’s legacy_session_id gets a TLS 1.3 handshake with the server instead, built from dest’s own ServerHello and padded to dest’s record lengths. Every other connection, and every connection whose handshake goes wrong before the server’s flight is sent, is relayed to dest untouched, so a probe only ever talks to dest. The crate also holds the matching client.

This page covers the crate as a whole: its scope, the rule that keeps it at the bottom of the dependency graph, the module layout, the configuration values and their decoders (keys.rs), the key exchange (kx.rs), REALITY’s own authentication (auth.rs) and what the crate never prints. TLS 1.3 subset, Server and Client cover the rest, and Transports: REALITY covers how the protocols crate and the supervisor use it.

The crate does It leaves to others
Parse the configuration values in Xray’s formats: the X25519 keys, the shortIds, the client version bounds, the server names. Config files and specs: a front end decodes the strings (with this crate’s decoders) and keeps bytes.
Run its own TLS 1.3 subset: one full handshake, three suites, two key-exchange groups, Ed25519. Any other TLS: the project’s TLS transport is OpenSSL’s stack in protocols/src/transports/tls/.
Decide whether a ClientHello is one of its clients, mirror dest’s handshake shape, and relay everyone else to dest. Dialing dest, resolving its name and the socket policy: the caller connects to dest and hands both sockets in.
Give an authenticated connection back as a RealityStream, and a refused one as a Camouflage relay with a reason safe to log. Accounting, routing, bans and connection limits: the supervisor decides what to do with each outcome.
Dial a REALITY server as a client with a Chrome 133 ClientHello. Which proxy protocol rides on top: VLESS, and its Vision flow, live in the protocols crate.

No stock TLS stack can replay dest’s ServerHello with only its key share replaced, or pad each record of its flight to the length dest used. REALITY needs both, so the crate carries the TLS 1.3 it needs and nothing more:

  • TLS 1.3 only, no TLS 1.2;
  • cipher suites TLS_AES_128_GCM_SHA256 (0x1301), TLS_AES_256_GCM_SHA384 (0x1302) and TLS_CHACHA20_POLY1305_SHA256 (0x1303);
  • key exchange X25519 and X25519MLKEM768;
  • Ed25519 signatures only, for the server’s temporary certificate;
  • one full handshake per connection: no PSK or resumption, no HelloRetryRequest, no 0-RTT, no client certificates.

The protocol follows XTLS/REALITY at commit 83f6217 (2026-10-14), the revision Xray-core v26.10.17 builds with. That revision requires a ClientHello to carry an X25519MLKEM768 key share before any X25519 share. The earlier revision 9234c772 (2026-03-22) authenticates against the first X25519 share, or else the X25519 half of a hybrid share. The server accepts either rule: the earlier one by default, the strict one when ServerConfig::with_hybrid_required(true) asks for it (see Server). The client always sends the hybrid share first, so it passes both.

XTLS/REALITY is MPL-2.0. The crate reimplements its behaviour and translates none of its code.

The crate depends on no other etemenanki crate. reality/Cargo.toml says so in a comment, and the crate has no dialer, resolver or routing of its own: every async entry point is generic over AsyncRead + AsyncWrite + Unpin streams the caller connected.

flowchart LR
  app["etemenanki-app"] --> sup["etemenanki-supervisor"]
  katana["katana"] --> sup
  sup --> proto["etemenanki-protocols"]
  proto -- "feature reality" --> real["etemenanki-reality"]
  proto --> env["etemenanki-environment"]
  proto --> con["etemenanki-concepts"]

The protocols crate reaches it only through its reality feature (reality = ["dep:etemenanki-reality"], off by default), and only behind wrapper types of its own: RealityServer, ClientConfig, RealityStream and Camouflage. No type of etemenanki-reality appears in the protocols crate’s public API. A REALITY fix therefore ships as a release of this crate alone, and reaches the app and katana through a lockfile update, without a protocols release. The supervisor turns the feature on.

Dependencies:

Crate Used for
tokio The async drivers and the relay.
openssl The record AEAD, X25519, Ed25519, the certificate, and every random byte.
hkdf, hmac, sha2 The key schedule, the auth key and the certificate HMAC. They run during the handshake and on a KeyUpdate, never per record.
ml-kem 0.3 with hazmat The ML-KEM-768 half of X25519MLKEM768. openssl 0.10 has no ML-KEM encapsulation API, and a system OpenSSL may predate 3.5. hazmat exposes encapsulation with caller-supplied randomness, which comes from OpenSSL’s RNG.

lib.rs denies clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing and clippy::arithmetic_side_effects outside tests, so parsing code reaches bytes only through checked accessors.

File Contents
reality/src/lib.rs Crate docs (scope, pin, boundaries), lint policy, re-exports: RealityStream, RawParts, ConfigError and the four decode_* functions. client and server are public modules.
reality/src/keys.rs The configuration values: PrivateKey, PublicKey, ShortId, ShortIds, ClientVersion, ServerNames, ConfigError, the decoders.
reality/src/kx.rs X25519 and X25519MLKEM768: server_exchange, x25519, ClientShares, SharedSecret.
reality/src/auth.rs AuthKey, SessionInfo, open_session_id, seal_session_id, TempCert, verify_certificate, verify_signature.
reality/src/codec.rs Reader and put_vec, the bounds-checked TLS encoding helpers; see TLS 1.3 subset.
reality/src/tls13/ Records, key schedule, handshake messages, session keys; see TLS 1.3 subset.
reality/src/server/ ServerConfig, CamouflageReason, the sans-I/O handshake and accept; see Server.
reality/src/camouflage.rs Camouflage, the relay to dest; see Server.
reality/src/stream.rs RealityStream and RawParts, the established connection; see Server.
reality/src/client/ ClientConfig, the Chrome hello, the client handshake and connect; see Client.

Every format is Xray’s, so a key, a shortId or a version copied from an Xray config means the same thing here.

Value Type Xray setting Format
Server private key PrivateKey privateKey Unpadded base64url (RFC 4648 §5) of exactly 32 bytes, a usable X25519 key
Server public key PublicKey publicKey (password) The same encoding, 32 bytes
shortId ShortId shortIds entry An even number of hex digits, at most 16, either case, zero-padded at the end to 8 bytes; "" is the all-zero shortId
shortId set ShortIds shortIds At least one; duplicates collapse
Client version ClientVersion minClientVer, maxClientVer x.y.z, one to three parts of decimal digits, each below 256; missing parts are zero (1.8 is 1.8.0). Compared as one 24-bit big-endian number
Server names ServerNames serverNames At least one; each 1 to 253 bytes of ASCII letters, digits, -, . and _, without a trailing dot. Matched exactly against the SNI, so a wildcard is refused rather than left to never match

The base64url decoder reads as Go’s base64.RawURLEncoding does: no padding, no line breaks, the URL-safe alphabet only, and, like Go’s non-strict decoder, it does not insist that the unused low bits of the last character are zero. A last group of one character is refused.

reality/src/keys.rs
#[non_exhaustive]
pub enum ConfigError {
Key(&'static str), // which key: "private key" or "public key"
NoShortIds,
ShortId,
NoServerNames,
ServerName(String),
ClientVersion(String),
ClientVersionRange(String, String),
Crypto(String), // OpenSSL failed to set up a key or the certificate
}
pub fn decode_private_key(text: &str) -> Result<[u8; 32], ConfigError>;
pub fn decode_public_key(text: &str) -> Result<[u8; 32], ConfigError>;
pub fn decode_short_id(text: &str) -> Result<[u8; 8], ConfigError>;
pub fn decode_client_version(text: &str) -> Result<[u8; 3], ConfigError>;

The messages:

Variant Message
Key invalid REALITY private key: expected 32 bytes of unpadded base64url (or public key)
NoShortIds REALITY needs at least one shortId
ShortId invalid REALITY shortId: expected an even number of hex digits, at most 16
NoServerNames REALITY needs at least one server name
ServerName invalid REALITY server name "…"
ClientVersion invalid REALITY client version "…": expected x.y.z, each part below 256
ClientVersionRange REALITY client version bounds are reversed: … is above …
Crypto REALITY crypto setup failed: …

A server name and a version are quoted back; a key and a shortId never are.

A front end that lowers a REALITY server or client into a typed spec keeps the key, the shortIds and the client version as bytes, so it has to decode Xray’s strings. The four decode_* functions are built on the same parsers the crate’s own configs use, so there is one copy of each rule. decode_private_key also checks that OpenSSL accepts the bytes as an X25519 key. The protocols crate wraps them in transports::reality with io::Error (see Transports: REALITY); etemenanki-app and katana call those wrappers.

kx.rs runs the handshake’s own key exchange, X25519 (RFC 7748) and X25519MLKEM768 (draft-ietf-tls-ecdhe-mlkem):

Group Code point Client share Server share Shared secret
X25519 0x001d 32-byte public key 32-byte public key 32 bytes
X25519MLKEM768 0x11ec 1184-byte ML-KEM-768 encapsulation key, then a 32-byte X25519 key (1216 bytes) 1088-byte ciphertext, then a 32-byte X25519 key (1120 bytes) ML-KEM secret, then X25519 secret (64 bytes)
reality/src/kx.rs
pub(crate) fn server_exchange(group: u16, client_share: &[u8]) -> Option<ServerExchange>;
pub(crate) fn x25519(ours: &PKey<Private>, peer: &[u8; 32]) -> Option<[u8; 32]>;
pub(crate) struct ClientShares { /* standalone X25519 key, optional (ML-KEM key, X25519 key) */ }
impl ClientShares {
pub(crate) fn generate(hybrid: bool) -> Option<Self>;
pub(crate) fn x25519_share(&self) -> Option<[u8; 32]>;
pub(crate) fn hybrid_share(&self) -> Option<Vec<u8>>;
pub(crate) fn complete(&self, group: u16, server_share: &[u8]) -> Option<SharedSecret>;
}
  • The server answers each exchange with a fresh key pair. For the hybrid group it encapsulates with 32 random bytes from OpenSSL’s RNG (encapsulate_deterministic, the hazmat call).
  • x25519 refuses a peer key OpenSSL rejects and a shared secret of all zeros (a low-order point), as Go’s crypto/ecdh does.
  • server_exchange returns None for any other group, a share of the wrong length, or an encapsulation key that does not decode.
  • ClientShares holds separate X25519 keys for the hybrid share and the standalone share, as Chrome sends them. The ML-KEM key comes from a 64-byte seed drawn from OpenSSL.

REALITY adds its own authentication on top of TLS 1.3, with one key per connection:

flowchart TB
  share["client X25519 share"] --> dh["X25519 with the server's static key"]
  dh --> hkdf["HKDF-SHA256, salt = random[0..20], info REALITY"]
  hkdf --> auth["AuthKey (32 bytes)"]
  auth --> seal["AES-256-GCM seal of version, time, shortId into session_id"]
  auth --> hmac["HMAC-SHA512 of the certificate's Ed25519 key"]
  seal --> server["the server opens the session_id"]
  hmac --> client["the client checks the certificate HMAC"]
reality/src/auth.rs
pub(crate) struct AuthKey([u8; 32]);
impl AuthKey {
pub(crate) fn derive(shared: &[u8; 32], random: &[u8; 32]) -> Result<Self, Fatal>;
}
pub(crate) struct SessionInfo {
pub(crate) version: ClientVersion,
pub(crate) time: u32, // Unix seconds when the client built the hello
pub(crate) short_id: ShortId,
}
pub(crate) fn open_session_id(key: &AuthKey, hello: &ClientHello) -> Option<SessionInfo>;
pub(crate) fn seal_session_id(key: &AuthKey, raw: &mut [u8], range: Range<usize>,
random: &[u8; 32], info: &SessionInfo) -> Result<(), Fatal>;
pub(crate) struct TempCert { /* Ed25519 key, its public key, the DER template */ }
impl TempCert {
pub(crate) fn generate() -> Result<Self, Fatal>;
pub(crate) fn certificate(&self, key: &AuthKey) -> Result<Vec<u8>, Fatal>;
pub(crate) fn sign(&self, transcript: &[u8]) -> Result<Vec<u8>, Fatal>;
}
pub(crate) fn verify_certificate(der: &[u8], key: &AuthKey) -> Option<[u8; 32]>;
pub(crate) fn verify_signature(public: &[u8; 32], transcript: &[u8], signature: &[u8]) -> bool;

The auth key. HKDF-SHA256 over the X25519 secret between the client’s share and the server’s static key, salted with the first 20 bytes of the ClientHello’s random, info "REALITY", expanded to 32 bytes. Which of the client’s shares the server uses is the key-share rule on Server; the client uses its standalone X25519 key.

The session_id. The client seals 16 bytes into the 32-byte legacy_session_id:

version[3] | 0x00 | time (u32, big-endian) | shortId[8]

The cipher is AES-256-GCM under the auth key. The nonce is the last 12 bytes of the ClientHello’s random. The associated data is the whole ClientHello message, header included, with the session_id bytes zeroed. The session_id holds the 16 ciphertext bytes, then the 16-byte tag. open_session_id returns None unless the session_id is 32 bytes and the tag authenticates the hello, so any change to the hello after sealing breaks it. The byte after the version is not looked at on opening, as in XTLS.

The certificate. TempCert::generate makes one throwaway Ed25519 key per server configuration, as XTLS makes one per process. Its certificate is the one Go’s x509.CreateCertificate makes from XTLS’s template, byte for byte: version 3, serial 0, empty issuer and subject, both validity times Go’s zero time (00010101000000Z) as GeneralizedTime, no extensions. For each connection, certificate copies the template and writes HMAC-SHA512(auth key, Ed25519 public key), 64 bytes, into the signature field. sign makes the CertificateVerify signature over the transcript with the same Ed25519 key, in the form RFC 8446 §4.4.3 gives (64 spaces, the context string, a zero byte, the transcript hash).

A client accepts the certificate only when it parses, carries an Ed25519 key, and its signature field equals the HMAC under the client’s own auth key, compared in constant time. A real certificate fails that check, which is how a client learns its hello reached dest.

The keys and shortIds are what let a client in. The crate never prints them:

Type Debug output
PrivateKey, PublicKey, ShortId PrivateKey(<redacted>) and so on
ShortIds ShortIds(<n redacted>), the count only
AuthKey, SharedSecret, TempCert AuthKey(<redacted>) and so on
Secret, TrafficKeys (key schedule) The hash or suite, then <redacted>
HashValue The hash algorithm only
Camouflage The reason only
RealityStream Role, suite and whether each side is closed

ConfigError messages name the field, never a key or a shortId. CamouflageReason, the reason a connection went to dest, is a category; it never holds a key, a shortId or a session_id. AuthKey, SharedSecret, Secret and TrafficKeys zero their bytes on drop with volatile writes.

  • No etemenanki crate in reality/Cargo.toml, and no type of this crate in the protocols crate’s public API.
  • Every configuration value is decoded once, when a ServerConfig or ClientConfig is built, so a bad value fails before any connection.
  • A key or shortId reaches no Debug output and no error message.
  • Every secret comes from OpenSSL’s RNG: the ephemeral keys, the ML-KEM randomness and seed, the client’s random.
  • X25519 never yields an all-zero secret.
File Covers
reality/tests/unit/keys.rs RFC 7748’s key pairs and shared secret, a low-order peer key refused, the base64url rules (padding, alphabet, length, Go’s lenient last character), shortId parsing and padding, versions parsed and compared like Xray, server names exact and refusing wildcards, Debug and error output free of keys and shortIds, and the_public_decoders_agree_with_the_parsers.
reality/tests/unit/kx.rs Both groups agree end to end with the ML-KEM secret first, fresh randomness per exchange, bad client and server shares refused, SharedSecret’s Debug.
reality/tests/unit/auth.rs The auth key and a sealed hello against vectors made with Python’s cryptography package, any change to the hello breaking the seal, a session_id of another length not opened, the certificate template against Go 1.26’s x509 output, the HMAC in the signature field, a real certificate refused, CertificateVerify over the transcript, and keys_and_short_ids_never_show_in_debug_output.

The crate’s tests run with cargo test -p etemenanki-reality and are part of the workspace gates; the interop tests with a real Xray are described on Transports: REALITY.