Commit a29d0971 authored by Luker's avatar Luker

User KDF

Provide the user with a common KDF.
Can be used to seed common RNG and other.
Will be used to initialize/randomize client/srv
streams (and possibly more)
parent 248485f4
......@@ -43,6 +43,14 @@ class Handler;
namespace Event {
class Loop;
}
namespace Crypto {
class Encryption;
class Hmac;
class KDF;
}
namespace Recover {
class ECC;
}
class FENRIR_LOCAL user_data {
public:
......@@ -170,6 +178,7 @@ private:
std::shared_ptr<Crypto::Encryption> _enc_recv;
std::shared_ptr<Crypto::Hmac> _hmac_recv;
std::shared_ptr<Recover::ECC> _ecc_recv;
std::shared_ptr<Crypto::KDF> _user_kdf;
Connection (const Role role, const User_ID user,
Event::Loop *const loop,
......@@ -187,7 +196,8 @@ private:
std::shared_ptr<Recover::ECC> ecc_send,
std::shared_ptr<Crypto::Encryption> enc_recv,
std::shared_ptr<Crypto::Hmac> hmac_recv,
std::shared_ptr<Recover::ECC> ecc_recv);
std::shared_ptr<Recover::ECC> ecc_recv,
std::shared_ptr<Crypto::KDF> user_kdf);
void parse_rel_control();
void parse_unrel_control();
void parse_control (const std::vector<uint8_t> &data);
......
......@@ -77,12 +77,13 @@ FENRIR_INLINE Connection::Connection (const Role role, const User_ID user,
const Packet::Alignment_Byte write_al,
const uint8_t max_read_padding,
const uint8_t max_write_padding,
std::shared_ptr<Crypto::Encryption> enc_send,
std::shared_ptr<Crypto::Hmac> hmac_send,
std::shared_ptr<Recover::ECC> ecc_send,
std::shared_ptr<Crypto::Encryption> enc_recv,
std::shared_ptr<Crypto::Hmac> hmac_recv,
std::shared_ptr<Recover::ECC> ecc_recv)
std::shared_ptr<Crypto::Encryption> enc_send,
std::shared_ptr<Crypto::Hmac> hmac_send,
std::shared_ptr<Recover::ECC> ecc_send,
std::shared_ptr<Crypto::Encryption> enc_recv,
std::shared_ptr<Crypto::Hmac> hmac_recv,
std::shared_ptr<Recover::ECC> ecc_recv,
std::shared_ptr<Crypto::KDF> user_kdf)
: _read_connection_id (read), _write_connection_id (write),
_user_id (user),
_rel_read_control_stream (read_control_stream),
......@@ -104,7 +105,8 @@ FENRIR_INLINE Connection::Connection (const Role role, const User_ID user,
_ecc_send (std::move(ecc_send)),
_enc_recv (std::move(enc_recv)),
_hmac_recv (std::move(hmac_recv)),
_ecc_recv (std::move(ecc_recv))
_ecc_recv (std::move(ecc_recv)),
_user_kdf (std::move(user_kdf))
{
_max_write_padding = 8;
auto rel_st_in = std::make_shared<Storage_Raw> ();
......
......@@ -113,6 +113,7 @@ private:
std::shared_ptr<Crypto::Encryption> _enc_write;
std::shared_ptr<Crypto::Hmac> _hmac_write;
std::shared_ptr<Recover::ECC> _ecc_write;
std::shared_ptr<Crypto::KDF> _user_kdf;
// TODO: provide KDF *and* deterministic rng for user
//std::shared_ptr<Crypto::KDF> _kdf;
......
......@@ -737,7 +737,8 @@ FENRIR_INLINE void Handshake::answer_s_keys (const Link_ID recv_from,
lock.early_unlock();
auto kdf = _load->get_shared<Crypto::KDF> (prev_data.r->_selected_kdf);
if (kdf == nullptr || !kdf->init (key))
auto user_kdf = _load->get_shared<Crypto::KDF> (prev_data.r->_selected_kdf);
if (kdf == nullptr || user_kdf == nullptr || !kdf->init (key))
return;
// all ok, build answer.
......@@ -778,6 +779,9 @@ FENRIR_INLINE void Handshake::answer_s_keys (const Link_ID recv_from,
kdf->get (6, context, tmp_key);
if (!ecc_read->init (tmp_key))
return;
kdf->get (7, context, tmp_key);
if (!user_kdf->init (tmp_key))
return;
// TODO: GET STREAMS DATA
const uint16_t streams_num = 1;
......@@ -916,6 +920,7 @@ FENRIR_INLINE void Handshake::answer_s_keys (const Link_ID recv_from,
std::get<state_client> (*pkt_it)._enc_write = std::move(enc_write);
std::get<state_client> (*pkt_it)._hmac_write = std::move(hmac_write);
std::get<state_client> (*pkt_it)._ecc_write = std::move(ecc_write);
std::get<state_client> (*pkt_it)._user_kdf = std::move(user_kdf);
std::get<state_client> (*pkt_it)._client_key = nullptr;
w_lock.early_unlock();
......@@ -962,7 +967,8 @@ FENRIR_INLINE void Handshake::answer_c_auth (const Link_ID recv_from,
return;
auto kdf = _load->get_shared<Crypto::KDF> (srv->_kdf);
if (kdf == nullptr || !kdf->init (srv->_key))
auto user_kdf = _load->get_shared<Crypto::KDF> (srv->_kdf);
if (kdf == nullptr || user_kdf == nullptr || !kdf->init (srv->_key))
return;
constexpr std::array<char, 8> context {{ "FENRIR_" }};
......@@ -984,6 +990,7 @@ FENRIR_INLINE void Handshake::answer_c_auth (const Link_ID recv_from,
if (!ecc_read->init (tmp_key))
return;
// test autenticity & decrypt
gsl::span<uint8_t> decrypted_data {data._enc_data};
if (ecc_read->correct (decrypted_data, decrypted_data) ==
......@@ -1053,6 +1060,9 @@ FENRIR_INLINE void Handshake::answer_c_auth (const Link_ID recv_from,
kdf->get (6, context, tmp_key);
if (!ecc_write->init (tmp_key))
return;
kdf->get (7, context, tmp_key);
if (!user_kdf->init (tmp_key))
return;
auto conn = Connection::mk_shared (Role::Server,
......@@ -1070,8 +1080,8 @@ FENRIR_INLINE void Handshake::answer_c_auth (const Link_ID recv_from,
client_auth_data.r->_max_padding,
srv_max_padding,
enc_write, hmac_write, ecc_write,
enc_read, hmac_read, ecc_read
);
enc_read, hmac_read, ecc_read,
user_kdf);
conn->add_Link_out (recv_from);
if (_handler->add_connection (std::move(conn)) != Error::NONE)
......@@ -1166,12 +1176,13 @@ FENRIR_INLINE void Handshake::parse_s_result (const Link_ID recv_from,
return; // random packet. Don't answer.
}
auto enc_read = std::get<state_client> (*it)._enc_read;
auto hmac_read = std::get<state_client> (*it)._hmac_read;
auto ecc_read = std::get<state_client> (*it)._ecc_read;
auto enc_read = std::get<state_client> (*it)._enc_read;
auto hmac_read = std::get<state_client> (*it)._hmac_read;
auto ecc_read = std::get<state_client> (*it)._ecc_read;
auto enc_write = std::get<state_client> (*it)._enc_write;
auto hmac_write = std::get<state_client> (*it)._hmac_write;
auto ecc_write = std::get<state_client> (*it)._ecc_write;
auto user_kdf = std::get<state_client> (*it)._user_kdf;
// save data for later, so we don't lock things too much
Conn0_C_AUTH prev_data (std::get<state_client>(*it)._pkt->stream[0].data());
......@@ -1243,7 +1254,8 @@ FENRIR_INLINE void Handshake::parse_s_result (const Link_ID recv_from,
std::move(ecc_write),
std::move(enc_read),
std::move(hmac_read),
std::move(ecc_read)
std::move(ecc_read),
std::move(user_kdf)
);
if (conn == nullptr) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment