Eric Biggers | 671e67b | 2019-07-22 09:26:21 -0700 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * fs-verity: read-only file-based authenticity protection |
| 4 | * |
| 5 | * Copyright 2019 Google LLC |
| 6 | */ |
| 7 | |
| 8 | #ifndef _FSVERITY_PRIVATE_H |
| 9 | #define _FSVERITY_PRIVATE_H |
| 10 | |
| 11 | #ifdef CONFIG_FS_VERITY_DEBUG |
| 12 | #define DEBUG |
| 13 | #endif |
| 14 | |
| 15 | #define pr_fmt(fmt) "fs-verity: " fmt |
| 16 | |
| 17 | #include <crypto/sha.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <uapi/linux/fsverity.h> |
| 20 | |
| 21 | struct ahash_request; |
| 22 | |
| 23 | /* |
| 24 | * Implementation limit: maximum depth of the Merkle tree. For now 8 is plenty; |
| 25 | * it's enough for over U64_MAX bytes of data using SHA-256 and 4K blocks. |
| 26 | */ |
| 27 | #define FS_VERITY_MAX_LEVELS 8 |
| 28 | |
| 29 | /* |
| 30 | * Largest digest size among all hash algorithms supported by fs-verity. |
| 31 | * Currently assumed to be <= size of fsverity_descriptor::root_hash. |
| 32 | */ |
| 33 | #define FS_VERITY_MAX_DIGEST_SIZE SHA256_DIGEST_SIZE |
| 34 | |
| 35 | /* A hash algorithm supported by fs-verity */ |
| 36 | struct fsverity_hash_alg { |
| 37 | struct crypto_ahash *tfm; /* hash tfm, allocated on demand */ |
| 38 | const char *name; /* crypto API name, e.g. sha256 */ |
| 39 | unsigned int digest_size; /* digest size in bytes, e.g. 32 for SHA-256 */ |
| 40 | unsigned int block_size; /* block size in bytes, e.g. 64 for SHA-256 */ |
| 41 | }; |
| 42 | |
| 43 | /* Merkle tree parameters: hash algorithm, initial hash state, and topology */ |
| 44 | struct merkle_tree_params { |
| 45 | const struct fsverity_hash_alg *hash_alg; /* the hash algorithm */ |
| 46 | const u8 *hashstate; /* initial hash state or NULL */ |
| 47 | unsigned int digest_size; /* same as hash_alg->digest_size */ |
| 48 | unsigned int block_size; /* size of data and tree blocks */ |
| 49 | unsigned int hashes_per_block; /* number of hashes per tree block */ |
| 50 | unsigned int log_blocksize; /* log2(block_size) */ |
| 51 | unsigned int log_arity; /* log2(hashes_per_block) */ |
| 52 | unsigned int num_levels; /* number of levels in Merkle tree */ |
| 53 | u64 tree_size; /* Merkle tree size in bytes */ |
| 54 | |
| 55 | /* |
| 56 | * Starting block index for each tree level, ordered from leaf level (0) |
| 57 | * to root level ('num_levels - 1') |
| 58 | */ |
| 59 | u64 level_start[FS_VERITY_MAX_LEVELS]; |
| 60 | }; |
| 61 | |
| 62 | /* hash_algs.c */ |
| 63 | |
| 64 | extern struct fsverity_hash_alg fsverity_hash_algs[]; |
| 65 | |
| 66 | const struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode, |
| 67 | unsigned int num); |
| 68 | const u8 *fsverity_prepare_hash_state(const struct fsverity_hash_alg *alg, |
| 69 | const u8 *salt, size_t salt_size); |
| 70 | int fsverity_hash_page(const struct merkle_tree_params *params, |
| 71 | const struct inode *inode, |
| 72 | struct ahash_request *req, struct page *page, u8 *out); |
| 73 | int fsverity_hash_buffer(const struct fsverity_hash_alg *alg, |
| 74 | const void *data, size_t size, u8 *out); |
| 75 | void __init fsverity_check_hash_algs(void); |
| 76 | |
| 77 | /* init.c */ |
| 78 | |
| 79 | extern void __printf(3, 4) __cold |
| 80 | fsverity_msg(const struct inode *inode, const char *level, |
| 81 | const char *fmt, ...); |
| 82 | |
| 83 | #define fsverity_warn(inode, fmt, ...) \ |
| 84 | fsverity_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__) |
| 85 | #define fsverity_err(inode, fmt, ...) \ |
| 86 | fsverity_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__) |
| 87 | |
| 88 | #endif /* _FSVERITY_PRIVATE_H */ |