From patchwork Tue Mar 22 12:17:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 8641931 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 5362AC0553 for ; Tue, 22 Mar 2016 12:17:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 648BE20160 for ; Tue, 22 Mar 2016 12:17:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8DE4E2015A for ; Tue, 22 Mar 2016 12:17:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756167AbcCVMRd (ORCPT ); Tue, 22 Mar 2016 08:17:33 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:36643 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755926AbcCVMRc (ORCPT ); Tue, 22 Mar 2016 08:17:32 -0400 Received: by mail-wm0-f65.google.com with SMTP id l68so29179676wml.3; Tue, 22 Mar 2016 05:17:31 -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=j3rSHblNZKlwcDPoHT+fC61A/a4MNX38k7RWGPm6no4=; b=0iEIZgQxKcwvmPBnI/LK0yqBhaWYszOuNmLgORoaL8/iEdWs0Y2cm+pAxm09TNmCte Cwa0VO30ErRiSUoJJKvZU3odNz2kfkwHfcK3geamGA2XS/c4WE8+vqyq6xM+qRS1p+e9 saxadgu/gknmQs1k0AnGPiPr138taNBDoS9MmFzh0CUfaTo+cz0IWFuY02VUo2mpO+Nv Cz/a59Af5jQm6PmrkG8AWZeuEhPA4PZSP/Mnhu+a4lyc06oDCfwIyX5PwSrB9pddai0Z bn9jPZrkOcgi3hYe18OwTXWbzuNao/sTuYp2TFl46aZ632CHUpumhTvQ8k6lCaiLJg3n dQgQ== 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=j3rSHblNZKlwcDPoHT+fC61A/a4MNX38k7RWGPm6no4=; b=VtY26ovlGE8RSUYu/hof2BHGQvms5KrTlhRQ885Aqa2jKVZWkkoT7hLTxGnVG9j0Wd 80TW6Lzjp/32DDjv8SMxDBnbjw/ZiQ9/x+wIx0V34+08lGwI3bzKd5S7gRyOEr3UAVSe Dbmue9lodssfKDyyP9IMJAerRHKojLygVRzcFuxLQ/229cpOvtmhv5inrXqwcfkbrdHC CZElDewP2qCpLV/+NMBMvxbK1Eqp7Q6vj7nhHJ990Dr+omNG4BEiYfS9pgJ78fZlSX16 Pxbplm4vxLFmxiHaEVO7MNv6Z0VudZA76XMwtwGLkD9dfsNVhHe3GEAJHIAjlRUYb+O/ 4PAA== X-Gm-Message-State: AD7BkJJByDqqfq6dYgT1yynseKURQmDPm8RjNf1NA1iuTczU5OdBFKH0vOsGDl2xn4kC+w== X-Received: by 10.194.84.2 with SMTP id u2mr35770996wjy.61.1458649051069; Tue, 22 Mar 2016 05:17:31 -0700 (PDT) Received: from localhost (x55b18fc0.dyn.telefonica.de. [85.177.143.192]) by smtp.gmail.com with ESMTPSA id e4sm17113519wma.10.2016.03.22.05.17.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Mar 2016 05:17:30 -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 12/14] lib/mpi: mpi_read_raw_from_sgl(): fix nbits calculation Date: Tue, 22 Mar 2016 13:17:27 +0100 Message-Id: <1458649047-1661-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 The number of bits, nbits, is calculated in mpi_read_raw_from_sgl() as follows: nbits = nbytes * 8; Afterwards, the number of leading zero bits of the first byte get subtracted: nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros)); However, count_leading_zeros() takes an unsigned long and thus, the u8 gets promoted to an unsigned long. Thus, the above doesn't subtract the number of leading zeros in the most significant nonzero input byte from nbits, but the number of leading zeros of the most significant nonzero input byte promoted to unsigned long, i.e. BITS_PER_LONG - 8 too many. Fix this by subtracting count_leading_zeros(...) - (BITS_PER_LONG - 8) from nbits only. Fixes: 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") Signed-off-by: Nicolai Stange --- lib/mpi/mpicoder.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index 27703aa..24a0155 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c @@ -460,7 +460,8 @@ MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes) } if (nbytes > 0) - nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros)); + nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros)) - + (BITS_PER_LONG - 8); nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); val = mpi_alloc(nlimbs);