Skip to content

Commit

Permalink
Merge pull request #678 from libtom/some-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaeckel authored Dec 11, 2024
2 parents 507c668 + f6e71df commit a81dab9
Show file tree
Hide file tree
Showing 25 changed files with 277 additions and 227 deletions.
4 changes: 0 additions & 4 deletions .ci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,3 @@ if [ -a testok.txt ] && [ -f testok.txt ]; then
exit 0
fi
exit 1
# ref: $Format:%D$
# git commit: $Format:%H$
# commit time: $Format:%ai$
4 changes: 0 additions & 4 deletions .ci/check_source.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,3 @@ echo "checking..."
./helper.pl --check-all || exit 1

exit 0

# ref: $Format:%D$
# git commit: $Format:%H$
# commit time: $Format:%ai$
4 changes: 0 additions & 4 deletions .ci/clang-tidy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,3 @@ portability-* -- -DUSE_LTM -DLTM_DESC -Isrc/headers -I../libtommath || { echo "c
echo "clang-tidy ok"

exit 0

# ref: $Format:%D$
# git commit: $Format:%H$
# commit time: $Format:%ai$
4 changes: 0 additions & 4 deletions .ci/coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,3 @@ else
fi

exit 0

# ref: $Format:%D$
# git commit: $Format:%H$
# commit time: $Format:%ai$
4 changes: 0 additions & 4 deletions .ci/coverage_more.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,3 @@ fi


exit 0

# ref: $Format:%D$
# git commit: $Format:%H$
# commit time: $Format:%ai$
4 changes: 0 additions & 4 deletions .ci/meta_builds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,3 @@ echo
echo "Build full debug..."

make -j$MAKE_JOBS CFLAGS="$2 $CFLAGS $4" EXTRALIBS="$EXTRALIBS" all_test LTC_DEBUG=2 V=1 1>gcc_1.txt 2>gcc_2.txt

# ref: $Format:%D$
# git commit: $Format:%H$
# commit time: $Format:%ai$
4 changes: 0 additions & 4 deletions .ci/printinfo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,3 @@ then
fi
echo "${CC}="`${CC} -dumpversion`
echo

# ref: $Format:%D$
# git commit: $Format:%H$
# commit time: $Format:%ai$
4 changes: 0 additions & 4 deletions .ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,3 @@ else
fi

exit 0

# ref: $Format:%D$
# git commit: $Format:%H$
# commit time: $Format:%ai$
4 changes: 0 additions & 4 deletions .ci/testbuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,3 @@ if find testok.txt -type f 1>/dev/null 2>/dev/null ; then
exit 0
fi
exit 1

# ref: $Format:%D$
# git commit: $Format:%H$
# commit time: $Format:%ai$
4 changes: 0 additions & 4 deletions .ci/valgrind.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,3 @@ readonly VALGRIND_EXTRA_OPTS=$(get_suppfile)
valgrind $VALGRIND_OPTS $VALGRIND_EXTRA_OPTS ./test >test_std.txt 2> >(tee -a test_err.txt >&2) || { kill $alive_pid; echo "Valgrind failed"; exit 1; }

kill $alive_pid

# ref: $Format:%D$
# git commit: $Format:%H$
# commit time: $Format:%ai$
42 changes: 18 additions & 24 deletions demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
# Options
# -----------------------------------------------------------------------------
option(BUILD_USEFUL_DEMOS "Build useful demos (hashsum)" FALSE)
option(BUILD_USABLE_DEMOS "Build usable demos (crypt sizes constants pem-info)" FALSE)
option(BUILD_BROKEN_DEMOS "Build broken demos (aesgcm openssh-privkey openssl-enc timing)" FALSE)
option(
BUILD_USABLE_DEMOS
"Build usable demos (aesgcm constants crypt openssh-privkey openssl-enc pem-info sizes timing)"
FALSE
)
option(BUILD_TEST_DEMOS "Build test demos (small tv_gen)" FALSE)

option(INSTALL_DEMOS "Install enabled demos (USEFUL and/or USABLE) and ltc wrapper script" FALSE)
option(INSTALL_BROKEN_DEMOS "Install broken demos and ltc wrapper script" FALSE)

# -----------------------------------------------------------------------------
# Useful demos
Expand All @@ -26,30 +28,22 @@ endif()
#
# Demos that are usable but only rarely make sense to be installed
#
# USEABLE_DEMOS = crypt sizes constants pem-info
# USEABLE_DEMOS = aesgcm constants crypt openssh-privkey openssl-enc pem-info sizes timing
# -----------------------------------------------------------------------------

if(BUILD_USABLE_DEMOS)
list(APPEND USABLE_DEMOS_TARGETS crypt sizes constants pem-info)
endif()

# -----------------------------------------------------------------------------
# Broken demos
#
# Demos that are kind of useful, but in some way broken
#
# * aesgcm - can't be built with LTC_EASY
# * openssl-enc - can't be built with LTC_EASY
# * openssh-privkey - can't be built with LTC_EASY
# * timing - not really broken, but older gcc builds spit warnings
#
# BROKEN_DEMOS = aesgcm openssl-enc openssh-privkey timing
# -----------------------------------------------------------------------------

if(BUILD_BROKEN_DEMOS AND INSTALL_BROKEN_DEMOS)
list(APPEND USABLE_DEMOS_TARGETS aesgcm openssh-privkey openssl-enc timing)
elseif(BUILD_BROKEN_DEMOS)
list(APPEND ALL_DEMOS_TARGETS aesgcm openssh-privkey openssl-enc timing)
list(
APPEND
USABLE_DEMOS_TARGETS
aesgcm
constants
crypt
openssh-privkey
openssl-enc
pem-info
sizes
timing
)
endif()

# -----------------------------------------------------------------------------
Expand Down
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 a81dab9

Please sign in to comment.