diff --git a/td/mtproto/crypto.cpp b/td/mtproto/crypto.cpp index 38f7a9b64..0a536b881 100644 --- a/td/mtproto/crypto.cpp +++ b/td/mtproto/crypto.cpp @@ -318,11 +318,30 @@ Status DhHandshake::run_checks(DhCallback *callback) { return dh_check(prime_str_, prime_, g_int_, g_a_, g_b_, ctx_, callback); } -std::pair DhHandshake::gen_key() { +BigNum DhHandshake::get_g() const { + CHECK(has_config_); + return g_; +} + +BigNum DhHandshake::get_p() const { + CHECK(has_config_); + return prime_; +} + +BigNum DhHandshake::get_b() const { + CHECK(has_config_); + return b_; +} + +BigNum DhHandshake::get_g_ab() { CHECK(has_g_a_ && has_config_); BigNum g_ab; BigNum::mod_exp(g_ab, g_a_, b_, prime_, ctx_); - string key = g_ab.to_binary(2048 / 8); + return g_ab; +} + +std::pair DhHandshake::gen_key() { + string key = get_g_ab().to_binary(2048 / 8); auto key_id = calc_key_id(key); return std::pair(key_id, std::move(key)); } diff --git a/td/mtproto/crypto.h b/td/mtproto/crypto.h index 1e411c7c7..648045d16 100644 --- a/td/mtproto/crypto.h +++ b/td/mtproto/crypto.h @@ -55,6 +55,7 @@ class DhCallback { virtual void add_good_prime(Slice prime_str) const = 0; virtual void add_bad_prime(Slice prime_str) const = 0; }; + class DhHandshake { public: void set_config(int32 g_int, Slice prime_str); @@ -72,6 +73,11 @@ class DhHandshake { string get_g_b_hash() const; Status run_checks(DhCallback *callback) TD_WARN_UNUSED_RESULT; + BigNum get_g() const; + BigNum get_p() const; + BigNum get_b() const; + BigNum get_g_ab(); + std::pair gen_key(); static int64 calc_key_id(const string &auth_key); @@ -157,4 +163,5 @@ int64 dh_auth_key_id(const string &auth_key); void KDF(const string &auth_key, const UInt128 &msg_key, int X, UInt256 *aes_key, UInt256 *aes_iv); void tmp_KDF(const UInt128 &server_nonce, const UInt256 &new_nonce, UInt256 *tmp_aes_key, UInt256 *tmp_aes_iv); void KDF2(Slice auth_key, const UInt128 &msg_key, int X, UInt256 *aes_key, UInt256 *aes_iv); + } // namespace td