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 --- curve.c | 63 +++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 22 deletions(-) (limited to 'curve.c') 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; +} -- cgit v1.2.3-54-g00ecf