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