xref: /OK3568_Linux_fs/kernel/drivers/crypto/nx/nx-aes-ctr.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /**
3*4882a593Smuzhiyun  * AES CTR routines supporting the Power 7+ Nest Accelerators driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2011-2012 International Business Machines Inc.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Kent Yoder <yoder1@us.ibm.com>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <crypto/aes.h>
11*4882a593Smuzhiyun #include <crypto/ctr.h>
12*4882a593Smuzhiyun #include <crypto/algapi.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/types.h>
15*4882a593Smuzhiyun #include <linux/crypto.h>
16*4882a593Smuzhiyun #include <asm/vio.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include "nx_csbcpb.h"
19*4882a593Smuzhiyun #include "nx.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun 
ctr_aes_nx_set_key(struct crypto_skcipher * tfm,const u8 * in_key,unsigned int key_len)22*4882a593Smuzhiyun static int ctr_aes_nx_set_key(struct crypto_skcipher *tfm,
23*4882a593Smuzhiyun 			      const u8               *in_key,
24*4882a593Smuzhiyun 			      unsigned int            key_len)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
27*4882a593Smuzhiyun 	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	nx_ctx_init(nx_ctx, HCOP_FC_AES);
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	switch (key_len) {
32*4882a593Smuzhiyun 	case AES_KEYSIZE_128:
33*4882a593Smuzhiyun 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
34*4882a593Smuzhiyun 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
35*4882a593Smuzhiyun 		break;
36*4882a593Smuzhiyun 	case AES_KEYSIZE_192:
37*4882a593Smuzhiyun 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
38*4882a593Smuzhiyun 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
39*4882a593Smuzhiyun 		break;
40*4882a593Smuzhiyun 	case AES_KEYSIZE_256:
41*4882a593Smuzhiyun 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
42*4882a593Smuzhiyun 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
43*4882a593Smuzhiyun 		break;
44*4882a593Smuzhiyun 	default:
45*4882a593Smuzhiyun 		return -EINVAL;
46*4882a593Smuzhiyun 	}
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	csbcpb->cpb.hdr.mode = NX_MODE_AES_CTR;
49*4882a593Smuzhiyun 	memcpy(csbcpb->cpb.aes_ctr.key, in_key, key_len);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	return 0;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
ctr3686_aes_nx_set_key(struct crypto_skcipher * tfm,const u8 * in_key,unsigned int key_len)54*4882a593Smuzhiyun static int ctr3686_aes_nx_set_key(struct crypto_skcipher *tfm,
55*4882a593Smuzhiyun 				  const u8               *in_key,
56*4882a593Smuzhiyun 				  unsigned int            key_len)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	if (key_len < CTR_RFC3686_NONCE_SIZE)
61*4882a593Smuzhiyun 		return -EINVAL;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	memcpy(nx_ctx->priv.ctr.nonce,
64*4882a593Smuzhiyun 	       in_key + key_len - CTR_RFC3686_NONCE_SIZE,
65*4882a593Smuzhiyun 	       CTR_RFC3686_NONCE_SIZE);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	key_len -= CTR_RFC3686_NONCE_SIZE;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	return ctr_aes_nx_set_key(tfm, in_key, key_len);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
ctr_aes_nx_crypt(struct skcipher_request * req,u8 * iv)72*4882a593Smuzhiyun static int ctr_aes_nx_crypt(struct skcipher_request *req, u8 *iv)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
75*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
76*4882a593Smuzhiyun 	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
77*4882a593Smuzhiyun 	unsigned long irq_flags;
78*4882a593Smuzhiyun 	unsigned int processed = 0, to_process;
79*4882a593Smuzhiyun 	int rc;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	do {
84*4882a593Smuzhiyun 		to_process = req->cryptlen - processed;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 		rc = nx_build_sg_lists(nx_ctx, iv, req->dst, req->src,
87*4882a593Smuzhiyun 				       &to_process, processed,
88*4882a593Smuzhiyun 				       csbcpb->cpb.aes_ctr.iv);
89*4882a593Smuzhiyun 		if (rc)
90*4882a593Smuzhiyun 			goto out;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 		if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
93*4882a593Smuzhiyun 			rc = -EINVAL;
94*4882a593Smuzhiyun 			goto out;
95*4882a593Smuzhiyun 		}
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 		rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
98*4882a593Smuzhiyun 				   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
99*4882a593Smuzhiyun 		if (rc)
100*4882a593Smuzhiyun 			goto out;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 		memcpy(iv, csbcpb->cpb.aes_cbc.cv, AES_BLOCK_SIZE);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 		atomic_inc(&(nx_ctx->stats->aes_ops));
105*4882a593Smuzhiyun 		atomic64_add(csbcpb->csb.processed_byte_count,
106*4882a593Smuzhiyun 			     &(nx_ctx->stats->aes_bytes));
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 		processed += to_process;
109*4882a593Smuzhiyun 	} while (processed < req->cryptlen);
110*4882a593Smuzhiyun out:
111*4882a593Smuzhiyun 	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
112*4882a593Smuzhiyun 	return rc;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
ctr3686_aes_nx_crypt(struct skcipher_request * req)115*4882a593Smuzhiyun static int ctr3686_aes_nx_crypt(struct skcipher_request *req)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
118*4882a593Smuzhiyun 	struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
119*4882a593Smuzhiyun 	u8 iv[16];
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_NONCE_SIZE);
122*4882a593Smuzhiyun 	memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
123*4882a593Smuzhiyun 	iv[12] = iv[13] = iv[14] = 0;
124*4882a593Smuzhiyun 	iv[15] = 1;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	return ctr_aes_nx_crypt(req, iv);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun struct skcipher_alg nx_ctr3686_aes_alg = {
130*4882a593Smuzhiyun 	.base.cra_name		= "rfc3686(ctr(aes))",
131*4882a593Smuzhiyun 	.base.cra_driver_name	= "rfc3686-ctr-aes-nx",
132*4882a593Smuzhiyun 	.base.cra_priority	= 300,
133*4882a593Smuzhiyun 	.base.cra_blocksize	= 1,
134*4882a593Smuzhiyun 	.base.cra_ctxsize	= sizeof(struct nx_crypto_ctx),
135*4882a593Smuzhiyun 	.base.cra_module	= THIS_MODULE,
136*4882a593Smuzhiyun 	.init			= nx_crypto_ctx_aes_ctr_init,
137*4882a593Smuzhiyun 	.exit			= nx_crypto_ctx_skcipher_exit,
138*4882a593Smuzhiyun 	.min_keysize		= AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
139*4882a593Smuzhiyun 	.max_keysize		= AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
140*4882a593Smuzhiyun 	.ivsize			= CTR_RFC3686_IV_SIZE,
141*4882a593Smuzhiyun 	.setkey			= ctr3686_aes_nx_set_key,
142*4882a593Smuzhiyun 	.encrypt		= ctr3686_aes_nx_crypt,
143*4882a593Smuzhiyun 	.decrypt		= ctr3686_aes_nx_crypt,
144*4882a593Smuzhiyun 	.chunksize		= AES_BLOCK_SIZE,
145*4882a593Smuzhiyun };
146