Skip to content

Commit

Permalink
kcapi-hasher: add -T option to override filenames in hash sums file
Browse files Browse the repository at this point in the history
This is useful when the hash sums file resides in a different directory
than the original file.

Signed-off-by: Zoltan Fridrich <[email protected]>
Signed-off-by: Stephan Mueller <[email protected]>
  • Loading branch information
ZoltanFridrich authored and smuellerDD committed Nov 4, 2023
1 parent e6e9288 commit 0e3e203
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
4 changes: 4 additions & 0 deletions apps/kcapi-hasher.1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ kcapi-hasher \- Kernel Crypto API Message Digest Check Helper
.B kcapi-hasher
[\-n \fI\,BASENAME\/\fR]
[\fI\,OPTION\/\fR]... \fB\-c\fR FILE
[\fB\-T\fR FILE]

.B kcapi-hasher
[\-n \fI\,BASENAME\/\fR]
Expand Down Expand Up @@ -71,6 +72,9 @@ Print checksum of the libkcapi library and exit
\fB\-c\fR \fB\-\-check\fR FILE
Verify hash sums from file
.TP
\fB\-T\fR \fB\-\-target\fR FILE
Override filenames found in hash sums file; use with -c
.TP
\fB\-u\fR \fB\-\-unkeyed\fR
Force unkeyed hash
.TP
Expand Down
59 changes: 34 additions & 25 deletions apps/kcapi-hasher.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,17 @@ static void usage(char *name, int fipscheck)
if (fipscheck)
fprintf(stderr, "\t%s [-n BASENAME] [OPTION]... FILE\n", base);
else {
fprintf(stderr, "\t%s [-n BASENAME] [OPTION]... -c FILE\n", base);
fprintf(stderr, "\t%s [-n BASENAME] [OPTION]... -c FILE [-T FILE]\n", base);
fprintf(stderr, "\t%s [-n BASENAME] [OPTION]... FILE...\n", base);
}
fprintf(stderr, "\nOptions:\n");
fprintf(stderr, "\t-n --name\t\tForce given application name (sha512hmac/...)\n");
fprintf(stderr, "\t-S --self-sum\t\tPrint checksum of this binary and exit\n");
fprintf(stderr, "\t-L --self-sum-lib\tPrint checksum of the libkcapi library and exit\n");
if (!fipscheck)
if (!fipscheck) {
fprintf(stderr, "\t-c --check FILE\t\tVerify hash sums from file\n");
fprintf(stderr, "\t-T --target FILE\tOverride filenames found in hash sums file; use with -c\n");
}
fprintf(stderr, "\t-u --unkeyed\t\tForce unkeyed hash\n");
fprintf(stderr, "\t-h --hash HASH\t\tUse given hash algorithm\n");
fprintf(stderr, "\t-t --truncate N\t\tUse hash truncated to N bits\n");
Expand Down Expand Up @@ -543,7 +545,7 @@ static int hash_files(const struct hash_params *params,
#define CHK_STATUS (2)

static int process_checkfile(const struct hash_params *params,
const char *checkfile, const char *targetfile, int log)
const char *checkfile, const char *targetfile, int log, int fipscheck)
{
FILE *file = NULL;
int ret = 0;
Expand Down Expand Up @@ -583,7 +585,7 @@ static int process_checkfile(const struct hash_params *params,
}

while (fgets(buf, sizeof(buf), file)) {
char *filename = NULL; // parsed file name
const char *filename = NULL; // parsed file name
char *hexhash = NULL; // parsed hex value of hash
uint32_t hexhashlen = 0; // length of hash hex value
uint32_t linelen = (uint32_t)strlen(buf);
Expand Down Expand Up @@ -658,17 +660,7 @@ static int process_checkfile(const struct hash_params *params,
goto out;
}

/* fipscheck does not have the filename in the check file */
if (targetfile) {
ret = hasher(handle, params, targetfile,
hexhash, hexhashlen, stdout);
checked_any = 1;
goto out;
}

if (filename) {
int r;

if (!bsd_style) {
if (!isblank(filename[0]) ||
(!isblank(filename[1]) && filename[1] != '*')) {
Expand All @@ -678,20 +670,28 @@ static int process_checkfile(const struct hash_params *params,
}
filename += 2;
}
}

/*
* if targetfile is specified, use it instead of the filename
* found inside the checkfile
*/
if (targetfile)
filename = targetfile;

r = hasher(handle, params, filename, hexhash, hexhashlen, stdout);
if (filename) {
ret = hasher(handle, params, filename, hexhash, hexhashlen, stdout);
checked_any = 1;
if (fipscheck)
goto out;

if (r == 0) {
if (ret == 0) {
if (log < CHK_QUIET)
printf("%s: OK\n", filename);
} else {
if (log < CHK_STATUS)
printf("%s: Not OK\n",
filename);
if (ret >= 0)
ret++;
printf("%s: Not OK\n", filename);
}
checked_any = 1;
}
}

Expand Down Expand Up @@ -783,7 +783,7 @@ static int fipscheck_self(const struct hash_params *params_bin,
goto out;
}

ret = process_checkfile(params_bin, checkfile, selfname, CHK_STATUS);
ret = process_checkfile(params_bin, checkfile, selfname, CHK_STATUS, 1);
if (ret)
goto out;
}
Expand Down Expand Up @@ -823,7 +823,7 @@ static int fipscheck_self(const struct hash_params *params_bin,
goto out;
}

ret = process_checkfile(params_lib, checkfile, selfname, CHK_STATUS);
ret = process_checkfile(params_lib, checkfile, selfname, CHK_STATUS, 1);
}

out:
Expand Down Expand Up @@ -878,12 +878,13 @@ int main(int argc, char *argv[])
{0, 0, 0, 0}
};

static const char *opts_short = "c:uh:t:SLqk:K:vbd:Pz";
static const char *opts_short = "c:T:uh:t:SLqk:K:vbd:Pz";
static const struct option opts[] = {
{"help", 0, 0, 0},
{"tag", 0, 0, 0},
{"quiet", 0, 0, 0},
{"check", 1, 0, 'c'},
{"target", 1, 0, 'T'},
{"unkeyed", 0, 0, 'u'},
{"hash", 1, 0, 'h'},
{"truncate", 1, 0, 't'},
Expand Down Expand Up @@ -1129,6 +1130,9 @@ int main(int argc, char *argv[])
version(argv[0]);
ret = 0;
goto out;
case 'T':
targetfile = optarg;
break;
case 'd':
checkdir = optarg;
break;
Expand Down Expand Up @@ -1198,6 +1202,11 @@ int main(int argc, char *argv[])
ret = 1;
goto out;
}
if (targetfile) {
fprintf(stderr, "-T is not valid for fipscheck\n");
ret = 1;
goto out;
}

targetfile = argv[optind];
if (checkfile)
Expand All @@ -1215,7 +1224,7 @@ int main(int argc, char *argv[])
(uint32_t)(argc - optind),
fipshmac, checkdir, 0);
else if (optind == argc)
ret = process_checkfile(&params, checkfile, targetfile, loglevel);
ret = process_checkfile(&params, checkfile, targetfile, loglevel, fipscheck);
else {
fprintf(stderr, "-c cannot be used with input files\n");
ret = 1;
Expand Down

0 comments on commit 0e3e203

Please sign in to comment.