Skip to contentSkip to conversation

Cryptodisk fixes for v2.06 redux

PATCH Superseded Glenn Washburn 3 participants 289 messages Oct 3, 2020 – Dec 15, 2020 Page 1 of 3

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/10 luks2: Fix use of incorrect index and some grub_error() messages.

In reply to Glenn Washburn

When looping over the digests and segments, the loop variable is j, but the variable i is used to index in the the digests and segments json array. The variable i is the keyslot index. Similarly, there are several grub_error() statements using the wrong index in constructing the error string.

---
 grub-core/disk/luks2.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
grub-core/disk/luks2.c +7 -7
index 31d7166fc..2241e0312 100644
@@ -275,34 +275,34 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
   for (j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&digest, &digests, i) ||
+      if (grub_json_getchild (&digest, &digests, j) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
 
       if ((d->keyslots & (1 << idx)))
 	break;
     }
   if (j == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, i);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
       grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-  for (j = 0; j < size; j++)
+  for (i = j, j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&segment, &segments, i) ||
+      if (grub_json_getchild (&segment, &segments, j) ||
 	  grub_json_getuint64 (&idx, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
 
       if ((d->segments & (1 << idx)))
 	break;
     }
   if (j == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE, i);
 
   return GRUB_ERR_NONE;
 }
Signature · 1 line
2.27.0
#0 lore raw

cover Cryptodisk fixes for v2.06 redux

This series is meant to address concerns Daniel had for patches 3, 4, 8, and 9 of Daniel's series with subject "[PATCH 0/9] Cryptodisk fixes for v2.06". These were patches authored by me and which Daniel handed back for me to take the lead in getting accepted. This series warrants a new thread because several patches were split and new patches have been added to addresses suggestions from Daniel.

The comments from Daniel can be seen in the following ML messages:
3: https://lists.gnu.org/archive/html/grub-devel/2020-09/msg00071.html
4: https://lists.gnu.org/archive/html/grub-devel/2020-09/msg00070.html
8: https://lists.gnu.org/archive/html/grub-devel/2020-09/msg00021.html
9: https://lists.gnu.org/archive/html/grub-devel/2020-09/msg00069.html

Patches 1 and 2 are equivalent to patch 3 of the previous series, which was split into patch 1 which fixed the indexing and error message bugs and patch 2 which improves readability of the code. Patch 3 is new and related to patches 1 and 2. It uses the keyslot number that cryptsetup understand when displaying the keyslot to the user.

Patch 4 has not changed from patch 4 of the previous series, except the adding of a line in the commit mesage mentioning the removal of the empty statement. Daniel's suggestion to rename total_length to total_sectors is implemented in patch 8. The other two suggestions, handling partial LUKS2 sectors and grub_strtoull() errors, I don't believe warrant action as I argued in more detail here:
https://lists.gnu.org/archive/html/grub-devel/2020-10/msg00020.html

Patch 5 has also not changed from the equivalent patch 8 of the previous series, but a more detailed commit message has been added per Daniel's suggestion.

Patch 6, previously patch 9, has been updated to change how the IV is updated for plain and plain64 modes. This change is functionally equivalent to the previous patch, but more easy to understand. A comment has been added to aid that end. Also, the commit message was changed to be less confusing.

Patch 7 comes out of a suggestion by Daniel in the previous patch 9. Some numerical literals are replaced with macros to compute those values. The appropriately named macros aid in understanding the code. Note: I have not functionally tested this as it affects mostly geli encrypted volumes which are on *BSD, which I do not use. The code compiles, and I expect it to work, but I would appreciate any *BSD geli users to verify that this still works for them.

Patches 8, 9, and 10 make no functional changes to the code. They solely rename some identifiers. Patch 8 as mentioned above, was suggested by Daniel. Patches 9 and 10 are in the same vein in that they aid in the understanding of the code by more accurately describing what the identifiers represent.

Glenn Washburn (10):
luks2: Fix use of incorrect index and some grub_error() messages.
luks2: Improve readability in luks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming
keyslot.
luks2: grub_cryptodisk_t->total_length is the max number of device
native sectors
cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
cryptodisk: Properly handle non-512 byte sized sectors.
cryptodisk: Replace some literals with constants in
grub_cryptodisk_endecrypt.
cryptodisk: Rename total_length field in grub_cryptodisk_t to
total_sectors.
cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as in

    luks.c.
 grub-core/disk/cryptodisk.c | 78 +++++++++++++++++++---------------
 grub-core/disk/geli.c       |  4 +-
 grub-core/disk/luks.c       |  9 ++--
 grub-core/disk/luks2.c      | 84 ++++++++++++++++++++-----------------
 include/grub/cryptodisk.h   | 18 ++++++--
 include/grub/types.h        |  3 ++
 6 files changed, 116 insertions(+), 80 deletions(-)
Signature · 1 line
2.27.0
#2 lore raw

PATCH 2/10 luks2: Improve readability in luks2_get_keyslot.

Introduce new variables keyslot_key, digest_key, and segment_key which represent the integer key of the item in the respective associative array when looping over the array items. This replaces using a generically named variable named "idx" used for all three values. The parameter "i" is renamed to "keyslot_idx" and the loop variable "j" is renamed to "i". Error messages now distinguish between indexes and keys. The former include "index" in the error string, and the later are surrounded in quotes.

---
 grub-core/disk/luks2.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
grub-core/disk/luks2.c +20 -19
index 2241e0312..c3cd63606 100644
@@ -255,54 +255,55 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
 static grub_err_t
 luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_segment_t *s,
-		   const grub_json_t *root, grub_size_t i)
+		   const grub_json_t *root, grub_size_t keyslot_idx)
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
-  grub_size_t j, size;
-  grub_uint64_t idx;
+  grub_size_t i, size;
+  grub_uint64_t keyslot_key, digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
-      grub_json_getchild (&keyslot, &keyslots, i) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
+      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
-    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, i);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
       grub_json_getsize (&size, &digests))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
-  for (j = 0; j < size; j++)
+  for (i = 0; i < size; i++)
     {
-      if (grub_json_getchild (&digest, &digests, j) ||
+      if (grub_json_getchild (&digest, &digests, i) ||
+	  grub_json_getuint64 (&digest_key, &digest, NULL) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
-      if ((d->keyslots & (1 << idx)))
+      if ((d->keyslots & (1 << keyslot_key)))
 	break;
     }
-  if (j == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, i);
+  if (i == size)
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
       grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-  for (i = j, j = 0; j < size; j++)
+  for (i = 0; i < size; i++)
     {
-      if (grub_json_getchild (&segment, &segments, j) ||
-	  grub_json_getuint64 (&idx, &segment, NULL) ||
+      if (grub_json_getchild (&segment, &segments, i) ||
+	  grub_json_getuint64 (&segment_key, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
-      if ((d->segments & (1 << idx)))
+      if ((d->segments & (1 << segment_key)))
 	break;
     }
-  if (j == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE, i);
+  if (i == size)
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", digest_key);
 
   return GRUB_ERR_NONE;
 }
Signature · 1 line
2.27.0
#3 lore raw

PATCH 3/10 luks2: Use more intuitive keyslot key instead of index when naming keyslot.

In reply to Glenn Washburn

Use the keyslot key value in the keyslot json array rather than the index of the keyslot in the json array. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore,with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

---
 grub-core/disk/luks2.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +12 -11
index c3cd63606..7917e02cd 100644
@@ -255,16 +255,16 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
 static grub_err_t
 luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_segment_t *s,
-		   const grub_json_t *root, grub_size_t keyslot_idx)
+		   grub_uint64_t *keyslot_key, const grub_json_t *root, grub_size_t keyslot_idx)
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t keyslot_key, digest_key, segment_key;
+  grub_uint64_t digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
+      grub_json_getuint64 (keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
@@ -281,11 +281,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
           luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
-      if ((d->keyslots & (1 << keyslot_key)))
+      if ((d->keyslots & (1 << *keyslot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", *keyslot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -593,17 +593,18 @@ luks2_recover_key (grub_disk_t disk,
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
-      ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
+      grub_uint64_t keyslot_key;
+      ret = luks2_get_keyslot (&keyslot, &digest, &segment, &keyslot_key, json, i);
       if (ret)
 	goto err;
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", keyslot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", keyslot_key);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -619,7 +620,7 @@ luks2_recover_key (grub_disk_t disk,
       if (ret)
 	{
 	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+			keyslot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -627,7 +628,7 @@ luks2_recover_key (grub_disk_t disk,
       if (ret)
 	{
 	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+			keyslot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -635,7 +636,7 @@ luks2_recover_key (grub_disk_t disk,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), keyslot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
#5 lore raw

PATCH 5/10 cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.

In reply to Glenn Washburn

When setting cipher IV mode, detection is done by prefix matching the cipher IV mode part of the cipher mode string. Since "plain" matches "plain64", we must check for "plain64" first. Otherwise, "plain64" will be detected as "plain".

---
 grub-core/disk/cryptodisk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/disk/cryptodisk.c +2 -2
index 13af84dd1..a3d672f68 100644
@@ -501,10 +501,10 @@ grub_cryptodisk_setcipher (grub_cryptodisk_t crypt, const char *ciphername, cons
 
   if (cipheriv == NULL)
       ;
-  else if (grub_memcmp (cipheriv, "plain", sizeof ("plain") - 1) == 0)
-      mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN;
   else if (grub_memcmp (cipheriv, "plain64", sizeof ("plain64") - 1) == 0)
       mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN64;
+  else if (grub_memcmp (cipheriv, "plain", sizeof ("plain") - 1) == 0)
+      mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN;
   else if (grub_memcmp (cipheriv, "benbi", sizeof ("benbi") - 1) == 0)
     {
       if (cipher->cipher->blocksize & (cipher->cipher->blocksize - 1)
Signature · 1 line
2.27.0
#4 lore raw

PATCH 4/10 luks2: grub_cryptodisk_t->total_length is the max number of device native sectors

In reply to Glenn Washburn

The total_length field is named confusingly because length usually refers to bytes, whereas in this case its really the total number of sectors on the device. Also counter-intuitively, grub_disk_get_size returns the total number of device native sectors sectors. We need to convert the sectors from the size of the underlying device to the cryptodisk sector size. And segment.size is in bytes which need to be converted to cryptodisk sectors.

Also, removed an empty statement.

---
 grub-core/disk/luks2.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +4 -3
index 7917e02cd..8e99a1820 100644
@@ -424,7 +424,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
   grub_uint8_t salt[GRUB_CRYPTODISK_MAX_KEYLEN];
   grub_uint8_t *split_key = NULL;
   grub_size_t saltlen = sizeof (salt);
-  char cipher[32], *p;;
+  char cipher[32], *p;
   const gcry_md_spec_t *hash;
   gcry_err_code_t gcry_ret;
   grub_err_t ret;
@@ -611,9 +611,10 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_length = grub_disk_get_size (disk) - crypt->offset;
+	crypt->total_length = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+			       - crypt->offset;
       else
-	crypt->total_length = grub_strtoull (segment.size, NULL, 10);
+	crypt->total_length = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
Signature · 1 line
2.27.0
#6 lore raw

PATCH 6/10 cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 52 ++++++++++++++++++++++---------------
 grub-core/disk/luks.c       |  5 ++--
 grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
grub-core/disk/cryptodisk.c +31 -21
index a3d672f68..623f0f396 100644
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
     return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len)
 	    : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));
 
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector << dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len);
 	    dev->iv_hash->write (ctx, &tmp, sizeof (tmp));
@@ -281,14 +282,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native sector
+	   * number. If using 32 bit IV mode, zero out the most significant
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;
+	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 - GRUB_CRYPTODISK_IV_LOG_SIZE));
+	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
+	      iv[1] = 0;
+	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
-	  iv[1] = grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size));
-	  iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
+	  iv[1] = grub_cpu_to_le32 (sector >> (32 - log_sector_size));
+	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
 				    & 0xFFFFFFFF);
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
@@ -311,10 +321,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_CBC:
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_cbc_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -322,10 +332,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_PCBC:
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_pcbc_decrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -337,7 +347,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (err)
 	      return err;
 	    
-	    for (j = 0; j < (1U << dev->log_sector_size);
+	    for (j = 0; j < (1U << log_sector_size);
 		 j += dev->cipher->cipher->blocksize)
 	      {
 		grub_crypto_xor (data + i + j, data + i + j, iv,
@@ -368,11 +378,11 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (do_encrypt)
 	      err = grub_crypto_ecb_encrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    else
 	      err = grub_crypto_ecb_decrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    if (err)
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -381,10 +391,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_ECB:
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  else
 	    err = grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  if (err)
 	    return err;
 	  break;
@@ -399,9 +409,9 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector)
+			 grub_disk_addr_t sector, grub_size_t log_sector_size)
 {
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector, log_sector_size, 0);
 }
 
 grub_err_t
@@ -766,7 +776,7 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
     }
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
 					size << disk->log_sector_size,
-					sector, 0);
+					sector, dev->log_sector_size, 0);
   return grub_crypto_gcry_error (gcry_err);
 }
 
@@ -807,7 +817,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
-					sector, 1);
+					sector, disk->log_sector_size, 1);
   if (gcry_err)
     {
       grub_free (tmp);
grub-core/disk/luks.c +3 -2
index 59702067a..20cc20b9b 100644
@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
-  newdev->log_sector_size = 9;
+  newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
   newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,
 	  return err;
 	}
 
-      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
+      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0,
+					  LUKS1_LOG_SECTOR_SIZE);
       if (gcry_err)
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 8e99a1820..054d5455d 100644
@@ -499,7 +499,12 @@ luks2_decrypt_key (grub_uint8_t *out_key,
       goto err;
     }
 
-  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0);
+  /*
+   * The key slots area is always encrypted in 512-byte sectors,
+   * regardless of encrypted data sector size.
+   */
+  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0,
+				      LUKS1_LOG_SECTOR_SIZE);
   if (gcry_ret)
     {
       ret = grub_crypto_gcry_error (gcry_ret);
include/grub/cryptodisk.h +7 -1
index e1b21e785..006f3ec49 100644
@@ -48,6 +48,12 @@ typedef enum
 
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
+/* LUKS1 specification defines the block size to always be 512 bytes. */
+#define LUKS1_LOG_SECTOR_SIZE 9
+
+/* By default dm-crypt increments the IV every 512 bytes. */
+#define GRUB_CRYPTODISK_IV_LOG_SIZE 9
+
 #define GRUB_CRYPTODISK_GF_LOG_SIZE 7
 #define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)
 #define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE - 3)
@@ -139,7 +145,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector);
+			 grub_disk_addr_t sector, grub_size_t log_sector_size);
 grub_err_t
 grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
 			grub_disk_t source);
Signature · 1 line
2.27.0
#7 lore raw

PATCH 7/10 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Glenn Washburn

This should improve readability of code by providing clues as to what the value represents.

---
 grub-core/disk/cryptodisk.c | 12 +++++++-----
 include/grub/types.h        |  3 +++
 2 files changed, 10 insertions(+), 5 deletions(-)
grub-core/disk/cryptodisk.c +7 -5
index 623f0f396..1a91c2d55 100644
@@ -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)
include/grub/types.h +3 -0
index 035a4b528..8b4267ebd 100644
@@ -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 */
Signature · 1 line
2.27.0
#8 lore raw

PATCH 8/10 cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.

In reply to Glenn Washburn

This makes the creates an alignment with grub_disk_t naming of the same field and is more intuitive as to how it should be used.

---
 grub-core/disk/cryptodisk.c | 2 +-
 grub-core/disk/geli.c       | 2 +-
 grub-core/disk/luks.c       | 2 +-
 grub-core/disk/luks2.c      | 4 ++--
 include/grub/cryptodisk.h   | 3 ++-
 5 files changed, 7 insertions(+), 6 deletions(-)
grub-core/disk/cryptodisk.c +1 -1
index 1a91c2d55..f5d098f65 100644
@@ -711,7 +711,7 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk)
     }
 
   disk->data = dev;
-  disk->total_sectors = dev->total_length;
+  disk->total_sectors = dev->total_sectors;
   disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
   disk->id = dev->id;
   dev->ref++;
grub-core/disk/geli.c +1 -1
index e9d23299a..4ec875821 100644
@@ -391,7 +391,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
 
   newdev->modname = "geli";
 
-  newdev->total_length = grub_disk_get_size (disk) - 1;
+  newdev->total_sectors = grub_disk_get_size (disk) - 1;
   grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
   COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= 32 * 2 + 1);
   return newdev;
grub-core/disk/luks.c +1 -1
index 20cc20b9b..545734342 100644
@@ -125,7 +125,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
-  newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index 054d5455d..995b413cd 100644
@@ -616,10 +616,10 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_length = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
 			       - crypt->offset;
       else
-	crypt->total_length = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
include/grub/cryptodisk.h +2 -1
index 006f3ec49..9e10ae927 100644
@@ -73,7 +73,8 @@ struct grub_cryptodisk
 
   char *source;
   grub_disk_addr_t offset;
-  grub_disk_addr_t total_length;
+  /* Total number of encrypted sectors of size (1<<log_sector_size) */
+  grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
   int ref;
   grub_crypto_cipher_handle_t cipher;
Signature · 1 line
2.27.0
#9 lore raw

PATCH 9/10 cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.

In reply to Glenn Washburn

This makes it clear that the offset represents sectors, not bytes, in order to improve readability.

---
 grub-core/disk/cryptodisk.c | 10 +++++-----
 grub-core/disk/geli.c       |  2 +-
 grub-core/disk/luks.c       |  4 ++--
 grub-core/disk/luks2.c      |  4 ++--
 include/grub/cryptodisk.h   |  7 ++++++-
 5 files changed, 16 insertions(+), 11 deletions(-)
grub-core/disk/cryptodisk.c +5 -5
index f5d098f65..64a51165c 100644
@@ -766,10 +766,10 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Reading %" PRIuGRUB_SIZE " sectors from sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   err = grub_disk_read (dev->source_disk,
-			grub_disk_from_native_sector (disk, sector + dev->offset),
+			grub_disk_from_native_sector (disk, sector + dev->offset_sectors),
 			0, size << disk->log_sector_size, buf);
   if (err)
     {
@@ -815,7 +815,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Writing %" PRIuGRUB_SIZE " sectors to sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
@@ -827,7 +827,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
     }
 
   /* Since ->write was called so disk.mod is loaded but be paranoid  */
-  sector = sector + dev->offset;
+  sector = sector + dev->offset_sectors;
   if (grub_disk_write_weak)
     err = grub_disk_write_weak (dev->source_disk,
 				grub_disk_from_native_sector (disk, sector),
@@ -1240,7 +1240,7 @@ luks_script_get (grub_size_t *sz)
 	ptr = grub_stpcpy (ptr, "luks_mount ");
 	ptr = grub_stpcpy (ptr, i->uuid);
 	*ptr++ = ' ';
-	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset);
+	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset_sectors);
 	while (*ptr)
 	  ptr++;
 	for (iptr = i->cipher->cipher->name; *iptr; iptr++)
grub-core/disk/geli.c +1 -1
index 4ec875821..0175ce4c4 100644
@@ -361,7 +361,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
     }
   newdev->cipher = cipher;
   newdev->secondary_cipher = secondary_cipher;
-  newdev->offset = 0;
+  newdev->offset_sectors = 0;
   newdev->source_disk = NULL;
   newdev->benbi_log = 0;
   if (grub_le_to_cpu16 (header.alg) == 0x16)
grub-core/disk/luks.c +2 -2
index 545734342..88f2da493 100644
@@ -122,10 +122,10 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev = grub_zalloc (sizeof (struct grub_cryptodisk));
   if (!newdev)
       return NULL;
-  newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
+  newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
-  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index 995b413cd..01474d60c 100644
@@ -612,12 +612,12 @@ luks2_recover_key (grub_disk_t disk,
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", keyslot_key);
 
       /* Set up disk according to keyslot's segment. */
-      crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
+      crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
 	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
-			       - crypt->offset;
+			       - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
include/grub/cryptodisk.h +6 -1
index 9e10ae927..c8eef2204 100644
@@ -72,7 +72,12 @@ struct grub_cryptodisk
   struct grub_cryptodisk **prev;
 
   char *source;
-  grub_disk_addr_t offset;
+
+  /*
+   * The number of sectors the start of the encrypted data is offset into the
+   * underlying disk, where sectors are the size noted by log_sector_size.
+   */
+  grub_disk_addr_t offset_sectors;
   /* Total number of encrypted sectors of size (1<<log_sector_size) */
   grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
Signature · 1 line
2.27.0
#10 lore raw

PATCH 10/10 luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

In reply to Glenn Washburn

This makes it more obvious to the reader that the disk referred to is the source disk, as opposed to say the disk holding the cryptodisk.

---
 grub-core/disk/luks2.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +11 -11
index 01474d60c..c7f2f3405 100644
@@ -416,7 +416,7 @@ luks2_verify_key (grub_luks2_digest_t *d, grub_uint8_t *candidate_key,
 
 static grub_err_t
 luks2_decrypt_key (grub_uint8_t *out_key,
-		   grub_disk_t disk, grub_cryptodisk_t crypt,
+		   grub_disk_t source, grub_cryptodisk_t crypt,
 		   grub_luks2_keyslot_t *k,
 		   const grub_uint8_t *passphrase, grub_size_t passphraselen)
 {
@@ -492,7 +492,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
     }
 
   grub_errno = GRUB_ERR_NONE;
-  ret = grub_disk_read (disk, 0, k->area.offset, k->area.size, split_key);
+  ret = grub_disk_read (source, 0, k->area.offset, k->area.size, split_key);
   if (ret)
     {
       grub_error (GRUB_ERR_IO, "Read error: %s\n", grub_errmsg);
@@ -536,7 +536,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
 }
 
 static grub_err_t
-luks2_recover_key (grub_disk_t disk,
+luks2_recover_key (grub_disk_t source,
 		   grub_cryptodisk_t crypt)
 {
   grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
@@ -551,7 +551,7 @@ luks2_recover_key (grub_disk_t disk,
   grub_json_t *json = NULL, keyslots;
   grub_err_t ret;
 
-  ret = luks2_read_header (disk, &header);
+  ret = luks2_read_header (source, &header);
   if (ret)
     return ret;
 
@@ -560,7 +560,7 @@ luks2_recover_key (grub_disk_t disk,
       return GRUB_ERR_OUT_OF_MEMORY;
 
   /* Read the JSON area. */
-  ret = grub_disk_read (disk, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
+  ret = grub_disk_read (source, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
 			grub_be_to_cpu64 (header.hdr_size) - sizeof (header), json_header);
   if (ret)
       goto err;
@@ -577,10 +577,10 @@ luks2_recover_key (grub_disk_t disk,
     }
 
   /* Get the passphrase from the user. */
-  if (disk->partition)
-    part = grub_partition_get_name (disk->partition);
-  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), disk->name,
-		disk->partition ? "," : "", part ? : "",
+  if (source->partition)
+    part = grub_partition_get_name (source->partition);
+  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
+		source->partition ? "," : "", part ? : "",
 		crypt->uuid);
   if (!grub_password_get (passphrase, MAX_PASSPHRASE))
     {
@@ -616,12 +616,12 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
 			       - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
-      ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
+      ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
Signature · 1 line
2.27.0
#12 lore raw

PATCH v2 1/10 luks2: Fix use of incorrect index and some grub_error() messages.

In reply to Glenn Washburn

When looping over the digests and segments, the loop variable is j, but the variable i is used to index in the the digests and segments json array. The variable i is the keyslot index. Similarly, there are several grub_error() statements using the wrong index in constructing the error string.

---
 grub-core/disk/luks2.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
grub-core/disk/luks2.c +7 -7
index 31d7166fc..2241e0312 100644
@@ -275,34 +275,34 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
   for (j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&digest, &digests, i) ||
+      if (grub_json_getchild (&digest, &digests, j) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
 
       if ((d->keyslots & (1 << idx)))
 	break;
     }
   if (j == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, i);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
       grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-  for (j = 0; j < size; j++)
+  for (i = j, j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&segment, &segments, i) ||
+      if (grub_json_getchild (&segment, &segments, j) ||
 	  grub_json_getuint64 (&idx, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
 
       if ((d->segments & (1 << idx)))
 	break;
     }
   if (j == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE, i);
 
   return GRUB_ERR_NONE;
 }
Signature · 1 line
2.27.0
#11 lore raw

v2 cover Cryptodisk fixes for v2.06 redux

In reply to Glenn Washburn

This is a minor update to fix patch 3, where I missed updating the format string type code. This was causing i386 builds to fail. Rangediff is included.

Glenn Washburn (10):
luks2: Fix use of incorrect index and some grub_error() messages.
luks2: Improve readability in luks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming
keyslot.
luks2: grub_cryptodisk_t->total_length is the max number of device
native sectors
cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
cryptodisk: Properly handle non-512 byte sized sectors.
cryptodisk: Replace some literals with constants in
grub_cryptodisk_endecrypt.
cryptodisk: Rename total_length field in grub_cryptodisk_t to
total_sectors.
cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as in

    luks.c.
 grub-core/disk/cryptodisk.c | 78 ++++++++++++++++++--------------
 grub-core/disk/geli.c       |  4 +-
 grub-core/disk/luks.c       |  9 ++--
 grub-core/disk/luks2.c      | 88 ++++++++++++++++++++-----------------
 include/grub/cryptodisk.h   | 18 ++++++--
 include/grub/types.h        |  3 ++
 6 files changed, 118 insertions(+), 82 deletions(-)

Range-diff against v1:
1: 00646c92f ! 1: e2433b8ab luks2: Use more intuitive keyslot key instead of index when naming keyslot.

    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
            if (keyslot.priority == 0)
      	{
     -	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
    -+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", keyslot_key);
    ++	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot_key);
      	  continue;
              }
      
     -      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
    -+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", keyslot_key);
    ++      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot_key);
      
            /* Set up disk according to keyslot's segment. */
            crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
     @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
    + 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
            if (ret)
      	{
    - 	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
    +-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
     -			i, grub_errmsg);
    ++	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
     +			keyslot_key, grub_errmsg);
      	  continue;
      	}
      
    -@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
    +       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
            if (ret)
      	{
    - 	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
    +-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
     -			i, grub_errmsg);
    ++	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
     +			keyslot_key, grub_errmsg);
      	  continue;
      	}
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
             * where each element is either empty or holds a key.
             */
     -      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
    -+      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), keyslot_key);
    ++      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot_key);
      
            candidate_key_len = keyslot.key_size;
            break;
2:  137909929 = 2:  3baffdd4f luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
3:  9cfbb3373 = 3:  6da3d8598 cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
4:  5f7bd00a6 = 4:  fd7cb6b16 cryptodisk: Properly handle non-512 byte sized sectors.
5:  4251c828a = 5:  b33733199 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
6:  feb8298b8 = 6:  de4f6b2e5 cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
7:  e26aed2ee ! 7:  a165791de cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.

@@ grub-core/disk/luks.c: configure_ciphers (grub_disk_t disk, const char *check_uu

## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
- grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", keyslot_key);
+ grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot_key);

/* Set up disk according to keyslot's segment. */
- crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
8: e083b25e2 = 8: 86beb5be8 luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

Signature · 1 line
2.27.0
#14 lore raw

PATCH v2 2/10 luks2: Improve readability in luks2_get_keyslot.

Introduce new variables keyslot_key, digest_key, and segment_key which represent the integer key of the item in the respective associative array when looping over the array items. This replaces using a generically named variable named "idx" used for all three values. The parameter "i" is renamed to "keyslot_idx" and the loop variable "j" is renamed to "i". Error messages now distinguish between indexes and keys. The former include "index" in the error string, and the later are surrounded in quotes.

---
 grub-core/disk/luks2.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
grub-core/disk/luks2.c +20 -19
index 2241e0312..c3cd63606 100644
@@ -255,54 +255,55 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
 static grub_err_t
 luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_segment_t *s,
-		   const grub_json_t *root, grub_size_t i)
+		   const grub_json_t *root, grub_size_t keyslot_idx)
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
-  grub_size_t j, size;
-  grub_uint64_t idx;
+  grub_size_t i, size;
+  grub_uint64_t keyslot_key, digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
-      grub_json_getchild (&keyslot, &keyslots, i) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
+      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
-    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, i);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
       grub_json_getsize (&size, &digests))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
-  for (j = 0; j < size; j++)
+  for (i = 0; i < size; i++)
     {
-      if (grub_json_getchild (&digest, &digests, j) ||
+      if (grub_json_getchild (&digest, &digests, i) ||
+	  grub_json_getuint64 (&digest_key, &digest, NULL) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
-      if ((d->keyslots & (1 << idx)))
+      if ((d->keyslots & (1 << keyslot_key)))
 	break;
     }
-  if (j == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, i);
+  if (i == size)
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
       grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-  for (i = j, j = 0; j < size; j++)
+  for (i = 0; i < size; i++)
     {
-      if (grub_json_getchild (&segment, &segments, j) ||
-	  grub_json_getuint64 (&idx, &segment, NULL) ||
+      if (grub_json_getchild (&segment, &segments, i) ||
+	  grub_json_getuint64 (&segment_key, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
-      if ((d->segments & (1 << idx)))
+      if ((d->segments & (1 << segment_key)))
 	break;
     }
-  if (j == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE, i);
+  if (i == size)
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", digest_key);
 
   return GRUB_ERR_NONE;
 }
Signature · 1 line
2.27.0
#15 lore raw

PATCH v2 3/10 luks2: Use more intuitive keyslot key instead of index when naming keyslot.

In reply to Glenn Washburn

Use the keyslot key value in the keyslot json array rather than the index of the keyslot in the json array. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore,with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

---
 grub-core/disk/luks2.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
grub-core/disk/luks2.c +14 -13
index c3cd63606..db251cce0 100644
@@ -255,16 +255,16 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
 static grub_err_t
 luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_segment_t *s,
-		   const grub_json_t *root, grub_size_t keyslot_idx)
+		   grub_uint64_t *keyslot_key, const grub_json_t *root, grub_size_t keyslot_idx)
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t keyslot_key, digest_key, segment_key;
+  grub_uint64_t digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
+      grub_json_getuint64 (keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
@@ -281,11 +281,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
           luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
-      if ((d->keyslots & (1 << keyslot_key)))
+      if ((d->keyslots & (1 << *keyslot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", *keyslot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -593,17 +593,18 @@ luks2_recover_key (grub_disk_t disk,
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
-      ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
+      grub_uint64_t keyslot_key;
+      ret = luks2_get_keyslot (&keyslot, &digest, &segment, &keyslot_key, json, i);
       if (ret)
 	goto err;
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot_key);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -618,16 +619,16 @@ luks2_recover_key (grub_disk_t disk,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
+			keyslot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
+			keyslot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -635,7 +636,7 @@ luks2_recover_key (grub_disk_t disk,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
#20 lore raw

PATCH v2 5/10 cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.

In reply to Glenn Washburn

When setting cipher IV mode, detection is done by prefix matching the cipher IV mode part of the cipher mode string. Since "plain" matches "plain64", we must check for "plain64" first. Otherwise, "plain64" will be detected as "plain".

---
 grub-core/disk/cryptodisk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/disk/cryptodisk.c +2 -2
index 13af84dd1..a3d672f68 100644
@@ -501,10 +501,10 @@ grub_cryptodisk_setcipher (grub_cryptodisk_t crypt, const char *ciphername, cons
 
   if (cipheriv == NULL)
       ;
-  else if (grub_memcmp (cipheriv, "plain", sizeof ("plain") - 1) == 0)
-      mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN;
   else if (grub_memcmp (cipheriv, "plain64", sizeof ("plain64") - 1) == 0)
       mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN64;
+  else if (grub_memcmp (cipheriv, "plain", sizeof ("plain") - 1) == 0)
+      mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN;
   else if (grub_memcmp (cipheriv, "benbi", sizeof ("benbi") - 1) == 0)
     {
       if (cipher->cipher->blocksize & (cipher->cipher->blocksize - 1)
Signature · 1 line
2.27.0
#18 lore raw

PATCH v2 4/10 luks2: grub_cryptodisk_t->total_length is the max number of device native sectors

In reply to Glenn Washburn

The total_length field is named confusingly because length usually refers to bytes, whereas in this case its really the total number of sectors on the device. Also counter-intuitively, grub_disk_get_size returns the total number of device native sectors sectors. We need to convert the sectors from the size of the underlying device to the cryptodisk sector size. And segment.size is in bytes which need to be converted to cryptodisk sectors.

Also, removed an empty statement.

---
 grub-core/disk/luks2.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +4 -3
index db251cce0..9f7d6e12b 100644
@@ -424,7 +424,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
   grub_uint8_t salt[GRUB_CRYPTODISK_MAX_KEYLEN];
   grub_uint8_t *split_key = NULL;
   grub_size_t saltlen = sizeof (salt);
-  char cipher[32], *p;;
+  char cipher[32], *p;
   const gcry_md_spec_t *hash;
   gcry_err_code_t gcry_ret;
   grub_err_t ret;
@@ -611,9 +611,10 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_length = grub_disk_get_size (disk) - crypt->offset;
+	crypt->total_length = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+			       - crypt->offset;
       else
-	crypt->total_length = grub_strtoull (segment.size, NULL, 10);
+	crypt->total_length = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
Signature · 1 line
2.27.0
#21 lore raw

PATCH v2 6/10 cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 52 ++++++++++++++++++++++---------------
 grub-core/disk/luks.c       |  5 ++--
 grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
grub-core/disk/cryptodisk.c +31 -21
index a3d672f68..623f0f396 100644
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
     return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len)
 	    : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));
 
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector << dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len);
 	    dev->iv_hash->write (ctx, &tmp, sizeof (tmp));
@@ -281,14 +282,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native sector
+	   * number. If using 32 bit IV mode, zero out the most significant
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;
+	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 - GRUB_CRYPTODISK_IV_LOG_SIZE));
+	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
+	      iv[1] = 0;
+	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
-	  iv[1] = grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size));
-	  iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
+	  iv[1] = grub_cpu_to_le32 (sector >> (32 - log_sector_size));
+	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
 				    & 0xFFFFFFFF);
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
@@ -311,10 +321,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_CBC:
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_cbc_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -322,10 +332,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_PCBC:
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_pcbc_decrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -337,7 +347,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (err)
 	      return err;
 	    
-	    for (j = 0; j < (1U << dev->log_sector_size);
+	    for (j = 0; j < (1U << log_sector_size);
 		 j += dev->cipher->cipher->blocksize)
 	      {
 		grub_crypto_xor (data + i + j, data + i + j, iv,
@@ -368,11 +378,11 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (do_encrypt)
 	      err = grub_crypto_ecb_encrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    else
 	      err = grub_crypto_ecb_decrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    if (err)
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -381,10 +391,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_ECB:
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  else
 	    err = grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  if (err)
 	    return err;
 	  break;
@@ -399,9 +409,9 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector)
+			 grub_disk_addr_t sector, grub_size_t log_sector_size)
 {
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector, log_sector_size, 0);
 }
 
 grub_err_t
@@ -766,7 +776,7 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
     }
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
 					size << disk->log_sector_size,
-					sector, 0);
+					sector, dev->log_sector_size, 0);
   return grub_crypto_gcry_error (gcry_err);
 }
 
@@ -807,7 +817,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
-					sector, 1);
+					sector, disk->log_sector_size, 1);
   if (gcry_err)
     {
       grub_free (tmp);
grub-core/disk/luks.c +3 -2
index 59702067a..20cc20b9b 100644
@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
-  newdev->log_sector_size = 9;
+  newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
   newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,
 	  return err;
 	}
 
-      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
+      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0,
+					  LUKS1_LOG_SECTOR_SIZE);
       if (gcry_err)
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 9f7d6e12b..b7d3b425a 100644
@@ -499,7 +499,12 @@ luks2_decrypt_key (grub_uint8_t *out_key,
       goto err;
     }
 
-  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0);
+  /*
+   * The key slots area is always encrypted in 512-byte sectors,
+   * regardless of encrypted data sector size.
+   */
+  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0,
+				      LUKS1_LOG_SECTOR_SIZE);
   if (gcry_ret)
     {
       ret = grub_crypto_gcry_error (gcry_ret);
include/grub/cryptodisk.h +7 -1
index e1b21e785..006f3ec49 100644
@@ -48,6 +48,12 @@ typedef enum
 
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
+/* LUKS1 specification defines the block size to always be 512 bytes. */
+#define LUKS1_LOG_SECTOR_SIZE 9
+
+/* By default dm-crypt increments the IV every 512 bytes. */
+#define GRUB_CRYPTODISK_IV_LOG_SIZE 9
+
 #define GRUB_CRYPTODISK_GF_LOG_SIZE 7
 #define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)
 #define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE - 3)
@@ -139,7 +145,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector);
+			 grub_disk_addr_t sector, grub_size_t log_sector_size);
 grub_err_t
 grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
 			grub_disk_t source);
