2 * AES GCM routines supporting the Power 7+ Nest Accelerators driver
4 * Copyright (C) 2012 International Business Machines Inc.
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.
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.
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.
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>
31 #include "nx_csbcpb.h"
35 static int gcm_aes_nx_set_key(struct crypto_aead *tfm,
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;
43 nx_ctx_init(nx_ctx, HCOP_FC_AES);
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];
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];
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];
65 csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM;
66 memcpy(csbcpb->cpb.aes_gcm.key, in_key, key_len);
68 csbcpb_aead->cpb.hdr.mode = NX_MODE_AES_GCA;
69 memcpy(csbcpb_aead->cpb.aes_gca.key, in_key, key_len);
74 static int gcm4106_aes_nx_set_key(struct crypto_aead *tfm,
78 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&tfm->base);
79 char *nonce = nx_ctx->priv.gcm.nonce;
87 rc = gcm_aes_nx_set_key(tfm, in_key, key_len);
91 memcpy(nonce, in_key + key_len, 4);
96 static int gcm_aes_nx_setauthsize(struct crypto_aead *tfm,
97 unsigned int authsize)
99 if (authsize > crypto_aead_alg(tfm)->maxauthsize)
102 crypto_aead_crt(tfm)->authsize = authsize;
107 static int gcm4106_aes_nx_setauthsize(struct crypto_aead *tfm,
108 unsigned int authsize)
119 crypto_aead_crt(tfm)->authsize = authsize;
124 static int nx_gca(struct nx_crypto_ctx *nx_ctx,
125 struct aead_request *req,
129 struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead;
130 struct scatter_walk walk;
131 struct nx_sg *nx_sg = nx_ctx->in_sg;
132 unsigned int nbytes = req->assoclen;
133 unsigned int processed = 0, to_process;
134 unsigned int max_sg_len;
136 if (nbytes <= AES_BLOCK_SIZE) {
137 scatterwalk_start(&walk, req->assoc);
138 scatterwalk_copychunks(out, &walk, nbytes, SCATTERWALK_FROM_SG);
139 scatterwalk_done(&walk, SCATTERWALK_FROM_SG, 0);
143 NX_CPB_FDM(csbcpb_aead) &= ~NX_FDM_CONTINUATION;
145 /* page_limit: number of sg entries that fit on one page */
146 max_sg_len = min_t(u64, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
148 max_sg_len = min_t(u64, max_sg_len,
149 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
153 * to_process: the data chunk to process in this update.
154 * This value is bound by sg list limits.
156 to_process = min_t(u64, nbytes - processed,
157 nx_ctx->ap->databytelen);
158 to_process = min_t(u64, to_process,
159 NX_PAGE_SIZE * (max_sg_len - 1));
161 nx_sg = nx_walk_and_build(nx_ctx->in_sg, max_sg_len,
162 req->assoc, processed, &to_process);
164 if ((to_process + processed) < nbytes)
165 NX_CPB_FDM(csbcpb_aead) |= NX_FDM_INTERMEDIATE;
167 NX_CPB_FDM(csbcpb_aead) &= ~NX_FDM_INTERMEDIATE;
169 nx_ctx->op_aead.inlen = (nx_ctx->in_sg - nx_sg)
170 * sizeof(struct nx_sg);
172 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op_aead,
173 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
177 memcpy(csbcpb_aead->cpb.aes_gca.in_pat,
178 csbcpb_aead->cpb.aes_gca.out_pat,
180 NX_CPB_FDM(csbcpb_aead) |= NX_FDM_CONTINUATION;
182 atomic_inc(&(nx_ctx->stats->aes_ops));
183 atomic64_add(req->assoclen, &(nx_ctx->stats->aes_bytes));
185 processed += to_process;
186 } while (processed < nbytes);
188 memcpy(out, csbcpb_aead->cpb.aes_gca.out_pat, AES_BLOCK_SIZE);
193 static int gmac(struct aead_request *req, struct blkcipher_desc *desc)
196 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
197 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
199 unsigned int nbytes = req->assoclen;
200 unsigned int processed = 0, to_process;
201 unsigned int max_sg_len;
204 csbcpb->cpb.hdr.mode = NX_MODE_AES_GMAC;
206 NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
208 /* page_limit: number of sg entries that fit on one page */
209 max_sg_len = min_t(u64, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
211 max_sg_len = min_t(u64, max_sg_len,
212 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
215 memcpy(csbcpb->cpb.aes_gcm.iv_or_cnt, desc->info, AES_BLOCK_SIZE);
219 * to_process: the data chunk to process in this update.
220 * This value is bound by sg list limits.
222 to_process = min_t(u64, nbytes - processed,
223 nx_ctx->ap->databytelen);
224 to_process = min_t(u64, to_process,
225 NX_PAGE_SIZE * (max_sg_len - 1));
227 nx_sg = nx_walk_and_build(nx_ctx->in_sg, max_sg_len,
228 req->assoc, processed, &to_process);
230 if ((to_process + processed) < nbytes)
231 NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
233 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
235 nx_ctx->op.inlen = (nx_ctx->in_sg - nx_sg)
236 * sizeof(struct nx_sg);
238 csbcpb->cpb.aes_gcm.bit_length_data = 0;
239 csbcpb->cpb.aes_gcm.bit_length_aad = 8 * nbytes;
241 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
242 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
246 memcpy(csbcpb->cpb.aes_gcm.in_pat_or_aad,
247 csbcpb->cpb.aes_gcm.out_pat_or_mac, AES_BLOCK_SIZE);
248 memcpy(csbcpb->cpb.aes_gcm.in_s0,
249 csbcpb->cpb.aes_gcm.out_s0, AES_BLOCK_SIZE);
251 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
253 atomic_inc(&(nx_ctx->stats->aes_ops));
254 atomic64_add(req->assoclen, &(nx_ctx->stats->aes_bytes));
256 processed += to_process;
257 } while (processed < nbytes);
260 /* Restore GCM mode */
261 csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM;
265 static int gcm_empty(struct aead_request *req, struct blkcipher_desc *desc,
269 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
270 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
271 char out[AES_BLOCK_SIZE];
272 struct nx_sg *in_sg, *out_sg;
275 /* For scenarios where the input message is zero length, AES CTR mode
276 * may be used. Set the source data to be a single block (16B) of all
277 * zeros, and set the input IV value to be the same as the GMAC IV
278 * value. - nx_wb 4.8.1.3 */
280 /* Change to ECB mode */
281 csbcpb->cpb.hdr.mode = NX_MODE_AES_ECB;
282 memcpy(csbcpb->cpb.aes_ecb.key, csbcpb->cpb.aes_gcm.key,
283 sizeof(csbcpb->cpb.aes_ecb.key));
285 NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
287 NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
289 len = AES_BLOCK_SIZE;
291 /* Encrypt the counter/IV */
292 in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) desc->info,
293 &len, nx_ctx->ap->sglen);
295 if (len != AES_BLOCK_SIZE)
299 out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *) out, &len,
302 if (len != sizeof(out))
305 nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
306 nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
308 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
309 desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
312 atomic_inc(&(nx_ctx->stats->aes_ops));
314 /* Copy out the auth tag */
315 memcpy(csbcpb->cpb.aes_gcm.out_pat_or_mac, out,
316 crypto_aead_authsize(crypto_aead_reqtfm(req)));
318 /* Restore XCBC mode */
319 csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM;
322 * ECB key uses the same region that GCM AAD and counter, so it's safe
323 * to just fill it with zeroes.
325 memset(csbcpb->cpb.aes_ecb.key, 0, sizeof(csbcpb->cpb.aes_ecb.key));
330 static int gcm_aes_nx_crypt(struct aead_request *req, int enc)
332 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
333 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
334 struct blkcipher_desc desc;
335 unsigned int nbytes = req->cryptlen;
336 unsigned int processed = 0, to_process;
337 unsigned long irq_flags;
340 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
342 desc.info = nx_ctx->priv.gcm.iv;
343 /* initialize the counter */
344 *(u32 *)(desc.info + NX_GCM_CTR_OFFSET) = 1;
347 if (req->assoclen == 0)
348 rc = gcm_empty(req, &desc, enc);
350 rc = gmac(req, &desc);
357 /* Process associated data */
358 csbcpb->cpb.aes_gcm.bit_length_aad = req->assoclen * 8;
360 rc = nx_gca(nx_ctx, req, csbcpb->cpb.aes_gcm.in_pat_or_aad);
365 /* Set flags for encryption */
366 NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
368 NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
370 NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
371 nbytes -= crypto_aead_authsize(crypto_aead_reqtfm(req));
375 to_process = nbytes - processed;
377 csbcpb->cpb.aes_gcm.bit_length_data = nbytes * 8;
378 desc.tfm = (struct crypto_blkcipher *) req->base.tfm;
379 rc = nx_build_sg_lists(nx_ctx, &desc, req->dst,
380 req->src, &to_process, processed,
381 csbcpb->cpb.aes_gcm.iv_or_cnt);
386 if ((to_process + processed) < nbytes)
387 NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
389 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
392 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
393 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
397 memcpy(desc.info, csbcpb->cpb.aes_gcm.out_cnt, AES_BLOCK_SIZE);
398 memcpy(csbcpb->cpb.aes_gcm.in_pat_or_aad,
399 csbcpb->cpb.aes_gcm.out_pat_or_mac, AES_BLOCK_SIZE);
400 memcpy(csbcpb->cpb.aes_gcm.in_s0,
401 csbcpb->cpb.aes_gcm.out_s0, AES_BLOCK_SIZE);
403 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
405 atomic_inc(&(nx_ctx->stats->aes_ops));
406 atomic64_add(csbcpb->csb.processed_byte_count,
407 &(nx_ctx->stats->aes_bytes));
409 processed += to_process;
410 } while (processed < nbytes);
414 /* copy out the auth tag */
415 scatterwalk_map_and_copy(csbcpb->cpb.aes_gcm.out_pat_or_mac,
417 crypto_aead_authsize(crypto_aead_reqtfm(req)),
420 u8 *itag = nx_ctx->priv.gcm.iauth_tag;
421 u8 *otag = csbcpb->cpb.aes_gcm.out_pat_or_mac;
423 scatterwalk_map_and_copy(itag, req->src, nbytes,
424 crypto_aead_authsize(crypto_aead_reqtfm(req)),
425 SCATTERWALK_FROM_SG);
426 rc = memcmp(itag, otag,
427 crypto_aead_authsize(crypto_aead_reqtfm(req))) ?
431 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
435 static int gcm_aes_nx_encrypt(struct aead_request *req)
437 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
438 char *iv = nx_ctx->priv.gcm.iv;
440 memcpy(iv, req->iv, 12);
442 return gcm_aes_nx_crypt(req, 1);
445 static int gcm_aes_nx_decrypt(struct aead_request *req)
447 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
448 char *iv = nx_ctx->priv.gcm.iv;
450 memcpy(iv, req->iv, 12);
452 return gcm_aes_nx_crypt(req, 0);
455 static int gcm4106_aes_nx_encrypt(struct aead_request *req)
457 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
458 char *iv = nx_ctx->priv.gcm.iv;
459 char *nonce = nx_ctx->priv.gcm.nonce;
461 memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
462 memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
464 return gcm_aes_nx_crypt(req, 1);
467 static int gcm4106_aes_nx_decrypt(struct aead_request *req)
469 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
470 char *iv = nx_ctx->priv.gcm.iv;
471 char *nonce = nx_ctx->priv.gcm.nonce;
473 memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
474 memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
476 return gcm_aes_nx_crypt(req, 0);
479 /* tell the block cipher walk routines that this is a stream cipher by
480 * setting cra_blocksize to 1. Even using blkcipher_walk_virt_block
481 * during encrypt/decrypt doesn't solve this problem, because it calls
482 * blkcipher_walk_done under the covers, which doesn't use walk->blocksize,
483 * but instead uses this tfm->blocksize. */
484 struct crypto_alg nx_gcm_aes_alg = {
485 .cra_name = "gcm(aes)",
486 .cra_driver_name = "gcm-aes-nx",
488 .cra_flags = CRYPTO_ALG_TYPE_AEAD,
490 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
491 .cra_type = &crypto_aead_type,
492 .cra_module = THIS_MODULE,
493 .cra_init = nx_crypto_ctx_aes_gcm_init,
494 .cra_exit = nx_crypto_ctx_exit,
496 .ivsize = AES_BLOCK_SIZE,
497 .maxauthsize = AES_BLOCK_SIZE,
498 .setkey = gcm_aes_nx_set_key,
499 .setauthsize = gcm_aes_nx_setauthsize,
500 .encrypt = gcm_aes_nx_encrypt,
501 .decrypt = gcm_aes_nx_decrypt,
505 struct crypto_alg nx_gcm4106_aes_alg = {
506 .cra_name = "rfc4106(gcm(aes))",
507 .cra_driver_name = "rfc4106-gcm-aes-nx",
509 .cra_flags = CRYPTO_ALG_TYPE_AEAD,
511 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
512 .cra_type = &crypto_nivaead_type,
513 .cra_module = THIS_MODULE,
514 .cra_init = nx_crypto_ctx_aes_gcm_init,
515 .cra_exit = nx_crypto_ctx_exit,
518 .maxauthsize = AES_BLOCK_SIZE,
520 .setkey = gcm4106_aes_nx_set_key,
521 .setauthsize = gcm4106_aes_nx_setauthsize,
522 .encrypt = gcm4106_aes_nx_encrypt,
523 .decrypt = gcm4106_aes_nx_decrypt,