Install
openclaw skills install mtproto-2-0MTProto 2.0 protocol implementation guide for Telegram backend development. Use when implementing MTProto encryption, handshake, message serialization, or bu...
openclaw skills install mtproto-2-0Complete implementation guide for Telegram's MTProto 2.0 encryption protocol.
⚠️ IMPORTANT SECURITY NOTICE
This skill provides educational guidance on MTProto 2.0 protocol. Before using in production:
See references/security-notice.md for detailed corrections.
Use this skill when:
Client Server
| |
| 1. req_pq |
|----------------------------------->|
| |
| 2. server_public_key_fingerprints|
|<-----------------------------------|
| |
| 3. req_DH_params |
|----------------------------------->|
| |
| 4. server_DH_params |
|<-----------------------------------|
| |
| 5. set_client_DH_params |
|----------------------------------->|
| |
| 6. dh_gen_ok |
|<-----------------------------------|
See references/handshake.md for detailed implementation.
// Key derivation from Auth Key + Message Key
func deriveKeys(authKey, msgKey []byte) (aesKey, aesIV []byte) {
x := sha256.Sum256(append(msgKey, authKey[:36]...))
y := sha256.Sum256(append(authKey[40:76], msgKey...))
aesKey = append(x[:8], y[8:24]...)
aesIV = append(y[:8], x[24:32]...)
return
}
See references/encryption.md for complete algorithm.
┌─────────────────────────────────────────────────────────┐
│ Auth Key ID │ 8 bytes │ SHA1(authKey)[96:128] │
├─────────────────────────────────────────────────────────┤
│ Message Key │ 16 bytes │ SHA256(plaintext)[8:24] │
├─────────────────────────────────────────────────────────┤
│ Encrypted Data │ Variable │ AES-256-IGE encrypted │
└─────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────┐
│ Salt │ 8 bytes │ Server Salt │
├─────────────────────────────────────────────────────────┤
│ Session ID │ 8 bytes │ Unique session ID │
├─────────────────────────────────────────────────────────┤
│ Message ID │ 8 bytes │ Timestamp + sequence │
├─────────────────────────────────────────────────────────┤
│ Sequence No │ 4 bytes │ Packet sequence │
├─────────────────────────────────────────────────────────┤
│ Length │ 4 bytes │ Message body length │
├─────────────────────────────────────────────────────────┤
│ Message Body │ Variable │ TL-serialized data │
├─────────────────────────────────────────────────────────┤
│ Padding │ 0-15 B │ Random padding │
└─────────────────────────────────────────────────────────┘
See references/message-format.md for details.
// Constructor with ID
user#938458c1 id:long first_name:string = User;
// Method definition
checkPhone#6fe51dfb phone_number:string = Bool;
See references/tl-language.md for TL schema reference.
type MTProtoConn struct {
conn net.Conn
authKey []byte
salt int64
sessionID int64
seqNo int32
}
func (m *MTProtoConn) Send(msg TLObject) error {
// 1. Serialize
plaintext := msg.Encode()
// 2. Calculate Message Key
msgKey := calcMsgKey(plaintext, m.authKey)
// 3. Derive AES keys
aesKey, aesIV := deriveKeys(m.authKey, msgKey)
// 4. Encrypt
encrypted := encryptAESIGE(plaintext, aesKey, aesIV)
// 5. Construct packet
packet := constructPacket(m.authKey, msgKey, encrypted)
// 6. Send
_, err := m.conn.Write(packet)
return err
}