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 bool cryptographic;
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 #define DEFAULT_HASH_ALG (&fsverity_hash_algs[FS_VERITY_ALG_SHA256])
30
31 /*
32 * Largest digest size among all hash algorithms supported by fs-verity.
33 * This can be increased if needed.
34 */
35 #define FS_VERITY_MAX_DIGEST_SIZE 64
36
hash_create(const struct fsverity_hash_alg * alg)37 static inline struct hash_ctx *hash_create(const struct fsverity_hash_alg *alg)
38 {
39 return alg->create_ctx(alg);
40 }
41
hash_init(struct hash_ctx * ctx)42 static inline void hash_init(struct hash_ctx *ctx)
43 {
44 ctx->init(ctx);
45 }
46
hash_update(struct hash_ctx * ctx,const void * data,size_t size)47 static inline void hash_update(struct hash_ctx *ctx,
48 const void *data, size_t size)
49 {
50 ctx->update(ctx, data, size);
51 }
52
hash_final(struct hash_ctx * ctx,u8 * digest)53 static inline void hash_final(struct hash_ctx *ctx, u8 *digest)
54 {
55 ctx->final(ctx, digest);
56 }
57
hash_free(struct hash_ctx * ctx)58 static inline void hash_free(struct hash_ctx *ctx)
59 {
60 if (ctx)
61 ctx->free(ctx);
62 }
63
64 #endif /* HASH_ALGS_H */
65