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