Cryptodisk fixes for v2.06 redux
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
PATCH 1/10 luks2: Fix use of incorrect index and some grub_error() messages.
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
@@ -275,34 +275,34 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sreturn 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
PATCH 2/10 luks2: Improve readability in luks2_get_keyslot.
In reply to Glenn WashburnIntroduce 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
@@ -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
PATCH 3/10 luks2: Use more intuitive keyslot key instead of index when naming keyslot.
In reply to Glenn WashburnUse 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
@@ -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
PATCH 4/10 luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
In reply to Glenn WashburnThe 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
@@ -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
PATCH 5/10 cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
In reply to Glenn WashburnWhen 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
@@ -501,10 +501,10 @@ grub_cryptodisk_setcipher (grub_cryptodisk_t crypt, const char *ciphername, consif (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
PATCH 6/10 cryptodisk: Properly handle non-512 byte sized sectors.
In reply to Glenn WashburnBy 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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 7/10 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
In reply to Glenn WashburnThis 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
@@ -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
@@ -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
PATCH 8/10 cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
In reply to Glenn WashburnThis 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -73,7 +73,8 @@ struct grub_cryptodiskchar *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
PATCH 9/10 cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
In reply to Glenn WashburnThis 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -72,7 +72,12 @@ struct grub_cryptodiskstruct 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
PATCH 10/10 luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.
In reply to Glenn WashburnThis 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
@@ -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
v2 cover Cryptodisk fixes for v2.06 redux
In reply to Glenn WashburnThis 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
PATCH v2 1/10 luks2: Fix use of incorrect index and some grub_error() messages.
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
@@ -275,34 +275,34 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sreturn 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
Re: [PATCH v2 01/10] luks2: Fix use of incorrect index and some grub_error() messages.
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.Signed-off-by Glenn Washburn <[email protected]>
Quoted 53 lines
--- grub-core/disk/luks2.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)grub-core/disk/luks2.c +7
-7@@ -275,34 +275,34 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sreturn 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
PATCH v2 2/10 luks2: Improve readability in luks2_get_keyslot.
In reply to Glenn WashburnIntroduce 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
@@ -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
PATCH v2 3/10 luks2: Use more intuitive keyslot key instead of index when naming keyslot.
In reply to Glenn WashburnUse 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
@@ -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
Re: [PATCH v2 03/10] luks2: Use more intuitive keyslot key instead of index when naming keyslot.
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
Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)grub-core/disk/luks2.c +14
-13@@ -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
Re: [PATCH v2 03/10] luks2: Use more intuitive keyslot key instead of index when naming keyslot.
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
Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)grub-core/disk/luks2.c +3
-3@@ -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))
returngrub_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_sluks2_parse_digest(d, &digest)) returngrub_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 forkeyslot \"%"PRIuGRUB_UINT64_T"\"",
keyslot_key);
+ returngrub_error(GRUB_ERR_FILE_NOT_FOUND, "No digest forkeyslot \"%"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_tdisk, (constgrub_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
PATCH v2 4/10 luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
In reply to Glenn WashburnThe 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
@@ -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
Re: [PATCH v2 04/10] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
Quoted 4 lines · Glenn Washburn
The
total_lengthfield 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_sizereturns 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.
Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)grub-core/disk/luks2.c +4
-3@@ -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
PATCH v2 5/10 cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
In reply to Glenn WashburnWhen 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
@@ -501,10 +501,10 @@ grub_cryptodisk_setcipher (grub_cryptodisk_t crypt, const char *ciphername, consif (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
PATCH v2 6/10 cryptodisk: Properly handle non-512 byte sized sectors.
In reply to Glenn WashburnBy 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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCH v2 06/10] cryptodisk: Properly handle non-512 byte sized sectors.
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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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@@ -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@@ -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@@ -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@@ -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
Re: [PATCH v2 06/10] cryptodisk: Properly handle non-512 byte sized sectors.
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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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.cindexa3d672f68..623f0f396100644grub-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, caseGRUB_CRYPTODISK_MODE_CBC:grub-core/disk/cryptodisk.c +0
-0if (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
-0return err;break ;@@ -322,10 +332,10 @@ grub_cryptodisk_endecrypt (struct
grub_cryptodisk*dev, caseGRUB_CRYPTODISK_MODE_PCBC:grub-core/disk/cryptodisk.c +0
-0if (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
-2return 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
-0return err;lrw_xor (&sec, dev, data + i);@@ -381,10 +391,10 @@ grub_cryptodisk_endecrypt (struct
grub_cryptodisk*dev, caseGRUB_CRYPTODISK_MODE_ECB:grub-core/disk/cryptodisk.c +0
-0if (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
-0return err;break ;@@ -399,9 +409,9 @@ grub_cryptodisk_endecrypt (struct
grub_cryptodisk*dev,gcry_err_code_t
grub_cryptodisk_decrypt(structgrub_cryptodisk*dev,grub-core/disk/cryptodisk.c +1
-1grub_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_tgrub-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@@ -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@@ -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@@ -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_SIZE9include/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_tdev,
gcry_err_code_t
grub_cryptodisk_decrypt(structgrub_cryptodisk*dev,include/grub/cryptodisk.h +1
-1grub_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_tnewdev, const char *name,
grub_disk_tsource);Signature · 1 line
2.27.0
PATCH v2 7/10 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
In reply to Glenn WashburnThis 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
@@ -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
@@ -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
Re: [PATCH v2 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
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.
Signed-off-by Glenn Washburn <[email protected]>--- 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@@ -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
-0dev->cipher->cipher->blocksize);if (err)include/grub/types.h +2
-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) +#define GRUB_TYPE_MAX (type) ((2 * ((1ULL << (GRUB_TYPE_BIT(type) -1)) - 1)) + 1) +
#endif /* !GRUB_TYPES_HEADER*/
Re: [PATCH v2 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
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.htmlI'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.
Signed-off-by Glenn Washburn <[email protected]>--- 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@@ -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
-0dev->cipher->cipher->blocksize);if (err)include/grub/types.h +2
-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) +#define GRUB_TYPE_MAX (type) ((2 * ((1ULL << (GRUB_TYPE_BIT(type) -1)) - 1)) + 1) +
#endif /* !GRUB_TYPES_HEADER*/
PATCH v2 8/10 cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
In reply to Glenn WashburnThis 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -73,7 +73,8 @@ struct grub_cryptodiskchar *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
Re: [PATCH v2 08/10] cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
Quoted 5 lines · Glenn Washburn
This makes the creates an alignment with
grub_disk_tnaming of the same field and is more intuitive as to how it should be used.Signed-off-by Glenn Washburn <[email protected]>---
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@@ -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@@ -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@@ -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@@ -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@@ -73,7 +73,8 @@ struct grub_cryptodiskchar *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
PATCH v2 9/10 cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
In reply to Glenn WashburnThis 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -72,7 +72,12 @@ struct grub_cryptodiskstruct 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
Re: [PATCH v2 09/10] cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
Quoted 4 lines · Glenn Washburn
This makes it clear that the offset represents sectors, not bytes, in order to improve readability.
Signed-off-by Glenn Washburn <[email protected]>
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@@ -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@@ -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@@ -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@@ -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@@ -72,7 +72,12 @@ struct grub_cryptodiskstruct 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
PATCH v2 10/10 luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.
In reply to Glenn WashburnThis 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
@@ -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
Re: [PATCH v2 10/10] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.
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
Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)grub-core/disk/luks2.c +11
-11@@ -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
Re: [PATCH v2 10/10] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.
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
Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)grub-core/disk/luks2.c +2
-2@@ -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, (constgrub_uint8_t*) passphrase,grub_strlen(passphrase)); if (ret)
{Signature · 1 line
2.27.0
Re: [PATCH v2 10/10] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.
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,
sourceis 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
sourcebecause it is where the data is coming from and to distinguish it from the cryptodiskgrub_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 somecryptodisk.candluks.ccode.Note that in
grub_cryptodisk_scan_device_realincryptodisk.cwhich callsluks2_recover_keythegrub_disk_tpassed is namedsource. And in ingrub_cryptodisk_open, the parameterdiskrefers to agrub_disk_tthat can be read to decrypt an associated encryptedgrub_disk_t(ie. the cryptodiskgrub_disk_t). Thegrub_cryptodisk_tassociated indisk->datahas a member named "source_disk" which points to the associatedgrub_disk_tand member "source" which is the name of the associated disk. In bothluks2_decrypt_keyandluks2_recover_keythegrub_disk_targument refers to the encryptedgrub_disk_twhich can be accessed as (unencrypted disk)->data->source_disk on the opened crypto disk. So I think its consistent withcryptodisk.cnaming conventions to call thegrub_disk_targument "source". Also as the subject line says, this creates consistency withluks.cin itsluks2_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
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 somegrub_error()messages.
luks2: Improve readability inluks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming
keyslot.
luks2:grub_cryptodisk_t->total_lengthis 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: Renametotal_lengthfield ingrub_cryptodisk_tto
total_sectors.
cryptodisk: Rename offset ingrub_cryptodisk_ttooffset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as inluks.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:e2433b8abluks2: 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_tdisk, const char*check_uu##
grub-core/disk/luks2.c##
@@grub-core/disk/luks2.c:luks2_recover_key(grub_disk_tdisk,
-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:86beb5be8luks2: Rename source disk variabled named 'disk' to 'source' as inluks.c.Signature · 1 line
2.27.0
v3 cover Cryptodisk fixes for v2.06 redux
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
PATCH v3 1/10 luks2: Fix use of incorrect index and some grub_error() messages.
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
@@ -275,34 +275,34 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sreturn 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
Re: [PATCH v3 01/10] luks2: Fix use of incorrect index and some grub_error() messages.
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.Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)grub-core/disk/luks2.c +2
-2@@ -275,34 +275,34 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sreturn 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
-2if (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
-2luks2_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
Patrick Steinhardt
Reviewed-byRe: [PATCH v3 01/10] luks2: Fix use of incorrect index and some grub_error() messages.
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.Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)grub-core/disk/luks2.c +3
-3@@ -275,34 +275,34 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sreturn 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
-2luks2_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
PATCH v3 2/10 luks2: Improve readability in luks2_get_keyslot.
In reply to Glenn WashburnIntroduce 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
@@ -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
Re: [PATCH v3 02/10] luks2: Improve readability in luks2_get_keyslot.
Quoted 7 lines · Glenn Washburn
Introduce new variables
keyslot_key,digest_key, andsegment_keywhich 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
Re: [PATCH v3 02/10] luks2: Improve readability in luks2_get_keyslot.
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, andsegment_keywhich 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
Re: [PATCH v3 02/10] luks2: Improve readability in luks2_get_keyslot.
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, andsegment_keywhich 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 inluks2_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 tokeyslot_idxinluks2_get_keyslot. 1: luks2: Use correct index variable when looping inluks2_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
PATCH v3 3/10 luks2: Use more intuitive keyslot key instead of index when naming keyslot.
In reply to Glenn WashburnUse 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
@@ -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
Re: [PATCH v3 03/10] luks2: Use more intuitive keyslot key instead of index when naming keyslot.
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.
Signed-off-by Glenn Washburn <[email protected]>
Patrick
Quoted 91 lines
--- grub-core/disk/luks2.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)grub-core/disk/luks2.c +12
-11@@ -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
PATCH v3 4/10 luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
In reply to Glenn WashburnThe 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
@@ -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
Re: [PATCH v3 04/10] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
Quoted 10 lines · Glenn Washburn
The
total_lengthfield 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_sizereturns 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.
Signed-off-by Glenn Washburn <[email protected]>
Patrick
Quoted 33 lines
--- grub-core/disk/luks2.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)grub-core/disk/luks2.c +4
-3@@ -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
PATCH v3 5/10 cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
In reply to Glenn WashburnWhen 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
@@ -501,10 +501,10 @@ grub_cryptodisk_setcipher (grub_cryptodisk_t crypt, const char *ciphername, consif (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
Re: [PATCH v3 05/10] cryptodisk: Fix cipher IV mode 'plain64' always being set as 'plain'.
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".
Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Patrick Steinhardt <[email protected]>
Daniel
PATCH v3 6/10 cryptodisk: Properly handle non-512 byte sized sectors.
In reply to Glenn WashburnBy 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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.
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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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@@ -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@@ -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@@ -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
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
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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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@@ -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@@ -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@@ -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@@ -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 9Nit: 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
Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.
In reply to Glenn WashburnQuoted 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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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@@ -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
Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.
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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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.cindexa3d672f68..623f0f396100644grub-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
Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.
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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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.cindexa3d672f68..623f0f396100644grub-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 errorsQuoted 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
@@ -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
@@ -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
@@ -115,7 +115,7 @@ intEXPORT_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
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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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.cindexa3d672f68..623f0f396100644grub-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(structgrub_cryptodisk*dev, }grub-core/disk/cryptodisk.c +2
-3break ;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 errorsQuoted 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@@ -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 (structgrub_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
-0iv[1 ] =0 ; }(quoted diff) +0
-0
b/grub-core/kern/compiler-rt.cindexa464200c6..2057c2e0c100644grub-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@@ -115,7 +115,7 @@ intEXPORT_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
Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.
Quoted 138 lines · Glenn Washburn
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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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.cindexa3d672f68..623f0f396100644grub-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(structgrub_cryptodisk*dev, }grub-core/disk/cryptodisk.c +2
-3break ;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 errorsQuoted 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
Quoted 9 lines
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_clzllinluks2.cinto a macro inmisc.h, but I don't think that would have necessitated this change because the only code that uses the macro is inluks2.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 am not sure why this pop up after your patch series. If I have some time I will dig into it.
Quoted 3 lines
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.cchanges.
Please provide compiler-rt* changes in a separate patch. Good example is in commit 9dab2f51e (sparc: Enable __clzsi2() and __clzdi2()). Please include this fix in new patch series.
Daniel
Re: [PATCH v3 06/10] cryptodisk: Properly handle non-512 byte sized sectors.
Quoted 160 lines · On Fri, 20 Nov 2020 15:17:25 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 142 lines · Glenn Washburn
Quoted 130 lines · On Thu, 19 Nov 2020 15:25:33 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 129 lines · Glenn Washburn
Quoted 105 lines · On Fri, 30 Oct 2020 21:47:14 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 87 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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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.cindexa3d672f68..623f0f396100644 ---a/grub-core/disk/cryptodisk.c
+++b/grub-core/disk/cryptodisk.c(quoted diff) +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;(quoted diff) +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));(quoted diff) +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(structgrub_cryptodisk*dev, }(quoted diff) +2
-3break; 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-cryptnative sector
(quoted diff) +1
-0+ * number. If using 32 bit IV mode, zero out themost significant
Review
Quoted 4 lines
(quoted diff) +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 errorsQuoted 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
Quoted 9 lines
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_clzllinluks2.cinto a macro inmisc.h, but I don't think that would have necessitated this change because the only code that uses the macro is inluks2.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 am not sure why this pop up after your patch series. If I have some time I will dig into it.
Quoted 3 lines
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.cchanges.
Quoted 5 lines
Please provide compiler-rt* changes in a separate patch. Good example is in commit
9dab2f51e(sparc: Enable__clzsi2()and__clzdi2()). Please include this fix in new patch series.Daniel
Ok, will do. I'm unsure how to commit the patch though. Since you are the author and I am the committer. I'm thinking I should have my name as the commit author, but yours should be in a Signed-off-by sig and I don't need to add a Signed-off-by sig. How should I proceed? I'll follow the commit message template from 9dab2f51e. Also, how should I attribute your contribution in cryptodisk.c?
Glenn
PATCH v3 7/10 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
In reply to Glenn WashburnThis 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
@@ -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
@@ -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
Re: [PATCH v3 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
Quoted 45 lines · Glenn Washburn
This should improve readability of code by providing clues as to what the value represents.
Signed-off-by Glenn Washburn <[email protected]>--- 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@@ -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
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
Re: [PATCH v3 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
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.
Signed-off-by Glenn Washburn <[email protected]>---
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@@ -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;
caseGRUB_CRYPTODISK_MODE_IV_BENBI:
{grub_uint64_tnum = (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;
caseGRUB_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@@ -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
Re: [PATCH v3 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
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.
Signed-off-by Glenn Washburn <[email protected]>---
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@@ -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;
caseGRUB_CRYPTODISK_MODE_IV_BENBI:
{grub_uint64_tnum = (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;
caseGRUB_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
-0include/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_BITabove it. Should there be a separate patch renamingGRUB_CHAR_BITalso?
Nah, your naming makes sense in that context. Let's keep it as-is in this patch.
Patrick
Re: [PATCH v3 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
In reply to Glenn WashburnQuoted 14 lines · Glenn Washburn
This should improve readability of code by providing clues as to what the value represents.
Signed-off-by Glenn Washburn <[email protected]>--- 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
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
-0err =grub_crypto_ecb_encrypt (dev->essiv_cipher, iv, iv, dev->cipher->cipher->blocksize);if (err)include/grub/types.h +0
-0@@ -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
Re: [PATCH v3 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
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.
Signed-off-by Glenn Washburn <[email protected]>--- 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.cindex623f0f396..1a91c2d55100644grub-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
-0err =grub_crypto_ecb_encrypt (dev->essiv_cipher, iv, iv, dev->cipher->cipher->blocksize);if (err)include/grub/types.h +0
-0@@ -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 8I 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_BITand/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 norGRUB_CHAR_BITis not equal 8.
I'm not sure exactly how you want that done.
Quoted 6 lines
Last but not least, the
GRUB_CHAR_BITdefinition should be right behind includes in theinclude/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_MINandGRUB_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
Re: [PATCH v3 07/10] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
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@@ -319,4 +319,7 @@ static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val)#define GRUB_CHAR_BIT 8I 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_BITand/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 norGRUB_CHAR_BITis 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_BITdefinition should be right behind includes in theinclude/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_MINandGRUB_TYPE_S_MAX.How about I just rename
GRUB_TYPE_MAXtoGRUB_TYPE_U_MAXand 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
PATCH v3 8/10 cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
In reply to Glenn WashburnThis 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -73,7 +73,8 @@ struct grub_cryptodiskchar *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
Re: [PATCH v3 08/10] cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
Quoted 4 lines · Glenn Washburn
This creates an alignment with
grub_disk_tnaming of the same field and is more intuitive as to how it should be used.Signed-off-by Glenn Washburn <[email protected]>
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@@ -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@@ -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@@ -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@@ -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@@ -73,7 +73,8 @@ struct grub_cryptodiskchar *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
Daniel Kiper
Reviewed-byRe: [PATCH v3 08/10] cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn
This creates an alignment with
grub_disk_tnaming of the same field and is more intuitive as to how it should be used.Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Patrick Steinhardt <[email protected]>
Daniel
PATCH v3 9/10 cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
In reply to Glenn WashburnThis 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -72,7 +72,12 @@ struct grub_cryptodiskstruct 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
Re: [PATCH v3 09/10] cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
Quoted 4 lines · Glenn Washburn
This makes it clear that the offset represents sectors, not bytes, in order to improve readability.
Signed-off-by Glenn Washburn <[email protected]>
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@@ -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@@ -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@@ -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@@ -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@@ -72,7 +72,12 @@ struct grub_cryptodiskstruct 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
Daniel Kiper
Reviewed-byRe: [PATCH v3 09/10] cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn
This makes it clear that the offset represents sectors, not bytes, in order to improve readability.
Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Patrick Steinhardt <[email protected]>
Daniel
PATCH v3 10/10 luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.
In reply to Glenn WashburnThis 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
@@ -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
Re: [PATCH v3 10/10] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.
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.
Signed-off-by Glenn Washburn <[email protected]>
Patrick
Quoted 86 lines
--- grub-core/disk/luks2.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)grub-core/disk/luks2.c +11
-11@@ -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
Daniel Kiper
Reviewed-byRe: [PATCH v3 10/10] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.
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.
Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Patrick Steinhardt <[email protected]>
Daniel
Quoted 19 lines · Glenn Washburn
Heres an updated patch series which addresses comment from Patrick. The only code change is adding a
slot_keymember togrub_luks2_keyslotand using that instead of an extra out parameter toluks2_get_keyslot.Glenn Washburn (10):
luks2: Fix use of incorrect index and somegrub_error()messages.
luks2: Improve readability inluks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming
keyslot.
luks2:grub_cryptodisk_t->total_lengthis 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: Renametotal_lengthfield ingrub_cryptodisk_tto
total_sectors.
cryptodisk: Rename offset ingrub_cryptodisk_ttooffset_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
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_keymember togrub_luks2_keyslotand using that instead of an extra out parameter toluks2_get_keyslot.Glenn Washburn (10):
luks2: Fix use of incorrect index and somegrub_error()messages.
luks2: Improve readability inluks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming
keyslot.
luks2:grub_cryptodisk_t->total_lengthis 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: Renametotal_lengthfield ingrub_cryptodisk_tto
total_sectors.
cryptodisk: Rename offset ingrub_cryptodisk_ttooffset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as inQuoted 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
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_keymember togrub_luks2_keyslotand using that instead of an extra out parameter toluks2_get_keyslot.Quoted 16 lines
Glenn Washburn (10):
luks2: Fix use of incorrect index and somegrub_error()messages.
luks2: Improve readability inluks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming keyslot.
luks2:grub_cryptodisk_t->total_lengthis 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 ingrub_cryptodisk_endecrypt.
cryptodisk: Renametotal_lengthfield ingrub_cryptodisk_ttototal_sectors.
cryptodisk: Rename offset ingrub_cryptodisk_ttooffset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as inluks.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
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_keymember togrub_luks2_keyslotand using that instead of an extra out parameter toluks2_get_keyslot.Quoted 16 lines
Glenn Washburn (10):
luks2: Fix use of incorrect index and somegrub_error()messages.
luks2: Improve readability inluks2_get_keyslot.
luks2: Use more intuitive keyslot key instead of index when naming keyslot.
luks2:grub_cryptodisk_t->total_lengthis 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 ingrub_cryptodisk_endecrypt.
cryptodisk: Renametotal_lengthfield ingrub_cryptodisk_ttototal_sectors.
cryptodisk: Rename offset ingrub_cryptodisk_ttooffset_sectors.
luks2: Rename source disk variabled named 'disk' to 'source' as inluks.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
v4 cover Cryptodisk fixes for v2.06 redux
In reply to Glenn WashburnHere 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
PATCH v4 1/15 cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -67,7 +67,8 @@ struct grub_cryptodiskchar *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
Re: [PATCH v4 01/15] cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
Quoted 4 lines · Glenn Washburn
This creates an alignment with
grub_disk_tnaming of the same field and is more intuitive as to how it should be used.Signed-off-by Glenn Washburn <[email protected]>
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@@ -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@@ -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@@ -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@@ -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@@ -67,7 +67,8 @@ struct grub_cryptodiskchar *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
Daniel Kiper
Reviewed-byRe: [PATCH v4 01/15] cryptodisk: Rename total_length field in grub_cryptodisk_t to total_sectors.
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn
This creates an alignment with
grub_disk_tnaming of the same field and is more intuitive as to how it should be used.Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Patrick Steinhardt <[email protected]>
Daniel
PATCH v4 2/15 cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
In reply to Glenn WashburnThis 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -66,7 +66,12 @@ struct grub_cryptodiskstruct 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
Re: [PATCH v4 02/15] cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
Quoted 4 lines · Glenn Washburn
This makes it clear that the offset represents sectors, not bytes, in order to improve readability.
Signed-off-by Glenn Washburn <[email protected]>
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@@ -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@@ -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@@ -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@@ -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@@ -66,7 +66,12 @@ struct grub_cryptodiskstruct 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
Daniel Kiper
Reviewed-byRe: [PATCH v4 02/15] cryptodisk: Rename offset in grub_cryptodisk_t to offset_sectors.
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn
This makes it clear that the offset represents sectors, not bytes, in order to improve readability.
Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Patrick Steinhardt <[email protected]>
Daniel
PATCH v4 3/15 luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.
In reply to Glenn WashburnThis 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
@@ -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
Re: [PATCH v4 03/15] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.
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.
Signed-off-by Glenn Washburn <[email protected]>
Quoted 85 lines
--- grub-core/disk/luks2.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)grub-core/disk/luks2.c +11
-11@@ -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
Daniel Kiper
Reviewed-byRe: [PATCH v4 03/15] luks2: Rename source disk variabled named 'disk' to 'source' as in luks.c.
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.
Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Patrick Steinhardt <[email protected]>
Daniel
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
@@ -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
Re: [PATCH v4 04/15] types: Define GRUB_CHAR_BIT based on compiler macro instead of using literal.
Quoted 11 lines · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>--- include/grub/types.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)include/grub/types.h +0
-0@@ -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
Daniel Kiper
Reviewed-byRe: [PATCH v4 04/15] types: Define GRUB_CHAR_BIT based on compiler macro instead of using literal.
Quoted 14 lines · Patrick Steinhardt
Quoted 13 lines · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>--- include/grub/types.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)include/grub/types.h +0
-0@@ -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
PATCH v4 5/15 luks2: Use correct index variable when looping in luks2_get_keyslot.
In reply to Glenn WashburnThe 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
@@ -275,10 +275,10 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sreturn 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_sreturn 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
Re: [PATCH v4 05/15] luks2: Use correct index variable when looping in luks2_get_keyslot.
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.
Signed-off-by Glenn Washburn <[email protected]>
Quoted 38 lines
--- grub-core/disk/luks2.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)grub-core/disk/luks2.c +4
-4@@ -275,10 +275,10 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sreturn 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_sreturn 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
Daniel Kiper
Reviewed-byRe: [PATCH v4 05/15] luks2: Use correct index variable when looping in luks2_get_keyslot.
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.
Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Patrick Steinhardt <[email protected]>
Daniel
PATCH v4 6/15 luks2: Rename variable i to keyslot_idx in luks2_get_keyslot.
In reply to Glenn WashburnVariables 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
@@ -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_sbreak ; }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
Re: [PATCH v4 06/15] luks2: Rename variable i to keyslot_idx in luks2_get_keyslot.
Quoted 4 lines · Glenn Washburn
Variables named i are usually looping variables, so rename to
keyslot_idxto allow for easier reading ofluks2_get_keyslot.Signed-off-by Glenn Washburn <[email protected]>
Quoted 43 lines
--- grub-core/disk/luks2.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)grub-core/disk/luks2.c +4
-4@@ -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_sbreak ; }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
Daniel Kiper
Reviewed-byRe: [PATCH v4 06/15] luks2: Rename variable i to keyslot_idx in luks2_get_keyslot.
Quoted 7 lines · Patrick Steinhardt
Quoted 4 lines · Glenn Washburn
Variables named i are usually looping variables, so rename to
keyslot_idxto allow for easier reading ofluks2_get_keyslot.Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Patrick Steinhardt <[email protected]>
Daniel
PATCH v4 7/15 luks2: Rename index variable j to i.
In reply to Glenn WashburnLooping 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
@@ -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_sif (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
Re: [PATCH v4 07/15] luks2: Rename index variable j to i.
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.
Signed-off-by Glenn Washburn <[email protected]>
Quoted 64 lines
--- grub-core/disk/luks2.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)grub-core/disk/luks2.c +9
-9@@ -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_sif (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
Daniel Kiper
Reviewed-byRe: [PATCH v4 07/15] luks2: Rename index variable j to i.
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.
Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Patrick Steinhardt <[email protected]>
Daniel
PATCH v4 8/15 luks2: Split idx into three variables: keyslot_key, digest_key, segment_key.
In reply to Glenn WashburnIntroduce 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
@@ -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_sfor (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_sfor (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
Re: [PATCH v4 08/15] luks2: Split idx into three variables: keyslot_key, digest_key, segment_key.
Quoted 30 lines · Glenn Washburn
Introduce new variables
keyslot_key,digest_key, andsegment_keywhich 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.Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)grub-core/disk/luks2.c +2
-2@@ -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_sfor (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
-2break ; }if (i == size)@@ -293,12 +294,12 @@ luks2_get_keyslot (grub_luks2_keyslot_t *k, grub_luks2_digest_t *d, grub_luks2_sfor (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
Re: [PATCH v4 08/15] luks2: Split idx into three variables: keyslot_key, digest_key, segment_key.
Quoted 39 lines · Patrick Steinhardt
Quoted 38 lines · Glenn Washburn
Introduce new variables
keyslot_key,digest_key, andsegment_keywhich 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.Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)grub-core/disk/luks2.c +3
-2@@ -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_sfor (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
Re: [PATCH v4 08/15] luks2: Split idx into three variables: keyslot_key, digest_key, segment_key.
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, andsegment_keywhich 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.Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)grub-core/disk/luks2.c +2
-2@@ -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 parsekeyslot %"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 notparse 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_keychange should be dropped from this patch.Daniel
Ok, I'll add it to the following, 09, patch in this series.
Glenn
PATCH v4 9/15 luks2: Improve error messages in luks2_get_keyslot.
In reply to Glenn WashburnError 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
@@ -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
Re: [PATCH v4 09/15] luks2: Improve error messages in luks2_get_keyslot.
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.
Signed-off-by Glenn Washburn <[email protected]>
Quoted 52 lines
--- grub-core/disk/luks2.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)grub-core/disk/luks2.c +5
-5@@ -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
Daniel Kiper
Reviewed-byRe: [PATCH v4 09/15] luks2: Improve error messages in luks2_get_keyslot.
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.
Signed-off-by Glenn Washburn <[email protected]>Reviewed-by Patrick Steinhardt <[email protected]>
Daniel
PATCH v4 10/15 luks2: Use more intuitive keyslot key instead of index when naming keyslot.
In reply to Glenn WashburnUse 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
@@ -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
Re: [PATCH v4 10/15] luks2: Use more intuitive keyslot key instead of index when naming keyslot.
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.
Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-)grub-core/disk/luks2.c +5
-2@@ -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
-8break ; }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
Re: [PATCH v4 10/15] luks2: Use more intuitive keyslot key instead of index when naming keyslot.
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.
Signed-off-by Glenn Washburn <[email protected]>--- grub-core/disk/luks2.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-)grub-core/disk/luks2.c +5
-2@@ -65,6 +65,7 @@ typedef struct grub_luks2_headergrub_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_keyslotgrub_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_segmentgrub_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 parsekeyslot index %"PRIuGRUB_SIZE,
keyslot_idx); @@ -281,11 +284,12 @@luks2_get_keyslot(grub_luks2_keyslot_t*k,grub_luks2_digest_t*d,grub_luks2_sluks2_parse_digest(d, &digest)) returngrub_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_keyandsegment_keyvariables.
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
-1break ; }if (i == size) - return grub_error (GRUB_ERR_FILE_NOT_FOUND, "No digest forkeyslot \"%"PRIuGRUB_UINT64_T"\"",
keyslot_key);
+ returngrub_error(GRUB_ERR_FILE_NOT_FOUND, "No digest forkeyslot \"%"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_tsource, (constgrub_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
PATCH v4 11/15 cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
In reply to Glenn WashburnThis 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
@@ -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
@@ -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
Re: [PATCH v4 11/15] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
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.Signed-off-by Glenn Washburn <[email protected]>--- 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@@ -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@@ -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
Re: [PATCH v4 11/15] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt.
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.Signed-off-by Glenn Washburn <[email protected]>--- 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.cindex473c93976..31b73c535100644grub-core/disk/cryptodisk.c +3
-2@@ -284,22 +284,23 @@ grub_cryptodisk_endecrypt (structgrub_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
-1break ;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
-0dev->cipher->cipher->blocksize);if (err)include/grub/types.h +2
-0@@ -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_tname[((size) + sizeof (grub_properly_aligned_t) - 1) / sizeof (grub_properly_aligned_t)] -- 2.27.0
PATCH v4 12/15 luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
In reply to Glenn WashburnThe 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
@@ -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
Re: [PATCH v4 12/15] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
Quoted 10 lines · Glenn Washburn
The
total_lengthfield 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_sizereturns 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.
Signed-off-by Glenn Washburn <[email protected]>
Quoted 33 lines
--- grub-core/disk/luks2.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)grub-core/disk/luks2.c +4
-3@@ -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
Re: [PATCH v4 12/15] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
In reply to Glenn WashburnQuoted 1 line · Glenn Washburn
The
total_lengthfield 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_sizereturns 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.
Signed-off-by Glenn Washburn <[email protected]>
Otherwise patch LGTM.
Daniel
Re: [PATCH v4 12/15] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors
Quoted 14 lines · On Tue, 17 Nov 2020 15:06:08 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 2 lines · Glenn Washburn
The
total_lengthfield 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_sizereturns 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.
Signed-off-by Glenn Washburn <[email protected]>Otherwise patch LGTM.
Daniel
Damn, I thought I changed that. Good catch.
Glenn
PATCH v4 13/15 cryptodisk: Properly handle non-512 byte sized sectors.
In reply to Glenn WashburnBy 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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCH v4 13/15] cryptodisk: Properly handle non-512 byte sized sectors.
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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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@@ -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@@ -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@@ -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
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
Re: [PATCH v4 13/15] cryptodisk: Properly handle non-512 byte sized sectors.
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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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.cindex31b73c535..61f8e57f4100644grub-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, caseGRUB_CRYPTODISK_MODE_CBC:grub-core/disk/cryptodisk.c +0
-0if (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
-0return err;break ;@@ -323,10 +334,10 @@ grub_cryptodisk_endecrypt (struct
grub_cryptodisk*dev, caseGRUB_CRYPTODISK_MODE_PCBC:grub-core/disk/cryptodisk.c +0
-0if (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
-2return 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
-0return err;lrw_xor (&sec, dev, data + i);@@ -382,10 +393,10 @@ grub_cryptodisk_endecrypt (struct
grub_cryptodisk*dev, caseGRUB_CRYPTODISK_MODE_ECB:grub-core/disk/cryptodisk.c +0
-0if (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
-0return err;break ;@@ -400,9 +411,9 @@ grub_cryptodisk_endecrypt (struct
grub_cryptodisk*dev,gcry_err_code_t
grub_cryptodisk_decrypt(structgrub_cryptodisk*dev,grub-core/disk/cryptodisk.c +1
-1grub_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_tgrub-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@@ -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@@ -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@@ -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_SIZE9Sorry 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_tdev,
gcry_err_code_t
grub_cryptodisk_decrypt(structgrub_cryptodisk*dev,include/grub/cryptodisk.h +1
-1grub_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_tnewdev, const char *name,
grub_disk_tsource);Signature · 1 line
2.27.0
Re: [PATCH v4 13/15] cryptodisk: Properly handle non-512 byte sized sectors.
Quoted 285 lines · Glenn Washburn
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_sectorsoption 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.
Signed-off-by Glenn Washburn <[email protected]>--- 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.cindex31b73c535..61f8e57f4100644grub-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, caseGRUB_CRYPTODISK_MODE_CBC:grub-core/disk/cryptodisk.c +0
-0if (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
-0return err;break ;@@ -323,10 +334,10 @@ grub_cryptodisk_endecrypt (struct
grub_cryptodisk*dev, caseGRUB_CRYPTODISK_MODE_PCBC:grub-core/disk/cryptodisk.c +0
-0if (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
-2return 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
-0return err;lrw_xor (&sec, dev, data + i);@@ -382,10 +393,10 @@ grub_cryptodisk_endecrypt (struct
grub_cryptodisk*dev, caseGRUB_CRYPTODISK_MODE_ECB:grub-core/disk/cryptodisk.c +0
-0if (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
-0return err;break ;@@ -400,9 +411,9 @@ grub_cryptodisk_endecrypt (struct
grub_cryptodisk*dev,gcry_err_code_t
grub_cryptodisk_decrypt(structgrub_cryptodisk*dev,grub-core/disk/cryptodisk.c +1
-1grub_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_tgrub-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@@ -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@@ -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@@ -48,6 +48,12 @@ typedef enum#define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71Quoted 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_SIZE9Sorry 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 8 lines
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
I mean we could split it up into two constants, GRUB_LUKS1_LOG_SECTOR_SIZE and GRUB_LUKS2_KEYSLOT_LOG_SECTOR_SIZE. Not sure if it's worth it, though.
Patrick
Quoted 19 lines
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_tdev,
gcry_err_code_t
grub_cryptodisk_decrypt(structgrub_cryptodisk*dev,include/grub/cryptodisk.h +1
-1grub_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_tnewdev, const char *name,
grub_disk_tsource);Signature · 1 line
2.27.0
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
@@ -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
@@ -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
Re: [PATCH v4 14/15] luks2: Better error handling when setting up the cryptodisk.
Quoted 1 line · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>
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@@ -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@@ -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
Re: [PATCH v4 14/15] luks2: Better error handling when setting up the cryptodisk.
Quoted 5 lines · On Sun, 15 Nov 2020 11:13:04 +0100 Patrick Steinhardt <[email protected]> wrote:
Quoted 1 line · Glenn Washburn
Signed-off-by Glenn Washburn <[email protected]>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@@ -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 asgrub_strtoulldoesn't unset it if no error occurred. We'd thus have to first setgrub_errno = GRUB_ERR_NONEbefore the call togrub_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. Theend 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 thesource"
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 --gita/include/grub/misc.hb/include/grub/misc.hindexb7ca6dd58..ec25131ba100644include/grub/misc.h +2
-0@@ -481,5 +481,7 @@ void EXPORT_FUNC(grub_real_boot_time) (constchar *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