1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/include/llarp/crypto_async.h

230 lines
5.3 KiB
C
Raw Normal View History

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