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>
|
2018-05-22 15:54:19 +00:00
|
|
|
#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
|
|
|
|
*/
|
|
|
|
|
2018-05-31 13:08:06 +00:00
|
|
|
/// 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
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_async_iwp *
|
2018-05-25 17:52:10 +00:00
|
|
|
llarp_async_iwp_new(struct llarp_crypto *crypto, struct llarp_logic *logic,
|
|
|
|
struct llarp_threadpool *worker);
|
2018-05-22 15:54:19 +00:00
|
|
|
|
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
|
2018-05-22 15:54:19 +00:00
|
|
|
struct llarp_async_iwp *iwp;
|
2018-05-31 13:08:06 +00:00
|
|
|
/// a pointer to pass ourself to thread worker
|
2018-05-22 15:54:19 +00:00
|
|
|
void *user;
|
2018-05-25 09:17:08 +00:00
|
|
|
/// destination key buffer
|
2018-05-22 15:54:19 +00:00
|
|
|
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
|
2018-05-22 15:54:19 +00:00
|
|
|
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-22 15:54:19 +00:00
|
|
|
|
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-22 15:54:19 +00:00
|
|
|
|
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
|
|
|
{
|
2018-05-22 15:54:19 +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-05-22 15:54:19 +00:00
|
|
|
uint8_t *nonce;
|
2018-05-25 09:17:08 +00:00
|
|
|
/// remote public key
|
2018-05-22 15:54:19 +00:00
|
|
|
uint8_t *remote_pubkey;
|
2018-05-25 09:17:08 +00:00
|
|
|
/// local private key
|
2018-05-22 15:54:19 +00:00
|
|
|
uint8_t *secretkey;
|
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
|
2018-05-22 15:54:19 +00:00
|
|
|
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-22 15:54:19 +00:00
|
|
|
|
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-22 15:54:19 +00:00
|
|
|
|
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
|
|
|
{
|
2018-05-22 15:54:19 +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-05-22 15:54:19 +00:00
|
|
|
uint8_t *nonce;
|
2018-05-25 09:17:08 +00:00
|
|
|
/// token paramter
|
2018-05-22 15:54:19 +00:00
|
|
|
uint8_t *token;
|
2018-05-25 09:17:08 +00:00
|
|
|
/// remote public key
|
2018-05-22 15:54:19 +00:00
|
|
|
uint8_t *remote_pubkey;
|
2018-05-25 09:17:08 +00:00
|
|
|
/// local private key
|
2018-05-22 15:54:19 +00:00
|
|
|
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
|
2018-05-22 15:54:19 +00:00
|
|
|
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
|
2018-05-22 15:54:19 +00:00
|
|
|
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-22 15:54:19 +00:00
|
|
|
|
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
|
|
|
{
|
2018-05-22 15:54:19 +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
|
2018-05-22 15:54:19 +00:00
|
|
|
uint8_t *nonce;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// token parameter
|
2018-05-22 15:54:19 +00:00
|
|
|
uint8_t *token;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// memory to write session key to
|
2018-05-22 15:54:19 +00:00
|
|
|
uint8_t *sessionkey;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// local secrkey key
|
2018-05-22 15:54:19 +00:00
|
|
|
uint8_t *secretkey;
|
2018-05-28 14:26:16 +00:00
|
|
|
/// remote public encryption key
|
2018-05-22 15:54:19 +00:00
|
|
|
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
|
2018-05-22 15:54:19 +00:00
|
|
|
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
|
2018-05-22 15:54:19 +00:00
|
|
|
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
|
2018-05-22 15:54:19 +00:00
|
|
|
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;
|
2018-05-24 16:04:34 +00:00
|
|
|
struct llarp_async_iwp *iwp;
|
2018-05-31 13:08:06 +00:00
|
|
|
/// a pointer to pass ourself
|
2018-05-24 16:04:34 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-08-17 19:49:58 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
#include <memory>
|
|
|
|
|
2018-07-09 03:25:20 +00:00
|
|
|
struct FramePutTime
|
|
|
|
{
|
|
|
|
void
|
|
|
|
operator()(iwp_async_frame *frame) const
|
|
|
|
{
|
|
|
|
frame->created = llarp_time_now_ms();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct FrameGetTime
|
|
|
|
{
|
|
|
|
llarp_time_t
|
|
|
|
operator()(const iwp_async_frame *frame) const
|
|
|
|
{
|
|
|
|
return frame->created;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-20 04:50:28 +00:00
|
|
|
struct FrameCompareTime
|
|
|
|
{
|
|
|
|
bool
|
2018-08-23 12:26:31 +00:00
|
|
|
operator()(const iwp_async_frame *left, const iwp_async_frame *right) const
|
2018-07-20 04:50:28 +00:00
|
|
|
{
|
|
|
|
return left->created < right->created;
|
|
|
|
}
|
|
|
|
};
|
2018-08-17 19:49:58 +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
|
2018-05-22 15:54:19 +00:00
|
|
|
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
|
2018-05-22 15:54:19 +00:00
|
|
|
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
|