2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-02-22 15:18:26 +00:00
|
|
|
#include <array>
|
|
|
|
#include <memory>
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
namespace MTP {
|
|
|
|
|
|
|
|
class AuthKey {
|
2014-05-30 08:53:19 +00:00
|
|
|
public:
|
2017-02-22 15:18:26 +00:00
|
|
|
static constexpr auto kSize = 256; // 2048 bits.
|
2017-04-24 12:16:38 +00:00
|
|
|
using Data = std::array<gsl::byte, kSize>;
|
2017-02-25 16:44:02 +00:00
|
|
|
using KeyId = uint64;
|
|
|
|
|
|
|
|
enum class Type {
|
|
|
|
Generated,
|
|
|
|
ReadFromFile,
|
|
|
|
Local,
|
|
|
|
};
|
|
|
|
AuthKey(Type type, DcId dcId, const Data &data) : _type(type), _dcId(dcId), _key(data) {
|
|
|
|
countKeyId();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-02-25 16:44:02 +00:00
|
|
|
AuthKey(const Data &data) : _type(Type::Local), _key(data) {
|
|
|
|
countKeyId();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
AuthKey(const AuthKey &other) = delete;
|
|
|
|
AuthKey &operator=(const AuthKey &other) = delete;
|
|
|
|
|
|
|
|
Type type() const {
|
|
|
|
return _type;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
int dcId() const {
|
|
|
|
return _dcId;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
KeyId keyId() const {
|
2014-05-30 08:53:19 +00:00
|
|
|
return _keyId;
|
|
|
|
}
|
|
|
|
|
2016-12-31 08:58:56 +00:00
|
|
|
void prepareAES_oldmtp(const MTPint128 &msgKey, MTPint256 &aesKey, MTPint256 &aesIV, bool send) const;
|
|
|
|
void prepareAES(const MTPint128 &msgKey, MTPint256 &aesKey, MTPint256 &aesIV, bool send) const;
|
|
|
|
|
|
|
|
const void *partForMsgKey(bool send) const {
|
|
|
|
return _key.data() + 88 + (send ? 0 : 8);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void write(QDataStream &to) const {
|
2017-04-24 12:16:38 +00:00
|
|
|
to.writeRawData(reinterpret_cast<const char*>(_key.data()), _key.size());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
bool equals(const std::shared_ptr<AuthKey> &other) const {
|
|
|
|
return other ? (_key == other->_key) : false;
|
|
|
|
}
|
2015-03-02 12:34:16 +00:00
|
|
|
|
2017-04-24 12:16:38 +00:00
|
|
|
static void FillData(Data &authKey, base::const_byte_span computedAuthKey) {
|
|
|
|
auto computedAuthKeySize = computedAuthKey.size();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(computedAuthKeySize <= kSize);
|
2017-04-24 12:16:38 +00:00
|
|
|
auto authKeyBytes = gsl::make_span(authKey);
|
|
|
|
if (computedAuthKeySize < kSize) {
|
|
|
|
base::set_bytes(authKeyBytes.subspan(0, kSize - computedAuthKeySize), gsl::byte());
|
|
|
|
base::copy_bytes(authKeyBytes.subspan(kSize - computedAuthKeySize), computedAuthKey);
|
|
|
|
} else {
|
|
|
|
base::copy_bytes(authKeyBytes, computedAuthKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
private:
|
2017-02-25 16:44:02 +00:00
|
|
|
void countKeyId() {
|
|
|
|
auto sha1 = hashSha1(_key.data(), _key.size());
|
|
|
|
|
|
|
|
// Lower 64 bits = 8 bytes of 20 byte SHA1 hash.
|
|
|
|
_keyId = *reinterpret_cast<KeyId*>(sha1.data() + 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
Type _type = Type::Generated;
|
|
|
|
DcId _dcId = 0;
|
2017-04-24 12:16:38 +00:00
|
|
|
Data _key = { { gsl::byte{} } };
|
2017-02-25 16:44:02 +00:00
|
|
|
KeyId _keyId = 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-02-22 15:18:26 +00:00
|
|
|
using AuthKeyPtr = std::shared_ptr<AuthKey>;
|
2017-02-25 16:44:02 +00:00
|
|
|
using AuthKeysList = std::vector<AuthKeyPtr>;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-12-31 08:58:56 +00:00
|
|
|
void aesIgeEncryptRaw(const void *src, void *dst, uint32 len, const void *key, const void *iv);
|
|
|
|
void aesIgeDecryptRaw(const void *src, void *dst, uint32 len, const void *key, const void *iv);
|
|
|
|
|
|
|
|
inline void aesIgeEncrypt_oldmtp(const void *src, void *dst, uint32 len, const AuthKeyPtr &authKey, const MTPint128 &msgKey) {
|
|
|
|
MTPint256 aesKey, aesIV;
|
|
|
|
authKey->prepareAES_oldmtp(msgKey, aesKey, aesIV, true);
|
|
|
|
|
|
|
|
return aesIgeEncryptRaw(src, dst, len, static_cast<const void*>(&aesKey), static_cast<const void*>(&aesIV));
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
inline void aesIgeEncrypt(const void *src, void *dst, uint32 len, const AuthKeyPtr &authKey, const MTPint128 &msgKey) {
|
2014-05-30 08:53:19 +00:00
|
|
|
MTPint256 aesKey, aesIV;
|
2016-12-31 08:58:56 +00:00
|
|
|
authKey->prepareAES(msgKey, aesKey, aesIV, true);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-12-31 08:58:56 +00:00
|
|
|
return aesIgeEncryptRaw(src, dst, len, static_cast<const void*>(&aesKey), static_cast<const void*>(&aesIV));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
inline void aesEncryptLocal(const void *src, void *dst, uint32 len, const AuthKeyPtr &authKey, const void *key128) {
|
2014-05-30 08:53:19 +00:00
|
|
|
MTPint256 aesKey, aesIV;
|
2016-12-31 08:58:56 +00:00
|
|
|
authKey->prepareAES_oldmtp(*(const MTPint128*)key128, aesKey, aesIV, false);
|
|
|
|
|
|
|
|
return aesIgeEncryptRaw(src, dst, len, static_cast<const void*>(&aesKey), static_cast<const void*>(&aesIV));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void aesIgeDecrypt_oldmtp(const void *src, void *dst, uint32 len, const AuthKeyPtr &authKey, const MTPint128 &msgKey) {
|
|
|
|
MTPint256 aesKey, aesIV;
|
|
|
|
authKey->prepareAES_oldmtp(msgKey, aesKey, aesIV, false);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-12-31 08:58:56 +00:00
|
|
|
return aesIgeDecryptRaw(src, dst, len, static_cast<const void*>(&aesKey), static_cast<const void*>(&aesIV));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
inline void aesIgeDecrypt(const void *src, void *dst, uint32 len, const AuthKeyPtr &authKey, const MTPint128 &msgKey) {
|
2014-05-30 08:53:19 +00:00
|
|
|
MTPint256 aesKey, aesIV;
|
|
|
|
authKey->prepareAES(msgKey, aesKey, aesIV, false);
|
|
|
|
|
2016-12-31 08:58:56 +00:00
|
|
|
return aesIgeDecryptRaw(src, dst, len, static_cast<const void*>(&aesKey), static_cast<const void*>(&aesIV));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
inline void aesDecryptLocal(const void *src, void *dst, uint32 len, const AuthKeyPtr &authKey, const void *key128) {
|
2014-05-30 08:53:19 +00:00
|
|
|
MTPint256 aesKey, aesIV;
|
2016-12-31 08:58:56 +00:00
|
|
|
authKey->prepareAES_oldmtp(*(const MTPint128*)key128, aesKey, aesIV, false);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-12-31 08:58:56 +00:00
|
|
|
return aesIgeDecryptRaw(src, dst, len, static_cast<const void*>(&aesKey), static_cast<const void*>(&aesIV));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
// ctr used inplace, encrypt the data and leave it at the same place
|
|
|
|
struct CTRState {
|
|
|
|
static constexpr int KeySize = 32;
|
|
|
|
static constexpr int IvecSize = 16;
|
|
|
|
static constexpr int EcountSize = 16;
|
|
|
|
|
|
|
|
uchar ivec[IvecSize] = { 0 };
|
|
|
|
uint32 num = 0;
|
|
|
|
uchar ecount[EcountSize] = { 0 };
|
|
|
|
};
|
|
|
|
void aesCtrEncrypt(void *data, uint32 len, const void *key, CTRState *state);
|
|
|
|
|
|
|
|
} // namespace MTP
|