2018-12-12 01:38:58 +01:00
|
|
|
#include <crypto.hpp>
|
2018-12-12 03:52:51 +01:00
|
|
|
#include <encrypted_frame.hpp>
|
|
|
|
#include <logger.hpp>
|
|
|
|
#include <mem.hpp>
|
2018-06-10 16:05:48 +02:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-06-11 15:44:49 +02:00
|
|
|
bool
|
2019-01-02 02:04:04 +01:00
|
|
|
EncryptedFrame::EncryptInPlace(const SecretKey& ourSecretKey,
|
|
|
|
const PubKey& otherPubkey,
|
2018-12-11 01:53:11 +01:00
|
|
|
llarp::Crypto* crypto)
|
2018-06-11 15:44:49 +02:00
|
|
|
{
|
|
|
|
// format of frame is
|
|
|
|
// <32 bytes keyed hash of following data>
|
|
|
|
// <32 bytes nonce>
|
|
|
|
// <32 bytes pubkey>
|
|
|
|
// <N bytes encrypted payload>
|
|
|
|
//
|
2018-06-19 19:11:24 +02:00
|
|
|
byte_t* hash = data();
|
2018-06-12 13:57:14 +02:00
|
|
|
byte_t* nonce = hash + SHORTHASHSIZE;
|
|
|
|
byte_t* pubkey = nonce + TUNNONCESIZE;
|
|
|
|
byte_t* body = pubkey + PUBKEYSIZE;
|
2018-06-11 15:44:49 +02:00
|
|
|
|
2018-06-12 13:57:14 +02:00
|
|
|
SharedSecret shared;
|
2018-06-11 15:44:49 +02:00
|
|
|
|
|
|
|
auto DH = crypto->dh_client;
|
|
|
|
auto Encrypt = crypto->xchacha20;
|
|
|
|
auto MDS = crypto->hmac;
|
|
|
|
|
|
|
|
llarp_buffer_t buf;
|
|
|
|
buf.base = body;
|
|
|
|
buf.cur = buf.base;
|
2018-12-20 17:49:05 +01:00
|
|
|
buf.sz = size() - EncryptedFrameOverheadSize;
|
2018-06-11 15:44:49 +02:00
|
|
|
|
|
|
|
// set our pubkey
|
2018-06-12 13:57:14 +02:00
|
|
|
memcpy(pubkey, llarp::seckey_topublic(ourSecretKey), PUBKEYSIZE);
|
2018-06-11 15:44:49 +02:00
|
|
|
// randomize nonce
|
2018-06-12 13:57:14 +02:00
|
|
|
crypto->randbytes(nonce, TUNNONCESIZE);
|
2018-06-11 15:44:49 +02:00
|
|
|
|
|
|
|
// derive shared key
|
2018-06-20 14:34:48 +02:00
|
|
|
if(!DH(shared, otherPubkey, ourSecretKey, nonce))
|
2018-06-11 15:44:49 +02:00
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogError("DH failed");
|
2018-06-11 15:44:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-20 14:34:48 +02:00
|
|
|
|
2018-06-11 15:44:49 +02:00
|
|
|
// encrypt body
|
|
|
|
if(!Encrypt(buf, shared, nonce))
|
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogError("encrypt failed");
|
2018-06-11 15:44:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate message auth
|
|
|
|
buf.base = nonce;
|
|
|
|
buf.cur = buf.base;
|
2018-06-19 19:11:24 +02:00
|
|
|
buf.sz = size() - SHORTHASHSIZE;
|
2018-06-11 15:44:49 +02:00
|
|
|
|
|
|
|
if(!MDS(hash, buf, shared))
|
|
|
|
{
|
2019-01-02 02:04:03 +01:00
|
|
|
llarp::LogError("Failed to generate message auth");
|
2018-06-11 15:44:49 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-10 16:05:48 +02:00
|
|
|
bool
|
2019-01-02 02:04:04 +01:00
|
|
|
EncryptedFrame::DecryptInPlace(const SecretKey& ourSecretKey,
|
2018-12-11 01:53:11 +01:00
|
|
|
llarp::Crypto* crypto)
|
2018-06-10 16:05:48 +02:00
|
|
|
{
|
2018-06-11 15:25:10 +02:00
|
|
|
// format of frame is
|
|
|
|
// <32 bytes keyed hash of following data>
|
|
|
|
// <32 bytes nonce>
|
|
|
|
// <32 bytes pubkey>
|
|
|
|
// <N bytes encrypted payload>
|
|
|
|
//
|
2018-06-19 19:11:24 +02:00
|
|
|
byte_t* hash = data();
|
2018-06-12 13:57:14 +02:00
|
|
|
byte_t* nonce = hash + SHORTHASHSIZE;
|
|
|
|
byte_t* otherPubkey = nonce + TUNNONCESIZE;
|
|
|
|
byte_t* body = otherPubkey + PUBKEYSIZE;
|
2018-06-11 15:25:10 +02:00
|
|
|
|
2019-01-02 02:04:03 +01:00
|
|
|
// use dh_server because we are not the creator of this message
|
2018-06-11 15:25:10 +02:00
|
|
|
auto DH = crypto->dh_server;
|
|
|
|
auto Decrypt = crypto->xchacha20;
|
2018-06-11 15:44:49 +02:00
|
|
|
auto MDS = crypto->hmac;
|
2018-06-11 15:25:10 +02:00
|
|
|
|
|
|
|
llarp_buffer_t buf;
|
2018-06-11 15:44:49 +02:00
|
|
|
buf.base = nonce;
|
2018-06-11 15:25:10 +02:00
|
|
|
buf.cur = buf.base;
|
2018-06-19 19:11:24 +02:00
|
|
|
buf.sz = size() - SHORTHASHSIZE;
|
2018-06-11 15:25:10 +02:00
|
|
|
|
2018-06-12 13:57:14 +02:00
|
|
|
SharedSecret shared;
|
|
|
|
ShortHash digest;
|
2018-06-11 15:25:10 +02:00
|
|
|
|
2018-06-20 14:34:48 +02:00
|
|
|
if(!DH(shared, otherPubkey, ourSecretKey, nonce))
|
2018-06-11 15:25:10 +02:00
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogError("DH failed");
|
2018-06-11 15:25:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-02 02:04:04 +01:00
|
|
|
if(!MDS(digest.data(), buf, shared))
|
2018-06-11 15:25:10 +02:00
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogError("Digest failed");
|
2018-06-11 15:25:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-02 02:04:04 +01:00
|
|
|
if(!std::equal(digest.begin(), digest.end(), hash))
|
2018-06-11 15:25:10 +02:00
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogError("message authentication failed");
|
2018-06-11 15:25:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:44:49 +02:00
|
|
|
buf.base = body;
|
|
|
|
buf.cur = body;
|
2018-12-20 17:49:05 +01:00
|
|
|
buf.sz = size() - EncryptedFrameOverheadSize;
|
2018-06-11 15:44:49 +02:00
|
|
|
|
2018-06-11 15:25:10 +02:00
|
|
|
if(!Decrypt(buf, shared, nonce))
|
|
|
|
{
|
2018-07-05 17:44:06 +02:00
|
|
|
llarp::LogError("decrypt failed");
|
2018-06-11 15:25:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-06-10 16:05:48 +02:00
|
|
|
}
|
2018-06-19 00:03:50 +02:00
|
|
|
} // namespace llarp
|