Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/f2fs/crypto.c |
| 3 | * |
| 4 | * Copied from linux/fs/ext4/crypto.c |
| 5 | * |
| 6 | * Copyright (C) 2015, Google, Inc. |
| 7 | * Copyright (C) 2015, Motorola Mobility |
| 8 | * |
| 9 | * This contains encryption functions for f2fs |
| 10 | * |
| 11 | * Written by Michael Halcrow, 2014. |
| 12 | * |
| 13 | * Filename encryption additions |
| 14 | * Uday Savagaonkar, 2014 |
| 15 | * Encryption policy handling additions |
| 16 | * Ildar Muslukhov, 2014 |
| 17 | * Remove ext4_encrypted_zeroout(), |
| 18 | * add f2fs_restore_and_release_control_page() |
| 19 | * Jaegeuk Kim, 2015. |
| 20 | * |
| 21 | * This has not yet undergone a rigorous security audit. |
| 22 | * |
| 23 | * The usage of AES-XTS should conform to recommendations in NIST |
| 24 | * Special Publication 800-38E and IEEE P1619/D16. |
| 25 | */ |
| 26 | #include <crypto/hash.h> |
| 27 | #include <crypto/sha.h> |
| 28 | #include <keys/user-type.h> |
| 29 | #include <keys/encrypted-type.h> |
| 30 | #include <linux/crypto.h> |
| 31 | #include <linux/ecryptfs.h> |
| 32 | #include <linux/gfp.h> |
| 33 | #include <linux/kernel.h> |
| 34 | #include <linux/key.h> |
| 35 | #include <linux/list.h> |
| 36 | #include <linux/mempool.h> |
| 37 | #include <linux/module.h> |
| 38 | #include <linux/mutex.h> |
| 39 | #include <linux/random.h> |
| 40 | #include <linux/scatterlist.h> |
| 41 | #include <linux/spinlock_types.h> |
| 42 | #include <linux/f2fs_fs.h> |
| 43 | #include <linux/ratelimit.h> |
| 44 | #include <linux/bio.h> |
| 45 | |
| 46 | #include "f2fs.h" |
| 47 | #include "xattr.h" |
| 48 | |
| 49 | /* Encryption added and removed here! (L: */ |
| 50 | |
| 51 | static unsigned int num_prealloc_crypto_pages = 32; |
| 52 | static unsigned int num_prealloc_crypto_ctxs = 128; |
| 53 | |
| 54 | module_param(num_prealloc_crypto_pages, uint, 0444); |
| 55 | MODULE_PARM_DESC(num_prealloc_crypto_pages, |
| 56 | "Number of crypto pages to preallocate"); |
| 57 | module_param(num_prealloc_crypto_ctxs, uint, 0444); |
| 58 | MODULE_PARM_DESC(num_prealloc_crypto_ctxs, |
| 59 | "Number of crypto contexts to preallocate"); |
| 60 | |
| 61 | static mempool_t *f2fs_bounce_page_pool; |
| 62 | |
| 63 | static LIST_HEAD(f2fs_free_crypto_ctxs); |
| 64 | static DEFINE_SPINLOCK(f2fs_crypto_ctx_lock); |
| 65 | |
Jaegeuk Kim | cfc4d97 | 2015-05-15 15:37:24 -0700 | [diff] [blame^] | 66 | static struct workqueue_struct *f2fs_read_workqueue; |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 67 | static DEFINE_MUTEX(crypto_init); |
| 68 | |
Jaegeuk Kim | 8bacf6de | 2015-05-12 13:26:54 -0700 | [diff] [blame] | 69 | static struct kmem_cache *f2fs_crypto_ctx_cachep; |
| 70 | struct kmem_cache *f2fs_crypt_info_cachep; |
| 71 | |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 72 | /** |
| 73 | * f2fs_release_crypto_ctx() - Releases an encryption context |
| 74 | * @ctx: The encryption context to release. |
| 75 | * |
| 76 | * If the encryption context was allocated from the pre-allocated pool, returns |
| 77 | * it to that pool. Else, frees it. |
| 78 | * |
| 79 | * If there's a bounce page in the context, this frees that. |
| 80 | */ |
| 81 | void f2fs_release_crypto_ctx(struct f2fs_crypto_ctx *ctx) |
| 82 | { |
| 83 | unsigned long flags; |
| 84 | |
Jaegeuk Kim | ca40b03 | 2015-05-12 13:40:20 -0700 | [diff] [blame] | 85 | if (ctx->flags & F2FS_WRITE_PATH_FL && ctx->w.bounce_page) { |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 86 | if (ctx->flags & F2FS_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL) |
Jaegeuk Kim | ca40b03 | 2015-05-12 13:40:20 -0700 | [diff] [blame] | 87 | __free_page(ctx->w.bounce_page); |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 88 | else |
Jaegeuk Kim | ca40b03 | 2015-05-12 13:40:20 -0700 | [diff] [blame] | 89 | mempool_free(ctx->w.bounce_page, f2fs_bounce_page_pool); |
| 90 | ctx->w.bounce_page = NULL; |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 91 | } |
Jaegeuk Kim | ca40b03 | 2015-05-12 13:40:20 -0700 | [diff] [blame] | 92 | ctx->w.control_page = NULL; |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 93 | if (ctx->flags & F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL) { |
| 94 | if (ctx->tfm) |
| 95 | crypto_free_tfm(ctx->tfm); |
Jaegeuk Kim | 8bacf6de | 2015-05-12 13:26:54 -0700 | [diff] [blame] | 96 | kmem_cache_free(f2fs_crypto_ctx_cachep, ctx); |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 97 | } else { |
| 98 | spin_lock_irqsave(&f2fs_crypto_ctx_lock, flags); |
| 99 | list_add(&ctx->free_list, &f2fs_free_crypto_ctxs); |
| 100 | spin_unlock_irqrestore(&f2fs_crypto_ctx_lock, flags); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 105 | * f2fs_get_crypto_ctx() - Gets an encryption context |
| 106 | * @inode: The inode for which we are doing the crypto |
| 107 | * |
| 108 | * Allocates and initializes an encryption context. |
| 109 | * |
| 110 | * Return: An allocated and initialized encryption context on success; error |
| 111 | * value or NULL otherwise. |
| 112 | */ |
| 113 | struct f2fs_crypto_ctx *f2fs_get_crypto_ctx(struct inode *inode) |
| 114 | { |
| 115 | struct f2fs_crypto_ctx *ctx = NULL; |
| 116 | int res = 0; |
| 117 | unsigned long flags; |
| 118 | struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info; |
| 119 | |
Jaegeuk Kim | edf3fb8 | 2015-05-05 20:20:29 -0700 | [diff] [blame] | 120 | if (ci == NULL) |
| 121 | return ERR_PTR(-EACCES); |
| 122 | |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 123 | /* |
| 124 | * We first try getting the ctx from a free list because in |
| 125 | * the common case the ctx will have an allocated and |
| 126 | * initialized crypto tfm, so it's probably a worthwhile |
| 127 | * optimization. For the bounce page, we first try getting it |
| 128 | * from the kernel allocator because that's just about as fast |
| 129 | * as getting it from a list and because a cache of free pages |
| 130 | * should generally be a "last resort" option for a filesystem |
| 131 | * to be able to do its job. |
| 132 | */ |
| 133 | spin_lock_irqsave(&f2fs_crypto_ctx_lock, flags); |
| 134 | ctx = list_first_entry_or_null(&f2fs_free_crypto_ctxs, |
| 135 | struct f2fs_crypto_ctx, free_list); |
| 136 | if (ctx) |
| 137 | list_del(&ctx->free_list); |
| 138 | spin_unlock_irqrestore(&f2fs_crypto_ctx_lock, flags); |
| 139 | if (!ctx) { |
Jaegeuk Kim | 8bacf6de | 2015-05-12 13:26:54 -0700 | [diff] [blame] | 140 | ctx = kmem_cache_zalloc(f2fs_crypto_ctx_cachep, GFP_NOFS); |
| 141 | if (!ctx) { |
| 142 | res = -ENOMEM; |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 143 | goto out; |
| 144 | } |
| 145 | ctx->flags |= F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL; |
| 146 | } else { |
| 147 | ctx->flags &= ~F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL; |
| 148 | } |
Jaegeuk Kim | ca40b03 | 2015-05-12 13:40:20 -0700 | [diff] [blame] | 149 | ctx->flags &= ~F2FS_WRITE_PATH_FL; |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * Allocate a new Crypto API context if we don't already have |
| 153 | * one or if it isn't the right mode. |
| 154 | */ |
Jaegeuk Kim | 640778f | 2015-05-12 13:33:00 -0700 | [diff] [blame] | 155 | if (ctx->tfm && (ctx->mode != ci->ci_data_mode)) { |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 156 | crypto_free_tfm(ctx->tfm); |
| 157 | ctx->tfm = NULL; |
| 158 | ctx->mode = F2FS_ENCRYPTION_MODE_INVALID; |
| 159 | } |
| 160 | if (!ctx->tfm) { |
Jaegeuk Kim | 640778f | 2015-05-12 13:33:00 -0700 | [diff] [blame] | 161 | switch (ci->ci_data_mode) { |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 162 | case F2FS_ENCRYPTION_MODE_AES_256_XTS: |
| 163 | ctx->tfm = crypto_ablkcipher_tfm( |
| 164 | crypto_alloc_ablkcipher("xts(aes)", 0, 0)); |
| 165 | break; |
| 166 | case F2FS_ENCRYPTION_MODE_AES_256_GCM: |
| 167 | /* |
| 168 | * TODO(mhalcrow): AEAD w/ gcm(aes); |
| 169 | * crypto_aead_setauthsize() |
| 170 | */ |
| 171 | ctx->tfm = ERR_PTR(-ENOTSUPP); |
| 172 | break; |
| 173 | default: |
| 174 | BUG(); |
| 175 | } |
| 176 | if (IS_ERR_OR_NULL(ctx->tfm)) { |
| 177 | res = PTR_ERR(ctx->tfm); |
| 178 | ctx->tfm = NULL; |
| 179 | goto out; |
| 180 | } |
Jaegeuk Kim | 640778f | 2015-05-12 13:33:00 -0700 | [diff] [blame] | 181 | ctx->mode = ci->ci_data_mode; |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 182 | } |
Jaegeuk Kim | 640778f | 2015-05-12 13:33:00 -0700 | [diff] [blame] | 183 | BUG_ON(ci->ci_size != f2fs_encryption_key_size(ci->ci_data_mode)); |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 184 | |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 185 | out: |
| 186 | if (res) { |
| 187 | if (!IS_ERR_OR_NULL(ctx)) |
| 188 | f2fs_release_crypto_ctx(ctx); |
| 189 | ctx = ERR_PTR(res); |
| 190 | } |
| 191 | return ctx; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Call f2fs_decrypt on every single page, reusing the encryption |
| 196 | * context. |
| 197 | */ |
| 198 | static void completion_pages(struct work_struct *work) |
| 199 | { |
| 200 | struct f2fs_crypto_ctx *ctx = |
Jaegeuk Kim | ca40b03 | 2015-05-12 13:40:20 -0700 | [diff] [blame] | 201 | container_of(work, struct f2fs_crypto_ctx, r.work); |
| 202 | struct bio *bio = ctx->r.bio; |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 203 | struct bio_vec *bv; |
| 204 | int i; |
| 205 | |
| 206 | bio_for_each_segment_all(bv, bio, i) { |
| 207 | struct page *page = bv->bv_page; |
| 208 | int ret = f2fs_decrypt(ctx, page); |
| 209 | |
| 210 | if (ret) { |
| 211 | WARN_ON_ONCE(1); |
| 212 | SetPageError(page); |
| 213 | } else |
| 214 | SetPageUptodate(page); |
| 215 | unlock_page(page); |
| 216 | } |
| 217 | f2fs_release_crypto_ctx(ctx); |
| 218 | bio_put(bio); |
| 219 | } |
| 220 | |
| 221 | void f2fs_end_io_crypto_work(struct f2fs_crypto_ctx *ctx, struct bio *bio) |
| 222 | { |
Jaegeuk Kim | ca40b03 | 2015-05-12 13:40:20 -0700 | [diff] [blame] | 223 | INIT_WORK(&ctx->r.work, completion_pages); |
| 224 | ctx->r.bio = bio; |
| 225 | queue_work(f2fs_read_workqueue, &ctx->r.work); |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Jaegeuk Kim | cfc4d97 | 2015-05-15 15:37:24 -0700 | [diff] [blame^] | 228 | static void f2fs_crypto_destroy(void) |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 229 | { |
| 230 | struct f2fs_crypto_ctx *pos, *n; |
| 231 | |
| 232 | list_for_each_entry_safe(pos, n, &f2fs_free_crypto_ctxs, free_list) { |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 233 | if (pos->tfm) |
| 234 | crypto_free_tfm(pos->tfm); |
Jaegeuk Kim | 8bacf6de | 2015-05-12 13:26:54 -0700 | [diff] [blame] | 235 | kmem_cache_free(f2fs_crypto_ctx_cachep, pos); |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 236 | } |
| 237 | INIT_LIST_HEAD(&f2fs_free_crypto_ctxs); |
| 238 | if (f2fs_bounce_page_pool) |
| 239 | mempool_destroy(f2fs_bounce_page_pool); |
| 240 | f2fs_bounce_page_pool = NULL; |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
Jaegeuk Kim | cfc4d97 | 2015-05-15 15:37:24 -0700 | [diff] [blame^] | 244 | * f2fs_crypto_initialize() - Set up for f2fs encryption. |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 245 | * |
| 246 | * We only call this when we start accessing encrypted files, since it |
| 247 | * results in memory getting allocated that wouldn't otherwise be used. |
| 248 | * |
| 249 | * Return: Zero on success, non-zero otherwise. |
| 250 | */ |
Jaegeuk Kim | cfc4d97 | 2015-05-15 15:37:24 -0700 | [diff] [blame^] | 251 | int f2fs_crypto_initialize(void) |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 252 | { |
Jaegeuk Kim | 8bacf6de | 2015-05-12 13:26:54 -0700 | [diff] [blame] | 253 | int i, res = -ENOMEM; |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 254 | |
Jaegeuk Kim | cfc4d97 | 2015-05-15 15:37:24 -0700 | [diff] [blame^] | 255 | if (f2fs_bounce_page_pool) |
| 256 | return 0; |
| 257 | |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 258 | mutex_lock(&crypto_init); |
Jaegeuk Kim | cfc4d97 | 2015-05-15 15:37:24 -0700 | [diff] [blame^] | 259 | if (f2fs_bounce_page_pool) |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 260 | goto already_initialized; |
| 261 | |
Jaegeuk Kim | cfc4d97 | 2015-05-15 15:37:24 -0700 | [diff] [blame^] | 262 | for (i = 0; i < num_prealloc_crypto_ctxs; i++) { |
| 263 | struct f2fs_crypto_ctx *ctx; |
| 264 | |
| 265 | ctx = kmem_cache_zalloc(f2fs_crypto_ctx_cachep, GFP_KERNEL); |
| 266 | if (!ctx) |
| 267 | goto fail; |
| 268 | list_add(&ctx->free_list, &f2fs_free_crypto_ctxs); |
| 269 | } |
| 270 | |
| 271 | /* must be allocated at the last step to avoid race condition above */ |
| 272 | f2fs_bounce_page_pool = |
| 273 | mempool_create_page_pool(num_prealloc_crypto_pages, 0); |
| 274 | if (!f2fs_bounce_page_pool) |
| 275 | goto fail; |
| 276 | |
| 277 | already_initialized: |
| 278 | mutex_unlock(&crypto_init); |
| 279 | return 0; |
| 280 | fail: |
| 281 | f2fs_crypto_destroy(); |
| 282 | mutex_unlock(&crypto_init); |
| 283 | return res; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * f2fs_exit_crypto() - Shutdown the f2fs encryption system |
| 288 | */ |
| 289 | void f2fs_exit_crypto(void) |
| 290 | { |
| 291 | f2fs_crypto_destroy(); |
| 292 | |
| 293 | if (f2fs_read_workqueue) |
| 294 | destroy_workqueue(f2fs_read_workqueue); |
| 295 | if (f2fs_crypto_ctx_cachep) |
| 296 | kmem_cache_destroy(f2fs_crypto_ctx_cachep); |
| 297 | if (f2fs_crypt_info_cachep) |
| 298 | kmem_cache_destroy(f2fs_crypt_info_cachep); |
| 299 | } |
| 300 | |
| 301 | int __init f2fs_init_crypto(void) |
| 302 | { |
| 303 | int res = -ENOMEM; |
| 304 | |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 305 | f2fs_read_workqueue = alloc_workqueue("f2fs_crypto", WQ_HIGHPRI, 0); |
Jaegeuk Kim | 8bacf6de | 2015-05-12 13:26:54 -0700 | [diff] [blame] | 306 | if (!f2fs_read_workqueue) |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 307 | goto fail; |
Jaegeuk Kim | 8bacf6de | 2015-05-12 13:26:54 -0700 | [diff] [blame] | 308 | |
| 309 | f2fs_crypto_ctx_cachep = KMEM_CACHE(f2fs_crypto_ctx, |
Jaegeuk Kim | cfc4d97 | 2015-05-15 15:37:24 -0700 | [diff] [blame^] | 310 | SLAB_RECLAIM_ACCOUNT); |
Jaegeuk Kim | 8bacf6de | 2015-05-12 13:26:54 -0700 | [diff] [blame] | 311 | if (!f2fs_crypto_ctx_cachep) |
| 312 | goto fail; |
| 313 | |
| 314 | f2fs_crypt_info_cachep = KMEM_CACHE(f2fs_crypt_info, |
Jaegeuk Kim | cfc4d97 | 2015-05-15 15:37:24 -0700 | [diff] [blame^] | 315 | SLAB_RECLAIM_ACCOUNT); |
Jaegeuk Kim | 8bacf6de | 2015-05-12 13:26:54 -0700 | [diff] [blame] | 316 | if (!f2fs_crypt_info_cachep) |
| 317 | goto fail; |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 318 | |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 319 | return 0; |
| 320 | fail: |
| 321 | f2fs_exit_crypto(); |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 322 | return res; |
| 323 | } |
| 324 | |
| 325 | void f2fs_restore_and_release_control_page(struct page **page) |
| 326 | { |
| 327 | struct f2fs_crypto_ctx *ctx; |
| 328 | struct page *bounce_page; |
| 329 | |
| 330 | /* The bounce data pages are unmapped. */ |
| 331 | if ((*page)->mapping) |
| 332 | return; |
| 333 | |
| 334 | /* The bounce data page is unmapped. */ |
| 335 | bounce_page = *page; |
| 336 | ctx = (struct f2fs_crypto_ctx *)page_private(bounce_page); |
| 337 | |
| 338 | /* restore control page */ |
Jaegeuk Kim | ca40b03 | 2015-05-12 13:40:20 -0700 | [diff] [blame] | 339 | *page = ctx->w.control_page; |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 340 | |
| 341 | f2fs_restore_control_page(bounce_page); |
| 342 | } |
| 343 | |
| 344 | void f2fs_restore_control_page(struct page *data_page) |
| 345 | { |
| 346 | struct f2fs_crypto_ctx *ctx = |
| 347 | (struct f2fs_crypto_ctx *)page_private(data_page); |
| 348 | |
| 349 | set_page_private(data_page, (unsigned long)NULL); |
| 350 | ClearPagePrivate(data_page); |
| 351 | unlock_page(data_page); |
| 352 | f2fs_release_crypto_ctx(ctx); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * f2fs_crypt_complete() - The completion callback for page encryption |
| 357 | * @req: The asynchronous encryption request context |
| 358 | * @res: The result of the encryption operation |
| 359 | */ |
| 360 | static void f2fs_crypt_complete(struct crypto_async_request *req, int res) |
| 361 | { |
| 362 | struct f2fs_completion_result *ecr = req->data; |
| 363 | |
| 364 | if (res == -EINPROGRESS) |
| 365 | return; |
| 366 | ecr->res = res; |
| 367 | complete(&ecr->completion); |
| 368 | } |
| 369 | |
| 370 | typedef enum { |
| 371 | F2FS_DECRYPT = 0, |
| 372 | F2FS_ENCRYPT, |
| 373 | } f2fs_direction_t; |
| 374 | |
| 375 | static int f2fs_page_crypto(struct f2fs_crypto_ctx *ctx, |
| 376 | struct inode *inode, |
| 377 | f2fs_direction_t rw, |
| 378 | pgoff_t index, |
| 379 | struct page *src_page, |
| 380 | struct page *dest_page) |
| 381 | { |
| 382 | u8 xts_tweak[F2FS_XTS_TWEAK_SIZE]; |
| 383 | struct ablkcipher_request *req = NULL; |
| 384 | DECLARE_F2FS_COMPLETION_RESULT(ecr); |
| 385 | struct scatterlist dst, src; |
| 386 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 387 | struct crypto_ablkcipher *atfm = __crypto_ablkcipher_cast(ctx->tfm); |
| 388 | int res = 0; |
| 389 | |
| 390 | BUG_ON(!ctx->tfm); |
Jaegeuk Kim | 640778f | 2015-05-12 13:33:00 -0700 | [diff] [blame] | 391 | BUG_ON(ctx->mode != fi->i_crypt_info->ci_data_mode); |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 392 | |
| 393 | if (ctx->mode != F2FS_ENCRYPTION_MODE_AES_256_XTS) { |
| 394 | printk_ratelimited(KERN_ERR |
| 395 | "%s: unsupported crypto algorithm: %d\n", |
| 396 | __func__, ctx->mode); |
| 397 | return -ENOTSUPP; |
| 398 | } |
| 399 | |
| 400 | crypto_ablkcipher_clear_flags(atfm, ~0); |
| 401 | crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_REQ_WEAK_KEY); |
| 402 | |
| 403 | res = crypto_ablkcipher_setkey(atfm, fi->i_crypt_info->ci_raw, |
| 404 | fi->i_crypt_info->ci_size); |
| 405 | if (res) { |
| 406 | printk_ratelimited(KERN_ERR |
| 407 | "%s: crypto_ablkcipher_setkey() failed\n", |
| 408 | __func__); |
| 409 | return res; |
| 410 | } |
| 411 | req = ablkcipher_request_alloc(atfm, GFP_NOFS); |
| 412 | if (!req) { |
| 413 | printk_ratelimited(KERN_ERR |
| 414 | "%s: crypto_request_alloc() failed\n", |
| 415 | __func__); |
| 416 | return -ENOMEM; |
| 417 | } |
| 418 | ablkcipher_request_set_callback( |
| 419 | req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 420 | f2fs_crypt_complete, &ecr); |
| 421 | |
| 422 | BUILD_BUG_ON(F2FS_XTS_TWEAK_SIZE < sizeof(index)); |
| 423 | memcpy(xts_tweak, &index, sizeof(index)); |
| 424 | memset(&xts_tweak[sizeof(index)], 0, |
| 425 | F2FS_XTS_TWEAK_SIZE - sizeof(index)); |
| 426 | |
| 427 | sg_init_table(&dst, 1); |
| 428 | sg_set_page(&dst, dest_page, PAGE_CACHE_SIZE, 0); |
| 429 | sg_init_table(&src, 1); |
| 430 | sg_set_page(&src, src_page, PAGE_CACHE_SIZE, 0); |
| 431 | ablkcipher_request_set_crypt(req, &src, &dst, PAGE_CACHE_SIZE, |
| 432 | xts_tweak); |
| 433 | if (rw == F2FS_DECRYPT) |
| 434 | res = crypto_ablkcipher_decrypt(req); |
| 435 | else |
| 436 | res = crypto_ablkcipher_encrypt(req); |
| 437 | if (res == -EINPROGRESS || res == -EBUSY) { |
| 438 | BUG_ON(req->base.data != &ecr); |
| 439 | wait_for_completion(&ecr.completion); |
| 440 | res = ecr.res; |
| 441 | } |
| 442 | ablkcipher_request_free(req); |
| 443 | if (res) { |
| 444 | printk_ratelimited(KERN_ERR |
| 445 | "%s: crypto_ablkcipher_encrypt() returned %d\n", |
| 446 | __func__, res); |
| 447 | return res; |
| 448 | } |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * f2fs_encrypt() - Encrypts a page |
| 454 | * @inode: The inode for which the encryption should take place |
| 455 | * @plaintext_page: The page to encrypt. Must be locked. |
| 456 | * |
| 457 | * Allocates a ciphertext page and encrypts plaintext_page into it using the ctx |
| 458 | * encryption context. |
| 459 | * |
| 460 | * Called on the page write path. The caller must call |
| 461 | * f2fs_restore_control_page() on the returned ciphertext page to |
| 462 | * release the bounce buffer and the encryption context. |
| 463 | * |
| 464 | * Return: An allocated page with the encrypted content on success. Else, an |
| 465 | * error value or NULL. |
| 466 | */ |
| 467 | struct page *f2fs_encrypt(struct inode *inode, |
| 468 | struct page *plaintext_page) |
| 469 | { |
| 470 | struct f2fs_crypto_ctx *ctx; |
| 471 | struct page *ciphertext_page = NULL; |
| 472 | int err; |
| 473 | |
| 474 | BUG_ON(!PageLocked(plaintext_page)); |
| 475 | |
| 476 | ctx = f2fs_get_crypto_ctx(inode); |
| 477 | if (IS_ERR(ctx)) |
| 478 | return (struct page *)ctx; |
| 479 | |
| 480 | /* The encryption operation will require a bounce page. */ |
| 481 | ciphertext_page = alloc_page(GFP_NOFS); |
| 482 | if (!ciphertext_page) { |
| 483 | /* |
| 484 | * This is a potential bottleneck, but at least we'll have |
| 485 | * forward progress. |
| 486 | */ |
| 487 | ciphertext_page = mempool_alloc(f2fs_bounce_page_pool, |
| 488 | GFP_NOFS); |
| 489 | if (WARN_ON_ONCE(!ciphertext_page)) |
| 490 | ciphertext_page = mempool_alloc(f2fs_bounce_page_pool, |
| 491 | GFP_NOFS | __GFP_WAIT); |
| 492 | ctx->flags &= ~F2FS_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL; |
| 493 | } else { |
| 494 | ctx->flags |= F2FS_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL; |
| 495 | } |
Jaegeuk Kim | ca40b03 | 2015-05-12 13:40:20 -0700 | [diff] [blame] | 496 | ctx->flags |= F2FS_WRITE_PATH_FL; |
| 497 | ctx->w.bounce_page = ciphertext_page; |
| 498 | ctx->w.control_page = plaintext_page; |
Jaegeuk Kim | 57e5055 | 2015-04-20 19:52:47 -0700 | [diff] [blame] | 499 | err = f2fs_page_crypto(ctx, inode, F2FS_ENCRYPT, plaintext_page->index, |
| 500 | plaintext_page, ciphertext_page); |
| 501 | if (err) { |
| 502 | f2fs_release_crypto_ctx(ctx); |
| 503 | return ERR_PTR(err); |
| 504 | } |
| 505 | SetPagePrivate(ciphertext_page); |
| 506 | set_page_private(ciphertext_page, (unsigned long)ctx); |
| 507 | lock_page(ciphertext_page); |
| 508 | return ciphertext_page; |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * f2fs_decrypt() - Decrypts a page in-place |
| 513 | * @ctx: The encryption context. |
| 514 | * @page: The page to decrypt. Must be locked. |
| 515 | * |
| 516 | * Decrypts page in-place using the ctx encryption context. |
| 517 | * |
| 518 | * Called from the read completion callback. |
| 519 | * |
| 520 | * Return: Zero on success, non-zero otherwise. |
| 521 | */ |
| 522 | int f2fs_decrypt(struct f2fs_crypto_ctx *ctx, struct page *page) |
| 523 | { |
| 524 | BUG_ON(!PageLocked(page)); |
| 525 | |
| 526 | return f2fs_page_crypto(ctx, page->mapping->host, |
| 527 | F2FS_DECRYPT, page->index, page, page); |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Convenience function which takes care of allocating and |
| 532 | * deallocating the encryption context |
| 533 | */ |
| 534 | int f2fs_decrypt_one(struct inode *inode, struct page *page) |
| 535 | { |
| 536 | struct f2fs_crypto_ctx *ctx = f2fs_get_crypto_ctx(inode); |
| 537 | int ret; |
| 538 | |
| 539 | if (!ctx) |
| 540 | return -ENOMEM; |
| 541 | ret = f2fs_decrypt(ctx, page); |
| 542 | f2fs_release_crypto_ctx(ctx); |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | bool f2fs_valid_contents_enc_mode(uint32_t mode) |
| 547 | { |
| 548 | return (mode == F2FS_ENCRYPTION_MODE_AES_256_XTS); |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * f2fs_validate_encryption_key_size() - Validate the encryption key size |
| 553 | * @mode: The key mode. |
| 554 | * @size: The key size to validate. |
| 555 | * |
| 556 | * Return: The validated key size for @mode. Zero if invalid. |
| 557 | */ |
| 558 | uint32_t f2fs_validate_encryption_key_size(uint32_t mode, uint32_t size) |
| 559 | { |
| 560 | if (size == f2fs_encryption_key_size(mode)) |
| 561 | return size; |
| 562 | return 0; |
| 563 | } |