Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1k1TkJ-0006qR-7k for mharc-grub-devel@gnu.org; Fri, 31 Jul 2020 08:02:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45256) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k1TkH-0006nP-RT for grub-devel@gnu.org; Fri, 31 Jul 2020 08:02:41 -0400 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]:54432) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k1TkF-0005Vk-Uf for grub-devel@gnu.org; Fri, 31 Jul 2020 08:02:41 -0400 Received: by mail-pj1-x102a.google.com with SMTP id mt12so7168958pjb.4 for ; Fri, 31 Jul 2020 05:02:39 -0700 (PDT) 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=L3LYLjr+M5sq3X+a/td43YnU9z2D7kBd4J5gRssaC/E=; b=G8fbhZqDRYKbwB+1bL7wgICM5mJ7816/WAFoVQqSzVfCvRL/vMwJmKmUq6TH4vY+nk Qc85zGYgtFTyKVRXGOpfPMPUKC6U5bAs2EZb20n9AqnPBA/uxBxSiOH4VObXGuasaGJd SClzmFtRRt4WOhF/vx0ij6BOhEDy5AqBkbKZpEAyjW4JNM9qAcz80UEGmRThezjli9Ez yCNaOXuDb8NNrk6lmuAVdLHGVRLjyGZJCW4kkzIb+Hzx2yvWnIfsWLuS2ST/otS8jeZd 4VEAYeZ3yKr8w/8BnPYjf8/XF73mgE2gZJgwjn+lkfyJfdZS2yBlMSJEoZwXfx0IARZz 6qMQ== 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=L3LYLjr+M5sq3X+a/td43YnU9z2D7kBd4J5gRssaC/E=; b=JKTvYZj0yydQJP/ETgJnCTo6ZpSkVJVilBJSJHuvoDeqFdxwVIiywU3X1h20yRmiBX 7iicUqdW9SQWIgY7Ot8dmW2afk3b7v/iNc79stA9IDb6ZZj7VQIN95mzEjJJTSD4nUrJ hnZpK4HFHVeCJecRaUs9mD+j/T6bs+yMhxotPEcIIqvIh871x4sCVz1WvEzf5dKi4YaR trcHcSUvNrrJBvi2vBDEqzxrJ14ahfkVjQ/fq4aw2hV9+xiWpHft44GBwSDT1gdw0VL6 n00jOqt4WnyHhtQX+y/6o2Ca9RXd+LpSVDViCigWa0VvAzo4sM3AJdg/H6KmqsCZtEhv jwsg== X-Gm-Message-State: AOAM532ye2nwxMfjh5LL49CeJ2NNMXyO/dEp+nq3Vjxf/RSuUsOM+9la C888oSfC+7HxB4Obk4BrUBsT5DMHrko= X-Google-Smtp-Source: ABdhPJxVoD0akZ7Ri+dNmwDCT/Hlb+DLe40Fyul6+gX6RXhTk1T3kkci0yBvTStqu2SE9AleGT0fvA== X-Received: by 2002:a17:90a:ee8d:: with SMTP id i13mr3898436pjz.19.1596196958259; Fri, 31 Jul 2020 05:02:38 -0700 (PDT) Received: from localhost.localdomain ([189.216.105.24]) by smtp.gmail.com with ESMTPSA id o4sm9781590pfd.25.2020.07.31.05.02.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Jul 2020 05:02:37 -0700 (PDT) From: Glenn Washburn To: grub-devel@gnu.org Cc: Glenn Washburn , Daniel Kiper , Patrick Steinhardt Subject: [CRYPTO-LUKS v1 07/19] luks2: grub_cryptodisk_t->total_length is the max number of device native sectors. Date: Fri, 31 Jul 2020 07:01:48 -0500 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::102a; envelope-from=development@efficientek.com; helo=mail-pj1-x102a.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. 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, 31 Jul 2020 12:02:42 -0000 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 segment.size is in bytes which need to be converted to cryptodisk sectors. Signed-off-by: Glenn Washburn --- grub-core/disk/luks2.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/grub-core/disk/luks2.c b/grub-core/disk/luks2.c index 9f58780a2..41329f745 100644 --- a/grub-core/disk/luks2.c +++ b/grub-core/disk/luks2.c @@ -416,7 +416,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; @@ -602,9 +602,14 @@ 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 (source) - crypt->offset; + /* + * Convert to cryptodisk sized sectors from source disk sized sectors + * before subtracting the offset, which is in cryptodisk sized sectors. + */ + crypt->total_length = (grub_disk_get_size (source) >> (crypt->log_sector_size - source->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, source, crypt, &keyslot, (const grub_uint8_t *) passphrase, grub_strlen (passphrase)); -- 2.25.1