xref: /openbmc/linux/fs/crypto/fscrypt_private.h (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
23325bea5STheodore Ts'o /*
33325bea5STheodore Ts'o  * fscrypt_private.h
43325bea5STheodore Ts'o  *
53325bea5STheodore Ts'o  * Copyright (C) 2015, Google, Inc.
63325bea5STheodore Ts'o  *
73ec4f2a6SEric Biggers  * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
83ec4f2a6SEric Biggers  * Heavily modified since then.
93325bea5STheodore Ts'o  */
103325bea5STheodore Ts'o 
113325bea5STheodore Ts'o #ifndef _FSCRYPT_PRIVATE_H
123325bea5STheodore Ts'o #define _FSCRYPT_PRIVATE_H
133325bea5STheodore Ts'o 
14734f0d24SDave Chinner #include <linux/fscrypt.h>
15aa408f83SDaniel Rosenberg #include <linux/siphash.h>
16b7e7cf7aSDaniel Walter #include <crypto/hash.h>
175fee3609SSatya Tangirala #include <linux/blk-crypto.h>
183325bea5STheodore Ts'o 
1922d94f49SEric Biggers #define CONST_STRLEN(str)	(sizeof(str) - 1)
2022d94f49SEric Biggers 
211d6217a4SEric Biggers #define FSCRYPT_FILE_NONCE_SIZE	16
22cc4e0df0STheodore Ts'o 
23b7e072f9SEric Biggers /*
24b7e072f9SEric Biggers  * Minimum size of an fscrypt master key.  Note: a longer key will be required
25b7e072f9SEric Biggers  * if ciphers with a 256-bit security strength are used.  This is just the
26b7e072f9SEric Biggers  * absolute minimum, which applies when only 128-bit encryption is used.
27b7e072f9SEric Biggers  */
2822d94f49SEric Biggers #define FSCRYPT_MIN_KEY_SIZE	16
2922d94f49SEric Biggers 
305dae460cSEric Biggers #define FSCRYPT_CONTEXT_V1	1
315dae460cSEric Biggers #define FSCRYPT_CONTEXT_V2	2
325dae460cSEric Biggers 
333ceb6543SEric Biggers /* Keep this in sync with include/uapi/linux/fscrypt.h */
346b2a51ffSNathan Huckleberry #define FSCRYPT_MODE_MAX	FSCRYPT_MODE_AES_256_HCTR2
353ceb6543SEric Biggers 
365dae460cSEric Biggers struct fscrypt_context_v1 {
375dae460cSEric Biggers 	u8 version; /* FSCRYPT_CONTEXT_V1 */
38cc4e0df0STheodore Ts'o 	u8 contents_encryption_mode;
39cc4e0df0STheodore Ts'o 	u8 filenames_encryption_mode;
40cc4e0df0STheodore Ts'o 	u8 flags;
413b6df59bSEric Biggers 	u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
421d6217a4SEric Biggers 	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
435dae460cSEric Biggers };
44cc4e0df0STheodore Ts'o 
455dae460cSEric Biggers struct fscrypt_context_v2 {
465dae460cSEric Biggers 	u8 version; /* FSCRYPT_CONTEXT_V2 */
475dae460cSEric Biggers 	u8 contents_encryption_mode;
485dae460cSEric Biggers 	u8 filenames_encryption_mode;
495dae460cSEric Biggers 	u8 flags;
505dae460cSEric Biggers 	u8 __reserved[4];
515dae460cSEric Biggers 	u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
521d6217a4SEric Biggers 	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
535dae460cSEric Biggers };
545dae460cSEric Biggers 
55d2fe9754SEric Biggers /*
565dae460cSEric Biggers  * fscrypt_context - the encryption context of an inode
575dae460cSEric Biggers  *
585dae460cSEric Biggers  * This is the on-disk equivalent of an fscrypt_policy, stored alongside each
595dae460cSEric Biggers  * encrypted file usually in a hidden extended attribute.  It contains the
605dae460cSEric Biggers  * fields from the fscrypt_policy, in order to identify the encryption algorithm
615dae460cSEric Biggers  * and key with which the file is encrypted.  It also contains a nonce that was
625dae460cSEric Biggers  * randomly generated by fscrypt itself; this is used as KDF input or as a tweak
635dae460cSEric Biggers  * to cause different files to be encrypted differently.
645dae460cSEric Biggers  */
655dae460cSEric Biggers union fscrypt_context {
665dae460cSEric Biggers 	u8 version;
675dae460cSEric Biggers 	struct fscrypt_context_v1 v1;
685dae460cSEric Biggers 	struct fscrypt_context_v2 v2;
695dae460cSEric Biggers };
705dae460cSEric Biggers 
715dae460cSEric Biggers /*
725dae460cSEric Biggers  * Return the size expected for the given fscrypt_context based on its version
735dae460cSEric Biggers  * number, or 0 if the context version is unrecognized.
745dae460cSEric Biggers  */
fscrypt_context_size(const union fscrypt_context * ctx)755dae460cSEric Biggers static inline int fscrypt_context_size(const union fscrypt_context *ctx)
765dae460cSEric Biggers {
775dae460cSEric Biggers 	switch (ctx->version) {
785dae460cSEric Biggers 	case FSCRYPT_CONTEXT_V1:
795dae460cSEric Biggers 		BUILD_BUG_ON(sizeof(ctx->v1) != 28);
805dae460cSEric Biggers 		return sizeof(ctx->v1);
815dae460cSEric Biggers 	case FSCRYPT_CONTEXT_V2:
825dae460cSEric Biggers 		BUILD_BUG_ON(sizeof(ctx->v2) != 40);
835dae460cSEric Biggers 		return sizeof(ctx->v2);
845dae460cSEric Biggers 	}
855dae460cSEric Biggers 	return 0;
865dae460cSEric Biggers }
875dae460cSEric Biggers 
88e98ad464SEric Biggers /* Check whether an fscrypt_context has a recognized version number and size */
fscrypt_context_is_valid(const union fscrypt_context * ctx,int ctx_size)89e98ad464SEric Biggers static inline bool fscrypt_context_is_valid(const union fscrypt_context *ctx,
90e98ad464SEric Biggers 					    int ctx_size)
91e98ad464SEric Biggers {
92e98ad464SEric Biggers 	return ctx_size >= 1 && ctx_size == fscrypt_context_size(ctx);
93e98ad464SEric Biggers }
94e98ad464SEric Biggers 
95e98ad464SEric Biggers /* Retrieve the context's nonce, assuming the context was already validated */
fscrypt_context_nonce(const union fscrypt_context * ctx)96e98ad464SEric Biggers static inline const u8 *fscrypt_context_nonce(const union fscrypt_context *ctx)
97e98ad464SEric Biggers {
98e98ad464SEric Biggers 	switch (ctx->version) {
99e98ad464SEric Biggers 	case FSCRYPT_CONTEXT_V1:
100e98ad464SEric Biggers 		return ctx->v1.nonce;
101e98ad464SEric Biggers 	case FSCRYPT_CONTEXT_V2:
102e98ad464SEric Biggers 		return ctx->v2.nonce;
103e98ad464SEric Biggers 	}
10441b2ad80SEric Biggers 	WARN_ON_ONCE(1);
105e98ad464SEric Biggers 	return NULL;
106e98ad464SEric Biggers }
107e98ad464SEric Biggers 
1085dae460cSEric Biggers union fscrypt_policy {
1095dae460cSEric Biggers 	u8 version;
1105dae460cSEric Biggers 	struct fscrypt_policy_v1 v1;
1115dae460cSEric Biggers 	struct fscrypt_policy_v2 v2;
1125dae460cSEric Biggers };
1135dae460cSEric Biggers 
1145dae460cSEric Biggers /*
1155dae460cSEric Biggers  * Return the size expected for the given fscrypt_policy based on its version
1165dae460cSEric Biggers  * number, or 0 if the policy version is unrecognized.
1175dae460cSEric Biggers  */
fscrypt_policy_size(const union fscrypt_policy * policy)1185dae460cSEric Biggers static inline int fscrypt_policy_size(const union fscrypt_policy *policy)
1195dae460cSEric Biggers {
1205dae460cSEric Biggers 	switch (policy->version) {
1215dae460cSEric Biggers 	case FSCRYPT_POLICY_V1:
1225dae460cSEric Biggers 		return sizeof(policy->v1);
1235dae460cSEric Biggers 	case FSCRYPT_POLICY_V2:
1245dae460cSEric Biggers 		return sizeof(policy->v2);
1255dae460cSEric Biggers 	}
1265dae460cSEric Biggers 	return 0;
1275dae460cSEric Biggers }
1285dae460cSEric Biggers 
1295dae460cSEric Biggers /* Return the contents encryption mode of a valid encryption policy */
1305dae460cSEric Biggers static inline u8
fscrypt_policy_contents_mode(const union fscrypt_policy * policy)1315dae460cSEric Biggers fscrypt_policy_contents_mode(const union fscrypt_policy *policy)
1325dae460cSEric Biggers {
1335dae460cSEric Biggers 	switch (policy->version) {
1345dae460cSEric Biggers 	case FSCRYPT_POLICY_V1:
1355dae460cSEric Biggers 		return policy->v1.contents_encryption_mode;
1365dae460cSEric Biggers 	case FSCRYPT_POLICY_V2:
1375dae460cSEric Biggers 		return policy->v2.contents_encryption_mode;
1385dae460cSEric Biggers 	}
1395dae460cSEric Biggers 	BUG();
1405dae460cSEric Biggers }
1415dae460cSEric Biggers 
1425dae460cSEric Biggers /* Return the filenames encryption mode of a valid encryption policy */
1435dae460cSEric Biggers static inline u8
fscrypt_policy_fnames_mode(const union fscrypt_policy * policy)1445dae460cSEric Biggers fscrypt_policy_fnames_mode(const union fscrypt_policy *policy)
1455dae460cSEric Biggers {
1465dae460cSEric Biggers 	switch (policy->version) {
1475dae460cSEric Biggers 	case FSCRYPT_POLICY_V1:
1485dae460cSEric Biggers 		return policy->v1.filenames_encryption_mode;
1495dae460cSEric Biggers 	case FSCRYPT_POLICY_V2:
1505dae460cSEric Biggers 		return policy->v2.filenames_encryption_mode;
1515dae460cSEric Biggers 	}
1525dae460cSEric Biggers 	BUG();
1535dae460cSEric Biggers }
1545dae460cSEric Biggers 
1555dae460cSEric Biggers /* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */
1565dae460cSEric Biggers static inline u8
fscrypt_policy_flags(const union fscrypt_policy * policy)1575dae460cSEric Biggers fscrypt_policy_flags(const union fscrypt_policy *policy)
1585dae460cSEric Biggers {
1595dae460cSEric Biggers 	switch (policy->version) {
1605dae460cSEric Biggers 	case FSCRYPT_POLICY_V1:
1615dae460cSEric Biggers 		return policy->v1.flags;
1625dae460cSEric Biggers 	case FSCRYPT_POLICY_V2:
1635dae460cSEric Biggers 		return policy->v2.flags;
1645dae460cSEric Biggers 	}
1655dae460cSEric Biggers 	BUG();
1665dae460cSEric Biggers }
1675dae460cSEric Biggers 
168d2fe9754SEric Biggers /*
1690eaab5b1SEric Biggers  * For encrypted symlinks, the ciphertext length is stored at the beginning
1700eaab5b1SEric Biggers  * of the string in little-endian format.
1710eaab5b1SEric Biggers  */
1720eaab5b1SEric Biggers struct fscrypt_symlink_data {
1730eaab5b1SEric Biggers 	__le16 len;
1740eaab5b1SEric Biggers 	char encrypted_path[];
1750eaab5b1SEric Biggers } __packed;
1760eaab5b1SEric Biggers 
1775fee3609SSatya Tangirala /**
1785fee3609SSatya Tangirala  * struct fscrypt_prepared_key - a key prepared for actual encryption/decryption
1795fee3609SSatya Tangirala  * @tfm: crypto API transform object
1805fee3609SSatya Tangirala  * @blk_key: key for blk-crypto
1815fee3609SSatya Tangirala  *
1825fee3609SSatya Tangirala  * Normally only one of the fields will be non-NULL.
1835fee3609SSatya Tangirala  */
1845fee3609SSatya Tangirala struct fscrypt_prepared_key {
1855fee3609SSatya Tangirala 	struct crypto_skcipher *tfm;
1865fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
18722e9947aSEric Biggers 	struct blk_crypto_key *blk_key;
1885fee3609SSatya Tangirala #endif
1895fee3609SSatya Tangirala };
1905fee3609SSatya Tangirala 
191cc4e0df0STheodore Ts'o /*
1928094c3ceSEric Biggers  * fscrypt_info - the "encryption key" for an inode
1938094c3ceSEric Biggers  *
1948094c3ceSEric Biggers  * When an encrypted file's key is made available, an instance of this struct is
1958094c3ceSEric Biggers  * allocated and stored in ->i_crypt_info.  Once created, it remains until the
1968094c3ceSEric Biggers  * inode is evicted.
197cc4e0df0STheodore Ts'o  */
198cc4e0df0STheodore Ts'o struct fscrypt_info {
1998094c3ceSEric Biggers 
2005fee3609SSatya Tangirala 	/* The key in a form prepared for actual encryption/decryption */
2015fee3609SSatya Tangirala 	struct fscrypt_prepared_key ci_enc_key;
2028094c3ceSEric Biggers 
2035fee3609SSatya Tangirala 	/* True if ci_enc_key should be freed when this fscrypt_info is freed */
204b103fb76SEric Biggers 	bool ci_owns_key;
205b103fb76SEric Biggers 
2065fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
2075fee3609SSatya Tangirala 	/*
2085fee3609SSatya Tangirala 	 * True if this inode will use inline encryption (blk-crypto) instead of
2095fee3609SSatya Tangirala 	 * the traditional filesystem-layer encryption.
2105fee3609SSatya Tangirala 	 */
2115fee3609SSatya Tangirala 	bool ci_inlinecrypt;
2125fee3609SSatya Tangirala #endif
2135fee3609SSatya Tangirala 
2148094c3ceSEric Biggers 	/*
2155dae460cSEric Biggers 	 * Encryption mode used for this inode.  It corresponds to either the
2165dae460cSEric Biggers 	 * contents or filenames encryption mode, depending on the inode type.
2178094c3ceSEric Biggers 	 */
2188094c3ceSEric Biggers 	struct fscrypt_mode *ci_mode;
2198094c3ceSEric Biggers 
22059dc6a8eSEric Biggers 	/* Back-pointer to the inode */
22159dc6a8eSEric Biggers 	struct inode *ci_inode;
22259dc6a8eSEric Biggers 
2238094c3ceSEric Biggers 	/*
224b1c0ec35SEric Biggers 	 * The master key with which this inode was unlocked (decrypted).  This
225b1c0ec35SEric Biggers 	 * will be NULL if the master key was found in a process-subscribed
226b1c0ec35SEric Biggers 	 * keyring rather than in the filesystem-level keyring.
227b1c0ec35SEric Biggers 	 */
228d7e7b9afSEric Biggers 	struct fscrypt_master_key *ci_master_key;
229b1c0ec35SEric Biggers 
230b1c0ec35SEric Biggers 	/*
231b1c0ec35SEric Biggers 	 * Link in list of inodes that were unlocked with the master key.
232b1c0ec35SEric Biggers 	 * Only used when ->ci_master_key is set.
233b1c0ec35SEric Biggers 	 */
234b1c0ec35SEric Biggers 	struct list_head ci_master_key_link;
235b1c0ec35SEric Biggers 
236b1c0ec35SEric Biggers 	/*
237a828daabSEric Biggers 	 * If non-NULL, then encryption is done using the master key directly
2385fee3609SSatya Tangirala 	 * and ci_enc_key will equal ci_direct_key->dk_key.
2398094c3ceSEric Biggers 	 */
240a828daabSEric Biggers 	struct fscrypt_direct_key *ci_direct_key;
2418094c3ceSEric Biggers 
242aa408f83SDaniel Rosenberg 	/*
243aa408f83SDaniel Rosenberg 	 * This inode's hash key for filenames.  This is a 128-bit SipHash-2-4
244aa408f83SDaniel Rosenberg 	 * key.  This is only set for directories that use a keyed dirhash over
245aa408f83SDaniel Rosenberg 	 * the plaintext filenames -- currently just casefolded directories.
246aa408f83SDaniel Rosenberg 	 */
247aa408f83SDaniel Rosenberg 	siphash_key_t ci_dirhash_key;
248aa408f83SDaniel Rosenberg 	bool ci_dirhash_key_initialized;
249aa408f83SDaniel Rosenberg 
2505dae460cSEric Biggers 	/* The encryption policy used by this inode */
2515dae460cSEric Biggers 	union fscrypt_policy ci_policy;
2525dae460cSEric Biggers 
2535dae460cSEric Biggers 	/* This inode's nonce, copied from the fscrypt_context */
2541d6217a4SEric Biggers 	u8 ci_nonce[FSCRYPT_FILE_NONCE_SIZE];
255e3b1078bSEric Biggers 
256e3b1078bSEric Biggers 	/* Hashed inode number.  Only set for IV_INO_LBLK_32 */
257e3b1078bSEric Biggers 	u32 ci_hashed_ino;
258cc4e0df0STheodore Ts'o };
259cc4e0df0STheodore Ts'o 
26058ae7468SRichard Weinberger typedef enum {
26158ae7468SRichard Weinberger 	FS_DECRYPT = 0,
26258ae7468SRichard Weinberger 	FS_ENCRYPT,
26358ae7468SRichard Weinberger } fscrypt_direction_t;
26458ae7468SRichard Weinberger 
265b98701dfSTheodore Ts'o /* crypto.c */
266e4de782aSEric Biggers extern struct kmem_cache *fscrypt_info_cachep;
267*83e57e47SEric Biggers int fscrypt_initialize(struct super_block *sb);
26860700902SEric Biggers int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw,
26960700902SEric Biggers 			u64 lblk_num, struct page *src_page,
27060700902SEric Biggers 			struct page *dest_page, unsigned int len,
27160700902SEric Biggers 			unsigned int offs, gfp_t gfp_flags);
27260700902SEric Biggers struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags);
273b98701dfSTheodore Ts'o 
27460700902SEric Biggers void __printf(3, 4) __cold
275886da8b3SEric Biggers fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...);
276544d08fdSEric Biggers 
277886da8b3SEric Biggers #define fscrypt_warn(inode, fmt, ...)		\
278886da8b3SEric Biggers 	fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
279886da8b3SEric Biggers #define fscrypt_err(inode, fmt, ...)		\
280886da8b3SEric Biggers 	fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
281544d08fdSEric Biggers 
2828094c3ceSEric Biggers #define FSCRYPT_MAX_IV_SIZE	32
2838094c3ceSEric Biggers 
2848094c3ceSEric Biggers union fscrypt_iv {
2858094c3ceSEric Biggers 	struct {
2868094c3ceSEric Biggers 		/* logical block number within the file */
2878094c3ceSEric Biggers 		__le64 lblk_num;
2888094c3ceSEric Biggers 
2898094c3ceSEric Biggers 		/* per-file nonce; only set in DIRECT_KEY mode */
2901d6217a4SEric Biggers 		u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
2918094c3ceSEric Biggers 	};
2928094c3ceSEric Biggers 	u8 raw[FSCRYPT_MAX_IV_SIZE];
2935fee3609SSatya Tangirala 	__le64 dun[FSCRYPT_MAX_IV_SIZE / sizeof(__le64)];
2948094c3ceSEric Biggers };
2958094c3ceSEric Biggers 
2968094c3ceSEric Biggers void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
2978094c3ceSEric Biggers 			 const struct fscrypt_info *ci);
2988094c3ceSEric Biggers 
29976e81d6dSEric Biggers /* fname.c */
300d3e94fdcSJeff Layton bool __fscrypt_fname_encrypted_size(const union fscrypt_policy *policy,
301ac4acb1fSEric Biggers 				    u32 orig_len, u32 max_len,
302ac4acb1fSEric Biggers 				    u32 *encrypted_len_ret);
30376e81d6dSEric Biggers 
304c1144c9bSEric Biggers /* hkdf.c */
305c1144c9bSEric Biggers struct fscrypt_hkdf {
306c1144c9bSEric Biggers 	struct crypto_shash *hmac_tfm;
307c1144c9bSEric Biggers };
308c1144c9bSEric Biggers 
30960700902SEric Biggers int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
310c1144c9bSEric Biggers 		      unsigned int master_key_size);
311c1144c9bSEric Biggers 
3125dae460cSEric Biggers /*
3135dae460cSEric Biggers  * The list of contexts in which fscrypt uses HKDF.  These values are used as
3145dae460cSEric Biggers  * the first byte of the HKDF application-specific info string to guarantee that
3155dae460cSEric Biggers  * info strings are never repeated between contexts.  This ensures that all HKDF
3165dae460cSEric Biggers  * outputs are unique and cryptographically isolated, i.e. knowledge of one
3175dae460cSEric Biggers  * output doesn't reveal another.
3185dae460cSEric Biggers  */
319e455de31SEric Biggers #define HKDF_CONTEXT_KEY_IDENTIFIER	1 /* info=<empty>		*/
320e455de31SEric Biggers #define HKDF_CONTEXT_PER_FILE_ENC_KEY	2 /* info=file_nonce		*/
321e455de31SEric Biggers #define HKDF_CONTEXT_DIRECT_KEY		3 /* info=mode_num		*/
322e455de31SEric Biggers #define HKDF_CONTEXT_IV_INO_LBLK_64_KEY	4 /* info=mode_num||fs_uuid	*/
323e455de31SEric Biggers #define HKDF_CONTEXT_DIRHASH_KEY	5 /* info=file_nonce		*/
324e455de31SEric Biggers #define HKDF_CONTEXT_IV_INO_LBLK_32_KEY	6 /* info=mode_num||fs_uuid	*/
325e455de31SEric Biggers #define HKDF_CONTEXT_INODE_HASH_KEY	7 /* info=<empty>		*/
3265dae460cSEric Biggers 
32760700902SEric Biggers int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
328c1144c9bSEric Biggers 			const u8 *info, unsigned int infolen,
329c1144c9bSEric Biggers 			u8 *okm, unsigned int okmlen);
330c1144c9bSEric Biggers 
33160700902SEric Biggers void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf);
332c1144c9bSEric Biggers 
3335fee3609SSatya Tangirala /* inline_crypt.c */
3345fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
3355fee3609SSatya Tangirala int fscrypt_select_encryption_impl(struct fscrypt_info *ci);
3365fee3609SSatya Tangirala 
3375fee3609SSatya Tangirala static inline bool
fscrypt_using_inline_encryption(const struct fscrypt_info * ci)3385fee3609SSatya Tangirala fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
3395fee3609SSatya Tangirala {
3405fee3609SSatya Tangirala 	return ci->ci_inlinecrypt;
3415fee3609SSatya Tangirala }
3425fee3609SSatya Tangirala 
3435fee3609SSatya Tangirala int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
3445fee3609SSatya Tangirala 				     const u8 *raw_key,
3455fee3609SSatya Tangirala 				     const struct fscrypt_info *ci);
3465fee3609SSatya Tangirala 
34722e9947aSEric Biggers void fscrypt_destroy_inline_crypt_key(struct super_block *sb,
34822e9947aSEric Biggers 				      struct fscrypt_prepared_key *prep_key);
3495fee3609SSatya Tangirala 
3505fee3609SSatya Tangirala /*
3515fee3609SSatya Tangirala  * Check whether the crypto transform or blk-crypto key has been allocated in
3525fee3609SSatya Tangirala  * @prep_key, depending on which encryption implementation the file will use.
3535fee3609SSatya Tangirala  */
3545fee3609SSatya Tangirala static inline bool
fscrypt_is_key_prepared(struct fscrypt_prepared_key * prep_key,const struct fscrypt_info * ci)3555fee3609SSatya Tangirala fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
3565fee3609SSatya Tangirala 			const struct fscrypt_info *ci)
3575fee3609SSatya Tangirala {
3585fee3609SSatya Tangirala 	/*
35997c6327fSEric Biggers 	 * The two smp_load_acquire()'s here pair with the smp_store_release()'s
36097c6327fSEric Biggers 	 * in fscrypt_prepare_inline_crypt_key() and fscrypt_prepare_key().
36197c6327fSEric Biggers 	 * I.e., in some cases (namely, if this prep_key is a per-mode
36297c6327fSEric Biggers 	 * encryption key) another task can publish blk_key or tfm concurrently,
36397c6327fSEric Biggers 	 * executing a RELEASE barrier.  We need to use smp_load_acquire() here
36497c6327fSEric Biggers 	 * to safely ACQUIRE the memory the other task published.
3655fee3609SSatya Tangirala 	 */
3665fee3609SSatya Tangirala 	if (fscrypt_using_inline_encryption(ci))
36797c6327fSEric Biggers 		return smp_load_acquire(&prep_key->blk_key) != NULL;
36897c6327fSEric Biggers 	return smp_load_acquire(&prep_key->tfm) != NULL;
3695fee3609SSatya Tangirala }
3705fee3609SSatya Tangirala 
3715fee3609SSatya Tangirala #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
3725fee3609SSatya Tangirala 
fscrypt_select_encryption_impl(struct fscrypt_info * ci)3735fee3609SSatya Tangirala static inline int fscrypt_select_encryption_impl(struct fscrypt_info *ci)
3745fee3609SSatya Tangirala {
3755fee3609SSatya Tangirala 	return 0;
3765fee3609SSatya Tangirala }
3775fee3609SSatya Tangirala 
3785fee3609SSatya Tangirala static inline bool
fscrypt_using_inline_encryption(const struct fscrypt_info * ci)3795fee3609SSatya Tangirala fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
3805fee3609SSatya Tangirala {
3815fee3609SSatya Tangirala 	return false;
3825fee3609SSatya Tangirala }
3835fee3609SSatya Tangirala 
3845fee3609SSatya Tangirala static inline int
fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key * prep_key,const u8 * raw_key,const struct fscrypt_info * ci)3855fee3609SSatya Tangirala fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
3865fee3609SSatya Tangirala 				 const u8 *raw_key,
3875fee3609SSatya Tangirala 				 const struct fscrypt_info *ci)
3885fee3609SSatya Tangirala {
38941b2ad80SEric Biggers 	WARN_ON_ONCE(1);
3905fee3609SSatya Tangirala 	return -EOPNOTSUPP;
3915fee3609SSatya Tangirala }
3925fee3609SSatya Tangirala 
3935fee3609SSatya Tangirala static inline void
fscrypt_destroy_inline_crypt_key(struct super_block * sb,struct fscrypt_prepared_key * prep_key)39422e9947aSEric Biggers fscrypt_destroy_inline_crypt_key(struct super_block *sb,
39522e9947aSEric Biggers 				 struct fscrypt_prepared_key *prep_key)
3965fee3609SSatya Tangirala {
3975fee3609SSatya Tangirala }
3985fee3609SSatya Tangirala 
3995fee3609SSatya Tangirala static inline bool
fscrypt_is_key_prepared(struct fscrypt_prepared_key * prep_key,const struct fscrypt_info * ci)4005fee3609SSatya Tangirala fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
4015fee3609SSatya Tangirala 			const struct fscrypt_info *ci)
4025fee3609SSatya Tangirala {
40397c6327fSEric Biggers 	return smp_load_acquire(&prep_key->tfm) != NULL;
4045fee3609SSatya Tangirala }
4055fee3609SSatya Tangirala #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
4065fee3609SSatya Tangirala 
40722d94f49SEric Biggers /* keyring.c */
40822d94f49SEric Biggers 
40922d94f49SEric Biggers /*
41022d94f49SEric Biggers  * fscrypt_master_key_secret - secret key material of an in-use master key
41122d94f49SEric Biggers  */
41222d94f49SEric Biggers struct fscrypt_master_key_secret {
41322d94f49SEric Biggers 
4145dae460cSEric Biggers 	/*
4155dae460cSEric Biggers 	 * For v2 policy keys: HKDF context keyed by this master key.
4165dae460cSEric Biggers 	 * For v1 policy keys: not set (hkdf.hmac_tfm == NULL).
4175dae460cSEric Biggers 	 */
4185dae460cSEric Biggers 	struct fscrypt_hkdf	hkdf;
4195dae460cSEric Biggers 
420b7e072f9SEric Biggers 	/*
421b7e072f9SEric Biggers 	 * Size of the raw key in bytes.  This remains set even if ->raw was
422b7e072f9SEric Biggers 	 * zeroized due to no longer being needed.  I.e. we still remember the
423b7e072f9SEric Biggers 	 * size of the key even if we don't need to remember the key itself.
424b7e072f9SEric Biggers 	 */
42522d94f49SEric Biggers 	u32			size;
42622d94f49SEric Biggers 
4275dae460cSEric Biggers 	/* For v1 policy keys: the raw key.  Wiped for v2 policy keys. */
42822d94f49SEric Biggers 	u8			raw[FSCRYPT_MAX_KEY_SIZE];
42922d94f49SEric Biggers 
43022d94f49SEric Biggers } __randomize_layout;
43122d94f49SEric Biggers 
43222d94f49SEric Biggers /*
43322d94f49SEric Biggers  * fscrypt_master_key - an in-use master key
43422d94f49SEric Biggers  *
43522d94f49SEric Biggers  * This represents a master encryption key which has been added to the
43622d94f49SEric Biggers  * filesystem and can be used to "unlock" the encrypted files which were
43722d94f49SEric Biggers  * encrypted with it.
43822d94f49SEric Biggers  */
43922d94f49SEric Biggers struct fscrypt_master_key {
44022d94f49SEric Biggers 
441b1c0ec35SEric Biggers 	/*
44202aef422SEric Biggers 	 * Link in ->s_master_keys->key_hashtable.
443d7e7b9afSEric Biggers 	 * Only valid if ->mk_active_refs > 0.
444d7e7b9afSEric Biggers 	 */
445d7e7b9afSEric Biggers 	struct hlist_node			mk_node;
446d7e7b9afSEric Biggers 
447d7e7b9afSEric Biggers 	/* Semaphore that protects ->mk_secret and ->mk_users */
448d7e7b9afSEric Biggers 	struct rw_semaphore			mk_sem;
449d7e7b9afSEric Biggers 
450d7e7b9afSEric Biggers 	/*
451d7e7b9afSEric Biggers 	 * Active and structural reference counts.  An active ref guarantees
452d7e7b9afSEric Biggers 	 * that the struct continues to exist, continues to be in the keyring
45302aef422SEric Biggers 	 * ->s_master_keys, and that any embedded subkeys (e.g.
454d7e7b9afSEric Biggers 	 * ->mk_direct_keys) that have been prepared continue to exist.
455d7e7b9afSEric Biggers 	 * A structural ref only guarantees that the struct continues to exist.
456d7e7b9afSEric Biggers 	 *
457d7e7b9afSEric Biggers 	 * There is one active ref associated with ->mk_secret being present,
458d7e7b9afSEric Biggers 	 * and one active ref for each inode in ->mk_decrypted_inodes.
459d7e7b9afSEric Biggers 	 *
460d7e7b9afSEric Biggers 	 * There is one structural ref associated with the active refcount being
461d7e7b9afSEric Biggers 	 * nonzero.  Finding a key in the keyring also takes a structural ref,
462d7e7b9afSEric Biggers 	 * which is then held temporarily while the key is operated on.
463d7e7b9afSEric Biggers 	 */
464d7e7b9afSEric Biggers 	refcount_t				mk_active_refs;
465d7e7b9afSEric Biggers 	refcount_t				mk_struct_refs;
466d7e7b9afSEric Biggers 
467d7e7b9afSEric Biggers 	struct rcu_head				mk_rcu_head;
468d7e7b9afSEric Biggers 
469d7e7b9afSEric Biggers 	/*
470b1c0ec35SEric Biggers 	 * The secret key material.  After FS_IOC_REMOVE_ENCRYPTION_KEY is
471b1c0ec35SEric Biggers 	 * executed, this is wiped and no new inodes can be unlocked with this
472b1c0ec35SEric Biggers 	 * key; however, there may still be inodes in ->mk_decrypted_inodes
473b1c0ec35SEric Biggers 	 * which could not be evicted.  As long as some inodes still remain,
474b1c0ec35SEric Biggers 	 * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or
475b1c0ec35SEric Biggers 	 * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again.
476b1c0ec35SEric Biggers 	 *
477d7e7b9afSEric Biggers 	 * While ->mk_secret is present, one ref in ->mk_active_refs is held.
478d7e7b9afSEric Biggers 	 *
479d7e7b9afSEric Biggers 	 * Locking: protected by ->mk_sem.  The manipulation of ->mk_active_refs
480d7e7b9afSEric Biggers 	 *	    associated with this field is protected by ->mk_sem as well.
481b1c0ec35SEric Biggers 	 */
48222d94f49SEric Biggers 	struct fscrypt_master_key_secret	mk_secret;
48322d94f49SEric Biggers 
4845dae460cSEric Biggers 	/*
4855dae460cSEric Biggers 	 * For v1 policy keys: an arbitrary key descriptor which was assigned by
4865dae460cSEric Biggers 	 * userspace (->descriptor).
4875dae460cSEric Biggers 	 *
4885dae460cSEric Biggers 	 * For v2 policy keys: a cryptographic hash of this key (->identifier).
4895dae460cSEric Biggers 	 */
49022d94f49SEric Biggers 	struct fscrypt_key_specifier		mk_spec;
49122d94f49SEric Biggers 
492b1c0ec35SEric Biggers 	/*
49323c688b5SEric Biggers 	 * Keyring which contains a key of type 'key_type_fscrypt_user' for each
49423c688b5SEric Biggers 	 * user who has added this key.  Normally each key will be added by just
49523c688b5SEric Biggers 	 * one user, but it's possible that multiple users share a key, and in
49623c688b5SEric Biggers 	 * that case we need to keep track of those users so that one user can't
49723c688b5SEric Biggers 	 * remove the key before the others want it removed too.
49823c688b5SEric Biggers 	 *
49923c688b5SEric Biggers 	 * This is NULL for v1 policy keys; those can only be added by root.
50023c688b5SEric Biggers 	 *
501d7e7b9afSEric Biggers 	 * Locking: protected by ->mk_sem.  (We don't just rely on the keyrings
502d7e7b9afSEric Biggers 	 * subsystem semaphore ->mk_users->sem, as we need support for atomic
503d7e7b9afSEric Biggers 	 * search+insert along with proper synchronization with ->mk_secret.)
50423c688b5SEric Biggers 	 */
50523c688b5SEric Biggers 	struct key		*mk_users;
50623c688b5SEric Biggers 
50723c688b5SEric Biggers 	/*
508b1c0ec35SEric Biggers 	 * List of inodes that were unlocked using this key.  This allows the
509b1c0ec35SEric Biggers 	 * inodes to be evicted efficiently if the key is removed.
510b1c0ec35SEric Biggers 	 */
511b1c0ec35SEric Biggers 	struct list_head	mk_decrypted_inodes;
512b1c0ec35SEric Biggers 	spinlock_t		mk_decrypted_inodes_lock;
513b1c0ec35SEric Biggers 
514b103fb76SEric Biggers 	/*
515e3b1078bSEric Biggers 	 * Per-mode encryption keys for the various types of encryption policies
516e3b1078bSEric Biggers 	 * that use them.  Allocated and derived on-demand.
517b103fb76SEric Biggers 	 */
5183ceb6543SEric Biggers 	struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1];
5193ceb6543SEric Biggers 	struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1];
5203ceb6543SEric Biggers 	struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1];
521e3b1078bSEric Biggers 
522e3b1078bSEric Biggers 	/* Hash key for inode numbers.  Initialized only when needed. */
523e3b1078bSEric Biggers 	siphash_key_t		mk_ino_hash_key;
524e3b1078bSEric Biggers 	bool			mk_ino_hash_key_initialized;
5255dae460cSEric Biggers 
52622d94f49SEric Biggers } __randomize_layout;
52722d94f49SEric Biggers 
528b1c0ec35SEric Biggers static inline bool
is_master_key_secret_present(const struct fscrypt_master_key_secret * secret)529b1c0ec35SEric Biggers is_master_key_secret_present(const struct fscrypt_master_key_secret *secret)
530b1c0ec35SEric Biggers {
531b1c0ec35SEric Biggers 	/*
532d7e7b9afSEric Biggers 	 * The READ_ONCE() is only necessary for fscrypt_drop_inode().
533d7e7b9afSEric Biggers 	 * fscrypt_drop_inode() runs in atomic context, so it can't take the key
534d7e7b9afSEric Biggers 	 * semaphore and thus 'secret' can change concurrently which would be a
535d7e7b9afSEric Biggers 	 * data race.  But fscrypt_drop_inode() only need to know whether the
5364a4b8721SEric Biggers 	 * secret *was* present at the time of check, so READ_ONCE() suffices.
537b1c0ec35SEric Biggers 	 */
538b1c0ec35SEric Biggers 	return READ_ONCE(secret->size) != 0;
539b1c0ec35SEric Biggers }
540b1c0ec35SEric Biggers 
master_key_spec_type(const struct fscrypt_key_specifier * spec)54122d94f49SEric Biggers static inline const char *master_key_spec_type(
54222d94f49SEric Biggers 				const struct fscrypt_key_specifier *spec)
54322d94f49SEric Biggers {
54422d94f49SEric Biggers 	switch (spec->type) {
54522d94f49SEric Biggers 	case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
54622d94f49SEric Biggers 		return "descriptor";
5475dae460cSEric Biggers 	case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
5485dae460cSEric Biggers 		return "identifier";
54922d94f49SEric Biggers 	}
55022d94f49SEric Biggers 	return "[unknown]";
55122d94f49SEric Biggers }
55222d94f49SEric Biggers 
master_key_spec_len(const struct fscrypt_key_specifier * spec)55322d94f49SEric Biggers static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
55422d94f49SEric Biggers {
55522d94f49SEric Biggers 	switch (spec->type) {
55622d94f49SEric Biggers 	case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
55722d94f49SEric Biggers 		return FSCRYPT_KEY_DESCRIPTOR_SIZE;
5585dae460cSEric Biggers 	case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
5595dae460cSEric Biggers 		return FSCRYPT_KEY_IDENTIFIER_SIZE;
56022d94f49SEric Biggers 	}
56122d94f49SEric Biggers 	return 0;
56222d94f49SEric Biggers }
56322d94f49SEric Biggers 
564d7e7b9afSEric Biggers void fscrypt_put_master_key(struct fscrypt_master_key *mk);
565d7e7b9afSEric Biggers 
56602aef422SEric Biggers void fscrypt_put_master_key_activeref(struct super_block *sb,
56702aef422SEric Biggers 				      struct fscrypt_master_key *mk);
568d7e7b9afSEric Biggers 
569d7e7b9afSEric Biggers struct fscrypt_master_key *
57022d94f49SEric Biggers fscrypt_find_master_key(struct super_block *sb,
57122d94f49SEric Biggers 			const struct fscrypt_key_specifier *mk_spec);
57222d94f49SEric Biggers 
573218d921bSEric Biggers int fscrypt_get_test_dummy_key_identifier(
574218d921bSEric Biggers 			  u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
575cdeb21daSEric Biggers 
576097d7c1fSEric Biggers int fscrypt_add_test_dummy_key(struct super_block *sb,
577097d7c1fSEric Biggers 			       struct fscrypt_key_specifier *key_spec);
578097d7c1fSEric Biggers 
57960700902SEric Biggers int fscrypt_verify_key_added(struct super_block *sb,
5805ab7189aSEric Biggers 			     const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
5815ab7189aSEric Biggers 
58260700902SEric Biggers int __init fscrypt_init_keyring(void);
58322d94f49SEric Biggers 
584feed8258SEric Biggers /* keysetup.c */
5858094c3ceSEric Biggers 
5868094c3ceSEric Biggers struct fscrypt_mode {
5878094c3ceSEric Biggers 	const char *friendly_name;
5888094c3ceSEric Biggers 	const char *cipher_str;
5897f595d6aSEric Biggers 	int keysize;		/* key size in bytes */
5907f595d6aSEric Biggers 	int security_strength;	/* security strength in bytes */
5917f595d6aSEric Biggers 	int ivsize;		/* IV size in bytes */
592a7a5bc5fSEric Biggers 	int logged_cryptoapi_impl;
593a7a5bc5fSEric Biggers 	int logged_blk_crypto_native;
594a7a5bc5fSEric Biggers 	int logged_blk_crypto_fallback;
5955fee3609SSatya Tangirala 	enum blk_crypto_mode_num blk_crypto_mode;
5968094c3ceSEric Biggers };
5978094c3ceSEric Biggers 
59885af90e5SEric Biggers extern struct fscrypt_mode fscrypt_modes[];
5993ec4f2a6SEric Biggers 
6005fee3609SSatya Tangirala int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,
6015fee3609SSatya Tangirala 			const u8 *raw_key, const struct fscrypt_info *ci);
6025fee3609SSatya Tangirala 
60322e9947aSEric Biggers void fscrypt_destroy_prepared_key(struct super_block *sb,
60422e9947aSEric Biggers 				  struct fscrypt_prepared_key *prep_key);
6050109ce76SEric Biggers 
60660700902SEric Biggers int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key);
6070109ce76SEric Biggers 
60860700902SEric Biggers int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
609aa408f83SDaniel Rosenberg 			       const struct fscrypt_master_key *mk);
610aa408f83SDaniel Rosenberg 
611a992b20cSEric Biggers void fscrypt_hash_inode_number(struct fscrypt_info *ci,
612a992b20cSEric Biggers 			       const struct fscrypt_master_key *mk);
613a992b20cSEric Biggers 
614a14d0b67SEric Biggers int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported);
6155b421f08SEric Biggers 
616de3cdc6eSEric Biggers /**
617de3cdc6eSEric Biggers  * fscrypt_require_key() - require an inode's encryption key
618de3cdc6eSEric Biggers  * @inode: the inode we need the key for
619de3cdc6eSEric Biggers  *
620de3cdc6eSEric Biggers  * If the inode is encrypted, set up its encryption key if not already done.
621de3cdc6eSEric Biggers  * Then require that the key be present and return -ENOKEY otherwise.
622de3cdc6eSEric Biggers  *
623de3cdc6eSEric Biggers  * No locks are needed, and the key will live as long as the struct inode --- so
624de3cdc6eSEric Biggers  * it won't go away from under you.
625de3cdc6eSEric Biggers  *
626de3cdc6eSEric Biggers  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
627de3cdc6eSEric Biggers  * if a problem occurred while setting up the encryption key.
628de3cdc6eSEric Biggers  */
fscrypt_require_key(struct inode * inode)629de3cdc6eSEric Biggers static inline int fscrypt_require_key(struct inode *inode)
630de3cdc6eSEric Biggers {
631de3cdc6eSEric Biggers 	if (IS_ENCRYPTED(inode)) {
632a14d0b67SEric Biggers 		int err = fscrypt_get_encryption_info(inode, false);
633de3cdc6eSEric Biggers 
634de3cdc6eSEric Biggers 		if (err)
635de3cdc6eSEric Biggers 			return err;
636de3cdc6eSEric Biggers 		if (!fscrypt_has_encryption_key(inode))
637de3cdc6eSEric Biggers 			return -ENOKEY;
638de3cdc6eSEric Biggers 	}
639de3cdc6eSEric Biggers 	return 0;
640de3cdc6eSEric Biggers }
641de3cdc6eSEric Biggers 
6420109ce76SEric Biggers /* keysetup_v1.c */
6430109ce76SEric Biggers 
64460700902SEric Biggers void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
6450109ce76SEric Biggers 
64660700902SEric Biggers int fscrypt_setup_v1_file_key(struct fscrypt_info *ci,
6470109ce76SEric Biggers 			      const u8 *raw_master_key);
6480109ce76SEric Biggers 
64960700902SEric Biggers int fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_info *ci);
65060700902SEric Biggers 
6515dae460cSEric Biggers /* policy.c */
6525dae460cSEric Biggers 
65360700902SEric Biggers bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
6545dae460cSEric Biggers 			    const union fscrypt_policy *policy2);
655bfb9700bSEric Biggers int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy,
656bfb9700bSEric Biggers 			       struct fscrypt_key_specifier *key_spec);
65760e463f0SEric Biggers const union fscrypt_policy *fscrypt_get_dummy_policy(struct super_block *sb);
65860700902SEric Biggers bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
6595dae460cSEric Biggers 			      const struct inode *inode);
66060700902SEric Biggers int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
6615dae460cSEric Biggers 				const union fscrypt_context *ctx_u,
6625dae460cSEric Biggers 				int ctx_size);
663ac4acb1fSEric Biggers const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir);
6640109ce76SEric Biggers 
6653325bea5STheodore Ts'o #endif /* _FSCRYPT_PRIVATE_H */
666