From 43f9be08c8064697263d1e4aac5ce12e5f5e3cca Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 1 Feb 2018 17:34:04 -0500 Subject: [PATCH] format --- include/llarp/crypto.h | 15 ++++----- include/llarp/crypto_async.h | 54 +++++++++++++++---------------- include/llarp/ev.h | 2 ++ llarp/crypto_async.c | 61 +++++++++++++++++------------------- llarp/crypto_libsodium.cpp | 9 +++--- llarp/ev.cpp | 18 +++++++++++ test/test_async_cipher.c | 21 ++++++------- test/test_async_dh.c | 17 +++++----- 8 files changed, 103 insertions(+), 94 deletions(-) diff --git a/include/llarp/crypto.h b/include/llarp/crypto.h index e232faa85..256abb414 100644 --- a/include/llarp/crypto.h +++ b/include/llarp/crypto.h @@ -27,23 +27,24 @@ typedef uint8_t llarp_hmacsec_t[HMACSECSIZE]; typedef uint8_t llarp_sig_t[SIGSIZE]; typedef uint8_t llarp_tunnel_nounce_t[TUNNOUNCESIZE]; -static inline uint8_t * llarp_seckey_topublic(llarp_seckey_t k) -{ +static inline uint8_t *llarp_seckey_topublic(llarp_seckey_t k) { return k + 32; } - + typedef bool (*llarp_dh_func)(llarp_sharedkey_t *, llarp_pubkey_t, llarp_tunnel_nounce_t, llarp_seckey_t); -typedef bool (*llarp_sym_cipher_func)(llarp_buffer_t, llarp_sharedkey_t, llarp_nounce_t); +typedef bool (*llarp_sym_cipher_func)(llarp_buffer_t, llarp_sharedkey_t, + llarp_nounce_t); typedef bool (*llarp_hash_func)(llarp_hash_t *, llarp_buffer_t); -typedef bool (*llarp_hmac_func)(llarp_hmac_t *, llarp_buffer_t, llarp_hmacsec_t); - +typedef bool (*llarp_hmac_func)(llarp_hmac_t *, llarp_buffer_t, + llarp_hmacsec_t); + typedef bool (*llarp_sign_func)(llarp_sig_t *, llarp_seckey_t, llarp_buffer_t); typedef bool (*llarp_verify_func)(llarp_pubkey_t, llarp_buffer_t, llarp_sig_t); - + struct llarp_crypto { llarp_sym_cipher_func xchacha20; llarp_dh_func dh_client; diff --git a/include/llarp/crypto_async.h b/include/llarp/crypto_async.h index d5fa88845..cb192a784 100644 --- a/include/llarp/crypto_async.h +++ b/include/llarp/crypto_async.h @@ -10,11 +10,10 @@ extern "C" { struct llarp_async_dh; -struct llarp_async_dh *llarp_async_dh_new( - llarp_seckey_t ourkey, - struct llarp_crypto *crypto, - struct llarp_ev_loop *ev, - struct llarp_threadpool *tp); +struct llarp_async_dh *llarp_async_dh_new(llarp_seckey_t ourkey, + struct llarp_crypto *crypto, + struct llarp_ev_loop *ev, + struct llarp_threadpool *tp); void llarp_async_dh_free(struct llarp_async_dh **dh); struct llarp_dh_result; @@ -27,39 +26,36 @@ struct llarp_dh_result { llarp_dh_complete_hook hook; }; -void llarp_async_client_dh(struct llarp_async_dh *dh, - llarp_pubkey_t theirkey, +void llarp_async_client_dh(struct llarp_async_dh *dh, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void *user); - -void llarp_async_server_dh(struct llarp_async_dh *dh, - llarp_pubkey_t theirkey, + +void llarp_async_server_dh(struct llarp_async_dh *dh, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void *user); +struct llarp_async_cipher; +struct llarp_cipher_result; + +typedef void (*llarp_cipher_complete_hook)(struct llarp_cipher_result *); + +struct llarp_cipher_result { + llarp_buffer_t buff; + void *user; + llarp_cipher_complete_hook hook; +}; - struct llarp_async_cipher; - struct llarp_cipher_result; - - typedef void (*llarp_cipher_complete_hook)(struct llarp_cipher_result*); - - struct llarp_cipher_result - { - llarp_buffer_t buff; - void * user; - llarp_cipher_complete_hook hook; - }; +struct llarp_async_cipher *llarp_async_cipher_new(llarp_sharedkey_t key, + struct llarp_crypto *crypto, + struct llarp_ev_loop *ev, + struct llarp_threadpool *tp); - struct llarp_async_cipher * llarp_async_cipher_new( - llarp_sharedkey_t key, - struct llarp_crypto *crypto, - struct llarp_ev_loop *ev, - struct llarp_threadpool *tp); +void llarp_async_cipher_free(struct llarp_async_cipher **c); - void llarp_async_cipher_free(struct llarp_async_cipher ** c); +void llarp_async_cipher_queue_op(struct llarp_async_cipher *c, + llarp_buffer_t *buff, llarp_nounce_t n, + llarp_cipher_complete_hook h, void *user); - void llarp_async_cipher_queue_op(struct llarp_async_cipher * c, llarp_buffer_t * buff, llarp_nounce_t n, llarp_cipher_complete_hook h, void * user); - #ifdef __cplusplus } #endif diff --git a/include/llarp/ev.h b/include/llarp/ev.h index bf06d952b..efe1ee746 100644 --- a/include/llarp/ev.h +++ b/include/llarp/ev.h @@ -58,6 +58,8 @@ struct llarp_ev_caller *llarp_ev_prepare_async(struct llarp_ev_loop *ev, bool llarp_ev_call_async(struct llarp_ev_caller *c, void *user); +bool larp_ev_call_many_async(struct llarp_ev_caller *c, void **users, size_t n); + void llarp_ev_caller_stop(struct llarp_ev_caller *c); #ifdef __cplusplus diff --git a/llarp/crypto_async.c b/llarp/crypto_async.c index 66d43cf9f..8970b7f4b 100644 --- a/llarp/crypto_async.c +++ b/llarp/crypto_async.c @@ -12,7 +12,7 @@ struct llarp_async_dh { struct llarp_dh_internal { llarp_pubkey_t theirkey; - uint8_t * ourkey; + uint8_t *ourkey; llarp_tunnel_nounce_t nounce; struct llarp_dh_result result; llarp_dh_func func; @@ -20,7 +20,8 @@ struct llarp_dh_internal { static void llarp_crypto_dh_work(void *user) { struct llarp_dh_internal *impl = (struct llarp_dh_internal *)user; - impl->func(&impl->result.sharedkey, impl->theirkey, impl->nounce, impl->ourkey); + impl->func(&impl->result.sharedkey, impl->theirkey, impl->nounce, + impl->ourkey); } static void llarp_crypto_dh_result(struct llarp_ev_async_call *call) { @@ -48,25 +49,22 @@ static void llarp_async_dh_exec(struct llarp_async_dh *dh, llarp_dh_func func, llarp_threadpool_queue_job(dh->tp, job); } -void llarp_async_client_dh(struct llarp_async_dh *dh, - llarp_pubkey_t theirkey, +void llarp_async_client_dh(struct llarp_async_dh *dh, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void *user) { llarp_async_dh_exec(dh, dh->client, theirkey, nounce, result, user); } -void llarp_async_server_dh(struct llarp_async_dh *dh, - llarp_pubkey_t theirkey, +void llarp_async_server_dh(struct llarp_async_dh *dh, llarp_pubkey_t theirkey, llarp_tunnel_nounce_t nounce, llarp_dh_complete_hook result, void *user) { llarp_async_dh_exec(dh, dh->server, theirkey, nounce, result, user); } -struct llarp_async_dh *llarp_async_dh_new( - llarp_seckey_t ourkey, - struct llarp_crypto *crypto, - struct llarp_ev_loop *ev, - struct llarp_threadpool *tp) { +struct llarp_async_dh *llarp_async_dh_new(llarp_seckey_t ourkey, + struct llarp_crypto *crypto, + struct llarp_ev_loop *ev, + struct llarp_threadpool *tp) { struct llarp_async_dh *dh = llarp_g_mem.alloc(sizeof(struct llarp_async_dh), 16); dh->client = crypto->dh_client; @@ -85,39 +83,38 @@ void llarp_async_dh_free(struct llarp_async_dh **dh) { } } - -struct llarp_async_cipher_internal -{ - uint8_t * key; +struct llarp_async_cipher_internal { + uint8_t *key; llarp_nounce_t nounce; struct llarp_cipher_result result; llarp_sym_cipher_func func; }; -struct llarp_async_cipher -{ +struct llarp_async_cipher { llarp_sharedkey_t key; struct llarp_threadpool *tp; struct llarp_ev_caller *caller; llarp_sym_cipher_func func; }; -static void llarp_crypto_cipher_result(struct llarp_ev_async_call * job) -{ - struct llarp_async_cipher_internal * impl = (struct llarp_async_cipher_internal *) job->user; +static void llarp_crypto_cipher_result(struct llarp_ev_async_call *job) { + struct llarp_async_cipher_internal *impl = + (struct llarp_async_cipher_internal *)job->user; impl->result.hook(&impl->result); llarp_g_mem.free(impl); } -static void llarp_crypto_cipher_work(void * work) -{ - struct llarp_async_cipher_internal * impl = (struct llarp_async_cipher_internal *) work; +static void llarp_crypto_cipher_work(void *work) { + struct llarp_async_cipher_internal *impl = + (struct llarp_async_cipher_internal *)work; impl->func(impl->result.buff, impl->key, impl->nounce); } -void llarp_async_cipher_queue_op(struct llarp_async_cipher * c, llarp_buffer_t * buff, llarp_nounce_t n, llarp_cipher_complete_hook h, void * user) -{ - struct llarp_async_cipher_internal * impl = llarp_g_mem.alloc(sizeof(struct llarp_async_cipher_internal), 16); +void llarp_async_cipher_queue_op(struct llarp_async_cipher *c, + llarp_buffer_t *buff, llarp_nounce_t n, + llarp_cipher_complete_hook h, void *user) { + struct llarp_async_cipher_internal *impl = + llarp_g_mem.alloc(sizeof(struct llarp_async_cipher_internal), 16); impl->key = c->key; memcpy(impl->nounce, n, sizeof(llarp_nounce_t)); impl->result.user = user; @@ -132,12 +129,12 @@ void llarp_async_cipher_queue_op(struct llarp_async_cipher * c, llarp_buffer_t * llarp_threadpool_queue_job(c->tp, job); } -struct llarp_async_cipher * llarp_async_cipher_new(llarp_sharedkey_t key, - struct llarp_crypto * crypto, - struct llarp_ev_loop * ev, - struct llarp_threadpool *tp) -{ - struct llarp_async_cipher * cipher = llarp_g_mem.alloc(sizeof(struct llarp_async_cipher), 16); +struct llarp_async_cipher *llarp_async_cipher_new(llarp_sharedkey_t key, + struct llarp_crypto *crypto, + struct llarp_ev_loop *ev, + struct llarp_threadpool *tp) { + struct llarp_async_cipher *cipher = + llarp_g_mem.alloc(sizeof(struct llarp_async_cipher), 16); cipher->func = crypto->xchacha20; cipher->tp = tp; cipher->caller = llarp_ev_prepare_async(ev, &llarp_crypto_cipher_result); diff --git a/llarp/crypto_libsodium.cpp b/llarp/crypto_libsodium.cpp index 540a984c0..8784852d1 100644 --- a/llarp/crypto_libsodium.cpp +++ b/llarp/crypto_libsodium.cpp @@ -73,14 +73,13 @@ static void randomize(llarp_buffer_t buff) { randombytes((unsigned char *)buff.base, buff.sz); } -static inline void randbytes(void * ptr, size_t sz) -{ - randombytes((unsigned char*)ptr, sz); +static inline void randbytes(void *ptr, size_t sz) { + randombytes((unsigned char *)ptr, sz); } - + static void keygen(llarp_seckey_t *keys) { unsigned char seed[32]; - uint8_t * pk = llarp_seckey_topublic(*keys); + uint8_t *pk = llarp_seckey_topublic(*keys); crypto_sign_seed_keypair(pk, *keys, seed); } } // namespace sodium diff --git a/llarp/ev.cpp b/llarp/ev.cpp index 40691052f..6dd2d5f4c 100644 --- a/llarp/ev.cpp +++ b/llarp/ev.cpp @@ -28,6 +28,16 @@ struct llarp_ev_caller { return should; } + bool appendManyCalls(void **users, size_t n) { + std::unique_lock lock(access); + bool should = pending.size() == 0; + while (n--) { + pending.push(new llarp_ev_async_call{loop, this, *users, this->work}); + users++; + } + return should; + } + void Call() { std::unique_lock lock(access); while (pending.size() > 0) { @@ -192,6 +202,14 @@ bool llarp_ev_call_async(struct llarp_ev_caller *caller, void *user) { return true; } +bool llarp_ev_call_many_async(struct llarp_ev_caller *caller, void **users, + size_t n) { + if (caller->appendManyCalls(users, n)) + return uv_async_send(&caller->async) == 0; + else + return true; +} + void llarp_ev_caller_stop(struct llarp_ev_caller *caller) { uv_close((uv_handle_t *)&caller->async, &llarp::ev_caller_async_closed); } diff --git a/test/test_async_cipher.c b/test/test_async_cipher.c index 7814615e0..98fe80009 100644 --- a/test/test_async_cipher.c +++ b/test/test_async_cipher.c @@ -17,16 +17,13 @@ static void handle_cipher_complete(struct llarp_cipher_result *res) { size_t sz = m->jobs; m->completed++; size_t left = m->num - m->completed; - if (m->completed % 10000 == 0) printf("completed %ld and %ld left\n", m->completed, left); - if (m->completed == m->num) - { + if (m->completed % 10000 == 0) + printf("completed %ld and %ld left\n", m->completed, left); + if (m->completed == m->num) { llarp_ev_loop_stop(m->ev); - } - else if(m->completed % sz == 0) - { + } else if (m->completed % sz == 0) { llarp_nounce_t nounce; - while(sz--) - { + while (sz--) { m->crypto.randbytes(nounce, sizeof(llarp_nounce_t)); llarp_async_cipher_queue_op(m->cipher, &res->buff, nounce, handle_cipher_complete, m); @@ -42,17 +39,17 @@ int main(int argc, char *argv[]) { llarp_crypto_libsodium_init(&b_main.crypto); llarp_ev_loop_alloc(&b_main.ev); - tp = llarp_init_threadpool(8); + tp = llarp_init_threadpool(2); b_main.num = 10000000; b_main.jobs = 10000; b_main.completed = 0; llarp_sharedkey_t key; b_main.crypto.randbytes(key, sizeof(llarp_sharedkey_t)); - + b_main.cipher = llarp_async_cipher_new(key, &b_main.crypto, b_main.ev, tp); llarp_threadpool_start(tp); - + llarp_nounce_t nounce; llarp_buffer_t n_buff; n_buff.base = nounce; @@ -62,7 +59,7 @@ int main(int argc, char *argv[]) { printf("starting %ld jobs\n", sz); /* do work here */ while (sz--) { - llarp_buffer_t * msg = llarp_g_mem.alloc(sizeof(llarp_buffer_t), 8); + llarp_buffer_t *msg = llarp_g_mem.alloc(sizeof(llarp_buffer_t), 8); msg->base = llarp_g_mem.alloc(1024, 1024); msg->sz = 1024; msg->cur = msg->base; diff --git a/test/test_async_dh.c b/test/test_async_dh.c index 74829fc02..c452e4605 100644 --- a/test/test_async_dh.c +++ b/test/test_async_dh.c @@ -15,8 +15,7 @@ static void handle_dh_complete(struct llarp_dh_result *res) { m->completed++; if (m->completed % 10000 == 0) printf("completed %ld\n", m->completed); - if (m->completed == m->num) - { + if (m->completed == m->num) { llarp_ev_loop_stop(m->ev); } } @@ -30,9 +29,9 @@ int main(int argc, char *argv[]) { llarp_crypto_libsodium_init(&crypto); llarp_ev_loop_alloc(&dh_main.ev); - tp = llarp_init_threadpool(8); + tp = llarp_init_threadpool(2); - dh_main.num = 100000; + dh_main.num = 10000; dh_main.completed = 0; llarp_seckey_t ourkey; llarp_seckey_t theirkey; @@ -42,22 +41,22 @@ int main(int argc, char *argv[]) { dh_main.dh = llarp_async_dh_new(ourkey, &crypto, dh_main.ev, tp); llarp_threadpool_start(tp); - + llarp_tunnel_nounce_t nounce; llarp_buffer_t n_buff; n_buff.base = nounce; n_buff.cur = n_buff.base; n_buff.sz = sizeof(llarp_tunnel_nounce_t); - uint8_t * theirpubkey = llarp_seckey_topublic(theirkey); - + uint8_t *theirpubkey = llarp_seckey_topublic(theirkey); + size_t sz = dh_main.num; printf("starting %ld dh jobs\n", sz); /* do work here */ while (sz--) { crypto.randomize(n_buff); - llarp_async_client_dh(dh_main.dh, theirpubkey, nounce, - handle_dh_complete, &dh_main); + llarp_async_client_dh(dh_main.dh, theirpubkey, nounce, handle_dh_complete, + &dh_main); } printf("started %ld dh jobs\n", dh_main.num); llarp_ev_loop_run(dh_main.ev);