xref: /openbmc/linux/fs/crypto/fscrypt_private.h (revision b1c0ec3599f42ad372063b0235a3c33f65eb1e30)
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>
15b7e7cf7aSDaniel Walter #include <crypto/hash.h>
163325bea5STheodore Ts'o 
1722d94f49SEric Biggers #define CONST_STRLEN(str)	(sizeof(str) - 1)
1822d94f49SEric Biggers 
19cc4e0df0STheodore Ts'o #define FS_KEY_DERIVATION_NONCE_SIZE	16
20cc4e0df0STheodore Ts'o 
2122d94f49SEric Biggers #define FSCRYPT_MIN_KEY_SIZE		16
2222d94f49SEric Biggers 
23cc4e0df0STheodore Ts'o /**
24cc4e0df0STheodore Ts'o  * Encryption context for inode
25cc4e0df0STheodore Ts'o  *
26cc4e0df0STheodore Ts'o  * Protector format:
27cc4e0df0STheodore Ts'o  *  1 byte: Protector format (1 = this version)
28cc4e0df0STheodore Ts'o  *  1 byte: File contents encryption mode
29cc4e0df0STheodore Ts'o  *  1 byte: File names encryption mode
30cc4e0df0STheodore Ts'o  *  1 byte: Flags
31cc4e0df0STheodore Ts'o  *  8 bytes: Master Key descriptor
32cc4e0df0STheodore Ts'o  *  16 bytes: Encryption Key derivation nonce
33cc4e0df0STheodore Ts'o  */
34cc4e0df0STheodore Ts'o struct fscrypt_context {
35cc4e0df0STheodore Ts'o 	u8 format;
36cc4e0df0STheodore Ts'o 	u8 contents_encryption_mode;
37cc4e0df0STheodore Ts'o 	u8 filenames_encryption_mode;
38cc4e0df0STheodore Ts'o 	u8 flags;
393b6df59bSEric Biggers 	u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
40cc4e0df0STheodore Ts'o 	u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
41cc4e0df0STheodore Ts'o } __packed;
42cc4e0df0STheodore Ts'o 
43cc4e0df0STheodore Ts'o #define FS_ENCRYPTION_CONTEXT_FORMAT_V1		1
44cc4e0df0STheodore Ts'o 
450eaab5b1SEric Biggers /**
460eaab5b1SEric Biggers  * For encrypted symlinks, the ciphertext length is stored at the beginning
470eaab5b1SEric Biggers  * of the string in little-endian format.
480eaab5b1SEric Biggers  */
490eaab5b1SEric Biggers struct fscrypt_symlink_data {
500eaab5b1SEric Biggers 	__le16 len;
510eaab5b1SEric Biggers 	char encrypted_path[1];
520eaab5b1SEric Biggers } __packed;
530eaab5b1SEric Biggers 
54cc4e0df0STheodore Ts'o /*
558094c3ceSEric Biggers  * fscrypt_info - the "encryption key" for an inode
568094c3ceSEric Biggers  *
578094c3ceSEric Biggers  * When an encrypted file's key is made available, an instance of this struct is
588094c3ceSEric Biggers  * allocated and stored in ->i_crypt_info.  Once created, it remains until the
598094c3ceSEric Biggers  * inode is evicted.
60cc4e0df0STheodore Ts'o  */
61cc4e0df0STheodore Ts'o struct fscrypt_info {
628094c3ceSEric Biggers 
638094c3ceSEric Biggers 	/* The actual crypto transform used for encryption and decryption */
648094c3ceSEric Biggers 	struct crypto_skcipher *ci_ctfm;
658094c3ceSEric Biggers 
668094c3ceSEric Biggers 	/*
678094c3ceSEric Biggers 	 * Cipher for ESSIV IV generation.  Only set for CBC contents
688094c3ceSEric Biggers 	 * encryption, otherwise is NULL.
698094c3ceSEric Biggers 	 */
708094c3ceSEric Biggers 	struct crypto_cipher *ci_essiv_tfm;
718094c3ceSEric Biggers 
728094c3ceSEric Biggers 	/*
738094c3ceSEric Biggers 	 * Encryption mode used for this inode.  It corresponds to either
748094c3ceSEric Biggers 	 * ci_data_mode or ci_filename_mode, depending on the inode type.
758094c3ceSEric Biggers 	 */
768094c3ceSEric Biggers 	struct fscrypt_mode *ci_mode;
778094c3ceSEric Biggers 
7859dc6a8eSEric Biggers 	/* Back-pointer to the inode */
7959dc6a8eSEric Biggers 	struct inode *ci_inode;
8059dc6a8eSEric Biggers 
818094c3ceSEric Biggers 	/*
82*b1c0ec35SEric Biggers 	 * The master key with which this inode was unlocked (decrypted).  This
83*b1c0ec35SEric Biggers 	 * will be NULL if the master key was found in a process-subscribed
84*b1c0ec35SEric Biggers 	 * keyring rather than in the filesystem-level keyring.
85*b1c0ec35SEric Biggers 	 */
86*b1c0ec35SEric Biggers 	struct key *ci_master_key;
87*b1c0ec35SEric Biggers 
88*b1c0ec35SEric Biggers 	/*
89*b1c0ec35SEric Biggers 	 * Link in list of inodes that were unlocked with the master key.
90*b1c0ec35SEric Biggers 	 * Only used when ->ci_master_key is set.
91*b1c0ec35SEric Biggers 	 */
92*b1c0ec35SEric Biggers 	struct list_head ci_master_key_link;
93*b1c0ec35SEric Biggers 
94*b1c0ec35SEric Biggers 	/*
95a828daabSEric Biggers 	 * If non-NULL, then encryption is done using the master key directly
96a828daabSEric Biggers 	 * and ci_ctfm will equal ci_direct_key->dk_ctfm.
978094c3ceSEric Biggers 	 */
98a828daabSEric Biggers 	struct fscrypt_direct_key *ci_direct_key;
998094c3ceSEric Biggers 
1008094c3ceSEric Biggers 	/* fields from the fscrypt_context */
101cc4e0df0STheodore Ts'o 	u8 ci_data_mode;
102cc4e0df0STheodore Ts'o 	u8 ci_filename_mode;
103cc4e0df0STheodore Ts'o 	u8 ci_flags;
1043b6df59bSEric Biggers 	u8 ci_master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
1058094c3ceSEric Biggers 	u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE];
106cc4e0df0STheodore Ts'o };
107cc4e0df0STheodore Ts'o 
10858ae7468SRichard Weinberger typedef enum {
10958ae7468SRichard Weinberger 	FS_DECRYPT = 0,
11058ae7468SRichard Weinberger 	FS_ENCRYPT,
11158ae7468SRichard Weinberger } fscrypt_direction_t;
11258ae7468SRichard Weinberger 
113cc4e0df0STheodore Ts'o #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL		0x00000001
114cc4e0df0STheodore Ts'o 
115bb8179e5SEric Biggers static inline bool fscrypt_valid_enc_modes(u32 contents_mode,
116bb8179e5SEric Biggers 					   u32 filenames_mode)
117bb8179e5SEric Biggers {
1183b6df59bSEric Biggers 	if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
1193b6df59bSEric Biggers 	    filenames_mode == FSCRYPT_MODE_AES_128_CTS)
120bb8179e5SEric Biggers 		return true;
121bb8179e5SEric Biggers 
1223b6df59bSEric Biggers 	if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
1233b6df59bSEric Biggers 	    filenames_mode == FSCRYPT_MODE_AES_256_CTS)
124bb8179e5SEric Biggers 		return true;
125bb8179e5SEric Biggers 
1263b6df59bSEric Biggers 	if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
1273b6df59bSEric Biggers 	    filenames_mode == FSCRYPT_MODE_ADIANTUM)
1288094c3ceSEric Biggers 		return true;
1298094c3ceSEric Biggers 
130bb8179e5SEric Biggers 	return false;
131bb8179e5SEric Biggers }
132bb8179e5SEric Biggers 
133b98701dfSTheodore Ts'o /* crypto.c */
134e4de782aSEric Biggers extern struct kmem_cache *fscrypt_info_cachep;
13558ae7468SRichard Weinberger extern int fscrypt_initialize(unsigned int cop_flags);
136f47fcbb2SEric Biggers extern int fscrypt_crypt_block(const struct inode *inode,
13758ae7468SRichard Weinberger 			       fscrypt_direction_t rw, u64 lblk_num,
138f47fcbb2SEric Biggers 			       struct page *src_page, struct page *dest_page,
13958ae7468SRichard Weinberger 			       unsigned int len, unsigned int offs,
14058ae7468SRichard Weinberger 			       gfp_t gfp_flags);
141d2d0727bSEric Biggers extern struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags);
14254222025SEric Biggers extern const struct dentry_operations fscrypt_d_ops;
143b98701dfSTheodore Ts'o 
144544d08fdSEric Biggers extern void __printf(3, 4) __cold
145886da8b3SEric Biggers fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...);
146544d08fdSEric Biggers 
147886da8b3SEric Biggers #define fscrypt_warn(inode, fmt, ...)		\
148886da8b3SEric Biggers 	fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
149886da8b3SEric Biggers #define fscrypt_err(inode, fmt, ...)		\
150886da8b3SEric Biggers 	fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
151544d08fdSEric Biggers 
1528094c3ceSEric Biggers #define FSCRYPT_MAX_IV_SIZE	32
1538094c3ceSEric Biggers 
1548094c3ceSEric Biggers union fscrypt_iv {
1558094c3ceSEric Biggers 	struct {
1568094c3ceSEric Biggers 		/* logical block number within the file */
1578094c3ceSEric Biggers 		__le64 lblk_num;
1588094c3ceSEric Biggers 
1598094c3ceSEric Biggers 		/* per-file nonce; only set in DIRECT_KEY mode */
1608094c3ceSEric Biggers 		u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
1618094c3ceSEric Biggers 	};
1628094c3ceSEric Biggers 	u8 raw[FSCRYPT_MAX_IV_SIZE];
1638094c3ceSEric Biggers };
1648094c3ceSEric Biggers 
1658094c3ceSEric Biggers void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
1668094c3ceSEric Biggers 			 const struct fscrypt_info *ci);
1678094c3ceSEric Biggers 
16876e81d6dSEric Biggers /* fname.c */
16950c961deSEric Biggers extern int fname_encrypt(struct inode *inode, const struct qstr *iname,
17050c961deSEric Biggers 			 u8 *out, unsigned int olen);
171b9db0b4aSEric Biggers extern bool fscrypt_fname_encrypted_size(const struct inode *inode,
172b9db0b4aSEric Biggers 					 u32 orig_len, u32 max_len,
173b9db0b4aSEric Biggers 					 u32 *encrypted_len_ret);
17476e81d6dSEric Biggers 
17522d94f49SEric Biggers /* keyring.c */
17622d94f49SEric Biggers 
17722d94f49SEric Biggers /*
17822d94f49SEric Biggers  * fscrypt_master_key_secret - secret key material of an in-use master key
17922d94f49SEric Biggers  */
18022d94f49SEric Biggers struct fscrypt_master_key_secret {
18122d94f49SEric Biggers 
18222d94f49SEric Biggers 	/* Size of the raw key in bytes */
18322d94f49SEric Biggers 	u32			size;
18422d94f49SEric Biggers 
18522d94f49SEric Biggers 	/* The raw key */
18622d94f49SEric Biggers 	u8			raw[FSCRYPT_MAX_KEY_SIZE];
18722d94f49SEric Biggers 
18822d94f49SEric Biggers } __randomize_layout;
18922d94f49SEric Biggers 
19022d94f49SEric Biggers /*
19122d94f49SEric Biggers  * fscrypt_master_key - an in-use master key
19222d94f49SEric Biggers  *
19322d94f49SEric Biggers  * This represents a master encryption key which has been added to the
19422d94f49SEric Biggers  * filesystem and can be used to "unlock" the encrypted files which were
19522d94f49SEric Biggers  * encrypted with it.
19622d94f49SEric Biggers  */
19722d94f49SEric Biggers struct fscrypt_master_key {
19822d94f49SEric Biggers 
199*b1c0ec35SEric Biggers 	/*
200*b1c0ec35SEric Biggers 	 * The secret key material.  After FS_IOC_REMOVE_ENCRYPTION_KEY is
201*b1c0ec35SEric Biggers 	 * executed, this is wiped and no new inodes can be unlocked with this
202*b1c0ec35SEric Biggers 	 * key; however, there may still be inodes in ->mk_decrypted_inodes
203*b1c0ec35SEric Biggers 	 * which could not be evicted.  As long as some inodes still remain,
204*b1c0ec35SEric Biggers 	 * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or
205*b1c0ec35SEric Biggers 	 * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again.
206*b1c0ec35SEric Biggers 	 *
207*b1c0ec35SEric Biggers 	 * Locking: protected by key->sem.
208*b1c0ec35SEric Biggers 	 */
20922d94f49SEric Biggers 	struct fscrypt_master_key_secret	mk_secret;
21022d94f49SEric Biggers 
21122d94f49SEric Biggers 	/* Arbitrary key descriptor which was assigned by userspace */
21222d94f49SEric Biggers 	struct fscrypt_key_specifier		mk_spec;
21322d94f49SEric Biggers 
214*b1c0ec35SEric Biggers 	/*
215*b1c0ec35SEric Biggers 	 * Length of ->mk_decrypted_inodes, plus one if mk_secret is present.
216*b1c0ec35SEric Biggers 	 * Once this goes to 0, the master key is removed from ->s_master_keys.
217*b1c0ec35SEric Biggers 	 * The 'struct fscrypt_master_key' will continue to live as long as the
218*b1c0ec35SEric Biggers 	 * 'struct key' whose payload it is, but we won't let this reference
219*b1c0ec35SEric Biggers 	 * count rise again.
220*b1c0ec35SEric Biggers 	 */
221*b1c0ec35SEric Biggers 	refcount_t		mk_refcount;
222*b1c0ec35SEric Biggers 
223*b1c0ec35SEric Biggers 	/*
224*b1c0ec35SEric Biggers 	 * List of inodes that were unlocked using this key.  This allows the
225*b1c0ec35SEric Biggers 	 * inodes to be evicted efficiently if the key is removed.
226*b1c0ec35SEric Biggers 	 */
227*b1c0ec35SEric Biggers 	struct list_head	mk_decrypted_inodes;
228*b1c0ec35SEric Biggers 	spinlock_t		mk_decrypted_inodes_lock;
229*b1c0ec35SEric Biggers 
23022d94f49SEric Biggers } __randomize_layout;
23122d94f49SEric Biggers 
232*b1c0ec35SEric Biggers static inline bool
233*b1c0ec35SEric Biggers is_master_key_secret_present(const struct fscrypt_master_key_secret *secret)
234*b1c0ec35SEric Biggers {
235*b1c0ec35SEric Biggers 	/*
236*b1c0ec35SEric Biggers 	 * The READ_ONCE() is only necessary for fscrypt_drop_inode() and
237*b1c0ec35SEric Biggers 	 * fscrypt_key_describe().  These run in atomic context, so they can't
238*b1c0ec35SEric Biggers 	 * take key->sem and thus 'secret' can change concurrently which would
239*b1c0ec35SEric Biggers 	 * be a data race.  But they only need to know whether the secret *was*
240*b1c0ec35SEric Biggers 	 * present at the time of check, so READ_ONCE() suffices.
241*b1c0ec35SEric Biggers 	 */
242*b1c0ec35SEric Biggers 	return READ_ONCE(secret->size) != 0;
243*b1c0ec35SEric Biggers }
244*b1c0ec35SEric Biggers 
24522d94f49SEric Biggers static inline const char *master_key_spec_type(
24622d94f49SEric Biggers 				const struct fscrypt_key_specifier *spec)
24722d94f49SEric Biggers {
24822d94f49SEric Biggers 	switch (spec->type) {
24922d94f49SEric Biggers 	case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
25022d94f49SEric Biggers 		return "descriptor";
25122d94f49SEric Biggers 	}
25222d94f49SEric Biggers 	return "[unknown]";
25322d94f49SEric Biggers }
25422d94f49SEric Biggers 
25522d94f49SEric Biggers static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
25622d94f49SEric Biggers {
25722d94f49SEric Biggers 	switch (spec->type) {
25822d94f49SEric Biggers 	case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
25922d94f49SEric Biggers 		return FSCRYPT_KEY_DESCRIPTOR_SIZE;
26022d94f49SEric Biggers 	}
26122d94f49SEric Biggers 	return 0;
26222d94f49SEric Biggers }
26322d94f49SEric Biggers 
26422d94f49SEric Biggers extern struct key *
26522d94f49SEric Biggers fscrypt_find_master_key(struct super_block *sb,
26622d94f49SEric Biggers 			const struct fscrypt_key_specifier *mk_spec);
26722d94f49SEric Biggers 
26822d94f49SEric Biggers extern int __init fscrypt_init_keyring(void);
26922d94f49SEric Biggers 
270feed8258SEric Biggers /* keysetup.c */
2718094c3ceSEric Biggers 
2728094c3ceSEric Biggers struct fscrypt_mode {
2738094c3ceSEric Biggers 	const char *friendly_name;
2748094c3ceSEric Biggers 	const char *cipher_str;
2758094c3ceSEric Biggers 	int keysize;
2768094c3ceSEric Biggers 	int ivsize;
2778094c3ceSEric Biggers 	bool logged_impl_name;
2788094c3ceSEric Biggers 	bool needs_essiv;
2798094c3ceSEric Biggers };
2808094c3ceSEric Biggers 
2813ec4f2a6SEric Biggers static inline bool
2823ec4f2a6SEric Biggers fscrypt_mode_supports_direct_key(const struct fscrypt_mode *mode)
2833ec4f2a6SEric Biggers {
2843ec4f2a6SEric Biggers 	return mode->ivsize >= offsetofend(union fscrypt_iv, nonce);
2853ec4f2a6SEric Biggers }
2863ec4f2a6SEric Biggers 
2870109ce76SEric Biggers extern struct crypto_skcipher *
2880109ce76SEric Biggers fscrypt_allocate_skcipher(struct fscrypt_mode *mode, const u8 *raw_key,
2890109ce76SEric Biggers 			  const struct inode *inode);
2900109ce76SEric Biggers 
2910109ce76SEric Biggers extern int fscrypt_set_derived_key(struct fscrypt_info *ci,
2920109ce76SEric Biggers 				   const u8 *derived_key);
2930109ce76SEric Biggers 
2940109ce76SEric Biggers /* keysetup_v1.c */
2950109ce76SEric Biggers 
2960109ce76SEric Biggers extern void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
2970109ce76SEric Biggers 
2980109ce76SEric Biggers extern int fscrypt_setup_v1_file_key(struct fscrypt_info *ci,
2990109ce76SEric Biggers 				     const u8 *raw_master_key);
3000109ce76SEric Biggers 
3010109ce76SEric Biggers extern int fscrypt_setup_v1_file_key_via_subscribed_keyrings(
3020109ce76SEric Biggers 					struct fscrypt_info *ci);
3030109ce76SEric Biggers 
3043325bea5STheodore Ts'o #endif /* _FSCRYPT_PRIVATE_H */
305