A Practical Cryptanalysis of The Telegram Messaging Protocol
A Practical Cryptanalysis of The Telegram Messaging Protocol
Computer Science
Master's thesis
Author: Supervisor:
Jakob Bjerre Jakobsen Claudio Orlandi
(20102095)
September 2015
AU AARHUS
UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE
Abstract
The number one rule for cryptography is never create your own crypto. In-
stant messaging application Telegram has disregarded this rule and decided
to create an original message encryption protocol. In this work we have done
a thorough cryptanalysis of the encryption protocol and its implementation.
We look at the underlying cryptographic primitives and how they are com-
bined to construct the protocol, and what vulnerabilities this has. We have
found that Telegram does not check integrity of the padding applied prior to
encryption, which lead us to come up with two novel attacks on Telegram.
The rst of these exploits the unchecked length of the padding, and the sec-
ond exploits the unchecked padding contents. Both of these attacks break
the basic notions of IND-CCA and INT-CTXT security, and are conrmed to
work in practice. Lastly, a brief analysis of the similar application TextSecure
is done, showing that by using well known primitives and a proper construc-
tion provable security is obtained. We conclude that Telegram should have
opted for a more standard approach.
ii
Acknowledgements
First of all I would like to thank my former oce partner Morten Djerns
Bech who had the energy to have helpful discussions with me about my thesis,
even though the deadline for his own thesis was much closer than mine. I
would like to thank Frederik Bitsch Kirk for proofreading and spell checking
my entire work, truly his English skill surpasses mine. And nally I would
like give great thanks to my supervisor Claudio Orlandi for helping me realize
my idea for this project, and for his guidance and lengthy discussions with
me that led to interesting discoveries.
iii
Contents
1 Introduction 5
1.1 Chapter overview . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Preliminaries 9
2.1 Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 Symmetric-key cryptosystems . . . . . . . . . . . . . . . . . . 9
2.3 Security denitions . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4 Hash functions . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3 Protocols 32
3.1 Device registration . . . . . . . . . . . . . . . . . . . . . . . . 32
3.2 Key exchange . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3 Message encryption . . . . . . . . . . . . . . . . . . . . . . . . 37
3.4 Key Derivation Function . . . . . . . . . . . . . . . . . . . . . 43
4 Result of analysis 47
4.1 Random padding vulnerability . . . . . . . . . . . . . . . . . . 47
4.2 Replay and mirroring attacks in older versions . . . . . . . . . 51
4.3 Timing attacks on MTProto . . . . . . . . . . . . . . . . . . . 51
5 Experimental validation 53
5.1 Attack #1: padding length extension . . . . . . . . . . . . . . 53
5.2 Attack #2: padding plaintext collision . . . . . . . . . . . . . 55
5.3 Malicious server attacks . . . . . . . . . . . . . . . . . . . . . 57
6 Known attacks 59
6.1 Known attacks on primitives . . . . . . . . . . . . . . . . . . . 59
6.2 Known attacks on MTProto . . . . . . . . . . . . . . . . . . . 65
iv
Chapter 1
Introduction
Goals The goal of this work is to give a thorough analysis of the security
of the Telegram instant messaging application and its message encryption
protocol MTProto. We will look at the cryptographic primitives used, and
how the message encryption protocol is constructed from these. We will see
which notions of security this protocol provides, and which it does not. With
this knowledge we will propose attacks on the Telegram messaging protocol,
exploiting the found weaknesses. Finally, we will propose improvements to
the protocol that solve found issues and mitigate proposed attacks.
1 Or ten US billion.
5
1.1. CHAPTER OVERVIEW
6
1.1. CHAPTER OVERVIEW
7
1.1. CHAPTER OVERVIEW
1.1.7 Contributions
The contributions of this work are two novel vulnerabilities that we found in
the protocol MTProto, proving it insecure under basic notions.
8
Chapter 2
Preliminaries
2.1 Notation
Throughout this work we will use consistent notation. We write for assign-
ment. '=' is used for conditional checks. Variable names are written in italic.
9
2.2. SYMMETRIC-KEY CRYPTOSYSTEMS
AES is a symmetric-key block cipher with block size 128 bit and keys ranging
from 128 to 256 bits. The internal components of AES are beyond the scope
of this work, and we will treat it as a black box for encryption.
Typically, a mode of operation for block ciphers needs a block length ini-
tialization vector, which we will later see is the case for CBC and CTR
10
2.2. SYMMETRIC-KEY CRYPTOSYSTEMS
mode. But as we see in Savard [18], IGE needs both an initial plaintext
block m0 and ciphertext block c0 . The original specication says the initial
ciphertext block is derived using a second random key K 0 : c0 = fK0 (m0 ),
but the OpenSSL implementation allows for it to be an arbitrary block given
as a parameter.
Writing out the operations for encrypting each block we get the following, as
visualized in gure 2.1:
c1 m0 EncK (m1 c0 )
c2 m1 EncK (m2 m0 EncK (m1 c0 ))
c3 m2 EncK (m3 m1 EncK (m2 m0 EncK (m1 c0 )))
ci mi1 EncK (mi mi2 EncK (mi1 . . . m0 EncK (m1 c0 ) . . . ))
11
2.2. SYMMETRIC-KEY CRYPTOSYSTEMS
Writing out the operations for decrypting each block we get the following, as
visualized in gure 2.2:
m1 c0 DecK (c1 m0 )
m2 c1 DecK (c2 c0 DecK (c1 m0 ))
m3 c2 DecK (c3 c1 DecK (c2 c0 DecK (c1 m0 )))
mi ci1 DecK (ci ci2 DecK (ci1 . . . c0 DecK (c1 m0 ) . . . ))
Figure 2.3: Non-malleability of IGE causing every block after the modication to become
garbled.
Figure 2.3 shows that ipping one bit in one ciphertext block would cause
the decrypted block to garble as well as all the following blocks. It is also
possible to use bi-directional IGE, garbling the entire plaintext output. This
can be used to provide plaintext integrity, guaranteeing the contents of a
message has not been tampered with, by appending a block of all 0's at the
end and checking this upon decryption.
12
2.2. SYMMETRIC-KEY CRYPTOSYSTEMS
Figure 2.4: Malleability of CBC mode allow modifying one plaintext output block, at
the cost of garbling the one before it.
This is bad if evil Eve knows Alice is sending Bob the message "Hi Bob,
please transfer $100 to Eve", Eve could capture the encrypted ciphertext
and modify the block before the one containing the value, so that the de-
crypted message becomes "[garbled] transfer $999 to Eve".
13
2.3. SECURITY DEFINITIONS
the ciphertext. It's easy to see that like CBC, CTR mode is malleable but
with no blocks being garbled in the process. The lack of block chaining also
makes counter mode trivially parallelizable, leading to higher performance
on multi-core systems.
Rather than adding padding to the plaintext, the key stream generated with
CTR mode can simply be truncated to match the plaintext length.
14
2.3. SECURITY DEFINITIONS
2. The adversary A is given input 1n and oracle access to EncK (), and
outputs a pair of messages M0 , M1 of the same length l.
4. The adversary A continues to have oracle access to EncK (), and out-
0
puts a bit b .
1
P r[Experiment outputs 1] + negl(n)
2
eg. probability of winning is only negligibly better than guessing at random.
15
2.3. SECURITY DEFINITIONS
A O
1n n
C K Gen(1 )
Mi
B
Ci
C
M0 , M1
Ci EncK (Mi )
Mi
B
Ci
C
Ci EncK (Mi )
b0
B if b = b0 then
Output 1
else
Output 0
16
2.3. SECURITY DEFINITIONS
before inputting the next plaintext block. We will see that this can be bro-
ken even if the underlying cryptosystem has IND-CPA, which is why we will
dene blockwise-adaptive IND-CPA, or IND-BACPA.
It may not be intuitive that this scenario can exist in practice, but it is
the case for some online services where the server will encrypt each block
as soon as it's uploaded, such as SSH. The denition is described by the
following LOR-oracle game:
For a symmetric key encryption system (Gen, Enc, Dec) with security
parameter n:
n
1. A key K is generated by running Gen(1 ). A random bit b {0, 1} is
chosen.
2. The adversary A is given input1n and oracle access to EncK (), and
sends a pair of message blocks m01 , m11 of xed length.
17
2.3. SECURITY DEFINITIONS
A O
1n
C K Gen(1 )
n
b {0, 1}
m0i , m1i {0, 1}l
m0i , m1i
B
ci
C ci EncK (mbi )
b0
B if b = b0 then
Output 1
else
Output 0
adversary is only allowed to output plaintext blocks m0i 6= m1i once, and for
any other query it must hold that m0i = m1i . We will use this denition in
section 6.1.1 where we will show that IGE does not have indistinguishable
encryptions under the primitive BACPA notion.
This gives us a strong denition for encryption oracles, but we have not
considered that the adversary might also have access to a decryption oracle.
18
2.3. SECURITY DEFINITIONS
4. The adversary A continues to have oracle access to EncK () and DecK (),
but is not allowed to query the latter on the challenge ciphertext itself.
0
Eventually, A outputs a bit b .
1
P r[Experiment outputs 1] + negl(n)
2
Again we assume to be probabilistic and not deterministic for the same
reason as with IND-CPA, that encrypting the same message twice outputs
dierent ciphertexts.
The decryption oracle returns if the plaintext message does not have the
correct structure, correct headers etc, in other words does not look like it
had been encrypted by the oracle. This distinction will be useful for proving
IND-CCA later on.
19
2.3. SECURITY DEFINITIONS
A O
1n n
C K Gen(1 )
Mi Ci EncK (Mi )
B
Ci
C
Ci 6= C
B Mi DecK (Ci )
Mi
C
Mi Ci EncK (Mi )
B
Ci
C
Ci 6= C
B Mi DecK (Ci )
Mi
C
b0
B if b = b0 then
Output 1
else
Output 0
20
2.3. SECURITY DEFINITIONS
and still be accepted by their receiver. This property will also be useful for
proving IND-CCA.
We say the symmetric encryption scheme has plaintext integrity if for all
probabilistic polynomial-time adversaries A there exists a negligible function
negl() such that
21
2.3. SECURITY DEFINITIONS
A O
1n n
C K Gen(1 )
M
B
C
C C EncK (M )
C0
B M 0 DecK (C 0 )
if M 0 6= M 0 / M then
Output 1
else
Output 0
We say the symmetric encryption scheme has ciphertext integrity if for all
probabilistic polynomial time adversaries A there exists a negligible function
negl() such that
22
2.3. SECURITY DEFINITIONS
A O
1n n
C K Gen(1 )
M
B
C
C C EncK (M )
C0
B M 0 DecK (C 0 )
if M 0 6= C 0 / C then
Output 1
else
Output 0
INT-CTXT INT-PTXT
Claim 2: From Paterson [15] we have that IND-CPA and INT-CTXT im-
plies IND-CCA,
23
2.3. SECURITY DEFINITIONS
1. The key generation algorithm Gen takes as input the security parameter
1n and outputs a key K with |K| b.
24
2.3. SECURITY DEFINITIONS
n
1. A random key K is generated by running Gen(1 ).
2. The adversary A is given input 1n and oracle access to MacK (). The
adversary eventually outputs a pair (M, t). Let T denote the set of all
MAC tags output by the oracle.
Note that there also exists the notion of (weakly) unforgeable MACs for
which the tag output by A is only required to match a message not queried
to the oracle, but we will not cover it in this work as Bellare's article [5]
shows that it will not help us guarantee Authenticated Encryption.
AE IND-CPA + INT-CTXT
25
2.3. SECURITY DEFINITIONS
26
2.3. SECURITY DEFINITIONS
Now the adversary simply ips the rst two bits of the second block (note
that the rst block of counter mode is the nonce). He queries the decryption
0
oracle with this new C , which is dierent from C . We now observe that if
0
the rst bit of the plaintext M was 1, then the decryption of C will be valid
as the transformed message was ipped from 01 to 10 or vice versa. And
since the underlying message is unchanged, the MAC will also verify without
issue.
If the rst bit of M was 0 however, the oracle will return because the
transformed message was ipped from 00 to the illegal 11. This means the
adversary has now learned the rst bit of the plaintext, and he can repeat
this to uncover the full plaintext one bit at a time.
And so we see that this construction can be insecure in some cases, and we
will therefore consider it insecure.
27
2.4. HASH FUNCTIONS
In the xed length case we require that l0 > l so that the hash function
compresses the input.
28
2.4. HASH FUNCTIONS
0
3. The output of the experiment is dened to be 1 if and only if x 6= x
s s 0
and H (x) = H (x ). In such a case we say that A has found a collision.
We say the hash function is collision resistant if for all probabilistic polynomial-
time adversaries A there exists a negligible function negl() such that
29
2.4. HASH FUNCTIONS
30
2.4. HASH FUNCTIONS
31
Chapter 3
Protocols
In this chapter we will take a close look at the protocols and algorithms used
in Telegram, and more specically in their MTProto encryption scheme. We
will look at how a client is rst registered with the server, how it establishes
a secure, end-to-end encrypted connection to another peer and how the in-
ternals such as the key derivation function are constructed.
This analysis is based on the ocial Telegram client source code for version
2.7.0, downloaded in mid April 2015 from github [22].
3. C decomposes n into primes p and q such that p < q, which will serve
as proof of work. C has a list of public keys stored locally, and selects
the key serverpk that matches the received ngerprint.
32
3.1. DEVICE REGISTRATION
The random bit strings nonce and server_nonce are both 128-bit chosen at
random by the client and server respectively. The number new_nonce is a
256-bit bit string chosen at random by the client.
n is a composite 64-bit integer, which is the product of two odd primes p and
q , and the public RSA key ngerprint is the 64 least signicant bits of the
SHA1 digest of the server's public key.
The RSA encryption is performed using default Java RSA from javax.crypto.Cipher,
which uses the standard PKCS #1 approach. In this approach the padding
is pseudorandom data according to the Internet Engineering Task Force [8].
In order to decrypt the server response and obtain the DH parameters, the
client must derive the 256-bit AES key and IV, as shown in gure 3.1. The de-
crypted response contains nonce, server_nonce, the DH parameters g, p, ga
and a server timestamp used for synchronization. It also contains a 160-bit
SHA1 digest of contents. The response is padded with randomness which is
discarded with no checking apart from the length, which must be 0-15 bytes.
The client checks that the response contents match the digest that was
p1
sent with it, p must be a safe prime, meaning q = is prime, and
2
22047 < p < 22048 . g is equal to 2, 3, 4, 5, 6 or 7 and it must hold that
g generates a cyclic subgroup of prime order p1 2
, and that 1 < ga < p 1,
and if any of these checks fail, the protocol is aborted.
The private value b is a randomly generated 2048-bit bit string. The re-
quest that is sent to the server contains nonce, server_nonce and gb , as well
as a 160-bit SHA1 digest of these. The request is padded with randomness
prior to encryption, and encrypted using the temporary AES key and IV.
b
The client also computes the shared long-term key auth_key ga mod p.
33
3.2. KEY EXCHANGE
Figure 3.1: Deriving temporary AES IGE key and IV during registration.
Upon receiving gb , the server does the same and sends response ok or failure
if nonce, server_nonce or any DH parameters are wrong.
Client and server now have a shared secret key which is used only for client-
server communication and regular (not end-to-end encrypted) chats, and the
client device is now registered and authorized. The Telegram application will
upload the client's phone contacts to the server in order to check if any of
them have already registered, and if so it automatically adds them as Tele-
gram contacts.
If the user tries to sign in from a new, unauthorized device, a new veri-
cation code will be sent. At rst through the Telegram application on an
authorized device, or through SMS if the former fails, and the above protocol
repeats.
34
3.2. KEY EXCHANGE
3. B now receives this chat request and accepts on only one of his autho-
rized devices. He contacts the server to get the latest DH parameters
and generates his secret value b.
5. Both users now compute the shared secret auth_key gab mod p =
gba mod p, and the exchange is complete.
The clients are to verify that the DH parameters returned by the server sat-
p1
isfy the following: p must be a safe prime, meaning q = is prime, and
2
2047 2048
2 < p < 2 . g is equal to 2, 3, 4, 5, 6 or 7 and it must hold that
g generates a cyclic subgroup of prime order p1
2
. Also note that the DH
parameters obtained from the server are xed, but can be updated between
application versions.
a = rclient rserver
Where rclient is 2048 random bits generated by the client, and rserver is 2048
random bits generated by the server. The reason for this is to help clients
1
with weak/awed random number generators .
1 Which was the case for Android back in August 2013 [1].
2 Or ten US quintillion.
35
3.2. KEY EXCHANGE
The ngerprint of the shared secret key is the 128 least signicant bits of
SHA1(auth_key). This is visualized within the Telegram application as an
8x8 grid, each cell having one of four colors, as seen in gure 3.2. Users
Figure 3.2: 128-bit key ngerprint visualization within Telegram for android.
are intended to meet in person and compare these, ensuring there are no
dierences. In this case, the chat session has not been compromised by a
man-in-the-middle. But meeting in person often defeats the purpose of the
chat, to communicate from afar, and is much more cumbersome than say,
checking the users public key as with public key cryptography. This leads
most users to simply take a screenshot of the key ngerprint and send this
in the newly instantiated, unauthenticated chat. The man-in-the-middle has
a key ngerprint for each user, and it is very easy for him to fool them by
replacing the screenshot with one of his own. But assuming the users fol-
36
3.3. MESSAGE ENCRYPTION
low the protocol, their chat session can be considered authenticated and safe
from third parties.
Header: each payload contains three xed headers related to the ver-
sion of the protocol and the last one specifying the type of attached
media, which we will always assume to be the media_empty header.
seq_in: 32-bit counter for messages sent to the initiator of the chat.
37
3.3. MESSAGE ENCRYPTION
seq_out: 32-bit counter for messages sent from the initiator of the
chat.
As of version 2.7.0 of April 2015, these are the contents of a message sent in
secret chats. Prior to being sent in a secret chat, this payload is encrypted
using MTProto as seen in gure 3.4.
38
3.3. MESSAGE ENCRYPTION
msg_key: truncated 128 least signicant bits of the SHA1 hash of the
content to be encrypted. Used for integrity check.
AES key and IV: the 256 bit key and 256 bit IV required by AES in
IGE mode. Derived using the KDF which we will see in section 3.4.
As shown in gure 3.4, the integrity check msg _key is computed from every-
thing but the padding. We will see later on that this diers from Telegram's
own description in gure 4.1 where padding is included in msg _key and fur-
thermore is said to be 0-15 bytes (0-120 bit).
The code snippet in gure 3.5 is from sending messages in secret chats. It
hashes the payload and truncates it to nd the message key, and afterwards
adds padding.
In section 4.1.2 we'll see why the padding is in fact only 0-96 bits, and not
0-120 bits as claimed.
39
3.3. MESSAGE ENCRYPTION
Figure 3.5: Padding added after deriving message key in implementation, as seen in
SecretChatHelper.java from the ocial repository.
2 seq _no + x
40
3.3. MESSAGE ENCRYPTION
Both clients maintain the raw (untransformed) counters, and the two trans-
formed counters are included in every sent message.
Not shown is that rst the received auth_key _id matches the one associ-
ated with the secret chat on the client. If not, the message is discarded. We
will go more in-depth with how parameters are checked upon decryption in
algorithm 1.
41
3.3. MESSAGE ENCRYPTION
42
3.4. KEY DERIVATION FUNCTION
MTProto uses AES in IGE mode with 256 bit keys. IGE requires an ini-
tialization vector twice the size of one AES block, 256 bit. A new key and
IV pair is generated for every message, and these are derived from the secret
auth_key and the message itself. This is to ensure only the other holder of
the auth_key can decrypt the message, and that the key will only decrypt
the one message.
The way the key and IV is derived is by the use of multiple SHA1 hashes
[27]. Presumably because a single SHA1 hash only outputs 160 of the 512
bits needed. Using a newer hash function such as SHA-512 would be ideal
here, but MTProto has chosen to stick with SHA1.
As shown in gure 3.7, four SHA1 hashes are performed, each taking as
input 256 bit of the auth_key and the 128 bit msg _key (which we recall
is the truncated SHA1 digest of the message). In total, the 1024 most sig-
nicant bits of the auth_key are used. Also note that the position of the
msg _key in the hash input is shifted each time, presumably to make the
outputs more "random" even though there is no evidence of this being true.
The AES key and IV are then formed by taking substrings of each of the
four SHA1 outputs and concatenating them to form two 256 bit strings. It
is unclear why they are combined the way they are, and not just using 64 bit
of each output per string.
43
3.4. KEY DERIVATION FUNCTION
In MTProto [25] this corresponds to the AES keys being derived from the
44
3.4. KEY DERIVATION FUNCTION
This new key exchange reuses the DH parameters that were received from
the server during the initial key exchange, and no server randomness is added
when generating new secret values a and b. One user will compute his public
value ga and send this to user B through the already established chat. B will
respond with his new public value gb , and both users compute and update the
auth_key for the existing session, the same way that it was done in section
3.2. All older messages will remain decrypted and readable on both clients.
The auth_key ngerprint used for clients to authenticate each other will not
be changed.
The ocial Telegram client does a layer version negotiation as soon as the
secret chat key exchange is completed. The auth_key is stored in an en-
cryptedChat object along with a layer initialized to 0. The initiator of the
chat A sends his layer in an encrypted message to B, and B responds with
his layer version. Both clients store the lower of the two layers in the en-
cryptedChat object in order to conform with the older version.
The current latest layer is version 23. Forward secrecy was introduced in
layer 20, but apart from that no major changes have happened since layer
17. In layer 16 and down to 8 (the version that introduced secret chats),
message sequence numbers were handled very dierently. Rather than being
included in each encrypted message, they were stored and maintained on the
45
3.4. KEY DERIVATION FUNCTION
proprietary server, and the message payload looked as shown in gure 3.9.
Figure 3.9: Contents of the payload to encrypt for versions below 17.
This meant that an encrypted messages did not contain any indication of the
message's sequence number nor which of the two users had sent it, and the
clients would have to trust the server to take care of this.
The messages themselves did not contain any mechanism to stop an unau-
thorized third party from sning sent messages and performing replay or
mirroring attacks as discussed in section 3.3.1, which is mitigated by further
encrypting the messages under the client-server authorization key from 3.1.
46
Chapter 4
Result of analysis
47
4.1. RANDOM PADDING VULNERABILITY
4. O decrypts C0 and reads the payload length from it. Anything that
comes after this length, e.g. the padding, is discarded, both the real
0
padding and the extra block. O outputs M Deck (C||cr ) = Mb .
5. A outputs 1 if M 0 = M1 or 0 if M 0 = M0 .
This attack works for any number of extra blocks (at least 1), and the ad-
versary wins with probability 1. This proves that MTProto does not have
indistinguishability of encryptions under chosen-ciphertext attack. The same
attack can be used to win the INT-CTXT game, just substitute the two mes-
sages M0 , M1 with a single message M and the challenge ciphertext C will
be the encryption of M. Again, the ciphertext C 0 is dierent from C , so it
48
4.1. RANDOM PADDING VULNERABILITY
Figure 4.2: Modifying the ciphertext by extending the padding will not be detected upon
decryption.
AE IND-CPA + INT-CTXT
Extending the message works ne, but it is in fact also possible to break
IND-CCA and INT-CTXT without changing the length, with non-negligible
probability.
49
4.1. RANDOM PADDING VULNERABILITY
C Enck (M ||r)
5. A outputs C 0.
This would be the best case scenario for the attack. However, when the
message String in Java is serialized, the result is a multiple of 4 bytes with
any unused bytes made 0. Take for example the string 'Test'. The rst byte
is the string length, 4. Next are the four bytes corresponding to each charac-
ter, followed by a message header from MTProto. We expect these to come
in succession, but we actually get a three 0 byte padding before the header.
1 Expected : 04 54 65 73 74 4 A 5 C 9 F 08
2 Reality : 04 54 65 73 74 00 00 00 4 A 5 C 9 F 08
As we see in gure 3.3, all data in the payload is a multiple of 4 bytes, there-
fore the total message encrypted is a multiple of 4 bytes, and as such it's
impossible to have a message length equal 1 mod 16 bytes. Instead, the best
case is to have a length equal 4 mod 16, with 12 bytes of padding instead of
8
15, and so the probability of nding a plaintext collision is reduced from 2
32 1
to 2 = 4294967296 , which is much less feasible.
The attack can be used to get the oracle to decrypt the challenge cipher-
text in the IND-CCA security game and will also decrypt to a valid plaintext
32
in the INT-CTXT game. As 2 is still non-negligible in the security pa-
rameter, we again conclude that MTProto does not have indistinguishability
of ciphertexts nor ciphertext integrity, and by extension not authenticated
encryption.
50
4.2. REPLAY AND MIRRORING ATTACKS IN OLDER VERSIONS
Do note however that the version negotiation is done after the key exchange
has taken place, and as such the server isn't able to actively lower the ne-
gotiated version. One of the clients will have to actually be at version 16 or
older for the chat to be vulnerable.
51
4.3. TIMING ATTACKS ON MTPROTO
The decryption process immediately aborts if any check fails, which led us to
believe that a timing attack might be possible. This was put to the test by
sending a message in a secret chat, but with the encrypted payload replaced
with random bits.
The result is that when any check fails, the message is simply not accepted
by the receiver and is forever marked as unread in the sending client, and the
sender does not get any notication about failure. As such, it is infeasible to
perform a remote timing attack on MTProto. However, if an attacker had
gotten a user to install a malicious application on their device, this could
gather information about when Telegram is running. But this is a whole
dierent topic, and we will not go into further detail with it.
52
Chapter 5
Experimental validation
In this chapter we will implement and verify the attacks proposed in chap-
ter 4. In order to verify these an MTProto simulator has been built, which
functions as a fake server between two users. This is built by extracting the
core functionality of the encrypt and decrypt methods from the Telegram for
Android source code, stripping them of all actual network connectivity, into
a simple Java program. Two fake users A and B are generated with random
user IDs, and a secret chat is set up with a randomly generated authorization
key, as opposed to running the Die-Hellman key exchange.
User A can then be instructed to encrypt a message, outputting the cipher-
text to the fake server. The fake server can apply any of the proposed attacks
before forwarding the modied ciphertext to user B. B will then use the de-
crypt method to verify that the message passes all checks and would have
been accepted in an ocial client, or reject if the message is corrupt.
A new byte array of size equal to the original ciphertext plus the extra blocks
is created, and the ciphertext is copied into it. Next an array of random bytes
is created and copied into the new array. The source code for this can be
seen in gure 5.1.
53
5.1. ATTACK #1: PADDING LENGTH EXTENSION
every case the modied ciphertext has been decrypted and accepted without
errors. This conrms the eectiveness of the attack, and if used as proposed
in section 4.1.1 this breaks IND-CCA security.
is.limit - 28 is the length of the payload contents with padding and len is
the length without padding. The padding should never be longer than 15
bytes, and so what we need to do is add is.limit() - 28 > len + 15.
This mitigates the padding length extension attack and as it is only a con-
ditional check, it has no impact on the encryption and decryption methods.
54
5.2. ATTACK #2: PADDING PLAINTEXT COLLISION
1 232 1
With a probability of success, there's a probability of failure. To
232 232
have a probability of .5 of nding a collision, one would have to do x attempts,
for x x
232 1 232 1
1 = 0.5 1 0.5 =
232 232
=2.977 109
One instance of the attack nished in 1.592 109 attempts, taking 5.8 hours
without any parallelization on a Intel Xeon E5-2680 v2 @ 2.80GHz. Compar-
55
5.2. ATTACK #2: PADDING PLAINTEXT COLLISION
ing plaintexts, the last block of the original message and the found collision
were
Other tests have nished in 1.606 109 to 7.109 109 attempts. This prob-
abilistic approach is trivially parallelizable as all attempts are fully indepen-
dent, but we will not implement optimizations in this work.
Now the padding is part of the msg _key , and thus has integrity. We will
56
5.3. MALICIOUS SERVER ATTACKS
Now we check the integrity of the padding, and the padding block plaintext
128
collision attack now has probability 2 of succeeding, which is negligibly
low in the length of an AES block.
Two users A and B communicate through the malicious server S . A's layer
is set to 16. The experiments are all based on S discarding or storing and
later forwarding messages, and the concepts of the four attacks are shown in
gure 5.3. Carrying out these attacks in our local test setup, we found that
in all cases the clients will accept messages and the attacks are successful.
57
5.3. MALICIOUS SERVER ATTACKS
A S B
Replay
C EncK (M ) C
B
C
B M DecK (C)
C
B M DecK (C)
Mirroring
C EncK (M ) C
B
M DecK (C) C
C
Withholding
C1 EncK (M1 ) C 1
B
C2 EncK (M2 ) C 2
B
C2
B M2 DecK (C2 )
Reordering
C1 EncK (M1 ) C1
B
C2 EncK (M2 ) C2 C2 M2 DecK (C2 )
B B
C1 M1 DecK (C1 )
B
58
Chapter 6
Known attacks
Recall that in IGE encryption, a plaintext block is XOR'd with the previous
ciphertext block prior to running the encryption function.
To win the BACPA security game, the adversary needs to nd a collision
between two blocks before they enter the encryption function, after they are
XOR'd with the precious ciphertexts. This way, the output ciphertext blocks
will be the same, just XOR'ed with a plaintext block that the adversary
already knows. Creating a collision is trivially accomplished due to the way
IGE mode does XOR before and after encryption. All the adversary has to
do is send two random message blocks to get their encryption, and then he
can compute a third block that will encrypt to the same as the second block,
causing a collision. This attack is also shown in gure 6.1.
59
6.1. KNOWN ATTACKS ON PRIMITIVES
n
1. A key K is generated by running Gen(1 ) and 1n is given to O and A.
O chooses two initial blocks m0 , c0 at random.
c1 m0 EncK (mb1 c0 )
6. A now computes
m03 m02 c1 c2
and chooses m13 6= m03 .
8. A now checks if
?
m01 m02 c3 = c2
If this is true then A knows b = 0, else b=1 and A has thus won the
game.
This attack wins the BACPA oracle game with probability 1. The reason for
the way m03 is chosen is that if b = 0, then:
If the result of the above is not c2 then we can say for sure that b = 1.
60
6.1. KNOWN ATTACKS ON PRIMITIVES
A O
1n n
C K Gen(1 )
m01 {0, 1}128
m11 m01 m01 , m11 b {0, 1}
B
m0 , c0 {0, 1}128
c1 c1 m0 EncK (mb1 c0 )
C
m02 {0, 1}128
m12 m02 m02 , m12
B
c2 mb1 EncK (mb2 c1 )
c2
C
m03 m02 c1 c2
m13 {0, 1}128 , m13 6= m03 m03 , m13
B
c3 mb2 EncK (mb3 c2 )
c3
C
if c2 = m01 m02 c3 then
b0 = 1
else
0
b =0
b0
B
61
6.1. KNOWN ATTACKS ON PRIMITIVES
This means systems using IGE do not even have indistinguishable of encryp-
tions under (blockwise-adaptive) chosen-plaintext attack, one of the weaker
notions, and as only a single query was made for which m1i 6= m0i it holds
for both the general and primitive notion. But as Telegram points out [26],
it does not apply to MTProto. This is because the AES key and IV are
dependent on the message content, which has to be xed prior to encryption.
Any blockwise-adaptive attack is impossible because one cannot change a
single block without changing the encryption key.
60 1
The short analysis, based on Stevens' 2 attack , assumes the cost of nding
74 14
a collision is 2 cycles as each SHA1 operation requires 2 cycles.
Jesse estimates that a modern server with 4 processors, each with 8 cores, is
61
able to carry out 2 cycles per year. Taking Moore's law into account, that
63 65
number should have increased to 2 by this year 2015, 2 by 2018, and so on.
In 2012 it would have cost $2.77 million to carry out this attack on an Ama-
zon rental server. Assuming the rental price is xed as the server hardware
gets upgraded, one could nd a collision for $700k in 2015, $173k by 2018,
and $43k by 2021. These lower costs are within reach of organized criminals
and university research projects.
Looking at Amazon's oerings shows that these estimates are not entirely
accurate. Jesse most likely looked at what is called the m1.small [2] which
indeed costs $0.04 per hour or $350 per year. This only oers a single core
and not four 8-core CPUs. Since then Amazon has upgraded to the newer
62
6.1. KNOWN ATTACKS ON PRIMITIVES
Intel Sandy Bridge architecture and the price remains comparable, but it
is not clear how big the performance gain is and hard to conrm the 2015
prediction.
590 24 24
= 339.84 kWh
1000
which at $0.12 per kWh [20] comes out to about $40 per collision (plus $370
for the hardware).
Bear in mind this is for computing SHA256 hashes, not SHA1, but as SHA1
is less computationally intensive it would presumably be cheaper to build
specialized hardware for computing it. This is shockingly cheap and could
be aorded by anyone, and we might as well assume that a SHA1 collision
could be trivially found. And let us have a look at the consequences if this
was the case.
63
6.1. KNOWN ATTACKS ON PRIMITIVES
Finding a collision for the 128 bit shared secret visualization itself will
not yield anything as the attacker is not in control of the SHA1 input.
Instead, the collision has to come from the secret values chosen during
the key exchange, and as such no known collision attack will help the
adversary here. Succeeding with this however leads to a perfect man-
in-the-middle attack, as we will describe in 6.2.3.
And so we see that there is no case where nding a regular SHA1 hash colli-
sion or pre-image can fool the protocol, either the found collision will contain
too little information to be of use, or SHA1 is used in combination with an-
other method that mitigates known attacks.
64
6.2. KNOWN ATTACKS ON MTPROTO
A S B
g, p g, p
C B
gA g a mod p g
A
B
gS gS g s mod p gS
C B
Bnonce {0, 1}2048 Bnonce gB g b mod p
B
gSB gSb mod p
gSB gBs mod p gB gAB gSB Bnonce
C
gAB gSB Bnonce
gAS gAs mod p
gAS gSa mod p Anonce Anonce gAS gAB
C
gAB gAS Anonce
In the last step we see A compute the shared secret as gAB gAS Anonce ,
and from the way Anonce is chosen this is equivalent to
gAB
gAS gAB
gAS
The attack is only possible under the assumption that users will send their
public DH value to the server before it gives them the nonce. This way the
65
6.2. KNOWN ATTACKS ON MTPROTO
server can trivially obtain the shared secret for a session, and gain access
to all sent messages. This is a very serious aw that Telegram claims to
have introduced by mistake, and although it has since been removed from
the protocol it has sparked suspicion that the authors of Telegram may have
intentionally backdoored the protocol to always have a way of accessing end-
to-end encrypted chats.
A E B
ga g a mod p ga ge g e mod p ge
B B
gb g b mod p
ge gb
C C
KAE gae mod p
KAE gea mod p KEB gbe mod p KEB geb mod p
The attack will be detected if the users compare key ngerprints like the one
seen in gure 3.2. This can be accomplished by the malicious client used by
A receiving the key ngerprint of KEB from the middle man, and displaying
this instead of the one computed from KAE . But in this case the malicious
client might as well just forward all the decrypted messages to the middle
man, and bypass security altogether.
This attack is mitigated by users not using a client from an untrusted source,
and verifying key ngerprints as intended.
66
6.2. KNOWN ATTACKS ON MTPROTO
The naive undetected approach is shown in gure 6.4. When user A ini-
tiates a secret chat with user B, and this request ga is forwarded through a
third userE , the best E can do is to send his own request ge1 to B . When B
responds with gb , B and E have agreed on a key KEB . E now has to come
up with a reply ge2 to A for which the key KAE will have the same ngerprint
128 127
as KEB . This requires 2 operations, or 2 to have a 50% probability of
nding a collision.
A E B
a
ga g mod p ga ge1 g e1
mod p ge1
B B
gb g b mod p
ge2 ge2 g e2 mod p gb
C C
The article assumes the adversary is capable of having A and B both ini-
tiate a secret chat at the same time, by means of social engineering. The
signicance of this is that now E hasn't committed to the value e1 yet, and
can freely choose both e1 and e2 , as shown in gure 6.5. This allows for
the adversary to do a birthday attack, reducing the complexity of nding an
auth_key collision to its square-root, 2128 = 264 . This needs to be done for
both e1 and e2 , and in total the cost of the attack is 264 +264 = 265 operations
to have a 50% probability of nding a collision.
We can also think of this as increasing the required security from the weaker
second pre-image resistance notion to the stronger collision resistance no-
tion, as described in section 2.4.3.
One might argue that this attack is ineective if the adversary lacks com-
puting power, as the two users will have to wait for him to nd the collision
before they can begin the conversation. Using the AntMiner S5 as we looked
at previously, this would take months. But if say the whole Bitcoin min-
ing community collaborated, this attack could be carried out mere seconds,
ignoring the cost of the Die-Hellman computations.
67
6.2. KNOWN ATTACKS ON MTPROTO
A E B
a
ga g mod p ga gb b
gb g mod p
B C
ge1 g e1 mod p
ge1 ge2 g e2 mod p ge2
C B
Figure 6.5: Having the freedom to choose e1 , e2 simultaneously, only the square root of
the number of operations is required.
68
Chapter 7
Proven crypto alternative
7.1 TextSecure
TextSecure (or Signal as the iOS avor is named) is a secure messaging
application for Android that uses state-of-the-art methods and makes no
compromise in security over performance. Its developer WhisperSystems is
even endorsed by Edward Snowden [10]! We will give a brief introduction to
the primitives it uses:
where H is any hash function with input block size n, K is a secret key padded
with zeroes to get length n, opad is a string of the byte 0x36 repeated to get
length n and ipad is the byte 0x5C repeated to get length n.
69
7.1. TEXTSECURE
In this case the hash function used is SHA256, a variant of the successor
to SHA1. This has input block size 512 bits and output size 256 bits, and
unlike SHA1 it is not cryptographically broken. The construction is shown
in gure 7.1, the output being hash sum 2.
70
7.1. TEXTSECURE
rootKey: the shared secret master key, used together with an ephemeral
key from each user to derive new shared rootKey and chainKey.
TextSecure only generates one messageKey per chainKey, and provides per-
message forward secrecy.
7.1.1 Registration
A new user registers his phone number and a username to the TextSecure
server. Along with this he generates a DH identity key pair and one or more
(ephemeral) prekey pairs using Curve25519. He sends the public half of the
identity and the prekeys to the server.
When one user wants to start messaging another, they perform a key ex-
change. User A wants to talk to user B . A is already registered to the server
and asks the server for B 's identity and one of his public prekeys.
The server sends A the public identity key of B , Bid1 , and one of his public
prekeys Bpk1 . A now generates a new ephemeral key pair (Apk0 , Apk1 ), and
from these keys he can compute the shared secrets rootKey and chainKey
using a triple DH:
71
7.1. TEXTSECURE
Furthermore, both the users now have the public identity key of one an-
other and these are used to authenticate each other through an out-of-band
channel that is hard to impersonate such as a phone call, or even better in
person.
prekey: an ephemeral public key half used to derive the next shared
rootKey .
counter: 32-bit counter increasing for every message sent under the
same ephemeralKey.
72
7.2. CONCLUSION
mac: HMAC-SHA256 of the above elds using MAC key derived from
Axolotl Ratchet, and truncated to 64 bits.
7.2 Conclusion
In this work we have shown that Telegram, with its use of aging primitives,
does not manage to provide data integrity of ciphertexts nor authenticated
encryption, and is vulnerable to chosen-ciphertext attacks. The attempt to
mitigate known attacks has introduced new vulnerabilities, and we suggest
73
7.2. CONCLUSION
that the Telegram team updates its protocol to use strong, modern primi-
tives. For message authentication codes it should use a good HMAC, use a
proper key derivation function, and update the key exchange to use elliptic
curve Die-Hellman based on Curve25519. Telegram has a great emphasis
on computational performance of its protocol, which is why CTR with its
parallelization seems to be the logical choice of encryption mode. We suggest
using CTR instead of IGE mode, as IGE oers no benets over CTR.
Overall, we can conclude yet again that homegrown cryptography is a bad
approach.
74
Bibliography
https://
[8] Internet Engineering Task Force. PKCS #1: RSA Encryption.
tools.ietf.org/html/rfc2313, 1998. [Online; accessed 10-September-
2015]. (page 33)
75
BIBLIOGRAPHY
[12] Philipp Jovanovic and Samuel Neves. Dumb crypto in smart grids: Prac-
tical cryptanalysis of the open smart grid protocol. Cryptology ePrint
Archive, Report 2015/428, 2015. http://eprint.iacr.org/. (page 73)
76
BIBLIOGRAPHY
[21] Marc Stevens. New collision attacks on sha-1 based on optimal joint
local-collision analysis. In Advances in CryptologyEUROCRYPT 2013,
pages 245261. Springer, 2013. (page 62)
[30] Jesus Diaz Vico. Telegram: bypassing the authentication protocol. 2014.
(page 66)
77
BIBLIOGRAPHY
78