[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 07/10] cryptodisk: Replace some literals with constants in gru
From: |
Glenn Washburn |
Subject: |
[PATCH v2 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt. |
Date: |
Sat, 3 Oct 2020 17:55:31 -0500 |
This should improve readability of code by providing clues as to what the
value represents.
Signed-off-by: Glenn Washburn <development@efficientek.com>
---
grub-core/disk/cryptodisk.c | 12 +++++++-----
include/grub/types.h | 3 +++
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
index 623f0f396..1a91c2d55 100644
--- a/grub-core/disk/cryptodisk.c
+++ b/grub-core/disk/cryptodisk.c
@@ -297,19 +297,21 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
}
break;
case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
- iv[1] = grub_cpu_to_le32 (sector >> (32 - log_sector_size));
+ /* The IV is the 64 bit byte offset of the sector. */
+ iv[1] = grub_cpu_to_le32 (sector >> (GRUB_TYPE_BIT(iv[0])
+ - log_sector_size));
iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
- & 0xFFFFFFFF);
+ & GRUB_TYPE_MAX(iv[0]));
break;
case GRUB_CRYPTODISK_MODE_IV_BENBI:
{
grub_uint64_t num = (sector << dev->benbi_log) + 1;
- iv[sz - 2] = grub_cpu_to_be32 (num >> 32);
- iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
+ iv[sz - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BIT(iv[0]));
+ iv[sz - 1] = grub_cpu_to_be32 (num & GRUB_TYPE_MAX(iv[0]));
}
break;
case GRUB_CRYPTODISK_MODE_IV_ESSIV:
- iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+ iv[0] = grub_cpu_to_le32 (sector & GRUB_TYPE_MAX(iv[0]));
err = grub_crypto_ecb_encrypt (dev->essiv_cipher, iv, iv,
dev->cipher->cipher->blocksize);
if (err)
diff --git a/include/grub/types.h b/include/grub/types.h
index 035a4b528..8b4267ebd 100644
--- a/include/grub/types.h
+++ b/include/grub/types.h
@@ -319,4 +319,7 @@ static inline void grub_set_unaligned64 (void *ptr,
grub_uint64_t val)
#define GRUB_CHAR_BIT 8
+#define GRUB_TYPE_BIT(type) (sizeof(type) * GRUB_CHAR_BIT)
+#define GRUB_TYPE_MAX(type) ((2 * ((1ULL << (GRUB_TYPE_BIT(type) - 1)) - 1)) +
1)
+
#endif /* ! GRUB_TYPES_HEADER */
--
2.27.0
- Re: [PATCH v2 03/10] luks2: Use more intuitive keyslot key instead of index when naming keyslot., (continued)
[PATCH v2 05/10] cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'., Glenn Washburn, 2020/10/03
[PATCH v2 04/10] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors, Glenn Washburn, 2020/10/03
[PATCH v2 06/10] cryptodisk: Properly handle non-512 byte sized sectors., Glenn Washburn, 2020/10/03
[PATCH v2 08/10] cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors., Glenn Washburn, 2020/10/03
[PATCH v2 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.,
Glenn Washburn <=
[PATCH v2 09/10] cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors., Glenn Washburn, 2020/10/03
[PATCH v2 10/10] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c., Glenn Washburn, 2020/10/03
Re: [PATCH v2 00/10] Cryptodisk fixes for v2.06 redux, Patrick Steinhardt, 2020/10/09