From patchwork Tue Mar 22 12:12:41 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 8641771 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 6718A9F294 for ; Tue, 22 Mar 2016 12:14:37 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 74ECA202FE for ; Tue, 22 Mar 2016 12:14:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5E72D20396 for ; Tue, 22 Mar 2016 12:14:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758265AbcCVMO0 (ORCPT ); Tue, 22 Mar 2016 08:14:26 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:35880 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757386AbcCVMOH (ORCPT ); Tue, 22 Mar 2016 08:14:07 -0400 Received: by mail-wm0-f65.google.com with SMTP id l68so29153380wml.3; Tue, 22 Mar 2016 05:14:05 -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=aoLNe0uYZeYDBAx1Srtz+Oae0s0s9qsKU8L0T5LQf4Y=; b=ASIykMoQUYNBfynViouAN2zjH6KaWZ4/5+HvWr7hb/u409MYXgnnqdSNC3/WMFvyb9 HOMzTtZ27bHPTLinz8TtOJp5JzD7qDm5zCI3usAvNYvRENIfWsH7JZ9zB//i1j5/kE9n 75HC8o5AK9Q+KmCsjtNvyeHJpyoSegboO7ot2rAecFP/cdDU9FaLGtg5f8t5czvlvHUK kXT4XJqNkpE1/hRt4Z3OK77YEZYlqzTDhml22NwAO3qISUTIsIAFHn8RvbxDnYJqi7la a4BEwCZWrtdQbdbQtE/GSo7EYJ2uupReL6UzWkY+ITMiCN6xcVQl8T5tYz+fNCjHO36W j4vg== 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=aoLNe0uYZeYDBAx1Srtz+Oae0s0s9qsKU8L0T5LQf4Y=; b=hn9t1vTUTU5/fUq5ZMsRqSN+ZCehv31jNp3gm2BiA5u1m5Ht3n8lLTLH5gAE9sDI2+ s7sroBTHD6Ftoy6DBbmjXaFUOrwWYhObr6MXmnEuoELnPyRFcwvUJ8V9WVEtnVGoGGXX Ths0HZFpg02LbO0ZMIVijUYC60bPTwh6JaRkdWoisnXd75WDr+FT7kGM7DqX6Mwk5cKL YdSem6XdHNCc6vQjK4ArgzHzflbQnLxycxobFAXIUKHWa0gGy3uDb7NII6pGm9fR/zmT PBxsBlEDdmc6rDCOWnTTbPVdiaeAY484IzB4nl3W6R/EJoVwksqieFQ+H8thCv6Ewx0N iTKQ== X-Gm-Message-State: AD7BkJKtfNOOg2W4iZgzNU/ywQ9cmVDUEjMA0WMeoTSI8MXRwBsjfEdqs622rzfDTbrH6g== X-Received: by 10.28.211.1 with SMTP id k1mr21186580wmg.93.1458648844489; Tue, 22 Mar 2016 05:14:04 -0700 (PDT) Received: from localhost (x55b18fc0.dyn.telefonica.de. [85.177.143.192]) by smtp.gmail.com with ESMTPSA id v188sm17027776wmv.3.2016.03.22.05.14.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Mar 2016 05:14:04 -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 07/14] lib/mpi: mpi_read_buffer(): replace open coded endian conversion Date: Tue, 22 Mar 2016 13:12:41 +0100 Message-Id: <1458648768-1469-8-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, the endian conversion from CPU order to BE is open coded in mpi_read_buffer(). Replace this by the centrally provided cpu_to_be*() macros. Copy from the temporary storage on stack to the destination buffer by means of memcpy(). Signed-off-by: Nicolai Stange --- lib/mpi/mpicoder.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index 2fd8d41..a999ee1 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "mpi-internal.h" #define MAX_EXTERN_MPI_BITS 16384 @@ -164,7 +165,13 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, int *sign) { uint8_t *p; - mpi_limb_t alimb; +#if BYTES_PER_MPI_LIMB == 4 + __be32 alimb; +#elif BYTES_PER_MPI_LIMB == 8 + __be64 alimb; +#else +#error please implement for this limb size. +#endif unsigned int n = mpi_get_size(a); int i, lzeros; @@ -187,25 +194,15 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes, for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB, lzeros %= BYTES_PER_MPI_LIMB; i >= 0; i--) { - alimb = a->d[i]; #if BYTES_PER_MPI_LIMB == 4 - *p++ = alimb >> 24; - *p++ = alimb >> 16; - *p++ = alimb >> 8; - *p++ = alimb; + alimb = cpu_to_be32(a->d[i]); #elif BYTES_PER_MPI_LIMB == 8 - *p++ = alimb >> 56; - *p++ = alimb >> 48; - *p++ = alimb >> 40; - *p++ = alimb >> 32; - *p++ = alimb >> 24; - *p++ = alimb >> 16; - *p++ = alimb >> 8; - *p++ = alimb; + alimb = cpu_to_be64(a->d[i]); #else #error please implement for this limb size. #endif - + memcpy(p, &alimb, BYTES_PER_MPI_LIMB); + p += BYTES_PER_MPI_LIMB; if (lzeros > 0) { mpi_limb_t *limb1 = (void *)p - sizeof(alimb); mpi_limb_t *limb2 = (void *)p - sizeof(alimb)