fscrypto.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * General per-file encryption definition
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. *
  6. * Written by Michael Halcrow, 2015.
  7. * Modified by Jaegeuk Kim, 2015.
  8. */
  9. #ifndef _LINUX_FSCRYPTO_H
  10. #define _LINUX_FSCRYPTO_H
  11. #include <linux/key.h>
  12. #include <linux/fs.h>
  13. #include <linux/mm.h>
  14. #include <linux/bio.h>
  15. #include <linux/dcache.h>
  16. #include <crypto/skcipher.h>
  17. #include <uapi/linux/fs.h>
  18. #define FS_KEY_DERIVATION_NONCE_SIZE 16
  19. #define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1
  20. #define FS_POLICY_FLAGS_PAD_4 0x00
  21. #define FS_POLICY_FLAGS_PAD_8 0x01
  22. #define FS_POLICY_FLAGS_PAD_16 0x02
  23. #define FS_POLICY_FLAGS_PAD_32 0x03
  24. #define FS_POLICY_FLAGS_PAD_MASK 0x03
  25. #define FS_POLICY_FLAGS_VALID 0x03
  26. /* Encryption algorithms */
  27. #define FS_ENCRYPTION_MODE_INVALID 0
  28. #define FS_ENCRYPTION_MODE_AES_256_XTS 1
  29. #define FS_ENCRYPTION_MODE_AES_256_GCM 2
  30. #define FS_ENCRYPTION_MODE_AES_256_CBC 3
  31. #define FS_ENCRYPTION_MODE_AES_256_CTS 4
  32. /**
  33. * Encryption context for inode
  34. *
  35. * Protector format:
  36. * 1 byte: Protector format (1 = this version)
  37. * 1 byte: File contents encryption mode
  38. * 1 byte: File names encryption mode
  39. * 1 byte: Flags
  40. * 8 bytes: Master Key descriptor
  41. * 16 bytes: Encryption Key derivation nonce
  42. */
  43. struct fscrypt_context {
  44. u8 format;
  45. u8 contents_encryption_mode;
  46. u8 filenames_encryption_mode;
  47. u8 flags;
  48. u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
  49. u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
  50. } __packed;
  51. /* Encryption parameters */
  52. #define FS_XTS_TWEAK_SIZE 16
  53. #define FS_AES_128_ECB_KEY_SIZE 16
  54. #define FS_AES_256_GCM_KEY_SIZE 32
  55. #define FS_AES_256_CBC_KEY_SIZE 32
  56. #define FS_AES_256_CTS_KEY_SIZE 32
  57. #define FS_AES_256_XTS_KEY_SIZE 64
  58. #define FS_MAX_KEY_SIZE 64
  59. #define FS_KEY_DESC_PREFIX "fscrypt:"
  60. #define FS_KEY_DESC_PREFIX_SIZE 8
  61. /* This is passed in from userspace into the kernel keyring */
  62. struct fscrypt_key {
  63. u32 mode;
  64. u8 raw[FS_MAX_KEY_SIZE];
  65. u32 size;
  66. } __packed;
  67. struct fscrypt_info {
  68. u8 ci_data_mode;
  69. u8 ci_filename_mode;
  70. u8 ci_flags;
  71. struct crypto_skcipher *ci_ctfm;
  72. u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE];
  73. };
  74. #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
  75. #define FS_WRITE_PATH_FL 0x00000002
  76. struct fscrypt_ctx {
  77. union {
  78. struct {
  79. struct page *bounce_page; /* Ciphertext page */
  80. struct page *control_page; /* Original page */
  81. } w;
  82. struct {
  83. struct bio *bio;
  84. struct work_struct work;
  85. } r;
  86. struct list_head free_list; /* Free list */
  87. };
  88. u8 flags; /* Flags */
  89. u8 mode; /* Encryption mode for tfm */
  90. };
  91. struct fscrypt_completion_result {
  92. struct completion completion;
  93. int res;
  94. };
  95. #define DECLARE_FS_COMPLETION_RESULT(ecr) \
  96. struct fscrypt_completion_result ecr = { \
  97. COMPLETION_INITIALIZER((ecr).completion), 0 }
  98. #define FS_FNAME_NUM_SCATTER_ENTRIES 4
  99. #define FS_CRYPTO_BLOCK_SIZE 16
  100. #define FS_FNAME_CRYPTO_DIGEST_SIZE 32
  101. /**
  102. * For encrypted symlinks, the ciphertext length is stored at the beginning
  103. * of the string in little-endian format.
  104. */
  105. struct fscrypt_symlink_data {
  106. __le16 len;
  107. char encrypted_path[1];
  108. } __packed;
  109. /**
  110. * This function is used to calculate the disk space required to
  111. * store a filename of length l in encrypted symlink format.
  112. */
  113. static inline u32 fscrypt_symlink_data_len(u32 l)
  114. {
  115. if (l < FS_CRYPTO_BLOCK_SIZE)
  116. l = FS_CRYPTO_BLOCK_SIZE;
  117. return (l + sizeof(struct fscrypt_symlink_data) - 1);
  118. }
  119. struct fscrypt_str {
  120. unsigned char *name;
  121. u32 len;
  122. };
  123. struct fscrypt_name {
  124. const struct qstr *usr_fname;
  125. struct fscrypt_str disk_name;
  126. u32 hash;
  127. u32 minor_hash;
  128. struct fscrypt_str crypto_buf;
  129. };
  130. #define FSTR_INIT(n, l) { .name = n, .len = l }
  131. #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
  132. #define fname_name(p) ((p)->disk_name.name)
  133. #define fname_len(p) ((p)->disk_name.len)
  134. /*
  135. * crypto opertions for filesystems
  136. */
  137. struct fscrypt_operations {
  138. int (*get_context)(struct inode *, void *, size_t);
  139. int (*key_prefix)(struct inode *, u8 **);
  140. int (*prepare_context)(struct inode *);
  141. int (*set_context)(struct inode *, const void *, size_t, void *);
  142. int (*dummy_context)(struct inode *);
  143. bool (*is_encrypted)(struct inode *);
  144. bool (*empty_dir)(struct inode *);
  145. unsigned (*max_namelen)(struct inode *);
  146. };
  147. static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
  148. {
  149. if (inode->i_sb->s_cop->dummy_context &&
  150. inode->i_sb->s_cop->dummy_context(inode))
  151. return true;
  152. return false;
  153. }
  154. static inline bool fscrypt_valid_contents_enc_mode(u32 mode)
  155. {
  156. return (mode == FS_ENCRYPTION_MODE_AES_256_XTS);
  157. }
  158. static inline bool fscrypt_valid_filenames_enc_mode(u32 mode)
  159. {
  160. return (mode == FS_ENCRYPTION_MODE_AES_256_CTS);
  161. }
  162. static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
  163. {
  164. if (str->len == 1 && str->name[0] == '.')
  165. return true;
  166. if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
  167. return true;
  168. return false;
  169. }
  170. static inline struct page *fscrypt_control_page(struct page *page)
  171. {
  172. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  173. return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
  174. #else
  175. WARN_ON_ONCE(1);
  176. return ERR_PTR(-EINVAL);
  177. #endif
  178. }
  179. static inline int fscrypt_has_encryption_key(struct inode *inode)
  180. {
  181. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  182. return (inode->i_crypt_info != NULL);
  183. #else
  184. return 0;
  185. #endif
  186. }
  187. static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)
  188. {
  189. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  190. spin_lock(&dentry->d_lock);
  191. dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
  192. spin_unlock(&dentry->d_lock);
  193. #endif
  194. }
  195. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  196. extern const struct dentry_operations fscrypt_d_ops;
  197. #endif
  198. static inline void fscrypt_set_d_op(struct dentry *dentry)
  199. {
  200. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  201. d_set_d_op(dentry, &fscrypt_d_ops);
  202. #endif
  203. }
  204. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  205. /* crypto.c */
  206. extern struct kmem_cache *fscrypt_info_cachep;
  207. int fscrypt_initialize(void);
  208. extern struct fscrypt_ctx *fscrypt_get_ctx(struct inode *, gfp_t);
  209. extern void fscrypt_release_ctx(struct fscrypt_ctx *);
  210. extern struct page *fscrypt_encrypt_page(struct inode *, struct page *, gfp_t);
  211. extern int fscrypt_decrypt_page(struct page *);
  212. extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *);
  213. extern void fscrypt_pullback_bio_page(struct page **, bool);
  214. extern void fscrypt_restore_control_page(struct page *);
  215. extern int fscrypt_zeroout_range(struct inode *, pgoff_t, sector_t,
  216. unsigned int);
  217. /* policy.c */
  218. extern int fscrypt_process_policy(struct file *, const struct fscrypt_policy *);
  219. extern int fscrypt_get_policy(struct inode *, struct fscrypt_policy *);
  220. extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
  221. extern int fscrypt_inherit_context(struct inode *, struct inode *,
  222. void *, bool);
  223. /* keyinfo.c */
  224. extern int fscrypt_get_encryption_info(struct inode *);
  225. extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *);
  226. /* fname.c */
  227. extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
  228. int lookup, struct fscrypt_name *);
  229. extern void fscrypt_free_filename(struct fscrypt_name *);
  230. extern u32 fscrypt_fname_encrypted_size(struct inode *, u32);
  231. extern int fscrypt_fname_alloc_buffer(struct inode *, u32,
  232. struct fscrypt_str *);
  233. extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
  234. extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
  235. const struct fscrypt_str *, struct fscrypt_str *);
  236. extern int fscrypt_fname_usr_to_disk(struct inode *, const struct qstr *,
  237. struct fscrypt_str *);
  238. #endif
  239. /* crypto.c */
  240. static inline struct fscrypt_ctx *fscrypt_notsupp_get_ctx(struct inode *i,
  241. gfp_t f)
  242. {
  243. return ERR_PTR(-EOPNOTSUPP);
  244. }
  245. static inline void fscrypt_notsupp_release_ctx(struct fscrypt_ctx *c)
  246. {
  247. return;
  248. }
  249. static inline struct page *fscrypt_notsupp_encrypt_page(struct inode *i,
  250. struct page *p, gfp_t f)
  251. {
  252. return ERR_PTR(-EOPNOTSUPP);
  253. }
  254. static inline int fscrypt_notsupp_decrypt_page(struct page *p)
  255. {
  256. return -EOPNOTSUPP;
  257. }
  258. static inline void fscrypt_notsupp_decrypt_bio_pages(struct fscrypt_ctx *c,
  259. struct bio *b)
  260. {
  261. return;
  262. }
  263. static inline void fscrypt_notsupp_pullback_bio_page(struct page **p, bool b)
  264. {
  265. return;
  266. }
  267. static inline void fscrypt_notsupp_restore_control_page(struct page *p)
  268. {
  269. return;
  270. }
  271. static inline int fscrypt_notsupp_zeroout_range(struct inode *i, pgoff_t p,
  272. sector_t s, unsigned int f)
  273. {
  274. return -EOPNOTSUPP;
  275. }
  276. /* policy.c */
  277. static inline int fscrypt_notsupp_process_policy(struct file *f,
  278. const struct fscrypt_policy *p)
  279. {
  280. return -EOPNOTSUPP;
  281. }
  282. static inline int fscrypt_notsupp_get_policy(struct inode *i,
  283. struct fscrypt_policy *p)
  284. {
  285. return -EOPNOTSUPP;
  286. }
  287. static inline int fscrypt_notsupp_has_permitted_context(struct inode *p,
  288. struct inode *i)
  289. {
  290. return 0;
  291. }
  292. static inline int fscrypt_notsupp_inherit_context(struct inode *p,
  293. struct inode *i, void *v, bool b)
  294. {
  295. return -EOPNOTSUPP;
  296. }
  297. /* keyinfo.c */
  298. static inline int fscrypt_notsupp_get_encryption_info(struct inode *i)
  299. {
  300. return -EOPNOTSUPP;
  301. }
  302. static inline void fscrypt_notsupp_put_encryption_info(struct inode *i,
  303. struct fscrypt_info *f)
  304. {
  305. return;
  306. }
  307. /* fname.c */
  308. static inline int fscrypt_notsupp_setup_filename(struct inode *dir,
  309. const struct qstr *iname,
  310. int lookup, struct fscrypt_name *fname)
  311. {
  312. if (dir->i_sb->s_cop->is_encrypted(dir))
  313. return -EOPNOTSUPP;
  314. memset(fname, 0, sizeof(struct fscrypt_name));
  315. fname->usr_fname = iname;
  316. fname->disk_name.name = (unsigned char *)iname->name;
  317. fname->disk_name.len = iname->len;
  318. return 0;
  319. }
  320. static inline void fscrypt_notsupp_free_filename(struct fscrypt_name *fname)
  321. {
  322. return;
  323. }
  324. static inline u32 fscrypt_notsupp_fname_encrypted_size(struct inode *i, u32 s)
  325. {
  326. /* never happens */
  327. WARN_ON(1);
  328. return 0;
  329. }
  330. static inline int fscrypt_notsupp_fname_alloc_buffer(struct inode *inode,
  331. u32 ilen, struct fscrypt_str *crypto_str)
  332. {
  333. return -EOPNOTSUPP;
  334. }
  335. static inline void fscrypt_notsupp_fname_free_buffer(struct fscrypt_str *c)
  336. {
  337. return;
  338. }
  339. static inline int fscrypt_notsupp_fname_disk_to_usr(struct inode *inode,
  340. u32 hash, u32 minor_hash,
  341. const struct fscrypt_str *iname,
  342. struct fscrypt_str *oname)
  343. {
  344. return -EOPNOTSUPP;
  345. }
  346. static inline int fscrypt_notsupp_fname_usr_to_disk(struct inode *inode,
  347. const struct qstr *iname,
  348. struct fscrypt_str *oname)
  349. {
  350. return -EOPNOTSUPP;
  351. }
  352. #endif /* _LINUX_FSCRYPTO_H */