1 /* SPDX-License-Identifier: GPL-2.0+ */
2 #ifndef HASH_ALGS_H
3 #define HASH_ALGS_H
4
5 #include <stdio.h>
6
7 #include "util.h"
8
9 struct fsverity_hash_alg {
10 const char *name;
11 unsigned int digest_size;
12 unsigned int block_size;
13 struct hash_ctx *(*create_ctx)(const struct fsverity_hash_alg *alg);
14 };
15
16 extern const struct fsverity_hash_alg fsverity_hash_algs[];
17
18 struct hash_ctx {
19 const struct fsverity_hash_alg *alg;
20 void (*init)(struct hash_ctx *ctx);
21 void (*update)(struct hash_ctx *ctx, const void *data, size_t size);
22 void (*final)(struct hash_ctx *ctx, u8 *out);
23 void (*free)(struct hash_ctx *ctx);
24 };
25
26 const struct fsverity_hash_alg *find_hash_alg_by_name(const char *name);
27 const struct fsverity_hash_alg *find_hash_alg_by_num(unsigned int num);
28 void show_all_hash_algs(FILE *fp);
29
30 /* The hash algorithm that fsverity-utils assumes when none is specified */
31 #define FS_VERITY_HASH_ALG_DEFAULT FS_VERITY_HASH_ALG_SHA256
32
33 /*
34 * Largest digest size among all hash algorithms supported by fs-verity.
35 * This can be increased if needed.
36 */
37 #define FS_VERITY_MAX_DIGEST_SIZE 64
38
hash_create(const struct fsverity_hash_alg * alg)39 static inline struct hash_ctx *hash_create(const struct fsverity_hash_alg *alg)
40 {
41 return alg->create_ctx(alg);
42 }
43
hash_init(struct hash_ctx * ctx)44 static inline void hash_init(struct hash_ctx *ctx)
45 {
46 ctx->init(ctx);
47 }
48
hash_update(struct hash_ctx * ctx,const void * data,size_t size)49 static inline void hash_update(struct hash_ctx *ctx,
50 const void *data, size_t size)
51 {
52 ctx->update(ctx, data, size);
53 }
54
hash_final(struct hash_ctx * ctx,u8 * digest)55 static inline void hash_final(struct hash_ctx *ctx, u8 *digest)
56 {
57 ctx->final(ctx, digest);
58 }
59
hash_free(struct hash_ctx * ctx)60 static inline void hash_free(struct hash_ctx *ctx)
61 {
62 if (ctx)
63 ctx->free(ctx);
64 }
65
66 void hash_full(struct hash_ctx *ctx, const void *data, size_t size, u8 *digest);
67
68 #endif /* HASH_ALGS_H */
69