18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/**
38c2ecf20Sopenharmony_ci * SHA-512 routines supporting the Power 7+ Nest Accelerators driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2011-2012 International Business Machines Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Kent Yoder <yoder1@us.ibm.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h>
118c2ecf20Sopenharmony_ci#include <crypto/sha.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <asm/vio.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "nx_csbcpb.h"
168c2ecf20Sopenharmony_ci#include "nx.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic int nx_crypto_ctx_sha512_init(struct crypto_tfm *tfm)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
228c2ecf20Sopenharmony_ci	int err;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	err = nx_crypto_ctx_sha_init(tfm);
258c2ecf20Sopenharmony_ci	if (err)
268c2ecf20Sopenharmony_ci		return err;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	nx_ctx_init(nx_ctx, HCOP_FC_SHA);
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA512];
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA512);
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	return 0;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int nx_sha512_init(struct shash_desc *desc)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	struct sha512_state *sctx = shash_desc_ctx(desc);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	memset(sctx, 0, sizeof *sctx);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	sctx->state[0] = __cpu_to_be64(SHA512_H0);
448c2ecf20Sopenharmony_ci	sctx->state[1] = __cpu_to_be64(SHA512_H1);
458c2ecf20Sopenharmony_ci	sctx->state[2] = __cpu_to_be64(SHA512_H2);
468c2ecf20Sopenharmony_ci	sctx->state[3] = __cpu_to_be64(SHA512_H3);
478c2ecf20Sopenharmony_ci	sctx->state[4] = __cpu_to_be64(SHA512_H4);
488c2ecf20Sopenharmony_ci	sctx->state[5] = __cpu_to_be64(SHA512_H5);
498c2ecf20Sopenharmony_ci	sctx->state[6] = __cpu_to_be64(SHA512_H6);
508c2ecf20Sopenharmony_ci	sctx->state[7] = __cpu_to_be64(SHA512_H7);
518c2ecf20Sopenharmony_ci	sctx->count[0] = 0;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return 0;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int nx_sha512_update(struct shash_desc *desc, const u8 *data,
578c2ecf20Sopenharmony_ci			    unsigned int len)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct sha512_state *sctx = shash_desc_ctx(desc);
608c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
618c2ecf20Sopenharmony_ci	struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
628c2ecf20Sopenharmony_ci	struct nx_sg *out_sg;
638c2ecf20Sopenharmony_ci	u64 to_process, leftover = 0, total;
648c2ecf20Sopenharmony_ci	unsigned long irq_flags;
658c2ecf20Sopenharmony_ci	int rc = 0;
668c2ecf20Sopenharmony_ci	int data_len;
678c2ecf20Sopenharmony_ci	u32 max_sg_len;
688c2ecf20Sopenharmony_ci	u64 buf_len = (sctx->count[0] % SHA512_BLOCK_SIZE);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/* 2 cases for total data len:
738c2ecf20Sopenharmony_ci	 *  1: < SHA512_BLOCK_SIZE: copy into state, return 0
748c2ecf20Sopenharmony_ci	 *  2: >= SHA512_BLOCK_SIZE: process X blocks, copy in leftover
758c2ecf20Sopenharmony_ci	 */
768c2ecf20Sopenharmony_ci	total = (sctx->count[0] % SHA512_BLOCK_SIZE) + len;
778c2ecf20Sopenharmony_ci	if (total < SHA512_BLOCK_SIZE) {
788c2ecf20Sopenharmony_ci		memcpy(sctx->buf + buf_len, data, len);
798c2ecf20Sopenharmony_ci		sctx->count[0] += len;
808c2ecf20Sopenharmony_ci		goto out;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	memcpy(csbcpb->cpb.sha512.message_digest, sctx->state, SHA512_DIGEST_SIZE);
848c2ecf20Sopenharmony_ci	NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
858c2ecf20Sopenharmony_ci	NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	max_sg_len = min_t(u64, nx_ctx->ap->sglen,
888c2ecf20Sopenharmony_ci			nx_driver.of.max_sg_len/sizeof(struct nx_sg));
898c2ecf20Sopenharmony_ci	max_sg_len = min_t(u64, max_sg_len,
908c2ecf20Sopenharmony_ci			nx_ctx->ap->databytelen/NX_PAGE_SIZE);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	data_len = SHA512_DIGEST_SIZE;
938c2ecf20Sopenharmony_ci	out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
948c2ecf20Sopenharmony_ci				  &data_len, max_sg_len);
958c2ecf20Sopenharmony_ci	nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	if (data_len != SHA512_DIGEST_SIZE) {
988c2ecf20Sopenharmony_ci		rc = -EINVAL;
998c2ecf20Sopenharmony_ci		goto out;
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	do {
1038c2ecf20Sopenharmony_ci		int used_sgs = 0;
1048c2ecf20Sopenharmony_ci		struct nx_sg *in_sg = nx_ctx->in_sg;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci		if (buf_len) {
1078c2ecf20Sopenharmony_ci			data_len = buf_len;
1088c2ecf20Sopenharmony_ci			in_sg = nx_build_sg_list(in_sg,
1098c2ecf20Sopenharmony_ci						 (u8 *) sctx->buf,
1108c2ecf20Sopenharmony_ci						 &data_len, max_sg_len);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci			if (data_len != buf_len) {
1138c2ecf20Sopenharmony_ci				rc = -EINVAL;
1148c2ecf20Sopenharmony_ci				goto out;
1158c2ecf20Sopenharmony_ci			}
1168c2ecf20Sopenharmony_ci			used_sgs = in_sg - nx_ctx->in_sg;
1178c2ecf20Sopenharmony_ci		}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci		/* to_process: SHA512_BLOCK_SIZE aligned chunk to be
1208c2ecf20Sopenharmony_ci		 * processed in this iteration. This value is restricted
1218c2ecf20Sopenharmony_ci		 * by sg list limits and number of sgs we already used
1228c2ecf20Sopenharmony_ci		 * for leftover data. (see above)
1238c2ecf20Sopenharmony_ci		 * In ideal case, we could allow NX_PAGE_SIZE * max_sg_len,
1248c2ecf20Sopenharmony_ci		 * but because data may not be aligned, we need to account
1258c2ecf20Sopenharmony_ci		 * for that too. */
1268c2ecf20Sopenharmony_ci		to_process = min_t(u64, total,
1278c2ecf20Sopenharmony_ci			(max_sg_len - 1 - used_sgs) * NX_PAGE_SIZE);
1288c2ecf20Sopenharmony_ci		to_process = to_process & ~(SHA512_BLOCK_SIZE - 1);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci		data_len = to_process - buf_len;
1318c2ecf20Sopenharmony_ci		in_sg = nx_build_sg_list(in_sg, (u8 *) data,
1328c2ecf20Sopenharmony_ci					 &data_len, max_sg_len);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci		if (data_len != (to_process - buf_len)) {
1378c2ecf20Sopenharmony_ci			rc = -EINVAL;
1388c2ecf20Sopenharmony_ci			goto out;
1398c2ecf20Sopenharmony_ci		}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		to_process = data_len + buf_len;
1428c2ecf20Sopenharmony_ci		leftover = total - to_process;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci		/*
1458c2ecf20Sopenharmony_ci		 * we've hit the nx chip previously and we're updating
1468c2ecf20Sopenharmony_ci		 * again, so copy over the partial digest.
1478c2ecf20Sopenharmony_ci		 */
1488c2ecf20Sopenharmony_ci		memcpy(csbcpb->cpb.sha512.input_partial_digest,
1498c2ecf20Sopenharmony_ci			       csbcpb->cpb.sha512.message_digest,
1508c2ecf20Sopenharmony_ci			       SHA512_DIGEST_SIZE);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci		if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
1538c2ecf20Sopenharmony_ci			rc = -EINVAL;
1548c2ecf20Sopenharmony_ci			goto out;
1558c2ecf20Sopenharmony_ci		}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
1588c2ecf20Sopenharmony_ci		if (rc)
1598c2ecf20Sopenharmony_ci			goto out;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci		atomic_inc(&(nx_ctx->stats->sha512_ops));
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci		total -= to_process;
1648c2ecf20Sopenharmony_ci		data += to_process - buf_len;
1658c2ecf20Sopenharmony_ci		buf_len = 0;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	} while (leftover >= SHA512_BLOCK_SIZE);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	/* copy the leftover back into the state struct */
1708c2ecf20Sopenharmony_ci	if (leftover)
1718c2ecf20Sopenharmony_ci		memcpy(sctx->buf, data, leftover);
1728c2ecf20Sopenharmony_ci	sctx->count[0] += len;
1738c2ecf20Sopenharmony_ci	memcpy(sctx->state, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
1748c2ecf20Sopenharmony_ciout:
1758c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
1768c2ecf20Sopenharmony_ci	return rc;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic int nx_sha512_final(struct shash_desc *desc, u8 *out)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	struct sha512_state *sctx = shash_desc_ctx(desc);
1828c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
1838c2ecf20Sopenharmony_ci	struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
1848c2ecf20Sopenharmony_ci	struct nx_sg *in_sg, *out_sg;
1858c2ecf20Sopenharmony_ci	u32 max_sg_len;
1868c2ecf20Sopenharmony_ci	u64 count0;
1878c2ecf20Sopenharmony_ci	unsigned long irq_flags;
1888c2ecf20Sopenharmony_ci	int rc = 0;
1898c2ecf20Sopenharmony_ci	int len;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	max_sg_len = min_t(u64, nx_ctx->ap->sglen,
1948c2ecf20Sopenharmony_ci			nx_driver.of.max_sg_len/sizeof(struct nx_sg));
1958c2ecf20Sopenharmony_ci	max_sg_len = min_t(u64, max_sg_len,
1968c2ecf20Sopenharmony_ci			nx_ctx->ap->databytelen/NX_PAGE_SIZE);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	/* final is represented by continuing the operation and indicating that
1998c2ecf20Sopenharmony_ci	 * this is not an intermediate operation */
2008c2ecf20Sopenharmony_ci	if (sctx->count[0] >= SHA512_BLOCK_SIZE) {
2018c2ecf20Sopenharmony_ci		/* we've hit the nx chip previously, now we're finalizing,
2028c2ecf20Sopenharmony_ci		 * so copy over the partial digest */
2038c2ecf20Sopenharmony_ci		memcpy(csbcpb->cpb.sha512.input_partial_digest, sctx->state,
2048c2ecf20Sopenharmony_ci							SHA512_DIGEST_SIZE);
2058c2ecf20Sopenharmony_ci		NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
2068c2ecf20Sopenharmony_ci		NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
2078c2ecf20Sopenharmony_ci	} else {
2088c2ecf20Sopenharmony_ci		NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
2098c2ecf20Sopenharmony_ci		NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	count0 = sctx->count[0] * 8;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	csbcpb->cpb.sha512.message_bit_length_lo = count0;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	len = sctx->count[0] & (SHA512_BLOCK_SIZE - 1);
2198c2ecf20Sopenharmony_ci	in_sg = nx_build_sg_list(nx_ctx->in_sg, sctx->buf, &len,
2208c2ecf20Sopenharmony_ci				 max_sg_len);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	if (len != (sctx->count[0] & (SHA512_BLOCK_SIZE - 1))) {
2238c2ecf20Sopenharmony_ci		rc = -EINVAL;
2248c2ecf20Sopenharmony_ci		goto out;
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	len = SHA512_DIGEST_SIZE;
2288c2ecf20Sopenharmony_ci	out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len,
2298c2ecf20Sopenharmony_ci				 max_sg_len);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
2328c2ecf20Sopenharmony_ci	nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	if (!nx_ctx->op.outlen) {
2358c2ecf20Sopenharmony_ci		rc = -EINVAL;
2368c2ecf20Sopenharmony_ci		goto out;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
2408c2ecf20Sopenharmony_ci	if (rc)
2418c2ecf20Sopenharmony_ci		goto out;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	atomic_inc(&(nx_ctx->stats->sha512_ops));
2448c2ecf20Sopenharmony_ci	atomic64_add(sctx->count[0], &(nx_ctx->stats->sha512_bytes));
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
2478c2ecf20Sopenharmony_ciout:
2488c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
2498c2ecf20Sopenharmony_ci	return rc;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic int nx_sha512_export(struct shash_desc *desc, void *out)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	struct sha512_state *sctx = shash_desc_ctx(desc);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	memcpy(out, sctx, sizeof(*sctx));
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	return 0;
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic int nx_sha512_import(struct shash_desc *desc, const void *in)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct sha512_state *sctx = shash_desc_ctx(desc);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	memcpy(sctx, in, sizeof(*sctx));
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	return 0;
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistruct shash_alg nx_shash_sha512_alg = {
2718c2ecf20Sopenharmony_ci	.digestsize = SHA512_DIGEST_SIZE,
2728c2ecf20Sopenharmony_ci	.init       = nx_sha512_init,
2738c2ecf20Sopenharmony_ci	.update     = nx_sha512_update,
2748c2ecf20Sopenharmony_ci	.final      = nx_sha512_final,
2758c2ecf20Sopenharmony_ci	.export     = nx_sha512_export,
2768c2ecf20Sopenharmony_ci	.import     = nx_sha512_import,
2778c2ecf20Sopenharmony_ci	.descsize   = sizeof(struct sha512_state),
2788c2ecf20Sopenharmony_ci	.statesize  = sizeof(struct sha512_state),
2798c2ecf20Sopenharmony_ci	.base       = {
2808c2ecf20Sopenharmony_ci		.cra_name        = "sha512",
2818c2ecf20Sopenharmony_ci		.cra_driver_name = "sha512-nx",
2828c2ecf20Sopenharmony_ci		.cra_priority    = 300,
2838c2ecf20Sopenharmony_ci		.cra_blocksize   = SHA512_BLOCK_SIZE,
2848c2ecf20Sopenharmony_ci		.cra_module      = THIS_MODULE,
2858c2ecf20Sopenharmony_ci		.cra_ctxsize     = sizeof(struct nx_crypto_ctx),
2868c2ecf20Sopenharmony_ci		.cra_init        = nx_crypto_ctx_sha512_init,
2878c2ecf20Sopenharmony_ci		.cra_exit        = nx_crypto_ctx_exit,
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci};
290