Skip to content

Commit 795e8e8

Browse files
committed
Re-work some of the demos
* 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 <s@jaeckel.eu>
1 parent d96605b commit 795e8e8

File tree

7 files changed

+232
-138
lines changed

7 files changed

+232
-138
lines changed

demos/aesgcm.c

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
#include <sys/types.h>
1717
#include <dirent.h>
1818
#include <string.h>
19-
#include <errno.h>
2019
#include <fcntl.h>
2120
#include <unistd.h>
2221

22+
#ifndef LTC_GCM_MODE
23+
int main(void)
24+
{
25+
return -1;
26+
}
27+
#else
28+
2329
#include "gcm-file/gcm_filehandle.c"
2430
#include "gcm-file/gcm_file.c"
2531

@@ -58,33 +64,7 @@ static int mv(const char *old_name, const char *new_name)
5864
return 0;
5965
}
6066

61-
/* https://stackoverflow.com/a/23898449 */
62-
static void scan_hex(const char* str, uint8_t* bytes, size_t blen)
63-
{
64-
uint8_t pos;
65-
uint8_t idx0;
66-
uint8_t idx1;
67-
68-
const uint8_t hashmap[] =
69-
{
70-
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 01234567 */
71-
0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 89:;<=>? */
72-
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, /* @ABCDEFG */
73-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* HIJKLMNO */
74-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* PQRSTUVW */
75-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* XYZ[\]^_ */
76-
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, /* `abcdefg */
77-
};
78-
79-
for (pos = 0; ((pos < (blen*2)) && (pos < XSTRLEN(str))); pos += 2)
80-
{
81-
idx0 = (uint8_t)(str[pos+0] & 0x1F) ^ 0x10;
82-
idx1 = (uint8_t)(str[pos+1] & 0x1F) ^ 0x10;
83-
bytes[pos/2] = (uint8_t)(hashmap[idx0] << 4) | hashmap[idx1];
84-
}
85-
}
86-
87-
static void die(int ret)
67+
static void LTC_NORETURN die(int ret)
8868
{
8969
fprintf(stderr, "Usage: aesgcm <-e|-d> <infile> <outfile> <88|96 char hex-string 'IV | key'>\n");
9070
exit(ret);
@@ -97,9 +77,14 @@ int main(int argc, char **argv)
9777
uint8_t keybuf[48] = {0};
9878
char *out = NULL;
9979
const char *mode, *in_file, *out_file, *key_string;
100-
unsigned long ivlen;
80+
unsigned long ivlen, key_len;
10181

102-
if (argc < 5) die(__LINE__);
82+
if (argc < 5) {
83+
if (argc > 1 && strstr(argv[1], "-h"))
84+
die(0);
85+
else
86+
die(__LINE__);
87+
}
10388

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

119-
scan_hex(key_string, keybuf, keylen/2);
104+
key_len = sizeof(keybuf);
105+
if ((err = base16_decode(key_string, keylen, keybuf, &key_len)) != CRYPT_OK) {
106+
fprintf(stderr, "boooh %s\n", error_to_string(err));
107+
die(__LINE__);
108+
}
120109

121110
register_all_ciphers();
122111

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

149138
return ret;
150139
}
140+
#endif

demos/crypt.c

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,29 @@
1212

1313
#include <tomcrypt.h>
1414

15-
static int LTC_NORETURN usage(char *name)
15+
static int LTC_NORETURN die(int status)
1616
{
17-
int x;
18-
19-
printf("Usage encrypt: %s cipher infile outfile\n", name);
20-
printf("Usage decrypt: %s -d cipher infile outfile\n", name);
21-
printf("Usage test: %s -t cipher\nCiphers:\n", name);
17+
int x, w, tot = 0;
18+
FILE* o = status == EXIT_SUCCESS ? stdout : stderr;
19+
fprintf(o,
20+
"Usage encrypt: crypt <cipher> <infile> <outfile>\n"
21+
"Usage decrypt: crypt -d <cipher> <infile> <outfile>\n"
22+
"Usage test: crypt -t <cipher>\n"
23+
"This help: crypt -h\n\nCiphers:\n\t");
2224
for (x = 0; cipher_descriptor[x].name != NULL; x++) {
23-
printf("%s\n",cipher_descriptor[x].name);
25+
w = fprintf(o, "%-14s",cipher_descriptor[x].name);
26+
if (w < 0) {
27+
status = EXIT_FAILURE;
28+
break;
29+
}
30+
tot += w;
31+
if (tot >= 70) {
32+
fprintf(o, "\n\t");
33+
tot = 0;
34+
}
2435
}
25-
exit(1);
36+
if (tot != 0) fprintf(o, "\n");
37+
exit(status);
2638
}
2739

