2018-01-25 16:24:33 +00:00
|
|
|
#ifndef LLARP_CRYPTO_H_
|
|
|
|
#define LLARP_CRYPTO_H_
|
|
|
|
#include <llarp/buffer.h>
|
2018-11-19 23:27:59 +00:00
|
|
|
#include <llarp/common.hpp>
|
2018-12-11 00:53:11 +00:00
|
|
|
#include <functional>
|
2018-01-25 16:24:33 +00:00
|
|
|
#include <stdbool.h>
|
2018-01-29 14:27:24 +00:00
|
|
|
#include <stdint.h>
|
2018-05-25 09:17:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* crypto.h
|
|
|
|
*
|
|
|
|
* libsodium abstraction layer
|
|
|
|
* potentially allow libssl support in the future
|
|
|
|
*/
|
|
|
|
|
2018-12-11 00:53:11 +00:00
|
|
|
static constexpr uint32_t PUBKEYSIZE = 32;
|
|
|
|
static constexpr uint32_t SECKEYSIZE = 64;
|
|
|
|
static constexpr uint32_t NONCESIZE = 24;
|
|
|
|
static constexpr uint32_t SHAREDKEYSIZE = 32;
|
|
|
|
static constexpr uint32_t HASHSIZE = 64;
|
|
|
|
static constexpr uint32_t SHORTHASHSIZE = 32;
|
|
|
|
static constexpr uint32_t HMACSECSIZE = 32;
|
|
|
|
static constexpr uint32_t SIGSIZE = 64;
|
|
|
|
static constexpr uint32_t TUNNONCESIZE = 32;
|
|
|
|
static constexpr uint32_t HMACSIZE = 32;
|
|
|
|
static constexpr uint32_t PATHIDSIZE = 16;
|
2018-01-25 16:24:33 +00:00
|
|
|
|
2018-08-13 23:22:31 +00:00
|
|
|
#include <libntrup/ntru.h>
|
|
|
|
|
|
|
|
#define PQ_CIPHERTEXTSIZE crypto_kem_CIPHERTEXTBYTES
|
|
|
|
#define PQ_PUBKEYSIZE crypto_kem_PUBLICKEYBYTES
|
|
|
|
#define PQ_SECRETKEYSIZE crypto_kem_SECRETKEYBYTES
|
2018-11-26 22:46:22 +00:00
|
|
|
#define PQ_KEYPAIRSIZE (PQ_SECRETKEYSIZE + PQ_PUBKEYSIZE)
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-12-11 00:53:11 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2018-05-25 09:17:08 +00:00
|
|
|
/// label functors
|
2018-05-28 14:26:16 +00:00
|
|
|
|
2018-06-20 17:45:44 +00:00
|
|
|
/// PKE(result, publickey, secretkey, nonce)
|
2018-12-11 00:53:11 +00:00
|
|
|
using path_dh_func = std::function<bool(byte_t *, const byte_t *, const byte_t *,
|
|
|
|
const byte_t *)>;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-06-22 13:59:28 +00:00
|
|
|
/// TKE(result, publickey, secretkey, nonce)
|
2018-12-11 00:53:11 +00:00
|
|
|
using transport_dh_func = std::function<bool(byte_t *, const byte_t *,
|
|
|
|
const byte_t *, const byte_t *)>;
|
2018-05-18 16:08:47 +00:00
|
|
|
|
2018-05-28 14:26:16 +00:00
|
|
|
/// SD/SE(buffer, key, nonce)
|
2018-12-11 00:53:11 +00:00
|
|
|
using sym_cipher_func = std::function<bool(llarp_buffer_t, const byte_t *,
|
|
|
|
const byte_t *)>;
|
2018-02-01 22:04:58 +00:00
|
|
|
|
2018-05-28 14:26:16 +00:00
|
|
|
/// H(result, body)
|
2018-12-11 00:53:11 +00:00
|
|
|
using hash_func = std::function<bool(byte_t *, llarp_buffer_t)>;
|
2018-02-01 22:04:58 +00:00
|
|
|
|
2018-05-28 14:26:16 +00:00
|
|
|
/// SH(result, body)
|
2018-12-11 00:53:11 +00:00
|
|
|
using shorthash_func = std::function<bool(byte_t *, llarp_buffer_t)>;
|
2018-05-18 16:08:47 +00:00
|
|
|
|
2018-05-28 14:26:16 +00:00
|
|
|
/// MDS(result, body, shared_secret)
|
2018-12-11 00:53:11 +00:00
|
|
|
using hmac_func = std::function<bool(byte_t *, llarp_buffer_t, const byte_t *)>;
|
2018-02-01 22:34:04 +00:00
|
|
|
|
2018-05-28 14:26:16 +00:00
|
|
|
/// S(sig, secretkey, body)
|
2018-12-11 00:53:11 +00:00
|
|
|
using sign_func = std::function<bool(byte_t *, const byte_t *, llarp_buffer_t)>;
|
2018-01-31 19:59:26 +00:00
|
|
|
|
2018-08-31 13:51:24 +00:00
|
|
|
/// V(pubkey, body, sig)
|
2018-12-11 00:53:11 +00:00
|
|
|
using verify_func = std::function<bool(const byte_t *, llarp_buffer_t,
|
|
|
|
const byte_t *)>;
|
2018-02-01 22:34:04 +00:00
|
|
|
|
2018-05-25 09:17:08 +00:00
|
|
|
/// library crypto configuration
|
2018-12-11 00:53:11 +00:00
|
|
|
struct Crypto
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-28 14:26:16 +00:00
|
|
|
/// xchacha symettric cipher
|
2018-12-11 00:53:11 +00:00
|
|
|
sym_cipher_func xchacha20;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// path dh creator's side
|
2018-12-11 00:53:11 +00:00
|
|
|
path_dh_func dh_client;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// path dh relay side
|
2018-12-11 00:53:11 +00:00
|
|
|
path_dh_func dh_server;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// transport dh client side
|
2018-12-11 00:53:11 +00:00
|
|
|
transport_dh_func transport_dh_client;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// transport dh server side
|
2018-12-11 00:53:11 +00:00
|
|
|
transport_dh_func transport_dh_server;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// blake2b 512 bit
|
2018-12-11 00:53:11 +00:00
|
|
|
hash_func hash;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// blake2b 256 bit
|
2018-12-11 00:53:11 +00:00
|
|
|
shorthash_func shorthash;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// blake2s 256 bit hmac
|
2018-12-11 00:53:11 +00:00
|
|
|
hmac_func hmac;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// ed25519 sign
|
2018-12-11 00:53:11 +00:00
|
|
|
sign_func sign;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// ed25519 verify
|
2018-12-11 00:53:11 +00:00
|
|
|
verify_func verify;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// randomize buffer
|
2018-12-11 00:53:11 +00:00
|
|
|
std::function<void(llarp_buffer_t)> randomize;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// randomizer memory
|
2018-12-11 00:53:11 +00:00
|
|
|
std::function<void(void *, size_t)> randbytes;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// generate signing keypair
|
2018-12-11 00:53:11 +00:00
|
|
|
std::function<void(byte_t *)> identity_keygen;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// generate encryption keypair
|
2018-12-11 00:53:11 +00:00
|
|
|
std::function<void(byte_t *)> encryption_keygen;
|
2018-08-13 23:22:31 +00:00
|
|
|
/// generate post quantum encrytion key
|
2018-12-11 00:53:11 +00:00
|
|
|
std::function<void(byte_t *)> pqe_keygen;
|
2018-08-13 23:22:31 +00:00
|
|
|
/// post quantum decrypt (buffer, sharedkey_dst, sec)
|
2018-12-11 00:53:11 +00:00
|
|
|
std::function<bool(const byte_t *, byte_t *, const byte_t *)> pqe_decrypt;
|
2018-08-13 23:22:31 +00:00
|
|
|
/// post quantum encrypt (buffer, sharedkey_dst, pub)
|
2018-12-11 00:53:11 +00:00
|
|
|
std::function<bool(byte_t *, byte_t *, const byte_t *)> pqe_encrypt;
|
2018-01-25 16:24:33 +00:00
|
|
|
|
2018-12-11 00:53:11 +00:00
|
|
|
// Give a basic type tag for the constructor to pick libsodium
|
|
|
|
struct sodium {};
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-12-11 00:53:11 +00:00
|
|
|
Crypto(Crypto::sodium tag);
|
|
|
|
};
|
2018-01-25 16:24:33 +00:00
|
|
|
|
2018-07-20 04:50:28 +00:00
|
|
|
/// return random 64bit unsigned interger
|
|
|
|
uint64_t
|
2018-12-11 00:53:11 +00:00
|
|
|
randint();
|
|
|
|
|
|
|
|
}
|
2018-07-20 04:50:28 +00:00
|
|
|
|
2018-01-25 16:24:33 +00:00
|
|
|
#endif
|