From patchwork Mon Mar 21 13:26:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 8632941 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 2F6FF9F372 for ; Mon, 21 Mar 2016 13:27:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 342AF2034B for ; Mon, 21 Mar 2016 13:27:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 57F8620306 for ; Mon, 21 Mar 2016 13:27:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756045AbcCUN04 (ORCPT ); Mon, 21 Mar 2016 09:26:56 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:33572 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755997AbcCUN0w (ORCPT ); Mon, 21 Mar 2016 09:26:52 -0400 Received: by mail-wm0-f66.google.com with SMTP id x188so21825930wmg.0; Mon, 21 Mar 2016 06:26:52 -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=OELbTTalp9nO94+NowrJ6/wf5zguWE52ohmRxISGFg8=; b=OxCg5v/u08m1a4GJH3wmtp+aWfpmbA7kT8IAblzmKF218Mdode7XZuPFeUadXWSR3a 3rmeftJEJa0KqE8hPh/Z/+iKQncWb0v9gjhVb9BcnfWqTTkXdXFnU6txlSByzl/ODrLb C1DHqXRqWgU4AqCTbTfOMNVVJ+21Ug+RWIZwidL7OkFm6hYlz5xRsH98c1vSCnYiOXxy 2KRXx/81U9esaymfWy+YOdlp9z3qsmLiIU0T8IHSgutpf+XjPD2JP0Of5MFg/9WYvEZO 9219IOiUPVmCMQ+nbbzVoqEzwz1/prGM2d4m/fne7sfqPSyrB1HqYoUUHKinpxF8RvZO OTSQ== 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=OELbTTalp9nO94+NowrJ6/wf5zguWE52ohmRxISGFg8=; b=iIs7qszCYeuzQRQTDuIdSThF2BojRNm/xTJbQ0GFGvBKRQJL+UOX1QRtiEk7tzMcXx 8EcNFN27LHyd3ajZAP/L7nbVLcdWPD/SKjor2NwvuB2BBy5TbcgFfCbXfjRtvgmIzvnh rNAWcl1IxtShFSxkiRVXXrtHwTrL/qf7QAy9Ro3Y7mca/noIDcpSGuLn/TnlQr+o7dkr kzCdcU2wOcjk8qSpGAiZqu5ndjZyfxiPIflS7ClmgT0GL+9+rL3T4rCxw7bcH0Jd/sIU 1EzGHtiFYNdo9Lkr6CJvaCQXRA6GiqKTyxvjChdxVrFLxRtVKVVu8KXDYTfLbzgljoCk 4QEg== X-Gm-Message-State: AD7BkJJj3Ib4o8xB5oUoM9xXcHtATOEwd4l+UaImxO01MO2UHkw8sWCGqONAeBz6IA5UBQ== X-Received: by 10.28.158.15 with SMTP id h15mr14452488wme.89.1458566811329; Mon, 21 Mar 2016 06:26:51 -0700 (PDT) Received: from localhost (x55b18fc0.dyn.telefonica.de. [85.177.143.192]) by smtp.gmail.com with ESMTPSA id jv7sm25467423wjb.36.2016.03.21.06.26.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2016 06:26:50 -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 12/14] lib/mpi: mpi_read_raw_from_sgl(): fix nbits calculation Date: Mon, 21 Mar 2016 14:26:13 +0100 Message-Id: <1458566775-5239-13-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 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 4cf8516..5d02efe 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);