crypto.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Ceph fscrypt functionality
  4. */
  5. #ifndef _CEPH_CRYPTO_H
  6. #define _CEPH_CRYPTO_H
  7. #include <crypto/sha2.h>
  8. #include <linux/fscrypt.h>
  9. #define CEPH_FSCRYPT_BLOCK_SHIFT 12
  10. #define CEPH_FSCRYPT_BLOCK_SIZE (_AC(1, UL) << CEPH_FSCRYPT_BLOCK_SHIFT)
  11. #define CEPH_FSCRYPT_BLOCK_MASK (~(CEPH_FSCRYPT_BLOCK_SIZE-1))
  12. struct ceph_fs_client;
  13. struct ceph_acl_sec_ctx;
  14. struct ceph_mds_request;
  15. struct ceph_fname {
  16. struct inode *dir;
  17. char *name; // b64 encoded, possibly hashed
  18. unsigned char *ctext; // binary crypttext (if any)
  19. u32 name_len; // length of name buffer
  20. u32 ctext_len; // length of crypttext
  21. bool no_copy;
  22. };
  23. /*
  24. * Header for the crypted file when truncating the size, this
  25. * will be sent to MDS, and the MDS will update the encrypted
  26. * last block and then truncate the size.
  27. */
  28. struct ceph_fscrypt_truncate_size_header {
  29. __u8 ver;
  30. __u8 compat;
  31. /*
  32. * It will be sizeof(assert_ver + file_offset + block_size)
  33. * if the last block is empty when it's located in a file
  34. * hole. Or the data_len will plus CEPH_FSCRYPT_BLOCK_SIZE.
  35. */
  36. __le32 data_len;
  37. __le64 change_attr;
  38. __le64 file_offset;
  39. __le32 block_size;
  40. } __packed;
  41. struct ceph_fscrypt_auth {
  42. __le32 cfa_version;
  43. __le32 cfa_blob_len;
  44. u8 cfa_blob[FSCRYPT_SET_CONTEXT_MAX_SIZE];
  45. } __packed;
  46. #define CEPH_FSCRYPT_AUTH_VERSION 1
  47. static inline u32 ceph_fscrypt_auth_len(struct ceph_fscrypt_auth *fa)
  48. {
  49. u32 ctxsize = le32_to_cpu(fa->cfa_blob_len);
  50. return offsetof(struct ceph_fscrypt_auth, cfa_blob) + ctxsize;
  51. }
  52. #ifdef CONFIG_FS_ENCRYPTION
  53. /*
  54. * We want to encrypt filenames when creating them, but the encrypted
  55. * versions of those names may have illegal characters in them. To mitigate
  56. * that, we base64 encode them, but that gives us a result that can exceed
  57. * NAME_MAX.
  58. *
  59. * Follow a similar scheme to fscrypt itself, and cap the filename to a
  60. * smaller size. If the ciphertext name is longer than the value below, then
  61. * sha256 hash the remaining bytes.
  62. *
  63. * For the fscrypt_nokey_name struct the dirhash[2] member is useless in ceph
  64. * so the corresponding struct will be:
  65. *
  66. * struct fscrypt_ceph_nokey_name {
  67. * u8 bytes[157];
  68. * u8 sha256[SHA256_DIGEST_SIZE];
  69. * }; // 180 bytes => 240 bytes base64-encoded, which is <= NAME_MAX (255)
  70. *
  71. * (240 bytes is the maximum size allowed for snapshot names to take into
  72. * account the format: '_<SNAPSHOT-NAME>_<INODE-NUMBER>'.)
  73. *
  74. * Note that for long names that end up having their tail portion hashed, we
  75. * must also store the full encrypted name (in the dentry's alternate_name
  76. * field).
  77. */
  78. #define CEPH_NOHASH_NAME_MAX (180 - SHA256_DIGEST_SIZE)
  79. #define CEPH_BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3)
  80. int ceph_base64_encode(const u8 *src, int srclen, char *dst);
  81. int ceph_base64_decode(const char *src, int srclen, u8 *dst);
  82. void ceph_fscrypt_set_ops(struct super_block *sb);
  83. void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc);
  84. int ceph_fscrypt_prepare_context(struct inode *dir, struct inode *inode,
  85. struct ceph_acl_sec_ctx *as);
  86. void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req,
  87. struct ceph_acl_sec_ctx *as);
  88. int ceph_encode_encrypted_dname(struct inode *parent, struct qstr *d_name,
  89. char *buf);
  90. int ceph_encode_encrypted_fname(struct inode *parent, struct dentry *dentry,
  91. char *buf);
  92. static inline int ceph_fname_alloc_buffer(struct inode *parent,
  93. struct fscrypt_str *fname)
  94. {
  95. if (!IS_ENCRYPTED(parent))
  96. return 0;
  97. return fscrypt_fname_alloc_buffer(NAME_MAX, fname);
  98. }
  99. static inline void ceph_fname_free_buffer(struct inode *parent,
  100. struct fscrypt_str *fname)
  101. {
  102. if (IS_ENCRYPTED(parent))
  103. fscrypt_fname_free_buffer(fname);
  104. }
  105. int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname,
  106. struct fscrypt_str *oname, bool *is_nokey);
  107. int ceph_fscrypt_prepare_readdir(struct inode *dir);
  108. static inline unsigned int ceph_fscrypt_blocks(u64 off, u64 len)
  109. {
  110. /* crypto blocks cannot span more than one page */
  111. BUILD_BUG_ON(CEPH_FSCRYPT_BLOCK_SHIFT > PAGE_SHIFT);
  112. return ((off+len+CEPH_FSCRYPT_BLOCK_SIZE-1) >> CEPH_FSCRYPT_BLOCK_SHIFT) -
  113. (off >> CEPH_FSCRYPT_BLOCK_SHIFT);
  114. }
  115. /*
  116. * If we have an encrypted inode then we must adjust the offset and
  117. * range of the on-the-wire read to cover an entire encryption block.
  118. * The copy will be done using the original offset and length, after
  119. * we've decrypted the result.
  120. */
  121. static inline void ceph_fscrypt_adjust_off_and_len(struct inode *inode,
  122. u64 *off, u64 *len)
  123. {
  124. if (IS_ENCRYPTED(inode)) {
  125. *len = ceph_fscrypt_blocks(*off, *len) * CEPH_FSCRYPT_BLOCK_SIZE;
  126. *off &= CEPH_FSCRYPT_BLOCK_MASK;
  127. }
  128. }
  129. int ceph_fscrypt_decrypt_block_inplace(const struct inode *inode,
  130. struct page *page, unsigned int len,
  131. unsigned int offs, u64 lblk_num);
  132. int ceph_fscrypt_encrypt_block_inplace(const struct inode *inode,
  133. struct page *page, unsigned int len,
  134. unsigned int offs, u64 lblk_num,
  135. gfp_t gfp_flags);
  136. int ceph_fscrypt_decrypt_pages(struct inode *inode, struct page **page,
  137. u64 off, int len);
  138. int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page,
  139. u64 off, struct ceph_sparse_extent *map,
  140. u32 ext_cnt);
  141. int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off,
  142. int len, gfp_t gfp);
  143. static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
  144. {
  145. return fscrypt_is_bounce_page(page) ? fscrypt_pagecache_page(page) : page;
  146. }
  147. #else /* CONFIG_FS_ENCRYPTION */
  148. static inline void ceph_fscrypt_set_ops(struct super_block *sb)
  149. {
  150. }
  151. static inline void ceph_fscrypt_free_dummy_policy(struct ceph_fs_client *fsc)
  152. {
  153. }
  154. static inline int ceph_fscrypt_prepare_context(struct inode *dir,
  155. struct inode *inode,
  156. struct ceph_acl_sec_ctx *as)
  157. {
  158. if (IS_ENCRYPTED(dir))
  159. return -EOPNOTSUPP;
  160. return 0;
  161. }
  162. static inline void ceph_fscrypt_as_ctx_to_req(struct ceph_mds_request *req,
  163. struct ceph_acl_sec_ctx *as_ctx)
  164. {
  165. }
  166. static inline int ceph_encode_encrypted_dname(struct inode *parent,
  167. struct qstr *d_name, char *buf)
  168. {
  169. memcpy(buf, d_name->name, d_name->len);
  170. return d_name->len;
  171. }
  172. static inline int ceph_encode_encrypted_fname(struct inode *parent,
  173. struct dentry *dentry, char *buf)
  174. {
  175. return -EOPNOTSUPP;
  176. }
  177. static inline int ceph_fname_alloc_buffer(struct inode *parent,
  178. struct fscrypt_str *fname)
  179. {
  180. return 0;
  181. }
  182. static inline void ceph_fname_free_buffer(struct inode *parent,
  183. struct fscrypt_str *fname)
  184. {
  185. }
  186. static inline int ceph_fname_to_usr(const struct ceph_fname *fname,
  187. struct fscrypt_str *tname,
  188. struct fscrypt_str *oname, bool *is_nokey)
  189. {
  190. oname->name = fname->name;
  191. oname->len = fname->name_len;
  192. return 0;
  193. }
  194. static inline int ceph_fscrypt_prepare_readdir(struct inode *dir)
  195. {
  196. return 0;
  197. }
  198. static inline void ceph_fscrypt_adjust_off_and_len(struct inode *inode,
  199. u64 *off, u64 *len)
  200. {
  201. }
  202. static inline int ceph_fscrypt_decrypt_block_inplace(const struct inode *inode,
  203. struct page *page, unsigned int len,
  204. unsigned int offs, u64 lblk_num)
  205. {
  206. return 0;
  207. }
  208. static inline int ceph_fscrypt_encrypt_block_inplace(const struct inode *inode,
  209. struct page *page, unsigned int len,
  210. unsigned int offs, u64 lblk_num,
  211. gfp_t gfp_flags)
  212. {
  213. return 0;
  214. }
  215. static inline int ceph_fscrypt_decrypt_pages(struct inode *inode,
  216. struct page **page, u64 off,
  217. int len)
  218. {
  219. return 0;
  220. }
  221. static inline int ceph_fscrypt_decrypt_extents(struct inode *inode,
  222. struct page **page, u64 off,
  223. struct ceph_sparse_extent *map,
  224. u32 ext_cnt)
  225. {
  226. return 0;
  227. }
  228. static inline int ceph_fscrypt_encrypt_pages(struct inode *inode,
  229. struct page **page, u64 off,
  230. int len, gfp_t gfp)
  231. {
  232. return 0;
  233. }
  234. static inline struct page *ceph_fscrypt_pagecache_page(struct page *page)
  235. {
  236. return page;
  237. }
  238. #endif /* CONFIG_FS_ENCRYPTION */
  239. static inline loff_t ceph_fscrypt_page_offset(struct page *page)
  240. {
  241. return page_offset(ceph_fscrypt_pagecache_page(page));
  242. }
  243. #endif /* _CEPH_CRYPTO_H */