File Index
Files and directories
Sign in
FILE

nx-aes-ecb.c

File preview with syntax highlighting.

linux-6.19.14/drivers/crypto/nx/nx-aes-ecb.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * AES ECB routines supporting the Power 7+ Nest Accelerators driver
  4 *
  5 * Copyright (C) 2011-2012 International Business Machines Inc.
  6 *
  7 * Author: Kent Yoder <yoder1@us.ibm.com>
  8 */
  9
 10#include <crypto/aes.h>
 11#include <crypto/internal/skcipher.h>
 12#include <linux/err.h>
 13#include <linux/kernel.h>
 14#include <linux/module.h>
 15#include <linux/spinlock.h>
 16#include <linux/string.h>
 17#include <asm/vio.h>
 18
 19#include "nx_csbcpb.h"
 20#include "nx.h"
 21
 22
 23static int ecb_aes_nx_set_key(struct crypto_skcipher *tfm,
 24			      const u8               *in_key,
 25			      unsigned int            key_len)
 26{
 27	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
 28	struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
 29
 30	nx_ctx_init(nx_ctx, HCOP_FC_AES);
 31
 32	switch (key_len) {
 33	case AES_KEYSIZE_128:
 34		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
 35		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
 36		break;
 37	case AES_KEYSIZE_192:
 38		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
 39		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
 40		break;
 41	case AES_KEYSIZE_256:
 42		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
 43		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
 44		break;
 45	default:
 46		return -EINVAL;
 47	}
 48
 49	csbcpb->cpb.hdr.mode = NX_MODE_AES_ECB;
 50	memcpy(csbcpb->cpb.aes_ecb.key, in_key, key_len);
 51
 52	return 0;
 53}
 54
 55static int ecb_aes_nx_crypt(struct skcipher_request *req,
 56			    int                      enc)
 57{
 58	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
 59	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
 60	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
 61	unsigned long irq_flags;
 62	unsigned int processed = 0, to_process;
 63	int rc;
 64
 65	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
 66
 67	if (enc)
 68		NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
 69	else
 70		NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
 71
 72	do {
 73		to_process = req->cryptlen - processed;
 74
 75		rc = nx_build_sg_lists(nx_ctx, NULL, req->dst, req->src,
 76				       &to_process, processed, NULL);
 77		if (rc)
 78			goto out;
 79
 80		if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
 81			rc = -EINVAL;
 82			goto out;
 83		}
 84
 85		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
 86				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
 87		if (rc)
 88			goto out;
 89
 90		atomic_inc(&(nx_ctx->stats->aes_ops));
 91		atomic64_add(be32_to_cpu(csbcpb->csb.processed_byte_count),
 92			     &(nx_ctx->stats->aes_bytes));
 93
 94		processed += to_process;
 95	} while (processed < req->cryptlen);
 96
 97out:
 98	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
 99	return rc;
100}
101
102static int ecb_aes_nx_encrypt(struct skcipher_request *req)
103{
104	return ecb_aes_nx_crypt(req, 1);
105}
106
107static int ecb_aes_nx_decrypt(struct skcipher_request *req)
108{
109	return ecb_aes_nx_crypt(req, 0);
110}
111
112struct skcipher_alg nx_ecb_aes_alg = {
113	.base.cra_name		= "ecb(aes)",
114	.base.cra_driver_name	= "ecb-aes-nx",
115	.base.cra_priority	= 300,
116	.base.cra_blocksize	= AES_BLOCK_SIZE,
117	.base.cra_alignmask	= 0xf,
118	.base.cra_ctxsize	= sizeof(struct nx_crypto_ctx),
119	.base.cra_module	= THIS_MODULE,
120	.init			= nx_crypto_ctx_aes_ecb_init,
121	.exit			= nx_crypto_ctx_skcipher_exit,
122	.min_keysize		= AES_MIN_KEY_SIZE,
123	.max_keysize		= AES_MAX_KEY_SIZE,
124	.setkey			= ecb_aes_nx_set_key,
125	.encrypt		= ecb_aes_nx_encrypt,
126	.decrypt		= ecb_aes_nx_decrypt,
127};
~/ Read-only file preview.