sigchk

sign and check files using ed25519
git clone git://z3bra.org/sick
Log | Files | Refs | Submodules | README | LICENSE

commit 39a83f6fdd7d3d18c87d6ecae79fe3eabb6db289
Author: z3bra <willyatmailoodotorg>
Date:   Tue Apr 26 08:51:02 2016

Initial commit

Diffstat:
 .gitmodules |  3 ++-
 arg.h       | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 config.mk   | 10 ++++++++-
 ed25519     |  1 +-
 mkfile      | 23 +++++++++++++++++++-
 sick.c      | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 177 insertions(+), 0 deletions(-)

diff --git a/.gitmodules b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "ed25519"] + path = ed25519 + url = https://github.com/orlp/ed25519.git diff --git a/arg.h b/arg.h @@ -0,0 +1,65 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][1]\ + && argv[0][0] == '-';\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +/* Handles obsolete -NUM syntax */ +#define ARGNUM case '0':\ + case '1':\ + case '2':\ + case '3':\ + case '4':\ + case '5':\ + case '6':\ + case '7':\ + case '8':\ + case '9' + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define ARGNUMF() (brk_ = 1, estrtonum(argv[0], 0, INT_MAX)) + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define LNGARG() &argv[0][0] + +#endif diff --git a/config.mk b/config.mk @@ -0,0 +1,10 @@ +CC = cc +LD = ${CC} + +PREFIX = /usr/local +MANDIR = ${PREFIX}/man + +CPPFLAGS = -I./ed25519/src +CFLAGS = ${CPPFLAGS} -Wall -Wextra -pedantic +LDFLAGS = +LIBS = diff --git a/ed25519 b/ed25519 @@ -0,0 +1 @@ +Subproject commit e65e7f944d03dda8564f9f901e2a9999bbdf71c8 diff --git a/mkfile b/mkfile @@ -0,0 +1,23 @@ +<config.mk + +ED25519_SRC = `{find ed25519/src -name '*.c'} + +SRC = sick.c ${ED25519_SRC} +OBJ = ${SRC:%.c=%.o} + +sick: $OBJ + ${CC} $OBJ ${LDFLAGS} ${LIBS} -o sick + +%.o: %.c + ${CC} ${CFLAGS} -c $stem.c -o $stem.o + +clean:V: + rm -f $OBJ sick + +install:V: sick + mkdir -p ${DESTDIR}${PREFIX}/bin + cp sick ${DESTDIR}${PREFIX}/bin/sick + chmod 755 ${DESTDIR}${PREFIX}/bin/sick + +uninstall:V: + rm ${DESTDIR}${PREFIX}/bin/sick diff --git a/sick.c b/sick.c @@ -0,0 +1,75 @@ +#include <limits.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#include "arg.h" +#include "ed25519.h" + +#define PUBSIZ 32 +#define PRIVSIZ 64 + +void usage(char *name); +int genkey(char *alias); + +void +usage(char *name) +{ + fprintf(stderr, "usage: %s [-g]\n", name); + exit(1); +} + +int +genkey(char *alias) +{ + int fd = 0; + char file[PATH_MAX]; + unsigned char pub[PUBSIZ], priv[PRIVSIZ], seed[PUBSIZ]; + + ed25519_create_seed(seed); + ed25519_create_keypair(pub, priv, seed); + + /* write pubkey to a file name <alias>.pub */ + memset(file, 0, PATH_MAX); + strncpy(file, alias, strnlen(alias, PATH_MAX)); + strncat(file, ".pub", PATH_MAX); + fd = open(file, O_CREAT|O_WRONLY|O_TRUNC, 0644); + write(fd, pub, PUBSIZ); + close(fd); + + /* write privkey to a file name <alias>.key */ + memset(file, 0, PATH_MAX); + strncpy(file, alias, strnlen(alias, PATH_MAX)); + strncat(file, ".key", PATH_MAX); + fd = open(file, O_CREAT|O_WRONLY|O_TRUNC, 0600); + write(fd, priv, PRIVSIZ); + close(fd); + return 0; +} + +int +main(int argc, char **argv) +{ + char *argf = NULL; + char *argv0 = NULL; + + ARGBEGIN{ + case 'g': + if (argc > 1) { + argf = ARGF(); + } else { + argf = malloc(sizeof("ed25519")); + strncpy(argf, "ed25519", sizeof("ed25519")); + } + genkey(argf); + break; + default: + usage(argv0); + }ARGEND; + + return 0; +}+ \ No newline at end of file