cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals

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>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
Glenn Washburn 2020-12-08 16:45:42 -06:00 committed by Daniel Kiper
parent 3e5f7f5112
commit b34cb38795
2 changed files with 11 additions and 4 deletions

View File

@ -284,23 +284,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
iv[1] = grub_cpu_to_le32 (sector >> GRUB_TYPE_BITS (iv[0])); iv[1] = grub_cpu_to_le32 (sector >> GRUB_TYPE_BITS (iv[0]));
/* FALLTHROUGH */ /* FALLTHROUGH */
case GRUB_CRYPTODISK_MODE_IV_PLAIN: 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; break;
case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64: case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
iv[1] = grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1]) iv[1] = grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS (iv[1])
- dev->log_sector_size)); - dev->log_sector_size));
iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size) iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
& 0xFFFFFFFF); & GRUB_TYPE_U_MAX (iv[0]));
break; break;
case GRUB_CRYPTODISK_MODE_IV_BENBI: case GRUB_CRYPTODISK_MODE_IV_BENBI:
{ {
grub_uint64_t num = (sector << dev->benbi_log) + 1; grub_uint64_t num = (sector << dev->benbi_log) + 1;
iv[sz - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BITS (iv[0])); 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; break;
case GRUB_CRYPTODISK_MODE_IV_ESSIV: 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, err = grub_crypto_ecb_encrypt (dev->essiv_cipher, iv, iv,
dev->cipher->cipher->blocksize); dev->cipher->cipher->blocksize);
if (err) if (err)

View File

@ -161,6 +161,13 @@ typedef grub_int32_t grub_ssize_t;
#endif #endif
# define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1) # 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; 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)] #define GRUB_PROPERLY_ALIGNED_ARRAY(name, size) grub_properly_aligned_t name[((size) + sizeof (grub_properly_aligned_t) - 1) / sizeof (grub_properly_aligned_t)]