mtproto v57.1.0-alpha MTProto.Crypto

Summary

Functions

auth_key_hash is computed := 64 lower-order bits of SHA1(auth_key)

sha1_a = SHA1(msg_key + substr(auth_key, x, 32)); sha1_b = SHA1(substr(auth_key, 32+x, 16) + msg_key + substr(auth_key, 48+x, 16)); sha1_с = SHA1(substr(auth_key, 64+x, 32) + msg_key); sha1_d = SHA1(msg_key + substr(auth_key, 96+x, 32)); aes_key = substr(sha1_a, 0, 8) + substr(sha1_b, 8, 12) + substr(sha1_c, 4, 12); aes_iv = substr(sha1_a, 8, 12) + substr(sha1_b, 0, 8) + substr(sha1_c, 16, 4) + substr(sha1_d, 0, 8);

new_nonce_hash1, new_nonce_hash2, and new_nonce_hash3 are obtained as the 128 lower-order bits of SHA1 of the byte string derived from the new_nonce string by adding a single byte with the value of 1, 2, or 3, and followed by another 8 bytes with auth_key_aux_hash

server_salt := substr(new_nonce, 0, 8) XOR substr(server_nonce, 0, 8)

Functions

auth_key_hash(auth_key)

auth_key_hash is computed := 64 lower-order bits of SHA1(auth_key).

The server checks whether there already is another key with the same auth_key_hash and responds in one of the following ways.

client_dh_inner_data_encrypt(tmp_aes_key, tmp_aes_iv, data)
decrypt_aes_ige256(aes_key, aes_iv, encrypted)
decrypt_packet(packet, auth_key)
encrypt_aes_ige256(aes_key, aes_iv, plain)
encrypt_packet(packet, auth_key, auth_key_hash)
generate_aes(msg_key, auth_key, action)

sha1_a = SHA1(msg_key + substr(auth_key, x, 32)); sha1_b = SHA1(substr(auth_key, 32+x, 16) + msg_key + substr(auth_key, 48+x, 16)); sha1_с = SHA1(substr(auth_key, 64+x, 32) + msg_key); sha1_d = SHA1(msg_key + substr(auth_key, 96+x, 32)); aes_key = substr(sha1_a, 0, 8) + substr(sha1_b, 8, 12) + substr(sha1_c, 4, 12); aes_iv = substr(sha1_a, 8, 12) + substr(sha1_b, 0, 8) + substr(sha1_c, 16, 4) + substr(sha1_d, 0, 8);

make_nonce(size \\ 16)
make_nonce_hash1(new_nonce, auth_key)

new_nonce_hash1, new_nonce_hash2, and new_nonce_hash3 are obtained as the 128 lower-order bits of SHA1 of the byte string derived from the new_nonce string by adding a single byte with the value of 1, 2, or 3, and followed by another 8 bytes with auth_key_aux_hash.

Different values are required to prevent an intruder from changing server response dh_gen_ok into dh_gen_retry.

make_server_salt(new_nonce, server_nonce)

server_salt := substr(new_nonce, 0, 8) XOR substr(server_nonce, 0, 8)

https://core.telegram.org/mtproto/auth_key#dh-key-exchange-complete (9)

make_session_id()
p_q_inner_data_rsa(p_q_inner_data)
server_dh_params_decode(new_nonce, server_nonce, encrypted_answer)
sha1(data)