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