Signature · 1 line
2.27.0
#27 lore raw

PATCH v2 8/10 cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.

In reply to Glenn Washburn

This makes the creates an alignment with grub_disk_t naming of the same field and is more intuitive as to how it should be used.

---
 grub-core/disk/cryptodisk.c | 2 +-
 grub-core/disk/geli.c       | 2 +-
 grub-core/disk/luks.c       | 2 +-
 grub-core/disk/luks2.c      | 4 ++--
 include/grub/cryptodisk.h   | 3 ++-
 5 files changed, 7 insertions(+), 6 deletions(-)
grub-core/disk/cryptodisk.c +1 -1
index 1a91c2d55..f5d098f65 100644
@@ -711,7 +711,7 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk)
     }
 
   disk->data = dev;
-  disk->total_sectors = dev->total_length;
+  disk->total_sectors = dev->total_sectors;
   disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
   disk->id = dev->id;
   dev->ref++;
grub-core/disk/geli.c +1 -1
index e9d23299a..4ec875821 100644
@@ -391,7 +391,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
 
   newdev->modname = "geli";
 
-  newdev->total_length = grub_disk_get_size (disk) - 1;
+  newdev->total_sectors = grub_disk_get_size (disk) - 1;
   grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
   COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= 32 * 2 + 1);
   return newdev;
grub-core/disk/luks.c +1 -1
index 20cc20b9b..545734342 100644
@@ -125,7 +125,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
-  newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index b7d3b425a..2e295cac8 100644
@@ -616,10 +616,10 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_length = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
 			       - crypt->offset;
       else
-	crypt->total_length = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
include/grub/cryptodisk.h +2 -1
index 006f3ec49..9e10ae927 100644
@@ -73,7 +73,8 @@ struct grub_cryptodisk
 
   char *source;
   grub_disk_addr_t offset;
-  grub_disk_addr_t total_length;
+  /* Total number of encrypted sectors of size (1<<log_sector_size) */
+  grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
   int ref;
   grub_crypto_cipher_handle_t cipher;
Signature · 1 line
2.27.0
#24 lore raw

PATCH v2 7/10 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Glenn Washburn

This should improve readability of code by providing clues as to what the value represents.

---
 grub-core/disk/cryptodisk.c | 12 +++++++-----
 include/grub/types.h        |  3 +++
 2 files changed, 10 insertions(+), 5 deletions(-)
grub-core/disk/cryptodisk.c +7 -5
index 623f0f396..1a91c2d55 100644
@@ -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)
include/grub/types.h +3 -0
index 035a4b528..8b4267ebd 100644
@@ -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 */
Signature · 1 line
2.27.0
#29 lore raw

PATCH v2 9/10 cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.

In reply to Glenn Washburn

This makes it clear that the offset represents sectors, not bytes, in order to improve readability.

---
 grub-core/disk/cryptodisk.c | 10 +++++-----
 grub-core/disk/geli.c       |  2 +-
 grub-core/disk/luks.c       |  4 ++--
 grub-core/disk/luks2.c      |  4 ++--
 include/grub/cryptodisk.h   |  7 ++++++-
 5 files changed, 16 insertions(+), 11 deletions(-)
grub-core/disk/cryptodisk.c +5 -5
index f5d098f65..64a51165c 100644
@@ -766,10 +766,10 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Reading %" PRIuGRUB_SIZE " sectors from sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   err = grub_disk_read (dev->source_disk,
-			grub_disk_from_native_sector (disk, sector + dev->offset),
+			grub_disk_from_native_sector (disk, sector + dev->offset_sectors),
 			0, size << disk->log_sector_size, buf);
   if (err)
     {
@@ -815,7 +815,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Writing %" PRIuGRUB_SIZE " sectors to sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
@@ -827,7 +827,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
     }
 
   /* Since ->write was called so disk.mod is loaded but be paranoid  */
-  sector = sector + dev->offset;
+  sector = sector + dev->offset_sectors;
   if (grub_disk_write_weak)
     err = grub_disk_write_weak (dev->source_disk,
 				grub_disk_from_native_sector (disk, sector),
@@ -1240,7 +1240,7 @@ luks_script_get (grub_size_t *sz)
 	ptr = grub_stpcpy (ptr, "luks_mount ");
 	ptr = grub_stpcpy (ptr, i->uuid);
 	*ptr++ = ' ';
-	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset);
+	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset_sectors);
 	while (*ptr)
 	  ptr++;
 	for (iptr = i->cipher->cipher->name; *iptr; iptr++)
grub-core/disk/geli.c +1 -1
index 4ec875821..0175ce4c4 100644
@@ -361,7 +361,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
     }
   newdev->cipher = cipher;
   newdev->secondary_cipher = secondary_cipher;
-  newdev->offset = 0;
+  newdev->offset_sectors = 0;
   newdev->source_disk = NULL;
   newdev->benbi_log = 0;
   if (grub_le_to_cpu16 (header.alg) == 0x16)
grub-core/disk/luks.c +2 -2
index 545734342..88f2da493 100644
@@ -122,10 +122,10 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev = grub_zalloc (sizeof (struct grub_cryptodisk));
   if (!newdev)
       return NULL;
-  newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
+  newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
-  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index 2e295cac8..46abc96ef 100644
@@ -612,12 +612,12 @@ luks2_recover_key (grub_disk_t disk,
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot_key);
 
       /* Set up disk according to keyslot's segment. */
-      crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
+      crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
 	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
-			       - crypt->offset;
+			       - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
include/grub/cryptodisk.h +6 -1
index 9e10ae927..c8eef2204 100644
@@ -72,7 +72,12 @@ struct grub_cryptodisk
   struct grub_cryptodisk **prev;
 
   char *source;
-  grub_disk_addr_t offset;
+
+  /*
+   * The number of sectors the start of the encrypted data is offset into the
+   * underlying disk, where sectors are the size noted by log_sector_size.
+   */
+  grub_disk_addr_t offset_sectors;
   /* Total number of encrypted sectors of size (1<<log_sector_size) */
   grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
Signature · 1 line
2.27.0
#31 lore raw

PATCH v2 10/10 luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

In reply to Glenn Washburn

This makes it more obvious to the reader that the disk referred to is the source disk, as opposed to say the disk holding the cryptodisk.

---
 grub-core/disk/luks2.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +11 -11
index 46abc96ef..f918f5a52 100644
@@ -416,7 +416,7 @@ luks2_verify_key (grub_luks2_digest_t *d, grub_uint8_t *candidate_key,
 
 static grub_err_t
 luks2_decrypt_key (grub_uint8_t *out_key,
-		   grub_disk_t disk, grub_cryptodisk_t crypt,
+		   grub_disk_t source, grub_cryptodisk_t crypt,
 		   grub_luks2_keyslot_t *k,
 		   const grub_uint8_t *passphrase, grub_size_t passphraselen)
 {
@@ -492,7 +492,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
     }
 
   grub_errno = GRUB_ERR_NONE;
-  ret = grub_disk_read (disk, 0, k->area.offset, k->area.size, split_key);
+  ret = grub_disk_read (source, 0, k->area.offset, k->area.size, split_key);
   if (ret)
     {
       grub_error (GRUB_ERR_IO, "Read error: %s\n", grub_errmsg);
@@ -536,7 +536,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
 }
 
 static grub_err_t
-luks2_recover_key (grub_disk_t disk,
+luks2_recover_key (grub_disk_t source,
 		   grub_cryptodisk_t crypt)
 {
   grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
@@ -551,7 +551,7 @@ luks2_recover_key (grub_disk_t disk,
   grub_json_t *json = NULL, keyslots;
   grub_err_t ret;
 
-  ret = luks2_read_header (disk, &header);
+  ret = luks2_read_header (source, &header);
   if (ret)
     return ret;
 
@@ -560,7 +560,7 @@ luks2_recover_key (grub_disk_t disk,
       return GRUB_ERR_OUT_OF_MEMORY;
 
   /* Read the JSON area. */
-  ret = grub_disk_read (disk, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
+  ret = grub_disk_read (source, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
 			grub_be_to_cpu64 (header.hdr_size) - sizeof (header), json_header);
   if (ret)
       goto err;
@@ -577,10 +577,10 @@ luks2_recover_key (grub_disk_t disk,
     }
 
   /* Get the passphrase from the user. */
-  if (disk->partition)
-    part = grub_partition_get_name (disk->partition);
-  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), disk->name,
-		disk->partition ? "," : "", part ? : "",
+  if (source->partition)
+    part = grub_partition_get_name (source->partition);
+  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
+		source->partition ? "," : "", part ? : "",
 		crypt->uuid);
   if (!grub_password_get (passphrase, MAX_PASSPHRASE))
     {
@@ -616,12 +616,12 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
 			       - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
-      ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
+      ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
Signature · 1 line
2.27.0
Reviewed-by on v2 1/10
#13 lore raw

Re: [PATCH v2 01/10] luks2: Fix use of incorrect index and some grub_error() messages.

In reply to Glenn Washburn
Quoted 6 lines · Glenn Washburn

When looping over the digests and segments, the loop variable is j, but the variable i is used to index in the the digests and segments json array. The variable i is the keyslot index. Similarly, there are several grub_error() statements using the wrong index in constructing the error string.

Quoted 53 lines
---
 grub-core/disk/luks2.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
grub-core/disk/luks2.c +7 -7
index 31d7166fc..2241e0312 100644
@@ -275,34 +275,34 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
   for (j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&digest, &digests, i) ||
+      if (grub_json_getchild (&digest, &digests, j) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
 
       if ((d->keyslots & (1 << idx)))
 	break;
     }
   if (j == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, i);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
       grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-  for (j = 0; j < size; j++)
+  for (i = j, j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&segment, &segments, i) ||
+      if (grub_json_getchild (&segment, &segments, j) ||
 	  grub_json_getuint64 (&idx, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
 
       if ((d->segments & (1 << idx)))
 	break;
     }
   if (j == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE, i);
 
   return GRUB_ERR_NONE;
 }
Signature · 1 line
2.27.0
#16 lore raw

Re: [PATCH v2 03/10] luks2: Use more intuitive keyslot key instead of index when naming keyslot.

In reply to Glenn Washburn
Quoted 8 lines · Glenn Washburn

Use the keyslot key value in the keyslot json array rather than the index of the keyslot in the json array. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore,with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

The rationale does make sense to me, but I'm not sure I like cramming this information into another out-parameter. The result feels a bit hard to read to me and is not immediately obvious. How about we instead add another member "index" or similar to struct grub_luks2_keyslot?

Patrick

Quoted 98 lines
---
 grub-core/disk/luks2.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
grub-core/disk/luks2.c +14 -13
index c3cd63606..db251cce0 100644
@@ -255,16 +255,16 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
 static grub_err_t
 luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_segment_t *s,
-		   const grub_json_t *root, grub_size_t keyslot_idx)
+		   grub_uint64_t *keyslot_key, const grub_json_t *root, grub_size_t keyslot_idx)
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t keyslot_key, digest_key, segment_key;
+  grub_uint64_t digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
+      grub_json_getuint64 (keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
@@ -281,11 +281,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
           luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
-      if ((d->keyslots & (1 << keyslot_key)))
+      if ((d->keyslots & (1 << *keyslot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", *keyslot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -593,17 +593,18 @@ luks2_recover_key (grub_disk_t disk,
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
-      ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
+      grub_uint64_t keyslot_key;
+      ret = luks2_get_keyslot (&keyslot, &digest, &segment, &keyslot_key, json, i);
       if (ret)
 	goto err;
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot_key);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -618,16 +619,16 @@ luks2_recover_key (grub_disk_t disk,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
+			keyslot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
+			keyslot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -635,7 +636,7 @@ luks2_recover_key (grub_disk_t disk,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
#19 lore raw

Re: [PATCH v2 04/10] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

The total_length field is named confusingly because length usually refers to bytes, whereas in this case its really the total number of sectors on the device. Also counter-intuitively, grub_disk_get_size returns the total number of device native sectors sectors. We need to convert the sectors from

                          ^~~~~~~~~~~~~~~

Nit: there's a duplicate word here.

Patrick

Quoted 39 lines

the size of the underlying device to the cryptodisk sector size. And segment.size is in bytes which need to be converted to cryptodisk sectors.

Also, removed an empty statement.

---
 grub-core/disk/luks2.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +4 -3
index db251cce0..9f7d6e12b 100644
@@ -424,7 +424,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
   grub_uint8_t salt[GRUB_CRYPTODISK_MAX_KEYLEN];
   grub_uint8_t *split_key = NULL;
   grub_size_t saltlen = sizeof (salt);
-  char cipher[32], *p;;
+  char cipher[32], *p;
   const gcry_md_spec_t *hash;
   gcry_err_code_t gcry_ret;
   grub_err_t ret;
@@ -611,9 +611,10 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_length = grub_disk_get_size (disk) - crypt->offset;
+	crypt->total_length = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+			       - crypt->offset;
       else
-	crypt->total_length = grub_strtoull (segment.size, NULL, 10);
+	crypt->total_length = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
Signature · 1 line
2.27.0
#22 lore raw

Re: [PATCH v2 06/10] cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Glenn Washburn
Quoted 72 lines · Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 52 ++++++++++++++++++++++---------------
 grub-core/disk/luks.c       |  5 ++--
 grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
grub-core/disk/cryptodisk.c +5 -6
index a3d672f68..623f0f396 100644
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
     return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len)
 	    : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));
 
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector << dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len);
 	    dev->iv_hash->write (ctx, &tmp, sizeof (tmp));
@@ -281,14 +282,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*

grub-core/disk/cryptodisk.c grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, · line 296

Quoted 10 lines
grub-core/disk/cryptodisk.c +10 -0
+	   * The IV is a 32 or 64 bit value of the dm-crypt native sector
+	   * number. If using 32 bit IV mode, zero out the most significant
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;
+	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 - GRUB_CRYPTODISK_IV_LOG_SIZE));
+	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
+	      iv[1] = 0;

I may be misreading this, but aren't we zeroing out 64 bits here instead of 32 bits as the comment states?

Patrick

Quoted 172 lines
grub-core/disk/cryptodisk.c +16 -15
+	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
-	  iv[1] = grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size));
-	  iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
+	  iv[1] = grub_cpu_to_le32 (sector >> (32 - log_sector_size));
+	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
 				    & 0xFFFFFFFF);
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
@@ -311,10 +321,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_CBC:
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_cbc_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -322,10 +332,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_PCBC:
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_pcbc_decrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -337,7 +347,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (err)
 	      return err;
 
-	    for (j = 0; j < (1U << dev->log_sector_size);
+	    for (j = 0; j < (1U << log_sector_size);
 		 j += dev->cipher->cipher->blocksize)
 	      {
 		grub_crypto_xor (data + i + j, data + i + j, iv,
@@ -368,11 +378,11 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (do_encrypt)
 	      err = grub_crypto_ecb_encrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    else
 	      err = grub_crypto_ecb_decrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    if (err)
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -381,10 +391,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_ECB:
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  else
 	    err = grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  if (err)
 	    return err;
 	  break;
@@ -399,9 +409,9 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector)
+			 grub_disk_addr_t sector, grub_size_t log_sector_size)
 {
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector, log_sector_size, 0);
 }
 
 grub_err_t
@@ -766,7 +776,7 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
     }
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
 					size << disk->log_sector_size,
-					sector, 0);
+					sector, dev->log_sector_size, 0);
   return grub_crypto_gcry_error (gcry_err);
 }
 
@@ -807,7 +817,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
-					sector, 1);
+					sector, disk->log_sector_size, 1);
   if (gcry_err)
     {
       grub_free (tmp);
grub-core/disk/luks.c +3 -2
index 59702067a..20cc20b9b 100644
@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
-  newdev->log_sector_size = 9;
+  newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
   newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,
 	  return err;
 	}
 
-      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
+      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0,
+					  LUKS1_LOG_SECTOR_SIZE);
       if (gcry_err)
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 9f7d6e12b..b7d3b425a 100644
@@ -499,7 +499,12 @@ luks2_decrypt_key (grub_uint8_t *out_key,
       goto err;
     }
 
-  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0);
+  /*
+   * The key slots area is always encrypted in 512-byte sectors,
+   * regardless of encrypted data sector size.
+   */
+  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0,
+				      LUKS1_LOG_SECTOR_SIZE);
   if (gcry_ret)
     {
       ret = grub_crypto_gcry_error (gcry_ret);
include/grub/cryptodisk.h +7 -1
index e1b21e785..006f3ec49 100644
@@ -48,6 +48,12 @@ typedef enum
 
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
+/* LUKS1 specification defines the block size to always be 512 bytes. */
+#define LUKS1_LOG_SECTOR_SIZE 9
+
+/* By default dm-crypt increments the IV every 512 bytes. */
+#define GRUB_CRYPTODISK_IV_LOG_SIZE 9
+
 #define GRUB_CRYPTODISK_GF_LOG_SIZE 7
 #define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)
 #define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE - 3)
@@ -139,7 +145,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector);
+			 grub_disk_addr_t sector, grub_size_t log_sector_size);
 grub_err_t
 grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
 			grub_disk_t source);
Signature · 1 line
2.27.0
Reviewed-by on v2 8/10
#28 lore raw

Re: [PATCH v2 08/10] cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.

In reply to Glenn Washburn
Quoted 5 lines · Glenn Washburn

This makes the creates an alignment with grub_disk_t naming of the same field and is more intuitive as to how it should be used.

---

There's something off with the commit message here. Other than that:

Quoted 80 lines
 grub-core/disk/cryptodisk.c | 2 +-
 grub-core/disk/geli.c       | 2 +-
 grub-core/disk/luks.c       | 2 +-
 grub-core/disk/luks2.c      | 4 ++--
 include/grub/cryptodisk.h   | 3 ++-
 5 files changed, 7 insertions(+), 6 deletions(-)
grub-core/disk/cryptodisk.c +1 -1
index 1a91c2d55..f5d098f65 100644
@@ -711,7 +711,7 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk)
     }
 
   disk->data = dev;
-  disk->total_sectors = dev->total_length;
+  disk->total_sectors = dev->total_sectors;
   disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
   disk->id = dev->id;
   dev->ref++;
grub-core/disk/geli.c +1 -1
index e9d23299a..4ec875821 100644
@@ -391,7 +391,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
 
   newdev->modname = "geli";
 
-  newdev->total_length = grub_disk_get_size (disk) - 1;
+  newdev->total_sectors = grub_disk_get_size (disk) - 1;
   grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
   COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= 32 * 2 + 1);
   return newdev;
grub-core/disk/luks.c +1 -1
index 20cc20b9b..545734342 100644
@@ -125,7 +125,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
-  newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index b7d3b425a..2e295cac8 100644
@@ -616,10 +616,10 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_length = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
 			       - crypt->offset;
       else
-	crypt->total_length = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
include/grub/cryptodisk.h +2 -1
index 006f3ec49..9e10ae927 100644
@@ -73,7 +73,8 @@ struct grub_cryptodisk
 
   char *source;
   grub_disk_addr_t offset;
-  grub_disk_addr_t total_length;
+  /* Total number of encrypted sectors of size (1<<log_sector_size) */
+  grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
   int ref;
   grub_crypto_cipher_handle_t cipher;
Signature · 1 line
2.27.0
Reviewed-by on v2 9/10
#30 lore raw

Re: [PATCH v2 09/10] cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

This makes it clear that the offset represents sectors, not bytes, in order to improve readability.

Quoted 122 lines
---
 grub-core/disk/cryptodisk.c | 10 +++++-----
 grub-core/disk/geli.c       |  2 +-
 grub-core/disk/luks.c       |  4 ++--
 grub-core/disk/luks2.c      |  4 ++--
 include/grub/cryptodisk.h   |  7 ++++++-
 5 files changed, 16 insertions(+), 11 deletions(-)
grub-core/disk/cryptodisk.c +5 -5
index f5d098f65..64a51165c 100644
@@ -766,10 +766,10 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Reading %" PRIuGRUB_SIZE " sectors from sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   err = grub_disk_read (dev->source_disk,
-			grub_disk_from_native_sector (disk, sector + dev->offset),
+			grub_disk_from_native_sector (disk, sector + dev->offset_sectors),
 			0, size << disk->log_sector_size, buf);
   if (err)
     {
@@ -815,7 +815,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Writing %" PRIuGRUB_SIZE " sectors to sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
@@ -827,7 +827,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
     }
 
   /* Since ->write was called so disk.mod is loaded but be paranoid  */
-  sector = sector + dev->offset;
+  sector = sector + dev->offset_sectors;
   if (grub_disk_write_weak)
     err = grub_disk_write_weak (dev->source_disk,
 				grub_disk_from_native_sector (disk, sector),
@@ -1240,7 +1240,7 @@ luks_script_get (grub_size_t *sz)
 	ptr = grub_stpcpy (ptr, "luks_mount ");
 	ptr = grub_stpcpy (ptr, i->uuid);
 	*ptr++ = ' ';
-	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset);
+	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset_sectors);
 	while (*ptr)
 	  ptr++;
 	for (iptr = i->cipher->cipher->name; *iptr; iptr++)
grub-core/disk/geli.c +1 -1
index 4ec875821..0175ce4c4 100644
@@ -361,7 +361,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
     }
   newdev->cipher = cipher;
   newdev->secondary_cipher = secondary_cipher;
-  newdev->offset = 0;
+  newdev->offset_sectors = 0;
   newdev->source_disk = NULL;
   newdev->benbi_log = 0;
   if (grub_le_to_cpu16 (header.alg) == 0x16)
grub-core/disk/luks.c +2 -2
index 545734342..88f2da493 100644
@@ -122,10 +122,10 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev = grub_zalloc (sizeof (struct grub_cryptodisk));
   if (!newdev)
       return NULL;
-  newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
+  newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
-  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index 2e295cac8..46abc96ef 100644
@@ -612,12 +612,12 @@ luks2_recover_key (grub_disk_t disk,
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot_key);
 
       /* Set up disk according to keyslot's segment. */
-      crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
+      crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
 	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
-			       - crypt->offset;
+			       - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
include/grub/cryptodisk.h +6 -1
index 9e10ae927..c8eef2204 100644
@@ -72,7 +72,12 @@ struct grub_cryptodisk
   struct grub_cryptodisk **prev;
 
   char *source;
-  grub_disk_addr_t offset;
+
+  /*
+   * The number of sectors the start of the encrypted data is offset into the
+   * underlying disk, where sectors are the size noted by log_sector_size.
+   */
+  grub_disk_addr_t offset_sectors;
   /* Total number of encrypted sectors of size (1<<log_sector_size) */
   grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
Signature · 1 line
2.27.0
#32 lore raw

Re: [PATCH v2 10/10] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

In reply to Glenn Washburn
Quoted 2 lines · Glenn Washburn

This makes it more obvious to the reader that the disk referred to is the source disk, as opposed to say the disk holding the cryptodisk.

Hum. I'm not sure this actually helps readability, mostly because I think that the distinction here is not that helpful in the context of encryption or decryption of the device. In the end we are trying to encrypt or decrypt the disk in order to create the new cryptodisk.

Anyway, I don't particularly care, so take this just as my two cents. The patch itself looks good to me.

Patrick

Quoted 87 lines
---
 grub-core/disk/luks2.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +11 -11
index 46abc96ef..f918f5a52 100644
@@ -416,7 +416,7 @@ luks2_verify_key (grub_luks2_digest_t *d, grub_uint8_t *candidate_key,
 
 static grub_err_t
 luks2_decrypt_key (grub_uint8_t *out_key,
-		   grub_disk_t disk, grub_cryptodisk_t crypt,
+		   grub_disk_t source, grub_cryptodisk_t crypt,
 		   grub_luks2_keyslot_t *k,
 		   const grub_uint8_t *passphrase, grub_size_t passphraselen)
 {
@@ -492,7 +492,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
     }
 
   grub_errno = GRUB_ERR_NONE;
-  ret = grub_disk_read (disk, 0, k->area.offset, k->area.size, split_key);
+  ret = grub_disk_read (source, 0, k->area.offset, k->area.size, split_key);
   if (ret)
     {
       grub_error (GRUB_ERR_IO, "Read error: %s\n", grub_errmsg);
@@ -536,7 +536,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
 }
 
 static grub_err_t
-luks2_recover_key (grub_disk_t disk,
+luks2_recover_key (grub_disk_t source,
 		   grub_cryptodisk_t crypt)
 {
   grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
@@ -551,7 +551,7 @@ luks2_recover_key (grub_disk_t disk,
   grub_json_t *json = NULL, keyslots;
   grub_err_t ret;
 
-  ret = luks2_read_header (disk, &header);
+  ret = luks2_read_header (source, &header);
   if (ret)
     return ret;
 
@@ -560,7 +560,7 @@ luks2_recover_key (grub_disk_t disk,
       return GRUB_ERR_OUT_OF_MEMORY;
 
   /* Read the JSON area. */
-  ret = grub_disk_read (disk, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
+  ret = grub_disk_read (source, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
 			grub_be_to_cpu64 (header.hdr_size) - sizeof (header), json_header);
   if (ret)
       goto err;
@@ -577,10 +577,10 @@ luks2_recover_key (grub_disk_t disk,
     }
 
   /* Get the passphrase from the user. */
-  if (disk->partition)
-    part = grub_partition_get_name (disk->partition);
-  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), disk->name,
-		disk->partition ? "," : "", part ? : "",
+  if (source->partition)
+    part = grub_partition_get_name (source->partition);
+  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
+		source->partition ? "," : "", part ? : "",
 		crypt->uuid);
   if (!grub_password_get (passphrase, MAX_PASSPHRASE))
     {
@@ -616,12 +616,12 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
 			       - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
-      ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
+      ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
Signature · 1 line
2.27.0
#35 lore raw
In reply to Glenn Washburn
Quoted 2 lines · Glenn Washburn

This is a minor update to fix patch 3, where I missed updating the format string type code. This was causing i386 builds to fail. Rangediff is included.

Cool, thanks a lot for taking care of this! I've left a few comments here and there, but overall things look good to me.

Patrick

Quoted 93 lines

Glenn Washburn (10):
luks2: Fix use of incorrect index and some grub_error() messages.
luks2: Improve readability in luks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming
keyslot.
luks2: grub_cryptodisk_t->total_length is the max number of device
native sectors
cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
cryptodisk: Properly handle non-512 byte sized sectors.
cryptodisk: Replace some literals with constants in
grub_cryptodisk_endecrypt.
cryptodisk: Rename total_length field in grub_cryptodisk_t to
total_sectors.
cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as in

    luks.c.
 grub-core/disk/cryptodisk.c | 78 ++++++++++++++++++--------------
 grub-core/disk/geli.c       |  4 +-
 grub-core/disk/luks.c       |  9 ++--
 grub-core/disk/luks2.c      | 88 ++++++++++++++++++++-----------------
 include/grub/cryptodisk.h   | 18 ++++++--
 include/grub/types.h        |  3 ++
 6 files changed, 118 insertions(+), 82 deletions(-)

Range-diff against v1:
1: 00646c92f ! 1: e2433b8ab luks2: Use more intuitive keyslot key instead of index when naming keyslot.

    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
            if (keyslot.priority == 0)
      	{
     -	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
    -+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", keyslot_key);
    ++	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot_key);
      	  continue;
              }
      
     -      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
    -+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", keyslot_key);
    ++      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot_key);
      
            /* Set up disk according to keyslot's segment. */
            crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
     @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
    + 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
            if (ret)
      	{
    - 	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
    +-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
     -			i, grub_errmsg);
    ++	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
     +			keyslot_key, grub_errmsg);
      	  continue;
      	}
      
    -@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
    +       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
            if (ret)
      	{
    - 	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
    +-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
     -			i, grub_errmsg);
    ++	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
     +			keyslot_key, grub_errmsg);
      	  continue;
      	}
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
             * where each element is either empty or holds a key.
             */
     -      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
    -+      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), keyslot_key);
    ++      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot_key);
      
            candidate_key_len = keyslot.key_size;
            break;
2:  137909929 = 2:  3baffdd4f luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
3:  9cfbb3373 = 3:  6da3d8598 cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
4:  5f7bd00a6 = 4:  fd7cb6b16 cryptodisk: Properly handle non-512 byte sized sectors.
5:  4251c828a = 5:  b33733199 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
6:  feb8298b8 = 6:  de4f6b2e5 cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
7:  e26aed2ee ! 7:  a165791de cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.

@@ grub-core/disk/luks.c: configure_ciphers (grub_disk_t disk, const char *check_uu

## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
- grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", keyslot_key);
+ grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot_key);

/* Set up disk according to keyslot's segment. */
- crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
8: e083b25e2 = 8: 86beb5be8 luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

Signature · 1 line
2.27.0
#25 lore raw

Re: [PATCH v2 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Glenn Washburn

Daniel, Patrick,

Have either of you had a chance to review this patch? Daniel, this is my interpretation of what you suggested earlier to remove constant literals in this message:
https://lists.gnu.org/archive/html/grub-devel/2020-09/msg00069.html

I'm going to make a v3 patchset with Patrick's suggested changes. Please reply to that patch.

Glenn

Quoted 54 lines · On Sat, 3 Oct 2020 17:55:31 -0500 Glenn Washburn…

This should improve readability of code by providing clues as to what the value represents.

---
 grub-core/disk/cryptodisk.c | 12 +++++++-----
 include/grub/types.h        |  3 +++
 2 files changed, 10 insertions(+), 5 deletions(-)
grub-core/disk/cryptodisk.c +7 -5
index 623f0f396..1a91c2d55 100644
@@ -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,
grub-core/disk/cryptodisk.c +0 -0
 					 dev->cipher->cipher->blocksize);
 	  if (err)
include/grub/types.h +2 -0
index 035a4b528..8b4267ebd 100644
@@ -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 */

#23 lore raw

Re: [PATCH v2 06/10] cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Patrick Steinhardt
Quoted 95 lines · On Fri, 9 Oct 2020 11:50:29 +0200 Patrick Steinhardt <[email protected]> wrote:
Quoted 88 lines · Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 52
++++++++++++++++++++++--------------- grub-core/disk/luks.c       |
 5 ++-- grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index a3d672f68..623f0f396 100644

grub-core/disk/cryptodisk.c +2 -1
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t
 log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
grub-core/disk/cryptodisk.c +0 -0
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len) : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));

grub-core/disk/cryptodisk.c +2 -2
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector <<
 dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix,

dev->iv_prefix_len); dev->iv_hash->write (ctx, &tmp, sizeof (tmp));

grub-core/disk/cryptodisk.c +2 -3
@@ -281,14 +282,23 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native

sector

grub-core/disk/cryptodisk.c +1 -0
+	   * number. If using 32 bit IV mode, zero out the most

significant

grub-core/disk/cryptodisk.c +6 -0
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;
+	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 -

GRUB_CRYPTODISK_IV_LOG_SIZE));

grub-core/disk/cryptodisk.c grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, · line 296

Quoted 2 lines
grub-core/disk/cryptodisk.c +2 -0
+	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
+	      iv[1] = 0;

I may be misreading this, but aren't we zeroing out 64 bits here instead of 32 bits as the comment states?

Quoted 1 line

Patrick

We are only zeroing out 32bits because iv is declared as an array of grub_uint32_t. iv[1] will be the 32 right-most bits of iv64 (and iv[0] the left-most 32 bits). Since iv64 is little-endian the right-most 32 bits will correspond to the most significant 32 bits of iv64.

Quoted 195 lines
Quoted 195 lines
grub-core/disk/cryptodisk.c +1 -1
+	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
-	  iv[1] = grub_cpu_to_le32 (sector >> (32 -

dev->log_sector_size));

grub-core/disk/cryptodisk.c +0 -1
-	  iv[0] = grub_cpu_to_le32 ((sector <<

dev->log_sector_size)

grub-core/disk/cryptodisk.c +1 -0
+	  iv[1] = grub_cpu_to_le32 (sector >> (32 -

log_sector_size));

grub-core/disk/cryptodisk.c +1 -0
+	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
 				    & 0xFFFFFFFF);
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
@@ -311,10 +321,10 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, case GRUB_CRYPTODISK_MODE_CBC:

grub-core/disk/cryptodisk.c +0 -0
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i,

data + i,

grub-core/disk/cryptodisk.c +0 -1
-					   (1U <<

dev->log_sector_size), iv);

grub-core/disk/cryptodisk.c +1 -0
+					   (1U <<

log_sector_size), iv); else
err = grub_crypto_cbc_decrypt (dev->cipher, data + i,
data + i,

grub-core/disk/cryptodisk.c +0 -1
-					   (1U <<

dev->log_sector_size), iv);

grub-core/disk/cryptodisk.c +1 -0
+					   (1U <<

log_sector_size), iv); if (err)

grub-core/disk/cryptodisk.c +0 -0
 	    return err;
 	  break;
@@ -322,10 +332,10 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, case GRUB_CRYPTODISK_MODE_PCBC:

grub-core/disk/cryptodisk.c +0 -0
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i,

data + i,

grub-core/disk/cryptodisk.c +0 -1
-					    (1U <<

dev->log_sector_size), iv);

grub-core/disk/cryptodisk.c +1 -0
+					    (1U <<

log_sector_size), iv); else
err = grub_crypto_pcbc_decrypt (dev->cipher, data + i,
data + i,

grub-core/disk/cryptodisk.c +0 -1
-					    (1U <<

dev->log_sector_size), iv);

grub-core/disk/cryptodisk.c +1 -0
+					    (1U <<

log_sector_size), iv); if (err)

grub-core/disk/cryptodisk.c +2 -2
 	    return err;
 	  break;
@@ -337,7 +347,7 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, if (err)
 	      return err;
 
-	    for (j = 0; j < (1U << dev->log_sector_size);
+	    for (j = 0; j < (1U << log_sector_size);
 		 j += dev->cipher->cipher->blocksize)
 	      {
 		grub_crypto_xor (data + i + j, data + i + j, iv,
@@ -368,11 +378,11 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, if (do_encrypt)
 	      err = grub_crypto_ecb_encrypt (dev->cipher, data +
 i, data + i,
-					     (1U <<
 dev->log_sector_size));
+					     (1U <<

log_sector_size)); else
err = grub_crypto_ecb_decrypt (dev->cipher, data +
i, data + i,

grub-core/disk/cryptodisk.c +0 -1
-					     (1U <<

dev->log_sector_size));

grub-core/disk/cryptodisk.c +1 -0
+					     (1U <<

log_sector_size)); if (err)

grub-core/disk/cryptodisk.c +0 -0
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -381,10 +391,10 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, case GRUB_CRYPTODISK_MODE_ECB:

grub-core/disk/cryptodisk.c +0 -0
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i,

data + i,

grub-core/disk/cryptodisk.c +0 -1
-					   (1U <<

dev->log_sector_size));

grub-core/disk/cryptodisk.c +1 -0
+					   (1U <<

log_sector_size)); else
err = grub_crypto_ecb_decrypt (dev->cipher, data + i,
data + i,

grub-core/disk/cryptodisk.c +0 -1
-					   (1U <<

dev->log_sector_size));

grub-core/disk/cryptodisk.c +1 -0
+					   (1U <<

log_sector_size)); if (err)

grub-core/disk/cryptodisk.c +0 -0
 	    return err;
 	  break;
@@ -399,9 +409,9 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, gcry_err_code_t
grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,

grub-core/disk/cryptodisk.c +1 -1
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector)
+			 grub_disk_addr_t sector, grub_size_t

log_sector_size) {

grub-core/disk/cryptodisk.c +1 -1
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector,

log_sector_size, 0); }

grub_err_t

grub-core/disk/cryptodisk.c +2 -2
@@ -766,7 +776,7 @@ grub_cryptodisk_read (grub_disk_t disk,
 grub_disk_addr_t sector, }
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
 					size <<
 disk->log_sector_size,
-					sector, 0);
+					sector,
 dev->log_sector_size, 0); return grub_crypto_gcry_error (gcry_err);
 }
@@ -807,7 +817,7 @@ grub_cryptodisk_write (grub_disk_t disk,
 grub_disk_addr_t sector, 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size <<
 disk->log_sector_size,
-					sector, 1);
+					sector,
 disk->log_sector_size, 1); if (gcry_err)
     {
       grub_free (tmp);
grub-core/disk/luks.c +3 -2
index 59702067a..20cc20b9b 100644
@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char
 *check_uuid, return NULL;
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
-  newdev->log_sector_size = 9;
+  newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
   newdev->total_length = grub_disk_get_size (disk) -
 newdev->offset; grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,
 	  return err;
 	}
 
-      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length,
 0);
+      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length,
 0,
+					  LUKS1_LOG_SECTOR_SIZE);
       if (gcry_err)
