From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1kQp2m-000733-No for mharc-grub-devel@gnu.org; Fri, 09 Oct 2020 05:50:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36828) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kQp2k-00072j-Gq for grub-devel@gnu.org; Fri, 09 Oct 2020 05:50:30 -0400 Received: from out2-smtp.messagingengine.com ([66.111.4.26]:47379) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kQp2i-0006cg-BB for grub-devel@gnu.org; Fri, 09 Oct 2020 05:50:30 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id CEBE05C0144; Fri, 9 Oct 2020 05:50:27 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Fri, 09 Oct 2020 05:50:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm2; bh=rAtc3gWvwaD3K7oXV4kp5Vp5QQ8 z+InYo6vlGVVmkh0=; b=qxAVePrlxbHfHes2nBAUdEG7a4vAFM5xAXdacK+vclR 1qv2SVPbAKGtCPbvVdICStA0ffTzVaq2fIpH8cV9f/cDh5v9AVRNKWGge64aH1Dp lupM9XAARTMVKTetxnJ7tiAF6uKYYE8t8Tu9TEBYzSlmDmFMSOdj6Y7JA5EsjZ7O PbMPrlBDuzoEH4JJ9EHUzoTW6pCRQ0mvQob7rNIqQTOFKnBFNv0MwRvBJK1qgyW/ fN7thk0Sn49CCcpZa/hdPp/iZ4peEiwPlBscFZwCSbooH0/M/5reLoMRUlsQSreP kbDXgWE5VMAgQ9wDbaq71vqe8D9tcJhIBeMxQAwQr5g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=rAtc3g WvwaD3K7oXV4kp5Vp5QQ8z+InYo6vlGVVmkh0=; b=OTG1jnkGgrVYL4Y5/+A12c Ol7/hlMqCrlOqHc0tkh4arZ0mJvHM28GcO8BTvu7VeX+1L1n9+1cxobZu3hoDrQI EQWEtUDLayoNmtphos+vhFB3wN48tZ0x0d0N4Bf4vf9C7IDlpQkN3n9mXdBRJT8T MZrUxWi6az1ihru5NiM88nsYDC/y9xcngUZaAfe+52fGQazO48vU+qKxkoCVNUuq oBms17RCO7c4LzXLXNqLZo2v95zXm4EjEYsZZNl1j4rn1d5fCmx05TQYaJQtnTA5 bd64pET264gG3BGe8laHWzOfIOvj3QwjcBIc90t8VJjNKB63H716FYxLBAwRZPMg == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrhedugddvudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhrihgt khcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvghrnh epheeghfdtfeeuffehkefgffduleffjedthfdvjeektdfhhedvlefgtefgvdettdfhnecu kfhppeejkedrheegrdefvddrudefgeenucevlhhushhtvghrufhiiigvpedunecurfgrrh grmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm X-ME-Proxy: Received: from vm-mail.pks.im (dynamic-078-054-032-134.78.54.pool.telefonica.de [78.54.32.134]) by mail.messagingengine.com (Postfix) with ESMTPA id D4EF93064674; Fri, 9 Oct 2020 05:50:26 -0400 (EDT) Received: from localhost (tanuki [10.192.0.23]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id 8797f595 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Fri, 9 Oct 2020 09:50:25 +0000 (UTC) Date: Fri, 9 Oct 2020 11:50:29 +0200 From: Patrick Steinhardt To: Glenn Washburn Cc: grub-devel@gnu.org, Daniel Kiper Subject: Re: [PATCH v2 06/10] cryptodisk: Properly handle non-512 byte sized sectors. Message-ID: <20201009095029.GD2088@tanuki> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="T7mxYSe680VjQnyC" Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=66.111.4.26; envelope-from=ps@pks.im; helo=out2-smtp.messagingengine.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/09 05:22:33 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 09 Oct 2020 09:50:30 -0000 --T7mxYSe680VjQnyC Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Oct 03, 2020 at 05:55:30PM -0500, Glenn Washburn wrote: > 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. >=20 > 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 --ty= pe > plain). However, support for this has not been included as grub does not > support plain devices right now. >=20 > One gotcha here is that the encrypted split keys are encrypted with a har= d- > coded 512-byte sector size. So even if your data is encrypted with 4K sec= tor > 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. >=20 > Signed-off-by: Glenn Washburn > --- > 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(-) >=20 > diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c > index a3d672f68..623f0f396 100644 > --- a/grub-core/disk/cryptodisk.c > +++ b/grub-core/disk/cryptodisk.c > @@ -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 *de= v, > return (do_encrypt ? grub_crypto_ecb_encrypt (dev->cipher, data, dat= a, len) > : grub_crypto_ecb_decrypt (dev->cipher, data, data, len)); > =20 > - for (i =3D 0; i < len; i +=3D (1U << dev->log_sector_size)) > + for (i =3D 0; i < len; i +=3D (1U << log_sector_size)) > { > grub_size_t sz =3D ((dev->cipher->cipher->blocksize > + sizeof (grub_uint32_t) - 1) > @@ -270,7 +271,7 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *de= v, > if (!ctx) > return GPG_ERR_OUT_OF_MEMORY; > =20 > - tmp =3D grub_cpu_to_le64 (sector << dev->log_sector_size); > + tmp =3D 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] =3D grub_cpu_to_le32 (sector >> 32); > - /* FALLTHROUGH */ > case GRUB_CRYPTODISK_MODE_IV_PLAIN: > - iv[0] =3D 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 =3D (grub_uint64_t *)iv; > + *iv64 =3D grub_cpu_to_le64 (sector << (log_sector_size > + - GRUB_CRYPTODISK_IV_LOG_SIZE)); > + if (dev->mode_iv =3D=3D GRUB_CRYPTODISK_MODE_IV_PLAIN) > + iv[1] =3D 0; I may be misreading this, but aren't we zeroing out 64 bits here instead of 32 bits as the comment states? Patrick > + } > break; > case GRUB_CRYPTODISK_MODE_IV_BYTECOUNT64: > - iv[1] =3D grub_cpu_to_le32 (sector >> (32 - dev->log_sector_size)); > - iv[0] =3D grub_cpu_to_le32 ((sector << dev->log_sector_size) > + iv[1] =3D grub_cpu_to_le32 (sector >> (32 - log_sector_size)); > + iv[0] =3D 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 =3D grub_crypto_cbc_encrypt (dev->cipher, data + i, data + i, > - (1U << dev->log_sector_size), iv); > + (1U << log_sector_size), iv); > else > err =3D 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 =3D grub_crypto_pcbc_encrypt (dev->cipher, data + i, data + i, > - (1U << dev->log_sector_size), iv); > + (1U << log_sector_size), iv); > else > err =3D 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 *de= v, > if (err) > return err; > =20 > - for (j =3D 0; j < (1U << dev->log_sector_size); > + for (j =3D 0; j < (1U << log_sector_size); > j +=3D 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 =3D grub_crypto_ecb_encrypt (dev->cipher, data + i,=20 > data + i, > - (1U << dev->log_sector_size)); > + (1U << log_sector_size)); > else > err =3D grub_crypto_ecb_decrypt (dev->cipher, data + i,=20 > 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 =3D grub_crypto_ecb_encrypt (dev->cipher, data + i, data + i, > - (1U << dev->log_sector_size)); > + (1U << log_sector_size)); > else > err =3D 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 *de= v, > 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_s= ize, 0); > } > =20 > grub_err_t > @@ -766,7 +776,7 @@ grub_cryptodisk_read (grub_disk_t disk, grub_disk_add= r_t sector, > } > gcry_err =3D 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); > } > =20 > @@ -807,7 +817,7 @@ grub_cryptodisk_write (grub_disk_t disk, grub_disk_ad= dr_t sector, > =20 > gcry_err =3D 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); > diff --git a/grub-core/disk/luks.c b/grub-core/disk/luks.c > index 59702067a..20cc20b9b 100644 > --- a/grub-core/disk/luks.c > +++ b/grub-core/disk/luks.c > @@ -124,7 +124,7 @@ configure_ciphers (grub_disk_t disk, const char *chec= k_uuid, > return NULL; > newdev->offset =3D grub_be_to_cpu32 (header.payloadOffset); > newdev->source_disk =3D NULL; > - newdev->log_sector_size =3D 9; > + newdev->log_sector_size =3D LUKS1_LOG_SECTOR_SIZE; > newdev->total_length =3D grub_disk_get_size (disk) - newdev->offset; > grub_memcpy (newdev->uuid, uuid, sizeof (uuid)); > newdev->modname =3D "luks"; > @@ -247,7 +247,8 @@ luks_recover_key (grub_disk_t source, > return err; > } > =20 > - gcry_err =3D grub_cryptodisk_decrypt (dev, split_key, length, 0); > + gcry_err =3D grub_cryptodisk_decrypt (dev, split_key, length, 0, > + LUKS1_LOG_SECTOR_SIZE); > if (gcry_err) > { > grub_free (split_key); > diff --git a/grub-core/disk/luks2.c b/grub-core/disk/luks2.c > index 9f7d6e12b..b7d3b425a 100644 > --- a/grub-core/disk/luks2.c > +++ b/grub-core/disk/luks2.c > @@ -499,7 +499,12 @@ luks2_decrypt_key (grub_uint8_t *out_key, > goto err; > } > =20 > - gcry_ret =3D 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 =3D grub_cryptodisk_decrypt (crypt, split_key, k->area.size, = 0, > + LUKS1_LOG_SECTOR_SIZE); > if (gcry_ret) > { > ret =3D grub_crypto_gcry_error (gcry_ret); > diff --git a/include/grub/cryptodisk.h b/include/grub/cryptodisk.h > index e1b21e785..006f3ec49 100644 > --- a/include/grub/cryptodisk.h > +++ b/include/grub/cryptodisk.h > @@ -48,6 +48,12 @@ typedef enum > =20 > #define GRUB_CRYPTODISK_MAX_UUID_LENGTH 71 > =20 > +/* 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); > --=20 > 2.27.0 >=20 --T7mxYSe680VjQnyC Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEF9hrgiFbCdvenl/rVbJhu7ckPpQFAl+AMmQACgkQVbJhu7ck PpSI1g/+MPG578muMG0bX+jDMiR102C94dt2z99PpeJ086ZiAqqzUcMqq9mQ6cKB 4DeYrDTNIkJJPGS5NhCOET4PjhCYVMfqpeCx1435HH/sL2nZAP8JHVBnP1ihTlql 7mSAJAsaACpgwgJtlQCb7gP+VM1HBU9a7wImkD/OD4pdYqb03caADCUJOiO12Ns9 ToDT0TsqVo2nvJ5qXecZ2TV3EJj9a6zlhBEpXvdnB9svfsQGHTL2JO2jenLTf04y 0JsXfu2qEVNCqTGXqsKVmeLjwUz5u9V8M6gIKuN/85SUuq1lEdJeUki8mW8ylxcj SxWtBJabB/p6fHOLXg6ROca2oNtMd2zLMvBctlD/RctU6k1husWKh/RgtXK7UHqq t5/lo4ws7Xzk1HWC2ys2Hf0ckWSU5otAgaHIu7nfj48Zdkd/mEryfTSmPUyjobUm lxXjsGX30G714FDj8Vun4fhK9KY665i8R+n1Reg9KuBqBOmln6MbI1y1czJka7nJ lJMtjnHBF7Mv1uYuQN6TIV8WJ1jNI+JnnJkYkdKvlpA92fs/1FOaDn5UqL/XjC/Q sxPdHF95GaYVRWQ5EKzxz1JyEmaNALIjvrS4+zmjYNDtm91Dem+AVuXqwBspHYaS xqpxLOaHKsSPXvpaibWhnF8cLTh9ufK9nq9AqYuo1qGlHHkC3iE= =rp92 -----END PGP SIGNATURE----- --T7mxYSe680VjQnyC--