fscrypt_private.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * fscrypt_private.h
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. *
  6. * This contains encryption key functions.
  7. *
  8. * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
  9. */
  10. #ifndef _FSCRYPT_PRIVATE_H
  11. #define _FSCRYPT_PRIVATE_H
  12. #include <linux/fscrypt.h>
  13. #include <crypto/hash.h>
  14. #include <linux/pfk.h>
  15. /* Encryption parameters */
  16. #define FS_AES_256_XTS_KEY_SIZE 64
  17. #define FS_KEY_DERIVATION_NONCE_SIZE 16
  18. /**
  19. * Encryption context for inode
  20. *
  21. * Protector format:
  22. * 1 byte: Protector format (1 = this version)
  23. * 1 byte: File contents encryption mode
  24. * 1 byte: File names encryption mode
  25. * 1 byte: Flags
  26. * 8 bytes: Master Key descriptor
  27. * 16 bytes: Encryption Key derivation nonce
  28. */
  29. struct fscrypt_context {
  30. u8 format;
  31. u8 contents_encryption_mode;
  32. u8 filenames_encryption_mode;
  33. u8 flags;
  34. u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
  35. u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
  36. } __packed;
  37. #define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1
  38. /**
  39. * For encrypted symlinks, the ciphertext length is stored at the beginning
  40. * of the string in little-endian format.
  41. */
  42. struct fscrypt_symlink_data {
  43. __le16 len;
  44. char encrypted_path[1];
  45. } __packed;
  46. /*
  47. * fscrypt_info - the "encryption key" for an inode
  48. *
  49. * When an encrypted file's key is made available, an instance of this struct is
  50. * allocated and stored in ->i_crypt_info. Once created, it remains until the
  51. * inode is evicted.
  52. */
  53. struct fscrypt_info {
  54. /* The actual crypto transform used for encryption and decryption */
  55. struct crypto_skcipher *ci_ctfm;
  56. /*
  57. * Cipher for ESSIV IV generation. Only set for CBC contents
  58. * encryption, otherwise is NULL.
  59. */
  60. struct crypto_cipher *ci_essiv_tfm;
  61. /*
  62. * Encryption mode used for this inode. It corresponds to either
  63. * ci_data_mode or ci_filename_mode, depending on the inode type.
  64. */
  65. struct fscrypt_mode *ci_mode;
  66. /*
  67. * If non-NULL, then this inode uses a master key directly rather than a
  68. * derived key, and ci_ctfm will equal ci_master_key->mk_ctfm.
  69. * Otherwise, this inode uses a derived key.
  70. */
  71. struct fscrypt_master_key *ci_master_key;
  72. /* fields from the fscrypt_context */
  73. u8 ci_data_mode;
  74. u8 ci_filename_mode;
  75. u8 ci_flags;
  76. u8 ci_master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
  77. u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE];
  78. /* Raw key, only for inline encryption w/ FS_ENCRYPTION_MODE_PRIVATE */
  79. u8 ci_raw_key[FS_MAX_KEY_SIZE];
  80. };
  81. typedef enum {
  82. FS_DECRYPT = 0,
  83. FS_ENCRYPT,
  84. } fscrypt_direction_t;
  85. #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
  86. #define FS_CTX_HAS_BOUNCE_BUFFER_FL 0x00000002
  87. static inline bool fscrypt_valid_enc_modes(u32 contents_mode,
  88. u32 filenames_mode)
  89. {
  90. if (contents_mode == FS_ENCRYPTION_MODE_AES_128_CBC &&
  91. filenames_mode == FS_ENCRYPTION_MODE_AES_128_CTS)
  92. return true;
  93. if (contents_mode == FS_ENCRYPTION_MODE_AES_256_XTS &&
  94. filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS)
  95. return true;
  96. if (contents_mode == FS_ENCRYPTION_MODE_ADIANTUM &&
  97. filenames_mode == FS_ENCRYPTION_MODE_ADIANTUM)
  98. return true;
  99. if (contents_mode == FS_ENCRYPTION_MODE_PRIVATE &&
  100. filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS)
  101. return true;
  102. return false;
  103. }
  104. /* crypto.c */
  105. extern struct kmem_cache *fscrypt_info_cachep;
  106. extern int fscrypt_initialize(unsigned int cop_flags);
  107. extern int fscrypt_do_page_crypto(const struct inode *inode,
  108. fscrypt_direction_t rw, u64 lblk_num,
  109. struct page *src_page,
  110. struct page *dest_page,
  111. unsigned int len, unsigned int offs,
  112. gfp_t gfp_flags);
  113. extern struct page *fscrypt_alloc_bounce_page(struct fscrypt_ctx *ctx,
  114. gfp_t gfp_flags);
  115. extern const struct dentry_operations fscrypt_d_ops;
  116. extern void __printf(3, 4) __cold
  117. fscrypt_msg(struct super_block *sb, const char *level, const char *fmt, ...);
  118. #define fscrypt_warn(sb, fmt, ...) \
  119. fscrypt_msg(sb, KERN_WARNING, fmt, ##__VA_ARGS__)
  120. #define fscrypt_err(sb, fmt, ...) \
  121. fscrypt_msg(sb, KERN_ERR, fmt, ##__VA_ARGS__)
  122. #define FSCRYPT_MAX_IV_SIZE 32
  123. union fscrypt_iv {
  124. struct {
  125. /* logical block number within the file */
  126. __le64 lblk_num;
  127. /* per-file nonce; only set in DIRECT_KEY mode */
  128. u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
  129. };
  130. u8 raw[FSCRYPT_MAX_IV_SIZE];
  131. };
  132. void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
  133. const struct fscrypt_info *ci);
  134. /* fname.c */
  135. extern int fname_encrypt(struct inode *inode, const struct qstr *iname,
  136. u8 *out, unsigned int olen);
  137. extern bool fscrypt_fname_encrypted_size(const struct inode *inode,
  138. u32 orig_len, u32 max_len,
  139. u32 *encrypted_len_ret);
  140. /* keyinfo.c */
  141. struct fscrypt_mode {
  142. const char *friendly_name;
  143. const char *cipher_str;
  144. int keysize;
  145. int ivsize;
  146. bool logged_impl_name;
  147. bool needs_essiv;
  148. };
  149. static inline bool is_private_mode(const struct fscrypt_mode *mode)
  150. {
  151. /* Using inline encryption with ICE, rather than the crypto API? */
  152. return mode->cipher_str == NULL;
  153. }
  154. extern void __exit fscrypt_essiv_cleanup(void);
  155. #endif /* _FSCRYPT_PRIVATE_H */