blob: 998d765ba635e6d77feeb7f26c79e18fd2fbf2ba [file] [log] [blame]
Eric Biggers5cd90ca2020-05-25 13:45:31 -07001/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Private header for libfsverity
4 *
5 * Copyright 2020 Google LLC
6 */
7#ifndef LIB_LIB_PRIVATE_H
8#define LIB_LIB_PRIVATE_H
9
10#include "../common/libfsverity.h"
11#include "../common/common_defs.h"
12#include "../common/fsverity_uapi.h"
13
14#include <stdarg.h>
15
16#define LIBEXPORT __attribute__((visibility("default")))
17
18/* hash_algs.c */
19
20struct fsverity_hash_alg {
21 const char *name;
22 unsigned int digest_size;
23 unsigned int block_size;
24 struct hash_ctx *(*create_ctx)(const struct fsverity_hash_alg *alg);
25};
26
27const struct fsverity_hash_alg *libfsverity_find_hash_alg_by_num(u32 alg_num);
28
29struct hash_ctx {
30 const struct fsverity_hash_alg *alg;
31 void (*init)(struct hash_ctx *ctx);
32 void (*update)(struct hash_ctx *ctx, const void *data, size_t size);
33 void (*final)(struct hash_ctx *ctx, u8 *out);
34 void (*free)(struct hash_ctx *ctx);
35};
36
37void libfsverity_hash_init(struct hash_ctx *ctx);
38void libfsverity_hash_update(struct hash_ctx *ctx, const void *data,
39 size_t size);
40void libfsverity_hash_final(struct hash_ctx *ctx, u8 *digest);
41void libfsverity_hash_full(struct hash_ctx *ctx, const void *data, size_t size,
42 u8 *digest);
43void libfsverity_free_hash_ctx(struct hash_ctx *ctx);
44
45/* utils.c */
46
47void *libfsverity_zalloc(size_t size);
48void *libfsverity_memdup(const void *mem, size_t size);
49
50__cold void
51libfsverity_do_error_msg(const char *format, va_list va, int err);
52
53__printf(1, 2) __cold void
54libfsverity_error_msg(const char *format, ...);
55
56__printf(1, 2) __cold void
57libfsverity_error_msg_errno(const char *format, ...);
58
59__cold void
60libfsverity_warn_on(const char *condition, const char *file, int line);
61
62#define WARN_ON(condition) \
63({ \
64 bool c = (condition); \
65 \
66 if (c) \
67 libfsverity_warn_on(#condition, __FILE__, __LINE__); \
68 c; \
69})
70
71__cold void
72libfsverity_bug_on(const char *condition, const char *file, int line);
73
74#define BUG_ON(condition) \
75({ \
76 bool c = (condition); \
77 \
78 if (c) \
79 libfsverity_bug_on(#condition, __FILE__, __LINE__); \
80 c; \
81})
82
83#endif /* LIB_LIB_PRIVATE_H */