2017-03-03 14:25:57 +01:00
|
|
|
#include <stdio.h>
|
2017-03-03 16:28:45 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#define OPTPARSE_IMPLEMENTATION
|
2017-03-03 19:08:36 +01:00
|
|
|
#include "docs.h"
|
2017-03-03 17:22:36 +01:00
|
|
|
#include "sha256.h"
|
2017-03-03 14:39:32 +01:00
|
|
|
#include "chacha.h"
|
2017-03-03 17:22:36 +01:00
|
|
|
#include "optparse.h"
|
2017-03-03 14:25:57 +01:00
|
|
|
|
2017-03-03 16:42:48 +01:00
|
|
|
int curve25519_donna(u8 *p, const u8 *s, const u8 *b);
|
|
|
|
|
2017-03-03 16:28:45 +01:00
|
|
|
static void
|
|
|
|
fatal(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fprintf(stderr, "enchive: ");
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2017-03-03 19:08:36 +01:00
|
|
|
/* Global options. */
|
|
|
|
static char *global_random_device = "/dev/urandom";
|
|
|
|
static char *global_pubkey = 0;
|
|
|
|
static char *global_seckey = 0;
|
|
|
|
|
2017-03-03 16:28:45 +01:00
|
|
|
static void
|
|
|
|
secure_entropy(void *buf, size_t len)
|
|
|
|
{
|
2017-03-03 19:08:36 +01:00
|
|
|
FILE *r = fopen(global_random_device, "rb");
|
2017-03-03 16:28:45 +01:00
|
|
|
if (!r)
|
|
|
|
fatal("failed to open /dev/urandom");
|
|
|
|
if (!fread(buf, len, 1, r))
|
|
|
|
fatal("failed to gather entropy");
|
|
|
|
fclose(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-03-03 16:42:48 +01:00
|
|
|
generate_secret(u8 *s)
|
2017-03-03 16:28:45 +01:00
|
|
|
{
|
|
|
|
secure_entropy(s, 32);
|
|
|
|
s[0] &= 248;
|
|
|
|
s[31] &= 127;
|
|
|
|
s[31] |= 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-03-03 16:42:48 +01:00
|
|
|
compute_public(u8 *p, const u8 *s)
|
2017-03-03 16:28:45 +01:00
|
|
|
{
|
2017-03-03 16:42:48 +01:00
|
|
|
static const u8 b[32] = {9};
|
2017-03-03 16:28:45 +01:00
|
|
|
curve25519_donna(p, s, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-03-03 16:42:48 +01:00
|
|
|
compute_shared(u8 *sh, const u8 *s, const u8 *p)
|
2017-03-03 16:28:45 +01:00
|
|
|
{
|
|
|
|
curve25519_donna(sh, s, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
symmetric_encrypt(FILE *in, FILE *out, u8 *key, u8 *iv)
|
2017-03-03 14:25:57 +01:00
|
|
|
{
|
2017-03-03 19:35:48 +01:00
|
|
|
static u8 buffer[2][CHACHA_BLOCKLENGTH * 1024];
|
2017-03-03 17:22:36 +01:00
|
|
|
u8 sha256[SHA256_BLOCK_SIZE];
|
|
|
|
SHA256_CTX hash[1];
|
2017-03-03 14:39:32 +01:00
|
|
|
chacha_ctx ctx[1];
|
|
|
|
chacha_keysetup(ctx, key, 256);
|
|
|
|
chacha_ivsetup(ctx, iv);
|
2017-03-03 17:22:36 +01:00
|
|
|
sha256_init(hash);
|
2017-03-03 16:28:45 +01:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
size_t z = fread(buffer[0], 1, sizeof(buffer[0]), in);
|
|
|
|
if (!z) {
|
|
|
|
if (ferror(in))
|
2017-03-03 19:08:36 +01:00
|
|
|
fatal("error reading plaintext file");
|
2017-03-03 16:28:45 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-03-03 17:22:36 +01:00
|
|
|
sha256_update(hash, buffer[0], z);
|
2017-03-03 16:28:45 +01:00
|
|
|
chacha_encrypt_bytes(ctx, buffer[0], buffer[1], z);
|
|
|
|
if (!fwrite(buffer[1], z, 1, out))
|
2017-03-03 19:08:36 +01:00
|
|
|
fatal("error writing ciphertext file");
|
2017-03-03 19:35:48 +01:00
|
|
|
if (z < sizeof(buffer[0]))
|
|
|
|
break;
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
2017-03-03 17:22:36 +01:00
|
|
|
|
|
|
|
sha256_final(hash, sha256);
|
|
|
|
if (!fwrite(sha256, SHA224_BLOCK_SIZE, 1, out))
|
2017-03-03 19:08:36 +01:00
|
|
|
fatal("error writing checksum to ciphertext file");
|
2017-03-03 17:22:36 +01:00
|
|
|
if (fflush(out))
|
2017-03-03 19:08:36 +01:00
|
|
|
fatal("error flushing to ciphertext file");
|
2017-03-03 17:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
symmetric_decrypt(FILE *in, FILE *out, u8 *key, u8 *iv)
|
|
|
|
{
|
2017-03-03 19:35:48 +01:00
|
|
|
static u8 buffer[2][CHACHA_BLOCKLENGTH * 1024 + SHA224_BLOCK_SIZE];
|
2017-03-03 17:22:36 +01:00
|
|
|
u8 sha256[SHA256_BLOCK_SIZE];
|
|
|
|
SHA256_CTX hash[1];
|
|
|
|
chacha_ctx ctx[1];
|
|
|
|
chacha_keysetup(ctx, key, 256);
|
|
|
|
chacha_ivsetup(ctx, iv);
|
|
|
|
sha256_init(hash);
|
|
|
|
|
|
|
|
/* Always keep SHA224_BLOCK_SIZE bytes in the buffer. */
|
|
|
|
if (!(fread(buffer[0], SHA224_BLOCK_SIZE, 1, in))) {
|
|
|
|
if (ferror(in))
|
2017-03-03 19:08:36 +01:00
|
|
|
fatal("cannot read ciphertext file");
|
2017-03-03 17:22:36 +01:00
|
|
|
else
|
2017-03-03 19:08:36 +01:00
|
|
|
fatal("ciphertext file too short");
|
2017-03-03 17:22:36 +01:00
|
|
|
}
|
2017-03-03 19:35:48 +01:00
|
|
|
|
2017-03-03 17:22:36 +01:00
|
|
|
for (;;) {
|
|
|
|
u8 *p = buffer[0] + SHA224_BLOCK_SIZE;
|
|
|
|
size_t z = fread(p, 1, sizeof(buffer[0]) - SHA224_BLOCK_SIZE, in);
|
|
|
|
if (!z) {
|
|
|
|
if (ferror(in))
|
2017-03-03 19:08:36 +01:00
|
|
|
fatal("error reading ciphertext file");
|
2017-03-03 17:22:36 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
chacha_encrypt_bytes(ctx, buffer[0], buffer[1], z);
|
|
|
|
sha256_update(hash, buffer[1], z);
|
|
|
|
if (!fwrite(buffer[1], z, 1, out))
|
2017-03-03 19:35:48 +01:00
|
|
|
fatal("error writing plaintext file");
|
2017-03-03 17:22:36 +01:00
|
|
|
|
|
|
|
/* Move last SHA224_BLOCK_SIZE bytes to the front. */
|
|
|
|
memmove(buffer[0], buffer[0] + z, SHA224_BLOCK_SIZE);
|
2017-03-03 19:35:48 +01:00
|
|
|
|
|
|
|
if (z < sizeof(buffer[0]) - SHA224_BLOCK_SIZE)
|
|
|
|
break;
|
2017-03-03 17:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sha256_final(hash, sha256);
|
|
|
|
if (memcmp(buffer[0], sha256, SHA224_BLOCK_SIZE) != 0)
|
|
|
|
fatal("checksum mismatch!");
|
2017-03-03 19:35:48 +01:00
|
|
|
if (fflush(out))
|
|
|
|
fatal("error flushing to plaintext file");
|
|
|
|
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
|
|
|
|
2017-03-03 19:08:36 +01:00
|
|
|
static void
|
|
|
|
prepend_home(char *buf, size_t buflen, char *file)
|
|
|
|
{
|
|
|
|
size_t filelen = strlen(file);
|
|
|
|
char *home = getenv("HOME");
|
|
|
|
size_t homelen;
|
|
|
|
|
|
|
|
if (!home)
|
|
|
|
fatal("no HOME environment, can't figure out public file");
|
|
|
|
homelen = strlen(home);
|
|
|
|
if (homelen + 1 + filelen + 1 > buflen)
|
|
|
|
fatal("HOME is too long");
|
|
|
|
|
|
|
|
memcpy(buf, home, homelen);
|
|
|
|
buf[homelen] = '/';
|
|
|
|
memcpy(buf + homelen + 1, file, filelen + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2017-03-03 16:28:45 +01:00
|
|
|
default_pubfile(void)
|
|
|
|
{
|
2017-03-03 19:08:36 +01:00
|
|
|
static char buf[4096];
|
|
|
|
prepend_home(buf, sizeof(buf), ".enchive.pub");
|
|
|
|
return buf;
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
|
|
|
|
2017-03-03 19:08:36 +01:00
|
|
|
static char *
|
2017-03-03 16:28:45 +01:00
|
|
|
default_secfile(void)
|
|
|
|
{
|
2017-03-03 19:08:36 +01:00
|
|
|
static char buf[4096];
|
|
|
|
prepend_home(buf, sizeof(buf), ".enchive.sec");
|
|
|
|
return buf;
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-03-03 16:42:48 +01:00
|
|
|
load_key(const char *file, u8 *key)
|
2017-03-03 16:28:45 +01:00
|
|
|
{
|
|
|
|
FILE *f = fopen(file, "rb");
|
|
|
|
if (!f)
|
2017-03-03 19:08:36 +01:00
|
|
|
fatal("failed to open key file for reading -- %s", file);
|
2017-03-03 16:28:45 +01:00
|
|
|
if (!fread(key, 32, 1, f))
|
2017-03-03 19:08:36 +01:00
|
|
|
fatal("failed to read key file -- %s", file);
|
2017-03-03 16:28:45 +01:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-03-03 19:08:36 +01:00
|
|
|
write_key(const char *file, const u8 *key, int clobber)
|
2017-03-03 16:28:45 +01:00
|
|
|
{
|
2017-03-03 19:08:36 +01:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (!clobber && fopen(file, "r"))
|
|
|
|
fatal("operation would clobber %s", file);
|
|
|
|
f = fopen(file, "wb");
|
2017-03-03 16:28:45 +01:00
|
|
|
if (!f)
|
2017-03-03 19:08:36 +01:00
|
|
|
fatal("failed to open key file for writing -- %s", file);
|
2017-03-03 16:28:45 +01:00
|
|
|
if (!fwrite(key, 32, 1, f))
|
2017-03-03 19:08:36 +01:00
|
|
|
fatal("failed to write key file -- %s", file);
|
2017-03-03 16:28:45 +01:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2017-03-03 19:08:36 +01:00
|
|
|
enum command {
|
|
|
|
COMMAND_UNKNOWN = -2,
|
|
|
|
COMMAND_AMBIGUOUS = -1,
|
|
|
|
COMMAND_KEYGEN,
|
|
|
|
COMMAND_ARCHIVE,
|
|
|
|
COMMAND_EXTRACT,
|
|
|
|
COMMAND_HELP
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char command_names[][8] = {
|
|
|
|
"keygen", "archive", "extract", "help"
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum command
|
|
|
|
parse_command(char *command)
|
|
|
|
{
|
|
|
|
int found = -2;
|
|
|
|
size_t len = strlen(command);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
if (strncmp(command, command_names[i], len) == 0) {
|
|
|
|
if (found >= 0)
|
|
|
|
return COMMAND_AMBIGUOUS;
|
|
|
|
found = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2017-03-03 16:28:45 +01:00
|
|
|
static void
|
|
|
|
command_keygen(struct optparse *options)
|
|
|
|
{
|
|
|
|
static const struct optparse_long keygen[] = {
|
2017-03-03 19:08:36 +01:00
|
|
|
{"force", 'f', OPTPARSE_NONE},
|
2017-03-03 16:28:45 +01:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2017-03-03 19:08:36 +01:00
|
|
|
char *pubfile = global_pubkey;
|
|
|
|
char *secfile = global_seckey;
|
2017-03-03 16:42:48 +01:00
|
|
|
u8 public[32];
|
|
|
|
u8 secret[32];
|
2017-03-03 19:08:36 +01:00
|
|
|
int clobber = 0;
|
2017-03-03 16:28:45 +01:00
|
|
|
|
|
|
|
int option;
|
|
|
|
while ((option = optparse_long(options, keygen, 0)) != -1) {
|
|
|
|
switch (option) {
|
2017-03-03 19:08:36 +01:00
|
|
|
case 'f':
|
|
|
|
clobber = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("%s", options->errmsg);
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 19:08:36 +01:00
|
|
|
if (!pubfile)
|
|
|
|
pubfile = default_pubfile();
|
|
|
|
if (!secfile)
|
|
|
|
secfile = default_secfile();
|
|
|
|
|
2017-03-03 16:28:45 +01:00
|
|
|
generate_secret(secret);
|
|
|
|
compute_public(public, secret);
|
2017-03-03 19:08:36 +01:00
|
|
|
write_key(pubfile, public, clobber);
|
|
|
|
write_key(secfile, secret, clobber);
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
command_archive(struct optparse *options)
|
|
|
|
{
|
|
|
|
static const struct optparse_long archive[] = {
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2017-03-03 19:52:26 +01:00
|
|
|
char *infile;
|
|
|
|
char *outfile;
|
|
|
|
FILE *in = stdin;
|
|
|
|
FILE *out = stdout;
|
2017-03-03 19:08:36 +01:00
|
|
|
char *pubfile = global_pubkey;
|
2017-03-03 16:42:48 +01:00
|
|
|
u8 public[32];
|
|
|
|
u8 esecret[32];
|
|
|
|
u8 epublic[32];
|
|
|
|
u8 shared[32];
|
|
|
|
u8 iv[8];
|
2017-03-03 16:28:45 +01:00
|
|
|
|
|
|
|
int option;
|
|
|
|
while ((option = optparse_long(options, archive, 0)) != -1) {
|
|
|
|
switch (option) {
|
2017-03-03 19:08:36 +01:00
|
|
|
default:
|
|
|
|
fatal("%s", options->errmsg);
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 19:08:36 +01:00
|
|
|
if (!pubfile)
|
|
|
|
pubfile = default_pubfile();
|
2017-03-03 16:28:45 +01:00
|
|
|
load_key(pubfile, public);
|
|
|
|
|
2017-03-03 19:52:26 +01:00
|
|
|
infile = optparse_arg(options);
|
|
|
|
if (infile) {
|
|
|
|
in = fopen(infile, "rb");
|
|
|
|
if (!in)
|
|
|
|
fatal("could not open input file -- %s", infile);
|
|
|
|
}
|
|
|
|
|
|
|
|
outfile = optparse_arg(options);
|
|
|
|
if (!outfile && infile) {
|
|
|
|
static const char suffix[] = ".enchive";
|
|
|
|
size_t len = strlen(infile);
|
|
|
|
outfile = malloc(len + sizeof(suffix));
|
|
|
|
if (!outfile)
|
|
|
|
fatal("out of memory");
|
|
|
|
memcpy(outfile, infile, len);
|
|
|
|
memcpy(outfile + len, suffix, sizeof(suffix));
|
|
|
|
}
|
|
|
|
if (outfile) {
|
|
|
|
out = fopen(outfile, "wb");
|
|
|
|
if (!out)
|
|
|
|
fatal("could not open output file -- %s", infile);
|
|
|
|
}
|
|
|
|
|
2017-03-03 16:28:45 +01:00
|
|
|
/* Generare ephemeral keypair. */
|
|
|
|
generate_secret(esecret);
|
|
|
|
compute_public(epublic, esecret);
|
|
|
|
|
|
|
|
compute_shared(shared, esecret, public);
|
|
|
|
secure_entropy(iv, sizeof(iv));
|
2017-03-03 19:52:26 +01:00
|
|
|
if (!fwrite(iv, sizeof(iv), 1, out))
|
2017-03-03 16:28:45 +01:00
|
|
|
fatal("failed to write IV to archive");
|
2017-03-03 19:52:26 +01:00
|
|
|
if (!fwrite(epublic, sizeof(epublic), 1, out))
|
2017-03-03 16:28:45 +01:00
|
|
|
fatal("failed to write ephemeral key to archive");
|
2017-03-03 19:52:26 +01:00
|
|
|
symmetric_encrypt(in, out, shared, iv);
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
command_extract(struct optparse *options)
|
|
|
|
{
|
|
|
|
static const struct optparse_long extract[] = {
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2017-03-03 19:52:26 +01:00
|
|
|
char *infile;
|
|
|
|
char *outfile;
|
|
|
|
FILE *in = stdin;
|
|
|
|
FILE *out = stdin;
|
2017-03-03 19:08:36 +01:00
|
|
|
char *secfile = global_seckey;
|
2017-03-03 16:42:48 +01:00
|
|
|
u8 secret[32];
|
|
|
|
u8 epublic[32];
|
|
|
|
u8 shared[32];
|
|
|
|
u8 iv[8];
|
2017-03-03 16:28:45 +01:00
|
|
|
|
|
|
|
int option;
|
|
|
|
while ((option = optparse_long(options, extract, 0)) != -1) {
|
|
|
|
switch (option) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 19:08:36 +01:00
|
|
|
if (!secfile)
|
|
|
|
secfile = default_secfile();
|
2017-03-03 16:28:45 +01:00
|
|
|
load_key(secfile, secret);
|
|
|
|
|
2017-03-03 19:52:26 +01:00
|
|
|
infile = optparse_arg(options);
|
|
|
|
if (infile) {
|
|
|
|
in = fopen(infile, "rb");
|
|
|
|
if (!in)
|
|
|
|
fatal("could not open input file -- %s", infile);
|
|
|
|
}
|
|
|
|
|
|
|
|
outfile = optparse_arg(options);
|
|
|
|
if (!outfile && infile) {
|
|
|
|
static const char suffix[] = ".enchive";
|
|
|
|
size_t slen = sizeof(suffix) - 1;
|
|
|
|
size_t len = strlen(infile);
|
|
|
|
if (len <= slen || strcmp(suffix, infile + len - slen) != 0)
|
|
|
|
fatal("could not determine output filename from %s", infile);
|
|
|
|
outfile = malloc(len - slen);
|
|
|
|
if (!outfile)
|
|
|
|
fatal("out of memory");
|
|
|
|
memcpy(outfile, infile, len - slen);
|
|
|
|
outfile[len - slen] = 0;
|
|
|
|
}
|
|
|
|
if (outfile) {
|
|
|
|
out = fopen(outfile, "wb");
|
|
|
|
if (!out)
|
|
|
|
fatal("could not open output file -- %s", infile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(fread(iv, sizeof(iv), 1, in)))
|
2017-03-03 16:28:45 +01:00
|
|
|
fatal("failed to read IV from archive");
|
2017-03-03 19:52:26 +01:00
|
|
|
if (!(fread(epublic, sizeof(epublic), 1, in)))
|
2017-03-03 16:28:45 +01:00
|
|
|
fatal("failed to read ephemeral key from archive");
|
|
|
|
compute_shared(shared, secret, epublic);
|
2017-03-03 19:52:26 +01:00
|
|
|
symmetric_decrypt(in, out, shared, iv);
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
command_help(struct optparse *options)
|
|
|
|
{
|
|
|
|
static const struct optparse_long help[] = {
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2017-03-03 19:08:36 +01:00
|
|
|
char *command;
|
|
|
|
|
2017-03-03 16:28:45 +01:00
|
|
|
int option;
|
|
|
|
while ((option = optparse_long(options, help, 0)) != -1) {
|
|
|
|
switch (option) {
|
2017-03-03 19:08:36 +01:00
|
|
|
default:
|
|
|
|
fatal("%s", options->errmsg);
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-03 19:08:36 +01:00
|
|
|
|
|
|
|
command = optparse_arg(options);
|
|
|
|
if (!command)
|
|
|
|
command = "help";
|
|
|
|
|
|
|
|
switch (parse_command(command)) {
|
|
|
|
case COMMAND_UNKNOWN:
|
|
|
|
case COMMAND_AMBIGUOUS:
|
|
|
|
fatal("unknown command -- %s\n", command);
|
|
|
|
break;
|
|
|
|
case COMMAND_KEYGEN:
|
|
|
|
fputs(docs_keygen, stdout);
|
|
|
|
break;
|
|
|
|
case COMMAND_ARCHIVE:
|
|
|
|
fputs(docs_archive, stdout);
|
|
|
|
break;
|
|
|
|
case COMMAND_EXTRACT:
|
|
|
|
fputs(docs_extract, stdout);
|
|
|
|
break;
|
|
|
|
case COMMAND_HELP:
|
|
|
|
fputs(docs_help, stdout);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_usage(FILE *f)
|
|
|
|
{
|
|
|
|
fputs(docs_usage, f);
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
static const struct optparse_long global[] = {
|
2017-03-03 19:08:36 +01:00
|
|
|
{"random-device", 'r', OPTPARSE_REQUIRED},
|
|
|
|
{"pubkey", 'p', OPTPARSE_REQUIRED},
|
|
|
|
{"seckey", 's', OPTPARSE_REQUIRED},
|
2017-03-03 16:28:45 +01:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
|
|
|
int option;
|
|
|
|
char *command;
|
|
|
|
struct optparse options[1];
|
|
|
|
optparse_init(options, argv);
|
|
|
|
options->permute = 0;
|
|
|
|
(void)argc;
|
|
|
|
|
|
|
|
while ((option = optparse_long(options, global, 0)) != -1) {
|
|
|
|
switch (option) {
|
2017-03-03 19:08:36 +01:00
|
|
|
case 'r':
|
|
|
|
global_random_device = options->optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
global_pubkey = options->optarg;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
global_seckey = options->optarg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("%s", options->errmsg);
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
command = optparse_arg(options);
|
|
|
|
options->permute = 1;
|
|
|
|
if (!command) {
|
2017-03-03 19:08:36 +01:00
|
|
|
fprintf(stderr, "enchive: missing command\n");
|
|
|
|
print_usage(stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (parse_command(command)) {
|
|
|
|
case COMMAND_UNKNOWN:
|
|
|
|
case COMMAND_AMBIGUOUS:
|
|
|
|
fprintf(stderr, "enchive: unknown command, %s\n", command);
|
|
|
|
print_usage(stderr);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
case COMMAND_KEYGEN:
|
|
|
|
command_keygen(options);
|
|
|
|
break;
|
|
|
|
case COMMAND_ARCHIVE:
|
|
|
|
command_archive(options);
|
|
|
|
break;
|
|
|
|
case COMMAND_EXTRACT:
|
|
|
|
command_extract(options);
|
|
|
|
break;
|
|
|
|
case COMMAND_HELP:
|
|
|
|
command_help(options);
|
|
|
|
break;
|
2017-03-03 16:28:45 +01:00
|
|
|
}
|
2017-03-03 14:25:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|