From bc41b879d5f8d2286f094d85222d0d40b90ace00 Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Sat, 15 Jun 2013 20:13:59 +0200 Subject: curve: curve25519_tfm_alloc/curve25519_tfm_free helpers Facilitate allocation and destruction of crypto objects through common helper functions. Signed-off-by: Daniel Borkmann --- ct_client.c | 14 ++++---------- ct_server.c | 9 +++------ curve.c | 63 ++++++++++++++++++++++++++++++++++++++++--------------------- curve.h | 7 +++++-- 4 files changed, 53 insertions(+), 40 deletions(-) diff --git a/ct_client.c b/ct_client.c index 1f4d10e..37b6090 100644 --- a/ct_client.c +++ b/ct_client.c @@ -315,10 +315,7 @@ retry: syslog(LOG_INFO, "curvetun client booting!\n"); } - c = xmalloc(sizeof(struct curve25519_struct)); - - curve25519_alloc_or_maybe_die(c); - + c = curve25519_tfm_alloc(); p = get_serv_store_entry_proto_inf(); if (!p) syslog_panic("Cannot proto!\n"); @@ -332,8 +329,7 @@ retry: ret = getaddrinfo(host, port, &hints, &ahead); if (ret < 0) { syslog(LOG_ERR, "Cannot get address info! Retry!\n"); - curve25519_free(c); - xfree(c); + curve25519_tfm_free(c); fd = -1; retry_server = 1; closed_by_server = 0; @@ -364,8 +360,7 @@ retry: if (fd < 0) { syslog(LOG_ERR, "Cannot create socket! Retry!\n"); - curve25519_free(c); - xfree(c); + curve25519_tfm_free(c); fd = -1; retry_server = 1; closed_by_server = 0; @@ -422,8 +417,7 @@ retry: xfree(buff); close(fd); - curve25519_free(c); - xfree(c); + curve25519_tfm_free(c); /* tundev still active */ if (closed_by_server && !sigint) { diff --git a/ct_server.c b/ct_server.c index eb53bd3..9737ffd 100644 --- a/ct_server.c +++ b/ct_server.c @@ -448,21 +448,19 @@ static void *worker(void *self) int fd, old_state; ssize_t ret; size_t blen = TUNBUFF_SIZ; //FIXME - const struct worker_struct *ws = self; + struct worker_struct *ws = self; struct pollfd fds; char *buff; fds.fd = ws->efd[0]; fds.events = POLLIN; - curve25519_alloc_or_maybe_die(ws->c); - + ws->c = curve25519_tfm_alloc(); buff = xmalloc_aligned(blen, 64); syslog(LOG_INFO, "curvetun thread on CPU%u up!\n", ws->cpu); - pthread_cleanup_push(xfree_func, ws->c); - pthread_cleanup_push(curve25519_free, ws->c); + pthread_cleanup_push(curve25519_tfm_free_void, ws->c); pthread_cleanup_push(xfree_func, buff); while (likely(!sigint)) { @@ -488,7 +486,6 @@ static void *worker(void *self) syslog(LOG_INFO, "curvetun thread on CPU%u down!\n", ws->cpu); - pthread_cleanup_pop(1); pthread_cleanup_pop(1); pthread_cleanup_pop(1); diff --git a/curve.c b/curve.c index e8b0c9a..56ecf47 100644 --- a/curve.c +++ b/curve.c @@ -29,25 +29,7 @@ #include "crypto.h" #include "config.h" -int curve25519_pubkey_hexparse_32(unsigned char *bin, size_t blen, - const char *ascii, size_t alen) -{ - int ret = sscanf(ascii, - "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:" - "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:" - "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:" - "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", - &bin[0], &bin[1], &bin[2], &bin[3], &bin[4], - &bin[5], &bin[6], &bin[7], &bin[8], &bin[9], - &bin[10], &bin[11], &bin[12], &bin[13], &bin[14], - &bin[15], &bin[16], &bin[17], &bin[18], &bin[19], - &bin[20], &bin[21], &bin[22], &bin[23], &bin[24], - &bin[25], &bin[26], &bin[27], &bin[28], &bin[29], - &bin[30], &bin[31]); - return ret == 32; -} - -void curve25519_alloc_or_maybe_die(struct curve25519_struct *curve) +static void curve25519_init(struct curve25519_struct *curve) { curve->enc_size = curve->dec_size = TUNBUFF_SIZ; @@ -58,10 +40,8 @@ void curve25519_alloc_or_maybe_die(struct curve25519_struct *curve) spinlock_init(&curve->dec_lock); } -void curve25519_free(void *curvep) +static void curve25519_destroy(struct curve25519_struct *curve) { - struct curve25519_struct *curve = curvep; - xzfree(curve->enc, curve->enc_size); xzfree(curve->dec, curve->dec_size); @@ -69,6 +49,27 @@ void curve25519_free(void *curvep) spinlock_destroy(&curve->dec_lock); } +struct curve25519_struct *curve25519_tfm_alloc(void) +{ + struct curve25519_struct *tfm; + + tfm = xzmalloc_aligned(sizeof(*tfm), 16); + curve25519_init(tfm); + + return tfm; +} + +void curve25519_tfm_free(struct curve25519_struct *tfm) +{ + curve25519_destroy(tfm); + xzfree(tfm, sizeof(*tfm)); +} + +void curve25519_tfm_free_void(void *tfm) +{ + curve25519_tfm_free(tfm); +} + void curve25519_proto_init(struct curve25519_proto *proto, unsigned char *pubkey_remote, size_t len) { @@ -182,3 +183,21 @@ out: spinlock_unlock(&curve->dec_lock); return done; } + +int curve25519_pubkey_hexparse_32(unsigned char *bin, size_t blen, + const char *ascii, size_t alen) +{ + int ret = sscanf(ascii, + "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:" + "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:" + "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:" + "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", + &bin[0], &bin[1], &bin[2], &bin[3], &bin[4], + &bin[5], &bin[6], &bin[7], &bin[8], &bin[9], + &bin[10], &bin[11], &bin[12], &bin[13], &bin[14], + &bin[15], &bin[16], &bin[17], &bin[18], &bin[19], + &bin[20], &bin[21], &bin[22], &bin[23], &bin[24], + &bin[25], &bin[26], &bin[27], &bin[28], &bin[29], + &bin[30], &bin[31]); + return ret == 32; +} diff --git a/curve.h b/curve.h index d9c2584..1b3a919 100644 --- a/curve.h +++ b/curve.h @@ -21,8 +21,11 @@ struct curve25519_struct { }; extern void curve25519_selftest(void); -extern void curve25519_alloc_or_maybe_die(struct curve25519_struct *curve); -extern void curve25519_free(void *curve); + +extern struct curve25519_struct *curve25519_tfm_alloc(void); +extern void curve25519_tfm_free(struct curve25519_struct *tfm); +extern void curve25519_tfm_free_void(void *tfm); + extern void curve25519_proto_init(struct curve25519_proto *proto, unsigned char *pubkey_remote, size_t len); extern int curve25519_pubkey_hexparse_32(unsigned char *bin, size_t blen, -- cgit v1.2.3-54-g00ecf