0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef _FSCRYPT_PRIVATE_H
0012 #define _FSCRYPT_PRIVATE_H
0013
0014 #include <linux/fscrypt.h>
0015 #include <linux/siphash.h>
0016 #include <crypto/hash.h>
0017 #include <linux/blk-crypto.h>
0018
0019 #define CONST_STRLEN(str) (sizeof(str) - 1)
0020
0021 #define FSCRYPT_FILE_NONCE_SIZE 16
0022
0023
0024
0025
0026
0027
0028 #define FSCRYPT_MIN_KEY_SIZE 16
0029
0030 #define FSCRYPT_CONTEXT_V1 1
0031 #define FSCRYPT_CONTEXT_V2 2
0032
0033
0034 #define FSCRYPT_MODE_MAX FSCRYPT_MODE_AES_256_HCTR2
0035
0036 struct fscrypt_context_v1 {
0037 u8 version;
0038 u8 contents_encryption_mode;
0039 u8 filenames_encryption_mode;
0040 u8 flags;
0041 u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
0042 u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
0043 };
0044
0045 struct fscrypt_context_v2 {
0046 u8 version;
0047 u8 contents_encryption_mode;
0048 u8 filenames_encryption_mode;
0049 u8 flags;
0050 u8 __reserved[4];
0051 u8 master_key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
0052 u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
0053 };
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065 union fscrypt_context {
0066 u8 version;
0067 struct fscrypt_context_v1 v1;
0068 struct fscrypt_context_v2 v2;
0069 };
0070
0071
0072
0073
0074
0075 static inline int fscrypt_context_size(const union fscrypt_context *ctx)
0076 {
0077 switch (ctx->version) {
0078 case FSCRYPT_CONTEXT_V1:
0079 BUILD_BUG_ON(sizeof(ctx->v1) != 28);
0080 return sizeof(ctx->v1);
0081 case FSCRYPT_CONTEXT_V2:
0082 BUILD_BUG_ON(sizeof(ctx->v2) != 40);
0083 return sizeof(ctx->v2);
0084 }
0085 return 0;
0086 }
0087
0088
0089 static inline bool fscrypt_context_is_valid(const union fscrypt_context *ctx,
0090 int ctx_size)
0091 {
0092 return ctx_size >= 1 && ctx_size == fscrypt_context_size(ctx);
0093 }
0094
0095
0096 static inline const u8 *fscrypt_context_nonce(const union fscrypt_context *ctx)
0097 {
0098 switch (ctx->version) {
0099 case FSCRYPT_CONTEXT_V1:
0100 return ctx->v1.nonce;
0101 case FSCRYPT_CONTEXT_V2:
0102 return ctx->v2.nonce;
0103 }
0104 WARN_ON(1);
0105 return NULL;
0106 }
0107
0108 union fscrypt_policy {
0109 u8 version;
0110 struct fscrypt_policy_v1 v1;
0111 struct fscrypt_policy_v2 v2;
0112 };
0113
0114
0115
0116
0117
0118 static inline int fscrypt_policy_size(const union fscrypt_policy *policy)
0119 {
0120 switch (policy->version) {
0121 case FSCRYPT_POLICY_V1:
0122 return sizeof(policy->v1);
0123 case FSCRYPT_POLICY_V2:
0124 return sizeof(policy->v2);
0125 }
0126 return 0;
0127 }
0128
0129
0130 static inline u8
0131 fscrypt_policy_contents_mode(const union fscrypt_policy *policy)
0132 {
0133 switch (policy->version) {
0134 case FSCRYPT_POLICY_V1:
0135 return policy->v1.contents_encryption_mode;
0136 case FSCRYPT_POLICY_V2:
0137 return policy->v2.contents_encryption_mode;
0138 }
0139 BUG();
0140 }
0141
0142
0143 static inline u8
0144 fscrypt_policy_fnames_mode(const union fscrypt_policy *policy)
0145 {
0146 switch (policy->version) {
0147 case FSCRYPT_POLICY_V1:
0148 return policy->v1.filenames_encryption_mode;
0149 case FSCRYPT_POLICY_V2:
0150 return policy->v2.filenames_encryption_mode;
0151 }
0152 BUG();
0153 }
0154
0155
0156 static inline u8
0157 fscrypt_policy_flags(const union fscrypt_policy *policy)
0158 {
0159 switch (policy->version) {
0160 case FSCRYPT_POLICY_V1:
0161 return policy->v1.flags;
0162 case FSCRYPT_POLICY_V2:
0163 return policy->v2.flags;
0164 }
0165 BUG();
0166 }
0167
0168
0169
0170
0171
0172 struct fscrypt_symlink_data {
0173 __le16 len;
0174 char encrypted_path[1];
0175 } __packed;
0176
0177
0178
0179
0180
0181
0182
0183
0184 struct fscrypt_prepared_key {
0185 struct crypto_skcipher *tfm;
0186 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
0187 struct fscrypt_blk_crypto_key *blk_key;
0188 #endif
0189 };
0190
0191
0192
0193
0194
0195
0196
0197
0198 struct fscrypt_info {
0199
0200
0201 struct fscrypt_prepared_key ci_enc_key;
0202
0203
0204 bool ci_owns_key;
0205
0206 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
0207
0208
0209
0210
0211 bool ci_inlinecrypt;
0212 #endif
0213
0214
0215
0216
0217
0218 struct fscrypt_mode *ci_mode;
0219
0220
0221 struct inode *ci_inode;
0222
0223
0224
0225
0226
0227
0228 struct key *ci_master_key;
0229
0230
0231
0232
0233
0234 struct list_head ci_master_key_link;
0235
0236
0237
0238
0239
0240 struct fscrypt_direct_key *ci_direct_key;
0241
0242
0243
0244
0245
0246
0247 siphash_key_t ci_dirhash_key;
0248 bool ci_dirhash_key_initialized;
0249
0250
0251 union fscrypt_policy ci_policy;
0252
0253
0254 u8 ci_nonce[FSCRYPT_FILE_NONCE_SIZE];
0255
0256
0257 u32 ci_hashed_ino;
0258 };
0259
0260 typedef enum {
0261 FS_DECRYPT = 0,
0262 FS_ENCRYPT,
0263 } fscrypt_direction_t;
0264
0265
0266 extern struct kmem_cache *fscrypt_info_cachep;
0267 int fscrypt_initialize(unsigned int cop_flags);
0268 int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw,
0269 u64 lblk_num, struct page *src_page,
0270 struct page *dest_page, unsigned int len,
0271 unsigned int offs, gfp_t gfp_flags);
0272 struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags);
0273
0274 void __printf(3, 4) __cold
0275 fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...);
0276
0277 #define fscrypt_warn(inode, fmt, ...) \
0278 fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
0279 #define fscrypt_err(inode, fmt, ...) \
0280 fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
0281
0282 #define FSCRYPT_MAX_IV_SIZE 32
0283
0284 union fscrypt_iv {
0285 struct {
0286
0287 __le64 lblk_num;
0288
0289
0290 u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
0291 };
0292 u8 raw[FSCRYPT_MAX_IV_SIZE];
0293 __le64 dun[FSCRYPT_MAX_IV_SIZE / sizeof(__le64)];
0294 };
0295
0296 void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
0297 const struct fscrypt_info *ci);
0298
0299
0300 bool __fscrypt_fname_encrypted_size(const union fscrypt_policy *policy,
0301 u32 orig_len, u32 max_len,
0302 u32 *encrypted_len_ret);
0303
0304
0305 struct fscrypt_hkdf {
0306 struct crypto_shash *hmac_tfm;
0307 };
0308
0309 int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
0310 unsigned int master_key_size);
0311
0312
0313
0314
0315
0316
0317
0318
0319 #define HKDF_CONTEXT_KEY_IDENTIFIER 1
0320 #define HKDF_CONTEXT_PER_FILE_ENC_KEY 2
0321 #define HKDF_CONTEXT_DIRECT_KEY 3
0322 #define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4
0323 #define HKDF_CONTEXT_DIRHASH_KEY 5
0324 #define HKDF_CONTEXT_IV_INO_LBLK_32_KEY 6
0325 #define HKDF_CONTEXT_INODE_HASH_KEY 7
0326
0327 int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
0328 const u8 *info, unsigned int infolen,
0329 u8 *okm, unsigned int okmlen);
0330
0331 void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf);
0332
0333
0334 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
0335 int fscrypt_select_encryption_impl(struct fscrypt_info *ci);
0336
0337 static inline bool
0338 fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
0339 {
0340 return ci->ci_inlinecrypt;
0341 }
0342
0343 int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
0344 const u8 *raw_key,
0345 const struct fscrypt_info *ci);
0346
0347 void fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key);
0348
0349
0350
0351
0352
0353 static inline bool
0354 fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
0355 const struct fscrypt_info *ci)
0356 {
0357
0358
0359
0360
0361
0362
0363
0364
0365 if (fscrypt_using_inline_encryption(ci))
0366 return smp_load_acquire(&prep_key->blk_key) != NULL;
0367 return smp_load_acquire(&prep_key->tfm) != NULL;
0368 }
0369
0370 #else
0371
0372 static inline int fscrypt_select_encryption_impl(struct fscrypt_info *ci)
0373 {
0374 return 0;
0375 }
0376
0377 static inline bool
0378 fscrypt_using_inline_encryption(const struct fscrypt_info *ci)
0379 {
0380 return false;
0381 }
0382
0383 static inline int
0384 fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
0385 const u8 *raw_key,
0386 const struct fscrypt_info *ci)
0387 {
0388 WARN_ON(1);
0389 return -EOPNOTSUPP;
0390 }
0391
0392 static inline void
0393 fscrypt_destroy_inline_crypt_key(struct fscrypt_prepared_key *prep_key)
0394 {
0395 }
0396
0397 static inline bool
0398 fscrypt_is_key_prepared(struct fscrypt_prepared_key *prep_key,
0399 const struct fscrypt_info *ci)
0400 {
0401 return smp_load_acquire(&prep_key->tfm) != NULL;
0402 }
0403 #endif
0404
0405
0406
0407
0408
0409
0410 struct fscrypt_master_key_secret {
0411
0412
0413
0414
0415
0416 struct fscrypt_hkdf hkdf;
0417
0418
0419
0420
0421
0422
0423 u32 size;
0424
0425
0426 u8 raw[FSCRYPT_MAX_KEY_SIZE];
0427
0428 } __randomize_layout;
0429
0430
0431
0432
0433
0434
0435
0436
0437 struct fscrypt_master_key {
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449 struct fscrypt_master_key_secret mk_secret;
0450
0451
0452
0453
0454
0455
0456
0457 struct fscrypt_key_specifier mk_spec;
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473 struct key *mk_users;
0474
0475
0476
0477
0478
0479
0480
0481
0482 refcount_t mk_refcount;
0483
0484
0485
0486
0487
0488 struct list_head mk_decrypted_inodes;
0489 spinlock_t mk_decrypted_inodes_lock;
0490
0491
0492
0493
0494
0495 struct fscrypt_prepared_key mk_direct_keys[FSCRYPT_MODE_MAX + 1];
0496 struct fscrypt_prepared_key mk_iv_ino_lblk_64_keys[FSCRYPT_MODE_MAX + 1];
0497 struct fscrypt_prepared_key mk_iv_ino_lblk_32_keys[FSCRYPT_MODE_MAX + 1];
0498
0499
0500 siphash_key_t mk_ino_hash_key;
0501 bool mk_ino_hash_key_initialized;
0502
0503 } __randomize_layout;
0504
0505 static inline bool
0506 is_master_key_secret_present(const struct fscrypt_master_key_secret *secret)
0507 {
0508
0509
0510
0511
0512
0513
0514
0515 return READ_ONCE(secret->size) != 0;
0516 }
0517
0518 static inline const char *master_key_spec_type(
0519 const struct fscrypt_key_specifier *spec)
0520 {
0521 switch (spec->type) {
0522 case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
0523 return "descriptor";
0524 case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
0525 return "identifier";
0526 }
0527 return "[unknown]";
0528 }
0529
0530 static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
0531 {
0532 switch (spec->type) {
0533 case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
0534 return FSCRYPT_KEY_DESCRIPTOR_SIZE;
0535 case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
0536 return FSCRYPT_KEY_IDENTIFIER_SIZE;
0537 }
0538 return 0;
0539 }
0540
0541 struct key *
0542 fscrypt_find_master_key(struct super_block *sb,
0543 const struct fscrypt_key_specifier *mk_spec);
0544
0545 int fscrypt_get_test_dummy_key_identifier(
0546 u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
0547
0548 int fscrypt_verify_key_added(struct super_block *sb,
0549 const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
0550
0551 int __init fscrypt_init_keyring(void);
0552
0553
0554
0555 struct fscrypt_mode {
0556 const char *friendly_name;
0557 const char *cipher_str;
0558 int keysize;
0559 int security_strength;
0560 int ivsize;
0561 int logged_cryptoapi_impl;
0562 int logged_blk_crypto_native;
0563 int logged_blk_crypto_fallback;
0564 enum blk_crypto_mode_num blk_crypto_mode;
0565 };
0566
0567 extern struct fscrypt_mode fscrypt_modes[];
0568
0569 int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,
0570 const u8 *raw_key, const struct fscrypt_info *ci);
0571
0572 void fscrypt_destroy_prepared_key(struct fscrypt_prepared_key *prep_key);
0573
0574 int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key);
0575
0576 int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
0577 const struct fscrypt_master_key *mk);
0578
0579 void fscrypt_hash_inode_number(struct fscrypt_info *ci,
0580 const struct fscrypt_master_key *mk);
0581
0582 int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported);
0583
0584
0585
0586
0587
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597 static inline int fscrypt_require_key(struct inode *inode)
0598 {
0599 if (IS_ENCRYPTED(inode)) {
0600 int err = fscrypt_get_encryption_info(inode, false);
0601
0602 if (err)
0603 return err;
0604 if (!fscrypt_has_encryption_key(inode))
0605 return -ENOKEY;
0606 }
0607 return 0;
0608 }
0609
0610
0611
0612 void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
0613
0614 int fscrypt_setup_v1_file_key(struct fscrypt_info *ci,
0615 const u8 *raw_master_key);
0616
0617 int fscrypt_setup_v1_file_key_via_subscribed_keyrings(struct fscrypt_info *ci);
0618
0619
0620
0621 bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
0622 const union fscrypt_policy *policy2);
0623 int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy,
0624 struct fscrypt_key_specifier *key_spec);
0625 bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
0626 const struct inode *inode);
0627 int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
0628 const union fscrypt_context *ctx_u,
0629 int ctx_size);
0630 const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir);
0631
0632 #endif