From patchwork Mon Mar 30 09:36:13 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 46477 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-yh0-f72.google.com (mail-yh0-f72.google.com [209.85.213.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 3F335214C7 for ; Mon, 30 Mar 2015 09:36:54 +0000 (UTC) Received: by yhhg46 with SMTP id g46sf142615792yhh.2 for ; Mon, 30 Mar 2015 02:36:54 -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=q1IYqTY3fK8gz/AdlwCVrh8ghewi8Mx6kPHcK1Uo824=; b=I/ayKfCJk1LStxW4OBfVcglTdbNRNGKCIoWXCgA3e0sGaPqxdksCTHLpqGc3k/iY5X xshWxO2J9NJ45QbHlchC+k7JLH27DXX627UXrZmq3yM+J/fBM19OK/yBtUfx/2WAFn2O FSpZA8QtK7ZTgMqGXyb5A2ZKZzqf+91qdhMqet7jrza7QTXjYGmXu/TNWFdP7dGIi0jT CQJVc/sb927a69ZhLGXLw15q4GbfFbDJszQO1YQ3HxwjGGDUAJGr6uiklLLcoIXI7avx /QMQemOpMGR/B2YfUHRsXgMI0Q2ZT3obi1uwOoNFid6YCtbEehIfkEigBk5q/b9EEaY/ rLkA== X-Gm-Message-State: ALoCoQmk9rgAvBhOMu1PUDUtPqSQztoU+quMQCRv1zRUFR9NNnx0fqz8l2Vl+sctESB+tq9UT6Nb X-Received: by 10.236.209.33 with SMTP id r21mr37777483yho.6.1427708214094; Mon, 30 Mar 2015 02:36:54 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.82.83 with SMTP id g77ls2339471qgd.99.gmail; Mon, 30 Mar 2015 02:36:54 -0700 (PDT) X-Received: by 10.229.118.5 with SMTP id t5mr41172391qcq.19.1427708214012; Mon, 30 Mar 2015 02:36:54 -0700 (PDT) Received: from mail-qc0-f169.google.com (mail-qc0-f169.google.com. [209.85.216.169]) by mx.google.com with ESMTPS id h7si2009773qge.80.2015.03.30.02.36.53 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Mar 2015 02:36:54 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.216.169 as permitted sender) client-ip=209.85.216.169; Received: by qcgx3 with SMTP id x3so8358472qcg.3 for ; Mon, 30 Mar 2015 02:36:53 -0700 (PDT) X-Received: by 10.140.18.175 with SMTP id 44mr19010607qgf.80.1427708213905; Mon, 30 Mar 2015 02:36:53 -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.96.155.231 with SMTP id vz7csp1323182qdb; Mon, 30 Mar 2015 02:36:52 -0700 (PDT) X-Received: by 10.66.55.68 with SMTP id q4mr57368373pap.71.1427708212490; Mon, 30 Mar 2015 02:36:52 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 10si6915535pde.142.2015.03.30.02.36.52 for ; Mon, 30 Mar 2015 02:36:52 -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 S1752832AbbC3Jgu (ORCPT ); Mon, 30 Mar 2015 05:36:50 -0400 Received: from mail-wi0-f177.google.com ([209.85.212.177]:37257 "EHLO mail-wi0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752322AbbC3Jgr (ORCPT ); Mon, 30 Mar 2015 05:36:47 -0400 Received: by wiaa2 with SMTP id a2so120320558wia.0 for ; Mon, 30 Mar 2015 02:36:46 -0700 (PDT) X-Received: by 10.194.239.65 with SMTP id vq1mr59864328wjc.98.1427708205317; Mon, 30 Mar 2015 02:36:45 -0700 (PDT) Received: from ards-macbook-pro.local (129.20.90.92.rev.sfr.net. [92.90.20.129]) by mx.google.com with ESMTPSA id jt8sm15507705wid.4.2015.03.30.02.36.42 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 30 Mar 2015 02:36:44 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, linux-crypto@vger.kernel.org, samitolvanen@google.com, herbert@gondor.apana.org.au, jussi.kivilinna@iki.fi, stockhausen@collogia.de, x86@kernel.org Cc: Ard Biesheuvel Subject: [RFC PATCH 2/6] crypto: sha512-generic: move to generic glue implementation Date: Mon, 30 Mar 2015 11:36:13 +0200 Message-Id: <1427708184-2353-4-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1427708184-2353-1-git-send-email-ard.biesheuvel@linaro.org> References: <1427708184-2353-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.216.169 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: , This updated the generic SHA-512 implementation to use the generic shared SHA-512 glue code. Signed-off-by: Ard Biesheuvel --- crypto/Kconfig | 1 + crypto/sha512_generic.c | 117 +++++++----------------------------------------- 2 files changed, 16 insertions(+), 102 deletions(-) diff --git a/crypto/Kconfig b/crypto/Kconfig index 3400cf4e3cdb..880aa518c2eb 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -646,6 +646,7 @@ config CRYPTO_SHA512_BASE config CRYPTO_SHA512 tristate "SHA384 and SHA512 digest algorithms" + select CRYPTO_SHA512_BASE select CRYPTO_HASH help SHA512 secure hash standard (DFIPS 180-2). diff --git a/crypto/sha512_generic.c b/crypto/sha512_generic.c index 1c3c3767e079..0d8e973d0d4b 100644 --- a/crypto/sha512_generic.c +++ b/crypto/sha512_generic.c @@ -130,123 +130,36 @@ sha512_transform(u64 *state, const u8 *input) a = b = c = d = e = f = g = h = t1 = t2 = 0; } -static int -sha512_init(struct shash_desc *desc) -{ - struct sha512_state *sctx = shash_desc_ctx(desc); - sctx->state[0] = SHA512_H0; - sctx->state[1] = SHA512_H1; - sctx->state[2] = SHA512_H2; - sctx->state[3] = SHA512_H3; - sctx->state[4] = SHA512_H4; - sctx->state[5] = SHA512_H5; - sctx->state[6] = SHA512_H6; - sctx->state[7] = SHA512_H7; - sctx->count[0] = sctx->count[1] = 0; - - return 0; -} - -static int -sha384_init(struct shash_desc *desc) +static void sha512_generic_block_fn(int blocks, u8 const *src, u64 *state, + const u8 *head, void *p) { - struct sha512_state *sctx = shash_desc_ctx(desc); - sctx->state[0] = SHA384_H0; - sctx->state[1] = SHA384_H1; - sctx->state[2] = SHA384_H2; - sctx->state[3] = SHA384_H3; - sctx->state[4] = SHA384_H4; - sctx->state[5] = SHA384_H5; - sctx->state[6] = SHA384_H6; - sctx->state[7] = SHA384_H7; - sctx->count[0] = sctx->count[1] = 0; + if (head) + sha512_transform(state, head); - return 0; + while (blocks--) { + sha512_transform(state, src); + src += SHA512_BLOCK_SIZE; + } } int crypto_sha512_update(struct shash_desc *desc, const u8 *data, unsigned int len) { - struct sha512_state *sctx = shash_desc_ctx(desc); - - unsigned int i, index, part_len; - - /* Compute number of bytes mod 128 */ - index = sctx->count[0] & 0x7f; - - /* Update number of bytes */ - if ((sctx->count[0] += len) < len) - sctx->count[1]++; - - part_len = 128 - index; - - /* Transform as many times as possible. */ - if (len >= part_len) { - memcpy(&sctx->buf[index], data, part_len); - sha512_transform(sctx->state, sctx->buf); - - for (i = part_len; i + 127 < len; i+=128) - sha512_transform(sctx->state, &data[i]); - - index = 0; - } else { - i = 0; - } - - /* Buffer remaining input */ - memcpy(&sctx->buf[index], &data[i], len - i); - - return 0; + return sha512_base_do_update(desc, data, len, sha512_generic_block_fn, + NULL); } EXPORT_SYMBOL(crypto_sha512_update); static int sha512_final(struct shash_desc *desc, u8 *hash) { - struct sha512_state *sctx = shash_desc_ctx(desc); - static u8 padding[128] = { 0x80, }; - __be64 *dst = (__be64 *)hash; - __be64 bits[2]; - unsigned int index, pad_len; - int i; - - /* Save number of bits */ - bits[1] = cpu_to_be64(sctx->count[0] << 3); - bits[0] = cpu_to_be64(sctx->count[1] << 3 | sctx->count[0] >> 61); - - /* Pad out to 112 mod 128. */ - index = sctx->count[0] & 0x7f; - pad_len = (index < 112) ? (112 - index) : ((128+112) - index); - crypto_sha512_update(desc, padding, pad_len); - - /* Append length (before padding) */ - crypto_sha512_update(desc, (const u8 *)bits, sizeof(bits)); - - /* Store state in digest */ - for (i = 0; i < 8; i++) - dst[i] = cpu_to_be64(sctx->state[i]); - - /* Zeroize sensitive information. */ - memset(sctx, 0, sizeof(struct sha512_state)); - - return 0; -} - -static int sha384_final(struct shash_desc *desc, u8 *hash) -{ - u8 D[64]; - - sha512_final(desc, D); - - memcpy(hash, D, 48); - memzero_explicit(D, 64); - - return 0; + sha512_base_do_finalize(desc, sha512_generic_block_fn, NULL); + return sha512_base_finish(desc, hash); } static struct shash_alg sha512_algs[2] = { { .digestsize = SHA512_DIGEST_SIZE, - .init = sha512_init, + .init = sha512_base_init, .update = crypto_sha512_update, .final = sha512_final, .descsize = sizeof(struct sha512_state), @@ -259,9 +172,9 @@ static struct shash_alg sha512_algs[2] = { { } }, { .digestsize = SHA384_DIGEST_SIZE, - .init = sha384_init, + .init = sha384_base_init, .update = crypto_sha512_update, - .final = sha384_final, + .final = sha512_final, .descsize = sizeof(struct sha512_state), .base = { .cra_name = "sha384",