File: core_cloud_password.h

package info (click to toggle)
telegram-desktop 4.6.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 53,300 kB
  • sloc: cpp: 605,857; python: 3,978; ansic: 1,636; sh: 965; makefile: 841; objc: 652; javascript: 187; xml: 165
file content (144 lines) | stat: -rw-r--r-- 3,400 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.

For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once

#include "base/bytes.h"

namespace Core {

constexpr auto kHandleSrpIdInvalidTimeout = 60 * crl::time(1000);

struct CloudPasswordAlgoModPow {
	static constexpr auto kIterations = 100000;

	bytes::vector salt1;
	bytes::vector salt2;
	int g = 0;
	bytes::vector p;
};

inline bool operator==(
		const CloudPasswordAlgoModPow &a,
		const CloudPasswordAlgoModPow &b) {
	return (a.salt1 == b.salt1)
		&& (a.salt2 == b.salt2)
		&& (a.g == b.g)
		&& (a.p == b.p);
}

using CloudPasswordAlgo = std::variant<v::null_t, CloudPasswordAlgoModPow>;

CloudPasswordAlgo ParseCloudPasswordAlgo(const MTPPasswordKdfAlgo &data);
CloudPasswordAlgo ValidateNewCloudPasswordAlgo(CloudPasswordAlgo &&parsed);
MTPPasswordKdfAlgo PrepareCloudPasswordAlgo(const CloudPasswordAlgo &data);

struct CloudPasswordCheckRequest {
	uint64 id = 0;
	bytes::vector B;
	CloudPasswordAlgo algo;

	explicit operator bool() const {
		return !v::is_null(algo);
	}
};

inline bool operator==(
		const CloudPasswordCheckRequest &a,
		const CloudPasswordCheckRequest &b) {
	return (a.id == b.id) && (a.B == b.B) && (a.algo == b.algo);
}

inline bool operator!=(
		const CloudPasswordCheckRequest &a,
		const CloudPasswordCheckRequest &b) {
	return !(a == b);
}

CloudPasswordCheckRequest ParseCloudPasswordCheckRequest(
	const MTPDaccount_password &data);

struct CloudPasswordResult {
	MTPInputCheckPasswordSRP result;

	explicit operator bool() const;
};

struct CloudPasswordDigest {
	bytes::vector modpow;
};

bytes::vector ComputeCloudPasswordHash(
	const CloudPasswordAlgo &algo,
	bytes::const_span password);

CloudPasswordDigest ComputeCloudPasswordDigest(
	const CloudPasswordAlgo &algo,
	bytes::const_span password);

CloudPasswordResult ComputeCloudPasswordCheck(
	const CloudPasswordCheckRequest &request,
	bytes::const_span hash);

struct SecureSecretAlgoSHA512 {
	bytes::vector salt;
};

inline bool operator==(
		const SecureSecretAlgoSHA512 &a,
		const SecureSecretAlgoSHA512 &b) {
	return (a.salt == b.salt);
}

struct SecureSecretAlgoPBKDF2 {
	static constexpr auto kIterations = 100000;

	bytes::vector salt;
};

inline bool operator==(
		const SecureSecretAlgoPBKDF2 &a,
		const SecureSecretAlgoPBKDF2 &b) {
	return (a.salt == b.salt);
}

using SecureSecretAlgo = std::variant<
	v::null_t,
	SecureSecretAlgoSHA512,
	SecureSecretAlgoPBKDF2>;

SecureSecretAlgo ParseSecureSecretAlgo(
	const MTPSecurePasswordKdfAlgo &data);
SecureSecretAlgo ValidateNewSecureSecretAlgo(SecureSecretAlgo &&parsed);
MTPSecurePasswordKdfAlgo PrepareSecureSecretAlgo(
	const SecureSecretAlgo &data);

bytes::vector ComputeSecureSecretHash(
	const SecureSecretAlgo &algo,
	bytes::const_span password);

struct CloudPasswordState {
	struct Mtp {
		CloudPasswordCheckRequest request;
		bool unknownAlgorithm = false;
		CloudPasswordAlgo newPassword;
		SecureSecretAlgo newSecureSecret;
	};
	Mtp mtp;
	bool hasPassword = false;
	bool hasRecovery = false;
	bool notEmptyPassport = false;
	bool outdatedClient = false;
	QString hint;
	QString unconfirmedPattern;
	TimeId pendingResetDate = 0;
};

CloudPasswordState ParseCloudPasswordState(
	const MTPDaccount_password &data);

} // namespace Core