18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/**
38c2ecf20Sopenharmony_ci * AES XCBC 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/aes.h>
128c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/types.h>
158c2ecf20Sopenharmony_ci#include <linux/crypto.h>
168c2ecf20Sopenharmony_ci#include <asm/vio.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include "nx_csbcpb.h"
198c2ecf20Sopenharmony_ci#include "nx.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct xcbc_state {
238c2ecf20Sopenharmony_ci	u8 state[AES_BLOCK_SIZE];
248c2ecf20Sopenharmony_ci	unsigned int count;
258c2ecf20Sopenharmony_ci	u8 buffer[AES_BLOCK_SIZE];
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic int nx_xcbc_set_key(struct crypto_shash *desc,
298c2ecf20Sopenharmony_ci			   const u8            *in_key,
308c2ecf20Sopenharmony_ci			   unsigned int         key_len)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_shash_ctx(desc);
338c2ecf20Sopenharmony_ci	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	switch (key_len) {
368c2ecf20Sopenharmony_ci	case AES_KEYSIZE_128:
378c2ecf20Sopenharmony_ci		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
388c2ecf20Sopenharmony_ci		break;
398c2ecf20Sopenharmony_ci	default:
408c2ecf20Sopenharmony_ci		return -EINVAL;
418c2ecf20Sopenharmony_ci	}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	memcpy(csbcpb->cpb.aes_xcbc.key, in_key, key_len);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	return 0;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/*
498c2ecf20Sopenharmony_ci * Based on RFC 3566, for a zero-length message:
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * n = 1
528c2ecf20Sopenharmony_ci * K1 = E(K, 0x01010101010101010101010101010101)
538c2ecf20Sopenharmony_ci * K3 = E(K, 0x03030303030303030303030303030303)
548c2ecf20Sopenharmony_ci * E[0] = 0x00000000000000000000000000000000
558c2ecf20Sopenharmony_ci * M[1] = 0x80000000000000000000000000000000 (0 length message with padding)
568c2ecf20Sopenharmony_ci * E[1] = (K1, M[1] ^ E[0] ^ K3)
578c2ecf20Sopenharmony_ci * Tag = M[1]
588c2ecf20Sopenharmony_ci */
598c2ecf20Sopenharmony_cistatic int nx_xcbc_empty(struct shash_desc *desc, u8 *out)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
628c2ecf20Sopenharmony_ci	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
638c2ecf20Sopenharmony_ci	struct nx_sg *in_sg, *out_sg;
648c2ecf20Sopenharmony_ci	u8 keys[2][AES_BLOCK_SIZE];
658c2ecf20Sopenharmony_ci	u8 key[32];
668c2ecf20Sopenharmony_ci	int rc = 0;
678c2ecf20Sopenharmony_ci	int len;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/* Change to ECB mode */
708c2ecf20Sopenharmony_ci	csbcpb->cpb.hdr.mode = NX_MODE_AES_ECB;
718c2ecf20Sopenharmony_ci	memcpy(key, csbcpb->cpb.aes_xcbc.key, AES_BLOCK_SIZE);
728c2ecf20Sopenharmony_ci	memcpy(csbcpb->cpb.aes_ecb.key, key, AES_BLOCK_SIZE);
738c2ecf20Sopenharmony_ci	NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	/* K1 and K3 base patterns */
768c2ecf20Sopenharmony_ci	memset(keys[0], 0x01, sizeof(keys[0]));
778c2ecf20Sopenharmony_ci	memset(keys[1], 0x03, sizeof(keys[1]));
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	len = sizeof(keys);
808c2ecf20Sopenharmony_ci	/* Generate K1 and K3 encrypting the patterns */
818c2ecf20Sopenharmony_ci	in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) keys, &len,
828c2ecf20Sopenharmony_ci				 nx_ctx->ap->sglen);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	if (len != sizeof(keys))
858c2ecf20Sopenharmony_ci		return -EINVAL;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *) keys, &len,
888c2ecf20Sopenharmony_ci				  nx_ctx->ap->sglen);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	if (len != sizeof(keys))
918c2ecf20Sopenharmony_ci		return -EINVAL;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
948c2ecf20Sopenharmony_ci	nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
978c2ecf20Sopenharmony_ci	if (rc)
988c2ecf20Sopenharmony_ci		goto out;
998c2ecf20Sopenharmony_ci	atomic_inc(&(nx_ctx->stats->aes_ops));
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/* XOr K3 with the padding for a 0 length message */
1028c2ecf20Sopenharmony_ci	keys[1][0] ^= 0x80;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	len = sizeof(keys[1]);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	/* Encrypt the final result */
1078c2ecf20Sopenharmony_ci	memcpy(csbcpb->cpb.aes_ecb.key, keys[0], AES_BLOCK_SIZE);
1088c2ecf20Sopenharmony_ci	in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) keys[1], &len,
1098c2ecf20Sopenharmony_ci				 nx_ctx->ap->sglen);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	if (len != sizeof(keys[1]))
1128c2ecf20Sopenharmony_ci		return -EINVAL;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	len = AES_BLOCK_SIZE;
1158c2ecf20Sopenharmony_ci	out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len,
1168c2ecf20Sopenharmony_ci				  nx_ctx->ap->sglen);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (len != AES_BLOCK_SIZE)
1198c2ecf20Sopenharmony_ci		return -EINVAL;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
1228c2ecf20Sopenharmony_ci	nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
1258c2ecf20Sopenharmony_ci	if (rc)
1268c2ecf20Sopenharmony_ci		goto out;
1278c2ecf20Sopenharmony_ci	atomic_inc(&(nx_ctx->stats->aes_ops));
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciout:
1308c2ecf20Sopenharmony_ci	/* Restore XCBC mode */
1318c2ecf20Sopenharmony_ci	csbcpb->cpb.hdr.mode = NX_MODE_AES_XCBC_MAC;
1328c2ecf20Sopenharmony_ci	memcpy(csbcpb->cpb.aes_xcbc.key, key, AES_BLOCK_SIZE);
1338c2ecf20Sopenharmony_ci	NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return rc;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int nx_crypto_ctx_aes_xcbc_init2(struct crypto_tfm *tfm)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
1418c2ecf20Sopenharmony_ci	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
1428c2ecf20Sopenharmony_ci	int err;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	err = nx_crypto_ctx_aes_xcbc_init(tfm);
1458c2ecf20Sopenharmony_ci	if (err)
1468c2ecf20Sopenharmony_ci		return err;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	nx_ctx_init(nx_ctx, HCOP_FC_AES);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
1518c2ecf20Sopenharmony_ci	csbcpb->cpb.hdr.mode = NX_MODE_AES_XCBC_MAC;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	return 0;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic int nx_xcbc_init(struct shash_desc *desc)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	struct xcbc_state *sctx = shash_desc_ctx(desc);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	memset(sctx, 0, sizeof *sctx);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return 0;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic int nx_xcbc_update(struct shash_desc *desc,
1668c2ecf20Sopenharmony_ci			  const u8          *data,
1678c2ecf20Sopenharmony_ci			  unsigned int       len)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct xcbc_state *sctx = shash_desc_ctx(desc);
1708c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
1718c2ecf20Sopenharmony_ci	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
1728c2ecf20Sopenharmony_ci	struct nx_sg *in_sg;
1738c2ecf20Sopenharmony_ci	struct nx_sg *out_sg;
1748c2ecf20Sopenharmony_ci	u32 to_process = 0, leftover, total;
1758c2ecf20Sopenharmony_ci	unsigned int max_sg_len;
1768c2ecf20Sopenharmony_ci	unsigned long irq_flags;
1778c2ecf20Sopenharmony_ci	int rc = 0;
1788c2ecf20Sopenharmony_ci	int data_len;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	total = sctx->count + len;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/* 2 cases for total data len:
1868c2ecf20Sopenharmony_ci	 *  1: <= AES_BLOCK_SIZE: copy into state, return 0
1878c2ecf20Sopenharmony_ci	 *  2: > AES_BLOCK_SIZE: process X blocks, copy in leftover
1888c2ecf20Sopenharmony_ci	 */
1898c2ecf20Sopenharmony_ci	if (total <= AES_BLOCK_SIZE) {
1908c2ecf20Sopenharmony_ci		memcpy(sctx->buffer + sctx->count, data, len);
1918c2ecf20Sopenharmony_ci		sctx->count += len;
1928c2ecf20Sopenharmony_ci		goto out;
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	in_sg = nx_ctx->in_sg;
1968c2ecf20Sopenharmony_ci	max_sg_len = min_t(u64, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
1978c2ecf20Sopenharmony_ci				nx_ctx->ap->sglen);
1988c2ecf20Sopenharmony_ci	max_sg_len = min_t(u64, max_sg_len,
1998c2ecf20Sopenharmony_ci				nx_ctx->ap->databytelen/NX_PAGE_SIZE);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	data_len = AES_BLOCK_SIZE;
2028c2ecf20Sopenharmony_ci	out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
2038c2ecf20Sopenharmony_ci				  &len, nx_ctx->ap->sglen);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	if (data_len != AES_BLOCK_SIZE) {
2068c2ecf20Sopenharmony_ci		rc = -EINVAL;
2078c2ecf20Sopenharmony_ci		goto out;
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	do {
2138c2ecf20Sopenharmony_ci		to_process = total - to_process;
2148c2ecf20Sopenharmony_ci		to_process = to_process & ~(AES_BLOCK_SIZE - 1);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci		leftover = total - to_process;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci		/* the hardware will not accept a 0 byte operation for this
2198c2ecf20Sopenharmony_ci		 * algorithm and the operation MUST be finalized to be correct.
2208c2ecf20Sopenharmony_ci		 * So if we happen to get an update that falls on a block sized
2218c2ecf20Sopenharmony_ci		 * boundary, we must save off the last block to finalize with
2228c2ecf20Sopenharmony_ci		 * later. */
2238c2ecf20Sopenharmony_ci		if (!leftover) {
2248c2ecf20Sopenharmony_ci			to_process -= AES_BLOCK_SIZE;
2258c2ecf20Sopenharmony_ci			leftover = AES_BLOCK_SIZE;
2268c2ecf20Sopenharmony_ci		}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci		if (sctx->count) {
2298c2ecf20Sopenharmony_ci			data_len = sctx->count;
2308c2ecf20Sopenharmony_ci			in_sg = nx_build_sg_list(nx_ctx->in_sg,
2318c2ecf20Sopenharmony_ci						(u8 *) sctx->buffer,
2328c2ecf20Sopenharmony_ci						&data_len,
2338c2ecf20Sopenharmony_ci						max_sg_len);
2348c2ecf20Sopenharmony_ci			if (data_len != sctx->count) {
2358c2ecf20Sopenharmony_ci				rc = -EINVAL;
2368c2ecf20Sopenharmony_ci				goto out;
2378c2ecf20Sopenharmony_ci			}
2388c2ecf20Sopenharmony_ci		}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci		data_len = to_process - sctx->count;
2418c2ecf20Sopenharmony_ci		in_sg = nx_build_sg_list(in_sg,
2428c2ecf20Sopenharmony_ci					(u8 *) data,
2438c2ecf20Sopenharmony_ci					&data_len,
2448c2ecf20Sopenharmony_ci					max_sg_len);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci		if (data_len != to_process - sctx->count) {
2478c2ecf20Sopenharmony_ci			rc = -EINVAL;
2488c2ecf20Sopenharmony_ci			goto out;
2498c2ecf20Sopenharmony_ci		}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci		nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) *
2528c2ecf20Sopenharmony_ci					sizeof(struct nx_sg);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci		/* we've hit the nx chip previously and we're updating again,
2558c2ecf20Sopenharmony_ci		 * so copy over the partial digest */
2568c2ecf20Sopenharmony_ci		if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
2578c2ecf20Sopenharmony_ci			memcpy(csbcpb->cpb.aes_xcbc.cv,
2588c2ecf20Sopenharmony_ci				csbcpb->cpb.aes_xcbc.out_cv_mac,
2598c2ecf20Sopenharmony_ci				AES_BLOCK_SIZE);
2608c2ecf20Sopenharmony_ci		}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci		NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
2638c2ecf20Sopenharmony_ci		if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
2648c2ecf20Sopenharmony_ci			rc = -EINVAL;
2658c2ecf20Sopenharmony_ci			goto out;
2668c2ecf20Sopenharmony_ci		}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
2698c2ecf20Sopenharmony_ci		if (rc)
2708c2ecf20Sopenharmony_ci			goto out;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci		atomic_inc(&(nx_ctx->stats->aes_ops));
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci		/* everything after the first update is continuation */
2758c2ecf20Sopenharmony_ci		NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci		total -= to_process;
2788c2ecf20Sopenharmony_ci		data += to_process - sctx->count;
2798c2ecf20Sopenharmony_ci		sctx->count = 0;
2808c2ecf20Sopenharmony_ci		in_sg = nx_ctx->in_sg;
2818c2ecf20Sopenharmony_ci	} while (leftover > AES_BLOCK_SIZE);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	/* copy the leftover back into the state struct */
2848c2ecf20Sopenharmony_ci	memcpy(sctx->buffer, data, leftover);
2858c2ecf20Sopenharmony_ci	sctx->count = leftover;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ciout:
2888c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
2898c2ecf20Sopenharmony_ci	return rc;
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic int nx_xcbc_final(struct shash_desc *desc, u8 *out)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	struct xcbc_state *sctx = shash_desc_ctx(desc);
2958c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
2968c2ecf20Sopenharmony_ci	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
2978c2ecf20Sopenharmony_ci	struct nx_sg *in_sg, *out_sg;
2988c2ecf20Sopenharmony_ci	unsigned long irq_flags;
2998c2ecf20Sopenharmony_ci	int rc = 0;
3008c2ecf20Sopenharmony_ci	int len;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
3058c2ecf20Sopenharmony_ci		/* we've hit the nx chip previously, now we're finalizing,
3068c2ecf20Sopenharmony_ci		 * so copy over the partial digest */
3078c2ecf20Sopenharmony_ci		memcpy(csbcpb->cpb.aes_xcbc.cv,
3088c2ecf20Sopenharmony_ci		       csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE);
3098c2ecf20Sopenharmony_ci	} else if (sctx->count == 0) {
3108c2ecf20Sopenharmony_ci		/*
3118c2ecf20Sopenharmony_ci		 * we've never seen an update, so this is a 0 byte op. The
3128c2ecf20Sopenharmony_ci		 * hardware cannot handle a 0 byte op, so just ECB to
3138c2ecf20Sopenharmony_ci		 * generate the hash.
3148c2ecf20Sopenharmony_ci		 */
3158c2ecf20Sopenharmony_ci		rc = nx_xcbc_empty(desc, out);
3168c2ecf20Sopenharmony_ci		goto out;
3178c2ecf20Sopenharmony_ci	}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	/* final is represented by continuing the operation and indicating that
3208c2ecf20Sopenharmony_ci	 * this is not an intermediate operation */
3218c2ecf20Sopenharmony_ci	NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	len = sctx->count;
3248c2ecf20Sopenharmony_ci	in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)sctx->buffer,
3258c2ecf20Sopenharmony_ci				 &len, nx_ctx->ap->sglen);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	if (len != sctx->count) {
3288c2ecf20Sopenharmony_ci		rc = -EINVAL;
3298c2ecf20Sopenharmony_ci		goto out;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	len = AES_BLOCK_SIZE;
3338c2ecf20Sopenharmony_ci	out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len,
3348c2ecf20Sopenharmony_ci				  nx_ctx->ap->sglen);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	if (len != AES_BLOCK_SIZE) {
3378c2ecf20Sopenharmony_ci		rc = -EINVAL;
3388c2ecf20Sopenharmony_ci		goto out;
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
3428c2ecf20Sopenharmony_ci	nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	if (!nx_ctx->op.outlen) {
3458c2ecf20Sopenharmony_ci		rc = -EINVAL;
3468c2ecf20Sopenharmony_ci		goto out;
3478c2ecf20Sopenharmony_ci	}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
3508c2ecf20Sopenharmony_ci	if (rc)
3518c2ecf20Sopenharmony_ci		goto out;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	atomic_inc(&(nx_ctx->stats->aes_ops));
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	memcpy(out, csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE);
3568c2ecf20Sopenharmony_ciout:
3578c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
3588c2ecf20Sopenharmony_ci	return rc;
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cistruct shash_alg nx_shash_aes_xcbc_alg = {
3628c2ecf20Sopenharmony_ci	.digestsize = AES_BLOCK_SIZE,
3638c2ecf20Sopenharmony_ci	.init       = nx_xcbc_init,
3648c2ecf20Sopenharmony_ci	.update     = nx_xcbc_update,
3658c2ecf20Sopenharmony_ci	.final      = nx_xcbc_final,
3668c2ecf20Sopenharmony_ci	.setkey     = nx_xcbc_set_key,
3678c2ecf20Sopenharmony_ci	.descsize   = sizeof(struct xcbc_state),
3688c2ecf20Sopenharmony_ci	.statesize  = sizeof(struct xcbc_state),
3698c2ecf20Sopenharmony_ci	.base       = {
3708c2ecf20Sopenharmony_ci		.cra_name        = "xcbc(aes)",
3718c2ecf20Sopenharmony_ci		.cra_driver_name = "xcbc-aes-nx",
3728c2ecf20Sopenharmony_ci		.cra_priority    = 300,
3738c2ecf20Sopenharmony_ci		.cra_blocksize   = AES_BLOCK_SIZE,
3748c2ecf20Sopenharmony_ci		.cra_module      = THIS_MODULE,
3758c2ecf20Sopenharmony_ci		.cra_ctxsize     = sizeof(struct nx_crypto_ctx),
3768c2ecf20Sopenharmony_ci		.cra_init        = nx_crypto_ctx_aes_xcbc_init2,
3778c2ecf20Sopenharmony_ci		.cra_exit        = nx_crypto_ctx_exit,
3788c2ecf20Sopenharmony_ci	}
3798c2ecf20Sopenharmony_ci};
380