fscrypt_private.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * fscrypt_private.h
  4. *
  5. * Copyright (C) 2015, Google, Inc.
  6. *
  7. * This contains encryption key functions.
  8. *
  9. * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
  10. */
  11. #ifndef _FSCRYPT_PRIVATE_H
  12. #define _FSCRYPT_PRIVATE_H
  13. #define __FS_HAS_ENCRYPTION 1
  14. #include <linux/fscrypt.h>
  15. #include <crypto/hash.h>
  16. /* Encryption parameters */
  17. #define FS_IV_SIZE 16
  18. #define FS_KEY_DERIVATION_NONCE_SIZE 16
  19. /**
  20. * Encryption context for inode
  21. *
  22. * Protector format:
  23. * 1 byte: Protector format (1 = this version)
  24. * 1 byte: File contents encryption mode
  25. * 1 byte: File names encryption mode
  26. * 1 byte: Flags
  27. * 8 bytes: Master Key descriptor
  28. * 16 bytes: Encryption Key derivation nonce
  29. */
  30. struct fscrypt_context {
  31. u8 format;
  32. u8 contents_encryption_mode;
  33. u8 filenames_encryption_mode;
  34. u8 flags;
  35. u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
  36. u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
  37. } __packed;
  38. #define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1
  39. /**
  40. * For encrypted symlinks, the ciphertext length is stored at the beginning
  41. * of the string in little-endian format.
  42. */
  43. struct fscrypt_symlink_data {
  44. __le16 len;
  45. char encrypted_path[1];
  46. } __packed;
  47. /*
  48. * A pointer to this structure is stored in the file system's in-core
  49. * representation of an inode.
  50. */
  51. struct fscrypt_info {
  52. u8 ci_data_mode;
  53. u8 ci_filename_mode;
  54. u8 ci_flags;
  55. struct crypto_skcipher *ci_ctfm;
  56. struct crypto_cipher *ci_essiv_tfm;
  57. u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE];
  58. };
  59. typedef enum {
  60. FS_DECRYPT = 0,
  61. FS_ENCRYPT,
  62. } fscrypt_direction_t;
  63. #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
  64. #define FS_CTX_HAS_BOUNCE_BUFFER_FL 0x00000002
  65. static inline bool fscrypt_valid_enc_modes(u32 contents_mode,
  66. u32 filenames_mode)
  67. {
  68. if (contents_mode == FS_ENCRYPTION_MODE_AES_128_CBC &&
  69. filenames_mode == FS_ENCRYPTION_MODE_AES_128_CTS)
  70. return true;
  71. if (contents_mode == FS_ENCRYPTION_MODE_AES_256_XTS &&
  72. filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS)
  73. return true;
  74. return false;
  75. }
  76. /* crypto.c */
  77. extern struct kmem_cache *fscrypt_info_cachep;
  78. extern int fscrypt_initialize(unsigned int cop_flags);
  79. extern int fscrypt_do_page_crypto(const struct inode *inode,
  80. fscrypt_direction_t rw, u64 lblk_num,
  81. struct page *src_page,
  82. struct page *dest_page,
  83. unsigned int len, unsigned int offs,
  84. gfp_t gfp_flags);
  85. extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx,
  86. gfp_t gfp_flags);
  87. extern const struct dentry_operations fscrypt_d_ops;
  88. extern void __printf(3, 4) __cold
  89. fscrypt_msg(struct super_block *sb, const char *level, const char *fmt, ...);
  90. #define fscrypt_warn(sb, fmt, ...) \
  91. fscrypt_msg(sb, KERN_WARNING, fmt, ##__VA_ARGS__)
  92. #define fscrypt_err(sb, fmt, ...) \
  93. fscrypt_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__)
  94. /* fname.c */
  95. extern int fname_encrypt(struct inode *inode, const struct qstr *iname,
  96. u8 *out, unsigned int olen);
  97. extern bool fscrypt_fname_encrypted_size(const struct inode *inode,
  98. u32 orig_len, u32 max_len,
  99. u32 *encrypted_len_ret);
  100. /* keyinfo.c */
  101. extern void __exit fscrypt_essiv_cleanup(void);
  102. #endif /* _FSCRYPT_PRIVATE_H */