2019-05-29 07:18:13 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2021-03-21 18:00:07 +05:30
|
|
|
/*
|
2012-05-14 11:06:31 +00:00
|
|
|
* SHA-512 routines supporting the Power 7+ Nest Accelerators driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011-2012 International Business Machines Inc.
|
|
|
|
*
|
|
|
|
* Author: Kent Yoder <yoder1@us.ibm.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <crypto/internal/hash.h>
|
2020-11-12 21:20:21 -08:00
|
|
|
#include <crypto/sha2.h>
|
2025-04-18 11:01:11 +08:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/kernel.h>
|
2012-05-14 11:06:31 +00:00
|
|
|
#include <linux/module.h>
|
2025-04-18 11:01:11 +08:00
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/unaligned.h>
|
2012-05-14 11:06:31 +00:00
|
|
|
|
|
|
|
#include "nx_csbcpb.h"
|
|
|
|
#include "nx.h"
|
|
|
|
|
2021-06-17 16:00:12 +08:00
|
|
|
struct sha512_state_be {
|
|
|
|
__be64 state[SHA512_DIGEST_SIZE / 8];
|
|
|
|
u64 count[2];
|
|
|
|
};
|
2012-05-14 11:06:31 +00:00
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
static int nx_crypto_ctx_sha512_init(struct crypto_shash *tfm)
|
2012-05-14 11:06:31 +00:00
|
|
|
{
|
2025-04-18 11:01:11 +08:00
|
|
|
struct nx_crypto_ctx *nx_ctx = crypto_shash_ctx(tfm);
|
2015-07-07 17:30:25 +08:00
|
|
|
int err;
|
2012-05-14 11:06:31 +00:00
|
|
|
|
2015-07-07 17:30:25 +08:00
|
|
|
err = nx_crypto_ctx_sha_init(tfm);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2012-05-14 11:06:31 +00:00
|
|
|
|
2015-07-07 17:30:25 +08:00
|
|
|
nx_ctx_init(nx_ctx, HCOP_FC_SHA);
|
2012-05-14 11:06:31 +00:00
|
|
|
|
|
|
|
nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA512];
|
|
|
|
|
|
|
|
NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA512);
|
|
|
|
|
2015-07-07 17:30:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-04-23 17:41:43 -03:00
|
|
|
|
2015-07-07 17:30:25 +08:00
|
|
|
static int nx_sha512_init(struct shash_desc *desc)
|
|
|
|
{
|
2021-06-17 16:00:12 +08:00
|
|
|
struct sha512_state_be *sctx = shash_desc_ctx(desc);
|
2014-10-28 15:49:46 -02: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;
|
2025-04-18 11:01:11 +08:00
|
|
|
sctx->count[1] = 0;
|
2014-10-28 15:49:46 -02:00
|
|
|
|
2012-05-14 11:06:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nx_sha512_update(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int len)
|
|
|
|
{
|
2025-04-18 11:01:11 +08:00
|
|
|
struct nx_crypto_ctx *nx_ctx = crypto_shash_ctx(desc->tfm);
|
2021-06-17 16:00:12 +08:00
|
|
|
struct sha512_state_be *sctx = shash_desc_ctx(desc);
|
2012-05-14 11:06:31 +00:00
|
|
|
struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
|
2025-04-18 11:01:11 +08:00
|
|
|
u64 to_process, leftover, total = len;
|
2015-07-07 17:30:25 +08:00
|
|
|
struct nx_sg *out_sg;
|
2013-08-12 18:49:37 -03:00
|
|
|
unsigned long irq_flags;
|
2012-05-14 11:06:31 +00:00
|
|
|
int rc = 0;
|
2014-10-28 15:49:46 -02:00
|
|
|
int data_len;
|
2015-04-23 17:41:43 -03:00
|
|
|
u32 max_sg_len;
|
2012-05-14 11:06:31 +00:00
|
|
|
|
2013-08-12 18:49:37 -03:00
|
|
|
spin_lock_irqsave(&nx_ctx->lock, irq_flags);
|
|
|
|
|
2014-10-28 15:49:46 -02: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 12:09:52 +00:00
|
|
|
|
2015-04-23 17:41:43 -03: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);
|
|
|
|
|
2015-07-07 17:30:25 +08:00
|
|
|
data_len = SHA512_DIGEST_SIZE;
|
|
|
|
out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
|
|
|
|
&data_len, max_sg_len);
|
|
|
|
nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
|
|
|
|
|
|
|
|
if (data_len != SHA512_DIGEST_SIZE) {
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-08-02 12:09:52 +00:00
|
|
|
do {
|
2015-08-08 08:47:28 +02:00
|
|
|
struct nx_sg *in_sg = nx_ctx->in_sg;
|
2013-08-02 12:09:52 +00:00
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
to_process = total & ~(SHA512_BLOCK_SIZE - 1);
|
2014-10-28 15:49:46 -02:00
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
data_len = to_process;
|
2015-04-23 17:41:43 -03:00
|
|
|
in_sg = nx_build_sg_list(in_sg, (u8 *) data,
|
|
|
|
&data_len, max_sg_len);
|
2014-10-28 15:49:46 -02:00
|
|
|
|
2015-04-23 17:41:43 -03:00
|
|
|
nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
|
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
to_process = data_len;
|
2014-10-28 15:49:46 -02:00
|
|
|
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 12:09:52 +00:00
|
|
|
csbcpb->cpb.sha512.message_digest,
|
|
|
|
SHA512_DIGEST_SIZE);
|
|
|
|
|
|
|
|
if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-04-14 17:37:08 -07:00
|
|
|
rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
|
2013-08-02 12:09:52 +00:00
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
atomic_inc(&(nx_ctx->stats->sha512_ops));
|
|
|
|
|
|
|
|
total -= to_process;
|
2025-04-18 11:01:11 +08:00
|
|
|
data += to_process;
|
|
|
|
sctx->count[0] += to_process;
|
|
|
|
if (sctx->count[0] < to_process)
|
|
|
|
sctx->count[1]++;
|
2013-08-02 12:09:52 +00:00
|
|
|
} while (leftover >= SHA512_BLOCK_SIZE);
|
2012-05-14 11:06:31 +00:00
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
rc = leftover;
|
2014-10-28 15:49:46 -02:00
|
|
|
memcpy(sctx->state, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
|
2012-05-14 11:06:31 +00:00
|
|
|
out:
|
2013-08-12 18:49:37 -03:00
|
|
|
spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
|
2012-05-14 11:06:31 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
static int nx_sha512_finup(struct shash_desc *desc, const u8 *src,
|
|
|
|
unsigned int nbytes, u8 *out)
|
2012-05-14 11:06:31 +00:00
|
|
|
{
|
2021-06-17 16:00:12 +08:00
|
|
|
struct sha512_state_be *sctx = shash_desc_ctx(desc);
|
2025-04-18 11:01:11 +08:00
|
|
|
struct nx_crypto_ctx *nx_ctx = crypto_shash_ctx(desc->tfm);
|
2012-05-14 11:06:31 +00:00
|
|
|
struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
|
2015-04-23 17:41:43 -03:00
|
|
|
struct nx_sg *in_sg, *out_sg;
|
|
|
|
u32 max_sg_len;
|
2013-08-12 18:49:37 -03:00
|
|
|
unsigned long irq_flags;
|
2025-04-18 11:01:11 +08:00
|
|
|
u64 count0, count1;
|
2015-04-23 17:41:43 -03:00
|
|
|
int rc = 0;
|
2014-10-28 15:49:46 -02:00
|
|
|
int len;
|
2012-05-14 11:06:31 +00:00
|
|
|
|
2013-08-12 18:49:37 -03:00
|
|
|
spin_lock_irqsave(&nx_ctx->lock, irq_flags);
|
|
|
|
|
2015-04-23 17:41:43 -03: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-28 15:49:46 -02:00
|
|
|
/* final is represented by continuing the operation and indicating that
|
2025-04-18 11:01:11 +08:00
|
|
|
* this is not an intermediate operation
|
|
|
|
* copy over the partial digest */
|
|
|
|
memcpy(csbcpb->cpb.sha512.input_partial_digest, sctx->state, SHA512_DIGEST_SIZE);
|
2012-05-14 11:06:31 +00:00
|
|
|
NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
|
2025-04-18 11:01:11 +08:00
|
|
|
NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
|
2012-05-14 11:06:31 +00:00
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
count0 = sctx->count[0] + nbytes;
|
|
|
|
count1 = sctx->count[1];
|
2012-05-14 11:06:31 +00:00
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
csbcpb->cpb.sha512.message_bit_length_lo = count0 << 3;
|
|
|
|
csbcpb->cpb.sha512.message_bit_length_hi = (count1 << 3) |
|
|
|
|
(count0 >> 61);
|
2012-05-14 11:06:31 +00:00
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
len = nbytes;
|
|
|
|
in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)src, &len, max_sg_len);
|
2014-10-28 15:49:46 -02:00
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
if (len != nbytes) {
|
2015-04-23 17:41:43 -03:00
|
|
|
rc = -EINVAL;
|
2014-10-28 15:49:46 -02:00
|
|
|
goto out;
|
2015-04-23 17:41:43 -03:00
|
|
|
}
|
2014-10-28 15:49:46 -02:00
|
|
|
|
|
|
|
len = SHA512_DIGEST_SIZE;
|
2015-04-23 17:41:43 -03:00
|
|
|
out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len,
|
|
|
|
max_sg_len);
|
2014-10-28 15:49:46 -02:00
|
|
|
|
2015-04-23 17:41:43 -03: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 11:06:31 +00:00
|
|
|
|
|
|
|
if (!nx_ctx->op.outlen) {
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-04-14 17:37:08 -07:00
|
|
|
rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
|
2012-05-14 11:06:31 +00:00
|
|
|
if (rc)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
atomic_inc(&(nx_ctx->stats->sha512_ops));
|
2025-04-18 11:01:11 +08:00
|
|
|
atomic64_add(count0, &(nx_ctx->stats->sha512_bytes));
|
2012-05-14 11:06:31 +00:00
|
|
|
|
|
|
|
memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
|
|
|
|
out:
|
2013-08-12 18:49:37 -03:00
|
|
|
spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
|
2012-05-14 11:06:31 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nx_sha512_export(struct shash_desc *desc, void *out)
|
|
|
|
{
|
2021-06-17 16:00:12 +08:00
|
|
|
struct sha512_state_be *sctx = shash_desc_ctx(desc);
|
2025-04-18 11:01:11 +08:00
|
|
|
union {
|
|
|
|
u8 *u8;
|
|
|
|
u64 *u64;
|
|
|
|
} p = { .u8 = out };
|
|
|
|
int i;
|
2013-08-12 18:49:37 -03:00
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
for (i = 0; i < SHA512_DIGEST_SIZE / sizeof(*p.u64); i++)
|
|
|
|
put_unaligned(be64_to_cpu(sctx->state[i]), p.u64++);
|
2012-05-14 11:06:31 +00:00
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
put_unaligned(sctx->count[0], p.u64++);
|
|
|
|
put_unaligned(sctx->count[1], p.u64++);
|
2012-05-14 11:06:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nx_sha512_import(struct shash_desc *desc, const void *in)
|
|
|
|
{
|
2021-06-17 16:00:12 +08:00
|
|
|
struct sha512_state_be *sctx = shash_desc_ctx(desc);
|
2025-04-18 11:01:11 +08:00
|
|
|
union {
|
|
|
|
const u8 *u8;
|
|
|
|
const u64 *u64;
|
|
|
|
} p = { .u8 = in };
|
|
|
|
int i;
|
2012-05-14 11:06:31 +00:00
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
for (i = 0; i < SHA512_DIGEST_SIZE / sizeof(*p.u64); i++)
|
|
|
|
sctx->state[i] = cpu_to_be64(get_unaligned(p.u64++));
|
2012-05-14 11:06:31 +00:00
|
|
|
|
2025-04-18 11:01:11 +08:00
|
|
|
sctx->count[0] = get_unaligned(p.u64++);
|
|
|
|
sctx->count[1] = get_unaligned(p.u64++);
|
2012-05-14 11:06:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct shash_alg nx_shash_sha512_alg = {
|
|
|
|
.digestsize = SHA512_DIGEST_SIZE,
|
|
|
|
.init = nx_sha512_init,
|
|
|
|
.update = nx_sha512_update,
|
2025-04-18 11:01:11 +08:00
|
|
|
.finup = nx_sha512_finup,
|
2012-05-14 11:06:31 +00:00
|
|
|
.export = nx_sha512_export,
|
|
|
|
.import = nx_sha512_import,
|
2025-04-18 11:01:11 +08:00
|
|
|
.init_tfm = nx_crypto_ctx_sha512_init,
|
|
|
|
.exit_tfm = nx_crypto_ctx_shash_exit,
|
2021-06-17 16:00:12 +08:00
|
|
|
.descsize = sizeof(struct sha512_state_be),
|
|
|
|
.statesize = sizeof(struct sha512_state_be),
|
2012-05-14 11:06:31 +00:00
|
|
|
.base = {
|
|
|
|
.cra_name = "sha512",
|
|
|
|
.cra_driver_name = "sha512-nx",
|
|
|
|
.cra_priority = 300,
|
2025-04-18 11:01:11 +08:00
|
|
|
.cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY,
|
2012-05-14 11:06:31 +00:00
|
|
|
.cra_blocksize = SHA512_BLOCK_SIZE,
|
|
|
|
.cra_module = THIS_MODULE,
|
|
|
|
.cra_ctxsize = sizeof(struct nx_crypto_ctx),
|
|
|
|
}
|
|
|
|
};
|