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