You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/crypto/encrypted_frame.cpp

120 lines
2.9 KiB
C++

#include <crypto/encrypted_frame.hpp>
#include <crypto/crypto.hpp>
#include <util/logger.hpp>
#include <util/mem.hpp>
namespace llarp
{
6 years ago
bool
EncryptedFrame::EncryptInPlace(const SecretKey& ourSecretKey,
const PubKey& otherPubkey,
llarp::Crypto* crypto)
6 years ago
{
// format of frame is
// <32 bytes keyed hash of following data>
// <32 bytes nonce>
// <32 bytes pubkey>
// <N bytes encrypted payload>
//
byte_t* hash = data();
byte_t* noncePtr = hash + SHORTHASHSIZE;
byte_t* pubkey = noncePtr + TUNNONCESIZE;
byte_t* body = pubkey + PUBKEYSIZE;
6 years ago
SharedSecret shared;
6 years ago
llarp_buffer_t buf;
buf.base = body;
buf.cur = buf.base;
buf.sz = size() - EncryptedFrameOverheadSize;
6 years ago
// set our pubkey
memcpy(pubkey, ourSecretKey.toPublic().data(), PUBKEYSIZE);
6 years ago
// randomize nonce
crypto->randbytes(noncePtr, TUNNONCESIZE);
TunnelNonce nonce(noncePtr);
6 years ago
// derive shared key
if(!crypto->dh_client(shared, otherPubkey, ourSecretKey, nonce))
6 years ago
{
llarp::LogError("DH failed");
6 years ago
return false;
}
6 years ago
6 years ago
// encrypt body
if(!crypto->xchacha20(buf, shared, nonce))
6 years ago
{
llarp::LogError("encrypt failed");
6 years ago
return false;
}
// generate message auth
buf.base = noncePtr;
6 years ago
buf.cur = buf.base;
buf.sz = size() - SHORTHASHSIZE;
6 years ago
if(!crypto->hmac(hash, buf, shared))
6 years ago
{
llarp::LogError("Failed to generate message auth");
6 years ago
return false;
}
return true;
}
bool
EncryptedFrame::DecryptInPlace(const SecretKey& ourSecretKey,
llarp::Crypto* crypto)
{
6 years ago
// format of frame is
// <32 bytes keyed hash of following data>
// <32 bytes nonce>
// <32 bytes pubkey>
// <N bytes encrypted payload>
//
ShortHash hash(data());
byte_t* noncePtr = data() + SHORTHASHSIZE;
byte_t* body = data() + EncryptedFrameOverheadSize;
TunnelNonce nonce(noncePtr);
PubKey otherPubkey(noncePtr + TUNNONCESIZE);
6 years ago
SharedSecret shared;
6 years ago
// use dh_server because we are not the creator of this message
if(!crypto->dh_server(shared, otherPubkey, ourSecretKey, nonce))
6 years ago
{
llarp::LogError("DH failed");
6 years ago
return false;
}
llarp_buffer_t buf;
buf.base = noncePtr;
buf.cur = buf.base;
buf.sz = size() - SHORTHASHSIZE;
ShortHash digest;
if(!crypto->hmac(digest.data(), buf, shared))
6 years ago
{
llarp::LogError("Digest failed");
6 years ago
return false;
}
if(!std::equal(digest.begin(), digest.end(), hash.begin()))
6 years ago
{
llarp::LogError("message authentication failed");
6 years ago
return false;
}
6 years ago
buf.base = body;
buf.cur = body;
buf.sz = size() - EncryptedFrameOverheadSize;
6 years ago
if(!crypto->xchacha20(buf, shared, nonce))
6 years ago
{
llarp::LogError("decrypt failed");
6 years ago
return false;
}
return true;
}
} // namespace llarp