]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
cryptodisk: Add macro GRUB_TYPE_BITS() to replace some literals
authorGlenn Washburn <development@efficientek.com>
Tue, 8 Dec 2020 22:45:41 +0000 (16:45 -0600)
committerDaniel Kiper <daniel.kiper@oracle.com>
Sat, 12 Dec 2020 00:19:04 +0000 (01:19 +0100)
The new macro GRUB_TYPE_BITS(type) returns the number of bits
allocated for type.

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
grub-core/disk/cryptodisk.c
include/grub/types.h

index 473c93976243dd8eb3b780ca4642ed1a95aa9e26..0e955a0202f63665e7ef4eeb3cc1c579dceebc8a 100644 (file)
@@ -281,20 +281,21 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
          }
          break;
        case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-         iv[1] = grub_cpu_to_le32 (sector >> 32);
+         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);
          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);
          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 - 2] = grub_cpu_to_be32 (num >> GRUB_TYPE_BITS (iv[0]));
            iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF);
          }
          break;
index f22055f98acf07aaecd5231ee5b9d075c3e6bd41..9989e3a16ddaa8d4c1fd0f7833c595982057081f 100644 (file)
@@ -80,6 +80,8 @@
 # define GRUB_CHAR_BIT __CHAR_BIT__
 #endif
 
+#define GRUB_TYPE_BITS(type) (sizeof(type) * GRUB_CHAR_BIT)
+
 /* Define various wide integers.  */
 typedef signed char            grub_int8_t;
 typedef short                  grub_int16_t;