18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/**
38c2ecf20Sopenharmony_ci * SHA-256 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#include <asm/byteorder.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "nx_csbcpb.h"
178c2ecf20Sopenharmony_ci#include "nx.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic int nx_crypto_ctx_sha256_init(struct crypto_tfm *tfm)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
238c2ecf20Sopenharmony_ci	int err;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	err = nx_crypto_ctx_sha_init(tfm);
268c2ecf20Sopenharmony_ci	if (err)
278c2ecf20Sopenharmony_ci		return err;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	nx_ctx_init(nx_ctx, HCOP_FC_SHA);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA256];
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA256);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	return 0;
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic int nx_sha256_init(struct shash_desc *desc) {
398c2ecf20Sopenharmony_ci	struct sha256_state *sctx = shash_desc_ctx(desc);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	memset(sctx, 0, sizeof *sctx);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	sctx->state[0] = __cpu_to_be32(SHA256_H0);
448c2ecf20Sopenharmony_ci	sctx->state[1] = __cpu_to_be32(SHA256_H1);
458c2ecf20Sopenharmony_ci	sctx->state[2] = __cpu_to_be32(SHA256_H2);
468c2ecf20Sopenharmony_ci	sctx->state[3] = __cpu_to_be32(SHA256_H3);
478c2ecf20Sopenharmony_ci	sctx->state[4] = __cpu_to_be32(SHA256_H4);
488c2ecf20Sopenharmony_ci	sctx->state[5] = __cpu_to_be32(SHA256_H5);
498c2ecf20Sopenharmony_ci	sctx->state[6] = __cpu_to_be32(SHA256_H6);
508c2ecf20Sopenharmony_ci	sctx->state[7] = __cpu_to_be32(SHA256_H7);
518c2ecf20Sopenharmony_ci	sctx->count = 0;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return 0;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int nx_sha256_update(struct shash_desc *desc, const u8 *data,
578c2ecf20Sopenharmony_ci			    unsigned int len)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct sha256_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 = 0, leftover, 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 % SHA256_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: < SHA256_BLOCK_SIZE: copy into state, return 0
748c2ecf20Sopenharmony_ci	 *  2: >= SHA256_BLOCK_SIZE: process X blocks, copy in leftover
758c2ecf20Sopenharmony_ci	 */
768c2ecf20Sopenharmony_ci	total = (sctx->count % SHA256_BLOCK_SIZE) + len;
778c2ecf20Sopenharmony_ci	if (total < SHA256_BLOCK_SIZE) {
788c2ecf20Sopenharmony_ci		memcpy(sctx->buf + buf_len, data, len);
798c2ecf20Sopenharmony_ci		sctx->count += len;
808c2ecf20Sopenharmony_ci		goto out;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	memcpy(csbcpb->cpb.sha256.message_digest, sctx->state, SHA256_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 = SHA256_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 != SHA256_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,
1118c2ecf20Sopenharmony_ci						 max_sg_len);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci			if (data_len != buf_len) {
1148c2ecf20Sopenharmony_ci				rc = -EINVAL;
1158c2ecf20Sopenharmony_ci				goto out;
1168c2ecf20Sopenharmony_ci			}
1178c2ecf20Sopenharmony_ci			used_sgs = in_sg - nx_ctx->in_sg;
1188c2ecf20Sopenharmony_ci		}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci		/* to_process: SHA256_BLOCK_SIZE aligned chunk to be
1218c2ecf20Sopenharmony_ci		 * processed in this iteration. This value is restricted
1228c2ecf20Sopenharmony_ci		 * by sg list limits and number of sgs we already used
1238c2ecf20Sopenharmony_ci		 * for leftover data. (see above)
1248c2ecf20Sopenharmony_ci		 * In ideal case, we could allow NX_PAGE_SIZE * max_sg_len,
1258c2ecf20Sopenharmony_ci		 * but because data may not be aligned, we need to account
1268c2ecf20Sopenharmony_ci		 * for that too. */
1278c2ecf20Sopenharmony_ci		to_process = min_t(u64, total,
1288c2ecf20Sopenharmony_ci			(max_sg_len - 1 - used_sgs) * NX_PAGE_SIZE);
1298c2ecf20Sopenharmony_ci		to_process = to_process & ~(SHA256_BLOCK_SIZE - 1);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci		data_len = to_process - buf_len;
1328c2ecf20Sopenharmony_ci		in_sg = nx_build_sg_list(in_sg, (u8 *) data,
1338c2ecf20Sopenharmony_ci					 &data_len, max_sg_len);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci		nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci		to_process = data_len + buf_len;
1388c2ecf20Sopenharmony_ci		leftover = total - to_process;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci		/*
1418c2ecf20Sopenharmony_ci		 * we've hit the nx chip previously and we're updating
1428c2ecf20Sopenharmony_ci		 * again, so copy over the partial digest.
1438c2ecf20Sopenharmony_ci		 */
1448c2ecf20Sopenharmony_ci		memcpy(csbcpb->cpb.sha256.input_partial_digest,
1458c2ecf20Sopenharmony_ci			       csbcpb->cpb.sha256.message_digest,
1468c2ecf20Sopenharmony_ci			       SHA256_DIGEST_SIZE);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci		if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
1498c2ecf20Sopenharmony_ci			rc = -EINVAL;
1508c2ecf20Sopenharmony_ci			goto out;
1518c2ecf20Sopenharmony_ci		}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
1548c2ecf20Sopenharmony_ci		if (rc)
1558c2ecf20Sopenharmony_ci			goto out;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci		atomic_inc(&(nx_ctx->stats->sha256_ops));
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci		total -= to_process;
1608c2ecf20Sopenharmony_ci		data += to_process - buf_len;
1618c2ecf20Sopenharmony_ci		buf_len = 0;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	} while (leftover >= SHA256_BLOCK_SIZE);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	/* copy the leftover back into the state struct */
1668c2ecf20Sopenharmony_ci	if (leftover)
1678c2ecf20Sopenharmony_ci		memcpy(sctx->buf, data, leftover);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	sctx->count += len;
1708c2ecf20Sopenharmony_ci	memcpy(sctx->state, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
1718c2ecf20Sopenharmony_ciout:
1728c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
1738c2ecf20Sopenharmony_ci	return rc;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic int nx_sha256_final(struct shash_desc *desc, u8 *out)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	struct sha256_state *sctx = shash_desc_ctx(desc);
1798c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
1808c2ecf20Sopenharmony_ci	struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
1818c2ecf20Sopenharmony_ci	struct nx_sg *in_sg, *out_sg;
1828c2ecf20Sopenharmony_ci	unsigned long irq_flags;
1838c2ecf20Sopenharmony_ci	u32 max_sg_len;
1848c2ecf20Sopenharmony_ci	int rc = 0;
1858c2ecf20Sopenharmony_ci	int len;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	max_sg_len = min_t(u64, nx_ctx->ap->sglen,
1908c2ecf20Sopenharmony_ci			nx_driver.of.max_sg_len/sizeof(struct nx_sg));
1918c2ecf20Sopenharmony_ci	max_sg_len = min_t(u64, max_sg_len,
1928c2ecf20Sopenharmony_ci			nx_ctx->ap->databytelen/NX_PAGE_SIZE);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	/* final is represented by continuing the operation and indicating that
1958c2ecf20Sopenharmony_ci	 * this is not an intermediate operation */
1968c2ecf20Sopenharmony_ci	if (sctx->count >= SHA256_BLOCK_SIZE) {
1978c2ecf20Sopenharmony_ci		/* we've hit the nx chip previously, now we're finalizing,
1988c2ecf20Sopenharmony_ci		 * so copy over the partial digest */
1998c2ecf20Sopenharmony_ci		memcpy(csbcpb->cpb.sha256.input_partial_digest, sctx->state, SHA256_DIGEST_SIZE);
2008c2ecf20Sopenharmony_ci		NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
2018c2ecf20Sopenharmony_ci		NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
2028c2ecf20Sopenharmony_ci	} else {
2038c2ecf20Sopenharmony_ci		NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
2048c2ecf20Sopenharmony_ci		NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	csbcpb->cpb.sha256.message_bit_length = (u64) (sctx->count * 8);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	len = sctx->count & (SHA256_BLOCK_SIZE - 1);
2108c2ecf20Sopenharmony_ci	in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) sctx->buf,
2118c2ecf20Sopenharmony_ci				 &len, max_sg_len);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	if (len != (sctx->count & (SHA256_BLOCK_SIZE - 1))) {
2148c2ecf20Sopenharmony_ci		rc = -EINVAL;
2158c2ecf20Sopenharmony_ci		goto out;
2168c2ecf20Sopenharmony_ci	}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	len = SHA256_DIGEST_SIZE;
2198c2ecf20Sopenharmony_ci	out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, max_sg_len);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	if (len != SHA256_DIGEST_SIZE) {
2228c2ecf20Sopenharmony_ci		rc = -EINVAL;
2238c2ecf20Sopenharmony_ci		goto out;
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
2278c2ecf20Sopenharmony_ci	nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
2288c2ecf20Sopenharmony_ci	if (!nx_ctx->op.outlen) {
2298c2ecf20Sopenharmony_ci		rc = -EINVAL;
2308c2ecf20Sopenharmony_ci		goto out;
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
2348c2ecf20Sopenharmony_ci	if (rc)
2358c2ecf20Sopenharmony_ci		goto out;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	atomic_inc(&(nx_ctx->stats->sha256_ops));
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	atomic64_add(sctx->count, &(nx_ctx->stats->sha256_bytes));
2408c2ecf20Sopenharmony_ci	memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE);
2418c2ecf20Sopenharmony_ciout:
2428c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
2438c2ecf20Sopenharmony_ci	return rc;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic int nx_sha256_export(struct shash_desc *desc, void *out)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	struct sha256_state *sctx = shash_desc_ctx(desc);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	memcpy(out, sctx, sizeof(*sctx));
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	return 0;
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic int nx_sha256_import(struct shash_desc *desc, const void *in)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	struct sha256_state *sctx = shash_desc_ctx(desc);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	memcpy(sctx, in, sizeof(*sctx));
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	return 0;
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistruct shash_alg nx_shash_sha256_alg = {
2658c2ecf20Sopenharmony_ci	.digestsize = SHA256_DIGEST_SIZE,
2668c2ecf20Sopenharmony_ci	.init       = nx_sha256_init,
2678c2ecf20Sopenharmony_ci	.update     = nx_sha256_update,
2688c2ecf20Sopenharmony_ci	.final      = nx_sha256_final,
2698c2ecf20Sopenharmony_ci	.export     = nx_sha256_export,
2708c2ecf20Sopenharmony_ci	.import     = nx_sha256_import,
2718c2ecf20Sopenharmony_ci	.descsize   = sizeof(struct sha256_state),
2728c2ecf20Sopenharmony_ci	.statesize  = sizeof(struct sha256_state),
2738c2ecf20Sopenharmony_ci	.base       = {
2748c2ecf20Sopenharmony_ci		.cra_name        = "sha256",
2758c2ecf20Sopenharmony_ci		.cra_driver_name = "sha256-nx",
2768c2ecf20Sopenharmony_ci		.cra_priority    = 300,
2778c2ecf20Sopenharmony_ci		.cra_blocksize   = SHA256_BLOCK_SIZE,
2788c2ecf20Sopenharmony_ci		.cra_module      = THIS_MODULE,
2798c2ecf20Sopenharmony_ci		.cra_ctxsize     = sizeof(struct nx_crypto_ctx),
2808c2ecf20Sopenharmony_ci		.cra_init        = nx_crypto_ctx_sha256_init,
2818c2ecf20Sopenharmony_ci		.cra_exit        = nx_crypto_ctx_exit,
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci};
284