mtproto_auth_key.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "mtproto/mtproto_auth_key.h"
  8. #include "base/openssl_help.h"
  9. #include <QtCore/QDataStream>
  10. namespace MTP {
  11. AuthKey::AuthKey(Type type, DcId dcId, const Data &data)
  12. : _type(type)
  13. , _dcId(dcId)
  14. , _key(data) {
  15. countKeyId();
  16. if (type == Type::Generated || type == Type::Temporary) {
  17. _creationTime = crl::now();
  18. }
  19. }
  20. AuthKey::AuthKey(const Data &data) : _type(Type::Local), _key(data) {
  21. countKeyId();
  22. }
  23. AuthKey::Type AuthKey::type() const {
  24. return _type;
  25. }
  26. int AuthKey::dcId() const {
  27. return _dcId;
  28. }
  29. AuthKey::KeyId AuthKey::keyId() const {
  30. return _keyId;
  31. }
  32. void AuthKey::prepareAES_oldmtp(const MTPint128 &msgKey, MTPint256 &aesKey, MTPint256 &aesIV, bool send) const {
  33. uint32 x = send ? 0 : 8;
  34. bytes::array<20> sha1_a, sha1_b, sha1_c, sha1_d;
  35. bytes::array<16 + 32> data_a;
  36. memcpy(data_a.data(), &msgKey, 16);
  37. memcpy(data_a.data() + 16, _key.data() + x, 32);
  38. openssl::Sha1To(sha1_a, data_a);
  39. bytes::array<16 + 16 + 16> data_b;
  40. memcpy(data_b.data(), _key.data() + 32 + x, 16);
  41. memcpy(data_b.data() + 16, &msgKey, 16);
  42. memcpy(data_b.data() + 32, _key.data() + 48 + x, 16);
  43. openssl::Sha1To(sha1_b, data_b);
  44. bytes::array<32 + 16> data_c;
  45. memcpy(data_c.data(), _key.data() + 64 + x, 32);
  46. memcpy(data_c.data() + 32, &msgKey, 16);
  47. openssl::Sha1To(sha1_c, data_c);
  48. bytes::array<16 + 32> data_d;
  49. memcpy(data_d.data(), &msgKey, 16);
  50. memcpy(data_d.data() + 16, _key.data() + 96 + x, 32);
  51. openssl::Sha1To(sha1_d, data_d);
  52. auto key = reinterpret_cast<bytes::type*>(&aesKey);
  53. auto iv = reinterpret_cast<bytes::type*>(&aesIV);
  54. memcpy(key, sha1_a.data(), 8);
  55. memcpy(key + 8, sha1_b.data() + 8, 12);
  56. memcpy(key + 8 + 12, sha1_c.data() + 4, 12);
  57. memcpy(iv, sha1_a.data() + 8, 12);
  58. memcpy(iv + 12, sha1_b.data(), 8);
  59. memcpy(iv + 12 + 8, sha1_c.data() + 16, 4);
  60. memcpy(iv + 12 + 8 + 4, sha1_d.data(), 8);
  61. }
  62. void AuthKey::prepareAES(const MTPint128 &msgKey, MTPint256 &aesKey, MTPint256 &aesIV, bool send) const {
  63. uint32 x = send ? 0 : 8;
  64. bytes::array<32> sha256_a, sha256_b;
  65. bytes::array<16 + 36> data_a;
  66. memcpy(data_a.data(), &msgKey, 16);
  67. memcpy(data_a.data() + 16, _key.data() + x, 36);
  68. openssl::Sha256To(sha256_a, data_a);
  69. bytes::array<36 + 16> data_b;
  70. memcpy(data_b.data(), _key.data() + 40 + x, 36);
  71. memcpy(data_b.data() + 36, &msgKey, 16);
  72. openssl::Sha256To(sha256_b, data_b);
  73. auto key = reinterpret_cast<uchar*>(&aesKey);
  74. auto iv = reinterpret_cast<uchar*>(&aesIV);
  75. memcpy(key, sha256_a.data(), 8);
  76. memcpy(key + 8, sha256_b.data() + 8, 16);
  77. memcpy(key + 8 + 16, sha256_a.data() + 24, 8);
  78. memcpy(iv, sha256_b.data(), 8);
  79. memcpy(iv + 8, sha256_a.data() + 8, 16);
  80. memcpy(iv + 8 + 16, sha256_b.data() + 24, 8);
  81. }
  82. const void *AuthKey::partForMsgKey(bool send) const {
  83. return _key.data() + 88 + (send ? 0 : 8);
  84. }
  85. void AuthKey::write(QDataStream &to) const {
  86. to.writeRawData(reinterpret_cast<const char*>(_key.data()), _key.size());
  87. }
  88. bytes::const_span AuthKey::data() const {
  89. return _key;
  90. }
  91. bool AuthKey::equals(const std::shared_ptr<AuthKey> &other) const {
  92. return other ? (_key == other->_key) : false;
  93. }
  94. crl::time AuthKey::creationTime() const {
  95. return _creationTime;
  96. }
  97. TimeId AuthKey::expiresAt() const {
  98. return _expiresAt;
  99. }
  100. void AuthKey::setExpiresAt(TimeId expiresAt) {
  101. Expects(_type == Type::Temporary);
  102. _expiresAt = expiresAt;
  103. }
  104. void AuthKey::FillData(Data &authKey, bytes::const_span computedAuthKey) {
  105. auto computedAuthKeySize = computedAuthKey.size();
  106. Assert(computedAuthKeySize <= kSize);
  107. auto authKeyBytes = gsl::make_span(authKey);
  108. if (computedAuthKeySize < kSize) {
  109. bytes::set_with_const(authKeyBytes.subspan(0, kSize - computedAuthKeySize), gsl::byte());
  110. bytes::copy(authKeyBytes.subspan(kSize - computedAuthKeySize), computedAuthKey);
  111. } else {
  112. bytes::copy(authKeyBytes, computedAuthKey);
  113. }
  114. }
  115. void AuthKey::countKeyId() {
  116. const auto hash = openssl::Sha1(_key);
  117. // Lower 64 bits = 8 bytes of 20 byte SHA1 hash.
  118. _keyId = *reinterpret_cast<const KeyId*>(hash.data() + 12);
  119. }
  120. void aesIgeEncryptRaw(const void *src, void *dst, uint32 len, const void *key, const void *iv) {
  121. uchar aes_key[32], aes_iv[32];
  122. memcpy(aes_key, key, 32);
  123. memcpy(aes_iv, iv, 32);
  124. AES_KEY aes;
  125. AES_set_encrypt_key(aes_key, 256, &aes);
  126. AES_ige_encrypt(static_cast<const uchar*>(src), static_cast<uchar*>(dst), len, &aes, aes_iv, AES_ENCRYPT);
  127. }
  128. void aesIgeDecryptRaw(const void *src, void *dst, uint32 len, const void *key, const void *iv) {
  129. uchar aes_key[32], aes_iv[32];
  130. memcpy(aes_key, key, 32);
  131. memcpy(aes_iv, iv, 32);
  132. AES_KEY aes;
  133. AES_set_decrypt_key(aes_key, 256, &aes);
  134. AES_ige_encrypt(static_cast<const uchar*>(src), static_cast<uchar*>(dst), len, &aes, aes_iv, AES_DECRYPT);
  135. }
  136. void aesCtrEncrypt(bytes::span data, const void *key, CTRState *state) {
  137. AES_KEY aes;
  138. AES_set_encrypt_key(static_cast<const uchar*>(key), 256, &aes);
  139. static_assert(CTRState::IvecSize == AES_BLOCK_SIZE, "Wrong size of ctr ivec!");
  140. static_assert(CTRState::EcountSize == AES_BLOCK_SIZE, "Wrong size of ctr ecount!");
  141. CRYPTO_ctr128_encrypt(
  142. reinterpret_cast<const uchar*>(data.data()),
  143. reinterpret_cast<uchar*>(data.data()),
  144. data.size(),
  145. &aes,
  146. state->ivec,
  147. state->ecount,
  148. &state->num,
  149. (block128_f)AES_encrypt);
  150. }
  151. } // namespace MTP