grub-core/disk/luks.c +0 -0
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 9f7d6e12b..b7d3b425a 100644
@@ -499,7 +499,12 @@ luks2_decrypt_key (grub_uint8_t *out_key,
       goto err;
     }
 
-  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key,
 k->area.size, 0);
+  /*
+   * The key slots area is always encrypted in 512-byte sectors,
+   * regardless of encrypted data sector size.
+   */
+  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key,
 k->area.size, 0,
+				      LUKS1_LOG_SECTOR_SIZE);
   if (gcry_ret)
     {
       ret = grub_crypto_gcry_error (gcry_ret);
include/grub/cryptodisk.h +1 -0
index e1b21e785..006f3ec49 100644
@@ -48,6 +48,12 @@ typedef enum
 
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
+/* LUKS1 specification defines the block size to always be 512

bytes. */ +#define LUKS1_LOG_SECTOR_SIZE 9

include/grub/cryptodisk.h +4 -1
+
+/* By default dm-crypt increments the IV every 512 bytes. */
+#define GRUB_CRYPTODISK_IV_LOG_SIZE 9
+
 #define GRUB_CRYPTODISK_GF_LOG_SIZE 7
 #define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)
 #define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE
- 3) @@ -139,7 +145,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t

dev, gcry_err_code_t
grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,

include/grub/cryptodisk.h +1 -1
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector);
+			 grub_disk_addr_t sector, grub_size_t

log_sector_size); grub_err_t
grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
grub_disk_t source);

Signature · 1 line
2.27.0
#17 lore raw

Re: [PATCH v2 03/10] luks2: Use more intuitive keyslot key instead of index when naming keyslot.

In reply to Patrick Steinhardt
Quoted 11 lines · On Fri, 9 Oct 2020 11:44:15 +0200 Patrick Steinhardt <[email protected]> wrote:
Quoted 9 lines · Glenn Washburn

Use the keyslot key value in the keyslot json array rather than the index of the keyslot in the json array. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore,with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

Quoted 7 lines

The rationale does make sense to me, but I'm not sure I like cramming this information into another out-parameter. The result feels a bit hard to read to me and is not immediately obvious. How about we instead add another member "index" or similar to struct grub_luks2_keyslot?

Patrick

That's a great idea. I'll add it in the next version of the patch.

Quoted 110 lines
Quoted 110 lines
---
 grub-core/disk/luks2.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
grub-core/disk/luks2.c +3 -3
index c3cd63606..db251cce0 100644
@@ -255,16 +255,16 @@ luks2_parse_digest (grub_luks2_digest_t *out,
 const grub_json_t *digest) 
 static grub_err_t
 luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t
 *d, grub_luks2_segment_t *s,
-		   const grub_json_t *root, grub_size_t
 keyslot_idx)
+		   grub_uint64_t *keyslot_key, const grub_json_t
 *root, grub_size_t keyslot_idx) {
   grub_json_t keyslots, keyslot, digests, digest, segments,
 segment; grub_size_t i, size;
-  grub_uint64_t keyslot_key, digest_key, segment_key;
+  grub_uint64_t digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
+      grub_json_getuint64 (keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||

luks2_parse_keyslot (k, &keyslot))
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse
keyslot index %"PRIuGRUB_SIZE, keyslot_idx); @@ -281,11 +281,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s luks2_parse_digest (d, &digest)) return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);

grub-core/disk/luks2.c +1 -2
-      if ((d->keyslots & (1 << keyslot_key)))
+      if ((d->keyslots & (1 << *keyslot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for

keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
+ return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for

keyslot \"%"PRIuGRUB_UINT64_T"\"", *keyslot_key); 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
grub-core/disk/luks2.c +4 -3
@@ -593,17 +593,18 @@ luks2_recover_key (grub_disk_t disk,
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
-      ret = luks2_get_keyslot (&keyslot, &digest, &segment, json,
 i);
+      grub_uint64_t keyslot_key;
+      ret = luks2_get_keyslot (&keyslot, &digest, &segment,
 &keyslot_key, json, i); if (ret)
 	goto err;
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot
 %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot
 %"PRIuGRUB_UINT64_T" due to priority\n", keyslot_key); continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n",
 i);
+      grub_dprintf ("luks2", "Trying keyslot
%"PRIuGRUB_UINT64_T"\n", keyslot_key); 
       /* Set up disk according to keyslot's segment. */
       crypt->offset = grub_divmod64 (segment.offset,

segment.sector_size, NULL); @@ -618,16 +619,16 @@ luks2_recover_key (grub_disk_t disk, (const grub_uint8_t *) passphrase, grub_strlen (passphrase)); if (ret)

grub-core/disk/luks2.c +0 -1
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot

%"PRIuGRUB_SIZE" failed: %s\n",

grub-core/disk/luks2.c +1 -1
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot

%"PRIuGRUB_UINT64_T" failed: %s\n",

grub-core/disk/luks2.c +1 -0
+			keyslot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key,

keyslot.key_size); if (ret)

grub-core/disk/luks2.c +0 -1
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot

%"PRIuGRUB_SIZE": %s\n",

grub-core/disk/luks2.c +1 -1
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot

%"PRIuGRUB_UINT64_T": %s\n",

grub-core/disk/luks2.c +1 -0
+			keyslot_key, grub_errmsg);
 	  continue;
 	}
@@ -635,7 +636,7 @@ luks2_recover_key (grub_disk_t disk,
        * TRANSLATORS: It's a cryptographic key slot: one element

of an array
* where each element is either empty or holds a key. */

grub-core/disk/luks2.c +1 -1
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"),
keyslot_key); 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
#26 lore raw

Re: [PATCH v2 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Glenn Washburn

Hi Glenn,

I will be looking at the patches on the grub-devel this week. I will send you my comments then.

Daniel

Quoted 69 lines · Glenn Washburn

Daniel, Patrick,

Have either of you had a chance to review this patch? Daniel, this is my interpretation of what you suggested earlier to remove constant literals in this message:
https://lists.gnu.org/archive/html/grub-devel/2020-09/msg00069.html

I'm going to make a v3 patchset with Patrick's suggested changes. Please reply to that patch.

Glenn

Quoted 54 lines · On Sat, 3 Oct 2020 17:55:31 -0500 Glenn Washburn…

This should improve readability of code by providing clues as to what the value represents.

---
 grub-core/disk/cryptodisk.c | 12 +++++++-----
 include/grub/types.h        |  3 +++
 2 files changed, 10 insertions(+), 5 deletions(-)
grub-core/disk/cryptodisk.c +7 -5
index 623f0f396..1a91c2d55 100644
@@ -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,
grub-core/disk/cryptodisk.c +0 -0
 					 dev->cipher->cipher->blocksize);
 	  if (err)
include/grub/types.h +2 -0
index 035a4b528..8b4267ebd 100644
@@ -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 */

#33 lore raw

Re: [PATCH v2 10/10] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

In reply to Patrick Steinhardt
Quoted 10 lines · On Fri, 9 Oct 2020 12:00:47 +0200 Patrick Steinhardt <[email protected]> wrote:
Quoted 3 lines · Glenn Washburn

This makes it more obvious to the reader that the disk referred to is the source disk, as opposed to say the disk holding the cryptodisk.

Hum. I'm not sure this actually helps readability, mostly because I think that the distinction here is not that helpful in the context of encryption or decryption of the device. In the end we are trying to encrypt or decrypt the disk in order to create the new cryptodisk.

Quoted 4 lines

Anyway, I don't particularly care, so take this just as my two cents. The patch itself looks good to me.

Patrick

If I'm following you, you're saying that because encryption is reversible, source is not helpful because either plaintext or encrypted data can be the source depending on if you're encrypting or decrypting.

In our case here, I think its intuitive to call the disk source because it is where the data is coming from and to distinguish it from the cryptodisk grub_disk_t. So its not called source because of the (encrypted) contents of the disk, as I think you're suggesting. I think this patch makes more sense in the context of some cryptodisk.c and luks.c code.

Note that in grub_cryptodisk_scan_device_real in cryptodisk.c which calls luks2_recover_key the grub_disk_t passed is named source. And in in grub_cryptodisk_open, the parameter disk refers to a grub_disk_t that can be read to decrypt an associated encrypted grub_disk_t (ie. the cryptodisk grub_disk_t). The grub_cryptodisk_t associated in disk->data has a member named "source_disk" which points to the associated grub_disk_t and member "source" which is the name of the associated disk. In both luks2_decrypt_key and luks2_recover_key the grub_disk_t argument refers to the encrypted grub_disk_t which can be accessed as (unencrypted disk)->data->source_disk on the opened crypto disk. So I think its consistent with cryptodisk.c naming conventions to call the grub_disk_t argument "source". Also as the subject line says, this creates consistency with luks.c in its luks2_decrypt_key, which I suspect is named "source" for the reasons I outlined above.

I'm a little confused by "In the end we are trying to encrypt or decrypt the disk in order to create the new cryptodisk." Where does "encrypt" fit in to creating the new cryptodisk?

Quoted 96 lines
Quoted 96 lines
---
 grub-core/disk/luks2.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +2 -2
index 46abc96ef..f918f5a52 100644
@@ -416,7 +416,7 @@ luks2_verify_key (grub_luks2_digest_t *d,
 grub_uint8_t *candidate_key, 
 static grub_err_t
 luks2_decrypt_key (grub_uint8_t *out_key,
-		   grub_disk_t disk, grub_cryptodisk_t crypt,
+		   grub_disk_t source, grub_cryptodisk_t crypt,
 		   grub_luks2_keyslot_t *k,
 		   const grub_uint8_t *passphrase, grub_size_t
 passphraselen) {
@@ -492,7 +492,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
     }
 
   grub_errno = GRUB_ERR_NONE;
-  ret = grub_disk_read (disk, 0, k->area.offset, k->area.size,
 split_key);
+  ret = grub_disk_read (source, 0, k->area.offset, k->area.size,
 split_key); if (ret)
     {
       grub_error (GRUB_ERR_IO, "Read error: %s\n", grub_errmsg);
grub-core/disk/luks2.c +3 -3
@@ -536,7 +536,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
 }
 
 static grub_err_t
-luks2_recover_key (grub_disk_t disk,
+luks2_recover_key (grub_disk_t source,
 		   grub_cryptodisk_t crypt)
 {
   grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
@@ -551,7 +551,7 @@ luks2_recover_key (grub_disk_t disk,
   grub_json_t *json = NULL, keyslots;
   grub_err_t ret;
 
-  ret = luks2_read_header (disk, &header);
+  ret = luks2_read_header (source, &header);
   if (ret)
     return ret;
 
@@ -560,7 +560,7 @@ luks2_recover_key (grub_disk_t disk,
       return GRUB_ERR_OUT_OF_MEMORY;
 
   /* Read the JSON area. */
-  ret = grub_disk_read (disk, 0, grub_be_to_cpu64
 (header.hdr_offset) + sizeof (header),
+  ret = grub_disk_read (source, 0, grub_be_to_cpu64

(header.hdr_offset) + sizeof (header), grub_be_to_cpu64 (header.hdr_size) - sizeof (header), json_header); if (ret)

       goto err;
grub-core/disk/luks2.c +4 -4
@@ -577,10 +577,10 @@ luks2_recover_key (grub_disk_t disk,
     }
 
   /* Get the passphrase from the user. */
-  if (disk->partition)
-    part = grub_partition_get_name (disk->partition);
-  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "),
 disk->name,
-		disk->partition ? "," : "", part ? : "",
+  if (source->partition)
+    part = grub_partition_get_name (source->partition);
+  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "),
 source->name,
+		source->partition ? "," : "", part ? : "",
 		crypt->uuid);
   if (!grub_password_get (passphrase, MAX_PASSPHRASE))
     {
grub-core/disk/luks2.c +1 -2
@@ -616,12 +616,12 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int)
 segment.sector_size) - 1; if (grub_strcmp (segment.size, "dynamic")
 == 0)
-	crypt->total_sectors = (grub_disk_get_size (disk) >>
 (crypt->log_sector_size - disk->log_sector_size))
+	crypt->total_sectors = (grub_disk_get_size (source) >>
 (crypt->log_sector_size - source->log_sector_size))
 			       - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL,
 10) >> crypt->log_sector_size; 
-      ret = luks2_decrypt_key (candidate_key, disk, crypt,

&keyslot,
+ ret = luks2_decrypt_key (candidate_key, source, crypt,
&keyslot, (const grub_uint8_t *) passphrase, grub_strlen (passphrase)); if (ret)
{

Signature · 1 line
2.27.0
#36 lore raw

v3 cover Cryptodisk fixes for v2.06 redux

In reply to Patrick Steinhardt

Heres an updated patch series which addresses comment from Patrick. The only code change is adding a slot_key member to grub_luks2_keyslot and using that instead of an extra out parameter to luks2_get_keyslot.

Glenn Washburn (10):
luks2: Fix use of incorrect index and some grub_error() messages.
luks2: Improve readability in luks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming
keyslot.
luks2: grub_cryptodisk_t->total_length is the max number of device
native sectors
cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
cryptodisk: Properly handle non-512 byte sized sectors.
cryptodisk: Replace some literals with constants in
grub_cryptodisk_endecrypt.
cryptodisk: Rename total_length field in grub_cryptodisk_t to
total_sectors.
cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as in

    luks.c.
 grub-core/disk/cryptodisk.c | 78 +++++++++++++++++++--------------
 grub-core/disk/geli.c       |  4 +-
 grub-core/disk/luks.c       |  9 ++--
 grub-core/disk/luks2.c      | 86 ++++++++++++++++++++-----------------
 include/grub/cryptodisk.h   | 18 ++++++--
 include/grub/types.h        |  3 ++
 6 files changed, 117 insertions(+), 81 deletions(-)

Range-diff against v2:
1: e2433b8ab ! 1: 1f65a04e0 luks2: Use more intuitive keyslot key instead of index when naming keyslot.

    @@ Commit message
         cryptsetup.
     
      ## grub-core/disk/luks2.c ##
    -@@ grub-core/disk/luks2.c: luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
    +@@ grub-core/disk/luks2.c: typedef struct grub_luks2_header grub_luks2_header_t;
      
    - static grub_err_t
    - luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_segment_t *s,
    --		   const grub_json_t *root, grub_size_t keyslot_idx)
    -+		   grub_uint64_t *keyslot_key, const grub_json_t *root, grub_size_t keyslot_idx)
    + struct grub_luks2_keyslot
    + {
    ++  grub_uint64_t slot_key;
    +   grub_int64_t key_size;
    +   grub_int64_t priority;
    +   struct
    +@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
      {
        grub_json_t keyslots, keyslot, digests, digest, segments, segment;
        grub_size_t i, size;
    @@ grub-core/disk/luks2.c: luks2_parse_digest (grub_luks2_digest_t *out, const grub
        if (grub_json_getvalue (&keyslots, root, "keyslots") ||
            grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
     -      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
    -+      grub_json_getuint64 (keyslot_key, &keyslot, NULL) ||
    ++      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||
            grub_json_getchild (&keyslot, &keyslot, 0) ||
            luks2_parse_keyslot (k, &keyslot))
          return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
    @@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
      	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
      
     -      if ((d->keyslots & (1 << keyslot_key)))
    -+      if ((d->keyslots & (1 << *keyslot_key)))
    ++      if ((d->keyslots & (1 << k->slot_key)))
      	break;
          }
        if (i == size)
     -      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
    -+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", *keyslot_key);
    ++      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
      
        /* Get segment that matches the digest. */
        if (grub_json_getvalue (&segments, root, "segments") ||
     @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
    -   /* Try all keyslot */
    -   for (i = 0; i < size; i++)
    -     {
    --      ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
    -+      grub_uint64_t keyslot_key;
    -+      ret = luks2_get_keyslot (&keyslot, &digest, &segment, &keyslot_key, json, i);
    -       if (ret)
    - 	goto err;
      
            if (keyslot.priority == 0)
      	{
     -	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
    -+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot_key);
    ++	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
      	  continue;
              }
      
     -      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
    -+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot_key);
    ++      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
      
            /* Set up disk according to keyslot's segment. */
            crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
     -	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
     -			i, grub_errmsg);
     +	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
    -+			keyslot_key, grub_errmsg);
    ++			keyslot.slot_key, grub_errmsg);
      	  continue;
      	}
      
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
     -	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
     -			i, grub_errmsg);
     +	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
    -+			keyslot_key, grub_errmsg);
    ++			keyslot.slot_key, grub_errmsg);
      	  continue;
      	}
      
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
             * where each element is either empty or holds a key.
             */
     -      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
    -+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot_key);
    ++      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot.slot_key);
      
            candidate_key_len = keyslot.key_size;
            break;

2: 3baffdd4f ! 2: fcb72f70d luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
@@ Commit message
The total_length field is named confusingly because length usually refers to
bytes, whereas in this case its really the total number of sectors on the
device. Also counter-intuitively, grub_disk_get_size returns the total
- number of device native sectors sectors. We need to convert the sectors from
- the size of the underlying device to the cryptodisk sector size. And
+ number of device native sectors. We need to convert the sectors from the
+ size of the underlying device to the cryptodisk sector size. And
segment.size is in bytes which need to be converted to cryptodisk sectors.

Also, removed an empty statement.

3:  6da3d8598 = 3:  e8e069cae cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
4:  fd7cb6b16 = 4:  6fe26ba56 cryptodisk: Properly handle non-512 byte sized sectors.
5:  b33733199 = 5:  3918a9013 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
6:  de4f6b2e5 ! 6:  28e0cac66 cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.

@@ Metadata
## Commit message ##
cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.

- This makes the creates an alignment with grub_disk_t naming of the same
- field and is more intuitive as to how it should be used.
+ This creates an alignment with grub_disk_t naming of the same field and is
+ more intuitive as to how it should be used.

## grub-core/disk/cryptodisk.c ##
@@ grub-core/disk/cryptodisk.c: grub_cryptodisk_open (const char *name, grub_disk_t disk)
7: a165791de ! 7: 74c6232c9 cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
@@ grub-core/disk/luks.c: configure_ciphers (grub_disk_t disk, const char *check_uu

## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
- grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot_key);
+ grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);

/* Set up disk according to keyslot's segment. */
- crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
8: 86beb5be8 = 8: 738fe0139 luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

Signature · 1 line
2.27.0
#40 lore raw

PATCH v3 2/10 luks2: Improve readability in luks2_get_keyslot.

Introduce new variables keyslot_key, digest_key, and segment_key which represent the integer key of the item in the respective associative array when looping over the array items. This replaces using a generically named variable named "idx" used for all three values. The parameter "i" is renamed to "keyslot_idx" and the loop variable "j" is renamed to "i". Error messages now distinguish between indexes and keys. The former include "index" in the error string, and the later are surrounded in quotes.

---
 grub-core/disk/luks2.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
grub-core/disk/luks2.c +20 -19
index 2241e0312..c3cd63606 100644
@@ -255,54 +255,55 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
 static grub_err_t
 luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_segment_t *s,
-		   const grub_json_t *root, grub_size_t i)
+		   const grub_json_t *root, grub_size_t keyslot_idx)
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
-  grub_size_t j, size;
-  grub_uint64_t idx;
+  grub_size_t i, size;
+  grub_uint64_t keyslot_key, digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
-      grub_json_getchild (&keyslot, &keyslots, i) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
+      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
-    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, i);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
       grub_json_getsize (&size, &digests))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
-  for (j = 0; j < size; j++)
+  for (i = 0; i < size; i++)
     {
-      if (grub_json_getchild (&digest, &digests, j) ||
+      if (grub_json_getchild (&digest, &digests, i) ||
+	  grub_json_getuint64 (&digest_key, &digest, NULL) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
-      if ((d->keyslots & (1 << idx)))
+      if ((d->keyslots & (1 << keyslot_key)))
 	break;
     }
-  if (j == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, i);
+  if (i == size)
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
       grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-  for (i = j, j = 0; j < size; j++)
+  for (i = 0; i < size; i++)
     {
-      if (grub_json_getchild (&segment, &segments, j) ||
-	  grub_json_getuint64 (&idx, &segment, NULL) ||
+      if (grub_json_getchild (&segment, &segments, i) ||
+	  grub_json_getuint64 (&segment_key, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
-      if ((d->segments & (1 << idx)))
+      if ((d->segments & (1 << segment_key)))
 	break;
     }
-  if (j == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE, i);
+  if (i == size)
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", digest_key);
 
   return GRUB_ERR_NONE;
 }
Signature · 1 line
2.27.0
#44 lore raw

PATCH v3 3/10 luks2: Use more intuitive keyslot key instead of index when naming keyslot.

In reply to Glenn Washburn

Use the keyslot key value in the keyslot json array rather than the index of the keyslot in the json array. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore,with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

---
 grub-core/disk/luks2.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +12 -11
index c3cd63606..4e1e47161 100644
@@ -65,6 +65,7 @@ typedef struct grub_luks2_header grub_luks2_header_t;
 
 struct grub_luks2_keyslot
 {
+  grub_uint64_t slot_key;
   grub_int64_t key_size;
   grub_int64_t priority;
   struct
@@ -259,12 +260,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t keyslot_key, digest_key, segment_key;
+  grub_uint64_t digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
@@ -281,11 +282,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
           luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
-      if ((d->keyslots & (1 << keyslot_key)))
+      if ((d->keyslots & (1 << k->slot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -599,11 +600,11 @@ luks2_recover_key (grub_disk_t disk,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -618,16 +619,16 @@ luks2_recover_key (grub_disk_t disk,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -635,7 +636,7 @@ luks2_recover_key (grub_disk_t disk,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot.slot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
#37 lore raw

PATCH v3 1/10 luks2: Fix use of incorrect index and some grub_error() messages.

In reply to Glenn Washburn

When looping over the digests and segments, the loop variable is j, but the variable i is used to index in the the digests and segments json array. The variable i is the keyslot index. Similarly, there are several grub_error() statements using the wrong index in constructing the error string.

---
 grub-core/disk/luks2.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
grub-core/disk/luks2.c +7 -7
index 31d7166fc..2241e0312 100644
@@ -275,34 +275,34 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
   for (j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&digest, &digests, i) ||
+      if (grub_json_getchild (&digest, &digests, j) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
 
       if ((d->keyslots & (1 << idx)))
 	break;
     }
   if (j == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, i);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
       grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-  for (j = 0; j < size; j++)
+  for (i = j, j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&segment, &segments, i) ||
+      if (grub_json_getchild (&segment, &segments, j) ||
 	  grub_json_getuint64 (&idx, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
 
       if ((d->segments & (1 << idx)))
 	break;
     }
   if (j == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE, i);
 
   return GRUB_ERR_NONE;
 }
Signature · 1 line
2.27.0
#59 lore raw

PATCH v3 7/10 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Glenn Washburn

This should improve readability of code by providing clues as to what the value represents.

---
 grub-core/disk/cryptodisk.c | 12 +++++++-----
 include/grub/types.h        |  3 +++
 2 files changed, 10 insertions(+), 5 deletions(-)
grub-core/disk/cryptodisk.c +7 -5
index 623f0f396..1a91c2d55 100644
@@ -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)
include/grub/types.h +3 -0
index 035a4b528..8b4267ebd 100644
@@ -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 */
Signature · 1 line
2.27.0
#72 lore raw

PATCH v3 10/10 luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

In reply to Glenn Washburn

This makes it more obvious to the reader that the disk referred to is the source disk, as opposed to say the disk holding the cryptodisk.

---
 grub-core/disk/luks2.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +11 -11
index cd573208a..ccb94e80d 100644
@@ -417,7 +417,7 @@ luks2_verify_key (grub_luks2_digest_t *d, grub_uint8_t *candidate_key,
 
 static grub_err_t
 luks2_decrypt_key (grub_uint8_t *out_key,
-		   grub_disk_t disk, grub_cryptodisk_t crypt,
+		   grub_disk_t source, grub_cryptodisk_t crypt,
 		   grub_luks2_keyslot_t *k,
 		   const grub_uint8_t *passphrase, grub_size_t passphraselen)
 {
@@ -493,7 +493,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
     }
 
   grub_errno = GRUB_ERR_NONE;
-  ret = grub_disk_read (disk, 0, k->area.offset, k->area.size, split_key);
+  ret = grub_disk_read (source, 0, k->area.offset, k->area.size, split_key);
   if (ret)
     {
       grub_error (GRUB_ERR_IO, "Read error: %s\n", grub_errmsg);
@@ -537,7 +537,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
 }
 
 static grub_err_t
-luks2_recover_key (grub_disk_t disk,
+luks2_recover_key (grub_disk_t source,
 		   grub_cryptodisk_t crypt)
 {
   grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
@@ -552,7 +552,7 @@ luks2_recover_key (grub_disk_t disk,
   grub_json_t *json = NULL, keyslots;
   grub_err_t ret;
 
-  ret = luks2_read_header (disk, &header);
+  ret = luks2_read_header (source, &header);
   if (ret)
     return ret;
 
@@ -561,7 +561,7 @@ luks2_recover_key (grub_disk_t disk,
       return GRUB_ERR_OUT_OF_MEMORY;
 
   /* Read the JSON area. */
-  ret = grub_disk_read (disk, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
+  ret = grub_disk_read (source, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
 			grub_be_to_cpu64 (header.hdr_size) - sizeof (header), json_header);
   if (ret)
       goto err;
@@ -578,10 +578,10 @@ luks2_recover_key (grub_disk_t disk,
     }
 
   /* Get the passphrase from the user. */
-  if (disk->partition)
-    part = grub_partition_get_name (disk->partition);
-  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), disk->name,
-		disk->partition ? "," : "", part ? : "",
+  if (source->partition)
+    part = grub_partition_get_name (source->partition);
+  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
+		source->partition ? "," : "", part ? : "",
 		crypt->uuid);
   if (!grub_password_get (passphrase, MAX_PASSPHRASE))
     {
@@ -616,12 +616,12 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
 			       - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
-      ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
+      ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
Signature · 1 line
2.27.0
#46 lore raw

PATCH v3 4/10 luks2: grub_cryptodisk_t->total_length is the max number of device native sectors

In reply to Glenn Washburn

The total_length field is named confusingly because length usually refers to bytes, whereas in this case its really the total number of sectors on the device. Also counter-intuitively, grub_disk_get_size returns the total number of device native sectors. We need to convert the sectors from the size of the underlying device to the cryptodisk sector size. And segment.size is in bytes which need to be converted to cryptodisk sectors.

Also, removed an empty statement.

---
 grub-core/disk/luks2.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +4 -3
index 4e1e47161..311d18684 100644
@@ -425,7 +425,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
   grub_uint8_t salt[GRUB_CRYPTODISK_MAX_KEYLEN];
   grub_uint8_t *split_key = NULL;
   grub_size_t saltlen = sizeof (salt);
-  char cipher[32], *p;;
+  char cipher[32], *p;
   const gcry_md_spec_t *hash;
   gcry_err_code_t gcry_ret;
   grub_err_t ret;
@@ -611,9 +611,10 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_length = grub_disk_get_size (disk) - crypt->offset;
+	crypt->total_length = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+			       - crypt->offset;
       else
-	crypt->total_length = grub_strtoull (segment.size, NULL, 10);
+	crypt->total_length = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
Signature · 1 line
2.27.0
#48 lore raw

PATCH v3 5/10 cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.

In reply to Glenn Washburn

When setting cipher IV mode, detection is done by prefix matching the cipher IV mode part of the cipher mode string. Since "plain" matches "plain64", we must check for "plain64" first. Otherwise, "plain64" will be detected as "plain".

---
 grub-core/disk/cryptodisk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/disk/cryptodisk.c +2 -2
index 13af84dd1..a3d672f68 100644
@@ -501,10 +501,10 @@ grub_cryptodisk_setcipher (grub_cryptodisk_t crypt, const char *ciphername, cons
 
   if (cipheriv == NULL)
       ;
-  else if (grub_memcmp (cipheriv, "plain", sizeof ("plain") - 1) == 0)
-      mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN;
   else if (grub_memcmp (cipheriv, "plain64", sizeof ("plain64") - 1) == 0)
       mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN64;
+  else if (grub_memcmp (cipheriv, "plain", sizeof ("plain") - 1) == 0)
+      mode_iv = GRUB_CRYPTODISK_MODE_IV_PLAIN;
   else if (grub_memcmp (cipheriv, "benbi", sizeof ("benbi") - 1) == 0)
     {
       if (cipher->cipher->blocksize & (cipher->cipher->blocksize - 1)
Signature · 1 line
2.27.0
#66 lore raw

PATCH v3 8/10 cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.

In reply to Glenn Washburn

This creates an alignment with grub_disk_t naming of the same field and is more intuitive as to how it should be used.

---
 grub-core/disk/cryptodisk.c | 2 +-
 grub-core/disk/geli.c       | 2 +-
 grub-core/disk/luks.c       | 2 +-
 grub-core/disk/luks2.c      | 4 ++--
 include/grub/cryptodisk.h   | 3 ++-
 5 files changed, 7 insertions(+), 6 deletions(-)
grub-core/disk/cryptodisk.c +1 -1
index 1a91c2d55..f5d098f65 100644
@@ -711,7 +711,7 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk)
     }
 
   disk->data = dev;
-  disk->total_sectors = dev->total_length;
+  disk->total_sectors = dev->total_sectors;
   disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
   disk->id = dev->id;
   dev->ref++;
grub-core/disk/geli.c +1 -1
index e9d23299a..4ec875821 100644
@@ -391,7 +391,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
 
   newdev->modname = "geli";
 
-  newdev->total_length = grub_disk_get_size (disk) - 1;
+  newdev->total_sectors = grub_disk_get_size (disk) - 1;
   grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
   COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= 32 * 2 + 1);
   return newdev;
grub-core/disk/luks.c +1 -1
index 20cc20b9b..545734342 100644
@@ -125,7 +125,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
-  newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index b9e0e98e1..bae54936c 100644
@@ -616,10 +616,10 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_length = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
 			       - crypt->offset;
       else
-	crypt->total_length = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
include/grub/cryptodisk.h +2 -1
index 006f3ec49..9e10ae927 100644
@@ -73,7 +73,8 @@ struct grub_cryptodisk
 
   char *source;
   grub_disk_addr_t offset;
-  grub_disk_addr_t total_length;
+  /* Total number of encrypted sectors of size (1<<log_sector_size) */
+  grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
   int ref;
   grub_crypto_cipher_handle_t cipher;
Signature · 1 line
2.27.0
#69 lore raw

PATCH v3 9/10 cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.

In reply to Glenn Washburn

This makes it clear that the offset represents sectors, not bytes, in order to improve readability.

---
 grub-core/disk/cryptodisk.c | 10 +++++-----
 grub-core/disk/geli.c       |  2 +-
 grub-core/disk/luks.c       |  4 ++--
 grub-core/disk/luks2.c      |  4 ++--
 include/grub/cryptodisk.h   |  7 ++++++-
 5 files changed, 16 insertions(+), 11 deletions(-)
grub-core/disk/cryptodisk.c +5 -5
index f5d098f65..64a51165c 100644
@@ -766,10 +766,10 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Reading %" PRIuGRUB_SIZE " sectors from sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   err = grub_disk_read (dev->source_disk,
-			grub_disk_from_native_sector (disk, sector + dev->offset),
+			grub_disk_from_native_sector (disk, sector + dev->offset_sectors),
 			0, size << disk->log_sector_size, buf);
   if (err)
     {
@@ -815,7 +815,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Writing %" PRIuGRUB_SIZE " sectors to sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
@@ -827,7 +827,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
     }
 
   /* Since ->write was called so disk.mod is loaded but be paranoid  */
-  sector = sector + dev->offset;
+  sector = sector + dev->offset_sectors;
   if (grub_disk_write_weak)
     err = grub_disk_write_weak (dev->source_disk,
 				grub_disk_from_native_sector (disk, sector),
@@ -1240,7 +1240,7 @@ luks_script_get (grub_size_t *sz)
 	ptr = grub_stpcpy (ptr, "luks_mount ");
 	ptr = grub_stpcpy (ptr, i->uuid);
 	*ptr++ = ' ';
-	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset);
+	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset_sectors);
 	while (*ptr)
 	  ptr++;
 	for (iptr = i->cipher->cipher->name; *iptr; iptr++)
grub-core/disk/geli.c +1 -1
index 4ec875821..0175ce4c4 100644
@@ -361,7 +361,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
     }
   newdev->cipher = cipher;
   newdev->secondary_cipher = secondary_cipher;
-  newdev->offset = 0;
+  newdev->offset_sectors = 0;
   newdev->source_disk = NULL;
   newdev->benbi_log = 0;
   if (grub_le_to_cpu16 (header.alg) == 0x16)
grub-core/disk/luks.c +2 -2
index 545734342..88f2da493 100644
@@ -122,10 +122,10 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev = grub_zalloc (sizeof (struct grub_cryptodisk));
   if (!newdev)
       return NULL;
-  newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
+  newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
-  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index bae54936c..cd573208a 100644
@@ -612,12 +612,12 @@ luks2_recover_key (grub_disk_t disk,
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
       /* Set up disk according to keyslot's segment. */
-      crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
+      crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
 	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
-			       - crypt->offset;
+			       - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
include/grub/cryptodisk.h +6 -1
index 9e10ae927..c8eef2204 100644
@@ -72,7 +72,12 @@ struct grub_cryptodisk
   struct grub_cryptodisk **prev;
 
   char *source;
-  grub_disk_addr_t offset;
+
+  /*
+   * The number of sectors the start of the encrypted data is offset into the
+   * underlying disk, where sectors are the size noted by log_sector_size.
+   */
+  grub_disk_addr_t offset_sectors;
   /* Total number of encrypted sectors of size (1<<log_sector_size) */
   grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
Signature · 1 line
2.27.0
#50 lore raw

PATCH v3 6/10 cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 52 ++++++++++++++++++++++---------------
 grub-core/disk/luks.c       |  5 ++--
 grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
grub-core/disk/cryptodisk.c +31 -21
index a3d672f68..623f0f396 100644
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
     return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len)
 	    : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));
 
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector << dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len);
 	    dev->iv_hash->write (ctx, &tmp, sizeof (tmp));
@@ -281,14 +282,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native sector
+	   * number. If using 32 bit IV mode, zero out the most significant
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;
+	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 - GRUB_CRYPTODISK_IV_LOG_SIZE));
+	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
+	      iv[1] = 0;
+	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
-	  iv[1] = grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size));
-	  iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
+	  iv[1] = grub_cpu_to_le32 (sector >> (32 - log_sector_size));
+	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
 				    & 0xFFFFFFFF);
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
@@ -311,10 +321,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_CBC:
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_cbc_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -322,10 +332,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_PCBC:
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_pcbc_decrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -337,7 +347,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (err)
 	      return err;
 	    
-	    for (j = 0; j < (1U << dev->log_sector_size);
+	    for (j = 0; j < (1U << log_sector_size);
 		 j += dev->cipher->cipher->blocksize)
 	      {
 		grub_crypto_xor (data + i + j, data + i + j, iv,
@@ -368,11 +378,11 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (do_encrypt)
 	      err = grub_crypto_ecb_encrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    else
 	      err = grub_crypto_ecb_decrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    if (err)
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -381,10 +391,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_ECB:
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  else
 	    err = grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  if (err)
 	    return err;
 	  break;
@@ -399,9 +409,9 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector)
+			 grub_disk_addr_t sector, grub_size_t log_sector_size)
 {
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector, log_sector_size, 0);
 }
 
 grub_err_t
@@ -766,7 +776,7 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
     }
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
 					size << disk->log_sector_size,
-					sector, 0);
+					sector, dev->log_sector_size, 0);
   return grub_crypto_gcry_error (gcry_err);
 }
 
@@ -807,7 +817,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
-					sector, 1);
+					sector, disk->log_sector_size, 1);
   if (gcry_err)
     {
       grub_free (tmp);
grub-core/disk/luks.c +3 -2
index 59702067a..20cc20b9b 100644
@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
-  newdev->log_sector_size = 9;
+  newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
   newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,
 	  return err;
 	}
 
-      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
+      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0,
+					  LUKS1_LOG_SECTOR_SIZE);
       if (gcry_err)
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 311d18684..b9e0e98e1 100644
@@ -500,7 +500,12 @@ luks2_decrypt_key (grub_uint8_t *out_key,
       goto err;
     }
 
-  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0);
+  /*
+   * The key slots area is always encrypted in 512-byte sectors,
+   * regardless of encrypted data sector size.
+   */
+  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0,
+				      LUKS1_LOG_SECTOR_SIZE);
   if (gcry_ret)
     {
       ret = grub_crypto_gcry_error (gcry_ret);
include/grub/cryptodisk.h +7 -1
index e1b21e785..006f3ec49 100644
@@ -48,6 +48,12 @@ typedef enum
 
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
+/* LUKS1 specification defines the block size to always be 512 bytes. */
+#define LUKS1_LOG_SECTOR_SIZE 9
+
+/* By default dm-crypt increments the IV every 512 bytes. */
+#define GRUB_CRYPTODISK_IV_LOG_SIZE 9
+
 #define GRUB_CRYPTODISK_GF_LOG_SIZE 7
 #define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)
 #define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE - 3)
@@ -139,7 +145,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector);
+			 grub_disk_addr_t sector, grub_size_t log_sector_size);
 grub_err_t
 grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
 			grub_disk_t source);
