2018-05-20 14:54:52 +00:00
|
|
|
#include <llarp/crypto_async.h>
|
|
|
|
#include <llarp/mem.h>
|
2018-08-30 18:48:43 +00:00
|
|
|
#include <llarp/router_contact.hpp>
|
2018-05-20 14:54:52 +00:00
|
|
|
#include <string.h>
|
2018-06-10 14:05:48 +00:00
|
|
|
#include <llarp/crypto.hpp>
|
2018-07-02 19:24:22 +00:00
|
|
|
#include <llarp/router_id.hpp>
|
2018-05-22 15:54:19 +00:00
|
|
|
#include "buffer.hpp"
|
2018-07-02 19:24:22 +00:00
|
|
|
#include "logger.hpp"
|
2018-05-20 14:54:52 +00:00
|
|
|
#include "mem.hpp"
|
|
|
|
|
|
|
|
struct llarp_async_iwp
|
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_crypto *crypto;
|
|
|
|
struct llarp_logic *logic;
|
|
|
|
struct llarp_threadpool *worker;
|
2018-05-20 14:54:52 +00:00
|
|
|
};
|
|
|
|
|
2018-05-20 15:35:59 +00:00
|
|
|
namespace iwp
|
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
2018-05-24 16:04:34 +00:00
|
|
|
inform_intro(void *user)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
iwp_async_intro *intro = static_cast< iwp_async_intro * >(user);
|
|
|
|
intro->hook(intro);
|
|
|
|
}
|
2018-05-20 14:54:52 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
gen_intro(void *user)
|
|
|
|
{
|
|
|
|
iwp_async_intro *intro = static_cast< iwp_async_intro * >(user);
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-09-24 13:11:30 +00:00
|
|
|
#ifdef neuroCryptoDebug
|
2018-08-13 08:47:24 +00:00
|
|
|
char ftmp[68] = {0};
|
|
|
|
const char *hexname = llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(
|
|
|
|
intro->remote_pubkey, ftmp);
|
|
|
|
llarp::LogDebug("gen_intro remote_pubkey: ", hexname);
|
2018-09-24 13:11:30 +00:00
|
|
|
#endif
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
llarp::SharedSecret sharedkey;
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_crypto *crypto = intro->iwp->crypto;
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// S = TKE(a.k, b.k, n)
|
|
|
|
crypto->transport_dh_client(sharedkey, intro->remote_pubkey,
|
|
|
|
intro->secretkey, intro->nonce);
|
2018-09-24 13:11:30 +00:00
|
|
|
|
|
|
|
#ifdef neuroCryptoDebug
|
2018-08-13 08:47:24 +00:00
|
|
|
char ftmpShared[68] = {0};
|
|
|
|
const char *hexShared =
|
|
|
|
llarp::HexEncode< llarp::SharedSecret, decltype(ftmpShared) >(
|
|
|
|
sharedkey, ftmpShared);
|
|
|
|
llarp::LogDebug("gen_intro sharedkey ", hexShared);
|
|
|
|
|
|
|
|
byte_t tmp[64];
|
2018-05-23 20:37:43 +00:00
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
2018-09-24 13:11:30 +00:00
|
|
|
#endif
|
2018-08-13 08:47:24 +00:00
|
|
|
|
|
|
|
llarp::SymmNonce n;
|
|
|
|
// copy nonce
|
2018-09-21 14:36:06 +00:00
|
|
|
memcpy(n, intro->nonce, 32); // was 24 bytes
|
2018-09-24 13:11:30 +00:00
|
|
|
|
|
|
|
/*
|
2018-08-13 08:47:24 +00:00
|
|
|
char ftmpN[68] = {0};
|
|
|
|
const char *hexN =
|
|
|
|
llarp::HexEncode< llarp::SymmNonce, decltype(ftmpN) >(n, ftmpN);
|
|
|
|
llarp::LogDebug("gen_intro nonce ", hexN);
|
2018-09-24 13:11:30 +00:00
|
|
|
*/
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// e_k = HS(b.k + n)
|
2018-08-13 08:47:24 +00:00
|
|
|
|
|
|
|
llarp::ShortHash e_k;
|
2018-05-22 15:54:19 +00:00
|
|
|
memcpy(tmp, intro->remote_pubkey, 32);
|
|
|
|
memcpy(tmp + 32, intro->nonce, 32);
|
|
|
|
crypto->shorthash(e_k, buf);
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-09-24 13:11:30 +00:00
|
|
|
#ifdef neuroCryptoDebug
|
2018-08-13 08:47:24 +00:00
|
|
|
char ftmpEk[68] = {0};
|
|
|
|
const char *hexEk =
|
|
|
|
llarp::HexEncode< llarp::ShortHash, decltype(ftmpEk) >(e_k, ftmpEk);
|
|
|
|
llarp::LogDebug("gen_intro e_k ", hexEk, " used ", strlen(hexEk));
|
2018-09-24 13:11:30 +00:00
|
|
|
#endif
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-08-29 20:40:26 +00:00
|
|
|
// put nonce
|
|
|
|
memcpy(intro->buf + 32, intro->nonce, 32);
|
2018-09-19 12:22:34 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// e = SE(a.k, e_k, n[0:24])
|
2018-06-10 14:05:48 +00:00
|
|
|
memcpy(intro->buf + 64, llarp::seckey_topublic(intro->secretkey), 32);
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-09-24 13:11:30 +00:00
|
|
|
#ifdef neuroCryptoDebug
|
2018-08-13 08:47:24 +00:00
|
|
|
char ftmpSk[68] = {0};
|
|
|
|
const char *hexSk = llarp::HexEncode< llarp::PubKey, decltype(ftmpSk) >(
|
|
|
|
intro->secretkey, ftmpSk);
|
|
|
|
llarp::LogDebug("gen_intro SK ", hexSk);
|
|
|
|
|
|
|
|
char ftmpSkPub[68] = {0};
|
|
|
|
const char *hexSkPub =
|
|
|
|
llarp::HexEncode< llarp::PubKey, decltype(ftmpSkPub) >(
|
|
|
|
llarp::seckey_topublic(intro->secretkey), ftmpSkPub);
|
|
|
|
llarp::LogDebug("gen_intro SK pub ", hexSkPub);
|
2018-09-24 13:11:30 +00:00
|
|
|
#endif
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-05-23 20:37:43 +00:00
|
|
|
buf.base = intro->buf + 64;
|
|
|
|
buf.cur = buf.base;
|
2018-05-22 15:54:19 +00:00
|
|
|
buf.sz = 32;
|
2018-07-02 19:24:22 +00:00
|
|
|
crypto->xchacha20(buf, e_k, intro->nonce);
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// h = MDS( n + e + w0, S)
|
2018-05-23 20:37:43 +00:00
|
|
|
buf.base = intro->buf + 32;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = intro->sz - 32;
|
2018-05-22 15:54:19 +00:00
|
|
|
crypto->hmac(intro->buf, buf, sharedkey);
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-09-24 13:11:30 +00:00
|
|
|
#ifdef neuroCryptoDebug
|
2018-08-13 08:47:24 +00:00
|
|
|
char ftmpHmac[68] = {0}; // probably could be 65
|
|
|
|
const char *hexHmac = llarp::HexEncode< llarp::PubKey, decltype(ftmpHmac) >(
|
|
|
|
intro->buf, ftmpHmac);
|
|
|
|
llarp::LogDebug("gen_intro Hmac ", hexHmac);
|
2018-09-24 13:11:30 +00:00
|
|
|
#endif
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// inform result
|
2018-07-26 21:08:56 +00:00
|
|
|
// intro->hook(intro);
|
|
|
|
llarp_logic_queue_job(intro->iwp->logic, {intro, &inform_intro});
|
2018-05-23 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
verify_intro(void *user)
|
|
|
|
{
|
|
|
|
iwp_async_intro *intro = static_cast< iwp_async_intro * >(user);
|
|
|
|
auto crypto = intro->iwp->crypto;
|
2018-06-12 11:57:14 +00:00
|
|
|
llarp::SharedSecret sharedkey;
|
|
|
|
llarp::ShortHash e_K;
|
|
|
|
llarp::SharedSecret h;
|
2018-05-23 20:37:43 +00:00
|
|
|
byte_t tmp[64];
|
2018-07-02 19:24:22 +00:00
|
|
|
const auto OurPK = llarp::seckey_topublic(intro->secretkey);
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-09-24 13:11:30 +00:00
|
|
|
#ifdef neuroCryptoDebug
|
2018-08-13 08:47:24 +00:00
|
|
|
char ftmp[68] = {0};
|
|
|
|
const char *hexPk =
|
|
|
|
llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(OurPK, ftmp);
|
|
|
|
llarp::LogDebug("intro OurPK ", hexPk);
|
2018-09-24 13:11:30 +00:00
|
|
|
#endif
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-05-23 20:37:43 +00:00
|
|
|
// e_k = HS(b.k + n)
|
|
|
|
memcpy(tmp, OurPK, 32);
|
|
|
|
memcpy(tmp + 32, intro->nonce, 32);
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
crypto->shorthash(e_K, buf);
|
|
|
|
|
|
|
|
buf.base = intro->remote_pubkey;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = 32;
|
2018-09-19 12:22:34 +00:00
|
|
|
|
|
|
|
// was using remote_pubkey directly (use buffer wrapper? directly?)
|
2018-09-21 14:36:06 +00:00
|
|
|
// memcpy(intro->remote_pubkey, intro->buf + 64, 32);
|
2018-08-29 20:40:26 +00:00
|
|
|
memcpy(buf.base, intro->buf + 64, 32);
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-07-02 19:24:22 +00:00
|
|
|
crypto->xchacha20(buf, e_K, intro->nonce);
|
2018-09-24 13:11:30 +00:00
|
|
|
#ifdef neuroCryptoDebug
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("handshake from ", llarp::RouterID(intro->remote_pubkey));
|
2018-08-13 08:47:24 +00:00
|
|
|
|
|
|
|
char ftmp2[68] = {0};
|
|
|
|
const char *hexRemotePK = llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(
|
|
|
|
intro->remote_pubkey, ftmp2);
|
|
|
|
llarp::LogDebug("intro remote_pubkey ", hexRemotePK);
|
2018-09-24 13:11:30 +00:00
|
|
|
#endif
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-05-23 20:37:43 +00:00
|
|
|
// S = TKE(a.k, b.k, n)
|
|
|
|
crypto->transport_dh_server(sharedkey, intro->remote_pubkey,
|
|
|
|
intro->secretkey, intro->nonce);
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-09-24 13:11:30 +00:00
|
|
|
#ifdef neuroCryptoDebug
|
2018-08-13 08:47:24 +00:00
|
|
|
char ftmpShared[68] = {0};
|
|
|
|
const char *hexShared =
|
|
|
|
llarp::HexEncode< llarp::PubKey, decltype(ftmpShared) >(sharedkey,
|
|
|
|
ftmpShared);
|
|
|
|
llarp::LogDebug("intro sharedkey ", hexShared);
|
2018-09-24 13:11:30 +00:00
|
|
|
#endif
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-07-02 19:24:22 +00:00
|
|
|
// h = MDS( n + e + w2, S)
|
2018-05-23 20:37:43 +00:00
|
|
|
buf.base = intro->buf + 32;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = intro->sz - 32;
|
|
|
|
crypto->hmac(h, buf, sharedkey);
|
|
|
|
if(memcmp(h, intro->buf, 32))
|
|
|
|
{
|
2018-08-13 08:47:24 +00:00
|
|
|
llarp::LogWarn("intro HMAC failure");
|
2018-05-23 20:37:43 +00:00
|
|
|
// hmac fail
|
2018-08-29 20:40:26 +00:00
|
|
|
delete[] intro->buf;
|
2018-05-23 20:37:43 +00:00
|
|
|
intro->buf = nullptr;
|
|
|
|
}
|
|
|
|
// inform result
|
2018-05-24 16:04:34 +00:00
|
|
|
llarp_logic_queue_job(intro->iwp->logic, {intro, &inform_intro});
|
2018-05-23 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
2018-05-24 16:04:34 +00:00
|
|
|
inform_introack(void *user)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
iwp_async_introack *introack = static_cast< iwp_async_introack * >(user);
|
|
|
|
introack->hook(introack);
|
|
|
|
}
|
2018-05-20 14:54:52 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
verify_introack(void *user)
|
2018-05-20 14:54:52 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
iwp_async_introack *introack = static_cast< iwp_async_introack * >(user);
|
|
|
|
auto crypto = introack->iwp->crypto;
|
2018-07-26 21:08:56 +00:00
|
|
|
auto logic = introack->iwp->logic;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
llarp::ShortHash digest;
|
|
|
|
llarp::SharedSecret sharedkey;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-08-31 19:46:25 +00:00
|
|
|
auto hmac = introack->buf;
|
|
|
|
auto body = introack->buf + 32;
|
|
|
|
const auto pubkey = introack->remote_pubkey;
|
|
|
|
auto secretkey = introack->secretkey;
|
|
|
|
auto nonce = introack->buf + 32;
|
|
|
|
auto token = introack->buf + 64;
|
|
|
|
size_t bodysz = introack->sz - 32;
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_buffer_t buf;
|
|
|
|
buf.base = body;
|
|
|
|
buf.cur = body;
|
|
|
|
buf.sz = bodysz;
|
|
|
|
|
|
|
|
// S = TKE(a.k, b.k, n)
|
|
|
|
crypto->transport_dh_client(sharedkey, pubkey, secretkey, nonce);
|
|
|
|
|
|
|
|
// h = MDS(n + x + w1, S)
|
|
|
|
crypto->hmac(digest, buf, sharedkey);
|
|
|
|
|
|
|
|
if(!llarp_eq(digest, hmac, 32))
|
|
|
|
{
|
|
|
|
// fail to verify hmac
|
|
|
|
introack->buf = nullptr;
|
|
|
|
}
|
2018-05-23 20:37:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
buf.base = token;
|
|
|
|
buf.sz = 32;
|
|
|
|
// token = SD(S, x, n[0:24])
|
|
|
|
crypto->xchacha20(buf, sharedkey, nonce);
|
|
|
|
// copy token
|
|
|
|
memcpy(introack->token, token, 32);
|
|
|
|
}
|
2018-07-26 21:08:56 +00:00
|
|
|
// introack->hook(introack);
|
|
|
|
llarp_logic_queue_job(logic, {introack, &inform_introack});
|
2018-05-23 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gen_introack(void *user)
|
|
|
|
{
|
|
|
|
iwp_async_introack *introack = static_cast< iwp_async_introack * >(user);
|
2018-06-12 11:57:14 +00:00
|
|
|
llarp::SharedSecret sharedkey;
|
2018-08-31 19:46:25 +00:00
|
|
|
auto crypto = introack->iwp->crypto;
|
|
|
|
const auto pubkey = introack->remote_pubkey;
|
|
|
|
auto secretkey = introack->secretkey;
|
|
|
|
auto nonce = introack->nonce;
|
2018-05-23 20:37:43 +00:00
|
|
|
// S = TKE(a.k, b.k, n)
|
|
|
|
crypto->transport_dh_server(sharedkey, pubkey, secretkey, nonce);
|
|
|
|
|
|
|
|
// x = SE(S, token, n[0:24])
|
|
|
|
llarp_buffer_t buf;
|
|
|
|
buf.base = introack->buf + 64;
|
2018-05-22 15:54:19 +00:00
|
|
|
buf.sz = 32;
|
2018-05-23 20:37:43 +00:00
|
|
|
buf.cur = buf.base;
|
|
|
|
memcpy(buf.base, introack->token, 32);
|
2018-05-22 15:54:19 +00:00
|
|
|
crypto->xchacha20(buf, sharedkey, nonce);
|
2018-05-23 20:37:43 +00:00
|
|
|
|
|
|
|
// h = MDS(n + x + w1, S)
|
|
|
|
buf.base = introack->buf + 32;
|
|
|
|
buf.sz = introack->sz - 32;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
crypto->hmac(introack->buf, buf, sharedkey);
|
2018-07-26 21:08:56 +00:00
|
|
|
// introack->hook(introack);
|
|
|
|
llarp_logic_queue_job(introack->iwp->logic, {introack, &inform_introack});
|
2018-05-20 14:54:52 +00:00
|
|
|
}
|
2018-05-20 15:35:59 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
2018-05-24 16:04:34 +00:00
|
|
|
inform_session_start(void *user)
|
2018-05-20 15:35:59 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
iwp_async_session_start *session =
|
|
|
|
static_cast< iwp_async_session_start * >(user);
|
|
|
|
session->hook(session);
|
2018-05-20 15:35:59 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
gen_session_start(void *user)
|
2018-05-20 15:35:59 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
iwp_async_session_start *session =
|
|
|
|
static_cast< iwp_async_session_start * >(user);
|
2018-05-20 15:35:59 +00:00
|
|
|
auto crypto = session->iwp->crypto;
|
2018-07-11 16:11:19 +00:00
|
|
|
auto logic = session->iwp->logic;
|
2018-05-20 15:35:59 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
auto dh = crypto->transport_dh_client;
|
2018-05-20 15:35:59 +00:00
|
|
|
auto shorthash = crypto->shorthash;
|
2018-05-22 15:54:19 +00:00
|
|
|
auto hmac = crypto->hmac;
|
|
|
|
auto encrypt = crypto->xchacha20;
|
|
|
|
|
2018-06-20 17:45:44 +00:00
|
|
|
// auto logic = session->iwp->logic;
|
2018-08-31 19:46:25 +00:00
|
|
|
auto a_sK = session->secretkey;
|
|
|
|
const auto b_K = session->remote_pubkey;
|
|
|
|
auto N = session->nonce;
|
|
|
|
auto token = session->token;
|
|
|
|
auto K = session->sessionkey;
|
2018-05-20 15:35:59 +00:00
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
llarp::SharedSecret e_K;
|
|
|
|
llarp::ShortHash T;
|
2018-05-20 15:35:59 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
byte_t tmp[64];
|
2018-07-17 04:37:50 +00:00
|
|
|
llarp_buffer_t buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
2018-05-20 15:35:59 +00:00
|
|
|
|
|
|
|
// T = HS(token + n)
|
|
|
|
memcpy(tmp, token, 32);
|
|
|
|
memcpy(tmp + 32, N, 32);
|
2018-05-22 15:54:19 +00:00
|
|
|
shorthash(T, buf);
|
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
// e_K = TKE(a.k, b.k, n)
|
2018-05-20 15:35:59 +00:00
|
|
|
dh(e_K, b_K, a_sK, N);
|
|
|
|
// K = TKE(a.k, b.k, T)
|
|
|
|
dh(K, b_K, a_sK, T);
|
|
|
|
|
|
|
|
// x = SE(e_K, token, n[0:24])
|
2018-05-24 16:04:34 +00:00
|
|
|
buf.base = (session->buf + 64);
|
2018-05-22 15:54:19 +00:00
|
|
|
buf.sz = 32;
|
2018-05-20 15:35:59 +00:00
|
|
|
memcpy(buf.base, token, 32);
|
|
|
|
encrypt(buf, e_K, N);
|
|
|
|
|
|
|
|
// h = MDS(n + x + w2, e_K)
|
2018-05-22 15:54:19 +00:00
|
|
|
buf.base = (session->buf + 32);
|
|
|
|
buf.sz = session->sz - 32;
|
2018-05-20 15:35:59 +00:00
|
|
|
hmac(session->buf, buf, e_K);
|
2018-07-11 16:11:19 +00:00
|
|
|
// session->hook(session);
|
2018-08-31 18:33:08 +00:00
|
|
|
llarp_logic_queue_job(logic, {session, &inform_session_start});
|
2018-05-24 16:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
verify_session_start(void *user)
|
|
|
|
{
|
|
|
|
iwp_async_session_start *session =
|
|
|
|
static_cast< iwp_async_session_start * >(user);
|
2018-08-04 02:59:32 +00:00
|
|
|
// possible repeat job
|
|
|
|
if(session->buf == nullptr)
|
|
|
|
return;
|
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
auto crypto = session->iwp->crypto;
|
2018-07-11 16:11:19 +00:00
|
|
|
auto logic = session->iwp->logic;
|
2018-05-24 16:04:34 +00:00
|
|
|
|
|
|
|
auto dh = crypto->transport_dh_server;
|
|
|
|
auto shorthash = crypto->shorthash;
|
|
|
|
auto hmac = crypto->hmac;
|
|
|
|
auto decrypt = crypto->xchacha20;
|
|
|
|
|
|
|
|
auto b_sK = session->secretkey;
|
|
|
|
auto a_K = session->remote_pubkey;
|
|
|
|
auto N = session->nonce;
|
|
|
|
auto token = session->token;
|
|
|
|
auto K = session->sessionkey;
|
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
llarp::SharedSecret e_K;
|
|
|
|
llarp::ShortHash T;
|
2018-05-24 16:04:34 +00:00
|
|
|
|
|
|
|
byte_t tmp[64];
|
|
|
|
|
|
|
|
llarp_buffer_t buf;
|
|
|
|
|
|
|
|
// e_K = TKE(a.k, b.k, N)
|
|
|
|
dh(e_K, a_K, b_sK, N);
|
|
|
|
// h = MDS( n + x + w2, e_K)
|
|
|
|
buf.base = session->buf + 32;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = session->sz - 32;
|
|
|
|
hmac(tmp, buf, e_K);
|
|
|
|
if(memcmp(tmp, session->buf, 32) == 0)
|
|
|
|
{
|
|
|
|
// hmac good
|
|
|
|
buf.base = session->buf + 64;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = 32;
|
|
|
|
// token = SD(e_K, x, n[0:24])
|
|
|
|
decrypt(buf, e_K, N);
|
|
|
|
// ensure it's the same token
|
|
|
|
if(memcmp(buf.base, token, 32) == 0)
|
|
|
|
{
|
|
|
|
// T = HS(token + n)
|
|
|
|
memcpy(tmp, token, 32);
|
|
|
|
memcpy(tmp + 32, N, 32);
|
2018-05-25 17:52:10 +00:00
|
|
|
buf.base = tmp;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = sizeof(tmp);
|
2018-05-24 16:04:34 +00:00
|
|
|
shorthash(T, buf);
|
|
|
|
// K = TKE(a.k, b.k, T)
|
|
|
|
dh(K, a_K, b_sK, T);
|
|
|
|
}
|
|
|
|
else // token missmatch
|
|
|
|
{
|
|
|
|
session->buf = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // hmac fail
|
|
|
|
session->buf = nullptr;
|
2018-07-11 16:11:19 +00:00
|
|
|
// session->hook(session);
|
2018-08-31 18:33:08 +00:00
|
|
|
llarp_logic_queue_job(logic, {session, &inform_session_start});
|
2018-05-24 16:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
inform_frame_done(void *user)
|
|
|
|
{
|
|
|
|
iwp_async_frame *frame = static_cast< iwp_async_frame * >(user);
|
|
|
|
frame->hook(frame);
|
2018-06-06 12:46:26 +00:00
|
|
|
delete frame;
|
2018-05-24 16:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hmac_then_decrypt(void *user)
|
|
|
|
{
|
|
|
|
iwp_async_frame *frame = static_cast< iwp_async_frame * >(user);
|
2018-06-26 01:30:36 +00:00
|
|
|
iwp_decrypt_frame(frame);
|
2018-05-24 16:04:34 +00:00
|
|
|
// inform result
|
2018-06-06 12:46:26 +00:00
|
|
|
llarp_logic_queue_job(frame->iwp->logic, {frame, &inform_frame_done});
|
2018-05-24 16:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
encrypt_then_hmac(void *user)
|
|
|
|
{
|
|
|
|
iwp_async_frame *frame = static_cast< iwp_async_frame * >(user);
|
2018-06-26 01:30:36 +00:00
|
|
|
iwp_encrypt_frame(frame);
|
2018-06-06 12:46:26 +00:00
|
|
|
// call result RIGHT HERE
|
|
|
|
frame->hook(frame);
|
|
|
|
delete frame;
|
2018-05-20 15:35:59 +00:00
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
} // namespace iwp
|
2018-05-20 14:54:52 +00:00
|
|
|
|
2018-06-26 01:30:36 +00:00
|
|
|
bool
|
|
|
|
iwp_decrypt_frame(struct iwp_async_frame *frame)
|
|
|
|
{
|
|
|
|
auto crypto = frame->iwp->crypto;
|
|
|
|
byte_t *hmac = frame->buf;
|
|
|
|
byte_t *nonce = frame->buf + 32;
|
|
|
|
byte_t *body = frame->buf + 64;
|
|
|
|
|
|
|
|
llarp::ShortHash digest;
|
|
|
|
|
|
|
|
llarp_buffer_t buf;
|
|
|
|
buf.base = nonce;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = frame->sz - 32;
|
|
|
|
|
|
|
|
// h = MDS(n + x, S)
|
|
|
|
crypto->hmac(digest, buf, frame->sessionkey);
|
|
|
|
// check hmac
|
|
|
|
frame->success = memcmp(digest, hmac, 32) == 0;
|
2018-08-13 08:47:24 +00:00
|
|
|
if(!frame->success)
|
|
|
|
{
|
|
|
|
// [", digest, "] vs [", hmac, "]
|
|
|
|
llarp::LogWarn("crypto_async::iwp_decrypt_frame failed to decrypt");
|
|
|
|
//} else {
|
|
|
|
// llarp::Debug("crypto_async::iwp_decrypt_frame decrypted");
|
|
|
|
}
|
2018-06-26 01:30:36 +00:00
|
|
|
// x = SE(S, p, n[0:24])
|
|
|
|
buf.base = body;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = frame->sz - 64;
|
|
|
|
crypto->xchacha20(buf, frame->sessionkey, nonce);
|
2018-08-13 08:47:24 +00:00
|
|
|
|
2018-06-26 01:30:36 +00:00
|
|
|
return frame->success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
iwp_encrypt_frame(struct iwp_async_frame *frame)
|
|
|
|
{
|
|
|
|
auto crypto = frame->iwp->crypto;
|
|
|
|
byte_t *hmac = frame->buf;
|
|
|
|
byte_t *nonce = frame->buf + 32;
|
|
|
|
byte_t *body = frame->buf + 64;
|
|
|
|
|
|
|
|
llarp_buffer_t buf;
|
|
|
|
buf.base = body;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = frame->sz - 64;
|
|
|
|
|
|
|
|
// randomize N
|
|
|
|
crypto->randbytes(nonce, 32);
|
|
|
|
// x = SE(S, p, n[0:24])
|
|
|
|
crypto->xchacha20(buf, frame->sessionkey, nonce);
|
|
|
|
// h = MDS(n + x, S)
|
|
|
|
buf.base = nonce;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = frame->sz - 32;
|
|
|
|
crypto->hmac(hmac, buf, frame->sessionkey);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
void
|
|
|
|
iwp_call_async_gen_intro(struct llarp_async_iwp *iwp,
|
|
|
|
struct iwp_async_intro *intro)
|
|
|
|
{
|
|
|
|
intro->iwp = iwp;
|
|
|
|
llarp_threadpool_queue_job(iwp->worker, {intro, &iwp::gen_intro});
|
|
|
|
}
|
2018-05-20 14:54:52 +00:00
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
void
|
|
|
|
iwp_call_async_verify_introack(struct llarp_async_iwp *iwp,
|
|
|
|
struct iwp_async_introack *introack)
|
|
|
|
{
|
|
|
|
introack->iwp = iwp;
|
|
|
|
llarp_threadpool_queue_job(iwp->worker, {introack, &iwp::verify_introack});
|
|
|
|
}
|
2018-05-20 14:54:52 +00:00
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
void
|
|
|
|
iwp_call_async_gen_session_start(struct llarp_async_iwp *iwp,
|
|
|
|
struct iwp_async_session_start *session)
|
|
|
|
{
|
|
|
|
session->iwp = iwp;
|
|
|
|
llarp_threadpool_queue_job(iwp->worker, {session, &iwp::gen_session_start});
|
|
|
|
}
|
2018-05-20 14:54:52 +00:00
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
void
|
|
|
|
iwp_call_async_verify_intro(struct llarp_async_iwp *iwp,
|
|
|
|
struct iwp_async_intro *intro)
|
|
|
|
{
|
|
|
|
intro->iwp = iwp;
|
|
|
|
llarp_threadpool_queue_job(iwp->worker, {intro, &iwp::verify_intro});
|
|
|
|
}
|
2018-05-23 20:37:43 +00:00
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
void
|
|
|
|
iwp_call_async_gen_introack(struct llarp_async_iwp *iwp,
|
|
|
|
struct iwp_async_introack *introack)
|
|
|
|
{
|
|
|
|
introack->iwp = iwp;
|
|
|
|
llarp_threadpool_queue_job(iwp->worker, {introack, &iwp::gen_introack});
|
|
|
|
}
|
2018-05-23 20:37:43 +00:00
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
void
|
|
|
|
iwp_call_async_frame_decrypt(struct llarp_async_iwp *iwp,
|
|
|
|
struct iwp_async_frame *frame)
|
|
|
|
{
|
|
|
|
frame->iwp = iwp;
|
|
|
|
llarp_threadpool_queue_job(iwp->worker, {frame, &iwp::hmac_then_decrypt});
|
|
|
|
}
|
2018-05-24 16:04:34 +00:00
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
void
|
|
|
|
iwp_call_async_frame_encrypt(struct llarp_async_iwp *iwp,
|
|
|
|
struct iwp_async_frame *frame)
|
|
|
|
{
|
|
|
|
frame->iwp = iwp;
|
|
|
|
llarp_threadpool_queue_job(iwp->worker, {frame, &iwp::encrypt_then_hmac});
|
|
|
|
}
|
2018-05-24 16:04:34 +00:00
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
void
|
|
|
|
iwp_call_async_verify_session_start(struct llarp_async_iwp *iwp,
|
|
|
|
struct iwp_async_session_start *session)
|
|
|
|
{
|
|
|
|
session->iwp = iwp;
|
|
|
|
llarp_threadpool_queue_job(iwp->worker,
|
|
|
|
{session, &iwp::verify_session_start});
|
|
|
|
}
|
2018-05-20 14:54:52 +00:00
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
struct llarp_async_iwp *
|
|
|
|
llarp_async_iwp_new(struct llarp_crypto *crypto, struct llarp_logic *logic,
|
|
|
|
struct llarp_threadpool *worker)
|
|
|
|
{
|
|
|
|
llarp_async_iwp *iwp = new llarp_async_iwp;
|
|
|
|
if(iwp)
|
2018-05-20 14:54:52 +00:00
|
|
|
{
|
2018-06-18 22:05:02 +00:00
|
|
|
iwp->crypto = crypto;
|
|
|
|
iwp->logic = logic;
|
|
|
|
iwp->worker = worker;
|
2018-05-20 14:54:52 +00:00
|
|
|
}
|
2018-06-18 22:05:02 +00:00
|
|
|
return iwp;
|
|
|
|
}
|
2018-05-22 19:19:06 +00:00
|
|
|
|
2018-06-18 22:05:02 +00:00
|
|
|
void
|
|
|
|
llarp_async_iwp_free(struct llarp_async_iwp *iwp)
|
|
|
|
{
|
|
|
|
delete iwp;
|
|
|
|
}
|