lokinet/include/llarp/crypto_async.h

230 lines
5.3 KiB
C
Raw Normal View History

2018-01-31 19:59:26 +00:00
#ifndef LLARP_CRYPTO_ASYNC_H_
#define LLARP_CRYPTO_ASYNC_H_
#include <llarp/crypto.h>
#include <llarp/ev.h>
2018-05-18 20:08:57 +00:00
#include <llarp/logic.h>
#include <llarp/threadpool.h>
2018-06-26 01:30:36 +00:00
#include <llarp/time.h>
2018-01-31 19:59:26 +00:00
2018-05-25 09:17:08 +00:00
/**
* crypto_async.h
*
* asynchronous crypto functions
*/
/// context for doing asynchronous cryptography for iwp
2018-05-28 14:26:16 +00:00
/// with a worker threadpool
2018-05-25 09:17:08 +00:00
/// defined in crypto_async.cpp
2018-05-18 20:08:57 +00:00
struct llarp_async_iwp;
2018-05-25 09:17:08 +00:00
/// allocator
2018-05-28 14:26:16 +00:00
/// use crypto as cryptograph implementation
/// use logic as the callback handler thread
/// use worker as threadpool that does the heavy lifting
struct llarp_async_iwp *
llarp_async_iwp_new(struct llarp_crypto *crypto, struct llarp_logic *logic,
struct llarp_threadpool *worker);
2018-05-25 09:17:08 +00:00
/// deallocator
2018-05-22 19:19:06 +00:00
void
llarp_async_iwp_free(struct llarp_async_iwp *iwp);
2018-05-18 20:08:57 +00:00
struct iwp_async_keygen;
2018-05-25 09:17:08 +00:00
/// define functor for keygen
2018-05-18 20:08:57 +00:00
typedef void (*iwp_keygen_hook)(struct iwp_async_keygen *);
2018-05-25 09:17:08 +00:00
/// key generation request
2018-05-18 20:08:57 +00:00
struct iwp_async_keygen
{
2018-05-25 09:17:08 +00:00
/// internal wire protocol async configuration
struct llarp_async_iwp *iwp;
/// a pointer to pass ourself to thread worker
void *user;
2018-05-25 09:17:08 +00:00
/// destination key buffer
uint8_t *keybuf;
2018-05-28 14:26:16 +00:00
/// result handler callback
2018-05-18 20:08:57 +00:00
iwp_keygen_hook hook;
};
2018-05-28 14:26:16 +00:00
/// generate an encryption keypair asynchronously
void
iwp_call_async_keygen(struct llarp_async_iwp *iwp,
struct iwp_async_keygen *keygen);
2018-05-18 20:08:57 +00:00
2018-05-19 13:36:42 +00:00
struct iwp_async_intro;
2018-05-25 09:17:08 +00:00
/// iwp_async_intro functor
2018-05-19 17:21:56 +00:00
typedef void (*iwp_intro_hook)(struct iwp_async_intro *);
2018-05-25 09:17:08 +00:00
/// iwp_async_intro request
2018-05-19 13:36:42 +00:00
struct iwp_async_intro
2018-05-18 20:08:57 +00:00
{
struct llarp_async_iwp *iwp;
void *user;
uint8_t *buf;
2018-05-18 20:08:57 +00:00
size_t sz;
2018-05-25 09:17:08 +00:00
/// nonce paramter
2018-08-29 20:40:26 +00:00
uint8_t nonce[32];
2018-05-25 09:17:08 +00:00
/// remote public key
2018-08-29 20:40:26 +00:00
uint8_t remote_pubkey[32];
2018-05-25 09:17:08 +00:00
/// local private key
2018-08-29 20:40:26 +00:00
uint8_t secretkey[64];
2018-05-25 09:17:08 +00:00
/// callback
2018-05-19 17:21:56 +00:00
iwp_intro_hook hook;
2018-05-18 20:08:57 +00:00
};
2018-05-28 14:26:16 +00:00
/// asynchronously generate an intro packet
void
iwp_call_async_gen_intro(struct llarp_async_iwp *iwp,
struct iwp_async_intro *intro);
2018-05-18 20:08:57 +00:00
2018-05-28 14:26:16 +00:00
/// asynchronously verify an intro packet
2018-05-23 20:37:43 +00:00
void
iwp_call_async_verify_intro(struct llarp_async_iwp *iwp,
struct iwp_async_intro *info);
2018-05-19 13:36:42 +00:00
struct iwp_async_introack;
2018-05-25 09:17:08 +00:00
/// introduction acknowledgement functor
2018-05-19 17:21:56 +00:00
typedef void (*iwp_introack_hook)(struct iwp_async_introack *);
2018-05-25 09:17:08 +00:00
/// introduction acknowledgement request
2018-05-19 13:36:42 +00:00
struct iwp_async_introack
2018-05-18 20:08:57 +00:00
{
struct llarp_async_iwp *iwp;
void *user;
uint8_t *buf;
2018-05-18 20:08:57 +00:00
size_t sz;
2018-05-25 09:17:08 +00:00
/// nonce paramter
uint8_t *nonce;
2018-05-25 09:17:08 +00:00
/// token paramter
uint8_t *token;
2018-05-25 09:17:08 +00:00
/// remote public key
uint8_t *remote_pubkey;
2018-05-25 09:17:08 +00:00
/// local private key
uint8_t *secretkey;
2018-05-25 09:17:08 +00:00
/// callback
2018-05-19 17:21:56 +00:00
iwp_introack_hook hook;
2018-05-18 20:08:57 +00:00
};
2018-05-28 14:26:16 +00:00
/// generate introduction acknowledgement packet asynchronously
void
iwp_call_async_gen_introack(struct llarp_async_iwp *iwp,
struct iwp_async_introack *introack);
2018-05-18 20:08:57 +00:00
2018-05-28 14:26:16 +00:00
/// verify introduction acknowledgement packet asynchronously
void
iwp_call_async_verify_introack(struct llarp_async_iwp *iwp,
struct iwp_async_introack *introack);
2018-05-19 17:21:56 +00:00
struct iwp_async_session_start;
2018-05-25 09:17:08 +00:00
/// start session functor
2018-05-19 17:21:56 +00:00
typedef void (*iwp_session_start_hook)(struct iwp_async_session_start *);
2018-05-25 09:17:08 +00:00
/// start session request
2018-05-19 17:21:56 +00:00
struct iwp_async_session_start
2018-05-19 13:36:42 +00:00
{
struct llarp_async_iwp *iwp;
void *user;
uint8_t *buf;
2018-05-19 13:36:42 +00:00
size_t sz;
2018-05-28 14:26:16 +00:00
/// nonce parameter
uint8_t *nonce;
2018-05-28 14:26:16 +00:00
/// token parameter
uint8_t *token;
2018-05-28 14:26:16 +00:00
/// memory to write session key to
uint8_t *sessionkey;
2018-05-28 14:26:16 +00:00
/// local secrkey key
uint8_t *secretkey;
2018-05-28 14:26:16 +00:00
/// remote public encryption key
uint8_t *remote_pubkey;
2018-05-28 14:26:16 +00:00
/// result callback handler
2018-05-19 17:21:56 +00:00
iwp_session_start_hook hook;
2018-05-19 13:36:42 +00:00
};
2018-05-19 17:21:56 +00:00
2018-05-28 14:26:16 +00:00
/// generate session start packet asynchronously
void
iwp_call_async_gen_session_start(struct llarp_async_iwp *iwp,
struct iwp_async_session_start *start);
2018-05-19 17:21:56 +00:00
2018-05-28 14:26:16 +00:00
/// verify session start packet asynchronously
void
iwp_call_async_verify_session_start(struct llarp_async_iwp *iwp,
struct iwp_async_session_start *start);
2018-05-19 17:21:56 +00:00
struct iwp_async_frame;
2018-05-25 09:17:08 +00:00
/// internal wire protocol frame request
typedef void (*iwp_async_frame_hook)(struct iwp_async_frame *);
2018-05-19 17:21:56 +00:00
struct iwp_async_frame
{
2018-05-28 14:26:16 +00:00
/// true if decryption succeded
2018-05-19 17:21:56 +00:00
bool success;
2018-06-26 01:30:36 +00:00
/// timestamp for CoDel
llarp_time_t created;
struct llarp_async_iwp *iwp;
/// a pointer to pass ourself
void *user;
2018-05-28 14:26:16 +00:00
/// current session key
2018-06-06 12:46:26 +00:00
byte_t *sessionkey;
2018-05-28 14:26:16 +00:00
/// size of the frame
2018-05-19 17:21:56 +00:00
size_t sz;
2018-05-28 14:26:16 +00:00
/// result handler
2018-05-19 17:21:56 +00:00
iwp_async_frame_hook hook;
2018-05-28 14:26:16 +00:00
/// memory holding the entire frame
2018-06-06 12:46:26 +00:00
byte_t buf[1500];
2018-05-19 17:21:56 +00:00
};
#ifdef __cplusplus
#include <memory>
struct FramePutTime
{
void
2018-08-31 14:41:04 +00:00
operator()(iwp_async_frame &frame) const
{
2018-08-31 14:41:04 +00:00
frame.created = llarp_time_now_ms();
}
};
struct FrameGetTime
{
llarp_time_t
2018-08-31 14:41:04 +00:00
operator()(const iwp_async_frame &frame) const
{
2018-08-31 14:41:04 +00:00
return frame.created;
}
};
2018-07-20 04:50:28 +00:00
struct FrameCompareTime
{
bool
2018-08-31 14:41:04 +00:00
operator()(const iwp_async_frame &left, const iwp_async_frame &right) const
2018-07-20 04:50:28 +00:00
{
2018-08-31 14:41:04 +00:00
return left.created < right.created;
2018-07-20 04:50:28 +00:00
}
};
#endif
2018-07-20 04:50:28 +00:00
2018-06-26 01:30:36 +00:00
/// synchronously decrypt a frame
bool
iwp_decrypt_frame(struct iwp_async_frame *frame);
/// synchronosuly encrypt a frame
bool
iwp_encrypt_frame(struct iwp_async_frame *frame);
2018-05-28 14:26:16 +00:00
/// decrypt iwp frame asynchronously
void
iwp_call_async_frame_decrypt(struct llarp_async_iwp *iwp,
struct iwp_async_frame *frame);
2018-05-19 17:21:56 +00:00
2018-05-28 14:26:16 +00:00
/// encrypt iwp frame asynchronously
void
iwp_call_async_frame_encrypt(struct llarp_async_iwp *iwp,
struct iwp_async_frame *frame);
2018-05-19 17:21:56 +00:00
2018-01-31 19:59:26 +00:00
#endif