| 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 |
#define _GNU_SOURCE |
| 46 |
#include <stdio.h> |
| 47 |
#include <stdlib.h> |
| 48 |
#include <stdint.h> |
| 49 |
#include <string.h> |
| 50 |
#include <unistd.h> |
| 51 |
#include <sys/mman.h> |
| 52 |
#include <sys/types.h> |
| 53 |
#include <sys/stat.h> |
| 54 |
#include <fcntl.h> |
| 55 |
#include <getopt.h> |
| 56 |
#include <errno.h> |
| 57 |
#include <ctype.h> |
| 58 |
#include <dlfcn.h> |
| 59 |
#include <libgen.h> |
| 60 |
#include <limits.h> |
| 61 |
#include <inttypes.h> |
| 62 |
|
| 63 |
#include <kcapi.h> |
| 64 |
|
| 65 |
#include "app-internal.h" |
| 66 |
|
| 67 |
#define GCC_VERSION (__GNUC__ * 10000 \ |
| 68 |
+ __GNUC_MINOR__ * 100 \ |
| 69 |
+ __GNUC_PATCHLEVEL__) |
| 70 |
|
| 71 |
struct hash_name { |
| 72 |
const char *kcapiname; |
| 73 |
const char *bsdname; |
| 74 |
}; |
| 75 |
|
| 76 |
struct hash_key { |
| 77 |
const char *checkdir; |
| 78 |
const uint8_t *data; |
| 79 |
off_t len; |
| 80 |
}; |
| 81 |
|
| 82 |
struct hash_params { |
| 83 |
struct hash_name name; |
| 84 |
struct hash_key key; |
| 85 |
uint32_t hashlen; |
| 86 |
int bsd_style; |
| 87 |
uint32_t newline; |
| 88 |
}; |
| 89 |
|
| 90 |
static const struct hash_name NAMES_MD5[2] = { |
| 91 |
{ "md5", "MD5" }, { "hmac(md5)", "HMAC(MD5)" } |
| 92 |
}; |
| 93 |
static const struct hash_name NAMES_SHA1[2] = { |
| 94 |
{ "sha1", "SHA1" }, { "hmac(sha1)", "HMAC(SHA1)" } |
| 95 |
}; |
| 96 |
static const struct hash_name NAMES_SHA224[2] = { |
| 97 |
{ "sha224", "SHA224" }, { "hmac(sha224)", "HMAC(SHA224)" } |
| 98 |
}; |
| 99 |
static const struct hash_name NAMES_SHA256[2] = { |
| 100 |
{ "sha256", "SHA256" }, { "hmac(sha256)", "HMAC(SHA256)" } |
| 101 |
}; |
| 102 |
static const struct hash_name NAMES_SHA384[2] = { |
| 103 |
{ "sha384", "SHA384" }, { "hmac(sha384)", "HMAC(SHA384)" } |
| 104 |
}; |
| 105 |
static const struct hash_name NAMES_SHA512[2] = { |
| 106 |
{ "sha512", "SHA512" }, { "hmac(sha512)", "HMAC(SHA512)" } |
| 107 |
}; |
| 108 |
static const struct hash_name NAMES_SM3[2] = { |
| 109 |
{ "sm3", "SM3" }, { "hmac(sm3)", "HMAC(SM3)" } |
| 110 |
}; |
| 111 |
|
| 112 |
static const char fipscheck_hmackey[] = "orboDeJITITejsirpADONivirpUkvarP"; |
| 113 |
static const char hmaccalc_hmackey[] = "FIPS-FTW-RHT2009"; |
| 114 |
|
| 115 |
static const struct hash_key KEY_FIPSCHECK = { |
| 116 |
.data = (const uint8_t *)fipscheck_hmackey, |
| 117 |
.len = sizeof(fipscheck_hmackey) - 1, |
| 118 |
#ifdef CHECK_DIR |
| 119 |
.checkdir = CHECK_DIR"/fipscheck", |
| 120 |
#else |
| 121 |
.checkdir = NULL, |
| 122 |
#endif |
| 123 |
}; |
| 124 |
static const struct hash_key KEY_HMACCALC = { |
| 125 |
.data = (const uint8_t *)hmaccalc_hmackey, |
| 126 |
.len = sizeof(hmaccalc_hmackey) - 1, |
| 127 |
#ifdef CHECK_DIR |
| 128 |
.checkdir = CHECK_DIR"/hmaccalc", |
| 129 |
#else |
| 130 |
.checkdir = NULL, |
| 131 |
#endif |
| 132 |
}; |
| 133 |
|
| 134 |
static void usage(char *name, int fipscheck) |
| 135 |
{ |
| 136 |
const char *base = basename(name); |
| 137 |
fprintf(stderr, "\n%s - calculation of hash sum (Using Linux Kernel Crypto API)\n", basename(name)); |
| 138 |
fprintf(stderr, "\nUsage:\n"); |
| 139 |
fprintf(stderr, "\t%s [-n BASENAME] [OPTION]... -S|-L\n", base); |
| 140 |
if (fipscheck) |
| 141 |
fprintf(stderr, "\t%s [-n BASENAME] [OPTION]... FILE\n", base); |
| 142 |
else { |
| 143 |
fprintf(stderr, "\t%s [-n BASENAME] [OPTION]... -c FILE\n", base); |
| 144 |
fprintf(stderr, "\t%s [-n BASENAME] [OPTION]... FILE...\n", base); |
| 145 |
} |
| 146 |
fprintf(stderr, "\nOptions:\n"); |
| 147 |
fprintf(stderr, "\t-n --name\t\tForce given application name (sha512hmac/...)\n"); |
| 148 |
fprintf(stderr, "\t-S --self-sum\t\tPrint checksum of this binary and exit\n"); |
| 149 |
fprintf(stderr, "\t-L --self-sum-lib\tPrint checksum of the libkcapi library and exit\n"); |
| 150 |
if (!fipscheck) |
| 151 |
fprintf(stderr, "\t-c --check FILE\t\tVerify hash sums from file\n"); |
| 152 |
fprintf(stderr, "\t-u --unkeyed\t\tForce unkeyed hash\n"); |
| 153 |
fprintf(stderr, "\t-h --hash HASH\t\tUse given hash algorithm\n"); |
| 154 |
fprintf(stderr, "\t-t --truncate N\t\tUse hash truncated to N bits\n"); |
| 155 |
fprintf(stderr, "\t-q --status\t\tSuppress verification output\n"); |
| 156 |
fprintf(stderr, "\t --quiet\t\tSuppress only success messages\n"); |
| 157 |
fprintf(stderr, "\t-k --key-file FILE\tUse HMAC key from given file\n"); |
| 158 |
fprintf(stderr, "\t-K --key KEY\t\tUse KEY as the HMAC key\n"); |
| 159 |
fprintf(stderr, "\t --tag\t\tCreate a BSD-style checksum\n"); |
| 160 |
fprintf(stderr, "\t-d\t\t\tCheck directory for fipshmac; otherwise ignored\n"); |
| 161 |
fprintf(stderr, "\t-b, -P\t\t\tCompatibility hmaccalc options; ignored\n"); |
| 162 |
fprintf(stderr, "\t-z\t\t\tNUL line termination\n"); |
| 163 |
fprintf(stderr, "\t --help\t\tPrint this help text\n"); |
| 164 |
fprintf(stderr, "\t-v --version\t\tShow version\n"); |
| 165 |
} |
| 166 |
|
| 167 |
static void version(char *name) |
| 168 |
{ |
| 169 |
char version[20]; |
| 170 |
|
| 171 |
memset(version, 0, 20); |
| 172 |
kcapi_versionstring(version, 20); |
| 173 |
|
| 174 |
fprintf(stderr, "%s: %s\n", basename(name), version); |
| 175 |
} |
| 176 |
|
| 177 |
static int mmap_file(const char *filename, uint8_t **memory, off_t *size, |
| 178 |
size_t *mapped, off_t offset) |
| 179 |
{ |
| 180 |
int fd = -1; |
| 181 |
int ret = 0; |
| 182 |
struct stat sb; |
| 183 |
|
| 184 |
fd = open(filename, O_RDONLY | O_CLOEXEC); |
| 185 |
if (fd < 0) { |
| 186 |
fprintf(stderr, "Cannot open file %s: %s\n", filename, |
| 187 |
strerror(errno)); |
| 188 |
return -EIO; |
| 189 |
} |
| 190 |
|
| 191 |
if (*size) { |
| 192 |
if ((*size - offset) < (off_t)*mapped ) |
| 193 |
*mapped = (size_t)(*size - offset); |
| 194 |
} else { |
| 195 |
ret = check_filetype(fd, &sb, filename); |
| 196 |
if (ret) |
| 197 |
goto out; |
| 198 |
*size = sb.st_size; |
| 199 |
if (*size <= (off_t)*mapped) { |
| 200 |
*mapped = (size_t)*size; |
| 201 |
if (*size == 0) |
| 202 |
goto out; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
*memory = mmap(NULL, *mapped, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, |
| 207 |
offset); |
| 208 |
if (*memory == MAP_FAILED) { |
| 209 |
*memory = NULL; |
| 210 |
ret = -errno; |
| 211 |
goto out; |
| 212 |
} |
| 213 |
madvise(*memory, *mapped, MADV_SEQUENTIAL | MADV_WILLNEED); |
| 214 |
|
| 215 |
out: |
| 216 |
close(fd); |
| 217 |
return ret; |
| 218 |
} |
| 219 |
|
| 220 |
static int load_file(const char *filename, uint8_t **memory, off_t *size) |
| 221 |
{ |
| 222 |
int fd = -1; |
| 223 |
int ret = 0; |
| 224 |
uint8_t *buffer = NULL; |
| 225 |
off_t buffer_size = 4096; |
| 226 |
off_t offset = 0; |
| 227 |
ssize_t rdbytes; |
| 228 |
|
| 229 |
fd = open(filename, O_RDONLY | O_CLOEXEC); |
| 230 |
if (fd < 0) { |
| 231 |
fprintf(stderr, "Cannot open file %s: %s\n", filename, |
| 232 |
strerror(errno)); |
| 233 |
return -EIO; |
| 234 |
} |
| 235 |
|
| 236 |
buffer = malloc(TMPBUFLEN); |
| 237 |
if (buffer == NULL) { |
| 238 |
fprintf(stderr, "Key memory allocation failed\n"); |
| 239 |
ret = -ENOMEM; |
| 240 |
goto out; |
| 241 |
} |
| 242 |
|
| 243 |
while ((rdbytes = read(fd, buffer + offset, (size_t)(buffer_size - offset))) != 0) { |
| 244 |
if (rdbytes < 0) { |
| 245 |
fprintf(stderr, "Error reading file %s: %s\n", filename, |
| 246 |
strerror(errno)); |
| 247 |
ret = -EIO; |
| 248 |
goto out; |
| 249 |
} |
| 250 |
|
| 251 |
offset += (off_t)rdbytes; |
| 252 |
if (offset == buffer_size) { |
| 253 |
uint8_t *new_buffer; |
| 254 |
|
| 255 |
if (buffer_size == UINT32_MAX) { |
| 256 |
fprintf(stderr, "Key longer than UINT32_MAX\n"); |
| 257 |
ret = -ERANGE; |
| 258 |
goto out; |
| 259 |
} else if (buffer_size * 2 < buffer_size) |
| 260 |
buffer_size = UINT32_MAX; |
| 261 |
else |
| 262 |
buffer_size *= 2; |
| 263 |
|
| 264 |
new_buffer = realloc(buffer, (size_t)buffer_size); |
| 265 |
if (new_buffer == NULL) { |
| 266 |
fprintf(stderr, "Key memory allocation failed\n"); |
| 267 |
ret = -ENOMEM; |
| 268 |
goto out; |
| 269 |
} |
| 270 |
|
| 271 |
buffer = new_buffer; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
*memory = buffer; |
| 276 |
*size = offset; |
| 277 |
|
| 278 |
close(fd); |
| 279 |
return 0; |
| 280 |
|
| 281 |
out: |
| 282 |
if (buffer) |
| 283 |
free(buffer); |
| 284 |
close(fd); |
| 285 |
return ret; |
| 286 |
} |
| 287 |
|
| 288 |
static int hasher(struct kcapi_handle *handle, const struct hash_params *params, |
| 289 |
const char *filename, const char *comphash, uint32_t comphashlen, |
| 290 |
FILE *outfile) |
| 291 |
{ |
| 292 |
|
| 293 |
size_t mapped = 16<<20; |
| 294 |
off_t offset = 0, size = 0; |
| 295 |
uint32_t hashlen = params->hashlen; |
| 296 |
ssize_t ret = 0; |
| 297 |
uint8_t *memblock = NULL; |
| 298 |
uint8_t *memblock_p; |
| 299 |
uint8_t md[64]; |
| 300 |
|
| 301 |
if (filename) { |
| 302 |
do { |
| 303 |
ret = mmap_file(filename, &memblock, &size, &mapped, |
| 304 |
offset); |
| 305 |
if (ret) { |
| 306 |
fprintf(stderr, |
| 307 |
"Use of mmap failed mapping %zu bytes at offset %" PRId64 " of file %s (%zd)\n", |
| 308 |
mapped, (int64_t)offset, filename, ret); |
| 309 |
return (int)ret; |
| 310 |
} |
| 311 |
|
| 312 |
memblock_p = memblock; |
| 313 |
size_t left = mapped; |
| 314 |
do { |
| 315 |
uint32_t todo = (left > INT_MAX) ? |
| 316 |
INT_MAX : (uint32_t)left; |
| 317 |
|
| 318 |
ret = kcapi_md_update(handle, memblock_p, todo); |
| 319 |
if (ret < 0) { |
| 320 |
munmap(memblock, mapped); |
| 321 |
return (int)ret; |
| 322 |
} |
| 323 |
left -= todo; |
| 324 |
memblock_p += todo; |
| 325 |
} while (left); |
| 326 |
munmap(memblock, mapped); |
| 327 |
offset = offset + (off_t)mapped; |
| 328 |
} while (offset ^ size); |
| 329 |
} else { |
| 330 |
uint8_t tmpbuf[TMPBUFLEN] __aligned(KCAPI_APP_ALIGN); |
| 331 |
uint32_t bufsize; |
| 332 |
|
| 333 |
while ((bufsize = |
| 334 |
(uint32_t)fread(tmpbuf, sizeof(uint8_t), TMPBUFLEN, |
| 335 |
stdin))) { |
| 336 |
|
| 337 |
ret = kcapi_md_update(handle, tmpbuf, bufsize); |
| 338 |
if (ret < 0) |
| 339 |
return (int)ret; |
| 340 |
} |
| 341 |
kcapi_memset_secure(tmpbuf, 0, sizeof(tmpbuf)); |
| 342 |
} |
| 343 |
|
| 344 |
ret = kcapi_md_final(handle, md, sizeof(md)); |
| 345 |
|
| 346 |
if (ret > 0) { |
| 347 |
if (hashlen > (uint32_t)ret) { |
| 348 |
fprintf(stderr, "Invalid truncated hash size: %lu > %zd\n", |
| 349 |
(unsigned long)hashlen, ret); |
| 350 |
return (int)ret; |
| 351 |
} |
| 352 |
|
| 353 |
if (!hashlen) |
| 354 |
hashlen = (uint32_t)ret; |
| 355 |
|
| 356 |
if (comphash && comphashlen) { |
| 357 |
uint8_t compmd[64]; |
| 358 |
|
| 359 |
memset(compmd, 0, sizeof(compmd)); |
| 360 |
hex2bin(comphash, comphashlen, compmd, sizeof(compmd)); |
| 361 |
if ((comphashlen != hashlen * 2) || |
| 362 |
memcmp(compmd, md, hashlen)) |
| 363 |
ret = 1; |
| 364 |
else |
| 365 |
ret = 0; |
| 366 |
} else { |
| 367 |
if (outfile == NULL) { |
| 368 |
bin2print(md, hashlen, NULL, stdout, |
| 369 |
params->newline); |
| 370 |
} else if (params->bsd_style) { |
| 371 |
fprintf(outfile, "%s (%s) = ", |
| 372 |
params->name.bsdname, |
| 373 |
filename ? filename : "-"); |
| 374 |
bin2print(md, hashlen, NULL, outfile, |
| 375 |
params->newline); |
| 376 |
} else { |
| 377 |
bin2print(md, hashlen, filename ? filename : "-", |
| 378 |
outfile, params->newline); |
| 379 |
} |
| 380 |
ret = 0; |
| 381 |
} |
| 382 |
} else { |
| 383 |
fprintf(stderr, "Generation of hash for file %s failed (%zd)\n", |
| 384 |
filename ? filename : "stdin", ret); |
| 385 |
} |
| 386 |
return (int)ret; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
#pragma GCC diagnostic push |
| 398 |
#if GCC_VERSION >= 80100 |
| 399 |
#pragma GCC diagnostic ignored "-Wstringop-truncation" |
| 400 |
#endif |
| 401 |
#if GCC_VERSION >= 80200 |
| 402 |
#pragma GCC diagnostic ignored "-Wstringop-overflow" |
| 403 |
#endif |
| 404 |
static char *paste(char *dst, const char *src, size_t size) |
| 405 |
{ |
| 406 |
strncpy(dst, src, size); |
| 407 |
return dst + size; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
static char *get_hmac_file(const char *filename, const char *checkdir) |
| 417 |
{ |
| 418 |
size_t i, filelen, pathlen, namelen, basenamestart = 0; |
| 419 |
const char *check_prefix = CHECK_PREFIX; |
| 420 |
size_t prefixlen = strlen(check_prefix); |
| 421 |
size_t suffixlen = strlen(CHECK_SUFFIX); |
| 422 |
char *cursor, *checkfile = NULL; |
| 423 |
|
| 424 |
if (prefixlen == 0 && checkdir == NULL) { |
| 425 |
check_prefix = "."; |
| 426 |
prefixlen = 1; |
| 427 |
} |
| 428 |
|
| 429 |
filelen = strlen(filename); |
| 430 |
if (filelen > 4096) { |
| 431 |
fprintf(stderr, "File too long\n"); |
| 432 |
return NULL; |
| 433 |
} |
| 434 |
for (i = 0; i < filelen; i++) { |
| 435 |
if (!strncmp(filename + i, "/", 1)) |
| 436 |
basenamestart = i + 1; |
| 437 |
} |
| 438 |
|
| 439 |
namelen = filelen - basenamestart; |
| 440 |
pathlen = checkdir ? strlen(checkdir) + 1 : basenamestart; |
| 441 |
|
| 442 |
checkfile = malloc(pathlen + namelen + prefixlen + 1 + |
| 443 |
suffixlen + 1 ); |
| 444 |
if (!checkfile) |
| 445 |
return NULL; |
| 446 |
|
| 447 |
cursor = checkfile; |
| 448 |
if (checkdir) { |
| 449 |
cursor = paste(cursor, checkdir, strlen(checkdir)); |
| 450 |
cursor = paste(cursor, "/", 1); |
| 451 |
} else if (pathlen > 0) |
| 452 |
cursor = paste(cursor, filename, pathlen); |
| 453 |
|
| 454 |
cursor = paste(cursor, check_prefix, prefixlen); |
| 455 |
cursor = paste(cursor, filename + basenamestart, namelen); |
| 456 |
cursor = paste(cursor, "."CHECK_SUFFIX, 1 + suffixlen); |
| 457 |
strncpy(cursor, "\0", 1); |
| 458 |
return checkfile; |
| 459 |
} |
| 460 |
#pragma GCC diagnostic pop |
| 461 |
|
| 462 |
static int hash_files(const struct hash_params *params, |
| 463 |
char *filenames[], uint32_t files, |
| 464 |
int fipshmac, const char *checkdir, int just_print) |
| 465 |
{ |
| 466 |
struct kcapi_handle *handle; |
| 467 |
const char *hashname = params->name.kcapiname; |
| 468 |
uint32_t i = 0; |
| 469 |
int ret = 0; |
| 470 |
|
| 471 |
ret = kcapi_md_init(&handle, hashname, 0); |
| 472 |
if (ret) { |
| 473 |
fprintf(stderr, "Allocation of %s cipher failed (ret=%d)\n", |
| 474 |
hashname, ret); |
| 475 |
return -EFAULT; |
| 476 |
} |
| 477 |
if (params->key.data) { |
| 478 |
ret = kcapi_md_setkey(handle, params->key.data, |
| 479 |
(uint32_t)params->key.len); |
| 480 |
if (ret) { |
| 481 |
fprintf(stderr, "Setting HMAC key for %s failed (%d)\n", |
| 482 |
hashname, ret); |
| 483 |
kcapi_md_destroy(handle); |
| 484 |
return -EINVAL; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
if (files) { |
| 489 |
for (i = 0; i < files; i++) { |
| 490 |
FILE *out = just_print ? NULL : stdout; |
| 491 |
const char *filename = filenames[i]; |
| 492 |
|
| 493 |
if (fipshmac) { |
| 494 |
char *outfile = get_hmac_file(filenames[i], checkdir); |
| 495 |
if (!outfile) { |
| 496 |
fprintf(stderr, |
| 497 |
"Cannot create HMAC file name\n"); |
| 498 |
continue; |
| 499 |
} |
| 500 |
out = fopen(outfile, "w"); |
| 501 |
if (!out) { |
| 502 |
fprintf(stderr, |
| 503 |
"Cannot open HMAC file %s\n", |
| 504 |
outfile); |
| 505 |
free(outfile); |
| 506 |
continue; |
| 507 |
} |
| 508 |
free(outfile); |
| 509 |
} else if (strcmp(filename, "-") == 0) { |
| 510 |
filename = NULL; |
| 511 |
} |
| 512 |
ret = hasher(handle, params, filename, NULL, 0, out); |
| 513 |
if (fipshmac) |
| 514 |
fclose(out); |
| 515 |
if (ret) |
| 516 |
break; |
| 517 |
} |
| 518 |
} else { |
| 519 |
ret = hasher(handle, params, NULL, NULL, 0, stdout); |
| 520 |
} |
| 521 |
|
| 522 |
kcapi_md_destroy(handle); |
| 523 |
return ret; |
| 524 |
} |
| 525 |
|
| 526 |
#define CHK_QUIET (1) |
| 527 |
#define CHK_STATUS (2) |
| 528 |
|
| 529 |
static int process_checkfile(const struct hash_params *params, |
| 530 |
const char *checkfile, const char *targetfile, int log) |
| 531 |
{ |
| 532 |
FILE *file = NULL; |
| 533 |
int ret = 0; |
| 534 |
int checked_any = 0; |
| 535 |
struct kcapi_handle *handle; |
| 536 |
const char *hashname = params->name.kcapiname; |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
char buf[(4096 + 128 + 2 + 1)]; |
| 544 |
|
| 545 |
ret = kcapi_md_init(&handle, hashname, 0); |
| 546 |
if (ret) { |
| 547 |
fprintf(stderr, "Allocation of %s cipher failed (%d)\n", |
| 548 |
hashname, ret); |
| 549 |
return -EFAULT; |
| 550 |
} |
| 551 |
if (params->key.data) { |
| 552 |
ret = kcapi_md_setkey(handle, params->key.data, |
| 553 |
(uint32_t)params->key.len); |
| 554 |
if (ret) { |
| 555 |
fprintf(stderr, "Setting HMAC key for %s failed (%d)\n", |
| 556 |
hashname, ret); |
| 557 |
ret = -EINVAL; |
| 558 |
goto out; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
file = strcmp(checkfile, "-") ? fopen(checkfile, "r") : stdin; |
| 563 |
if (!file) { |
| 564 |
fprintf(stderr, "Cannot open file %s\n", checkfile); |
| 565 |
ret = 253; |
| 566 |
goto out; |
| 567 |
} |
| 568 |
|
| 569 |
while (fgets(buf, sizeof(buf), file)) { |
| 570 |
char *filename = NULL; |
| 571 |
char *hexhash = NULL; |
| 572 |
uint32_t hexhashlen = 0; |
| 573 |
uint32_t linelen = (uint32_t)strlen(buf); |
| 574 |
uint32_t i; |
| 575 |
uint32_t bsd_style = 0; |
| 576 |
|
| 577 |
if (linelen == 0) |
| 578 |
break; |
| 579 |
|
| 580 |
|
| 581 |
for (i = linelen - 1; i > 0; i--) { |
| 582 |
if (!isprint(buf[i])) { |
| 583 |
buf[i] = '\0'; |
| 584 |
linelen--; |
| 585 |
} else |
| 586 |
break; |
| 587 |
} |
| 588 |
|
| 589 |
for (i = 1; i < linelen; i++) { |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
if (((linelen - i) >= 3) && |
| 595 |
isblank(buf[i]) && |
| 596 |
buf[i+1] == '=' && |
| 597 |
isblank(buf[i+2])) { |
| 598 |
|
| 599 |
bsd_style = i + 3; |
| 600 |
hexhash = buf + bsd_style; |
| 601 |
break; |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
for (i = 0; i < linelen; i++) { |
| 606 |
|
| 607 |
if (!bsd_style && isblank(buf[i])) { |
| 608 |
filename = buf + i; |
| 609 |
break; |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
if (!bsd_style && !filename) |
| 614 |
hexhashlen++; |
| 615 |
|
| 616 |
|
| 617 |
if (bsd_style && |
| 618 |
(linelen - i) >= 2 && |
| 619 |
isblank(buf[i]) && |
| 620 |
buf[i + 1] == '(') { |
| 621 |
filename = buf + i + 2; |
| 622 |
break; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
if (!bsd_style) |
| 628 |
hexhash = buf; |
| 629 |
|
| 630 |
if (bsd_style) { |
| 631 |
|
| 632 |
hexhashlen = linelen - bsd_style + 1; |
| 633 |
|
| 634 |
|
| 635 |
if (buf[(bsd_style - 4)] == ')') |
| 636 |
buf[(bsd_style - 4)] = '\0'; |
| 637 |
} |
| 638 |
|
| 639 |
if (!hexhash || !hexhashlen) { |
| 640 |
fprintf(stderr, "Invalid checkfile format\n"); |
| 641 |
ret = 1; |
| 642 |
goto out; |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
if (targetfile) { |
| 647 |
ret = hasher(handle, params, targetfile, |
| 648 |
hexhash, hexhashlen, stdout); |
| 649 |
checked_any = 1; |
| 650 |
goto out; |
| 651 |
} |
| 652 |
|
| 653 |
if (filename) { |
| 654 |
int r; |
| 655 |
|
| 656 |
if (!bsd_style) { |
| 657 |
if (!isblank(filename[0]) || |
| 658 |
(!isblank(filename[1]) && filename[1] != '*')) { |
| 659 |
fprintf(stderr, "Invalid checkfile format\n"); |
| 660 |
ret = 1; |
| 661 |
goto out; |
| 662 |
} |
| 663 |
filename += 2; |
| 664 |
} |
| 665 |
|
| 666 |
r = hasher(handle, params, filename, hexhash, hexhashlen, stdout); |
| 667 |
|
| 668 |
if (r == 0) { |
| 669 |
if (log < CHK_QUIET) |
| 670 |
printf("%s: OK\n", filename); |
| 671 |
} else { |
| 672 |
if (log < CHK_STATUS) |
| 673 |
printf("%s: Not OK\n", |
| 674 |
filename); |
| 675 |
if (ret >= 0) |
| 676 |
ret++; |
| 677 |
} |
| 678 |
checked_any = 1; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
out: |
| 683 |
if (file) |
| 684 |
fclose(file); |
| 685 |
kcapi_md_destroy(handle); |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
return ret != 0 ? ret : !checked_any; |
| 692 |
|
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
#define SELFCHECK_CHECK 0 |
| 697 |
#define SELFCHECK_PRINT_SELF 1 |
| 698 |
#define SELFCHECK_PRINT_LIB 2 |
| 699 |
|
| 700 |
static int fipscheck_self(const struct hash_params *params_bin, |
| 701 |
const struct hash_params *params_lib, int mode) |
| 702 |
{ |
| 703 |
char *checkfile = NULL; |
| 704 |
uint32_t n = 0; |
| 705 |
int ret = -EINVAL; |
| 706 |
char fipsflag[1]; |
| 707 |
#define BUFSIZE 4096 |
| 708 |
char selfname[BUFSIZE]; |
| 709 |
char *names[] = { selfname }; |
| 710 |
ssize_t selfnamesize = 0; |
| 711 |
Dl_info info; |
| 712 |
void *dl = NULL, *sym; |
| 713 |
|
| 714 |
#ifdef HAVE_SECURE_GETENV |
| 715 |
if (secure_getenv("KCAPI_HASHER_FORCE_FIPS") || mode != SELFCHECK_CHECK) { |
| 716 |
#else |
| 717 |
if (getenv("KCAPI_HASHER_FORCE_FIPS") || mode != SELFCHECK_CHECK) { |
| 718 |
#endif |
| 719 |
fipsflag[0] = 1; |
| 720 |
} else { |
| 721 |
FILE *fipsfile = NULL; |
| 722 |
|
| 723 |
fipsfile = fopen("/proc/sys/crypto/fips_enabled", "r"); |
| 724 |
if (!fipsfile) { |
| 725 |
if (errno == ENOENT) { |
| 726 |
|
| 727 |
return 0; |
| 728 |
} else { |
| 729 |
fprintf(stderr, "Cannot open fips_enabled file: %s\n", |
| 730 |
strerror(errno)); |
| 731 |
return -EIO; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
n = (uint32_t)fread((void *)fipsflag, 1, 1, fipsfile); |
| 736 |
fclose(fipsfile); |
| 737 |
if (n != 1) { |
| 738 |
fprintf(stderr, "Cannot read FIPS flag\n"); |
| 739 |
goto out; |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
if (fipsflag[0] == '0') { |
| 744 |
ret = 0; |
| 745 |
goto out; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
if (mode == SELFCHECK_CHECK || mode == SELFCHECK_PRINT_SELF) { |
| 750 |
memset(selfname, 0, sizeof(selfname)); |
| 751 |
selfnamesize = readlink("/proc/self/exe", selfname, BUFSIZE); |
| 752 |
if (selfnamesize >= BUFSIZE || selfnamesize < 0) { |
| 753 |
fprintf(stderr, "Cannot obtain my filename\n"); |
| 754 |
ret = -EFAULT; |
| 755 |
goto out; |
| 756 |
} |
| 757 |
|
| 758 |
if (mode == SELFCHECK_PRINT_SELF) { |
| 759 |
ret = hash_files(params_bin, names, 1, 0, NULL, 1); |
| 760 |
goto out; |
| 761 |
} |
| 762 |
|
| 763 |
checkfile = get_hmac_file(selfname, params_bin->key.checkdir); |
| 764 |
if (!checkfile) { |
| 765 |
ret = -ENOMEM; |
| 766 |
goto out; |
| 767 |
} |
| 768 |
|
| 769 |
ret = process_checkfile(params_bin, checkfile, selfname, CHK_STATUS); |
| 770 |
if (ret) |
| 771 |
goto out; |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
if (mode == SELFCHECK_CHECK || mode == SELFCHECK_PRINT_LIB) { |
| 776 |
memset(selfname, 0, sizeof(selfname)); |
| 777 |
snprintf(selfname, (sizeof(selfname) - 1), "libkcapi.so.%u", |
| 778 |
KCAPI_MAJVERSION); |
| 779 |
dl = dlopen(selfname, RTLD_NODELETE|RTLD_NOLOAD|RTLD_LAZY); |
| 780 |
if (dl == NULL) { |
| 781 |
fprintf(stderr, "dlopen of file %s failed\n", selfname); |
| 782 |
ret = -EFAULT; |
| 783 |
goto out; |
| 784 |
} |
| 785 |
|
| 786 |
memset(selfname, 0, sizeof(selfname)); |
| 787 |
sym = dlsym(dl, "kcapi_md_init"); |
| 788 |
if (sym == NULL || !dladdr(sym, &info)) { |
| 789 |
fprintf(stderr, "finding symbol kcapi_md_init failed\n"); |
| 790 |
ret = -EFAULT; |
| 791 |
goto out; |
| 792 |
} |
| 793 |
|
| 794 |
strncpy(selfname, info.dli_fname, (sizeof(selfname) - 1)); |
| 795 |
|
| 796 |
if (mode == SELFCHECK_PRINT_LIB) { |
| 797 |
ret = hash_files(params_lib, names, 1, 0, NULL, 1); |
| 798 |
goto out; |
| 799 |
} |
| 800 |
|
| 801 |
if (checkfile) |
| 802 |
free(checkfile); |
| 803 |
checkfile = get_hmac_file(selfname, params_lib->key.checkdir); |
| 804 |
if (!checkfile) { |
| 805 |
ret = -ENOMEM; |
| 806 |
goto out; |
| 807 |
} |
| 808 |
|
| 809 |
ret = process_checkfile(params_lib, checkfile, selfname, CHK_STATUS); |
| 810 |
} |
| 811 |
|
| 812 |
out: |
| 813 |
if (checkfile) |
| 814 |
free(checkfile); |
| 815 |
if (dl) |
| 816 |
dlclose(dl); |
| 817 |
return ret; |
| 818 |
} |
| 819 |
|
| 820 |
int strtou32(const char *str, uint32_t *out) |
| 821 |
{ |
| 822 |
char *end; |
| 823 |
unsigned long value = strtoul(str, &end, 10); |
| 824 |
if (*str == '\0' || *end != '\0' || value > UINT32_MAX) |
| 825 |
return -EINVAL; |
| 826 |
*out = (uint32_t)value; |
| 827 |
return 0; |
| 828 |
} |
| 829 |
|
| 830 |
int main(int argc, char *argv[]) |
| 831 |
{ |
| 832 |
const struct hash_name *names; |
| 833 |
struct hash_params params = { |
| 834 |
.name = { NULL, NULL }, |
| 835 |
.key = { NULL, NULL, 0 }, |
| 836 |
.hashlen = 0, |
| 837 |
.bsd_style = 0, |
| 838 |
.newline = 1, |
| 839 |
}; |
| 840 |
const struct hash_params *params_self; |
| 841 |
char *basec = NULL; |
| 842 |
const char *basen = NULL; |
| 843 |
int ret = -EFAULT; |
| 844 |
|
| 845 |
size_t mapped = (sizeof(void*) == 4) ? 64<<20 : 1<<30; |
| 846 |
|
| 847 |
char *checkfile = NULL; |
| 848 |
const char *targetfile = NULL; |
| 849 |
const char *checkdir = NULL; |
| 850 |
uint8_t *hmackey_alloc = NULL; |
| 851 |
uint8_t *hmackey_mmap = NULL; |
| 852 |
int opt_index = 0; |
| 853 |
int loglevel = 0; |
| 854 |
int hmac = 0; |
| 855 |
int fipscheck = 0; |
| 856 |
int fipshmac = 0; |
| 857 |
int selfcheck_mode = SELFCHECK_CHECK; |
| 858 |
|
| 859 |
static const char *opts_name_short = "n:"; |
| 860 |
static const struct option opts_name[] = { |
| 861 |
{"name", 1, 0, 'n'}, |
| 862 |
{0, 0, 0, 0} |
| 863 |
}; |
| 864 |
|
| 865 |
static const char *opts_short = "c:uh:t:SLqk:K:vbd:Pz"; |
| 866 |
static const struct option opts[] = { |
| 867 |
{"help", 0, 0, 0}, |
| 868 |
{"tag", 0, 0, 0}, |
| 869 |
{"quiet", 0, 0, 0}, |
| 870 |
{"check", 1, 0, 'c'}, |
| 871 |
{"unkeyed", 0, 0, 'u'}, |
| 872 |
{"hash", 1, 0, 'h'}, |
| 873 |
{"truncate", 1, 0, 't'}, |
| 874 |
{"self-sum", 0, 0, 'S'}, |
| 875 |
{"self-sum-lib", 0, 0, 'L'}, |
| 876 |
{"status", 0, 0, 'q'}, |
| 877 |
{"key-file", 1, 0, 'k'}, |
| 878 |
{"key", 1, 0, 'K'}, |
| 879 |
{"version", 0, 0, 'v'}, |
| 880 |
{0, 0, 0, 0} |
| 881 |
}; |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
const struct hash_params PARAMS_SELF_FIPSCHECK = { |
| 891 |
.name = NAMES_SHA256[1], |
| 892 |
.bsd_style = 0, |
| 893 |
.hashlen = 0, |
| 894 |
.key = KEY_FIPSCHECK, |
| 895 |
}; |
| 896 |
const struct hash_params PARAMS_SELF_HMACCALC = { |
| 897 |
.name = NAMES_SHA512[1], |
| 898 |
.bsd_style = 0, |
| 899 |
.hashlen = 0, |
| 900 |
.key = KEY_HMACCALC, |
| 901 |
}; |
| 902 |
|
| 903 |
basec = strdup(argv[0]); |
| 904 |
if (!basec) { |
| 905 |
fprintf(stderr, "Error copying file name: %s\n", |
| 906 |
strerror(errno)); |
| 907 |
return 255; |
| 908 |
} |
| 909 |
basen = basename(basec); |
| 910 |
|
| 911 |
opterr = 0; |
| 912 |
if (getopt_long(argc, argv, opts_name_short, opts_name, &opt_index) == 'n') |
| 913 |
basen = optarg; |
| 914 |
else |
| 915 |
optind = 1; |
| 916 |
opterr = 1; |
| 917 |
|
| 918 |
params_self = &PARAMS_SELF_FIPSCHECK; |
| 919 |
if (0 == strncmp(basen, "sha256sum", 9)) { |
| 920 |
names = NAMES_SHA256; |
| 921 |
} else if (0 == strncmp(basen, "sha512sum", 9)) { |
| 922 |
names = NAMES_SHA512; |
| 923 |
} else if (0 == strncmp(basen, "sha1sum", 7)) { |
| 924 |
names = NAMES_SHA1; |
| 925 |
} else if (0 == strncmp(basen, "sha224sum", 9)) { |
| 926 |
names = NAMES_SHA224; |
| 927 |
} else if (0 == strncmp(basen, "sha384sum", 9)) { |
| 928 |
names = NAMES_SHA384; |
| 929 |
} else if (0 == strncmp(basen, "md5sum", 6)) { |
| 930 |
names = NAMES_MD5; |
| 931 |
} else if (0 == strncmp(basen, "sm3sum", 6)) { |
| 932 |
names = NAMES_SM3; |
| 933 |
} else if (0 == strncmp(basen, "fipshmac", 8)) { |
| 934 |
names = NAMES_SHA256; |
| 935 |
hmac = 1; |
| 936 |
params.key = KEY_FIPSCHECK; |
| 937 |
fipshmac = 1; |
| 938 |
} else if (0 == strncmp(basen, "fipscheck", 9)) { |
| 939 |
names = NAMES_SHA256; |
| 940 |
hmac = 1; |
| 941 |
params.key = KEY_FIPSCHECK; |
| 942 |
fipscheck = 1; |
| 943 |
} else if (0 == strncmp(basen, "sha1hmac", 8)) { |
| 944 |
names = NAMES_SHA1; |
| 945 |
hmac = 1; |
| 946 |
params.key = KEY_HMACCALC; |
| 947 |
params_self = &PARAMS_SELF_HMACCALC; |
| 948 |
} else if (0 == strncmp(basen, "sha224hmac", 10)) { |
| 949 |
names = NAMES_SHA224; |
| 950 |
hmac = 1; |
| 951 |
params.key = KEY_HMACCALC; |
| 952 |
params_self = &PARAMS_SELF_HMACCALC; |
| 953 |
} else if (0 == strncmp(basen, "sha256hmac", 10)) { |
| 954 |
names = NAMES_SHA256; |
| 955 |
hmac = 1; |
| 956 |
params.key = KEY_HMACCALC; |
| 957 |
params_self = &PARAMS_SELF_HMACCALC; |
| 958 |
} else if (0 == strncmp(basen, "sha384hmac", 10)) { |
| 959 |
names = NAMES_SHA384; |
| 960 |
hmac = 1; |
| 961 |
params.key = KEY_HMACCALC; |
| 962 |
params_self = &PARAMS_SELF_HMACCALC; |
| 963 |
} else if (0 == strncmp(basen, "sha512hmac", 10)) { |
| 964 |
names = NAMES_SHA512; |
| 965 |
hmac = 1; |
| 966 |
params.key = KEY_HMACCALC; |
| 967 |
params_self = &PARAMS_SELF_HMACCALC; |
| 968 |
} else if (0 == strncmp(basen, "sm3hmac", 7)) { |
| 969 |
names = NAMES_SM3; |
| 970 |
hmac = 1; |
| 971 |
params.key = KEY_HMACCALC; |
| 972 |
params_self = &PARAMS_SELF_HMACCALC; |
| 973 |
} else { |
| 974 |
fprintf(stderr, "Unknown invocation name: %s\n", basen); |
| 975 |
usage(argv[0], fipscheck); |
| 976 |
ret = 1; |
| 977 |
goto out; |
| 978 |
} |
| 979 |
|
| 980 |
while (1) { |
| 981 |
int c = getopt_long(argc, argv, opts_short, opts, &opt_index); |
| 982 |
|
| 983 |
if (-1 == c) |
| 984 |
break; |
| 985 |
switch (c) { |
| 986 |
case 0: |
| 987 |
switch (opt_index) { |
| 988 |
case 0: |
| 989 |
usage(argv[0], fipscheck); |
| 990 |
ret = 0; |
| 991 |
goto out; |
| 992 |
case 1: |
| 993 |
params.bsd_style = 1; |
| 994 |
break; |
| 995 |
case 2: |
| 996 |
loglevel = CHK_QUIET; |
| 997 |
break; |
| 998 |
} |
| 999 |
break; |
| 1000 |
case 'c': |
| 1001 |
if (checkfile) |
| 1002 |
free(checkfile); |
| 1003 |
checkfile = strdup(optarg); |
| 1004 |
if (!checkfile) { |
| 1005 |
fprintf(stderr, "Error copying file name: %s\n", |
| 1006 |
strerror(errno)); |
| 1007 |
ret = 1; |
| 1008 |
goto out; |
| 1009 |
} |
| 1010 |
break; |
| 1011 |
case 'u': |
| 1012 |
if (hmackey_alloc) { |
| 1013 |
kcapi_memset_secure(hmackey_alloc, 0, |
| 1014 |
(uint32_t)params.key.len); |
| 1015 |
free(hmackey_alloc); |
| 1016 |
hmackey_alloc = NULL; |
| 1017 |
} else if (hmackey_mmap) { |
| 1018 |
munmap(hmackey_mmap, |
| 1019 |
(size_t)params.key.len); |
| 1020 |
hmackey_mmap = NULL; |
| 1021 |
} |
| 1022 |
params.key.data = NULL; |
| 1023 |
params.key.len = 0; |
| 1024 |
hmac = 0; |
| 1025 |
break; |
| 1026 |
case 'h': |
| 1027 |
if (0 == strcmp(optarg, "sha1")) |
| 1028 |
names = NAMES_SHA1; |
| 1029 |
else if (0 == strcmp(optarg, "sha224")) |
| 1030 |
names = NAMES_SHA224; |
| 1031 |
else if (0 == strcmp(optarg, "sha256")) |
| 1032 |
names = NAMES_SHA256; |
| 1033 |
else if (0 == strcmp(optarg, "sha384")) |
| 1034 |
names = NAMES_SHA384; |
| 1035 |
else if (0 == strcmp(optarg, "sha512")) |
| 1036 |
names = NAMES_SHA512; |
| 1037 |
else if (0 == strcmp(optarg, "sm3")) |
| 1038 |
names = NAMES_SM3; |
| 1039 |
else { |
| 1040 |
fprintf(stderr, "Invalid hash: %s\n", optarg); |
| 1041 |
ret = 1; |
| 1042 |
goto out; |
| 1043 |
} |
| 1044 |
break; |
| 1045 |
case 't': |
| 1046 |
if (strtou32(optarg, ¶ms.hashlen)) { |
| 1047 |
fprintf(stderr, "Invalid number: %s\n", optarg); |
| 1048 |
ret = 1; |
| 1049 |
goto out; |
| 1050 |
} |
| 1051 |
if (params.hashlen % 8 != 0) { |
| 1052 |
fprintf(stderr, "Truncated hash size must be " |
| 1053 |
"a multiple of 8 bits!\n"); |
| 1054 |
ret = 1; |
| 1055 |
goto out; |
| 1056 |
} |
| 1057 |
params.hashlen /= 8; |
| 1058 |
break; |
| 1059 |
case 'S': |
| 1060 |
selfcheck_mode = SELFCHECK_PRINT_SELF; |
| 1061 |
break; |
| 1062 |
case 'L': |
| 1063 |
selfcheck_mode = SELFCHECK_PRINT_LIB; |
| 1064 |
break; |
| 1065 |
case 'q': |
| 1066 |
loglevel = CHK_STATUS; |
| 1067 |
break; |
| 1068 |
case 'k': |
| 1069 |
if (hmackey_alloc) { |
| 1070 |
kcapi_memset_secure(hmackey_alloc, 0, |
| 1071 |
(uint32_t)params.key.len); |
| 1072 |
free(hmackey_alloc); |
| 1073 |
hmackey_alloc = NULL; |
| 1074 |
} else if (hmackey_mmap) { |
| 1075 |
munmap(hmackey_mmap, |
| 1076 |
(size_t)params.key.len); |
| 1077 |
hmackey_mmap = NULL; |
| 1078 |
} |
| 1079 |
params.key.len = 0; |
| 1080 |
ret = mmap_file(optarg, &hmackey_mmap, |
| 1081 |
¶ms.key.len, |
| 1082 |
&mapped, 0); |
| 1083 |
if (!ret) { |
| 1084 |
params.key.data = hmackey_mmap; |
| 1085 |
hmac = 1; |
| 1086 |
break; |
| 1087 |
} |
| 1088 |
|
| 1089 |
ret = load_file(optarg, &hmackey_alloc, |
| 1090 |
¶ms.key.len); |
| 1091 |
if (ret) { |
| 1092 |
ret = 1; |
| 1093 |
goto out; |
| 1094 |
} |
| 1095 |
params.key.data = hmackey_alloc; |
| 1096 |
hmac = 1; |
| 1097 |
break; |
| 1098 |
case 'K': |
| 1099 |
if (hmackey_alloc) { |
| 1100 |
kcapi_memset_secure(hmackey_alloc, 0, |
| 1101 |
(uint32_t)params.key.len); |
| 1102 |
free(hmackey_alloc); |
| 1103 |
hmackey_alloc = NULL; |
| 1104 |
} else if (hmackey_mmap) { |
| 1105 |
munmap(hmackey_mmap, |
| 1106 |
(size_t)params.key.len); |
| 1107 |
hmackey_mmap = NULL; |
| 1108 |
} |
| 1109 |
hmackey_alloc = (uint8_t *)strdup(optarg); |
| 1110 |
if (!hmackey_alloc) { |
| 1111 |
fprintf(stderr, "Cannot allocate memory for HMAC key\n"); |
| 1112 |
ret = 1; |
| 1113 |
goto out; |
| 1114 |
} |
| 1115 |
params.key.data = hmackey_alloc; |
| 1116 |
params.key.len = (off_t)strlen(optarg); |
| 1117 |
hmac = 1; |
| 1118 |
break; |
| 1119 |
case 'v': |
| 1120 |
version(argv[0]); |
| 1121 |
ret = 0; |
| 1122 |
goto out; |
| 1123 |
case 'd': |
| 1124 |
checkdir = optarg; |
| 1125 |
break; |
| 1126 |
case 'b': |
| 1127 |
case 'P': |
| 1128 |
|
| 1129 |
break; |
| 1130 |
case 'z': |
| 1131 |
params.newline = 2; |
| 1132 |
break; |
| 1133 |
default: |
| 1134 |
usage(argv[0], fipscheck); |
| 1135 |
ret = 1; |
| 1136 |
goto out; |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
if (selfcheck_mode != SELFCHECK_CHECK) { |
| 1141 |
if (checkfile) { |
| 1142 |
fprintf(stderr, "-S/-L and -c cannot be combined\n"); |
| 1143 |
ret = 1; |
| 1144 |
goto out; |
| 1145 |
} |
| 1146 |
if (optind != argc) { |
| 1147 |
fprintf(stderr, "-S/-L cannot be used with input files\n"); |
| 1148 |
ret = 1; |
| 1149 |
goto out; |
| 1150 |
} |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
if (fipscheck_self(params_self, &PARAMS_SELF_FIPSCHECK, selfcheck_mode)) { |
| 1155 |
fprintf(stderr, "Integrity check of application %s failed\n", |
| 1156 |
basen); |
| 1157 |
ret = 1; |
| 1158 |
goto out; |
| 1159 |
} |
| 1160 |
|
| 1161 |
if (selfcheck_mode != SELFCHECK_CHECK) { |
| 1162 |
ret = 0; |
| 1163 |
goto out; |
| 1164 |
} |
| 1165 |
|
| 1166 |
params.name = names[hmac]; |
| 1167 |
|
| 1168 |
if (fipscheck) { |
| 1169 |
if (optind >= argc) { |
| 1170 |
fprintf(stderr, "No file to check given for fipscheck\n"); |
| 1171 |
ret = 1; |
| 1172 |
goto out; |
| 1173 |
} |
| 1174 |
if (checkfile) { |
| 1175 |
fprintf(stderr, "-c is not valid for fipscheck\n"); |
| 1176 |
ret = 1; |
| 1177 |
goto out; |
| 1178 |
} |
| 1179 |
|
| 1180 |
targetfile = argv[optind]; |
| 1181 |
if (checkfile) |
| 1182 |
free(checkfile); |
| 1183 |
checkfile = get_hmac_file(targetfile, params.key.checkdir); |
| 1184 |
if (!checkfile) { |
| 1185 |
ret = 1; |
| 1186 |
goto out; |
| 1187 |
} |
| 1188 |
optind++; |
| 1189 |
} |
| 1190 |
|
| 1191 |
if (!checkfile) |
| 1192 |
ret = hash_files(¶ms, argv + optind, |
| 1193 |
(uint32_t)(argc - optind), |
| 1194 |
fipshmac, checkdir, 0); |
| 1195 |
else if (optind == argc) |
| 1196 |
ret = process_checkfile(¶ms, checkfile, targetfile, loglevel); |
| 1197 |
else { |
| 1198 |
fprintf(stderr, "-c cannot be used with input files\n"); |
| 1199 |
ret = 1; |
| 1200 |
} |
| 1201 |
|
| 1202 |
|
| 1203 |
out: |
| 1204 |
if (basec) |
| 1205 |
free(basec); |
| 1206 |
if (checkfile) |
| 1207 |
free(checkfile); |
| 1208 |
if (hmackey_alloc) { |
| 1209 |
kcapi_memset_secure(hmackey_alloc, 0, (uint32_t)params.key.len); |
| 1210 |
free(hmackey_alloc); |
| 1211 |
} else if (hmackey_mmap) { |
| 1212 |
munmap(hmackey_mmap, (size_t)params.key.len); |
| 1213 |
} |
| 1214 |
|
| 1215 |
return ret; |
| 1216 |
} |