Signature · 1 line
2.27.0
#38 lore raw

Re: [PATCH v3 01/10] luks2: Fix use of incorrect index and some grub_error() messages.

In reply to Glenn Washburn
Quoted 28 lines · Glenn Washburn

When looping over the digests and segments, the loop variable is j, but the variable i is used to index in the the digests and segments json array. The variable i is the keyslot index. Similarly, there are several grub_error() statements using the wrong index in constructing the error string.

---
 grub-core/disk/luks2.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
grub-core/disk/luks2.c +2 -2
index 31d7166fc..2241e0312 100644
@@ -275,34 +275,34 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
   for (j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&digest, &digests, i) ||
+      if (grub_json_getchild (&digest, &digests, j) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
 
       if ((d->keyslots & (1 << idx)))
 	break;
     }

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 293

Quoted 10 lines
grub-core/disk/luks2.c +2 -2
   if (j == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, i);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
       grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-  for (j = 0; j < size; j++)
+  for (i = j, j = 0; j < size; j++)

Should not it be "i = j = 0" instead of "i = j, j = 0"?

Quoted 5 lines
grub-core/disk/luks2.c +1 -1
     {
-      if (grub_json_getchild (&segment, &segments, i) ||
+      if (grub_json_getchild (&segment, &segments, j) ||
 	  grub_json_getuint64 (&idx, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 305

Quoted 10 lines
grub-core/disk/luks2.c +2 -2
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
 
       if ((d->segments & (1 << idx)))
 	break;
     }
   if (j == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE, i);

s/PRIuGRUB_SIZE, i/PRIuGRUB_SIZE, j/?

Daniel

#41 lore raw

Re: [PATCH v3 02/10] luks2: Improve readability in luks2_get_keyslot.

In reply to Glenn Washburn
Quoted 7 lines · Glenn Washburn

Introduce new variables keyslot_key, digest_key, and segment_key which represent the integer key of the item in the respective associative array when looping over the array items. This replaces using a generically named variable named "idx" used for all three values. The parameter "i" is renamed to "keyslot_idx" and the loop variable "j" is renamed to "i". Error messages now distinguish between indexes and keys. The former include "index" in the error string, and the later are surrounded in quotes.

I think this patch does too many things in one step. Please do one rename per patch if possible. Additionally, I think this patch, or series of the patches, should be before current patch #1.

Daniel

Reviewed-by on v3 5/10
#49 lore raw

Re: [PATCH v3 05/10] cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.

In reply to Glenn Washburn
Quoted 7 lines · Glenn Washburn

When setting cipher IV mode, detection is done by prefix matching the cipher IV mode part of the cipher mode string. Since "plain" matches "plain64", we must check for "plain64" first. Otherwise, "plain64" will be detected as "plain".

Daniel

#34 lore raw

Re: [PATCH v2 10/10] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

In reply to Glenn Washburn
Quoted 45 lines · Glenn Washburn
Quoted 10 lines · On Fri, 9 Oct 2020 12:00:47 +0200 Patrick Steinhardt <[email protected]> wrote:
Quoted 3 lines · Glenn Washburn

This makes it more obvious to the reader that the disk referred to is the source disk, as opposed to say the disk holding the cryptodisk.

Hum. I'm not sure this actually helps readability, mostly because I think that the distinction here is not that helpful in the context of encryption or decryption of the device. In the end we are trying to encrypt or decrypt the disk in order to create the new cryptodisk.

Quoted 4 lines

Anyway, I don't particularly care, so take this just as my two cents. The patch itself looks good to me.

Patrick

If I'm following you, you're saying that because encryption is reversible, source is not helpful because either plaintext or encrypted data can be the source depending on if you're encrypting or decrypting.

In our case here, I think its intuitive to call the disk source because it is where the data is coming from and to distinguish it from the cryptodisk grub_disk_t. So its not called source because of the (encrypted) contents of the disk, as I think you're suggesting. I think this patch makes more sense in the context of some cryptodisk.c and luks.c code.

Note that in grub_cryptodisk_scan_device_real in cryptodisk.c which calls luks2_recover_key the grub_disk_t passed is named source. And in in grub_cryptodisk_open, the parameter disk refers to a grub_disk_t that can be read to decrypt an associated encrypted grub_disk_t (ie. the cryptodisk grub_disk_t). The grub_cryptodisk_t associated in disk->data has a member named "source_disk" which points to the associated grub_disk_t and member "source" which is the name of the associated disk. In both luks2_decrypt_key and luks2_recover_key the grub_disk_t argument refers to the encrypted grub_disk_t which can be accessed as (unencrypted disk)->data->source_disk on the opened crypto disk. So I think its consistent with cryptodisk.c naming conventions to call the grub_disk_t argument "source". Also as the subject line says, this creates consistency with luks.c in its luks2_decrypt_key, which I suspect is named "source" for the reasons I outlined above.

Quoted 3 lines

I'm a little confused by "In the end we are trying to encrypt or decrypt the disk in order to create the new cryptodisk." Where does "encrypt" fit in to creating the new cryptodisk?

Let's just keep your patch if others think it helps readability. As I said, I don't particularly care as to me the old naming was already quite clear.

Patrick

Reviewed-by
#39 lore raw

Re: [PATCH v3 01/10] luks2: Fix use of incorrect index and some grub_error() messages.

In reply to Daniel Kiper
Quoted 33 lines · Daniel Kiper
Quoted 32 lines · Glenn Washburn

When looping over the digests and segments, the loop variable is j, but the variable i is used to index in the the digests and segments json array. The variable i is the keyslot index. Similarly, there are several grub_error() statements using the wrong index in constructing the error string.

---
 grub-core/disk/luks2.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
grub-core/disk/luks2.c +3 -3
index 31d7166fc..2241e0312 100644
@@ -275,34 +275,34 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
   for (j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&digest, &digests, i) ||
+      if (grub_json_getchild (&digest, &digests, j) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
 
       if ((d->keyslots & (1 << idx)))
 	break;
     }
   if (j == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, i);
 
Quoted 8 lines

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 293

Quoted 6 lines
grub-core/disk/luks2.c +1 -1
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
       grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-  for (j = 0; j < size; j++)
+  for (i = j, j = 0; j < size; j++)

Should not it be "i = j = 0" instead of "i = j, j = 0"?

The intent is to save the digest index here...

Quoted 18 lines
Quoted 5 lines
grub-core/disk/luks2.c +1 -1
     {
-      if (grub_json_getchild (&segment, &segments, i) ||
+      if (grub_json_getchild (&segment, &segments, j) ||
 	  grub_json_getuint64 (&idx, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 305

Quoted 10 lines
grub-core/disk/luks2.c +2 -2
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
 
       if ((d->segments & (1 << idx)))
 	break;
     }
   if (j == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE, i);

s/PRIuGRUB_SIZE, i/PRIuGRUB_SIZE, j/?

Quoted 1 line

Daniel

... which then gets used here. It initially confused me as well, but it should be correct. The problem is the awkward naming, but the patch following this one improves the situation.

Patrick

Reviewed-by on v3 3/10
#45 lore raw

Re: [PATCH v3 03/10] luks2: Use more intuitive keyslot key instead of index when naming keyslot.

In reply to Glenn Washburn
Quoted 10 lines · Glenn Washburn

Use the keyslot key value in the keyslot json array rather than the index of the keyslot in the json array. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore,with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

Patrick

Quoted 91 lines
---
 grub-core/disk/luks2.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +12 -11
index c3cd63606..4e1e47161 100644
@@ -65,6 +65,7 @@ typedef struct grub_luks2_header grub_luks2_header_t;
 
 struct grub_luks2_keyslot
 {
+  grub_uint64_t slot_key;
   grub_int64_t key_size;
   grub_int64_t priority;
   struct
@@ -259,12 +260,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t keyslot_key, digest_key, segment_key;
+  grub_uint64_t digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
@@ -281,11 +282,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
           luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
-      if ((d->keyslots & (1 << keyslot_key)))
+      if ((d->keyslots & (1 << k->slot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -599,11 +600,11 @@ luks2_recover_key (grub_disk_t disk,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -618,16 +619,16 @@ luks2_recover_key (grub_disk_t disk,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -635,7 +636,7 @@ luks2_recover_key (grub_disk_t disk,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot.slot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
Reviewed-by on v3 9/10
#70 lore raw

Re: [PATCH v3 09/10] cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

This makes it clear that the offset represents sectors, not bytes, in order to improve readability.

Patrick

Quoted 122 lines
---
 grub-core/disk/cryptodisk.c | 10 +++++-----
 grub-core/disk/geli.c       |  2 +-
 grub-core/disk/luks.c       |  4 ++--
 grub-core/disk/luks2.c      |  4 ++--
 include/grub/cryptodisk.h   |  7 ++++++-
 5 files changed, 16 insertions(+), 11 deletions(-)
grub-core/disk/cryptodisk.c +5 -5
index f5d098f65..64a51165c 100644
@@ -766,10 +766,10 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Reading %" PRIuGRUB_SIZE " sectors from sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   err = grub_disk_read (dev->source_disk,
-			grub_disk_from_native_sector (disk, sector + dev->offset),
+			grub_disk_from_native_sector (disk, sector + dev->offset_sectors),
 			0, size << disk->log_sector_size, buf);
   if (err)
     {
@@ -815,7 +815,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Writing %" PRIuGRUB_SIZE " sectors to sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
@@ -827,7 +827,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
     }
 
   /* Since ->write was called so disk.mod is loaded but be paranoid  */
-  sector = sector + dev->offset;
+  sector = sector + dev->offset_sectors;
   if (grub_disk_write_weak)
     err = grub_disk_write_weak (dev->source_disk,
 				grub_disk_from_native_sector (disk, sector),
@@ -1240,7 +1240,7 @@ luks_script_get (grub_size_t *sz)
 	ptr = grub_stpcpy (ptr, "luks_mount ");
 	ptr = grub_stpcpy (ptr, i->uuid);
 	*ptr++ = ' ';
-	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset);
+	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset_sectors);
 	while (*ptr)
 	  ptr++;
 	for (iptr = i->cipher->cipher->name; *iptr; iptr++)
grub-core/disk/geli.c +1 -1
index 4ec875821..0175ce4c4 100644
@@ -361,7 +361,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
     }
   newdev->cipher = cipher;
   newdev->secondary_cipher = secondary_cipher;
-  newdev->offset = 0;
+  newdev->offset_sectors = 0;
   newdev->source_disk = NULL;
   newdev->benbi_log = 0;
   if (grub_le_to_cpu16 (header.alg) == 0x16)
grub-core/disk/luks.c +2 -2
index 545734342..88f2da493 100644
@@ -122,10 +122,10 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev = grub_zalloc (sizeof (struct grub_cryptodisk));
   if (!newdev)
       return NULL;
-  newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
+  newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
-  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index bae54936c..cd573208a 100644
@@ -612,12 +612,12 @@ luks2_recover_key (grub_disk_t disk,
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
       /* Set up disk according to keyslot's segment. */
-      crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
+      crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
 	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
-			       - crypt->offset;
+			       - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
include/grub/cryptodisk.h +6 -1
index 9e10ae927..c8eef2204 100644
@@ -72,7 +72,12 @@ struct grub_cryptodisk
   struct grub_cryptodisk **prev;
 
   char *source;
-  grub_disk_addr_t offset;
+
+  /*
+   * The number of sectors the start of the encrypted data is offset into the
+   * underlying disk, where sectors are the size noted by log_sector_size.
+   */
+  grub_disk_addr_t offset_sectors;
   /* Total number of encrypted sectors of size (1<<log_sector_size) */
   grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
Signature · 1 line
2.27.0
Reviewed-by on v3 8/10
#67 lore raw

Re: [PATCH v3 08/10] cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

This creates an alignment with grub_disk_t naming of the same field and is more intuitive as to how it should be used.

Patrick

Quoted 81 lines
---
 grub-core/disk/cryptodisk.c | 2 +-
 grub-core/disk/geli.c       | 2 +-
 grub-core/disk/luks.c       | 2 +-
 grub-core/disk/luks2.c      | 4 ++--
 include/grub/cryptodisk.h   | 3 ++-
 5 files changed, 7 insertions(+), 6 deletions(-)
grub-core/disk/cryptodisk.c +1 -1
index 1a91c2d55..f5d098f65 100644
@@ -711,7 +711,7 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk)
     }
 
   disk->data = dev;
-  disk->total_sectors = dev->total_length;
+  disk->total_sectors = dev->total_sectors;
   disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
   disk->id = dev->id;
   dev->ref++;
grub-core/disk/geli.c +1 -1
index e9d23299a..4ec875821 100644
@@ -391,7 +391,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
 
   newdev->modname = "geli";
 
-  newdev->total_length = grub_disk_get_size (disk) - 1;
+  newdev->total_sectors = grub_disk_get_size (disk) - 1;
   grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
   COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= 32 * 2 + 1);
   return newdev;
grub-core/disk/luks.c +1 -1
index 20cc20b9b..545734342 100644
@@ -125,7 +125,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
-  newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index b9e0e98e1..bae54936c 100644
@@ -616,10 +616,10 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_length = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
 			       - crypt->offset;
       else
-	crypt->total_length = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
include/grub/cryptodisk.h +2 -1
index 006f3ec49..9e10ae927 100644
@@ -73,7 +73,8 @@ struct grub_cryptodisk
 
   char *source;
   grub_disk_addr_t offset;
-  grub_disk_addr_t total_length;
+  /* Total number of encrypted sectors of size (1<<log_sector_size) */
+  grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
   int ref;
   grub_crypto_cipher_handle_t cipher;
Signature · 1 line
2.27.0
#60 lore raw

Re: [PATCH v3 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Glenn Washburn
Quoted 45 lines · Glenn Washburn

This should improve readability of code by providing clues as to what the value represents.

---
 grub-core/disk/cryptodisk.c | 12 +++++++-----
 include/grub/types.h        |  3 +++
 2 files changed, 10 insertions(+), 5 deletions(-)
grub-core/disk/cryptodisk.c +7 -5
index 623f0f396..1a91c2d55 100644
@@ -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)
include/grub/types.h +0 -0
index 035a4b528..8b4267ebd 100644

include/grub/types.h static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val) · line 322

Quoted 4 lines
include/grub/types.h +1 -0
@@ -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)

Hum. I'd rather name this GRUB_TYPE_BITS, as the current name implies that we only get a single bit. Other than that the change looks good to me and I agree that it helps readability.

Patrick

Quoted 6 lines
include/grub/types.h +2 -0
+#define GRUB_TYPE_MAX(type) ((2 * ((1ULL << (GRUB_TYPE_BIT(type) - 1)) - 1)) + 1)
+
 #endif /* ! GRUB_TYPES_HEADER */
Signature · 1 line
2.27.0
Reviewed-by on v3 10/10
#73 lore raw

Re: [PATCH v3 10/10] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

This makes it more obvious to the reader that the disk referred to is the source disk, as opposed to say the disk holding the cryptodisk.

Patrick

Quoted 86 lines
---
 grub-core/disk/luks2.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +11 -11
index cd573208a..ccb94e80d 100644
@@ -417,7 +417,7 @@ luks2_verify_key (grub_luks2_digest_t *d, grub_uint8_t *candidate_key,
 
 static grub_err_t
 luks2_decrypt_key (grub_uint8_t *out_key,
-		   grub_disk_t disk, grub_cryptodisk_t crypt,
+		   grub_disk_t source, grub_cryptodisk_t crypt,
 		   grub_luks2_keyslot_t *k,
 		   const grub_uint8_t *passphrase, grub_size_t passphraselen)
 {
@@ -493,7 +493,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
     }
 
   grub_errno = GRUB_ERR_NONE;
-  ret = grub_disk_read (disk, 0, k->area.offset, k->area.size, split_key);
+  ret = grub_disk_read (source, 0, k->area.offset, k->area.size, split_key);
   if (ret)
     {
       grub_error (GRUB_ERR_IO, "Read error: %s\n", grub_errmsg);
@@ -537,7 +537,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
 }
 
 static grub_err_t
-luks2_recover_key (grub_disk_t disk,
+luks2_recover_key (grub_disk_t source,
 		   grub_cryptodisk_t crypt)
 {
   grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
@@ -552,7 +552,7 @@ luks2_recover_key (grub_disk_t disk,
   grub_json_t *json = NULL, keyslots;
   grub_err_t ret;
 
-  ret = luks2_read_header (disk, &header);
+  ret = luks2_read_header (source, &header);
   if (ret)
     return ret;
 
@@ -561,7 +561,7 @@ luks2_recover_key (grub_disk_t disk,
       return GRUB_ERR_OUT_OF_MEMORY;
 
   /* Read the JSON area. */
-  ret = grub_disk_read (disk, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
+  ret = grub_disk_read (source, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
 			grub_be_to_cpu64 (header.hdr_size) - sizeof (header), json_header);
   if (ret)
       goto err;
@@ -578,10 +578,10 @@ luks2_recover_key (grub_disk_t disk,
     }
 
   /* Get the passphrase from the user. */
-  if (disk->partition)
-    part = grub_partition_get_name (disk->partition);
-  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), disk->name,
-		disk->partition ? "," : "", part ? : "",
+  if (source->partition)
+    part = grub_partition_get_name (source->partition);
+  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
+		source->partition ? "," : "", part ? : "",
 		crypt->uuid);
   if (!grub_password_get (passphrase, MAX_PASSPHRASE))
     {
@@ -616,12 +616,12 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
 			       - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
-      ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
+      ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
Signature · 1 line
2.27.0
Reviewed-by on v3 4/10
#47 lore raw

Re: [PATCH v3 04/10] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors

In reply to Glenn Washburn
Quoted 10 lines · Glenn Washburn

The total_length field is named confusingly because length usually refers to bytes, whereas in this case its really the total number of sectors on the device. Also counter-intuitively, grub_disk_get_size returns the total number of device native sectors. We need to convert the sectors from the size of the underlying device to the cryptodisk sector size. And segment.size is in bytes which need to be converted to cryptodisk sectors.

Also, removed an empty statement.

Patrick

Quoted 33 lines
---
 grub-core/disk/luks2.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +4 -3
index 4e1e47161..311d18684 100644
@@ -425,7 +425,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
   grub_uint8_t salt[GRUB_CRYPTODISK_MAX_KEYLEN];
   grub_uint8_t *split_key = NULL;
   grub_size_t saltlen = sizeof (salt);
-  char cipher[32], *p;;
+  char cipher[32], *p;
   const gcry_md_spec_t *hash;
   gcry_err_code_t gcry_ret;
   grub_err_t ret;
@@ -611,9 +611,10 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_length = grub_disk_get_size (disk) - crypt->offset;
+	crypt->total_length = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
+			       - crypt->offset;
       else
-	crypt->total_length = grub_strtoull (segment.size, NULL, 10);
+	crypt->total_length = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
Signature · 1 line
2.27.0
#51 lore raw

Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Glenn Washburn
Quoted 230 lines · Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 52 ++++++++++++++++++++++---------------
 grub-core/disk/luks.c       |  5 ++--
 grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
grub-core/disk/cryptodisk.c +31 -21
index a3d672f68..623f0f396 100644
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
     return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len)
 	    : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));
 
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector << dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len);
 	    dev->iv_hash->write (ctx, &tmp, sizeof (tmp));
@@ -281,14 +282,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native sector
+	   * number. If using 32 bit IV mode, zero out the most significant
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;
+	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 - GRUB_CRYPTODISK_IV_LOG_SIZE));
+	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
+	      iv[1] = 0;
+	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
-	  iv[1] = grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size));
-	  iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
+	  iv[1] = grub_cpu_to_le32 (sector >> (32 - log_sector_size));
+	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
 				    & 0xFFFFFFFF);
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
@@ -311,10 +321,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_CBC:
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_cbc_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -322,10 +332,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_PCBC:
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_pcbc_decrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -337,7 +347,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (err)
 	      return err;
 
-	    for (j = 0; j < (1U << dev->log_sector_size);
+	    for (j = 0; j < (1U << log_sector_size);
 		 j += dev->cipher->cipher->blocksize)
 	      {
 		grub_crypto_xor (data + i + j, data + i + j, iv,
@@ -368,11 +378,11 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (do_encrypt)
 	      err = grub_crypto_ecb_encrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    else
 	      err = grub_crypto_ecb_decrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    if (err)
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -381,10 +391,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_ECB:
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  else
 	    err = grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  if (err)
 	    return err;
 	  break;
@@ -399,9 +409,9 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector)
+			 grub_disk_addr_t sector, grub_size_t log_sector_size)
 {
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector, log_sector_size, 0);
 }
 
 grub_err_t
@@ -766,7 +776,7 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
     }
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
 					size << disk->log_sector_size,
-					sector, 0);
+					sector, dev->log_sector_size, 0);
   return grub_crypto_gcry_error (gcry_err);
 }
 
@@ -807,7 +817,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
-					sector, 1);
+					sector, disk->log_sector_size, 1);
   if (gcry_err)
     {
       grub_free (tmp);
grub-core/disk/luks.c +3 -2
index 59702067a..20cc20b9b 100644
@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
-  newdev->log_sector_size = 9;
+  newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
   newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,
 	  return err;
 	}
 
-      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
+      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0,
+					  LUKS1_LOG_SECTOR_SIZE);
       if (gcry_err)
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 311d18684..b9e0e98e1 100644
@@ -500,7 +500,12 @@ luks2_decrypt_key (grub_uint8_t *out_key,
       goto err;
     }
 
-  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0);
+  /*
+   * The key slots area is always encrypted in 512-byte sectors,
+   * regardless of encrypted data sector size.
+   */
+  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0,
+				      LUKS1_LOG_SECTOR_SIZE);
   if (gcry_ret)
     {
       ret = grub_crypto_gcry_error (gcry_ret);
include/grub/cryptodisk.h +0 -0
index e1b21e785..006f3ec49 100644

include/grub/cryptodisk.h typedef enum · line 52

Quoted 5 lines
include/grub/cryptodisk.h +2 -0
@@ -48,6 +48,12 @@ typedef enum
 
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
+/* LUKS1 specification defines the block size to always be 512 bytes. */
+#define LUKS1_LOG_SECTOR_SIZE 9

Nit: This should probably be called GRUB_LUKS1_LOG_SECTOR_SIZE. Otherwise the patch looks good to me.

Patrick

Quoted 18 lines
include/grub/cryptodisk.h +4 -1
+/* By default dm-crypt increments the IV every 512 bytes. */
+#define GRUB_CRYPTODISK_IV_LOG_SIZE 9
+
 #define GRUB_CRYPTODISK_GF_LOG_SIZE 7
 #define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)
 #define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE - 3)
@@ -139,7 +145,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector);
+			 grub_disk_addr_t sector, grub_size_t log_sector_size);
 grub_err_t
 grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
 			grub_disk_t source);
Signature · 1 line
2.27.0
#52 lore raw

Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.

Quoted 232 lines · Patrick Steinhardt
Quoted 231 lines · Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 52 ++++++++++++++++++++++---------------
 grub-core/disk/luks.c       |  5 ++--
 grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
grub-core/disk/cryptodisk.c +31 -21
index a3d672f68..623f0f396 100644
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
     return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len)
 	    : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));
 
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector << dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len);
 	    dev->iv_hash->write (ctx, &tmp, sizeof (tmp));
@@ -281,14 +282,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native sector
+	   * number. If using 32 bit IV mode, zero out the most significant
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;
+	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 - GRUB_CRYPTODISK_IV_LOG_SIZE));
+	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
+	      iv[1] = 0;
+	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
-	  iv[1] = grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size));
-	  iv[0] = grub_cpu_to_le32 ((sector << dev->log_sector_size)
+	  iv[1] = grub_cpu_to_le32 (sector >> (32 - log_sector_size));
+	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
 				    & 0xFFFFFFFF);
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
@@ -311,10 +321,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_CBC:
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_cbc_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -322,10 +332,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_PCBC:
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_pcbc_decrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -337,7 +347,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (err)
 	      return err;
 
-	    for (j = 0; j < (1U << dev->log_sector_size);
+	    for (j = 0; j < (1U << log_sector_size);
 		 j += dev->cipher->cipher->blocksize)
 	      {
 		grub_crypto_xor (data + i + j, data + i + j, iv,
@@ -368,11 +378,11 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (do_encrypt)
 	      err = grub_crypto_ecb_encrypt (dev->cipher, data + i,
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    else
 	      err = grub_crypto_ecb_decrypt (dev->cipher, data + i,
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    if (err)
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -381,10 +391,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_ECB:
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  else
 	    err = grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  if (err)
 	    return err;
 	  break;
@@ -399,9 +409,9 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector)
+			 grub_disk_addr_t sector, grub_size_t log_sector_size)
 {
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector, log_sector_size, 0);
 }
 
 grub_err_t
@@ -766,7 +776,7 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
     }
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
 					size << disk->log_sector_size,
-					sector, 0);
+					sector, dev->log_sector_size, 0);
   return grub_crypto_gcry_error (gcry_err);
 }
 
@@ -807,7 +817,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
-					sector, 1);
+					sector, disk->log_sector_size, 1);
   if (gcry_err)
     {
       grub_free (tmp);
grub-core/disk/luks.c +3 -2
index 59702067a..20cc20b9b 100644
@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
-  newdev->log_sector_size = 9;
+  newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
   newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,
 	  return err;
 	}
 
-      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
+      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0,
+					  LUKS1_LOG_SECTOR_SIZE);
       if (gcry_err)
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 311d18684..b9e0e98e1 100644
@@ -500,7 +500,12 @@ luks2_decrypt_key (grub_uint8_t *out_key,
       goto err;
     }
 
-  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0);
+  /*
+   * The key slots area is always encrypted in 512-byte sectors,
+   * regardless of encrypted data sector size.
+   */
+  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0,
+				      LUKS1_LOG_SECTOR_SIZE);
   if (gcry_ret)
     {
       ret = grub_crypto_gcry_error (gcry_ret);
include/grub/cryptodisk.h +0 -0
index e1b21e785..006f3ec49 100644
@@ -48,6 +48,12 @@ typedef enum
 
Quoted 6 lines

include/grub/cryptodisk.h typedef enum · line 52

Quoted 4 lines
include/grub/cryptodisk.h +2 -0
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
+/* LUKS1 specification defines the block size to always be 512 bytes. */
+#define LUKS1_LOG_SECTOR_SIZE 9

Nit: This should probably be called GRUB_LUKS1_LOG_SECTOR_SIZE.

Yep.

Quoted 1 line

Otherwise the patch looks good to me.

Same for me, Reviewed-by: Daniel Kiper <[email protected]>

I can fix this minor issue before committing the patch.

Daniel

#61 lore raw

Re: [PATCH v3 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Patrick Steinhardt

Oct 23, 2020 1:00:59 PM Patrick Steinhardt <[email protected]>:

Quoted 49 lines · Glenn Washburn
Quoted 48 lines

This should improve readability of code by providing clues as to what the value represents.

---
grub-core/disk/cryptodisk.c | 12 +++++++-----
include/grub/types.h        |  3 +++

2 files changed, 10 insertions(+), 5 deletions(-)
grub-core/disk/cryptodisk.c +0 -0
index 623f0f396..1a91c2d55 100644
@@ -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)

include/grub/types.h +0 -0
index 035a4b528..8b4267ebd 100644
@@ -319,4 +319,7 @@ static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val)
 
 #define GRUB_CHAR_BIT 8
 
Quoted 7 lines

include/grub/types.h static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val) · line 322

Quoted 1 line
include/grub/types.h +1 -0
+#define GRUB_TYPE_BIT(type) (sizeof(type) * GRUB_CHAR_BIT)

Hum. I'd rather name this GRUB_TYPE_BITS, as the current name implies that we only get a single bit. Other than that the change looks good to me and I agree that it helps readability.

Patrick

Yes, I would've liked to also, but I decided to stick with the naming convention as can be seen in the definition of GRUB_CHAR_BIT above it. Should there be a separate patch renaming GRUB_CHAR_BIT also?

Quoted 7 lines
Quoted 7 lines
include/grub/types.h +2 -1
+#define GRUB_TYPE_MAX(type) ((2 * ((1ULL << (GRUB_TYPE_BIT(type) - 1)) - 1)) + 1)
+
 #endif /* ! GRUB_TYPES_HEADER */
--

2.27.0

#63 lore raw

Re: [PATCH v3 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Glenn Washburn
Quoted 14 lines · Glenn Washburn

This should improve readability of code by providing clues as to what the value represents.

---
 grub-core/disk/cryptodisk.c | 12 +++++++-----
 include/grub/types.h        |  3 +++
 2 files changed, 10 insertions(+), 5 deletions(-)
grub-core/disk/cryptodisk.c +0 -0
index 623f0f396..1a91c2d55 100644

grub-core/disk/cryptodisk.c grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, · line 301

Quoted 6 lines
grub-core/disk/cryptodisk.c +2 -1
@@ -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])

Missing space between macro name and "(".

grub-core/disk/cryptodisk.c grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, · line 309

Quoted 11 lines
grub-core/disk/cryptodisk.c +3 -3
+					       - 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]));

Ditto.

grub-core/disk/cryptodisk.c grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, · line 310

Quoted 1 line
grub-core/disk/cryptodisk.c +1 -0
+	    iv[sz - 1] = grub_cpu_to_be32 (num & GRUB_TYPE_MAX(iv[0]));

Ditto.

grub-core/disk/cryptodisk.c grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, · line 314

Quoted 5 lines
grub-core/disk/cryptodisk.c +1 -1
 	  }
 	  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]));

Ditto.

include/grub/types.h static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val) · line 320

Quoted 10 lines
grub-core/disk/cryptodisk.c +0 -0
 	  err = grub_crypto_ecb_encrypt (dev->essiv_cipher, iv, iv,
 					 dev->cipher->cipher->blocksize);
 	  if (err)
include/grub/types.h +0 -0
index 035a4b528..8b4267ebd 100644
@@ -319,4 +319,7 @@ static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val)
 
 #define GRUB_CHAR_BIT 8

I think we should not change the name of this constant. It seems to me that it was named in similar way to C/gcc/clang CHAR_BIT and/or __CHAR_BIT__. However, I think we should change its definition. It should be

  #define GRUB_CHAR_BIT __CHAR_BIT__

Additionally, the build should stop if __CHAR_BIT__ is not defined nor GRUB_CHAR_BIT is not equal 8.

Last but not least, the GRUB_CHAR_BIT definition should be right behind includes in the include/grub/types.h.

include/grub/types.h static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val) · line 322

Quoted 1 line
include/grub/types.h +1 -0
+#define GRUB_TYPE_BIT(type) (sizeof(type) * GRUB_CHAR_BIT)

I agree with Patrick, it should be named GRUB_TYPE_BITS.

include/grub/types.h static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val) · line 323

Quoted 1 line
include/grub/types.h +1 -0
+#define GRUB_TYPE_MAX(type) ((2 * ((1ULL << (GRUB_TYPE_BIT(type) - 1)) - 1)) + 1)

This only works for unsigned types. So, I think we should have three (four?) macros here: (GRUB_TYPE_U_MIN? (which is obviously 0)), GRUB_TYPE_U_MAX, GRUB_TYPE_S_MIN and GRUB_TYPE_S_MAX.

Daniel

Reviewed-by
#68 lore raw

Re: [PATCH v3 08/10] cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.

In reply to Patrick Steinhardt
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn

This creates an alignment with grub_disk_t naming of the same field and is more intuitive as to how it should be used.

Daniel

Reviewed-by
#74 lore raw

Re: [PATCH v3 10/10] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

In reply to Patrick Steinhardt
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn

This makes it more obvious to the reader that the disk referred to is the source disk, as opposed to say the disk holding the cryptodisk.

Daniel

#75 lore raw
In reply to Glenn Washburn
Quoted 19 lines · Glenn Washburn

Heres an updated patch series which addresses comment from Patrick. The only code change is adding a slot_key member to grub_luks2_keyslot and using that instead of an extra out parameter to luks2_get_keyslot.

Glenn Washburn (10):
luks2: Fix use of incorrect index and some grub_error() messages.
luks2: Improve readability in luks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming
keyslot.
luks2: grub_cryptodisk_t->total_length is the max number of device
native sectors
cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
cryptodisk: Properly handle non-512 byte sized sectors.
cryptodisk: Replace some literals with constants in
grub_cryptodisk_endecrypt.
cryptodisk: Rename total_length field in grub_cryptodisk_t to
total_sectors.
cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as in

Quoted 1 line
    luks.c.

Most of LUKS2 patches require rework. The rest which got Patrick's and my RB I will push into the GRUB git repo by the end of this week. Of course if there are no objections...

Daniel

#62 lore raw

Re: [PATCH v3 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Glenn Washburn
Quoted 57 lines · Glenn Washburn

Oct 23, 2020 1:00:59 PM Patrick Steinhardt <[email protected]>:

Quoted 55 lines · Glenn Washburn
Quoted 45 lines

This should improve readability of code by providing clues as to what the value represents.

---
grub-core/disk/cryptodisk.c | 12 +++++++-----
include/grub/types.h        |  3 +++

2 files changed, 10 insertions(+), 5 deletions(-)
grub-core/disk/cryptodisk.c +0 -0
index 623f0f396..1a91c2d55 100644
@@ -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)

include/grub/types.h +0 -0
index 035a4b528..8b4267ebd 100644

include/grub/types.h static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val) · line 322

Quoted 4 lines
include/grub/types.h +1 -0
@@ -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)

Hum. I'd rather name this GRUB_TYPE_BITS, as the current name implies that we only get a single bit. Other than that the change looks good to me and I agree that it helps readability.

Quoted 5 lines
Quoted 1 line

Patrick

Yes, I would've liked to also, but I decided to stick with the naming convention as can be seen in the definition of GRUB_CHAR_BIT above it. Should there be a separate patch renaming GRUB_CHAR_BIT also?

Nah, your naming makes sense in that context. Let's keep it as-is in this patch.

Patrick

#64 lore raw

Re: [PATCH v3 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Daniel Kiper
Quoted 63 lines · On Tue, 27 Oct 2020 19:03:36 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 14 lines · Glenn Washburn

This should improve readability of code by providing clues as to what the value represents.

---
 grub-core/disk/cryptodisk.c | 12 +++++++-----
 include/grub/types.h        |  3 +++
 2 files changed, 10 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index 623f0f396..1a91c2d55 100644

grub-core/disk/cryptodisk.c grub_cryptodisk_endecrypt (struct · line 302

Quoted 7 lines
grub-core/disk/cryptodisk.c +2 -1
@@ -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])

Missing space between macro name and "(".

grub-core/disk/cryptodisk.c grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,

Quoted 12 lines
grub-core/disk/cryptodisk.c +3 -3
+					       - 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]));

Ditto.

grub-core/disk/cryptodisk.c grub_cryptodisk_endecrypt (struct · line 313

Quoted 2 lines
grub-core/disk/cryptodisk.c +1 -0
+	    iv[sz - 1] = grub_cpu_to_be32 (num &
 GRUB_TYPE_MAX(iv[0]));

Ditto.

grub-core/disk/cryptodisk.c grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, · line 314

Quoted 5 lines
grub-core/disk/cryptodisk.c +1 -1
 	  }
 	  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]));

Ditto.

Quoted 10 lines
grub-core/disk/cryptodisk.c +0 -0
 	  err = grub_crypto_ecb_encrypt (dev->essiv_cipher, iv, iv,
 					 dev->cipher->cipher->blocksize);
 	  if (err)
include/grub/types.h +0 -0
index 035a4b528..8b4267ebd 100644
@@ -319,4 +319,7 @@ static inline void grub_set_unaligned64 (void
 *ptr, grub_uint64_t val)
 
Quoted 8 lines

include/grub/types.h static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val) · line 320

Quoted 1 line
include/grub/types.h +0 -0
 #define GRUB_CHAR_BIT 8

I think we should not change the name of this constant. It seems to me that it was named in similar way to C/gcc/clang CHAR_BIT and/or __CHAR_BIT__. However, I think we should change its definition. It should be

  #define GRUB_CHAR_BIT __CHAR_BIT__

I can add that into this commit.

Quoted 2 lines

Additionally, the build should stop if __CHAR_BIT__ is not defined nor GRUB_CHAR_BIT is not equal 8.

I'm not sure exactly how you want that done.

Quoted 6 lines

Last but not least, the GRUB_CHAR_BIT definition should be right behind includes in the include/grub/types.h.

include/grub/types.h static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val) · line 322

