diff options
| author | Tyler Hicks <tyhicks@canonical.com> | 2013-04-06 03:08:48 -0400 |
|---|---|---|
| committer | Tyler Hicks <tyhicks@canonical.com> | 2013-06-07 20:28:27 -0400 |
| commit | d78de618962d1e9d28c602e3c75991fe9c94e961 (patch) | |
| tree | 674894d0c1a3bf69f0b901f24113f35e2814db7f | |
| parent | a8ca90e2071edb3d3f3272ae73d73411f0b70b54 (diff) | |
eCryptfs: Merge ecryptfs_encrypt_extent() and ecryptfs_decrypt_extent()
They are identical except if the src_page or dst_page index is used, so
they can be merged safely if page_index is conditionally assigned.
Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
| -rw-r--r-- | fs/ecryptfs/crypto.c | 74 |
1 files changed, 20 insertions, 54 deletions
diff --git a/fs/ecryptfs/crypto.c b/fs/ecryptfs/crypto.c index 609efc01d5c2..9845d2fd2506 100644 --- a/fs/ecryptfs/crypto.c +++ b/fs/ecryptfs/crypto.c | |||
| @@ -416,28 +416,29 @@ static loff_t lower_offset_for_page(struct ecryptfs_crypt_stat *crypt_stat, | |||
| 416 | } | 416 | } |
| 417 | 417 | ||
| 418 | /** | 418 | /** |
| 419 | * ecryptfs_encrypt_extent | 419 | * crypt_extent |
| 420 | * @enc_extent_page: Allocated page into which to encrypt the data in | 420 | * @dst_page: The page to write the result into |
| 421 | * @page | ||
| 422 | * @crypt_stat: crypt_stat containing cryptographic context for the | 421 | * @crypt_stat: crypt_stat containing cryptographic context for the |
| 423 | * encryption operation | 422 | * encryption operation |
| 424 | * @page: Page containing plaintext data extent to encrypt | 423 | * @src_page: The page to read from |
| 425 | * @extent_offset: Page extent offset for use in generating IV | 424 | * @extent_offset: Page extent offset for use in generating IV |
| 425 | * @op: ENCRYPT or DECRYPT to indicate the desired operation | ||
| 426 | * | 426 | * |
| 427 | * Encrypts one extent of data. | 427 | * Encrypts or decrypts one extent of data. |
| 428 | * | 428 | * |
| 429 | * Return zero on success; non-zero otherwise | 429 | * Return zero on success; non-zero otherwise |
| 430 | */ | 430 | */ |
| 431 | static int ecryptfs_encrypt_extent(struct page *enc_extent_page, | 431 | static int crypt_extent(struct page *dst_page, |
| 432 | struct ecryptfs_crypt_stat *crypt_stat, | 432 | struct ecryptfs_crypt_stat *crypt_stat, |
| 433 | struct page *page, | 433 | struct page *src_page, |
| 434 | unsigned long extent_offset) | 434 | unsigned long extent_offset, int op) |
| 435 | { | 435 | { |
| 436 | pgoff_t page_index = op == ENCRYPT ? src_page->index : dst_page->index; | ||
| 436 | loff_t extent_base; | 437 | loff_t extent_base; |
| 437 | char extent_iv[ECRYPTFS_MAX_IV_BYTES]; | 438 | char extent_iv[ECRYPTFS_MAX_IV_BYTES]; |
| 438 | int rc; | 439 | int rc; |
| 439 | 440 | ||
| 440 | extent_base = (((loff_t)page->index) | 441 | extent_base = (((loff_t)page_index) |
| 441 | * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); | 442 | * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); |
| 442 | rc = ecryptfs_derive_iv(extent_iv, crypt_stat, | 443 | rc = ecryptfs_derive_iv(extent_iv, crypt_stat, |
| 443 | (extent_base + extent_offset)); | 444 | (extent_base + extent_offset)); |
| @@ -447,14 +448,13 @@ static int ecryptfs_encrypt_extent(struct page *enc_extent_page, | |||
| 447 | (unsigned long long)(extent_base + extent_offset), rc); | 448 | (unsigned long long)(extent_base + extent_offset), rc); |
| 448 | goto out; | 449 | goto out; |
| 449 | } | 450 | } |
| 450 | rc = crypt_page_offset(crypt_stat, enc_extent_page, page, | 451 | rc = crypt_page_offset(crypt_stat, dst_page, src_page, |
| 451 | (extent_offset * crypt_stat->extent_size), | 452 | (extent_offset * crypt_stat->extent_size), |
| 452 | crypt_stat->extent_size, extent_iv, ENCRYPT); | 453 | crypt_stat->extent_size, extent_iv, op); |
| 453 | if (rc < 0) { | 454 | if (rc < 0) { |
| 454 | printk(KERN_ERR "%s: Error attempting to encrypt page with " | 455 | printk(KERN_ERR "%s: Error attempting to crypt page with " |
| 455 | "page->index = [%ld], extent_offset = [%ld]; " | 456 | "page_index = [%ld], extent_offset = [%ld]; " |
| 456 | "rc = [%d]\n", __func__, page->index, extent_offset, | 457 | "rc = [%d]\n", __func__, page_index, extent_offset, rc); |
| 457 | rc); | ||
| 458 | goto out; | 458 | goto out; |
| 459 | } | 459 | } |
| 460 | rc = 0; | 460 | rc = 0; |
| @@ -503,8 +503,8 @@ int ecryptfs_encrypt_page(struct page *page) | |||
| 503 | for (extent_offset = 0; | 503 | for (extent_offset = 0; |
| 504 | extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); | 504 | extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); |
| 505 | extent_offset++) { | 505 | extent_offset++) { |
| 506 | rc = ecryptfs_encrypt_extent(enc_extent_page, crypt_stat, page, | 506 | rc = crypt_extent(enc_extent_page, crypt_stat, page, |
| 507 | extent_offset); | 507 | extent_offset, ENCRYPT); |
| 508 | if (rc) { | 508 | if (rc) { |
| 509 | printk(KERN_ERR "%s: Error encrypting extent; " | 509 | printk(KERN_ERR "%s: Error encrypting extent; " |
| 510 | "rc = [%d]\n", __func__, rc); | 510 | "rc = [%d]\n", __func__, rc); |
| @@ -531,40 +531,6 @@ out: | |||
| 531 | return rc; | 531 | return rc; |
| 532 | } | 532 | } |
| 533 | 533 | ||
| 534 | static int ecryptfs_decrypt_extent(struct page *page, | ||
| 535 | struct ecryptfs_crypt_stat *crypt_stat, | ||
| 536 | struct page *enc_extent_page, | ||
| 537 | unsigned long extent_offset) | ||
| 538 | { | ||
| 539 | loff_t extent_base; | ||
| 540 | char extent_iv[ECRYPTFS_MAX_IV_BYTES]; | ||
| 541 | int rc; | ||
| 542 | |||
| 543 | extent_base = (((loff_t)page->index) | ||
| 544 | * (PAGE_CACHE_SIZE / crypt_stat->extent_size)); | ||
| 545 | rc = ecryptfs_derive_iv(extent_iv, crypt_stat, | ||
| 546 | (extent_base + extent_offset)); | ||
| 547 | if (rc) { | ||
| 548 | ecryptfs_printk(KERN_ERR, "Error attempting to derive IV for " | ||
| 549 | "extent [0x%.16llx]; rc = [%d]\n", | ||
| 550 | (unsigned long long)(extent_base + extent_offset), rc); | ||
| 551 | goto out; | ||
| 552 | } | ||
| 553 | rc = crypt_page_offset(crypt_stat, page, enc_extent_page, | ||
| 554 | (extent_offset * crypt_stat->extent_size), | ||
| 555 | crypt_stat->extent_size, extent_iv, DECRYPT); | ||
| 556 | if (rc < 0) { | ||
| 557 | printk(KERN_ERR "%s: Error attempting to decrypt to page with " | ||
| 558 | "page->index = [%ld], extent_offset = [%ld]; " | ||
| 559 | "rc = [%d]\n", __func__, page->index, extent_offset, | ||
| 560 | rc); | ||
| 561 | goto out; | ||
| 562 | } | ||
| 563 | rc = 0; | ||
| 564 | out: | ||
| 565 | return rc; | ||
| 566 | } | ||
| 567 | |||
| 568 | /** | 534 | /** |
| 569 | * ecryptfs_decrypt_page | 535 | * ecryptfs_decrypt_page |
| 570 | * @page: Page mapped from the eCryptfs inode for the file; data read | 536 | * @page: Page mapped from the eCryptfs inode for the file; data read |
| @@ -610,8 +576,8 @@ int ecryptfs_decrypt_page(struct page *page) | |||
| 610 | for (extent_offset = 0; | 576 | for (extent_offset = 0; |
| 611 | extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); | 577 | extent_offset < (PAGE_CACHE_SIZE / crypt_stat->extent_size); |
| 612 | extent_offset++) { | 578 | extent_offset++) { |
| 613 | rc = ecryptfs_decrypt_extent(page, crypt_stat, page, | 579 | rc = crypt_extent(page, crypt_stat, page, |
| 614 | extent_offset); | 580 | extent_offset, DECRYPT); |
| 615 | if (rc) { | 581 | if (rc) { |
| 616 | printk(KERN_ERR "%s: Error encrypting extent; " | 582 | printk(KERN_ERR "%s: Error encrypting extent; " |
| 617 | "rc = [%d]\n", __func__, rc); | 583 | "rc = [%d]\n", __func__, rc); |
