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 ||
231 filenames_mode == LLCRYPT_MODE_NULL))
234 if (contents_mode == LLCRYPT_MODE_AES_256_XTS &&
235 (filenames_mode == LLCRYPT_MODE_AES_256_CTS ||
236 filenames_mode == LLCRYPT_MODE_NULL))
239 if (contents_mode == LLCRYPT_MODE_ADIANTUM &&
240 (filenames_mode == LLCRYPT_MODE_ADIANTUM ||
241 filenames_mode == LLCRYPT_MODE_NULL))
248 extern struct kmem_cache *llcrypt_info_cachep;
249 extern int llcrypt_initialize(unsigned int cop_flags);
250 extern int llcrypt_crypt_block(const struct inode *inode,
251 llcrypt_direction_t rw, u64 lblk_num,
252 struct page *src_page, struct page *dest_page,
253 unsigned int len, unsigned int offs,
255 extern struct page *llcrypt_alloc_bounce_page(gfp_t gfp_flags);
256 extern const struct dentry_operations llcrypt_d_ops;
258 extern void __printf(3, 4) __cold
259 llcrypt_msg(const struct inode *inode, int mask, const char *fmt, ...);
261 #define llcrypt_warn(inode, fmt, ...) \
262 llcrypt_msg((inode), D_SEC, fmt, ##__VA_ARGS__)
263 #define llcrypt_err(inode, fmt, ...) \
264 llcrypt_msg((inode), D_ERROR, fmt, ##__VA_ARGS__)
266 #define LLCRYPT_MAX_IV_SIZE 32
270 /* logical block number within the file */
273 /* per-file nonce; only set in DIRECT_KEY mode */
274 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
276 u8 raw[LLCRYPT_MAX_IV_SIZE];
279 void llcrypt_generate_iv(union llcrypt_iv *iv, u64 lblk_num,
280 const struct llcrypt_info *ci);
283 extern int fname_encrypt(struct inode *inode, const struct qstr *iname,
284 u8 *out, unsigned int olen);
285 extern bool llcrypt_fname_encrypted_size(const struct inode *inode,
286 u32 orig_len, u32 max_len,
287 u32 *encrypted_len_ret);
291 struct llcrypt_hkdf {
292 struct crypto_shash *hmac_tfm;
295 extern int llcrypt_init_hkdf(struct llcrypt_hkdf *hkdf, const u8 *master_key,
296 unsigned int master_key_size);
299 * The list of contexts in which llcrypt uses HKDF. These values are used as
300 * the first byte of the HKDF application-specific info string to guarantee that
301 * info strings are never repeated between contexts. This ensures that all HKDF
302 * outputs are unique and cryptographically isolated, i.e. knowledge of one
303 * output doesn't reveal another.
305 #define HKDF_CONTEXT_KEY_IDENTIFIER 1
306 #define HKDF_CONTEXT_PER_FILE_KEY 2
307 #define HKDF_CONTEXT_PER_MODE_KEY 3
309 extern int llcrypt_hkdf_expand(struct llcrypt_hkdf *hkdf, u8 context,
310 const u8 *info, unsigned int infolen,
311 u8 *okm, unsigned int okmlen);
313 extern void llcrypt_destroy_hkdf(struct llcrypt_hkdf *hkdf);
318 * llcrypt_master_key_secret - secret key material of an in-use master key
320 struct llcrypt_master_key_secret {
323 * For v2 policy keys: HKDF context keyed by this master key.
324 * For v1 policy keys: not set (hkdf.hmac_tfm == NULL).
326 struct llcrypt_hkdf hkdf;
328 /* Size of the raw key in bytes. Set even if ->raw isn't set. */
331 /* For v1 policy keys: the raw key. Wiped for v2 policy keys. */
332 u8 raw[LLCRYPT_MAX_KEY_SIZE];
334 } __randomize_layout;
337 * llcrypt_master_key - an in-use master key
339 * This represents a master encryption key which has been added to the
340 * filesystem and can be used to "unlock" the encrypted files which were
343 struct llcrypt_master_key {
346 * The secret key material. After LL_IOC_REMOVE_ENCRYPTION_KEY is
347 * executed, this is wiped and no new inodes can be unlocked with this
348 * key; however, there may still be inodes in ->mk_decrypted_inodes
349 * which could not be evicted. As long as some inodes still remain,
350 * LL_IOC_REMOVE_ENCRYPTION_KEY can be retried, or
351 * LL_IOC_ADD_ENCRYPTION_KEY can add the secret again.
353 * Locking: protected by key->sem (outer) and mk_secret_sem (inner).
354 * The reason for two locks is that key->sem also protects modifying
355 * mk_users, which ranks it above the semaphore for the keyring key
356 * type, which is in turn above page faults (via keyring_read). But
357 * sometimes filesystems call llcrypt_get_encryption_info() from within
358 * a transaction, which ranks it below page faults. So we need a
359 * separate lock which protects mk_secret but not also mk_users.
361 struct llcrypt_master_key_secret mk_secret;
362 struct rw_semaphore mk_secret_sem;
365 * For v1 policy keys: an arbitrary key descriptor which was assigned by
366 * userspace (->descriptor).
368 * For v2 policy keys: a cryptographic hash of this key (->identifier).
370 struct llcrypt_key_specifier mk_spec;
373 * Keyring which contains a key of type 'key_type_llcrypt_user' for each
374 * user who has added this key. Normally each key will be added by just
375 * one user, but it's possible that multiple users share a key, and in
376 * that case we need to keep track of those users so that one user can't
377 * remove the key before the others want it removed too.
379 * This is NULL for v1 policy keys; those can only be added by root.
381 * Locking: in addition to this keyrings own semaphore, this is
382 * protected by the master key's key->sem, so we can do atomic
383 * search+insert. It can also be searched without taking any locks, but
384 * in that case the returned key may have already been removed.
386 struct key *mk_users;
389 * Length of ->mk_decrypted_inodes, plus one if mk_secret is present.
390 * Once this goes to 0, the master key is removed from ->lsi_master_keys.
391 * The 'struct llcrypt_master_key' will continue to live as long as the
392 * 'struct key' whose payload it is, but we won't let this reference
395 refcount_t mk_refcount;
398 * List of inodes that were unlocked using this key. This allows the
399 * inodes to be evicted efficiently if the key is removed.
401 struct list_head mk_decrypted_inodes;
402 spinlock_t mk_decrypted_inodes_lock;
404 /* Per-mode tfms for DIRECT_KEY policies, allocated on-demand */
405 struct crypto_skcipher *mk_mode_keys[__LLCRYPT_MODE_MAX + 1];
407 } __randomize_layout;
410 is_master_key_secret_present(const struct llcrypt_master_key_secret *secret)
413 * The READ_ONCE() is only necessary for llcrypt_drop_inode() and
414 * llcrypt_key_describe(). These run in atomic context, so they can't
415 * take ->mk_secret_sem and thus 'secret' can change concurrently which
416 * would be a data race. But they only need to know whether the secret
417 * *was* present at the time of check, so READ_ONCE() suffices.
419 return READ_ONCE(secret->size) != 0;
422 static inline const char *master_key_spec_type(
423 const struct llcrypt_key_specifier *spec)
425 switch (spec->type) {
426 case LLCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
428 case LLCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
434 static inline int master_key_spec_len(const struct llcrypt_key_specifier *spec)
436 switch (spec->type) {
437 case LLCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
438 return LLCRYPT_KEY_DESCRIPTOR_SIZE;
439 case LLCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
440 return LLCRYPT_KEY_IDENTIFIER_SIZE;
446 llcrypt_find_master_key(struct super_block *sb,
447 const struct llcrypt_key_specifier *mk_spec);
449 extern int llcrypt_verify_key_added(struct super_block *sb,
450 const u8 identifier[LLCRYPT_KEY_IDENTIFIER_SIZE]);
452 extern int __init llcrypt_init_keyring(void);
454 extern void __exit llcrypt_exit_keyring(void);
458 struct llcrypt_mode {
459 const char *friendly_name;
460 const char *cipher_str;
463 bool logged_impl_name;
468 llcrypt_mode_supports_direct_key(const struct llcrypt_mode *mode)
470 return mode->ivsize >= offsetofend(union llcrypt_iv, nonce);
473 extern struct crypto_skcipher *
474 llcrypt_allocate_skcipher(struct llcrypt_mode *mode, const u8 *raw_key,
475 const struct inode *inode);
477 extern int llcrypt_set_derived_key(struct llcrypt_info *ci,
478 const u8 *derived_key);
482 extern void llcrypt_put_direct_key(struct llcrypt_direct_key *dk);
484 extern int llcrypt_setup_v1_file_key(struct llcrypt_info *ci,
485 const u8 *raw_master_key);
487 extern int llcrypt_setup_v1_file_key_via_subscribed_keyrings(
488 struct llcrypt_info *ci);
491 extern bool llcrypt_policies_equal(const union llcrypt_policy *policy1,
492 const union llcrypt_policy *policy2);
493 extern bool llcrypt_supported_policy(const union llcrypt_policy *policy_u,
494 const struct inode *inode);
495 extern int llcrypt_policy_from_context(union llcrypt_policy *policy_u,
496 const union llcrypt_context *ctx_u,
499 #endif /* _LLCRYPT_PRIVATE_H */