summaryrefslogtreecommitdiff
path: root/genisoimage/checksum.c
blob: 704167ce1a5d81c0e71df1250e16cc4ab6e5765d (plain)
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
/*
 * checksum.c
 *
 * Copyright (c) 2008- Steve McIntyre <steve@einval.com>
 *
 * Implementation of a generic checksum interface, used in JTE.
 *
 * GNU GPL v2
 */

#include <mconfig.h>
#include "genisoimage.h"
#include <timedefs.h>
#include <fctldefs.h>
#include <regex.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#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_process_bytes(buf, len, context);
}
static void sha1_final(unsigned char *digest, void *context)
{
    sha1_finish_ctx(context, digest);
}

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);
};

static const struct checksum_details algorithms[] = 
{
    {
        "MD5",
        "md5sum",
        16,
        sizeof(struct mk_MD5Context),
        md5_init,
        md5_update,
        md5_final
    },
    {
        "SHA1",
        "sha1sum",
        20,
        sizeof(struct sha1_ctx),
        sha1_init,
        sha1_update,
        sha1_final
    },
    {
        "SHA256",
        "sha256sum",
        32,
        sizeof(struct sha256_ctx),
        sha256_init,
        sha256_update,
        sha256_final
    },
    {
        "SHA512",
        "sha512sum",
        64,
        sizeof(struct sha512_ctx),
        sha512_init,
        sha512_update,
        sha512_final
    }
};

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(0);
}
#endif

checksum_context_t *checksum_init_context(int checksums, const char *owner)
{
    int i = 0;
    int ret = 0;
    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)
            {
                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;
    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
    void *thread_ret;
    /* Clean up the threads */
    c->threads_running = c->threads_desired;    

    for (i = 0; i < NUM_CHECKSUMS; i++)
    {
        if (c->algo[i].enabled)
        {
            void *ret = 0;
            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
        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 error = 0;
    int i = 0;
    char *start_ptr = arg;
    int len = 0;

    *algo = 0;

    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 |= (1 << i);
                }
            }
        
            if (!match)
            {
                fprintf(stderr, "invalid algorithm name found in %s\n", arg);
                return EINVAL;
            }
        }
        
        if (start_ptr[len] == 0)
            break;
            
        start_ptr += len + 1;
    }
    
    if (! (*algo & CHECK_MD5_USED))
    {
        fprintf(stderr, "invalid choices: algorithms *must* include MD5\n");
        return EINVAL;
    }
    
    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");
        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 */