Quoted 1 line
include/grub/types.h +1 -0
+#define GRUB_TYPE_BIT(type) (sizeof(type) * GRUB_CHAR_BIT)

I agree with Patrick, it should be named GRUB_TYPE_BITS.

Patrick says my logic makes sense for him. I can still change it, yes?

Quoted 6 lines

include/grub/types.h static inline void grub_set_unaligned64 (void · line 324

Quoted 2 lines
include/grub/types.h +1 -0
+#define GRUB_TYPE_MAX(type) ((2 * ((1ULL << (GRUB_TYPE_BIT(type) -
 1)) - 1)) + 1)

This only works for unsigned types. So, I think we should have three (four?) macros here: (GRUB_TYPE_U_MIN? (which is obviously 0)), GRUB_TYPE_U_MAX, GRUB_TYPE_S_MIN and GRUB_TYPE_S_MAX.

How about I just rename GRUB_TYPE_MAX to GRUB_TYPE_U_MAX and someone (you?) can add in the rest? I'm not sure how I'd do the signed variants that works across all architectures.

Quoted 1 line

Daniel

#42 lore raw

Re: [PATCH v3 02/10] luks2: Improve readability in luks2_get_keyslot.

In reply to Daniel Kiper
Quoted 10 lines · On Fri, 23 Oct 2020 14:14:52 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 8 lines · Glenn Washburn

Introduce new variables keyslot_key, digest_key, and segment_key which represent the integer key of the item in the respective associative array when looping over the array items. This replaces using a generically named variable named "idx" used for all three values. The parameter "i" is renamed to "keyslot_idx" and the loop variable "j" is renamed to "i". Error messages now distinguish between indexes and keys. The former include "index" in the error string, and the later are surrounded in quotes.

Quoted 5 lines

I think this patch does too many things in one step. Please do one rename per patch if possible. Additionally, I think this patch, or series of the patches, should be before current patch #1.

Daniel

Okay, I've split these two patches into 5 patches:
5: luks2: Improve error messages in luks2_get_keyslot.
4: luks2: Split idx into three variables: keyslot_key, digest_key,
segment_key.
3: luks2: Rename index variable j to i.
2: luks2: Rename variable i to keyslot_idx in luks2_get_keyslot. 1: luks2: Use correct index variable when looping in luks2_get_keyslot.

I'm traveling the next few days and thinking that it would be best to send these with the next iteration of the patch series which includes some of the changes suggested for other patches.

Glenn

#65 lore raw

Re: [PATCH v3 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Glenn Washburn
Quoted 3 lines · Glenn Washburn
Quoted 1 line · On Tue, 27 Oct 2020 19:03:36 +0100 Daniel Kiper <[email protected]> wrote:

On Mon, Oct 19, 2020 at 06:09:55PM -0500, Glenn Washburn wrote:

[...]

Quoted 14 lines
Quoted 14 lines

include/grub/types.h static inline void grub_set_unaligned64 (void · line 321

Quoted 8 lines
include/grub/types.h +0 -0
index 035a4b528..8b4267ebd 100644
@@ -319,4 +319,7 @@ static inline void grub_set_unaligned64 (void
 *ptr, grub_uint64_t val)
 
 #define GRUB_CHAR_BIT 8

I think we should not change the name of this constant. It seems to me that it was named in similar way to C/gcc/clang CHAR_BIT and/or __CHAR_BIT__. However, I think we should change its definition. It should be

Quoted 3 lines
Quoted 1 line
  #define GRUB_CHAR_BIT __CHAR_BIT__

I can add that into this commit.

It has to be separate commit because it is logically different change.

Quoted 4 lines
Quoted 2 lines

Additionally, the build should stop if __CHAR_BIT__ is not defined nor GRUB_CHAR_BIT is not equal 8.

I'm not sure exactly how you want that done.

#ifndef __CHAR_BIT__
#error __CHAR_BIT__ is not defined
#else __CHAR_BIT__ != 8
#error __CHAR_BIT__ is not equal 8
#else
#define GRUB_CHAR_BIT __CHAR_BIT__
#endif

Quoted 8 lines
Quoted 6 lines

Last but not least, the GRUB_CHAR_BIT definition should be right behind includes in the include/grub/types.h.

include/grub/types.h static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val) · line 322

Quoted 1 line
include/grub/types.h +1 -0
+#define GRUB_TYPE_BIT(type) (sizeof(type) * GRUB_CHAR_BIT)

I agree with Patrick, it should be named GRUB_TYPE_BITS.

Patrick says my logic makes sense for him. I can still change it, yes?

Ugh... I missed his last email somehow. Sorry about that. I still think it should be GRUB_TYPE_BITS(). So, please rename GRUB_TYPE_BIT() to GRUB_TYPE_BITS(). GRUB_CHAR_BIT name should stay as is.

Quoted 10 lines
Quoted 6 lines

include/grub/types.h static inline void grub_set_unaligned64 (void · line 324

Quoted 2 lines
include/grub/types.h +1 -0
+#define GRUB_TYPE_MAX(type) ((2 * ((1ULL << (GRUB_TYPE_BIT(type) -
 1)) - 1)) + 1)

This only works for unsigned types. So, I think we should have three (four?) macros here: (GRUB_TYPE_U_MIN? (which is obviously 0)), GRUB_TYPE_U_MAX, GRUB_TYPE_S_MIN and GRUB_TYPE_S_MAX.

How about I just rename GRUB_TYPE_MAX to GRUB_TYPE_U_MAX and someone (you?) can add in the rest? I'm not sure how I'd do the signed variants that works across all architectures.

I am not going to insist on adding GRUB_TYPE_S_MIN/GRUB_TYPE_S_MAX if you think it is complicated. Though please rename GRUB_TYPE_MAX to GRUB_TYPE_U_MAX.

Daniel

#43 lore raw

Re: [PATCH v3 02/10] luks2: Improve readability in luks2_get_keyslot.

In reply to Glenn Washburn
Quoted 18 lines · Glenn Washburn
Quoted 10 lines · On Fri, 23 Oct 2020 14:14:52 +0200 Daniel Kiper <[email protected]> wrote:
Quoted 8 lines · Glenn Washburn

Introduce new variables keyslot_key, digest_key, and segment_key which represent the integer key of the item in the respective associative array when looping over the array items. This replaces using a generically named variable named "idx" used for all three values. The parameter "i" is renamed to "keyslot_idx" and the loop variable "j" is renamed to "i". Error messages now distinguish between indexes and keys. The former include "index" in the error string, and the later are surrounded in quotes.

Quoted 5 lines

I think this patch does too many things in one step. Please do one rename per patch if possible. Additionally, I think this patch, or series of the patches, should be before current patch #1.

Daniel

Quoted 7 lines

Okay, I've split these two patches into 5 patches:
5: luks2: Improve error messages in luks2_get_keyslot.
4: luks2: Split idx into three variables: keyslot_key, digest_key,
segment_key.
3: luks2: Rename index variable j to i.
2: luks2: Rename variable i to keyslot_idx in luks2_get_keyslot. 1: luks2: Use correct index variable when looping in luks2_get_keyslot.

LGTM...

Quoted 3 lines

I'm traveling the next few days and thinking that it would be best to send these with the next iteration of the patch series which includes some of the changes suggested for other patches.

Yeah, that would be perfect.

Thank you for doing the work.

Have a nice weekend,

Daniel

#53 lore raw

Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Glenn Washburn
Quoted 68 lines · Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 52 ++++++++++++++++++++++---------------
 grub-core/disk/luks.c       |  5 ++--
 grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
grub-core/disk/cryptodisk.c +4 -4
index a3d672f68..623f0f396 100644
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
     return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len)
 	    : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));
 
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector << dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len);
 	    dev->iv_hash->write (ctx, &tmp, sizeof (tmp));
@@ -281,14 +282,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);

grub-core/disk/cryptodisk.c grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, · line 292

Quoted 10 lines
grub-core/disk/cryptodisk.c +7 -2
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native sector
+	   * number. If using 32 bit IV mode, zero out the most significant
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;

./configure --target=arm-linux-gnueabihf --with-platform=coreboot ... make

...and you get this:

disk/cryptodisk.c: In function ‘grub_cryptodisk_endecrypt’:
disk/cryptodisk.c:292:28: error: cast increases required alignment of target type [-Werror=cast-align]

        grub_uint64_t *iv64 = (grub_uint64_t *)iv;
                              ^

cc1: all warnings being treated as errors

I think every 32-bit build will/may fail. It seems to me that
(grub_uint64_t *)(void *) iv;
or
(grub_uint64_t *)(grub_addr_t) iv;
should help.

Daniel

#76 lore raw
In reply to Daniel Kiper
Quoted 22 lines · Daniel Kiper
Quoted 19 lines · Glenn Washburn

Heres an updated patch series which addresses comment from Patrick. The only code change is adding a slot_key member to grub_luks2_keyslot and using that instead of an extra out parameter to luks2_get_keyslot.

Glenn Washburn (10):
luks2: Fix use of incorrect index and some grub_error() messages.
luks2: Improve readability in luks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming
keyslot.
luks2: grub_cryptodisk_t->total_length is the max number of device
native sectors
cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
cryptodisk: Properly handle non-512 byte sized sectors.
cryptodisk: Replace some literals with constants in
grub_cryptodisk_endecrypt.
cryptodisk: Rename total_length field in grub_cryptodisk_t to
total_sectors.
cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as in

Quoted 1 line
    luks.c.
Quoted 3 lines

Most of LUKS2 patches require rework. The rest which got Patrick's and my RB I will push into the GRUB git repo by the end of this week. Of course if there are no objections...

I was only able to take 'cryptodisk: Fix cipher IV mode "plain64" always being set as "plain"' patch. The other patches depend on some changes in earlier patches in the series. So, feel free to add my RB to the patches which got it from me.

Daniel

#77 lore raw

Nov 2, 2020 9:56:58 AM Daniel Kiper <[email protected]>:

Quoted 27 lines · Daniel Kiper
Quoted 22 lines · Glenn Washburn
Quoted 4 lines

Heres an updated patch series which addresses comment from Patrick. The only code change is adding a slot_key member to grub_luks2_keyslot and using that instead of an extra out parameter to luks2_get_keyslot.

Quoted 16 lines

Glenn Washburn (10):
luks2: Fix use of incorrect index and some grub_error() messages.
luks2: Improve readability in luks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming keyslot.
luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
cryptodisk: Properly handle non-512 byte sized sectors.
cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

Quoted 3 lines

Most of LUKS2 patches require rework. The rest which got Patrick's and my RB I will push into the GRUB git repo by the end of this week. Of course if there are no objections...

Quoted 7 lines

I was only able to take 'cryptodisk: Fix cipher IV mode "plain64" always being set as "plain"' patch. The other patches depend on some changes in earlier patches in the series. So, feel free to add my RB to the patches which got it from me.

Daniel

If I move the rework the rename patches to be earlier in the patch series, you should be able to get those in right? It's annoying to keep them in the patch series while reworking the other patches.

Glenn

#78 lore raw
Quoted 33 lines · Glenn Washburn

Nov 2, 2020 9:56:58 AM Daniel Kiper <[email protected]>:

Quoted 32 lines · Daniel Kiper
Quoted 22 lines · Glenn Washburn
Quoted 4 lines

Heres an updated patch series which addresses comment from Patrick. The only code change is adding a slot_key member to grub_luks2_keyslot and using that instead of an extra out parameter to luks2_get_keyslot.

Quoted 16 lines

Glenn Washburn (10):
luks2: Fix use of incorrect index and some grub_error() messages.
luks2: Improve readability in luks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming keyslot.
luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
cryptodisk: Properly handle non-512 byte sized sectors.
cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

Quoted 3 lines

Most of LUKS2 patches require rework. The rest which got Patrick's and my RB I will push into the GRUB git repo by the end of this week. Of course if there are no objections...

I was only able to take 'cryptodisk: Fix cipher IV mode "plain64" always being set as "plain"' patch. The other patches depend on some changes in earlier patches in the series. So, feel free to add my RB to the patches which got it from me.

Quoted 5 lines
Quoted 1 line

Daniel

If I move the rework the rename patches to be earlier in the patch series, you should be able to get those in right? It's annoying to keep them in the patch series while reworking the other patches.

Yeah, it should help. Anyway, I think you can put all the patches with RB at the beginning of the series. Then I will take them without any issues.

Daniel

#54 lore raw

Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Daniel Kiper
Quoted 99 lines · On Fri, 30 Oct 2020 21:47:14 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 82 lines · Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 52
++++++++++++++++++++++--------------- grub-core/disk/luks.c       |
 5 ++-- grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index a3d672f68..623f0f396 100644

grub-core/disk/cryptodisk.c +2 -1
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t
 log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
grub-core/disk/cryptodisk.c +0 -0
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len) : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));

grub-core/disk/cryptodisk.c +2 -2
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector <<
 dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix,

dev->iv_prefix_len); dev->iv_hash->write (ctx, &tmp, sizeof (tmp));

grub-core/disk/cryptodisk.c +2 -3
@@ -281,14 +282,23 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native

sector

grub-core/disk/cryptodisk.c +1 -0
+	   * number. If using 32 bit IV mode, zero out the most

significant

grub-core/disk/cryptodisk.c grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, · line 292

Quoted 4 lines
grub-core/disk/cryptodisk.c +4 -0
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;

./configure --target=arm-linux-gnueabihf --with-platform=coreboot ... make

...and you get this:

disk/cryptodisk.c: In function ‘grub_cryptodisk_endecrypt’:
disk/cryptodisk.c:292:28: error: cast increases required alignment
of target type [-Werror=cast-align] grub_uint64_t *iv64 = (grub_uint64_t *)iv; ^
cc1: all warnings being treated as errors

Quoted 5 lines

I think every 32-bit build will/may fail. It seems to me that
(grub_uint64_t *)(void *) iv;
or
(grub_uint64_t *)(grub_addr_t) iv;
should help.

I'm having issues building for arm-linux-gnueabihf, so I can't effectively test this. This code does compile for --target=i386 --with-platform=pc, which I believe is 32-bit.

Since I can't test your suggestion, I can't verify that it works. However, I suspect it may get rid of the compiler problem, but not solve the underlying issue, which is that iv may not be 8-byte aligned. My guess is that if the compiler message goes away you could get a crash at the next line if iv is not 8-byte aligned. Can you see if the warning disappears if you define iv like this:

  grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4]
  __attribute__((aligned(sizeof(grub_uint64_t))));

I'm not sure the best way to handle this the "grub way". Do you see a better way to do this?

Glenn

#79 lore raw

v4 cover Cryptodisk fixes for v2.06 redux

In reply to Glenn Washburn

Here we go again. I have not added RB sigs to any patches since I've had to modify them in moving them earlier in the patch set. This would be the first three rename patches. The first two patches of v3 have been reworked into 5 patches (05-09) which combine to be equivalent. Most of the changes since v3 are added error handling around luks2 cryptodisk setup. Also, I addressed an IV alignment issue in grub_cryptodisk_endecrypt for the GRUB_CRYPTODISK_MODE_IV_PLAIN* case, found by Daniel. I'm not sure it works (I can't reproduce the build error) or that its the desired way of doing it.

Glenn

Glenn Washburn (15):
cryptodisk: Rename total_length field in grub_cryptodisk_t to
total_sectors.
cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as in

    luks.c.

types: Define GRUB_CHAR_BIT based on compiler macro instead of using
literal.
luks2: Use correct index variable when looping in luks2_get_keyslot.
luks2: Rename variable i to keyslot_idx in luks2_get_keyslot.
luks2: Rename index variable j to i.
luks2: Split idx into three variables: keyslot_key, digest_key,
segment_key.
luks2: Improve error messages in luks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming
keyslot.
cryptodisk: Replace some literals with constants in
grub_cryptodisk_endecrypt.
luks2: grub_cryptodisk_t->total_length is the max number of device
native sectors
cryptodisk: Properly handle non-512 byte sized sectors.
luks2: Better error handling when setting up the cryptodisk.
luks2: Error check segment.sector_size.

 grub-core/disk/cryptodisk.c |  76 ++++++++++-------
 grub-core/disk/geli.c       |   4 +-
 grub-core/disk/luks.c       |   9 +-
 grub-core/disk/luks2.c      | 165 +++++++++++++++++++++++++++---------
 include/grub/cryptodisk.h   |  18 +++-
 include/grub/misc.h         |   2 +
 include/grub/types.h        |  15 +++-
 7 files changed, 205 insertions(+), 84 deletions(-)

Range-diff against v3:

 5:  e8e069cae =  1:  90fedb50c cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
 8:  28e0cac66 !  2:  d72632f3f cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
    @@ grub-core/disk/luks.c
     @@ grub-core/disk/luks.c: configure_ciphers (grub_disk_t disk, const char *check_uuid,
        newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
        newdev->source_disk = NULL;
    -   newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
    +   newdev->log_sector_size = 9;
     -  newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
     +  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
        grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
    @@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
            crypt->log_sector_size = sizeof (unsigned int) * 8
      		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
            if (grub_strcmp (segment.size, "dynamic") == 0)
    --	crypt->total_length = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
    -+	crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
    - 			       - crypt->offset;
    +-	crypt->total_length = grub_disk_get_size (disk) - crypt->offset;
    ++	crypt->total_sectors = grub_disk_get_size (disk) - crypt->offset;
            else
    --	crypt->total_length = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
    -+	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
    +-	crypt->total_length = grub_strtoull (segment.size, NULL, 10);
    ++	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
      
            ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
      			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));

9: 74c6232c9 ! 3: d79204f6c cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
@@ grub-core/disk/luks.c: configure_ciphers (grub_disk_t disk, const char *check_uu
- newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
+ newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset); newdev->source_disk = NULL;
- newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
+ newdev->log_sector_size = 9;
- newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
+ newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors; grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
@@ grub-core/disk/luks.c: configure_ciphers (grub_disk_t disk, const char *check_uu

## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
- grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
+ grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);

/* Set up disk according to keyslot's segment. */
- crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
crypt->log_sector_size = sizeof (unsigned int) * 8
- __builtin_clz ((unsigned int) segment.sector_size) - 1;
if (grub_strcmp (segment.size, "dynamic") == 0)
- crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
-- - crypt->offset;
-+ - crypt->offset_sectors;
+- crypt->total_sectors = grub_disk_get_size (disk) - crypt->offset;
++ crypt->total_sectors = grub_disk_get_size (disk) - crypt->offset_sectors;
else
- crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+ crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);

## include/grub/cryptodisk.h ##
10: 738fe0139 ! 4: cceded198 luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
crypt->log_sector_size = sizeof (unsigned int) * 8
- __builtin_clz ((unsigned int) segment.sector_size) - 1;
if (grub_strcmp (segment.size, "dynamic") == 0)
-- crypt->total_sectors = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
-+ crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
- - crypt->offset_sectors;
+- crypt->total_sectors = grub_disk_get_size (disk) - crypt->offset_sectors;
++ crypt->total_sectors = grub_disk_get_size (source) - crypt->offset_sectors;
else
- crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+ crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);

- ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
+ ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,

 -:  --------- >  5:  badbd93f8 types: Define GRUB_CHAR_BIT based on compiler macro instead of using literal.
 1:  80dd653c9 !  6:  4839ead9b luks2: Fix use of incorrect index and some grub_error() messages.

@@ Metadata
Author: Glenn Washburn <[email protected]>

## Commit message ##
- luks2: Fix use of incorrect index and some grub_error() messages.
+ luks2: Use correct index variable when looping in luks2_get_keyslot.

- When looping over the digests and segments, the loop variable is j, but the
- variable i is used to index in the the digests and segments json array. The
- variable i is the keyslot index. Similarly, there are several grub_error()
- statements using the wrong index in constructing the error string.
+ The loop variable j should be used to index the digests and segments json
+ array, instead of the variable i, which is the keyslot index.

## grub-core/disk/luks2.c ##
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d

if ((d->keyslots & (1 << idx)))
break;
- }
- if (j == size)
-- return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE);
-+ return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, i);
-
- /* Get segment that matches the digest. */
- if (grub_json_getvalue (&segments, root, "segments") ||
- grub_json_getsize (&size, &segments))
+@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-- for (j = 0; j < size; j++)
-+ for (i = j, j = 0; j < size; j++)
+ for (j = 0; j < size; j++)
{
- if (grub_json_getchild (&segment, &segments, i) ||
+ if (grub_json_getchild (&segment, &segments, j) ||
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d

if ((d->segments & (1 << idx)))
break;
- }
- if (j == size)
-- return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
-+ return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE, i);
-
- return GRUB_ERR_NONE;
- }

 2:  e4cf8ab40 <  -:  --------- luks2: Improve readability in luks2_get_keyslot.
 -:  --------- >  7:  21b745c11 luks2: Rename variable i to keyslot_idx in luks2_get_keyslot.
 -:  --------- >  8:  21a3962b9 luks2: Rename index variable j to i.
 -:  --------- >  9:  b3b361c49 luks2: Split idx into three variables: keyslot_key, digest_key, segment_key.
 -:  --------- > 10:  8ac7af919 luks2: Improve error messages in luks2_get_keyslot.
 3:  1f65a04e0 ! 11:  31b9f0b83 luks2: Use more intuitive keyslot key instead of index when naming keyslot.

@@ Commit message
example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index).
- Furthermore,with this change the keyslot number will align with the number
+ Furthermore, with this change the keyslot number will align with the number
used to reference the keyslot when using the --key-slot argument to cryptsetup.

@@ grub-core/disk/luks2.c: typedef struct grub_luks2_header grub_luks2_header_t;
grub_int64_t key_size;
grub_int64_t priority;
struct
+@@ grub-core/disk/luks2.c: typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
+
+ struct grub_luks2_segment
+ {
++ grub_uint64_t slot_key;
+ grub_uint64_t offset;
+ const char *size;
+ const char *encryption;
+@@ grub-core/disk/luks2.c: typedef struct grub_luks2_segment grub_luks2_segment_t;
+
+ struct grub_luks2_digest
+ {
++ grub_uint64_t slot_key;
+ /* Both keyslots and segments are interpreted as bitfields here */
+ grub_uint64_t keyslots;
+ grub_uint64_t segments;
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
{
grub_json_t keyslots, keyslot, digests, digest, segments, segment;
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d
return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);

- if ((d->keyslots & (1 << keyslot_key)))
++ d->slot_key = digest_key;
+ if ((d->keyslots & (1 << k->slot_key)))
break;
}
@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_d

/* Get segment that matches the digest. */
if (grub_json_getvalue (&segments, root, "segments") ||
-@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
+@@ grub-core/disk/luks2.c: luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
+ luks2_parse_segment (s, &segment))
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
+
++ s->slot_key = segment_key;
+ if ((d->segments & (1 << segment_key)))
+ break;
+ }
+@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,

if (keyslot.priority == 0)
{
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
+ grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);

/* Set up disk according to keyslot's segment. */
- crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
-@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
+ crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
+@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
(const grub_uint8_t *) passphrase, grub_strlen (passphrase));
if (ret)
{
@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
continue;
}

-@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk, +@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
* TRANSLATORS: It's a cryptographic key slot: one element of an array
* where each element is either empty or holds a key.
*/

 -:  --------- > 12:  87bafa5d9 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
 4:  fcb72f70d ! 13:  1bc8c867c luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
    @@ grub-core/disk/luks2.c: luks2_decrypt_key (grub_uint8_t *out_key,
        const gcry_md_spec_t *hash;
        gcry_err_code_t gcry_ret;
        grub_err_t ret;
    -@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t disk,
    +@@ grub-core/disk/luks2.c: luks2_recover_key (grub_disk_t source,
            crypt->log_sector_size = sizeof (unsigned int) * 8
      		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
            if (grub_strcmp (segment.size, "dynamic") == 0)
    --	crypt->total_length = grub_disk_get_size (disk) - crypt->offset;
    -+	crypt->total_length = (grub_disk_get_size (disk) >> (crypt->log_sector_size - disk->log_sector_size))
    -+			       - crypt->offset;
    +-	crypt->total_sectors = grub_disk_get_size (source) - crypt->offset_sectors;
    ++	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
    ++			       - crypt->offset_sectors;
            else
    --	crypt->total_length = grub_strtoull (segment.size, NULL, 10);
    -+	crypt->total_length = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
    +-	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
    ++	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
      
    -       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
    +       ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
      			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
 6:  6fe26ba56 ! 14:  c3b6adaeb cryptodisk: Properly handle non-512 byte sized sectors.
    @@ grub-core/disk/cryptodisk.c: grub_cryptodisk_endecrypt (struct grub_cryptodisk *
          {
            grub_size_t sz = ((dev->cipher->cipher->blocksize
      			 + sizeof (grub_uint32_t) - 1)
    + 			/ sizeof (grub_uint32_t));
    +-      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4];
    ++      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4] __attribute__((aligned (sizeof (grub_uint64_t))));
    + 
    +       if (dev->rekey)
    + 	{
     @@ grub-core/disk/cryptodisk.c: grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
      	    if (!ctx)
      	      return GPG_ERR_OUT_OF_MEMORY;
    @@ grub-core/disk/cryptodisk.c: grub_cryptodisk_endecrypt (struct grub_cryptodisk *
     -	  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]));
     +	  /*
     +	   * The IV is a 32 or 64 bit value of the dm-crypt native sector
     +	   * number. If using 32 bit IV mode, zero out the most significant
     +	   * 32 bits.
     +	   */
     +	  {
    -+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;
    ++	    grub_uint64_t *iv64 = (grub_uint64_t *) iv;
     +	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
     +						 - GRUB_CRYPTODISK_IV_LOG_SIZE));
     +	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
    @@ grub-core/disk/cryptodisk.c: grub_cryptodisk_endecrypt (struct grub_cryptodisk *
     +	  }
      	  break;
      	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
    --	  iv[1] = grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size));
    ++	  /* The IV is the 64 bit byte offset of the sector. */
    + 	  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)
    -+	  iv[1] = grub_cpu_to_le32 (sector >> (32 - log_sector_size));
    ++					       - log_sector_size));
     +	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
    - 				    & 0xFFFFFFFF);
    + 				    & GRUB_TYPE_U_MAX (iv[0]));
      	  break;
      	case GRUB_CRYPTODISK_MODE_IV_BENBI:
     @@ grub-core/disk/cryptodisk.c: grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
    @@ grub-core/disk/cryptodisk.c: grub_cryptodisk_write (grub_disk_t disk, grub_disk_
      ## grub-core/disk/luks.c ##
     @@ grub-core/disk/luks.c: configure_ciphers (grub_disk_t disk, const char *check_uuid,
            return NULL;
    -   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
    +   newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
        newdev->source_disk = NULL;
     -  newdev->log_sector_size = 9;
    -+  newdev->log_sector_size = LUKS1_LOG_SECTOR_SIZE;
    -   newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
    ++  newdev->log_sector_size = GRUB_LUKS1_LOG_SECTOR_SIZE;
    +   newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
        grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
        newdev->modname = "luks";
     @@ grub-core/disk/luks.c: luks_recover_key (grub_disk_t source,
    @@ grub-core/disk/luks.c: luks_recover_key (grub_disk_t source,
      
     -      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
     +      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0,
    -+					  LUKS1_LOG_SECTOR_SIZE);
    ++					  GRUB_LUKS1_LOG_SECTOR_SIZE);
            if (gcry_err)
      	{
      	  grub_free (split_key);
    @@ grub-core/disk/luks2.c: luks2_decrypt_key (grub_uint8_t *out_key,
     +   * regardless of encrypted data sector size.
     +   */
     +  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0,
    -+				      LUKS1_LOG_SECTOR_SIZE);
    ++				      GRUB_LUKS1_LOG_SECTOR_SIZE);
        if (gcry_ret)
          {
            ret = grub_crypto_gcry_error (gcry_ret);
    @@ include/grub/cryptodisk.h: typedef enum
      #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
      
     +/* LUKS1 specification defines the block size to always be 512 bytes. */
    -+#define LUKS1_LOG_SECTOR_SIZE 9
    ++#define GRUB_LUKS1_LOG_SECTOR_SIZE 9
     +
     +/* By default dm-crypt increments the IV every 512 bytes. */
     +#define GRUB_CRYPTODISK_IV_LOG_SIZE 9
 7:  3918a9013 <  -:  --------- cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
 -:  --------- > 15:  f0758a06f luks2: Better error handling when setting up the cryptodisk.
 -:  --------- > 16:  6ef5c9965 luks2: Error check segment.sector_size.
Signature · 1 line
2.27.0
#83 lore raw

PATCH v4 2/15 cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.

This makes it clear that the offset represents sectors, not bytes, in order to improve readability.

---
 grub-core/disk/cryptodisk.c | 10 +++++-----
 grub-core/disk/geli.c       |  2 +-
 grub-core/disk/luks.c       |  4 ++--
 grub-core/disk/luks2.c      |  4 ++--
 include/grub/cryptodisk.h   |  7 ++++++-
 5 files changed, 16 insertions(+), 11 deletions(-)
grub-core/disk/cryptodisk.c +5 -5
index 3703da221..473c93976 100644
@@ -754,10 +754,10 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Reading %" PRIuGRUB_SIZE " sectors from sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   err = grub_disk_read (dev->source_disk,
-			grub_disk_from_native_sector (disk, sector + dev->offset),
+			grub_disk_from_native_sector (disk, sector + dev->offset_sectors),
 			0, size << disk->log_sector_size, buf);
   if (err)
     {
@@ -803,7 +803,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Writing %" PRIuGRUB_SIZE " sectors to sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
@@ -815,7 +815,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
     }
 
   /* Since ->write was called so disk.mod is loaded but be paranoid  */
-  sector = sector + dev->offset;
+  sector = sector + dev->offset_sectors;
   if (grub_disk_write_weak)
     err = grub_disk_write_weak (dev->source_disk,
 				grub_disk_from_native_sector (disk, sector),
@@ -1228,7 +1228,7 @@ luks_script_get (grub_size_t *sz)
 	ptr = grub_stpcpy (ptr, "luks_mount ");
 	ptr = grub_stpcpy (ptr, i->uuid);
 	*ptr++ = ' ';
-	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset);
+	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset_sectors);
 	while (*ptr)
 	  ptr++;
 	for (iptr = i->cipher->cipher->name; *iptr; iptr++)
grub-core/disk/geli.c +1 -1
index 4ec875821..0175ce4c4 100644
@@ -361,7 +361,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
     }
   newdev->cipher = cipher;
   newdev->secondary_cipher = secondary_cipher;
-  newdev->offset = 0;
+  newdev->offset_sectors = 0;
   newdev->source_disk = NULL;
   newdev->benbi_log = 0;
   if (grub_le_to_cpu16 (header.alg) == 0x16)
grub-core/disk/luks.c +2 -2
index 429db4ad8..aa9877b68 100644
@@ -122,10 +122,10 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev = grub_zalloc (sizeof (struct grub_cryptodisk));
   if (!newdev)
       return NULL;
-  newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
+  newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = 9;
-  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index 3173a098d..26fb14a12 100644
@@ -605,11 +605,11 @@ luks2_recover_key (grub_disk_t disk,
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
 
       /* Set up disk according to keyslot's segment. */
-      crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
+      crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = grub_disk_get_size (disk) - crypt->offset;
+	crypt->total_sectors = grub_disk_get_size (disk) - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
 
include/grub/cryptodisk.h +6 -1
index 21dc69501..258b777bf 100644
@@ -66,7 +66,12 @@ struct grub_cryptodisk
   struct grub_cryptodisk **prev;
 
   char *source;
-  grub_disk_addr_t offset;
+
+  /*
+   * The number of sectors the start of the encrypted data is offset into the
+   * underlying disk, where sectors are the size noted by log_sector_size.
+   */
+  grub_disk_addr_t offset_sectors;
   /* Total number of encrypted sectors of size (1<<log_sector_size) */
   grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
Signature · 1 line
2.27.0
#95 lore raw

PATCH v4 6/15 luks2: Rename variable i to keyslot_idx in luks2_get_keyslot.

In reply to Glenn Washburn

Variables named i are usually looping variables, so rename to keyslot_idx to allow for easier reading of luks2_get_keyslot.

---
 grub-core/disk/luks2.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
grub-core/disk/luks2.c +4 -4
index be42deb14..2830f83cf 100644
@@ -255,7 +255,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
 static grub_err_t
 luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_segment_t *s,
-		   const grub_json_t *root, grub_size_t i)
+		   const grub_json_t *root, grub_size_t keyslot_idx)
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t j, size;
@@ -263,11 +263,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
-      grub_json_getchild (&keyslot, &keyslots, i) ||
+      grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
       grub_json_getuint64 (&idx, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
-    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, i);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
@@ -284,7 +284,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	break;
     }
   if (j == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
Signature · 1 line
2.27.0
#80 lore raw

PATCH v4 1/15 cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.

In reply to Glenn Washburn

This creates an alignment with grub_disk_t naming of the same field and is more intuitive as to how it should be used.

---
 grub-core/disk/cryptodisk.c | 2 +-
 grub-core/disk/geli.c       | 2 +-
 grub-core/disk/luks.c       | 2 +-
 grub-core/disk/luks2.c      | 4 ++--
 include/grub/cryptodisk.h   | 3 ++-
 5 files changed, 7 insertions(+), 6 deletions(-)
grub-core/disk/cryptodisk.c +1 -1
index a3d672f68..3703da221 100644
@@ -699,7 +699,7 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk)
     }
 
   disk->data = dev;
-  disk->total_sectors = dev->total_length;
+  disk->total_sectors = dev->total_sectors;
   disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
   disk->id = dev->id;
   dev->ref++;
grub-core/disk/geli.c +1 -1
index e9d23299a..4ec875821 100644
@@ -391,7 +391,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
 
   newdev->modname = "geli";
 
-  newdev->total_length = grub_disk_get_size (disk) - 1;
+  newdev->total_sectors = grub_disk_get_size (disk) - 1;
   grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
   COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= 32 * 2 + 1);
   return newdev;
grub-core/disk/luks.c +1 -1
index 59702067a..429db4ad8 100644
@@ -125,7 +125,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = 9;
-  newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index 31d7166fc..3173a098d 100644
@@ -609,9 +609,9 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_length = grub_disk_get_size (disk) - crypt->offset;
+	crypt->total_sectors = grub_disk_get_size (disk) - crypt->offset;
       else
-	crypt->total_length = grub_strtoull (segment.size, NULL, 10);
+	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
 
       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
include/grub/cryptodisk.h +2 -1
index e1b21e785..21dc69501 100644
@@ -67,7 +67,8 @@ struct grub_cryptodisk
 
   char *source;
   grub_disk_addr_t offset;
-  grub_disk_addr_t total_length;
+  /* Total number of encrypted sectors of size (1<<log_sector_size) */
+  grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
   int ref;
   grub_crypto_cipher_handle_t cipher;
Signature · 1 line
2.27.0
#86 lore raw

PATCH v4 3/15 luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

In reply to Glenn Washburn

This makes it more obvious to the reader that the disk referred to is the source disk, as opposed to say the disk holding the cryptodisk.

---
 grub-core/disk/luks2.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +11 -11
