1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * The 'fsverity measure' command
4  *
5  * Copyright (C) 2018 Google LLC
6  *
7  * Written by Eric Biggers.
8  */
9 
10 #include <fcntl.h>
11 #include <stdlib.h>
12 #include <sys/ioctl.h>
13 
14 #include "commands.h"
15 #include "fsverity_uapi.h"
16 #include "hash_algs.h"
17 
fsverity_cmd_measure(const struct fsverity_command * cmd,int argc,char * argv[])18 int fsverity_cmd_measure(const struct fsverity_command *cmd,
19 			 int argc, char *argv[])
20 {
21 	struct fsverity_digest *d = NULL;
22 	struct filedes file;
23 	char digest_hex[FS_VERITY_MAX_DIGEST_SIZE * 2 + 1];
24 	const struct fsverity_hash_alg *hash_alg;
25 	char _hash_alg_name[32];
26 	const char *hash_alg_name;
27 	int status;
28 	int i;
29 
30 	if (argc < 2)
31 		goto out_usage;
32 
33 	d = xzalloc(sizeof(*d) + FS_VERITY_MAX_DIGEST_SIZE);
34 
35 	for (i = 1; i < argc; i++) {
36 		d->digest_size = FS_VERITY_MAX_DIGEST_SIZE;
37 
38 		if (!open_file(&file, argv[i], O_RDONLY, 0))
39 			goto out_err;
40 		if (ioctl(file.fd, FS_IOC_MEASURE_VERITY, d) != 0) {
41 			error_msg_errno("FS_IOC_MEASURE_VERITY failed on '%s'",
42 					file.name);
43 			filedes_close(&file);
44 			goto out_err;
45 		}
46 		filedes_close(&file);
47 
48 		ASSERT(d->digest_size <= FS_VERITY_MAX_DIGEST_SIZE);
49 		bin2hex(d->digest, d->digest_size, digest_hex);
50 		hash_alg = find_hash_alg_by_num(d->digest_algorithm);
51 		if (hash_alg) {
52 			hash_alg_name = hash_alg->name;
53 		} else {
54 			sprintf(_hash_alg_name, "ALG_%u", d->digest_algorithm);
55 			hash_alg_name = _hash_alg_name;
56 		}
57 		printf("%s:%s %s\n", hash_alg_name, digest_hex, argv[i]);
58 	}
59 	status = 0;
60 out:
61 	free(d);
62 	return status;
63 
64 out_err:
65 	status = 1;
66 	goto out;
67 
68 out_usage:
69 	usage(cmd, stderr);
70 	status = 2;
71 	goto out;
72 }
73