1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright (C) 2015, Google, Inc.
7 * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
8 * Heavily modified since then.
11 * Linux commit 219d54332a09
15 #ifndef _LLCRYPT_PRIVATE_H
16 #define _LLCRYPT_PRIVATE_H
18 #include <libcfs/crypto/llcrypt.h>
19 #include <crypto/hash.h>
20 #include <lustre_disk.h>
22 #ifndef CRYPTO_TFM_REQ_FORBID_WEAK_KEYS
23 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS CRYPTO_TFM_REQ_WEAK_KEY
26 #define llcrypt_info(inode) ((struct llcrypt_info *)(inode)->i_private)
27 #define llcrypt_info_nocast(inode) ((inode)->i_private)
29 #define CONST_STRLEN(str) (sizeof(str) - 1)
31 #define FS_KEY_DERIVATION_NONCE_SIZE 16
33 #define LLCRYPT_MIN_KEY_SIZE 16
35 #define LLCRYPT_CONTEXT_V1 1
36 #define LLCRYPT_CONTEXT_V2 2
38 struct llcrypt_context_v1 {
39 u8 version; /* LLCRYPT_CONTEXT_V1 */
40 u8 contents_encryption_mode;
41 u8 filenames_encryption_mode;
43 u8 master_key_descriptor[LLCRYPT_KEY_DESCRIPTOR_SIZE];
44 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
47 struct llcrypt_context_v2 {
48 u8 version; /* LLCRYPT_CONTEXT_V2 */
49 u8 contents_encryption_mode;
50 u8 filenames_encryption_mode;
53 u8 master_key_identifier[LLCRYPT_KEY_IDENTIFIER_SIZE];
54 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
58 * llcrypt_context - the encryption context of an inode
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.
67 union llcrypt_context {
69 struct llcrypt_context_v1 v1;
70 struct llcrypt_context_v2 v2;
74 * Return the size expected for the given llcrypt_context based on its version
75 * number, or 0 if the context version is unrecognized.
77 static inline int llcrypt_context_size(const union llcrypt_context *ctx)
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);
91 union llcrypt_policy {
93 struct llcrypt_policy_v1 v1;
94 struct llcrypt_policy_v2 v2;
98 * Return the size expected for the given llcrypt_policy based on its version
99 * number, or 0 if the policy version is unrecognized.
101 static inline int llcrypt_policy_size(const union llcrypt_policy *policy)
103 switch (policy->version) {
104 case LLCRYPT_POLICY_V1:
105 return sizeof(policy->v1);
106 case LLCRYPT_POLICY_V2:
107 return sizeof(policy->v2);
112 /* Return the contents encryption mode of a valid encryption policy */
114 llcrypt_policy_contents_mode(const union llcrypt_policy *policy)
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;
125 /* Return the filenames encryption mode of a valid encryption policy */
127 llcrypt_policy_fnames_mode(const union llcrypt_policy *policy)
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;
138 /* Return the flags (LLCRYPT_POLICY_FLAG*) of a valid encryption policy */
140 llcrypt_policy_flags(const union llcrypt_policy *policy)
142 switch (policy->version) {
143 case LLCRYPT_POLICY_V1:
144 return policy->v1.flags;
145 case LLCRYPT_POLICY_V2:
146 return policy->v2.flags;
152 llcrypt_is_direct_key_policy(const union llcrypt_policy *policy)
154 return llcrypt_policy_flags(policy) & LLCRYPT_POLICY_FLAG_DIRECT_KEY;
158 * For encrypted symlinks, the ciphertext length is stored at the beginning
159 * of the string in little-endian format.
161 struct llcrypt_symlink_data {
163 char encrypted_path[1];
167 * llcrypt_info - the "encryption key" for an inode
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.
173 struct llcrypt_info {
175 /* The actual crypto transform used for encryption and decryption */
176 struct crypto_skcipher *ci_ctfm;
179 * Cipher for ESSIV IV generation. Only set for CBC contents
180 * encryption, otherwise is NULL.
182 struct crypto_cipher *ci_essiv_tfm;
185 * Encryption mode used for this inode. It corresponds to either the
186 * contents or filenames encryption mode, depending on the inode type.
188 struct llcrypt_mode *ci_mode;
190 /* Back-pointer to the inode */
191 struct inode *ci_inode;
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.
198 struct key *ci_master_key;
201 * Link in list of inodes that were unlocked with the master key.
202 * Only used when ->ci_master_key is set.
204 struct list_head ci_master_key_link;
207 * If non-NULL, then encryption is done using the master key directly
208 * and ci_ctfm will equal ci_direct_key->dk_ctfm.
210 struct llcrypt_direct_key *ci_direct_key;
212 /* The encryption policy used by this inode */
213 union llcrypt_policy ci_policy;
215 /* This inode's nonce, copied from the llcrypt_context */
216 u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE];
222 } llcrypt_direction_t;
224 #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
226 static inline bool llcrypt_valid_enc_modes(u32 contents_mode,
229 if (contents_mode == LLCRYPT_MODE_AES_128_CBC &&
230 filenames_mode == LLCRYPT_MODE_AES_128_CTS)
233 if (contents_mode == LLCRYPT_MODE_AES_256_XTS &&
234 filenames_mode == LLCRYPT_MODE_AES_256_CTS)
237 if (contents_mode == LLCRYPT_MODE_ADIANTUM &&
238 filenames_mode == LLCRYPT_MODE_ADIANTUM)
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,
252 extern struct page *llcrypt_alloc_bounce_page(gfp_t gfp_flags);
253 extern const struct dentry_operations llcrypt_d_ops;
255 extern void __printf(3, 4) __cold
256 llcrypt_msg(const struct inode *inode, int mask, const char *fmt, ...);
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__)
263 #define LLCRYPT_MAX_IV_SIZE 32
267 /* logical block number within the file */
270 /* per-file nonce; only set in DIRECT_KEY mode */
271 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
273 u8 raw[LLCRYPT_MAX_IV_SIZE];
276 void llcrypt_generate_iv(union llcrypt_iv *iv, u64 lblk_num,
277 const struct llcrypt_info *ci);
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);
288 struct llcrypt_hkdf {
289 struct crypto_shash *hmac_tfm;
292 extern int llcrypt_init_hkdf(struct llcrypt_hkdf *hkdf, const u8 *master_key,
293 unsigned int master_key_size);
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.
302 #define HKDF_CONTEXT_KEY_IDENTIFIER 1
303 #define HKDF_CONTEXT_PER_FILE_KEY 2
304 #define HKDF_CONTEXT_PER_MODE_KEY 3
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);
310 extern void llcrypt_destroy_hkdf(struct llcrypt_hkdf *hkdf);
315 * llcrypt_master_key_secret - secret key material of an in-use master key
317 struct llcrypt_master_key_secret {
320 * For v2 policy keys: HKDF context keyed by this master key.
321 * For v1 policy keys: not set (hkdf.hmac_tfm == NULL).
323 struct llcrypt_hkdf hkdf;
325 /* Size of the raw key in bytes. Set even if ->raw isn't set. */
328 /* For v1 policy keys: the raw key. Wiped for v2 policy keys. */
329 u8 raw[LLCRYPT_MAX_KEY_SIZE];
331 } __randomize_layout;
334 * llcrypt_master_key - an in-use master key
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
340 struct llcrypt_master_key {
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.
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.
358 struct llcrypt_master_key_secret mk_secret;
359 struct rw_semaphore mk_secret_sem;
362 * For v1 policy keys: an arbitrary key descriptor which was assigned by
363 * userspace (->descriptor).
365 * For v2 policy keys: a cryptographic hash of this key (->identifier).
367 struct llcrypt_key_specifier mk_spec;
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.
376 * This is NULL for v1 policy keys; those can only be added by root.
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.
383 struct key *mk_users;
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
392 refcount_t mk_refcount;
395 * List of inodes that were unlocked using this key. This allows the
396 * inodes to be evicted efficiently if the key is removed.
398 struct list_head mk_decrypted_inodes;
399 spinlock_t mk_decrypted_inodes_lock;
401 /* Per-mode tfms for DIRECT_KEY policies, allocated on-demand */
402 struct crypto_skcipher *mk_mode_keys[__LLCRYPT_MODE_MAX + 1];
404 } __randomize_layout;
407 is_master_key_secret_present(const struct llcrypt_master_key_secret *secret)
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.
416 return READ_ONCE(secret->size) != 0;
419 static inline const char *master_key_spec_type(
420 const struct llcrypt_key_specifier *spec)
422 switch (spec->type) {
423 case LLCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
425 case LLCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
431 static inline int master_key_spec_len(const struct llcrypt_key_specifier *spec)
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;
443 llcrypt_find_master_key(struct super_block *sb,
444 const struct llcrypt_key_specifier *mk_spec);
446 extern int llcrypt_verify_key_added(struct super_block *sb,
447 const u8 identifier[LLCRYPT_KEY_IDENTIFIER_SIZE]);
449 extern int __init llcrypt_init_keyring(void);
451 extern void __exit llcrypt_exit_keyring(void);
455 struct llcrypt_mode {
456 const char *friendly_name;
457 const char *cipher_str;
460 bool logged_impl_name;
465 llcrypt_mode_supports_direct_key(const struct llcrypt_mode *mode)
467 return mode->ivsize >= offsetofend(union llcrypt_iv, nonce);
470 extern struct crypto_skcipher *
471 llcrypt_allocate_skcipher(struct llcrypt_mode *mode, const u8 *raw_key,
472 const struct inode *inode);
474 extern int llcrypt_set_derived_key(struct llcrypt_info *ci,
475 const u8 *derived_key);
479 extern void llcrypt_put_direct_key(struct llcrypt_direct_key *dk);
481 extern int llcrypt_setup_v1_file_key(struct llcrypt_info *ci,
482 const u8 *raw_master_key);
484 extern int llcrypt_setup_v1_file_key_via_subscribed_keyrings(
485 struct llcrypt_info *ci);
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,
496 #endif /* _LLCRYPT_PRIVATE_H */