index 26fb14a12..d963255d4 100644
@@ -415,7 +415,7 @@ luks2_verify_key (grub_luks2_digest_t *d, grub_uint8_t *candidate_key,
 
 static grub_err_t
 luks2_decrypt_key (grub_uint8_t *out_key,
-		   grub_disk_t disk, grub_cryptodisk_t crypt,
+		   grub_disk_t source, grub_cryptodisk_t crypt,
 		   grub_luks2_keyslot_t *k,
 		   const grub_uint8_t *passphrase, grub_size_t passphraselen)
 {
@@ -491,7 +491,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
     }
 
   grub_errno = GRUB_ERR_NONE;
-  ret = grub_disk_read (disk, 0, k->area.offset, k->area.size, split_key);
+  ret = grub_disk_read (source, 0, k->area.offset, k->area.size, split_key);
   if (ret)
     {
       grub_error (GRUB_ERR_IO, "Read error: %s\n", grub_errmsg);
@@ -530,7 +530,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
 }
 
 static grub_err_t
-luks2_recover_key (grub_disk_t disk,
+luks2_recover_key (grub_disk_t source,
 		   grub_cryptodisk_t crypt)
 {
   grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
@@ -545,7 +545,7 @@ luks2_recover_key (grub_disk_t disk,
   grub_json_t *json = NULL, keyslots;
   grub_err_t ret;
 
-  ret = luks2_read_header (disk, &header);
+  ret = luks2_read_header (source, &header);
   if (ret)
     return ret;
 
@@ -554,7 +554,7 @@ luks2_recover_key (grub_disk_t disk,
       return GRUB_ERR_OUT_OF_MEMORY;
 
   /* Read the JSON area. */
-  ret = grub_disk_read (disk, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
+  ret = grub_disk_read (source, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
 			grub_be_to_cpu64 (header.hdr_size) - sizeof (header), json_header);
   if (ret)
       goto err;
@@ -571,10 +571,10 @@ luks2_recover_key (grub_disk_t disk,
     }
 
   /* Get the passphrase from the user. */
-  if (disk->partition)
-    part = grub_partition_get_name (disk->partition);
-  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), disk->name,
-		disk->partition ? "," : "", part ? : "",
+  if (source->partition)
+    part = grub_partition_get_name (source->partition);
+  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
+		source->partition ? "," : "", part ? : "",
 		crypt->uuid);
   if (!grub_password_get (passphrase, MAX_PASSPHRASE))
     {
@@ -609,11 +609,11 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = grub_disk_get_size (disk) - crypt->offset_sectors;
+	crypt->total_sectors = grub_disk_get_size (source) - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
 
-      ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
+      ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
Signature · 1 line
2.27.0
#89 lore raw

PATCH v4 4/15 types: Define GRUB_CHAR_BIT based on compiler macro instead of using literal.

In reply to Glenn Washburn
---
 include/grub/types.h | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
include/grub/types.h +8 -2
index 035a4b528..495c86bb1 100644
@@ -72,6 +72,14 @@
 # endif
 #endif
 
+#ifndef __CHAR_BIT__
+#error __CHAR_BIT__ is not defined
+#elif __CHAR_BIT__ != 8
+#error __CHAR_BIT__ is not equal 8
+#else
+#define GRUB_CHAR_BIT __CHAR_BIT__
+#endif
+
 /* Define various wide integers.  */
 typedef signed char		grub_int8_t;
 typedef short			grub_int16_t;
@@ -317,6 +325,4 @@ static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val)
   dd->d = val;
 }
 
-#define GRUB_CHAR_BIT 8
-
 #endif /* ! GRUB_TYPES_HEADER */
Signature · 1 line
2.27.0
#101 lore raw

PATCH v4 8/15 luks2: Split idx into three variables: keyslot_key, digest_key, segment_key.

In reply to Glenn Washburn

Introduce new variables keyslot_key, digest_key, and segment_key which represent the integer key of the item in the respective associative array when looping over the array items. This replaces using a generically named variable named "idx" used for all three values.

---
 grub-core/disk/luks2.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +6 -5
index d96764a02..1a4034f28 100644
@@ -259,12 +259,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t idx;
+  grub_uint64_t keyslot_key, digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, keyslot_idx);
@@ -276,11 +276,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&digest, &digests, i) ||
+	  grub_json_getuint64 (&digest_key, &digest, NULL) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
 
-      if ((d->keyslots & (1 << idx)))
+      if ((d->keyslots & (1 << keyslot_key)))
 	break;
     }
   if (i == size)
@@ -293,12 +294,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&segment, &segments, i) ||
-	  grub_json_getuint64 (&idx, &segment, NULL) ||
+	  grub_json_getuint64 (&segment_key, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
 
-      if ((d->segments & (1 << idx)))
+      if ((d->segments & (1 << segment_key)))
 	break;
     }
   if (i == size)
Signature · 1 line
2.27.0
#92 lore raw

PATCH v4 5/15 luks2: Use correct index variable when looping in luks2_get_keyslot.

In reply to Glenn Washburn

The loop variable j should be used to index the digests and segments json array, instead of the variable i, which is the keyslot index.

---
 grub-core/disk/luks2.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
grub-core/disk/luks2.c +4 -4
index d963255d4..be42deb14 100644
@@ -275,10 +275,10 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
   for (j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&digest, &digests, i) ||
+      if (grub_json_getchild (&digest, &digests, j) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
 
       if ((d->keyslots & (1 << idx)))
 	break;
@@ -292,11 +292,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
   for (j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&segment, &segments, i) ||
+      if (grub_json_getchild (&segment, &segments, j) ||
 	  grub_json_getuint64 (&idx, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
 
       if ((d->segments & (1 << idx)))
 	break;
Signature · 1 line
2.27.0
#98 lore raw

PATCH v4 7/15 luks2: Rename index variable j to i.

In reply to Glenn Washburn

Looping variable j was named such because the variable name i was taken. Since i has been renamed in the previous patch, we can rename j to i.

---
 grub-core/disk/luks2.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
grub-core/disk/luks2.c +9 -9
index 2830f83cf..d96764a02 100644
@@ -258,7 +258,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 		   const grub_json_t *root, grub_size_t keyslot_idx)
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
-  grub_size_t j, size;
+  grub_size_t i, size;
   grub_uint64_t idx;
 
   /* Get nth keyslot */
@@ -273,35 +273,35 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   if (grub_json_getvalue (&digests, root, "digests") ||
       grub_json_getsize (&size, &digests))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
-  for (j = 0; j < size; j++)
+  for (i = 0; i < size; i++)
     {
-      if (grub_json_getchild (&digest, &digests, j) ||
+      if (grub_json_getchild (&digest, &digests, i) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << idx)))
 	break;
     }
-  if (j == size)
+  if (i == size)
       return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
       grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-  for (j = 0; j < size; j++)
+  for (i = 0; i < size; i++)
     {
-      if (grub_json_getchild (&segment, &segments, j) ||
+      if (grub_json_getchild (&segment, &segments, i) ||
 	  grub_json_getuint64 (&idx, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << idx)))
 	break;
     }
-  if (j == size)
+  if (i == size)
     return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
 
   return GRUB_ERR_NONE;
Signature · 1 line
2.27.0
#105 lore raw

PATCH v4 9/15 luks2: Improve error messages in luks2_get_keyslot.

In reply to Glenn Washburn

Error messages now distinguish between indexes and keys. The former include "index" in the error string, and the later are surrounded in quotes.

---
 grub-core/disk/luks2.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +5 -5
index 1a4034f28..9b171bf9d 100644
@@ -267,7 +267,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
       grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
-    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
@@ -279,13 +279,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&digest_key, &digest, NULL) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << keyslot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -297,13 +297,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&segment_key, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << segment_key)))
 	break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", digest_key);
 
   return GRUB_ERR_NONE;
 }
Signature · 1 line
2.27.0
#108 lore raw

PATCH v4 10/15 luks2: Use more intuitive keyslot key instead of index when naming keyslot.

In reply to Glenn Washburn

Use the keyslot key value in the keyslot json array rather than the index of the keyslot in the json array. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

---
 grub-core/disk/luks2.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +16 -11
index 9b171bf9d..ca830d73b 100644
@@ -65,6 +65,7 @@ typedef struct grub_luks2_header grub_luks2_header_t;
 
 struct grub_luks2_keyslot
 {
+  grub_uint64_t slot_key;
   grub_int64_t key_size;
   grub_int64_t priority;
   struct
@@ -103,6 +104,7 @@ typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
 
 struct grub_luks2_segment
 {
+  grub_uint64_t slot_key;
   grub_uint64_t offset;
   const char	*size;
   const char	*encryption;
@@ -112,6 +114,7 @@ typedef struct grub_luks2_segment grub_luks2_segment_t;
 
 struct grub_luks2_digest
 {
+  grub_uint64_t slot_key;
   /* Both keyslots and segments are interpreted as bitfields here */
   grub_uint64_t	keyslots;
   grub_uint64_t	segments;
@@ -259,12 +262,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t keyslot_key, digest_key, segment_key;
+  grub_uint64_t digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
@@ -281,11 +284,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
           luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
-      if ((d->keyslots & (1 << keyslot_key)))
+      d->slot_key = digest_key;
+      if ((d->keyslots & (1 << k->slot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -299,6 +303,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
           luks2_parse_segment (s, &segment))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
+      s->slot_key = segment_key;
       if ((d->segments & (1 << segment_key)))
 	break;
     }
@@ -599,11 +604,11 @@ luks2_recover_key (grub_disk_t source,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -618,16 +623,16 @@ luks2_recover_key (grub_disk_t source,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -635,7 +640,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot.slot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
#111 lore raw

PATCH v4 11/15 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Glenn Washburn

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.

---
 grub-core/disk/cryptodisk.c | 13 +++++++------
 include/grub/types.h        |  5 +++++
 2 files changed, 12 insertions(+), 6 deletions(-)
grub-core/disk/cryptodisk.c +7 -6
index 473c93976..31b73c535 100644
@@ -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)
include/grub/types.h +5 -0
index 495c86bb1..b57acc3b1 100644
@@ -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) ((2 * ((1ULL << (GRUB_TYPE_BITS (type) - 1)) - 1)) + 1)
+#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)]
Signature · 1 line
2.27.0
#114 lore raw

PATCH v4 12/15 luks2: grub_cryptodisk_t->total_length is the max number of device native sectors

In reply to Glenn Washburn

The total_length field is named confusingly because length usually refers to bytes, whereas in this case its really the total number of sectors on the device. Also counter-intuitively, grub_disk_get_size returns the total number of device native sectors. We need to convert the sectors from the size of the underlying device to the cryptodisk sector size. And segment.size is in bytes which need to be converted to cryptodisk sectors.

Also, removed an empty statement.

---
 grub-core/disk/luks2.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +4 -3
index ca830d73b..355bb4aec 100644
@@ -429,7 +429,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
   grub_uint8_t salt[GRUB_CRYPTODISK_MAX_KEYLEN];
   grub_uint8_t *split_key = NULL;
   grub_size_t saltlen = sizeof (salt);
-  char cipher[32], *p;;
+  char cipher[32], *p;
   const gcry_md_spec_t *hash;
   gcry_err_code_t gcry_ret;
   grub_err_t ret;
@@ -615,9 +615,10 @@ luks2_recover_key (grub_disk_t source,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = grub_disk_get_size (source) - crypt->offset_sectors;
+	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
+			       - crypt->offset_sectors;
       else
-	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
+	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
Signature · 1 line
2.27.0
#118 lore raw

PATCH v4 13/15 cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 55 ++++++++++++++++++++++---------------
 grub-core/disk/luks.c       |  5 ++--
 grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 49 insertions(+), 26 deletions(-)
grub-core/disk/cryptodisk.c +33 -22
index 31b73c535..61f8e57f4 100644
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
@@ -237,12 +238,12 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
     return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len)
 	    : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));
 
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
 			/ sizeof (grub_uint32_t));
-      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4];
+      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4] __attribute__((aligned (sizeof (grub_uint64_t))));
 
       if (dev->rekey)
 	{
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector << dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len);
 	    dev->iv_hash->write (ctx, &tmp, sizeof (tmp));
@@ -281,15 +282,25 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & GRUB_TYPE_U_MAX (iv[0]));
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native sector
+	   * number. If using 32 bit IV mode, zero out the most significant
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *) iv;
+	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 - GRUB_CRYPTODISK_IV_LOG_SIZE));
+	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
+	      iv[1] = 0;
+	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
+	  /* The IV is the 64 bit byte offset of the sector. */
 	  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)
+					       - log_sector_size));
+	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
 				    & GRUB_TYPE_U_MAX (iv[0]));
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
@@ -312,10 +323,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_CBC:
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_cbc_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -323,10 +334,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_PCBC:
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_pcbc_decrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -338,7 +349,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (err)
 	      return err;
 	    
-	    for (j = 0; j < (1U << dev->log_sector_size);
+	    for (j = 0; j < (1U << log_sector_size);
 		 j += dev->cipher->cipher->blocksize)
 	      {
 		grub_crypto_xor (data + i + j, data + i + j, iv,
@@ -369,11 +380,11 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (do_encrypt)
 	      err = grub_crypto_ecb_encrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    else
 	      err = grub_crypto_ecb_decrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    if (err)
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -382,10 +393,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_ECB:
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  else
 	    err = grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  if (err)
 	    return err;
 	  break;
@@ -400,9 +411,9 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector)
+			 grub_disk_addr_t sector, grub_size_t log_sector_size)
 {
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector, log_sector_size, 0);
 }
 
 grub_err_t
@@ -767,7 +778,7 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
     }
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
 					size << disk->log_sector_size,
-					sector, 0);
+					sector, dev->log_sector_size, 0);
   return grub_crypto_gcry_error (gcry_err);
 }
 
@@ -808,7 +819,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
-					sector, 1);
+					sector, disk->log_sector_size, 1);
   if (gcry_err)
     {
       grub_free (tmp);
grub-core/disk/luks.c +3 -2
index aa9877b68..84c3fa73a 100644
@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
   newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
-  newdev->log_sector_size = 9;
+  newdev->log_sector_size = GRUB_LUKS1_LOG_SECTOR_SIZE;
   newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,
 	  return err;
 	}
 
-      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
+      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0,
+					  GRUB_LUKS1_LOG_SECTOR_SIZE);
       if (gcry_err)
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 355bb4aec..4a4a0dec4 100644
@@ -504,7 +504,12 @@ luks2_decrypt_key (grub_uint8_t *out_key,
       goto err;
     }
 
-  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0);
+  /*
+   * The key slots area is always encrypted in 512-byte sectors,
+   * regardless of encrypted data sector size.
+   */
+  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0,
+				      GRUB_LUKS1_LOG_SECTOR_SIZE);
   if (gcry_ret)
     {
       ret = grub_crypto_gcry_error (gcry_ret);
include/grub/cryptodisk.h +7 -1
index 258b777bf..ee30e4537 100644
@@ -48,6 +48,12 @@ typedef enum
 
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
+/* LUKS1 specification defines the block size to always be 512 bytes. */
+#define GRUB_LUKS1_LOG_SECTOR_SIZE 9
+
+/* By default dm-crypt increments the IV every 512 bytes. */
+#define GRUB_CRYPTODISK_IV_LOG_SIZE 9
+
 #define GRUB_CRYPTODISK_GF_LOG_SIZE 7
 #define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)
 #define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE - 3)
@@ -145,7 +151,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector);
+			 grub_disk_addr_t sector, grub_size_t log_sector_size);
 grub_err_t
 grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
 			grub_disk_t source);
Signature · 1 line
2.27.0
#122 lore raw

PATCH v4 14/15 luks2: Better error handling when setting up the cryptodisk.

In reply to Glenn Washburn
---
 grub-core/disk/luks2.c | 70 +++++++++++++++++++++++++++++++++++++++---
 include/grub/misc.h    |  2 ++
 2 files changed, 67 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +65 -5
index 4a4a0dec4..751b48d6a 100644
@@ -600,9 +600,16 @@ luks2_recover_key (grub_disk_t source,
       goto err;
     }
 
+  if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN)
+    {
+      ret = grub_error (GRUB_ERR_BUG, "not a luks2 device");
+      goto err;
+    }
+
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
       if (ret)
 	goto err;
@@ -617,13 +624,66 @@ luks2_recover_key (grub_disk_t source,
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
-      crypt->log_sector_size = sizeof (unsigned int) * 8
-		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
+      crypt->log_sector_size = grub_log2ull (segment.sector_size);
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
-			       - crypt->offset_sectors;
+	{
+	  /* Convert source sized number of sectors to cryptodisk sized sectors */
+	  crypt->total_sectors = source->total_sectors >> (crypt->log_sector_size - source->log_sector_size);
+	  if (crypt->total_sectors < crypt->offset_sectors)
+	    {
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" offset"
+				     " is greater than disk size.",
+				      segment.slot_key);
+	      continue;
+	    }
+
+	  crypt->total_sectors -= crypt->offset_sectors;
+	}
       else
-	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	{
+	  crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;
+	  else if(grub_errno == GRUB_ERR_BAD_NUMBER)
+	    {
+	      /* TODO: Unparsable number-string, try to use the whole disk */
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
+				     " is not a parsable number.",
+				     segment.slot_key);
+	      continue;
+	    }
+	  else if(grub_errno == GRUB_ERR_OUT_OF_RANGE)
+	    {
+	      /* There was an overflow in parsing segment.size, so disk must
+	       * be very large or the string is incorrect. */
+	      if ((source->total_sectors
+		    >> (crypt->log_sector_size - source->log_sector_size))
+		  > crypt->total_sectors)
+		{
+		  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\""
+				         " size is very large. The end may be"
+				         " inaccessible.",
+				         segment.slot_key);
+		}
+	      else
+		{
+		  /* FIXME: Set total_sectors as in "dynamic" case. */
+		  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\""
+				         " size greater than the source"
+					 " device.",
+				         segment.slot_key);
+		  continue;
+		}
+	    }
+	}
+
+      if (crypt->total_sectors == 0)
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has"
+				 " zero sectors, skipping.",
+				 segment.slot_key);
+	  continue;
+	}
 
       ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
include/grub/misc.h +2 -0
index b7ca6dd58..ec25131ba 100644
@@ -481,5 +481,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,
 
 #define grub_max(a, b) (((a) > (b)) ? (a) : (b))
 #define grub_min(a, b) (((a) < (b)) ? (a) : (b))
+#define grub_log2ull(n) (GRUB_TYPE_BITS (grub_uint64_t) \
+                         - __builtin_clzll (n) - 1)
 
 #endif /* ! GRUB_MISC_HEADER */
Signature · 1 line
2.27.0
#129 lore raw

PATCH v4 15/15 luks2: Error check segment.sector_size.

In reply to Glenn Washburn
---
 grub-core/disk/luks2.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
grub-core/disk/luks2.c +11 -0
index 751b48d6a..b628043e0 100644
@@ -622,6 +622,17 @@ luks2_recover_key (grub_disk_t source,
 
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
+      /* Sector size should be one of 512, 1024, 2048, or 4096. */
+      if (!(segment.sector_size == 512 || segment.sector_size == 1024 ||
+            segment.sector_size == 2048 || segment.sector_size == 4096))
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" sector"
+				 " size %"PRIuGRUB_UINT64_T" is not one of"
+				 " 512, 1024, 2048, or 4096.",
+				 segment.slot_key, segment.sector_size);
+	  continue;
+	}
+
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = grub_log2ull (segment.sector_size);
Signature · 1 line
2.27.0
Reviewed-by on v4 1/15
#81 lore raw

Re: [PATCH v4 01/15] cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

This creates an alignment with grub_disk_t naming of the same field and is more intuitive as to how it should be used.

Quoted 80 lines
---
 grub-core/disk/cryptodisk.c | 2 +-
 grub-core/disk/geli.c       | 2 +-
 grub-core/disk/luks.c       | 2 +-
 grub-core/disk/luks2.c      | 4 ++--
 include/grub/cryptodisk.h   | 3 ++-
 5 files changed, 7 insertions(+), 6 deletions(-)
grub-core/disk/cryptodisk.c +1 -1
index a3d672f68..3703da221 100644
@@ -699,7 +699,7 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk)
     }
 
   disk->data = dev;
-  disk->total_sectors = dev->total_length;
+  disk->total_sectors = dev->total_sectors;
   disk->max_agglomerate = GRUB_DISK_MAX_MAX_AGGLOMERATE;
   disk->id = dev->id;
   dev->ref++;
grub-core/disk/geli.c +1 -1
index e9d23299a..4ec875821 100644
@@ -391,7 +391,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
 
   newdev->modname = "geli";
 
-  newdev->total_length = grub_disk_get_size (disk) - 1;
+  newdev->total_sectors = grub_disk_get_size (disk) - 1;
   grub_memcpy (newdev->uuid, uuid, sizeof (newdev->uuid));
   COMPILE_TIME_ASSERT (sizeof (newdev->uuid) >= 32 * 2 + 1);
   return newdev;
grub-core/disk/luks.c +1 -1
index 59702067a..429db4ad8 100644
@@ -125,7 +125,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = 9;
-  newdev->total_length = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index 31d7166fc..3173a098d 100644
@@ -609,9 +609,9 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_length = grub_disk_get_size (disk) - crypt->offset;
+	crypt->total_sectors = grub_disk_get_size (disk) - crypt->offset;
       else
-	crypt->total_length = grub_strtoull (segment.size, NULL, 10);
+	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
 
       ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
include/grub/cryptodisk.h +2 -1
index e1b21e785..21dc69501 100644
@@ -67,7 +67,8 @@ struct grub_cryptodisk
 
   char *source;
   grub_disk_addr_t offset;
-  grub_disk_addr_t total_length;
+  /* Total number of encrypted sectors of size (1<<log_sector_size) */
+  grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
   int ref;
   grub_crypto_cipher_handle_t cipher;
Signature · 1 line
2.27.0
Reviewed-by on v4 2/15
#84 lore raw

Re: [PATCH v4 02/15] cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

This makes it clear that the offset represents sectors, not bytes, in order to improve readability.

Quoted 121 lines
---
 grub-core/disk/cryptodisk.c | 10 +++++-----
 grub-core/disk/geli.c       |  2 +-
 grub-core/disk/luks.c       |  4 ++--
 grub-core/disk/luks2.c      |  4 ++--
 include/grub/cryptodisk.h   |  7 ++++++-
 5 files changed, 16 insertions(+), 11 deletions(-)
grub-core/disk/cryptodisk.c +5 -5
index 3703da221..473c93976 100644
@@ -754,10 +754,10 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Reading %" PRIuGRUB_SIZE " sectors from sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   err = grub_disk_read (dev->source_disk,
-			grub_disk_from_native_sector (disk, sector + dev->offset),
+			grub_disk_from_native_sector (disk, sector + dev->offset_sectors),
 			0, size << disk->log_sector_size, buf);
   if (err)
     {
@@ -803,7 +803,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
   grub_dprintf ("cryptodisk",
 		"Writing %" PRIuGRUB_SIZE " sectors to sector 0x%"
 		PRIxGRUB_UINT64_T " with offset of %" PRIuGRUB_UINT64_T "\n",
-		size, sector, dev->offset);
+		size, sector, dev->offset_sectors);
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
@@ -815,7 +815,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
     }
 
   /* Since ->write was called so disk.mod is loaded but be paranoid  */
-  sector = sector + dev->offset;
+  sector = sector + dev->offset_sectors;
   if (grub_disk_write_weak)
     err = grub_disk_write_weak (dev->source_disk,
 				grub_disk_from_native_sector (disk, sector),
@@ -1228,7 +1228,7 @@ luks_script_get (grub_size_t *sz)
 	ptr = grub_stpcpy (ptr, "luks_mount ");
 	ptr = grub_stpcpy (ptr, i->uuid);
 	*ptr++ = ' ';
-	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset);
+	grub_snprintf (ptr, 21, "%" PRIuGRUB_UINT64_T " ", i->offset_sectors);
 	while (*ptr)
 	  ptr++;
 	for (iptr = i->cipher->cipher->name; *iptr; iptr++)
grub-core/disk/geli.c +1 -1
index 4ec875821..0175ce4c4 100644
@@ -361,7 +361,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
     }
   newdev->cipher = cipher;
   newdev->secondary_cipher = secondary_cipher;
-  newdev->offset = 0;
+  newdev->offset_sectors = 0;
   newdev->source_disk = NULL;
   newdev->benbi_log = 0;
   if (grub_le_to_cpu16 (header.alg) == 0x16)
grub-core/disk/luks.c +2 -2
index 429db4ad8..aa9877b68 100644
@@ -122,10 +122,10 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
   newdev = grub_zalloc (sizeof (struct grub_cryptodisk));
   if (!newdev)
       return NULL;
-  newdev->offset = grub_be_to_cpu32 (header.payloadOffset);
+  newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
   newdev->log_sector_size = 9;
-  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset;
+  newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
 
grub-core/disk/luks2.c +2 -2
index 3173a098d..26fb14a12 100644
@@ -605,11 +605,11 @@ luks2_recover_key (grub_disk_t disk,
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
 
       /* Set up disk according to keyslot's segment. */
-      crypt->offset = grub_divmod64 (segment.offset, segment.sector_size, NULL);
+      crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = grub_disk_get_size (disk) - crypt->offset;
+	crypt->total_sectors = grub_disk_get_size (disk) - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
 
include/grub/cryptodisk.h +6 -1
index 21dc69501..258b777bf 100644
@@ -66,7 +66,12 @@ struct grub_cryptodisk
   struct grub_cryptodisk **prev;
 
   char *source;
-  grub_disk_addr_t offset;
+
+  /*
+   * The number of sectors the start of the encrypted data is offset into the
+   * underlying disk, where sectors are the size noted by log_sector_size.
+   */
+  grub_disk_addr_t offset_sectors;
   /* Total number of encrypted sectors of size (1<<log_sector_size) */
   grub_disk_addr_t total_sectors;
   grub_disk_t source_disk;
Signature · 1 line
2.27.0
Reviewed-by on v4 3/15
#87 lore raw

Re: [PATCH v4 03/15] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

This makes it more obvious to the reader that the disk referred to is the source disk, as opposed to say the disk holding the cryptodisk.

Quoted 85 lines
---
 grub-core/disk/luks2.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +11 -11
index 26fb14a12..d963255d4 100644
@@ -415,7 +415,7 @@ luks2_verify_key (grub_luks2_digest_t *d, grub_uint8_t *candidate_key,
 
 static grub_err_t
 luks2_decrypt_key (grub_uint8_t *out_key,
-		   grub_disk_t disk, grub_cryptodisk_t crypt,
+		   grub_disk_t source, grub_cryptodisk_t crypt,
 		   grub_luks2_keyslot_t *k,
 		   const grub_uint8_t *passphrase, grub_size_t passphraselen)
 {
@@ -491,7 +491,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
     }
 
   grub_errno = GRUB_ERR_NONE;
-  ret = grub_disk_read (disk, 0, k->area.offset, k->area.size, split_key);
+  ret = grub_disk_read (source, 0, k->area.offset, k->area.size, split_key);
   if (ret)
     {
       grub_error (GRUB_ERR_IO, "Read error: %s\n", grub_errmsg);
@@ -530,7 +530,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
 }
 
 static grub_err_t
-luks2_recover_key (grub_disk_t disk,
+luks2_recover_key (grub_disk_t source,
 		   grub_cryptodisk_t crypt)
 {
   grub_uint8_t candidate_key[GRUB_CRYPTODISK_MAX_KEYLEN];
@@ -545,7 +545,7 @@ luks2_recover_key (grub_disk_t disk,
   grub_json_t *json = NULL, keyslots;
   grub_err_t ret;
 
-  ret = luks2_read_header (disk, &header);
+  ret = luks2_read_header (source, &header);
   if (ret)
     return ret;
 
@@ -554,7 +554,7 @@ luks2_recover_key (grub_disk_t disk,
       return GRUB_ERR_OUT_OF_MEMORY;
 
   /* Read the JSON area. */
-  ret = grub_disk_read (disk, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
+  ret = grub_disk_read (source, 0, grub_be_to_cpu64 (header.hdr_offset) + sizeof (header),
 			grub_be_to_cpu64 (header.hdr_size) - sizeof (header), json_header);
   if (ret)
       goto err;
@@ -571,10 +571,10 @@ luks2_recover_key (grub_disk_t disk,
     }
 
   /* Get the passphrase from the user. */
-  if (disk->partition)
-    part = grub_partition_get_name (disk->partition);
-  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), disk->name,
-		disk->partition ? "," : "", part ? : "",
+  if (source->partition)
+    part = grub_partition_get_name (source->partition);
+  grub_printf_ (N_("Enter passphrase for %s%s%s (%s): "), source->name,
+		source->partition ? "," : "", part ? : "",
 		crypt->uuid);
   if (!grub_password_get (passphrase, MAX_PASSPHRASE))
     {
@@ -609,11 +609,11 @@ luks2_recover_key (grub_disk_t disk,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = grub_disk_get_size (disk) - crypt->offset_sectors;
+	crypt->total_sectors = grub_disk_get_size (source) - crypt->offset_sectors;
       else
 	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
 
-      ret = luks2_decrypt_key (candidate_key, disk, crypt, &keyslot,
+      ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
Signature · 1 line
2.27.0
#90 lore raw

Re: [PATCH v4 04/15] types: Define GRUB_CHAR_BIT based on compiler macro instead of using literal.

In reply to Glenn Washburn
Quoted 11 lines · Glenn Washburn
---
 include/grub/types.h | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
include/grub/types.h +0 -0
index 035a4b528..495c86bb1 100644
@@ -72,6 +72,14 @@
 # endif

include/grub/types.h line 82

Quoted 10 lines
include/grub/types.h +8 -0
 #endif
 
+#ifndef __CHAR_BIT__
+#error __CHAR_BIT__ is not defined
+#elif __CHAR_BIT__ != 8
+#error __CHAR_BIT__ is not equal 8
+#else
+#define GRUB_CHAR_BIT __CHAR_BIT__
+#endif
+

Nested defines typically use spaces:

#ifndef __CHAR_BIT__

# error __CHAR_BIT__ is not defined
#elif __CHAR_BIT__ != 8
# error __CHAR_BIT__ is not equal 8
#else
# define GRUB_CHAR_BIT __CHAR_BIT__
#endif

Patrick

Quoted 13 lines
include/grub/types.h +0 -2
 /* Define various wide integers.  */
 typedef signed char		grub_int8_t;
 typedef short			grub_int16_t;
@@ -317,6 +325,4 @@ static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val)
   dd->d = val;
 }
 
-#define GRUB_CHAR_BIT 8
-
 #endif /* ! GRUB_TYPES_HEADER */
Signature · 1 line
2.27.0
Reviewed-by on v4 5/15
#93 lore raw

Re: [PATCH v4 05/15] luks2: Use correct index variable when looping in luks2_get_keyslot.

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

The loop variable j should be used to index the digests and segments json array, instead of the variable i, which is the keyslot index.

Quoted 38 lines
---
 grub-core/disk/luks2.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
grub-core/disk/luks2.c +4 -4
index d963255d4..be42deb14 100644
@@ -275,10 +275,10 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
   for (j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&digest, &digests, i) ||
+      if (grub_json_getchild (&digest, &digests, j) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
 
       if ((d->keyslots & (1 << idx)))
 	break;
@@ -292,11 +292,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
   for (j = 0; j < size; j++)
     {
-      if (grub_json_getchild (&segment, &segments, i) ||
+      if (grub_json_getchild (&segment, &segments, j) ||
 	  grub_json_getuint64 (&idx, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
 
       if ((d->segments & (1 << idx)))
 	break;
Signature · 1 line
2.27.0
Reviewed-by on v4 6/15
#96 lore raw

Re: [PATCH v4 06/15] luks2: Rename variable i to keyslot_idx in luks2_get_keyslot.

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

Variables named i are usually looping variables, so rename to keyslot_idx to allow for easier reading of luks2_get_keyslot.

Quoted 43 lines
---
 grub-core/disk/luks2.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
grub-core/disk/luks2.c +4 -4
index be42deb14..2830f83cf 100644
@@ -255,7 +255,7 @@ luks2_parse_digest (grub_luks2_digest_t *out, const grub_json_t *digest)
 
 static grub_err_t
 luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_segment_t *s,
-		   const grub_json_t *root, grub_size_t i)
+		   const grub_json_t *root, grub_size_t keyslot_idx)
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t j, size;
@@ -263,11 +263,11 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
-      grub_json_getchild (&keyslot, &keyslots, i) ||
+      grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
       grub_json_getuint64 (&idx, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
-    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, i);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
@@ -284,7 +284,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	break;
     }
   if (j == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
Signature · 1 line
2.27.0
Reviewed-by on v4 7/15
#99 lore raw

Re: [PATCH v4 07/15] luks2: Rename index variable j to i.

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

Looping variable j was named such because the variable name i was taken. Since i has been renamed in the previous patch, we can rename j to i.

Quoted 64 lines
---
 grub-core/disk/luks2.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
grub-core/disk/luks2.c +9 -9
index 2830f83cf..d96764a02 100644
@@ -258,7 +258,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 		   const grub_json_t *root, grub_size_t keyslot_idx)
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
-  grub_size_t j, size;
+  grub_size_t i, size;
   grub_uint64_t idx;
 
   /* Get nth keyslot */
@@ -273,35 +273,35 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   if (grub_json_getvalue (&digests, root, "digests") ||
       grub_json_getsize (&size, &digests))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get digests");
-  for (j = 0; j < size; j++)
+  for (i = 0; i < size; i++)
     {
-      if (grub_json_getchild (&digest, &digests, j) ||
+      if (grub_json_getchild (&digest, &digests, i) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, j);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << idx)))
 	break;
     }
-  if (j == size)
+  if (i == size)
       return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
       grub_json_getsize (&size, &segments))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not get segments");
-  for (j = 0; j < size; j++)
+  for (i = 0; i < size; i++)
     {
-      if (grub_json_getchild (&segment, &segments, j) ||
+      if (grub_json_getchild (&segment, &segments, i) ||
 	  grub_json_getuint64 (&idx, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, j);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << idx)))
 	break;
     }
-  if (j == size)
+  if (i == size)
     return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
 
   return GRUB_ERR_NONE;
Signature · 1 line
2.27.0
#102 lore raw

Re: [PATCH v4 08/15] luks2: Split idx into three variables: keyslot_key, digest_key, segment_key.

In reply to Glenn Washburn
Quoted 30 lines · Glenn Washburn

Introduce new variables keyslot_key, digest_key, and segment_key which represent the integer key of the item in the respective associative array when looping over the array items. This replaces using a generically named variable named "idx" used for all three values.

---
 grub-core/disk/luks2.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +2 -2
index d96764a02..1a4034f28 100644
@@ -259,12 +259,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t idx;
+  grub_uint64_t keyslot_key, digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, keyslot_idx);

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 284

Quoted 10 lines
grub-core/disk/luks2.c +2 -1
@@ -276,11 +276,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&digest, &digests, i) ||
+	  grub_json_getuint64 (&digest_key, &digest, NULL) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
 
-      if ((d->keyslots & (1 << idx)))
+      if ((d->keyslots & (1 << keyslot_key)))

It's a bit confusing that this commit introduces digest_key, which isn't used at all right now. It makes sense considering its being used in the subsequent commit, but it would've helped if this was pointed out in the commit message.

Other than that: Reviewed-by: Patrick Steinhardt <[email protected]>

Patrick

Quoted 21 lines
grub-core/disk/luks2.c +2 -2
 	break;
     }
   if (i == size)
@@ -293,12 +294,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&segment, &segments, i) ||
-	  grub_json_getuint64 (&idx, &segment, NULL) ||
+	  grub_json_getuint64 (&segment_key, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
 
-      if ((d->segments & (1 << idx)))
+      if ((d->segments & (1 << segment_key)))
 	break;
     }
   if (i == size)
Signature · 1 line
2.27.0
Reviewed-by on v4 9/15
#106 lore raw

Re: [PATCH v4 09/15] luks2: Improve error messages in luks2_get_keyslot.

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

Error messages now distinguish between indexes and keys. The former include "index" in the error string, and the later are surrounded in quotes.

Quoted 52 lines
---
 grub-core/disk/luks2.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +5 -5
index 1a4034f28..9b171bf9d 100644
@@ -267,7 +267,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
       grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
-    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);
 
   /* Get digest that matches the keyslot. */
   if (grub_json_getvalue (&digests, root, "digests") ||
@@ -279,13 +279,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&digest_key, &digest, NULL) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
       if ((d->keyslots & (1 << keyslot_key)))
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot %"PRIuGRUB_SIZE, keyslot_idx);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -297,13 +297,13 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 	  grub_json_getuint64 (&segment_key, &segment, NULL) ||
 	  grub_json_getchild (&segment, &segment, 0) ||
           luks2_parse_segment (s, &segment))
-	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment %"PRIuGRUB_SIZE, i);
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
       if ((d->segments & (1 << segment_key)))
 	break;
     }
   if (i == size)
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest %"PRIuGRUB_SIZE);
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No segment for digest \"%"PRIuGRUB_UINT64_T"\"", digest_key);
 
   return GRUB_ERR_NONE;
 }
Signature · 1 line
2.27.0
#109 lore raw

Re: [PATCH v4 10/15] luks2: Use more intuitive keyslot key instead of index when naming keyslot.

In reply to Glenn Washburn
Quoted 58 lines · Glenn Washburn

Use the keyslot key value in the keyslot json array rather than the index of the keyslot in the json array. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

---
 grub-core/disk/luks2.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +5 -2
index 9b171bf9d..ca830d73b 100644
@@ -65,6 +65,7 @@ typedef struct grub_luks2_header grub_luks2_header_t;
 
 struct grub_luks2_keyslot
 {
+  grub_uint64_t slot_key;
   grub_int64_t key_size;
   grub_int64_t priority;
   struct
@@ -103,6 +104,7 @@ typedef struct grub_luks2_keyslot grub_luks2_keyslot_t;
 
 struct grub_luks2_segment
 {
+  grub_uint64_t slot_key;
   grub_uint64_t offset;
   const char	*size;
   const char	*encryption;
@@ -112,6 +114,7 @@ typedef struct grub_luks2_segment grub_luks2_segment_t;
 
 struct grub_luks2_digest
 {
+  grub_uint64_t slot_key;
   /* Both keyslots and segments are interpreted as bitfields here */
   grub_uint64_t	keyslots;
   grub_uint64_t	segments;
@@ -259,12 +262,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t keyslot_key, digest_key, segment_key;
+  grub_uint64_t digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot index %"PRIuGRUB_SIZE, keyslot_idx);

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 288

Quoted 6 lines
grub-core/disk/luks2.c +2 -1
@@ -281,11 +284,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
           luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);
 
-      if ((d->keyslots & (1 << keyslot_key)))
+      d->slot_key = digest_key;
+      if ((d->keyslots & (1 << k->slot_key)))

For my own understanding: why don't you directly parse the digest key into the structure as you do for the keyslot? That'd also allow us to get rid of the digest_key and segment_key variables.

Patrick

Quoted 63 lines
grub-core/disk/luks2.c +9 -8
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key);
 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
@@ -299,6 +303,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
           luks2_parse_segment (s, &segment))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);
 
