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>
21 #include <uapi/linux/lustre/lgss.h>
23 #ifndef CRYPTO_TFM_REQ_FORBID_WEAK_KEYS
24 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS CRYPTO_TFM_REQ_WEAK_KEY
27 #define llcrypt_info(inode) ((struct llcrypt_info *)(inode)->i_private)
28 #define llcrypt_info_nocast(inode) ((inode)->i_private)
30 #define CONST_STRLEN(str) (sizeof(str) - 1)
32 #define FS_KEY_DERIVATION_NONCE_SIZE 16
34 #define LLCRYPT_MIN_KEY_SIZE 16
36 #define LLCRYPT_CONTEXT_V1 1
37 #define LLCRYPT_CONTEXT_V2 2
39 struct llcrypt_context_v1 {
40 u8 version; /* LLCRYPT_CONTEXT_V1 */
41 u8 contents_encryption_mode;
42 u8 filenames_encryption_mode;
44 u8 master_key_descriptor[LLCRYPT_KEY_DESCRIPTOR_SIZE];
45 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
48 struct llcrypt_context_v2 {
49 u8 version; /* LLCRYPT_CONTEXT_V2 */
50 u8 contents_encryption_mode;
51 u8 filenames_encryption_mode;
54 u8 master_key_identifier[LLCRYPT_KEY_IDENTIFIER_SIZE];
55 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
59 * llcrypt_context - the encryption context of an inode
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.
68 union llcrypt_context {
70 struct llcrypt_context_v1 v1;
71 struct llcrypt_context_v2 v2;
75 * Return the size expected for the given llcrypt_context based on its version
76 * number, or 0 if the context version is unrecognized.
78 static inline int llcrypt_context_size(const union llcrypt_context *ctx)
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);
92 union llcrypt_policy {
94 struct llcrypt_policy_v1 v1;
95 struct llcrypt_policy_v2 v2;
99 * Return the size expected for the given llcrypt_policy based on its version
100 * number, or 0 if the policy version is unrecognized.
102 static inline int llcrypt_policy_size(const union llcrypt_policy *policy)
104 switch (policy->version) {
105 case LLCRYPT_POLICY_V1:
106 return sizeof(policy->v1);
107 case LLCRYPT_POLICY_V2:
108 return sizeof(policy->v2);
113 /* Return the contents encryption mode of a valid encryption policy */
115 llcrypt_policy_contents_mode(const union llcrypt_policy *policy)
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;
126 /* Return the filenames encryption mode of a valid encryption policy */
128 llcrypt_policy_fnames_mode(const union llcrypt_policy *policy)
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;
139 /* Return the flags (LLCRYPT_POLICY_FLAG*) of a valid encryption policy */
141 llcrypt_policy_flags(const union llcrypt_policy *policy)
143 switch (policy->version) {
144 case LLCRYPT_POLICY_V1:
145 return policy->v1.flags;
146 case LLCRYPT_POLICY_V2:
147 return policy->v2.flags;
153 llcrypt_is_direct_key_policy(const union llcrypt_policy *policy)
155 return llcrypt_policy_flags(policy) & LLCRYPT_POLICY_FLAG_DIRECT_KEY;
159 * For encrypted symlinks, the ciphertext length is stored at the beginning
160 * of the string in little-endian format.
162 struct llcrypt_symlink_data {
164 char encrypted_path[1];
168 * llcrypt_info - the "encryption key" for an inode
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.
174 struct llcrypt_info {
176 /* The actual crypto transform used for encryption and decryption */
177 struct crypto_skcipher *ci_ctfm;
180 * Cipher for ESSIV IV generation. Only set for CBC contents
181 * encryption, otherwise is NULL.
183 struct crypto_cipher *ci_essiv_tfm;
186 * Encryption mode used for this inode. It corresponds to either the
187 * contents or filenames encryption mode, depending on the inode type.
189 struct llcrypt_mode *ci_mode;
191 /* Back-pointer to the inode */
192 struct inode *ci_inode;
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.
199 struct key *ci_master_key;
202 * Link in list of inodes that were unlocked with the master key.
203 * Only used when ->ci_master_key is set.
205 struct list_head ci_master_key_link;
208 * If non-NULL, then encryption is done using the master key directly
209 * and ci_ctfm will equal ci_direct_key->dk_ctfm.
211 struct llcrypt_direct_key *ci_direct_key;
213 /* The encryption policy used by this inode */
214 union llcrypt_policy ci_policy;
216 /* This inode's nonce, copied from the llcrypt_context */
217 u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE];
223 } llcrypt_direction_t;
225 #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
227 static inline bool llcrypt_valid_enc_modes(u32 contents_mode,
230 if (contents_mode == LLCRYPT_MODE_AES_128_CBC &&
231 (filenames_mode == LLCRYPT_MODE_AES_128_CTS ||
232 filenames_mode == LLCRYPT_MODE_NULL))
235 if (contents_mode == LLCRYPT_MODE_AES_256_XTS &&
236 (filenames_mode == LLCRYPT_MODE_AES_256_CTS ||
237 filenames_mode == LLCRYPT_MODE_NULL))
240 if (contents_mode == LLCRYPT_MODE_ADIANTUM &&
241 (filenames_mode == LLCRYPT_MODE_ADIANTUM ||
242 filenames_mode == LLCRYPT_MODE_NULL))
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,
256 extern struct page *llcrypt_alloc_bounce_page(gfp_t gfp_flags);
257 extern const struct dentry_operations llcrypt_d_ops;
259 extern void __printf(3, 4) __cold
260 llcrypt_msg(const struct inode *inode, int mask, const char *fmt, ...);
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__)
267 #define LLCRYPT_MAX_IV_SIZE 32
271 /* logical block number within the file */
274 /* per-file nonce; only set in DIRECT_KEY mode */
275 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
277 u8 raw[LLCRYPT_MAX_IV_SIZE];
280 void llcrypt_generate_iv(union llcrypt_iv *iv, u64 lblk_num,
281 const struct llcrypt_info *ci);
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);
292 struct llcrypt_hkdf {
293 struct crypto_shash *hmac_tfm;
296 extern int llcrypt_init_hkdf(struct llcrypt_hkdf *hkdf, const u8 *master_key,
297 unsigned int master_key_size);
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.
306 #define HKDF_CONTEXT_KEY_IDENTIFIER 1
307 #define HKDF_CONTEXT_PER_FILE_KEY 2
308 #define HKDF_CONTEXT_PER_MODE_KEY 3
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);
314 extern void llcrypt_destroy_hkdf(struct llcrypt_hkdf *hkdf);
319 * llcrypt_master_key_secret - secret key material of an in-use master key
321 struct llcrypt_master_key_secret {
324 * For v2 policy keys: HKDF context keyed by this master key.
325 * For v1 policy keys: not set (hkdf.hmac_tfm == NULL).
327 struct llcrypt_hkdf hkdf;
329 /* Size of the raw key in bytes. Set even if ->raw isn't set. */
332 /* For v1 policy keys: the raw key. Wiped for v2 policy keys. */
333 u8 raw[LLCRYPT_MAX_KEY_SIZE];
335 } __randomize_layout;
338 * llcrypt_master_key - an in-use master key
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
344 struct llcrypt_master_key {
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.
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.
362 struct llcrypt_master_key_secret mk_secret;
363 struct rw_semaphore mk_secret_sem;
366 * For v1 policy keys: an arbitrary key descriptor which was assigned by
367 * userspace (->descriptor).
369 * For v2 policy keys: a cryptographic hash of this key (->identifier).
371 struct llcrypt_key_specifier mk_spec;
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.
380 * This is NULL for v1 policy keys; those can only be added by root.
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.
387 struct key *mk_users;
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
396 refcount_t mk_refcount;
399 * List of inodes that were unlocked using this key. This allows the
400 * inodes to be evicted efficiently if the key is removed.
402 struct list_head mk_decrypted_inodes;
403 spinlock_t mk_decrypted_inodes_lock;
405 /* Per-mode tfms for DIRECT_KEY policies, allocated on-demand */
406 struct crypto_skcipher *mk_mode_keys[__LLCRYPT_MODE_MAX + 1];
408 } __randomize_layout;
411 is_master_key_secret_present(const struct llcrypt_master_key_secret *secret)
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.
420 return READ_ONCE(secret->size) != 0;
423 static inline const char *master_key_spec_type(
424 const struct llcrypt_key_specifier *spec)
426 switch (spec->type) {
427 case LLCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
429 case LLCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
435 static inline int master_key_spec_len(const struct llcrypt_key_specifier *spec)
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;
447 llcrypt_find_master_key(struct super_block *sb,
448 const struct llcrypt_key_specifier *mk_spec);
450 extern int llcrypt_verify_key_added(struct super_block *sb,
451 const u8 identifier[LLCRYPT_KEY_IDENTIFIER_SIZE]);
453 extern int __init llcrypt_init_keyring(void);
455 extern void __exit llcrypt_exit_keyring(void);
459 struct llcrypt_mode {
460 const char *friendly_name;
461 const char *cipher_str;
464 bool logged_impl_name;
469 llcrypt_mode_supports_direct_key(const struct llcrypt_mode *mode)
471 return mode->ivsize >= offsetofend(union llcrypt_iv, nonce);
474 extern struct crypto_skcipher *
475 llcrypt_allocate_skcipher(struct llcrypt_mode *mode, const u8 *raw_key,
476 const struct inode *inode);
478 extern int llcrypt_set_derived_key(struct llcrypt_info *ci,
479 const u8 *derived_key);
483 extern void llcrypt_put_direct_key(struct llcrypt_direct_key *dk);
485 extern int llcrypt_setup_v1_file_key(struct llcrypt_info *ci,
486 const u8 *raw_master_key);
488 extern int llcrypt_setup_v1_file_key_via_subscribed_keyrings(
489 struct llcrypt_info *ci);
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,
500 #endif /* _LLCRYPT_PRIVATE_H */