/* 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 <crypto/sha2.h>14#include <linux/fscrypt.h>15#include <linux/minmax.h>16#include <linux/siphash.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 a pointer to it is stored in the file's in-memory inode. Once252* created, it remains until the 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 */383void fscrypt_init_hkdf(struct hmac_sha512_key *hkdf, const u8 *master_key,384unsigned int master_key_size);385386/*387* The list of contexts in which fscrypt uses HKDF. These values are used as388* the first byte of the HKDF application-specific info string to guarantee that389* info strings are never repeated between contexts. This ensures that all HKDF390* outputs are unique and cryptographically isolated, i.e. knowledge of one391* output doesn't reveal another.392*/393#define HKDF_CONTEXT_KEY_IDENTIFIER_FOR_RAW_KEY 1 /* info=<empty> */394#define HKDF_CONTEXT_PER_FILE_ENC_KEY 2 /* info=file_nonce */395#define HKDF_CONTEXT_DIRECT_KEY 3 /* info=mode_num */396#define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4 /* info=mode_num||fs_uuid */397#define HKDF_CONTEXT_DIRHASH_KEY 5 /* info=file_nonce */398#define HKDF_CONTEXT_IV_INO_LBLK_32_KEY 6 /* info=mode_num||fs_uuid */399#define HKDF_CONTEXT_INODE_HASH_KEY 7 /* info=<empty> */400#define HKDF_CONTEXT_KEY_IDENTIFIER_FOR_HW_WRAPPED_KEY \4018 /* info=<empty> */402403void fscrypt_hkdf_expand(const struct hmac_sha512_key *hkdf, u8 context,404const u8 *info, unsigned int infolen,405u8 *okm, unsigned int okmlen);406407/* inline_crypt.c */408#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT409int fscrypt_select_encryption_impl(struct fscrypt_inode_info *ci,410bool is_hw_wrapped_key);411412static inline bool413fscrypt_using_inline_encryption(const struct fscrypt_inode_info *ci)414{415return ci->ci_inlinecrypt;416}417418int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,419const u8 *key_bytes, size_t key_size,420bool is_hw_wrapped,421const struct fscrypt_inode_info *ci);422423void fscrypt_destroy_inline_crypt_key(struct super_block *sb,424struct fscrypt_prepared_key *prep_key);425426int fscrypt_derive_sw_secret(struct super_block *sb,427const u8 *wrapped_key, size_t wrapped_key_size,428u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE]);429430/*431* Check whether the crypto transform or blk-crypto key has been allocated in432* @prep_key, depending on which encryption implementation the file will use.433*/434static inline bool435fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,436const struct fscrypt_inode_info *ci)437{438/*439* The two smp_load_acquire()'s here pair with the smp_store_release()'s440* in fscrypt_prepare_inline_crypt_key() and fscrypt_prepare_key().441* I.e., in some cases (namely, if this prep_key is a per-mode442* encryption key) another task can publish blk_key or tfm concurrently,443* executing a RELEASE barrier. We need to use smp_load_acquire() here444* to safely ACQUIRE the memory the other task published.445*/446if (fscrypt_using_inline_encryption(ci))447return smp_load_acquire(&prep_key->blk_key) != NULL;448return smp_load_acquire(&prep_key->tfm) != NULL;449}450451#else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */452453static inline int fscrypt_select_encryption_impl(struct fscrypt_inode_info *ci,454bool is_hw_wrapped_key)455{456return 0;457}458459static inline bool460fscrypt_using_inline_encryption(const struct fscrypt_inode_info *ci)461{462return false;463}464465static inline int466fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,467const u8 *key_bytes, size_t key_size,468bool is_hw_wrapped,469const struct fscrypt_inode_info *ci)470{471WARN_ON_ONCE(1);472return -EOPNOTSUPP;473}474475static inline void476fscrypt_destroy_inline_crypt_key(struct super_block *sb,477struct fscrypt_prepared_key *prep_key)478{479}480481static inline int482fscrypt_derive_sw_secret(struct super_block *sb,483const u8 *wrapped_key, size_t wrapped_key_size,484u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE])485{486fscrypt_warn(NULL, "kernel doesn't support hardware-wrapped keys");487return -EOPNOTSUPP;488}489490static inline bool491fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,492const struct fscrypt_inode_info *ci)493{494return smp_load_acquire(&prep_key->tfm) != NULL;495}496#endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */497498/* keyring.c */499500/*501* fscrypt_master_key_secret - secret key material of an in-use master key502*/503struct fscrypt_master_key_secret {504505/*506* The KDF with which subkeys of this key can be derived.507*508* For v1 policy keys, this isn't applicable and won't be set.509* Otherwise, this KDF will be keyed by this master key if510* ->is_hw_wrapped=false, or by the "software secret" that hardware511* derived from this master key if ->is_hw_wrapped=true.512*/513struct hmac_sha512_key hkdf;514515/*516* True if this key is a hardware-wrapped key; false if this key is a517* raw key (i.e. a "software key"). For v1 policy keys this will always518* be false, as v1 policy support is a legacy feature which doesn't519* support newer functionality such as hardware-wrapped keys.520*/521bool is_hw_wrapped;522523/*524* Size of the key in bytes. This remains set even if ->bytes was525* zeroized due to no longer being needed. I.e. we still remember the526* size of the key even if we don't need to remember the key itself.527*/528u32 size;529530/*531* The bytes of the key, when still needed. This can be either a raw532* key or a hardware-wrapped key, as indicated by ->is_hw_wrapped. In533* the case of a raw, v2 policy key, there is no need to remember the534* actual key separately from ->hkdf so this field will be zeroized as535* soon as ->hkdf is initialized.536*/537u8 bytes[FSCRYPT_MAX_ANY_KEY_SIZE];538539} __randomize_layout;540541/*542* fscrypt_master_key - an in-use master key543*544* This represents a master encryption key which has been added to the545* filesystem. There are three high-level states that a key can be in:546*547* FSCRYPT_KEY_STATUS_PRESENT548* Key is fully usable; it can be used to unlock inodes that are encrypted549* with it (this includes being able to create new inodes). ->mk_present550* indicates whether the key is in this state. ->mk_secret exists, the key551* is in the keyring, and ->mk_active_refs > 0 due to ->mk_present.552*553* FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED554* Removal of this key has been initiated, but some inodes that were555* unlocked with it are still in-use. Like ABSENT, ->mk_secret is wiped,556* and the key can no longer be used to unlock inodes. Unlike ABSENT, the557* key is still in the keyring; ->mk_decrypted_inodes is nonempty; and558* ->mk_active_refs > 0, being equal to the size of ->mk_decrypted_inodes.559*560* This state transitions to ABSENT if ->mk_decrypted_inodes becomes empty,561* or to PRESENT if FS_IOC_ADD_ENCRYPTION_KEY is called again for this key.562*563* FSCRYPT_KEY_STATUS_ABSENT564* Key is fully removed. The key is no longer in the keyring,565* ->mk_decrypted_inodes is empty, ->mk_active_refs == 0, ->mk_secret is566* wiped, and the key can no longer be used to unlock inodes.567*/568struct fscrypt_master_key {569570/*571* Link in ->s_master_keys->key_hashtable.572* Only valid if ->mk_active_refs > 0.573*/574struct hlist_node mk_node;575576/* Semaphore that protects ->mk_secret, ->mk_users, and ->mk_present */577struct rw_semaphore mk_sem;578579/*580* Active and structural reference counts. An active ref guarantees581* that the struct continues to exist, continues to be in the keyring582* ->s_master_keys, and that any embedded subkeys (e.g.583* ->mk_direct_keys) that have been prepared continue to exist.584* A structural ref only guarantees that the struct continues to exist.585*586* There is one active ref associated with ->mk_present being true, and587* one active ref for each inode in ->mk_decrypted_inodes.588*589* There is one structural ref associated with the active refcount being590* nonzero. Finding a key in the keyring also takes a structural ref,591* which is then held temporarily while the key is operated on.592*/593refcount_t mk_active_refs;594refcount_t mk_struct_refs;595596struct rcu_head mk_rcu_head;597598/*599* The secret key material. Wiped as soon as it is no longer needed;600* for details, see the fscrypt_master_key struct comment.601*602* Locking: protected by ->mk_sem.603*/604struct fscrypt_master_key_secret mk_secret;605606/*607* For v1 policy keys: an arbitrary key descriptor which was assigned by608* userspace (->descriptor).609*610* For v2 policy keys: a cryptographic hash of this key (->identifier).611*/612struct fscrypt_key_specifier mk_spec;613614/*615* Keyring which contains a key of type 'key_type_fscrypt_user' for each616* user who has added this key. Normally each key will be added by just617* one user, but it's possible that multiple users share a key, and in618* that case we need to keep track of those users so that one user can't619* remove the key before the others want it removed too.620*621* This is NULL for v1 policy keys; those can only be added by root.622*623* Locking: protected by ->mk_sem. (We don't just rely on the keyrings624* subsystem semaphore ->mk_users->sem, as we need support for atomic625* search+insert along with proper synchronization with other fields.)626*/627struct key *mk_users;628629/*630* List of inodes that were unlocked using this key. This allows the631* inodes to be evicted efficiently if the key is removed.632*/633struct list_head mk_decrypted_inodes;634spinlock_t mk_decrypted_inodes_lock;635636/*637* Per-mode encryption keys for the various types of encryption policies638* that use them. Allocated and derived on-demand.639*/640struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1];641struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1];642struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1];643644/* Hash key for inode numbers. Initialized only when needed. */645siphash_key_t mk_ino_hash_key;646bool mk_ino_hash_key_initialized;647648/*649* Whether this key is in the "present" state, i.e. fully usable. For650* details, see the fscrypt_master_key struct comment.651*652* Locking: protected by ->mk_sem, but can be read locklessly using653* READ_ONCE(). Writers must use WRITE_ONCE() when concurrent readers654* are possible.655*/656bool mk_present;657658} __randomize_layout;659660static inline const char *master_key_spec_type(661const struct fscrypt_key_specifier *spec)662{663switch (spec->type) {664case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:665return "descriptor";666case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:667return "identifier";668}669return "[unknown]";670}671672static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)673{674switch (spec->type) {675case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:676return FSCRYPT_KEY_DESCRIPTOR_SIZE;677case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:678return FSCRYPT_KEY_IDENTIFIER_SIZE;679}680return 0;681}682683void fscrypt_put_master_key(struct fscrypt_master_key *mk);684685void fscrypt_put_master_key_activeref(struct super_block *sb,686struct fscrypt_master_key *mk);687688struct fscrypt_master_key *689fscrypt_find_master_key(struct super_block *sb,690const struct fscrypt_key_specifier *mk_spec);691692void fscrypt_get_test_dummy_key_identifier(693u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);694695int fscrypt_add_test_dummy_key(struct super_block *sb,696struct fscrypt_key_specifier *key_spec);697698int fscrypt_verify_key_added(struct super_block *sb,699const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);700701int __init fscrypt_init_keyring(void);702703/* keysetup.c */704705struct fscrypt_mode {706const char *friendly_name;707const char *cipher_str;708int keysize; /* key size in bytes */709int security_strength; /* security strength in bytes */710int ivsize; /* IV size in bytes */711int logged_cryptoapi_impl;712int logged_blk_crypto_native;713int logged_blk_crypto_fallback;714enum blk_crypto_mode_num blk_crypto_mode;715};716717extern struct fscrypt_mode fscrypt_modes[];718719int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,720const u8 *raw_key, const struct fscrypt_inode_info *ci);721722void fscrypt_destroy_prepared_key(struct super_block *sb,723struct fscrypt_prepared_key *prep_key);724725int fscrypt_set_per_file_enc_key(struct fscrypt_inode_info *ci,726const u8 *raw_key);727728void fscrypt_derive_dirhash_key(struct fscrypt_inode_info *ci,729const struct fscrypt_master_key *mk);730731void fscrypt_hash_inode_number(struct fscrypt_inode_info *ci,732const struct fscrypt_master_key *mk);733734int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported);735736/**737* fscrypt_require_key() - require an inode's encryption key738* @inode: the inode we need the key for739*740* If the inode is encrypted, set up its encryption key if not already done.741* Then require that the key be present and return -ENOKEY otherwise.742*743* No locks are needed, and the key will live as long as the struct inode --- so744* it won't go away from under you.745*746* Return: 0 on success, -ENOKEY if the key is missing, or another -errno code747* if a problem occurred while setting up the encryption key.748*/749static inline int fscrypt_require_key(struct inode *inode)750{751if (IS_ENCRYPTED(inode)) {752int err = fscrypt_get_encryption_info(inode, false);753754if (err)755return err;756if (!fscrypt_has_encryption_key(inode))757return -ENOKEY;758}759return 0;760}761762/* keysetup_v1.c */763764void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);765766int fscrypt_setup_v1_file_key(struct fscrypt_inode_info *ci,767const u8 *raw_master_key);768769int fscrypt_setup_v1_file_key_via_subscribed_keyrings(770struct fscrypt_inode_info *ci);771772/* policy.c */773774bool fscrypt_policies_equal(const union fscrypt_policy *policy1,775const union fscrypt_policy *policy2);776int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy,777struct fscrypt_key_specifier *key_spec);778const union fscrypt_policy *fscrypt_get_dummy_policy(struct super_block *sb);779bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,780const struct inode *inode);781int fscrypt_policy_from_context(union fscrypt_policy *policy_u,782const union fscrypt_context *ctx_u,783int ctx_size);784const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir);785786#endif /* _FSCRYPT_PRIVATE_H */787788789