pull/1/head
Jeff Becker 6 years ago
parent 02d785d30c
commit 24caf1fc8c
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -124,7 +124,7 @@ bdecode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result)
buffer->cur++; buffer->cur++;
len = llarp_buffer_size_left(buffer); len = llarp_buffer_size_left(*buffer);
if(len < slen) if(len < slen)
return false; return false;
@ -157,7 +157,7 @@ bdecode_read_dict(llarp_buffer_t* buff, struct dict_reader* r)
if(*r->buffer->cur != 'd') // ensure is a dictionary if(*r->buffer->cur != 'd') // ensure is a dictionary
return false; return false;
r->buffer->cur++; r->buffer->cur++;
while(llarp_buffer_size_left(r->buffer) && *r->buffer->cur != 'e') while(llarp_buffer_size_left(*r->buffer) && *r->buffer->cur != 'e')
{ {
if(bdecode_read_string(r->buffer, &strbuf)) if(bdecode_read_string(r->buffer, &strbuf))
{ {
@ -180,8 +180,8 @@ struct list_reader
/// information to on_item /// information to on_item
void* user; void* user;
/** /**
* called when we got an element, return true to continue iteration * called with true when we got an element, return true to continue iteration
* called with null item on iteration completion * called with false on iteration completion
*/ */
bool (*on_item)(struct list_reader*, bool); bool (*on_item)(struct list_reader*, bool);
}; };
@ -194,7 +194,7 @@ bdecode_read_list(llarp_buffer_t* buff, struct list_reader* r)
return false; return false;
r->buffer->cur++; r->buffer->cur++;
while(llarp_buffer_size_left(r->buffer) && *r->buffer->cur != 'e') while(llarp_buffer_size_left(*r->buffer) && *r->buffer->cur != 'e')
{ {
if(!r->on_item(r, true)) // check for early abort if(!r->on_item(r, true)) // check for early abort
return false; return false;

@ -10,7 +10,7 @@
/** /**
* buffer.h * buffer.h
* *
* buffer used for bencoding * generic memory buffer
*/ */
#ifdef __cplusplus #ifdef __cplusplus
@ -19,6 +19,29 @@ extern "C" {
typedef uint8_t byte_t; typedef uint8_t byte_t;
/**
llarp_buffer_t represents a region of memory that is ONLY
accessable and valid in the current scope.
rules:
ALWAYS copy the contents of the buffer
if that data is to be used outside the current scope.
ALWAYS pass a llarp_buffer_t * if you plan on modifying
the data associated with the buffer
ALWAYS pass a llarp_buffer_t * if you plan on advancing
the stream position
ALWAYS pass a llarp_buffer_t if you are doing a read
only operation that does not modify anything
NEVER dallocate the pointers in the buffer
NEVER use llarp_buffer_t ** (double pointer)
*/
typedef struct llarp_buffer_t typedef struct llarp_buffer_t
{ {
/// starting memory address /// starting memory address
@ -31,7 +54,7 @@ typedef struct llarp_buffer_t
/// how much room is left in buffer /// how much room is left in buffer
size_t size_t
llarp_buffer_size_left(llarp_buffer_t *buff); llarp_buffer_size_left(llarp_buffer_t buff);
/// write a chunk of data size "sz" /// write a chunk of data size "sz"
bool bool

@ -43,53 +43,72 @@ byte_t *
llarp_seckey_topublic(byte_t *secret); llarp_seckey_topublic(byte_t *secret);
/// label functors /// label functors
/// PKE(result, publickey, nonce, secretkey)
typedef bool (*llarp_dh_func)(llarp_sharedkey_t *, llarp_pubkey_t, typedef bool (*llarp_dh_func)(llarp_sharedkey_t *, llarp_pubkey_t,
llarp_tunnel_nonce_t, llarp_seckey_t); llarp_tunnel_nonce_t, llarp_seckey_t);
/// TKE(result publickey, secretkey, nonce)
typedef bool (*llarp_transport_dh_func)(byte_t *, byte_t *, byte_t *, byte_t *); typedef bool (*llarp_transport_dh_func)(byte_t *, byte_t *, byte_t *, byte_t *);
/// SD/SE(buffer, key, nonce)
typedef bool (*llarp_sym_cipher_func)(llarp_buffer_t, llarp_sharedkey_t, typedef bool (*llarp_sym_cipher_func)(llarp_buffer_t, llarp_sharedkey_t,
llarp_nonce_t); llarp_nonce_t);
/// H(result, body)
typedef bool (*llarp_hash_func)(byte_t *, llarp_buffer_t); typedef bool (*llarp_hash_func)(byte_t *, llarp_buffer_t);
/// SH(result, body)
typedef bool (*llarp_shorthash_func)(byte_t *, llarp_buffer_t); typedef bool (*llarp_shorthash_func)(byte_t *, llarp_buffer_t);
/// MDS(result, body, shared_secret)
typedef bool (*llarp_hmac_func)(byte_t *, llarp_buffer_t, const byte_t *); typedef bool (*llarp_hmac_func)(byte_t *, llarp_buffer_t, const byte_t *);
/// S(sig, secretkey, body)
typedef bool (*llarp_sign_func)(byte_t *, const byte_t *, llarp_buffer_t); typedef bool (*llarp_sign_func)(byte_t *, const byte_t *, llarp_buffer_t);
/// V(sig, body, secretkey)
typedef bool (*llarp_verify_func)(const byte_t *, llarp_buffer_t, typedef bool (*llarp_verify_func)(const byte_t *, llarp_buffer_t,
const byte_t *); const byte_t *);
typedef bool (*llarp_frame_crypto_func)(llarp_buffer_t *, const byte_t *);
/// library crypto configuration /// library crypto configuration
struct llarp_crypto struct llarp_crypto
{ {
/// xchacha symettric cipher
llarp_sym_cipher_func xchacha20; llarp_sym_cipher_func xchacha20;
/// path dh creator's side
llarp_dh_func dh_client; llarp_dh_func dh_client;
/// path dh relay side
llarp_dh_func dh_server; llarp_dh_func dh_server;
/// transport dh client side
llarp_transport_dh_func transport_dh_client; llarp_transport_dh_func transport_dh_client;
/// transport dh server side
llarp_transport_dh_func transport_dh_server; llarp_transport_dh_func transport_dh_server;
/// blake2b 512 bit
llarp_hash_func hash; llarp_hash_func hash;
/// blake2b 256 bit
llarp_shorthash_func shorthash; llarp_shorthash_func shorthash;
/// blake2s 256 bit hmac
llarp_hmac_func hmac; llarp_hmac_func hmac;
/// ed25519 sign
llarp_sign_func sign; llarp_sign_func sign;
/// ed25519 verify
llarp_verify_func verify; llarp_verify_func verify;
llarp_frame_crypto_func encrypt_frame; /// randomize buffer
llarp_frame_crypto_func decrypt_frame;
void (*randomize)(llarp_buffer_t); void (*randomize)(llarp_buffer_t);
/// randomizer memory
void (*randbytes)(void *, size_t); void (*randbytes)(void *, size_t);
/// generate signing keypair
void (*identity_keygen)(byte_t *); void (*identity_keygen)(byte_t *);
/// generate encryption keypair
void (*encryption_keygen)(byte_t *); void (*encryption_keygen)(byte_t *);
}; };
/// allocate crypto /// set crypto function pointers to use libsodium
void void
llarp_crypto_libsodium_init(struct llarp_crypto *c); llarp_crypto_libsodium_init(struct llarp_crypto *c);
/// initialize crypto /// check for initialize crypto
bool bool
llarp_crypto_initialized(struct llarp_crypto *c); llarp_crypto_initialized(struct llarp_crypto *c);

@ -15,10 +15,15 @@
extern "C" { extern "C" {
#endif #endif
/// context for doing asynchronous crpytography for iwp
/// with a worker threadpool
/// defined in crypto_async.cpp /// defined in crypto_async.cpp
struct llarp_async_iwp; struct llarp_async_iwp;
/// allocator /// allocator
/// 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 * struct llarp_async_iwp *
llarp_async_iwp_new(struct llarp_crypto *crypto, struct llarp_logic *logic, llarp_async_iwp_new(struct llarp_crypto *crypto, struct llarp_logic *logic,
struct llarp_threadpool *worker); struct llarp_threadpool *worker);
@ -41,11 +46,11 @@ struct iwp_async_keygen
void *user; void *user;
/// destination key buffer /// destination key buffer
uint8_t *keybuf; uint8_t *keybuf;
/// iteration functor /// result handler callback
iwp_keygen_hook hook; iwp_keygen_hook hook;
}; };
/// generate a key by iterating on "iwp" using "keygen" request /// generate an encryption keypair asynchronously
void void
iwp_call_async_keygen(struct llarp_async_iwp *iwp, iwp_call_async_keygen(struct llarp_async_iwp *iwp,
struct iwp_async_keygen *keygen); struct iwp_async_keygen *keygen);
@ -72,11 +77,12 @@ struct iwp_async_intro
iwp_intro_hook hook; iwp_intro_hook hook;
}; };
/// introduce internal wire protocol "iwp" using "intro" request /// asynchronously generate an intro packet
void void
iwp_call_async_gen_intro(struct llarp_async_iwp *iwp, iwp_call_async_gen_intro(struct llarp_async_iwp *iwp,
struct iwp_async_intro *intro); struct iwp_async_intro *intro);
/// asynchronously verify an intro packet
void void
iwp_call_async_verify_intro(struct llarp_async_iwp *iwp, iwp_call_async_verify_intro(struct llarp_async_iwp *iwp,
struct iwp_async_intro *info); struct iwp_async_intro *info);
@ -105,12 +111,12 @@ struct iwp_async_introack
iwp_introack_hook hook; iwp_introack_hook hook;
}; };
/// generate introduction acknowledgement "iwp" using "introack" request /// generate introduction acknowledgement packet asynchronously
void void
iwp_call_async_gen_introack(struct llarp_async_iwp *iwp, iwp_call_async_gen_introack(struct llarp_async_iwp *iwp,
struct iwp_async_introack *introack); struct iwp_async_introack *introack);
/// verify introduction acknowledgement "iwp" using "introack" request /// verify introduction acknowledgement packet asynchronously
void void
iwp_call_async_verify_introack(struct llarp_async_iwp *iwp, iwp_call_async_verify_introack(struct llarp_async_iwp *iwp,
struct iwp_async_introack *introack); struct iwp_async_introack *introack);
@ -127,20 +133,26 @@ struct iwp_async_session_start
void *user; void *user;
uint8_t *buf; uint8_t *buf;
size_t sz; size_t sz;
/// nonce parameter
uint8_t *nonce; uint8_t *nonce;
/// token parameter
uint8_t *token; uint8_t *token;
/// memory to write session key to
uint8_t *sessionkey; uint8_t *sessionkey;
/// local secrkey key
uint8_t *secretkey; uint8_t *secretkey;
/// remote public encryption key
uint8_t *remote_pubkey; uint8_t *remote_pubkey;
/// result callback handler
iwp_session_start_hook hook; iwp_session_start_hook hook;
}; };
/// generate session start "iwp" using "start" request /// generate session start packet asynchronously
void void
iwp_call_async_gen_session_start(struct llarp_async_iwp *iwp, iwp_call_async_gen_session_start(struct llarp_async_iwp *iwp,
struct iwp_async_session_start *start); struct iwp_async_session_start *start);
/// verify session start "iwp" using "start" request /// verify session start packet asynchronously
void void
iwp_call_async_verify_session_start(struct llarp_async_iwp *iwp, iwp_call_async_verify_session_start(struct llarp_async_iwp *iwp,
struct iwp_async_session_start *start); struct iwp_async_session_start *start);
@ -152,21 +164,26 @@ typedef void (*iwp_async_frame_hook)(struct iwp_async_frame *);
struct iwp_async_frame struct iwp_async_frame
{ {
/// true if decryption succeded
bool success; bool success;
struct llarp_async_iwp *iwp; struct llarp_async_iwp *iwp;
void *user; void *user;
/// current session key
uint8_t *sessionkey; uint8_t *sessionkey;
/// size of the frame
size_t sz; size_t sz;
/// result handler
iwp_async_frame_hook hook; iwp_async_frame_hook hook;
/// memory holding the entire frame
uint8_t buf[1500]; uint8_t buf[1500];
}; };
/// decrypt iwp frame "iwp" using "frame" request /// decrypt iwp frame asynchronously
void void
iwp_call_async_frame_decrypt(struct llarp_async_iwp *iwp, iwp_call_async_frame_decrypt(struct llarp_async_iwp *iwp,
struct iwp_async_frame *frame); struct iwp_async_frame *frame);
/// encrypt iwp frame "iwp" using "frame" request /// encrypt iwp frame asynchronously
void void
iwp_call_async_frame_encrypt(struct llarp_async_iwp *iwp, iwp_call_async_frame_encrypt(struct llarp_async_iwp *iwp,
struct iwp_async_frame *frame); struct iwp_async_frame *frame);

@ -5,20 +5,20 @@
extern "C" { extern "C" {
size_t size_t
llarp_buffer_size_left(llarp_buffer_t* buff) llarp_buffer_size_left(llarp_buffer_t buff)
{ {
size_t diff = buff->cur - buff->base; size_t diff = buff.cur - buff.base;
if(diff > buff->sz) if(diff > buff.sz)
return 0; return 0;
else else
return buff->sz - diff; return buff.sz - diff;
} }
bool bool
llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...) llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...)
{ {
int written; int written;
ssize_t sz = llarp_buffer_size_left(buff); ssize_t sz = llarp_buffer_size_left(*buff);
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
written = vsnprintf((char*)buff->cur, sz, fmt, args); written = vsnprintf((char*)buff->cur, sz, fmt, args);
@ -34,7 +34,7 @@ llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...)
bool bool
llarp_buffer_write(llarp_buffer_t* buff, const void* data, size_t sz) llarp_buffer_write(llarp_buffer_t* buff, const void* data, size_t sz)
{ {
size_t left = llarp_buffer_size_left(buff); size_t left = llarp_buffer_size_left(*buff);
if(left >= sz) if(left >= sz)
{ {
memcpy(buff->cur, data, sz); memcpy(buff->cur, data, sz);
@ -60,7 +60,7 @@ llarp_buffer_read_until(llarp_buffer_t* buff, char delim, byte_t* result,
read++; read++;
} }
if(llarp_buffer_size_left(buff)) if(llarp_buffer_size_left(*buff))
return read; return read;
else else
return 0; return 0;

Loading…
Cancel
Save