From patchwork Tue Apr 7 08:52:02 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 46822 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f198.google.com (mail-lb0-f198.google.com [209.85.217.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 7ED1520553 for ; Tue, 7 Apr 2015 08:53:15 +0000 (UTC) Received: by lbfb1 with SMTP id b1sf11257014lbf.2 for ; Tue, 07 Apr 2015 01:53:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=qAEHzh0EzU57phkqIW1YpHcE3ejOPY0BNsLjwqZ3HTQ=; b=dS3dfpqsg+IS0XUoAwK8kjvGA4lUtbf98AZd07QkGfcTUkod+GvRcdTZx8kRp8mEXg puE0uynO/nn6gG4pBxhGehpQv9o/o3UyJGRKIzGnJ8N0c2A1+TOc3sAL7fOFEZlvQWfa V1aunOYP22eJKwrPvu2QGtQ9WLP9zX1nnPRr2fzMC5s+uajpLjuSmRlMspHEwG3Xl5/n b2lUHAKu+T5vhl76pONNq21e9E4zdbuNuF5Vlz6l1gQiw7X2b2+MoYZ0eFe3WoZXK3bw MRkCsFd3r3vEbgWMBMv+YaWtYdA7eDkPXULpiJ/d0168OGx1YexlfO9hRSkhDa8mBqie BlOg== X-Gm-Message-State: ALoCoQlixVtYYuF0vLO/4DsQ+uOG6tNHJM5IFsx8lFlEQmBgWWZSknH0vueL9HrAxo826uI0aC2x X-Received: by 10.112.142.1 with SMTP id rs1mr3881872lbb.19.1428396794414; Tue, 07 Apr 2015 01:53:14 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.87.204 with SMTP id ba12ls15396lab.41.gmail; Tue, 07 Apr 2015 01:53:14 -0700 (PDT) X-Received: by 10.112.83.135 with SMTP id q7mr8044988lby.13.1428396794268; Tue, 07 Apr 2015 01:53:14 -0700 (PDT) Received: from mail-la0-f53.google.com (mail-la0-f53.google.com. [209.85.215.53]) by mx.google.com with ESMTPS id x11si5463804lba.54.2015.04.07.01.53.14 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 07 Apr 2015 01:53:14 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.53 as permitted sender) client-ip=209.85.215.53; Received: by lagv1 with SMTP id v1so36951074lag.3 for ; Tue, 07 Apr 2015 01:53:14 -0700 (PDT) X-Received: by 10.152.26.34 with SMTP id i2mr16858429lag.117.1428396794172; Tue, 07 Apr 2015 01:53:14 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.67.65 with SMTP id l1csp291813lbt; Tue, 7 Apr 2015 01:53:12 -0700 (PDT) X-Received: by 10.66.189.105 with SMTP id gh9mr34085888pac.41.1428396781597; Tue, 07 Apr 2015 01:53:01 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h4si2547968pdj.248.2015.04.07.01.52.59 for ; Tue, 07 Apr 2015 01:53:01 -0700 (PDT) Received-SPF: none (google.com: linux-crypto-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753209AbbDGIw7 (ORCPT ); Tue, 7 Apr 2015 04:52:59 -0400 Received: from mail-wi0-f169.google.com ([209.85.212.169]:36454 "EHLO mail-wi0-f169.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752911AbbDGIw6 (ORCPT ); Tue, 7 Apr 2015 04:52:58 -0400 Received: by wizk4 with SMTP id k4so8936831wiz.1 for ; Tue, 07 Apr 2015 01:52:56 -0700 (PDT) X-Received: by 10.194.237.34 with SMTP id uz2mr37016503wjc.157.1428396776868; Tue, 07 Apr 2015 01:52:56 -0700 (PDT) Received: from ards-macbook-pro.local ([90.174.4.191]) by mx.google.com with ESMTPSA id q10sm9935641wjr.41.2015.04.07.01.52.54 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 07 Apr 2015 01:52:56 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org, linux-arm-kernel@lists.infradead.org, x86@kernel.org, herbert@gondor.apana.org.au, samitolvanen@google.com, jussi.kivilinna@iki.fi Cc: stockhausen@collogia.de, Ard Biesheuvel Subject: [PATCH v3 14/16] crypto/x86: move SHA-1 SSSE3 implementation to base layer Date: Tue, 7 Apr 2015 10:52:02 +0200 Message-Id: <1428396724-19962-15-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1428396724-19962-1-git-send-email-ard.biesheuvel@linaro.org> References: <1428396724-19962-1-git-send-email-ard.biesheuvel@linaro.org> Sender: linux-crypto-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ard.biesheuvel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.53 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Signed-off-by: Ard Biesheuvel --- arch/x86/crypto/sha1_ssse3_glue.c | 136 +++++++++----------------------------- 1 file changed, 30 insertions(+), 106 deletions(-) diff --git a/arch/x86/crypto/sha1_ssse3_glue.c b/arch/x86/crypto/sha1_ssse3_glue.c index 6c20fe04a738..8678dc75fbf3 100644 --- a/arch/x86/crypto/sha1_ssse3_glue.c +++ b/arch/x86/crypto/sha1_ssse3_glue.c @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include @@ -49,127 +49,50 @@ asmlinkage void sha1_transform_avx2(u32 *digest, const char *data, static asmlinkage void (*sha1_transform_asm)(u32 *, const char *, unsigned int); - -static int sha1_ssse3_init(struct shash_desc *desc) +static void sha1_ssse3_block_fn(int blocks, u8 const *src, u32 *state, + const u8 *head, void *p) { - struct sha1_state *sctx = shash_desc_ctx(desc); - - *sctx = (struct sha1_state){ - .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 }, - }; - - return 0; -} - -static int __sha1_ssse3_update(struct shash_desc *desc, const u8 *data, - unsigned int len, unsigned int partial) -{ - struct sha1_state *sctx = shash_desc_ctx(desc); - unsigned int done = 0; - - sctx->count += len; - - if (partial) { - done = SHA1_BLOCK_SIZE - partial; - memcpy(sctx->buffer + partial, data, done); - sha1_transform_asm(sctx->state, sctx->buffer, 1); - } - - if (len - done >= SHA1_BLOCK_SIZE) { - const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE; - - sha1_transform_asm(sctx->state, data + done, rounds); - done += rounds * SHA1_BLOCK_SIZE; - } - - memcpy(sctx->buffer, data + done, len - done); - - return 0; + if (head) + sha1_transform_asm(state, head, 1); + if (blocks) + sha1_transform_asm(state, src, blocks); } static int sha1_ssse3_update(struct shash_desc *desc, const u8 *data, unsigned int len) { struct sha1_state *sctx = shash_desc_ctx(desc); - unsigned int partial = sctx->count % SHA1_BLOCK_SIZE; - int res; - /* Handle the fast case right here */ - if (partial + len < SHA1_BLOCK_SIZE) { - sctx->count += len; - memcpy(sctx->buffer + partial, data, len); + if (!irq_fpu_usable() || + (sctx->count % SHA1_BLOCK_SIZE) + len < SHA1_BLOCK_SIZE) + return crypto_sha1_update(desc, data, len); - return 0; - } - - if (!irq_fpu_usable()) { - res = crypto_sha1_update(desc, data, len); - } else { - kernel_fpu_begin(); - res = __sha1_ssse3_update(desc, data, len, partial); - kernel_fpu_end(); - } - - return res; -} - - -/* Add padding and return the message digest. */ -static int sha1_ssse3_final(struct shash_desc *desc, u8 *out) -{ - struct sha1_state *sctx = shash_desc_ctx(desc); - unsigned int i, index, padlen; - __be32 *dst = (__be32 *)out; - __be64 bits; - static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, }; - - bits = cpu_to_be64(sctx->count << 3); - - /* Pad out to 56 mod 64 and append length */ - index = sctx->count % SHA1_BLOCK_SIZE; - padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index); - if (!irq_fpu_usable()) { - crypto_sha1_update(desc, padding, padlen); - crypto_sha1_update(desc, (const u8 *)&bits, sizeof(bits)); - } else { - kernel_fpu_begin(); - /* We need to fill a whole block for __sha1_ssse3_update() */ - if (padlen <= 56) { - sctx->count += padlen; - memcpy(sctx->buffer + index, padding, padlen); - } else { - __sha1_ssse3_update(desc, padding, padlen, index); - } - __sha1_ssse3_update(desc, (const u8 *)&bits, sizeof(bits), 56); - kernel_fpu_end(); - } - - /* Store state in digest */ - for (i = 0; i < 5; i++) - dst[i] = cpu_to_be32(sctx->state[i]); - - /* Wipe context */ - memset(sctx, 0, sizeof(*sctx)); + kernel_fpu_begin(); + sha1_base_do_update(desc, data, len, sha1_ssse3_block_fn, NULL); + kernel_fpu_end(); return 0; } -static int sha1_ssse3_export(struct shash_desc *desc, void *out) +static int sha1_ssse3_finup(struct shash_desc *desc, const u8 *data, + unsigned int len, u8 *out) { - struct sha1_state *sctx = shash_desc_ctx(desc); + if (!irq_fpu_usable()) + return crypto_sha1_finup(desc, data, len, out); - memcpy(out, sctx, sizeof(*sctx)); + kernel_fpu_begin(); + if (len) + sha1_base_do_update(desc, data, len, sha1_ssse3_block_fn, NULL); + sha1_base_do_finalize(desc, sha1_ssse3_block_fn, NULL); + kernel_fpu_end(); - return 0; + return sha1_base_finish(desc, out); } -static int sha1_ssse3_import(struct shash_desc *desc, const void *in) +/* Add padding and return the message digest. */ +static int sha1_ssse3_final(struct shash_desc *desc, u8 *out) { - struct sha1_state *sctx = shash_desc_ctx(desc); - - memcpy(sctx, in, sizeof(*sctx)); - - return 0; + return sha1_ssse3_finup(desc, NULL, 0, out); } #ifdef CONFIG_AS_AVX2 @@ -186,11 +109,12 @@ static void sha1_apply_transform_avx2(u32 *digest, const char *data, static struct shash_alg alg = { .digestsize = SHA1_DIGEST_SIZE, - .init = sha1_ssse3_init, + .init = sha1_base_init, .update = sha1_ssse3_update, .final = sha1_ssse3_final, - .export = sha1_ssse3_export, - .import = sha1_ssse3_import, + .finup = sha1_ssse3_finup, + .export = sha1_base_export, + .import = sha1_base_import, .descsize = sizeof(struct sha1_state), .statesize = sizeof(struct sha1_state), .base = {