1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 23325bea5STheodore Ts'o /* 33325bea5STheodore Ts'o * fscrypt_private.h 43325bea5STheodore Ts'o * 53325bea5STheodore Ts'o * Copyright (C) 2015, Google, Inc. 63325bea5STheodore Ts'o * 73ec4f2a6SEric Biggers * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar. 83ec4f2a6SEric Biggers * Heavily modified since then. 93325bea5STheodore Ts'o */ 103325bea5STheodore Ts'o 113325bea5STheodore Ts'o #ifndef _FSCRYPT_PRIVATE_H 123325bea5STheodore Ts'o #define _FSCRYPT_PRIVATE_H 133325bea5STheodore Ts'o 14734f0d24SDave Chinner #include <linux/fscrypt.h> 15aa408f83SDaniel Rosenberg #include <linux/siphash.h> 16b7e7cf7aSDaniel Walter #include <crypto/hash.h> 173325bea5STheodore Ts'o 1822d94f49SEric Biggers #define CONST_STRLEN(str) (sizeof(str) - 1) 1922d94f49SEric Biggers 20cc4e0df0STheodore Ts'o #define FS_KEY_DERIVATION_NONCE_SIZE 16 21cc4e0df0STheodore Ts'o 2222d94f49SEric Biggers #define FSCRYPT_MIN_KEY_SIZE 16 2322d94f49SEric Biggers 245dae460cSEric Biggers #define FSCRYPT_CONTEXT_V1 1 255dae460cSEric Biggers #define FSCRYPT_CONTEXT_V2 2 265dae460cSEric Biggers 275dae460cSEric Biggers struct fscrypt_context_v1 { 285dae460cSEric Biggers u8 version; /* FSCRYPT_CONTEXT_V1 */ 29cc4e0df0STheodore Ts'o u8 contents_encryption_mode; 30cc4e0df0STheodore Ts'o u8 filenames_encryption_mode; 31cc4e0df0STheodore Ts'o u8 flags; 323b6df59bSEric Biggers u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; 33cc4e0df0STheodore Ts'o u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; 345dae460cSEric Biggers }; 35cc4e0df0STheodore Ts'o 365dae460cSEric Biggers struct fscrypt_context_v2 { 375dae460cSEric Biggers u8 version; /* FSCRYPT_CONTEXT_V2 */ 385dae460cSEric Biggers u8 contents_encryption_mode; 395dae460cSEric Biggers u8 filenames_encryption_mode; 405dae460cSEric Biggers u8 flags; 415dae460cSEric Biggers u8 __reserved[4]; 425dae460cSEric Biggers u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]; 435dae460cSEric Biggers u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; 445dae460cSEric Biggers }; 455dae460cSEric Biggers 46d2fe9754SEric Biggers /* 475dae460cSEric Biggers * fscrypt_context - the encryption context of an inode 485dae460cSEric Biggers * 495dae460cSEric Biggers * This is the on-disk equivalent of an fscrypt_policy, stored alongside each 505dae460cSEric Biggers * encrypted file usually in a hidden extended attribute. It contains the 515dae460cSEric Biggers * fields from the fscrypt_policy, in order to identify the encryption algorithm 525dae460cSEric Biggers * and key with which the file is encrypted. It also contains a nonce that was 535dae460cSEric Biggers * randomly generated by fscrypt itself; this is used as KDF input or as a tweak 545dae460cSEric Biggers * to cause different files to be encrypted differently. 555dae460cSEric Biggers */ 565dae460cSEric Biggers union fscrypt_context { 575dae460cSEric Biggers u8 version; 585dae460cSEric Biggers struct fscrypt_context_v1 v1; 595dae460cSEric Biggers struct fscrypt_context_v2 v2; 605dae460cSEric Biggers }; 615dae460cSEric Biggers 625dae460cSEric Biggers /* 635dae460cSEric Biggers * Return the size expected for the given fscrypt_context based on its version 645dae460cSEric Biggers * number, or 0 if the context version is unrecognized. 655dae460cSEric Biggers */ 665dae460cSEric Biggers static inline int fscrypt_context_size(const union fscrypt_context *ctx) 675dae460cSEric Biggers { 685dae460cSEric Biggers switch (ctx->version) { 695dae460cSEric Biggers case FSCRYPT_CONTEXT_V1: 705dae460cSEric Biggers BUILD_BUG_ON(sizeof(ctx->v1) != 28); 715dae460cSEric Biggers return sizeof(ctx->v1); 725dae460cSEric Biggers case FSCRYPT_CONTEXT_V2: 735dae460cSEric Biggers BUILD_BUG_ON(sizeof(ctx->v2) != 40); 745dae460cSEric Biggers return sizeof(ctx->v2); 755dae460cSEric Biggers } 765dae460cSEric Biggers return 0; 775dae460cSEric Biggers } 785dae460cSEric Biggers 79e98ad464SEric Biggers /* Check whether an fscrypt_context has a recognized version number and size */ 80e98ad464SEric Biggers static inline bool fscrypt_context_is_valid(const union fscrypt_context *ctx, 81e98ad464SEric Biggers int ctx_size) 82e98ad464SEric Biggers { 83e98ad464SEric Biggers return ctx_size >= 1 && ctx_size == fscrypt_context_size(ctx); 84e98ad464SEric Biggers } 85e98ad464SEric Biggers 86e98ad464SEric Biggers /* Retrieve the context's nonce, assuming the context was already validated */ 87e98ad464SEric Biggers static inline const u8 *fscrypt_context_nonce(const union fscrypt_context *ctx) 88e98ad464SEric Biggers { 89e98ad464SEric Biggers switch (ctx->version) { 90e98ad464SEric Biggers case FSCRYPT_CONTEXT_V1: 91e98ad464SEric Biggers return ctx->v1.nonce; 92e98ad464SEric Biggers case FSCRYPT_CONTEXT_V2: 93e98ad464SEric Biggers return ctx->v2.nonce; 94e98ad464SEric Biggers } 95e98ad464SEric Biggers WARN_ON(1); 96e98ad464SEric Biggers return NULL; 97e98ad464SEric Biggers } 98e98ad464SEric Biggers 995dae460cSEric Biggers #undef fscrypt_policy 1005dae460cSEric Biggers union fscrypt_policy { 1015dae460cSEric Biggers u8 version; 1025dae460cSEric Biggers struct fscrypt_policy_v1 v1; 1035dae460cSEric Biggers struct fscrypt_policy_v2 v2; 1045dae460cSEric Biggers }; 1055dae460cSEric Biggers 1065dae460cSEric Biggers /* 1075dae460cSEric Biggers * Return the size expected for the given fscrypt_policy based on its version 1085dae460cSEric Biggers * number, or 0 if the policy version is unrecognized. 1095dae460cSEric Biggers */ 1105dae460cSEric Biggers static inline int fscrypt_policy_size(const union fscrypt_policy *policy) 1115dae460cSEric Biggers { 1125dae460cSEric Biggers switch (policy->version) { 1135dae460cSEric Biggers case FSCRYPT_POLICY_V1: 1145dae460cSEric Biggers return sizeof(policy->v1); 1155dae460cSEric Biggers case FSCRYPT_POLICY_V2: 1165dae460cSEric Biggers return sizeof(policy->v2); 1175dae460cSEric Biggers } 1185dae460cSEric Biggers return 0; 1195dae460cSEric Biggers } 1205dae460cSEric Biggers 1215dae460cSEric Biggers /* Return the contents encryption mode of a valid encryption policy */ 1225dae460cSEric Biggers static inline u8 1235dae460cSEric Biggers fscrypt_policy_contents_mode(const union fscrypt_policy *policy) 1245dae460cSEric Biggers { 1255dae460cSEric Biggers switch (policy->version) { 1265dae460cSEric Biggers case FSCRYPT_POLICY_V1: 1275dae460cSEric Biggers return policy->v1.contents_encryption_mode; 1285dae460cSEric Biggers case FSCRYPT_POLICY_V2: 1295dae460cSEric Biggers return policy->v2.contents_encryption_mode; 1305dae460cSEric Biggers } 1315dae460cSEric Biggers BUG(); 1325dae460cSEric Biggers } 1335dae460cSEric Biggers 1345dae460cSEric Biggers /* Return the filenames encryption mode of a valid encryption policy */ 1355dae460cSEric Biggers static inline u8 1365dae460cSEric Biggers fscrypt_policy_fnames_mode(const union fscrypt_policy *policy) 1375dae460cSEric Biggers { 1385dae460cSEric Biggers switch (policy->version) { 1395dae460cSEric Biggers case FSCRYPT_POLICY_V1: 1405dae460cSEric Biggers return policy->v1.filenames_encryption_mode; 1415dae460cSEric Biggers case FSCRYPT_POLICY_V2: 1425dae460cSEric Biggers return policy->v2.filenames_encryption_mode; 1435dae460cSEric Biggers } 1445dae460cSEric Biggers BUG(); 1455dae460cSEric Biggers } 1465dae460cSEric Biggers 1475dae460cSEric Biggers /* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */ 1485dae460cSEric Biggers static inline u8 1495dae460cSEric Biggers fscrypt_policy_flags(const union fscrypt_policy *policy) 1505dae460cSEric Biggers { 1515dae460cSEric Biggers switch (policy->version) { 1525dae460cSEric Biggers case FSCRYPT_POLICY_V1: 1535dae460cSEric Biggers return policy->v1.flags; 1545dae460cSEric Biggers case FSCRYPT_POLICY_V2: 1555dae460cSEric Biggers return policy->v2.flags; 1565dae460cSEric Biggers } 1575dae460cSEric Biggers BUG(); 1585dae460cSEric Biggers } 1595dae460cSEric Biggers 160d2fe9754SEric Biggers /* 1610eaab5b1SEric Biggers * For encrypted symlinks, the ciphertext length is stored at the beginning 1620eaab5b1SEric Biggers * of the string in little-endian format. 1630eaab5b1SEric Biggers */ 1640eaab5b1SEric Biggers struct fscrypt_symlink_data { 1650eaab5b1SEric Biggers __le16 len; 1660eaab5b1SEric Biggers char encrypted_path[1]; 1670eaab5b1SEric Biggers } __packed; 1680eaab5b1SEric Biggers 169cc4e0df0STheodore Ts'o /* 1708094c3ceSEric Biggers * fscrypt_info - the "encryption key" for an inode 1718094c3ceSEric Biggers * 1728094c3ceSEric Biggers * When an encrypted file's key is made available, an instance of this struct is 1738094c3ceSEric Biggers * allocated and stored in ->i_crypt_info. Once created, it remains until the 1748094c3ceSEric Biggers * inode is evicted. 175cc4e0df0STheodore Ts'o */ 176cc4e0df0STheodore Ts'o struct fscrypt_info { 1778094c3ceSEric Biggers 1788094c3ceSEric Biggers /* The actual crypto transform used for encryption and decryption */ 1798094c3ceSEric Biggers struct crypto_skcipher *ci_ctfm; 1808094c3ceSEric Biggers 181b103fb76SEric Biggers /* True if the key should be freed when this fscrypt_info is freed */ 182b103fb76SEric Biggers bool ci_owns_key; 183b103fb76SEric Biggers 1848094c3ceSEric Biggers /* 1855dae460cSEric Biggers * Encryption mode used for this inode. It corresponds to either the 1865dae460cSEric Biggers * contents or filenames encryption mode, depending on the inode type. 1878094c3ceSEric Biggers */ 1888094c3ceSEric Biggers struct fscrypt_mode *ci_mode; 1898094c3ceSEric Biggers 19059dc6a8eSEric Biggers /* Back-pointer to the inode */ 19159dc6a8eSEric Biggers struct inode *ci_inode; 19259dc6a8eSEric Biggers 1938094c3ceSEric Biggers /* 194b1c0ec35SEric Biggers * The master key with which this inode was unlocked (decrypted). This 195b1c0ec35SEric Biggers * will be NULL if the master key was found in a process-subscribed 196b1c0ec35SEric Biggers * keyring rather than in the filesystem-level keyring. 197b1c0ec35SEric Biggers */ 198b1c0ec35SEric Biggers struct key *ci_master_key; 199b1c0ec35SEric Biggers 200b1c0ec35SEric Biggers /* 201b1c0ec35SEric Biggers * Link in list of inodes that were unlocked with the master key. 202b1c0ec35SEric Biggers * Only used when ->ci_master_key is set. 203b1c0ec35SEric Biggers */ 204b1c0ec35SEric Biggers struct list_head ci_master_key_link; 205b1c0ec35SEric Biggers 206b1c0ec35SEric Biggers /* 207a828daabSEric Biggers * If non-NULL, then encryption is done using the master key directly 208a828daabSEric Biggers * and ci_ctfm will equal ci_direct_key->dk_ctfm. 2098094c3ceSEric Biggers */ 210a828daabSEric Biggers struct fscrypt_direct_key *ci_direct_key; 2118094c3ceSEric Biggers 212aa408f83SDaniel Rosenberg /* 213aa408f83SDaniel Rosenberg * This inode's hash key for filenames. This is a 128-bit SipHash-2-4 214aa408f83SDaniel Rosenberg * key. This is only set for directories that use a keyed dirhash over 215aa408f83SDaniel Rosenberg * the plaintext filenames -- currently just casefolded directories. 216aa408f83SDaniel Rosenberg */ 217aa408f83SDaniel Rosenberg siphash_key_t ci_dirhash_key; 218aa408f83SDaniel Rosenberg bool ci_dirhash_key_initialized; 219aa408f83SDaniel Rosenberg 2205dae460cSEric Biggers /* The encryption policy used by this inode */ 2215dae460cSEric Biggers union fscrypt_policy ci_policy; 2225dae460cSEric Biggers 2235dae460cSEric Biggers /* This inode's nonce, copied from the fscrypt_context */ 2248094c3ceSEric Biggers u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE]; 225cc4e0df0STheodore Ts'o }; 226cc4e0df0STheodore Ts'o 22758ae7468SRichard Weinberger typedef enum { 22858ae7468SRichard Weinberger FS_DECRYPT = 0, 22958ae7468SRichard Weinberger FS_ENCRYPT, 23058ae7468SRichard Weinberger } fscrypt_direction_t; 23158ae7468SRichard Weinberger 232b98701dfSTheodore Ts'o /* crypto.c */ 233e4de782aSEric Biggers extern struct kmem_cache *fscrypt_info_cachep; 23460700902SEric Biggers int fscrypt_initialize(unsigned int cop_flags); 23560700902SEric Biggers int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw, 23660700902SEric Biggers u64 lblk_num, struct page *src_page, 23760700902SEric Biggers struct page *dest_page, unsigned int len, 23860700902SEric Biggers unsigned int offs, gfp_t gfp_flags); 23960700902SEric Biggers struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags); 240b98701dfSTheodore Ts'o 24160700902SEric Biggers void __printf(3, 4) __cold 242886da8b3SEric Biggers fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...); 243544d08fdSEric Biggers 244886da8b3SEric Biggers #define fscrypt_warn(inode, fmt, ...) \ 245886da8b3SEric Biggers fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__) 246886da8b3SEric Biggers #define fscrypt_err(inode, fmt, ...) \ 247886da8b3SEric Biggers fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__) 248544d08fdSEric Biggers 2498094c3ceSEric Biggers #define FSCRYPT_MAX_IV_SIZE 32 2508094c3ceSEric Biggers 2518094c3ceSEric Biggers union fscrypt_iv { 2528094c3ceSEric Biggers struct { 2538094c3ceSEric Biggers /* logical block number within the file */ 2548094c3ceSEric Biggers __le64 lblk_num; 2558094c3ceSEric Biggers 2568094c3ceSEric Biggers /* per-file nonce; only set in DIRECT_KEY mode */ 2578094c3ceSEric Biggers u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; 2588094c3ceSEric Biggers }; 2598094c3ceSEric Biggers u8 raw[FSCRYPT_MAX_IV_SIZE]; 2608094c3ceSEric Biggers }; 2618094c3ceSEric Biggers 2628094c3ceSEric Biggers void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num, 2638094c3ceSEric Biggers const struct fscrypt_info *ci); 2648094c3ceSEric Biggers 26576e81d6dSEric Biggers /* fname.c */ 26660700902SEric Biggers int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname, 26750c961deSEric Biggers u8 *out, unsigned int olen); 26860700902SEric Biggers bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len, 26960700902SEric Biggers u32 max_len, u32 *encrypted_len_ret); 2702ebdef6dSEric Biggers extern const struct dentry_operations fscrypt_d_ops; 27176e81d6dSEric Biggers 272c1144c9bSEric Biggers /* hkdf.c */ 273c1144c9bSEric Biggers 274c1144c9bSEric Biggers struct fscrypt_hkdf { 275c1144c9bSEric Biggers struct crypto_shash *hmac_tfm; 276c1144c9bSEric Biggers }; 277c1144c9bSEric Biggers 27860700902SEric Biggers int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key, 279c1144c9bSEric Biggers unsigned int master_key_size); 280c1144c9bSEric Biggers 2815dae460cSEric Biggers /* 2825dae460cSEric Biggers * The list of contexts in which fscrypt uses HKDF. These values are used as 2835dae460cSEric Biggers * the first byte of the HKDF application-specific info string to guarantee that 2845dae460cSEric Biggers * info strings are never repeated between contexts. This ensures that all HKDF 2855dae460cSEric Biggers * outputs are unique and cryptographically isolated, i.e. knowledge of one 2865dae460cSEric Biggers * output doesn't reveal another. 2875dae460cSEric Biggers */ 2885dae460cSEric Biggers #define HKDF_CONTEXT_KEY_IDENTIFIER 1 289f592efe7SEric Biggers #define HKDF_CONTEXT_PER_FILE_ENC_KEY 2 290b103fb76SEric Biggers #define HKDF_CONTEXT_DIRECT_KEY 3 291b103fb76SEric Biggers #define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4 292aa408f83SDaniel Rosenberg #define HKDF_CONTEXT_DIRHASH_KEY 5 2935dae460cSEric Biggers 29460700902SEric Biggers int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context, 295c1144c9bSEric Biggers const u8 *info, unsigned int infolen, 296c1144c9bSEric Biggers u8 *okm, unsigned int okmlen); 297c1144c9bSEric Biggers 29860700902SEric Biggers void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf); 299c1144c9bSEric Biggers 30022d94f49SEric Biggers /* keyring.c */ 30122d94f49SEric Biggers 30222d94f49SEric Biggers /* 30322d94f49SEric Biggers * fscrypt_master_key_secret - secret key material of an in-use master key 30422d94f49SEric Biggers */ 30522d94f49SEric Biggers struct fscrypt_master_key_secret { 30622d94f49SEric Biggers 3075dae460cSEric Biggers /* 3085dae460cSEric Biggers * For v2 policy keys: HKDF context keyed by this master key. 3095dae460cSEric Biggers * For v1 policy keys: not set (hkdf.hmac_tfm == NULL). 3105dae460cSEric Biggers */ 3115dae460cSEric Biggers struct fscrypt_hkdf hkdf; 3125dae460cSEric Biggers 3135dae460cSEric Biggers /* Size of the raw key in bytes. Set even if ->raw isn't set. */ 31422d94f49SEric Biggers u32 size; 31522d94f49SEric Biggers 3165dae460cSEric Biggers /* For v1 policy keys: the raw key. Wiped for v2 policy keys. */ 31722d94f49SEric Biggers u8 raw[FSCRYPT_MAX_KEY_SIZE]; 31822d94f49SEric Biggers 31922d94f49SEric Biggers } __randomize_layout; 32022d94f49SEric Biggers 32122d94f49SEric Biggers /* 32222d94f49SEric Biggers * fscrypt_master_key - an in-use master key 32322d94f49SEric Biggers * 32422d94f49SEric Biggers * This represents a master encryption key which has been added to the 32522d94f49SEric Biggers * filesystem and can be used to "unlock" the encrypted files which were 32622d94f49SEric Biggers * encrypted with it. 32722d94f49SEric Biggers */ 32822d94f49SEric Biggers struct fscrypt_master_key { 32922d94f49SEric Biggers 330b1c0ec35SEric Biggers /* 331b1c0ec35SEric Biggers * The secret key material. After FS_IOC_REMOVE_ENCRYPTION_KEY is 332b1c0ec35SEric Biggers * executed, this is wiped and no new inodes can be unlocked with this 333b1c0ec35SEric Biggers * key; however, there may still be inodes in ->mk_decrypted_inodes 334b1c0ec35SEric Biggers * which could not be evicted. As long as some inodes still remain, 335b1c0ec35SEric Biggers * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or 336b1c0ec35SEric Biggers * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again. 337b1c0ec35SEric Biggers * 33823c688b5SEric Biggers * Locking: protected by key->sem (outer) and mk_secret_sem (inner). 33923c688b5SEric Biggers * The reason for two locks is that key->sem also protects modifying 34023c688b5SEric Biggers * mk_users, which ranks it above the semaphore for the keyring key 34123c688b5SEric Biggers * type, which is in turn above page faults (via keyring_read). But 34223c688b5SEric Biggers * sometimes filesystems call fscrypt_get_encryption_info() from within 34323c688b5SEric Biggers * a transaction, which ranks it below page faults. So we need a 34423c688b5SEric Biggers * separate lock which protects mk_secret but not also mk_users. 345b1c0ec35SEric Biggers */ 34622d94f49SEric Biggers struct fscrypt_master_key_secret mk_secret; 34723c688b5SEric Biggers struct rw_semaphore mk_secret_sem; 34822d94f49SEric Biggers 3495dae460cSEric Biggers /* 3505dae460cSEric Biggers * For v1 policy keys: an arbitrary key descriptor which was assigned by 3515dae460cSEric Biggers * userspace (->descriptor). 3525dae460cSEric Biggers * 3535dae460cSEric Biggers * For v2 policy keys: a cryptographic hash of this key (->identifier). 3545dae460cSEric Biggers */ 35522d94f49SEric Biggers struct fscrypt_key_specifier mk_spec; 35622d94f49SEric Biggers 357b1c0ec35SEric Biggers /* 35823c688b5SEric Biggers * Keyring which contains a key of type 'key_type_fscrypt_user' for each 35923c688b5SEric Biggers * user who has added this key. Normally each key will be added by just 36023c688b5SEric Biggers * one user, but it's possible that multiple users share a key, and in 36123c688b5SEric Biggers * that case we need to keep track of those users so that one user can't 36223c688b5SEric Biggers * remove the key before the others want it removed too. 36323c688b5SEric Biggers * 36423c688b5SEric Biggers * This is NULL for v1 policy keys; those can only be added by root. 36523c688b5SEric Biggers * 36623c688b5SEric Biggers * Locking: in addition to this keyrings own semaphore, this is 36723c688b5SEric Biggers * protected by the master key's key->sem, so we can do atomic 36823c688b5SEric Biggers * search+insert. It can also be searched without taking any locks, but 36923c688b5SEric Biggers * in that case the returned key may have already been removed. 37023c688b5SEric Biggers */ 37123c688b5SEric Biggers struct key *mk_users; 37223c688b5SEric Biggers 37323c688b5SEric Biggers /* 374b1c0ec35SEric Biggers * Length of ->mk_decrypted_inodes, plus one if mk_secret is present. 375b1c0ec35SEric Biggers * Once this goes to 0, the master key is removed from ->s_master_keys. 376b1c0ec35SEric Biggers * The 'struct fscrypt_master_key' will continue to live as long as the 377b1c0ec35SEric Biggers * 'struct key' whose payload it is, but we won't let this reference 378b1c0ec35SEric Biggers * count rise again. 379b1c0ec35SEric Biggers */ 380b1c0ec35SEric Biggers refcount_t mk_refcount; 381b1c0ec35SEric Biggers 382b1c0ec35SEric Biggers /* 383b1c0ec35SEric Biggers * List of inodes that were unlocked using this key. This allows the 384b1c0ec35SEric Biggers * inodes to be evicted efficiently if the key is removed. 385b1c0ec35SEric Biggers */ 386b1c0ec35SEric Biggers struct list_head mk_decrypted_inodes; 387b1c0ec35SEric Biggers spinlock_t mk_decrypted_inodes_lock; 388b1c0ec35SEric Biggers 389b103fb76SEric Biggers /* Crypto API transforms for DIRECT_KEY policies, allocated on-demand */ 390b103fb76SEric Biggers struct crypto_skcipher *mk_direct_tfms[__FSCRYPT_MODE_MAX + 1]; 391b103fb76SEric Biggers 392b103fb76SEric Biggers /* 393b103fb76SEric Biggers * Crypto API transforms for filesystem-layer implementation of 394b103fb76SEric Biggers * IV_INO_LBLK_64 policies, allocated on-demand. 395b103fb76SEric Biggers */ 396b103fb76SEric Biggers struct crypto_skcipher *mk_iv_ino_lblk_64_tfms[__FSCRYPT_MODE_MAX + 1]; 3975dae460cSEric Biggers 39822d94f49SEric Biggers } __randomize_layout; 39922d94f49SEric Biggers 400b1c0ec35SEric Biggers static inline bool 401b1c0ec35SEric Biggers is_master_key_secret_present(const struct fscrypt_master_key_secret *secret) 402b1c0ec35SEric Biggers { 403b1c0ec35SEric Biggers /* 404b1c0ec35SEric Biggers * The READ_ONCE() is only necessary for fscrypt_drop_inode() and 405b1c0ec35SEric Biggers * fscrypt_key_describe(). These run in atomic context, so they can't 40623c688b5SEric Biggers * take ->mk_secret_sem and thus 'secret' can change concurrently which 40723c688b5SEric Biggers * would be a data race. But they only need to know whether the secret 40823c688b5SEric Biggers * *was* present at the time of check, so READ_ONCE() suffices. 409b1c0ec35SEric Biggers */ 410b1c0ec35SEric Biggers return READ_ONCE(secret->size) != 0; 411b1c0ec35SEric Biggers } 412b1c0ec35SEric Biggers 41322d94f49SEric Biggers static inline const char *master_key_spec_type( 41422d94f49SEric Biggers const struct fscrypt_key_specifier *spec) 41522d94f49SEric Biggers { 41622d94f49SEric Biggers switch (spec->type) { 41722d94f49SEric Biggers case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR: 41822d94f49SEric Biggers return "descriptor"; 4195dae460cSEric Biggers case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER: 4205dae460cSEric Biggers return "identifier"; 42122d94f49SEric Biggers } 42222d94f49SEric Biggers return "[unknown]"; 42322d94f49SEric Biggers } 42422d94f49SEric Biggers 42522d94f49SEric Biggers static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec) 42622d94f49SEric Biggers { 42722d94f49SEric Biggers switch (spec->type) { 42822d94f49SEric Biggers case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR: 42922d94f49SEric Biggers return FSCRYPT_KEY_DESCRIPTOR_SIZE; 4305dae460cSEric Biggers case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER: 4315dae460cSEric Biggers return FSCRYPT_KEY_IDENTIFIER_SIZE; 43222d94f49SEric Biggers } 43322d94f49SEric Biggers return 0; 43422d94f49SEric Biggers } 43522d94f49SEric Biggers 43660700902SEric Biggers struct key * 43722d94f49SEric Biggers fscrypt_find_master_key(struct super_block *sb, 43822d94f49SEric Biggers const struct fscrypt_key_specifier *mk_spec); 43922d94f49SEric Biggers 440*cdeb21daSEric Biggers int fscrypt_add_test_dummy_key(struct super_block *sb, 441*cdeb21daSEric Biggers struct fscrypt_key_specifier *key_spec); 442*cdeb21daSEric Biggers 44360700902SEric Biggers int fscrypt_verify_key_added(struct super_block *sb, 4445ab7189aSEric Biggers const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]); 4455ab7189aSEric Biggers 44660700902SEric Biggers int __init fscrypt_init_keyring(void); 44722d94f49SEric Biggers 448feed8258SEric Biggers /* keysetup.c */ 4498094c3ceSEric Biggers 4508094c3ceSEric Biggers struct fscrypt_mode { 4518094c3ceSEric Biggers const char *friendly_name; 4528094c3ceSEric Biggers const char *cipher_str; 4538094c3ceSEric Biggers int keysize; 4548094c3ceSEric Biggers int ivsize; 455ff73c2c0SEric Biggers int logged_impl_name; 4568094c3ceSEric Biggers }; 4578094c3ceSEric Biggers 45885af90e5SEric Biggers extern struct fscrypt_mode fscrypt_modes[]; 4593ec4f2a6SEric Biggers 46060700902SEric Biggers struct crypto_skcipher *fscrypt_allocate_skcipher(struct fscrypt_mode *mode, 46160700902SEric Biggers const u8 *raw_key, 4620109ce76SEric Biggers const struct inode *inode); 4630109ce76SEric Biggers 46460700902SEric Biggers int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key); 4650109ce76SEric Biggers 46660700902SEric Biggers int fscrypt_derive_dirhash_key(struct fscrypt_info *ci, 467aa408f83SDaniel Rosenberg const struct fscrypt_master_key *mk); 468aa408f83SDaniel Rosenberg 4690109ce76SEric Biggers /* keysetup_v1.c */ 4700109ce76SEric Biggers 47160700902SEric Biggers void fscrypt_put_direct_key(struct fscrypt_direct_key *dk); 4720109ce76SEric Biggers 47360700902SEric Biggers int fscrypt_setup_v1_file_key(struct fscrypt_info *ci, 4740109ce76SEric Biggers const u8 *raw_master_key); 4750109ce76SEric Biggers 47660700902SEric Biggers int fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_info *ci); 47760700902SEric Biggers 4785dae460cSEric Biggers /* policy.c */ 4795dae460cSEric Biggers 48060700902SEric Biggers bool fscrypt_policies_equal(const union fscrypt_policy *policy1, 4815dae460cSEric Biggers const union fscrypt_policy *policy2); 48260700902SEric Biggers bool fscrypt_supported_policy(const union fscrypt_policy *policy_u, 4835dae460cSEric Biggers const struct inode *inode); 48460700902SEric Biggers int fscrypt_policy_from_context(union fscrypt_policy *policy_u, 4855dae460cSEric Biggers const union fscrypt_context *ctx_u, 4865dae460cSEric Biggers int ctx_size); 4870109ce76SEric Biggers 4883325bea5STheodore Ts'o #endif /* _FSCRYPT_PRIVATE_H */ 489