Whamcloud - gitweb
fbf7c73a233c04a907963a7207cbedf3669e723b
[fs/lustre-release.git] / libcfs / libcfs / crypto / llcrypt_private.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * llcrypt_private.h
4  *
5  * Copyright (C) 2015, Google, Inc.
6  *
7  * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
8  * Heavily modified since then.
9  */
10 /*
11  * Linux commit 219d54332a09
12  * tags/v5.4
13  */
14
15 #ifndef _LLCRYPT_PRIVATE_H
16 #define _LLCRYPT_PRIVATE_H
17
18 #include <libcfs/crypto/llcrypt.h>
19 #include <crypto/hash.h>
20 #include <lustre_disk.h>
21
22 #ifndef CRYPTO_TFM_REQ_FORBID_WEAK_KEYS
23 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS CRYPTO_TFM_REQ_WEAK_KEY
24 #endif
25
26 #define llcrypt_info(inode)          ((struct llcrypt_info *)(inode)->i_private)
27 #define llcrypt_info_nocast(inode)   ((inode)->i_private)
28
29 #define CONST_STRLEN(str)       (sizeof(str) - 1)
30
31 #define FS_KEY_DERIVATION_NONCE_SIZE    16
32
33 #define LLCRYPT_MIN_KEY_SIZE            16
34
35 #define LLCRYPT_CONTEXT_V1      1
36 #define LLCRYPT_CONTEXT_V2      2
37
38 struct llcrypt_context_v1 {
39         u8 version; /* LLCRYPT_CONTEXT_V1 */
40         u8 contents_encryption_mode;
41         u8 filenames_encryption_mode;
42         u8 flags;
43         u8 master_key_descriptor[LLCRYPT_KEY_DESCRIPTOR_SIZE];
44         u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
45 };
46
47 struct llcrypt_context_v2 {
48         u8 version; /* LLCRYPT_CONTEXT_V2 */
49         u8 contents_encryption_mode;
50         u8 filenames_encryption_mode;
51         u8 flags;
52         u8 __reserved[4];
53         u8 master_key_identifier[LLCRYPT_KEY_IDENTIFIER_SIZE];
54         u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
55 };
56
57 /**
58  * llcrypt_context - the encryption context of an inode
59  *
60  * This is the on-disk equivalent of an llcrypt_policy, stored alongside each
61  * encrypted file usually in a hidden extended attribute.  It contains the
62  * fields from the llcrypt_policy, in order to identify the encryption algorithm
63  * and key with which the file is encrypted.  It also contains a nonce that was
64  * randomly generated by llcrypt itself; this is used as KDF input or as a tweak
65  * to cause different files to be encrypted differently.
66  */
67 union llcrypt_context {
68         u8 version;
69         struct llcrypt_context_v1 v1;
70         struct llcrypt_context_v2 v2;
71 };
72
73 /*
74  * Return the size expected for the given llcrypt_context based on its version
75  * number, or 0 if the context version is unrecognized.
76  */
77 static inline int llcrypt_context_size(const union llcrypt_context *ctx)
78 {
79         switch (ctx->version) {
80         case LLCRYPT_CONTEXT_V1:
81                 BUILD_BUG_ON(sizeof(ctx->v1) != 28);
82                 return sizeof(ctx->v1);
83         case LLCRYPT_CONTEXT_V2:
84                 BUILD_BUG_ON(sizeof(ctx->v2) != 40);
85                 return sizeof(ctx->v2);
86         }
87         return 0;
88 }
89
90 #undef llcrypt_policy
91 union llcrypt_policy {
92         u8 version;
93         struct llcrypt_policy_v1 v1;
94         struct llcrypt_policy_v2 v2;
95 };
96
97 /*
98  * Return the size expected for the given llcrypt_policy based on its version
99  * number, or 0 if the policy version is unrecognized.
100  */
101 static inline int llcrypt_policy_size(const union llcrypt_policy *policy)
102 {
103         switch (policy->version) {
104         case LLCRYPT_POLICY_V1:
105                 return sizeof(policy->v1);
106         case LLCRYPT_POLICY_V2:
107                 return sizeof(policy->v2);
108         }
109         return 0;
110 }
111
112 /* Return the contents encryption mode of a valid encryption policy */
113 static inline u8
114 llcrypt_policy_contents_mode(const union llcrypt_policy *policy)
115 {
116         switch (policy->version) {
117         case LLCRYPT_POLICY_V1:
118                 return policy->v1.contents_encryption_mode;
119         case LLCRYPT_POLICY_V2:
120                 return policy->v2.contents_encryption_mode;
121         }
122         BUG();
123 }
124
125 /* Return the filenames encryption mode of a valid encryption policy */
126 static inline u8
127 llcrypt_policy_fnames_mode(const union llcrypt_policy *policy)
128 {
129         switch (policy->version) {
130         case LLCRYPT_POLICY_V1:
131                 return policy->v1.filenames_encryption_mode;
132         case LLCRYPT_POLICY_V2:
133                 return policy->v2.filenames_encryption_mode;
134         }
135         BUG();
136 }
137
138 /* Return the flags (LLCRYPT_POLICY_FLAG*) of a valid encryption policy */
139 static inline u8
140 llcrypt_policy_flags(const union llcrypt_policy *policy)
141 {
142         switch (policy->version) {
143         case LLCRYPT_POLICY_V1:
144                 return policy->v1.flags;
145         case LLCRYPT_POLICY_V2:
146                 return policy->v2.flags;
147         }
148         BUG();
149 }
150
151 static inline bool
152 llcrypt_is_direct_key_policy(const union llcrypt_policy *policy)
153 {
154         return llcrypt_policy_flags(policy) & LLCRYPT_POLICY_FLAG_DIRECT_KEY;
155 }
156
157 /**
158  * For encrypted symlinks, the ciphertext length is stored at the beginning
159  * of the string in little-endian format.
160  */
161 struct llcrypt_symlink_data {
162         __le16 len;
163         char encrypted_path[1];
164 } __packed;
165
166 /*
167  * llcrypt_info - the "encryption key" for an inode
168  *
169  * When an encrypted file's key is made available, an instance of this struct is
170  * allocated and stored in '(struct llcrypt_info *)inode->i_private'.
171  * Once created, it remains until the inode is evicted.
172  */
173 struct llcrypt_info {
174
175         /* The actual crypto transform used for encryption and decryption */
176         struct crypto_skcipher *ci_ctfm;
177
178         /*
179          * Cipher for ESSIV IV generation.  Only set for CBC contents
180          * encryption, otherwise is NULL.
181          */
182         struct crypto_cipher *ci_essiv_tfm;
183
184         /*
185          * Encryption mode used for this inode.  It corresponds to either the
186          * contents or filenames encryption mode, depending on the inode type.
187          */
188         struct llcrypt_mode *ci_mode;
189
190         /* Back-pointer to the inode */
191         struct inode *ci_inode;
192
193         /*
194          * The master key with which this inode was unlocked (decrypted).  This
195          * will be NULL if the master key was found in a process-subscribed
196          * keyring rather than in the filesystem-level keyring.
197          */
198         struct key *ci_master_key;
199
200         /*
201          * Link in list of inodes that were unlocked with the master key.
202          * Only used when ->ci_master_key is set.
203          */
204         struct list_head ci_master_key_link;
205
206         /*
207          * If non-NULL, then encryption is done using the master key directly
208          * and ci_ctfm will equal ci_direct_key->dk_ctfm.
209          */
210         struct llcrypt_direct_key *ci_direct_key;
211
212         /* The encryption policy used by this inode */
213         union llcrypt_policy ci_policy;
214
215         /* This inode's nonce, copied from the llcrypt_context */
216         u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE];
217 };
218
219 typedef enum {
220         FS_DECRYPT = 0,
221         FS_ENCRYPT,
222 } llcrypt_direction_t;
223
224 #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL         0x00000001
225
226 static inline bool llcrypt_valid_enc_modes(u32 contents_mode,
227                                            u32 filenames_mode)
228 {
229         if (contents_mode == LLCRYPT_MODE_AES_128_CBC &&
230             filenames_mode == LLCRYPT_MODE_AES_128_CTS)
231                 return true;
232
233         if (contents_mode == LLCRYPT_MODE_AES_256_XTS &&
234             filenames_mode == LLCRYPT_MODE_AES_256_CTS)
235                 return true;
236
237         if (contents_mode == LLCRYPT_MODE_ADIANTUM &&
238             filenames_mode == LLCRYPT_MODE_ADIANTUM)
239                 return true;
240
241         return false;
242 }
243
244 /* crypto.c */
245 extern struct kmem_cache *llcrypt_info_cachep;
246 extern int llcrypt_initialize(unsigned int cop_flags);
247 extern int llcrypt_crypt_block(const struct inode *inode,
248                                llcrypt_direction_t rw, u64 lblk_num,
249                                struct page *src_page, struct page *dest_page,
250                                unsigned int len, unsigned int offs,
251                                gfp_t gfp_flags);
252 extern struct page *llcrypt_alloc_bounce_page(gfp_t gfp_flags);
253 extern const struct dentry_operations llcrypt_d_ops;
254
255 extern void __printf(3, 4) __cold
256 llcrypt_msg(const struct inode *inode, int mask, const char *fmt, ...);
257
258 #define llcrypt_warn(inode, fmt, ...)           \
259         llcrypt_msg((inode), D_SEC, fmt, ##__VA_ARGS__)
260 #define llcrypt_err(inode, fmt, ...)            \
261         llcrypt_msg((inode), D_ERROR, fmt, ##__VA_ARGS__)
262
263 #define LLCRYPT_MAX_IV_SIZE     32
264
265 union llcrypt_iv {
266         struct {
267                 /* logical block number within the file */
268                 __le64 lblk_num;
269
270                 /* per-file nonce; only set in DIRECT_KEY mode */
271                 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
272         };
273         u8 raw[LLCRYPT_MAX_IV_SIZE];
274 };
275
276 void llcrypt_generate_iv(union llcrypt_iv *iv, u64 lblk_num,
277                          const struct llcrypt_info *ci);
278
279 /* fname.c */
280 extern int fname_encrypt(struct inode *inode, const struct qstr *iname,
281                          u8 *out, unsigned int olen);
282 extern bool llcrypt_fname_encrypted_size(const struct inode *inode,
283                                          u32 orig_len, u32 max_len,
284                                          u32 *encrypted_len_ret);
285
286 /* hkdf.c */
287
288 struct llcrypt_hkdf {
289         struct crypto_shash *hmac_tfm;
290 };
291
292 extern int llcrypt_init_hkdf(struct llcrypt_hkdf *hkdf, const u8 *master_key,
293                              unsigned int master_key_size);
294
295 /*
296  * The list of contexts in which llcrypt uses HKDF.  These values are used as
297  * the first byte of the HKDF application-specific info string to guarantee that
298  * info strings are never repeated between contexts.  This ensures that all HKDF
299  * outputs are unique and cryptographically isolated, i.e. knowledge of one
300  * output doesn't reveal another.
301  */
302 #define HKDF_CONTEXT_KEY_IDENTIFIER     1
303 #define HKDF_CONTEXT_PER_FILE_KEY       2
304 #define HKDF_CONTEXT_PER_MODE_KEY       3
305
306 extern int llcrypt_hkdf_expand(struct llcrypt_hkdf *hkdf, u8 context,
307                                const u8 *info, unsigned int infolen,
308                                u8 *okm, unsigned int okmlen);
309
310 extern void llcrypt_destroy_hkdf(struct llcrypt_hkdf *hkdf);
311
312 /* keyring.c */
313
314 /*
315  * llcrypt_master_key_secret - secret key material of an in-use master key
316  */
317 struct llcrypt_master_key_secret {
318
319         /*
320          * For v2 policy keys: HKDF context keyed by this master key.
321          * For v1 policy keys: not set (hkdf.hmac_tfm == NULL).
322          */
323         struct llcrypt_hkdf     hkdf;
324
325         /* Size of the raw key in bytes.  Set even if ->raw isn't set. */
326         u32                     size;
327
328         /* For v1 policy keys: the raw key.  Wiped for v2 policy keys. */
329         u8                      raw[LLCRYPT_MAX_KEY_SIZE];
330
331 } __randomize_layout;
332
333 /*
334  * llcrypt_master_key - an in-use master key
335  *
336  * This represents a master encryption key which has been added to the
337  * filesystem and can be used to "unlock" the encrypted files which were
338  * encrypted with it.
339  */
340 struct llcrypt_master_key {
341
342         /*
343          * The secret key material.  After LL_IOC_REMOVE_ENCRYPTION_KEY is
344          * executed, this is wiped and no new inodes can be unlocked with this
345          * key; however, there may still be inodes in ->mk_decrypted_inodes
346          * which could not be evicted.  As long as some inodes still remain,
347          * LL_IOC_REMOVE_ENCRYPTION_KEY can be retried, or
348          * LL_IOC_ADD_ENCRYPTION_KEY can add the secret again.
349          *
350          * Locking: protected by key->sem (outer) and mk_secret_sem (inner).
351          * The reason for two locks is that key->sem also protects modifying
352          * mk_users, which ranks it above the semaphore for the keyring key
353          * type, which is in turn above page faults (via keyring_read).  But
354          * sometimes filesystems call llcrypt_get_encryption_info() from within
355          * a transaction, which ranks it below page faults.  So we need a
356          * separate lock which protects mk_secret but not also mk_users.
357          */
358         struct llcrypt_master_key_secret        mk_secret;
359         struct rw_semaphore                     mk_secret_sem;
360
361         /*
362          * For v1 policy keys: an arbitrary key descriptor which was assigned by
363          * userspace (->descriptor).
364          *
365          * For v2 policy keys: a cryptographic hash of this key (->identifier).
366          */
367         struct llcrypt_key_specifier            mk_spec;
368
369         /*
370          * Keyring which contains a key of type 'key_type_llcrypt_user' for each
371          * user who has added this key.  Normally each key will be added by just
372          * one user, but it's possible that multiple users share a key, and in
373          * that case we need to keep track of those users so that one user can't
374          * remove the key before the others want it removed too.
375          *
376          * This is NULL for v1 policy keys; those can only be added by root.
377          *
378          * Locking: in addition to this keyrings own semaphore, this is
379          * protected by the master key's key->sem, so we can do atomic
380          * search+insert.  It can also be searched without taking any locks, but
381          * in that case the returned key may have already been removed.
382          */
383         struct key              *mk_users;
384
385         /*
386          * Length of ->mk_decrypted_inodes, plus one if mk_secret is present.
387          * Once this goes to 0, the master key is removed from ->lsi_master_keys.
388          * The 'struct llcrypt_master_key' will continue to live as long as the
389          * 'struct key' whose payload it is, but we won't let this reference
390          * count rise again.
391          */
392         refcount_t              mk_refcount;
393
394         /*
395          * List of inodes that were unlocked using this key.  This allows the
396          * inodes to be evicted efficiently if the key is removed.
397          */
398         struct list_head        mk_decrypted_inodes;
399         spinlock_t              mk_decrypted_inodes_lock;
400
401         /* Per-mode tfms for DIRECT_KEY policies, allocated on-demand */
402         struct crypto_skcipher  *mk_mode_keys[__LLCRYPT_MODE_MAX + 1];
403
404 } __randomize_layout;
405
406 static inline bool
407 is_master_key_secret_present(const struct llcrypt_master_key_secret *secret)
408 {
409         /*
410          * The READ_ONCE() is only necessary for llcrypt_drop_inode() and
411          * llcrypt_key_describe().  These run in atomic context, so they can't
412          * take ->mk_secret_sem and thus 'secret' can change concurrently which
413          * would be a data race.  But they only need to know whether the secret
414          * *was* present at the time of check, so READ_ONCE() suffices.
415          */
416         return READ_ONCE(secret->size) != 0;
417 }
418
419 static inline const char *master_key_spec_type(
420                                 const struct llcrypt_key_specifier *spec)
421 {
422         switch (spec->type) {
423         case LLCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
424                 return "descriptor";
425         case LLCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
426                 return "identifier";
427         }
428         return "[unknown]";
429 }
430
431 static inline int master_key_spec_len(const struct llcrypt_key_specifier *spec)
432 {
433         switch (spec->type) {
434         case LLCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
435                 return LLCRYPT_KEY_DESCRIPTOR_SIZE;
436         case LLCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
437                 return LLCRYPT_KEY_IDENTIFIER_SIZE;
438         }
439         return 0;
440 }
441
442 extern struct key *
443 llcrypt_find_master_key(struct super_block *sb,
444                         const struct llcrypt_key_specifier *mk_spec);
445
446 extern int llcrypt_verify_key_added(struct super_block *sb,
447                                     const u8 identifier[LLCRYPT_KEY_IDENTIFIER_SIZE]);
448
449 extern int __init llcrypt_init_keyring(void);
450
451 extern void __exit llcrypt_exit_keyring(void);
452
453 /* keysetup.c */
454
455 struct llcrypt_mode {
456         const char *friendly_name;
457         const char *cipher_str;
458         int keysize;
459         int ivsize;
460         bool logged_impl_name;
461         bool needs_essiv;
462 };
463
464 static inline bool
465 llcrypt_mode_supports_direct_key(const struct llcrypt_mode *mode)
466 {
467         return mode->ivsize >= offsetofend(union llcrypt_iv, nonce);
468 }
469
470 extern struct crypto_skcipher *
471 llcrypt_allocate_skcipher(struct llcrypt_mode *mode, const u8 *raw_key,
472                           const struct inode *inode);
473
474 extern int llcrypt_set_derived_key(struct llcrypt_info *ci,
475                                    const u8 *derived_key);
476
477 /* keysetup_v1.c */
478
479 extern void llcrypt_put_direct_key(struct llcrypt_direct_key *dk);
480
481 extern int llcrypt_setup_v1_file_key(struct llcrypt_info *ci,
482                                      const u8 *raw_master_key);
483
484 extern int llcrypt_setup_v1_file_key_via_subscribed_keyrings(
485                                         struct llcrypt_info *ci);
486 /* policy.c */
487
488 extern bool llcrypt_policies_equal(const union llcrypt_policy *policy1,
489                                    const union llcrypt_policy *policy2);
490 extern bool llcrypt_supported_policy(const union llcrypt_policy *policy_u,
491                                      const struct inode *inode);
492 extern int llcrypt_policy_from_context(union llcrypt_policy *policy_u,
493                                        const union llcrypt_context *ctx_u,
494                                        int ctx_size);
495
496 #endif /* _LLCRYPT_PRIVATE_H */