2019-04-21 16:44:27 +00:00
|
|
|
#include <service/async_key_exchange.hpp>
|
|
|
|
|
|
|
|
#include <crypto/crypto.hpp>
|
|
|
|
#include <crypto/types.hpp>
|
|
|
|
#include <util/logic.hpp>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
2019-05-28 19:45:08 +00:00
|
|
|
AsyncKeyExchange::AsyncKeyExchange(std::shared_ptr< Logic > l,
|
2019-04-21 16:44:27 +00:00
|
|
|
const ServiceInfo& r,
|
|
|
|
const Identity& localident,
|
|
|
|
const PQPubKey& introsetPubKey,
|
|
|
|
const Introduction& remote,
|
|
|
|
IDataHandler* h, const ConvoTag& t)
|
|
|
|
: logic(l)
|
|
|
|
, remote(r)
|
|
|
|
, m_LocalIdentity(localident)
|
|
|
|
, introPubKey(introsetPubKey)
|
|
|
|
, remoteIntro(remote)
|
|
|
|
, handler(h)
|
|
|
|
, tag(t)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncKeyExchange::Result(void* user)
|
|
|
|
{
|
|
|
|
AsyncKeyExchange* self = static_cast< AsyncKeyExchange* >(user);
|
|
|
|
// put values
|
|
|
|
self->handler->PutCachedSessionKeyFor(self->msg.tag, self->sharedKey);
|
|
|
|
self->handler->PutIntroFor(self->msg.tag, self->remoteIntro);
|
|
|
|
self->handler->PutSenderFor(self->msg.tag, self->remote);
|
|
|
|
self->handler->PutReplyIntroFor(self->msg.tag, self->msg.introReply);
|
|
|
|
self->hook(self->frame);
|
|
|
|
delete self;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncKeyExchange::Encrypt(void* user)
|
|
|
|
{
|
|
|
|
AsyncKeyExchange* self = static_cast< AsyncKeyExchange* >(user);
|
|
|
|
// derive ntru session key component
|
|
|
|
SharedSecret K;
|
2019-05-28 19:45:08 +00:00
|
|
|
auto crypto = CryptoManager::instance();
|
|
|
|
crypto->pqe_encrypt(self->frame.C, K, self->introPubKey);
|
2019-04-21 16:44:27 +00:00
|
|
|
// randomize Nonce
|
|
|
|
self->frame.N.Randomize();
|
|
|
|
// compure post handshake session key
|
|
|
|
// PKE (A, B, N)
|
|
|
|
SharedSecret sharedSecret;
|
|
|
|
using namespace std::placeholders;
|
|
|
|
path_dh_func dh_client =
|
2019-05-28 19:45:08 +00:00
|
|
|
std::bind(&Crypto::dh_client, crypto, _1, _2, _3, _4);
|
2019-04-21 16:44:27 +00:00
|
|
|
if(!self->m_LocalIdentity.KeyExchange(dh_client, sharedSecret,
|
|
|
|
self->remote, self->frame.N))
|
|
|
|
{
|
|
|
|
LogError("failed to derive x25519 shared key component");
|
|
|
|
}
|
|
|
|
std::array< byte_t, 64 > tmp = {{0}};
|
|
|
|
// K
|
|
|
|
std::copy(K.begin(), K.end(), tmp.begin());
|
|
|
|
// H (K + PKE(A, B, N))
|
|
|
|
std::copy(sharedSecret.begin(), sharedSecret.end(), tmp.begin() + 32);
|
2019-05-28 19:45:08 +00:00
|
|
|
crypto->shorthash(self->sharedKey, llarp_buffer_t(tmp));
|
2019-04-21 16:44:27 +00:00
|
|
|
// set tag
|
|
|
|
self->msg.tag = self->tag;
|
|
|
|
// set sender
|
|
|
|
self->msg.sender = self->m_LocalIdentity.pub;
|
|
|
|
// set version
|
|
|
|
self->msg.version = LLARP_PROTO_VERSION;
|
|
|
|
// set protocol
|
|
|
|
self->msg.proto = eProtocolTraffic;
|
|
|
|
// encrypt and sign
|
2019-05-28 19:45:08 +00:00
|
|
|
if(self->frame.EncryptAndSign(self->msg, K, self->m_LocalIdentity))
|
2019-04-21 16:44:27 +00:00
|
|
|
self->logic->queue_job({self, &Result});
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogError("failed to encrypt and sign");
|
|
|
|
delete self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|