2840
int main(int argc, char *argv[])
@@ -48,24 +60,24 @@ int main(int argc, char *argv[])
4860
cipher = argv[2];
4961
cipher_idx = find_cipher(cipher);
5062
if (cipher_idx == -1) {
51-
printf("Invalid cipher %s entered on command line.\n", cipher);
52-
exit(-1);
63+
fprintf(stderr, "Invalid cipher %s entered on command line.\n", cipher);
64+
die(EXIT_FAILURE);
5365
} /* if */
54-
if (cipher_descriptor[cipher_idx].test)
55-
{
56-
if (cipher_descriptor[cipher_idx].test() != CRYPT_OK)
57-
{
58-
printf("Error when testing cipher %s.\n", cipher);
59-
exit(-1);
66+
if (cipher_descriptor[cipher_idx].test) {
67+
if (cipher_descriptor[cipher_idx].test() != CRYPT_OK) {
68+
fprintf(stderr, "Error when testing cipher %s.\n", cipher);
69+
die(EXIT_FAILURE);
6070
}
61-
else
62-
{
71+
else {
6372
printf("Testing cipher %s succeeded.\n", cipher);
64-
exit(0);
65-
} /* if ... else */
66-
} /* if */
73+
exit(EXIT_SUCCESS);
74+
}
75+
} else {
76+
fprintf(stderr, "Cipher %s has no tests.\n", cipher);
77+
exit(EXIT_SUCCESS);
78+
}
6779
}
68-
return usage(argv[0]);
80+
return die(argc > 1 && strstr(argv[1], "-h") != NULL ? EXIT_SUCCESS : EXIT_FAILURE);
6981
}
7082

7183
if (!strcmp(argv[1], "-d")) {

demos/hashsum.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,17 @@ static void die(int status)
4141
{
4242
unsigned long w, x;
4343
FILE* o = status == EXIT_SUCCESS ? stdout : stderr;
44-
fprintf(o, "usage: %s -a algorithm [-c] [file...]\n\n", hashsum);
45-
fprintf(o, "\t-c\tCheck the hash(es) of the file(s) written in [file].\n");
46-
fprintf(o, "\t\t(-a not required)\n");
47-
fprintf(o, "\nAlgorithms:\n\t");
44+
fprintf(o,
45+
"Usage: %s [-a <algorithm>...] [-c|-h] [<file>...]\n\n"
46+
"\t-c\tCheck the hash(es) of the file(s) written in <file>.\n"
47+
"\t\tNote: -a is not required when checking the hash(es).\n"
48+
"\t-h\tThis help\n\n"
49+
"Examples:\n"
50+
"\t%s -a sha1 file > file.sha1sum\n"
51+
"\t%s -c file.sha1sum\n"
52+
"\t%s -a sha1 -a sha256 -a sha512-256 file > file.hashsum\n"
53+
"\t%s -c file.hashsum\n\n"
54+
"Algorithms:\n\t", hashsum, hashsum, hashsum, hashsum, hashsum);
4855
w = 0;
4956
for (x = 0; hash_descriptor[x].name != NULL; x++) {
5057
w += fprintf(o, "%-14s", hash_descriptor[x].name);
@@ -67,7 +74,7 @@ static void printf_hex(unsigned char* hash_buffer, unsigned long w)
6774

6875
static void check_file(int argn, int argc, char **argv)
6976
{
70-
int err, failed, invalid;
77+
int err, failed = 0, invalid = 0;
7178
unsigned char is_buffer[MAXBLOCKSIZE], should_buffer[MAXBLOCKSIZE];
7279
char buf[PATH_MAX + (MAXBLOCKSIZE * 3)];
7380
/* iterate through all files */
@@ -82,8 +89,6 @@ static void check_file(int argn, int argc, char **argv)
8289
perror(argv[argn]);
8390
exit(EXIT_FAILURE);
8491
}
85-
failed = 0;
86-
invalid = 0;
8792
/* read the file line by line */
8893
while((s = fgets(buf, sizeof(buf), f)) != NULL)
8994
{
@@ -163,7 +168,7 @@ static void check_file(int argn, int argc, char **argv)
163168
}
164169
argn++;
165170
}
166-
exit(EXIT_SUCCESS);
171+
exit(failed == 0 && invalid == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
167172
}
168173

169174
int main(int argc, char **argv)
@@ -178,7 +183,7 @@ int main(int argc, char **argv)
178183
/* You need to register algorithms before using them */
179184
register_all_ciphers();
180185
register_all_hashes();
181-
if (argc > 1 && (strcmp("-h", argv[1]) == 0 || strcmp("--help", argv[1]) == 0)) {
186+
if (argc > 1 && strstr(argv[1], "-h")) {
182187
die(EXIT_SUCCESS);
183188
}
184189
if (argc < 3) {

0 commit comments

Comments
 (0)