2018-12-12 00:38:58 +00:00
|
|
|
#ifndef LLARP_CRYPTO_HPP
|
|
|
|
#define LLARP_CRYPTO_HPP
|
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
#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 <functional>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
/**
|
2019-01-11 00:12:43 +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
|
|
|
/// PKE(result, publickey, secretkey, nonce)
|
2019-01-02 01:04:04 +00:00
|
|
|
using path_dh_func = std::function< bool(
|
|
|
|
SharedSecret &, const PubKey &, const SecretKey &, const TunnelNonce &) >;
|
2018-12-12 00:38:58 +00:00
|
|
|
|
2018-12-13 21:28:50 +00:00
|
|
|
/// TKE(result, publickey, secretkey, nonce)
|
|
|
|
using transport_dh_func = std::function< bool(
|
2019-01-02 01:04:04 +00:00
|
|
|
SharedSecret &, const PubKey &, const SecretKey &, const TunnelNonce &) >;
|
2018-12-13 21:28:50 +00:00
|
|
|
|
|
|
|
/// SH(result, body)
|
2019-02-01 01:58:06 +00:00
|
|
|
using shorthash_func =
|
|
|
|
std::function< bool(ShortHash &, const llarp_buffer_t &) >;
|
2018-12-13 21:28:50 +00:00
|
|
|
|
|
|
|
/// library crypto configuration
|
|
|
|
struct Crypto
|
|
|
|
{
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual ~Crypto() = 0;
|
|
|
|
|
2019-01-02 01:04:03 +00:00
|
|
|
/// xchacha symmetric cipher
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual bool
|
2019-02-01 01:58:06 +00:00
|
|
|
xchacha20(const llarp_buffer_t &, const SharedSecret &,
|
|
|
|
const TunnelNonce &) = 0;
|
2019-01-26 15:40:58 +00:00
|
|
|
|
2019-01-02 01:04:03 +00:00
|
|
|
/// xchacha symmetric cipher (multibuffer)
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual bool
|
2019-02-01 01:58:06 +00:00
|
|
|
xchacha20_alt(const llarp_buffer_t &, const llarp_buffer_t &,
|
|
|
|
const SharedSecret &, const byte_t *) = 0;
|
2019-01-26 15:40:58 +00:00
|
|
|
|
2018-12-13 21:28:50 +00:00
|
|
|
/// path dh creator's side
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual bool
|
|
|
|
dh_client(SharedSecret &, const PubKey &, const SecretKey &,
|
|
|
|
const TunnelNonce &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// path dh relay side
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual bool
|
|
|
|
dh_server(SharedSecret &, const PubKey &, const SecretKey &,
|
|
|
|
const TunnelNonce &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// transport dh client side
|
2019-01-26 15:40:58 +00:00
|
|
|
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
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual bool
|
|
|
|
transport_dh_server(SharedSecret &, const PubKey &, const SecretKey &,
|
|
|
|
const TunnelNonce &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// blake2b 512 bit
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual bool
|
2019-02-01 01:58:06 +00:00
|
|
|
hash(byte_t *, const llarp_buffer_t &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// blake2b 256 bit
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual bool
|
2019-02-01 01:58:06 +00:00
|
|
|
shorthash(ShortHash &, const llarp_buffer_t &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// blake2s 256 bit hmac
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual bool
|
2019-02-01 01:58:06 +00:00
|
|
|
hmac(byte_t *, const llarp_buffer_t &, const SharedSecret &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// ed25519 sign
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual bool
|
2019-02-01 01:58:06 +00:00
|
|
|
sign(Signature &, const SecretKey &, const llarp_buffer_t &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// ed25519 verify
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual bool
|
2019-02-01 01:58:06 +00:00
|
|
|
verify(const PubKey &, const llarp_buffer_t &, const Signature &) = 0;
|
2019-01-21 15:45:18 +00:00
|
|
|
/// seed to secretkey
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual bool
|
|
|
|
seed_to_secretkey(llarp::SecretKey &, const llarp::IdentitySecret &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// randomize buffer
|
2019-02-01 01:58:06 +00:00
|
|
|
virtual void
|
|
|
|
randomize(const llarp_buffer_t &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// randomizer memory
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual void
|
|
|
|
randbytes(void *, size_t) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// generate signing keypair
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual void
|
|
|
|
identity_keygen(SecretKey &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// generate encryption keypair
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual void
|
|
|
|
encryption_keygen(SecretKey &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// generate post quantum encrytion key
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual void
|
|
|
|
pqe_keygen(PQKeyPair &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
/// post quantum decrypt (buffer, sharedkey_dst, sec)
|
2019-01-26 15:40:58 +00:00
|
|
|
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)
|
2019-01-26 15:40:58 +00:00
|
|
|
virtual bool
|
|
|
|
pqe_encrypt(PQCipherBlock &, SharedSecret &, const PQPubKey &) = 0;
|
2018-12-13 21:28:50 +00:00
|
|
|
};
|
|
|
|
|
2019-01-26 15:40:58 +00:00
|
|
|
inline Crypto::~Crypto()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:28:50 +00:00
|
|
|
/// return random 64bit unsigned interger
|
|
|
|
uint64_t
|
|
|
|
randint();
|
|
|
|
|
|
|
|
const byte_t *
|
2019-01-02 01:04:04 +00:00
|
|
|
seckey_topublic(const SecretKey &secret);
|
2018-12-13 21:28:50 +00:00
|
|
|
|
|
|
|
const byte_t *
|
2019-01-02 01:04:04 +00:00
|
|
|
pq_keypair_to_public(const PQKeyPair &keypair);
|
2018-12-13 21:28:50 +00:00
|
|
|
|
|
|
|
const byte_t *
|
2019-01-02 01:04:04 +00:00
|
|
|
pq_keypair_to_secret(const PQKeyPair &keypair);
|
2018-12-12 00:38:58 +00:00
|
|
|
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif
|