#include <sys/cdefs.h>
__RCSID("$NetBSD: wg-keygen.c,v 1.1 2020/08/20 21:28:02 riastradh Exp $");
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <resolv.h>
#include <string.h>
#define KEY_LEN 32
#define KEY_BASE64_LEN 44
__dead static void
usage(void)
{
const char *progname = getprogname();
fprintf(stderr, "Usage:\n");
fprintf(stderr, "\t%s : Generate a private key\n", progname);
fprintf(stderr, "\t%s --pub : Generate a public key from a private key via stdin\n", progname);
fprintf(stderr, "\t%s --psk : Generate a pre-shared key\n", progname);
exit(EXIT_FAILURE);
}
#define CURVE25519_SIZE 32
extern int crypto_scalarmult_curve25519(uint8_t [CURVE25519_SIZE],
const uint8_t [CURVE25519_SIZE], const uint8_t [CURVE25519_SIZE]);
static void
gen_pubkey(uint8_t key[CURVE25519_SIZE], uint8_t pubkey[CURVE25519_SIZE])
{
static const uint8_t basepoint[CURVE25519_SIZE] = {9};
crypto_scalarmult_curve25519(pubkey, key, basepoint);
}
static void
normalize_key(uint8_t key[KEY_LEN])
{
key[0] &= 248;
key[31] &= 127;
key[31] |= 64;
}
static char *
base64(uint8_t key[KEY_LEN])
{
static char key_b64[KEY_BASE64_LEN + 1];
int error;
error = b64_ntop(key, KEY_LEN, key_b64, KEY_BASE64_LEN + 1);
if (error == -1)
errx(EXIT_FAILURE, "b64_ntop failed");
key_b64[KEY_BASE64_LEN] = '\0';
return key_b64;
}
int
main(int argc, char *argv[])
{
uint8_t key[KEY_LEN];
if (!(argc == 1 || argc == 2))
usage();
if (argc == 1) {
arc4random_buf(key, KEY_LEN);
normalize_key(key);
printf("%s\n", base64(key));
return 0;
}
if (strcmp(argv[1], "--psk") == 0) {
arc4random_buf(key, KEY_LEN);
printf("%s\n", base64(key));
return 0;
}
if (strcmp(argv[1], "--pub") == 0) {
char key_b64[KEY_BASE64_LEN + 1];
int ret;
char *retc;
uint8_t pubkey[KEY_LEN];
retc = fgets(key_b64, KEY_BASE64_LEN + 1, stdin);
if (retc == NULL)
err(EXIT_FAILURE, "fgets");
key_b64[KEY_BASE64_LEN] = '\0';
if (strlen(key_b64) != KEY_BASE64_LEN)
errx(EXIT_FAILURE, "Invalid length of a private key");
ret = b64_pton(key_b64, key, KEY_LEN);
if (ret == -1)
errx(EXIT_FAILURE, "b64_pton failed");
gen_pubkey(key, pubkey);
printf("%s\n", base64(pubkey));
return 0;
}
usage();
}