lokinet/llarp/crypto/crypto.hpp

153 lines
3.8 KiB
C++
Raw Normal View History

2018-12-12 00:38:58 +00:00
#ifndef LLARP_CRYPTO_HPP
#define LLARP_CRYPTO_HPP
#include <crypto/constants.hpp>
#include <crypto/types.hpp>
2019-02-02 23:12:42 +00:00
#include <util/buffer.hpp>
2018-12-13 21:28:50 +00:00
#include <absl/base/optimization.h>
2018-12-13 21:28:50 +00:00
#include <functional>
2019-07-30 23:42:13 +00:00
#include <cstdint>
2018-12-13 21:28:50 +00:00
/**
* crypto.hpp
2018-12-13 21:28:50 +00:00
*
* libsodium abstraction layer
* potentially allow libssl support in the future
*/
2018-12-12 00:38:58 +00:00
namespace llarp
{
2018-12-13 21:28:50 +00:00
/// library crypto configuration
struct Crypto
{
virtual ~Crypto() = 0;
/// xchacha symmetric cipher
virtual bool
xchacha20(const llarp_buffer_t &, const SharedSecret &,
const TunnelNonce &) = 0;
/// xchacha symmetric cipher (multibuffer)
virtual bool
xchacha20_alt(const llarp_buffer_t &, const llarp_buffer_t &,
const SharedSecret &, const byte_t *) = 0;
2018-12-13 21:28:50 +00:00
/// path dh creator's side
virtual bool
dh_client(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) = 0;
2018-12-13 21:28:50 +00:00
/// path dh relay side
virtual bool
dh_server(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) = 0;
2018-12-13 21:28:50 +00:00
/// transport dh client side
virtual bool
transport_dh_client(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) = 0;
2018-12-13 21:28:50 +00:00
/// transport dh server side
virtual bool
transport_dh_server(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) = 0;
2018-12-13 21:28:50 +00:00
/// blake2b 256 bit
virtual bool
shorthash(ShortHash &, const llarp_buffer_t &) = 0;
2020-01-27 21:30:41 +00:00
/// blake2s 256 bit "hmac" (keyed hash)
virtual bool
hmac(byte_t *, const llarp_buffer_t &, const SharedSecret &) = 0;
2018-12-13 21:28:50 +00:00
/// ed25519 sign
virtual bool
sign(Signature &, const SecretKey &, const llarp_buffer_t &) = 0;
2018-12-13 21:28:50 +00:00
/// ed25519 verify
virtual bool
verify(const PubKey &, const llarp_buffer_t &, const Signature &) = 0;
2020-01-27 21:30:41 +00:00
/// derive sub keys for public keys
virtual bool
derive_subkey(PubKey &, const PubKey &, uint64_t) = 0;
/// derive sub keys for secret keys
virtual bool
derive_subkey_secret(SecretKey &, const SecretKey &, uint64_t) = 0;
/// seed to secretkey
virtual bool
seed_to_secretkey(llarp::SecretKey &, const llarp::IdentitySecret &) = 0;
2018-12-13 21:28:50 +00:00
/// randomize buffer
virtual void
randomize(const llarp_buffer_t &) = 0;
2018-12-13 21:28:50 +00:00
/// randomizer memory
virtual void
2019-05-28 19:45:09 +00:00
randbytes(byte_t *, size_t) = 0;
2018-12-13 21:28:50 +00:00
/// generate signing keypair
virtual void
identity_keygen(SecretKey &) = 0;
2018-12-13 21:28:50 +00:00
/// generate encryption keypair
virtual void
encryption_keygen(SecretKey &) = 0;
2018-12-13 21:28:50 +00:00
/// generate post quantum encrytion key
virtual void
pqe_keygen(PQKeyPair &) = 0;
2018-12-13 21:28:50 +00:00
/// post quantum decrypt (buffer, sharedkey_dst, sec)
virtual bool
pqe_decrypt(const PQCipherBlock &, SharedSecret &, const byte_t *) = 0;
2018-12-13 21:28:50 +00:00
/// post quantum encrypt (buffer, sharedkey_dst, pub)
virtual bool
pqe_encrypt(PQCipherBlock &, SharedSecret &, const PQPubKey &) = 0;
2019-11-27 01:40:55 +00:00
virtual bool
check_identity_privkey(const SecretKey &) = 0;
2018-12-13 21:28:50 +00:00
};
2019-07-30 23:42:13 +00:00
inline Crypto::~Crypto() = default;
2018-12-13 21:28:50 +00:00
/// return random 64bit unsigned interger
uint64_t
randint();
const byte_t *
seckey_topublic(const SecretKey &secret);
2018-12-13 21:28:50 +00:00
const byte_t *
pq_keypair_to_public(const PQKeyPair &keypair);
2018-12-13 21:28:50 +00:00
const byte_t *
pq_keypair_to_secret(const PQKeyPair &keypair);
2018-12-12 00:38:58 +00:00
struct CryptoManager
{
private:
static Crypto *m_crypto;
Crypto *m_prevCrypto;
public:
CryptoManager(Crypto *crypto) : m_prevCrypto(m_crypto)
{
m_crypto = crypto;
}
~CryptoManager()
{
m_crypto = m_prevCrypto;
}
static Crypto *
instance() ABSL_ATTRIBUTE_RETURNS_NONNULL
{
if(ABSL_PREDICT_TRUE(m_crypto))
{
return m_crypto;
}
2019-07-16 00:40:28 +00:00
assert(false && "Cryptomanager::instance() was undefined");
abort();
}
};
2018-12-12 00:38:58 +00:00
} // namespace llarp
#endif