This is a translation of the original article by Scratch.
There is a lot of discussion about how secure the new Telegram Passport service, so lets talk about how the service encrypts your personal data and understand how is real End-To-End works.
How Telegram Passport works:
Lets analyse the first part, which deals with the encryption and storage of your personal data.
According to the developers understanding of End to End — Telegram cloud cannot decrypt your personal data.
Lets inspect the encrypting algorithm. It starts from the password when it turns into an intermediate encryption key:
bytes::vector CountPasswordHashForSecret(bytes::const_span salt,bytes::const_span password) {return openssl::Sha512(bytes::concatenate(salt,password,salt));}
The random salt is taken and concatenated twice with a password and then sent through the SHA-512 hash.
We are in 2k18! One good GPU can brute force a little more than a billion SHA-512 per second. 10 GPU will handle all possible combinations of 8-digit passwords in 80 days. One hundred will be managed in a week.
There are many ways to make life difficult for those who brute force passwords on the GPU, but the developers of Telegram decided not to bother themselves with its implementation.
Further, password hash encrypts another almost random key, which is generated like this:
bytes::vector GenerateSecretBytes() {auto result = bytes::vector(kSecretSize);memset_rand(result.data(), result.size());const auto full = ranges::accumulate(result,0ULL,[](uint64 sum, gsl::byte value) { return sum + uchar(value); });const auto mod = (full % 255ULL);const auto add = 255ULL + 239 - mod;auto first = (static_cast<uchar>(result[0]) + add) % 255ULL; result[0] = static_castgsl::byte(first);return result;}
It is “almost” random because Telegram developers have never heard of HMAC and AEAD and instead of using conventional tools to check the correctness of the decryption, they make sure that the remainder of the division of the byte sum of the key is equal to 239, which verified on a decryption:
bool CheckBytesMod255(bytes::const_span bytes) {const auto full = ranges::accumulate(bytes,0ULL,[](uint64 sum, gsl::byte value) { return sum + uchar(value); });const auto mod = (full % 255ULL);return (mod == 239);}
This array of bytes is not so random. It will be many false positives on a brute force. However, to calculate the amount of bytes after decoding is much simpler than HMAC, so basically this design is accelerating the brute force.
Now lets observe the data encrypting method:
EncryptedData EncryptData(bytes::const_span bytes,bytes::const_span dataSecret) {constexpr auto kFromPadding = kMinPadding + kAlignTo - 1;constexpr auto kPaddingDelta = kMaxPadding - kFromPadding;const auto randomPadding = kFromPadding+ (rand_value<uint32>() % kPaddingDelta);const auto padding = randomPadding- ((bytes.size() + randomPadding) % kAlignTo);Assert(padding >= kMinPadding && padding <= kMaxPadding);
auto unencrypted = bytes::vector(padding + bytes.size());
Assert(unencrypted.size() % kAlignTo == 0);
unencrypted\[0\] = static\_cast<gsl::byte>(padding);
memset\_rand(unencrypted.data() + 1, padding - 1);
bytes::copy(
gsl::make\_span(unencrypted).subspan(padding),
bytes);
Data is appended from 32 to 255 random bytes. It developed to diversifyvariable dataHash. This is a hash of unencrypted data mixed with random bytes.
const auto dataHash = openssl::Sha256(unencrypted);
const auto bytesForEncryptionKey = bytes::concatenate(
dataSecret,
dataHash);
auto params = PrepareAesParams(bytesForEncryptionKey);
return {
{ dataSecret.begin(), dataSecret.end() },
{ dataHash.begin(), dataHash.end() },
Encrypt(unencrypted, std::move(params))
};
}
The personal data encryption key is generated. It’s obtained with another SHA-512 call from the almost random generated key, which is concatenated with the dataHash.
The cloud transmits:
This is definitely not a “random noise”, there is everything, including the password-protected encryption key, which allows you to get the user data much, much faster than brute forcing all possible combinations of AES keys (2 ^ 256).
Much doubt about key validation mechanisms invented by Telegram team instead of HMAC.
An example of a brute force algorithm:
if (padding < kMinPadding|| padding > kMaxPadding|| padding > decrypted.size()) {
The first byte of the decrypted text always be “{“ or “[“, because of the JSON use.
We figured out that the encryption of personal data critically depends on the complexity of the password. All stages of a brute force perfectly accelerated by hardware, either using a GPU or AES-NI instructions.
Of course, you can set a long secure password and hope it’ll do the job. But how do you think, what percentage of the two hundred million Telegram users will make their passwords longer than eight characters?
In addition the dubious techniques of generating keys and verifying the validity of the decrypted information which do not use standard proven mechanisms, directly violate the ”Do not roll your own crypto” principle, it becomes clear that this is not an End-to-End.
E2E is called so, because it allows you to show third parties encrypted data without fear for their safety. As you can see, this condition is not fulfilled by Telegram Passport.
Thank you for attention.