• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: MIT
2 /*
3  * Implementation of libfsverity_enable() and libfsverity_enable_with_sig().
4  *
5  * Copyright 2020 Google LLC
6  *
7  * Use of this source code is governed by an MIT-style
8  * license that can be found in the LICENSE file or at
9  * https://opensource.org/licenses/MIT.
10  */
11 
12 #include "lib_private.h"
13 
14 #include <sys/ioctl.h>
15 
16 LIBEXPORT int
libfsverity_enable(int fd,const struct libfsverity_merkle_tree_params * params)17 libfsverity_enable(int fd, const struct libfsverity_merkle_tree_params *params)
18 {
19 	return libfsverity_enable_with_sig(fd, params, NULL, 0);
20 }
21 
22 LIBEXPORT int
libfsverity_enable_with_sig(int fd,const struct libfsverity_merkle_tree_params * params,const uint8_t * sig,size_t sig_size)23 libfsverity_enable_with_sig(int fd,
24 			    const struct libfsverity_merkle_tree_params *params,
25 			    const uint8_t *sig, size_t sig_size)
26 {
27 	struct fsverity_enable_arg arg = {};
28 
29 	if (!params) {
30 		libfsverity_error_msg("missing required parameters for enable");
31 		return -EINVAL;
32 	}
33 
34 	if (params->version != 1) {
35 		libfsverity_error_msg("unsupported version (%u)",
36 				      params->version);
37 		return -EINVAL;
38 	}
39 
40 	arg.version = 1;
41 	arg.hash_algorithm =
42 		params->hash_algorithm ?: FS_VERITY_HASH_ALG_DEFAULT;
43 	arg.block_size =
44 		params->block_size ?: FS_VERITY_BLOCK_SIZE_DEFAULT;
45 	arg.salt_size = params->salt_size;
46 	arg.salt_ptr = (uintptr_t)params->salt;
47 	arg.sig_size = sig_size;
48 	arg.sig_ptr = (uintptr_t)sig;
49 
50 	if (ioctl(fd, FS_IOC_ENABLE_VERITY, &arg) != 0)
51 		return -errno;
52 	return 0;
53 }
54