From patchwork Tue Mar 22 12:18:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 8641951 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id E36B79F3D1 for ; Tue, 22 Mar 2016 12:18:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BF4892037F for ; Tue, 22 Mar 2016 12:18:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D9D542035B for ; Tue, 22 Mar 2016 12:18:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758447AbcCVMSN (ORCPT ); Tue, 22 Mar 2016 08:18:13 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:36195 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756546AbcCVMSM (ORCPT ); Tue, 22 Mar 2016 08:18:12 -0400 Received: by mail-wm0-f65.google.com with SMTP id l68so29186045wml.3; Tue, 22 Mar 2016 05:18:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=nk4noRa/gQ7i0WPfhj1PpDtQiE8mHC2Zt+YxIfPjEow=; b=fVgQa09CIz4ydOV4hu+8SbiRCGewn6jSlglPtegIHmb1BZcKr/ra/PldellwNhhEtu ZC1XJJ2fpA4CCquVngRddS47AEBKfZHMl+BJQiCaZaJaULI9l0Mf2BVI2s/6m1IDcJDF 69t9mj3kjUaLh0fpHt6SDsKN9THOR4PZkRkO69k3Hbi7e1dZidSbQmdhIC62Jtja2oOT gdD6oRcWuohR1dnf1emLtJGitkvk7bZKSYsaXXfKI7Mw7VRcGXo21iOMmyhfp3nPb7DJ fumf0P1YlekpvSuwl2lhkWRmZj1FydpgXd82SPJ6TT5Rn2TIcGzx2nkG3+E7iyCpSOXT 7ETA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=nk4noRa/gQ7i0WPfhj1PpDtQiE8mHC2Zt+YxIfPjEow=; b=OH0JfW8p5rbg8TdijzQUFnIYzENrunnj7KGIvJSnD/lK+A0xXCBRgiIXe01vewD7L3 Tgb7Lsr3SuelBrFlubc7pFdUEFrtnxhzcg5YagLiCEZpO2ce/Xq4ZMt0W4YxlLT/Hv1/ BZpPAPv5qyip+f65lph4ObLl+AV5PXWFiIJVpXjMvqasf4npd0ECmIz9nzqwnVw8pn5x xuDoIzMhhvrMwmewSNxoPDbP6AvuJarAimbLG7bsjqMqVFq9PyZJSgAY/FozDlOKxGYP H8Dszrk18CDbTJNVPSCYEk9Hrktkzty1aYLhh+QX3ZhrKWVjoW0TDX2LUQHMJ9m+eNs2 LqBQ== X-Gm-Message-State: AD7BkJL1ZYFtSsTJ+l5KKTlUechMJynHvIFXQaVzm400EjWq9BA+WZOVDbDLxKSh0QyZpg== X-Received: by 10.28.218.145 with SMTP id r139mr21115003wmg.52.1458649091126; Tue, 22 Mar 2016 05:18:11 -0700 (PDT) Received: from localhost (x55b18fc0.dyn.telefonica.de. [85.177.143.192]) by smtp.gmail.com with ESMTPSA id h7sm17077823wmf.9.2016.03.22.05.18.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Mar 2016 05:18:10 -0700 (PDT) From: Nicolai Stange To: Herbert Xu , "David S. Miller" Cc: Tadeusz Struk , Michal Marek , Andrzej Zaborowski , Stephan Mueller , Arnd Bergmann , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Nicolai Stange Subject: [PATCH v3 13/14] lib/mpi: mpi_read_raw_from_sgl(): sanitize meaning of indices Date: Tue, 22 Mar 2016 13:18:07 +0100 Message-Id: <1458649087-1721-1-git-send-email-nicstange@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1458648768-1469-1-git-send-email-nicstange@gmail.com> References: <1458648768-1469-1-git-send-email-nicstange@gmail.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Within the byte reading loop in mpi_read_raw_sgl(), there are two housekeeping indices used, z and x. At all times, the index z represents the number of output bytes covered by the input SGEs for which processing has completed so far. This includes any leading zero bytes within the most significant limb. The index x changes its meaning after the first outer loop's iteration though: while processing the first input SGE, it represents "number of leading zero bytes in most significant output limb" + "current position within current SGE" For the remaining SGEs OTOH, x corresponds just to "current position within current SGE" After all, it is only the sum of z and x that has any meaning for the output buffer and thus, the "number of leading zero bytes in most significant output limb" part can be moved away from x into z from the beginning, opening up the opportunity for cleaner code. Before the outer loop iterating over the SGEs, don't initialize z with zero, but with the number of leading zero bytes in the most significant output limb. For the inner loop iterating over a single SGE's bytes, get rid of the buf_shift offset to x' bounds and let x run from zero to sg->length - 1. Signed-off-by: Nicolai Stange --- lib/mpi/mpicoder.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index 24a0155..a9f1097 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c @@ -477,19 +477,17 @@ MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes) j = nlimbs - 1; a = 0; - z = 0; - x = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; - x %= BYTES_PER_MPI_LIMB; + z = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; + z %= BYTES_PER_MPI_LIMB; for_each_sg(sgl, sg, ents, i) { const u8 *buffer = sg_virt(sg) + lzeros; int len = sg->length - lzeros; - int buf_shift = x; if (sg_is_last(sg) && (len % BYTES_PER_MPI_LIMB)) len += BYTES_PER_MPI_LIMB - (len % BYTES_PER_MPI_LIMB); - for (; x < len + buf_shift; x++) { + for (x = 0; x < len; x++) { a <<= 8; a |= *buffer++; if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) { @@ -498,7 +496,6 @@ MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes) } } z += x; - x = 0; lzeros = 0; } return val;