+      s->slot_key = segment_key;
       if ((d->segments & (1 << segment_key)))
 	break;
     }
@@ -599,11 +604,11 @@ luks2_recover_key (grub_disk_t source,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
 	  continue;
         }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n", i);
+      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
@@ -618,16 +623,16 @@ luks2_recover_key (grub_disk_t source,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_SIZE" failed: %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot %"PRIuGRUB_UINT64_T" failed: %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key, keyslot.key_size);
       if (ret)
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_SIZE": %s\n",
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot %"PRIuGRUB_UINT64_T": %s\n",
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
@@ -635,7 +640,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element of an array
        * where each element is either empty or holds a key.
        */
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"), keyslot.slot_key);
 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
#112 lore raw

Re: [PATCH v4 11/15] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Glenn Washburn
Quoted 59 lines · Glenn Washburn

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.

---
 grub-core/disk/cryptodisk.c | 13 +++++++------
 include/grub/types.h        |  5 +++++
 2 files changed, 12 insertions(+), 6 deletions(-)
grub-core/disk/cryptodisk.c +7 -6
index 473c93976..31b73c535 100644
@@ -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)
include/grub/types.h +2 -0
index 495c86bb1..b57acc3b1 100644
@@ -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

include/grub/types.h typedef grub_int32_t grub_ssize_t; · line 164

Quoted 4 lines
include/grub/types.h +1 -0
@@ -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) ((2 * ((1ULL << (GRUB_TYPE_BITS (type) - 1)) - 1)) + 1)

This macro is quite complicated. Considering this is specific to unsigned types now, couldn't we just use the following?

#define GRUB_TYPE_U_MAX(type) (~((type)0))

Patrick

Quoted 8 lines
include/grub/types.h +2 -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)]
Signature · 1 line
2.27.0
Reviewed-by on v4 12/15
#115 lore raw

Re: [PATCH v4 12/15] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors

In reply to Glenn Washburn
Quoted 10 lines · Glenn Washburn

The total_length field is named confusingly because length usually refers to bytes, whereas in this case its really the total number of sectors on the device. Also counter-intuitively, grub_disk_get_size returns the total number of device native sectors. We need to convert the sectors from the size of the underlying device to the cryptodisk sector size. And segment.size is in bytes which need to be converted to cryptodisk sectors.

Also, removed an empty statement.

Quoted 33 lines
---
 grub-core/disk/luks2.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
grub-core/disk/luks2.c +4 -3
index ca830d73b..355bb4aec 100644
@@ -429,7 +429,7 @@ luks2_decrypt_key (grub_uint8_t *out_key,
   grub_uint8_t salt[GRUB_CRYPTODISK_MAX_KEYLEN];
   grub_uint8_t *split_key = NULL;
   grub_size_t saltlen = sizeof (salt);
-  char cipher[32], *p;;
+  char cipher[32], *p;
   const gcry_md_spec_t *hash;
   gcry_err_code_t gcry_ret;
   grub_err_t ret;
@@ -615,9 +615,10 @@ luks2_recover_key (grub_disk_t source,
       crypt->log_sector_size = sizeof (unsigned int) * 8
 		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = grub_disk_get_size (source) - crypt->offset_sectors;
+	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
+			       - crypt->offset_sectors;
       else
-	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10);
+	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
 
       ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
Signature · 1 line
2.27.0
#119 lore raw

Re: [PATCH v4 13/15] cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Glenn Washburn
Quoted 238 lines · Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 55 ++++++++++++++++++++++---------------
 grub-core/disk/luks.c       |  5 ++--
 grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 49 insertions(+), 26 deletions(-)
grub-core/disk/cryptodisk.c +33 -22
index 31b73c535..61f8e57f4 100644
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
@@ -237,12 +238,12 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
     return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len)
 	    : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));
 
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
 			/ sizeof (grub_uint32_t));
-      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4];
+      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4] __attribute__((aligned (sizeof (grub_uint64_t))));
 
       if (dev->rekey)
 	{
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector << dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix, dev->iv_prefix_len);
 	    dev->iv_hash->write (ctx, &tmp, sizeof (tmp));
@@ -281,15 +282,25 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & GRUB_TYPE_U_MAX (iv[0]));
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native sector
+	   * number. If using 32 bit IV mode, zero out the most significant
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *) iv;
+	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 - GRUB_CRYPTODISK_IV_LOG_SIZE));
+	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
+	      iv[1] = 0;
+	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
+	  /* The IV is the 64 bit byte offset of the sector. */
 	  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)
+					       - log_sector_size));
+	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
 				    & GRUB_TYPE_U_MAX (iv[0]));
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
@@ -312,10 +323,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_CBC:
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_cbc_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size), iv);
+					   (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -323,10 +334,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_PCBC:
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  else
 	    err = grub_crypto_pcbc_decrypt (dev->cipher, data + i, data + i,
-					    (1U << dev->log_sector_size), iv);
+					    (1U << log_sector_size), iv);
 	  if (err)
 	    return err;
 	  break;
@@ -338,7 +349,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (err)
 	      return err;
 
-	    for (j = 0; j < (1U << dev->log_sector_size);
+	    for (j = 0; j < (1U << log_sector_size);
 		 j += dev->cipher->cipher->blocksize)
 	      {
 		grub_crypto_xor (data + i + j, data + i + j, iv,
@@ -369,11 +380,11 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	    if (do_encrypt)
 	      err = grub_crypto_ecb_encrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    else
 	      err = grub_crypto_ecb_decrypt (dev->cipher, data + i, 
 					     data + i,
-					     (1U << dev->log_sector_size));
+					     (1U << log_sector_size));
 	    if (err)
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -382,10 +393,10 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	case GRUB_CRYPTODISK_MODE_ECB:
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  else
 	    err = grub_crypto_ecb_decrypt (dev->cipher, data + i, data + i,
-					   (1U << dev->log_sector_size));
+					   (1U << log_sector_size));
 	  if (err)
 	    return err;
 	  break;
@@ -400,9 +411,9 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector)
+			 grub_disk_addr_t sector, grub_size_t log_sector_size)
 {
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector, log_sector_size, 0);
 }
 
 grub_err_t
@@ -767,7 +778,7 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_addr_t sector,
     }
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
 					size << disk->log_sector_size,
-					sector, 0);
+					sector, dev->log_sector_size, 0);
   return grub_crypto_gcry_error (gcry_err);
 }
 
@@ -808,7 +819,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_addr_t sector,
 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size << disk->log_sector_size,
-					sector, 1);
+					sector, disk->log_sector_size, 1);
   if (gcry_err)
     {
       grub_free (tmp);
grub-core/disk/luks.c +3 -2
index aa9877b68..84c3fa73a 100644
@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char *check_uuid,
       return NULL;
   newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
-  newdev->log_sector_size = 9;
+  newdev->log_sector_size = GRUB_LUKS1_LOG_SECTOR_SIZE;
   newdev->total_sectors = grub_disk_get_size (disk) - newdev->offset_sectors;
   grub_memcpy (newdev->uuid, uuid, sizeof (uuid));
   newdev->modname = "luks";
@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,
 	  return err;
 	}
 
-      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0);
+      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length, 0,
+					  GRUB_LUKS1_LOG_SECTOR_SIZE);
       if (gcry_err)
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 355bb4aec..4a4a0dec4 100644
@@ -504,7 +504,12 @@ luks2_decrypt_key (grub_uint8_t *out_key,
       goto err;
     }
 
-  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0);
+  /*
+   * The key slots area is always encrypted in 512-byte sectors,
+   * regardless of encrypted data sector size.
+   */
+  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key, k->area.size, 0,
+				      GRUB_LUKS1_LOG_SECTOR_SIZE);
   if (gcry_ret)
     {
       ret = grub_crypto_gcry_error (gcry_ret);
include/grub/cryptodisk.h +0 -0
index 258b777bf..ee30e4537 100644

include/grub/cryptodisk.h typedef enum · line 52

Quoted 5 lines
include/grub/cryptodisk.h +2 -0
@@ -48,6 +48,12 @@ typedef enum
 
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
+/* LUKS1 specification defines the block size to always be 512 bytes. */
+#define GRUB_LUKS1_LOG_SECTOR_SIZE 9

Sorry to be nitpicky, but this constant is used for both LUKS1 and LUKS2. Shouldn't it just be called GRUB_LUKS_LOG_SECTOR_SIZE?

Patrick

Quoted 18 lines
include/grub/cryptodisk.h +4 -1
+/* By default dm-crypt increments the IV every 512 bytes. */
+#define GRUB_CRYPTODISK_IV_LOG_SIZE 9
+
 #define GRUB_CRYPTODISK_GF_LOG_SIZE 7
 #define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)
 #define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE - 3)
@@ -145,7 +151,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t dev,
 gcry_err_code_t
 grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector);
+			 grub_disk_addr_t sector, grub_size_t log_sector_size);
 grub_err_t
 grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
 			grub_disk_t source);
Signature · 1 line
2.27.0
#123 lore raw

Re: [PATCH v4 14/15] luks2: Better error handling when setting up the cryptodisk.

In reply to Glenn Washburn
Quoted 1 line · Glenn Washburn

A commit message would help to set the stage for your changes here, especially so as they're non-trivial.

Quoted 45 lines
---
 grub-core/disk/luks2.c | 70 +++++++++++++++++++++++++++++++++++++++---
 include/grub/misc.h    |  2 ++
 2 files changed, 67 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +17 -4
index 4a4a0dec4..751b48d6a 100644
@@ -600,9 +600,16 @@ luks2_recover_key (grub_disk_t source,
       goto err;
     }
 
+  if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN)
+    {
+      ret = grub_error (GRUB_ERR_BUG, "not a luks2 device");
+      goto err;
+    }
+
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
       if (ret)
 	goto err;
@@ -617,13 +624,66 @@ luks2_recover_key (grub_disk_t source,
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
-      crypt->log_sector_size = sizeof (unsigned int) * 8
-		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
+      crypt->log_sector_size = grub_log2ull (segment.sector_size);
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
-			       - crypt->offset_sectors;
+	{
+	  /* Convert source sized number of sectors to cryptodisk sized sectors */
+	  crypt->total_sectors = source->total_sectors >> (crypt->log_sector_size - source->log_sector_size);
+	  if (crypt->total_sectors < crypt->offset_sectors)
+	    {
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" offset"
+				     " is greater than disk size.",
+				      segment.slot_key);
+	      continue;

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 646

Quoted 10 lines
grub-core/disk/luks2.c +8 -1
+	    }
+
+	  crypt->total_sectors -= crypt->offset_sectors;
+	}
       else
-	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	{
+	  crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;

If any other code previously ran which set grub_errno, then it would still carry the old error value here as grub_strtoull doesn't unset it if no error occurred. We'd thus have to first set grub_errno = GRUB_ERR_NONE before the call to grub_strtoull.

Patrick

Quoted 58 lines
grub-core/disk/luks2.c +40 -0
+	  else if(grub_errno == GRUB_ERR_BAD_NUMBER)
+	    {
+	      /* TODO: Unparsable number-string, try to use the whole disk */
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
+				     " is not a parsable number.",
+				     segment.slot_key);
+	      continue;
+	    }
+	  else if(grub_errno == GRUB_ERR_OUT_OF_RANGE)
+	    {
+	      /* There was an overflow in parsing segment.size, so disk must
+	       * be very large or the string is incorrect. */
+	      if ((source->total_sectors
+		    >> (crypt->log_sector_size - source->log_sector_size))
+		  > crypt->total_sectors)
+		{
+		  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\""
+				         " size is very large. The end may be"
+				         " inaccessible.",
+				         segment.slot_key);
+		}
+	      else
+		{
+		  /* FIXME: Set total_sectors as in "dynamic" case. */
+		  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\""
+				         " size greater than the source"
+					 " device.",
+				         segment.slot_key);
+		  continue;
+		}
+	    }
+	}
+
+      if (crypt->total_sectors == 0)
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has"
+				 " zero sectors, skipping.",
+				 segment.slot_key);
+	  continue;
+	}
 
       ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
include/grub/misc.h +2 -0
index b7ca6dd58..ec25131ba 100644
@@ -481,5 +481,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,
 
 #define grub_max(a, b) (((a) > (b)) ? (a) : (b))
 #define grub_min(a, b) (((a) < (b)) ? (a) : (b))
+#define grub_log2ull(n) (GRUB_TYPE_BITS (grub_uint64_t) \
+                         - __builtin_clzll (n) - 1)
 
 #endif /* ! GRUB_MISC_HEADER */
Signature · 1 line
2.27.0
Reviewed-by on v4 15/15
#130 lore raw

Re: [PATCH v4 15/15] luks2: Error check segment.sector_size.

In reply to Glenn Washburn
Quoted 1 line · Glenn Washburn
Quoted 29 lines
---
 grub-core/disk/luks2.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
grub-core/disk/luks2.c +11 -0
index 751b48d6a..b628043e0 100644
@@ -622,6 +622,17 @@ luks2_recover_key (grub_disk_t source,
 
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
+      /* Sector size should be one of 512, 1024, 2048, or 4096. */
+      if (!(segment.sector_size == 512 || segment.sector_size == 1024 ||
+            segment.sector_size == 2048 || segment.sector_size == 4096))
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" sector"
+				 " size %"PRIuGRUB_UINT64_T" is not one of"
+				 " 512, 1024, 2048, or 4096.",
+				 segment.slot_key, segment.sector_size);
+	  continue;
+	}
+
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
       crypt->log_sector_size = grub_log2ull (segment.sector_size);
Signature · 1 line
2.27.0
Reviewed-by
#82 lore raw

Re: [PATCH v4 01/15] cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.

In reply to Patrick Steinhardt
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn

This creates an alignment with grub_disk_t naming of the same field and is more intuitive as to how it should be used.

Daniel

Reviewed-by
#88 lore raw

Re: [PATCH v4 03/15] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.

In reply to Patrick Steinhardt
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn

This makes it more obvious to the reader that the disk referred to is the source disk, as opposed to say the disk holding the cryptodisk.

Daniel

Reviewed-by
#91 lore raw

Re: [PATCH v4 04/15] types: Define GRUB_CHAR_BIT based on compiler macro instead of using literal.

In reply to Patrick Steinhardt
Quoted 14 lines · Patrick Steinhardt
Quoted 13 lines · Glenn Washburn
---
 include/grub/types.h | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
include/grub/types.h +0 -0
index 035a4b528..495c86bb1 100644
@@ -72,6 +72,14 @@
 # endif
 #endif
 
Quoted 10 lines

include/grub/types.h line 82

Quoted 8 lines
include/grub/types.h +8 -0
+#ifndef __CHAR_BIT__
+#error __CHAR_BIT__ is not defined
+#elif __CHAR_BIT__ != 8
+#error __CHAR_BIT__ is not equal 8
+#else
+#define GRUB_CHAR_BIT __CHAR_BIT__
+#endif
+

Nested defines typically use spaces:

Yeah, good point. Anyway, I will fix it before commiting.

Quoted 7 lines

#ifndef __CHAR_BIT__

# error __CHAR_BIT__ is not defined
#elif __CHAR_BIT__ != 8
# error __CHAR_BIT__ is not equal 8
#else
# define GRUB_CHAR_BIT __CHAR_BIT__
#endif

Daniel

Reviewed-by
#94 lore raw

Re: [PATCH v4 05/15] luks2: Use correct index variable when looping in luks2_get_keyslot.

In reply to Patrick Steinhardt
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn

The loop variable j should be used to index the digests and segments json array, instead of the variable i, which is the keyslot index.

Daniel

Reviewed-by
#97 lore raw

Re: [PATCH v4 06/15] luks2: Rename variable i to keyslot_idx in luks2_get_keyslot.

In reply to Patrick Steinhardt
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn

Variables named i are usually looping variables, so rename to keyslot_idx to allow for easier reading of luks2_get_keyslot.

Daniel

Reviewed-by
#100 lore raw

Re: [PATCH v4 07/15] luks2: Rename index variable j to i.

In reply to Patrick Steinhardt
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn

Looping variable j was named such because the variable name i was taken. Since i has been renamed in the previous patch, we can rename j to i.

Daniel

#103 lore raw

Re: [PATCH v4 08/15] luks2: Split idx into three variables: keyslot_key, digest_key, segment_key.

In reply to Patrick Steinhardt
Quoted 39 lines · Patrick Steinhardt
Quoted 38 lines · Glenn Washburn

Introduce new variables keyslot_key, digest_key, and segment_key which represent the integer key of the item in the respective associative array when looping over the array items. This replaces using a generically named variable named "idx" used for all three values.

---
 grub-core/disk/luks2.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +3 -2
index d96764a02..1a4034f28 100644
@@ -259,12 +259,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
 {
   grub_json_t keyslots, keyslot, digests, digest, segments, segment;
   grub_size_t i, size;
-  grub_uint64_t idx;
+  grub_uint64_t keyslot_key, digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse keyslot %"PRIuGRUB_SIZE, keyslot_idx);
@@ -276,11 +276,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s
   for (i = 0; i < size; i++)
     {
       if (grub_json_getchild (&digest, &digests, i) ||
+	  grub_json_getuint64 (&digest_key, &digest, NULL) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest %"PRIuGRUB_SIZE, i);
 
Quoted 7 lines

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 284

Quoted 2 lines
grub-core/disk/luks2.c +1 -1
-      if ((d->keyslots & (1 << idx)))
+      if ((d->keyslots & (1 << keyslot_key)))

It's a bit confusing that this commit introduces digest_key, which isn't used at all right now. It makes sense considering its being used in the subsequent commit, but it would've helped if this was pointed out in the commit message.

Yeah, I think the digest_key change should be dropped from this patch.

Daniel

Reviewed-by
#107 lore raw

Re: [PATCH v4 09/15] luks2: Improve error messages in luks2_get_keyslot.

In reply to Patrick Steinhardt
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn

Error messages now distinguish between indexes and keys. The former include "index" in the error string, and the later are surrounded in quotes.

Daniel

#116 lore raw

Re: [PATCH v4 12/15] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors

In reply to Glenn Washburn
Quoted 1 line · Glenn Washburn

The total_length field is named confusingly because length usually refers to

total_length? I think by mistake you left original commit message here. It is confusing right now.

Quoted 9 lines

bytes, whereas in this case its really the total number of sectors on the device. Also counter-intuitively, grub_disk_get_size returns the total number of device native sectors. We need to convert the sectors from the size of the underlying device to the cryptodisk sector size. And segment.size is in bytes which need to be converted to cryptodisk sectors.

Also, removed an empty statement.

Otherwise patch LGTM.

Daniel

#125 lore raw

Re: [PATCH v4 14/15] luks2: Better error handling when setting up the cryptodisk.

In reply to Glenn Washburn
Quoted 28 lines · Glenn Washburn
---
 grub-core/disk/luks2.c | 70 +++++++++++++++++++++++++++++++++++++++---
 include/grub/misc.h    |  2 ++
 2 files changed, 67 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +7 -0
index 4a4a0dec4..751b48d6a 100644
@@ -600,9 +600,16 @@ luks2_recover_key (grub_disk_t source,
       goto err;
     }
 
+  if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN)
+    {
+      ret = grub_error (GRUB_ERR_BUG, "not a luks2 device");
+      goto err;
+    }
+
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json, i);
       if (ret)
 	goto err;

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 627

Quoted 6 lines
grub-core/disk/luks2.c +1 -2
@@ -617,13 +624,66 @@ luks2_recover_key (grub_disk_t source,
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset, segment.sector_size, NULL);
-      crypt->log_sector_size = sizeof (unsigned int) * 8
-		- __builtin_clz ((unsigned int) segment.sector_size) - 1;
+      crypt->log_sector_size = grub_log2ull (segment.sector_size);

This change does not belong to this patch.

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 631

Quoted 6 lines
grub-core/disk/luks2.c +3 -2
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->log_sector_size))
-			       - crypt->offset_sectors;
+	{
+	  /* Convert source sized number of sectors to cryptodisk sized sectors */
+	  crypt->total_sectors = source->total_sectors >> (crypt->log_sector_size - source->log_sector_size);

If you add the other checks could you also add the following check:
crypt->log_sector_size >= source->log_sector_size ?

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 635

Quoted 4 lines
grub-core/disk/luks2.c +4 -0
+	  if (crypt->total_sectors < crypt->offset_sectors)
+	    {
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" offset"
+				     " is greater than disk size.",

Please replace "." at the end of the message with "\n" here and below...

Quoted 14 lines
grub-core/disk/luks2.c +12 -1
+				      segment.slot_key);
+	      continue;
+	    }
+
+	  crypt->total_sectors -= crypt->offset_sectors;
+	}
       else
-	crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	{
+	  crypt->total_sectors = grub_strtoull (segment.size, NULL, 10) >> crypt->log_sector_size;
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;
+	  else if(grub_errno == GRUB_ERR_BAD_NUMBER)
+	    {

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 658

Quoted 10 lines
grub-core/disk/luks2.c +10 -0
+	      /* TODO: Unparsable number-string, try to use the whole disk */
+	      grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" size"
+				     " is not a parsable number.",
+				     segment.slot_key);
+	      continue;
+	    }
+	  else if(grub_errno == GRUB_ERR_OUT_OF_RANGE)
+	    {
+	      /* There was an overflow in parsing segment.size, so disk must
+	       * be very large or the string is incorrect. */

Please format this comment properly.

Quoted 36 lines
grub-core/disk/luks2.c +28 -0
+	      if ((source->total_sectors
+		    >> (crypt->log_sector_size - source->log_sector_size))
+		  > crypt->total_sectors)
+		{
+		  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\""
+				         " size is very large. The end may be"
+				         " inaccessible.",
+				         segment.slot_key);
+		}
+	      else
+		{
+		  /* FIXME: Set total_sectors as in "dynamic" case. */
+		  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\""
+				         " size greater than the source"
+					 " device.",
+				         segment.slot_key);
+		  continue;
+		}
+	    }
+	}
+
+      if (crypt->total_sectors == 0)
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" has"
+				 " zero sectors, skipping.",
+				 segment.slot_key);
+	  continue;
+	}
 
       ret = luks2_decrypt_key (candidate_key, source, crypt, &keyslot,
 			       (const grub_uint8_t *) passphrase, grub_strlen (passphrase));
include/grub/misc.h +0 -0
index b7ca6dd58..ec25131ba 100644

include/grub/misc.h void EXPORT_FUNC(grub_real_boot_time) (const char *file, · line 483

Quoted 3 lines
include/grub/misc.h +0 -0
@@ -481,5 +481,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,
 
 #define grub_max(a, b) (((a) > (b)) ? (a) : (b))
 #define grub_min(a, b) (((a) < (b)) ? (a) : (b))

Please add empty line here.

include/grub/misc.h void EXPORT_FUNC(grub_real_boot_time) (const char *file, · line 485

Quoted 2 lines
include/grub/misc.h +2 -0
+#define grub_log2ull(n) (GRUB_TYPE_BITS (grub_uint64_t) \
+                         - __builtin_clzll (n) - 1)

This change does not belong to this patch too. Additionally, please double check that misc.h includes all headers which define GRUB_TYPE_BITS(), grub_uint64_t and __builtin_clzll().

By the way, may I ask you to stop adding full stop to the end of email subject?

Daniel

#131 lore raw

Re: [PATCH v4 15/15] luks2: Error check segment.sector_size.

In reply to Patrick Steinhardt
Quoted 17 lines · Patrick Steinhardt
Quoted 1 line · Glenn Washburn
Quoted 12 lines
---
 grub-core/disk/luks2.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
grub-core/disk/luks2.c +0 -0
index 751b48d6a..b628043e0 100644
@@ -622,6 +622,17 @@ luks2_recover_key (grub_disk_t source,
 
       grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key);
 
Quoted 8 lines

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 632

Quoted 8 lines
grub-core/disk/luks2.c +8 -0
+      /* Sector size should be one of 512, 1024, 2048, or 4096. */
+      if (!(segment.sector_size == 512 || segment.sector_size == 1024 ||
+            segment.sector_size == 2048 || segment.sector_size == 4096))
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\" sector"
+				 " size %"PRIuGRUB_UINT64_T" is not one of"
+				 " 512, 1024, 2048, or 4096.",
+				 segment.slot_key, segment.sector_size);

s/\.$/\n/

Otherwise Reviewed-by: Daniel Kiper <[email protected]>

Daniel

#132 lore raw
In reply to Glenn Washburn

Hey,

Quoted 8 lines · Glenn Washburn

Here we go again. I have not added RB sigs to any patches since I've had to modify them in moving them earlier in the patch set. This would be the first three rename patches. The first two patches of v3 have been reworked into 5 patches (05-09) which combine to be equivalent. Most of the changes since v3 are added error handling around luks2 cryptodisk setup. Also, I addressed an IV alignment issue in grub_cryptodisk_endecrypt for the GRUB_CRYPTODISK_MODE_IV_PLAIN* case, found by Daniel. I'm not sure it works (I can't reproduce the build error) or that its the desired way of doing it.

I will take all the patches which got Patrick's and my RB (probably by the end of this week). Additionally, I will do test build of this whole patch series and get back to you when I have results in hand. The other patches still require some rework but it is going in good direction right now.

Thanks,

Daniel

#55 lore raw

Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Glenn Washburn
Quoted 119 lines · Glenn Washburn
Quoted 99 lines · On Fri, 30 Oct 2020 21:47:14 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 82 lines · Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 52
++++++++++++++++++++++--------------- grub-core/disk/luks.c       |
 5 ++-- grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index a3d672f68..623f0f396 100644

grub-core/disk/cryptodisk.c +2 -1
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t
 log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
grub-core/disk/cryptodisk.c +0 -0
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len) : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));

grub-core/disk/cryptodisk.c +2 -2
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector <<
 dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix,

dev->iv_prefix_len); dev->iv_hash->write (ctx, &tmp, sizeof (tmp));

grub-core/disk/cryptodisk.c +2 -3
@@ -281,14 +282,23 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native

sector

grub-core/disk/cryptodisk.c +1 -0
+	   * number. If using 32 bit IV mode, zero out the most

significant

grub-core/disk/cryptodisk.c

Quoted 4 lines
grub-core/disk/cryptodisk.c +4 -0
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;

./configure --target=arm-linux-gnueabihf --with-platform=coreboot ... make

...and you get this:

disk/cryptodisk.c: In function ‘grub_cryptodisk_endecrypt’:
disk/cryptodisk.c:292:28: error: cast increases required alignment
of target type [-Werror=cast-align] grub_uint64_t *iv64 = (grub_uint64_t *)iv; ^
cc1: all warnings being treated as errors

Quoted 5 lines

I think every 32-bit build will/may fail. It seems to me that
(grub_uint64_t *)(void *) iv;
or
(grub_uint64_t *)(grub_addr_t) iv;
should help.

I'm having issues building for arm-linux-gnueabihf, so I can't effectively test this. This code does compile for --target=i386 --with-platform=pc, which I believe is 32-bit.

Since I can't test your suggestion, I can't verify that it works. However, I suspect it may get rid of the compiler problem, but not solve the underlying issue, which is that iv may not be 8-byte aligned. My guess is that if the compiler message goes away you could get a crash at the next line if iv is not 8-byte aligned. Can you see if the warning disappears if you define iv like this:

Quoted 5 lines
  grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4]
  __attribute__((aligned(sizeof(grub_uint64_t))));

I'm not sure the best way to handle this the "grub way". Do you see a better way to do this?

Below you can find all fixes which are needed to properly build the GRUB with your patches on all platforms. If you have any questions drop me a line.

Daniel

grub-core/disk/cryptodisk.c +5 -3
index 61f8e57f4..b62835acc 100644
@@ -243,7 +243,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
 			/ sizeof (grub_uint32_t));
-      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4] __attribute__((aligned (sizeof (grub_uint64_t))));
+      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4];
 
       if (dev->rekey)
 	{
@@ -289,9 +289,11 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 	   * 32 bits.
 	   */
 	  {
-	    grub_uint64_t *iv64 = (grub_uint64_t *) iv;
-	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+	    grub_uint64_t iv64;
+
+	    iv64 = grub_cpu_to_le64 (sector << (log_sector_size
 						 - GRUB_CRYPTODISK_IV_LOG_SIZE));
+	    grub_set_unaligned64 (iv, iv64);
 	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
 	      iv[1] = 0;
 	  }
grub-core/kern/compiler-rt.c +1 -1
index a464200c6..2057c2e0c 100644
@@ -448,7 +448,7 @@ __clzsi2 (grub_uint32_t val)
 }
 #endif
 
-#if defined(__riscv) || defined(__sparc__)
+#if defined(__mips__) || defined(__riscv) || defined(__sparc__)
 int
 __clzdi2 (grub_uint64_t val)
 {
include/grub/compiler-rt.h +1 -1
index 7591980b4..17828b322 100644
@@ -115,7 +115,7 @@ int
 EXPORT_FUNC (__clzsi2) (grub_uint32_t val);
 #endif
 
-#if defined(__riscv) || defined(__sparc__)
+#if defined(__mips__) || defined(__riscv) || defined(__sparc__)
 int
 EXPORT_FUNC (__clzdi2) (grub_uint64_t val);
 #endif
#56 lore raw

Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.

Quoted 126 lines · On Thu, 19 Nov 2020 15:25:33 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 125 lines · Glenn Washburn
Quoted 102 lines · On Fri, 30 Oct 2020 21:47:14 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 85 lines · Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 52
++++++++++++++++++++++--------------- grub-core/disk/luks.c
  | 5 ++-- grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 47 insertions(+), 25 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index a3d672f68..623f0f396 100644

grub-core/disk/cryptodisk.c +2 -1
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t
 len,
-			   grub_disk_addr_t sector, int
 do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t
 log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
grub-core/disk/cryptodisk.c +0 -0
@@ -237,7 +238,7 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len) :
grub_crypto_ecb_decrypt (dev->cipher, data, data, len));

grub-core/disk/cryptodisk.c +2 -2
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector <<
 dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix,

dev->iv_prefix_len); dev->iv_hash->write (ctx, &tmp, sizeof (tmp)); @@ -281,14 +282,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, }

grub-core/disk/cryptodisk.c +2 -3
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & 0xFFFFFFFF);
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt

native sector

grub-core/disk/cryptodisk.c +1 -0
+	   * number. If using 32 bit IV mode, zero out the most

significant

grub-core/disk/cryptodisk.c

Quoted 4 lines
grub-core/disk/cryptodisk.c +4 -0
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *)iv;

./configure --target=arm-linux-gnueabihf --with-platform=coreboot ... make

...and you get this:

disk/cryptodisk.c: In function ‘grub_cryptodisk_endecrypt’:
disk/cryptodisk.c:292:28: error: cast increases required
alignment of target type [-Werror=cast-align] grub_uint64_t *iv64 = (grub_uint64_t *)iv; ^
cc1: all warnings being treated as errors

Quoted 5 lines

I think every 32-bit build will/may fail. It seems to me that
(grub_uint64_t *)(void *) iv;
or
(grub_uint64_t *)(grub_addr_t) iv;
should help.

I'm having issues building for arm-linux-gnueabihf, so I can't effectively test this. This code does compile for --target=i386 --with-platform=pc, which I believe is 32-bit.

Since I can't test your suggestion, I can't verify that it works. However, I suspect it may get rid of the compiler problem, but not solve the underlying issue, which is that iv may not be 8-byte aligned. My guess is that if the compiler message goes away you could get a crash at the next line if iv is not 8-byte aligned. Can you see if the warning disappears if you define iv like this:

  grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4]
  __attribute__((aligned(sizeof(grub_uint64_t))));
Quoted 8 lines
Quoted 2 lines

I'm not sure the best way to handle this the "grub way". Do you see a better way to do this?

Below you can find all fixes which are needed to properly build the GRUB with your patches on all platforms. If you have any questions drop me a line.

Daniel

Great, thanks for the help. I'm confused as to why the changes to compiler-rt* are needed now. I assume that they are needed because of the use of __builtin_clzll. My v4 changes include a patch that moves the use of __builtin_clzll in luks2.c into a macro in misc.h, but I don't think that would have necessitated this change because the only code that uses the macro is in luks2.c. So the compiler should see no difference from current master. Could it be that mips targets are not properly building with current master due to the use of __builtin_clzll? Or am I totally off?

I'll add these in the forth coming patch series. For now, I'll assume that I should put the compiler-rt* changes in a separate patch and merge in the cryptodisk.c changes.

Glenn

Quoted 57 lines
grub-core/disk/cryptodisk.c +1 -1
index 61f8e57f4..b62835acc 100644
@@ -243,7 +243,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk
 *dev, grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
 			/ sizeof (grub_uint32_t));
-      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4]
 __attribute__((aligned (sizeof (grub_uint64_t))));
+      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4];
 
       if (dev->rekey)
grub-core/disk/cryptodisk.c +4 -2
 	{
@@ -289,9 +289,11 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev,
 	   * 32 bits.
 	   */
 	  {
-	    grub_uint64_t *iv64 = (grub_uint64_t *) iv;
-	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+	    grub_uint64_t iv64;
+
+	    iv64 = grub_cpu_to_le64 (sector << (log_sector_size
 						 -
 GRUB_CRYPTODISK_IV_LOG_SIZE));
+	    grub_set_unaligned64 (iv, iv64);
 	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
grub-core/disk/cryptodisk.c +0 -0
 	      iv[1] = 0;
 	  }
(quoted diff) +0 -0

b/grub-core/kern/compiler-rt.c index a464200c6..2057c2e0c 100644

grub-core/kern/compiler-rt.c +1 -1
@@ -448,7 +448,7 @@ __clzsi2 (grub_uint32_t val)
 }
 #endif
 
