2021-03-09 22:24:35 +00:00
|
|
|
#include "encrypted_frame.hpp"
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "crypto.hpp"
|
2022-07-16 00:41:14 +00:00
|
|
|
#include <llarp/util/logging.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/mem.hpp>
|
2018-06-10 14:05:48 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-06-11 13:44:49 +00:00
|
|
|
bool
|
2019-06-04 18:31:17 +00:00
|
|
|
EncryptedFrame::DoEncrypt(const SharedSecret& shared, bool noDH)
|
2018-06-11 13:44:49 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
byte_t* hash = data();
|
2019-01-02 01:04:06 +00:00
|
|
|
byte_t* noncePtr = hash + SHORTHASHSIZE;
|
2020-04-07 18:38:56 +00:00
|
|
|
byte_t* pubkey = noncePtr + TUNNONCESIZE;
|
|
|
|
byte_t* body = pubkey + PUBKEYSIZE;
|
2018-06-11 13:44:49 +00:00
|
|
|
|
2019-06-04 18:31:17 +00:00
|
|
|
auto crypto = CryptoManager::instance();
|
|
|
|
|
|
|
|
// if noDH flag, means key exchange has already taken place
|
|
|
|
// in this case, set pubkey to random noise and choose a
|
|
|
|
// random nonce here
|
2020-04-07 18:38:56 +00:00
|
|
|
if (noDH)
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
crypto->randbytes(noncePtr, TUNNONCESIZE);
|
|
|
|
crypto->randbytes(pubkey, PUBKEYSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
TunnelNonce nonce(noncePtr);
|
2018-06-11 13:44:49 +00:00
|
|
|
|
|
|
|
llarp_buffer_t buf;
|
|
|
|
buf.base = body;
|
2020-04-07 18:38:56 +00:00
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = size() - EncryptedFrameOverheadSize;
|
2018-06-11 13:44:49 +00:00
|
|
|
|
|
|
|
// encrypt body
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!crypto->xchacha20(buf, shared, nonce))
|
2018-06-11 13:44:49 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("encrypt failed");
|
2018-06-11 13:44:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate message auth
|
2019-01-02 01:04:06 +00:00
|
|
|
buf.base = noncePtr;
|
2020-04-07 18:38:56 +00:00
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = size() - SHORTHASHSIZE;
|
2018-06-11 13:44:49 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!crypto->hmac(hash, buf, shared))
|
2018-06-11 13:44:49 +00:00
|
|
|
{
|
2019-01-02 01:04:03 +00:00
|
|
|
llarp::LogError("Failed to generate message auth");
|
2018-06-11 13:44:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-04 18:31:17 +00:00
|
|
|
|
2018-06-11 13:44:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-10 14:05:48 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
EncryptedFrame::EncryptInPlace(const SecretKey& ourSecretKey, const PubKey& otherPubkey)
|
2018-06-10 14:05:48 +00:00
|
|
|
{
|
2018-06-11 13:25:10 +00:00
|
|
|
// format of frame is
|
|
|
|
// <32 bytes keyed hash of following data>
|
|
|
|
// <32 bytes nonce>
|
|
|
|
// <32 bytes pubkey>
|
|
|
|
// <N bytes encrypted payload>
|
|
|
|
//
|
2020-04-07 18:38:56 +00:00
|
|
|
byte_t* hash = data();
|
2019-06-04 18:31:17 +00:00
|
|
|
byte_t* noncePtr = hash + SHORTHASHSIZE;
|
2020-04-07 18:38:56 +00:00
|
|
|
byte_t* pubkey = noncePtr + TUNNONCESIZE;
|
2018-06-11 13:25:10 +00:00
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
SharedSecret shared;
|
2018-06-11 13:25:10 +00:00
|
|
|
|
2019-05-28 19:45:08 +00:00
|
|
|
auto crypto = CryptoManager::instance();
|
|
|
|
|
2019-06-04 18:31:17 +00:00
|
|
|
// set our pubkey
|
|
|
|
memcpy(pubkey, ourSecretKey.toPublic().data(), PUBKEYSIZE);
|
|
|
|
// randomize nonce
|
|
|
|
crypto->randbytes(noncePtr, TUNNONCESIZE);
|
|
|
|
TunnelNonce nonce(noncePtr);
|
|
|
|
|
|
|
|
// derive shared key
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!crypto->dh_client(shared, otherPubkey, ourSecretKey, nonce))
|
2018-06-11 13:25:10 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("DH failed");
|
2018-06-11 13:25:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:31:17 +00:00
|
|
|
return DoEncrypt(shared, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
EncryptedFrame::DoDecrypt(const SharedSecret& shared)
|
|
|
|
{
|
|
|
|
ShortHash hash(data());
|
|
|
|
byte_t* noncePtr = data() + SHORTHASHSIZE;
|
2020-04-07 18:38:56 +00:00
|
|
|
byte_t* body = data() + EncryptedFrameOverheadSize;
|
2019-06-04 18:31:17 +00:00
|
|
|
TunnelNonce nonce(noncePtr);
|
|
|
|
|
|
|
|
auto crypto = CryptoManager::instance();
|
|
|
|
|
2019-01-02 01:04:06 +00:00
|
|
|
llarp_buffer_t buf;
|
|
|
|
buf.base = noncePtr;
|
2020-04-07 18:38:56 +00:00
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = size() - SHORTHASHSIZE;
|
2019-01-02 01:04:06 +00:00
|
|
|
|
|
|
|
ShortHash digest;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!crypto->hmac(digest.data(), buf, shared))
|
2018-06-11 13:25:10 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("Digest failed");
|
2018-06-11 13:25:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!std::equal(digest.begin(), digest.end(), hash.begin()))
|
2018-06-11 13:25:10 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("message authentication failed");
|
2018-06-11 13:25:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:44:49 +00:00
|
|
|
buf.base = body;
|
2020-04-07 18:38:56 +00:00
|
|
|
buf.cur = body;
|
|
|
|
buf.sz = size() - EncryptedFrameOverheadSize;
|
2018-06-11 13:44:49 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!crypto->xchacha20(buf, shared, nonce))
|
2018-06-11 13:25:10 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("decrypt failed");
|
2018-06-11 13:25:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-04 18:31:17 +00:00
|
|
|
|
2018-06-11 13:25:10 +00:00
|
|
|
return true;
|
2018-06-10 14:05:48 +00:00
|
|
|
}
|
2019-06-04 18:31:17 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
EncryptedFrame::DecryptInPlace(const SecretKey& ourSecretKey)
|
|
|
|
{
|
|
|
|
// format of frame is
|
|
|
|
// <32 bytes keyed hash of following data>
|
|
|
|
// <32 bytes nonce>
|
|
|
|
// <32 bytes pubkey>
|
|
|
|
// <N bytes encrypted payload>
|
|
|
|
//
|
|
|
|
byte_t* noncePtr = data() + SHORTHASHSIZE;
|
|
|
|
TunnelNonce nonce(noncePtr);
|
|
|
|
PubKey otherPubkey(noncePtr + TUNNONCESIZE);
|
|
|
|
|
|
|
|
SharedSecret shared;
|
|
|
|
|
|
|
|
auto crypto = CryptoManager::instance();
|
|
|
|
|
|
|
|
// use dh_server because we are not the creator of this message
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!crypto->dh_server(shared, otherPubkey, ourSecretKey, nonce))
|
2019-06-04 18:31:17 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("DH failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DoDecrypt(shared);
|
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
} // namespace llarp
|