blob: abd465f479c433641f3e5024cfe6442d6b68024d [file] [log] [blame]
Kent Yoderf2a15f12012-05-14 11:05:59 +00001/**
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>
David Gstircb8affb2015-11-15 17:14:41 +010024#include <crypto/algapi.h>
Kent Yoderf2a15f12012-05-14 11:05:59 +000025#include <crypto/scatterwalk.h>
26#include <linux/module.h>
27#include <linux/types.h>
Kent Yoderf2a15f12012-05-14 11:05:59 +000028#include <asm/vio.h>
29
30#include "nx_csbcpb.h"
31#include "nx.h"
32
33
34static int gcm_aes_nx_set_key(struct crypto_aead *tfm,
35 const u8 *in_key,
36 unsigned int key_len)
37{
Herbert Xuc3d21942015-07-09 07:17:31 +080038 struct nx_crypto_ctx *nx_ctx = crypto_aead_ctx(tfm);
Kent Yoderf2a15f12012-05-14 11:05:59 +000039 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
40 struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead;
41
42 nx_ctx_init(nx_ctx, HCOP_FC_AES);
43
44 switch (key_len) {
45 case AES_KEYSIZE_128:
46 NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
47 NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_128);
48 nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
49 break;
50 case AES_KEYSIZE_192:
51 NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
52 NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_192);
53 nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
54 break;
55 case AES_KEYSIZE_256:
56 NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
57 NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_256);
58 nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
59 break;
60 default:
61 return -EINVAL;
62 }
63
64 csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM;
65 memcpy(csbcpb->cpb.aes_gcm.key, in_key, key_len);
66
67 csbcpb_aead->cpb.hdr.mode = NX_MODE_AES_GCA;
68 memcpy(csbcpb_aead->cpb.aes_gca.key, in_key, key_len);
69
70 return 0;
71}
72
73static int gcm4106_aes_nx_set_key(struct crypto_aead *tfm,
74 const u8 *in_key,
75 unsigned int key_len)
76{
Herbert Xuc3d21942015-07-09 07:17:31 +080077 struct nx_crypto_ctx *nx_ctx = crypto_aead_ctx(tfm);
Kent Yoderf2a15f12012-05-14 11:05:59 +000078 char *nonce = nx_ctx->priv.gcm.nonce;
79 int rc;
80
81 if (key_len < 4)
82 return -EINVAL;
83
84 key_len -= 4;
85
86 rc = gcm_aes_nx_set_key(tfm, in_key, key_len);
87 if (rc)
88 goto out;
89
90 memcpy(nonce, in_key + key_len, 4);
91out:
92 return rc;
93}
94
Kent Yoderf2a15f12012-05-14 11:05:59 +000095static int gcm4106_aes_nx_setauthsize(struct crypto_aead *tfm,
96 unsigned int authsize)
97{
98 switch (authsize) {
99 case 8:
100 case 12:
101 case 16:
102 break;
103 default:
104 return -EINVAL;
105 }
106
Kent Yoderf2a15f12012-05-14 11:05:59 +0000107 return 0;
108}
109
110static int nx_gca(struct nx_crypto_ctx *nx_ctx,
111 struct aead_request *req,
Herbert Xuc3d21942015-07-09 07:17:31 +0800112 u8 *out,
113 unsigned int assoclen)
Kent Yoderf2a15f12012-05-14 11:05:59 +0000114{
Marcelo Cerri79980432013-08-29 11:36:35 -0300115 int rc;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000116 struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000117 struct scatter_walk walk;
118 struct nx_sg *nx_sg = nx_ctx->in_sg;
Herbert Xuc3d21942015-07-09 07:17:31 +0800119 unsigned int nbytes = assoclen;
Marcelo Cerri79980432013-08-29 11:36:35 -0300120 unsigned int processed = 0, to_process;
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200121 unsigned int max_sg_len;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000122
Marcelo Cerri79980432013-08-29 11:36:35 -0300123 if (nbytes <= AES_BLOCK_SIZE) {
Herbert Xu201f28f2015-06-16 13:54:21 +0800124 scatterwalk_start(&walk, req->src);
Marcelo Cerri79980432013-08-29 11:36:35 -0300125 scatterwalk_copychunks(out, &walk, nbytes, SCATTERWALK_FROM_SG);
Kent Yoderf2a15f12012-05-14 11:05:59 +0000126 scatterwalk_done(&walk, SCATTERWALK_FROM_SG, 0);
Marcelo Cerri79980432013-08-29 11:36:35 -0300127 return 0;
128 }
Kent Yoderf2a15f12012-05-14 11:05:59 +0000129
Marcelo Cerri79980432013-08-29 11:36:35 -0300130 NX_CPB_FDM(csbcpb_aead) &= ~NX_FDM_CONTINUATION;
131
132 /* page_limit: number of sg entries that fit on one page */
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200133 max_sg_len = min_t(u64, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
Marcelo Cerri79980432013-08-29 11:36:35 -0300134 nx_ctx->ap->sglen);
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200135 max_sg_len = min_t(u64, max_sg_len,
136 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
Marcelo Cerri79980432013-08-29 11:36:35 -0300137
138 do {
139 /*
140 * to_process: the data chunk to process in this update.
141 * This value is bound by sg list limits.
142 */
143 to_process = min_t(u64, nbytes - processed,
144 nx_ctx->ap->databytelen);
145 to_process = min_t(u64, to_process,
146 NX_PAGE_SIZE * (max_sg_len - 1));
147
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200148 nx_sg = nx_walk_and_build(nx_ctx->in_sg, max_sg_len,
Herbert Xu201f28f2015-06-16 13:54:21 +0800149 req->src, processed, &to_process);
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200150
Marcelo Cerri79980432013-08-29 11:36:35 -0300151 if ((to_process + processed) < nbytes)
152 NX_CPB_FDM(csbcpb_aead) |= NX_FDM_INTERMEDIATE;
153 else
154 NX_CPB_FDM(csbcpb_aead) &= ~NX_FDM_INTERMEDIATE;
155
Marcelo Cerri79980432013-08-29 11:36:35 -0300156 nx_ctx->op_aead.inlen = (nx_ctx->in_sg - nx_sg)
157 * sizeof(struct nx_sg);
158
159 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op_aead,
160 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
161 if (rc)
162 return rc;
163
164 memcpy(csbcpb_aead->cpb.aes_gca.in_pat,
165 csbcpb_aead->cpb.aes_gca.out_pat,
166 AES_BLOCK_SIZE);
167 NX_CPB_FDM(csbcpb_aead) |= NX_FDM_CONTINUATION;
168
169 atomic_inc(&(nx_ctx->stats->aes_ops));
Herbert Xuc3d21942015-07-09 07:17:31 +0800170 atomic64_add(assoclen, &(nx_ctx->stats->aes_bytes));
Marcelo Cerri79980432013-08-29 11:36:35 -0300171
172 processed += to_process;
173 } while (processed < nbytes);
174
175 memcpy(out, csbcpb_aead->cpb.aes_gca.out_pat, AES_BLOCK_SIZE);
176
177 return rc;
178}
179
Herbert Xuc3d21942015-07-09 07:17:31 +0800180static int gmac(struct aead_request *req, struct blkcipher_desc *desc,
181 unsigned int assoclen)
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300182{
183 int rc;
Herbert Xuc3d21942015-07-09 07:17:31 +0800184 struct nx_crypto_ctx *nx_ctx =
185 crypto_aead_ctx(crypto_aead_reqtfm(req));
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300186 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
187 struct nx_sg *nx_sg;
Herbert Xuc3d21942015-07-09 07:17:31 +0800188 unsigned int nbytes = assoclen;
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300189 unsigned int processed = 0, to_process;
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200190 unsigned int max_sg_len;
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300191
192 /* Set GMAC mode */
193 csbcpb->cpb.hdr.mode = NX_MODE_AES_GMAC;
194
195 NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
196
197 /* page_limit: number of sg entries that fit on one page */
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200198 max_sg_len = min_t(u64, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300199 nx_ctx->ap->sglen);
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200200 max_sg_len = min_t(u64, max_sg_len,
201 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300202
203 /* Copy IV */
204 memcpy(csbcpb->cpb.aes_gcm.iv_or_cnt, desc->info, AES_BLOCK_SIZE);
205
206 do {
207 /*
208 * to_process: the data chunk to process in this update.
209 * This value is bound by sg list limits.
210 */
211 to_process = min_t(u64, nbytes - processed,
212 nx_ctx->ap->databytelen);
213 to_process = min_t(u64, to_process,
214 NX_PAGE_SIZE * (max_sg_len - 1));
215
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200216 nx_sg = nx_walk_and_build(nx_ctx->in_sg, max_sg_len,
Herbert Xu201f28f2015-06-16 13:54:21 +0800217 req->src, processed, &to_process);
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200218
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300219 if ((to_process + processed) < nbytes)
220 NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
221 else
222 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
223
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300224 nx_ctx->op.inlen = (nx_ctx->in_sg - nx_sg)
225 * sizeof(struct nx_sg);
226
227 csbcpb->cpb.aes_gcm.bit_length_data = 0;
228 csbcpb->cpb.aes_gcm.bit_length_aad = 8 * nbytes;
229
230 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
231 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
232 if (rc)
233 goto out;
234
235 memcpy(csbcpb->cpb.aes_gcm.in_pat_or_aad,
236 csbcpb->cpb.aes_gcm.out_pat_or_mac, AES_BLOCK_SIZE);
237 memcpy(csbcpb->cpb.aes_gcm.in_s0,
238 csbcpb->cpb.aes_gcm.out_s0, AES_BLOCK_SIZE);
239
240 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
241
242 atomic_inc(&(nx_ctx->stats->aes_ops));
Herbert Xuc3d21942015-07-09 07:17:31 +0800243 atomic64_add(assoclen, &(nx_ctx->stats->aes_bytes));
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300244
245 processed += to_process;
246 } while (processed < nbytes);
247
248out:
249 /* Restore GCM mode */
250 csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM;
251 return rc;
252}
253
Marcelo Cerri79980432013-08-29 11:36:35 -0300254static int gcm_empty(struct aead_request *req, struct blkcipher_desc *desc,
255 int enc)
256{
257 int rc;
Herbert Xuc3d21942015-07-09 07:17:31 +0800258 struct nx_crypto_ctx *nx_ctx =
259 crypto_aead_ctx(crypto_aead_reqtfm(req));
Marcelo Cerri79980432013-08-29 11:36:35 -0300260 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300261 char out[AES_BLOCK_SIZE];
262 struct nx_sg *in_sg, *out_sg;
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200263 int len;
Marcelo Cerri79980432013-08-29 11:36:35 -0300264
265 /* For scenarios where the input message is zero length, AES CTR mode
266 * may be used. Set the source data to be a single block (16B) of all
267 * zeros, and set the input IV value to be the same as the GMAC IV
268 * value. - nx_wb 4.8.1.3 */
Marcelo Cerri79980432013-08-29 11:36:35 -0300269
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300270 /* Change to ECB mode */
271 csbcpb->cpb.hdr.mode = NX_MODE_AES_ECB;
272 memcpy(csbcpb->cpb.aes_ecb.key, csbcpb->cpb.aes_gcm.key,
273 sizeof(csbcpb->cpb.aes_ecb.key));
Marcelo Cerri79980432013-08-29 11:36:35 -0300274 if (enc)
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300275 NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
Marcelo Cerri79980432013-08-29 11:36:35 -0300276 else
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300277 NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000278
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200279 len = AES_BLOCK_SIZE;
280
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300281 /* Encrypt the counter/IV */
282 in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) desc->info,
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200283 &len, nx_ctx->ap->sglen);
284
285 if (len != AES_BLOCK_SIZE)
286 return -EINVAL;
287
288 len = sizeof(out);
289 out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *) out, &len,
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300290 nx_ctx->ap->sglen);
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200291
292 if (len != sizeof(out))
293 return -EINVAL;
294
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300295 nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
296 nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
297
298 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
299 desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
300 if (rc)
301 goto out;
302 atomic_inc(&(nx_ctx->stats->aes_ops));
303
304 /* Copy out the auth tag */
305 memcpy(csbcpb->cpb.aes_gcm.out_pat_or_mac, out,
306 crypto_aead_authsize(crypto_aead_reqtfm(req)));
Kent Yoderf2a15f12012-05-14 11:05:59 +0000307out:
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300308 /* Restore XCBC mode */
309 csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM;
310
311 /*
312 * ECB key uses the same region that GCM AAD and counter, so it's safe
313 * to just fill it with zeroes.
314 */
315 memset(csbcpb->cpb.aes_ecb.key, 0, sizeof(csbcpb->cpb.aes_ecb.key));
316
Kent Yoderf2a15f12012-05-14 11:05:59 +0000317 return rc;
318}
319
Herbert Xuc3d21942015-07-09 07:17:31 +0800320static int gcm_aes_nx_crypt(struct aead_request *req, int enc,
321 unsigned int assoclen)
Kent Yoderf2a15f12012-05-14 11:05:59 +0000322{
Herbert Xuc3d21942015-07-09 07:17:31 +0800323 struct nx_crypto_ctx *nx_ctx =
324 crypto_aead_ctx(crypto_aead_reqtfm(req));
Herbert Xu030f4e92015-07-07 17:30:25 +0800325 struct nx_gcm_rctx *rctx = aead_request_ctx(req);
Kent Yoderf2a15f12012-05-14 11:05:59 +0000326 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
327 struct blkcipher_desc desc;
328 unsigned int nbytes = req->cryptlen;
Marcelo Cerri79980432013-08-29 11:36:35 -0300329 unsigned int processed = 0, to_process;
Marcelo Cerric8491632013-08-12 18:49:37 -0300330 unsigned long irq_flags;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000331 int rc = -EINVAL;
332
Marcelo Cerric8491632013-08-12 18:49:37 -0300333 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
334
Herbert Xu030f4e92015-07-07 17:30:25 +0800335 desc.info = rctx->iv;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000336 /* initialize the counter */
337 *(u32 *)(desc.info + NX_GCM_CTR_OFFSET) = 1;
338
Kent Yoderf2a15f12012-05-14 11:05:59 +0000339 if (nbytes == 0) {
Herbert Xuc3d21942015-07-09 07:17:31 +0800340 if (assoclen == 0)
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300341 rc = gcm_empty(req, &desc, enc);
342 else
Herbert Xuc3d21942015-07-09 07:17:31 +0800343 rc = gmac(req, &desc, assoclen);
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300344 if (rc)
345 goto out;
346 else
347 goto mac;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000348 }
349
Marcelo Cerri79980432013-08-29 11:36:35 -0300350 /* Process associated data */
Herbert Xuc3d21942015-07-09 07:17:31 +0800351 csbcpb->cpb.aes_gcm.bit_length_aad = assoclen * 8;
352 if (assoclen) {
353 rc = nx_gca(nx_ctx, req, csbcpb->cpb.aes_gcm.in_pat_or_aad,
354 assoclen);
Kent Yoderf2a15f12012-05-14 11:05:59 +0000355 if (rc)
356 goto out;
357 }
358
Marcelo Cerri79980432013-08-29 11:36:35 -0300359 /* Set flags for encryption */
360 NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
361 if (enc) {
Kent Yoderf2a15f12012-05-14 11:05:59 +0000362 NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
Marcelo Cerri79980432013-08-29 11:36:35 -0300363 } else {
364 NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
Kent Yoder1ad936e2013-04-12 17:13:59 +0000365 nbytes -= crypto_aead_authsize(crypto_aead_reqtfm(req));
Marcelo Cerri79980432013-08-29 11:36:35 -0300366 }
Kent Yoderf2a15f12012-05-14 11:05:59 +0000367
Marcelo Cerri79980432013-08-29 11:36:35 -0300368 do {
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200369 to_process = nbytes - processed;
370
371 csbcpb->cpb.aes_gcm.bit_length_data = nbytes * 8;
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200372 rc = nx_build_sg_lists(nx_ctx, &desc, req->dst,
Herbert Xu201f28f2015-06-16 13:54:21 +0800373 req->src, &to_process,
374 processed + req->assoclen,
Leonidas S. Barbosae13a79a2014-10-28 15:47:48 -0200375 csbcpb->cpb.aes_gcm.iv_or_cnt);
376
377 if (rc)
378 goto out;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000379
Marcelo Cerri79980432013-08-29 11:36:35 -0300380 if ((to_process + processed) < nbytes)
381 NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
382 else
383 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000384
Marcelo Cerri79980432013-08-29 11:36:35 -0300385
386 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
387 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
388 if (rc)
389 goto out;
390
391 memcpy(desc.info, csbcpb->cpb.aes_gcm.out_cnt, AES_BLOCK_SIZE);
392 memcpy(csbcpb->cpb.aes_gcm.in_pat_or_aad,
393 csbcpb->cpb.aes_gcm.out_pat_or_mac, AES_BLOCK_SIZE);
394 memcpy(csbcpb->cpb.aes_gcm.in_s0,
395 csbcpb->cpb.aes_gcm.out_s0, AES_BLOCK_SIZE);
396
397 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
398
399 atomic_inc(&(nx_ctx->stats->aes_ops));
400 atomic64_add(csbcpb->csb.processed_byte_count,
401 &(nx_ctx->stats->aes_bytes));
402
403 processed += to_process;
404 } while (processed < nbytes);
Kent Yoderf2a15f12012-05-14 11:05:59 +0000405
Marcelo Cerridec0ed62013-08-29 11:36:39 -0300406mac:
Kent Yoderf2a15f12012-05-14 11:05:59 +0000407 if (enc) {
408 /* copy out the auth tag */
Herbert Xu201f28f2015-06-16 13:54:21 +0800409 scatterwalk_map_and_copy(
410 csbcpb->cpb.aes_gcm.out_pat_or_mac,
411 req->dst, req->assoclen + nbytes,
412 crypto_aead_authsize(crypto_aead_reqtfm(req)),
413 SCATTERWALK_TO_SG);
jmlatten@linux.vnet.ibm.comb4eba0c2013-08-14 17:17:57 -0500414 } else {
Kent Yoderf2a15f12012-05-14 11:05:59 +0000415 u8 *itag = nx_ctx->priv.gcm.iauth_tag;
416 u8 *otag = csbcpb->cpb.aes_gcm.out_pat_or_mac;
417
Herbert Xu201f28f2015-06-16 13:54:21 +0800418 scatterwalk_map_and_copy(
419 itag, req->src, req->assoclen + nbytes,
420 crypto_aead_authsize(crypto_aead_reqtfm(req)),
421 SCATTERWALK_FROM_SG);
David Gstircb8affb2015-11-15 17:14:41 +0100422 rc = crypto_memneq(itag, otag,
Kent Yoderf2a15f12012-05-14 11:05:59 +0000423 crypto_aead_authsize(crypto_aead_reqtfm(req))) ?
424 -EBADMSG : 0;
425 }
426out:
Marcelo Cerric8491632013-08-12 18:49:37 -0300427 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
Kent Yoderf2a15f12012-05-14 11:05:59 +0000428 return rc;
429}
430
431static int gcm_aes_nx_encrypt(struct aead_request *req)
432{
Herbert Xu030f4e92015-07-07 17:30:25 +0800433 struct nx_gcm_rctx *rctx = aead_request_ctx(req);
434 char *iv = rctx->iv;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000435
436 memcpy(iv, req->iv, 12);
437
Herbert Xuc3d21942015-07-09 07:17:31 +0800438 return gcm_aes_nx_crypt(req, 1, req->assoclen);
Kent Yoderf2a15f12012-05-14 11:05:59 +0000439}
440
441static int gcm_aes_nx_decrypt(struct aead_request *req)
442{
Herbert Xu030f4e92015-07-07 17:30:25 +0800443 struct nx_gcm_rctx *rctx = aead_request_ctx(req);
444 char *iv = rctx->iv;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000445
446 memcpy(iv, req->iv, 12);
447
Herbert Xuc3d21942015-07-09 07:17:31 +0800448 return gcm_aes_nx_crypt(req, 0, req->assoclen);
Kent Yoderf2a15f12012-05-14 11:05:59 +0000449}
450
451static int gcm4106_aes_nx_encrypt(struct aead_request *req)
452{
Herbert Xuc3d21942015-07-09 07:17:31 +0800453 struct nx_crypto_ctx *nx_ctx =
454 crypto_aead_ctx(crypto_aead_reqtfm(req));
Herbert Xu030f4e92015-07-07 17:30:25 +0800455 struct nx_gcm_rctx *rctx = aead_request_ctx(req);
456 char *iv = rctx->iv;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000457 char *nonce = nx_ctx->priv.gcm.nonce;
458
459 memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
460 memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
461
Herbert Xuc3d21942015-07-09 07:17:31 +0800462 if (req->assoclen < 8)
463 return -EINVAL;
464
465 return gcm_aes_nx_crypt(req, 1, req->assoclen - 8);
Kent Yoderf2a15f12012-05-14 11:05:59 +0000466}
467
468static int gcm4106_aes_nx_decrypt(struct aead_request *req)
469{
Herbert Xuc3d21942015-07-09 07:17:31 +0800470 struct nx_crypto_ctx *nx_ctx =
471 crypto_aead_ctx(crypto_aead_reqtfm(req));
Herbert Xu030f4e92015-07-07 17:30:25 +0800472 struct nx_gcm_rctx *rctx = aead_request_ctx(req);
473 char *iv = rctx->iv;
Kent Yoderf2a15f12012-05-14 11:05:59 +0000474 char *nonce = nx_ctx->priv.gcm.nonce;
475
476 memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
477 memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
478
Herbert Xuc3d21942015-07-09 07:17:31 +0800479 if (req->assoclen < 8)
480 return -EINVAL;
481
482 return gcm_aes_nx_crypt(req, 0, req->assoclen - 8);
Kent Yoderf2a15f12012-05-14 11:05:59 +0000483}
484
485/* tell the block cipher walk routines that this is a stream cipher by
486 * setting cra_blocksize to 1. Even using blkcipher_walk_virt_block
487 * during encrypt/decrypt doesn't solve this problem, because it calls
488 * blkcipher_walk_done under the covers, which doesn't use walk->blocksize,
489 * but instead uses this tfm->blocksize. */
Herbert Xu201f28f2015-06-16 13:54:21 +0800490struct aead_alg nx_gcm_aes_alg = {
491 .base = {
492 .cra_name = "gcm(aes)",
493 .cra_driver_name = "gcm-aes-nx",
494 .cra_priority = 300,
495 .cra_blocksize = 1,
496 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
497 .cra_module = THIS_MODULE,
498 },
499 .init = nx_crypto_ctx_aes_gcm_init,
500 .exit = nx_crypto_ctx_aead_exit,
501 .ivsize = 12,
502 .maxauthsize = AES_BLOCK_SIZE,
503 .setkey = gcm_aes_nx_set_key,
504 .encrypt = gcm_aes_nx_encrypt,
505 .decrypt = gcm_aes_nx_decrypt,
Kent Yoderf2a15f12012-05-14 11:05:59 +0000506};
507
Herbert Xu201f28f2015-06-16 13:54:21 +0800508struct aead_alg nx_gcm4106_aes_alg = {
509 .base = {
510 .cra_name = "rfc4106(gcm(aes))",
511 .cra_driver_name = "rfc4106-gcm-aes-nx",
512 .cra_priority = 300,
513 .cra_blocksize = 1,
514 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
515 .cra_module = THIS_MODULE,
516 },
517 .init = nx_crypto_ctx_aes_gcm_init,
518 .exit = nx_crypto_ctx_aead_exit,
519 .ivsize = 8,
520 .maxauthsize = AES_BLOCK_SIZE,
521 .setkey = gcm4106_aes_nx_set_key,
522 .setauthsize = gcm4106_aes_nx_setauthsize,
523 .encrypt = gcm4106_aes_nx_encrypt,
524 .decrypt = gcm4106_aes_nx_decrypt,
Kent Yoderf2a15f12012-05-14 11:05:59 +0000525};