-#if defined(__riscv) || defined(__sparc__)
+#if defined(__mips__) || defined(__riscv) || defined(__sparc__)
 int
 __clzdi2 (grub_uint64_t val)
 {
include/grub/compiler-rt.h +1 -1
index 7591980b4..17828b322 100644
@@ -115,7 +115,7 @@ int
 EXPORT_FUNC (__clzsi2) (grub_uint32_t val);
 #endif
 
-#if defined(__riscv) || defined(__sparc__)
+#if defined(__mips__) || defined(__riscv) || defined(__sparc__)
 int
 EXPORT_FUNC (__clzdi2) (grub_uint64_t val);
 #endif
#104 lore raw

Re: [PATCH v4 08/15] luks2: Split idx into three variables: keyslot_key, digest_key, segment_key.

In reply to Daniel Kiper
Quoted 50 lines · On Tue, 17 Nov 2020 14:47:39 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 41 lines · Patrick Steinhardt
Quoted 40 lines · Glenn Washburn

Introduce new variables keyslot_key, digest_key, and segment_key which represent the integer key of the item in the respective associative array when looping over the array items. This replaces using a generically named variable named "idx" used for all three values.

---
 grub-core/disk/luks2.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
grub-core/disk/luks2.c +2 -2
index d96764a02..1a4034f28 100644
@@ -259,12 +259,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,
 grub_luks2_digest_t *d, grub_luks2_s {
   grub_json_t keyslots, keyslot, digests, digest, segments,
 segment; grub_size_t i, size;
-  grub_uint64_t idx;
+  grub_uint64_t keyslot_key, digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&idx, &keyslot, NULL) ||
+      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse

keyslot %"PRIuGRUB_SIZE, keyslot_idx); @@ -276,11 +276,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t

*d, grub_luks2_s for (i = 0; i < size; i++) {
       if (grub_json_getchild (&digest, &digests, i) ||
grub-core/disk/luks2.c +1 -0
+	  grub_json_getuint64 (&digest_key, &digest, NULL) ||
           grub_json_getchild (&digest, &digest, 0) ||
           luks2_parse_digest (d, &digest))
 	return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not

parse digest %"PRIuGRUB_SIZE, i);

Quoted 7 lines

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 284

Quoted 2 lines
grub-core/disk/luks2.c +1 -1
-      if ((d->keyslots & (1 << idx)))
+      if ((d->keyslots & (1 << keyslot_key)))

It's a bit confusing that this commit introduces digest_key, which isn't used at all right now. It makes sense considering its being used in the subsequent commit, but it would've helped if this was pointed out in the commit message.

Quoted 3 lines

Yeah, I think the digest_key change should be dropped from this patch.

Daniel

Ok, I'll add it to the following, 09, patch in this series.

Glenn

#110 lore raw

Re: [PATCH v4 10/15] luks2: Use more intuitive keyslot key instead of index when naming keyslot.

In reply to Patrick Steinhardt
Quoted 72 lines · On Sun, 15 Nov 2020 10:55:21 +0100 Patrick Steinhardt <[email protected]> wrote:
Quoted 63 lines · Glenn Washburn

Use the keyslot key value in the keyslot json array rather than the index of the keyslot in the json array. This is less confusing for the end user. For example, say you have a LUKS2 device with a key in slot 1 and slot 4. When using the password for slot 4 to unlock the device, the messages using the index of the keyslot will mention keyslot 1 (its a zero-based index). Furthermore, with this change the keyslot number will align with the number used to reference the keyslot when using the --key-slot argument to cryptsetup.

---
 grub-core/disk/luks2.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
grub-core/disk/luks2.c +5 -2
index 9b171bf9d..ca830d73b 100644
@@ -65,6 +65,7 @@ typedef struct grub_luks2_header
 grub_luks2_header_t; 
 struct grub_luks2_keyslot
 {
+  grub_uint64_t slot_key;
   grub_int64_t key_size;
   grub_int64_t priority;
   struct
@@ -103,6 +104,7 @@ typedef struct grub_luks2_keyslot
 grub_luks2_keyslot_t; 
 struct grub_luks2_segment
 {
+  grub_uint64_t slot_key;
   grub_uint64_t offset;
   const char	*size;
   const char	*encryption;
@@ -112,6 +114,7 @@ typedef struct grub_luks2_segment
 grub_luks2_segment_t; 
 struct grub_luks2_digest
 {
+  grub_uint64_t slot_key;
   /* Both keyslots and segments are interpreted as bitfields here
 */ grub_uint64_t	keyslots;
   grub_uint64_t	segments;
@@ -259,12 +262,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,
 grub_luks2_digest_t *d, grub_luks2_s {
   grub_json_t keyslots, keyslot, digests, digest, segments,
 segment; grub_size_t i, size;
-  grub_uint64_t keyslot_key, digest_key, segment_key;
+  grub_uint64_t digest_key, segment_key;
 
   /* Get nth keyslot */
   if (grub_json_getvalue (&keyslots, root, "keyslots") ||
       grub_json_getchild (&keyslot, &keyslots, keyslot_idx) ||
-      grub_json_getuint64 (&keyslot_key, &keyslot, NULL) ||
+      grub_json_getuint64 (&k->slot_key, &keyslot, NULL) ||
       grub_json_getchild (&keyslot, &keyslot, 0) ||
       luks2_parse_keyslot (k, &keyslot))
     return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse

keyslot index %"PRIuGRUB_SIZE, keyslot_idx); @@ -281,11 +284,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s luks2_parse_digest (d, &digest)) return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse digest index %"PRIuGRUB_SIZE, i);

grub-core/disk/luks2.c luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_s · line 288

Quoted 3 lines
grub-core/disk/luks2.c +2 -1
-      if ((d->keyslots & (1 << keyslot_key)))
+      d->slot_key = digest_key;
+      if ((d->keyslots & (1 << k->slot_key)))

For my own understanding: why don't you directly parse the digest key into the structure as you do for the keyslot? That'd also allow us to get rid of the digest_key and segment_key variables.

Quoted 1 line

Patrick

Thanks for pointing that out. I was only thinking about the keyslot key because that's what was needed in luks2_recover_key. I'll fix that.

Quoted 72 lines
Quoted 72 lines
grub-core/disk/luks2.c +0 -1
 	break;
     }
   if (i == size)
-      return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for

keyslot \"%"PRIuGRUB_UINT64_T"\"", keyslot_key);
+ return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest for

keyslot \"%"PRIuGRUB_UINT64_T"\"", k->slot_key); 
   /* Get segment that matches the digest. */
   if (grub_json_getvalue (&segments, root, "segments") ||
grub-core/disk/luks2.c +0 -0
@@ -299,6 +303,7 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k,
 grub_luks2_digest_t *d, grub_luks2_s luks2_parse_segment (s,

&segment)) return grub_error (GRUB_ERR_BAD_ARGUMENT, "Could not parse segment index %"PRIuGRUB_SIZE, i);

grub-core/disk/luks2.c +3 -2
+      s->slot_key = segment_key;
       if ((d->segments & (1 << segment_key)))
 	break;
     }
@@ -599,11 +604,11 @@ luks2_recover_key (grub_disk_t source,
 
       if (keyslot.priority == 0)
 	{
-	  grub_dprintf ("luks2", "Ignoring keyslot
 %"PRIuGRUB_SIZE" due to priority\n", i);
+	  grub_dprintf ("luks2", "Ignoring keyslot
 %"PRIuGRUB_UINT64_T" due to priority\n", keyslot.slot_key);
 continue; }
 
-      grub_dprintf ("luks2", "Trying keyslot %"PRIuGRUB_SIZE"\n",
 i);
+      grub_dprintf ("luks2", "Trying keyslot
 %"PRIuGRUB_UINT64_T"\n", keyslot.slot_key); 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset,

segment.sector_size, NULL); @@ -618,16 +623,16 @@ luks2_recover_key (grub_disk_t source, (const grub_uint8_t *) passphrase, grub_strlen (passphrase)); if (ret)

grub-core/disk/luks2.c +0 -1
 	{
-	  grub_dprintf ("luks2", "Decryption with keyslot

%"PRIuGRUB_SIZE" failed: %s\n",

grub-core/disk/luks2.c +1 -1
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Decryption with keyslot

%"PRIuGRUB_UINT64_T" failed: %s\n",

grub-core/disk/luks2.c +1 -0
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
 
       ret = luks2_verify_key (&digest, candidate_key,

keyslot.key_size); if (ret)

grub-core/disk/luks2.c +0 -1
 	{
-	  grub_dprintf ("luks2", "Could not open keyslot

%"PRIuGRUB_SIZE": %s\n",

grub-core/disk/luks2.c +1 -1
-			i, grub_errmsg);
+	  grub_dprintf ("luks2", "Could not open keyslot

%"PRIuGRUB_UINT64_T": %s\n",

grub-core/disk/luks2.c +1 -0
+			keyslot.slot_key, grub_errmsg);
 	  continue;
 	}
@@ -635,7 +640,7 @@ luks2_recover_key (grub_disk_t source,
        * TRANSLATORS: It's a cryptographic key slot: one element

of an array
* where each element is either empty or holds a key. */

grub-core/disk/luks2.c +1 -1
-      grub_printf_ (N_("Slot %"PRIuGRUB_SIZE" opened\n"), i);
+      grub_printf_ (N_("Slot %"PRIuGRUB_UINT64_T" opened\n"),
keyslot.slot_key); 
       candidate_key_len = keyslot.key_size;
       break;
Signature · 1 line
2.27.0
#113 lore raw

Re: [PATCH v4 11/15] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.

In reply to Patrick Steinhardt
Quoted 74 lines · On Sun, 15 Nov 2020 10:59:33 +0100 Patrick Steinhardt <[email protected]> wrote:
Quoted 64 lines · Glenn Washburn

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.

---
 grub-core/disk/cryptodisk.c | 13 +++++++------
 include/grub/types.h        |  5 +++++
 2 files changed, 12 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index 473c93976..31b73c535 100644

grub-core/disk/cryptodisk.c +3 -2
@@ -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)

grub-core/disk/cryptodisk.c +2 -3
-				    & 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]));

grub-core/disk/cryptodisk.c +1 -0
+	    iv[sz - 1] = grub_cpu_to_be32 (num & GRUB_TYPE_U_MAX

(iv[0])); }

grub-core/disk/cryptodisk.c +1 -1
 	  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,

grub-core/disk/cryptodisk.c +0 -0
 					 dev->cipher->cipher->blocksize);
 	  if (err)
include/grub/types.h +2 -0
index 495c86bb1..b57acc3b1 100644
@@ -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

include/grub/types.h typedef grub_int32_t grub_ssize_t; · line 165

Quoted 5 lines
include/grub/types.h +1 -0
@@ -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) ((2 * ((1ULL << (GRUB_TYPE_BITS
 (type) - 1)) - 1)) + 1)

This macro is quite complicated. Considering this is specific to unsigned types now, couldn't we just use the following?

Quoted 3 lines

#define GRUB_TYPE_U_MAX(type) (~((type)0))

Patrick

The way its currently defined works for arbitrary bit lengths. I suppose its safe to assume that it would only be needed for bit lengths of actual types.

And your suggestion won't quite work because the bit-complement operator promotes its argument to an int (or an unsigned int if it won't fit in an int). So if type is unsigned short, then ~((unsigned short)0) -> ~((int)0x0) -> 0xffffffff.

However, I think you've hit on a reasonable idea. This looks like it'll do what you were intending:

#define GRUB_TYPE_U_MAX(type) ((typeof (1ULL))((typeof (type))(~0)))

I'll update with this.

Glenn

Quoted 10 lines
Quoted 10 lines
include/grub/types.h +2 -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

#117 lore raw

Re: [PATCH v4 12/15] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors

In reply to Daniel Kiper
Quoted 14 lines · On Tue, 17 Nov 2020 15:06:08 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 2 lines · Glenn Washburn

The total_length field is named confusingly because length usually refers to

total_length? I think by mistake you left original commit message here. It is confusing right now.

Quoted 7 lines

bytes, whereas in this case its really the total number of sectors on the device. Also counter-intuitively, grub_disk_get_size returns the total number of device native sectors. We need to convert the sectors from the size of the underlying device to the cryptodisk sector size. And segment.size is in bytes which need to be converted to cryptodisk sectors.

Quoted 7 lines
Quoted 3 lines

Also, removed an empty statement.

Otherwise patch LGTM.

Daniel

Damn, I thought I changed that. Good catch.

Glenn

#120 lore raw

Re: [PATCH v4 13/15] cryptodisk: Properly handle non-512 byte sized sectors.

In reply to Patrick Steinhardt
Quoted 274 lines · On Sun, 15 Nov 2020 11:07:27 +0100 Patrick Steinhardt <[email protected]> wrote:
Quoted 273 lines · Glenn Washburn

By default, dm-crypt internally uses an IV that corresponds to 512-byte sectors, even when a larger sector size is specified. What this means is that when using a larger sector size, the IV is incremented every sector. However, the amount the IV is incremented is the number of 512 byte blocks in a sector (ie 8 for 4K sectors). Confusingly the IV does not corespond to the number of, for example, 4K sectors. So each 512 byte cipher block in a sector will be encrypted with the same IV and the IV will be incremented afterwards by the number of 512 byte cipher blocks in the sector.

There are some encryption utilities which do it the intuitive way and have the IV equal to the sector number regardless of sector size (ie. the fifth sector would have an IV of 4 for each cipher block). And this is supported by dm-crypt with the iv_large_sectors option and also cryptsetup as of 2.3.3 with the --iv-large-sectors, though not with LUKS headers (only with --type plain). However, support for this has not been included as grub does not support plain devices right now.

One gotcha here is that the encrypted split keys are encrypted with a hard- coded 512-byte sector size. So even if your data is encrypted with 4K sector sizes, the split key encrypted area must be decrypted with a block size of 512 (ie the IV increments every 512 bytes). This made these changes less aestetically pleasing than desired.

---
 grub-core/disk/cryptodisk.c | 55
++++++++++++++++++++++--------------- grub-core/disk/luks.c       |
 5 ++-- grub-core/disk/luks2.c      |  7 ++++-
 include/grub/cryptodisk.h   |  8 +++++-
 4 files changed, 49 insertions(+), 26 deletions(-)
(quoted diff) +0 -0

b/grub-core/disk/cryptodisk.c index 31b73c535..61f8e57f4 100644

grub-core/disk/cryptodisk.c +2 -1
@@ -224,7 +224,8 @@ lrw_xor (const struct lrw_sector *sec,
 static gcry_err_code_t
 grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev,
 			   grub_uint8_t * data, grub_size_t len,
-			   grub_disk_addr_t sector, int do_encrypt)
+			   grub_disk_addr_t sector, grub_size_t
 log_sector_size,
+			   int do_encrypt)
 {
   grub_size_t i;
   gcry_err_code_t err;
grub-core/disk/cryptodisk.c +0 -0
@@ -237,12 +238,12 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, data, len) : grub_crypto_ecb_decrypt (dev->cipher, data, data, len));

grub-core/disk/cryptodisk.c +2 -2
-  for (i = 0; i < len; i += (1U << dev->log_sector_size))
+  for (i = 0; i < len; i += (1U << log_sector_size))
     {
       grub_size_t sz = ((dev->cipher->cipher->blocksize
 			 + sizeof (grub_uint32_t) - 1)
 			/ sizeof (grub_uint32_t));
-      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4];
+      grub_uint32_t iv[(GRUB_CRYPTO_MAX_CIPHER_BLOCKSIZE + 3) / 4]
__attribute__((aligned (sizeof (grub_uint64_t)))); 
       if (dev->rekey)
grub-core/disk/cryptodisk.c +1 -1
 	{
@@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, if (!ctx)
 	      return GPG_ERR_OUT_OF_MEMORY;
 
-	    tmp = grub_cpu_to_le64 (sector <<
 dev->log_sector_size);
+	    tmp = grub_cpu_to_le64 (sector << log_sector_size);
 	    dev->iv_hash->init (ctx);
 	    dev->iv_hash->write (ctx, dev->iv_prefix,

dev->iv_prefix_len); dev->iv_hash->write (ctx, &tmp, sizeof (tmp));

grub-core/disk/cryptodisk.c +2 -3
@@ -281,15 +282,25 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN64:
-	  iv[1] = grub_cpu_to_le32 (sector >> 32);
-	  /* FALLTHROUGH */
 	case GRUB_CRYPTODISK_MODE_IV_PLAIN:
-	  iv[0] = grub_cpu_to_le32 (sector & GRUB_TYPE_U_MAX
 (iv[0]));
+	  /*
+	   * The IV is a 32 or 64 bit value of the dm-crypt native

sector

grub-core/disk/cryptodisk.c +1 -0
+	   * number. If using 32 bit IV mode, zero out the most

significant

grub-core/disk/cryptodisk.c +6 -0
+	   * 32 bits.
+	   */
+	  {
+	    grub_uint64_t *iv64 = (grub_uint64_t *) iv;
+	    *iv64 = grub_cpu_to_le64 (sector << (log_sector_size
+						 -

GRUB_CRYPTODISK_IV_LOG_SIZE));

grub-core/disk/cryptodisk.c +4 -0
+	    if (dev->mode_iv == GRUB_CRYPTODISK_MODE_IV_PLAIN)
+	      iv[1] = 0;
+	  }
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64:
+	  /* The IV is the 64 bit byte offset of the sector. */
 	  iv[1] = grub_cpu_to_le32 (sector >> (GRUB_TYPE_BITS

(iv[1])

grub-core/disk/cryptodisk.c +0 -1
-					       -

dev->log_sector_size));

grub-core/disk/cryptodisk.c +0 -1
-	  iv[0] = grub_cpu_to_le32 ((sector <<

dev->log_sector_size)

grub-core/disk/cryptodisk.c +2 -0
+					       - log_sector_size));
+	  iv[0] = grub_cpu_to_le32 ((sector << log_sector_size)
 				    & GRUB_TYPE_U_MAX (iv[0]));
 	  break;
 	case GRUB_CRYPTODISK_MODE_IV_BENBI:
@@ -312,10 +323,10 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, case GRUB_CRYPTODISK_MODE_CBC:

grub-core/disk/cryptodisk.c +0 -0
 	  if (do_encrypt)
 	    err = grub_crypto_cbc_encrypt (dev->cipher, data + i,

data + i,

grub-core/disk/cryptodisk.c +0 -1
-					   (1U <<

dev->log_sector_size), iv);

grub-core/disk/cryptodisk.c +1 -0
+					   (1U <<

log_sector_size), iv); else
err = grub_crypto_cbc_decrypt (dev->cipher, data + i,
data + i,

grub-core/disk/cryptodisk.c +0 -1
-					   (1U <<

dev->log_sector_size), iv);

grub-core/disk/cryptodisk.c +1 -0
+					   (1U <<

log_sector_size), iv); if (err)

grub-core/disk/cryptodisk.c +0 -0
 	    return err;
 	  break;
@@ -323,10 +334,10 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, case GRUB_CRYPTODISK_MODE_PCBC:

grub-core/disk/cryptodisk.c +0 -0
 	  if (do_encrypt)
 	    err = grub_crypto_pcbc_encrypt (dev->cipher, data + i,

data + i,

grub-core/disk/cryptodisk.c +0 -1
-					    (1U <<

dev->log_sector_size), iv);

grub-core/disk/cryptodisk.c +1 -0
+					    (1U <<

log_sector_size), iv); else
err = grub_crypto_pcbc_decrypt (dev->cipher, data + i,
data + i,

grub-core/disk/cryptodisk.c +0 -1
-					    (1U <<

dev->log_sector_size), iv);

grub-core/disk/cryptodisk.c +1 -0
+					    (1U <<

log_sector_size), iv); if (err)

grub-core/disk/cryptodisk.c +2 -2
 	    return err;
 	  break;
@@ -338,7 +349,7 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, if (err)
 	      return err;
 
-	    for (j = 0; j < (1U << dev->log_sector_size);
+	    for (j = 0; j < (1U << log_sector_size);
 		 j += dev->cipher->cipher->blocksize)
 	      {
 		grub_crypto_xor (data + i + j, data + i + j, iv,
@@ -369,11 +380,11 @@ grub_cryptodisk_endecrypt (struct
 grub_cryptodisk *dev, if (do_encrypt)
 	      err = grub_crypto_ecb_encrypt (dev->cipher, data +
 i, data + i,
-					     (1U <<
 dev->log_sector_size));
+					     (1U <<

log_sector_size)); else
err = grub_crypto_ecb_decrypt (dev->cipher, data +
i, data + i,

grub-core/disk/cryptodisk.c +0 -1
-					     (1U <<

dev->log_sector_size));

grub-core/disk/cryptodisk.c +1 -0
+					     (1U <<

log_sector_size)); if (err)

grub-core/disk/cryptodisk.c +0 -0
 	      return err;
 	    lrw_xor (&sec, dev, data + i);
@@ -382,10 +393,10 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, case GRUB_CRYPTODISK_MODE_ECB:

grub-core/disk/cryptodisk.c +0 -0
 	  if (do_encrypt)
 	    err = grub_crypto_ecb_encrypt (dev->cipher, data + i,

data + i,

grub-core/disk/cryptodisk.c +0 -1
-					   (1U <<

dev->log_sector_size));

grub-core/disk/cryptodisk.c +1 -0
+					   (1U <<

log_sector_size)); else
err = grub_crypto_ecb_decrypt (dev->cipher, data + i,
data + i,

grub-core/disk/cryptodisk.c +0 -1
-					   (1U <<

dev->log_sector_size));

grub-core/disk/cryptodisk.c +1 -0
+					   (1U <<

log_sector_size)); if (err)

grub-core/disk/cryptodisk.c +0 -0
 	    return err;
 	  break;
@@ -400,9 +411,9 @@ grub_cryptodisk_endecrypt (struct

grub_cryptodisk *dev, gcry_err_code_t
grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,

grub-core/disk/cryptodisk.c +1 -1
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector)
+			 grub_disk_addr_t sector, grub_size_t

log_sector_size) {

grub-core/disk/cryptodisk.c +1 -1
-  return grub_cryptodisk_endecrypt (dev, data, len, sector, 0);
+  return grub_cryptodisk_endecrypt (dev, data, len, sector,

log_sector_size, 0); }

grub_err_t

grub-core/disk/cryptodisk.c +2 -2
@@ -767,7 +778,7 @@ grub_cryptodisk_read (grub_disk_t disk,
 grub_disk_addr_t sector, }
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) buf,
 					size <<
 disk->log_sector_size,
-					sector, 0);
+					sector,
 dev->log_sector_size, 0); return grub_crypto_gcry_error (gcry_err);
 }
@@ -808,7 +819,7 @@ grub_cryptodisk_write (grub_disk_t disk,
 grub_disk_addr_t sector, 
   gcry_err = grub_cryptodisk_endecrypt (dev, (grub_uint8_t *) tmp,
 					size <<
 disk->log_sector_size,
-					sector, 1);
+					sector,
 disk->log_sector_size, 1); if (gcry_err)
     {
       grub_free (tmp);
grub-core/disk/luks.c +1 -1
index aa9877b68..84c3fa73a 100644
@@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char
 *check_uuid, return NULL;
   newdev->offset_sectors = grub_be_to_cpu32 (header.payloadOffset);
   newdev->source_disk = NULL;
-  newdev->log_sector_size = 9;
+  newdev->log_sector_size = GRUB_LUKS1_LOG_SECTOR_SIZE;
   newdev->total_sectors = grub_disk_get_size (disk) -

newdev->offset_sectors; grub_memcpy (newdev->uuid, uuid, sizeof (uuid)); newdev->modname = "luks";

grub-core/disk/luks.c +2 -1
@@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source,
 	  return err;
 	}
 
-      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length,
 0);
+      gcry_err = grub_cryptodisk_decrypt (dev, split_key, length,
 0,
+
 GRUB_LUKS1_LOG_SECTOR_SIZE); if (gcry_err)
grub-core/disk/luks.c +0 -0
 	{
 	  grub_free (split_key);
grub-core/disk/luks2.c +6 -1
index 355bb4aec..4a4a0dec4 100644
@@ -504,7 +504,12 @@ luks2_decrypt_key (grub_uint8_t *out_key,
       goto err;
     }
 
-  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key,
 k->area.size, 0);
+  /*
+   * The key slots area is always encrypted in 512-byte sectors,
+   * regardless of encrypted data sector size.
+   */
+  gcry_ret = grub_cryptodisk_decrypt (crypt, split_key,
 k->area.size, 0,
+				      GRUB_LUKS1_LOG_SECTOR_SIZE);
   if (gcry_ret)
     {
       ret = grub_crypto_gcry_error (gcry_ret);
include/grub/cryptodisk.h +0 -0
index 258b777bf..ee30e4537 100644
@@ -48,6 +48,12 @@ typedef enum
 
 #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71
 
Quoted 7 lines
Quoted 2 lines
include/grub/cryptodisk.h +1 -0
+/* LUKS1 specification defines the block size to always be 512

bytes. */ +#define GRUB_LUKS1_LOG_SECTOR_SIZE 9

Sorry to be nitpicky, but this constant is used for both LUKS1 and LUKS2. Shouldn't it just be called GRUB_LUKS_LOG_SECTOR_SIZE?

Patrick

Its named LUKS1 because that macro is meant to represent the log of the sector size for the encrypted data segment, which is fixed at 512-bytes (as you know LUKS2 is variable). I think a name suggesting its only for the encrypted key data would be even longer and more unwieldy. And I don't think just removing the '1' would be an accurate name. Do you have other suggestions?

Glenn

Quoted 19 lines
Quoted 19 lines
include/grub/cryptodisk.h +3 -1
+/* By default dm-crypt increments the IV every 512 bytes. */
+#define GRUB_CRYPTODISK_IV_LOG_SIZE 9
+
 #define GRUB_CRYPTODISK_GF_LOG_SIZE 7
 #define GRUB_CRYPTODISK_GF_SIZE (1U << GRUB_CRYPTODISK_GF_LOG_SIZE)
 #define GRUB_CRYPTODISK_GF_LOG_BYTES (GRUB_CRYPTODISK_GF_LOG_SIZE
- 3) @@ -145,7 +151,7 @@ grub_cryptodisk_setkey (grub_cryptodisk_t

dev, gcry_err_code_t
grub_cryptodisk_decrypt (struct grub_cryptodisk *dev,

include/grub/cryptodisk.h +1 -1
 			 grub_uint8_t * data, grub_size_t len,
-			 grub_disk_addr_t sector);
+			 grub_disk_addr_t sector, grub_size_t

log_sector_size); grub_err_t
grub_cryptodisk_insert (grub_cryptodisk_t newdev, const char *name,
grub_disk_t source);

Signature · 1 line
2.27.0
#124 lore raw

Re: [PATCH v4 14/15] luks2: Better error handling when setting up the cryptodisk.

In reply to Patrick Steinhardt
Quoted 5 lines · On Sun, 15 Nov 2020 11:13:04 +0100 Patrick Steinhardt <[email protected]> wrote:
Quoted 1 line · Glenn Washburn

A commit message would help to set the stage for your changes here, especially so as they're non-trivial.

That's reasonable. I'll add some commentary.

Quoted 69 lines
Quoted 61 lines
---
 grub-core/disk/luks2.c | 70

+++++++++++++++++++++++++++++++++++++++--- include/grub/misc.h |
2 ++ 2 files changed, 67 insertions(+), 5 deletions(-)

grub-core/disk/luks2.c +10 -4
index 4a4a0dec4..751b48d6a 100644
@@ -600,9 +600,16 @@ luks2_recover_key (grub_disk_t source,
       goto err;
     }
 
+  if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN)
+    {
+      ret = grub_error (GRUB_ERR_BUG, "not a luks2 device");
+      goto err;
+    }
+
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json,
 i); if (ret)
 	goto err;
@@ -617,13 +624,66 @@ luks2_recover_key (grub_disk_t source,
 
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset,
 segment.sector_size, NULL);
-      crypt->log_sector_size = sizeof (unsigned int) * 8
-		- __builtin_clz ((unsigned int)
 segment.sector_size) - 1;
+      crypt->log_sector_size = grub_log2ull (segment.sector_size);
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (source) >>
 (crypt->log_sector_size - source->log_sector_size))
-			       - crypt->offset_sectors;
+	{
+	  /* Convert source sized number of sectors to cryptodisk

sized sectors */

grub-core/disk/luks2.c +1 -0
+	  crypt->total_sectors = source->total_sectors >>

(crypt->log_sector_size - source->log_sector_size);

grub-core/disk/luks2.c +3 -0
+	  if (crypt->total_sectors < crypt->offset_sectors)
+	    {
+	      grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\" offset"

grub-core/disk/luks2.c +7 -1
+				     " is greater than disk size.",
+				      segment.slot_key);
+	      continue;
+	    }
+
+	  crypt->total_sectors -= crypt->offset_sectors;
+	}
       else
-	crypt->total_sectors = grub_strtoull (segment.size, NULL,

10) >> crypt->log_sector_size;

grub-core/disk/luks2.c +2 -0
+	{
+	  crypt->total_sectors = grub_strtoull (segment.size,

NULL, 10) >> crypt->log_sector_size;

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 646

Quoted 2 lines
grub-core/disk/luks2.c +2 -0
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;

If any other code previously ran which set grub_errno, then it would still carry the old error value here as grub_strtoull doesn't unset it if no error occurred. We'd thus have to first set grub_errno = GRUB_ERR_NONE before the call to grub_strtoull.

Quoted 1 line

Patrick

I intentionally set "grub_errno = GRUB_ERR_NONE" higher up at the start of the for block. I was wanting to not clobber any uncaught errors coming out of luks2_get_keyslot. But now that I think about it, the error codes tested for here could come out of luks2_get_keyslot. So I'll reset grub_errno here and check for uncaught errors after luks2_get_keyslot.

Glenn

Quoted 68 lines
Quoted 68 lines
grub-core/disk/luks2.c +3 -0
+	  else if(grub_errno == GRUB_ERR_BAD_NUMBER)
+	    {
+	      /* TODO: Unparsable number-string, try to use the

whole disk */

grub-core/disk/luks2.c +1 -0
+	      grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\" size"

grub-core/disk/luks2.c +7 -0
+				     " is not a parsable number.",
+				     segment.slot_key);
+	      continue;
+	    }
+	  else if(grub_errno == GRUB_ERR_OUT_OF_RANGE)
+	    {
+	      /* There was an overflow in parsing segment.size, so

disk must

grub-core/disk/luks2.c +3 -0
+	       * be very large or the string is incorrect. */
+	      if ((source->total_sectors
+		    >> (crypt->log_sector_size -

source->log_sector_size))

grub-core/disk/luks2.c +3 -0
+		  > crypt->total_sectors)
+		{
+		  grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\""

grub-core/disk/luks2.c +1 -0
+				         " size is very large. The

end may be"

grub-core/disk/luks2.c +6 -0
+				         " inaccessible.",
+				         segment.slot_key);
+		}
+	      else
+		{
+		  /* FIXME: Set total_sectors as in "dynamic"

case. */

grub-core/disk/luks2.c +1 -0
+		  grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\""

grub-core/disk/luks2.c +1 -0
+				         " size greater than the

source"

grub-core/disk/luks2.c +10 -0
+					 " device.",
+				         segment.slot_key);
+		  continue;
+		}
+	    }
+	}
+
+      if (crypt->total_sectors == 0)
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\"

has"

grub-core/disk/luks2.c +4 -0
+				 " zero sectors, skipping.",
+				 segment.slot_key);
+	  continue;
+	}
 
       ret = luks2_decrypt_key (candidate_key, source, crypt,

&keyslot, (const grub_uint8_t *) passphrase, grub_strlen (passphrase)); diff --git a/include/grub/misc.h b/include/grub/misc.h index b7ca6dd58..ec25131ba 100644

include/grub/misc.h +2 -0
@@ -481,5 +481,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const
 char *file, 
 #define grub_max(a, b) (((a) > (b)) ? (a) : (b))
 #define grub_min(a, b) (((a) < (b)) ? (a) : (b))
+#define grub_log2ull(n) (GRUB_TYPE_BITS (grub_uint64_t) \
+                         - __builtin_clzll (n) - 1)
 
 #endif /* ! GRUB_MISC_HEADER */
Signature · 1 line
2.27.0
#126 lore raw

Re: [PATCH v4 14/15] luks2: Better error handling when setting up the cryptodisk.

In reply to Daniel Kiper
Quoted 30 lines · On Tue, 17 Nov 2020 15:26:08 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 29 lines · Glenn Washburn
---
 grub-core/disk/luks2.c | 70

+++++++++++++++++++++++++++++++++++++++--- include/grub/misc.h |
2 ++ 2 files changed, 67 insertions(+), 5 deletions(-)

grub-core/disk/luks2.c +7 -0
index 4a4a0dec4..751b48d6a 100644
@@ -600,9 +600,16 @@ luks2_recover_key (grub_disk_t source,
       goto err;
     }
 
+  if (source->total_sectors == GRUB_DISK_SIZE_UNKNOWN)
+    {
+      ret = grub_error (GRUB_ERR_BUG, "not a luks2 device");
+      goto err;
+    }
+
   /* Try all keyslot */
   for (i = 0; i < size; i++)
     {
+      grub_errno = GRUB_ERR_NONE;
       ret = luks2_get_keyslot (&keyslot, &digest, &segment, json,
 i); if (ret)
 	goto err;
@@ -617,13 +624,66 @@ luks2_recover_key (grub_disk_t source,
 
Quoted 9 lines

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 629

Quoted 7 lines
grub-core/disk/luks2.c +1 -2
       /* Set up disk according to keyslot's segment. */
       crypt->offset_sectors = grub_divmod64 (segment.offset,
 segment.sector_size, NULL);
-      crypt->log_sector_size = sizeof (unsigned int) * 8
-		- __builtin_clz ((unsigned int)
 segment.sector_size) - 1;
+      crypt->log_sector_size = grub_log2ull (segment.sector_size);

This change does not belong to this patch.

I'll put it in a new patch.

Quoted 4 lines
Quoted 4 lines
grub-core/disk/luks2.c +0 -2
       if (grub_strcmp (segment.size, "dynamic") == 0)
-	crypt->total_sectors = (grub_disk_get_size (source) >>
 (crypt->log_sector_size - source->log_sector_size))
-			       - crypt->offset_sectors;
Quoted 8 lines
Quoted 5 lines
grub-core/disk/luks2.c +2 -0
+	{
+	  /* Convert source sized number of sectors to cryptodisk

sized sectors */

grub-core/disk/luks2.c +1 -0
+	  crypt->total_sectors = source->total_sectors >>

(crypt->log_sector_size - source->log_sector_size);

If you add the other checks could you also add the following check:
crypt->log_sector_size >= source->log_sector_size ?

Why should this be a constraint? I could add it, but I don't see the problem when crypt->log_sector_size < source->log_sector_size.

Quoted 8 lines

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 635

Quoted 5 lines
grub-core/disk/luks2.c +3 -0
+	  if (crypt->total_sectors < crypt->offset_sectors)
+	    {
+	      grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\" offset"

grub-core/disk/luks2.c +1 -0
+				     " is greater than disk size.",

Please replace "." at the end of the message with "\n" here and below...

Good catch.

Quoted 23 lines
Quoted 23 lines
grub-core/disk/luks2.c +6 -1
+				      segment.slot_key);
+	      continue;
+	    }
+
+	  crypt->total_sectors -= crypt->offset_sectors;
+	}
       else
-	crypt->total_sectors = grub_strtoull (segment.size, NULL,

10) >> crypt->log_sector_size;

grub-core/disk/luks2.c +2 -0
+	{
+	  crypt->total_sectors = grub_strtoull (segment.size,

NULL, 10) >> crypt->log_sector_size;

grub-core/disk/luks2.c +5 -0
+	  if (grub_errno == GRUB_ERR_NONE)
+	    ;
+	  else if(grub_errno == GRUB_ERR_BAD_NUMBER)
+	    {
+	      /* TODO: Unparsable number-string, try to use the

whole disk */

grub-core/disk/luks2.c +1 -0
+	      grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\" size"

grub-core/disk/luks2.c +3 -0
+				     " is not a parsable number.",
+				     segment.slot_key);
+	      continue;
Quoted 8 lines

grub-core/disk/luks2.c luks2_recover_key (grub_disk_t source, · line 658

Quoted 6 lines
grub-core/disk/luks2.c +4 -0
+	    }
+	  else if(grub_errno == GRUB_ERR_OUT_OF_RANGE)
+	    {
+	      /* There was an overflow in parsing segment.size, so

disk must

grub-core/disk/luks2.c +1 -0
+	       * be very large or the string is incorrect. */

Please format this comment properly.

Will do.

Quoted 45 lines
Quoted 45 lines
grub-core/disk/luks2.c +2 -0
+	      if ((source->total_sectors
+		    >> (crypt->log_sector_size -

source->log_sector_size))

grub-core/disk/luks2.c +3 -0
+		  > crypt->total_sectors)
+		{
+		  grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\""

grub-core/disk/luks2.c +1 -0
+				         " size is very large. The

end may be"

grub-core/disk/luks2.c +6 -0
+				         " inaccessible.",
+				         segment.slot_key);
+		}
+	      else
+		{
+		  /* FIXME: Set total_sectors as in "dynamic"

case. */

grub-core/disk/luks2.c +1 -0
+		  grub_dprintf ("luks2", "Segment

\"%"PRIuGRUB_UINT64_T"\""

grub-core/disk/luks2.c +1 -0
+				         " size greater than the

source"

grub-core/disk/luks2.c +10 -0
+					 " device.",
+				         segment.slot_key);
+		  continue;
+		}
+	    }
+	}
+
+      if (crypt->total_sectors == 0)
+	{
+	  grub_dprintf ("luks2", "Segment \"%"PRIuGRUB_UINT64_T"\"

has"

grub-core/disk/luks2.c +4 -0
+				 " zero sectors, skipping.",
+				 segment.slot_key);
+	  continue;
+	}
 
       ret = luks2_decrypt_key (candidate_key, source, crypt,

&keyslot, (const grub_uint8_t *) passphrase, grub_strlen (passphrase)); diff --git a/include/grub/misc.h b/include/grub/misc.h index b7ca6dd58..ec25131ba 100644

include/grub/misc.h +0 -0
@@ -481,5 +481,7 @@ void EXPORT_FUNC(grub_real_boot_time) (const
 char *file,
 
Quoted 4 lines

include/grub/misc.h void EXPORT_FUNC(grub_real_boot_time) (const char *file, · line 483

Quoted 2 lines
include/grub/misc.h +0 -0
 #define grub_max(a, b) (((a) > (b)) ? (a) : (b))
 #define grub_min(a, b) (((a) < (b)) ? (a) : (b))

Please add empty line here.

Ok.

Quoted 6 lines

include/grub/misc.h void EXPORT_FUNC(grub_real_boot_time) (const char *file, · line 485

Quoted 2 lines
include/grub/misc.h +2 -0
+#define grub_log2ull(n) (GRUB_TYPE_BITS (grub_uint64_t) \
+                         - __builtin_clzll (n) - 1)

This change does not belong to this patch too. Additionally, please double check that misc.h includes all headers which define GRUB_TYPE_BITS(), grub_uint64_t and __builtin_clzll().

I'll add to a new patch.

Quoted 2 lines

By the way, may I ask you to stop adding full stop to the end of email subject?

If I understand correctly, you dislike periods ('.') at the end of the email subject line. I can not add them. What do you dislike about them there? (extra superfluous character?)

Glenn