From patchwork Mon Mar 21 13:26:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 8633271 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 A07BA9F3D1 for ; Mon, 21 Mar 2016 13:32:52 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C43DE2026F for ; Mon, 21 Mar 2016 13:32:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DEAE120204 for ; Mon, 21 Mar 2016 13:32:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755911AbcCUNcs (ORCPT ); Mon, 21 Mar 2016 09:32:48 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:33381 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754638AbcCUN0g (ORCPT ); Mon, 21 Mar 2016 09:26:36 -0400 Received: by mail-wm0-f65.google.com with SMTP id x188so21824372wmg.0; Mon, 21 Mar 2016 06:26:35 -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=lV1L0Dy2UvTaCrtE+bUBWf26aElHNEjvSbPFKU55klQ=; b=ItCk25/hWEoOUa8k3KlBt1FJR2lP+na9OHOb08IL3eS11liHPKTFItZOXs9VGbBhCK /u6N35XF5xFRKrWVGTNwkGlw/buvYfjkUP9072+21D8GXgd0F/Jr8a2ULgSzzyNm0WOz QCPNgIEkbvb8VKjW3uTccwMKFuu1LQxzjZ/S+X2T2cuqA12uuwx+iLW1ztxSzK0xZ+2A C8Oe15dGFzD0XnMIFpHXOldbusjI+qwUIPyn8CK4RMNtfQoHcisaRdZngioib7+bQeq1 kU4plMWi7ryp6bej4j4mtdzwh5FhBi05HyuLMsbdsqPRGN39TWeG+g+PDB44Zd+wtRDT GiDA== 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=lV1L0Dy2UvTaCrtE+bUBWf26aElHNEjvSbPFKU55klQ=; b=mEiIbXWaG5u9+wHDaykKHozsYO6U2k0e4ZI0NX7Jv5v7CWMmXV+r1tf99g7NzWwTqI bjHWkHFufQMzL5JmmvLrK19jb0rrQZy9IO1OuH6oNEFcZuMfwr6PToTFkdw3fKKPnx0n ZEb1fBmkszF90Ezqc197KUFEW58N/SIszBkpLlIX5OqkP8pbHXNImcn0oLcASd+tw2r9 yYOeOB6kA/ixZ/0/2or9v/7Z6zUfK/wttIOJAycPlr2te5L4aWv0xJndUPJ+rwFSH29M QF70FSteEzouVX4MbkqzDhbV5RkU6RQUHw0xCtX25BMxMtZzDvl7eMyP1zMwC4RyhRCJ JYcg== X-Gm-Message-State: AD7BkJJ0nKP+dhN+aBRlcspZC8yx1qNxLbVuQp6ToUpGzaAGTT+2aiODc3RtPMcF7kNMCQ== X-Received: by 10.194.89.70 with SMTP id bm6mr34649824wjb.0.1458566794871; Mon, 21 Mar 2016 06:26:34 -0700 (PDT) Received: from localhost (x55b18fc0.dyn.telefonica.de. [85.177.143.192]) by smtp.gmail.com with ESMTPSA id hh8sm25275248wjc.42.2016.03.21.06.26.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2016 06:26:34 -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 01/14] lib/mpi: mpi_write_sgl(): fix skipping of leading zero limbs Date: Mon, 21 Mar 2016 14:26:02 +0100 Message-Id: <1458566775-5239-2-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 Currently, if the number of leading zeros is greater than fits into a complete limb, mpi_write_sgl() skips them by iterating over them limb-wise. However, it fails to adjust its internal leading zeros tracking variable, lzeros, accordingly: it does a p -= sizeof(alimb); continue; which should really have been a lzeros -= sizeof(alimb); continue; Since lzeros never decreases if its initial value >= sizeof(alimb), nothing gets copied by mpi_write_sgl() in that case. Instead of skipping the high order zero limbs within the loop as shown above, fix the issue by adjusting the copying loop's bounds. Fixes: 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") Signed-off-by: Nicolai Stange --- lib/mpi/mpicoder.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index eb15e7d..6bb52be 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c @@ -380,7 +380,9 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, buf_len = sgl->length; p2 = sg_virt(sgl); - for (i = a->nlimbs - 1; i >= 0; i--) { + for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, + lzeros %= BYTES_PER_MPI_LIMB; + i >= 0; i--) { alimb = a->d[i]; p = (u8 *)&alimb2; #if BYTES_PER_MPI_LIMB == 4 @@ -401,17 +403,12 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes, #error please implement for this limb size. #endif if (lzeros > 0) { - if (lzeros >= sizeof(alimb)) { - p -= sizeof(alimb); - continue; - } else { - mpi_limb_t *limb1 = (void *)p - sizeof(alimb); - mpi_limb_t *limb2 = (void *)p - sizeof(alimb) - + lzeros; - *limb1 = *limb2; - p -= lzeros; - y = lzeros; - } + mpi_limb_t *limb1 = (void *)p - sizeof(alimb); + mpi_limb_t *limb2 = (void *)p - sizeof(alimb) + + lzeros; + *limb1 = *limb2; + p -= lzeros; + y = lzeros; lzeros -= sizeof(alimb); }