From patchwork Mon Mar 21 13:26:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolai Stange X-Patchwork-Id: 8633081 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 49A4E9F372 for ; Mon, 21 Mar 2016 13:30:01 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6FD21202E5 for ; Mon, 21 Mar 2016 13:30:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 877F32026F for ; Mon, 21 Mar 2016 13:29:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755873AbcCUN0q (ORCPT ); Mon, 21 Mar 2016 09:26:46 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:36350 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755839AbcCUN0p (ORCPT ); Mon, 21 Mar 2016 09:26:45 -0400 Received: by mail-wm0-f67.google.com with SMTP id l68so21876925wml.3; Mon, 21 Mar 2016 06:26:44 -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=qbrjHgDRbfyuG6YcTgmcMt1d8+Ma4CNcL8o/mEeq5gg=; b=sH02saiXOqbOnszCdHvNNp/rmLwamfr83KWnCkpXHZ5x13AkKO377n0B54EHUmdvLr jdowAVwFlXW/uUhq+0U3rTETmDj/47XsjlRapJPtJ5t1/JxJ/v22jU3d5hEB6NvsLBJI O8iYaS2fz/jAx0D+NitiGNf8sdv1ZPdtZsEVE0+nAKprvu7duck5/1Juo/h4BHaUMW1J wC9z1ui+wTryugF8Dl8sR8qGM4UQWU58KpTi9PHl2ZSG4JK0CFFBtdjAzvOmQg0+1kzs VoSRWTVM5HopRK/S+oLz3GHz0ZyUB/8PMEOxm5iFC8D9OHFleL/A/z6kHc4XfV33S44o TwIg== 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=qbrjHgDRbfyuG6YcTgmcMt1d8+Ma4CNcL8o/mEeq5gg=; b=R+oLIVHQWUo0PMMsDQrW5u9qDXnfMl3MoQHng5GAdhKbVR+6+nd5N7D56x7oyeRnOr 6HrfVEZFa9T0eOOeb8lXGzASay1xy6p+wFMpb+lOftsvjd5NyIAnoILtLU16X4U2uaH3 skablEWY8zNmJqOpWE7L7U3PB5QzjxqbveK57nrJZST3k+VwKwcSjdORQOar2uYqbvH6 FYCZsWptsoZCisn3N29nT4JTdmXCQiIL4D4WWEplr4Lof0pQsH/6D0wKJhbG6olmnjM2 zOa2bj8BAS0kLhybPzNdewNoqZU5N01vGcxmFJbTU4nQD2M6tiXOwVlwy7eubZeg0FSD 7nqw== X-Gm-Message-State: AD7BkJKj2ivK0HcDlNtu0JpEUsZfo2WUcSIoufY+fElS/T+8LSJkkr96iSPaYm3zbmqOog== X-Received: by 10.28.226.212 with SMTP id z203mr14481625wmg.9.1458566803851; Mon, 21 Mar 2016 06:26:43 -0700 (PDT) Received: from localhost (x55b18fc0.dyn.telefonica.de. [85.177.143.192]) by smtp.gmail.com with ESMTPSA id 82sm12443055wmd.4.2016.03.21.06.26.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Mar 2016 06:26:43 -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 07/14] lib/mpi: mpi_read_buffer(): replace open coded endian conversion Date: Mon, 21 Mar 2016 14:26:08 +0100 Message-Id: <1458566775-5239-8-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=unavailable 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)