1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * fscrypt_private.h
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2015, Google, Inc.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
8*4882a593Smuzhiyun * Heavily modified since then.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #ifndef _FSCRYPT_PRIVATE_H
12*4882a593Smuzhiyun #define _FSCRYPT_PRIVATE_H
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/fscrypt.h>
15*4882a593Smuzhiyun #include <linux/siphash.h>
16*4882a593Smuzhiyun #include <crypto/hash.h>
17*4882a593Smuzhiyun #include <linux/blk-crypto.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #define CONST_STRLEN(str) (sizeof(str) - 1)
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define FSCRYPT_FILE_NONCE_SIZE 16
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #define FSCRYPT_MIN_KEY_SIZE 16
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #define FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE 128
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #define FSCRYPT_CONTEXT_V1 1
28*4882a593Smuzhiyun #define FSCRYPT_CONTEXT_V2 2
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /* Keep this in sync with include/uapi/linux/fscrypt.h */
31*4882a593Smuzhiyun #define FSCRYPT_MODE_MAX FSCRYPT_MODE_ADIANTUM
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun struct fscrypt_context_v1 {
34*4882a593Smuzhiyun u8 version; /* FSCRYPT_CONTEXT_V1 */
35*4882a593Smuzhiyun u8 contents_encryption_mode;
36*4882a593Smuzhiyun u8 filenames_encryption_mode;
37*4882a593Smuzhiyun u8 flags;
38*4882a593Smuzhiyun u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
39*4882a593Smuzhiyun u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun struct fscrypt_context_v2 {
43*4882a593Smuzhiyun u8 version; /* FSCRYPT_CONTEXT_V2 */
44*4882a593Smuzhiyun u8 contents_encryption_mode;
45*4882a593Smuzhiyun u8 filenames_encryption_mode;
46*4882a593Smuzhiyun u8 flags;
47*4882a593Smuzhiyun u8 __reserved[4];
48*4882a593Smuzhiyun u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
49*4882a593Smuzhiyun u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun * fscrypt_context - the encryption context of an inode
54*4882a593Smuzhiyun *
55*4882a593Smuzhiyun * This is the on-disk equivalent of an fscrypt_policy, stored alongside each
56*4882a593Smuzhiyun * encrypted file usually in a hidden extended attribute. It contains the
57*4882a593Smuzhiyun * fields from the fscrypt_policy, in order to identify the encryption algorithm
58*4882a593Smuzhiyun * and key with which the file is encrypted. It also contains a nonce that was
59*4882a593Smuzhiyun * randomly generated by fscrypt itself; this is used as KDF input or as a tweak
60*4882a593Smuzhiyun * to cause different files to be encrypted differently.
61*4882a593Smuzhiyun */
62*4882a593Smuzhiyun union fscrypt_context {
63*4882a593Smuzhiyun u8 version;
64*4882a593Smuzhiyun struct fscrypt_context_v1 v1;
65*4882a593Smuzhiyun struct fscrypt_context_v2 v2;
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /*
69*4882a593Smuzhiyun * Return the size expected for the given fscrypt_context based on its version
70*4882a593Smuzhiyun * number, or 0 if the context version is unrecognized.
71*4882a593Smuzhiyun */
fscrypt_context_size(const union fscrypt_context * ctx)72*4882a593Smuzhiyun static inline int fscrypt_context_size(const union fscrypt_context *ctx)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun switch (ctx->version) {
75*4882a593Smuzhiyun case FSCRYPT_CONTEXT_V1:
76*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(ctx->v1) != 28);
77*4882a593Smuzhiyun return sizeof(ctx->v1);
78*4882a593Smuzhiyun case FSCRYPT_CONTEXT_V2:
79*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(ctx->v2) != 40);
80*4882a593Smuzhiyun return sizeof(ctx->v2);
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun return 0;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /* Check whether an fscrypt_context has a recognized version number and size */
fscrypt_context_is_valid(const union fscrypt_context * ctx,int ctx_size)86*4882a593Smuzhiyun static inline bool fscrypt_context_is_valid(const union fscrypt_context *ctx,
87*4882a593Smuzhiyun int ctx_size)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun return ctx_size >= 1 && ctx_size == fscrypt_context_size(ctx);
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /* Retrieve the context's nonce, assuming the context was already validated */
fscrypt_context_nonce(const union fscrypt_context * ctx)93*4882a593Smuzhiyun static inline const u8 *fscrypt_context_nonce(const union fscrypt_context *ctx)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun switch (ctx->version) {
96*4882a593Smuzhiyun case FSCRYPT_CONTEXT_V1:
97*4882a593Smuzhiyun return ctx->v1.nonce;
98*4882a593Smuzhiyun case FSCRYPT_CONTEXT_V2:
99*4882a593Smuzhiyun return ctx->v2.nonce;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun WARN_ON(1);
102*4882a593Smuzhiyun return NULL;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun union fscrypt_policy {
106*4882a593Smuzhiyun u8 version;
107*4882a593Smuzhiyun struct fscrypt_policy_v1 v1;
108*4882a593Smuzhiyun struct fscrypt_policy_v2 v2;
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /*
112*4882a593Smuzhiyun * Return the size expected for the given fscrypt_policy based on its version
113*4882a593Smuzhiyun * number, or 0 if the policy version is unrecognized.
114*4882a593Smuzhiyun */
fscrypt_policy_size(const union fscrypt_policy * policy)115*4882a593Smuzhiyun static inline int fscrypt_policy_size(const union fscrypt_policy *policy)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun switch (policy->version) {
118*4882a593Smuzhiyun case FSCRYPT_POLICY_V1:
119*4882a593Smuzhiyun return sizeof(policy->v1);
120*4882a593Smuzhiyun case FSCRYPT_POLICY_V2:
121*4882a593Smuzhiyun return sizeof(policy->v2);
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun return 0;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /* Return the contents encryption mode of a valid encryption policy */
127*4882a593Smuzhiyun static inline u8
fscrypt_policy_contents_mode(const union fscrypt_policy * policy)128*4882a593Smuzhiyun fscrypt_policy_contents_mode(const union fscrypt_policy *policy)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun switch (policy->version) {
131*4882a593Smuzhiyun case FSCRYPT_POLICY_V1:
132*4882a593Smuzhiyun return policy->v1.contents_encryption_mode;
133*4882a593Smuzhiyun case FSCRYPT_POLICY_V2:
134*4882a593Smuzhiyun return policy->v2.contents_encryption_mode;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun BUG();
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /* Return the filenames encryption mode of a valid encryption policy */
140*4882a593Smuzhiyun static inline u8
fscrypt_policy_fnames_mode(const union fscrypt_policy * policy)141*4882a593Smuzhiyun fscrypt_policy_fnames_mode(const union fscrypt_policy *policy)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun switch (policy->version) {
144*4882a593Smuzhiyun case FSCRYPT_POLICY_V1:
145*4882a593Smuzhiyun return policy->v1.filenames_encryption_mode;
146*4882a593Smuzhiyun case FSCRYPT_POLICY_V2:
147*4882a593Smuzhiyun return policy->v2.filenames_encryption_mode;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun BUG();
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */
153*4882a593Smuzhiyun static inline u8
fscrypt_policy_flags(const union fscrypt_policy * policy)154*4882a593Smuzhiyun fscrypt_policy_flags(const union fscrypt_policy *policy)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun switch (policy->version) {
157*4882a593Smuzhiyun case FSCRYPT_POLICY_V1:
158*4882a593Smuzhiyun return policy->v1.flags;
159*4882a593Smuzhiyun case FSCRYPT_POLICY_V2:
160*4882a593Smuzhiyun return policy->v2.flags;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun BUG();
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /*
166*4882a593Smuzhiyun * For encrypted symlinks, the ciphertext length is stored at the beginning
167*4882a593Smuzhiyun * of the string in little-endian format.
168*4882a593Smuzhiyun */
169*4882a593Smuzhiyun struct fscrypt_symlink_data {
170*4882a593Smuzhiyun __le16 len;
171*4882a593Smuzhiyun char encrypted_path[1];
172*4882a593Smuzhiyun } __packed;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /**
175*4882a593Smuzhiyun * struct fscrypt_prepared_key - a key prepared for actual encryption/decryption
176*4882a593Smuzhiyun * @tfm: crypto API transform object
177*4882a593Smuzhiyun * @blk_key: key for blk-crypto
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * Normally only one of the fields will be non-NULL.
180*4882a593Smuzhiyun */
181*4882a593Smuzhiyun struct fscrypt_prepared_key {
182*4882a593Smuzhiyun struct crypto_skcipher *tfm;
183*4882a593Smuzhiyun #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
184*4882a593Smuzhiyun struct fscrypt_blk_crypto_key *blk_key;
185*4882a593Smuzhiyun #endif
186*4882a593Smuzhiyun };
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /*
189*4882a593Smuzhiyun * fscrypt_info - the "encryption key" for an inode
190*4882a593Smuzhiyun *
191*4882a593Smuzhiyun * When an encrypted file's key is made available, an instance of this struct is
192*4882a593Smuzhiyun * allocated and stored in ->i_crypt_info. Once created, it remains until the
193*4882a593Smuzhiyun * inode is evicted.
194*4882a593Smuzhiyun */
195*4882a593Smuzhiyun struct fscrypt_info {
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun /* The key in a form prepared for actual encryption/decryption */
198*4882a593Smuzhiyun struct fscrypt_prepared_key ci_enc_key;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /* True if ci_enc_key should be freed when this fscrypt_info is freed */
201*4882a593Smuzhiyun bool ci_owns_key;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
204*4882a593Smuzhiyun /*
205*4882a593Smuzhiyun * True if this inode will use inline encryption (blk-crypto) instead of
206*4882a593Smuzhiyun * the traditional filesystem-layer encryption.
207*4882a593Smuzhiyun */
208*4882a593Smuzhiyun bool ci_inlinecrypt;
209*4882a593Smuzhiyun #endif
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /*
212*4882a593Smuzhiyun * Encryption mode used for this inode. It corresponds to either the
213*4882a593Smuzhiyun * contents or filenames encryption mode, depending on the inode type.
214*4882a593Smuzhiyun */
215*4882a593Smuzhiyun struct fscrypt_mode *ci_mode;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /* Back-pointer to the inode */
218*4882a593Smuzhiyun struct inode *ci_inode;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /*
221*4882a593Smuzhiyun * The master key with which this inode was unlocked (decrypted). This
222*4882a593Smuzhiyun * will be NULL if the master key was found in a process-subscribed
223*4882a593Smuzhiyun * keyring rather than in the filesystem-level keyring.
224*4882a593Smuzhiyun */
225*4882a593Smuzhiyun #ifdef __GENKSYMS__
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun * Android ABI CRC preservation due to commit 391cceee6d43 ("fscrypt:
228*4882a593Smuzhiyun * stop using keyrings subsystem for fscrypt_master_key") changing this
229*4882a593Smuzhiyun * type. Size is the same, this is a private field.
230*4882a593Smuzhiyun */
231*4882a593Smuzhiyun struct key *ci_master_key;
232*4882a593Smuzhiyun #else
233*4882a593Smuzhiyun struct fscrypt_master_key *ci_master_key;
234*4882a593Smuzhiyun #endif
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun /*
237*4882a593Smuzhiyun * Link in list of inodes that were unlocked with the master key.
238*4882a593Smuzhiyun * Only used when ->ci_master_key is set.
239*4882a593Smuzhiyun */
240*4882a593Smuzhiyun struct list_head ci_master_key_link;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /*
243*4882a593Smuzhiyun * If non-NULL, then encryption is done using the master key directly
244*4882a593Smuzhiyun * and ci_enc_key will equal ci_direct_key->dk_key.
245*4882a593Smuzhiyun */
246*4882a593Smuzhiyun struct fscrypt_direct_key *ci_direct_key;
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /*
249*4882a593Smuzhiyun * This inode's hash key for filenames. This is a 128-bit SipHash-2-4
250*4882a593Smuzhiyun * key. This is only set for directories that use a keyed dirhash over
251*4882a593Smuzhiyun * the plaintext filenames -- currently just casefolded directories.
252*4882a593Smuzhiyun */
253*4882a593Smuzhiyun siphash_key_t ci_dirhash_key;
254*4882a593Smuzhiyun bool ci_dirhash_key_initialized;
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun /* The encryption policy used by this inode */
257*4882a593Smuzhiyun union fscrypt_policy ci_policy;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun /* This inode's nonce, copied from the fscrypt_context */
260*4882a593Smuzhiyun u8 ci_nonce[FSCRYPT_FILE_NONCE_SIZE];
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun /* Hashed inode number. Only set for IV_INO_LBLK_32 */
263*4882a593Smuzhiyun u32 ci_hashed_ino;
264*4882a593Smuzhiyun };
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun typedef enum {
267*4882a593Smuzhiyun FS_DECRYPT = 0,
268*4882a593Smuzhiyun FS_ENCRYPT,
269*4882a593Smuzhiyun } fscrypt_direction_t;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /* crypto.c */
272*4882a593Smuzhiyun extern struct kmem_cache *fscrypt_info_cachep;
273*4882a593Smuzhiyun int fscrypt_initialize(unsigned int cop_flags);
274*4882a593Smuzhiyun int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw,
275*4882a593Smuzhiyun u64 lblk_num, struct page *src_page,
276*4882a593Smuzhiyun struct page *dest_page, unsigned int len,
277*4882a593Smuzhiyun unsigned int offs, gfp_t gfp_flags);
278*4882a593Smuzhiyun struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun void __printf(3, 4) __cold
281*4882a593Smuzhiyun fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun #define fscrypt_warn(inode, fmt, ...) \
284*4882a593Smuzhiyun fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
285*4882a593Smuzhiyun #define fscrypt_err(inode, fmt, ...) \
286*4882a593Smuzhiyun fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun #define FSCRYPT_MAX_IV_SIZE 32
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun union fscrypt_iv {
291*4882a593Smuzhiyun struct {
292*4882a593Smuzhiyun /* logical block number within the file */
293*4882a593Smuzhiyun __le64 lblk_num;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /* per-file nonce; only set in DIRECT_KEY mode */
296*4882a593Smuzhiyun u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
297*4882a593Smuzhiyun };
298*4882a593Smuzhiyun u8 raw[FSCRYPT_MAX_IV_SIZE];
299*4882a593Smuzhiyun __le64 dun[FSCRYPT_MAX_IV_SIZE / sizeof(__le64)];
300*4882a593Smuzhiyun };
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
303*4882a593Smuzhiyun const struct fscrypt_info *ci);
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun /* fname.c */
306*4882a593Smuzhiyun int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
307*4882a593Smuzhiyun u8 *out, unsigned int olen);
308*4882a593Smuzhiyun bool fscrypt_fname_encrypted_size(const union fscrypt_policy *policy,
309*4882a593Smuzhiyun u32 orig_len, u32 max_len,
310*4882a593Smuzhiyun u32 *encrypted_len_ret);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /* hkdf.c */
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun struct fscrypt_hkdf {
315*4882a593Smuzhiyun struct crypto_shash *hmac_tfm;
316*4882a593Smuzhiyun };
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
319*4882a593Smuzhiyun unsigned int master_key_size);
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun /*
322*4882a593Smuzhiyun * The list of contexts in which fscrypt uses HKDF. These values are used as
323*4882a593Smuzhiyun * the first byte of the HKDF application-specific info string to guarantee that
324*4882a593Smuzhiyun * info strings are never repeated between contexts. This ensures that all HKDF
325*4882a593Smuzhiyun * outputs are unique and cryptographically isolated, i.e. knowledge of one
326*4882a593Smuzhiyun * output doesn't reveal another.
327*4882a593Smuzhiyun */
328*4882a593Smuzhiyun #define HKDF_CONTEXT_KEY_IDENTIFIER 1 /* info=<empty> */
329*4882a593Smuzhiyun #define HKDF_CONTEXT_PER_FILE_ENC_KEY 2 /* info=file_nonce */
330*4882a593Smuzhiyun #define HKDF_CONTEXT_DIRECT_KEY 3 /* info=mode_num */
331*4882a593Smuzhiyun #define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4 /* info=mode_num||fs_uuid */
332*4882a593Smuzhiyun #define HKDF_CONTEXT_DIRHASH_KEY 5 /* info=file_nonce */
333*4882a593Smuzhiyun #define HKDF_CONTEXT_IV_INO_LBLK_32_KEY 6 /* info=mode_num||fs_uuid */
334*4882a593Smuzhiyun #define HKDF_CONTEXT_INODE_HASH_KEY 7 /* info=<empty> */
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
337*4882a593Smuzhiyun const u8 *info, unsigned int infolen,
338*4882a593Smuzhiyun u8 *okm, unsigned int okmlen);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf);
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun /* inline_crypt.c */
343*4882a593Smuzhiyun #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
344*4882a593Smuzhiyun int fscrypt_select_encryption_impl(struct fscrypt_info *ci,
345*4882a593Smuzhiyun bool is_hw_wrapped_key);
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun static inline bool
fscrypt_using_inline_encryption(const struct fscrypt_info * ci)348*4882a593Smuzhiyun fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun return ci->ci_inlinecrypt;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
354*4882a593Smuzhiyun const u8 *raw_key,
355*4882a593Smuzhiyun unsigned int raw_key_size,
356*4882a593Smuzhiyun bool is_hw_wrapped,
357*4882a593Smuzhiyun const struct fscrypt_info *ci);
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun void fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key);
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun extern int fscrypt_derive_raw_secret(struct super_block *sb,
362*4882a593Smuzhiyun const u8 *wrapped_key,
363*4882a593Smuzhiyun unsigned int wrapped_key_size,
364*4882a593Smuzhiyun u8 *raw_secret,
365*4882a593Smuzhiyun unsigned int raw_secret_size);
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun /*
368*4882a593Smuzhiyun * Check whether the crypto transform or blk-crypto key has been allocated in
369*4882a593Smuzhiyun * @prep_key, depending on which encryption implementation the file will use.
370*4882a593Smuzhiyun */
371*4882a593Smuzhiyun static inline bool
fscrypt_is_key_prepared(struct fscrypt_prepared_key * prep_key,const struct fscrypt_info * ci)372*4882a593Smuzhiyun fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
373*4882a593Smuzhiyun const struct fscrypt_info *ci)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun /*
376*4882a593Smuzhiyun * The two smp_load_acquire()'s here pair with the smp_store_release()'s
377*4882a593Smuzhiyun * in fscrypt_prepare_inline_crypt_key() and fscrypt_prepare_key().
378*4882a593Smuzhiyun * I.e., in some cases (namely, if this prep_key is a per-mode
379*4882a593Smuzhiyun * encryption key) another task can publish blk_key or tfm concurrently,
380*4882a593Smuzhiyun * executing a RELEASE barrier. We need to use smp_load_acquire() here
381*4882a593Smuzhiyun * to safely ACQUIRE the memory the other task published.
382*4882a593Smuzhiyun */
383*4882a593Smuzhiyun if (fscrypt_using_inline_encryption(ci))
384*4882a593Smuzhiyun return smp_load_acquire(&prep_key->blk_key) != NULL;
385*4882a593Smuzhiyun return smp_load_acquire(&prep_key->tfm) != NULL;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
389*4882a593Smuzhiyun
fscrypt_select_encryption_impl(struct fscrypt_info * ci,bool is_hw_wrapped_key)390*4882a593Smuzhiyun static inline int fscrypt_select_encryption_impl(struct fscrypt_info *ci,
391*4882a593Smuzhiyun bool is_hw_wrapped_key)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun return 0;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun static inline bool
fscrypt_using_inline_encryption(const struct fscrypt_info * ci)397*4882a593Smuzhiyun fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun return false;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun static inline int
fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key * prep_key,const u8 * raw_key,unsigned int raw_key_size,bool is_hw_wrapped,const struct fscrypt_info * ci)403*4882a593Smuzhiyun fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
404*4882a593Smuzhiyun const u8 *raw_key, unsigned int raw_key_size,
405*4882a593Smuzhiyun bool is_hw_wrapped,
406*4882a593Smuzhiyun const struct fscrypt_info *ci)
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun WARN_ON(1);
409*4882a593Smuzhiyun return -EOPNOTSUPP;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun static inline void
fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key * prep_key)413*4882a593Smuzhiyun fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
fscrypt_derive_raw_secret(struct super_block * sb,const u8 * wrapped_key,unsigned int wrapped_key_size,u8 * raw_secret,unsigned int raw_secret_size)417*4882a593Smuzhiyun static inline int fscrypt_derive_raw_secret(struct super_block *sb,
418*4882a593Smuzhiyun const u8 *wrapped_key,
419*4882a593Smuzhiyun unsigned int wrapped_key_size,
420*4882a593Smuzhiyun u8 *raw_secret,
421*4882a593Smuzhiyun unsigned int raw_secret_size)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun fscrypt_warn(NULL,
424*4882a593Smuzhiyun "kernel built without support for hardware-wrapped keys");
425*4882a593Smuzhiyun return -EOPNOTSUPP;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun static inline bool
fscrypt_is_key_prepared(struct fscrypt_prepared_key * prep_key,const struct fscrypt_info * ci)429*4882a593Smuzhiyun fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
430*4882a593Smuzhiyun const struct fscrypt_info *ci)
431*4882a593Smuzhiyun {
432*4882a593Smuzhiyun return smp_load_acquire(&prep_key->tfm) != NULL;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun /* keyring.c */
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun /*
439*4882a593Smuzhiyun * fscrypt_master_key_secret - secret key material of an in-use master key
440*4882a593Smuzhiyun */
441*4882a593Smuzhiyun struct fscrypt_master_key_secret {
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun /*
444*4882a593Smuzhiyun * For v2 policy keys: HKDF context keyed by this master key.
445*4882a593Smuzhiyun * For v1 policy keys: not set (hkdf.hmac_tfm == NULL).
446*4882a593Smuzhiyun */
447*4882a593Smuzhiyun struct fscrypt_hkdf hkdf;
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /* Size of the raw key in bytes. Set even if ->raw isn't set. */
450*4882a593Smuzhiyun u32 size;
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun /* True if the key in ->raw is a hardware-wrapped key. */
453*4882a593Smuzhiyun bool is_hw_wrapped;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun /*
456*4882a593Smuzhiyun * For v1 policy keys: the raw key. Wiped for v2 policy keys, unless
457*4882a593Smuzhiyun * ->is_hw_wrapped is true, in which case this contains the wrapped key
458*4882a593Smuzhiyun * rather than the key with which 'hkdf' was keyed.
459*4882a593Smuzhiyun */
460*4882a593Smuzhiyun u8 raw[FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE];
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun } __randomize_layout;
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun /*
465*4882a593Smuzhiyun * fscrypt_master_key - an in-use master key
466*4882a593Smuzhiyun *
467*4882a593Smuzhiyun * This represents a master encryption key which has been added to the
468*4882a593Smuzhiyun * filesystem and can be used to "unlock" the encrypted files which were
469*4882a593Smuzhiyun * encrypted with it.
470*4882a593Smuzhiyun */
471*4882a593Smuzhiyun struct fscrypt_master_key {
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun /*
474*4882a593Smuzhiyun * Back-pointer to the super_block of the filesystem to which this
475*4882a593Smuzhiyun * master key has been added. Only valid if ->mk_active_refs > 0.
476*4882a593Smuzhiyun */
477*4882a593Smuzhiyun struct super_block *mk_sb;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun /*
480*4882a593Smuzhiyun * Link in ->mk_sb->s_master_keys->key_hashtable.
481*4882a593Smuzhiyun * Only valid if ->mk_active_refs > 0.
482*4882a593Smuzhiyun */
483*4882a593Smuzhiyun struct hlist_node mk_node;
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun /* Semaphore that protects ->mk_secret and ->mk_users */
486*4882a593Smuzhiyun struct rw_semaphore mk_sem;
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun /*
489*4882a593Smuzhiyun * Active and structural reference counts. An active ref guarantees
490*4882a593Smuzhiyun * that the struct continues to exist, continues to be in the keyring
491*4882a593Smuzhiyun * ->mk_sb->s_master_keys, and that any embedded subkeys (e.g.
492*4882a593Smuzhiyun * ->mk_direct_keys) that have been prepared continue to exist.
493*4882a593Smuzhiyun * A structural ref only guarantees that the struct continues to exist.
494*4882a593Smuzhiyun *
495*4882a593Smuzhiyun * There is one active ref associated with ->mk_secret being present,
496*4882a593Smuzhiyun * and one active ref for each inode in ->mk_decrypted_inodes.
497*4882a593Smuzhiyun *
498*4882a593Smuzhiyun * There is one structural ref associated with the active refcount being
499*4882a593Smuzhiyun * nonzero. Finding a key in the keyring also takes a structural ref,
500*4882a593Smuzhiyun * which is then held temporarily while the key is operated on.
501*4882a593Smuzhiyun */
502*4882a593Smuzhiyun refcount_t mk_active_refs;
503*4882a593Smuzhiyun refcount_t mk_struct_refs;
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun struct rcu_head mk_rcu_head;
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun /*
508*4882a593Smuzhiyun * The secret key material. After FS_IOC_REMOVE_ENCRYPTION_KEY is
509*4882a593Smuzhiyun * executed, this is wiped and no new inodes can be unlocked with this
510*4882a593Smuzhiyun * key; however, there may still be inodes in ->mk_decrypted_inodes
511*4882a593Smuzhiyun * which could not be evicted. As long as some inodes still remain,
512*4882a593Smuzhiyun * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or
513*4882a593Smuzhiyun * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again.
514*4882a593Smuzhiyun *
515*4882a593Smuzhiyun * While ->mk_secret is present, one ref in ->mk_active_refs is held.
516*4882a593Smuzhiyun *
517*4882a593Smuzhiyun * Locking: protected by ->mk_sem. The manipulation of ->mk_active_refs
518*4882a593Smuzhiyun * associated with this field is protected by ->mk_sem as well.
519*4882a593Smuzhiyun */
520*4882a593Smuzhiyun struct fscrypt_master_key_secret mk_secret;
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun /*
523*4882a593Smuzhiyun * For v1 policy keys: an arbitrary key descriptor which was assigned by
524*4882a593Smuzhiyun * userspace (->descriptor).
525*4882a593Smuzhiyun *
526*4882a593Smuzhiyun * For v2 policy keys: a cryptographic hash of this key (->identifier).
527*4882a593Smuzhiyun */
528*4882a593Smuzhiyun struct fscrypt_key_specifier mk_spec;
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun /*
531*4882a593Smuzhiyun * Keyring which contains a key of type 'key_type_fscrypt_user' for each
532*4882a593Smuzhiyun * user who has added this key. Normally each key will be added by just
533*4882a593Smuzhiyun * one user, but it's possible that multiple users share a key, and in
534*4882a593Smuzhiyun * that case we need to keep track of those users so that one user can't
535*4882a593Smuzhiyun * remove the key before the others want it removed too.
536*4882a593Smuzhiyun *
537*4882a593Smuzhiyun * This is NULL for v1 policy keys; those can only be added by root.
538*4882a593Smuzhiyun *
539*4882a593Smuzhiyun * Locking: protected by ->mk_sem. (We don't just rely on the keyrings
540*4882a593Smuzhiyun * subsystem semaphore ->mk_users->sem, as we need support for atomic
541*4882a593Smuzhiyun * search+insert along with proper synchronization with ->mk_secret.)
542*4882a593Smuzhiyun */
543*4882a593Smuzhiyun struct key *mk_users;
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /*
546*4882a593Smuzhiyun * List of inodes that were unlocked using this key. This allows the
547*4882a593Smuzhiyun * inodes to be evicted efficiently if the key is removed.
548*4882a593Smuzhiyun */
549*4882a593Smuzhiyun struct list_head mk_decrypted_inodes;
550*4882a593Smuzhiyun spinlock_t mk_decrypted_inodes_lock;
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun /*
553*4882a593Smuzhiyun * Per-mode encryption keys for the various types of encryption policies
554*4882a593Smuzhiyun * that use them. Allocated and derived on-demand.
555*4882a593Smuzhiyun */
556*4882a593Smuzhiyun struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1];
557*4882a593Smuzhiyun struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1];
558*4882a593Smuzhiyun struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1];
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun /* Hash key for inode numbers. Initialized only when needed. */
561*4882a593Smuzhiyun siphash_key_t mk_ino_hash_key;
562*4882a593Smuzhiyun bool mk_ino_hash_key_initialized;
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun } __randomize_layout;
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun static inline bool
is_master_key_secret_present(const struct fscrypt_master_key_secret * secret)567*4882a593Smuzhiyun is_master_key_secret_present(const struct fscrypt_master_key_secret *secret)
568*4882a593Smuzhiyun {
569*4882a593Smuzhiyun /*
570*4882a593Smuzhiyun * The READ_ONCE() is only necessary for fscrypt_drop_inode().
571*4882a593Smuzhiyun * fscrypt_drop_inode() runs in atomic context, so it can't take the key
572*4882a593Smuzhiyun * semaphore and thus 'secret' can change concurrently which would be a
573*4882a593Smuzhiyun * data race. But fscrypt_drop_inode() only need to know whether the
574*4882a593Smuzhiyun * secret *was* present at the time of check, so READ_ONCE() suffices.
575*4882a593Smuzhiyun */
576*4882a593Smuzhiyun return READ_ONCE(secret->size) != 0;
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun
master_key_spec_type(const struct fscrypt_key_specifier * spec)579*4882a593Smuzhiyun static inline const char *master_key_spec_type(
580*4882a593Smuzhiyun const struct fscrypt_key_specifier *spec)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun switch (spec->type) {
583*4882a593Smuzhiyun case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
584*4882a593Smuzhiyun return "descriptor";
585*4882a593Smuzhiyun case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
586*4882a593Smuzhiyun return "identifier";
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun return "[unknown]";
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun
master_key_spec_len(const struct fscrypt_key_specifier * spec)591*4882a593Smuzhiyun static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
592*4882a593Smuzhiyun {
593*4882a593Smuzhiyun switch (spec->type) {
594*4882a593Smuzhiyun case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
595*4882a593Smuzhiyun return FSCRYPT_KEY_DESCRIPTOR_SIZE;
596*4882a593Smuzhiyun case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
597*4882a593Smuzhiyun return FSCRYPT_KEY_IDENTIFIER_SIZE;
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun return 0;
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun void fscrypt_put_master_key(struct fscrypt_master_key *mk);
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun void fscrypt_put_master_key_activeref(struct fscrypt_master_key *mk);
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun struct fscrypt_master_key *
607*4882a593Smuzhiyun fscrypt_find_master_key(struct super_block *sb,
608*4882a593Smuzhiyun const struct fscrypt_key_specifier *mk_spec);
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun int fscrypt_add_test_dummy_key(struct super_block *sb,
611*4882a593Smuzhiyun struct fscrypt_key_specifier *key_spec);
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun int fscrypt_verify_key_added(struct super_block *sb,
614*4882a593Smuzhiyun const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun int __init fscrypt_init_keyring(void);
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun /* keysetup.c */
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun struct fscrypt_mode {
621*4882a593Smuzhiyun const char *friendly_name;
622*4882a593Smuzhiyun const char *cipher_str;
623*4882a593Smuzhiyun int keysize; /* key size in bytes */
624*4882a593Smuzhiyun int security_strength; /* security strength in bytes */
625*4882a593Smuzhiyun int ivsize; /* IV size in bytes */
626*4882a593Smuzhiyun int logged_impl_name;
627*4882a593Smuzhiyun enum blk_crypto_mode_num blk_crypto_mode;
628*4882a593Smuzhiyun };
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun extern struct fscrypt_mode fscrypt_modes[];
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,
633*4882a593Smuzhiyun const u8 *raw_key, unsigned int raw_key_size,
634*4882a593Smuzhiyun bool is_hw_wrapped, const struct fscrypt_info *ci);
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun void fscrypt_destroy_prepared_key(struct fscrypt_prepared_key *prep_key);
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key);
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
641*4882a593Smuzhiyun const struct fscrypt_master_key *mk);
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun void fscrypt_hash_inode_number(struct fscrypt_info *ci,
644*4882a593Smuzhiyun const struct fscrypt_master_key *mk);
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported);
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun /**
649*4882a593Smuzhiyun * fscrypt_require_key() - require an inode's encryption key
650*4882a593Smuzhiyun * @inode: the inode we need the key for
651*4882a593Smuzhiyun *
652*4882a593Smuzhiyun * If the inode is encrypted, set up its encryption key if not already done.
653*4882a593Smuzhiyun * Then require that the key be present and return -ENOKEY otherwise.
654*4882a593Smuzhiyun *
655*4882a593Smuzhiyun * No locks are needed, and the key will live as long as the struct inode --- so
656*4882a593Smuzhiyun * it won't go away from under you.
657*4882a593Smuzhiyun *
658*4882a593Smuzhiyun * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
659*4882a593Smuzhiyun * if a problem occurred while setting up the encryption key.
660*4882a593Smuzhiyun */
fscrypt_require_key(struct inode * inode)661*4882a593Smuzhiyun static inline int fscrypt_require_key(struct inode *inode)
662*4882a593Smuzhiyun {
663*4882a593Smuzhiyun if (IS_ENCRYPTED(inode)) {
664*4882a593Smuzhiyun int err = fscrypt_get_encryption_info(inode, false);
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun if (err)
667*4882a593Smuzhiyun return err;
668*4882a593Smuzhiyun if (!fscrypt_has_encryption_key(inode))
669*4882a593Smuzhiyun return -ENOKEY;
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun return 0;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun /* keysetup_v1.c */
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun int fscrypt_setup_v1_file_key(struct fscrypt_info *ci,
679*4882a593Smuzhiyun const u8 *raw_master_key);
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun int fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_info *ci);
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun /* policy.c */
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
686*4882a593Smuzhiyun const union fscrypt_policy *policy2);
687*4882a593Smuzhiyun bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
688*4882a593Smuzhiyun const struct inode *inode);
689*4882a593Smuzhiyun int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
690*4882a593Smuzhiyun const union fscrypt_context *ctx_u,
691*4882a593Smuzhiyun int ctx_size);
692*4882a593Smuzhiyun const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir);
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun #endif /* _FSCRYPT_PRIVATE_H */
695