/* SPDX-License-Identifier: GPL-2.0 */1/*2* fscrypt_private.h3*4* Copyright (C) 2015, Google, Inc.5*6* Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.7* Heavily modified since then.8*/910#ifndef _FSCRYPT_PRIVATE_H11#define _FSCRYPT_PRIVATE_H1213#include <linux/fscrypt.h>14#include <linux/minmax.h>15#include <linux/siphash.h>16#include <crypto/hash.h>17#include <linux/blk-crypto.h>1819#define CONST_STRLEN(str) (sizeof(str) - 1)2021#define FSCRYPT_FILE_NONCE_SIZE 162223/*24* Minimum size of an fscrypt master key. Note: a longer key will be required25* if ciphers with a 256-bit security strength are used. This is just the26* absolute minimum, which applies when only 128-bit encryption is used.27*/28#define FSCRYPT_MIN_KEY_SIZE 162930/* Maximum size of a raw fscrypt master key */31#define FSCRYPT_MAX_RAW_KEY_SIZE 643233/* Maximum size of a hardware-wrapped fscrypt master key */34#define FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE3536/* Maximum size of an fscrypt master key across both key types */37#define FSCRYPT_MAX_ANY_KEY_SIZE \38MAX(FSCRYPT_MAX_RAW_KEY_SIZE, FSCRYPT_MAX_HW_WRAPPED_KEY_SIZE)3940/*41* FSCRYPT_MAX_KEY_SIZE is defined in the UAPI header, but the addition of42* hardware-wrapped keys has made it misleading as it's only for raw keys.43* Don't use it in kernel code; use one of the above constants instead.44*/45#undef FSCRYPT_MAX_KEY_SIZE4647/*48* This mask is passed as the third argument to the crypto_alloc_*() functions49* to prevent fscrypt from using the Crypto API drivers for non-inline crypto50* engines. Those drivers have been problematic for fscrypt. fscrypt users51* have reported hangs and even incorrect en/decryption with these drivers.52* Since going to the driver, off CPU, and back again is really slow, such53* drivers can be over 50 times slower than the CPU-based code for fscrypt's54* workload. Even on platforms that lack AES instructions on the CPU, using the55* offloads has been shown to be slower, even staying with AES. (Of course,56* Adiantum is faster still, and is the recommended option on such platforms...)57*58* Note that fscrypt also supports inline crypto engines. Those don't use the59* Crypto API and work much better than the old-style (non-inline) engines.60*/61#define FSCRYPT_CRYPTOAPI_MASK \62(CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY | \63CRYPTO_ALG_KERN_DRIVER_ONLY)6465#define FSCRYPT_CONTEXT_V1 166#define FSCRYPT_CONTEXT_V2 26768/* Keep this in sync with include/uapi/linux/fscrypt.h */69#define FSCRYPT_MODE_MAX FSCRYPT_MODE_AES_256_HCTR27071struct fscrypt_context_v1 {72u8 version; /* FSCRYPT_CONTEXT_V1 */73u8 contents_encryption_mode;74u8 filenames_encryption_mode;75u8 flags;76u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];77u8 nonce[FSCRYPT_FILE_NONCE_SIZE];78};7980struct fscrypt_context_v2 {81u8 version; /* FSCRYPT_CONTEXT_V2 */82u8 contents_encryption_mode;83u8 filenames_encryption_mode;84u8 flags;85u8 log2_data_unit_size;86u8 __reserved[3];87u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];88u8 nonce[FSCRYPT_FILE_NONCE_SIZE];89};9091/*92* fscrypt_context - the encryption context of an inode93*94* This is the on-disk equivalent of an fscrypt_policy, stored alongside each95* encrypted file usually in a hidden extended attribute. It contains the96* fields from the fscrypt_policy, in order to identify the encryption algorithm97* and key with which the file is encrypted. It also contains a nonce that was98* randomly generated by fscrypt itself; this is used as KDF input or as a tweak99* to cause different files to be encrypted differently.100*/101union fscrypt_context {102u8 version;103struct fscrypt_context_v1 v1;104struct fscrypt_context_v2 v2;105};106107/*108* Return the size expected for the given fscrypt_context based on its version109* number, or 0 if the context version is unrecognized.110*/111static inline int fscrypt_context_size(const union fscrypt_context *ctx)112{113switch (ctx->version) {114case FSCRYPT_CONTEXT_V1:115BUILD_BUG_ON(sizeof(ctx->v1) != 28);116return sizeof(ctx->v1);117case FSCRYPT_CONTEXT_V2:118BUILD_BUG_ON(sizeof(ctx->v2) != 40);119return sizeof(ctx->v2);120}121return 0;122}123124/* Check whether an fscrypt_context has a recognized version number and size */125static inline bool fscrypt_context_is_valid(const union fscrypt_context *ctx,126int ctx_size)127{128return ctx_size >= 1 && ctx_size == fscrypt_context_size(ctx);129}130131/* Retrieve the context's nonce, assuming the context was already validated */132static inline const u8 *fscrypt_context_nonce(const union fscrypt_context *ctx)133{134switch (ctx->version) {135case FSCRYPT_CONTEXT_V1:136return ctx->v1.nonce;137case FSCRYPT_CONTEXT_V2:138return ctx->v2.nonce;139}140WARN_ON_ONCE(1);141return NULL;142}143144union fscrypt_policy {145u8 version;146struct fscrypt_policy_v1 v1;147struct fscrypt_policy_v2 v2;148};149150/*151* Return the size expected for the given fscrypt_policy based on its version152* number, or 0 if the policy version is unrecognized.153*/154static inline int fscrypt_policy_size(const union fscrypt_policy *policy)155{156switch (policy->version) {157case FSCRYPT_POLICY_V1:158return sizeof(policy->v1);159case FSCRYPT_POLICY_V2:160return sizeof(policy->v2);161}162return 0;163}164165/* Return the contents encryption mode of a valid encryption policy */166static inline u8167fscrypt_policy_contents_mode(const union fscrypt_policy *policy)168{169switch (policy->version) {170case FSCRYPT_POLICY_V1:171return policy->v1.contents_encryption_mode;172case FSCRYPT_POLICY_V2:173return policy->v2.contents_encryption_mode;174}175BUG();176}177178/* Return the filenames encryption mode of a valid encryption policy */179static inline u8180fscrypt_policy_fnames_mode(const union fscrypt_policy *policy)181{182switch (policy->version) {183case FSCRYPT_POLICY_V1:184return policy->v1.filenames_encryption_mode;185case FSCRYPT_POLICY_V2:186return policy->v2.filenames_encryption_mode;187}188BUG();189}190191/* Return the flags (FSCRYPT_POLICY_FLAG*) of a valid encryption policy */192static inline u8193fscrypt_policy_flags(const union fscrypt_policy *policy)194{195switch (policy->version) {196case FSCRYPT_POLICY_V1:197return policy->v1.flags;198case FSCRYPT_POLICY_V2:199return policy->v2.flags;200}201BUG();202}203204static inline int205fscrypt_policy_v2_du_bits(const struct fscrypt_policy_v2 *policy,206const struct inode *inode)207{208return policy->log2_data_unit_size ?: inode->i_blkbits;209}210211static inline int212fscrypt_policy_du_bits(const union fscrypt_policy *policy,213const struct inode *inode)214{215switch (policy->version) {216case FSCRYPT_POLICY_V1:217return inode->i_blkbits;218case FSCRYPT_POLICY_V2:219return fscrypt_policy_v2_du_bits(&policy->v2, inode);220}221BUG();222}223224/*225* For encrypted symlinks, the ciphertext length is stored at the beginning226* of the string in little-endian format.227*/228struct fscrypt_symlink_data {229__le16 len;230char encrypted_path[];231} __packed;232233/**234* struct fscrypt_prepared_key - a key prepared for actual encryption/decryption235* @tfm: crypto API transform object236* @blk_key: key for blk-crypto237*238* Normally only one of the fields will be non-NULL.239*/240struct fscrypt_prepared_key {241struct crypto_sync_skcipher *tfm;242#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT243struct blk_crypto_key *blk_key;244#endif245};246247/*248* fscrypt_inode_info - the "encryption key" for an inode249*250* When an encrypted file's key is made available, an instance of this struct is251* allocated and stored in ->i_crypt_info. Once created, it remains until the252* inode is evicted.253*/254struct fscrypt_inode_info {255256/* The key in a form prepared for actual encryption/decryption */257struct fscrypt_prepared_key ci_enc_key;258259/* True if ci_enc_key should be freed when this struct is freed */260u8 ci_owns_key : 1;261262#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT263/*264* True if this inode will use inline encryption (blk-crypto) instead of265* the traditional filesystem-layer encryption.266*/267u8 ci_inlinecrypt : 1;268#endif269270/* True if ci_dirhash_key is initialized */271u8 ci_dirhash_key_initialized : 1;272273/*274* log2 of the data unit size (granularity of contents encryption) of275* this file. This is computable from ci_policy and ci_inode but is276* cached here for efficiency. Only used for regular files.277*/278u8 ci_data_unit_bits;279280/* Cached value: log2 of number of data units per FS block */281u8 ci_data_units_per_block_bits;282283/* Hashed inode number. Only set for IV_INO_LBLK_32 */284u32 ci_hashed_ino;285286/*287* Encryption mode used for this inode. It corresponds to either the288* contents or filenames encryption mode, depending on the inode type.289*/290struct fscrypt_mode *ci_mode;291292/* Back-pointer to the inode */293struct inode *ci_inode;294295/*296* The master key with which this inode was unlocked (decrypted). This297* will be NULL if the master key was found in a process-subscribed298* keyring rather than in the filesystem-level keyring.299*/300struct fscrypt_master_key *ci_master_key;301302/*303* Link in list of inodes that were unlocked with the master key.304* Only used when ->ci_master_key is set.305*/306struct list_head ci_master_key_link;307308/*309* If non-NULL, then encryption is done using the master key directly310* and ci_enc_key will equal ci_direct_key->dk_key.311*/312struct fscrypt_direct_key *ci_direct_key;313314/*315* This inode's hash key for filenames. This is a 128-bit SipHash-2-4316* key. This is only set for directories that use a keyed dirhash over317* the plaintext filenames -- currently just casefolded directories.318*/319siphash_key_t ci_dirhash_key;320321/* The encryption policy used by this inode */322union fscrypt_policy ci_policy;323324/* This inode's nonce, copied from the fscrypt_context */325u8 ci_nonce[FSCRYPT_FILE_NONCE_SIZE];326};327328typedef enum {329FS_DECRYPT = 0,330FS_ENCRYPT,331} fscrypt_direction_t;332333/* crypto.c */334extern struct kmem_cache *fscrypt_inode_info_cachep;335int fscrypt_initialize(struct super_block *sb);336int fscrypt_crypt_data_unit(const struct fscrypt_inode_info *ci,337fscrypt_direction_t rw, u64 index,338struct page *src_page, struct page *dest_page,339unsigned int len, unsigned int offs);340struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags);341342void __printf(3, 4) __cold343fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...);344345#define fscrypt_warn(inode, fmt, ...) \346fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)347#define fscrypt_err(inode, fmt, ...) \348fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)349350#define FSCRYPT_MAX_IV_SIZE 32351352union fscrypt_iv {353struct {354/* zero-based index of data unit within the file */355__le64 index;356357/* per-file nonce; only set in DIRECT_KEY mode */358u8 nonce[FSCRYPT_FILE_NONCE_SIZE];359};360u8 raw[FSCRYPT_MAX_IV_SIZE];361__le64 dun[FSCRYPT_MAX_IV_SIZE / sizeof(__le64)];362};363364void fscrypt_generate_iv(union fscrypt_iv *iv, u64 index,365const struct fscrypt_inode_info *ci);366367/*368* Return the number of bits used by the maximum file data unit index that is369* possible on the given filesystem, using the given log2 data unit size.370*/371static inline int372fscrypt_max_file_dun_bits(const struct super_block *sb, int du_bits)373{374return fls64(sb->s_maxbytes - 1) - du_bits;375}376377/* fname.c */378bool __fscrypt_fname_encrypted_size(const union fscrypt_policy *policy,379u32 orig_len, u32 max_len,380u32 *encrypted_len_ret);381382/* hkdf.c */383struct fscrypt_hkdf {384struct crypto_shash *hmac_tfm;385};386387int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,388unsigned int master_key_size);389390/*391* The list of contexts in which fscrypt uses HKDF. These values are used as392* the first byte of the HKDF application-specific info string to guarantee that393* info strings are never repeated between contexts. This ensures that all HKDF394* outputs are unique and cryptographically isolated, i.e. knowledge of one395* output doesn't reveal another.396*/397#define HKDF_CONTEXT_KEY_IDENTIFIER_FOR_RAW_KEY 1 /* info=<empty> */398#define HKDF_CONTEXT_PER_FILE_ENC_KEY 2 /* info=file_nonce */399#define HKDF_CONTEXT_DIRECT_KEY 3 /* info=mode_num */400#define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4 /* info=mode_num||fs_uuid */401#define HKDF_CONTEXT_DIRHASH_KEY 5 /* info=file_nonce */402#define HKDF_CONTEXT_IV_INO_LBLK_32_KEY 6 /* info=mode_num||fs_uuid */403#define HKDF_CONTEXT_INODE_HASH_KEY 7 /* info=<empty> */404#define HKDF_CONTEXT_KEY_IDENTIFIER_FOR_HW_WRAPPED_KEY \4058 /* info=<empty> */406407int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,408const u8 *info, unsigned int infolen,409u8 *okm, unsigned int okmlen);410411void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf);412413/* inline_crypt.c */414#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT415int fscrypt_select_encryption_impl(struct fscrypt_inode_info *ci,416bool is_hw_wrapped_key);417418static inline bool419fscrypt_using_inline_encryption(const struct fscrypt_inode_info *ci)420{421return ci->ci_inlinecrypt;422}423424int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,425const u8 *key_bytes, size_t key_size,426bool is_hw_wrapped,427const struct fscrypt_inode_info *ci);428429void fscrypt_destroy_inline_crypt_key(struct super_block *sb,430struct fscrypt_prepared_key *prep_key);431432int fscrypt_derive_sw_secret(struct super_block *sb,433const u8 *wrapped_key, size_t wrapped_key_size,434u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE]);435436/*437* Check whether the crypto transform or blk-crypto key has been allocated in438* @prep_key, depending on which encryption implementation the file will use.439*/440static inline bool441fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,442const struct fscrypt_inode_info *ci)443{444/*445* The two smp_load_acquire()'s here pair with the smp_store_release()'s446* in fscrypt_prepare_inline_crypt_key() and fscrypt_prepare_key().447* I.e., in some cases (namely, if this prep_key is a per-mode448* encryption key) another task can publish blk_key or tfm concurrently,449* executing a RELEASE barrier. We need to use smp_load_acquire() here450* to safely ACQUIRE the memory the other task published.451*/452if (fscrypt_using_inline_encryption(ci))453return smp_load_acquire(&prep_key->blk_key) != NULL;454return smp_load_acquire(&prep_key->tfm) != NULL;455}456457#else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */458459static inline int fscrypt_select_encryption_impl(struct fscrypt_inode_info *ci,460bool is_hw_wrapped_key)461{462return 0;463}464465static inline bool466fscrypt_using_inline_encryption(const struct fscrypt_inode_info *ci)467{468return false;469}470471static inline int472fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,473const u8 *key_bytes, size_t key_size,474bool is_hw_wrapped,475const struct fscrypt_inode_info *ci)476{477WARN_ON_ONCE(1);478return -EOPNOTSUPP;479}480481static inline void482fscrypt_destroy_inline_crypt_key(struct super_block *sb,483struct fscrypt_prepared_key *prep_key)484{485}486487static inline int488fscrypt_derive_sw_secret(struct super_block *sb,489const u8 *wrapped_key, size_t wrapped_key_size,490u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE])491{492fscrypt_warn(NULL, "kernel doesn't support hardware-wrapped keys");493return -EOPNOTSUPP;494}495496static inline bool497fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,498const struct fscrypt_inode_info *ci)499{500return smp_load_acquire(&prep_key->tfm) != NULL;501}502#endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */503504/* keyring.c */505506/*507* fscrypt_master_key_secret - secret key material of an in-use master key508*/509struct fscrypt_master_key_secret {510511/*512* The KDF with which subkeys of this key can be derived.513*514* For v1 policy keys, this isn't applicable and won't be set.515* Otherwise, this KDF will be keyed by this master key if516* ->is_hw_wrapped=false, or by the "software secret" that hardware517* derived from this master key if ->is_hw_wrapped=true.518*/519struct fscrypt_hkdf hkdf;520521/*522* True if this key is a hardware-wrapped key; false if this key is a523* raw key (i.e. a "software key"). For v1 policy keys this will always524* be false, as v1 policy support is a legacy feature which doesn't525* support newer functionality such as hardware-wrapped keys.526*/527bool is_hw_wrapped;528529/*530* Size of the key in bytes. This remains set even if ->bytes was531* zeroized due to no longer being needed. I.e. we still remember the532* size of the key even if we don't need to remember the key itself.533*/534u32 size;535536/*537* The bytes of the key, when still needed. This can be either a raw538* key or a hardware-wrapped key, as indicated by ->is_hw_wrapped. In539* the case of a raw, v2 policy key, there is no need to remember the540* actual key separately from ->hkdf so this field will be zeroized as541* soon as ->hkdf is initialized.542*/543u8 bytes[FSCRYPT_MAX_ANY_KEY_SIZE];544545} __randomize_layout;546547/*548* fscrypt_master_key - an in-use master key549*550* This represents a master encryption key which has been added to the551* filesystem. There are three high-level states that a key can be in:552*553* FSCRYPT_KEY_STATUS_PRESENT554* Key is fully usable; it can be used to unlock inodes that are encrypted555* with it (this includes being able to create new inodes). ->mk_present556* indicates whether the key is in this state. ->mk_secret exists, the key557* is in the keyring, and ->mk_active_refs > 0 due to ->mk_present.558*559* FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED560* Removal of this key has been initiated, but some inodes that were561* unlocked with it are still in-use. Like ABSENT, ->mk_secret is wiped,562* and the key can no longer be used to unlock inodes. Unlike ABSENT, the563* key is still in the keyring; ->mk_decrypted_inodes is nonempty; and564* ->mk_active_refs > 0, being equal to the size of ->mk_decrypted_inodes.565*566* This state transitions to ABSENT if ->mk_decrypted_inodes becomes empty,567* or to PRESENT if FS_IOC_ADD_ENCRYPTION_KEY is called again for this key.568*569* FSCRYPT_KEY_STATUS_ABSENT570* Key is fully removed. The key is no longer in the keyring,571* ->mk_decrypted_inodes is empty, ->mk_active_refs == 0, ->mk_secret is572* wiped, and the key can no longer be used to unlock inodes.573*/574struct fscrypt_master_key {575576/*577* Link in ->s_master_keys->key_hashtable.578* Only valid if ->mk_active_refs > 0.579*/580struct hlist_node mk_node;581582/* Semaphore that protects ->mk_secret, ->mk_users, and ->mk_present */583struct rw_semaphore mk_sem;584585/*586* Active and structural reference counts. An active ref guarantees587* that the struct continues to exist, continues to be in the keyring588* ->s_master_keys, and that any embedded subkeys (e.g.589* ->mk_direct_keys) that have been prepared continue to exist.590* A structural ref only guarantees that the struct continues to exist.591*592* There is one active ref associated with ->mk_present being true, and593* one active ref for each inode in ->mk_decrypted_inodes.594*595* There is one structural ref associated with the active refcount being596* nonzero. Finding a key in the keyring also takes a structural ref,597* which is then held temporarily while the key is operated on.598*/599refcount_t mk_active_refs;600refcount_t mk_struct_refs;601602struct rcu_head mk_rcu_head;603604/*605* The secret key material. Wiped as soon as it is no longer needed;606* for details, see the fscrypt_master_key struct comment.607*608* Locking: protected by ->mk_sem.609*/610struct fscrypt_master_key_secret mk_secret;611612/*613* For v1 policy keys: an arbitrary key descriptor which was assigned by614* userspace (->descriptor).615*616* For v2 policy keys: a cryptographic hash of this key (->identifier).617*/618struct fscrypt_key_specifier mk_spec;619620/*621* Keyring which contains a key of type 'key_type_fscrypt_user' for each622* user who has added this key. Normally each key will be added by just623* one user, but it's possible that multiple users share a key, and in624* that case we need to keep track of those users so that one user can't625* remove the key before the others want it removed too.626*627* This is NULL for v1 policy keys; those can only be added by root.628*629* Locking: protected by ->mk_sem. (We don't just rely on the keyrings630* subsystem semaphore ->mk_users->sem, as we need support for atomic631* search+insert along with proper synchronization with other fields.)632*/633struct key *mk_users;634635/*636* List of inodes that were unlocked using this key. This allows the637* inodes to be evicted efficiently if the key is removed.638*/639struct list_head mk_decrypted_inodes;640spinlock_t mk_decrypted_inodes_lock;641642/*643* Per-mode encryption keys for the various types of encryption policies644* that use them. Allocated and derived on-demand.645*/646struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1];647struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1];648struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1];649650/* Hash key for inode numbers. Initialized only when needed. */651siphash_key_t mk_ino_hash_key;652bool mk_ino_hash_key_initialized;653654/*655* Whether this key is in the "present" state, i.e. fully usable. For656* details, see the fscrypt_master_key struct comment.657*658* Locking: protected by ->mk_sem, but can be read locklessly using659* READ_ONCE(). Writers must use WRITE_ONCE() when concurrent readers660* are possible.661*/662bool mk_present;663664} __randomize_layout;665666static inline const char *master_key_spec_type(667const struct fscrypt_key_specifier *spec)668{669switch (spec->type) {670case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:671return "descriptor";672case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:673return "identifier";674}675return "[unknown]";676}677678static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)679{680switch (spec->type) {681case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:682return FSCRYPT_KEY_DESCRIPTOR_SIZE;683case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:684return FSCRYPT_KEY_IDENTIFIER_SIZE;685}686return 0;687}688689void fscrypt_put_master_key(struct fscrypt_master_key *mk);690691void fscrypt_put_master_key_activeref(struct super_block *sb,692struct fscrypt_master_key *mk);693694struct fscrypt_master_key *695fscrypt_find_master_key(struct super_block *sb,696const struct fscrypt_key_specifier *mk_spec);697698int fscrypt_get_test_dummy_key_identifier(699u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);700701int fscrypt_add_test_dummy_key(struct super_block *sb,702struct fscrypt_key_specifier *key_spec);703704int fscrypt_verify_key_added(struct super_block *sb,705const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);706707int __init fscrypt_init_keyring(void);708709/* keysetup.c */710711struct fscrypt_mode {712const char *friendly_name;713const char *cipher_str;714int keysize; /* key size in bytes */715int security_strength; /* security strength in bytes */716int ivsize; /* IV size in bytes */717int logged_cryptoapi_impl;718int logged_blk_crypto_native;719int logged_blk_crypto_fallback;720enum blk_crypto_mode_num blk_crypto_mode;721};722723extern struct fscrypt_mode fscrypt_modes[];724725int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,726const u8 *raw_key, const struct fscrypt_inode_info *ci);727728void fscrypt_destroy_prepared_key(struct super_block *sb,729struct fscrypt_prepared_key *prep_key);730731int fscrypt_set_per_file_enc_key(struct fscrypt_inode_info *ci,732const u8 *raw_key);733734int fscrypt_derive_dirhash_key(struct fscrypt_inode_info *ci,735const struct fscrypt_master_key *mk);736737void fscrypt_hash_inode_number(struct fscrypt_inode_info *ci,738const struct fscrypt_master_key *mk);739740int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported);741742/**743* fscrypt_require_key() - require an inode's encryption key744* @inode: the inode we need the key for745*746* If the inode is encrypted, set up its encryption key if not already done.747* Then require that the key be present and return -ENOKEY otherwise.748*749* No locks are needed, and the key will live as long as the struct inode --- so750* it won't go away from under you.751*752* Return: 0 on success, -ENOKEY if the key is missing, or another -errno code753* if a problem occurred while setting up the encryption key.754*/755static inline int fscrypt_require_key(struct inode *inode)756{757if (IS_ENCRYPTED(inode)) {758int err = fscrypt_get_encryption_info(inode, false);759760if (err)761return err;762if (!fscrypt_has_encryption_key(inode))763return -ENOKEY;764}765return 0;766}767768/* keysetup_v1.c */769770void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);771772int fscrypt_setup_v1_file_key(struct fscrypt_inode_info *ci,773const u8 *raw_master_key);774775int fscrypt_setup_v1_file_key_via_subscribed_keyrings(776struct fscrypt_inode_info *ci);777778/* policy.c */779780bool fscrypt_policies_equal(const union fscrypt_policy *policy1,781const union fscrypt_policy *policy2);782int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy,783struct fscrypt_key_specifier *key_spec);784const union fscrypt_policy *fscrypt_get_dummy_policy(struct super_block *sb);785bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,786const struct inode *inode);787int fscrypt_policy_from_context(union fscrypt_policy *policy_u,788const union fscrypt_context *ctx_u,789int ctx_size);790const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir);791792#endif /* _FSCRYPT_PRIVATE_H */793794795