From patchwork Mon Mar 21 13:26:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 8633041 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 2601DC0553 for ; Mon, 21 Mar 2016 13:28:54 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 45B2A20328 for ; Mon, 21 Mar 2016 13:28:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4C99420263 for ; Mon, 21 Mar 2016 13:28:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754886AbcCUN2u (ORCPT ); Mon, 21 Mar 2016 09:28:50 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:34954 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755468AbcCUN0y (ORCPT ); Mon, 21 Mar 2016 09:26:54 -0400 Received: by mail-wm0-f65.google.com with SMTP id r129so10000009wmr.2; Mon, 21 Mar 2016 06:26:53 -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=VnbvvP9vGVUssUJEJuycxolyIOyJrw3tu8uX6Eblj4s=; b=i+2+FmZtRKBmKKiJdd6iQbFgt6lybQxm0Ns0E/vesoxaXOAM2ZsZmNLUOxJpWjmlTj 3PMonQz4/I2Em6A6tu+11oaLge2Ly2YwmV1UgewalBl3HwX9VttgXGl8Ty5cE3heh1mD 6OFa8oJq8nkkge5XiUYUgAciqTGjSg+J9vnJz17VUmyETRpDManpRrDP76OrS8mtqhfH eUTLAhKsVn0yylPK1qtpmW9go/Y9nxGmbLc9uHcio0plS84ocM1LcPQTItQrxRHYdvBP Au/FcOBVua5lGZ/ezNtpHjJiEHRazDP34pveYU+NCjK4v5sRhMa5VH/X0NaKfBd0bBz6 CwsQ== 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=VnbvvP9vGVUssUJEJuycxolyIOyJrw3tu8uX6Eblj4s=; b=eeVDrDx8AHlnTFkH30VAzzUN98dyD4F87y4Pz4nKx6LCwkHqlY2vDr/z0BoYPhX4BG 0q+rIZB1hYghH55NdMd/YABAC6KyqPy8l0KhkqYOy1vrDDQ4xOoOV/ZPCNNwBrRJBnEt gFMHtamR5qoBArCRTJEZUcBzhG/mi7IrJT9WaCrgC++KEavgmOhO0EgqWEbcxKGmm+ly n7dad0VjZrU3AftDW1eLPHcsaZ5qA+vXuAN9/lns+77gXbtx3evk9LMb9ZSAP03soH/M pceAeOPjac65IrrZ6OEZcfaqrgMiTrhbpFBExV6jWyg1ERzCWV3AUTWOxsrxTMSCJKUd j5SA== X-Gm-Message-State: AD7BkJIuAfKQuwQtrd0y8pxes/sTnu7MQs8XQDF6w2/voSjfeJLB6KV6+Kd/azhXvOeYPA== X-Received: by 10.28.73.66 with SMTP id w63mr14476647wma.53.1458566812767; Mon, 21 Mar 2016 06:26:52 -0700 (PDT) Received: from localhost (x55b18fc0.dyn.telefonica.de. [85.177.143.192]) by smtp.gmail.com with ESMTPSA id t82sm6086929wmt.17.2016.03.21.06.26.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2016 06:26:52 -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 RESEND v2 13/14] lib/mpi: mpi_read_raw_from_sgl(): sanitize meaning of indices Date: Mon, 21 Mar 2016 14:26:14 +0100 Message-Id: <1458566775-5239-14-git-send-email-nicstange@gmail.com> X-Mailer: git-send-email 2.7.3 In-Reply-To: <1458566775-5239-1-git-send-email-nicstange@gmail.com> References: <1458566775-5239-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, RP_MATCHES_RCVD, T_DKIM_INVALID, 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 5d02efe..3f114d2 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;