Merge pull request #203 from libtom/improve/crypt

Improve hashsum
This commit is contained in:
Steffen Jaeckel 2017-05-04 16:03:57 +02:00 committed by GitHub
commit 87be6d3d70
4 changed files with 250 additions and 55 deletions

View File

@ -32,7 +32,7 @@ fi
./sizes
./constants
for i in $(./hashsum | tail -n +3 | awk '{print $1}' | sort); do echo -n "$i: " && ./hashsum $i testprof/test.key ; done > hashsum_tv.txt
for i in $(for j in $(echo $(./hashsum -h | tail -n +3)); do echo $j; done | sort); do echo -n "$i: " && ./hashsum -a $i testprof/test.key ; done > hashsum_tv.txt
difftroubles=$(diff -i -w -B hashsum_tv.txt notes/hashsum_tv.txt | grep '^<') || true
if [ -n "$difftroubles" ]; then
echo "FAILURE: hashsum_tv.tx"

View File

@ -9,37 +9,232 @@
#include <tomcrypt.h>
int errno;
#if _POSIX_C_SOURCE >= 200112L
#include <libgen.h>
#else
#define basename(x) x
#endif
void register_algs(void);
/* thanks http://stackoverflow.com/a/8198009 */
#define _base(x) ((x >= '0' && x <= '9') ? '0' : \
(x >= 'a' && x <= 'f') ? 'a' - 10 : \
(x >= 'A' && x <= 'F') ? 'A' - 10 : \
'\255')
#define HEXOF(x) (x - _base(x))
static void register_algs(void);
static char* hashsum;
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", hashsum);
fprintf(o, "Algorithms:\n");
w = 0;
for (x = 0; hash_descriptor[x].name != NULL; x++) {
w += fprintf(o, "%-14s", hash_descriptor[x].name);
if (w >= 70) {
fprintf(o, "\n");
w = 0;
}
}
if (w != 0) fprintf(o, "\n");
free(hashsum);
exit(status);
}
static void printf_hex(unsigned char* hash_buffer, unsigned long w)
{
unsigned long x;
for (x = 0; x < w; x++) {
printf("%02x",hash_buffer[x]);
}
}
static void check_file(int argn, int argc, char **argv)
{
int err, failed, invalid;
unsigned char is_buffer[MAXBLOCKSIZE], should_buffer[MAXBLOCKSIZE];
char buf[PATH_MAX + (MAXBLOCKSIZE * 3)];
/* iterate through all files */
while(argn < argc) {
char* s;
FILE* f = fopen(argv[argn], "rb");
if(f == NULL) {
int n = snprintf(buf, sizeof(buf), "%s: %s", hashsum, argv[argn]);
if (n > 0 && n < (int)sizeof(buf))
perror(buf);
else
perror(argv[argn]);
exit(EXIT_FAILURE);
}
failed = 0;
invalid = 0;
/* read the file line by line */
while((s = fgets(buf, sizeof(buf), f)) != NULL)
{
int tries, n;
unsigned long hash_len, w, x;
char* space = strstr(s, " ");
if (space == NULL) {
fprintf(stderr, "%s: no properly formatted checksum lines found\n", hashsum);
goto ERR;
}
hash_len = space - s;
hash_len /= 2;
/* convert the hex-string back to binary */
for (x = 0; x < hash_len; ++x) {
should_buffer[x] = HEXOF(s[x*2]) << 4 | HEXOF(s[x*2 + 1]);
}
space++;
if (*space != '*') {
fprintf(stderr, "%s: unsupported input mode '%c'\n", hashsum, *space);
goto ERR;
}
space++;
for (n = 0; n < (buf + sizeof(buf)) - space; ++n) {
if(iscntrl(space[n])) {
space[n] = '\0';
break;
}
}
/* try all hash algorithms that have the appropriate hash size */
tries = 0;
for (x = 0; hash_descriptor[x].name != NULL; ++x) {
if (hash_descriptor[x].hashsize == hash_len) {
tries++;
w = sizeof(is_buffer);
if ((err = hash_file(x, space, is_buffer, &w)) != CRYPT_OK) {
fprintf(stderr, "%s: File hash error: %s: %s\n", hashsum, space, error_to_string(err));
ERR:
fclose(f);
exit(EXIT_FAILURE);
}
if(XMEMCMP(should_buffer, is_buffer, w) == 0) {
printf("%s: OK\n", space);
break;
}
}
} /* for */
if (hash_descriptor[x].name == NULL) {
if(tries > 0) {
printf("%s: FAILED\n", space);
failed++;
}
else {
invalid++;
}
}
} /* while */
fclose(f);
if(invalid) {
fprintf(stderr, "%s: WARNING: %d %s is improperly formatted\n", hashsum, invalid, invalid > 1?"lines":"line");
}
if(failed) {
fprintf(stderr, "%s: WARNING: %d computed %s did NOT match\n", hashsum, failed, failed > 1?"checksums":"checksum");
}
argn++;
}
exit(EXIT_SUCCESS);
}
int main(int argc, char **argv)
{
int idx, z;
int idx, check, z, err, argn;
unsigned long w, x;
unsigned char hash_buffer[MAXBLOCKSIZE];
hashsum = strdup(basename(argv[0]));
/* You need to register algorithms before using them */
register_algs();
if (argc < 2) {
printf("usage: ./hash algorithm file [file ...]\n");
printf("Algorithms:\n");
for (x = 0; hash_descriptor[x].name != NULL; x++) {
printf(" %s (%d)\n", hash_descriptor[x].name, hash_descriptor[x].ID);
if (argc > 1 && (strcmp("-h", argv[1]) == 0 || strcmp("--help", argv[1]) == 0)) {
die(EXIT_SUCCESS);
}
if (argc < 3) {
die(EXIT_FAILURE);
}
argn = 1;
check = 0;
idx = -2;
while(argn < argc){
if(strcmp("-a", argv[argn]) == 0) {
argn++;
if(argn < argc) {
idx = find_hash(argv[argn]);
if (idx == -1) {
struct {
const char* is;
const char* should;
} shasum_compat[] =
{
#ifdef LTC_SHA1
{ "1", sha1_desc.name },
#endif
#ifdef LTC_SHA224
{ "224", sha224_desc.name },
#endif
#ifdef LTC_SHA256
{ "256", sha256_desc.name },
#endif
#ifdef LTC_SHA384
{ "384", sha384_desc.name },
#endif
#ifdef LTC_SHA512
{ "512", sha512_desc.name },
#endif
#ifdef LTC_SHA512_224
{ "512224", sha512_224_desc.name },
#endif
#ifdef LTC_SHA512_256
{ "512256", sha512_256_desc.name },
#endif
{ NULL, NULL }
};
for (x = 0; shasum_compat[x].is != NULL; ++x) {
if(XSTRCMP(shasum_compat[x].is, argv[argn]) == 0) {
idx = find_hash(shasum_compat[x].should);
break;
}
}
}
if (idx == -1) {
fprintf(stderr, "%s: Unrecognized algorithm\n", hashsum);
die(EXIT_FAILURE);
}
argn++;
continue;
}
else {
die(EXIT_FAILURE);
}
}
exit(EXIT_SUCCESS);
if(strcmp("-c", argv[argn]) == 0) {
check = 1;
argn++;
continue;
}
break;
}
idx = find_hash(argv[1]);
if (idx == -1) {
fprintf(stderr, "\nInvalid hash specified on command line.\n");
return -1;
if (check == 1) {
check_file(argn, argc, argv);
}
if (argc == 2) {
if (argc == argn) {
w = sizeof(hash_buffer);
if ((errno = hash_filehandle(idx, stdin, hash_buffer, &w)) != CRYPT_OK) {
printf("File hash error: %s\n", error_to_string(errno));
if ((err = hash_filehandle(idx, stdin, hash_buffer, &w)) != CRYPT_OK) {
fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err));
return EXIT_FAILURE;
} else {
for (x = 0; x < w; x++) {
printf("%02x",hash_buffer[x]);
@ -47,14 +242,13 @@ int main(int argc, char **argv)
printf(" *-\n");
}
} else {
for (z = 2; z < argc; z++) {
for (z = 3; z < argc; z++) {
w = sizeof(hash_buffer);
if ((errno = hash_file(idx,argv[z],hash_buffer,&w)) != CRYPT_OK) {
printf("File hash error: %s\n", error_to_string(errno));
if ((err = hash_file(idx,argv[z],hash_buffer,&w)) != CRYPT_OK) {
fprintf(stderr, "%s: File hash error: %s\n", hashsum, error_to_string(err));
return EXIT_FAILURE;
} else {
for (x = 0; x < w; x++) {
printf("%02x",hash_buffer[x]);
}
printf_hex(hash_buffer, w);
printf(" *%s\n", argv[z]);
}
}
@ -62,7 +256,7 @@ int main(int argc, char **argv)
return EXIT_SUCCESS;
}
void register_algs(void)
static void register_algs(void)
{
int err;
LTC_UNUSED_PARAM(err);

View File

@ -1,29 +1,29 @@
blake2b-160: 684b540eb245ebba2cd66b7814fe6d787868a5bb *testprof/test.key
blake2b-256: 3aaa08120aa0174c105fd1f3412e3da30c3bbb9073569b0b8ae458b34de7780e *testprof/test.key
blake2b-384: c33f39ab19099d5dea6789479e23447857803a3f45361ea58dd69c7b8037c8697544b6a741e236215adcb8b38fa0cfc4 *testprof/test.key
blake2b-512: a2daabae6996e3bbfb76c5d277fb881107037c756173f510d83a78f669cf0b70a5088b971e30ca2168381568ea3d30b80a7b500944a5e2e3294f25aed4c3a5f1 *testprof/test.key
blake2s-128: 7e1f9b6de11a3c597308f9d8aacd7321 *testprof/test.key
blake2s-160: 3d1c9ae1a0afe8a6ef8b61d5380de7b061cd353c *testprof/test.key
blake2s-224: 95dacd83d0421b2d69a52d92ee54143bb97c91a61450d917a88d4388 *testprof/test.key
blake2s-256: 92ac6dc88b9ce4a75b2a54783435a37919303f713396db0e9ccd976d8c55f1cf *testprof/test.key
chc_hash: d50da1ea9b53c0b891c8bcb0a4c6e5e0 *testprof/test.key
md2: 0b327a17a102580ccde8081c18f137c7 *testprof/test.key
md4: 6df7b3c1f7fbcbf6b8246dc0046e9d6b *testprof/test.key
md5: 48fd9ed11c633069c3cf721611696a73 *testprof/test.key
rmd128: a78170e11d56605eb0f5e05e6a218e61 *testprof/test.key
rmd160: 56236b14cd15958c58136a295fa2b07c5edee009 *testprof/test.key
rmd256: a0a658b25b8841f20be8151eb5e2c1229f085dd5cf7aa8bef4c8e07ea5738913 *testprof/test.key
rmd320: 2ec15ca947f29dd8cef4782aeaeb4541ac00dc29e1649c4f5f4578ef0287091e513f9da423ce7319 *testprof/test.key
sha1: c17a11eef02d7a2ad7250bc2ee22bdf37ed21f0f *testprof/test.key
sha224: b226daa069512c849d55866470c23543d87e1fb768171efdcc6ab59a *testprof/test.key
sha256: 76ec7faebdc42a4de35ca70024c2d273e9f7856ca61612e89f5f66350ba8cf5f *testprof/test.key
sha3-224: 09525d6410c462e00e72ccd4304290bc59ff2b1b8e5322dbc197e169 *testprof/test.key
sha3-256: b9016fc9976545ad0183775d88f20df5295d40b31db00d85f6916333260b8ae2 *testprof/test.key
sha3-384: 5452977763b602df4185bbe7cce193346b09ee20d4a45a780fd44237e335ceaa71b9da06242621e159d796db9043c9be *testprof/test.key
sha3-512: 1fadf63073c8613d38d5c944e2e3f7e086d94d93dceb2eec1e9693fd44158f3127c90cbaa8a13035cc9ff9b3572c9dce836bea15c92e8a78b036ec382caceb0f *testprof/test.key
sha384: 7cc080c662524617e56d5a49f1c185909e9b1858a86684161ddd97fc5426f693b53f59d779de16da25dcb1259df879e5 *testprof/test.key
sha512: f90afe4d254716a9770fd2c4f29ca544f2975f961cbd7fa207117383e4e48f7a2e1ade4aac333a2cb8c227dd6af2fff4d87c31c2a3916ae24d507c7c94c21818 *testprof/test.key
sha512-224: 64ec566b5c2de736eab1b4e8542dc110f736819ea7f8f48e3712fbad *testprof/test.key
sha512-256: db880bce4beb7246510febb961c7595aab57a15de6f90cd079f145e476b5d773 *testprof/test.key
tiger: 97d713850e7affac30a642572c1ee7b18793d5b9e0ef5932 *testprof/test.key
whirlpool: d7d41c755e0f28313f254cb198e0bfa42f56670595d97b80dceec754825d69938a9c11e5bf16e9a3809a62a09bddf021f3dbff4302ceec7ba46c88b41772b711 *testprof/test.key
blake2b-160: 684b540eb245ebba2cd66b7814fe6d787868a5bb *testprof/test.key
blake2b-256: 3aaa08120aa0174c105fd1f3412e3da30c3bbb9073569b0b8ae458b34de7780e *testprof/test.key
blake2b-384: c33f39ab19099d5dea6789479e23447857803a3f45361ea58dd69c7b8037c8697544b6a741e236215adcb8b38fa0cfc4 *testprof/test.key
blake2b-512: a2daabae6996e3bbfb76c5d277fb881107037c756173f510d83a78f669cf0b70a5088b971e30ca2168381568ea3d30b80a7b500944a5e2e3294f25aed4c3a5f1 *testprof/test.key
blake2s-128: 7e1f9b6de11a3c597308f9d8aacd7321 *testprof/test.key
blake2s-160: 3d1c9ae1a0afe8a6ef8b61d5380de7b061cd353c *testprof/test.key
blake2s-224: 95dacd83d0421b2d69a52d92ee54143bb97c91a61450d917a88d4388 *testprof/test.key
blake2s-256: 92ac6dc88b9ce4a75b2a54783435a37919303f713396db0e9ccd976d8c55f1cf *testprof/test.key
chc_hash: d50da1ea9b53c0b891c8bcb0a4c6e5e0 *testprof/test.key
md2: 0b327a17a102580ccde8081c18f137c7 *testprof/test.key
md4: 6df7b3c1f7fbcbf6b8246dc0046e9d6b *testprof/test.key
md5: 48fd9ed11c633069c3cf721611696a73 *testprof/test.key
rmd128: a78170e11d56605eb0f5e05e6a218e61 *testprof/test.key
rmd160: 56236b14cd15958c58136a295fa2b07c5edee009 *testprof/test.key
rmd256: a0a658b25b8841f20be8151eb5e2c1229f085dd5cf7aa8bef4c8e07ea5738913 *testprof/test.key
rmd320: 2ec15ca947f29dd8cef4782aeaeb4541ac00dc29e1649c4f5f4578ef0287091e513f9da423ce7319 *testprof/test.key
sha1: c17a11eef02d7a2ad7250bc2ee22bdf37ed21f0f *testprof/test.key
sha224: b226daa069512c849d55866470c23543d87e1fb768171efdcc6ab59a *testprof/test.key
sha256: 76ec7faebdc42a4de35ca70024c2d273e9f7856ca61612e89f5f66350ba8cf5f *testprof/test.key
sha3-224: 09525d6410c462e00e72ccd4304290bc59ff2b1b8e5322dbc197e169 *testprof/test.key
sha3-256: b9016fc9976545ad0183775d88f20df5295d40b31db00d85f6916333260b8ae2 *testprof/test.key
sha3-384: 5452977763b602df4185bbe7cce193346b09ee20d4a45a780fd44237e335ceaa71b9da06242621e159d796db9043c9be *testprof/test.key
sha3-512: 1fadf63073c8613d38d5c944e2e3f7e086d94d93dceb2eec1e9693fd44158f3127c90cbaa8a13035cc9ff9b3572c9dce836bea15c92e8a78b036ec382caceb0f *testprof/test.key
sha384: 7cc080c662524617e56d5a49f1c185909e9b1858a86684161ddd97fc5426f693b53f59d779de16da25dcb1259df879e5 *testprof/test.key
sha512: f90afe4d254716a9770fd2c4f29ca544f2975f961cbd7fa207117383e4e48f7a2e1ade4aac333a2cb8c227dd6af2fff4d87c31c2a3916ae24d507c7c94c21818 *testprof/test.key
sha512-224: 64ec566b5c2de736eab1b4e8542dc110f736819ea7f8f48e3712fbad *testprof/test.key
sha512-256: db880bce4beb7246510febb961c7595aab57a15de6f90cd079f145e476b5d773 *testprof/test.key
tiger: 97d713850e7affac30a642572c1ee7b18793d5b9e0ef5932 *testprof/test.key
whirlpool: d7d41c755e0f28313f254cb198e0bfa42f56670595d97b80dceec754825d69938a9c11e5bf16e9a3809a62a09bddf021f3dbff4302ceec7ba46c88b41772b711 *testprof/test.key

View File

@ -52,14 +52,15 @@ int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outle
goto LBL_ERR;
}
*outlen = hash_descriptor[hash].hashsize;
do {
x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
if ((err = hash_descriptor[hash].process(&md, buf, (unsigned long)x)) != CRYPT_OK) {
goto LBL_CLEANBUF;
}
} while (x == LTC_FILE_READ_BUFSIZE);
err = hash_descriptor[hash].done(&md, out);
if ((err = hash_descriptor[hash].done(&md, out)) == CRYPT_OK) {
*outlen = hash_descriptor[hash].hashsize;
}
LBL_CLEANBUF:
zeromem(buf, LTC_FILE_READ_BUFSIZE);