From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1kmlkj-0004qr-Mj for mharc-grub-devel@gnu.org; Tue, 08 Dec 2020 17:46:37 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:36798) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kmlkf-0004iM-Kr for grub-devel@gnu.org; Tue, 08 Dec 2020 17:46:33 -0500 Received: from mail-ot1-x342.google.com ([2607:f8b0:4864:20::342]:41258) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kmlkX-0000kx-M0 for grub-devel@gnu.org; Tue, 08 Dec 2020 17:46:33 -0500 Received: by mail-ot1-x342.google.com with SMTP id x13so338136oto.8 for ; Tue, 08 Dec 2020 14:46:23 -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=lHLvmguKV0ApvBMqK8FY78byGRMUzwis4tbonCQli6c=; b=csmtmKkxETIdHVYgpCmg8uUgkpE/mS4KZYllzwRojKE3Tp2VJlpWDO1stRSOvDhxQB Td468iRJu68XnNT7YPxn4vYWiHt71uIAcNsGUXnz/82/Qwes27E0/3tYBoswCU74SjLt sF04MvFlWJNccgO29u3t4hHH6wxtcvnU5AkdRIGGqTSblO8lD2kyCGZZ1bQaXEsWoLD2 w8wKQMFZhe/Ir9IPFzbmKlcdeWOtYM72q2Vypvrt5V8beKQRiON2F+JclxZCqIKwj71c mNiNziCeaaMmy3Q+WgsOXR+DgjlAOiKQ0Byu3gGurVHa0Hq7rM8EQdr7tNBoacN7Fa5G PPeg== 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=lHLvmguKV0ApvBMqK8FY78byGRMUzwis4tbonCQli6c=; b=l6G8YcMHaIu/0Qrh7nzRua4xDA+jJdLjV0ma1Klv42s+sTuua2EXYR/tjlhH9lZNCd SBfHGfrm2J16/xrwauQaeYjTu1OEQMGv8uMsZAbr1j0Twz7XvlY0AQw4LcSC4FbfRmHt 3lTsuTWuu6Te8kK3c3oifdqvG6Mt7+Qb638mqoZhoHta2+ZUx8nJ8qeAiEWioQEASjZT l/QN2vt9AhABMDYFV7BF/2iXH3Uf2FY3kYBy4c5vtNoIh0dv8oEp738e6gLt2y36QjG+ qGjWj7eRoqgZ83oJONSK6D0HMYr6KK0F/JfzvyZdQGpb4fkdZ3BpgpF/nyuxTS1eCF8P SExA== X-Gm-Message-State: AOAM530kDW6LdXN2G8aldePGu8UN/ZfIi3p+wx5eYtj4wBDiodKawMxv r9wZCQVAf4c/RM/DolvylrE4OK5G+1lxFA== X-Google-Smtp-Source: ABdhPJwsE8x8V+CHy/T/ITw661nbegmYWpbh71QovDj6qwp18IZEHc6Pz+8HwB91NmEox8Ew3OB9PQ== X-Received: by 2002:a05:6830:1d8f:: with SMTP id y15mr280607oti.332.1607467582936; Tue, 08 Dec 2020 14:46:22 -0800 (PST) Received: from crass-HP-ZBook-15-G2.attlocal.net ([136.49.211.192]) by smtp.gmail.com with ESMTPSA id v8sm45538otp.10.2020.12.08.14.46.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Dec 2020 14:46:22 -0800 (PST) From: Glenn Washburn To: grub-devel@gnu.org Cc: Patrick Steinhardt , Daniel Kiper , Glenn Washburn Subject: [PATCH v8 11/18] cryptodisk: Add macros GRUB_TYPE_U_MAX/MIN(type) to replace literals Date: Tue, 8 Dec 2020 16:45:42 -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=2607:f8b0:4864:20::342; envelope-from=development@efficientek.com; helo=mail-ot1-x342.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: Tue, 08 Dec 2020 22:46:34 -0000 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 | 8 ++++---- include/grub/types.h | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c index 0e955a020..5aa0c4720 100644 --- a/grub-core/disk/cryptodisk.c +++ b/grub-core/disk/cryptodisk.c @@ -284,23 +284,23 @@ grub_cryptodisk_endecrypt (struct grub_cryptodisk *dev, iv[1] = grub_cpu_to_le32 (sector >> GRUB_TYPE_BITS (iv[0])); /* 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 >> (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 >> GRUB_TYPE_BITS (iv[0])); - iv[sz - 1] = grub_cpu_to_be32 (num & 0xFFFFFFFF); + 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 9989e3a16..b36b26a79 100644 --- a/include/grub/types.h +++ b/include/grub/types.h @@ -161,6 +161,13 @@ typedef grub_int32_t grub_ssize_t; #endif # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1) +/* + * Cast to unsigned long long so that the "return value" is always a consistent + * type and to ensure an unsigned value regardless of type parameter. + */ +#define GRUB_TYPE_U_MAX(type) ((unsigned long long)((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