mtproto_auth_key.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #pragma once
  8. #include "base/bytes.h"
  9. #include <array>
  10. #include <memory>
  11. namespace MTP {
  12. class AuthKey {
  13. public:
  14. static constexpr auto kSize = 256; // 2048 bits.
  15. using Data = std::array<gsl::byte, kSize>;
  16. using KeyId = uint64;
  17. enum class Type {
  18. Generated,
  19. Temporary,
  20. ReadFromFile,
  21. Local,
  22. };
  23. AuthKey(Type type, DcId dcId, const Data &data);
  24. explicit AuthKey(const Data &data);
  25. AuthKey(const AuthKey &other) = delete;
  26. AuthKey &operator=(const AuthKey &other) = delete;
  27. [[nodiscard]] Type type() const;
  28. [[nodiscard]] int dcId() const;
  29. [[nodiscard]] KeyId keyId() const;
  30. void prepareAES_oldmtp(const MTPint128 &msgKey, MTPint256 &aesKey, MTPint256 &aesIV, bool send) const;
  31. void prepareAES(const MTPint128 &msgKey, MTPint256 &aesKey, MTPint256 &aesIV, bool send) const;
  32. [[nodiscard]] const void *partForMsgKey(bool send) const;
  33. void write(QDataStream &to) const;
  34. [[nodiscard]] bytes::const_span data() const;
  35. [[nodiscard]] bool equals(const std::shared_ptr<AuthKey> &other) const;
  36. [[nodiscard]] crl::time creationTime() const; // > 0 if known.
  37. [[nodiscard]] TimeId expiresAt() const;
  38. void setExpiresAt(TimeId expiresAt);
  39. static void FillData(Data &authKey, bytes::const_span computedAuthKey);
  40. private:
  41. void countKeyId();
  42. Type _type = Type::Generated;
  43. DcId _dcId = 0;
  44. Data _key = { { gsl::byte{} } };
  45. KeyId _keyId = 0;
  46. crl::time _creationTime = 0;
  47. TimeId _expiresAt = 0;
  48. };
  49. using AuthKeyPtr = std::shared_ptr<AuthKey>;
  50. using AuthKeysList = std::vector<AuthKeyPtr>;
  51. void aesIgeEncryptRaw(const void *src, void *dst, uint32 len, const void *key, const void *iv);
  52. void aesIgeDecryptRaw(const void *src, void *dst, uint32 len, const void *key, const void *iv);
  53. inline void aesIgeEncrypt_oldmtp(const void *src, void *dst, uint32 len, const AuthKeyPtr &authKey, const MTPint128 &msgKey) {
  54. MTPint256 aesKey, aesIV;
  55. authKey->prepareAES_oldmtp(msgKey, aesKey, aesIV, true);
  56. return aesIgeEncryptRaw(src, dst, len, static_cast<const void*>(&aesKey), static_cast<const void*>(&aesIV));
  57. }
  58. inline void aesIgeEncrypt(const void *src, void *dst, uint32 len, const AuthKeyPtr &authKey, const MTPint128 &msgKey) {
  59. MTPint256 aesKey, aesIV;
  60. authKey->prepareAES(msgKey, aesKey, aesIV, true);
  61. return aesIgeEncryptRaw(src, dst, len, static_cast<const void*>(&aesKey), static_cast<const void*>(&aesIV));
  62. }
  63. inline void aesEncryptLocal(const void *src, void *dst, uint32 len, const AuthKeyPtr &authKey, const void *key128) {
  64. MTPint256 aesKey, aesIV;
  65. authKey->prepareAES_oldmtp(*(const MTPint128*)key128, aesKey, aesIV, false);
  66. return aesIgeEncryptRaw(src, dst, len, static_cast<const void*>(&aesKey), static_cast<const void*>(&aesIV));
  67. }
  68. inline void aesIgeDecrypt_oldmtp(const void *src, void *dst, uint32 len, const AuthKeyPtr &authKey, const MTPint128 &msgKey) {
  69. MTPint256 aesKey, aesIV;
  70. authKey->prepareAES_oldmtp(msgKey, aesKey, aesIV, false);
  71. return aesIgeDecryptRaw(src, dst, len, static_cast<const void*>(&aesKey), static_cast<const void*>(&aesIV));
  72. }
  73. inline void aesIgeDecrypt(const void *src, void *dst, uint32 len, const AuthKeyPtr &authKey, const MTPint128 &msgKey) {
  74. MTPint256 aesKey, aesIV;
  75. authKey->prepareAES(msgKey, aesKey, aesIV, false);
  76. return aesIgeDecryptRaw(src, dst, len, static_cast<const void*>(&aesKey), static_cast<const void*>(&aesIV));
  77. }
  78. inline void aesDecryptLocal(const void *src, void *dst, uint32 len, const AuthKeyPtr &authKey, const void *key128) {
  79. MTPint256 aesKey, aesIV;
  80. authKey->prepareAES_oldmtp(*(const MTPint128*)key128, aesKey, aesIV, false);
  81. return aesIgeDecryptRaw(src, dst, len, static_cast<const void*>(&aesKey), static_cast<const void*>(&aesIV));
  82. }
  83. // ctr used inplace, encrypt the data and leave it at the same place
  84. struct CTRState {
  85. static constexpr int KeySize = 32;
  86. static constexpr int IvecSize = 16;
  87. static constexpr int EcountSize = 16;
  88. uchar ivec[IvecSize] = { 0 };
  89. uint32 num = 0;
  90. uchar ecount[EcountSize] = { 0 };
  91. };
  92. void aesCtrEncrypt(bytes::span data, const void *key, CTRState *state);
  93. } // namespace MTP