
MTProto is an open-source messaging protocol developed by Telegram. It's used for secure communication between clients and servers.
MTProto is designed to be fast, secure, and scalable. This is achieved through the use of encryption and other security measures.
MTProto uses a combination of symmetric and asymmetric encryption to protect data in transit. This ensures that even if an attacker intercepts the data, it will be unreadable without the decryption key.
The protocol is also designed to be highly available and fault-tolerant, with multiple servers and redundancy built-in.
A unique perspective: Mtproto Proxy Erlang Docker
Key Generation and Management
Keys are generated using the Diffie-Hellman protocol.
To initiate end-to-end encrypted communication, both clients must check that g, g_a, and g_b are greater than one and smaller than p-1.
Checking that g_a and g_b are between 2^2048-64 and p - 2^2048-64 is also recommended.
The final shared key is generated by taking the power of g_a with b and taking the modulus with dh_prime.

If the key length is less than 256 bytes, leading zero bytes are added as padding to make it exactly 256 bytes long.
The fingerprint of the key, key_fingerprint, is equal to the 64 last bits of SHA1 (key).
SHA1 is used for key fingerprinting, even in MTProto 2.0 secret chats.
Upon de-authorization, the event queue of the corresponding device will be forcibly cleared.
Request Handling
To initiate a secret chat, User A must first obtain Diffie-Hellman parameters, including a prime p and a high-order element g, by executing the messages.getDhConfig method. This method should be executed before each new key generation procedure to ensure up-to-date parameters.
The client must cache the values of p, g, and the version to avoid having to receive all of the values every time. If the stored version is still up-to-date, the server will return the constructor messages.dhConfigNotModified. The client should also check whether p is a safe 2048-bit prime and that g generates a cyclic subgroup of prime order.
To request encryption, Client A computes a 2048-bit number a and executes messages.requestEncryption after passing in g_a := pow(g, a) mod dh_prime.
Sending a Request
To initiate a secret chat, User A must first obtain the Diffie-Hellman parameters, including a prime number p and a high-order element g, by executing the messages.getDhConfig method.
Executing this method before each new key generation procedure is crucial to ensure the security of the chat.
The client should cache the values of the parameters together with the version to avoid having to receive all of the values every time.
If the version stored on the client is still up-to-date, the server will return the constructor messages.dhConfigNotModified.
Before proceeding, the client must check whether p is a safe 2048-bit prime by verifying that both p and (p-1)/2 are prime, and that 22048.
The client should also check that g generates a cyclic subgroup of prime order (p-1)/2, which is easily done using the quadratic reciprocity law.
The client can use a cache to store the result of these checks to avoid repeating lengthy computations in the future.
This cache might be shared with one used for Authorization Key generation.
If the client needs additional entropy for the random number generator, it can pass the random_length parameter to the server to generate its own random sequence of the appropriate length.
Working with Updates

Working with updates is crucial in secret chats to ensure proper handling of messages and maintain data integrity. Sequence numbers play a vital role in this process, as they help protect against possible manipulations by interpreting all messages in their original order.
A special mechanism for handling seq_no counters independently from the server is supported in secret chats. This allows for proper handling of these counters, which is further described in the article "Sequence numbers in Secret Chats."
To handle updates efficiently, a temporary message queue is introduced, which stores events in a way that allows for easy reconstruction of the difference in case of a long break in the connection or loss of an update. The value of qts increases by 1 with each new event, and the initial value may not be equal to 0.
The client acknowledges the receipt and storage of events from the temporary queue through a call to the messages.receivedQueue method or implicitly by a call to updates.getDifference. This ensures that all messages acknowledged as delivered by the client, as well as any messages older than 7 days, may be deleted from the server.

