18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/**
38c2ecf20Sopenharmony_ci * AES CTR 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/aes.h>
118c2ecf20Sopenharmony_ci#include <crypto/ctr.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_cistatic int ctr_aes_nx_set_key(struct crypto_skcipher *tfm,
238c2ecf20Sopenharmony_ci			      const u8               *in_key,
248c2ecf20Sopenharmony_ci			      unsigned int            key_len)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
278c2ecf20Sopenharmony_ci	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	nx_ctx_init(nx_ctx, HCOP_FC_AES);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	switch (key_len) {
328c2ecf20Sopenharmony_ci	case AES_KEYSIZE_128:
338c2ecf20Sopenharmony_ci		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
348c2ecf20Sopenharmony_ci		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
358c2ecf20Sopenharmony_ci		break;
368c2ecf20Sopenharmony_ci	case AES_KEYSIZE_192:
378c2ecf20Sopenharmony_ci		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
388c2ecf20Sopenharmony_ci		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
398c2ecf20Sopenharmony_ci		break;
408c2ecf20Sopenharmony_ci	case AES_KEYSIZE_256:
418c2ecf20Sopenharmony_ci		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
428c2ecf20Sopenharmony_ci		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
438c2ecf20Sopenharmony_ci		break;
448c2ecf20Sopenharmony_ci	default:
458c2ecf20Sopenharmony_ci		return -EINVAL;
468c2ecf20Sopenharmony_ci	}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	csbcpb->cpb.hdr.mode = NX_MODE_AES_CTR;
498c2ecf20Sopenharmony_ci	memcpy(csbcpb->cpb.aes_ctr.key, in_key, key_len);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	return 0;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic int ctr3686_aes_nx_set_key(struct crypto_skcipher *tfm,
558c2ecf20Sopenharmony_ci				  const u8               *in_key,
568c2ecf20Sopenharmony_ci				  unsigned int            key_len)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	if (key_len < CTR_RFC3686_NONCE_SIZE)
618c2ecf20Sopenharmony_ci		return -EINVAL;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	memcpy(nx_ctx->priv.ctr.nonce,
648c2ecf20Sopenharmony_ci	       in_key + key_len - CTR_RFC3686_NONCE_SIZE,
658c2ecf20Sopenharmony_ci	       CTR_RFC3686_NONCE_SIZE);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	key_len -= CTR_RFC3686_NONCE_SIZE;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	return ctr_aes_nx_set_key(tfm, in_key, key_len);
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic int ctr_aes_nx_crypt(struct skcipher_request *req, u8 *iv)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
758c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
768c2ecf20Sopenharmony_ci	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
778c2ecf20Sopenharmony_ci	unsigned long irq_flags;
788c2ecf20Sopenharmony_ci	unsigned int processed = 0, to_process;
798c2ecf20Sopenharmony_ci	int rc;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	do {
848c2ecf20Sopenharmony_ci		to_process = req->cryptlen - processed;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci		rc = nx_build_sg_lists(nx_ctx, iv, req->dst, req->src,
878c2ecf20Sopenharmony_ci				       &to_process, processed,
888c2ecf20Sopenharmony_ci				       csbcpb->cpb.aes_ctr.iv);
898c2ecf20Sopenharmony_ci		if (rc)
908c2ecf20Sopenharmony_ci			goto out;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci		if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
938c2ecf20Sopenharmony_ci			rc = -EINVAL;
948c2ecf20Sopenharmony_ci			goto out;
958c2ecf20Sopenharmony_ci		}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
988c2ecf20Sopenharmony_ci				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
998c2ecf20Sopenharmony_ci		if (rc)
1008c2ecf20Sopenharmony_ci			goto out;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci		memcpy(iv, csbcpb->cpb.aes_cbc.cv, AES_BLOCK_SIZE);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		atomic_inc(&(nx_ctx->stats->aes_ops));
1058c2ecf20Sopenharmony_ci		atomic64_add(csbcpb->csb.processed_byte_count,
1068c2ecf20Sopenharmony_ci			     &(nx_ctx->stats->aes_bytes));
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci		processed += to_process;
1098c2ecf20Sopenharmony_ci	} while (processed < req->cryptlen);
1108c2ecf20Sopenharmony_ciout:
1118c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
1128c2ecf20Sopenharmony_ci	return rc;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic int ctr3686_aes_nx_crypt(struct skcipher_request *req)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1188c2ecf20Sopenharmony_ci	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
1198c2ecf20Sopenharmony_ci	u8 iv[16];
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_NONCE_SIZE);
1228c2ecf20Sopenharmony_ci	memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
1238c2ecf20Sopenharmony_ci	iv[12] = iv[13] = iv[14] = 0;
1248c2ecf20Sopenharmony_ci	iv[15] = 1;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	return ctr_aes_nx_crypt(req, iv);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistruct skcipher_alg nx_ctr3686_aes_alg = {
1308c2ecf20Sopenharmony_ci	.base.cra_name		= "rfc3686(ctr(aes))",
1318c2ecf20Sopenharmony_ci	.base.cra_driver_name	= "rfc3686-ctr-aes-nx",
1328c2ecf20Sopenharmony_ci	.base.cra_priority	= 300,
1338c2ecf20Sopenharmony_ci	.base.cra_blocksize	= 1,
1348c2ecf20Sopenharmony_ci	.base.cra_ctxsize	= sizeof(struct nx_crypto_ctx),
1358c2ecf20Sopenharmony_ci	.base.cra_module	= THIS_MODULE,
1368c2ecf20Sopenharmony_ci	.init			= nx_crypto_ctx_aes_ctr_init,
1378c2ecf20Sopenharmony_ci	.exit			= nx_crypto_ctx_skcipher_exit,
1388c2ecf20Sopenharmony_ci	.min_keysize		= AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
1398c2ecf20Sopenharmony_ci	.max_keysize		= AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
1408c2ecf20Sopenharmony_ci	.ivsize			= CTR_RFC3686_IV_SIZE,
1418c2ecf20Sopenharmony_ci	.setkey			= ctr3686_aes_nx_set_key,
1428c2ecf20Sopenharmony_ci	.encrypt		= ctr3686_aes_nx_crypt,
1438c2ecf20Sopenharmony_ci	.decrypt		= ctr3686_aes_nx_crypt,
1448c2ecf20Sopenharmony_ci	.chunksize		= AES_BLOCK_SIZE,
1458c2ecf20Sopenharmony_ci};
146