xref: /openbmc/linux/fs/crypto/fscrypt_private.h (revision de3cdc6e75179a2324c23400b21483a1372c95e1)
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 
2322d94f49SEric Biggers #define FSCRYPT_MIN_KEY_SIZE	16
2422d94f49SEric Biggers 
255dae460cSEric Biggers #define FSCRYPT_CONTEXT_V1	1
265dae460cSEric Biggers #define FSCRYPT_CONTEXT_V2	2
275dae460cSEric Biggers 
283ceb6543SEric Biggers /* Keep this in sync with include/uapi/linux/fscrypt.h */
293ceb6543SEric Biggers #define FSCRYPT_MODE_MAX	FSCRYPT_MODE_ADIANTUM
303ceb6543SEric Biggers 
315dae460cSEric Biggers struct fscrypt_context_v1 {
325dae460cSEric Biggers 	u8 version; /* FSCRYPT_CONTEXT_V1 */
33cc4e0df0STheodore Ts'o 	u8 contents_encryption_mode;
34cc4e0df0STheodore Ts'o 	u8 filenames_encryption_mode;
35cc4e0df0STheodore Ts'o 	u8 flags;
363b6df59bSEric Biggers 	u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
371d6217a4SEric Biggers 	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
385dae460cSEric Biggers };
39cc4e0df0STheodore Ts'o 
405dae460cSEric Biggers struct fscrypt_context_v2 {
415dae460cSEric Biggers 	u8 version; /* FSCRYPT_CONTEXT_V2 */
425dae460cSEric Biggers 	u8 contents_encryption_mode;
435dae460cSEric Biggers 	u8 filenames_encryption_mode;
445dae460cSEric Biggers 	u8 flags;
455dae460cSEric Biggers 	u8 __reserved[4];
465dae460cSEric Biggers 	u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
471d6217a4SEric Biggers 	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
485dae460cSEric Biggers };
495dae460cSEric Biggers 
50d2fe9754SEric Biggers /*
515dae460cSEric Biggers  * fscrypt_context - the encryption context of an inode
525dae460cSEric Biggers  *
535dae460cSEric Biggers  * This is the on-disk equivalent of an fscrypt_policy, stored alongside each
545dae460cSEric Biggers  * encrypted file usually in a hidden extended attribute.  It contains the
555dae460cSEric Biggers  * fields from the fscrypt_policy, in order to identify the encryption algorithm
565dae460cSEric Biggers  * and key with which the file is encrypted.  It also contains a nonce that was
575dae460cSEric Biggers  * randomly generated by fscrypt itself; this is used as KDF input or as a tweak
585dae460cSEric Biggers  * to cause different files to be encrypted differently.
595dae460cSEric Biggers  */
605dae460cSEric Biggers union fscrypt_context {
615dae460cSEric Biggers 	u8 version;
625dae460cSEric Biggers 	struct fscrypt_context_v1 v1;
635dae460cSEric Biggers 	struct fscrypt_context_v2 v2;
645dae460cSEric Biggers };
655dae460cSEric Biggers 
665dae460cSEric Biggers /*
675dae460cSEric Biggers  * Return the size expected for the given fscrypt_context based on its version
685dae460cSEric Biggers  * number, or 0 if the context version is unrecognized.
695dae460cSEric Biggers  */
705dae460cSEric Biggers static inline int fscrypt_context_size(const union fscrypt_context *ctx)
715dae460cSEric Biggers {
725dae460cSEric Biggers 	switch (ctx->version) {
735dae460cSEric Biggers 	case FSCRYPT_CONTEXT_V1:
745dae460cSEric Biggers 		BUILD_BUG_ON(sizeof(ctx->v1) != 28);
755dae460cSEric Biggers 		return sizeof(ctx->v1);
765dae460cSEric Biggers 	case FSCRYPT_CONTEXT_V2:
775dae460cSEric Biggers 		BUILD_BUG_ON(sizeof(ctx->v2) != 40);
785dae460cSEric Biggers 		return sizeof(ctx->v2);
795dae460cSEric Biggers 	}
805dae460cSEric Biggers 	return 0;
815dae460cSEric Biggers }
825dae460cSEric Biggers 
83e98ad464SEric Biggers /* Check whether an fscrypt_context has a recognized version number and size */
84e98ad464SEric Biggers static inline bool fscrypt_context_is_valid(const union fscrypt_context *ctx,
85e98ad464SEric Biggers 					    int ctx_size)
86e98ad464SEric Biggers {
87e98ad464SEric Biggers 	return ctx_size >= 1 && ctx_size == fscrypt_context_size(ctx);
88e98ad464SEric Biggers }
89e98ad464SEric Biggers 
90e98ad464SEric Biggers /* Retrieve the context's nonce, assuming the context was already validated */
91e98ad464SEric Biggers static inline const u8 *fscrypt_context_nonce(const union fscrypt_context *ctx)
92e98ad464SEric Biggers {
93e98ad464SEric Biggers 	switch (ctx->version) {
94e98ad464SEric Biggers 	case FSCRYPT_CONTEXT_V1:
95e98ad464SEric Biggers 		return ctx->v1.nonce;
96e98ad464SEric Biggers 	case FSCRYPT_CONTEXT_V2:
97e98ad464SEric Biggers 		return ctx->v2.nonce;
98e98ad464SEric Biggers 	}
99e98ad464SEric Biggers 	WARN_ON(1);
100e98ad464SEric Biggers 	return NULL;
101e98ad464SEric Biggers }
102e98ad464SEric Biggers 
1035dae460cSEric Biggers union fscrypt_policy {
1045dae460cSEric Biggers 	u8 version;
1055dae460cSEric Biggers 	struct fscrypt_policy_v1 v1;
1065dae460cSEric Biggers 	struct fscrypt_policy_v2 v2;
1075dae460cSEric Biggers };
1085dae460cSEric Biggers 
1095dae460cSEric Biggers /*
1105dae460cSEric Biggers  * Return the size expected for the given fscrypt_policy based on its version
1115dae460cSEric Biggers  * number, or 0 if the policy version is unrecognized.
1125dae460cSEric Biggers  */
1135dae460cSEric Biggers static inline int fscrypt_policy_size(const union fscrypt_policy *policy)
1145dae460cSEric Biggers {
1155dae460cSEric Biggers 	switch (policy->version) {
1165dae460cSEric Biggers 	case FSCRYPT_POLICY_V1:
1175dae460cSEric Biggers 		return sizeof(policy->v1);
1185dae460cSEric Biggers 	case FSCRYPT_POLICY_V2:
1195dae460cSEric Biggers 		return sizeof(policy->v2);
1205dae460cSEric Biggers 	}
1215dae460cSEric Biggers 	return 0;
1225dae460cSEric Biggers }
1235dae460cSEric Biggers 
1245dae460cSEric Biggers /* Return the contents encryption mode of a valid encryption policy */
1255dae460cSEric Biggers static inline u8
1265dae460cSEric Biggers fscrypt_policy_contents_mode(const union fscrypt_policy *policy)
1275dae460cSEric Biggers {
1285dae460cSEric Biggers 	switch (policy->version) {
1295dae460cSEric Biggers 	case FSCRYPT_POLICY_V1:
1305dae460cSEric Biggers 		return policy->v1.contents_encryption_mode;
1315dae460cSEric Biggers 	case FSCRYPT_POLICY_V2:
1325dae460cSEric Biggers 		return policy->v2.contents_encryption_mode;
1335dae460cSEric Biggers 	}
1345dae460cSEric Biggers 	BUG();
1355dae460cSEric Biggers }
1365dae460cSEric Biggers 
1375dae460cSEric Biggers /* Return the filenames encryption mode of a valid encryption policy */
1385dae460cSEric Biggers static inline u8
1395dae460cSEric Biggers fscrypt_policy_fnames_mode(const union fscrypt_policy *policy)
1405dae460cSEric Biggers {
1415dae460cSEric Biggers 	switch (policy->version) {
1425dae460cSEric Biggers 	case FSCRYPT_POLICY_V1:
1435dae460cSEric Biggers 		return policy->v1.filenames_encryption_mode;
1445dae460cSEric Biggers 	case FSCRYPT_POLICY_V2:
1455dae460cSEric Biggers 		return policy->v2.filenames_encryption_mode;
1465dae460cSEric Biggers 	}
1475dae460cSEric Biggers 	BUG();
1485dae460cSEric Biggers }
1495dae460cSEric Biggers 
1505dae460cSEric Biggers /* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */
1515dae460cSEric Biggers static inline u8
1525dae460cSEric Biggers fscrypt_policy_flags(const union fscrypt_policy *policy)
1535dae460cSEric Biggers {
1545dae460cSEric Biggers 	switch (policy->version) {
1555dae460cSEric Biggers 	case FSCRYPT_POLICY_V1:
1565dae460cSEric Biggers 		return policy->v1.flags;
1575dae460cSEric Biggers 	case FSCRYPT_POLICY_V2:
1585dae460cSEric Biggers 		return policy->v2.flags;
1595dae460cSEric Biggers 	}
1605dae460cSEric Biggers 	BUG();
1615dae460cSEric Biggers }
1625dae460cSEric Biggers 
163d2fe9754SEric Biggers /*
1640eaab5b1SEric Biggers  * For encrypted symlinks, the ciphertext length is stored at the beginning
1650eaab5b1SEric Biggers  * of the string in little-endian format.
1660eaab5b1SEric Biggers  */
1670eaab5b1SEric Biggers struct fscrypt_symlink_data {
1680eaab5b1SEric Biggers 	__le16 len;
1690eaab5b1SEric Biggers 	char encrypted_path[1];
1700eaab5b1SEric Biggers } __packed;
1710eaab5b1SEric Biggers 
1725fee3609SSatya Tangirala /**
1735fee3609SSatya Tangirala  * struct fscrypt_prepared_key - a key prepared for actual encryption/decryption
1745fee3609SSatya Tangirala  * @tfm: crypto API transform object
1755fee3609SSatya Tangirala  * @blk_key: key for blk-crypto
1765fee3609SSatya Tangirala  *
1775fee3609SSatya Tangirala  * Normally only one of the fields will be non-NULL.
1785fee3609SSatya Tangirala  */
1795fee3609SSatya Tangirala struct fscrypt_prepared_key {
1805fee3609SSatya Tangirala 	struct crypto_skcipher *tfm;
1815fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
1825fee3609SSatya Tangirala 	struct fscrypt_blk_crypto_key *blk_key;
1835fee3609SSatya Tangirala #endif
1845fee3609SSatya Tangirala };
1855fee3609SSatya Tangirala 
186cc4e0df0STheodore Ts'o /*
1878094c3ceSEric Biggers  * fscrypt_info - the "encryption key" for an inode
1888094c3ceSEric Biggers  *
1898094c3ceSEric Biggers  * When an encrypted file's key is made available, an instance of this struct is
1908094c3ceSEric Biggers  * allocated and stored in ->i_crypt_info.  Once created, it remains until the
1918094c3ceSEric Biggers  * inode is evicted.
192cc4e0df0STheodore Ts'o  */
193cc4e0df0STheodore Ts'o struct fscrypt_info {
1948094c3ceSEric Biggers 
1955fee3609SSatya Tangirala 	/* The key in a form prepared for actual encryption/decryption */
1965fee3609SSatya Tangirala 	struct fscrypt_prepared_key ci_enc_key;
1978094c3ceSEric Biggers 
1985fee3609SSatya Tangirala 	/* True if ci_enc_key should be freed when this fscrypt_info is freed */
199b103fb76SEric Biggers 	bool ci_owns_key;
200b103fb76SEric Biggers 
2015fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
2025fee3609SSatya Tangirala 	/*
2035fee3609SSatya Tangirala 	 * True if this inode will use inline encryption (blk-crypto) instead of
2045fee3609SSatya Tangirala 	 * the traditional filesystem-layer encryption.
2055fee3609SSatya Tangirala 	 */
2065fee3609SSatya Tangirala 	bool ci_inlinecrypt;
2075fee3609SSatya Tangirala #endif
2085fee3609SSatya Tangirala 
2098094c3ceSEric Biggers 	/*
2105dae460cSEric Biggers 	 * Encryption mode used for this inode.  It corresponds to either the
2115dae460cSEric Biggers 	 * contents or filenames encryption mode, depending on the inode type.
2128094c3ceSEric Biggers 	 */
2138094c3ceSEric Biggers 	struct fscrypt_mode *ci_mode;
2148094c3ceSEric Biggers 
21559dc6a8eSEric Biggers 	/* Back-pointer to the inode */
21659dc6a8eSEric Biggers 	struct inode *ci_inode;
21759dc6a8eSEric Biggers 
2188094c3ceSEric Biggers 	/*
219b1c0ec35SEric Biggers 	 * The master key with which this inode was unlocked (decrypted).  This
220b1c0ec35SEric Biggers 	 * will be NULL if the master key was found in a process-subscribed
221b1c0ec35SEric Biggers 	 * keyring rather than in the filesystem-level keyring.
222b1c0ec35SEric Biggers 	 */
223b1c0ec35SEric Biggers 	struct key *ci_master_key;
224b1c0ec35SEric Biggers 
225b1c0ec35SEric Biggers 	/*
226b1c0ec35SEric Biggers 	 * Link in list of inodes that were unlocked with the master key.
227b1c0ec35SEric Biggers 	 * Only used when ->ci_master_key is set.
228b1c0ec35SEric Biggers 	 */
229b1c0ec35SEric Biggers 	struct list_head ci_master_key_link;
230b1c0ec35SEric Biggers 
231b1c0ec35SEric Biggers 	/*
232a828daabSEric Biggers 	 * If non-NULL, then encryption is done using the master key directly
2335fee3609SSatya Tangirala 	 * and ci_enc_key will equal ci_direct_key->dk_key.
2348094c3ceSEric Biggers 	 */
235a828daabSEric Biggers 	struct fscrypt_direct_key *ci_direct_key;
2368094c3ceSEric Biggers 
237aa408f83SDaniel Rosenberg 	/*
238aa408f83SDaniel Rosenberg 	 * This inode's hash key for filenames.  This is a 128-bit SipHash-2-4
239aa408f83SDaniel Rosenberg 	 * key.  This is only set for directories that use a keyed dirhash over
240aa408f83SDaniel Rosenberg 	 * the plaintext filenames -- currently just casefolded directories.
241aa408f83SDaniel Rosenberg 	 */
242aa408f83SDaniel Rosenberg 	siphash_key_t ci_dirhash_key;
243aa408f83SDaniel Rosenberg 	bool ci_dirhash_key_initialized;
244aa408f83SDaniel Rosenberg 
2455dae460cSEric Biggers 	/* The encryption policy used by this inode */
2465dae460cSEric Biggers 	union fscrypt_policy ci_policy;
2475dae460cSEric Biggers 
2485dae460cSEric Biggers 	/* This inode's nonce, copied from the fscrypt_context */
2491d6217a4SEric Biggers 	u8 ci_nonce[FSCRYPT_FILE_NONCE_SIZE];
250e3b1078bSEric Biggers 
251e3b1078bSEric Biggers 	/* Hashed inode number.  Only set for IV_INO_LBLK_32 */
252e3b1078bSEric Biggers 	u32 ci_hashed_ino;
253cc4e0df0STheodore Ts'o };
254cc4e0df0STheodore Ts'o 
25558ae7468SRichard Weinberger typedef enum {
25658ae7468SRichard Weinberger 	FS_DECRYPT = 0,
25758ae7468SRichard Weinberger 	FS_ENCRYPT,
25858ae7468SRichard Weinberger } fscrypt_direction_t;
25958ae7468SRichard Weinberger 
260b98701dfSTheodore Ts'o /* crypto.c */
261e4de782aSEric Biggers extern struct kmem_cache *fscrypt_info_cachep;
26260700902SEric Biggers int fscrypt_initialize(unsigned int cop_flags);
26360700902SEric Biggers int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw,
26460700902SEric Biggers 			u64 lblk_num, struct page *src_page,
26560700902SEric Biggers 			struct page *dest_page, unsigned int len,
26660700902SEric Biggers 			unsigned int offs, gfp_t gfp_flags);
26760700902SEric Biggers struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags);
268b98701dfSTheodore Ts'o 
26960700902SEric Biggers void __printf(3, 4) __cold
270886da8b3SEric Biggers fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...);
271544d08fdSEric Biggers 
272886da8b3SEric Biggers #define fscrypt_warn(inode, fmt, ...)		\
273886da8b3SEric Biggers 	fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
274886da8b3SEric Biggers #define fscrypt_err(inode, fmt, ...)		\
275886da8b3SEric Biggers 	fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
276544d08fdSEric Biggers 
2778094c3ceSEric Biggers #define FSCRYPT_MAX_IV_SIZE	32
2788094c3ceSEric Biggers 
2798094c3ceSEric Biggers union fscrypt_iv {
2808094c3ceSEric Biggers 	struct {
2818094c3ceSEric Biggers 		/* logical block number within the file */
2828094c3ceSEric Biggers 		__le64 lblk_num;
2838094c3ceSEric Biggers 
2848094c3ceSEric Biggers 		/* per-file nonce; only set in DIRECT_KEY mode */
2851d6217a4SEric Biggers 		u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
2868094c3ceSEric Biggers 	};
2878094c3ceSEric Biggers 	u8 raw[FSCRYPT_MAX_IV_SIZE];
2885fee3609SSatya Tangirala 	__le64 dun[FSCRYPT_MAX_IV_SIZE / sizeof(__le64)];
2898094c3ceSEric Biggers };
2908094c3ceSEric Biggers 
2918094c3ceSEric Biggers void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
2928094c3ceSEric Biggers 			 const struct fscrypt_info *ci);
2938094c3ceSEric Biggers 
29476e81d6dSEric Biggers /* fname.c */
29560700902SEric Biggers int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
29650c961deSEric Biggers 			  u8 *out, unsigned int olen);
297ac4acb1fSEric Biggers bool fscrypt_fname_encrypted_size(const union fscrypt_policy *policy,
298ac4acb1fSEric Biggers 				  u32 orig_len, u32 max_len,
299ac4acb1fSEric Biggers 				  u32 *encrypted_len_ret);
3002ebdef6dSEric Biggers extern const struct dentry_operations fscrypt_d_ops;
30176e81d6dSEric Biggers 
302c1144c9bSEric Biggers /* hkdf.c */
303c1144c9bSEric Biggers 
304c1144c9bSEric Biggers struct fscrypt_hkdf {
305c1144c9bSEric Biggers 	struct crypto_shash *hmac_tfm;
306c1144c9bSEric Biggers };
307c1144c9bSEric Biggers 
30860700902SEric Biggers int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
309c1144c9bSEric Biggers 		      unsigned int master_key_size);
310c1144c9bSEric Biggers 
3115dae460cSEric Biggers /*
3125dae460cSEric Biggers  * The list of contexts in which fscrypt uses HKDF.  These values are used as
3135dae460cSEric Biggers  * the first byte of the HKDF application-specific info string to guarantee that
3145dae460cSEric Biggers  * info strings are never repeated between contexts.  This ensures that all HKDF
3155dae460cSEric Biggers  * outputs are unique and cryptographically isolated, i.e. knowledge of one
3165dae460cSEric Biggers  * output doesn't reveal another.
3175dae460cSEric Biggers  */
318e455de31SEric Biggers #define HKDF_CONTEXT_KEY_IDENTIFIER	1 /* info=<empty>		*/
319e455de31SEric Biggers #define HKDF_CONTEXT_PER_FILE_ENC_KEY	2 /* info=file_nonce		*/
320e455de31SEric Biggers #define HKDF_CONTEXT_DIRECT_KEY		3 /* info=mode_num		*/
321e455de31SEric Biggers #define HKDF_CONTEXT_IV_INO_LBLK_64_KEY	4 /* info=mode_num||fs_uuid	*/
322e455de31SEric Biggers #define HKDF_CONTEXT_DIRHASH_KEY	5 /* info=file_nonce		*/
323e455de31SEric Biggers #define HKDF_CONTEXT_IV_INO_LBLK_32_KEY	6 /* info=mode_num||fs_uuid	*/
324e455de31SEric Biggers #define HKDF_CONTEXT_INODE_HASH_KEY	7 /* info=<empty>		*/
3255dae460cSEric Biggers 
32660700902SEric Biggers int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
327c1144c9bSEric Biggers 			const u8 *info, unsigned int infolen,
328c1144c9bSEric Biggers 			u8 *okm, unsigned int okmlen);
329c1144c9bSEric Biggers 
33060700902SEric Biggers void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf);
331c1144c9bSEric Biggers 
3325fee3609SSatya Tangirala /* inline_crypt.c */
3335fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
3345fee3609SSatya Tangirala int fscrypt_select_encryption_impl(struct fscrypt_info *ci);
3355fee3609SSatya Tangirala 
3365fee3609SSatya Tangirala static inline bool
3375fee3609SSatya Tangirala fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
3385fee3609SSatya Tangirala {
3395fee3609SSatya Tangirala 	return ci->ci_inlinecrypt;
3405fee3609SSatya Tangirala }
3415fee3609SSatya Tangirala 
3425fee3609SSatya Tangirala int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
3435fee3609SSatya Tangirala 				     const u8 *raw_key,
3445fee3609SSatya Tangirala 				     const struct fscrypt_info *ci);
3455fee3609SSatya Tangirala 
3465fee3609SSatya Tangirala void fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key);
3475fee3609SSatya Tangirala 
3485fee3609SSatya Tangirala /*
3495fee3609SSatya Tangirala  * Check whether the crypto transform or blk-crypto key has been allocated in
3505fee3609SSatya Tangirala  * @prep_key, depending on which encryption implementation the file will use.
3515fee3609SSatya Tangirala  */
3525fee3609SSatya Tangirala static inline bool
3535fee3609SSatya Tangirala fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
3545fee3609SSatya Tangirala 			const struct fscrypt_info *ci)
3555fee3609SSatya Tangirala {
3565fee3609SSatya Tangirala 	/*
35797c6327fSEric Biggers 	 * The two smp_load_acquire()'s here pair with the smp_store_release()'s
35897c6327fSEric Biggers 	 * in fscrypt_prepare_inline_crypt_key() and fscrypt_prepare_key().
35997c6327fSEric Biggers 	 * I.e., in some cases (namely, if this prep_key is a per-mode
36097c6327fSEric Biggers 	 * encryption key) another task can publish blk_key or tfm concurrently,
36197c6327fSEric Biggers 	 * executing a RELEASE barrier.  We need to use smp_load_acquire() here
36297c6327fSEric Biggers 	 * to safely ACQUIRE the memory the other task published.
3635fee3609SSatya Tangirala 	 */
3645fee3609SSatya Tangirala 	if (fscrypt_using_inline_encryption(ci))
36597c6327fSEric Biggers 		return smp_load_acquire(&prep_key->blk_key) != NULL;
36697c6327fSEric Biggers 	return smp_load_acquire(&prep_key->tfm) != NULL;
3675fee3609SSatya Tangirala }
3685fee3609SSatya Tangirala 
3695fee3609SSatya Tangirala #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
3705fee3609SSatya Tangirala 
3715fee3609SSatya Tangirala static inline int fscrypt_select_encryption_impl(struct fscrypt_info *ci)
3725fee3609SSatya Tangirala {
3735fee3609SSatya Tangirala 	return 0;
3745fee3609SSatya Tangirala }
3755fee3609SSatya Tangirala 
3765fee3609SSatya Tangirala static inline bool
3775fee3609SSatya Tangirala fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
3785fee3609SSatya Tangirala {
3795fee3609SSatya Tangirala 	return false;
3805fee3609SSatya Tangirala }
3815fee3609SSatya Tangirala 
3825fee3609SSatya Tangirala static inline int
3835fee3609SSatya Tangirala fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
3845fee3609SSatya Tangirala 				 const u8 *raw_key,
3855fee3609SSatya Tangirala 				 const struct fscrypt_info *ci)
3865fee3609SSatya Tangirala {
3875fee3609SSatya Tangirala 	WARN_ON(1);
3885fee3609SSatya Tangirala 	return -EOPNOTSUPP;
3895fee3609SSatya Tangirala }
3905fee3609SSatya Tangirala 
3915fee3609SSatya Tangirala static inline void
3925fee3609SSatya Tangirala fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key)
3935fee3609SSatya Tangirala {
3945fee3609SSatya Tangirala }
3955fee3609SSatya Tangirala 
3965fee3609SSatya Tangirala static inline bool
3975fee3609SSatya Tangirala fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
3985fee3609SSatya Tangirala 			const struct fscrypt_info *ci)
3995fee3609SSatya Tangirala {
40097c6327fSEric Biggers 	return smp_load_acquire(&prep_key->tfm) != NULL;
4015fee3609SSatya Tangirala }
4025fee3609SSatya Tangirala #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
4035fee3609SSatya Tangirala 
40422d94f49SEric Biggers /* keyring.c */
40522d94f49SEric Biggers 
40622d94f49SEric Biggers /*
40722d94f49SEric Biggers  * fscrypt_master_key_secret - secret key material of an in-use master key
40822d94f49SEric Biggers  */
40922d94f49SEric Biggers struct fscrypt_master_key_secret {
41022d94f49SEric Biggers 
4115dae460cSEric Biggers 	/*
4125dae460cSEric Biggers 	 * For v2 policy keys: HKDF context keyed by this master key.
4135dae460cSEric Biggers 	 * For v1 policy keys: not set (hkdf.hmac_tfm == NULL).
4145dae460cSEric Biggers 	 */
4155dae460cSEric Biggers 	struct fscrypt_hkdf	hkdf;
4165dae460cSEric Biggers 
4175dae460cSEric Biggers 	/* Size of the raw key in bytes.  Set even if ->raw isn't set. */
41822d94f49SEric Biggers 	u32			size;
41922d94f49SEric Biggers 
4205dae460cSEric Biggers 	/* For v1 policy keys: the raw key.  Wiped for v2 policy keys. */
42122d94f49SEric Biggers 	u8			raw[FSCRYPT_MAX_KEY_SIZE];
42222d94f49SEric Biggers 
42322d94f49SEric Biggers } __randomize_layout;
42422d94f49SEric Biggers 
42522d94f49SEric Biggers /*
42622d94f49SEric Biggers  * fscrypt_master_key - an in-use master key
42722d94f49SEric Biggers  *
42822d94f49SEric Biggers  * This represents a master encryption key which has been added to the
42922d94f49SEric Biggers  * filesystem and can be used to "unlock" the encrypted files which were
43022d94f49SEric Biggers  * encrypted with it.
43122d94f49SEric Biggers  */
43222d94f49SEric Biggers struct fscrypt_master_key {
43322d94f49SEric Biggers 
434b1c0ec35SEric Biggers 	/*
435b1c0ec35SEric Biggers 	 * The secret key material.  After FS_IOC_REMOVE_ENCRYPTION_KEY is
436b1c0ec35SEric Biggers 	 * executed, this is wiped and no new inodes can be unlocked with this
437b1c0ec35SEric Biggers 	 * key; however, there may still be inodes in ->mk_decrypted_inodes
438b1c0ec35SEric Biggers 	 * which could not be evicted.  As long as some inodes still remain,
439b1c0ec35SEric Biggers 	 * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or
440b1c0ec35SEric Biggers 	 * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again.
441b1c0ec35SEric Biggers 	 *
4424a4b8721SEric Biggers 	 * Locking: protected by this master key's key->sem.
443b1c0ec35SEric Biggers 	 */
44422d94f49SEric Biggers 	struct fscrypt_master_key_secret	mk_secret;
44522d94f49SEric Biggers 
4465dae460cSEric Biggers 	/*
4475dae460cSEric Biggers 	 * For v1 policy keys: an arbitrary key descriptor which was assigned by
4485dae460cSEric Biggers 	 * userspace (->descriptor).
4495dae460cSEric Biggers 	 *
4505dae460cSEric Biggers 	 * For v2 policy keys: a cryptographic hash of this key (->identifier).
4515dae460cSEric Biggers 	 */
45222d94f49SEric Biggers 	struct fscrypt_key_specifier		mk_spec;
45322d94f49SEric Biggers 
454b1c0ec35SEric Biggers 	/*
45523c688b5SEric Biggers 	 * Keyring which contains a key of type 'key_type_fscrypt_user' for each
45623c688b5SEric Biggers 	 * user who has added this key.  Normally each key will be added by just
45723c688b5SEric Biggers 	 * one user, but it's possible that multiple users share a key, and in
45823c688b5SEric Biggers 	 * that case we need to keep track of those users so that one user can't
45923c688b5SEric Biggers 	 * remove the key before the others want it removed too.
46023c688b5SEric Biggers 	 *
46123c688b5SEric Biggers 	 * This is NULL for v1 policy keys; those can only be added by root.
46223c688b5SEric Biggers 	 *
4634a4b8721SEric Biggers 	 * Locking: in addition to this keyring's own semaphore, this is
4644a4b8721SEric Biggers 	 * protected by this master key's key->sem, so we can do atomic
46523c688b5SEric Biggers 	 * search+insert.  It can also be searched without taking any locks, but
46623c688b5SEric Biggers 	 * in that case the returned key may have already been removed.
46723c688b5SEric Biggers 	 */
46823c688b5SEric Biggers 	struct key		*mk_users;
46923c688b5SEric Biggers 
47023c688b5SEric Biggers 	/*
471b1c0ec35SEric Biggers 	 * Length of ->mk_decrypted_inodes, plus one if mk_secret is present.
472b1c0ec35SEric Biggers 	 * Once this goes to 0, the master key is removed from ->s_master_keys.
473b1c0ec35SEric Biggers 	 * The 'struct fscrypt_master_key' will continue to live as long as the
474b1c0ec35SEric Biggers 	 * 'struct key' whose payload it is, but we won't let this reference
475b1c0ec35SEric Biggers 	 * count rise again.
476b1c0ec35SEric Biggers 	 */
477b1c0ec35SEric Biggers 	refcount_t		mk_refcount;
478b1c0ec35SEric Biggers 
479b1c0ec35SEric Biggers 	/*
480b1c0ec35SEric Biggers 	 * List of inodes that were unlocked using this key.  This allows the
481b1c0ec35SEric Biggers 	 * inodes to be evicted efficiently if the key is removed.
482b1c0ec35SEric Biggers 	 */
483b1c0ec35SEric Biggers 	struct list_head	mk_decrypted_inodes;
484b1c0ec35SEric Biggers 	spinlock_t		mk_decrypted_inodes_lock;
485b1c0ec35SEric Biggers 
486b103fb76SEric Biggers 	/*
487e3b1078bSEric Biggers 	 * Per-mode encryption keys for the various types of encryption policies
488e3b1078bSEric Biggers 	 * that use them.  Allocated and derived on-demand.
489b103fb76SEric Biggers 	 */
4903ceb6543SEric Biggers 	struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1];
4913ceb6543SEric Biggers 	struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1];
4923ceb6543SEric Biggers 	struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1];
493e3b1078bSEric Biggers 
494e3b1078bSEric Biggers 	/* Hash key for inode numbers.  Initialized only when needed. */
495e3b1078bSEric Biggers 	siphash_key_t		mk_ino_hash_key;
496e3b1078bSEric Biggers 	bool			mk_ino_hash_key_initialized;
4975dae460cSEric Biggers 
49822d94f49SEric Biggers } __randomize_layout;
49922d94f49SEric Biggers 
500b1c0ec35SEric Biggers static inline bool
501b1c0ec35SEric Biggers is_master_key_secret_present(const struct fscrypt_master_key_secret *secret)
502b1c0ec35SEric Biggers {
503b1c0ec35SEric Biggers 	/*
504b1c0ec35SEric Biggers 	 * The READ_ONCE() is only necessary for fscrypt_drop_inode() and
505b1c0ec35SEric Biggers 	 * fscrypt_key_describe().  These run in atomic context, so they can't
5064a4b8721SEric Biggers 	 * take the key semaphore and thus 'secret' can change concurrently
5074a4b8721SEric Biggers 	 * which would be a data race.  But they only need to know whether the
5084a4b8721SEric Biggers 	 * secret *was* present at the time of check, so READ_ONCE() suffices.
509b1c0ec35SEric Biggers 	 */
510b1c0ec35SEric Biggers 	return READ_ONCE(secret->size) != 0;
511b1c0ec35SEric Biggers }
512b1c0ec35SEric Biggers 
51322d94f49SEric Biggers static inline const char *master_key_spec_type(
51422d94f49SEric Biggers 				const struct fscrypt_key_specifier *spec)
51522d94f49SEric Biggers {
51622d94f49SEric Biggers 	switch (spec->type) {
51722d94f49SEric Biggers 	case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
51822d94f49SEric Biggers 		return "descriptor";
5195dae460cSEric Biggers 	case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
5205dae460cSEric Biggers 		return "identifier";
52122d94f49SEric Biggers 	}
52222d94f49SEric Biggers 	return "[unknown]";
52322d94f49SEric Biggers }
52422d94f49SEric Biggers 
52522d94f49SEric Biggers static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
52622d94f49SEric Biggers {
52722d94f49SEric Biggers 	switch (spec->type) {
52822d94f49SEric Biggers 	case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
52922d94f49SEric Biggers 		return FSCRYPT_KEY_DESCRIPTOR_SIZE;
5305dae460cSEric Biggers 	case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
5315dae460cSEric Biggers 		return FSCRYPT_KEY_IDENTIFIER_SIZE;
53222d94f49SEric Biggers 	}
53322d94f49SEric Biggers 	return 0;
53422d94f49SEric Biggers }
53522d94f49SEric Biggers 
53660700902SEric Biggers struct key *
53722d94f49SEric Biggers fscrypt_find_master_key(struct super_block *sb,
53822d94f49SEric Biggers 			const struct fscrypt_key_specifier *mk_spec);
53922d94f49SEric Biggers 
540cdeb21daSEric Biggers int fscrypt_add_test_dummy_key(struct super_block *sb,
541cdeb21daSEric Biggers 			       struct fscrypt_key_specifier *key_spec);
542cdeb21daSEric Biggers 
54360700902SEric Biggers int fscrypt_verify_key_added(struct super_block *sb,
5445ab7189aSEric Biggers 			     const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
5455ab7189aSEric Biggers 
54660700902SEric Biggers int __init fscrypt_init_keyring(void);
54722d94f49SEric Biggers 
548feed8258SEric Biggers /* keysetup.c */
5498094c3ceSEric Biggers 
5508094c3ceSEric Biggers struct fscrypt_mode {
5518094c3ceSEric Biggers 	const char *friendly_name;
5528094c3ceSEric Biggers 	const char *cipher_str;
5538094c3ceSEric Biggers 	int keysize;
5548094c3ceSEric Biggers 	int ivsize;
555ff73c2c0SEric Biggers 	int logged_impl_name;
5565fee3609SSatya Tangirala 	enum blk_crypto_mode_num blk_crypto_mode;
5578094c3ceSEric Biggers };
5588094c3ceSEric Biggers 
55985af90e5SEric Biggers extern struct fscrypt_mode fscrypt_modes[];
5603ec4f2a6SEric Biggers 
5615fee3609SSatya Tangirala int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,
5625fee3609SSatya Tangirala 			const u8 *raw_key, const struct fscrypt_info *ci);
5635fee3609SSatya Tangirala 
5645fee3609SSatya Tangirala void fscrypt_destroy_prepared_key(struct fscrypt_prepared_key *prep_key);
5650109ce76SEric Biggers 
56660700902SEric Biggers int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key);
5670109ce76SEric Biggers 
56860700902SEric Biggers int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
569aa408f83SDaniel Rosenberg 			       const struct fscrypt_master_key *mk);
570aa408f83SDaniel Rosenberg 
571a992b20cSEric Biggers void fscrypt_hash_inode_number(struct fscrypt_info *ci,
572a992b20cSEric Biggers 			       const struct fscrypt_master_key *mk);
573a992b20cSEric Biggers 
574*de3cdc6eSEric Biggers /**
575*de3cdc6eSEric Biggers  * fscrypt_require_key() - require an inode's encryption key
576*de3cdc6eSEric Biggers  * @inode: the inode we need the key for
577*de3cdc6eSEric Biggers  *
578*de3cdc6eSEric Biggers  * If the inode is encrypted, set up its encryption key if not already done.
579*de3cdc6eSEric Biggers  * Then require that the key be present and return -ENOKEY otherwise.
580*de3cdc6eSEric Biggers  *
581*de3cdc6eSEric Biggers  * No locks are needed, and the key will live as long as the struct inode --- so
582*de3cdc6eSEric Biggers  * it won't go away from under you.
583*de3cdc6eSEric Biggers  *
584*de3cdc6eSEric Biggers  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
585*de3cdc6eSEric Biggers  * if a problem occurred while setting up the encryption key.
586*de3cdc6eSEric Biggers  */
587*de3cdc6eSEric Biggers static inline int fscrypt_require_key(struct inode *inode)
588*de3cdc6eSEric Biggers {
589*de3cdc6eSEric Biggers 	if (IS_ENCRYPTED(inode)) {
590*de3cdc6eSEric Biggers 		int err = fscrypt_get_encryption_info(inode);
591*de3cdc6eSEric Biggers 
592*de3cdc6eSEric Biggers 		if (err)
593*de3cdc6eSEric Biggers 			return err;
594*de3cdc6eSEric Biggers 		if (!fscrypt_has_encryption_key(inode))
595*de3cdc6eSEric Biggers 			return -ENOKEY;
596*de3cdc6eSEric Biggers 	}
597*de3cdc6eSEric Biggers 	return 0;
598*de3cdc6eSEric Biggers }
599*de3cdc6eSEric Biggers 
6000109ce76SEric Biggers /* keysetup_v1.c */
6010109ce76SEric Biggers 
60260700902SEric Biggers void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
6030109ce76SEric Biggers 
60460700902SEric Biggers int fscrypt_setup_v1_file_key(struct fscrypt_info *ci,
6050109ce76SEric Biggers 			      const u8 *raw_master_key);
6060109ce76SEric Biggers 
60760700902SEric Biggers int fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_info *ci);
60860700902SEric Biggers 
6095dae460cSEric Biggers /* policy.c */
6105dae460cSEric Biggers 
61160700902SEric Biggers bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
6125dae460cSEric Biggers 			    const union fscrypt_policy *policy2);
61360700902SEric Biggers bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
6145dae460cSEric Biggers 			      const struct inode *inode);
61560700902SEric Biggers int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
6165dae460cSEric Biggers 				const union fscrypt_context *ctx_u,
6175dae460cSEric Biggers 				int ctx_size);
618ac4acb1fSEric Biggers const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir);
6190109ce76SEric Biggers 
6203325bea5STheodore Ts'o #endif /* _FSCRYPT_PRIVATE_H */
621