Back to Blog
By AriesZhou · · 4 min read

WeChat xlog Encryption Analysis: TEA + ECDH Key Exchange

Android

How does WeChat’s xlog logging component protect user privacy? This article analyzes the use of the TEA encryption algorithm and ECDH key exchange within it.

Introduction to WeChat Mars and xlog

Mars is a cross-platform infrastructure component suite developed by the WeChat team, consisting of four main modules:

ModulePurpose
commCommon base library
XlogHigh-performance logging component
SDTNetwork detection component
STNSignaling network component

xlog is designed specifically for mobile, featuring high performance, high availability, security, and fault tolerance. More importantly, xlog supports encrypted transmission, ensuring log content cannot be intercepted or tampered with during transit.

The core value of xlog encryption

In mobile log collection scenarios, encryption is especially critical:

  • User devices may be on insecure network environments
  • Logs may contain sensitive business information
  • Prevents logs from being maliciously analyzed or tampered with

The TEA encryption algorithm in detail

TEA (Tiny Encryption Algorithm) is a lightweight symmetric encryption algorithm designed by British cryptographers Roger Needham and David Wheeler. It is known for its small code footprint and fast execution, making it well-suited for resource-constrained mobile environments.

Algorithm principles

TEA uses a Feistel structure with 64-bit data blocks and a 128-bit key, performing encryption over 64 rounds of iteration. The core encryption function is as follows:

// TEA 加密函数简化版
void tea_encrypt(uint32_t* v, uint32_t* k) {
    uint32_t v0 = v[0], v1 = v[1];
    uint32_t sum = 0;
    uint32_t delta = 0x9e3779b9;

    for (int i = 0; i < 32; i++) {
        v0 += ((v1 << 4) ^ (v1 >> 5)) + v1 ^ (sum + k[sum & 3]);
        sum += delta;
        v1 += ((v0 << 4) ^ (v0 >> 5)) + v0 ^ (sum + k[(sum >> 11) & 3]);
    }
    v[0] = v0;
    v[1] = v1;
}

Characteristics of TEA

  • Lightweight: the core encryption code is only a few dozen lines
  • Efficient: extremely fast encryption and decryption, suitable for high-frequency logging scenarios
  • Reversible: decryption is the inverse of encryption
  • Security: although designed in 1997, it remains secure with proper configuration (such as multiple iterations)

TEA in xlog

In xlog, the TEA algorithm is used for actual data encryption:

// xlog 中的加密调用(伪代码)
void __TeaEncrypt(char* logData, int len, char* key) {
    // 将日志数据按 8 字节(64 位)分块
    // 对每块使用 TEA 算法加密
    // ...
}

xlog uses a 16-byte (128-bit) key, perfectly matching TEA’s 128-bit key length.

ECDH Key Exchange Explained

While TEA encryption is strong, key distribution is the core challenge. You can’t just hardcode the key in the app, right?

ECDH (Elliptic Curve Diffie-Hellman) elegantly solves this problem: both parties can negotiate the same symmetric key without ever transmitting the key itself!

ECDH Workflow

sequenceDiagram
    participant Client as 客户端 (App)
    participant Server as 服务器端

    Note over Client: 生成 ECC 公私钥对<br/>pubKeyA, priKeyA
    Client->>Server: 发送 pubKeyA
    Note over Server: 生成 ECC 公私钥对<br/>pubKeyB, priKeyB
    Server-->>Client: 发送 pubKeyB

    Note over Client: 使用 pubKeyB + priKeyA<br/>计算共享密钥 secret
    Note over Server: 使用 pubKeyA + priKeyB<br/>计算共享密钥 secret

    Note over Client,Server: 双方得到相同的 secret!

xlog’s Key Negotiation Flow

xlog uses a preset public key + ephemeral private key approach:

  1. Server: Preset the server-side ECC key pair (private key kept strictly confidential)
  2. Client: Dynamically generate a client-side ECC key pair on each startup
  3. Key derivation: Client uses server public key + client private key to derive the session key
  4. Encrypted transmission: Use the session key to encrypt logs with TEA
// xlog 密钥协商简化流程
void key_negotiation() {
    // 1. 生成客户端公私钥
    uECC_make_key(client_pubkey, client_pri);

    // 2. 使用服务端公钥计算共享密钥
    uECC_shared_secret(server_pubkey, client_pri, session_key);

    // 3. session_key 即为 TEA 加密用的对称密钥
}

Security analysis

StageSecurity
Public key transmissionSecure (public keys are meant to be public)
Private key protectionClient private key generated in memory; server private key kept offline
Key agreementA third party cannot derive the private key from the public key (elliptic curve discrete logarithm problem)
Key storageSymmetric key stored on the stack, not persisted, destroyed when the process exits

Configuration in practice

Generate the key pair

Run the following under mars/log/crypt:

python gen_key.py

This generates:

  • public.key - Public key, embedded in the app
  • private.key - Private key, kept securely on the server

App integration

// 初始化 xlog 并启用加密
appender_open(
    kAppenderAsync,                    // 异步模式
    logPath,                           // 日志路径
    "your_public_key_here",            // 公钥
    0,                                 //缓存大小
    kryptonDebug,                      // 加密模式
    0                                  // 加密标志
);

⚠️ WeChat officially recommends: use async logging mode in production; sync logging does not account for encryption efficiency.

Summary

xlog’s encryption scheme reflects a simple and efficient design philosophy:

LayerTechnologyAdvantage
Key exchangeECDHNo pre-shared key required; secure negotiation
Symmetric encryptionTEALightweight, high-performance, mobile-friendly
Key storageStack memoryAutomatically destroyed after process exit; no persistence risk
Mode selectionAsync-firstBalances performance and security

This ECDH + TEA combination ensures secure key distribution while meeting mobile performance requirements, making it a classic practice in lightweight encryption.