1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Private header for libfsverity
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 #ifndef LIB_LIB_PRIVATE_H
12 #define LIB_LIB_PRIVATE_H
13 
14 #include "libfsverity.h"
15 #include "../common/common_defs.h"
16 #include "../common/fsverity_uapi.h"
17 
18 #include <stdarg.h>
19 
20 #define LIBEXPORT	__attribute__((visibility("default")))
21 
22 /* The hash algorithm that libfsverity assumes when none is specified */
23 #define FS_VERITY_HASH_ALG_DEFAULT	FS_VERITY_HASH_ALG_SHA256
24 
25 /* The block size that libfsverity assumes when none is specified */
26 #define FS_VERITY_BLOCK_SIZE_DEFAULT	4096
27 
28 /* hash_algs.c */
29 
30 struct fsverity_hash_alg {
31 	const char *name;
32 	unsigned int digest_size;
33 	unsigned int block_size;
34 	struct hash_ctx *(*create_ctx)(const struct fsverity_hash_alg *alg);
35 };
36 
37 const struct fsverity_hash_alg *libfsverity_find_hash_alg_by_num(u32 alg_num);
38 
39 struct hash_ctx {
40 	const struct fsverity_hash_alg *alg;
41 	void (*init)(struct hash_ctx *ctx);
42 	void (*update)(struct hash_ctx *ctx, const void *data, size_t size);
43 	void (*final)(struct hash_ctx *ctx, u8 *out);
44 	void (*free)(struct hash_ctx *ctx);
45 };
46 
47 void libfsverity_hash_init(struct hash_ctx *ctx);
48 void libfsverity_hash_update(struct hash_ctx *ctx, const void *data,
49 			     size_t size);
50 void libfsverity_hash_final(struct hash_ctx *ctx, u8 *digest);
51 void libfsverity_hash_full(struct hash_ctx *ctx, const void *data, size_t size,
52 			   u8 *digest);
53 void libfsverity_free_hash_ctx(struct hash_ctx *ctx);
54 
55 /* utils.c */
56 
57 void *libfsverity_zalloc(size_t size);
58 void *libfsverity_memdup(const void *mem, size_t size);
59 
60 __cold void
61 libfsverity_do_error_msg(const char *format, va_list va, int err);
62 
63 __printf(1, 2) __cold void
64 libfsverity_error_msg(const char *format, ...);
65 
66 __printf(1, 2) __cold void
67 libfsverity_error_msg_errno(const char *format, ...);
68 
69 __cold void
70 libfsverity_warn_on(const char *condition, const char *file, int line);
71 
72 #define WARN_ON(condition)						\
73 ({									\
74 	bool c = (condition);						\
75 									\
76 	if (c)								\
77 		libfsverity_warn_on(#condition, __FILE__, __LINE__);	\
78 	c;								\
79 })
80 
81 __cold void
82 libfsverity_bug_on(const char *condition, const char *file, int line);
83 
84 #define BUG_ON(condition)						\
85 ({									\
86 	bool c = (condition);						\
87 									\
88 	if (c)								\
89 		libfsverity_bug_on(#condition, __FILE__, __LINE__);	\
90 	c;								\
91 })
92 
93 bool libfsverity_mem_is_zeroed(const void *mem, size_t size);
94 
95 #endif /* LIB_LIB_PRIVATE_H */
96