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.
19 * Author: Kent Yoder <yoder1@us.ibm.com>
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;
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(u32, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
151 * to_process: the data chunk to process in this update.
152 * This value is bound by sg list limits.
154 to_process = min_t(u64, nbytes - processed,
155 nx_ctx->ap->databytelen);
156 to_process = min_t(u64, to_process,
157 NX_PAGE_SIZE * (max_sg_len - 1));
159 if ((to_process + processed) < nbytes)
160 NX_CPB_FDM(csbcpb_aead) |= NX_FDM_INTERMEDIATE;
162 NX_CPB_FDM(csbcpb_aead) &= ~NX_FDM_INTERMEDIATE;
164 nx_sg = nx_walk_and_build(nx_ctx->in_sg, nx_ctx->ap->sglen,
165 req->assoc, processed, to_process);
166 nx_ctx->op_aead.inlen = (nx_ctx->in_sg - nx_sg)
167 * sizeof(struct nx_sg);
169 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op_aead,
170 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
174 memcpy(csbcpb_aead->cpb.aes_gca.in_pat,
175 csbcpb_aead->cpb.aes_gca.out_pat,
177 NX_CPB_FDM(csbcpb_aead) |= NX_FDM_CONTINUATION;
179 atomic_inc(&(nx_ctx->stats->aes_ops));
180 atomic64_add(req->assoclen, &(nx_ctx->stats->aes_bytes));
182 processed += to_process;
183 } while (processed < nbytes);
185 memcpy(out, csbcpb_aead->cpb.aes_gca.out_pat, AES_BLOCK_SIZE);
190 static int gcm_empty(struct aead_request *req, struct blkcipher_desc *desc,
194 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
195 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
197 /* For scenarios where the input message is zero length, AES CTR mode
198 * may be used. Set the source data to be a single block (16B) of all
199 * zeros, and set the input IV value to be the same as the GMAC IV
200 * value. - nx_wb 4.8.1.3 */
201 char src[AES_BLOCK_SIZE] = {};
202 struct scatterlist sg;
204 desc->tfm = crypto_alloc_blkcipher("ctr(aes)", 0, 0);
205 if (IS_ERR(desc->tfm)) {
210 crypto_blkcipher_setkey(desc->tfm, csbcpb->cpb.aes_gcm.key,
211 NX_CPB_KEY_SIZE(csbcpb) == NX_KS_AES_128 ? 16 :
212 NX_CPB_KEY_SIZE(csbcpb) == NX_KS_AES_192 ? 24 : 32);
214 sg_init_one(&sg, src, AES_BLOCK_SIZE);
216 rc = crypto_blkcipher_encrypt_iv(desc, req->dst, &sg,
219 rc = crypto_blkcipher_decrypt_iv(desc, req->dst, &sg,
221 crypto_free_blkcipher(desc->tfm);
227 static int gcm_aes_nx_crypt(struct aead_request *req, int enc)
229 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
230 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
231 struct blkcipher_desc desc;
232 unsigned int nbytes = req->cryptlen;
233 unsigned int processed = 0, to_process;
234 unsigned long irq_flags;
238 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
240 desc.info = nx_ctx->priv.gcm.iv;
241 /* initialize the counter */
242 *(u32 *)(desc.info + NX_GCM_CTR_OFFSET) = 1;
245 rc = gcm_empty(req, &desc, enc);
249 /* Process associated data */
250 csbcpb->cpb.aes_gcm.bit_length_aad = req->assoclen * 8;
252 rc = nx_gca(nx_ctx, req, csbcpb->cpb.aes_gcm.in_pat_or_aad);
257 /* Set flags for encryption */
258 NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION;
260 NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
262 NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
263 nbytes -= crypto_aead_authsize(crypto_aead_reqtfm(req));
266 /* page_limit: number of sg entries that fit on one page */
267 max_sg_len = min_t(u32, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
272 * to_process: the data chunk to process in this update.
273 * This value is bound by sg list limits.
275 to_process = min_t(u64, nbytes - processed,
276 nx_ctx->ap->databytelen);
277 to_process = min_t(u64, to_process,
278 NX_PAGE_SIZE * (max_sg_len - 1));
280 if ((to_process + processed) < nbytes)
281 NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
283 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
285 csbcpb->cpb.aes_gcm.bit_length_data = nbytes * 8;
286 desc.tfm = (struct crypto_blkcipher *) req->base.tfm;
287 rc = nx_build_sg_lists(nx_ctx, &desc, req->dst,
288 req->src, to_process, processed,
289 csbcpb->cpb.aes_gcm.iv_or_cnt);
293 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
294 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
298 memcpy(desc.info, csbcpb->cpb.aes_gcm.out_cnt, AES_BLOCK_SIZE);
299 memcpy(csbcpb->cpb.aes_gcm.in_pat_or_aad,
300 csbcpb->cpb.aes_gcm.out_pat_or_mac, AES_BLOCK_SIZE);
301 memcpy(csbcpb->cpb.aes_gcm.in_s0,
302 csbcpb->cpb.aes_gcm.out_s0, AES_BLOCK_SIZE);
304 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
306 atomic_inc(&(nx_ctx->stats->aes_ops));
307 atomic64_add(csbcpb->csb.processed_byte_count,
308 &(nx_ctx->stats->aes_bytes));
310 processed += to_process;
311 } while (processed < nbytes);
314 /* copy out the auth tag */
315 scatterwalk_map_and_copy(csbcpb->cpb.aes_gcm.out_pat_or_mac,
317 crypto_aead_authsize(crypto_aead_reqtfm(req)),
320 u8 *itag = nx_ctx->priv.gcm.iauth_tag;
321 u8 *otag = csbcpb->cpb.aes_gcm.out_pat_or_mac;
323 scatterwalk_map_and_copy(itag, req->src, nbytes,
324 crypto_aead_authsize(crypto_aead_reqtfm(req)),
325 SCATTERWALK_FROM_SG);
326 rc = memcmp(itag, otag,
327 crypto_aead_authsize(crypto_aead_reqtfm(req))) ?
331 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
335 static int gcm_aes_nx_encrypt(struct aead_request *req)
337 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
338 char *iv = nx_ctx->priv.gcm.iv;
340 memcpy(iv, req->iv, 12);
342 return gcm_aes_nx_crypt(req, 1);
345 static int gcm_aes_nx_decrypt(struct aead_request *req)
347 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
348 char *iv = nx_ctx->priv.gcm.iv;
350 memcpy(iv, req->iv, 12);
352 return gcm_aes_nx_crypt(req, 0);
355 static int gcm4106_aes_nx_encrypt(struct aead_request *req)
357 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
358 char *iv = nx_ctx->priv.gcm.iv;
359 char *nonce = nx_ctx->priv.gcm.nonce;
361 memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
362 memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
364 return gcm_aes_nx_crypt(req, 1);
367 static int gcm4106_aes_nx_decrypt(struct aead_request *req)
369 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
370 char *iv = nx_ctx->priv.gcm.iv;
371 char *nonce = nx_ctx->priv.gcm.nonce;
373 memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
374 memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
376 return gcm_aes_nx_crypt(req, 0);
379 /* tell the block cipher walk routines that this is a stream cipher by
380 * setting cra_blocksize to 1. Even using blkcipher_walk_virt_block
381 * during encrypt/decrypt doesn't solve this problem, because it calls
382 * blkcipher_walk_done under the covers, which doesn't use walk->blocksize,
383 * but instead uses this tfm->blocksize. */
384 struct crypto_alg nx_gcm_aes_alg = {
385 .cra_name = "gcm(aes)",
386 .cra_driver_name = "gcm-aes-nx",
388 .cra_flags = CRYPTO_ALG_TYPE_AEAD,
390 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
391 .cra_type = &crypto_aead_type,
392 .cra_module = THIS_MODULE,
393 .cra_init = nx_crypto_ctx_aes_gcm_init,
394 .cra_exit = nx_crypto_ctx_exit,
396 .ivsize = AES_BLOCK_SIZE,
397 .maxauthsize = AES_BLOCK_SIZE,
398 .setkey = gcm_aes_nx_set_key,
399 .setauthsize = gcm_aes_nx_setauthsize,
400 .encrypt = gcm_aes_nx_encrypt,
401 .decrypt = gcm_aes_nx_decrypt,
405 struct crypto_alg nx_gcm4106_aes_alg = {
406 .cra_name = "rfc4106(gcm(aes))",
407 .cra_driver_name = "rfc4106-gcm-aes-nx",
409 .cra_flags = CRYPTO_ALG_TYPE_AEAD,
411 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
412 .cra_type = &crypto_nivaead_type,
413 .cra_module = THIS_MODULE,
414 .cra_init = nx_crypto_ctx_aes_gcm_init,
415 .cra_exit = nx_crypto_ctx_exit,
418 .maxauthsize = AES_BLOCK_SIZE,
420 .setkey = gcm4106_aes_nx_set_key,
421 .setauthsize = gcm4106_aes_nx_setauthsize,
422 .encrypt = gcm4106_aes_nx_encrypt,
423 .decrypt = gcm4106_aes_nx_decrypt,