[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v8 11/18] cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to rep
From: |
Glenn Washburn |
Subject: |
[PATCH v8 11/18] cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals |
Date: |
Tue, 8 Dec 2020 16:45:42 -0600 |
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 | 8 ++++----
include/grub/types.h | 7 +++++++
2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c
index 0e955a020..5aa0c4720 100644
--- a/grub-core/disk/cryptodisk.c
+++ b/grub-core/disk/cryptodisk.c
@@ -284,23 +284,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
iv[1] = grub_cpu_to_le32 (sector >> GRUB_TYPE_BITS (iv[0]));
/* 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 >> (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 >> GRUB_TYPE_BITS (iv[0]));
- iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
+ 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 9989e3a16..b36b26a79 100644
--- a/include/grub/types.h
+++ b/include/grub/types.h
@@ -161,6 +161,13 @@ typedef grub_int32_t grub_ssize_t;
#endif
# define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1)
+/*
+ * Cast to unsigned long long so that the "return value" is always a consistent
+ * type and to ensure an unsigned value regardless of type parameter.
+ */
+#define GRUB_TYPE_U_MAX(type) ((unsigned long long)((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
- [PATCH v8 01/18] disk: Rename grub_disk_get_size to grub_disk_native_sectors, (continued)
- [PATCH v8 01/18] disk: Rename grub_disk_get_size to grub_disk_native_sectors, Glenn Washburn, 2020/12/08
- [PATCH v8 03/18] whitespace: convert 8 spaces to tabs, Glenn Washburn, 2020/12/08
- [PATCH v8 07/18] luks2: Use more intuitive object name instead of json index in user messages, Glenn Washburn, 2020/12/08
- [PATCH v8 04/18] luks2: Remove unused argument in grub_error, Glenn Washburn, 2020/12/08
- [PATCH v8 08/18] luks2: Rename json index variables to names that they are obviously json indexes, Glenn Washburn, 2020/12/08
- [PATCH v8 11/18] cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals,
Glenn Washburn <=
- [PATCH v8 12/18] luks2: grub_cryptodisk_t->total_sectors is the max number of device native sectors, Glenn Washburn, 2020/12/08
- [PATCH v8 05/18] luks2: Make sure all fields of output argument in luks2_parse_digest() are written to, Glenn Washburn, 2020/12/08
- [PATCH v8 13/18] cryptodisk: Properly handle non-512 byte sized sectors, Glenn Washburn, 2020/12/08
- [PATCH v8 06/18] luks2: Add idx member to struct grub_luks2_keyslot/segment/digest, Glenn Washburn, 2020/12/08