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