Here's a summary of the steps involved in handling updates:
It's essential to notify the remote client about your local layer in two cases: as soon as a new secret chat has been created, and immediately after the local client has been updated to support a new secret chat layer.
Encryption and Decryption
Encryption is a crucial part of secure communication, and MTProto uses AES-256 encryption with infinite garble extension (IGE). The encryption key fingerprint and the message key are added at the top of the resulting byte array.
For MTProto 2.0, the AES key and initialization vector are computed using a key derivation function (KDF) that takes the message key and authentication key as input. This KDF is used to generate the AES key and IV.
The encryption process involves padding the data to make its length divisible by 16 bytes, then computing the message key as the 128 middle bits of the SHA256 hash of the padded data. The AES key and IV are then generated using the KDF, and the data is encrypted using AES-256 encryption.
The decryption process involves verifying the authenticity of the authentication key ID, computing the AES key and IV using the KDF, and then decrypting the data using AES-256 decryption with IGE.
Here's a summary of the encryption and decryption process:
The result of the decryption process is the original message, which is then verified by comparing the message key with the one computed during encryption.
Security Features
Mtproto's security features are a mixed bag. The protocol lacks authenticated encryption (AE), which is a crucial aspect of secure communication.
This means that data sent over mtproto is not guaranteed to be authentic or tamper-proof. I've seen instances where this lack of security can lead to serious issues, such as data manipulation or eavesdropping.
The protocol also fails to provide indistinguishability under chosen-ciphertext attack (INDCCA), which is a key feature in preventing attacks where an attacker tries to decrypt data.
Perfect Forward Secrecy
Perfect Forward Secrecy is a security feature that protects past communications by automatically replacing old keys with new ones after a certain period.
Official Telegram clients re-key once a key has been used to decrypt and encrypt more than 100 messages.
Old keys are then securely discarded and cannot be reconstructed, even with access to the new keys currently in use.
This re-keying protocol is designed to keep past communications safe from prying eyes.
For example, if a key has been in use for more than one week and has been used to encrypt at least one message, it will be securely discarded and replaced with a new key.
Secret Chat Messaging
In a Secret Chat, messages are encrypted with a shared key that's discarded after a certain period of time.
Official Telegram clients initiate re-keying once a key has been used to decrypt and encrypt more than 100 messages, or has been in use for more than one week, provided the key has been used to encrypt at least one message.
Old keys are then securely discarded and cannot be reconstructed, even with access to the new keys currently in use.
If you're receiving an encrypted message, you must check that the message key matches the 128 middle bits of the SHA256 hash of the decrypted message, prepended by 32 bytes taken from the shared key.
If the message layer is greater than the one supported by your client, you'll be notified that your client version is out of date and prompted to update.
Cryptoanalysis
In cryptoanalysis, the lack of certain security features is a concern.
The protocol in question does not have authenticated encryption (AE), which is a crucial component for ensuring the integrity and authenticity of data.
This means that any modifications or tampering with the data will not be detectable, making it vulnerable to attacks.
The protocol also lacks indistinguishability under chosen-ciphertext attack (INDCCA), which is a measure to prevent an attacker from determining the original message even if they have access to the encrypted message and the decryption key.
Advanced Topics
Mtproto is a powerful tool for interacting with the Telegram API, but it's not without its complexities.
In mtproto, encryption is handled by the client, which sends encrypted requests to the server.
This approach allows for more flexibility and control over the encryption process, but it also requires more effort from the developer to implement correctly.
Mtproto uses a combination of RSA and AES encryption to secure the data being transmitted, making it a robust solution for secure communication.
By using mtproto, developers can create more secure and reliable applications that take advantage of the Telegram API.
Version 2.0
MTProto 2.0 is an upgrade to the original protocol, with significant improvements to its end-to-end encryption layer.
One key difference between MTProto 1.0 and MTProto 2.0 is the use of SHA-256 instead of SHA-1 for encryption.
SHA-256 is a more secure hashing algorithm, making it harder for attackers to compromise the encryption.
Another change is the involvement of padding bytes in the computation of msg_key.
This means that msg_key now depends not only on the message to be encrypted, but also on a portion of the secret chat key.
As a result, the encryption is more robust and secure.
Here are the key differences between MTProto 1.0 and MTProto 2.0:
- SHA-256 is used instead of SHA-1;
- Padding bytes are involved in the computation of msg_key;
- msg_key depends on both the message and the secret chat key;
- 12..1024 padding bytes are used instead of 0..15 padding bytes.
To upgrade from MTProto 1.0 to MTProto 2.0, both parties in a secret chat need to use at least Layer 73.
Updating Layers
Your client should always store the maximal layer that is known to be supported by the client on the other side of a secret chat. This value should be initialized to 46 when the secret chat is first created.
When you receive any packet containing information of an upper layer, you must update the remote layer value immediately. This can happen in two ways: when you receive a secret chat message with a layer number 46 or higher, or when you receive a decryptedMessageActionNotifyLayer service message.

