From patchwork Tue Mar 22 12:12:35 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 8641751 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 9FC2D9F294 for ; Tue, 22 Mar 2016 12:14:32 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B8319202FE for ; Tue, 22 Mar 2016 12:14:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C9ABA20397 for ; Tue, 22 Mar 2016 12:14:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758515AbcCVMOS (ORCPT ); Tue, 22 Mar 2016 08:14:18 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:35321 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755926AbcCVMN4 (ORCPT ); Tue, 22 Mar 2016 08:13:56 -0400 Received: by mail-wm0-f66.google.com with SMTP id r129so17282696wmr.2; Tue, 22 Mar 2016 05:13:56 -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=vHh33PNsVRGYPACYjcW9ec/1RzDN/RgqAhNfGX3u584=; b=O0yYym+IQxBnbzEkm9PnM6ZPAzNw6+SbB3rt9C9d1bIug14B13MeZNrIK/6GTmbeZw jxfzcxTgxiz0CizbbmHiJpNmc+dV0BULdnVadXF9IarNCB5PQkmMVEnq1XEJ/Wz0tQNO viAlIQk8SvGshBeGyRM9S977wpPbYyGfCoSksqk7KVcW6xSKVeo/VXuccHc7OXs/CfBE aHPlmMHyjHRUf+VzOMuICl2g7Koib549uR+qafYgh0hyQkIGWwxa2P7FDAeCkjYVzaBQ sPTunHlR4PNhs372MnXk3MHdylfyme9s3/15MkpYM5813H095iOwHmklC/0/8653wW7E STCA== 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=vHh33PNsVRGYPACYjcW9ec/1RzDN/RgqAhNfGX3u584=; b=OiMO2lLW9yQytwqXWBeYZWR6CyaQJcILP/71zRpfsDttC/u1ay8xEYcwCAa1uzs14e u/3j8DQqBm5lJxpArRIUeZWQATJjqr/4lW7YQjmGbEiDRJQUHv6xXAJFNLK/eyqU8kCc lvhlujUTpqub72PpPXf8DaBA05yhcO5ZCaRluDlwL9zhRpP/H19KIIWI+Y6Bw7yQGfuf es4GfaRzxBaAmZEAaO4IPNhc/OLmiHyuHhgZEi9j1mS82VzI6UjyZeVoC1G8ivExs3Ug EPN/OvrU26xiAKTw8MZaMtFnuq2c5rcg5pboulJfx9ygIkXGz+RyilPNWnviVHTAQQaN B8Lw== X-Gm-Message-State: AD7BkJL2cBQVRJz06dclcWvfF+2xThAuMt9dAe+p/q4VbkrwJpNJgtDkOFHe/jSE4G0Pwg== X-Received: by 10.194.113.130 with SMTP id iy2mr36157101wjb.56.1458648835259; Tue, 22 Mar 2016 05:13:55 -0700 (PDT) Received: from localhost (x55b18fc0.dyn.telefonica.de. [85.177.143.192]) by smtp.gmail.com with ESMTPSA id gk4sm29991484wjd.7.2016.03.22.05.13.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Mar 2016 05:13:54 -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 01/14] lib/mpi: mpi_write_sgl(): fix skipping of leading zero limbs Date: Tue, 22 Mar 2016 13:12:35 +0100 Message-Id: <1458648768-1469-2-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 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); }