2018-01-25 17:24:33 +01:00
|
|
|
#ifndef LLARP_CRYPTO_H_
|
|
|
|
#define LLARP_CRYPTO_H_
|
|
|
|
#include <llarp/buffer.h>
|
2018-11-20 00:27:59 +01:00
|
|
|
#include <llarp/common.hpp>
|
2018-01-25 17:24:33 +01:00
|
|
|
#include <stdbool.h>
|
2018-01-29 15:27:24 +01:00
|
|
|
#include <stdint.h>
|
2018-05-25 11:17:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* crypto.h
|
|
|
|
*
|
|
|
|
* libsodium abstraction layer
|
|
|
|
* potentially allow libssl support in the future
|
|
|
|
*/
|
|
|
|
|
2018-01-25 17:24:33 +01:00
|
|
|
#define PUBKEYSIZE 32
|
2018-02-01 23:04:58 +01:00
|
|
|
#define SECKEYSIZE 64
|
2018-05-22 17:54:19 +02:00
|
|
|
#define NONCESIZE 24
|
2018-01-25 17:24:33 +01:00
|
|
|
#define SHAREDKEYSIZE 32
|
|
|
|
#define HASHSIZE 64
|
2018-05-18 18:08:47 +02:00
|
|
|
#define SHORTHASHSIZE 32
|
2018-01-25 17:24:33 +01:00
|
|
|
#define HMACSECSIZE 32
|
|
|
|
#define SIGSIZE 64
|
2018-05-22 17:54:19 +02:00
|
|
|
#define TUNNONCESIZE 32
|
2018-05-20 15:43:42 +02:00
|
|
|
#define HMACSIZE 32
|
2018-06-19 19:11:24 +02:00
|
|
|
#define PATHIDSIZE 16
|
2018-01-25 17:24:33 +01:00
|
|
|
|
2018-08-14 01:22:31 +02: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 23:46:22 +01:00
|
|
|
#define PQ_KEYPAIRSIZE (PQ_SECRETKEYSIZE + PQ_PUBKEYSIZE)
|
2018-05-22 17:54:19 +02:00
|
|
|
|
2018-05-25 11:17:08 +02:00
|
|
|
/// label functors
|
2018-05-28 16:26:16 +02:00
|
|
|
|
2018-06-20 19:45:44 +02:00
|
|
|
/// PKE(result, publickey, secretkey, nonce)
|
2018-08-14 01:22:31 +02:00
|
|
|
typedef bool (*llarp_path_dh_func)(byte_t *, const byte_t *, const byte_t *,
|
|
|
|
const byte_t *);
|
2018-05-22 17:54:19 +02:00
|
|
|
|
2018-06-22 15:59:28 +02:00
|
|
|
/// TKE(result, publickey, secretkey, nonce)
|
2018-08-14 01:22:31 +02:00
|
|
|
typedef bool (*llarp_transport_dh_func)(byte_t *, const byte_t *,
|
|
|
|
const byte_t *, const byte_t *);
|
2018-05-18 18:08:47 +02:00
|
|
|
|
2018-05-28 16:26:16 +02:00
|
|
|
/// SD/SE(buffer, key, nonce)
|
2018-06-12 13:57:14 +02:00
|
|
|
typedef bool (*llarp_sym_cipher_func)(llarp_buffer_t, const byte_t *,
|
|
|
|
const byte_t *);
|
2018-02-01 23:04:58 +01:00
|
|
|
|
2018-05-28 16:26:16 +02:00
|
|
|
/// H(result, body)
|
2018-05-22 17:54:19 +02:00
|
|
|
typedef bool (*llarp_hash_func)(byte_t *, llarp_buffer_t);
|
2018-02-01 23:04:58 +01:00
|
|
|
|
2018-05-28 16:26:16 +02:00
|
|
|
/// SH(result, body)
|
2018-05-22 17:54:19 +02:00
|
|
|
typedef bool (*llarp_shorthash_func)(byte_t *, llarp_buffer_t);
|
2018-05-18 18:08:47 +02:00
|
|
|
|
2018-05-28 16:26:16 +02:00
|
|
|
/// MDS(result, body, shared_secret)
|
2018-05-22 17:54:19 +02:00
|
|
|
typedef bool (*llarp_hmac_func)(byte_t *, llarp_buffer_t, const byte_t *);
|
2018-02-01 23:34:04 +01:00
|
|
|
|
2018-05-28 16:26:16 +02:00
|
|
|
/// S(sig, secretkey, body)
|
2018-05-22 17:54:19 +02:00
|
|
|
typedef bool (*llarp_sign_func)(byte_t *, const byte_t *, llarp_buffer_t);
|
2018-01-31 20:59:26 +01:00
|
|
|
|
2018-08-31 15:51:24 +02:00
|
|
|
/// V(pubkey, body, sig)
|
2018-05-22 17:54:19 +02:00
|
|
|
typedef bool (*llarp_verify_func)(const byte_t *, llarp_buffer_t,
|
|
|
|
const byte_t *);
|
2018-02-01 23:34:04 +01:00
|
|
|
|
2018-05-25 11:17:08 +02:00
|
|
|
/// library crypto configuration
|
2018-05-22 17:54:19 +02:00
|
|
|
struct llarp_crypto
|
|
|
|
{
|
2018-05-28 16:26:16 +02:00
|
|
|
/// xchacha symettric cipher
|
2018-02-01 23:04:58 +01:00
|
|
|
llarp_sym_cipher_func xchacha20;
|
2018-05-28 16:26:16 +02:00
|
|
|
/// path dh creator's side
|
2018-06-10 16:05:48 +02:00
|
|
|
llarp_path_dh_func dh_client;
|
2018-05-28 16:26:16 +02:00
|
|
|
/// path dh relay side
|
2018-06-10 16:05:48 +02:00
|
|
|
llarp_path_dh_func dh_server;
|
2018-05-28 16:26:16 +02:00
|
|
|
/// transport dh client side
|
2018-05-18 18:08:47 +02:00
|
|
|
llarp_transport_dh_func transport_dh_client;
|
2018-05-28 16:26:16 +02:00
|
|
|
/// transport dh server side
|
2018-05-18 18:08:47 +02:00
|
|
|
llarp_transport_dh_func transport_dh_server;
|
2018-05-28 16:26:16 +02:00
|
|
|
/// blake2b 512 bit
|
2018-02-01 23:04:58 +01:00
|
|
|
llarp_hash_func hash;
|
2018-05-28 16:26:16 +02:00
|
|
|
/// blake2b 256 bit
|
2018-05-18 18:08:47 +02:00
|
|
|
llarp_shorthash_func shorthash;
|
2018-05-28 16:26:16 +02:00
|
|
|
/// blake2s 256 bit hmac
|
2018-02-01 23:04:58 +01:00
|
|
|
llarp_hmac_func hmac;
|
2018-05-28 16:26:16 +02:00
|
|
|
/// ed25519 sign
|
2018-02-01 23:04:58 +01:00
|
|
|
llarp_sign_func sign;
|
2018-05-28 16:26:16 +02:00
|
|
|
/// ed25519 verify
|
2018-02-01 23:04:58 +01:00
|
|
|
llarp_verify_func verify;
|
2018-05-28 16:26:16 +02:00
|
|
|
/// randomize buffer
|
2018-01-31 20:59:26 +01:00
|
|
|
void (*randomize)(llarp_buffer_t);
|
2018-05-28 16:26:16 +02:00
|
|
|
/// randomizer memory
|
2018-02-01 23:04:58 +01:00
|
|
|
void (*randbytes)(void *, size_t);
|
2018-05-28 16:26:16 +02:00
|
|
|
/// generate signing keypair
|
2018-05-23 22:37:43 +02:00
|
|
|
void (*identity_keygen)(byte_t *);
|
2018-05-28 16:26:16 +02:00
|
|
|
/// generate encryption keypair
|
2018-05-23 22:37:43 +02:00
|
|
|
void (*encryption_keygen)(byte_t *);
|
2018-08-14 01:22:31 +02:00
|
|
|
/// generate post quantum encrytion key
|
|
|
|
void (*pqe_keygen)(byte_t *);
|
|
|
|
/// post quantum decrypt (buffer, sharedkey_dst, sec)
|
|
|
|
bool (*pqe_decrypt)(const byte_t *, byte_t *, const byte_t *);
|
|
|
|
/// post quantum encrypt (buffer, sharedkey_dst, pub)
|
|
|
|
bool (*pqe_encrypt)(byte_t *, byte_t *, const byte_t *);
|
2018-01-29 15:27:24 +01:00
|
|
|
};
|
2018-01-25 17:24:33 +01:00
|
|
|
|
2018-09-02 20:25:42 +02:00
|
|
|
/// initialize crypto subsystem
|
2018-05-22 17:54:19 +02:00
|
|
|
void
|
2018-09-02 20:25:42 +02:00
|
|
|
llarp_crypto_init(struct llarp_crypto *c);
|
2018-05-22 17:54:19 +02:00
|
|
|
|
2018-05-28 16:26:16 +02:00
|
|
|
/// check for initialize crypto
|
2018-05-22 17:54:19 +02:00
|
|
|
bool
|
|
|
|
llarp_crypto_initialized(struct llarp_crypto *c);
|
2018-01-25 17:24:33 +01:00
|
|
|
|
2018-07-20 06:50:28 +02:00
|
|
|
/// return random 64bit unsigned interger
|
|
|
|
uint64_t
|
|
|
|
llarp_randint();
|
|
|
|
|
2018-01-25 17:24:33 +01:00
|
|
|
#endif
|