2012-05-14 19:06:31 +08:00
|
|
|
/**
|
|
|
|
* SHA-512 routines supporting the Power 7+ Nest Accelerators driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011-2012 International Business Machines Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 only.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
* Author: Kent Yoder <yoder1@us.ibm.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <crypto/internal/hash.h>
|
|
|
|
#include <crypto/sha.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <asm/vio.h>
|
|
|
|
|
|
|
|
#include "nx_csbcpb.h"
|
|
|
|
#include "nx.h"
|
|
|
|
|
|
|
|
|
|
|
|
static int nx_sha512_init(struct shash_desc *desc)
|
|
|
|
{
|
|
|
|
struct sha512_state *sctx = shash_desc_ctx(desc);
|
|
|
|
struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
|
2015-04-24 04:41:43 +08:00
|
|
|
struct nx_sg *out_sg;
|
2014-10-29 01:49:46 +08:00
|
|
|
int len;
|
2015-04-24 04:41:43 +08:00
|
|
|
u32 max_sg_len;
|
2012-05-14 19:06:31 +08:00
|
|
|
|
|
|
|
nx_ctx_init(nx_ctx, HCOP_FC_SHA);
|
|
|
|
|
|
|
|
memset(sctx, 0, sizeof *sctx);
|
|
|
|
|
|
|
|
nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA512];
|
|
|
|
|
|
|
|
NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA512);
|
|
|
|
|
2015-04-24 04:41:43 +08:00
|
|
|
max_sg_len = min_t(u64, nx_ctx->ap->sglen,
|
|
|
|
nx_driver.of.max_sg_len/sizeof(struct nx_sg));
|
|
|
|
max_sg_len = min_t(u64, max_sg_len,
|
|
|
|
nx_ctx->ap->databytelen/NX_PAGE_SIZE);
|
|
|
|
|
2014-10-29 01:49:46 +08:00
|
|
|
len = SHA512_DIGEST_SIZE;
|
2015-04-24 04:41:43 +08:00
|
|
|
out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
|
|
|
|
&len, max_sg_len);
|
|
|
|
nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
|
2014-10-29 01:49:46 +08:00
|
|
|
|
2015-04-24 04:41:43 +08:00
|
|
|
if (len != SHA512_DIGEST_SIZE)
|
|
|
|
return -EINVAL;
|
2014-10-29 01:49:46 +08:00
|
|
|
|
|
|
|
sctx->state[0] = __cpu_to_be64(SHA512_H0);
|
|
|
|
sctx->state[1] = __cpu_to_be64(SHA512_H1);
|
|
|
|
sctx->state[2] = __cpu_to_be64(SHA512_H2);
|
|
|
|
sctx->state[3] = __cpu_to_be64(SHA512_H3);
|
|
|
|
sctx->state[4] = __cpu_to_be64(SHA512_H4);
|
|
|
|
sctx->state[5] = __cpu_to_be64(SHA512_H5);
|
|
|
|
sctx->state[6] = __cpu_to_be64(SHA512_H6);
|
|
|
|
sctx->state[7] = __cpu_to_be64(SHA512_H7);
|
|
|
|
sctx->count[0] = 0;
|
|
|
|
|
2012-05-14 19:06:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nx_sha512_update(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int len)
|
|
|
|
{
|
|
|
|
struct sha512_state *sctx = shash_desc_ctx(desc);
|
|
|
|
struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
|
|
|
|
struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
|
2015-04-24 04:41:43 +08:00
|
|
|
struct nx_sg *in_sg;
|
2014-10-29 01:49:46 +08:00
|
|
|
u64 to_process, leftover = 0, total;
|
2013-08-13 05:49:37 +08:00
|
|
|
unsigned long irq_flags;
|
2012-05-14 19:06:31 +08:00
|
|
|
int rc = 0;
|
2014-10-29 01:49:46 +08:00
|
|
|
int data_len;
|
2015-04-24 04:41:43 +08:00
|
|
|
u32 max_sg_len;
|
2014-10-29 01:49:46 +08:00
|
|
|
u64 buf_len = (sctx->count[0] % SHA512_BLOCK_SIZE);
|
2012-05-14 19:06:31 +08:00
|
|
|
|
2013-08-13 05:49:37 +08:00
|
|
|
spin_lock_irqsave(&nx_ctx->lock, irq_flags);
|
|
|
|
|
2012-05-14 19:06:31 +08:00
|
|
|
/* 2 cases for total data len:
|
2013-08-02 20:09:52 +08:00
|
|
|
* 1: < SHA512_BLOCK_SIZE: copy into state, return 0
|
|
|
|
* 2: >= SHA512_BLOCK_SIZE: process X blocks, copy in leftover
|
2012-05-14 19:06:31 +08:00
|
|
|
*/
|
2014-10-29 01:49:46 +08:00
|
|
|
total = (sctx->count[0] % SHA512_BLOCK_SIZE) + len;
|
2013-08-02 20:09:52 +08:00
|
|
|
if (total < SHA512_BLOCK_SIZE) {
|
2014-10-29 01:49:46 +08:00
|
|
|
memcpy(sctx->buf + buf_len, data, len);
|
2012-05-14 19:06:31 +08:00
|
|
|
sctx->count[0] += len;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:49:46 +08:00
|
|
|
memcpy(csbcpb->cpb.sha512.message_digest, sctx->state, SHA512_DIGEST_SIZE);
|
|
|
|
NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
|
|
|
|
NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
|
2013-08-02 20:09:52 +08:00
|
|
|
|
2015-04-24 04:41:43 +08:00
|
|
|
in_sg = nx_ctx->in_sg;
|
|
|
|
max_sg_len = min_t(u64, nx_ctx->ap->sglen,
|
|
|
|
nx_driver.of.max_sg_len/sizeof(struct nx_sg));
|
|
|
|
max_sg_len = min_t(u64, max_sg_len,
|
|
|
|
nx_ctx->ap->databytelen/NX_PAGE_SIZE);
|
|
|
|
|
2013-08-02 20:09:52 +08:00
|
|
|
do {
|
|
|
|
/*
|
|
|
|
* to_process: the SHA512_BLOCK_SIZE data chunk to process in
|
|
|
|
* this update. This value is also restricted by the sg list
|
|
|
|
* limits.
|
|
|
|
*/
|
2014-10-29 01:49:46 +08:00
|
|
|
to_process = total - leftover;
|
2013-08-02 20:09:52 +08:00
|
|
|
to_process = to_process & ~(SHA512_BLOCK_SIZE - 1);
|
|
|
|
leftover = total - to_process;
|
|
|
|
|
2014-10-29 01:49:46 +08:00
|
|
|
if (buf_len) {
|
|
|
|
data_len = buf_len;
|
2015-04-24 04:41:43 +08:00
|
|
|
in_sg = nx_build_sg_list(nx_ctx->in_sg,
|
|
|
|
(u8 *) sctx->buf,
|
|
|
|
&data_len, max_sg_len);
|
2014-10-29 01:49:46 +08:00
|
|
|
|
2015-04-24 04:41:43 +08:00
|
|
|
if (data_len != buf_len) {
|
|
|
|
rc = -EINVAL;
|
2014-10-29 01:49:46 +08:00
|
|
|
goto out;
|
2015-04-24 04:41:43 +08:00
|
|
|
}
|
2013-08-02 20:09:52 +08:00
|
|
|
}
|
2014-10-29 01:49:46 +08:00
|
|
|
|
|
|
|
data_len = to_process - buf_len;
|
2015-04-24 04:41:43 +08:00
|
|
|
in_sg = nx_build_sg_list(in_sg, (u8 *) data,
|
|
|
|
&data_len, max_sg_len);
|
2014-10-29 01:49:46 +08:00
|
|
|
|
2015-04-24 04:41:43 +08:00
|
|
|
nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
|
|
|
|
|
|
|
|
if (data_len != (to_process - buf_len)) {
|
|
|
|
rc = -EINVAL;
|
2014-10-29 01:49:46 +08:00
|
|
|
goto out;
|
2015-04-24 04:41:43 +08:00
|
|
|
}
|
2014-10-29 01:49:46 +08:00
|
|
|
|
|
|
|
to_process = (data_len + buf_len);
|
|
|
|
leftover = total - to_process;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we've hit the nx chip previously and we're updating
|
|
|
|
* again, so copy over the partial digest.
|
|
|
|
*/
|
|
|
|
memcpy(csbcpb->cpb.sha512.input_partial_digest,
|
2013-08-02 20:09:52 +08:00
|
|
|
csbcpb->cpb.sha512.message_digest,
|
|
|
|
SHA512_DIGEST_SIZE);
|
|
|
|
|
|
|
|
if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
|
|
|
|
desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
|
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
atomic_inc(&(nx_ctx->stats->sha512_ops));
|
|
|
|
|
|
|
|
total -= to_process;
|
2014-10-29 01:49:46 +08:00
|
|
|
data += to_process - buf_len;
|
|
|
|
buf_len = 0;
|
|
|
|
|
2013-08-02 20:09:52 +08:00
|
|
|
} while (leftover >= SHA512_BLOCK_SIZE);
|
2012-05-14 19:06:31 +08:00
|
|
|
|
|
|
|
/* copy the leftover back into the state struct */
|
2013-04-13 01:13:59 +08:00
|
|
|
if (leftover)
|
2013-08-02 20:09:52 +08:00
|
|
|
memcpy(sctx->buf, data, leftover);
|
2014-10-29 01:49:46 +08:00
|
|
|
sctx->count[0] += len;
|
|
|
|
memcpy(sctx->state, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
|
2012-05-14 19:06:31 +08:00
|
|
|
out:
|
2013-08-13 05:49:37 +08:00
|
|
|
spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
|
2012-05-14 19:06:31 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nx_sha512_final(struct shash_desc *desc, u8 *out)
|
|
|
|
{
|
|
|
|
struct sha512_state *sctx = shash_desc_ctx(desc);
|
|
|
|
struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
|
|
|
|
struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
|
2015-04-24 04:41:43 +08:00
|
|
|
struct nx_sg *in_sg, *out_sg;
|
|
|
|
u32 max_sg_len;
|
2012-05-14 19:06:31 +08:00
|
|
|
u64 count0;
|
2013-08-13 05:49:37 +08:00
|
|
|
unsigned long irq_flags;
|
2015-04-24 04:41:43 +08:00
|
|
|
int rc = 0;
|
2014-10-29 01:49:46 +08:00
|
|
|
int len;
|
2012-05-14 19:06:31 +08:00
|
|
|
|
2013-08-13 05:49:37 +08:00
|
|
|
spin_lock_irqsave(&nx_ctx->lock, irq_flags);
|
|
|
|
|
2015-04-24 04:41:43 +08:00
|
|
|
max_sg_len = min_t(u64, nx_ctx->ap->sglen,
|
|
|
|
nx_driver.of.max_sg_len/sizeof(struct nx_sg));
|
|
|
|
max_sg_len = min_t(u64, max_sg_len,
|
|
|
|
nx_ctx->ap->databytelen/NX_PAGE_SIZE);
|
|
|
|
|
2014-10-29 01:49:46 +08:00
|
|
|
/* final is represented by continuing the operation and indicating that
|
|
|
|
* this is not an intermediate operation */
|
|
|
|
if (sctx->count[0] >= SHA512_BLOCK_SIZE) {
|
2012-05-14 19:06:31 +08:00
|
|
|
/* we've hit the nx chip previously, now we're finalizing,
|
|
|
|
* so copy over the partial digest */
|
2014-10-29 01:49:46 +08:00
|
|
|
memcpy(csbcpb->cpb.sha512.input_partial_digest, sctx->state,
|
|
|
|
SHA512_DIGEST_SIZE);
|
|
|
|
NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
|
|
|
|
NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
|
|
|
|
} else {
|
|
|
|
NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
|
|
|
|
NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
|
2012-05-14 19:06:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
|
|
|
|
|
|
|
|
count0 = sctx->count[0] * 8;
|
|
|
|
|
2014-10-29 01:49:46 +08:00
|
|
|
csbcpb->cpb.sha512.message_bit_length_lo = count0;
|
2012-05-14 19:06:31 +08:00
|
|
|
|
2014-10-29 01:49:46 +08:00
|
|
|
len = sctx->count[0] & (SHA512_BLOCK_SIZE - 1);
|
2015-04-24 04:41:43 +08:00
|
|
|
in_sg = nx_build_sg_list(nx_ctx->in_sg, sctx->buf, &len,
|
|
|
|
max_sg_len);
|
2014-10-29 01:49:46 +08:00
|
|
|
|
2015-04-24 04:41:43 +08:00
|
|
|
if (len != (sctx->count[0] & (SHA512_BLOCK_SIZE - 1))) {
|
|
|
|
rc = -EINVAL;
|
2014-10-29 01:49:46 +08:00
|
|
|
goto out;
|
2015-04-24 04:41:43 +08:00
|
|
|
}
|
2014-10-29 01:49:46 +08:00
|
|
|
|
|
|
|
len = SHA512_DIGEST_SIZE;
|
2015-04-24 04:41:43 +08:00
|
|
|
out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len,
|
|
|
|
max_sg_len);
|
2014-10-29 01:49:46 +08:00
|
|
|
|
2015-04-24 04:41:43 +08:00
|
|
|
nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
|
|
|
|
nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
|
2012-05-14 19:06:31 +08:00
|
|
|
|
|
|
|
if (!nx_ctx->op.outlen) {
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
|
|
|
|
desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
|
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
atomic_inc(&(nx_ctx->stats->sha512_ops));
|
2014-10-29 01:49:46 +08:00
|
|
|
atomic64_add(sctx->count[0], &(nx_ctx->stats->sha512_bytes));
|
2012-05-14 19:06:31 +08:00
|
|
|
|
|
|
|
memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
|
|
|
|
out:
|
2013-08-13 05:49:37 +08:00
|
|
|
spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
|
2012-05-14 19:06:31 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nx_sha512_export(struct shash_desc *desc, void *out)
|
|
|
|
{
|
|
|
|
struct sha512_state *sctx = shash_desc_ctx(desc);
|
2013-08-13 05:49:37 +08:00
|
|
|
|
2014-10-29 01:49:46 +08:00
|
|
|
memcpy(out, sctx, sizeof(*sctx));
|
2012-05-14 19:06:31 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nx_sha512_import(struct shash_desc *desc, const void *in)
|
|
|
|
{
|
|
|
|
struct sha512_state *sctx = shash_desc_ctx(desc);
|
|
|
|
|
2014-10-29 01:49:46 +08:00
|
|
|
memcpy(sctx, in, sizeof(*sctx));
|
2012-05-14 19:06:31 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct shash_alg nx_shash_sha512_alg = {
|
|
|
|
.digestsize = SHA512_DIGEST_SIZE,
|
|
|
|
.init = nx_sha512_init,
|
|
|
|
.update = nx_sha512_update,
|
|
|
|
.final = nx_sha512_final,
|
|
|
|
.export = nx_sha512_export,
|
|
|
|
.import = nx_sha512_import,
|
|
|
|
.descsize = sizeof(struct sha512_state),
|
|
|
|
.statesize = sizeof(struct sha512_state),
|
|
|
|
.base = {
|
|
|
|
.cra_name = "sha512",
|
|
|
|
.cra_driver_name = "sha512-nx",
|
|
|
|
.cra_priority = 300,
|
|
|
|
.cra_flags = CRYPTO_ALG_TYPE_SHASH,
|
|
|
|
.cra_blocksize = SHA512_BLOCK_SIZE,
|
|
|
|
.cra_module = THIS_MODULE,
|
|
|
|
.cra_ctxsize = sizeof(struct nx_crypto_ctx),
|
|
|
|
.cra_init = nx_crypto_ctx_sha_init,
|
|
|
|
.cra_exit = nx_crypto_ctx_exit,
|
|
|
|
}
|
|
|
|
};
|