mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-24 01:46:36 +02:00
calculate shared key in separate therad
This commit is contained in:
parent
298c5f0de2
commit
34939f9381
2 changed files with 42 additions and 24 deletions
|
@ -1,5 +1,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <future>
|
||||||
|
|
||||||
#include "I2PEndian.h"
|
#include "I2PEndian.h"
|
||||||
#include "Base.h"
|
#include "Base.h"
|
||||||
|
@ -38,7 +39,7 @@ namespace transport
|
||||||
void NTCPSession::CreateAESKey (uint8_t * pubKey, i2p::crypto::AESKey& key)
|
void NTCPSession::CreateAESKey (uint8_t * pubKey, i2p::crypto::AESKey& key)
|
||||||
{
|
{
|
||||||
uint8_t sharedKey[256];
|
uint8_t sharedKey[256];
|
||||||
m_DHKeysPair->Agree (pubKey, sharedKey);
|
m_DHKeysPair->Agree (pubKey, sharedKey); // time consuming operation
|
||||||
|
|
||||||
uint8_t * aesKey = key;
|
uint8_t * aesKey = key;
|
||||||
if (sharedKey[0] & 0x80)
|
if (sharedKey[0] & 0x80)
|
||||||
|
@ -228,9 +229,26 @@ namespace transport
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
auto s = shared_from_this ();
|
||||||
|
// create AES key in separate thread
|
||||||
|
auto createKey = std::async (std::launch::async, [s] ()->i2p::crypto::AESKey
|
||||||
{
|
{
|
||||||
i2p::crypto::AESKey aesKey;
|
i2p::crypto::AESKey aesKey;
|
||||||
CreateAESKey (m_Establisher->phase2.pubKey, aesKey);
|
s->CreateAESKey (s->m_Establisher->phase2.pubKey, aesKey);
|
||||||
|
return std::move (aesKey);
|
||||||
|
}).share (); // TODO: use move capture in C++ 14 instead shared_future
|
||||||
|
// let other operations execute while a key gets created
|
||||||
|
m_Server.GetService ().post ([s, createKey]()
|
||||||
|
{
|
||||||
|
auto aesKey = createKey.get (); // we might wait if no more pending operations
|
||||||
|
s->HandlePhase2 (aesKey);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void NTCPSession::HandlePhase2 (const i2p::crypto::AESKey& aesKey)
|
||||||
|
{
|
||||||
m_Decryption.SetKey (aesKey);
|
m_Decryption.SetKey (aesKey);
|
||||||
m_Decryption.SetIV (m_Establisher->phase2.pubKey + 240);
|
m_Decryption.SetIV (m_Establisher->phase2.pubKey + 240);
|
||||||
m_Encryption.SetKey (aesKey);
|
m_Encryption.SetKey (aesKey);
|
||||||
|
@ -253,7 +271,6 @@ namespace transport
|
||||||
}
|
}
|
||||||
SendPhase3 ();
|
SendPhase3 ();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void NTCPSession::SendPhase3 ()
|
void NTCPSession::SendPhase3 ()
|
||||||
{
|
{
|
||||||
|
|
|
@ -73,6 +73,7 @@ namespace transport
|
||||||
void SendPhase3 ();
|
void SendPhase3 ();
|
||||||
void HandlePhase1Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
void HandlePhase1Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
void HandlePhase2Received (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
void HandlePhase2Received (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||||
|
void HandlePhase2 (const i2p::crypto::AESKey& aesKey);
|
||||||
void HandlePhase3Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA);
|
void HandlePhase3Sent (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA);
|
||||||
void HandlePhase4Received (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA);
|
void HandlePhase4Received (const boost::system::error_code& ecode, std::size_t bytes_transferred, uint32_t tsA);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue