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> 175fee3609SSatya Tangirala #include <linux/blk-crypto.h> 183325bea5STheodore Ts'o 1922d94f49SEric Biggers #define CONST_STRLEN(str) (sizeof(str) - 1) 2022d94f49SEric Biggers 211d6217a4SEric Biggers #define FSCRYPT_FILE_NONCE_SIZE 16 22cc4e0df0STheodore Ts'o 2322d94f49SEric Biggers #define FSCRYPT_MIN_KEY_SIZE 16 2422d94f49SEric Biggers 255dae460cSEric Biggers #define FSCRYPT_CONTEXT_V1 1 265dae460cSEric Biggers #define FSCRYPT_CONTEXT_V2 2 275dae460cSEric Biggers 285dae460cSEric Biggers struct fscrypt_context_v1 { 295dae460cSEric Biggers u8 version; /* FSCRYPT_CONTEXT_V1 */ 30cc4e0df0STheodore Ts'o u8 contents_encryption_mode; 31cc4e0df0STheodore Ts'o u8 filenames_encryption_mode; 32cc4e0df0STheodore Ts'o u8 flags; 333b6df59bSEric Biggers u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; 341d6217a4SEric Biggers u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; 355dae460cSEric Biggers }; 36cc4e0df0STheodore Ts'o 375dae460cSEric Biggers struct fscrypt_context_v2 { 385dae460cSEric Biggers u8 version; /* FSCRYPT_CONTEXT_V2 */ 395dae460cSEric Biggers u8 contents_encryption_mode; 405dae460cSEric Biggers u8 filenames_encryption_mode; 415dae460cSEric Biggers u8 flags; 425dae460cSEric Biggers u8 __reserved[4]; 435dae460cSEric Biggers u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]; 441d6217a4SEric Biggers u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; 455dae460cSEric Biggers }; 465dae460cSEric Biggers 47d2fe9754SEric Biggers /* 485dae460cSEric Biggers * fscrypt_context - the encryption context of an inode 495dae460cSEric Biggers * 505dae460cSEric Biggers * This is the on-disk equivalent of an fscrypt_policy, stored alongside each 515dae460cSEric Biggers * encrypted file usually in a hidden extended attribute. It contains the 525dae460cSEric Biggers * fields from the fscrypt_policy, in order to identify the encryption algorithm 535dae460cSEric Biggers * and key with which the file is encrypted. It also contains a nonce that was 545dae460cSEric Biggers * randomly generated by fscrypt itself; this is used as KDF input or as a tweak 555dae460cSEric Biggers * to cause different files to be encrypted differently. 565dae460cSEric Biggers */ 575dae460cSEric Biggers union fscrypt_context { 585dae460cSEric Biggers u8 version; 595dae460cSEric Biggers struct fscrypt_context_v1 v1; 605dae460cSEric Biggers struct fscrypt_context_v2 v2; 615dae460cSEric Biggers }; 625dae460cSEric Biggers 635dae460cSEric Biggers /* 645dae460cSEric Biggers * Return the size expected for the given fscrypt_context based on its version 655dae460cSEric Biggers * number, or 0 if the context version is unrecognized. 665dae460cSEric Biggers */ 675dae460cSEric Biggers static inline int fscrypt_context_size(const union fscrypt_context *ctx) 685dae460cSEric Biggers { 695dae460cSEric Biggers switch (ctx->version) { 705dae460cSEric Biggers case FSCRYPT_CONTEXT_V1: 715dae460cSEric Biggers BUILD_BUG_ON(sizeof(ctx->v1) != 28); 725dae460cSEric Biggers return sizeof(ctx->v1); 735dae460cSEric Biggers case FSCRYPT_CONTEXT_V2: 745dae460cSEric Biggers BUILD_BUG_ON(sizeof(ctx->v2) != 40); 755dae460cSEric Biggers return sizeof(ctx->v2); 765dae460cSEric Biggers } 775dae460cSEric Biggers return 0; 785dae460cSEric Biggers } 795dae460cSEric Biggers 80e98ad464SEric Biggers /* Check whether an fscrypt_context has a recognized version number and size */ 81e98ad464SEric Biggers static inline bool fscrypt_context_is_valid(const union fscrypt_context *ctx, 82e98ad464SEric Biggers int ctx_size) 83e98ad464SEric Biggers { 84e98ad464SEric Biggers return ctx_size >= 1 && ctx_size == fscrypt_context_size(ctx); 85e98ad464SEric Biggers } 86e98ad464SEric Biggers 87e98ad464SEric Biggers /* Retrieve the context's nonce, assuming the context was already validated */ 88e98ad464SEric Biggers static inline const u8 *fscrypt_context_nonce(const union fscrypt_context *ctx) 89e98ad464SEric Biggers { 90e98ad464SEric Biggers switch (ctx->version) { 91e98ad464SEric Biggers case FSCRYPT_CONTEXT_V1: 92e98ad464SEric Biggers return ctx->v1.nonce; 93e98ad464SEric Biggers case FSCRYPT_CONTEXT_V2: 94e98ad464SEric Biggers return ctx->v2.nonce; 95e98ad464SEric Biggers } 96e98ad464SEric Biggers WARN_ON(1); 97e98ad464SEric Biggers return NULL; 98e98ad464SEric Biggers } 99e98ad464SEric Biggers 1005dae460cSEric Biggers #undef fscrypt_policy 1015dae460cSEric Biggers union fscrypt_policy { 1025dae460cSEric Biggers u8 version; 1035dae460cSEric Biggers struct fscrypt_policy_v1 v1; 1045dae460cSEric Biggers struct fscrypt_policy_v2 v2; 1055dae460cSEric Biggers }; 1065dae460cSEric Biggers 1075dae460cSEric Biggers /* 1085dae460cSEric Biggers * Return the size expected for the given fscrypt_policy based on its version 1095dae460cSEric Biggers * number, or 0 if the policy version is unrecognized. 1105dae460cSEric Biggers */ 1115dae460cSEric Biggers static inline int fscrypt_policy_size(const union fscrypt_policy *policy) 1125dae460cSEric Biggers { 1135dae460cSEric Biggers switch (policy->version) { 1145dae460cSEric Biggers case FSCRYPT_POLICY_V1: 1155dae460cSEric Biggers return sizeof(policy->v1); 1165dae460cSEric Biggers case FSCRYPT_POLICY_V2: 1175dae460cSEric Biggers return sizeof(policy->v2); 1185dae460cSEric Biggers } 1195dae460cSEric Biggers return 0; 1205dae460cSEric Biggers } 1215dae460cSEric Biggers 1225dae460cSEric Biggers /* Return the contents encryption mode of a valid encryption policy */ 1235dae460cSEric Biggers static inline u8 1245dae460cSEric Biggers fscrypt_policy_contents_mode(const union fscrypt_policy *policy) 1255dae460cSEric Biggers { 1265dae460cSEric Biggers switch (policy->version) { 1275dae460cSEric Biggers case FSCRYPT_POLICY_V1: 1285dae460cSEric Biggers return policy->v1.contents_encryption_mode; 1295dae460cSEric Biggers case FSCRYPT_POLICY_V2: 1305dae460cSEric Biggers return policy->v2.contents_encryption_mode; 1315dae460cSEric Biggers } 1325dae460cSEric Biggers BUG(); 1335dae460cSEric Biggers } 1345dae460cSEric Biggers 1355dae460cSEric Biggers /* Return the filenames encryption mode of a valid encryption policy */ 1365dae460cSEric Biggers static inline u8 1375dae460cSEric Biggers fscrypt_policy_fnames_mode(const union fscrypt_policy *policy) 1385dae460cSEric Biggers { 1395dae460cSEric Biggers switch (policy->version) { 1405dae460cSEric Biggers case FSCRYPT_POLICY_V1: 1415dae460cSEric Biggers return policy->v1.filenames_encryption_mode; 1425dae460cSEric Biggers case FSCRYPT_POLICY_V2: 1435dae460cSEric Biggers return policy->v2.filenames_encryption_mode; 1445dae460cSEric Biggers } 1455dae460cSEric Biggers BUG(); 1465dae460cSEric Biggers } 1475dae460cSEric Biggers 1485dae460cSEric Biggers /* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */ 1495dae460cSEric Biggers static inline u8 1505dae460cSEric Biggers fscrypt_policy_flags(const union fscrypt_policy *policy) 1515dae460cSEric Biggers { 1525dae460cSEric Biggers switch (policy->version) { 1535dae460cSEric Biggers case FSCRYPT_POLICY_V1: 1545dae460cSEric Biggers return policy->v1.flags; 1555dae460cSEric Biggers case FSCRYPT_POLICY_V2: 1565dae460cSEric Biggers return policy->v2.flags; 1575dae460cSEric Biggers } 1585dae460cSEric Biggers BUG(); 1595dae460cSEric Biggers } 1605dae460cSEric Biggers 161d2fe9754SEric Biggers /* 1620eaab5b1SEric Biggers * For encrypted symlinks, the ciphertext length is stored at the beginning 1630eaab5b1SEric Biggers * of the string in little-endian format. 1640eaab5b1SEric Biggers */ 1650eaab5b1SEric Biggers struct fscrypt_symlink_data { 1660eaab5b1SEric Biggers __le16 len; 1670eaab5b1SEric Biggers char encrypted_path[1]; 1680eaab5b1SEric Biggers } __packed; 1690eaab5b1SEric Biggers 1705fee3609SSatya Tangirala /** 1715fee3609SSatya Tangirala * struct fscrypt_prepared_key - a key prepared for actual encryption/decryption 1725fee3609SSatya Tangirala * @tfm: crypto API transform object 1735fee3609SSatya Tangirala * @blk_key: key for blk-crypto 1745fee3609SSatya Tangirala * 1755fee3609SSatya Tangirala * Normally only one of the fields will be non-NULL. 1765fee3609SSatya Tangirala */ 1775fee3609SSatya Tangirala struct fscrypt_prepared_key { 1785fee3609SSatya Tangirala struct crypto_skcipher *tfm; 1795fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 1805fee3609SSatya Tangirala struct fscrypt_blk_crypto_key *blk_key; 1815fee3609SSatya Tangirala #endif 1825fee3609SSatya Tangirala }; 1835fee3609SSatya Tangirala 184cc4e0df0STheodore Ts'o /* 1858094c3ceSEric Biggers * fscrypt_info - the "encryption key" for an inode 1868094c3ceSEric Biggers * 1878094c3ceSEric Biggers * When an encrypted file's key is made available, an instance of this struct is 1888094c3ceSEric Biggers * allocated and stored in ->i_crypt_info. Once created, it remains until the 1898094c3ceSEric Biggers * inode is evicted. 190cc4e0df0STheodore Ts'o */ 191cc4e0df0STheodore Ts'o struct fscrypt_info { 1928094c3ceSEric Biggers 1935fee3609SSatya Tangirala /* The key in a form prepared for actual encryption/decryption */ 1945fee3609SSatya Tangirala struct fscrypt_prepared_key ci_enc_key; 1958094c3ceSEric Biggers 1965fee3609SSatya Tangirala /* True if ci_enc_key should be freed when this fscrypt_info is freed */ 197b103fb76SEric Biggers bool ci_owns_key; 198b103fb76SEric Biggers 1995fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 2005fee3609SSatya Tangirala /* 2015fee3609SSatya Tangirala * True if this inode will use inline encryption (blk-crypto) instead of 2025fee3609SSatya Tangirala * the traditional filesystem-layer encryption. 2035fee3609SSatya Tangirala */ 2045fee3609SSatya Tangirala bool ci_inlinecrypt; 2055fee3609SSatya Tangirala #endif 2065fee3609SSatya Tangirala 2078094c3ceSEric Biggers /* 2085dae460cSEric Biggers * Encryption mode used for this inode. It corresponds to either the 2095dae460cSEric Biggers * contents or filenames encryption mode, depending on the inode type. 2108094c3ceSEric Biggers */ 2118094c3ceSEric Biggers struct fscrypt_mode *ci_mode; 2128094c3ceSEric Biggers 21359dc6a8eSEric Biggers /* Back-pointer to the inode */ 21459dc6a8eSEric Biggers struct inode *ci_inode; 21559dc6a8eSEric Biggers 2168094c3ceSEric Biggers /* 217b1c0ec35SEric Biggers * The master key with which this inode was unlocked (decrypted). This 218b1c0ec35SEric Biggers * will be NULL if the master key was found in a process-subscribed 219b1c0ec35SEric Biggers * keyring rather than in the filesystem-level keyring. 220b1c0ec35SEric Biggers */ 221b1c0ec35SEric Biggers struct key *ci_master_key; 222b1c0ec35SEric Biggers 223b1c0ec35SEric Biggers /* 224b1c0ec35SEric Biggers * Link in list of inodes that were unlocked with the master key. 225b1c0ec35SEric Biggers * Only used when ->ci_master_key is set. 226b1c0ec35SEric Biggers */ 227b1c0ec35SEric Biggers struct list_head ci_master_key_link; 228b1c0ec35SEric Biggers 229b1c0ec35SEric Biggers /* 230a828daabSEric Biggers * If non-NULL, then encryption is done using the master key directly 2315fee3609SSatya Tangirala * and ci_enc_key will equal ci_direct_key->dk_key. 2328094c3ceSEric Biggers */ 233a828daabSEric Biggers struct fscrypt_direct_key *ci_direct_key; 2348094c3ceSEric Biggers 235aa408f83SDaniel Rosenberg /* 236aa408f83SDaniel Rosenberg * This inode's hash key for filenames. This is a 128-bit SipHash-2-4 237aa408f83SDaniel Rosenberg * key. This is only set for directories that use a keyed dirhash over 238aa408f83SDaniel Rosenberg * the plaintext filenames -- currently just casefolded directories. 239aa408f83SDaniel Rosenberg */ 240aa408f83SDaniel Rosenberg siphash_key_t ci_dirhash_key; 241aa408f83SDaniel Rosenberg bool ci_dirhash_key_initialized; 242aa408f83SDaniel Rosenberg 2435dae460cSEric Biggers /* The encryption policy used by this inode */ 2445dae460cSEric Biggers union fscrypt_policy ci_policy; 2455dae460cSEric Biggers 2465dae460cSEric Biggers /* This inode's nonce, copied from the fscrypt_context */ 2471d6217a4SEric Biggers u8 ci_nonce[FSCRYPT_FILE_NONCE_SIZE]; 248e3b1078bSEric Biggers 249e3b1078bSEric Biggers /* Hashed inode number. Only set for IV_INO_LBLK_32 */ 250e3b1078bSEric Biggers u32 ci_hashed_ino; 251cc4e0df0STheodore Ts'o }; 252cc4e0df0STheodore Ts'o 25358ae7468SRichard Weinberger typedef enum { 25458ae7468SRichard Weinberger FS_DECRYPT = 0, 25558ae7468SRichard Weinberger FS_ENCRYPT, 25658ae7468SRichard Weinberger } fscrypt_direction_t; 25758ae7468SRichard Weinberger 258b98701dfSTheodore Ts'o /* crypto.c */ 259e4de782aSEric Biggers extern struct kmem_cache *fscrypt_info_cachep; 26060700902SEric Biggers int fscrypt_initialize(unsigned int cop_flags); 26160700902SEric Biggers int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw, 26260700902SEric Biggers u64 lblk_num, struct page *src_page, 26360700902SEric Biggers struct page *dest_page, unsigned int len, 26460700902SEric Biggers unsigned int offs, gfp_t gfp_flags); 26560700902SEric Biggers struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags); 266b98701dfSTheodore Ts'o 26760700902SEric Biggers void __printf(3, 4) __cold 268886da8b3SEric Biggers fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...); 269544d08fdSEric Biggers 270886da8b3SEric Biggers #define fscrypt_warn(inode, fmt, ...) \ 271886da8b3SEric Biggers fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__) 272886da8b3SEric Biggers #define fscrypt_err(inode, fmt, ...) \ 273886da8b3SEric Biggers fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__) 274544d08fdSEric Biggers 2758094c3ceSEric Biggers #define FSCRYPT_MAX_IV_SIZE 32 2768094c3ceSEric Biggers 2778094c3ceSEric Biggers union fscrypt_iv { 2788094c3ceSEric Biggers struct { 2798094c3ceSEric Biggers /* logical block number within the file */ 2808094c3ceSEric Biggers __le64 lblk_num; 2818094c3ceSEric Biggers 2828094c3ceSEric Biggers /* per-file nonce; only set in DIRECT_KEY mode */ 2831d6217a4SEric Biggers u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; 2848094c3ceSEric Biggers }; 2858094c3ceSEric Biggers u8 raw[FSCRYPT_MAX_IV_SIZE]; 2865fee3609SSatya Tangirala __le64 dun[FSCRYPT_MAX_IV_SIZE / sizeof(__le64)]; 2878094c3ceSEric Biggers }; 2888094c3ceSEric Biggers 2898094c3ceSEric Biggers void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num, 2908094c3ceSEric Biggers const struct fscrypt_info *ci); 2918094c3ceSEric Biggers 29276e81d6dSEric Biggers /* fname.c */ 29360700902SEric Biggers int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname, 29450c961deSEric Biggers u8 *out, unsigned int olen); 29560700902SEric Biggers bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len, 29660700902SEric Biggers u32 max_len, u32 *encrypted_len_ret); 2972ebdef6dSEric Biggers extern const struct dentry_operations fscrypt_d_ops; 29876e81d6dSEric Biggers 299c1144c9bSEric Biggers /* hkdf.c */ 300c1144c9bSEric Biggers 301c1144c9bSEric Biggers struct fscrypt_hkdf { 302c1144c9bSEric Biggers struct crypto_shash *hmac_tfm; 303c1144c9bSEric Biggers }; 304c1144c9bSEric Biggers 30560700902SEric Biggers int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key, 306c1144c9bSEric Biggers unsigned int master_key_size); 307c1144c9bSEric Biggers 3085dae460cSEric Biggers /* 3095dae460cSEric Biggers * The list of contexts in which fscrypt uses HKDF. These values are used as 3105dae460cSEric Biggers * the first byte of the HKDF application-specific info string to guarantee that 3115dae460cSEric Biggers * info strings are never repeated between contexts. This ensures that all HKDF 3125dae460cSEric Biggers * outputs are unique and cryptographically isolated, i.e. knowledge of one 3135dae460cSEric Biggers * output doesn't reveal another. 3145dae460cSEric Biggers */ 315e455de31SEric Biggers #define HKDF_CONTEXT_KEY_IDENTIFIER 1 /* info=<empty> */ 316e455de31SEric Biggers #define HKDF_CONTEXT_PER_FILE_ENC_KEY 2 /* info=file_nonce */ 317e455de31SEric Biggers #define HKDF_CONTEXT_DIRECT_KEY 3 /* info=mode_num */ 318e455de31SEric Biggers #define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4 /* info=mode_num||fs_uuid */ 319e455de31SEric Biggers #define HKDF_CONTEXT_DIRHASH_KEY 5 /* info=file_nonce */ 320e455de31SEric Biggers #define HKDF_CONTEXT_IV_INO_LBLK_32_KEY 6 /* info=mode_num||fs_uuid */ 321e455de31SEric Biggers #define HKDF_CONTEXT_INODE_HASH_KEY 7 /* info=<empty> */ 3225dae460cSEric Biggers 32360700902SEric Biggers int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context, 324c1144c9bSEric Biggers const u8 *info, unsigned int infolen, 325c1144c9bSEric Biggers u8 *okm, unsigned int okmlen); 326c1144c9bSEric Biggers 32760700902SEric Biggers void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf); 328c1144c9bSEric Biggers 3295fee3609SSatya Tangirala /* inline_crypt.c */ 3305fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 3315fee3609SSatya Tangirala int fscrypt_select_encryption_impl(struct fscrypt_info *ci); 3325fee3609SSatya Tangirala 3335fee3609SSatya Tangirala static inline bool 3345fee3609SSatya Tangirala fscrypt_using_inline_encryption(const struct fscrypt_info *ci) 3355fee3609SSatya Tangirala { 3365fee3609SSatya Tangirala return ci->ci_inlinecrypt; 3375fee3609SSatya Tangirala } 3385fee3609SSatya Tangirala 3395fee3609SSatya Tangirala int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key, 3405fee3609SSatya Tangirala const u8 *raw_key, 3415fee3609SSatya Tangirala const struct fscrypt_info *ci); 3425fee3609SSatya Tangirala 3435fee3609SSatya Tangirala void fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key); 3445fee3609SSatya Tangirala 3455fee3609SSatya Tangirala /* 3465fee3609SSatya Tangirala * Check whether the crypto transform or blk-crypto key has been allocated in 3475fee3609SSatya Tangirala * @prep_key, depending on which encryption implementation the file will use. 3485fee3609SSatya Tangirala */ 3495fee3609SSatya Tangirala static inline bool 3505fee3609SSatya Tangirala fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key, 3515fee3609SSatya Tangirala const struct fscrypt_info *ci) 3525fee3609SSatya Tangirala { 3535fee3609SSatya Tangirala /* 35497c6327fSEric Biggers * The two smp_load_acquire()'s here pair with the smp_store_release()'s 35597c6327fSEric Biggers * in fscrypt_prepare_inline_crypt_key() and fscrypt_prepare_key(). 35697c6327fSEric Biggers * I.e., in some cases (namely, if this prep_key is a per-mode 35797c6327fSEric Biggers * encryption key) another task can publish blk_key or tfm concurrently, 35897c6327fSEric Biggers * executing a RELEASE barrier. We need to use smp_load_acquire() here 35997c6327fSEric Biggers * to safely ACQUIRE the memory the other task published. 3605fee3609SSatya Tangirala */ 3615fee3609SSatya Tangirala if (fscrypt_using_inline_encryption(ci)) 36297c6327fSEric Biggers return smp_load_acquire(&prep_key->blk_key) != NULL; 36397c6327fSEric Biggers return smp_load_acquire(&prep_key->tfm) != NULL; 3645fee3609SSatya Tangirala } 3655fee3609SSatya Tangirala 3665fee3609SSatya Tangirala #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ 3675fee3609SSatya Tangirala 3685fee3609SSatya Tangirala static inline int fscrypt_select_encryption_impl(struct fscrypt_info *ci) 3695fee3609SSatya Tangirala { 3705fee3609SSatya Tangirala return 0; 3715fee3609SSatya Tangirala } 3725fee3609SSatya Tangirala 3735fee3609SSatya Tangirala static inline bool 3745fee3609SSatya Tangirala fscrypt_using_inline_encryption(const struct fscrypt_info *ci) 3755fee3609SSatya Tangirala { 3765fee3609SSatya Tangirala return false; 3775fee3609SSatya Tangirala } 3785fee3609SSatya Tangirala 3795fee3609SSatya Tangirala static inline int 3805fee3609SSatya Tangirala fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key, 3815fee3609SSatya Tangirala const u8 *raw_key, 3825fee3609SSatya Tangirala const struct fscrypt_info *ci) 3835fee3609SSatya Tangirala { 3845fee3609SSatya Tangirala WARN_ON(1); 3855fee3609SSatya Tangirala return -EOPNOTSUPP; 3865fee3609SSatya Tangirala } 3875fee3609SSatya Tangirala 3885fee3609SSatya Tangirala static inline void 3895fee3609SSatya Tangirala fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key) 3905fee3609SSatya Tangirala { 3915fee3609SSatya Tangirala } 3925fee3609SSatya Tangirala 3935fee3609SSatya Tangirala static inline bool 3945fee3609SSatya Tangirala fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key, 3955fee3609SSatya Tangirala const struct fscrypt_info *ci) 3965fee3609SSatya Tangirala { 39797c6327fSEric Biggers return smp_load_acquire(&prep_key->tfm) != NULL; 3985fee3609SSatya Tangirala } 3995fee3609SSatya Tangirala #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ 4005fee3609SSatya Tangirala 40122d94f49SEric Biggers /* keyring.c */ 40222d94f49SEric Biggers 40322d94f49SEric Biggers /* 40422d94f49SEric Biggers * fscrypt_master_key_secret - secret key material of an in-use master key 40522d94f49SEric Biggers */ 40622d94f49SEric Biggers struct fscrypt_master_key_secret { 40722d94f49SEric Biggers 4085dae460cSEric Biggers /* 4095dae460cSEric Biggers * For v2 policy keys: HKDF context keyed by this master key. 4105dae460cSEric Biggers * For v1 policy keys: not set (hkdf.hmac_tfm == NULL). 4115dae460cSEric Biggers */ 4125dae460cSEric Biggers struct fscrypt_hkdf hkdf; 4135dae460cSEric Biggers 4145dae460cSEric Biggers /* Size of the raw key in bytes. Set even if ->raw isn't set. */ 41522d94f49SEric Biggers u32 size; 41622d94f49SEric Biggers 4175dae460cSEric Biggers /* For v1 policy keys: the raw key. Wiped for v2 policy keys. */ 41822d94f49SEric Biggers u8 raw[FSCRYPT_MAX_KEY_SIZE]; 41922d94f49SEric Biggers 42022d94f49SEric Biggers } __randomize_layout; 42122d94f49SEric Biggers 42222d94f49SEric Biggers /* 42322d94f49SEric Biggers * fscrypt_master_key - an in-use master key 42422d94f49SEric Biggers * 42522d94f49SEric Biggers * This represents a master encryption key which has been added to the 42622d94f49SEric Biggers * filesystem and can be used to "unlock" the encrypted files which were 42722d94f49SEric Biggers * encrypted with it. 42822d94f49SEric Biggers */ 42922d94f49SEric Biggers struct fscrypt_master_key { 43022d94f49SEric Biggers 431b1c0ec35SEric Biggers /* 432b1c0ec35SEric Biggers * The secret key material. After FS_IOC_REMOVE_ENCRYPTION_KEY is 433b1c0ec35SEric Biggers * executed, this is wiped and no new inodes can be unlocked with this 434b1c0ec35SEric Biggers * key; however, there may still be inodes in ->mk_decrypted_inodes 435b1c0ec35SEric Biggers * which could not be evicted. As long as some inodes still remain, 436b1c0ec35SEric Biggers * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or 437b1c0ec35SEric Biggers * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again. 438b1c0ec35SEric Biggers * 43923c688b5SEric Biggers * Locking: protected by key->sem (outer) and mk_secret_sem (inner). 44023c688b5SEric Biggers * The reason for two locks is that key->sem also protects modifying 44123c688b5SEric Biggers * mk_users, which ranks it above the semaphore for the keyring key 44223c688b5SEric Biggers * type, which is in turn above page faults (via keyring_read). But 44323c688b5SEric Biggers * sometimes filesystems call fscrypt_get_encryption_info() from within 44423c688b5SEric Biggers * a transaction, which ranks it below page faults. So we need a 44523c688b5SEric Biggers * separate lock which protects mk_secret but not also mk_users. 446b1c0ec35SEric Biggers */ 44722d94f49SEric Biggers struct fscrypt_master_key_secret mk_secret; 44823c688b5SEric Biggers struct rw_semaphore mk_secret_sem; 44922d94f49SEric Biggers 4505dae460cSEric Biggers /* 4515dae460cSEric Biggers * For v1 policy keys: an arbitrary key descriptor which was assigned by 4525dae460cSEric Biggers * userspace (->descriptor). 4535dae460cSEric Biggers * 4545dae460cSEric Biggers * For v2 policy keys: a cryptographic hash of this key (->identifier). 4555dae460cSEric Biggers */ 45622d94f49SEric Biggers struct fscrypt_key_specifier mk_spec; 45722d94f49SEric Biggers 458b1c0ec35SEric Biggers /* 45923c688b5SEric Biggers * Keyring which contains a key of type 'key_type_fscrypt_user' for each 46023c688b5SEric Biggers * user who has added this key. Normally each key will be added by just 46123c688b5SEric Biggers * one user, but it's possible that multiple users share a key, and in 46223c688b5SEric Biggers * that case we need to keep track of those users so that one user can't 46323c688b5SEric Biggers * remove the key before the others want it removed too. 46423c688b5SEric Biggers * 46523c688b5SEric Biggers * This is NULL for v1 policy keys; those can only be added by root. 46623c688b5SEric Biggers * 46723c688b5SEric Biggers * Locking: in addition to this keyrings own semaphore, this is 46823c688b5SEric Biggers * protected by the master key's key->sem, so we can do atomic 46923c688b5SEric Biggers * search+insert. It can also be searched without taking any locks, but 47023c688b5SEric Biggers * in that case the returned key may have already been removed. 47123c688b5SEric Biggers */ 47223c688b5SEric Biggers struct key *mk_users; 47323c688b5SEric Biggers 47423c688b5SEric Biggers /* 475b1c0ec35SEric Biggers * Length of ->mk_decrypted_inodes, plus one if mk_secret is present. 476b1c0ec35SEric Biggers * Once this goes to 0, the master key is removed from ->s_master_keys. 477b1c0ec35SEric Biggers * The 'struct fscrypt_master_key' will continue to live as long as the 478b1c0ec35SEric Biggers * 'struct key' whose payload it is, but we won't let this reference 479b1c0ec35SEric Biggers * count rise again. 480b1c0ec35SEric Biggers */ 481b1c0ec35SEric Biggers refcount_t mk_refcount; 482b1c0ec35SEric Biggers 483b1c0ec35SEric Biggers /* 484b1c0ec35SEric Biggers * List of inodes that were unlocked using this key. This allows the 485b1c0ec35SEric Biggers * inodes to be evicted efficiently if the key is removed. 486b1c0ec35SEric Biggers */ 487b1c0ec35SEric Biggers struct list_head mk_decrypted_inodes; 488b1c0ec35SEric Biggers spinlock_t mk_decrypted_inodes_lock; 489b1c0ec35SEric Biggers 490b103fb76SEric Biggers /* 491e3b1078bSEric Biggers * Per-mode encryption keys for the various types of encryption policies 492e3b1078bSEric Biggers * that use them. Allocated and derived on-demand. 493b103fb76SEric Biggers */ 4945fee3609SSatya Tangirala struct fscrypt_prepared_key mk_direct_keys[__FSCRYPT_MODE_MAX + 1]; 4955fee3609SSatya Tangirala struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[__FSCRYPT_MODE_MAX + 1]; 4965fee3609SSatya Tangirala struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[__FSCRYPT_MODE_MAX + 1]; 497e3b1078bSEric Biggers 498e3b1078bSEric Biggers /* Hash key for inode numbers. Initialized only when needed. */ 499e3b1078bSEric Biggers siphash_key_t mk_ino_hash_key; 500e3b1078bSEric Biggers bool mk_ino_hash_key_initialized; 5015dae460cSEric Biggers 50222d94f49SEric Biggers } __randomize_layout; 50322d94f49SEric Biggers 504b1c0ec35SEric Biggers static inline bool 505b1c0ec35SEric Biggers is_master_key_secret_present(const struct fscrypt_master_key_secret *secret) 506b1c0ec35SEric Biggers { 507b1c0ec35SEric Biggers /* 508b1c0ec35SEric Biggers * The READ_ONCE() is only necessary for fscrypt_drop_inode() and 509b1c0ec35SEric Biggers * fscrypt_key_describe(). These run in atomic context, so they can't 51023c688b5SEric Biggers * take ->mk_secret_sem and thus 'secret' can change concurrently which 51123c688b5SEric Biggers * would be a data race. But they only need to know whether the secret 51223c688b5SEric Biggers * *was* present at the time of check, so READ_ONCE() suffices. 513b1c0ec35SEric Biggers */ 514b1c0ec35SEric Biggers return READ_ONCE(secret->size) != 0; 515b1c0ec35SEric Biggers } 516b1c0ec35SEric Biggers 51722d94f49SEric Biggers static inline const char *master_key_spec_type( 51822d94f49SEric Biggers const struct fscrypt_key_specifier *spec) 51922d94f49SEric Biggers { 52022d94f49SEric Biggers switch (spec->type) { 52122d94f49SEric Biggers case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR: 52222d94f49SEric Biggers return "descriptor"; 5235dae460cSEric Biggers case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER: 5245dae460cSEric Biggers return "identifier"; 52522d94f49SEric Biggers } 52622d94f49SEric Biggers return "[unknown]"; 52722d94f49SEric Biggers } 52822d94f49SEric Biggers 52922d94f49SEric Biggers static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec) 53022d94f49SEric Biggers { 53122d94f49SEric Biggers switch (spec->type) { 53222d94f49SEric Biggers case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR: 53322d94f49SEric Biggers return FSCRYPT_KEY_DESCRIPTOR_SIZE; 5345dae460cSEric Biggers case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER: 5355dae460cSEric Biggers return FSCRYPT_KEY_IDENTIFIER_SIZE; 53622d94f49SEric Biggers } 53722d94f49SEric Biggers return 0; 53822d94f49SEric Biggers } 53922d94f49SEric Biggers 54060700902SEric Biggers struct key * 54122d94f49SEric Biggers fscrypt_find_master_key(struct super_block *sb, 54222d94f49SEric Biggers const struct fscrypt_key_specifier *mk_spec); 54322d94f49SEric Biggers 544cdeb21daSEric Biggers int fscrypt_add_test_dummy_key(struct super_block *sb, 545cdeb21daSEric Biggers struct fscrypt_key_specifier *key_spec); 546cdeb21daSEric Biggers 54760700902SEric Biggers int fscrypt_verify_key_added(struct super_block *sb, 5485ab7189aSEric Biggers const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]); 5495ab7189aSEric Biggers 55060700902SEric Biggers int __init fscrypt_init_keyring(void); 55122d94f49SEric Biggers 552feed8258SEric Biggers /* keysetup.c */ 5538094c3ceSEric Biggers 5548094c3ceSEric Biggers struct fscrypt_mode { 5558094c3ceSEric Biggers const char *friendly_name; 5568094c3ceSEric Biggers const char *cipher_str; 5578094c3ceSEric Biggers int keysize; 5588094c3ceSEric Biggers int ivsize; 559ff73c2c0SEric Biggers int logged_impl_name; 5605fee3609SSatya Tangirala enum blk_crypto_mode_num blk_crypto_mode; 5618094c3ceSEric Biggers }; 5628094c3ceSEric Biggers 56385af90e5SEric Biggers extern struct fscrypt_mode fscrypt_modes[]; 5643ec4f2a6SEric Biggers 5655fee3609SSatya Tangirala int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key, 5665fee3609SSatya Tangirala const u8 *raw_key, const struct fscrypt_info *ci); 5675fee3609SSatya Tangirala 5685fee3609SSatya Tangirala void fscrypt_destroy_prepared_key(struct fscrypt_prepared_key *prep_key); 5690109ce76SEric Biggers 57060700902SEric Biggers int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key); 5710109ce76SEric Biggers 57260700902SEric Biggers int fscrypt_derive_dirhash_key(struct fscrypt_info *ci, 573aa408f83SDaniel Rosenberg const struct fscrypt_master_key *mk); 574aa408f83SDaniel Rosenberg 575*a992b20cSEric Biggers void fscrypt_hash_inode_number(struct fscrypt_info *ci, 576*a992b20cSEric Biggers const struct fscrypt_master_key *mk); 577*a992b20cSEric Biggers 5780109ce76SEric Biggers /* keysetup_v1.c */ 5790109ce76SEric Biggers 58060700902SEric Biggers void fscrypt_put_direct_key(struct fscrypt_direct_key *dk); 5810109ce76SEric Biggers 58260700902SEric Biggers int fscrypt_setup_v1_file_key(struct fscrypt_info *ci, 5830109ce76SEric Biggers const u8 *raw_master_key); 5840109ce76SEric Biggers 58560700902SEric Biggers int fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_info *ci); 58660700902SEric Biggers 5875dae460cSEric Biggers /* policy.c */ 5885dae460cSEric Biggers 58960700902SEric Biggers bool fscrypt_policies_equal(const union fscrypt_policy *policy1, 5905dae460cSEric Biggers const union fscrypt_policy *policy2); 59160700902SEric Biggers bool fscrypt_supported_policy(const union fscrypt_policy *policy_u, 5925dae460cSEric Biggers const struct inode *inode); 59360700902SEric Biggers int fscrypt_policy_from_context(union fscrypt_policy *policy_u, 5945dae460cSEric Biggers const union fscrypt_context *ctx_u, 5955dae460cSEric Biggers int ctx_size); 5960109ce76SEric Biggers 5973325bea5STheodore Ts'o #endif /* _FSCRYPT_PRIVATE_H */ 598