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 23b7e072f9SEric Biggers /* 24b7e072f9SEric Biggers * Minimum size of an fscrypt master key. Note: a longer key will be required 25b7e072f9SEric Biggers * if ciphers with a 256-bit security strength are used. This is just the 26b7e072f9SEric Biggers * absolute minimum, which applies when only 128-bit encryption is used. 27b7e072f9SEric Biggers */ 2822d94f49SEric Biggers #define FSCRYPT_MIN_KEY_SIZE 16 2922d94f49SEric Biggers 305dae460cSEric Biggers #define FSCRYPT_CONTEXT_V1 1 315dae460cSEric Biggers #define FSCRYPT_CONTEXT_V2 2 325dae460cSEric Biggers 333ceb6543SEric Biggers /* Keep this in sync with include/uapi/linux/fscrypt.h */ 343ceb6543SEric Biggers #define FSCRYPT_MODE_MAX FSCRYPT_MODE_ADIANTUM 353ceb6543SEric Biggers 365dae460cSEric Biggers struct fscrypt_context_v1 { 375dae460cSEric Biggers u8 version; /* FSCRYPT_CONTEXT_V1 */ 38cc4e0df0STheodore Ts'o u8 contents_encryption_mode; 39cc4e0df0STheodore Ts'o u8 filenames_encryption_mode; 40cc4e0df0STheodore Ts'o u8 flags; 413b6df59bSEric Biggers u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE]; 421d6217a4SEric Biggers u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; 435dae460cSEric Biggers }; 44cc4e0df0STheodore Ts'o 455dae460cSEric Biggers struct fscrypt_context_v2 { 465dae460cSEric Biggers u8 version; /* FSCRYPT_CONTEXT_V2 */ 475dae460cSEric Biggers u8 contents_encryption_mode; 485dae460cSEric Biggers u8 filenames_encryption_mode; 495dae460cSEric Biggers u8 flags; 505dae460cSEric Biggers u8 __reserved[4]; 515dae460cSEric Biggers u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]; 521d6217a4SEric Biggers u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; 535dae460cSEric Biggers }; 545dae460cSEric Biggers 55d2fe9754SEric Biggers /* 565dae460cSEric Biggers * fscrypt_context - the encryption context of an inode 575dae460cSEric Biggers * 585dae460cSEric Biggers * This is the on-disk equivalent of an fscrypt_policy, stored alongside each 595dae460cSEric Biggers * encrypted file usually in a hidden extended attribute. It contains the 605dae460cSEric Biggers * fields from the fscrypt_policy, in order to identify the encryption algorithm 615dae460cSEric Biggers * and key with which the file is encrypted. It also contains a nonce that was 625dae460cSEric Biggers * randomly generated by fscrypt itself; this is used as KDF input or as a tweak 635dae460cSEric Biggers * to cause different files to be encrypted differently. 645dae460cSEric Biggers */ 655dae460cSEric Biggers union fscrypt_context { 665dae460cSEric Biggers u8 version; 675dae460cSEric Biggers struct fscrypt_context_v1 v1; 685dae460cSEric Biggers struct fscrypt_context_v2 v2; 695dae460cSEric Biggers }; 705dae460cSEric Biggers 715dae460cSEric Biggers /* 725dae460cSEric Biggers * Return the size expected for the given fscrypt_context based on its version 735dae460cSEric Biggers * number, or 0 if the context version is unrecognized. 745dae460cSEric Biggers */ 755dae460cSEric Biggers static inline int fscrypt_context_size(const union fscrypt_context *ctx) 765dae460cSEric Biggers { 775dae460cSEric Biggers switch (ctx->version) { 785dae460cSEric Biggers case FSCRYPT_CONTEXT_V1: 795dae460cSEric Biggers BUILD_BUG_ON(sizeof(ctx->v1) != 28); 805dae460cSEric Biggers return sizeof(ctx->v1); 815dae460cSEric Biggers case FSCRYPT_CONTEXT_V2: 825dae460cSEric Biggers BUILD_BUG_ON(sizeof(ctx->v2) != 40); 835dae460cSEric Biggers return sizeof(ctx->v2); 845dae460cSEric Biggers } 855dae460cSEric Biggers return 0; 865dae460cSEric Biggers } 875dae460cSEric Biggers 88e98ad464SEric Biggers /* Check whether an fscrypt_context has a recognized version number and size */ 89e98ad464SEric Biggers static inline bool fscrypt_context_is_valid(const union fscrypt_context *ctx, 90e98ad464SEric Biggers int ctx_size) 91e98ad464SEric Biggers { 92e98ad464SEric Biggers return ctx_size >= 1 && ctx_size == fscrypt_context_size(ctx); 93e98ad464SEric Biggers } 94e98ad464SEric Biggers 95e98ad464SEric Biggers /* Retrieve the context's nonce, assuming the context was already validated */ 96e98ad464SEric Biggers static inline const u8 *fscrypt_context_nonce(const union fscrypt_context *ctx) 97e98ad464SEric Biggers { 98e98ad464SEric Biggers switch (ctx->version) { 99e98ad464SEric Biggers case FSCRYPT_CONTEXT_V1: 100e98ad464SEric Biggers return ctx->v1.nonce; 101e98ad464SEric Biggers case FSCRYPT_CONTEXT_V2: 102e98ad464SEric Biggers return ctx->v2.nonce; 103e98ad464SEric Biggers } 104e98ad464SEric Biggers WARN_ON(1); 105e98ad464SEric Biggers return NULL; 106e98ad464SEric Biggers } 107e98ad464SEric Biggers 1085dae460cSEric Biggers union fscrypt_policy { 1095dae460cSEric Biggers u8 version; 1105dae460cSEric Biggers struct fscrypt_policy_v1 v1; 1115dae460cSEric Biggers struct fscrypt_policy_v2 v2; 1125dae460cSEric Biggers }; 1135dae460cSEric Biggers 1145dae460cSEric Biggers /* 1155dae460cSEric Biggers * Return the size expected for the given fscrypt_policy based on its version 1165dae460cSEric Biggers * number, or 0 if the policy version is unrecognized. 1175dae460cSEric Biggers */ 1185dae460cSEric Biggers static inline int fscrypt_policy_size(const union fscrypt_policy *policy) 1195dae460cSEric Biggers { 1205dae460cSEric Biggers switch (policy->version) { 1215dae460cSEric Biggers case FSCRYPT_POLICY_V1: 1225dae460cSEric Biggers return sizeof(policy->v1); 1235dae460cSEric Biggers case FSCRYPT_POLICY_V2: 1245dae460cSEric Biggers return sizeof(policy->v2); 1255dae460cSEric Biggers } 1265dae460cSEric Biggers return 0; 1275dae460cSEric Biggers } 1285dae460cSEric Biggers 1295dae460cSEric Biggers /* Return the contents encryption mode of a valid encryption policy */ 1305dae460cSEric Biggers static inline u8 1315dae460cSEric Biggers fscrypt_policy_contents_mode(const union fscrypt_policy *policy) 1325dae460cSEric Biggers { 1335dae460cSEric Biggers switch (policy->version) { 1345dae460cSEric Biggers case FSCRYPT_POLICY_V1: 1355dae460cSEric Biggers return policy->v1.contents_encryption_mode; 1365dae460cSEric Biggers case FSCRYPT_POLICY_V2: 1375dae460cSEric Biggers return policy->v2.contents_encryption_mode; 1385dae460cSEric Biggers } 1395dae460cSEric Biggers BUG(); 1405dae460cSEric Biggers } 1415dae460cSEric Biggers 1425dae460cSEric Biggers /* Return the filenames encryption mode of a valid encryption policy */ 1435dae460cSEric Biggers static inline u8 1445dae460cSEric Biggers fscrypt_policy_fnames_mode(const union fscrypt_policy *policy) 1455dae460cSEric Biggers { 1465dae460cSEric Biggers switch (policy->version) { 1475dae460cSEric Biggers case FSCRYPT_POLICY_V1: 1485dae460cSEric Biggers return policy->v1.filenames_encryption_mode; 1495dae460cSEric Biggers case FSCRYPT_POLICY_V2: 1505dae460cSEric Biggers return policy->v2.filenames_encryption_mode; 1515dae460cSEric Biggers } 1525dae460cSEric Biggers BUG(); 1535dae460cSEric Biggers } 1545dae460cSEric Biggers 1555dae460cSEric Biggers /* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */ 1565dae460cSEric Biggers static inline u8 1575dae460cSEric Biggers fscrypt_policy_flags(const union fscrypt_policy *policy) 1585dae460cSEric Biggers { 1595dae460cSEric Biggers switch (policy->version) { 1605dae460cSEric Biggers case FSCRYPT_POLICY_V1: 1615dae460cSEric Biggers return policy->v1.flags; 1625dae460cSEric Biggers case FSCRYPT_POLICY_V2: 1635dae460cSEric Biggers return policy->v2.flags; 1645dae460cSEric Biggers } 1655dae460cSEric Biggers BUG(); 1665dae460cSEric Biggers } 1675dae460cSEric Biggers 168d2fe9754SEric Biggers /* 1690eaab5b1SEric Biggers * For encrypted symlinks, the ciphertext length is stored at the beginning 1700eaab5b1SEric Biggers * of the string in little-endian format. 1710eaab5b1SEric Biggers */ 1720eaab5b1SEric Biggers struct fscrypt_symlink_data { 1730eaab5b1SEric Biggers __le16 len; 1740eaab5b1SEric Biggers char encrypted_path[1]; 1750eaab5b1SEric Biggers } __packed; 1760eaab5b1SEric Biggers 1775fee3609SSatya Tangirala /** 1785fee3609SSatya Tangirala * struct fscrypt_prepared_key - a key prepared for actual encryption/decryption 1795fee3609SSatya Tangirala * @tfm: crypto API transform object 1805fee3609SSatya Tangirala * @blk_key: key for blk-crypto 1815fee3609SSatya Tangirala * 1825fee3609SSatya Tangirala * Normally only one of the fields will be non-NULL. 1835fee3609SSatya Tangirala */ 1845fee3609SSatya Tangirala struct fscrypt_prepared_key { 1855fee3609SSatya Tangirala struct crypto_skcipher *tfm; 1865fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 1875fee3609SSatya Tangirala struct fscrypt_blk_crypto_key *blk_key; 1885fee3609SSatya Tangirala #endif 1895fee3609SSatya Tangirala }; 1905fee3609SSatya Tangirala 191cc4e0df0STheodore Ts'o /* 1928094c3ceSEric Biggers * fscrypt_info - the "encryption key" for an inode 1938094c3ceSEric Biggers * 1948094c3ceSEric Biggers * When an encrypted file's key is made available, an instance of this struct is 1958094c3ceSEric Biggers * allocated and stored in ->i_crypt_info. Once created, it remains until the 1968094c3ceSEric Biggers * inode is evicted. 197cc4e0df0STheodore Ts'o */ 198cc4e0df0STheodore Ts'o struct fscrypt_info { 1998094c3ceSEric Biggers 2005fee3609SSatya Tangirala /* The key in a form prepared for actual encryption/decryption */ 2015fee3609SSatya Tangirala struct fscrypt_prepared_key ci_enc_key; 2028094c3ceSEric Biggers 2035fee3609SSatya Tangirala /* True if ci_enc_key should be freed when this fscrypt_info is freed */ 204b103fb76SEric Biggers bool ci_owns_key; 205b103fb76SEric Biggers 2065fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 2075fee3609SSatya Tangirala /* 2085fee3609SSatya Tangirala * True if this inode will use inline encryption (blk-crypto) instead of 2095fee3609SSatya Tangirala * the traditional filesystem-layer encryption. 2105fee3609SSatya Tangirala */ 2115fee3609SSatya Tangirala bool ci_inlinecrypt; 2125fee3609SSatya Tangirala #endif 2135fee3609SSatya Tangirala 2148094c3ceSEric Biggers /* 2155dae460cSEric Biggers * Encryption mode used for this inode. It corresponds to either the 2165dae460cSEric Biggers * contents or filenames encryption mode, depending on the inode type. 2178094c3ceSEric Biggers */ 2188094c3ceSEric Biggers struct fscrypt_mode *ci_mode; 2198094c3ceSEric Biggers 22059dc6a8eSEric Biggers /* Back-pointer to the inode */ 22159dc6a8eSEric Biggers struct inode *ci_inode; 22259dc6a8eSEric Biggers 2238094c3ceSEric Biggers /* 224b1c0ec35SEric Biggers * The master key with which this inode was unlocked (decrypted). This 225b1c0ec35SEric Biggers * will be NULL if the master key was found in a process-subscribed 226b1c0ec35SEric Biggers * keyring rather than in the filesystem-level keyring. 227b1c0ec35SEric Biggers */ 228b1c0ec35SEric Biggers struct key *ci_master_key; 229b1c0ec35SEric Biggers 230b1c0ec35SEric Biggers /* 231b1c0ec35SEric Biggers * Link in list of inodes that were unlocked with the master key. 232b1c0ec35SEric Biggers * Only used when ->ci_master_key is set. 233b1c0ec35SEric Biggers */ 234b1c0ec35SEric Biggers struct list_head ci_master_key_link; 235b1c0ec35SEric Biggers 236b1c0ec35SEric Biggers /* 237a828daabSEric Biggers * If non-NULL, then encryption is done using the master key directly 2385fee3609SSatya Tangirala * and ci_enc_key will equal ci_direct_key->dk_key. 2398094c3ceSEric Biggers */ 240a828daabSEric Biggers struct fscrypt_direct_key *ci_direct_key; 2418094c3ceSEric Biggers 242aa408f83SDaniel Rosenberg /* 243aa408f83SDaniel Rosenberg * This inode's hash key for filenames. This is a 128-bit SipHash-2-4 244aa408f83SDaniel Rosenberg * key. This is only set for directories that use a keyed dirhash over 245aa408f83SDaniel Rosenberg * the plaintext filenames -- currently just casefolded directories. 246aa408f83SDaniel Rosenberg */ 247aa408f83SDaniel Rosenberg siphash_key_t ci_dirhash_key; 248aa408f83SDaniel Rosenberg bool ci_dirhash_key_initialized; 249aa408f83SDaniel Rosenberg 2505dae460cSEric Biggers /* The encryption policy used by this inode */ 2515dae460cSEric Biggers union fscrypt_policy ci_policy; 2525dae460cSEric Biggers 2535dae460cSEric Biggers /* This inode's nonce, copied from the fscrypt_context */ 2541d6217a4SEric Biggers u8 ci_nonce[FSCRYPT_FILE_NONCE_SIZE]; 255e3b1078bSEric Biggers 256e3b1078bSEric Biggers /* Hashed inode number. Only set for IV_INO_LBLK_32 */ 257e3b1078bSEric Biggers u32 ci_hashed_ino; 258cc4e0df0STheodore Ts'o }; 259cc4e0df0STheodore Ts'o 26058ae7468SRichard Weinberger typedef enum { 26158ae7468SRichard Weinberger FS_DECRYPT = 0, 26258ae7468SRichard Weinberger FS_ENCRYPT, 26358ae7468SRichard Weinberger } fscrypt_direction_t; 26458ae7468SRichard Weinberger 265b98701dfSTheodore Ts'o /* crypto.c */ 266e4de782aSEric Biggers extern struct kmem_cache *fscrypt_info_cachep; 26760700902SEric Biggers int fscrypt_initialize(unsigned int cop_flags); 26860700902SEric Biggers int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw, 26960700902SEric Biggers u64 lblk_num, struct page *src_page, 27060700902SEric Biggers struct page *dest_page, unsigned int len, 27160700902SEric Biggers unsigned int offs, gfp_t gfp_flags); 27260700902SEric Biggers struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags); 273b98701dfSTheodore Ts'o 27460700902SEric Biggers void __printf(3, 4) __cold 275886da8b3SEric Biggers fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...); 276544d08fdSEric Biggers 277886da8b3SEric Biggers #define fscrypt_warn(inode, fmt, ...) \ 278886da8b3SEric Biggers fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__) 279886da8b3SEric Biggers #define fscrypt_err(inode, fmt, ...) \ 280886da8b3SEric Biggers fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__) 281544d08fdSEric Biggers 2828094c3ceSEric Biggers #define FSCRYPT_MAX_IV_SIZE 32 2838094c3ceSEric Biggers 2848094c3ceSEric Biggers union fscrypt_iv { 2858094c3ceSEric Biggers struct { 2868094c3ceSEric Biggers /* logical block number within the file */ 2878094c3ceSEric Biggers __le64 lblk_num; 2888094c3ceSEric Biggers 2898094c3ceSEric Biggers /* per-file nonce; only set in DIRECT_KEY mode */ 2901d6217a4SEric Biggers u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; 2918094c3ceSEric Biggers }; 2928094c3ceSEric Biggers u8 raw[FSCRYPT_MAX_IV_SIZE]; 2935fee3609SSatya Tangirala __le64 dun[FSCRYPT_MAX_IV_SIZE / sizeof(__le64)]; 2948094c3ceSEric Biggers }; 2958094c3ceSEric Biggers 2968094c3ceSEric Biggers void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num, 2978094c3ceSEric Biggers const struct fscrypt_info *ci); 2988094c3ceSEric Biggers 29976e81d6dSEric Biggers /* fname.c */ 30060700902SEric Biggers int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname, 30150c961deSEric Biggers u8 *out, unsigned int olen); 302ac4acb1fSEric Biggers bool fscrypt_fname_encrypted_size(const union fscrypt_policy *policy, 303ac4acb1fSEric Biggers u32 orig_len, u32 max_len, 304ac4acb1fSEric Biggers u32 *encrypted_len_ret); 30576e81d6dSEric Biggers 306c1144c9bSEric Biggers /* hkdf.c */ 307c1144c9bSEric Biggers 308c1144c9bSEric Biggers struct fscrypt_hkdf { 309c1144c9bSEric Biggers struct crypto_shash *hmac_tfm; 310c1144c9bSEric Biggers }; 311c1144c9bSEric Biggers 31260700902SEric Biggers int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key, 313c1144c9bSEric Biggers unsigned int master_key_size); 314c1144c9bSEric Biggers 3155dae460cSEric Biggers /* 3165dae460cSEric Biggers * The list of contexts in which fscrypt uses HKDF. These values are used as 3175dae460cSEric Biggers * the first byte of the HKDF application-specific info string to guarantee that 3185dae460cSEric Biggers * info strings are never repeated between contexts. This ensures that all HKDF 3195dae460cSEric Biggers * outputs are unique and cryptographically isolated, i.e. knowledge of one 3205dae460cSEric Biggers * output doesn't reveal another. 3215dae460cSEric Biggers */ 322e455de31SEric Biggers #define HKDF_CONTEXT_KEY_IDENTIFIER 1 /* info=<empty> */ 323e455de31SEric Biggers #define HKDF_CONTEXT_PER_FILE_ENC_KEY 2 /* info=file_nonce */ 324e455de31SEric Biggers #define HKDF_CONTEXT_DIRECT_KEY 3 /* info=mode_num */ 325e455de31SEric Biggers #define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4 /* info=mode_num||fs_uuid */ 326e455de31SEric Biggers #define HKDF_CONTEXT_DIRHASH_KEY 5 /* info=file_nonce */ 327e455de31SEric Biggers #define HKDF_CONTEXT_IV_INO_LBLK_32_KEY 6 /* info=mode_num||fs_uuid */ 328e455de31SEric Biggers #define HKDF_CONTEXT_INODE_HASH_KEY 7 /* info=<empty> */ 3295dae460cSEric Biggers 33060700902SEric Biggers int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context, 331c1144c9bSEric Biggers const u8 *info, unsigned int infolen, 332c1144c9bSEric Biggers u8 *okm, unsigned int okmlen); 333c1144c9bSEric Biggers 33460700902SEric Biggers void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf); 335c1144c9bSEric Biggers 3365fee3609SSatya Tangirala /* inline_crypt.c */ 3375fee3609SSatya Tangirala #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 3385fee3609SSatya Tangirala int fscrypt_select_encryption_impl(struct fscrypt_info *ci); 3395fee3609SSatya Tangirala 3405fee3609SSatya Tangirala static inline bool 3415fee3609SSatya Tangirala fscrypt_using_inline_encryption(const struct fscrypt_info *ci) 3425fee3609SSatya Tangirala { 3435fee3609SSatya Tangirala return ci->ci_inlinecrypt; 3445fee3609SSatya Tangirala } 3455fee3609SSatya Tangirala 3465fee3609SSatya Tangirala int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key, 3475fee3609SSatya Tangirala const u8 *raw_key, 3485fee3609SSatya Tangirala const struct fscrypt_info *ci); 3495fee3609SSatya Tangirala 3505fee3609SSatya Tangirala void fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key); 3515fee3609SSatya Tangirala 3525fee3609SSatya Tangirala /* 3535fee3609SSatya Tangirala * Check whether the crypto transform or blk-crypto key has been allocated in 3545fee3609SSatya Tangirala * @prep_key, depending on which encryption implementation the file will use. 3555fee3609SSatya Tangirala */ 3565fee3609SSatya Tangirala static inline bool 3575fee3609SSatya Tangirala fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key, 3585fee3609SSatya Tangirala const struct fscrypt_info *ci) 3595fee3609SSatya Tangirala { 3605fee3609SSatya Tangirala /* 36197c6327fSEric Biggers * The two smp_load_acquire()'s here pair with the smp_store_release()'s 36297c6327fSEric Biggers * in fscrypt_prepare_inline_crypt_key() and fscrypt_prepare_key(). 36397c6327fSEric Biggers * I.e., in some cases (namely, if this prep_key is a per-mode 36497c6327fSEric Biggers * encryption key) another task can publish blk_key or tfm concurrently, 36597c6327fSEric Biggers * executing a RELEASE barrier. We need to use smp_load_acquire() here 36697c6327fSEric Biggers * to safely ACQUIRE the memory the other task published. 3675fee3609SSatya Tangirala */ 3685fee3609SSatya Tangirala if (fscrypt_using_inline_encryption(ci)) 36997c6327fSEric Biggers return smp_load_acquire(&prep_key->blk_key) != NULL; 37097c6327fSEric Biggers return smp_load_acquire(&prep_key->tfm) != NULL; 3715fee3609SSatya Tangirala } 3725fee3609SSatya Tangirala 3735fee3609SSatya Tangirala #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ 3745fee3609SSatya Tangirala 3755fee3609SSatya Tangirala static inline int fscrypt_select_encryption_impl(struct fscrypt_info *ci) 3765fee3609SSatya Tangirala { 3775fee3609SSatya Tangirala return 0; 3785fee3609SSatya Tangirala } 3795fee3609SSatya Tangirala 3805fee3609SSatya Tangirala static inline bool 3815fee3609SSatya Tangirala fscrypt_using_inline_encryption(const struct fscrypt_info *ci) 3825fee3609SSatya Tangirala { 3835fee3609SSatya Tangirala return false; 3845fee3609SSatya Tangirala } 3855fee3609SSatya Tangirala 3865fee3609SSatya Tangirala static inline int 3875fee3609SSatya Tangirala fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key, 3885fee3609SSatya Tangirala const u8 *raw_key, 3895fee3609SSatya Tangirala const struct fscrypt_info *ci) 3905fee3609SSatya Tangirala { 3915fee3609SSatya Tangirala WARN_ON(1); 3925fee3609SSatya Tangirala return -EOPNOTSUPP; 3935fee3609SSatya Tangirala } 3945fee3609SSatya Tangirala 3955fee3609SSatya Tangirala static inline void 3965fee3609SSatya Tangirala fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key) 3975fee3609SSatya Tangirala { 3985fee3609SSatya Tangirala } 3995fee3609SSatya Tangirala 4005fee3609SSatya Tangirala static inline bool 4015fee3609SSatya Tangirala fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key, 4025fee3609SSatya Tangirala const struct fscrypt_info *ci) 4035fee3609SSatya Tangirala { 40497c6327fSEric Biggers return smp_load_acquire(&prep_key->tfm) != NULL; 4055fee3609SSatya Tangirala } 4065fee3609SSatya Tangirala #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */ 4075fee3609SSatya Tangirala 40822d94f49SEric Biggers /* keyring.c */ 40922d94f49SEric Biggers 41022d94f49SEric Biggers /* 41122d94f49SEric Biggers * fscrypt_master_key_secret - secret key material of an in-use master key 41222d94f49SEric Biggers */ 41322d94f49SEric Biggers struct fscrypt_master_key_secret { 41422d94f49SEric Biggers 4155dae460cSEric Biggers /* 4165dae460cSEric Biggers * For v2 policy keys: HKDF context keyed by this master key. 4175dae460cSEric Biggers * For v1 policy keys: not set (hkdf.hmac_tfm == NULL). 4185dae460cSEric Biggers */ 4195dae460cSEric Biggers struct fscrypt_hkdf hkdf; 4205dae460cSEric Biggers 421b7e072f9SEric Biggers /* 422b7e072f9SEric Biggers * Size of the raw key in bytes. This remains set even if ->raw was 423b7e072f9SEric Biggers * zeroized due to no longer being needed. I.e. we still remember the 424b7e072f9SEric Biggers * size of the key even if we don't need to remember the key itself. 425b7e072f9SEric Biggers */ 42622d94f49SEric Biggers u32 size; 42722d94f49SEric Biggers 4285dae460cSEric Biggers /* For v1 policy keys: the raw key. Wiped for v2 policy keys. */ 42922d94f49SEric Biggers u8 raw[FSCRYPT_MAX_KEY_SIZE]; 43022d94f49SEric Biggers 43122d94f49SEric Biggers } __randomize_layout; 43222d94f49SEric Biggers 43322d94f49SEric Biggers /* 43422d94f49SEric Biggers * fscrypt_master_key - an in-use master key 43522d94f49SEric Biggers * 43622d94f49SEric Biggers * This represents a master encryption key which has been added to the 43722d94f49SEric Biggers * filesystem and can be used to "unlock" the encrypted files which were 43822d94f49SEric Biggers * encrypted with it. 43922d94f49SEric Biggers */ 44022d94f49SEric Biggers struct fscrypt_master_key { 44122d94f49SEric Biggers 442b1c0ec35SEric Biggers /* 443b1c0ec35SEric Biggers * The secret key material. After FS_IOC_REMOVE_ENCRYPTION_KEY is 444b1c0ec35SEric Biggers * executed, this is wiped and no new inodes can be unlocked with this 445b1c0ec35SEric Biggers * key; however, there may still be inodes in ->mk_decrypted_inodes 446b1c0ec35SEric Biggers * which could not be evicted. As long as some inodes still remain, 447b1c0ec35SEric Biggers * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or 448b1c0ec35SEric Biggers * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again. 449b1c0ec35SEric Biggers * 4504a4b8721SEric Biggers * Locking: protected by this master key's key->sem. 451b1c0ec35SEric Biggers */ 45222d94f49SEric Biggers struct fscrypt_master_key_secret mk_secret; 45322d94f49SEric Biggers 4545dae460cSEric Biggers /* 4555dae460cSEric Biggers * For v1 policy keys: an arbitrary key descriptor which was assigned by 4565dae460cSEric Biggers * userspace (->descriptor). 4575dae460cSEric Biggers * 4585dae460cSEric Biggers * For v2 policy keys: a cryptographic hash of this key (->identifier). 4595dae460cSEric Biggers */ 46022d94f49SEric Biggers struct fscrypt_key_specifier mk_spec; 46122d94f49SEric Biggers 462b1c0ec35SEric Biggers /* 46323c688b5SEric Biggers * Keyring which contains a key of type 'key_type_fscrypt_user' for each 46423c688b5SEric Biggers * user who has added this key. Normally each key will be added by just 46523c688b5SEric Biggers * one user, but it's possible that multiple users share a key, and in 46623c688b5SEric Biggers * that case we need to keep track of those users so that one user can't 46723c688b5SEric Biggers * remove the key before the others want it removed too. 46823c688b5SEric Biggers * 46923c688b5SEric Biggers * This is NULL for v1 policy keys; those can only be added by root. 47023c688b5SEric Biggers * 4714a4b8721SEric Biggers * Locking: in addition to this keyring's own semaphore, this is 4724a4b8721SEric Biggers * protected by this master key's key->sem, so we can do atomic 47323c688b5SEric Biggers * search+insert. It can also be searched without taking any locks, but 47423c688b5SEric Biggers * in that case the returned key may have already been removed. 47523c688b5SEric Biggers */ 47623c688b5SEric Biggers struct key *mk_users; 47723c688b5SEric Biggers 47823c688b5SEric Biggers /* 479b1c0ec35SEric Biggers * Length of ->mk_decrypted_inodes, plus one if mk_secret is present. 480b1c0ec35SEric Biggers * Once this goes to 0, the master key is removed from ->s_master_keys. 481b1c0ec35SEric Biggers * The 'struct fscrypt_master_key' will continue to live as long as the 482b1c0ec35SEric Biggers * 'struct key' whose payload it is, but we won't let this reference 483b1c0ec35SEric Biggers * count rise again. 484b1c0ec35SEric Biggers */ 485b1c0ec35SEric Biggers refcount_t mk_refcount; 486b1c0ec35SEric Biggers 487b1c0ec35SEric Biggers /* 488b1c0ec35SEric Biggers * List of inodes that were unlocked using this key. This allows the 489b1c0ec35SEric Biggers * inodes to be evicted efficiently if the key is removed. 490b1c0ec35SEric Biggers */ 491b1c0ec35SEric Biggers struct list_head mk_decrypted_inodes; 492b1c0ec35SEric Biggers spinlock_t mk_decrypted_inodes_lock; 493b1c0ec35SEric Biggers 494b103fb76SEric Biggers /* 495e3b1078bSEric Biggers * Per-mode encryption keys for the various types of encryption policies 496e3b1078bSEric Biggers * that use them. Allocated and derived on-demand. 497b103fb76SEric Biggers */ 4983ceb6543SEric Biggers struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1]; 4993ceb6543SEric Biggers struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1]; 5003ceb6543SEric Biggers struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1]; 501e3b1078bSEric Biggers 502e3b1078bSEric Biggers /* Hash key for inode numbers. Initialized only when needed. */ 503e3b1078bSEric Biggers siphash_key_t mk_ino_hash_key; 504e3b1078bSEric Biggers bool mk_ino_hash_key_initialized; 5055dae460cSEric Biggers 50622d94f49SEric Biggers } __randomize_layout; 50722d94f49SEric Biggers 508b1c0ec35SEric Biggers static inline bool 509b1c0ec35SEric Biggers is_master_key_secret_present(const struct fscrypt_master_key_secret *secret) 510b1c0ec35SEric Biggers { 511b1c0ec35SEric Biggers /* 512b1c0ec35SEric Biggers * The READ_ONCE() is only necessary for fscrypt_drop_inode() and 513b1c0ec35SEric Biggers * fscrypt_key_describe(). These run in atomic context, so they can't 5144a4b8721SEric Biggers * take the key semaphore and thus 'secret' can change concurrently 5154a4b8721SEric Biggers * which would be a data race. But they only need to know whether the 5164a4b8721SEric Biggers * secret *was* present at the time of check, so READ_ONCE() suffices. 517b1c0ec35SEric Biggers */ 518b1c0ec35SEric Biggers return READ_ONCE(secret->size) != 0; 519b1c0ec35SEric Biggers } 520b1c0ec35SEric Biggers 52122d94f49SEric Biggers static inline const char *master_key_spec_type( 52222d94f49SEric Biggers const struct fscrypt_key_specifier *spec) 52322d94f49SEric Biggers { 52422d94f49SEric Biggers switch (spec->type) { 52522d94f49SEric Biggers case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR: 52622d94f49SEric Biggers return "descriptor"; 5275dae460cSEric Biggers case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER: 5285dae460cSEric Biggers return "identifier"; 52922d94f49SEric Biggers } 53022d94f49SEric Biggers return "[unknown]"; 53122d94f49SEric Biggers } 53222d94f49SEric Biggers 53322d94f49SEric Biggers static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec) 53422d94f49SEric Biggers { 53522d94f49SEric Biggers switch (spec->type) { 53622d94f49SEric Biggers case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR: 53722d94f49SEric Biggers return FSCRYPT_KEY_DESCRIPTOR_SIZE; 5385dae460cSEric Biggers case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER: 5395dae460cSEric Biggers return FSCRYPT_KEY_IDENTIFIER_SIZE; 54022d94f49SEric Biggers } 54122d94f49SEric Biggers return 0; 54222d94f49SEric Biggers } 54322d94f49SEric Biggers 54460700902SEric Biggers struct key * 54522d94f49SEric Biggers fscrypt_find_master_key(struct super_block *sb, 54622d94f49SEric Biggers const struct fscrypt_key_specifier *mk_spec); 54722d94f49SEric Biggers 548*218d921bSEric Biggers int fscrypt_get_test_dummy_key_identifier( 549*218d921bSEric Biggers u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]); 550cdeb21daSEric Biggers 55160700902SEric Biggers int fscrypt_verify_key_added(struct super_block *sb, 5525ab7189aSEric Biggers const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]); 5535ab7189aSEric Biggers 55460700902SEric Biggers int __init fscrypt_init_keyring(void); 55522d94f49SEric Biggers 556feed8258SEric Biggers /* keysetup.c */ 5578094c3ceSEric Biggers 5588094c3ceSEric Biggers struct fscrypt_mode { 5598094c3ceSEric Biggers const char *friendly_name; 5608094c3ceSEric Biggers const char *cipher_str; 5617f595d6aSEric Biggers int keysize; /* key size in bytes */ 5627f595d6aSEric Biggers int security_strength; /* security strength in bytes */ 5637f595d6aSEric Biggers int ivsize; /* IV size in bytes */ 564a7a5bc5fSEric Biggers int logged_cryptoapi_impl; 565a7a5bc5fSEric Biggers int logged_blk_crypto_native; 566a7a5bc5fSEric Biggers int logged_blk_crypto_fallback; 5675fee3609SSatya Tangirala enum blk_crypto_mode_num blk_crypto_mode; 5688094c3ceSEric Biggers }; 5698094c3ceSEric Biggers 57085af90e5SEric Biggers extern struct fscrypt_mode fscrypt_modes[]; 5713ec4f2a6SEric Biggers 5725fee3609SSatya Tangirala int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key, 5735fee3609SSatya Tangirala const u8 *raw_key, const struct fscrypt_info *ci); 5745fee3609SSatya Tangirala 5755fee3609SSatya Tangirala void fscrypt_destroy_prepared_key(struct fscrypt_prepared_key *prep_key); 5760109ce76SEric Biggers 57760700902SEric Biggers int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key); 5780109ce76SEric Biggers 57960700902SEric Biggers int fscrypt_derive_dirhash_key(struct fscrypt_info *ci, 580aa408f83SDaniel Rosenberg const struct fscrypt_master_key *mk); 581aa408f83SDaniel Rosenberg 582a992b20cSEric Biggers void fscrypt_hash_inode_number(struct fscrypt_info *ci, 583a992b20cSEric Biggers const struct fscrypt_master_key *mk); 584a992b20cSEric Biggers 585a14d0b67SEric Biggers int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported); 5865b421f08SEric Biggers 587de3cdc6eSEric Biggers /** 588de3cdc6eSEric Biggers * fscrypt_require_key() - require an inode's encryption key 589de3cdc6eSEric Biggers * @inode: the inode we need the key for 590de3cdc6eSEric Biggers * 591de3cdc6eSEric Biggers * If the inode is encrypted, set up its encryption key if not already done. 592de3cdc6eSEric Biggers * Then require that the key be present and return -ENOKEY otherwise. 593de3cdc6eSEric Biggers * 594de3cdc6eSEric Biggers * No locks are needed, and the key will live as long as the struct inode --- so 595de3cdc6eSEric Biggers * it won't go away from under you. 596de3cdc6eSEric Biggers * 597de3cdc6eSEric Biggers * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code 598de3cdc6eSEric Biggers * if a problem occurred while setting up the encryption key. 599de3cdc6eSEric Biggers */ 600de3cdc6eSEric Biggers static inline int fscrypt_require_key(struct inode *inode) 601de3cdc6eSEric Biggers { 602de3cdc6eSEric Biggers if (IS_ENCRYPTED(inode)) { 603a14d0b67SEric Biggers int err = fscrypt_get_encryption_info(inode, false); 604de3cdc6eSEric Biggers 605de3cdc6eSEric Biggers if (err) 606de3cdc6eSEric Biggers return err; 607de3cdc6eSEric Biggers if (!fscrypt_has_encryption_key(inode)) 608de3cdc6eSEric Biggers return -ENOKEY; 609de3cdc6eSEric Biggers } 610de3cdc6eSEric Biggers return 0; 611de3cdc6eSEric Biggers } 612de3cdc6eSEric Biggers 6130109ce76SEric Biggers /* keysetup_v1.c */ 6140109ce76SEric Biggers 61560700902SEric Biggers void fscrypt_put_direct_key(struct fscrypt_direct_key *dk); 6160109ce76SEric Biggers 61760700902SEric Biggers int fscrypt_setup_v1_file_key(struct fscrypt_info *ci, 6180109ce76SEric Biggers const u8 *raw_master_key); 6190109ce76SEric Biggers 62060700902SEric Biggers int fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_info *ci); 62160700902SEric Biggers 6225dae460cSEric Biggers /* policy.c */ 6235dae460cSEric Biggers 62460700902SEric Biggers bool fscrypt_policies_equal(const union fscrypt_policy *policy1, 6255dae460cSEric Biggers const union fscrypt_policy *policy2); 626bfb9700bSEric Biggers int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy, 627bfb9700bSEric Biggers struct fscrypt_key_specifier *key_spec); 62860700902SEric Biggers bool fscrypt_supported_policy(const union fscrypt_policy *policy_u, 6295dae460cSEric Biggers const struct inode *inode); 63060700902SEric Biggers int fscrypt_policy_from_context(union fscrypt_policy *policy_u, 6315dae460cSEric Biggers const union fscrypt_context *ctx_u, 6325dae460cSEric Biggers int ctx_size); 633ac4acb1fSEric Biggers const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir); 6340109ce76SEric Biggers 6353325bea5STheodore Ts'o #endif /* _FSCRYPT_PRIVATE_H */ 636