sigchk

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

commit 5718ccdf09d6f1ef47cfd57d94c2ff9bbdbcbdd6
parent 90d54f352d87fa5d78ad774fbf13f6039dc825dd
Author: z3bra <willyatmailoodotorg>
Date:   Fri Apr 29 21:18:03 2016

Use const on arguments passed

Diffstat:
 sick.c | 53 ++++++++++++++++++++++++++---------------------------
 1 file changed, 26 insertions(+), 27 deletions(-)

diff --git a/sick.c b/sick.c @@ -29,21 +29,21 @@ enum { ACTION_DEFAULT = ACTION_CHECK }; -void usage(char *name); +void usage(const char *name); char *base_name(char *path); -int check_magic(char *file); -unsigned char *read_sig(char *file); -int unsign(char *file); -int keygen(char *alias); -int sign(char *file, char *key); -int verify(char *file, char *key); -int check(char *file, char *keyring); +int check_magic(const char *file); +unsigned char *read_sig(const char *file); +int unsign(const char *file); +int keygen(const char *alias); +int sign(const char *file, const char *key); +int verify(const char *file, const char *key); +int check(const char *file, char *keyring); int verbose = 0; void -usage(char *name) +usage(const char *name) { fprintf(stderr, "usage: %s [-f key] [-cgsuv] [file]\n", name); exit(1); @@ -57,7 +57,7 @@ base_name(char *path) } int -check_magic(char *file) +check_magic(const char *file) { int fd = 0, is_magic = -1; struct stat sb; @@ -91,7 +91,7 @@ check_magic(char *file) } unsigned char * -read_sig(char *file) +read_sig(const char *file) { int fd = 0; struct stat sb; @@ -121,7 +121,7 @@ read_sig(char *file) * string is found */ int -unsign(char *file) +unsign(const char *file) { size_t len = 0; struct stat sb; @@ -145,18 +145,13 @@ unsign(char *file) } int -keygen(char *alias) +keygen(const char *alias) { int fd = 0; size_t len = 0; char file[PATH_MAX]; unsigned char pub[PUBSIZ], priv[PRIVSIZ], seed[PUBSIZ]; - if (alias == NULL) { - alias = malloc(sizeof(DEFAULT_ALIAS)); - strncpy(alias, DEFAULT_ALIAS, sizeof(DEFAULT_ALIAS)); - } - len = strnlen(alias, PATH_MAX); ed25519_create_seed(seed); @@ -199,15 +194,16 @@ keygen(char *alias) } int -sign(char *file, char *key) +sign(const char *file, const char *key) { int fd = 0; size_t len = 0; - char *base = NULL; + char *tmp, *base = NULL; unsigned char sig[64], priv[PRIVSIZ], *msg = NULL; /* set the file basename as the message for signing */ - base = base_name(file); + tmp = strdup(file); + base = base_name(tmp); len = strnlen(base, PATH_MAX); msg = malloc(len + 1); if (msg == NULL) { @@ -216,6 +212,7 @@ sign(char *file, char *key) } memset(msg, 0, len + 1); memcpy(msg, base, len); + free(tmp); /* read private key content into the priv[] buffer */ fd = open(key, O_RDONLY); @@ -259,11 +256,11 @@ sign(char *file, char *key) } int -verify(char *file, char *key) +verify(const char *file, const char *key) { int fd = 0, check = 0; size_t len = 0; - char *base = NULL; + char *base = NULL, *tmp; unsigned char sig[64], pub[PRIVSIZ], *msg = NULL; memcpy(sig, read_sig(file), 64); @@ -281,11 +278,13 @@ verify(char *file, char *key) close(fd); /* the message is the sigfile's base name */ - base = base_name(file); + tmp = strdup(file); + base = base_name(tmp); len = strnlen(base, PATH_MAX); msg = malloc(len + 1); memset(msg, 0, len + 1); memcpy(msg, base, len); + free(tmp); check = ed25519_verify(sig, msg, len + 1, pub) ? 0 : -1; @@ -297,7 +296,7 @@ verify(char *file, char *key) } int -check(char *file, char *keyring) +check(const char *file, char *keyring) { size_t len = 0; DIR *dirp = NULL; @@ -340,7 +339,7 @@ int main(int argc, char **argv) { int action = ACTION_DEFAULT; - char *argv0 = NULL, *key = NULL; + const char *argv0 = NULL, *key = NULL; char keyring[PATH_MAX]; memset(keyring, 0, PATH_MAX); @@ -380,7 +379,7 @@ main(int argc, char **argv) } break; case ACTION_GENKEY: - keygen(key); + keygen(key ? key : DEFAULT_ALIAS); break; case ACTION_UNSIGN: unsign(*argv);