From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1kg1yv-0000Me-W4 for mharc-grub-devel@gnu.org; Fri, 20 Nov 2020 03:41:26 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51380) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kg1yu-0000MU-Dd for grub-devel@gnu.org; Fri, 20 Nov 2020 03:41:24 -0500 Received: from mail-lf1-x141.google.com ([2a00:1450:4864:20::141]:40140) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kg1yr-0005JA-Od for grub-devel@gnu.org; Fri, 20 Nov 2020 03:41:23 -0500 Received: by mail-lf1-x141.google.com with SMTP id u19so12315968lfr.7 for ; Fri, 20 Nov 2020 00:41:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficientek-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=pzRVh0bwkyiORq9fHnWP3lHo2XmTEF2dgsNBuuaGAkU=; b=vVEVbL4WzNaQgr1Y01hKyHYLZZJP4c4BVY2YCfZImEu5SiJsSfqO5seRLnYrtCBXZC XhR7+/HIuxjOhNYZSDXKtpNJkllM5+QWlylANj63ffkB5NkACpJcikQcy+zRA7PxYsko 6fFQaRAbAmGdqvT6bdeN9d8Gr+DBdZF7d2MI8DhYgKvzvEbFgOMqozvWnIGXkM69QS5i im3/+Xi+rnqmFZDwSombeQgFamy0bFrt1LiU6ze1iCByR4dQIbUrzVsUs6EfhxyjFvIp U4g4zdFt76LIvmW3us4EBBLUObiVNB2DASHbTKVwAZLH094O8F+L9i8S5JaqRrYlgWMK +1fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=pzRVh0bwkyiORq9fHnWP3lHo2XmTEF2dgsNBuuaGAkU=; b=f5CMxERlvMqUkcymQ0SCSdvcuBiRk1zFaFpiC3BYhwmIxuOsKgxgBdfAQiy2FsdwOV 6f1HTcq/SbzJ7rmWN5ItfQSe/kc+nQMhiMt54Zso8gQhYtH/xLiBWN7ZgavKz72dFskE 8G+QT8MluuwPZIS+x4ZSD2CclLYb+F2SszlE/cD7JYm/mmIgtS5dOVmlu3uXf73SHg5/ YAArtd5JfovvIDcsK5VuyQhFBFvVTt1iMBGTPVuK8ReJiQSU9QTlsIDUr4qGAJok8rid 3fUx/KQqiNY6xbXNtiyJMORVtK1gPtMW1bktUx8Uw6lJ3FZ2R3RBNlnKhma/iXKeWept GePA== X-Gm-Message-State: AOAM530TIlTD+d3J8ClrQJ0SJbx6ZsiWvEiwZ3ezuqrfRlT6ziRb5wy7 CnS4TSINwCjO/G7TDqCw0tiSOg== X-Google-Smtp-Source: ABdhPJwERi+mna3s9vbIXw1cfXOsTxaT0oQw5dkI6oud1ADxDU67b71OwKxSy/NQM01Cfn8vfAbSfQ== X-Received: by 2002:a19:6a0c:: with SMTP id u12mr7334734lfu.492.1605861679790; Fri, 20 Nov 2020 00:41:19 -0800 (PST) Received: from crass-HP-ZBook-15-G2 ([136.49.211.192]) by smtp.gmail.com with ESMTPSA id s5sm267335lfd.58.2020.11.20.00.41.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Nov 2020 00:41:19 -0800 (PST) Date: Fri, 20 Nov 2020 02:41:11 -0600 From: Glenn Washburn To: Patrick Steinhardt Cc: grub-devel@gnu.org, Daniel Kiper Subject: Re: [PATCH v4 11/15] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt. Message-ID: <20201120024111.52f2bd69@crass-HP-ZBook-15-G2> In-Reply-To: References: <87bafa5d9ef66472506a7a92b6e0a1cd1421c19e.1604723348.git.development@efficientek.com> Reply-To: development@efficientek.com X-Mailer: Claws Mail 3.17.4 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=2a00:1450:4864:20::141; envelope-from=development@efficientek.com; helo=mail-lf1-x141.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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, 20 Nov 2020 08:41:24 -0000 On Sun, 15 Nov 2020 10:59:33 +0100 Patrick Steinhardt wrote: > On Fri, Nov 06, 2020 at 10:44:31PM -0600, Glenn Washburn wrote: > > 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 > > --- > > grub-core/disk/cryptodisk.c | 13 +++++++------ > > include/grub/types.h | 5 +++++ > > 2 files changed, 12 insertions(+), 6 deletions(-) > > > > diff --git a/grub-core/disk/cryptodisk.c > > b/grub-core/disk/cryptodisk.c index 473c93976..31b73c535 100644 > > --- a/grub-core/disk/cryptodisk.c > > +++ b/grub-core/disk/cryptodisk.c > > @@ -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) > > diff --git a/include/grub/types.h b/include/grub/types.h > > index 495c86bb1..b57acc3b1 100644 > > --- a/include/grub/types.h > > +++ b/include/grub/types.h > > @@ -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) > > 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 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 > > > +#define GRUB_TYPE_U_MIN(type) 0ULL > > + > > typedef grub_uint64_t grub_properly_aligned_t; > > > > #define GRUB_PROPERLY_ALIGNED_ARRAY(name, size) > > grub_properly_aligned_t name[((size) + sizeof > > (grub_properly_aligned_t) - 1) / sizeof (grub_properly_aligned_t)] > > -- 2.27.0 > >