2 * AES CCM 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 ccm_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];
55 csbcpb->cpb.hdr.mode = NX_MODE_AES_CCM;
56 memcpy(csbcpb->cpb.aes_ccm.key, in_key, key_len);
58 csbcpb_aead->cpb.hdr.mode = NX_MODE_AES_CCA;
59 memcpy(csbcpb_aead->cpb.aes_cca.key, in_key, key_len);
65 static int ccm4309_aes_nx_set_key(struct crypto_aead *tfm,
69 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&tfm->base);
76 memcpy(nx_ctx->priv.ccm.nonce, in_key + key_len, 3);
78 return ccm_aes_nx_set_key(tfm, in_key, key_len);
81 static int ccm_aes_nx_setauthsize(struct crypto_aead *tfm,
82 unsigned int authsize)
97 crypto_aead_crt(tfm)->authsize = authsize;
102 static int ccm4309_aes_nx_setauthsize(struct crypto_aead *tfm,
103 unsigned int authsize)
114 crypto_aead_crt(tfm)->authsize = authsize;
119 /* taken from crypto/ccm.c */
120 static int set_msg_len(u8 *block, unsigned int msglen, int csize)
124 memset(block, 0, csize);
129 else if (msglen > (unsigned int)(1 << (8 * csize)))
132 data = cpu_to_be32(msglen);
133 memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
138 /* taken from crypto/ccm.c */
139 static inline int crypto_ccm_check_iv(const u8 *iv)
141 /* 2 <= L <= 8, so 1 <= L' <= 7. */
142 if (1 > iv[0] || iv[0] > 7)
148 /* based on code from crypto/ccm.c */
149 static int generate_b0(u8 *iv, unsigned int assoclen, unsigned int authsize,
150 unsigned int cryptlen, u8 *b0)
152 unsigned int l, lp, m = authsize;
160 /* set m, bits 3-5 */
161 *b0 |= (8 * ((m - 2) / 2));
163 /* set adata, bit 6, if associated data is used */
167 rc = set_msg_len(b0 + 16 - l, cryptlen, l);
172 static int generate_pat(u8 *iv,
173 struct aead_request *req,
174 struct nx_crypto_ctx *nx_ctx,
175 unsigned int authsize,
179 struct nx_sg *nx_insg = nx_ctx->in_sg;
180 struct nx_sg *nx_outsg = nx_ctx->out_sg;
181 unsigned int iauth_len = 0;
182 struct vio_pfo_op *op = NULL;
183 u8 tmp[16], *b1 = NULL, *b0 = NULL, *result = NULL;
186 /* zero the ctr value */
187 memset(iv + 15 - iv[0], 0, iv[0] + 1);
189 if (!req->assoclen) {
190 b0 = nx_ctx->csbcpb->cpb.aes_ccm.in_pat_or_b0;
191 } else if (req->assoclen <= 14) {
192 /* if associated data is 14 bytes or less, we do 1 GCM
193 * operation on 2 AES blocks, B0 (stored in the csbcpb) and B1,
194 * which is fed in through the source buffers here */
195 b0 = nx_ctx->csbcpb->cpb.aes_ccm.in_pat_or_b0;
196 b1 = nx_ctx->priv.ccm.iauth_tag;
197 iauth_len = req->assoclen;
199 nx_insg = nx_build_sg_list(nx_insg, b1, 16, nx_ctx->ap->sglen);
200 nx_outsg = nx_build_sg_list(nx_outsg, tmp, 16,
203 /* inlen should be negative, indicating to phyp that its a
204 * pointer to an sg list */
205 nx_ctx->op.inlen = (nx_ctx->in_sg - nx_insg) *
206 sizeof(struct nx_sg);
207 nx_ctx->op.outlen = (nx_ctx->out_sg - nx_outsg) *
208 sizeof(struct nx_sg);
210 NX_CPB_FDM(nx_ctx->csbcpb) |= NX_FDM_ENDE_ENCRYPT;
211 NX_CPB_FDM(nx_ctx->csbcpb) |= NX_FDM_INTERMEDIATE;
214 result = nx_ctx->csbcpb->cpb.aes_ccm.out_pat_or_mac;
215 } else if (req->assoclen <= 65280) {
216 /* if associated data is less than (2^16 - 2^8), we construct
217 * B1 differently and feed in the associated data to a CCA
219 b0 = nx_ctx->csbcpb_aead->cpb.aes_cca.b0;
220 b1 = nx_ctx->csbcpb_aead->cpb.aes_cca.b1;
223 /* remaining assoc data must have scatterlist built for it */
224 nx_insg = nx_walk_and_build(nx_insg, nx_ctx->ap->sglen,
225 req->assoc, iauth_len,
226 req->assoclen - iauth_len);
227 nx_ctx->op_aead.inlen = (nx_ctx->in_sg - nx_insg) *
228 sizeof(struct nx_sg);
230 op = &nx_ctx->op_aead;
231 result = nx_ctx->csbcpb_aead->cpb.aes_cca.out_pat_or_b0;
233 /* if associated data is less than (2^32), we construct B1
234 * differently yet again and feed in the associated data to a
236 pr_err("associated data len is %u bytes (returning -EINVAL)\n",
241 rc = generate_b0(iv, req->assoclen, authsize, nbytes, b0);
247 *(u16 *)b1 = (u16)req->assoclen;
249 scatterwalk_map_and_copy(b1 + 2, req->assoc, 0,
250 iauth_len, SCATTERWALK_FROM_SG);
252 rc = nx_hcall_sync(nx_ctx, op,
253 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
257 atomic_inc(&(nx_ctx->stats->aes_ops));
258 atomic64_add(req->assoclen, &(nx_ctx->stats->aes_bytes));
260 memcpy(out, result, AES_BLOCK_SIZE);
266 static int ccm_nx_decrypt(struct aead_request *req,
267 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 unsigned int nbytes = req->cryptlen;
272 unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
273 struct nx_ccm_priv *priv = &nx_ctx->priv.ccm;
274 unsigned long irq_flags;
277 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
279 if (nbytes > nx_ctx->ap->databytelen) {
286 /* copy out the auth tag to compare with later */
287 scatterwalk_map_and_copy(priv->oauth_tag,
288 req->src, nbytes, authsize,
289 SCATTERWALK_FROM_SG);
291 rc = generate_pat(desc->info, req, nx_ctx, authsize, nbytes,
292 csbcpb->cpb.aes_ccm.in_pat_or_b0);
296 rc = nx_build_sg_lists(nx_ctx, desc, req->dst, req->src, nbytes, 0,
297 csbcpb->cpb.aes_ccm.iv_or_ctr);
301 NX_CPB_FDM(nx_ctx->csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
302 NX_CPB_FDM(nx_ctx->csbcpb) &= ~NX_FDM_INTERMEDIATE;
304 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
305 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
309 atomic_inc(&(nx_ctx->stats->aes_ops));
310 atomic64_add(csbcpb->csb.processed_byte_count,
311 &(nx_ctx->stats->aes_bytes));
313 rc = memcmp(csbcpb->cpb.aes_ccm.out_pat_or_mac, priv->oauth_tag,
314 authsize) ? -EBADMSG : 0;
316 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
320 static int ccm_nx_encrypt(struct aead_request *req,
321 struct blkcipher_desc *desc)
323 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
324 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
325 unsigned int nbytes = req->cryptlen;
326 unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
327 unsigned long irq_flags;
330 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
332 if (nbytes > nx_ctx->ap->databytelen) {
337 rc = generate_pat(desc->info, req, nx_ctx, authsize, nbytes,
338 csbcpb->cpb.aes_ccm.in_pat_or_b0);
342 rc = nx_build_sg_lists(nx_ctx, desc, req->dst, req->src, nbytes, 0,
343 csbcpb->cpb.aes_ccm.iv_or_ctr);
347 NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
348 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
350 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
351 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
355 atomic_inc(&(nx_ctx->stats->aes_ops));
356 atomic64_add(csbcpb->csb.processed_byte_count,
357 &(nx_ctx->stats->aes_bytes));
359 /* copy out the auth tag */
360 scatterwalk_map_and_copy(csbcpb->cpb.aes_ccm.out_pat_or_mac,
361 req->dst, nbytes, authsize,
364 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
368 static int ccm4309_aes_nx_encrypt(struct aead_request *req)
370 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
371 struct blkcipher_desc desc;
372 u8 *iv = nx_ctx->priv.ccm.iv;
375 memcpy(iv + 1, nx_ctx->priv.ccm.nonce, 3);
376 memcpy(iv + 4, req->iv, 8);
379 desc.tfm = (struct crypto_blkcipher *)req->base.tfm;
381 return ccm_nx_encrypt(req, &desc);
384 static int ccm_aes_nx_encrypt(struct aead_request *req)
386 struct blkcipher_desc desc;
390 desc.tfm = (struct crypto_blkcipher *)req->base.tfm;
392 rc = crypto_ccm_check_iv(desc.info);
396 return ccm_nx_encrypt(req, &desc);
399 static int ccm4309_aes_nx_decrypt(struct aead_request *req)
401 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
402 struct blkcipher_desc desc;
403 u8 *iv = nx_ctx->priv.ccm.iv;
406 memcpy(iv + 1, nx_ctx->priv.ccm.nonce, 3);
407 memcpy(iv + 4, req->iv, 8);
410 desc.tfm = (struct crypto_blkcipher *)req->base.tfm;
412 return ccm_nx_decrypt(req, &desc);
415 static int ccm_aes_nx_decrypt(struct aead_request *req)
417 struct blkcipher_desc desc;
421 desc.tfm = (struct crypto_blkcipher *)req->base.tfm;
423 rc = crypto_ccm_check_iv(desc.info);
427 return ccm_nx_decrypt(req, &desc);
430 /* tell the block cipher walk routines that this is a stream cipher by
431 * setting cra_blocksize to 1. Even using blkcipher_walk_virt_block
432 * during encrypt/decrypt doesn't solve this problem, because it calls
433 * blkcipher_walk_done under the covers, which doesn't use walk->blocksize,
434 * but instead uses this tfm->blocksize. */
435 struct crypto_alg nx_ccm_aes_alg = {
436 .cra_name = "ccm(aes)",
437 .cra_driver_name = "ccm-aes-nx",
439 .cra_flags = CRYPTO_ALG_TYPE_AEAD |
440 CRYPTO_ALG_NEED_FALLBACK,
442 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
443 .cra_type = &crypto_aead_type,
444 .cra_module = THIS_MODULE,
445 .cra_init = nx_crypto_ctx_aes_ccm_init,
446 .cra_exit = nx_crypto_ctx_exit,
448 .ivsize = AES_BLOCK_SIZE,
449 .maxauthsize = AES_BLOCK_SIZE,
450 .setkey = ccm_aes_nx_set_key,
451 .setauthsize = ccm_aes_nx_setauthsize,
452 .encrypt = ccm_aes_nx_encrypt,
453 .decrypt = ccm_aes_nx_decrypt,
457 struct crypto_alg nx_ccm4309_aes_alg = {
458 .cra_name = "rfc4309(ccm(aes))",
459 .cra_driver_name = "rfc4309-ccm-aes-nx",
461 .cra_flags = CRYPTO_ALG_TYPE_AEAD |
462 CRYPTO_ALG_NEED_FALLBACK,
464 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
465 .cra_type = &crypto_nivaead_type,
466 .cra_module = THIS_MODULE,
467 .cra_init = nx_crypto_ctx_aes_ccm_init,
468 .cra_exit = nx_crypto_ctx_exit,
471 .maxauthsize = AES_BLOCK_SIZE,
472 .setkey = ccm4309_aes_nx_set_key,
473 .setauthsize = ccm4309_aes_nx_setauthsize,
474 .encrypt = ccm4309_aes_nx_encrypt,
475 .decrypt = ccm4309_aes_nx_decrypt,