From 14a44b4016d3350c85c419e1e1b683c1574cd86e Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 28 Mar 2012 20:47:06 +0200 Subject: Add keygen function --- src/config.c | 12 ++++++++++ src/fastd.h | 2 ++ src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c | 30 +++++++++++++++++++++++++ src/protocol_null.c | 5 +++++ 4 files changed, 49 insertions(+) diff --git a/src/config.c b/src/config.c index af32975..6fd8def 100644 --- a/src/config.c +++ b/src/config.c @@ -146,6 +146,7 @@ void fastd_configure(fastd_context *ctx, fastd_config *conf, int argc, char *con char *charptr; char *endptr; char *addrstr; + bool keygen = false; while (i < argc) { @@ -305,9 +306,20 @@ void fastd_configure(fastd_context *ctx, fastd_config *conf, int argc, char *con continue; } + IF_OPTION("--generate-key") { + keygen = true; + continue; + } + exit_error(ctx, "config error: unknown option `%s'", argv[i]); } + if (keygen) { + ctx->conf = conf; + conf->protocol->generate_key(ctx); + exit(0); + } + conf->n_floating = 0; conf->n_v4 = 0; conf->n_v6 = 0; diff --git a/src/fastd.h b/src/fastd.h index cd6dbb2..5ea4260 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -71,6 +71,8 @@ struct _fastd_protocol { void (*send)(fastd_context *ctx, fastd_peer *peer, fastd_buffer buffer); void (*free_peer_state)(fastd_context *ctx, fastd_peer *peer); + + void (*generate_key)(fastd_context *ctx); }; struct _fastd_config { diff --git a/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c b/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c index 93f0432..33f20b3 100644 --- a/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c +++ b/src/protocol_ec25519_fhmqvc_xsalsa20_poly1305.c @@ -691,6 +691,34 @@ static void protocol_free_peer_state(fastd_context *ctx, fastd_peer *peer) { } +static void hexdump(const char *desc, unsigned char d[32]) { + printf("%s", desc); + + int i; + for (i = 0; i < 32; i++) + printf("%02x", d[i]); + + printf("\n"); +} + +static void protocol_generate_key(fastd_context *ctx) { + ecc_secret_key_256 secret_key; + ecc_public_key_256 public_key; + + pr_info(ctx, "Reading 32 bytes from /dev/random..."); + + fastd_random_bytes(ctx, secret_key.s, 32, true); + ecc_25519_secret_sanitize(&secret_key, &secret_key); + + ecc_25519_work work; + ecc_25519_scalarmult_base(&work, &secret_key); + ecc_25519_store(&public_key, &work); + + hexdump("Secret: ", secret_key.s); + hexdump("Public: ", public_key.p); +} + + const fastd_protocol fastd_protocol_ec25519_fhmqvc_xsalsa20_poly1305 = { .name = "ec25519-fhmqvc-xsalsa20-poly1305", @@ -705,4 +733,6 @@ const fastd_protocol fastd_protocol_ec25519_fhmqvc_xsalsa20_poly1305 = { .send = protocol_send, .free_peer_state = protocol_free_peer_state, + + .generate_key = protocol_generate_key, }; diff --git a/src/protocol_null.c b/src/protocol_null.c index cdf6694..77839d9 100644 --- a/src/protocol_null.c +++ b/src/protocol_null.c @@ -89,6 +89,9 @@ static void protocol_send(fastd_context *ctx, fastd_peer *peer, fastd_buffer buf static void protocol_free_peer_state(fastd_context *ctx, fastd_peer *peer) { } +static void protocol_generate_key(fastd_context *ctx) { + exit_error(ctx, "trying to generate key for `null' protocol"); +} const fastd_protocol fastd_protocol_null = { .name = "null", @@ -104,4 +107,6 @@ const fastd_protocol fastd_protocol_null = { .send = protocol_send, .free_peer_state = protocol_free_peer_state, + + .generate_key = protocol_generate_key, }; -- cgit v1.2.3