From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1kh4Kp-0008Ia-DX for mharc-grub-devel@gnu.org; Mon, 23 Nov 2020 00:24:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40570) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kh4Kg-0008Eh-2P for grub-devel@gnu.org; Mon, 23 Nov 2020 00:24:11 -0500 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]:52730) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kh4Ke-0006Ue-ET for grub-devel@gnu.org; Mon, 23 Nov 2020 00:24:09 -0500 Received: by mail-wm1-x343.google.com with SMTP id 10so16732148wml.2 for ; Sun, 22 Nov 2020 21:24:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficientek-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XNsifPGyTzrUuT4FQW8LTKQ6kvsx8g0EllJFfWMaQZw=; b=cBlRwxZA0exa6cCKLNRWNitJnxvsN7AGqVuQ6mCpwnikG51XvDukwmRwKzgPitMo7F x9jk1C4M56jeIS9ks7Bk05b+eVR/SOthpUjIj/rntkLjYGuozz5NtT2+r+uPJkI0fmt9 AAY8BxF93cHVlRBktBWym2JLFalSBEi8D5jc6cEosY4LcD0ZzdysLwu4juVfU+WZ7Z71 WxgCHsC0XI7/QYfwrQlP/v33K0AHq5ZjJ+AXcYDAzCl5ud+AT2E372hGl2OyhnbUeEd2 YJ5i4eLxF8QAmAKJs9reoUw2FD4dMAvLi0mUoqbRA0hK6dxHBZU4jvEQTHABqYcfqfP4 yRxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XNsifPGyTzrUuT4FQW8LTKQ6kvsx8g0EllJFfWMaQZw=; b=R6kWQWCsobaUmcXLqRCjCmveNe6Dfnqrgdqlx75Sa7p/RNPlAhJPZovrieT3rUwQEb G2h4qn9W4beHg5WafmbChFmtUHOGH+5sQMP28IXCCZqSNSJ/kNmtut9fO7MkNpBU6SiJ raJV2JgBhFSM3CdqpmZSH+gnGXyLw3tf2pLsBsPNXp0EquSXL4nE/QPBBvpgehjhUF8e hqkv8EBQAmcCCTcbBlwbp4NNa8ox8yXsBYvX2htSGU1LW8HoZiFQ+KGRFdNZJLPdOhgU FSJXtlI7j8R7ObKaxvAJOwVzdGasRmAxlLUZTJ9JbvpQIsrmVCX+b+7++eZYI31TCRlR DPWA== X-Gm-Message-State: AOAM531UbzFDQUz542/cDQociHYZNX18NyPxaaP1V52azU2zpx7hdKAO 976g6gc2JiZ6ygo3mcT/yLEyyqyiLNq1OQ== X-Google-Smtp-Source: ABdhPJzz6JFmqtEYJIWcZbpExrdsQ0istzFznN8kXfPDiOxmfOOwxsVcrcOx47uoZWtoX8MD99uUww== X-Received: by 2002:a1c:490b:: with SMTP id w11mr21757699wma.101.1606109046867; Sun, 22 Nov 2020 21:24:06 -0800 (PST) Received: from localhost.localdomain ([136.49.211.192]) by smtp.gmail.com with ESMTPSA id f16sm11631780wmh.7.2020.11.22.21.24.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 Nov 2020 21:24:06 -0800 (PST) From: Glenn Washburn To: grub-devel@gnu.org Cc: Patrick Steinhardt , Daniel Kiper , Glenn Washburn Subject: [PATCH v5 03/11] cryptodisk: Replace some literals with constants in grub_cryptodisk_endecrypt Date: Sun, 22 Nov 2020 23:23:16 -0600 Message-Id: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2a00:1450:4864:20::343; envelope-from=development@efficientek.com; helo=mail-wm1-x343.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: Mon, 23 Nov 2020 05:24:11 -0000 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 9cd7e2525..b9c7426fc 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) ((typeof (1ULL))((typeof (type))(~0))) +#define GRUB_TYPE_U_MIN(type) 0ULL + typedef grub_uint64_t grub_properly_aligned_t; #define GRUB_PROPERLY_ALIGNED_ARRAY(name, size) grub_properly_aligned_t name[((size) + sizeof (grub_properly_aligned_t) - 1) / sizeof (grub_properly_aligned_t)] -- 2.27.0