| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <stdint.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include <unistd.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <sys/types.h> |
| 34 |
#include <sys/stat.h> |
| 35 |
#include <fcntl.h> |
| 36 |
#include <crapi/crapi.h> |
| 37 |
#include <crapi/digest.h> |
| 38 |
|
| 39 |
static int mem2hex (uint8_t *mem, size_t mlen, char *str, size_t slen) |
| 40 |
{ |
| 41 |
const char ch[] = "0123456789abcdef"; |
| 42 |
register size_t i; |
| 43 |
|
| 44 |
if (str == NULL) { |
| 45 |
return -1; |
| 46 |
} |
| 47 |
|
| 48 |
if (slen < (mlen * 2) + 1) { |
| 49 |
errno = ENOBUFS; |
| 50 |
return (-1); |
| 51 |
} |
| 52 |
|
| 53 |
for (i = 0; i < mlen; ++i) { |
| 54 |
str[i*2 ] = ch[(mem[i] & 0xf0) >> 4]; |
| 55 |
str[i*2+1] = ch[(mem[i] & 0x0f)]; |
| 56 |
} |
| 57 |
|
| 58 |
str[i*2] = '\0'; |
| 59 |
|
| 60 |
return (0); |
| 61 |
} |
| 62 |
|
| 63 |
int main (int argc, char *argv[]) |
| 64 |
{ |
| 65 |
if (argc != 4) { |
| 66 |
fprintf (stderr, "Usage: %s <file> <checksum> <algorithm>\n", argv[0]); |
| 67 |
return (1); |
| 68 |
} |
| 69 |
|
| 70 |
char *filename = argv[1]; |
| 71 |
char *orig_sum = argv[2]; |
| 72 |
char *algorithm_str = argv[3]; |
| 73 |
|
| 74 |
crapi_alg_t algorithm; |
| 75 |
size_t dstlen = 0; |
| 76 |
|
| 77 |
if (!strcmp(algorithm_str, "md5")) { |
| 78 |
#ifdef OPENSCAP_ENABLE_MD5 |
| 79 |
algorithm = CRAPI_DIGEST_MD5; |
| 80 |
dstlen = 16; |
| 81 |
#else |
| 82 |
return 1; |
| 83 |
#endif |
| 84 |
} else if (!strcmp(algorithm_str, "sha1")) { |
| 85 |
#ifdef OPENSCAP_ENABLE_SHA1 |
| 86 |
algorithm = CRAPI_DIGEST_SHA1; |
| 87 |
dstlen = 20; |
| 88 |
#else |
| 89 |
return 1; |
| 90 |
#endif |
| 91 |
} else if (!strcmp(algorithm_str, "sha256")) { |
| 92 |
algorithm = CRAPI_DIGEST_SHA256; |
| 93 |
dstlen = 32; |
| 94 |
} else { |
| 95 |
return 1; |
| 96 |
} |
| 97 |
|
| 98 |
int fd = open(filename, O_RDONLY); |
| 99 |
|
| 100 |
if (fd < 0) { |
| 101 |
perror ("open"); |
| 102 |
return (2); |
| 103 |
} |
| 104 |
|
| 105 |
if (crapi_init (NULL) != 0) { |
| 106 |
fprintf (stderr, "crapi_init() != 0\n"); |
| 107 |
abort (); |
| 108 |
} |
| 109 |
|
| 110 |
uint8_t *dst = malloc(dstlen); |
| 111 |
|
| 112 |
size_t comp_sum_len = (dstlen * 2) + 1; |
| 113 |
char *comp_sum = malloc(comp_sum_len); |
| 114 |
|
| 115 |
if (crapi_digest_fd(fd, algorithm, dst, &dstlen) != 0) { |
| 116 |
fprintf (stderr, "crapi_digest() != 0\n"); |
| 117 |
abort (); |
| 118 |
} |
| 119 |
|
| 120 |
mem2hex (dst, dstlen, comp_sum, comp_sum_len); |
| 121 |
|
| 122 |
if (strcmp(orig_sum, comp_sum) != 0) { |
| 123 |
fprintf (stderr, "crapi_digest::%s(%s) != %s (== %s)\n", algorithm_str, filename, orig_sum, comp_sum); |
| 124 |
abort (); |
| 125 |
} |
| 126 |
free(dst); |
| 127 |
free(comp_sum); |
| 128 |
|
| 129 |
close (fd); |
| 130 |
|
| 131 |
return (0); |
| 132 |
} |