Skip to content

Commit

Permalink
Add a mode to the echo examples to fix the local ephemeral key
Browse files Browse the repository at this point in the history
  • Loading branch information
rweather committed Apr 23, 2016
1 parent 75dad44 commit 6eeaa22
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
30 changes: 29 additions & 1 deletion examples/echo/echo-client/echo-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
#include <unistd.h>
#include <getopt.h>

#define short_options "c:s:p:gv"
#define short_options "c:s:p:gvf"

static struct option const long_options[] = {
{"client-private-key", required_argument, NULL, 'c'},
{"server-public-key", required_argument, NULL, 's'},
{"psk", required_argument, NULL, 'p'},
{"padding", no_argument, NULL, 'g'},
{"verbose", no_argument, NULL, 'v'},
{"fixed-ephemeral", no_argument, NULL, 'f'},
{NULL, 0, NULL, 0 }
};

Expand All @@ -48,11 +49,24 @@ static const char *protocol = NULL;
static const char *hostname = NULL;
static int port = 7000;
static int padding = 0;
static int fixed_ephemeral = 0;

/* Message buffer for send/receive */
#define MAX_MESSAGE_LEN 4096
static uint8_t message[MAX_MESSAGE_LEN + 2];

/* Internal function to directly supply ephemeral keys during testing */
NoiseDHState *noise_handshakestate_get_fixed_ephemeral_dh_
(NoiseHandshakeState *state);

/* Value to use when fixed ephemeral mode is selected */
static uint8_t const fixed_ephemeral_value[32] = {
0x89, 0x3e, 0x28, 0xb9, 0xdc, 0x6c, 0xa8, 0xd6,
0x11, 0xab, 0x66, 0x47, 0x54, 0xb8, 0xce, 0xb7,
0xba, 0xc5, 0x11, 0x73, 0x49, 0xa4, 0x43, 0x9a,
0x6b, 0x05, 0x69, 0xda, 0x97, 0x7c, 0x46, 0x4a
};

/* Print usage information */
static void usage(const char *progname)
{
Expand All @@ -68,6 +82,8 @@ static void usage(const char *progname)
fprintf(stderr, " Pad messages with random data to a uniform size.\n\n");
fprintf(stderr, " --verbose, -v\n");
fprintf(stderr, " Print all messages to and from the echo server.\n\n");
fprintf(stderr, " --fixed-ephemeral, -f\n");
fprintf(stderr, " Use a fixed local ephemeral key for testing.\n\n");
}

/* Parse the command-line options */
Expand All @@ -83,6 +99,7 @@ static int parse_options(int argc, char *argv[])
case 'p': psk_file = optarg; break;
case 'g': padding = 1; break;
case 'v': echo_verbose = 1; break;
case 'f': fixed_ephemeral = 1; break;
default:
usage(progname);
return 0;
Expand Down Expand Up @@ -168,6 +185,17 @@ static int initialize_handshake
}
}

/* Set the fixed local ephemeral value if necessary */
if (fixed_ephemeral) {
dh = noise_handshakestate_get_fixed_ephemeral_dh_(handshake);
err = noise_dhstate_set_keypair_private
(dh, fixed_ephemeral_value, sizeof(fixed_ephemeral_value));
if (err != NOISE_ERROR_NONE) {
noise_perror("fixed ephemeral value", err);
return 0;
}
}

/* Ready to go */
return 1;
}
Expand Down
30 changes: 29 additions & 1 deletion examples/echo/echo-server/echo-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@
#include <unistd.h>
#include <getopt.h>

#define short_options "k:v"
#define short_options "k:vf"

static struct option const long_options[] = {
{"key-dir", required_argument, NULL, 'k'},
{"verbose", no_argument, NULL, 'v'},
{"fixed-ephemeral", no_argument, NULL, 'f'},
{NULL, 0, NULL, 0 }
};

/* Parsed command-line options */
static const char *key_dir = ".";
static int port = 7000;
static int fixed_ephemeral = 0;

/* Loaded keys */
#define CURVE25519_KEY_LEN 32
Expand All @@ -53,6 +55,18 @@ static uint8_t psk[32];
#define MAX_MESSAGE_LEN 65535
static uint8_t message[MAX_MESSAGE_LEN + 2];

/* Internal function to directly supply ephemeral keys during testing */
NoiseDHState *noise_handshakestate_get_fixed_ephemeral_dh_
(NoiseHandshakeState *state);

/* Value to use when fixed ephemeral mode is selected */
static uint8_t const fixed_ephemeral_value[32] = {
0xbb, 0xdb, 0x4c, 0xdb, 0xd3, 0x09, 0xf1, 0xa1,
0xf2, 0xe1, 0x45, 0x69, 0x67, 0xfe, 0x28, 0x8c,
0xad, 0xd6, 0xf7, 0x12, 0xd6, 0x5d, 0xc7, 0xb7,
0x79, 0x3d, 0x5e, 0x63, 0xda, 0x6b, 0x37, 0x5b
};

/* Print usage information */
static void usage(const char *progname)
{
Expand All @@ -64,6 +78,8 @@ static void usage(const char *progname)
fprintf(stderr, " Pre-shared key value to use.\n\n");
fprintf(stderr, " --verbose, -v\n");
fprintf(stderr, " Print all messages to and from the echo client.\n\n");
fprintf(stderr, " --fixed-ephemeral, -f\n");
fprintf(stderr, " Use a fixed local ephemeral key for testing.\n\n");
}

/* Parse the command-line options */
Expand All @@ -76,6 +92,7 @@ static int parse_options(int argc, char *argv[])
switch (ch) {
case 'k': key_dir = optarg; break;
case 'v': echo_verbose = 1; break;
case 'f': fixed_ephemeral = 1; break;
default:
usage(progname);
return 0;
Expand Down Expand Up @@ -157,6 +174,17 @@ static int initialize_handshake
}
}

/* Set the fixed local ephemeral value if necessary */
if (fixed_ephemeral) {
dh = noise_handshakestate_get_fixed_ephemeral_dh_(handshake);
err = noise_dhstate_set_keypair_private
(dh, fixed_ephemeral_value, sizeof(fixed_ephemeral_value));
if (err != NOISE_ERROR_NONE) {
noise_perror("fixed ephemeral value", err);
return 0;
}
}

/* Ready to go */
return 1;
}
Expand Down

0 comments on commit 6eeaa22

Please sign in to comment.