Skip to content
TGWAY
Channels & Bots STEP 97 / 99

Telegram Client Security Guidelines if You Implement MTProto

Implementing MTProto yourself? The key-exchange and per-message checks the official guidelines require, why a failed check means disconnect, and why use TDLib.

  • Applies toDevelopers
  • LevelL4
  • Reading time3 min
  • Verified

The short answer

  1. 1The safest choice is a proven implementation: TDLib.
  2. 2During key creation, check the prime, the value ranges and the random values.
  3. 3On every message check key, length, session, ID and time; any failure means drop and disconnect.
In this article
  1. The conclusion first: don't build it yourself
  2. Checks when creating the key
  3. Checks on every message you receive
  4. If any check fails, drop it and disconnect
  5. Before you implement it yourself
  6. FAQ
  7. At a glance

The conclusion first: don't build it yourself

Telegram publishes client security guidelines for developers who want to implement MTProto themselves. Most people who read them reach the same conclusion: using a proven implementation, TDLib, is the safest choice. See building TDLib.

Below are the names and purposes of the checks you must make if you do implement it yourself. The actual conditions, including exact ranges, are in the official guidelines; follow them as written.

Checks when creating the key

CheckWhat it means
Is the received prime safe?The prime the server sent for the key exchange really is a safe prime, of the right size
Range of the generator and public valuesThey are not too small or right at the edge of the range; weak values reveal the key
Response hashAfter decrypting a response, the hash attached to it matches the content
Random values matchThe random values exchanged in this session are the same as at the start, so no other exchange slipped in
Quality of your secret valueYour own secret exponent comes from a secure random number generator, not straight from randomness the server supplied

What these steps do is explained in creating an authorization key.

Checks on every message you receive

CheckWhat it means
Message keyThe value recomputed from the decrypted content equals the message key attached to it; this detects forgery and tampering
Length and paddingThe length written inside is not larger than the real data, and the padding length is within the allowed range
Session IDIt is the session your app created
Message IDIt has the right even or odd parity for its direction, and repeats no ID already received
TimeThe time encoded in the message ID is not too far from now, which blocks replays of old messages

If any check fails, drop it and disconnect

Before you implement it yourself

  • Is there truly a reason TDLib will not work? Language and platform issues are mostly solved by tdjson.
  • Have you read the official guidelines and protocol documents from start to finish, and written a test for every check above?
  • If you support secret chats, do you follow the rekeying and sequence number rules? See secret chat encryption.
  • Does your app meet the privacy and security obligations in the terms? See the API terms of service.

FAQ

Should I implement MTProto myself?

Usually not. The guidelines show how many checks are required; TDLib already implements them.

What must I check during the key exchange?

That the prime is safe, the generator and public values are in range, hashes match and nonces are unchanged.

What if a message fails a security check?

Discard it, close the connection and reconnect.

Sources

Summary

At a glance

Telegram Client Security Guidelines if You Implement MTProto — at a glance illustration
  1. 1Key checks

    Safe prime, ranges, hashes, nonces, your own randomness.

  2. 2Message checks

    msg_key, length, session, parity and replay, time.

  3. 3On failure

    Discard, disconnect, retry; never log details.

Share this guide

Send it to a friend who has the same question.

Telegram WhatsApp
More

Open on your phonePoint your phone camera here to open this guide.

Updated

Related articles

View all ›