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