^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright 2019 Google LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #ifndef _INCFS_INTEGRITY_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #define _INCFS_INTEGRITY_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <uapi/linux/incrementalfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define INCFS_MAX_MTREE_LEVELS 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define INCFS_MAX_HASH_AREA_SIZE (1280 * 1024 * 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct incfs_hash_alg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) enum incfs_hash_tree_algorithm id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct crypto_shash *shash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* Merkle tree structure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct mtree {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct incfs_hash_alg *alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) u8 root_hash[INCFS_MAX_HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* Offset of each hash level in the hash area. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) u32 hash_level_suboffset[INCFS_MAX_MTREE_LEVELS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) u32 hash_tree_area_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* Number of levels in hash_level_suboffset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct incfs_hash_alg *incfs_get_hash_alg(enum incfs_hash_tree_algorithm id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct mtree *incfs_alloc_mtree(struct mem_range signature,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int data_block_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) void incfs_free_mtree(struct mtree *tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) size_t incfs_get_mtree_depth(enum incfs_hash_tree_algorithm alg, loff_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) size_t incfs_get_mtree_hash_count(enum incfs_hash_tree_algorithm alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) loff_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int incfs_calc_digest(struct incfs_hash_alg *alg, struct mem_range data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct mem_range digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #endif /* _INCFS_INTEGRITY_H */