1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
/*
* checksum.c
*
* Copyright (c) 2008-2019 Steve McIntyre <steve@einval.com>
*
* Implementation of a generic checksum interface, used in JTE.
*
* GNU GPL v2+
*/
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include <sys/types.h>
#include <regex.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#else
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#endif
#include "md5.h"
#include "sha1.h"
#include "sha256.h"
#include "sha512.h"
#include "checksum.h"
#ifdef THREADED_CHECKSUMS
#include <pthread.h>
#endif
static void md5_init(void *context)
{
mk_MD5Init(context);
}
static void md5_update(void *context, unsigned char const *buf, unsigned int len)
{
mk_MD5Update(context, buf, len);
}
static void md5_final(unsigned char *digest, void *context)
{
mk_MD5Final(digest, context);
}
static void sha1_init(void *context)
{
sha1_init_ctx(context);
}
static void sha1_update(void *context, unsigned char const *buf, unsigned int len)
{
sha1_write(context, buf, len);
}
static void sha1_final(unsigned char *digest, void *context)
{
sha1_finish_ctx(context);
memcpy(digest, sha1_read(context), 20);
}
static void sha256_init(void *context)
{
sha256_init_ctx(context);
}
static void sha256_update(void *context, unsigned char const *buf, unsigned int len)
{
sha256_process_bytes(buf, len, context);
}
static void sha256_final(unsigned char *digest, void *context)
{
sha256_finish_ctx(context, digest);
}
static void sha512_init(void *context)
{
sha512_init_ctx(context);
}
static void sha512_update(void *context, unsigned char const *buf, unsigned int len)
{
sha512_process_bytes(buf, len, context);
}
static void sha512_final(unsigned char *digest, void *context)
{
sha512_finish_ctx(context, digest);
}
struct checksum_details
{
char *name;
char *prog;
int digest_size;
int context_size;
void (*init)(void *context);
void (*update)(void *context, unsigned char const *buf, unsigned int len);
void (*final)(unsigned char *digest, void *context);
int check_used_value;
};
static const struct checksum_details algorithms[] =
{
{
"MD5",
"md5sum",
16,
sizeof(struct mk_MD5Context),
md5_init,
md5_update,
md5_final,
CHECK_MD5_USED
},
{
"SHA1",
"sha1sum",
20,
sizeof(SHA1_CONTEXT),
sha1_init,
sha1_update,
sha1_final,
CHECK_SHA1_USED
},
{
"SHA256",
"sha256sum",
32,
sizeof(struct sha256_ctx),
sha256_init,
sha256_update,
sha256_final,
CHECK_SHA256_USED
},
{
"SHA512",
"sha512sum",
64,
sizeof(struct sha512_ctx),
sha512_init,
sha512_update,
sha512_final,
CHECK_SHA512_USED
}
};
struct algo_context
{
void *context;
unsigned char *digest;
int enabled;
int finalised;
char *hexdump;
#ifdef THREADED_CHECKSUMS
unsigned char const *buf;
unsigned int len;
int which;
pthread_t thread;
struct _checksum_context *parent;
pthread_mutex_t start_mutex;
pthread_cond_t start_cv;
#endif
};
struct _checksum_context
{
#ifdef THREADED_CHECKSUMS
unsigned int index;
unsigned int threads_running;
unsigned int threads_desired;
pthread_mutex_t done_mutex;
pthread_cond_t done_cv;
#endif
char *owner;
struct algo_context algo[NUM_CHECKSUMS];
};
struct checksum_info *checksum_information(enum checksum_types which)
{
return (struct checksum_info *)&algorithms[which];
}
/* Dump a buffer in hex */
static void hex_dump_to_buffer(char *output_buffer, unsigned char *buf, size_t buf_size)
{
unsigned int i;
char *p = output_buffer;
memset(output_buffer, 0, 1 + (2*buf_size));
for (i = 0; i < buf_size ; i++)
p += sprintf(p, "%2.2x", buf[i]);
}
#ifdef THREADED_CHECKSUMS
static void *checksum_thread(void *arg)
{
struct algo_context *a = arg;
struct _checksum_context *c = a->parent;
int num_blocks_summed = 0;
while (1)
{
/* wait to be given some work to do */
pthread_mutex_lock(&a->start_mutex);
while (a->buf == NULL)
{
pthread_cond_wait(&a->start_cv, &a->start_mutex);
}
pthread_mutex_unlock(&a->start_mutex);
/* if we're given a zero-length buffer, then that means we're
* done */
if (a->len == 0)
break;
/* actually do the checksum on the supplied buffer */
algorithms[a->which].update(a->context, a->buf, a->len);
num_blocks_summed++;
a->buf = NULL;
/* and tell the main thread that we're done with that
* buffer */
pthread_mutex_lock(&c->done_mutex);
c->threads_running--;
if (c->threads_running == 0)
pthread_cond_signal(&c->done_cv);
pthread_mutex_unlock(&c->done_mutex);
}
pthread_exit(NULL);
}
#endif
checksum_context_t *checksum_init_context(int checksums, const char *owner)
{
int i = 0;
#ifdef THREADED_CHECKSUMS
int ret = 0;
#endif
struct _checksum_context *context = calloc(1, sizeof(struct _checksum_context));
if (!context)
return NULL;
context->owner = strdup(owner);
if (!context->owner)
{
free(context);
return NULL;
}
#ifdef THREADED_CHECKSUMS
pthread_mutex_init(&context->done_mutex, NULL);
pthread_cond_init(&context->done_cv, NULL);
context->index = 0;
context->threads_running = 0;
context->threads_desired = 0;
for (i = 0; i < NUM_CHECKSUMS; i++)
if ( (1 << i) & checksums)
context->threads_desired++;
#endif
for (i = 0; i < NUM_CHECKSUMS; i++)
{
struct algo_context *a = &context->algo[i];
if ( (1 << i) & checksums)
{
a->context = malloc(algorithms[i].context_size);
if (!a->context)
{
checksum_free_context(context);
return NULL;
}
a->digest = malloc(algorithms[i].digest_size);
if (!a->digest)
{
checksum_free_context(context);
return NULL;
}
a->hexdump = malloc(1 + (2*algorithms[i].digest_size));
if (!a->hexdump)
{
checksum_free_context(context);
return NULL;
}
algorithms[i].init(a->context);
a->enabled = 1;
a->finalised = 0;
#ifdef THREADED_CHECKSUMS
a->which = i;
a->parent = context;
a->buf = NULL;
a->len = 0;
pthread_mutex_init(&a->start_mutex, NULL);
pthread_cond_init(&a->start_cv, NULL);
ret = pthread_create(&a->thread, NULL, checksum_thread, a);
if (ret != 0)
{
/* libjte issues an own message:
fprintf(stderr, "failed to create new thread: %d\n", ret);
*/
checksum_free_context(context);
return NULL;
}
#endif
}
else
a->enabled = 0;
}
return context;
}
void checksum_free_context(checksum_context_t *context)
{
int i = 0;
struct _checksum_context *c = context;
for (i = 0; i < NUM_CHECKSUMS; i++)
{
struct algo_context *a = &c->algo[i];
#ifdef THREADED_CHECKSUMS
if (a->thread)
{
void *ret;
pthread_cancel(a->thread);
pthread_join(a->thread, &ret);
a->thread = 0;
}
#endif
free(a->context);
free(a->digest);
free(a->hexdump);
}
free(c->owner);
free(c);
}
#ifdef THREADED_CHECKSUMS
void checksum_update(checksum_context_t *context,
unsigned char const *buf, unsigned int len)
{
int i = 0;
struct _checksum_context *c = context;
/* >>> TODO : Find out for what purpose index shall serve.
It is defined here and incremented. Not more.
*/
static int index = 0;
index++;
c->threads_running = c->threads_desired;
for (i = 0; i < NUM_CHECKSUMS; i++)
{
if (c->algo[i].enabled)
{
struct algo_context *a = &c->algo[i];
pthread_mutex_lock(&a->start_mutex);
a->len = len;
a->buf = buf;
pthread_cond_signal(&a->start_cv);
pthread_mutex_unlock(&a->start_mutex);
}
}
/* Should now all be running, wait on them all to return */
pthread_mutex_lock(&c->done_mutex);
while (c->threads_running > 0)
{
pthread_cond_wait(&c->done_cv, &c->done_mutex);
}
pthread_mutex_unlock(&c->done_mutex);
}
#else /* THREADED_CHECKSUMS */
void checksum_update(checksum_context_t *context,
unsigned char const *buf, unsigned int len)
{
int i = 0;
struct _checksum_context *c = context;
for (i = 0; i < NUM_CHECKSUMS; i++)
{
if (c->algo[i].enabled)
{
struct algo_context *a = &c->algo[i];
algorithms[i].update(a->context, buf, len);
}
}
}
#endif /* THREADED_CHECKSUMS */
void checksum_final(checksum_context_t *context)
{
int i = 0;
struct _checksum_context *c = context;
#ifdef THREADED_CHECKSUMS
/* Clean up the threads */
c->threads_running = c->threads_desired;
for (i = 0; i < NUM_CHECKSUMS; i++)
{
if (c->algo[i].enabled)
{
void *ret = NULL;
struct algo_context *a = &c->algo[i];
pthread_mutex_lock(&a->start_mutex);
a->len = 0;
a->buf = (unsigned char *)-1;
pthread_cond_signal(&a->start_cv);
pthread_mutex_unlock(&a->start_mutex);
pthread_join(a->thread, &ret);
a->thread = 0;
}
}
#endif
for (i = 0; i < NUM_CHECKSUMS; i++)
{
struct algo_context *a = &c->algo[i];
if (a->enabled)
{
algorithms[i].final(a->digest, a->context);
hex_dump_to_buffer(a->hexdump, a->digest, algorithms[i].digest_size);
a->finalised = 1;
}
}
}
void checksum_copy(checksum_context_t *context,
enum checksum_types which,
unsigned char *digest)
{
struct _checksum_context *c = context;
if (c->algo[which].enabled)
{
if (c->algo[which].finalised)
memcpy(digest, c->algo[which].digest, algorithms[which].digest_size);
else
memset(digest, 0, algorithms[which].digest_size);
}
else
/* >>> TODO : ??? Can this happen ? Why print and then go on ? */
fprintf(stderr, "Asked for %s checksum, not enabled!\n",
algorithms[which].name);
}
const char *checksum_hex(checksum_context_t *context,
enum checksum_types which)
{
struct _checksum_context *c = context;
if (c->algo[which].enabled && c->algo[which].finalised)
return c->algo[which].hexdump;
/* else */
return NULL;
}
/* Parse the command line options for which checksums to use */
int parse_checksum_algo(char *arg, int *algo)
{
int i = 0;
char *start_ptr = arg;
int len = 0;
(*algo) |= CHECK_MD5_USED;
if (!strcasecmp(arg, "all"))
{
*algo = 0xFF;
return 0;
}
while (*start_ptr != 0)
{
int match = 0;
len = 0;
while (start_ptr[len] != ',' && start_ptr[len] != 0)
len++;
if (len)
{
for (i = 0; i < NUM_CHECKSUMS; i++)
{
if (len == strlen(algorithms[i].name) &&
!strncasecmp(start_ptr, algorithms[i].name, len))
{
match = 1;
*algo |= algorithms[i].check_used_value;
}
}
if (!match)
{
return EINVAL;
}
}
if (start_ptr[len] == 0)
break;
start_ptr += len + 1;
}
return 0;
}
/* Helper function: Simply calculate the checksum of the first "size"
* bytes of a file using the specified algorithm. If size == -1,
* calculate the checksum for the whole of the file. The caller is
* responsible for passing in a large enough buffer as "digest", based
* on their choice of algorithm. */
int checksum_calculate(char *filename,
int64_t size,
unsigned char *out,
enum checksum_types which)
{
char buffer[32768];
FILE *infile = NULL;
int64_t remain = 0;
int use;
struct checksum_context_t *context;
context = checksum_init_context(1 << which, "misc");
if (!context)
{
errno = ENOMEM;
return -1;
}
infile = fopen(filename, "rb");
if (!infile)
return -1;
if (-1 == size)
{
struct stat st;
stat(filename, &st);
size = st.st_size;
}
remain = size;
while (remain > 0)
{
use = (remain > sizeof(buffer) ? sizeof(buffer) : remain);
if (fread(buffer, 1, use, infile) == 0)
return -1;
/* Update the checksum */
checksum_update(context, (unsigned char *)buffer, use);
remain -= use;
}
fclose(infile);
checksum_final(context);
checksum_copy(context, which, out);
checksum_free_context(context);
return 0;
}
/* Read in a hex-dumped checksum and parse it */
int checksum_parse_hex(char *in, unsigned char *out, int size)
{
int i = 0;
if (size % 2) /* odd number */
return EINVAL;
for (i = 0; i < size / 2; i++)
{
if (in[2*i] >= '0' && in[2*i] <= '9')
in[2*i] -= '0';
else if (in[2*i] >= 'A' && in[2*i] <= 'F')
in[2*i] += 10 - 'A';
else if (in[2*i] >= 'a' && in[2*i] <= 'f')
in[2*i] += 10 - 'a';
else
return 1;
if (in[1+(2*i)] >= '0' && in[1+(2*i)] <= '9')
in[1+(2*i)] -= '0';
else if (in[1+(2*i)] >= 'A' && in[1+(2*i)] <= 'F')
in[1+(2*i)] += 10 - 'A';
else if (in[1+(2*i)] >= 'a' && in[1+(2*i)] <= 'f')
in[1+(2*i)] += 10 - 'a';
else
return 1;
out[i] = in[2*i] << 4 | in[1+(2*i)];
}
return 0;
}
#ifdef CHECKSUM_SELF_TEST
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char buf[1024];
int fd = -1;
char *filename;
int err = 0;
static checksum_context_t *test_context = NULL;
int i = 0;
if (argc != 2)
{
fprintf(stderr, "Need a filename to act on!\n");
return 1;
}
filename = argv[1];
fd = open(filename, O_RDONLY);
if (fd < 0)
{
fprintf(stderr, "Unable to open file %s, errno %d\n", filename, errno);
return 1;
}
test_context = checksum_init_context(CHECK_ALL_USED, "test");
if (!test_context)
{
fprintf(stderr, "Unable to initialise checksum context\n");
close(fd);
return 1;
}
while(1)
{
err = read(fd, buf, sizeof(buf));
if (err < 0)
{
fprintf(stderr, "Failed to read from file, errno %d\n", errno);
return 1;
}
if (err == 0)
break; /* EOF */
/* else */
checksum_update(test_context, buf, err);
}
close(fd);
checksum_final(test_context);
for (i = 0; i < NUM_CHECKSUMS; i++)
{
struct checksum_info *info;
unsigned char r[64];
int j = 0;
info = checksum_information(i);
memset(r, 0, sizeof(r));
checksum_copy(test_context, i, r);
printf("OUR %s:\n", info->name);
for (j = 0; j < info->digest_size; j++)
printf("%2.2x", r[j]);
printf(" %s\n", filename);
printf("system checksum program (%s):\n", info->prog);
sprintf(buf, "%s %s", info->prog, filename);
system(buf);
printf("\n");
}
return 0;
}
#endif /* CHECKSUM_SELF_TEST */
|