To notify the remote client of your local layer, you must send a message of the decryptedMessageActionNotifyLayer type. This notification must be wrapped in a constructor of an appropriate layer.
You need to send this notification in two situations: as soon as a new secret chat has been created, immediately after the secret key has been successfully exchanged, and immediately after the local client has been updated to support a new secret chat layer.
Here are the conditions under which you need to notify the remote client about your local layer:
- When a new secret chat is created
- When the local client is updated to support a new secret chat layer
Note that you only need to notify the remote client when updating to new layers that contain changes in the secret chats implementation.
Telegram Communication History and Metadata Analysis
Telegram's communication history is stored on the user's device, making it difficult to retrieve.
This is in contrast to cloud-based services like Google Drive or Dropbox, where data is stored on remote servers.

Telegram's encrypted messages are stored in a SQLite database, which is a self-contained file that stores data in a structured format.
This database is not easily accessible, even to Telegram's developers.
As a result, analyzing Telegram's communication history and metadata requires specialized tools and expertise.
Metadata analysis can provide valuable insights into user behavior and communication patterns.
However, the complexity of Telegram's encryption and storage methods makes it a challenging task.
In 2019, a group of researchers successfully extracted metadata from a Telegram user's database.
Their findings highlighted the importance of metadata analysis in understanding user behavior.
Despite the challenges, researchers continue to develop new methods for analyzing Telegram's communication history and metadata.
Security Threats and Countermeasures
Security threats can occur when attackers send a fake message in a secret chat by replacing the encrypted packet with a random sequence. This can cause the recipient to not confirm receipt of the message, leaving the sender's message unread.
Attackers can also exploit the mtproto protocol by sending a message with a replaced encrypted packet, which will be flagged as unread in the sender's chat window. This can be a significant issue for users who rely on mtproto for secure communication.
To avoid these security threats, users should be cautious when sending messages in secret chats and ensure that the encrypted packets are not tampered with.
Attacks on Protocol
Sending a message in a secret chat with a tampered package can lead to a failed verification, leaving the message unread forever.
This type of attack is possible because the receiver doesn't confirm the message, and the sender is left with a message that remains unread.
The server in a mirrored attack or a repeat attack has control over the message flow, allowing it to hold messages, change their order, or resend them.
This vulnerability exists when at least one client uses a version of the protocol below 16.
A server-controlled chat is a reality when using the protocol version 16 and below.
Abuse-Prone Function and Channel Characteristics
A common security threat is the abuse of functions and channels that are not designed with security in mind.
Functions like password reset and account recovery can be exploited by attackers to gain unauthorized access to user accounts.
Attackers can use brute-force attacks to try thousands of passwords per minute, making it difficult to prevent.
The channel of communication can also be a vulnerability, especially if it's not properly secured.
For example, a company's customer support channel can be used to send phishing emails or malware attachments to unsuspecting customers.
Libraries and Tools
There are several libraries and tools available for working with mtproto, making it easy to integrate into your projects.
The @mtproto/core library is a JavaScript client library for the Telegram API (MTProto) that can be used in a browser.
The @mtproto/core library is a JavaScript client library for the Telegram API (MTProto) that can be used in a browser. It's available on GitHub at https://github.com/alik0211/mtproto-core.
You can also use MadelineProto, a PHP client/server API for the telegram MTProto protocol, which is available on GitHub at https://github.com/danog/MadelineProto. This library is a good option if you're working with PHP.
Here are some popular libraries and tools for working with mtproto:
Libraries for Programming Languages
Libraries for Programming Languages can be a game-changer for developers working with Telegram's API.
The @mtproto/core library is a JavaScript client library for browser, specifically designed for Telegram's MTProto protocol.
If you're a PHP developer, MadelineProto is a great option - it's an async client/server API for the telegram MTProto protocol.
Telethon is a pure Python 3 MTProto API client library that's perfect for bots and regular users alike.
pyrogram is another Python library that's a Telegram MTProto API Client Library and Framework.
Rust developers can use the grammers library, a set of Rust libraries to interact with Telegram’s API.
C# developers can use TLSharp, a Telegram client library implemented in C#.
TDLib is a cross-platform library that can be used from almost any programming language, including Python, JavaScript, Go, Java, and many others.
MTProto is a full-native implementation of MTProto protocol on Golang.
Here's a list of libraries for various programming languages:
MTProxy
MTProxy is a simple MT-Proto proxy that you can use to connect to Telegram servers. To get started, you'll need to obtain a secret, which is used to authenticate your connection.
This secret is crucial for establishing a secure connection to Telegram's servers.
Telegram's Structure and Features
Telegram uses a protocol called mtproto, which is a binary protocol for sending messages between the client and server.
The client and server communicate using a binary protocol, which is a more efficient way to send data over the internet compared to text-based protocols.
The mtproto protocol is designed to be fast and secure, which is essential for a messaging app like Telegram that handles millions of messages per second.
Telegram's server uses a load balancer to distribute incoming requests across multiple servers, ensuring that no single server is overwhelmed with traffic.
This load balancing helps Telegram to scale its service to handle a large number of users and messages.
The mtproto protocol uses a combination of HTTP and WebSocket connections to establish a persistent connection between the client and server.
This persistent connection allows for real-time updates and efficient data transfer, making Telegram's messaging experience feel smooth and responsive.
The client and server use a session ID to identify each other and establish a secure connection.
The session ID is used to authenticate and authorize requests between the client and server.
Telegram's server uses a concept called "databases" to store and manage user data, including messages, contacts, and settings.
These databases are used to efficiently retrieve and update user data, making Telegram's messaging experience feel fast and responsive.
Encryption and Anonymity Comparison of Telegram, LINE, Signal, and Major Apps
Telegram uses mtproto, a protocol that allows for end-to-end encryption and anonymity.
Telegram's encryption is based on the MTProto protocol, which uses AES-256 encryption.
LINE's encryption is based on the OpenSSL library, which uses AES-256 encryption.
Signal uses the Signal Protocol, which uses AES-256 and 2048-bit RSA encryption.
Telegram's encryption is end-to-end encrypted, meaning only the sender and recipient can read the messages.
LINE's encryption is also end-to-end encrypted, but only for secret chats.
Signal's encryption is always end-to-end encrypted, with no exceptions.
Telegram allows for anonymity through its mtproto protocol, which hides IP addresses.
LINE does not offer true anonymity, as it collects user data.
Signal is known for its strong focus on user anonymity and privacy.
Regulation and Response
Mtproto's regulation and response are shaped by its Russian heritage, with the protocol being developed by the Russian company Telegram.
Mtproto's developers have been open about their efforts to comply with Russian regulations, including the requirement to store user data within the country.
The protocol's use of end-to-end encryption and decentralized architecture makes it difficult for authorities to access user data, but this has not prevented Russian authorities from issuing fines and warnings to Telegram.
日本の規制状況とその影響 アプリ運営体制と規制動向の現実
In Japan, the regulatory environment is quite complex and has a significant impact on app operations.
The Japanese government has introduced various regulations to ensure the safety and security of users, such as the Act on Regulation of Transaction of Records of Credit Information.
The Act on Regulation of Transaction of Records of Credit Information requires app developers to obtain users' consent before collecting and using their personal data.
The Japanese Ministry of Economy, Trade and Industry (METI) is responsible for overseeing the app industry and has established guidelines for app developers to follow.
These guidelines include requirements for data protection, user consent, and transparency.
The guidelines also specify that app developers must clearly inform users about the purposes of data collection and how their data will be used.
The Japanese government has also introduced the Act on the Protection of Personal Information to protect users' personal data.
This act requires app developers to implement appropriate security measures to prevent data breaches and unauthorized access to user data.
The Japanese app industry has responded to these regulations by implementing various measures to ensure compliance, such as hiring data protection officers and conducting regular security audits.
The Japanese government has also established a system for reporting data breaches, which requires app developers to notify the authorities and affected users in the event of a breach.
This system aims to ensure that users are informed and protected in the event of a data breach.
The Japanese app industry has also seen an increase in the number of apps that offer opt-out options for data collection and usage.
This is a result of the government's emphasis on transparency and user consent.
Telegraph Regulation and Global Responses

The International Telegraph Union, established in 1865, played a crucial role in regulating global telegraph communication.
In 1871, the ITU adopted the first international telegraph convention, which set standards for telegraph services and facilitated global communication.
The ITU's regulations helped establish a standardized system of telegraph communication, making it easier for people to send messages across borders.
The British government imposed regulations on the telegraph industry in the late 19th century, requiring companies to provide standardized services and adhering to set rates.
These regulations helped to standardize the telegraph industry in the UK, making it more efficient and accessible to the public.
The British government's regulations also led to the establishment of the General Post Office, which took over the management of the telegraph system in 1869.
The General Post Office was responsible for overseeing the telegraph system, setting rates, and regulating the industry.
As a result of the ITU's regulations and the British government's actions, global telegraph communication became more standardized, efficient, and accessible.
Frequently Asked Questions
What is the secret port of MTProto server?
The default secret for MTProto server is d0d6e111bada5511fcce9584deadbeef. This secret is printed on start along with the default port 1443.
Featured Images: pexels.com


