• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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);
62 
63 __printf(1, 2) __cold void
64 libfsverity_error_msg(const char *format, ...);
65 
66 __cold void
67 libfsverity_warn_on(const char *condition, const char *file, int line);
68 
69 #define WARN_ON(condition)						\
70 ({									\
71 	bool c = (condition);						\
72 									\
73 	if (c)								\
74 		libfsverity_warn_on(#condition, __FILE__, __LINE__);	\
75 	c;								\
76 })
77 
78 __cold void
79 libfsverity_bug_on(const char *condition, const char *file, int line);
80 
81 #define BUG_ON(condition)						\
82 ({									\
83 	bool c = (condition);						\
84 									\
85 	if (c)								\
86 		libfsverity_bug_on(#condition, __FILE__, __LINE__);	\
87 	c;								\
88 })
89 
90 bool libfsverity_mem_is_zeroed(const void *mem, size_t size);
91 
92 #endif /* LIB_LIB_PRIVATE_H */
93