Skip to content

Commit

Permalink
Re-work some of the demos
Browse files Browse the repository at this point in the history
* Add at least a `-h` option, sometimes more.
* Fix the exit codes of some of them.
* Make them all compile with `LTC_EASY`.

Signed-off-by: Steffen Jaeckel <[email protected]>
  • Loading branch information
sjaeckel committed Dec 11, 2024
1 parent d96605b commit 795e8e8
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 138 deletions.
52 changes: 21 additions & 31 deletions demos/aesgcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#ifndef LTC_GCM_MODE
int main(void)
{
return -1;
}
#else

#include "gcm-file/gcm_filehandle.c"
#include "gcm-file/gcm_file.c"

Expand Down Expand Up @@ -58,33 +64,7 @@ static int mv(const char *old_name, const char *new_name)
return 0;
}

/* https://stackoverflow.com/a/23898449 */
static void scan_hex(const char* str, uint8_t* bytes, size_t blen)
{
uint8_t pos;
uint8_t idx0;
uint8_t idx1;

const uint8_t hashmap[] =
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 01234567 */
0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 89:;<=>? */
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, /* @ABCDEFG */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* HIJKLMNO */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* PQRSTUVW */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* XYZ[\]^_ */
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, /* `abcdefg */
};

for (pos = 0; ((pos < (blen*2)) && (pos < XSTRLEN(str))); pos += 2)
{
idx0 = (uint8_t)(str[pos+0] & 0x1F) ^ 0x10;
idx1 = (uint8_t)(str[pos+1] & 0x1F) ^ 0x10;
bytes[pos/2] = (uint8_t)(hashmap[idx0] << 4) | hashmap[idx1];
}
}

static void die(int ret)
static void LTC_NORETURN die(int ret)
{
fprintf(stderr, "Usage: aesgcm <-e|-d> <infile> <outfile> <88|96 char hex-string 'IV | key'>\n");
exit(ret);
Expand All @@ -97,9 +77,14 @@ int main(int argc, char **argv)
uint8_t keybuf[48] = {0};
char *out = NULL;
const char *mode, *in_file, *out_file, *key_string;
unsigned long ivlen;
unsigned long ivlen, key_len;

if (argc < 5) die(__LINE__);
if (argc < 5) {
if (argc > 1 && strstr(argv[1], "-h"))
die(0);
else
die(__LINE__);
}

arg = 1;
mode = argv[arg++];
Expand All @@ -116,7 +101,11 @@ int main(int argc, char **argv)
keylen = XSTRLEN(key_string);
if (keylen != 88 && keylen != 96) die(__LINE__);

scan_hex(key_string, keybuf, keylen/2);
key_len = sizeof(keybuf);
if ((err = base16_decode(key_string, keylen, keybuf, &key_len)) != CRYPT_OK) {
fprintf(stderr, "boooh %s\n", error_to_string(err));
die(__LINE__);
}

register_all_ciphers();

Expand Down Expand Up @@ -148,3 +137,4 @@ int main(int argc, char **argv)

return ret;
}
#endif
56 changes: 34 additions & 22 deletions demos/crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,29 @@

#include <tomcrypt.h>

static int LTC_NORETURN usage(char *name)
static int LTC_NORETURN die(int status)
{
int x;

printf("Usage encrypt: %s cipher infile outfile\n", name);
printf("Usage decrypt: %s -d cipher infile outfile\n", name);
printf("Usage test: %s -t cipher\nCiphers:\n", name);
int x, w, tot = 0;
FILE* o = status == EXIT_SUCCESS ? stdout : stderr;
fprintf(o,
"Usage encrypt: crypt <cipher> <infile> <outfile>\n"
"Usage decrypt: crypt -d <cipher> <infile> <outfile>\n"
"Usage test: crypt -t <cipher>\n"
"This help: crypt -h\n\nCiphers:\n\t");
for (x = 0; cipher_descriptor[x].name != NULL; x++) {
printf("%s\n",cipher_descriptor[x].name);
w = fprintf(o, "%-14s",cipher_descriptor[x].name);
if (w < 0) {
status = EXIT_FAILURE;
break;
}
tot += w;
if (tot >= 70) {
fprintf(o, "\n\t");
tot = 0;
}
}
exit(1);
if (tot != 0) fprintf(o, "\n");
exit(status);
}

