Skip to content

Commit

Permalink
keyed mode OK
Browse files Browse the repository at this point in the history
Signed-off-by: Pantelis Antoniou <[email protected]>
  • Loading branch information
pantoniou committed Sep 8, 2023
1 parent 931a595 commit 68157e5
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/internal/fy-b3sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ int main(int argc, char *argv[])
bool do_list_backends = false, check = false;
unsigned int mt_degree = 0, num_threads = 0, cpusimd_num_cpus = 0, cpusimd_mult_fact = 0, length = BLAKE3_OUT_LEN;
const char *backend = NULL, *context = NULL;
uint8_t key[BLAKE3_OUT_LEN];
ssize_t rdn;

while ((opt = getopt_long_only(argc, argv, "cl:b:dh", lopts, &lidx)) != -1) {
switch (opt) {
Expand Down Expand Up @@ -442,6 +444,22 @@ int main(int argc, char *argv[])
goto err_out_usage;
}

if (keyed) {
rdn = fread(key, 1, BLAKE3_KEY_LEN, stdin);
if (rdn != BLAKE3_KEY_LEN) {
if (rdn >= 0 && rdn < BLAKE3_KEY_LEN)
fprintf(stderr, "Error: could not read secret key from <stdin>: short key\n\n");
else
fprintf(stderr, "Error: could not read secret key from <stdin>: error %s\n\n", strerror(errno));
goto err_out_usage;
}
rc = fgetc(stdin);
if (rc != EOF) {
fprintf(stderr, "Error: garbage trailing secret key from <stdin>\n\n");
goto err_out_usage;
}
}

memset(&host_cfg, 0, sizeof(host_cfg));
host_cfg.debug = debug;
host_cfg.no_mthread = no_mthread;
Expand All @@ -458,7 +476,10 @@ int main(int argc, char *argv[])
}

/* context is NULL when not supplied */
hasher = blake3_hasher_create(host_state, NULL, context, 0);
if (!keyed)
hasher = blake3_hasher_create(host_state, NULL, context, 0);
else
hasher = blake3_hasher_create(host_state, key, NULL, 0);
if (!hasher) {
fprintf(stderr, "unable to create blake3 hasher\n");
goto err_out;
Expand All @@ -483,6 +504,12 @@ int main(int argc, char *argv[])
/* if no arguments, use stdin */
filename = i < argc ? argv[i] : "-";

/* we can't handle '-' in keyed mode */
if (keyed && !strcmp(filename, "-")) {
fprintf(stderr, "Cannot use <stdin> in keyed mode\n");
goto err_out_usage;
}

if (!check)
rc = do_hash_file(host_state, hasher, filename, no_names, raw, length);
else
Expand Down

0 comments on commit 68157e5

Please sign in to comment.