[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v5 03/11] cryptodisk: Replace some literals with constants in gru
From: |
Glenn Washburn |
Subject: |
[PATCH v5 03/11] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt |
Date: |
Sun, 22 Nov 2020 23:23:16 -0600 |
This should improve readability of code by providing clues as to what the
value represents. The new macro GRUB_TYPE_BITS(type) returns the number of
bits allocated for type. Also add GRUB_TYPE_U_MAX/MIN(type) macros to get
the max/min values for an unsigned number with size of type.
Signed-off-by: Glenn Washburn <development@efficientek.com>
---
grub-core/disk/cryptodisk.c | 13 +++++++------
include/grub/types.h | 5 +++++
2 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
index 473c93976..31b73c535 100644
--- a/grub-core/disk/cryptodisk.c
+++ b/grub-core/disk/cryptodisk.c
@@ -284,22 +284,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
iv[1] = grub_cpu_to_le32 (sector >> 32);
/* FALLTHROUGH */
case GRUB_CRYPTODISK_MODE_IV_PLAIN:
- iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+ iv[0] = grub_cpu_to_le32 (sector & GRUB_TYPE_U_MAX (iv[0]));
break;
case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
- iv[1] = grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size));
+ iv[1] = grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1])
+ - dev->log_sector_size));
iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
- & 0xFFFFFFFF);
+ & GRUB_TYPE_U_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_BITS (iv[0]));
+ iv[sz - 1] = grub_cpu_to_be32 (num & GRUB_TYPE_U_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_U_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 9cd7e2525..b9c7426fc 100644
--- a/include/grub/types.h
+++ b/include/grub/types.h
@@ -72,6 +72,8 @@
# endif
#endif
+#define GRUB_TYPE_BITS(type) (sizeof(type) * GRUB_CHAR_BIT)
+
#ifndef __CHAR_BIT__
# error __CHAR_BIT__ is not defined
#elif __CHAR_BIT__ != 8
@@ -159,6 +161,9 @@ typedef grub_int32_t grub_ssize_t;
#endif
# define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)
+#define GRUB_TYPE_U_MAX(type) ((typeof (1ULL))((typeof (type))(~0)))
+#define GRUB_TYPE_U_MIN(type) 0ULL
+
typedef grub_uint64_t grub_properly_aligned_t;
#define GRUB_PROPERLY_ALIGNED_ARRAY(name, size) grub_properly_aligned_t
name[((size) + sizeof (grub_properly_aligned_t) - 1) / sizeof
(grub_properly_aligned_t)]
--
2.27.0
- Re: [PATCH v4 14/15] luks2: Better error handling when setting up the cryptodisk., (continued)
[PATCH v4 15/15] luks2: Error check segment.sector_size., Glenn Washburn, 2020/11/06
Re: [PATCH v4 00/15] Cryptodisk fixes for v2.06 redux, Daniel Kiper, 2020/11/17
[PATCH v5 00/11] Cryptodisk fixes for v2.06 redux, Glenn Washburn, 2020/11/23
- [PATCH v5 01/11] luks2: Add slot_key member to struct grub_luks2_keyslot/segment/digest, Glenn Washburn, 2020/11/23
- [PATCH v5 02/11] luks: Use more intuitive slot key instead of index in user messages., Glenn Washburn, 2020/11/23
- [PATCH v5 03/11] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt,
Glenn Washburn <=
- [PATCH v5 04/11] luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors, Glenn Washburn, 2020/11/23
- [PATCH v5 05/11] cryptodisk: Properly handle non-512 byte sized sectors, Glenn Washburn, 2020/11/23
- [PATCH v5 06/11] luks2: Better error handling when setting up the cryptodisk, Glenn Washburn, 2020/11/23
- [PATCH v5 08/11] whitespace: convert 8 spaces to tabs., Glenn Washburn, 2020/11/23
- [PATCH v5 07/11] luks2: Error check segment.sector_size, Glenn Washburn, 2020/11/23
- [PATCH v5 09/11] mips: Enable __clzdi2(), Glenn Washburn, 2020/11/23
- [PATCH v5 10/11] misc: Add grub_log2ull macro for calculating log base 2 of 64-bit integers, Glenn Washburn, 2020/11/23
- [PATCH v5 11/11] luks2: Use grub_log2ull to calculate log_sector_size and improve readability, Glenn Washburn, 2020/11/23
[PATCH v6 00/12] Cryptodisk fixes for v2.06 redux, Glenn Washburn, 2020/11/27