xref: /openbmc/linux/drivers/crypto/nx/nx-aes-gcm.c (revision c849163b)
1 /**
2  * AES GCM routines supporting the Power 7+ Nest Accelerators driver
3  *
4  * Copyright (C) 2012 International Business Machines Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 only.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * Author: Kent Yoder <yoder1@us.ibm.com>
20  */
21 
22 #include <crypto/internal/aead.h>
23 #include <crypto/aes.h>
24 #include <crypto/algapi.h>
25 #include <crypto/scatterwalk.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/crypto.h>
29 #include <asm/vio.h>
30 
31 #include "nx_csbcpb.h"
32 #include "nx.h"
33 
34 
35 static int gcm_aes_nx_set_key(struct crypto_aead *tfm,
36 			      const u8           *in_key,
37 			      unsigned int        key_len)
38 {
39 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&tfm->base);
40 	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
41 	struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead;
42 
43 	nx_ctx_init(nx_ctx, HCOP_FC_AES);
44 
45 	switch (key_len) {
46 	case AES_KEYSIZE_128:
47 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
48 		NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_128);
49 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
50 		break;
51 	case AES_KEYSIZE_192:
52 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
53 		NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_192);
54 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
55 		break;
56 	case AES_KEYSIZE_256:
57 		NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
58 		NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_256);
59 		nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
60 		break;
61 	default:
62 		return -EINVAL;
63 	}
64 
65 	csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM;
66 	memcpy(csbcpb->cpb.aes_gcm.key, in_key, key_len);
67 
68 	csbcpb_aead->cpb.hdr.mode = NX_MODE_AES_GCA;
69 	memcpy(csbcpb_aead->cpb.aes_gca.key, in_key, key_len);
70 
71 	return 0;
72 }
73 
74 static int gcm4106_aes_nx_set_key(struct crypto_aead *tfm,
75 				  const u8           *in_key,
76 				  unsigned int        key_len)
77 {
78 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&tfm->base);
79 	char *nonce = nx_ctx->priv.gcm.nonce;
80 	int rc;
81 
82 	if (key_len < 4)
83 		return -EINVAL;
84 
85 	key_len -= 4;
86 
87 	rc = gcm_aes_nx_set_key(tfm, in_key, key_len);
88 	if (rc)
89 		goto out;
90 
91 	memcpy(nonce, in_key + key_len, 4);
92 out:
93 	return rc;
94 }
95 
96 static int gcm_aes_nx_setauthsize(struct crypto_aead *tfm,
97 				  unsigned int authsize)
98 {
99 	if (authsize > crypto_aead_alg(tfm)->maxauthsize)
100 		return -EINVAL;
101 
102 	crypto_aead_crt(tfm)->authsize = authsize;
103 
104 	return 0;
105 }
106 
107 static int gcm4106_aes_nx_setauthsize(struct crypto_aead *tfm,
108 				      unsigned int authsize)
109 {
110 	switch (authsize) {
111 	case 8:
112 	case 12:
113 	case 16:
114 		break;
115 	default:
116 		return -EINVAL;
117 	}
118 
119 	crypto_aead_crt(tfm)->authsize = authsize;
120 
121 	return 0;
122 }
123 
124 static int nx_gca(struct nx_crypto_ctx  *nx_ctx,
125 		  struct aead_request   *req,
126 		  u8                    *out)
127 {
128 	struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead;
129 	int rc = -EINVAL;
130 	struct scatter_walk walk;
131 	struct nx_sg *nx_sg = nx_ctx->in_sg;
132 
133 	if (req->assoclen > nx_ctx->ap->databytelen)
134 		goto out;
135 
136 	if (req->assoclen <= AES_BLOCK_SIZE) {
137 		scatterwalk_start(&walk, req->assoc);
138 		scatterwalk_copychunks(out, &walk, req->assoclen,
139 				       SCATTERWALK_FROM_SG);
140 		scatterwalk_done(&walk, SCATTERWALK_FROM_SG, 0);
141 
142 		rc = 0;
143 		goto out;
144 	}
145 
146 	nx_sg = nx_walk_and_build(nx_sg, nx_ctx->ap->sglen, req->assoc, 0,
147 				  req->assoclen);
148 	nx_ctx->op_aead.inlen = (nx_ctx->in_sg - nx_sg) * sizeof(struct nx_sg);
149 
150 	rc = nx_hcall_sync(nx_ctx, &nx_ctx->op_aead,
151 			   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
152 	if (rc)
153 		goto out;
154 
155 	atomic_inc(&(nx_ctx->stats->aes_ops));
156 	atomic64_add(req->assoclen, &(nx_ctx->stats->aes_bytes));
157 
158 	memcpy(out, csbcpb_aead->cpb.aes_gca.out_pat, AES_BLOCK_SIZE);
159 out:
160 	return rc;
161 }
162 
163 static int gcm_aes_nx_crypt(struct aead_request *req, int enc)
164 {
165 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
166 	struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
167 	struct blkcipher_desc desc;
168 	unsigned int nbytes = req->cryptlen;
169 	unsigned long irq_flags;
170 	int rc = -EINVAL;
171 
172 	spin_lock_irqsave(&nx_ctx->lock, irq_flags);
173 
174 	if (nbytes > nx_ctx->ap->databytelen)
175 		goto out;
176 
177 	desc.info = nx_ctx->priv.gcm.iv;
178 	/* initialize the counter */
179 	*(u32 *)(desc.info + NX_GCM_CTR_OFFSET) = 1;
180 
181 	/* For scenarios where the input message is zero length, AES CTR mode
182 	 * may be used. Set the source data to be a single block (16B) of all
183 	 * zeros, and set the input IV value to be the same as the GMAC IV
184 	 * value. - nx_wb 4.8.1.3 */
185 	if (nbytes == 0) {
186 		char src[AES_BLOCK_SIZE] = {};
187 		struct scatterlist sg;
188 
189 		desc.tfm = crypto_alloc_blkcipher("ctr(aes)", 0, 0);
190 		if (IS_ERR(desc.tfm)) {
191 			rc = -ENOMEM;
192 			goto out;
193 		}
194 
195 		crypto_blkcipher_setkey(desc.tfm, csbcpb->cpb.aes_gcm.key,
196 			NX_CPB_KEY_SIZE(csbcpb) == NX_KS_AES_128 ? 16 :
197 			NX_CPB_KEY_SIZE(csbcpb) == NX_KS_AES_192 ? 24 : 32);
198 
199 		sg_init_one(&sg, src, AES_BLOCK_SIZE);
200 		if (enc)
201 			crypto_blkcipher_encrypt_iv(&desc, req->dst, &sg,
202 						    AES_BLOCK_SIZE);
203 		else
204 			crypto_blkcipher_decrypt_iv(&desc, req->dst, &sg,
205 						    AES_BLOCK_SIZE);
206 		crypto_free_blkcipher(desc.tfm);
207 
208 		rc = 0;
209 		goto out;
210 	}
211 
212 	desc.tfm = (struct crypto_blkcipher *)req->base.tfm;
213 
214 	csbcpb->cpb.aes_gcm.bit_length_aad = req->assoclen * 8;
215 
216 	if (req->assoclen) {
217 		rc = nx_gca(nx_ctx, req, csbcpb->cpb.aes_gcm.in_pat_or_aad);
218 		if (rc)
219 			goto out;
220 	}
221 
222 	if (enc)
223 		NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
224 	else
225 		nbytes -= crypto_aead_authsize(crypto_aead_reqtfm(req));
226 
227 	csbcpb->cpb.aes_gcm.bit_length_data = nbytes * 8;
228 
229 	rc = nx_build_sg_lists(nx_ctx, &desc, req->dst, req->src, nbytes,
230 			       csbcpb->cpb.aes_gcm.iv_or_cnt);
231 	if (rc)
232 		goto out;
233 
234 	rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
235 			   req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
236 	if (rc)
237 		goto out;
238 
239 	atomic_inc(&(nx_ctx->stats->aes_ops));
240 	atomic64_add(csbcpb->csb.processed_byte_count,
241 		     &(nx_ctx->stats->aes_bytes));
242 
243 	if (enc) {
244 		/* copy out the auth tag */
245 		scatterwalk_map_and_copy(csbcpb->cpb.aes_gcm.out_pat_or_mac,
246 				 req->dst, nbytes,
247 				 crypto_aead_authsize(crypto_aead_reqtfm(req)),
248 				 SCATTERWALK_TO_SG);
249 	} else if (req->assoclen) {
250 		u8 *itag = nx_ctx->priv.gcm.iauth_tag;
251 		u8 *otag = csbcpb->cpb.aes_gcm.out_pat_or_mac;
252 
253 		scatterwalk_map_and_copy(itag, req->dst, nbytes,
254 				 crypto_aead_authsize(crypto_aead_reqtfm(req)),
255 				 SCATTERWALK_FROM_SG);
256 		rc = memcmp(itag, otag,
257 			    crypto_aead_authsize(crypto_aead_reqtfm(req))) ?
258 		     -EBADMSG : 0;
259 	}
260 out:
261 	spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
262 	return rc;
263 }
264 
265 static int gcm_aes_nx_encrypt(struct aead_request *req)
266 {
267 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
268 	char *iv = nx_ctx->priv.gcm.iv;
269 
270 	memcpy(iv, req->iv, 12);
271 
272 	return gcm_aes_nx_crypt(req, 1);
273 }
274 
275 static int gcm_aes_nx_decrypt(struct aead_request *req)
276 {
277 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
278 	char *iv = nx_ctx->priv.gcm.iv;
279 
280 	memcpy(iv, req->iv, 12);
281 
282 	return gcm_aes_nx_crypt(req, 0);
283 }
284 
285 static int gcm4106_aes_nx_encrypt(struct aead_request *req)
286 {
287 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
288 	char *iv = nx_ctx->priv.gcm.iv;
289 	char *nonce = nx_ctx->priv.gcm.nonce;
290 
291 	memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
292 	memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
293 
294 	return gcm_aes_nx_crypt(req, 1);
295 }
296 
297 static int gcm4106_aes_nx_decrypt(struct aead_request *req)
298 {
299 	struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
300 	char *iv = nx_ctx->priv.gcm.iv;
301 	char *nonce = nx_ctx->priv.gcm.nonce;
302 
303 	memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
304 	memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
305 
306 	return gcm_aes_nx_crypt(req, 0);
307 }
308 
309 /* tell the block cipher walk routines that this is a stream cipher by
310  * setting cra_blocksize to 1. Even using blkcipher_walk_virt_block
311  * during encrypt/decrypt doesn't solve this problem, because it calls
312  * blkcipher_walk_done under the covers, which doesn't use walk->blocksize,
313  * but instead uses this tfm->blocksize. */
314 struct crypto_alg nx_gcm_aes_alg = {
315 	.cra_name        = "gcm(aes)",
316 	.cra_driver_name = "gcm-aes-nx",
317 	.cra_priority    = 300,
318 	.cra_flags       = CRYPTO_ALG_TYPE_AEAD,
319 	.cra_blocksize   = 1,
320 	.cra_ctxsize     = sizeof(struct nx_crypto_ctx),
321 	.cra_type        = &crypto_aead_type,
322 	.cra_module      = THIS_MODULE,
323 	.cra_init        = nx_crypto_ctx_aes_gcm_init,
324 	.cra_exit        = nx_crypto_ctx_exit,
325 	.cra_aead = {
326 		.ivsize      = AES_BLOCK_SIZE,
327 		.maxauthsize = AES_BLOCK_SIZE,
328 		.setkey      = gcm_aes_nx_set_key,
329 		.setauthsize = gcm_aes_nx_setauthsize,
330 		.encrypt     = gcm_aes_nx_encrypt,
331 		.decrypt     = gcm_aes_nx_decrypt,
332 	}
333 };
334 
335 struct crypto_alg nx_gcm4106_aes_alg = {
336 	.cra_name        = "rfc4106(gcm(aes))",
337 	.cra_driver_name = "rfc4106-gcm-aes-nx",
338 	.cra_priority    = 300,
339 	.cra_flags       = CRYPTO_ALG_TYPE_AEAD,
340 	.cra_blocksize   = 1,
341 	.cra_ctxsize     = sizeof(struct nx_crypto_ctx),
342 	.cra_type        = &crypto_nivaead_type,
343 	.cra_module      = THIS_MODULE,
344 	.cra_init        = nx_crypto_ctx_aes_gcm_init,
345 	.cra_exit        = nx_crypto_ctx_exit,
346 	.cra_aead = {
347 		.ivsize      = 8,
348 		.maxauthsize = AES_BLOCK_SIZE,
349 		.geniv       = "seqiv",
350 		.setkey      = gcm4106_aes_nx_set_key,
351 		.setauthsize = gcm4106_aes_nx_setauthsize,
352 		.encrypt     = gcm4106_aes_nx_encrypt,
353 		.decrypt     = gcm4106_aes_nx_decrypt,
354 	}
355 };
356