int main(int argc, char *argv[])
Expand All @@ -48,24 +60,24 @@ int main(int argc, char *argv[])
cipher = argv[2];
cipher_idx = find_cipher(cipher);
if (cipher_idx == -1) {
printf("Invalid cipher %s entered on command line.\n", cipher);
exit(-1);
fprintf(stderr, "Invalid cipher %s entered on command line.\n", cipher);
die(EXIT_FAILURE);
} /* if */
if (cipher_descriptor[cipher_idx].test)
{
if (cipher_descriptor[cipher_idx].test() != CRYPT_OK)
{
printf("Error when testing cipher %s.\n", cipher);
exit(-1);
if (cipher_descriptor[cipher_idx].test) {
if (cipher_descriptor[cipher_idx].test() != CRYPT_OK) {
fprintf(stderr, "Error when testing cipher %s.\n", cipher);
die(EXIT_FAILURE);
}
else
{
else {
printf("Testing cipher %s succeeded.\n", cipher);
exit(0);
} /* if ... else */
} /* if */
exit(EXIT_SUCCESS);
}
} else {
fprintf(stderr, "Cipher %s has no tests.\n", cipher);
exit(EXIT_SUCCESS);
}
}
return usage(argv[0]);
return die(argc > 1 && strstr(argv[1], "-h") != NULL ? EXIT_SUCCESS : EXIT_FAILURE);
}

if (!strcmp(argv[1], "-d")) {
Expand Down
23 changes: 14 additions & 9 deletions demos/hashsum.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,17 @@ static void die(int status)
{
unsigned long w, x;
FILE* o = status == EXIT_SUCCESS ? stdout : stderr;
fprintf(o, "usage: %s -a algorithm [-c] [file...]\n\n", hashsum);
fprintf(o, "\t-c\tCheck the hash(es) of the file(s) written in [file].\n");
fprintf(o, "\t\t(-a not required)\n");
fprintf(o, "\nAlgorithms:\n\t");
fprintf(o,
"Usage: %s [-a <algorithm>...] [-c|-h] [<file>...]\n\n"
"\t-c\tCheck the hash(es) of the file(s) written in <file>.\n"
"\t\tNote: -a is not required when checking the hash(es).\n"
"\t-h\tThis help\n\n"
"Examples:\n"
"\t%s -a sha1 file > file.sha1sum\n"
"\t%s -c file.sha1sum\n"
"\t%s -a sha1 -a sha256 -a sha512-256 file > file.hashsum\n"
"\t%s -c file.hashsum\n\n"
"Algorithms:\n\t", hashsum, hashsum, hashsum, hashsum, hashsum);
w = 0;
for (x = 0; hash_descriptor[x].name != NULL; x++) {
w += fprintf(o, "%-14s", hash_descriptor[x].name);
Expand All @@ -67,7 +74,7 @@ static void printf_hex(unsigned char* hash_buffer, unsigned long w)

static void check_file(int argn, int argc, char **argv)
{
int err, failed, invalid;
int err, failed = 0, invalid = 0;
unsigned char is_buffer[MAXBLOCKSIZE], should_buffer[MAXBLOCKSIZE];
char buf[PATH_MAX + (MAXBLOCKSIZE * 3)];
/* iterate through all files */
Expand All @@ -82,8 +89,6 @@ static void check_file(int argn, int argc, char **argv)
perror(argv[argn]);
exit(EXIT_FAILURE);
}
failed = 0;
invalid = 0;
/* read the file line by line */
while((s = fgets(buf, sizeof(buf), f)) != NULL)
{
Expand Down Expand Up @@ -163,7 +168,7 @@ static void check_file(int argn, int argc, char **argv)
}
argn++;
}
exit(EXIT_SUCCESS);
exit(failed == 0 && invalid == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}

int main(int argc, char **argv)
Expand All @@ -178,7 +183,7 @@ int main(int argc, char **argv)
/* You need to register algorithms before using them */
register_all_ciphers();
register_all_hashes();
if (argc > 1 && (strcmp("-h", argv[1]) == 0 || strcmp("--help", argv[1]) == 0)) {
if (argc > 1 && strstr(argv[1], "-h")) {
die(EXIT_SUCCESS);
}
if (argc < 3) {
Expand Down
Loading

0 comments on commit 795e8e8

Please sign in to comment.