From patchwork Thu May 26 21:19:55 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 9137415 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7D187602A7 for ; Thu, 26 May 2016 21:20:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6EB73282F6 for ; Thu, 26 May 2016 21:20:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6331E282FB; Thu, 26 May 2016 21:20:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 16390282F6 for ; Thu, 26 May 2016 21:20:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755482AbcEZVUS (ORCPT ); Thu, 26 May 2016 17:20:18 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:33233 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755114AbcEZVUQ (ORCPT ); Thu, 26 May 2016 17:20:16 -0400 Received: by mail-wm0-f65.google.com with SMTP id a136so9054399wme.0; Thu, 26 May 2016 14:20:15 -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=wdjMkCrlQaJ+fBlGEOmkqnisHgAtmJkjl7P1lMJ8aDU=; b=rDMT2eVkqY159BrasLw6N9U1vpqNL8ErzyLjG9KqsQ/C4mtLuYSZVNWPkDMbrCOrNC z7wiXxqdG9EVhFyB4W/0P+vXdD4hFb+GCcF+CzjjWMPZRlbZ8AgB5//s+qMy4X1KmX13 Qyf91sYBYQb+/CEVcoKhm34ppr+8sKdivh+CQaowfDPHr6V6Wu9ow3GwfL1w/oCQAqVI FYg1jMd8Hpu6IJpfRi3M1MQQ9aGkXNsWNsxIaNvcZ7S5wpa9x7nJ3rpmEr8Ym0Ac26Ae MX7S4eGAe5BkD5KAg0KSBTqDAzbTwjkQwbxK+NIfInFv0Wnpj5Lvo+p297KCwXBMOMlS 8ZtA== 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=wdjMkCrlQaJ+fBlGEOmkqnisHgAtmJkjl7P1lMJ8aDU=; b=lVC75ex9DIcD0/sDWspQp6UjMxZn7JCQiT6zgK+dzCncYxjW3555X7wmcM/r5pg5Iq yl1avPsY71UUhzSW4xBM3tIY3hxSkIM8cvumdPZGkl/R6c5rRFd2X5Mi5JnX/3S2fPfM N/qHipulvQGz4Zirp9/gXRpzecMkssdDVNdRJOS8Rsy/D+QoAddf6pOSP2QTb4RmnlNf 5qMqFunEv6v+srWnoJjd33ywraCU/TyuEeK54q1TzSdEpEClLWgkd9EdazS1AA6dVYRT 5IOnb4rxENs+eWZ/4WpBX3hovU5N/QD5L4HgMdSyM0WmRmJ/duSy2Pb8wP+fnp1I0W8G 4naQ== X-Gm-Message-State: ALyK8tIOxjmBpCqJsx8t2aQhbRSCpoBQ+3YAMT/NFeHGFpu7OGVB/jhiWraKa+XgxSGYIQ== X-Received: by 10.194.121.8 with SMTP id lg8mr11392790wjb.16.1464297614591; Thu, 26 May 2016 14:20:14 -0700 (PDT) Received: from localhost (x55b166e2.dyn.telefonica.de. [85.177.102.226]) by smtp.gmail.com with ESMTPSA id o4sm16145344wjx.45.2016.05.26.14.20.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 May 2016 14:20:14 -0700 (PDT) From: Nicolai Stange To: Herbert Xu Cc: David Howells , Tadeusz Struk , Michal Marek , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Nicolai Stange Subject: [PATCH 5/5] lib/mpi: refactor mpi_read_from_buffer() in terms of mpi_read_raw_data() Date: Thu, 26 May 2016 23:19:55 +0200 Message-Id: <1464297595-24032-6-git-send-email-nicstange@gmail.com> X-Mailer: git-send-email 2.8.2 In-Reply-To: <1464297595-24032-1-git-send-email-nicstange@gmail.com> References: <1464297595-24032-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-Virus-Scanned: ClamAV using ClamSMTP mpi_read_from_buffer() and mpi_read_raw_data() do basically the same thing except that the former extracts the number of payload bits from the first two bytes of the input buffer. Besides that, the data copying logic is exactly the same. Replace the open coded buffer to MPI instance conversion by a call to mpi_read_raw_data(). Signed-off-by: Nicolai Stange --- lib/mpi/mpicoder.c | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index 2f4d039..e8a5742 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c @@ -82,10 +82,8 @@ EXPORT_SYMBOL_GPL(mpi_read_raw_data); MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread) { const uint8_t *buffer = xbuffer; - int i, j; - unsigned nbits, nbytes, nlimbs; - mpi_limb_t a; - MPI val = NULL; + unsigned int nbits, nbytes; + MPI val; if (*ret_nread < 2) return ERR_PTR(-EINVAL); @@ -95,7 +93,6 @@ MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread) pr_info("MPI: mpi too large (%u bits)\n", nbits); return ERR_PTR(-EINVAL); } - buffer += 2; nbytes = DIV_ROUND_UP(nbits, 8); if (nbytes + 2 > *ret_nread) { @@ -104,24 +101,9 @@ MPI mpi_read_from_buffer(const void *xbuffer, unsigned *ret_nread) return ERR_PTR(-EINVAL); } - nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB); - val = mpi_alloc(nlimbs); + val = mpi_read_raw_data(buffer + 2, nbytes); if (!val) return ERR_PTR(-ENOMEM); - i = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB; - i %= BYTES_PER_MPI_LIMB; - val->nbits = nbits; - j = val->nlimbs = nlimbs; - val->sign = 0; - for (; j > 0; j--) { - a = 0; - for (; i < BYTES_PER_MPI_LIMB; i++) { - a <<= 8; - a |= *buffer++; - } - i = 0; - val->d[j - 1] = a; - } *ret_nread = nbytes + 2; return val;