dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / crypto / chelsio / chcr_algo.c
blob6c2cd36048ea2b3659b9f7de0f9d97e933d85a45
1 /*
2 * This file is part of the Chelsio T6 Crypto driver for Linux.
4 * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
34 * Written and Maintained by:
35 * Manoj Malviya (manojmalviya@chelsio.com)
36 * Atul Gupta (atul.gupta@chelsio.com)
37 * Jitendra Lulla (jlulla@chelsio.com)
38 * Yeshaswi M R Gowda (yeshaswi@chelsio.com)
39 * Harsh Jain (harsh@chelsio.com)
42 #define pr_fmt(fmt) "chcr:" fmt
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/crypto.h>
47 #include <linux/cryptohash.h>
48 #include <linux/skbuff.h>
49 #include <linux/rtnetlink.h>
50 #include <linux/highmem.h>
51 #include <linux/scatterlist.h>
53 #include <crypto/aes.h>
54 #include <crypto/algapi.h>
55 #include <crypto/hash.h>
56 #include <crypto/gcm.h>
57 #include <crypto/sha.h>
58 #include <crypto/authenc.h>
59 #include <crypto/ctr.h>
60 #include <crypto/gf128mul.h>
61 #include <crypto/internal/aead.h>
62 #include <crypto/null.h>
63 #include <crypto/internal/skcipher.h>
64 #include <crypto/aead.h>
65 #include <crypto/scatterwalk.h>
66 #include <crypto/internal/hash.h>
68 #include "t4fw_api.h"
69 #include "t4_msg.h"
70 #include "chcr_core.h"
71 #include "chcr_algo.h"
72 #include "chcr_crypto.h"
74 #define IV AES_BLOCK_SIZE
76 static unsigned int sgl_ent_len[] = {
77 0, 0, 16, 24, 40, 48, 64, 72, 88,
78 96, 112, 120, 136, 144, 160, 168, 184,
79 192, 208, 216, 232, 240, 256, 264, 280,
80 288, 304, 312, 328, 336, 352, 360, 376
83 static unsigned int dsgl_ent_len[] = {
84 0, 32, 32, 48, 48, 64, 64, 80, 80,
85 112, 112, 128, 128, 144, 144, 160, 160,
86 192, 192, 208, 208, 224, 224, 240, 240,
87 272, 272, 288, 288, 304, 304, 320, 320
90 static u32 round_constant[11] = {
91 0x01000000, 0x02000000, 0x04000000, 0x08000000,
92 0x10000000, 0x20000000, 0x40000000, 0x80000000,
93 0x1B000000, 0x36000000, 0x6C000000
96 static int chcr_handle_cipher_resp(struct skcipher_request *req,
97 unsigned char *input, int err);
99 static inline struct chcr_aead_ctx *AEAD_CTX(struct chcr_context *ctx)
101 return ctx->crypto_ctx->aeadctx;
104 static inline struct ablk_ctx *ABLK_CTX(struct chcr_context *ctx)
106 return ctx->crypto_ctx->ablkctx;
109 static inline struct hmac_ctx *HMAC_CTX(struct chcr_context *ctx)
111 return ctx->crypto_ctx->hmacctx;
114 static inline struct chcr_gcm_ctx *GCM_CTX(struct chcr_aead_ctx *gctx)
116 return gctx->ctx->gcm;
119 static inline struct chcr_authenc_ctx *AUTHENC_CTX(struct chcr_aead_ctx *gctx)
121 return gctx->ctx->authenc;
124 static inline struct uld_ctx *ULD_CTX(struct chcr_context *ctx)
126 return container_of(ctx->dev, struct uld_ctx, dev);
129 static inline int is_ofld_imm(const struct sk_buff *skb)
131 return (skb->len <= SGE_MAX_WR_LEN);
134 static inline void chcr_init_hctx_per_wr(struct chcr_ahash_req_ctx *reqctx)
136 memset(&reqctx->hctx_wr, 0, sizeof(struct chcr_hctx_per_wr));
139 static int sg_nents_xlen(struct scatterlist *sg, unsigned int reqlen,
140 unsigned int entlen,
141 unsigned int skip)
143 int nents = 0;
144 unsigned int less;
145 unsigned int skip_len = 0;
147 while (sg && skip) {
148 if (sg_dma_len(sg) <= skip) {
149 skip -= sg_dma_len(sg);
150 skip_len = 0;
151 sg = sg_next(sg);
152 } else {
153 skip_len = skip;
154 skip = 0;
158 while (sg && reqlen) {
159 less = min(reqlen, sg_dma_len(sg) - skip_len);
160 nents += DIV_ROUND_UP(less, entlen);
161 reqlen -= less;
162 skip_len = 0;
163 sg = sg_next(sg);
165 return nents;
168 static inline int get_aead_subtype(struct crypto_aead *aead)
170 struct aead_alg *alg = crypto_aead_alg(aead);
171 struct chcr_alg_template *chcr_crypto_alg =
172 container_of(alg, struct chcr_alg_template, alg.aead);
173 return chcr_crypto_alg->type & CRYPTO_ALG_SUB_TYPE_MASK;
176 void chcr_verify_tag(struct aead_request *req, u8 *input, int *err)
178 u8 temp[SHA512_DIGEST_SIZE];
179 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
180 int authsize = crypto_aead_authsize(tfm);
181 struct cpl_fw6_pld *fw6_pld;
182 int cmp = 0;
184 fw6_pld = (struct cpl_fw6_pld *)input;
185 if ((get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) ||
186 (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_GCM)) {
187 cmp = crypto_memneq(&fw6_pld->data[2], (fw6_pld + 1), authsize);
188 } else {
190 sg_pcopy_to_buffer(req->src, sg_nents(req->src), temp,
191 authsize, req->assoclen +
192 req->cryptlen - authsize);
193 cmp = crypto_memneq(temp, (fw6_pld + 1), authsize);
195 if (cmp)
196 *err = -EBADMSG;
197 else
198 *err = 0;
201 static int chcr_inc_wrcount(struct chcr_dev *dev)
203 if (dev->state == CHCR_DETACH)
204 return 1;
205 atomic_inc(&dev->inflight);
206 return 0;
209 static inline void chcr_dec_wrcount(struct chcr_dev *dev)
211 atomic_dec(&dev->inflight);
214 static inline int chcr_handle_aead_resp(struct aead_request *req,
215 unsigned char *input,
216 int err)
218 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
219 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
220 struct chcr_dev *dev = a_ctx(tfm)->dev;
222 chcr_aead_common_exit(req);
223 if (reqctx->verify == VERIFY_SW) {
224 chcr_verify_tag(req, input, &err);
225 reqctx->verify = VERIFY_HW;
227 chcr_dec_wrcount(dev);
228 req->base.complete(&req->base, err);
230 return err;
233 static void get_aes_decrypt_key(unsigned char *dec_key,
234 const unsigned char *key,
235 unsigned int keylength)
237 u32 temp;
238 u32 w_ring[MAX_NK];
239 int i, j, k;
240 u8 nr, nk;
242 switch (keylength) {
243 case AES_KEYLENGTH_128BIT:
244 nk = KEYLENGTH_4BYTES;
245 nr = NUMBER_OF_ROUNDS_10;
246 break;
247 case AES_KEYLENGTH_192BIT:
248 nk = KEYLENGTH_6BYTES;
249 nr = NUMBER_OF_ROUNDS_12;
250 break;
251 case AES_KEYLENGTH_256BIT:
252 nk = KEYLENGTH_8BYTES;
253 nr = NUMBER_OF_ROUNDS_14;
254 break;
255 default:
256 return;
258 for (i = 0; i < nk; i++)
259 w_ring[i] = be32_to_cpu(*(u32 *)&key[4 * i]);
261 i = 0;
262 temp = w_ring[nk - 1];
263 while (i + nk < (nr + 1) * 4) {
264 if (!(i % nk)) {
265 /* RotWord(temp) */
266 temp = (temp << 8) | (temp >> 24);
267 temp = aes_ks_subword(temp);
268 temp ^= round_constant[i / nk];
269 } else if (nk == 8 && (i % 4 == 0)) {
270 temp = aes_ks_subword(temp);
272 w_ring[i % nk] ^= temp;
273 temp = w_ring[i % nk];
274 i++;
276 i--;
277 for (k = 0, j = i % nk; k < nk; k++) {
278 *((u32 *)dec_key + k) = htonl(w_ring[j]);
279 j--;
280 if (j < 0)
281 j += nk;
285 static struct crypto_shash *chcr_alloc_shash(unsigned int ds)
287 struct crypto_shash *base_hash = ERR_PTR(-EINVAL);
289 switch (ds) {
290 case SHA1_DIGEST_SIZE:
291 base_hash = crypto_alloc_shash("sha1", 0, 0);
292 break;
293 case SHA224_DIGEST_SIZE:
294 base_hash = crypto_alloc_shash("sha224", 0, 0);
295 break;
296 case SHA256_DIGEST_SIZE:
297 base_hash = crypto_alloc_shash("sha256", 0, 0);
298 break;
299 case SHA384_DIGEST_SIZE:
300 base_hash = crypto_alloc_shash("sha384", 0, 0);
301 break;
302 case SHA512_DIGEST_SIZE:
303 base_hash = crypto_alloc_shash("sha512", 0, 0);
304 break;
307 return base_hash;
310 static int chcr_compute_partial_hash(struct shash_desc *desc,
311 char *iopad, char *result_hash,
312 int digest_size)
314 struct sha1_state sha1_st;
315 struct sha256_state sha256_st;
316 struct sha512_state sha512_st;
317 int error;
319 if (digest_size == SHA1_DIGEST_SIZE) {
320 error = crypto_shash_init(desc) ?:
321 crypto_shash_update(desc, iopad, SHA1_BLOCK_SIZE) ?:
322 crypto_shash_export(desc, (void *)&sha1_st);
323 memcpy(result_hash, sha1_st.state, SHA1_DIGEST_SIZE);
324 } else if (digest_size == SHA224_DIGEST_SIZE) {
325 error = crypto_shash_init(desc) ?:
326 crypto_shash_update(desc, iopad, SHA256_BLOCK_SIZE) ?:
327 crypto_shash_export(desc, (void *)&sha256_st);
328 memcpy(result_hash, sha256_st.state, SHA256_DIGEST_SIZE);
330 } else if (digest_size == SHA256_DIGEST_SIZE) {
331 error = crypto_shash_init(desc) ?:
332 crypto_shash_update(desc, iopad, SHA256_BLOCK_SIZE) ?:
333 crypto_shash_export(desc, (void *)&sha256_st);
334 memcpy(result_hash, sha256_st.state, SHA256_DIGEST_SIZE);
336 } else if (digest_size == SHA384_DIGEST_SIZE) {
337 error = crypto_shash_init(desc) ?:
338 crypto_shash_update(desc, iopad, SHA512_BLOCK_SIZE) ?:
339 crypto_shash_export(desc, (void *)&sha512_st);
340 memcpy(result_hash, sha512_st.state, SHA512_DIGEST_SIZE);
342 } else if (digest_size == SHA512_DIGEST_SIZE) {
343 error = crypto_shash_init(desc) ?:
344 crypto_shash_update(desc, iopad, SHA512_BLOCK_SIZE) ?:
345 crypto_shash_export(desc, (void *)&sha512_st);
346 memcpy(result_hash, sha512_st.state, SHA512_DIGEST_SIZE);
347 } else {
348 error = -EINVAL;
349 pr_err("Unknown digest size %d\n", digest_size);
351 return error;
354 static void chcr_change_order(char *buf, int ds)
356 int i;
358 if (ds == SHA512_DIGEST_SIZE) {
359 for (i = 0; i < (ds / sizeof(u64)); i++)
360 *((__be64 *)buf + i) =
361 cpu_to_be64(*((u64 *)buf + i));
362 } else {
363 for (i = 0; i < (ds / sizeof(u32)); i++)
364 *((__be32 *)buf + i) =
365 cpu_to_be32(*((u32 *)buf + i));
369 static inline int is_hmac(struct crypto_tfm *tfm)
371 struct crypto_alg *alg = tfm->__crt_alg;
372 struct chcr_alg_template *chcr_crypto_alg =
373 container_of(__crypto_ahash_alg(alg), struct chcr_alg_template,
374 alg.hash);
375 if (chcr_crypto_alg->type == CRYPTO_ALG_TYPE_HMAC)
376 return 1;
377 return 0;
380 static inline void dsgl_walk_init(struct dsgl_walk *walk,
381 struct cpl_rx_phys_dsgl *dsgl)
383 walk->dsgl = dsgl;
384 walk->nents = 0;
385 walk->to = (struct phys_sge_pairs *)(dsgl + 1);
388 static inline void dsgl_walk_end(struct dsgl_walk *walk, unsigned short qid,
389 int pci_chan_id)
391 struct cpl_rx_phys_dsgl *phys_cpl;
393 phys_cpl = walk->dsgl;
395 phys_cpl->op_to_tid = htonl(CPL_RX_PHYS_DSGL_OPCODE_V(CPL_RX_PHYS_DSGL)
396 | CPL_RX_PHYS_DSGL_ISRDMA_V(0));
397 phys_cpl->pcirlxorder_to_noofsgentr =
398 htonl(CPL_RX_PHYS_DSGL_PCIRLXORDER_V(0) |
399 CPL_RX_PHYS_DSGL_PCINOSNOOP_V(0) |
400 CPL_RX_PHYS_DSGL_PCITPHNTENB_V(0) |
401 CPL_RX_PHYS_DSGL_PCITPHNT_V(0) |
402 CPL_RX_PHYS_DSGL_DCAID_V(0) |
403 CPL_RX_PHYS_DSGL_NOOFSGENTR_V(walk->nents));
404 phys_cpl->rss_hdr_int.opcode = CPL_RX_PHYS_ADDR;
405 phys_cpl->rss_hdr_int.qid = htons(qid);
406 phys_cpl->rss_hdr_int.hash_val = 0;
407 phys_cpl->rss_hdr_int.channel = pci_chan_id;
410 static inline void dsgl_walk_add_page(struct dsgl_walk *walk,
411 size_t size,
412 dma_addr_t addr)
414 int j;
416 if (!size)
417 return;
418 j = walk->nents;
419 walk->to->len[j % 8] = htons(size);
420 walk->to->addr[j % 8] = cpu_to_be64(addr);
421 j++;
422 if ((j % 8) == 0)
423 walk->to++;
424 walk->nents = j;
427 static void dsgl_walk_add_sg(struct dsgl_walk *walk,
428 struct scatterlist *sg,
429 unsigned int slen,
430 unsigned int skip)
432 int skip_len = 0;
433 unsigned int left_size = slen, len = 0;
434 unsigned int j = walk->nents;
435 int offset, ent_len;
437 if (!slen)
438 return;
439 while (sg && skip) {
440 if (sg_dma_len(sg) <= skip) {
441 skip -= sg_dma_len(sg);
442 skip_len = 0;
443 sg = sg_next(sg);
444 } else {
445 skip_len = skip;
446 skip = 0;
450 while (left_size && sg) {
451 len = min_t(u32, left_size, sg_dma_len(sg) - skip_len);
452 offset = 0;
453 while (len) {
454 ent_len = min_t(u32, len, CHCR_DST_SG_SIZE);
455 walk->to->len[j % 8] = htons(ent_len);
456 walk->to->addr[j % 8] = cpu_to_be64(sg_dma_address(sg) +
457 offset + skip_len);
458 offset += ent_len;
459 len -= ent_len;
460 j++;
461 if ((j % 8) == 0)
462 walk->to++;
464 walk->last_sg = sg;
465 walk->last_sg_len = min_t(u32, left_size, sg_dma_len(sg) -
466 skip_len) + skip_len;
467 left_size -= min_t(u32, left_size, sg_dma_len(sg) - skip_len);
468 skip_len = 0;
469 sg = sg_next(sg);
471 walk->nents = j;
474 static inline void ulptx_walk_init(struct ulptx_walk *walk,
475 struct ulptx_sgl *ulp)
477 walk->sgl = ulp;
478 walk->nents = 0;
479 walk->pair_idx = 0;
480 walk->pair = ulp->sge;
481 walk->last_sg = NULL;
482 walk->last_sg_len = 0;
485 static inline void ulptx_walk_end(struct ulptx_walk *walk)
487 walk->sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
488 ULPTX_NSGE_V(walk->nents));
492 static inline void ulptx_walk_add_page(struct ulptx_walk *walk,
493 size_t size,
494 dma_addr_t addr)
496 if (!size)
497 return;
499 if (walk->nents == 0) {
500 walk->sgl->len0 = cpu_to_be32(size);
501 walk->sgl->addr0 = cpu_to_be64(addr);
502 } else {
503 walk->pair->addr[walk->pair_idx] = cpu_to_be64(addr);
504 walk->pair->len[walk->pair_idx] = cpu_to_be32(size);
505 walk->pair_idx = !walk->pair_idx;
506 if (!walk->pair_idx)
507 walk->pair++;
509 walk->nents++;
512 static void ulptx_walk_add_sg(struct ulptx_walk *walk,
513 struct scatterlist *sg,
514 unsigned int len,
515 unsigned int skip)
517 int small;
518 int skip_len = 0;
519 unsigned int sgmin;
521 if (!len)
522 return;
523 while (sg && skip) {
524 if (sg_dma_len(sg) <= skip) {
525 skip -= sg_dma_len(sg);
526 skip_len = 0;
527 sg = sg_next(sg);
528 } else {
529 skip_len = skip;
530 skip = 0;
533 WARN(!sg, "SG should not be null here\n");
534 if (sg && (walk->nents == 0)) {
535 small = min_t(unsigned int, sg_dma_len(sg) - skip_len, len);
536 sgmin = min_t(unsigned int, small, CHCR_SRC_SG_SIZE);
537 walk->sgl->len0 = cpu_to_be32(sgmin);
538 walk->sgl->addr0 = cpu_to_be64(sg_dma_address(sg) + skip_len);
539 walk->nents++;
540 len -= sgmin;
541 walk->last_sg = sg;
542 walk->last_sg_len = sgmin + skip_len;
543 skip_len += sgmin;
544 if (sg_dma_len(sg) == skip_len) {
545 sg = sg_next(sg);
546 skip_len = 0;
550 while (sg && len) {
551 small = min(sg_dma_len(sg) - skip_len, len);
552 sgmin = min_t(unsigned int, small, CHCR_SRC_SG_SIZE);
553 walk->pair->len[walk->pair_idx] = cpu_to_be32(sgmin);
554 walk->pair->addr[walk->pair_idx] =
555 cpu_to_be64(sg_dma_address(sg) + skip_len);
556 walk->pair_idx = !walk->pair_idx;
557 walk->nents++;
558 if (!walk->pair_idx)
559 walk->pair++;
560 len -= sgmin;
561 skip_len += sgmin;
562 walk->last_sg = sg;
563 walk->last_sg_len = skip_len;
564 if (sg_dma_len(sg) == skip_len) {
565 sg = sg_next(sg);
566 skip_len = 0;
571 static inline int get_cryptoalg_subtype(struct crypto_skcipher *tfm)
573 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
574 struct chcr_alg_template *chcr_crypto_alg =
575 container_of(alg, struct chcr_alg_template, alg.skcipher);
577 return chcr_crypto_alg->type & CRYPTO_ALG_SUB_TYPE_MASK;
580 static int cxgb4_is_crypto_q_full(struct net_device *dev, unsigned int idx)
582 struct adapter *adap = netdev2adap(dev);
583 struct sge_uld_txq_info *txq_info =
584 adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
585 struct sge_uld_txq *txq;
586 int ret = 0;
588 local_bh_disable();
589 txq = &txq_info->uldtxq[idx];
590 spin_lock(&txq->sendq.lock);
591 if (txq->full)
592 ret = -1;
593 spin_unlock(&txq->sendq.lock);
594 local_bh_enable();
595 return ret;
598 static int generate_copy_rrkey(struct ablk_ctx *ablkctx,
599 struct _key_ctx *key_ctx)
601 if (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) {
602 memcpy(key_ctx->key, ablkctx->rrkey, ablkctx->enckey_len);
603 } else {
604 memcpy(key_ctx->key,
605 ablkctx->key + (ablkctx->enckey_len >> 1),
606 ablkctx->enckey_len >> 1);
607 memcpy(key_ctx->key + (ablkctx->enckey_len >> 1),
608 ablkctx->rrkey, ablkctx->enckey_len >> 1);
610 return 0;
613 static int chcr_hash_ent_in_wr(struct scatterlist *src,
614 unsigned int minsg,
615 unsigned int space,
616 unsigned int srcskip)
618 int srclen = 0;
619 int srcsg = minsg;
620 int soffset = 0, sless;
622 if (sg_dma_len(src) == srcskip) {
623 src = sg_next(src);
624 srcskip = 0;
626 while (src && space > (sgl_ent_len[srcsg + 1])) {
627 sless = min_t(unsigned int, sg_dma_len(src) - soffset - srcskip,
628 CHCR_SRC_SG_SIZE);
629 srclen += sless;
630 soffset += sless;
631 srcsg++;
632 if (sg_dma_len(src) == (soffset + srcskip)) {
633 src = sg_next(src);
634 soffset = 0;
635 srcskip = 0;
638 return srclen;
641 static int chcr_sg_ent_in_wr(struct scatterlist *src,
642 struct scatterlist *dst,
643 unsigned int minsg,
644 unsigned int space,
645 unsigned int srcskip,
646 unsigned int dstskip)
648 int srclen = 0, dstlen = 0;
649 int srcsg = minsg, dstsg = minsg;
650 int offset = 0, soffset = 0, less, sless = 0;
652 if (sg_dma_len(src) == srcskip) {
653 src = sg_next(src);
654 srcskip = 0;
656 if (sg_dma_len(dst) == dstskip) {
657 dst = sg_next(dst);
658 dstskip = 0;
661 while (src && dst &&
662 space > (sgl_ent_len[srcsg + 1] + dsgl_ent_len[dstsg])) {
663 sless = min_t(unsigned int, sg_dma_len(src) - srcskip - soffset,
664 CHCR_SRC_SG_SIZE);
665 srclen += sless;
666 srcsg++;
667 offset = 0;
668 while (dst && ((dstsg + 1) <= MAX_DSGL_ENT) &&
669 space > (sgl_ent_len[srcsg] + dsgl_ent_len[dstsg + 1])) {
670 if (srclen <= dstlen)
671 break;
672 less = min_t(unsigned int, sg_dma_len(dst) - offset -
673 dstskip, CHCR_DST_SG_SIZE);
674 dstlen += less;
675 offset += less;
676 if ((offset + dstskip) == sg_dma_len(dst)) {
677 dst = sg_next(dst);
678 offset = 0;
680 dstsg++;
681 dstskip = 0;
683 soffset += sless;
684 if ((soffset + srcskip) == sg_dma_len(src)) {
685 src = sg_next(src);
686 srcskip = 0;
687 soffset = 0;
691 return min(srclen, dstlen);
694 static int chcr_cipher_fallback(struct crypto_sync_skcipher *cipher,
695 u32 flags,
696 struct scatterlist *src,
697 struct scatterlist *dst,
698 unsigned int nbytes,
699 u8 *iv,
700 unsigned short op_type)
702 int err;
704 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, cipher);
706 skcipher_request_set_sync_tfm(subreq, cipher);
707 skcipher_request_set_callback(subreq, flags, NULL, NULL);
708 skcipher_request_set_crypt(subreq, src, dst,
709 nbytes, iv);
711 err = op_type ? crypto_skcipher_decrypt(subreq) :
712 crypto_skcipher_encrypt(subreq);
713 skcipher_request_zero(subreq);
715 return err;
719 static inline int get_qidxs(struct crypto_async_request *req,
720 unsigned int *txqidx, unsigned int *rxqidx)
722 struct crypto_tfm *tfm = req->tfm;
723 int ret = 0;
725 switch (tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
726 case CRYPTO_ALG_TYPE_AEAD:
728 struct aead_request *aead_req =
729 container_of(req, struct aead_request, base);
730 struct chcr_aead_reqctx *reqctx = aead_request_ctx(aead_req);
731 *txqidx = reqctx->txqidx;
732 *rxqidx = reqctx->rxqidx;
733 break;
735 case CRYPTO_ALG_TYPE_SKCIPHER:
737 struct skcipher_request *sk_req =
738 container_of(req, struct skcipher_request, base);
739 struct chcr_skcipher_req_ctx *reqctx =
740 skcipher_request_ctx(sk_req);
741 *txqidx = reqctx->txqidx;
742 *rxqidx = reqctx->rxqidx;
743 break;
745 case CRYPTO_ALG_TYPE_AHASH:
747 struct ahash_request *ahash_req =
748 container_of(req, struct ahash_request, base);
749 struct chcr_ahash_req_ctx *reqctx =
750 ahash_request_ctx(ahash_req);
751 *txqidx = reqctx->txqidx;
752 *rxqidx = reqctx->rxqidx;
753 break;
755 default:
756 ret = -EINVAL;
757 /* should never get here */
758 BUG();
759 break;
761 return ret;
764 static inline void create_wreq(struct chcr_context *ctx,
765 struct chcr_wr *chcr_req,
766 struct crypto_async_request *req,
767 unsigned int imm,
768 int hash_sz,
769 unsigned int len16,
770 unsigned int sc_len,
771 unsigned int lcb)
773 struct uld_ctx *u_ctx = ULD_CTX(ctx);
774 unsigned int tx_channel_id, rx_channel_id;
775 unsigned int txqidx = 0, rxqidx = 0;
776 unsigned int qid, fid;
778 get_qidxs(req, &txqidx, &rxqidx);
779 qid = u_ctx->lldi.rxq_ids[rxqidx];
780 fid = u_ctx->lldi.rxq_ids[0];
781 tx_channel_id = txqidx / ctx->txq_perchan;
782 rx_channel_id = rxqidx / ctx->rxq_perchan;
785 chcr_req->wreq.op_to_cctx_size = FILL_WR_OP_CCTX_SIZE;
786 chcr_req->wreq.pld_size_hash_size =
787 htonl(FW_CRYPTO_LOOKASIDE_WR_HASH_SIZE_V(hash_sz));
788 chcr_req->wreq.len16_pkd =
789 htonl(FW_CRYPTO_LOOKASIDE_WR_LEN16_V(DIV_ROUND_UP(len16, 16)));
790 chcr_req->wreq.cookie = cpu_to_be64((uintptr_t)req);
791 chcr_req->wreq.rx_chid_to_rx_q_id = FILL_WR_RX_Q_ID(rx_channel_id, qid,
792 !!lcb, txqidx);
794 chcr_req->ulptx.cmd_dest = FILL_ULPTX_CMD_DEST(tx_channel_id, fid);
795 chcr_req->ulptx.len = htonl((DIV_ROUND_UP(len16, 16) -
796 ((sizeof(chcr_req->wreq)) >> 4)));
797 chcr_req->sc_imm.cmd_more = FILL_CMD_MORE(!imm);
798 chcr_req->sc_imm.len = cpu_to_be32(sizeof(struct cpl_tx_sec_pdu) +
799 sizeof(chcr_req->key_ctx) + sc_len);
803 * create_cipher_wr - form the WR for cipher operations
804 * @req: cipher req.
805 * @ctx: crypto driver context of the request.
806 * @qid: ingress qid where response of this WR should be received.
807 * @op_type: encryption or decryption
809 static struct sk_buff *create_cipher_wr(struct cipher_wr_param *wrparam)
811 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(wrparam->req);
812 struct chcr_context *ctx = c_ctx(tfm);
813 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
814 struct sk_buff *skb = NULL;
815 struct chcr_wr *chcr_req;
816 struct cpl_rx_phys_dsgl *phys_cpl;
817 struct ulptx_sgl *ulptx;
818 struct chcr_skcipher_req_ctx *reqctx =
819 skcipher_request_ctx(wrparam->req);
820 unsigned int temp = 0, transhdr_len, dst_size;
821 int error;
822 int nents;
823 unsigned int kctx_len;
824 gfp_t flags = wrparam->req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
825 GFP_KERNEL : GFP_ATOMIC;
826 struct adapter *adap = padap(ctx->dev);
827 unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
829 nents = sg_nents_xlen(reqctx->dstsg, wrparam->bytes, CHCR_DST_SG_SIZE,
830 reqctx->dst_ofst);
831 dst_size = get_space_for_phys_dsgl(nents);
832 kctx_len = roundup(ablkctx->enckey_len, 16);
833 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
834 nents = sg_nents_xlen(reqctx->srcsg, wrparam->bytes,
835 CHCR_SRC_SG_SIZE, reqctx->src_ofst);
836 temp = reqctx->imm ? roundup(wrparam->bytes, 16) :
837 (sgl_len(nents) * 8);
838 transhdr_len += temp;
839 transhdr_len = roundup(transhdr_len, 16);
840 skb = alloc_skb(SGE_MAX_WR_LEN, flags);
841 if (!skb) {
842 error = -ENOMEM;
843 goto err;
845 chcr_req = __skb_put_zero(skb, transhdr_len);
846 chcr_req->sec_cpl.op_ivinsrtofst =
847 FILL_SEC_CPL_OP_IVINSR(rx_channel_id, 2, 1);
849 chcr_req->sec_cpl.pldlen = htonl(IV + wrparam->bytes);
850 chcr_req->sec_cpl.aadstart_cipherstop_hi =
851 FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, IV + 1, 0);
853 chcr_req->sec_cpl.cipherstop_lo_authinsert =
854 FILL_SEC_CPL_AUTHINSERT(0, 0, 0, 0);
855 chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op, 0,
856 ablkctx->ciph_mode,
857 0, 0, IV >> 1);
858 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 0,
859 0, 1, dst_size);
861 chcr_req->key_ctx.ctx_hdr = ablkctx->key_ctx_hdr;
862 if ((reqctx->op == CHCR_DECRYPT_OP) &&
863 (!(get_cryptoalg_subtype(tfm) ==
864 CRYPTO_ALG_SUB_TYPE_CTR)) &&
865 (!(get_cryptoalg_subtype(tfm) ==
866 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686))) {
867 generate_copy_rrkey(ablkctx, &chcr_req->key_ctx);
868 } else {
869 if ((ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) ||
870 (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CTR)) {
871 memcpy(chcr_req->key_ctx.key, ablkctx->key,
872 ablkctx->enckey_len);
873 } else {
874 memcpy(chcr_req->key_ctx.key, ablkctx->key +
875 (ablkctx->enckey_len >> 1),
876 ablkctx->enckey_len >> 1);
877 memcpy(chcr_req->key_ctx.key +
878 (ablkctx->enckey_len >> 1),
879 ablkctx->key,
880 ablkctx->enckey_len >> 1);
883 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
884 ulptx = (struct ulptx_sgl *)((u8 *)(phys_cpl + 1) + dst_size);
885 chcr_add_cipher_src_ent(wrparam->req, ulptx, wrparam);
886 chcr_add_cipher_dst_ent(wrparam->req, phys_cpl, wrparam, wrparam->qid);
888 atomic_inc(&adap->chcr_stats.cipher_rqst);
889 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + kctx_len + IV
890 + (reqctx->imm ? (wrparam->bytes) : 0);
891 create_wreq(c_ctx(tfm), chcr_req, &(wrparam->req->base), reqctx->imm, 0,
892 transhdr_len, temp,
893 ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC);
894 reqctx->skb = skb;
896 if (reqctx->op && (ablkctx->ciph_mode ==
897 CHCR_SCMD_CIPHER_MODE_AES_CBC))
898 sg_pcopy_to_buffer(wrparam->req->src,
899 sg_nents(wrparam->req->src), wrparam->req->iv, 16,
900 reqctx->processed + wrparam->bytes - AES_BLOCK_SIZE);
902 return skb;
903 err:
904 return ERR_PTR(error);
907 static inline int chcr_keyctx_ck_size(unsigned int keylen)
909 int ck_size = 0;
911 if (keylen == AES_KEYSIZE_128)
912 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
913 else if (keylen == AES_KEYSIZE_192)
914 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
915 else if (keylen == AES_KEYSIZE_256)
916 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
917 else
918 ck_size = 0;
920 return ck_size;
922 static int chcr_cipher_fallback_setkey(struct crypto_skcipher *cipher,
923 const u8 *key,
924 unsigned int keylen)
926 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
928 crypto_sync_skcipher_clear_flags(ablkctx->sw_cipher,
929 CRYPTO_TFM_REQ_MASK);
930 crypto_sync_skcipher_set_flags(ablkctx->sw_cipher,
931 cipher->base.crt_flags & CRYPTO_TFM_REQ_MASK);
932 return crypto_sync_skcipher_setkey(ablkctx->sw_cipher, key, keylen);
935 static int chcr_aes_cbc_setkey(struct crypto_skcipher *cipher,
936 const u8 *key,
937 unsigned int keylen)
939 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
940 unsigned int ck_size, context_size;
941 u16 alignment = 0;
942 int err;
944 err = chcr_cipher_fallback_setkey(cipher, key, keylen);
945 if (err)
946 goto badkey_err;
948 ck_size = chcr_keyctx_ck_size(keylen);
949 alignment = ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192 ? 8 : 0;
950 memcpy(ablkctx->key, key, keylen);
951 ablkctx->enckey_len = keylen;
952 get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, keylen << 3);
953 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
954 keylen + alignment) >> 4;
956 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
957 0, 0, context_size);
958 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CBC;
959 return 0;
960 badkey_err:
961 ablkctx->enckey_len = 0;
963 return err;
966 static int chcr_aes_ctr_setkey(struct crypto_skcipher *cipher,
967 const u8 *key,
968 unsigned int keylen)
970 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
971 unsigned int ck_size, context_size;
972 u16 alignment = 0;
973 int err;
975 err = chcr_cipher_fallback_setkey(cipher, key, keylen);
976 if (err)
977 goto badkey_err;
978 ck_size = chcr_keyctx_ck_size(keylen);
979 alignment = (ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) ? 8 : 0;
980 memcpy(ablkctx->key, key, keylen);
981 ablkctx->enckey_len = keylen;
982 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
983 keylen + alignment) >> 4;
985 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
986 0, 0, context_size);
987 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR;
989 return 0;
990 badkey_err:
991 ablkctx->enckey_len = 0;
993 return err;
996 static int chcr_aes_rfc3686_setkey(struct crypto_skcipher *cipher,
997 const u8 *key,
998 unsigned int keylen)
1000 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
1001 unsigned int ck_size, context_size;
1002 u16 alignment = 0;
1003 int err;
1005 if (keylen < CTR_RFC3686_NONCE_SIZE)
1006 return -EINVAL;
1007 memcpy(ablkctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE),
1008 CTR_RFC3686_NONCE_SIZE);
1010 keylen -= CTR_RFC3686_NONCE_SIZE;
1011 err = chcr_cipher_fallback_setkey(cipher, key, keylen);
1012 if (err)
1013 goto badkey_err;
1015 ck_size = chcr_keyctx_ck_size(keylen);
1016 alignment = (ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) ? 8 : 0;
1017 memcpy(ablkctx->key, key, keylen);
1018 ablkctx->enckey_len = keylen;
1019 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
1020 keylen + alignment) >> 4;
1022 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
1023 0, 0, context_size);
1024 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR;
1026 return 0;
1027 badkey_err:
1028 ablkctx->enckey_len = 0;
1030 return err;
1032 static void ctr_add_iv(u8 *dstiv, u8 *srciv, u32 add)
1034 unsigned int size = AES_BLOCK_SIZE;
1035 __be32 *b = (__be32 *)(dstiv + size);
1036 u32 c, prev;
1038 memcpy(dstiv, srciv, AES_BLOCK_SIZE);
1039 for (; size >= 4; size -= 4) {
1040 prev = be32_to_cpu(*--b);
1041 c = prev + add;
1042 *b = cpu_to_be32(c);
1043 if (prev < c)
1044 break;
1045 add = 1;
1050 static unsigned int adjust_ctr_overflow(u8 *iv, u32 bytes)
1052 __be32 *b = (__be32 *)(iv + AES_BLOCK_SIZE);
1053 u64 c;
1054 u32 temp = be32_to_cpu(*--b);
1056 temp = ~temp;
1057 c = (u64)temp + 1; // No of block can processed without overflow
1058 if ((bytes / AES_BLOCK_SIZE) >= c)
1059 bytes = c * AES_BLOCK_SIZE;
1060 return bytes;
1063 static int chcr_update_tweak(struct skcipher_request *req, u8 *iv,
1064 u32 isfinal)
1066 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1067 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
1068 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1069 struct crypto_aes_ctx aes;
1070 int ret, i;
1071 u8 *key;
1072 unsigned int keylen;
1073 int round = reqctx->last_req_len / AES_BLOCK_SIZE;
1074 int round8 = round / 8;
1076 memcpy(iv, reqctx->iv, AES_BLOCK_SIZE);
1078 keylen = ablkctx->enckey_len / 2;
1079 key = ablkctx->key + keylen;
1080 ret = aes_expandkey(&aes, key, keylen);
1081 if (ret)
1082 return ret;
1083 aes_encrypt(&aes, iv, iv);
1084 for (i = 0; i < round8; i++)
1085 gf128mul_x8_ble((le128 *)iv, (le128 *)iv);
1087 for (i = 0; i < (round % 8); i++)
1088 gf128mul_x_ble((le128 *)iv, (le128 *)iv);
1090 if (!isfinal)
1091 aes_decrypt(&aes, iv, iv);
1093 memzero_explicit(&aes, sizeof(aes));
1094 return 0;
1097 static int chcr_update_cipher_iv(struct skcipher_request *req,
1098 struct cpl_fw6_pld *fw6_pld, u8 *iv)
1100 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1101 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1102 int subtype = get_cryptoalg_subtype(tfm);
1103 int ret = 0;
1105 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR)
1106 ctr_add_iv(iv, req->iv, (reqctx->processed /
1107 AES_BLOCK_SIZE));
1108 else if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_RFC3686)
1109 *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE +
1110 CTR_RFC3686_IV_SIZE) = cpu_to_be32((reqctx->processed /
1111 AES_BLOCK_SIZE) + 1);
1112 else if (subtype == CRYPTO_ALG_SUB_TYPE_XTS)
1113 ret = chcr_update_tweak(req, iv, 0);
1114 else if (subtype == CRYPTO_ALG_SUB_TYPE_CBC) {
1115 if (reqctx->op)
1116 /*Updated before sending last WR*/
1117 memcpy(iv, req->iv, AES_BLOCK_SIZE);
1118 else
1119 memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE);
1122 return ret;
1126 /* We need separate function for final iv because in rfc3686 Initial counter
1127 * starts from 1 and buffer size of iv is 8 byte only which remains constant
1128 * for subsequent update requests
1131 static int chcr_final_cipher_iv(struct skcipher_request *req,
1132 struct cpl_fw6_pld *fw6_pld, u8 *iv)
1134 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1135 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1136 int subtype = get_cryptoalg_subtype(tfm);
1137 int ret = 0;
1139 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR)
1140 ctr_add_iv(iv, req->iv, DIV_ROUND_UP(reqctx->processed,
1141 AES_BLOCK_SIZE));
1142 else if (subtype == CRYPTO_ALG_SUB_TYPE_XTS) {
1143 if (!reqctx->partial_req)
1144 memcpy(iv, reqctx->iv, AES_BLOCK_SIZE);
1145 else
1146 ret = chcr_update_tweak(req, iv, 1);
1148 else if (subtype == CRYPTO_ALG_SUB_TYPE_CBC) {
1149 /*Already updated for Decrypt*/
1150 if (!reqctx->op)
1151 memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE);
1154 return ret;
1158 static int chcr_handle_cipher_resp(struct skcipher_request *req,
1159 unsigned char *input, int err)
1161 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1162 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1163 struct cpl_fw6_pld *fw6_pld = (struct cpl_fw6_pld *)input;
1164 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
1165 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
1166 struct chcr_dev *dev = c_ctx(tfm)->dev;
1167 struct chcr_context *ctx = c_ctx(tfm);
1168 struct adapter *adap = padap(ctx->dev);
1169 struct cipher_wr_param wrparam;
1170 struct sk_buff *skb;
1171 int bytes;
1173 if (err)
1174 goto unmap;
1175 if (req->cryptlen == reqctx->processed) {
1176 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
1177 req);
1178 err = chcr_final_cipher_iv(req, fw6_pld, req->iv);
1179 goto complete;
1182 if (!reqctx->imm) {
1183 bytes = chcr_sg_ent_in_wr(reqctx->srcsg, reqctx->dstsg, 0,
1184 CIP_SPACE_LEFT(ablkctx->enckey_len),
1185 reqctx->src_ofst, reqctx->dst_ofst);
1186 if ((bytes + reqctx->processed) >= req->cryptlen)
1187 bytes = req->cryptlen - reqctx->processed;
1188 else
1189 bytes = rounddown(bytes, 16);
1190 } else {
1191 /*CTR mode counter overfloa*/
1192 bytes = req->cryptlen - reqctx->processed;
1194 err = chcr_update_cipher_iv(req, fw6_pld, reqctx->iv);
1195 if (err)
1196 goto unmap;
1198 if (unlikely(bytes == 0)) {
1199 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
1200 req);
1201 memcpy(req->iv, reqctx->init_iv, IV);
1202 atomic_inc(&adap->chcr_stats.fallback);
1203 err = chcr_cipher_fallback(ablkctx->sw_cipher,
1204 req->base.flags,
1205 req->src,
1206 req->dst,
1207 req->cryptlen,
1208 req->iv,
1209 reqctx->op);
1210 goto complete;
1213 if (get_cryptoalg_subtype(tfm) ==
1214 CRYPTO_ALG_SUB_TYPE_CTR)
1215 bytes = adjust_ctr_overflow(reqctx->iv, bytes);
1216 wrparam.qid = u_ctx->lldi.rxq_ids[reqctx->rxqidx];
1217 wrparam.req = req;
1218 wrparam.bytes = bytes;
1219 skb = create_cipher_wr(&wrparam);
1220 if (IS_ERR(skb)) {
1221 pr_err("chcr : %s : Failed to form WR. No memory\n", __func__);
1222 err = PTR_ERR(skb);
1223 goto unmap;
1225 skb->dev = u_ctx->lldi.ports[0];
1226 set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx);
1227 chcr_send_wr(skb);
1228 reqctx->last_req_len = bytes;
1229 reqctx->processed += bytes;
1230 if (get_cryptoalg_subtype(tfm) ==
1231 CRYPTO_ALG_SUB_TYPE_CBC && req->base.flags ==
1232 CRYPTO_TFM_REQ_MAY_SLEEP ) {
1233 complete(&ctx->cbc_aes_aio_done);
1235 return 0;
1236 unmap:
1237 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
1238 complete:
1239 if (get_cryptoalg_subtype(tfm) ==
1240 CRYPTO_ALG_SUB_TYPE_CBC && req->base.flags ==
1241 CRYPTO_TFM_REQ_MAY_SLEEP ) {
1242 complete(&ctx->cbc_aes_aio_done);
1244 chcr_dec_wrcount(dev);
1245 req->base.complete(&req->base, err);
1246 return err;
1249 static int process_cipher(struct skcipher_request *req,
1250 unsigned short qid,
1251 struct sk_buff **skb,
1252 unsigned short op_type)
1254 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1255 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1256 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1257 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
1258 struct adapter *adap = padap(c_ctx(tfm)->dev);
1259 struct cipher_wr_param wrparam;
1260 int bytes, err = -EINVAL;
1261 int subtype;
1263 reqctx->processed = 0;
1264 reqctx->partial_req = 0;
1265 if (!req->iv)
1266 goto error;
1267 subtype = get_cryptoalg_subtype(tfm);
1268 if ((ablkctx->enckey_len == 0) || (ivsize > AES_BLOCK_SIZE) ||
1269 (req->cryptlen == 0) ||
1270 (req->cryptlen % crypto_skcipher_blocksize(tfm))) {
1271 if (req->cryptlen == 0 && subtype != CRYPTO_ALG_SUB_TYPE_XTS)
1272 goto fallback;
1273 else if (req->cryptlen % crypto_skcipher_blocksize(tfm) &&
1274 subtype == CRYPTO_ALG_SUB_TYPE_XTS)
1275 goto fallback;
1276 pr_err("AES: Invalid value of Key Len %d nbytes %d IV Len %d\n",
1277 ablkctx->enckey_len, req->cryptlen, ivsize);
1278 goto error;
1281 err = chcr_cipher_dma_map(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
1282 if (err)
1283 goto error;
1284 if (req->cryptlen < (SGE_MAX_WR_LEN - (sizeof(struct chcr_wr) +
1285 AES_MIN_KEY_SIZE +
1286 sizeof(struct cpl_rx_phys_dsgl) +
1287 /*Min dsgl size*/
1288 32))) {
1289 /* Can be sent as Imm*/
1290 unsigned int dnents = 0, transhdr_len, phys_dsgl, kctx_len;
1292 dnents = sg_nents_xlen(req->dst, req->cryptlen,
1293 CHCR_DST_SG_SIZE, 0);
1294 phys_dsgl = get_space_for_phys_dsgl(dnents);
1295 kctx_len = roundup(ablkctx->enckey_len, 16);
1296 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, phys_dsgl);
1297 reqctx->imm = (transhdr_len + IV + req->cryptlen) <=
1298 SGE_MAX_WR_LEN;
1299 bytes = IV + req->cryptlen;
1301 } else {
1302 reqctx->imm = 0;
1305 if (!reqctx->imm) {
1306 bytes = chcr_sg_ent_in_wr(req->src, req->dst, 0,
1307 CIP_SPACE_LEFT(ablkctx->enckey_len),
1308 0, 0);
1309 if ((bytes + reqctx->processed) >= req->cryptlen)
1310 bytes = req->cryptlen - reqctx->processed;
1311 else
1312 bytes = rounddown(bytes, 16);
1313 } else {
1314 bytes = req->cryptlen;
1316 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR) {
1317 bytes = adjust_ctr_overflow(req->iv, bytes);
1319 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_RFC3686) {
1320 memcpy(reqctx->iv, ablkctx->nonce, CTR_RFC3686_NONCE_SIZE);
1321 memcpy(reqctx->iv + CTR_RFC3686_NONCE_SIZE, req->iv,
1322 CTR_RFC3686_IV_SIZE);
1324 /* initialize counter portion of counter block */
1325 *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE +
1326 CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
1327 memcpy(reqctx->init_iv, reqctx->iv, IV);
1329 } else {
1331 memcpy(reqctx->iv, req->iv, IV);
1332 memcpy(reqctx->init_iv, req->iv, IV);
1334 if (unlikely(bytes == 0)) {
1335 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
1336 req);
1337 fallback: atomic_inc(&adap->chcr_stats.fallback);
1338 err = chcr_cipher_fallback(ablkctx->sw_cipher,
1339 req->base.flags,
1340 req->src,
1341 req->dst,
1342 req->cryptlen,
1343 subtype ==
1344 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686 ?
1345 reqctx->iv : req->iv,
1346 op_type);
1347 goto error;
1349 reqctx->op = op_type;
1350 reqctx->srcsg = req->src;
1351 reqctx->dstsg = req->dst;
1352 reqctx->src_ofst = 0;
1353 reqctx->dst_ofst = 0;
1354 wrparam.qid = qid;
1355 wrparam.req = req;
1356 wrparam.bytes = bytes;
1357 *skb = create_cipher_wr(&wrparam);
1358 if (IS_ERR(*skb)) {
1359 err = PTR_ERR(*skb);
1360 goto unmap;
1362 reqctx->processed = bytes;
1363 reqctx->last_req_len = bytes;
1364 reqctx->partial_req = !!(req->cryptlen - reqctx->processed);
1366 return 0;
1367 unmap:
1368 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
1369 error:
1370 return err;
1373 static int chcr_aes_encrypt(struct skcipher_request *req)
1375 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1376 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1377 struct chcr_dev *dev = c_ctx(tfm)->dev;
1378 struct sk_buff *skb = NULL;
1379 int err;
1380 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
1381 struct chcr_context *ctx = c_ctx(tfm);
1382 unsigned int cpu;
1384 cpu = get_cpu();
1385 reqctx->txqidx = cpu % ctx->ntxq;
1386 reqctx->rxqidx = cpu % ctx->nrxq;
1387 put_cpu();
1389 err = chcr_inc_wrcount(dev);
1390 if (err)
1391 return -ENXIO;
1392 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1393 reqctx->txqidx) &&
1394 (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) {
1395 err = -ENOSPC;
1396 goto error;
1399 err = process_cipher(req, u_ctx->lldi.rxq_ids[reqctx->rxqidx],
1400 &skb, CHCR_ENCRYPT_OP);
1401 if (err || !skb)
1402 return err;
1403 skb->dev = u_ctx->lldi.ports[0];
1404 set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx);
1405 chcr_send_wr(skb);
1406 if (get_cryptoalg_subtype(tfm) ==
1407 CRYPTO_ALG_SUB_TYPE_CBC && req->base.flags ==
1408 CRYPTO_TFM_REQ_MAY_SLEEP ) {
1409 reqctx->partial_req = 1;
1410 wait_for_completion(&ctx->cbc_aes_aio_done);
1412 return -EINPROGRESS;
1413 error:
1414 chcr_dec_wrcount(dev);
1415 return err;
1418 static int chcr_aes_decrypt(struct skcipher_request *req)
1420 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1421 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1422 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
1423 struct chcr_dev *dev = c_ctx(tfm)->dev;
1424 struct sk_buff *skb = NULL;
1425 int err;
1426 struct chcr_context *ctx = c_ctx(tfm);
1427 unsigned int cpu;
1429 cpu = get_cpu();
1430 reqctx->txqidx = cpu % ctx->ntxq;
1431 reqctx->rxqidx = cpu % ctx->nrxq;
1432 put_cpu();
1434 err = chcr_inc_wrcount(dev);
1435 if (err)
1436 return -ENXIO;
1438 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1439 reqctx->txqidx) &&
1440 (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))))
1441 return -ENOSPC;
1442 err = process_cipher(req, u_ctx->lldi.rxq_ids[reqctx->rxqidx],
1443 &skb, CHCR_DECRYPT_OP);
1444 if (err || !skb)
1445 return err;
1446 skb->dev = u_ctx->lldi.ports[0];
1447 set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx);
1448 chcr_send_wr(skb);
1449 return -EINPROGRESS;
1451 static int chcr_device_init(struct chcr_context *ctx)
1453 struct uld_ctx *u_ctx = NULL;
1454 int txq_perchan, ntxq;
1455 int err = 0, rxq_perchan;
1457 if (!ctx->dev) {
1458 u_ctx = assign_chcr_device();
1459 if (!u_ctx) {
1460 err = -ENXIO;
1461 pr_err("chcr device assignment fails\n");
1462 goto out;
1464 ctx->dev = &u_ctx->dev;
1465 ntxq = u_ctx->lldi.ntxq;
1466 rxq_perchan = u_ctx->lldi.nrxq / u_ctx->lldi.nchan;
1467 txq_perchan = ntxq / u_ctx->lldi.nchan;
1468 ctx->ntxq = ntxq;
1469 ctx->nrxq = u_ctx->lldi.nrxq;
1470 ctx->rxq_perchan = rxq_perchan;
1471 ctx->txq_perchan = txq_perchan;
1473 out:
1474 return err;
1477 static int chcr_init_tfm(struct crypto_skcipher *tfm)
1479 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
1480 struct chcr_context *ctx = crypto_skcipher_ctx(tfm);
1481 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
1483 ablkctx->sw_cipher = crypto_alloc_sync_skcipher(alg->base.cra_name, 0,
1484 CRYPTO_ALG_NEED_FALLBACK);
1485 if (IS_ERR(ablkctx->sw_cipher)) {
1486 pr_err("failed to allocate fallback for %s\n", alg->base.cra_name);
1487 return PTR_ERR(ablkctx->sw_cipher);
1489 init_completion(&ctx->cbc_aes_aio_done);
1490 crypto_skcipher_set_reqsize(tfm, sizeof(struct chcr_skcipher_req_ctx));
1492 return chcr_device_init(ctx);
1495 static int chcr_rfc3686_init(struct crypto_skcipher *tfm)
1497 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
1498 struct chcr_context *ctx = crypto_skcipher_ctx(tfm);
1499 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
1501 /*RFC3686 initialises IV counter value to 1, rfc3686(ctr(aes))
1502 * cannot be used as fallback in chcr_handle_cipher_response
1504 ablkctx->sw_cipher = crypto_alloc_sync_skcipher("ctr(aes)", 0,
1505 CRYPTO_ALG_NEED_FALLBACK);
1506 if (IS_ERR(ablkctx->sw_cipher)) {
1507 pr_err("failed to allocate fallback for %s\n", alg->base.cra_name);
1508 return PTR_ERR(ablkctx->sw_cipher);
1510 crypto_skcipher_set_reqsize(tfm, sizeof(struct chcr_skcipher_req_ctx));
1511 return chcr_device_init(ctx);
1515 static void chcr_exit_tfm(struct crypto_skcipher *tfm)
1517 struct chcr_context *ctx = crypto_skcipher_ctx(tfm);
1518 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
1520 crypto_free_sync_skcipher(ablkctx->sw_cipher);
1523 static int get_alg_config(struct algo_param *params,
1524 unsigned int auth_size)
1526 switch (auth_size) {
1527 case SHA1_DIGEST_SIZE:
1528 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_160;
1529 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA1;
1530 params->result_size = SHA1_DIGEST_SIZE;
1531 break;
1532 case SHA224_DIGEST_SIZE:
1533 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
1534 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA224;
1535 params->result_size = SHA256_DIGEST_SIZE;
1536 break;
1537 case SHA256_DIGEST_SIZE:
1538 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
1539 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA256;
1540 params->result_size = SHA256_DIGEST_SIZE;
1541 break;
1542 case SHA384_DIGEST_SIZE:
1543 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
1544 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_384;
1545 params->result_size = SHA512_DIGEST_SIZE;
1546 break;
1547 case SHA512_DIGEST_SIZE:
1548 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
1549 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_512;
1550 params->result_size = SHA512_DIGEST_SIZE;
1551 break;
1552 default:
1553 pr_err("chcr : ERROR, unsupported digest size\n");
1554 return -EINVAL;
1556 return 0;
1559 static inline void chcr_free_shash(struct crypto_shash *base_hash)
1561 crypto_free_shash(base_hash);
1565 * create_hash_wr - Create hash work request
1566 * @req - Cipher req base
1568 static struct sk_buff *create_hash_wr(struct ahash_request *req,
1569 struct hash_wr_param *param)
1571 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1572 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1573 struct chcr_context *ctx = h_ctx(tfm);
1574 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
1575 struct sk_buff *skb = NULL;
1576 struct uld_ctx *u_ctx = ULD_CTX(ctx);
1577 struct chcr_wr *chcr_req;
1578 struct ulptx_sgl *ulptx;
1579 unsigned int nents = 0, transhdr_len;
1580 unsigned int temp = 0;
1581 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1582 GFP_ATOMIC;
1583 struct adapter *adap = padap(h_ctx(tfm)->dev);
1584 int error = 0;
1585 unsigned int rx_channel_id = req_ctx->rxqidx / ctx->rxq_perchan;
1587 transhdr_len = HASH_TRANSHDR_SIZE(param->kctx_len);
1588 req_ctx->hctx_wr.imm = (transhdr_len + param->bfr_len +
1589 param->sg_len) <= SGE_MAX_WR_LEN;
1590 nents = sg_nents_xlen(req_ctx->hctx_wr.srcsg, param->sg_len,
1591 CHCR_SRC_SG_SIZE, req_ctx->hctx_wr.src_ofst);
1592 nents += param->bfr_len ? 1 : 0;
1593 transhdr_len += req_ctx->hctx_wr.imm ? roundup(param->bfr_len +
1594 param->sg_len, 16) : (sgl_len(nents) * 8);
1595 transhdr_len = roundup(transhdr_len, 16);
1597 skb = alloc_skb(transhdr_len, flags);
1598 if (!skb)
1599 return ERR_PTR(-ENOMEM);
1600 chcr_req = __skb_put_zero(skb, transhdr_len);
1602 chcr_req->sec_cpl.op_ivinsrtofst =
1603 FILL_SEC_CPL_OP_IVINSR(rx_channel_id, 2, 0);
1605 chcr_req->sec_cpl.pldlen = htonl(param->bfr_len + param->sg_len);
1607 chcr_req->sec_cpl.aadstart_cipherstop_hi =
1608 FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, 0, 0);
1609 chcr_req->sec_cpl.cipherstop_lo_authinsert =
1610 FILL_SEC_CPL_AUTHINSERT(0, 1, 0, 0);
1611 chcr_req->sec_cpl.seqno_numivs =
1612 FILL_SEC_CPL_SCMD0_SEQNO(0, 0, 0, param->alg_prm.auth_mode,
1613 param->opad_needed, 0);
1615 chcr_req->sec_cpl.ivgen_hdrlen =
1616 FILL_SEC_CPL_IVGEN_HDRLEN(param->last, param->more, 0, 1, 0, 0);
1618 memcpy(chcr_req->key_ctx.key, req_ctx->partial_hash,
1619 param->alg_prm.result_size);
1621 if (param->opad_needed)
1622 memcpy(chcr_req->key_ctx.key +
1623 ((param->alg_prm.result_size <= 32) ? 32 :
1624 CHCR_HASH_MAX_DIGEST_SIZE),
1625 hmacctx->opad, param->alg_prm.result_size);
1627 chcr_req->key_ctx.ctx_hdr = FILL_KEY_CTX_HDR(CHCR_KEYCTX_NO_KEY,
1628 param->alg_prm.mk_size, 0,
1629 param->opad_needed,
1630 ((param->kctx_len +
1631 sizeof(chcr_req->key_ctx)) >> 4));
1632 chcr_req->sec_cpl.scmd1 = cpu_to_be64((u64)param->scmd1);
1633 ulptx = (struct ulptx_sgl *)((u8 *)(chcr_req + 1) + param->kctx_len +
1634 DUMMY_BYTES);
1635 if (param->bfr_len != 0) {
1636 req_ctx->hctx_wr.dma_addr =
1637 dma_map_single(&u_ctx->lldi.pdev->dev, req_ctx->reqbfr,
1638 param->bfr_len, DMA_TO_DEVICE);
1639 if (dma_mapping_error(&u_ctx->lldi.pdev->dev,
1640 req_ctx->hctx_wr. dma_addr)) {
1641 error = -ENOMEM;
1642 goto err;
1644 req_ctx->hctx_wr.dma_len = param->bfr_len;
1645 } else {
1646 req_ctx->hctx_wr.dma_addr = 0;
1648 chcr_add_hash_src_ent(req, ulptx, param);
1649 /* Request upto max wr size */
1650 temp = param->kctx_len + DUMMY_BYTES + (req_ctx->hctx_wr.imm ?
1651 (param->sg_len + param->bfr_len) : 0);
1652 atomic_inc(&adap->chcr_stats.digest_rqst);
1653 create_wreq(h_ctx(tfm), chcr_req, &req->base, req_ctx->hctx_wr.imm,
1654 param->hash_size, transhdr_len,
1655 temp, 0);
1656 req_ctx->hctx_wr.skb = skb;
1657 return skb;
1658 err:
1659 kfree_skb(skb);
1660 return ERR_PTR(error);
1663 static int chcr_ahash_update(struct ahash_request *req)
1665 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1666 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1667 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(rtfm));
1668 struct chcr_context *ctx = h_ctx(rtfm);
1669 struct chcr_dev *dev = h_ctx(rtfm)->dev;
1670 struct sk_buff *skb;
1671 u8 remainder = 0, bs;
1672 unsigned int nbytes = req->nbytes;
1673 struct hash_wr_param params;
1674 int error;
1675 unsigned int cpu;
1677 cpu = get_cpu();
1678 req_ctx->txqidx = cpu % ctx->ntxq;
1679 req_ctx->rxqidx = cpu % ctx->nrxq;
1680 put_cpu();
1682 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1684 if (nbytes + req_ctx->reqlen >= bs) {
1685 remainder = (nbytes + req_ctx->reqlen) % bs;
1686 nbytes = nbytes + req_ctx->reqlen - remainder;
1687 } else {
1688 sg_pcopy_to_buffer(req->src, sg_nents(req->src), req_ctx->reqbfr
1689 + req_ctx->reqlen, nbytes, 0);
1690 req_ctx->reqlen += nbytes;
1691 return 0;
1693 error = chcr_inc_wrcount(dev);
1694 if (error)
1695 return -ENXIO;
1696 /* Detach state for CHCR means lldi or padap is freed. Increasing
1697 * inflight count for dev guarantees that lldi and padap is valid
1699 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1700 req_ctx->txqidx) &&
1701 (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) {
1702 error = -ENOSPC;
1703 goto err;
1706 chcr_init_hctx_per_wr(req_ctx);
1707 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
1708 if (error) {
1709 error = -ENOMEM;
1710 goto err;
1712 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1713 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1714 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
1715 HASH_SPACE_LEFT(params.kctx_len), 0);
1716 if (params.sg_len > req->nbytes)
1717 params.sg_len = req->nbytes;
1718 params.sg_len = rounddown(params.sg_len + req_ctx->reqlen, bs) -
1719 req_ctx->reqlen;
1720 params.opad_needed = 0;
1721 params.more = 1;
1722 params.last = 0;
1723 params.bfr_len = req_ctx->reqlen;
1724 params.scmd1 = 0;
1725 req_ctx->hctx_wr.srcsg = req->src;
1727 params.hash_size = params.alg_prm.result_size;
1728 req_ctx->data_len += params.sg_len + params.bfr_len;
1729 skb = create_hash_wr(req, &params);
1730 if (IS_ERR(skb)) {
1731 error = PTR_ERR(skb);
1732 goto unmap;
1735 req_ctx->hctx_wr.processed += params.sg_len;
1736 if (remainder) {
1737 /* Swap buffers */
1738 swap(req_ctx->reqbfr, req_ctx->skbfr);
1739 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
1740 req_ctx->reqbfr, remainder, req->nbytes -
1741 remainder);
1743 req_ctx->reqlen = remainder;
1744 skb->dev = u_ctx->lldi.ports[0];
1745 set_wr_txq(skb, CPL_PRIORITY_DATA, req_ctx->txqidx);
1746 chcr_send_wr(skb);
1747 return -EINPROGRESS;
1748 unmap:
1749 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
1750 err:
1751 chcr_dec_wrcount(dev);
1752 return error;
1755 static void create_last_hash_block(char *bfr_ptr, unsigned int bs, u64 scmd1)
1757 memset(bfr_ptr, 0, bs);
1758 *bfr_ptr = 0x80;
1759 if (bs == 64)
1760 *(__be64 *)(bfr_ptr + 56) = cpu_to_be64(scmd1 << 3);
1761 else
1762 *(__be64 *)(bfr_ptr + 120) = cpu_to_be64(scmd1 << 3);
1765 static int chcr_ahash_final(struct ahash_request *req)
1767 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1768 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1769 struct chcr_dev *dev = h_ctx(rtfm)->dev;
1770 struct hash_wr_param params;
1771 struct sk_buff *skb;
1772 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(rtfm));
1773 struct chcr_context *ctx = h_ctx(rtfm);
1774 u8 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1775 int error = -EINVAL;
1776 unsigned int cpu;
1778 cpu = get_cpu();
1779 req_ctx->txqidx = cpu % ctx->ntxq;
1780 req_ctx->rxqidx = cpu % ctx->nrxq;
1781 put_cpu();
1783 error = chcr_inc_wrcount(dev);
1784 if (error)
1785 return -ENXIO;
1787 chcr_init_hctx_per_wr(req_ctx);
1788 if (is_hmac(crypto_ahash_tfm(rtfm)))
1789 params.opad_needed = 1;
1790 else
1791 params.opad_needed = 0;
1792 params.sg_len = 0;
1793 req_ctx->hctx_wr.isfinal = 1;
1794 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1795 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1796 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1797 params.opad_needed = 1;
1798 params.kctx_len *= 2;
1799 } else {
1800 params.opad_needed = 0;
1803 req_ctx->hctx_wr.result = 1;
1804 params.bfr_len = req_ctx->reqlen;
1805 req_ctx->data_len += params.bfr_len + params.sg_len;
1806 req_ctx->hctx_wr.srcsg = req->src;
1807 if (req_ctx->reqlen == 0) {
1808 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
1809 params.last = 0;
1810 params.more = 1;
1811 params.scmd1 = 0;
1812 params.bfr_len = bs;
1814 } else {
1815 params.scmd1 = req_ctx->data_len;
1816 params.last = 1;
1817 params.more = 0;
1819 params.hash_size = crypto_ahash_digestsize(rtfm);
1820 skb = create_hash_wr(req, &params);
1821 if (IS_ERR(skb)) {
1822 error = PTR_ERR(skb);
1823 goto err;
1825 req_ctx->reqlen = 0;
1826 skb->dev = u_ctx->lldi.ports[0];
1827 set_wr_txq(skb, CPL_PRIORITY_DATA, req_ctx->txqidx);
1828 chcr_send_wr(skb);
1829 return -EINPROGRESS;
1830 err:
1831 chcr_dec_wrcount(dev);
1832 return error;
1835 static int chcr_ahash_finup(struct ahash_request *req)
1837 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1838 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1839 struct chcr_dev *dev = h_ctx(rtfm)->dev;
1840 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(rtfm));
1841 struct chcr_context *ctx = h_ctx(rtfm);
1842 struct sk_buff *skb;
1843 struct hash_wr_param params;
1844 u8 bs;
1845 int error;
1846 unsigned int cpu;
1848 cpu = get_cpu();
1849 req_ctx->txqidx = cpu % ctx->ntxq;
1850 req_ctx->rxqidx = cpu % ctx->nrxq;
1851 put_cpu();
1853 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1854 error = chcr_inc_wrcount(dev);
1855 if (error)
1856 return -ENXIO;
1858 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1859 req_ctx->txqidx) &&
1860 (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) {
1861 error = -ENOSPC;
1862 goto err;
1864 chcr_init_hctx_per_wr(req_ctx);
1865 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
1866 if (error) {
1867 error = -ENOMEM;
1868 goto err;
1871 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1872 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1873 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1874 params.kctx_len *= 2;
1875 params.opad_needed = 1;
1876 } else {
1877 params.opad_needed = 0;
1880 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
1881 HASH_SPACE_LEFT(params.kctx_len), 0);
1882 if (params.sg_len < req->nbytes) {
1883 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1884 params.kctx_len /= 2;
1885 params.opad_needed = 0;
1887 params.last = 0;
1888 params.more = 1;
1889 params.sg_len = rounddown(params.sg_len + req_ctx->reqlen, bs)
1890 - req_ctx->reqlen;
1891 params.hash_size = params.alg_prm.result_size;
1892 params.scmd1 = 0;
1893 } else {
1894 params.last = 1;
1895 params.more = 0;
1896 params.sg_len = req->nbytes;
1897 params.hash_size = crypto_ahash_digestsize(rtfm);
1898 params.scmd1 = req_ctx->data_len + req_ctx->reqlen +
1899 params.sg_len;
1901 params.bfr_len = req_ctx->reqlen;
1902 req_ctx->data_len += params.bfr_len + params.sg_len;
1903 req_ctx->hctx_wr.result = 1;
1904 req_ctx->hctx_wr.srcsg = req->src;
1905 if ((req_ctx->reqlen + req->nbytes) == 0) {
1906 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
1907 params.last = 0;
1908 params.more = 1;
1909 params.scmd1 = 0;
1910 params.bfr_len = bs;
1912 skb = create_hash_wr(req, &params);
1913 if (IS_ERR(skb)) {
1914 error = PTR_ERR(skb);
1915 goto unmap;
1917 req_ctx->reqlen = 0;
1918 req_ctx->hctx_wr.processed += params.sg_len;
1919 skb->dev = u_ctx->lldi.ports[0];
1920 set_wr_txq(skb, CPL_PRIORITY_DATA, req_ctx->txqidx);
1921 chcr_send_wr(skb);
1922 return -EINPROGRESS;
1923 unmap:
1924 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
1925 err:
1926 chcr_dec_wrcount(dev);
1927 return error;
1930 static int chcr_ahash_digest(struct ahash_request *req)
1932 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1933 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1934 struct chcr_dev *dev = h_ctx(rtfm)->dev;
1935 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(rtfm));
1936 struct chcr_context *ctx = h_ctx(rtfm);
1937 struct sk_buff *skb;
1938 struct hash_wr_param params;
1939 u8 bs;
1940 int error;
1941 unsigned int cpu;
1943 cpu = get_cpu();
1944 req_ctx->txqidx = cpu % ctx->ntxq;
1945 req_ctx->rxqidx = cpu % ctx->nrxq;
1946 put_cpu();
1948 rtfm->init(req);
1949 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1950 error = chcr_inc_wrcount(dev);
1951 if (error)
1952 return -ENXIO;
1954 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1955 req_ctx->txqidx) &&
1956 (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) {
1957 error = -ENOSPC;
1958 goto err;
1961 chcr_init_hctx_per_wr(req_ctx);
1962 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
1963 if (error) {
1964 error = -ENOMEM;
1965 goto err;
1968 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1969 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1970 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1971 params.kctx_len *= 2;
1972 params.opad_needed = 1;
1973 } else {
1974 params.opad_needed = 0;
1976 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
1977 HASH_SPACE_LEFT(params.kctx_len), 0);
1978 if (params.sg_len < req->nbytes) {
1979 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1980 params.kctx_len /= 2;
1981 params.opad_needed = 0;
1983 params.last = 0;
1984 params.more = 1;
1985 params.scmd1 = 0;
1986 params.sg_len = rounddown(params.sg_len, bs);
1987 params.hash_size = params.alg_prm.result_size;
1988 } else {
1989 params.sg_len = req->nbytes;
1990 params.hash_size = crypto_ahash_digestsize(rtfm);
1991 params.last = 1;
1992 params.more = 0;
1993 params.scmd1 = req->nbytes + req_ctx->data_len;
1996 params.bfr_len = 0;
1997 req_ctx->hctx_wr.result = 1;
1998 req_ctx->hctx_wr.srcsg = req->src;
1999 req_ctx->data_len += params.bfr_len + params.sg_len;
2001 if (req->nbytes == 0) {
2002 create_last_hash_block(req_ctx->reqbfr, bs, 0);
2003 params.more = 1;
2004 params.bfr_len = bs;
2007 skb = create_hash_wr(req, &params);
2008 if (IS_ERR(skb)) {
2009 error = PTR_ERR(skb);
2010 goto unmap;
2012 req_ctx->hctx_wr.processed += params.sg_len;
2013 skb->dev = u_ctx->lldi.ports[0];
2014 set_wr_txq(skb, CPL_PRIORITY_DATA, req_ctx->txqidx);
2015 chcr_send_wr(skb);
2016 return -EINPROGRESS;
2017 unmap:
2018 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
2019 err:
2020 chcr_dec_wrcount(dev);
2021 return error;
2024 static int chcr_ahash_continue(struct ahash_request *req)
2026 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
2027 struct chcr_hctx_per_wr *hctx_wr = &reqctx->hctx_wr;
2028 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
2029 struct chcr_context *ctx = h_ctx(rtfm);
2030 struct uld_ctx *u_ctx = ULD_CTX(ctx);
2031 struct sk_buff *skb;
2032 struct hash_wr_param params;
2033 u8 bs;
2034 int error;
2035 unsigned int cpu;
2037 cpu = get_cpu();
2038 reqctx->txqidx = cpu % ctx->ntxq;
2039 reqctx->rxqidx = cpu % ctx->nrxq;
2040 put_cpu();
2042 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
2043 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
2044 params.kctx_len = roundup(params.alg_prm.result_size, 16);
2045 if (is_hmac(crypto_ahash_tfm(rtfm))) {
2046 params.kctx_len *= 2;
2047 params.opad_needed = 1;
2048 } else {
2049 params.opad_needed = 0;
2051 params.sg_len = chcr_hash_ent_in_wr(hctx_wr->srcsg, 0,
2052 HASH_SPACE_LEFT(params.kctx_len),
2053 hctx_wr->src_ofst);
2054 if ((params.sg_len + hctx_wr->processed) > req->nbytes)
2055 params.sg_len = req->nbytes - hctx_wr->processed;
2056 if (!hctx_wr->result ||
2057 ((params.sg_len + hctx_wr->processed) < req->nbytes)) {
2058 if (is_hmac(crypto_ahash_tfm(rtfm))) {
2059 params.kctx_len /= 2;
2060 params.opad_needed = 0;
2062 params.last = 0;
2063 params.more = 1;
2064 params.sg_len = rounddown(params.sg_len, bs);
2065 params.hash_size = params.alg_prm.result_size;
2066 params.scmd1 = 0;
2067 } else {
2068 params.last = 1;
2069 params.more = 0;
2070 params.hash_size = crypto_ahash_digestsize(rtfm);
2071 params.scmd1 = reqctx->data_len + params.sg_len;
2073 params.bfr_len = 0;
2074 reqctx->data_len += params.sg_len;
2075 skb = create_hash_wr(req, &params);
2076 if (IS_ERR(skb)) {
2077 error = PTR_ERR(skb);
2078 goto err;
2080 hctx_wr->processed += params.sg_len;
2081 skb->dev = u_ctx->lldi.ports[0];
2082 set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx);
2083 chcr_send_wr(skb);
2084 return 0;
2085 err:
2086 return error;
2089 static inline void chcr_handle_ahash_resp(struct ahash_request *req,
2090 unsigned char *input,
2091 int err)
2093 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
2094 struct chcr_hctx_per_wr *hctx_wr = &reqctx->hctx_wr;
2095 int digestsize, updated_digestsize;
2096 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2097 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(tfm));
2098 struct chcr_dev *dev = h_ctx(tfm)->dev;
2100 if (input == NULL)
2101 goto out;
2102 digestsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
2103 updated_digestsize = digestsize;
2104 if (digestsize == SHA224_DIGEST_SIZE)
2105 updated_digestsize = SHA256_DIGEST_SIZE;
2106 else if (digestsize == SHA384_DIGEST_SIZE)
2107 updated_digestsize = SHA512_DIGEST_SIZE;
2109 if (hctx_wr->dma_addr) {
2110 dma_unmap_single(&u_ctx->lldi.pdev->dev, hctx_wr->dma_addr,
2111 hctx_wr->dma_len, DMA_TO_DEVICE);
2112 hctx_wr->dma_addr = 0;
2114 if (hctx_wr->isfinal || ((hctx_wr->processed + reqctx->reqlen) ==
2115 req->nbytes)) {
2116 if (hctx_wr->result == 1) {
2117 hctx_wr->result = 0;
2118 memcpy(req->result, input + sizeof(struct cpl_fw6_pld),
2119 digestsize);
2120 } else {
2121 memcpy(reqctx->partial_hash,
2122 input + sizeof(struct cpl_fw6_pld),
2123 updated_digestsize);
2126 goto unmap;
2128 memcpy(reqctx->partial_hash, input + sizeof(struct cpl_fw6_pld),
2129 updated_digestsize);
2131 err = chcr_ahash_continue(req);
2132 if (err)
2133 goto unmap;
2134 return;
2135 unmap:
2136 if (hctx_wr->is_sg_map)
2137 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
2140 out:
2141 chcr_dec_wrcount(dev);
2142 req->base.complete(&req->base, err);
2146 * chcr_handle_resp - Unmap the DMA buffers associated with the request
2147 * @req: crypto request
2149 int chcr_handle_resp(struct crypto_async_request *req, unsigned char *input,
2150 int err)
2152 struct crypto_tfm *tfm = req->tfm;
2153 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2154 struct adapter *adap = padap(ctx->dev);
2156 switch (tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
2157 case CRYPTO_ALG_TYPE_AEAD:
2158 err = chcr_handle_aead_resp(aead_request_cast(req), input, err);
2159 break;
2161 case CRYPTO_ALG_TYPE_SKCIPHER:
2162 chcr_handle_cipher_resp(skcipher_request_cast(req),
2163 input, err);
2164 break;
2165 case CRYPTO_ALG_TYPE_AHASH:
2166 chcr_handle_ahash_resp(ahash_request_cast(req), input, err);
2168 atomic_inc(&adap->chcr_stats.complete);
2169 return err;
2171 static int chcr_ahash_export(struct ahash_request *areq, void *out)
2173 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2174 struct chcr_ahash_req_ctx *state = out;
2176 state->reqlen = req_ctx->reqlen;
2177 state->data_len = req_ctx->data_len;
2178 memcpy(state->bfr1, req_ctx->reqbfr, req_ctx->reqlen);
2179 memcpy(state->partial_hash, req_ctx->partial_hash,
2180 CHCR_HASH_MAX_DIGEST_SIZE);
2181 chcr_init_hctx_per_wr(state);
2182 return 0;
2185 static int chcr_ahash_import(struct ahash_request *areq, const void *in)
2187 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2188 struct chcr_ahash_req_ctx *state = (struct chcr_ahash_req_ctx *)in;
2190 req_ctx->reqlen = state->reqlen;
2191 req_ctx->data_len = state->data_len;
2192 req_ctx->reqbfr = req_ctx->bfr1;
2193 req_ctx->skbfr = req_ctx->bfr2;
2194 memcpy(req_ctx->bfr1, state->bfr1, CHCR_HASH_MAX_BLOCK_SIZE_128);
2195 memcpy(req_ctx->partial_hash, state->partial_hash,
2196 CHCR_HASH_MAX_DIGEST_SIZE);
2197 chcr_init_hctx_per_wr(req_ctx);
2198 return 0;
2201 static int chcr_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
2202 unsigned int keylen)
2204 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(tfm));
2205 unsigned int digestsize = crypto_ahash_digestsize(tfm);
2206 unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2207 unsigned int i, err = 0, updated_digestsize;
2209 SHASH_DESC_ON_STACK(shash, hmacctx->base_hash);
2211 /* use the key to calculate the ipad and opad. ipad will sent with the
2212 * first request's data. opad will be sent with the final hash result
2213 * ipad in hmacctx->ipad and opad in hmacctx->opad location
2215 shash->tfm = hmacctx->base_hash;
2216 if (keylen > bs) {
2217 err = crypto_shash_digest(shash, key, keylen,
2218 hmacctx->ipad);
2219 if (err)
2220 goto out;
2221 keylen = digestsize;
2222 } else {
2223 memcpy(hmacctx->ipad, key, keylen);
2225 memset(hmacctx->ipad + keylen, 0, bs - keylen);
2226 memcpy(hmacctx->opad, hmacctx->ipad, bs);
2228 for (i = 0; i < bs / sizeof(int); i++) {
2229 *((unsigned int *)(&hmacctx->ipad) + i) ^= IPAD_DATA;
2230 *((unsigned int *)(&hmacctx->opad) + i) ^= OPAD_DATA;
2233 updated_digestsize = digestsize;
2234 if (digestsize == SHA224_DIGEST_SIZE)
2235 updated_digestsize = SHA256_DIGEST_SIZE;
2236 else if (digestsize == SHA384_DIGEST_SIZE)
2237 updated_digestsize = SHA512_DIGEST_SIZE;
2238 err = chcr_compute_partial_hash(shash, hmacctx->ipad,
2239 hmacctx->ipad, digestsize);
2240 if (err)
2241 goto out;
2242 chcr_change_order(hmacctx->ipad, updated_digestsize);
2244 err = chcr_compute_partial_hash(shash, hmacctx->opad,
2245 hmacctx->opad, digestsize);
2246 if (err)
2247 goto out;
2248 chcr_change_order(hmacctx->opad, updated_digestsize);
2249 out:
2250 return err;
2253 static int chcr_aes_xts_setkey(struct crypto_skcipher *cipher, const u8 *key,
2254 unsigned int key_len)
2256 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
2257 unsigned short context_size = 0;
2258 int err;
2260 err = chcr_cipher_fallback_setkey(cipher, key, key_len);
2261 if (err)
2262 goto badkey_err;
2264 memcpy(ablkctx->key, key, key_len);
2265 ablkctx->enckey_len = key_len;
2266 get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, key_len << 2);
2267 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len) >> 4;
2268 ablkctx->key_ctx_hdr =
2269 FILL_KEY_CTX_HDR((key_len == AES_KEYSIZE_256) ?
2270 CHCR_KEYCTX_CIPHER_KEY_SIZE_128 :
2271 CHCR_KEYCTX_CIPHER_KEY_SIZE_256,
2272 CHCR_KEYCTX_NO_KEY, 1,
2273 0, context_size);
2274 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_XTS;
2275 return 0;
2276 badkey_err:
2277 ablkctx->enckey_len = 0;
2279 return err;
2282 static int chcr_sha_init(struct ahash_request *areq)
2284 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2285 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
2286 int digestsize = crypto_ahash_digestsize(tfm);
2288 req_ctx->data_len = 0;
2289 req_ctx->reqlen = 0;
2290 req_ctx->reqbfr = req_ctx->bfr1;
2291 req_ctx->skbfr = req_ctx->bfr2;
2292 copy_hash_init_values(req_ctx->partial_hash, digestsize);
2294 return 0;
2297 static int chcr_sha_cra_init(struct crypto_tfm *tfm)
2299 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2300 sizeof(struct chcr_ahash_req_ctx));
2301 return chcr_device_init(crypto_tfm_ctx(tfm));
2304 static int chcr_hmac_init(struct ahash_request *areq)
2306 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2307 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(areq);
2308 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(rtfm));
2309 unsigned int digestsize = crypto_ahash_digestsize(rtfm);
2310 unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
2312 chcr_sha_init(areq);
2313 req_ctx->data_len = bs;
2314 if (is_hmac(crypto_ahash_tfm(rtfm))) {
2315 if (digestsize == SHA224_DIGEST_SIZE)
2316 memcpy(req_ctx->partial_hash, hmacctx->ipad,
2317 SHA256_DIGEST_SIZE);
2318 else if (digestsize == SHA384_DIGEST_SIZE)
2319 memcpy(req_ctx->partial_hash, hmacctx->ipad,
2320 SHA512_DIGEST_SIZE);
2321 else
2322 memcpy(req_ctx->partial_hash, hmacctx->ipad,
2323 digestsize);
2325 return 0;
2328 static int chcr_hmac_cra_init(struct crypto_tfm *tfm)
2330 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2331 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
2332 unsigned int digestsize =
2333 crypto_ahash_digestsize(__crypto_ahash_cast(tfm));
2335 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2336 sizeof(struct chcr_ahash_req_ctx));
2337 hmacctx->base_hash = chcr_alloc_shash(digestsize);
2338 if (IS_ERR(hmacctx->base_hash))
2339 return PTR_ERR(hmacctx->base_hash);
2340 return chcr_device_init(crypto_tfm_ctx(tfm));
2343 static void chcr_hmac_cra_exit(struct crypto_tfm *tfm)
2345 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2346 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
2348 if (hmacctx->base_hash) {
2349 chcr_free_shash(hmacctx->base_hash);
2350 hmacctx->base_hash = NULL;
2354 inline void chcr_aead_common_exit(struct aead_request *req)
2356 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2357 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2358 struct uld_ctx *u_ctx = ULD_CTX(a_ctx(tfm));
2360 chcr_aead_dma_unmap(&u_ctx->lldi.pdev->dev, req, reqctx->op);
2363 static int chcr_aead_common_init(struct aead_request *req)
2365 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2366 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2367 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2368 unsigned int authsize = crypto_aead_authsize(tfm);
2369 int error = -EINVAL;
2371 /* validate key size */
2372 if (aeadctx->enckey_len == 0)
2373 goto err;
2374 if (reqctx->op && req->cryptlen < authsize)
2375 goto err;
2376 if (reqctx->b0_len)
2377 reqctx->scratch_pad = reqctx->iv + IV;
2378 else
2379 reqctx->scratch_pad = NULL;
2381 error = chcr_aead_dma_map(&ULD_CTX(a_ctx(tfm))->lldi.pdev->dev, req,
2382 reqctx->op);
2383 if (error) {
2384 error = -ENOMEM;
2385 goto err;
2388 return 0;
2389 err:
2390 return error;
2393 static int chcr_aead_need_fallback(struct aead_request *req, int dst_nents,
2394 int aadmax, int wrlen,
2395 unsigned short op_type)
2397 unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
2399 if (((req->cryptlen - (op_type ? authsize : 0)) == 0) ||
2400 dst_nents > MAX_DSGL_ENT ||
2401 (req->assoclen > aadmax) ||
2402 (wrlen > SGE_MAX_WR_LEN))
2403 return 1;
2404 return 0;
2407 static int chcr_aead_fallback(struct aead_request *req, unsigned short op_type)
2409 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2410 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2411 struct aead_request *subreq = aead_request_ctx(req);
2413 aead_request_set_tfm(subreq, aeadctx->sw_cipher);
2414 aead_request_set_callback(subreq, req->base.flags,
2415 req->base.complete, req->base.data);
2416 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
2417 req->iv);
2418 aead_request_set_ad(subreq, req->assoclen);
2419 return op_type ? crypto_aead_decrypt(subreq) :
2420 crypto_aead_encrypt(subreq);
2423 static struct sk_buff *create_authenc_wr(struct aead_request *req,
2424 unsigned short qid,
2425 int size)
2427 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2428 struct chcr_context *ctx = a_ctx(tfm);
2429 struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
2430 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
2431 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2432 struct sk_buff *skb = NULL;
2433 struct chcr_wr *chcr_req;
2434 struct cpl_rx_phys_dsgl *phys_cpl;
2435 struct ulptx_sgl *ulptx;
2436 unsigned int transhdr_len;
2437 unsigned int dst_size = 0, temp, subtype = get_aead_subtype(tfm);
2438 unsigned int kctx_len = 0, dnents, snents;
2439 unsigned int authsize = crypto_aead_authsize(tfm);
2440 int error = -EINVAL;
2441 u8 *ivptr;
2442 int null = 0;
2443 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
2444 GFP_ATOMIC;
2445 struct adapter *adap = padap(ctx->dev);
2446 unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
2448 if (req->cryptlen == 0)
2449 return NULL;
2451 reqctx->b0_len = 0;
2452 error = chcr_aead_common_init(req);
2453 if (error)
2454 return ERR_PTR(error);
2456 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL ||
2457 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
2458 null = 1;
2460 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen +
2461 (reqctx->op ? -authsize : authsize), CHCR_DST_SG_SIZE, 0);
2462 dnents += MIN_AUTH_SG; // For IV
2463 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
2464 CHCR_SRC_SG_SIZE, 0);
2465 dst_size = get_space_for_phys_dsgl(dnents);
2466 kctx_len = (KEY_CONTEXT_CTX_LEN_G(ntohl(aeadctx->key_ctx_hdr)) << 4)
2467 - sizeof(chcr_req->key_ctx);
2468 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
2469 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen) <
2470 SGE_MAX_WR_LEN;
2471 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen, 16)
2472 : (sgl_len(snents) * 8);
2473 transhdr_len += temp;
2474 transhdr_len = roundup(transhdr_len, 16);
2476 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE,
2477 transhdr_len, reqctx->op)) {
2478 atomic_inc(&adap->chcr_stats.fallback);
2479 chcr_aead_common_exit(req);
2480 return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
2482 skb = alloc_skb(transhdr_len, flags);
2483 if (!skb) {
2484 error = -ENOMEM;
2485 goto err;
2488 chcr_req = __skb_put_zero(skb, transhdr_len);
2490 temp = (reqctx->op == CHCR_ENCRYPT_OP) ? 0 : authsize;
2493 * Input order is AAD,IV and Payload. where IV should be included as
2494 * the part of authdata. All other fields should be filled according
2495 * to the hardware spec
2497 chcr_req->sec_cpl.op_ivinsrtofst =
2498 FILL_SEC_CPL_OP_IVINSR(rx_channel_id, 2, 1);
2499 chcr_req->sec_cpl.pldlen = htonl(req->assoclen + IV + req->cryptlen);
2500 chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
2501 null ? 0 : 1 + IV,
2502 null ? 0 : IV + req->assoclen,
2503 req->assoclen + IV + 1,
2504 (temp & 0x1F0) >> 4);
2505 chcr_req->sec_cpl.cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(
2506 temp & 0xF,
2507 null ? 0 : req->assoclen + IV + 1,
2508 temp, temp);
2509 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL ||
2510 subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA)
2511 temp = CHCR_SCMD_CIPHER_MODE_AES_CTR;
2512 else
2513 temp = CHCR_SCMD_CIPHER_MODE_AES_CBC;
2514 chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op,
2515 (reqctx->op == CHCR_ENCRYPT_OP) ? 1 : 0,
2516 temp,
2517 actx->auth_mode, aeadctx->hmac_ctrl,
2518 IV >> 1);
2519 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
2520 0, 0, dst_size);
2522 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
2523 if (reqctx->op == CHCR_ENCRYPT_OP ||
2524 subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
2525 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL)
2526 memcpy(chcr_req->key_ctx.key, aeadctx->key,
2527 aeadctx->enckey_len);
2528 else
2529 memcpy(chcr_req->key_ctx.key, actx->dec_rrkey,
2530 aeadctx->enckey_len);
2532 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
2533 actx->h_iopad, kctx_len - roundup(aeadctx->enckey_len, 16));
2534 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
2535 ivptr = (u8 *)(phys_cpl + 1) + dst_size;
2536 ulptx = (struct ulptx_sgl *)(ivptr + IV);
2537 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
2538 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
2539 memcpy(ivptr, aeadctx->nonce, CTR_RFC3686_NONCE_SIZE);
2540 memcpy(ivptr + CTR_RFC3686_NONCE_SIZE, req->iv,
2541 CTR_RFC3686_IV_SIZE);
2542 *(__be32 *)(ivptr + CTR_RFC3686_NONCE_SIZE +
2543 CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
2544 } else {
2545 memcpy(ivptr, req->iv, IV);
2547 chcr_add_aead_dst_ent(req, phys_cpl, qid);
2548 chcr_add_aead_src_ent(req, ulptx);
2549 atomic_inc(&adap->chcr_stats.cipher_rqst);
2550 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
2551 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen) : 0);
2552 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, size,
2553 transhdr_len, temp, 0);
2554 reqctx->skb = skb;
2556 return skb;
2557 err:
2558 chcr_aead_common_exit(req);
2560 return ERR_PTR(error);
2563 int chcr_aead_dma_map(struct device *dev,
2564 struct aead_request *req,
2565 unsigned short op_type)
2567 int error;
2568 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2569 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2570 unsigned int authsize = crypto_aead_authsize(tfm);
2571 int dst_size;
2573 dst_size = req->assoclen + req->cryptlen + (op_type ?
2574 -authsize : authsize);
2575 if (!req->cryptlen || !dst_size)
2576 return 0;
2577 reqctx->iv_dma = dma_map_single(dev, reqctx->iv, (IV + reqctx->b0_len),
2578 DMA_BIDIRECTIONAL);
2579 if (dma_mapping_error(dev, reqctx->iv_dma))
2580 return -ENOMEM;
2581 if (reqctx->b0_len)
2582 reqctx->b0_dma = reqctx->iv_dma + IV;
2583 else
2584 reqctx->b0_dma = 0;
2585 if (req->src == req->dst) {
2586 error = dma_map_sg(dev, req->src,
2587 sg_nents_for_len(req->src, dst_size),
2588 DMA_BIDIRECTIONAL);
2589 if (!error)
2590 goto err;
2591 } else {
2592 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2593 DMA_TO_DEVICE);
2594 if (!error)
2595 goto err;
2596 error = dma_map_sg(dev, req->dst, sg_nents(req->dst),
2597 DMA_FROM_DEVICE);
2598 if (!error) {
2599 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2600 DMA_TO_DEVICE);
2601 goto err;
2605 return 0;
2606 err:
2607 dma_unmap_single(dev, reqctx->iv_dma, IV, DMA_BIDIRECTIONAL);
2608 return -ENOMEM;
2611 void chcr_aead_dma_unmap(struct device *dev,
2612 struct aead_request *req,
2613 unsigned short op_type)
2615 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2616 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2617 unsigned int authsize = crypto_aead_authsize(tfm);
2618 int dst_size;
2620 dst_size = req->assoclen + req->cryptlen + (op_type ?
2621 -authsize : authsize);
2622 if (!req->cryptlen || !dst_size)
2623 return;
2625 dma_unmap_single(dev, reqctx->iv_dma, (IV + reqctx->b0_len),
2626 DMA_BIDIRECTIONAL);
2627 if (req->src == req->dst) {
2628 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2629 DMA_BIDIRECTIONAL);
2630 } else {
2631 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2632 DMA_TO_DEVICE);
2633 dma_unmap_sg(dev, req->dst, sg_nents(req->dst),
2634 DMA_FROM_DEVICE);
2638 void chcr_add_aead_src_ent(struct aead_request *req,
2639 struct ulptx_sgl *ulptx)
2641 struct ulptx_walk ulp_walk;
2642 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2644 if (reqctx->imm) {
2645 u8 *buf = (u8 *)ulptx;
2647 if (reqctx->b0_len) {
2648 memcpy(buf, reqctx->scratch_pad, reqctx->b0_len);
2649 buf += reqctx->b0_len;
2651 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
2652 buf, req->cryptlen + req->assoclen, 0);
2653 } else {
2654 ulptx_walk_init(&ulp_walk, ulptx);
2655 if (reqctx->b0_len)
2656 ulptx_walk_add_page(&ulp_walk, reqctx->b0_len,
2657 reqctx->b0_dma);
2658 ulptx_walk_add_sg(&ulp_walk, req->src, req->cryptlen +
2659 req->assoclen, 0);
2660 ulptx_walk_end(&ulp_walk);
2664 void chcr_add_aead_dst_ent(struct aead_request *req,
2665 struct cpl_rx_phys_dsgl *phys_cpl,
2666 unsigned short qid)
2668 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2669 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2670 struct dsgl_walk dsgl_walk;
2671 unsigned int authsize = crypto_aead_authsize(tfm);
2672 struct chcr_context *ctx = a_ctx(tfm);
2673 u32 temp;
2674 unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
2676 dsgl_walk_init(&dsgl_walk, phys_cpl);
2677 dsgl_walk_add_page(&dsgl_walk, IV + reqctx->b0_len, reqctx->iv_dma);
2678 temp = req->assoclen + req->cryptlen +
2679 (reqctx->op ? -authsize : authsize);
2680 dsgl_walk_add_sg(&dsgl_walk, req->dst, temp, 0);
2681 dsgl_walk_end(&dsgl_walk, qid, rx_channel_id);
2684 void chcr_add_cipher_src_ent(struct skcipher_request *req,
2685 void *ulptx,
2686 struct cipher_wr_param *wrparam)
2688 struct ulptx_walk ulp_walk;
2689 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
2690 u8 *buf = ulptx;
2692 memcpy(buf, reqctx->iv, IV);
2693 buf += IV;
2694 if (reqctx->imm) {
2695 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
2696 buf, wrparam->bytes, reqctx->processed);
2697 } else {
2698 ulptx_walk_init(&ulp_walk, (struct ulptx_sgl *)buf);
2699 ulptx_walk_add_sg(&ulp_walk, reqctx->srcsg, wrparam->bytes,
2700 reqctx->src_ofst);
2701 reqctx->srcsg = ulp_walk.last_sg;
2702 reqctx->src_ofst = ulp_walk.last_sg_len;
2703 ulptx_walk_end(&ulp_walk);
2707 void chcr_add_cipher_dst_ent(struct skcipher_request *req,
2708 struct cpl_rx_phys_dsgl *phys_cpl,
2709 struct cipher_wr_param *wrparam,
2710 unsigned short qid)
2712 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
2713 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(wrparam->req);
2714 struct chcr_context *ctx = c_ctx(tfm);
2715 struct dsgl_walk dsgl_walk;
2716 unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
2718 dsgl_walk_init(&dsgl_walk, phys_cpl);
2719 dsgl_walk_add_sg(&dsgl_walk, reqctx->dstsg, wrparam->bytes,
2720 reqctx->dst_ofst);
2721 reqctx->dstsg = dsgl_walk.last_sg;
2722 reqctx->dst_ofst = dsgl_walk.last_sg_len;
2723 dsgl_walk_end(&dsgl_walk, qid, rx_channel_id);
2726 void chcr_add_hash_src_ent(struct ahash_request *req,
2727 struct ulptx_sgl *ulptx,
2728 struct hash_wr_param *param)
2730 struct ulptx_walk ulp_walk;
2731 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
2733 if (reqctx->hctx_wr.imm) {
2734 u8 *buf = (u8 *)ulptx;
2736 if (param->bfr_len) {
2737 memcpy(buf, reqctx->reqbfr, param->bfr_len);
2738 buf += param->bfr_len;
2741 sg_pcopy_to_buffer(reqctx->hctx_wr.srcsg,
2742 sg_nents(reqctx->hctx_wr.srcsg), buf,
2743 param->sg_len, 0);
2744 } else {
2745 ulptx_walk_init(&ulp_walk, ulptx);
2746 if (param->bfr_len)
2747 ulptx_walk_add_page(&ulp_walk, param->bfr_len,
2748 reqctx->hctx_wr.dma_addr);
2749 ulptx_walk_add_sg(&ulp_walk, reqctx->hctx_wr.srcsg,
2750 param->sg_len, reqctx->hctx_wr.src_ofst);
2751 reqctx->hctx_wr.srcsg = ulp_walk.last_sg;
2752 reqctx->hctx_wr.src_ofst = ulp_walk.last_sg_len;
2753 ulptx_walk_end(&ulp_walk);
2757 int chcr_hash_dma_map(struct device *dev,
2758 struct ahash_request *req)
2760 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
2761 int error = 0;
2763 if (!req->nbytes)
2764 return 0;
2765 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2766 DMA_TO_DEVICE);
2767 if (!error)
2768 return -ENOMEM;
2769 req_ctx->hctx_wr.is_sg_map = 1;
2770 return 0;
2773 void chcr_hash_dma_unmap(struct device *dev,
2774 struct ahash_request *req)
2776 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
2778 if (!req->nbytes)
2779 return;
2781 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2782 DMA_TO_DEVICE);
2783 req_ctx->hctx_wr.is_sg_map = 0;
2787 int chcr_cipher_dma_map(struct device *dev,
2788 struct skcipher_request *req)
2790 int error;
2792 if (req->src == req->dst) {
2793 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2794 DMA_BIDIRECTIONAL);
2795 if (!error)
2796 goto err;
2797 } else {
2798 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2799 DMA_TO_DEVICE);
2800 if (!error)
2801 goto err;
2802 error = dma_map_sg(dev, req->dst, sg_nents(req->dst),
2803 DMA_FROM_DEVICE);
2804 if (!error) {
2805 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2806 DMA_TO_DEVICE);
2807 goto err;
2811 return 0;
2812 err:
2813 return -ENOMEM;
2816 void chcr_cipher_dma_unmap(struct device *dev,
2817 struct skcipher_request *req)
2819 if (req->src == req->dst) {
2820 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2821 DMA_BIDIRECTIONAL);
2822 } else {
2823 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2824 DMA_TO_DEVICE);
2825 dma_unmap_sg(dev, req->dst, sg_nents(req->dst),
2826 DMA_FROM_DEVICE);
2830 static int set_msg_len(u8 *block, unsigned int msglen, int csize)
2832 __be32 data;
2834 memset(block, 0, csize);
2835 block += csize;
2837 if (csize >= 4)
2838 csize = 4;
2839 else if (msglen > (unsigned int)(1 << (8 * csize)))
2840 return -EOVERFLOW;
2842 data = cpu_to_be32(msglen);
2843 memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
2845 return 0;
2848 static int generate_b0(struct aead_request *req, u8 *ivptr,
2849 unsigned short op_type)
2851 unsigned int l, lp, m;
2852 int rc;
2853 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2854 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2855 u8 *b0 = reqctx->scratch_pad;
2857 m = crypto_aead_authsize(aead);
2859 memcpy(b0, ivptr, 16);
2861 lp = b0[0];
2862 l = lp + 1;
2864 /* set m, bits 3-5 */
2865 *b0 |= (8 * ((m - 2) / 2));
2867 /* set adata, bit 6, if associated data is used */
2868 if (req->assoclen)
2869 *b0 |= 64;
2870 rc = set_msg_len(b0 + 16 - l,
2871 (op_type == CHCR_DECRYPT_OP) ?
2872 req->cryptlen - m : req->cryptlen, l);
2874 return rc;
2877 static inline int crypto_ccm_check_iv(const u8 *iv)
2879 /* 2 <= L <= 8, so 1 <= L' <= 7. */
2880 if (iv[0] < 1 || iv[0] > 7)
2881 return -EINVAL;
2883 return 0;
2886 static int ccm_format_packet(struct aead_request *req,
2887 u8 *ivptr,
2888 unsigned int sub_type,
2889 unsigned short op_type,
2890 unsigned int assoclen)
2892 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2893 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2894 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2895 int rc = 0;
2897 if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
2898 ivptr[0] = 3;
2899 memcpy(ivptr + 1, &aeadctx->salt[0], 3);
2900 memcpy(ivptr + 4, req->iv, 8);
2901 memset(ivptr + 12, 0, 4);
2902 } else {
2903 memcpy(ivptr, req->iv, 16);
2905 if (assoclen)
2906 *((unsigned short *)(reqctx->scratch_pad + 16)) =
2907 htons(assoclen);
2909 rc = generate_b0(req, ivptr, op_type);
2910 /* zero the ctr value */
2911 memset(ivptr + 15 - ivptr[0], 0, ivptr[0] + 1);
2912 return rc;
2915 static void fill_sec_cpl_for_aead(struct cpl_tx_sec_pdu *sec_cpl,
2916 unsigned int dst_size,
2917 struct aead_request *req,
2918 unsigned short op_type)
2920 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2921 struct chcr_context *ctx = a_ctx(tfm);
2922 struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
2923 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2924 unsigned int cipher_mode = CHCR_SCMD_CIPHER_MODE_AES_CCM;
2925 unsigned int mac_mode = CHCR_SCMD_AUTH_MODE_CBCMAC;
2926 unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
2927 unsigned int ccm_xtra;
2928 unsigned int tag_offset = 0, auth_offset = 0;
2929 unsigned int assoclen;
2931 if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
2932 assoclen = req->assoclen - 8;
2933 else
2934 assoclen = req->assoclen;
2935 ccm_xtra = CCM_B0_SIZE +
2936 ((assoclen) ? CCM_AAD_FIELD_SIZE : 0);
2938 auth_offset = req->cryptlen ?
2939 (req->assoclen + IV + 1 + ccm_xtra) : 0;
2940 if (op_type == CHCR_DECRYPT_OP) {
2941 if (crypto_aead_authsize(tfm) != req->cryptlen)
2942 tag_offset = crypto_aead_authsize(tfm);
2943 else
2944 auth_offset = 0;
2947 sec_cpl->op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(rx_channel_id, 2, 1);
2948 sec_cpl->pldlen =
2949 htonl(req->assoclen + IV + req->cryptlen + ccm_xtra);
2950 /* For CCM there wil be b0 always. So AAD start will be 1 always */
2951 sec_cpl->aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
2952 1 + IV, IV + assoclen + ccm_xtra,
2953 req->assoclen + IV + 1 + ccm_xtra, 0);
2955 sec_cpl->cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(0,
2956 auth_offset, tag_offset,
2957 (op_type == CHCR_ENCRYPT_OP) ? 0 :
2958 crypto_aead_authsize(tfm));
2959 sec_cpl->seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(op_type,
2960 (op_type == CHCR_ENCRYPT_OP) ? 0 : 1,
2961 cipher_mode, mac_mode,
2962 aeadctx->hmac_ctrl, IV >> 1);
2964 sec_cpl->ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1, 0,
2965 0, dst_size);
2968 static int aead_ccm_validate_input(unsigned short op_type,
2969 struct aead_request *req,
2970 struct chcr_aead_ctx *aeadctx,
2971 unsigned int sub_type)
2973 if (sub_type != CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
2974 if (crypto_ccm_check_iv(req->iv)) {
2975 pr_err("CCM: IV check fails\n");
2976 return -EINVAL;
2978 } else {
2979 if (req->assoclen != 16 && req->assoclen != 20) {
2980 pr_err("RFC4309: Invalid AAD length %d\n",
2981 req->assoclen);
2982 return -EINVAL;
2985 return 0;
2988 static struct sk_buff *create_aead_ccm_wr(struct aead_request *req,
2989 unsigned short qid,
2990 int size)
2992 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2993 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2994 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2995 struct sk_buff *skb = NULL;
2996 struct chcr_wr *chcr_req;
2997 struct cpl_rx_phys_dsgl *phys_cpl;
2998 struct ulptx_sgl *ulptx;
2999 unsigned int transhdr_len;
3000 unsigned int dst_size = 0, kctx_len, dnents, temp, snents;
3001 unsigned int sub_type, assoclen = req->assoclen;
3002 unsigned int authsize = crypto_aead_authsize(tfm);
3003 int error = -EINVAL;
3004 u8 *ivptr;
3005 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
3006 GFP_ATOMIC;
3007 struct adapter *adap = padap(a_ctx(tfm)->dev);
3009 sub_type = get_aead_subtype(tfm);
3010 if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
3011 assoclen -= 8;
3012 reqctx->b0_len = CCM_B0_SIZE + (assoclen ? CCM_AAD_FIELD_SIZE : 0);
3013 error = chcr_aead_common_init(req);
3014 if (error)
3015 return ERR_PTR(error);
3017 error = aead_ccm_validate_input(reqctx->op, req, aeadctx, sub_type);
3018 if (error)
3019 goto err;
3020 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen
3021 + (reqctx->op ? -authsize : authsize),
3022 CHCR_DST_SG_SIZE, 0);
3023 dnents += MIN_CCM_SG; // For IV and B0
3024 dst_size = get_space_for_phys_dsgl(dnents);
3025 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
3026 CHCR_SRC_SG_SIZE, 0);
3027 snents += MIN_CCM_SG; //For B0
3028 kctx_len = roundup(aeadctx->enckey_len, 16) * 2;
3029 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
3030 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen +
3031 reqctx->b0_len) <= SGE_MAX_WR_LEN;
3032 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen +
3033 reqctx->b0_len, 16) :
3034 (sgl_len(snents) * 8);
3035 transhdr_len += temp;
3036 transhdr_len = roundup(transhdr_len, 16);
3038 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE -
3039 reqctx->b0_len, transhdr_len, reqctx->op)) {
3040 atomic_inc(&adap->chcr_stats.fallback);
3041 chcr_aead_common_exit(req);
3042 return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
3044 skb = alloc_skb(transhdr_len, flags);
3046 if (!skb) {
3047 error = -ENOMEM;
3048 goto err;
3051 chcr_req = __skb_put_zero(skb, transhdr_len);
3053 fill_sec_cpl_for_aead(&chcr_req->sec_cpl, dst_size, req, reqctx->op);
3055 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
3056 memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len);
3057 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
3058 aeadctx->key, aeadctx->enckey_len);
3060 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
3061 ivptr = (u8 *)(phys_cpl + 1) + dst_size;
3062 ulptx = (struct ulptx_sgl *)(ivptr + IV);
3063 error = ccm_format_packet(req, ivptr, sub_type, reqctx->op, assoclen);
3064 if (error)
3065 goto dstmap_fail;
3066 chcr_add_aead_dst_ent(req, phys_cpl, qid);
3067 chcr_add_aead_src_ent(req, ulptx);
3069 atomic_inc(&adap->chcr_stats.aead_rqst);
3070 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
3071 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen +
3072 reqctx->b0_len) : 0);
3073 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, 0,
3074 transhdr_len, temp, 0);
3075 reqctx->skb = skb;
3077 return skb;
3078 dstmap_fail:
3079 kfree_skb(skb);
3080 err:
3081 chcr_aead_common_exit(req);
3082 return ERR_PTR(error);
3085 static struct sk_buff *create_gcm_wr(struct aead_request *req,
3086 unsigned short qid,
3087 int size)
3089 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3090 struct chcr_context *ctx = a_ctx(tfm);
3091 struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
3092 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
3093 struct sk_buff *skb = NULL;
3094 struct chcr_wr *chcr_req;
3095 struct cpl_rx_phys_dsgl *phys_cpl;
3096 struct ulptx_sgl *ulptx;
3097 unsigned int transhdr_len, dnents = 0, snents;
3098 unsigned int dst_size = 0, temp = 0, kctx_len, assoclen = req->assoclen;
3099 unsigned int authsize = crypto_aead_authsize(tfm);
3100 int error = -EINVAL;
3101 u8 *ivptr;
3102 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
3103 GFP_ATOMIC;
3104 struct adapter *adap = padap(ctx->dev);
3105 unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
3107 if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106)
3108 assoclen = req->assoclen - 8;
3110 reqctx->b0_len = 0;
3111 error = chcr_aead_common_init(req);
3112 if (error)
3113 return ERR_PTR(error);
3114 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen +
3115 (reqctx->op ? -authsize : authsize),
3116 CHCR_DST_SG_SIZE, 0);
3117 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
3118 CHCR_SRC_SG_SIZE, 0);
3119 dnents += MIN_GCM_SG; // For IV
3120 dst_size = get_space_for_phys_dsgl(dnents);
3121 kctx_len = roundup(aeadctx->enckey_len, 16) + AEAD_H_SIZE;
3122 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
3123 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen) <=
3124 SGE_MAX_WR_LEN;
3125 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen, 16) :
3126 (sgl_len(snents) * 8);
3127 transhdr_len += temp;
3128 transhdr_len = roundup(transhdr_len, 16);
3129 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE,
3130 transhdr_len, reqctx->op)) {
3132 atomic_inc(&adap->chcr_stats.fallback);
3133 chcr_aead_common_exit(req);
3134 return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
3136 skb = alloc_skb(transhdr_len, flags);
3137 if (!skb) {
3138 error = -ENOMEM;
3139 goto err;
3142 chcr_req = __skb_put_zero(skb, transhdr_len);
3144 //Offset of tag from end
3145 temp = (reqctx->op == CHCR_ENCRYPT_OP) ? 0 : authsize;
3146 chcr_req->sec_cpl.op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(
3147 rx_channel_id, 2, 1);
3148 chcr_req->sec_cpl.pldlen =
3149 htonl(req->assoclen + IV + req->cryptlen);
3150 chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
3151 assoclen ? 1 + IV : 0,
3152 assoclen ? IV + assoclen : 0,
3153 req->assoclen + IV + 1, 0);
3154 chcr_req->sec_cpl.cipherstop_lo_authinsert =
3155 FILL_SEC_CPL_AUTHINSERT(0, req->assoclen + IV + 1,
3156 temp, temp);
3157 chcr_req->sec_cpl.seqno_numivs =
3158 FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op, (reqctx->op ==
3159 CHCR_ENCRYPT_OP) ? 1 : 0,
3160 CHCR_SCMD_CIPHER_MODE_AES_GCM,
3161 CHCR_SCMD_AUTH_MODE_GHASH,
3162 aeadctx->hmac_ctrl, IV >> 1);
3163 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
3164 0, 0, dst_size);
3165 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
3166 memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len);
3167 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
3168 GCM_CTX(aeadctx)->ghash_h, AEAD_H_SIZE);
3170 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
3171 ivptr = (u8 *)(phys_cpl + 1) + dst_size;
3172 /* prepare a 16 byte iv */
3173 /* S A L T | IV | 0x00000001 */
3174 if (get_aead_subtype(tfm) ==
3175 CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) {
3176 memcpy(ivptr, aeadctx->salt, 4);
3177 memcpy(ivptr + 4, req->iv, GCM_RFC4106_IV_SIZE);
3178 } else {
3179 memcpy(ivptr, req->iv, GCM_AES_IV_SIZE);
3181 *((unsigned int *)(ivptr + 12)) = htonl(0x01);
3183 ulptx = (struct ulptx_sgl *)(ivptr + 16);
3185 chcr_add_aead_dst_ent(req, phys_cpl, qid);
3186 chcr_add_aead_src_ent(req, ulptx);
3187 atomic_inc(&adap->chcr_stats.aead_rqst);
3188 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
3189 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen) : 0);
3190 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, size,
3191 transhdr_len, temp, reqctx->verify);
3192 reqctx->skb = skb;
3193 return skb;
3195 err:
3196 chcr_aead_common_exit(req);
3197 return ERR_PTR(error);
3202 static int chcr_aead_cra_init(struct crypto_aead *tfm)
3204 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3205 struct aead_alg *alg = crypto_aead_alg(tfm);
3207 aeadctx->sw_cipher = crypto_alloc_aead(alg->base.cra_name, 0,
3208 CRYPTO_ALG_NEED_FALLBACK |
3209 CRYPTO_ALG_ASYNC);
3210 if (IS_ERR(aeadctx->sw_cipher))
3211 return PTR_ERR(aeadctx->sw_cipher);
3212 crypto_aead_set_reqsize(tfm, max(sizeof(struct chcr_aead_reqctx),
3213 sizeof(struct aead_request) +
3214 crypto_aead_reqsize(aeadctx->sw_cipher)));
3215 return chcr_device_init(a_ctx(tfm));
3218 static void chcr_aead_cra_exit(struct crypto_aead *tfm)
3220 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3222 crypto_free_aead(aeadctx->sw_cipher);
3225 static int chcr_authenc_null_setauthsize(struct crypto_aead *tfm,
3226 unsigned int authsize)
3228 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3230 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NOP;
3231 aeadctx->mayverify = VERIFY_HW;
3232 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3234 static int chcr_authenc_setauthsize(struct crypto_aead *tfm,
3235 unsigned int authsize)
3237 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3238 u32 maxauth = crypto_aead_maxauthsize(tfm);
3240 /*SHA1 authsize in ipsec is 12 instead of 10 i.e maxauthsize / 2 is not
3241 * true for sha1. authsize == 12 condition should be before
3242 * authsize == (maxauth >> 1)
3244 if (authsize == ICV_4) {
3245 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
3246 aeadctx->mayverify = VERIFY_HW;
3247 } else if (authsize == ICV_6) {
3248 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
3249 aeadctx->mayverify = VERIFY_HW;
3250 } else if (authsize == ICV_10) {
3251 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
3252 aeadctx->mayverify = VERIFY_HW;
3253 } else if (authsize == ICV_12) {
3254 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3255 aeadctx->mayverify = VERIFY_HW;
3256 } else if (authsize == ICV_14) {
3257 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
3258 aeadctx->mayverify = VERIFY_HW;
3259 } else if (authsize == (maxauth >> 1)) {
3260 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3261 aeadctx->mayverify = VERIFY_HW;
3262 } else if (authsize == maxauth) {
3263 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3264 aeadctx->mayverify = VERIFY_HW;
3265 } else {
3266 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3267 aeadctx->mayverify = VERIFY_SW;
3269 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3273 static int chcr_gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
3275 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3277 switch (authsize) {
3278 case ICV_4:
3279 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
3280 aeadctx->mayverify = VERIFY_HW;
3281 break;
3282 case ICV_8:
3283 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3284 aeadctx->mayverify = VERIFY_HW;
3285 break;
3286 case ICV_12:
3287 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3288 aeadctx->mayverify = VERIFY_HW;
3289 break;
3290 case ICV_14:
3291 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
3292 aeadctx->mayverify = VERIFY_HW;
3293 break;
3294 case ICV_16:
3295 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3296 aeadctx->mayverify = VERIFY_HW;
3297 break;
3298 case ICV_13:
3299 case ICV_15:
3300 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3301 aeadctx->mayverify = VERIFY_SW;
3302 break;
3303 default:
3304 return -EINVAL;
3306 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3309 static int chcr_4106_4309_setauthsize(struct crypto_aead *tfm,
3310 unsigned int authsize)
3312 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3314 switch (authsize) {
3315 case ICV_8:
3316 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3317 aeadctx->mayverify = VERIFY_HW;
3318 break;
3319 case ICV_12:
3320 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3321 aeadctx->mayverify = VERIFY_HW;
3322 break;
3323 case ICV_16:
3324 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3325 aeadctx->mayverify = VERIFY_HW;
3326 break;
3327 default:
3328 return -EINVAL;
3330 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3333 static int chcr_ccm_setauthsize(struct crypto_aead *tfm,
3334 unsigned int authsize)
3336 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3338 switch (authsize) {
3339 case ICV_4:
3340 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
3341 aeadctx->mayverify = VERIFY_HW;
3342 break;
3343 case ICV_6:
3344 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
3345 aeadctx->mayverify = VERIFY_HW;
3346 break;
3347 case ICV_8:
3348 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3349 aeadctx->mayverify = VERIFY_HW;
3350 break;
3351 case ICV_10:
3352 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
3353 aeadctx->mayverify = VERIFY_HW;
3354 break;
3355 case ICV_12:
3356 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3357 aeadctx->mayverify = VERIFY_HW;
3358 break;
3359 case ICV_14:
3360 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
3361 aeadctx->mayverify = VERIFY_HW;
3362 break;
3363 case ICV_16:
3364 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3365 aeadctx->mayverify = VERIFY_HW;
3366 break;
3367 default:
3368 return -EINVAL;
3370 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3373 static int chcr_ccm_common_setkey(struct crypto_aead *aead,
3374 const u8 *key,
3375 unsigned int keylen)
3377 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
3378 unsigned char ck_size, mk_size;
3379 int key_ctx_size = 0;
3381 key_ctx_size = sizeof(struct _key_ctx) + roundup(keylen, 16) * 2;
3382 if (keylen == AES_KEYSIZE_128) {
3383 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3384 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_128;
3385 } else if (keylen == AES_KEYSIZE_192) {
3386 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3387 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_192;
3388 } else if (keylen == AES_KEYSIZE_256) {
3389 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3390 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
3391 } else {
3392 aeadctx->enckey_len = 0;
3393 return -EINVAL;
3395 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, mk_size, 0, 0,
3396 key_ctx_size >> 4);
3397 memcpy(aeadctx->key, key, keylen);
3398 aeadctx->enckey_len = keylen;
3400 return 0;
3403 static int chcr_aead_ccm_setkey(struct crypto_aead *aead,
3404 const u8 *key,
3405 unsigned int keylen)
3407 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
3408 int error;
3410 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3411 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) &
3412 CRYPTO_TFM_REQ_MASK);
3413 error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3414 if (error)
3415 return error;
3416 return chcr_ccm_common_setkey(aead, key, keylen);
3419 static int chcr_aead_rfc4309_setkey(struct crypto_aead *aead, const u8 *key,
3420 unsigned int keylen)
3422 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
3423 int error;
3425 if (keylen < 3) {
3426 aeadctx->enckey_len = 0;
3427 return -EINVAL;
3429 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3430 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) &
3431 CRYPTO_TFM_REQ_MASK);
3432 error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3433 if (error)
3434 return error;
3435 keylen -= 3;
3436 memcpy(aeadctx->salt, key + keylen, 3);
3437 return chcr_ccm_common_setkey(aead, key, keylen);
3440 static int chcr_gcm_setkey(struct crypto_aead *aead, const u8 *key,
3441 unsigned int keylen)
3443 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
3444 struct chcr_gcm_ctx *gctx = GCM_CTX(aeadctx);
3445 unsigned int ck_size;
3446 int ret = 0, key_ctx_size = 0;
3447 struct crypto_aes_ctx aes;
3449 aeadctx->enckey_len = 0;
3450 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3451 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead)
3452 & CRYPTO_TFM_REQ_MASK);
3453 ret = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3454 if (ret)
3455 goto out;
3457 if (get_aead_subtype(aead) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 &&
3458 keylen > 3) {
3459 keylen -= 4; /* nonce/salt is present in the last 4 bytes */
3460 memcpy(aeadctx->salt, key + keylen, 4);
3462 if (keylen == AES_KEYSIZE_128) {
3463 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3464 } else if (keylen == AES_KEYSIZE_192) {
3465 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3466 } else if (keylen == AES_KEYSIZE_256) {
3467 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3468 } else {
3469 pr_err("GCM: Invalid key length %d\n", keylen);
3470 ret = -EINVAL;
3471 goto out;
3474 memcpy(aeadctx->key, key, keylen);
3475 aeadctx->enckey_len = keylen;
3476 key_ctx_size = sizeof(struct _key_ctx) + roundup(keylen, 16) +
3477 AEAD_H_SIZE;
3478 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size,
3479 CHCR_KEYCTX_MAC_KEY_SIZE_128,
3480 0, 0,
3481 key_ctx_size >> 4);
3482 /* Calculate the H = CIPH(K, 0 repeated 16 times).
3483 * It will go in key context
3485 ret = aes_expandkey(&aes, key, keylen);
3486 if (ret) {
3487 aeadctx->enckey_len = 0;
3488 goto out;
3490 memset(gctx->ghash_h, 0, AEAD_H_SIZE);
3491 aes_encrypt(&aes, gctx->ghash_h, gctx->ghash_h);
3492 memzero_explicit(&aes, sizeof(aes));
3494 out:
3495 return ret;
3498 static int chcr_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
3499 unsigned int keylen)
3501 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(authenc));
3502 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
3503 /* it contains auth and cipher key both*/
3504 struct crypto_authenc_keys keys;
3505 unsigned int bs, subtype;
3506 unsigned int max_authsize = crypto_aead_alg(authenc)->maxauthsize;
3507 int err = 0, i, key_ctx_len = 0;
3508 unsigned char ck_size = 0;
3509 unsigned char pad[CHCR_HASH_MAX_BLOCK_SIZE_128] = { 0 };
3510 struct crypto_shash *base_hash = ERR_PTR(-EINVAL);
3511 struct algo_param param;
3512 int align;
3513 u8 *o_ptr = NULL;
3515 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3516 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
3517 & CRYPTO_TFM_REQ_MASK);
3518 err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3519 if (err)
3520 goto out;
3522 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
3523 goto out;
3525 if (get_alg_config(&param, max_authsize)) {
3526 pr_err("chcr : Unsupported digest size\n");
3527 goto out;
3529 subtype = get_aead_subtype(authenc);
3530 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
3531 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
3532 if (keys.enckeylen < CTR_RFC3686_NONCE_SIZE)
3533 goto out;
3534 memcpy(aeadctx->nonce, keys.enckey + (keys.enckeylen
3535 - CTR_RFC3686_NONCE_SIZE), CTR_RFC3686_NONCE_SIZE);
3536 keys.enckeylen -= CTR_RFC3686_NONCE_SIZE;
3538 if (keys.enckeylen == AES_KEYSIZE_128) {
3539 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3540 } else if (keys.enckeylen == AES_KEYSIZE_192) {
3541 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3542 } else if (keys.enckeylen == AES_KEYSIZE_256) {
3543 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3544 } else {
3545 pr_err("chcr : Unsupported cipher key\n");
3546 goto out;
3549 /* Copy only encryption key. We use authkey to generate h(ipad) and
3550 * h(opad) so authkey is not needed again. authkeylen size have the
3551 * size of the hash digest size.
3553 memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
3554 aeadctx->enckey_len = keys.enckeylen;
3555 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_SHA ||
3556 subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL) {
3558 get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
3559 aeadctx->enckey_len << 3);
3561 base_hash = chcr_alloc_shash(max_authsize);
3562 if (IS_ERR(base_hash)) {
3563 pr_err("chcr : Base driver cannot be loaded\n");
3564 aeadctx->enckey_len = 0;
3565 memzero_explicit(&keys, sizeof(keys));
3566 return -EINVAL;
3569 SHASH_DESC_ON_STACK(shash, base_hash);
3571 shash->tfm = base_hash;
3572 bs = crypto_shash_blocksize(base_hash);
3573 align = KEYCTX_ALIGN_PAD(max_authsize);
3574 o_ptr = actx->h_iopad + param.result_size + align;
3576 if (keys.authkeylen > bs) {
3577 err = crypto_shash_digest(shash, keys.authkey,
3578 keys.authkeylen,
3579 o_ptr);
3580 if (err) {
3581 pr_err("chcr : Base driver cannot be loaded\n");
3582 goto out;
3584 keys.authkeylen = max_authsize;
3585 } else
3586 memcpy(o_ptr, keys.authkey, keys.authkeylen);
3588 /* Compute the ipad-digest*/
3589 memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
3590 memcpy(pad, o_ptr, keys.authkeylen);
3591 for (i = 0; i < bs >> 2; i++)
3592 *((unsigned int *)pad + i) ^= IPAD_DATA;
3594 if (chcr_compute_partial_hash(shash, pad, actx->h_iopad,
3595 max_authsize))
3596 goto out;
3597 /* Compute the opad-digest */
3598 memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
3599 memcpy(pad, o_ptr, keys.authkeylen);
3600 for (i = 0; i < bs >> 2; i++)
3601 *((unsigned int *)pad + i) ^= OPAD_DATA;
3603 if (chcr_compute_partial_hash(shash, pad, o_ptr, max_authsize))
3604 goto out;
3606 /* convert the ipad and opad digest to network order */
3607 chcr_change_order(actx->h_iopad, param.result_size);
3608 chcr_change_order(o_ptr, param.result_size);
3609 key_ctx_len = sizeof(struct _key_ctx) +
3610 roundup(keys.enckeylen, 16) +
3611 (param.result_size + align) * 2;
3612 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, param.mk_size,
3613 0, 1, key_ctx_len >> 4);
3614 actx->auth_mode = param.auth_mode;
3615 chcr_free_shash(base_hash);
3617 memzero_explicit(&keys, sizeof(keys));
3618 return 0;
3620 out:
3621 aeadctx->enckey_len = 0;
3622 memzero_explicit(&keys, sizeof(keys));
3623 if (!IS_ERR(base_hash))
3624 chcr_free_shash(base_hash);
3625 return -EINVAL;
3628 static int chcr_aead_digest_null_setkey(struct crypto_aead *authenc,
3629 const u8 *key, unsigned int keylen)
3631 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(authenc));
3632 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
3633 struct crypto_authenc_keys keys;
3634 int err;
3635 /* it contains auth and cipher key both*/
3636 unsigned int subtype;
3637 int key_ctx_len = 0;
3638 unsigned char ck_size = 0;
3640 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3641 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
3642 & CRYPTO_TFM_REQ_MASK);
3643 err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3644 if (err)
3645 goto out;
3647 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
3648 goto out;
3650 subtype = get_aead_subtype(authenc);
3651 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
3652 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
3653 if (keys.enckeylen < CTR_RFC3686_NONCE_SIZE)
3654 goto out;
3655 memcpy(aeadctx->nonce, keys.enckey + (keys.enckeylen
3656 - CTR_RFC3686_NONCE_SIZE), CTR_RFC3686_NONCE_SIZE);
3657 keys.enckeylen -= CTR_RFC3686_NONCE_SIZE;
3659 if (keys.enckeylen == AES_KEYSIZE_128) {
3660 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3661 } else if (keys.enckeylen == AES_KEYSIZE_192) {
3662 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3663 } else if (keys.enckeylen == AES_KEYSIZE_256) {
3664 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3665 } else {
3666 pr_err("chcr : Unsupported cipher key %d\n", keys.enckeylen);
3667 goto out;
3669 memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
3670 aeadctx->enckey_len = keys.enckeylen;
3671 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_SHA ||
3672 subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL) {
3673 get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
3674 aeadctx->enckey_len << 3);
3676 key_ctx_len = sizeof(struct _key_ctx) + roundup(keys.enckeylen, 16);
3678 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY, 0,
3679 0, key_ctx_len >> 4);
3680 actx->auth_mode = CHCR_SCMD_AUTH_MODE_NOP;
3681 memzero_explicit(&keys, sizeof(keys));
3682 return 0;
3683 out:
3684 aeadctx->enckey_len = 0;
3685 memzero_explicit(&keys, sizeof(keys));
3686 return -EINVAL;
3689 static int chcr_aead_op(struct aead_request *req,
3690 int size,
3691 create_wr_t create_wr_fn)
3693 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3694 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
3695 struct chcr_context *ctx = a_ctx(tfm);
3696 struct uld_ctx *u_ctx = ULD_CTX(ctx);
3697 struct sk_buff *skb;
3698 struct chcr_dev *cdev;
3700 cdev = a_ctx(tfm)->dev;
3701 if (!cdev) {
3702 pr_err("chcr : %s : No crypto device.\n", __func__);
3703 return -ENXIO;
3706 if (chcr_inc_wrcount(cdev)) {
3707 /* Detach state for CHCR means lldi or padap is freed.
3708 * We cannot increment fallback here.
3710 return chcr_aead_fallback(req, reqctx->op);
3713 if (cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
3714 reqctx->txqidx) &&
3715 (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))) {
3716 chcr_dec_wrcount(cdev);
3717 return -ENOSPC;
3720 /* Form a WR from req */
3721 skb = create_wr_fn(req, u_ctx->lldi.rxq_ids[reqctx->rxqidx], size);
3723 if (IS_ERR_OR_NULL(skb)) {
3724 chcr_dec_wrcount(cdev);
3725 return PTR_ERR_OR_ZERO(skb);
3728 skb->dev = u_ctx->lldi.ports[0];
3729 set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx);
3730 chcr_send_wr(skb);
3731 return -EINPROGRESS;
3734 static int chcr_aead_encrypt(struct aead_request *req)
3736 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3737 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
3738 struct chcr_context *ctx = a_ctx(tfm);
3739 unsigned int cpu;
3741 cpu = get_cpu();
3742 reqctx->txqidx = cpu % ctx->ntxq;
3743 reqctx->rxqidx = cpu % ctx->nrxq;
3744 put_cpu();
3746 reqctx->verify = VERIFY_HW;
3747 reqctx->op = CHCR_ENCRYPT_OP;
3749 switch (get_aead_subtype(tfm)) {
3750 case CRYPTO_ALG_SUB_TYPE_CTR_SHA:
3751 case CRYPTO_ALG_SUB_TYPE_CBC_SHA:
3752 case CRYPTO_ALG_SUB_TYPE_CBC_NULL:
3753 case CRYPTO_ALG_SUB_TYPE_CTR_NULL:
3754 return chcr_aead_op(req, 0, create_authenc_wr);
3755 case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
3756 case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
3757 return chcr_aead_op(req, 0, create_aead_ccm_wr);
3758 default:
3759 return chcr_aead_op(req, 0, create_gcm_wr);
3763 static int chcr_aead_decrypt(struct aead_request *req)
3765 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3766 struct chcr_context *ctx = a_ctx(tfm);
3767 struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx);
3768 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
3769 int size;
3770 unsigned int cpu;
3772 cpu = get_cpu();
3773 reqctx->txqidx = cpu % ctx->ntxq;
3774 reqctx->rxqidx = cpu % ctx->nrxq;
3775 put_cpu();
3777 if (aeadctx->mayverify == VERIFY_SW) {
3778 size = crypto_aead_maxauthsize(tfm);
3779 reqctx->verify = VERIFY_SW;
3780 } else {
3781 size = 0;
3782 reqctx->verify = VERIFY_HW;
3784 reqctx->op = CHCR_DECRYPT_OP;
3785 switch (get_aead_subtype(tfm)) {
3786 case CRYPTO_ALG_SUB_TYPE_CBC_SHA:
3787 case CRYPTO_ALG_SUB_TYPE_CTR_SHA:
3788 case CRYPTO_ALG_SUB_TYPE_CBC_NULL:
3789 case CRYPTO_ALG_SUB_TYPE_CTR_NULL:
3790 return chcr_aead_op(req, size, create_authenc_wr);
3791 case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
3792 case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
3793 return chcr_aead_op(req, size, create_aead_ccm_wr);
3794 default:
3795 return chcr_aead_op(req, size, create_gcm_wr);
3799 static struct chcr_alg_template driver_algs[] = {
3800 /* AES-CBC */
3802 .type = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_SUB_TYPE_CBC,
3803 .is_registered = 0,
3804 .alg.skcipher = {
3805 .base.cra_name = "cbc(aes)",
3806 .base.cra_driver_name = "cbc-aes-chcr",
3807 .base.cra_blocksize = AES_BLOCK_SIZE,
3809 .init = chcr_init_tfm,
3810 .exit = chcr_exit_tfm,
3811 .min_keysize = AES_MIN_KEY_SIZE,
3812 .max_keysize = AES_MAX_KEY_SIZE,
3813 .ivsize = AES_BLOCK_SIZE,
3814 .setkey = chcr_aes_cbc_setkey,
3815 .encrypt = chcr_aes_encrypt,
3816 .decrypt = chcr_aes_decrypt,
3820 .type = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_SUB_TYPE_XTS,
3821 .is_registered = 0,
3822 .alg.skcipher = {
3823 .base.cra_name = "xts(aes)",
3824 .base.cra_driver_name = "xts-aes-chcr",
3825 .base.cra_blocksize = AES_BLOCK_SIZE,
3827 .init = chcr_init_tfm,
3828 .exit = chcr_exit_tfm,
3829 .min_keysize = 2 * AES_MIN_KEY_SIZE,
3830 .max_keysize = 2 * AES_MAX_KEY_SIZE,
3831 .ivsize = AES_BLOCK_SIZE,
3832 .setkey = chcr_aes_xts_setkey,
3833 .encrypt = chcr_aes_encrypt,
3834 .decrypt = chcr_aes_decrypt,
3838 .type = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_SUB_TYPE_CTR,
3839 .is_registered = 0,
3840 .alg.skcipher = {
3841 .base.cra_name = "ctr(aes)",
3842 .base.cra_driver_name = "ctr-aes-chcr",
3843 .base.cra_blocksize = 1,
3845 .init = chcr_init_tfm,
3846 .exit = chcr_exit_tfm,
3847 .min_keysize = AES_MIN_KEY_SIZE,
3848 .max_keysize = AES_MAX_KEY_SIZE,
3849 .ivsize = AES_BLOCK_SIZE,
3850 .setkey = chcr_aes_ctr_setkey,
3851 .encrypt = chcr_aes_encrypt,
3852 .decrypt = chcr_aes_decrypt,
3856 .type = CRYPTO_ALG_TYPE_SKCIPHER |
3857 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686,
3858 .is_registered = 0,
3859 .alg.skcipher = {
3860 .base.cra_name = "rfc3686(ctr(aes))",
3861 .base.cra_driver_name = "rfc3686-ctr-aes-chcr",
3862 .base.cra_blocksize = 1,
3864 .init = chcr_rfc3686_init,
3865 .exit = chcr_exit_tfm,
3866 .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
3867 .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
3868 .ivsize = CTR_RFC3686_IV_SIZE,
3869 .setkey = chcr_aes_rfc3686_setkey,
3870 .encrypt = chcr_aes_encrypt,
3871 .decrypt = chcr_aes_decrypt,
3874 /* SHA */
3876 .type = CRYPTO_ALG_TYPE_AHASH,
3877 .is_registered = 0,
3878 .alg.hash = {
3879 .halg.digestsize = SHA1_DIGEST_SIZE,
3880 .halg.base = {
3881 .cra_name = "sha1",
3882 .cra_driver_name = "sha1-chcr",
3883 .cra_blocksize = SHA1_BLOCK_SIZE,
3888 .type = CRYPTO_ALG_TYPE_AHASH,
3889 .is_registered = 0,
3890 .alg.hash = {
3891 .halg.digestsize = SHA256_DIGEST_SIZE,
3892 .halg.base = {
3893 .cra_name = "sha256",
3894 .cra_driver_name = "sha256-chcr",
3895 .cra_blocksize = SHA256_BLOCK_SIZE,
3900 .type = CRYPTO_ALG_TYPE_AHASH,
3901 .is_registered = 0,
3902 .alg.hash = {
3903 .halg.digestsize = SHA224_DIGEST_SIZE,
3904 .halg.base = {
3905 .cra_name = "sha224",
3906 .cra_driver_name = "sha224-chcr",
3907 .cra_blocksize = SHA224_BLOCK_SIZE,
3912 .type = CRYPTO_ALG_TYPE_AHASH,
3913 .is_registered = 0,
3914 .alg.hash = {
3915 .halg.digestsize = SHA384_DIGEST_SIZE,
3916 .halg.base = {
3917 .cra_name = "sha384",
3918 .cra_driver_name = "sha384-chcr",
3919 .cra_blocksize = SHA384_BLOCK_SIZE,
3924 .type = CRYPTO_ALG_TYPE_AHASH,
3925 .is_registered = 0,
3926 .alg.hash = {
3927 .halg.digestsize = SHA512_DIGEST_SIZE,
3928 .halg.base = {
3929 .cra_name = "sha512",
3930 .cra_driver_name = "sha512-chcr",
3931 .cra_blocksize = SHA512_BLOCK_SIZE,
3935 /* HMAC */
3937 .type = CRYPTO_ALG_TYPE_HMAC,
3938 .is_registered = 0,
3939 .alg.hash = {
3940 .halg.digestsize = SHA1_DIGEST_SIZE,
3941 .halg.base = {
3942 .cra_name = "hmac(sha1)",
3943 .cra_driver_name = "hmac-sha1-chcr",
3944 .cra_blocksize = SHA1_BLOCK_SIZE,
3949 .type = CRYPTO_ALG_TYPE_HMAC,
3950 .is_registered = 0,
3951 .alg.hash = {
3952 .halg.digestsize = SHA224_DIGEST_SIZE,
3953 .halg.base = {
3954 .cra_name = "hmac(sha224)",
3955 .cra_driver_name = "hmac-sha224-chcr",
3956 .cra_blocksize = SHA224_BLOCK_SIZE,
3961 .type = CRYPTO_ALG_TYPE_HMAC,
3962 .is_registered = 0,
3963 .alg.hash = {
3964 .halg.digestsize = SHA256_DIGEST_SIZE,
3965 .halg.base = {
3966 .cra_name = "hmac(sha256)",
3967 .cra_driver_name = "hmac-sha256-chcr",
3968 .cra_blocksize = SHA256_BLOCK_SIZE,
3973 .type = CRYPTO_ALG_TYPE_HMAC,
3974 .is_registered = 0,
3975 .alg.hash = {
3976 .halg.digestsize = SHA384_DIGEST_SIZE,
3977 .halg.base = {
3978 .cra_name = "hmac(sha384)",
3979 .cra_driver_name = "hmac-sha384-chcr",
3980 .cra_blocksize = SHA384_BLOCK_SIZE,
3985 .type = CRYPTO_ALG_TYPE_HMAC,
3986 .is_registered = 0,
3987 .alg.hash = {
3988 .halg.digestsize = SHA512_DIGEST_SIZE,
3989 .halg.base = {
3990 .cra_name = "hmac(sha512)",
3991 .cra_driver_name = "hmac-sha512-chcr",
3992 .cra_blocksize = SHA512_BLOCK_SIZE,
3996 /* Add AEAD Algorithms */
3998 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_GCM,
3999 .is_registered = 0,
4000 .alg.aead = {
4001 .base = {
4002 .cra_name = "gcm(aes)",
4003 .cra_driver_name = "gcm-aes-chcr",
4004 .cra_blocksize = 1,
4005 .cra_priority = CHCR_AEAD_PRIORITY,
4006 .cra_ctxsize = sizeof(struct chcr_context) +
4007 sizeof(struct chcr_aead_ctx) +
4008 sizeof(struct chcr_gcm_ctx),
4010 .ivsize = GCM_AES_IV_SIZE,
4011 .maxauthsize = GHASH_DIGEST_SIZE,
4012 .setkey = chcr_gcm_setkey,
4013 .setauthsize = chcr_gcm_setauthsize,
4017 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106,
4018 .is_registered = 0,
4019 .alg.aead = {
4020 .base = {
4021 .cra_name = "rfc4106(gcm(aes))",
4022 .cra_driver_name = "rfc4106-gcm-aes-chcr",
4023 .cra_blocksize = 1,
4024 .cra_priority = CHCR_AEAD_PRIORITY + 1,
4025 .cra_ctxsize = sizeof(struct chcr_context) +
4026 sizeof(struct chcr_aead_ctx) +
4027 sizeof(struct chcr_gcm_ctx),
4030 .ivsize = GCM_RFC4106_IV_SIZE,
4031 .maxauthsize = GHASH_DIGEST_SIZE,
4032 .setkey = chcr_gcm_setkey,
4033 .setauthsize = chcr_4106_4309_setauthsize,
4037 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_CCM,
4038 .is_registered = 0,
4039 .alg.aead = {
4040 .base = {
4041 .cra_name = "ccm(aes)",
4042 .cra_driver_name = "ccm-aes-chcr",
4043 .cra_blocksize = 1,
4044 .cra_priority = CHCR_AEAD_PRIORITY,
4045 .cra_ctxsize = sizeof(struct chcr_context) +
4046 sizeof(struct chcr_aead_ctx),
4049 .ivsize = AES_BLOCK_SIZE,
4050 .maxauthsize = GHASH_DIGEST_SIZE,
4051 .setkey = chcr_aead_ccm_setkey,
4052 .setauthsize = chcr_ccm_setauthsize,
4056 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309,
4057 .is_registered = 0,
4058 .alg.aead = {
4059 .base = {
4060 .cra_name = "rfc4309(ccm(aes))",
4061 .cra_driver_name = "rfc4309-ccm-aes-chcr",
4062 .cra_blocksize = 1,
4063 .cra_priority = CHCR_AEAD_PRIORITY + 1,
4064 .cra_ctxsize = sizeof(struct chcr_context) +
4065 sizeof(struct chcr_aead_ctx),
4068 .ivsize = 8,
4069 .maxauthsize = GHASH_DIGEST_SIZE,
4070 .setkey = chcr_aead_rfc4309_setkey,
4071 .setauthsize = chcr_4106_4309_setauthsize,
4075 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4076 .is_registered = 0,
4077 .alg.aead = {
4078 .base = {
4079 .cra_name = "authenc(hmac(sha1),cbc(aes))",
4080 .cra_driver_name =
4081 "authenc-hmac-sha1-cbc-aes-chcr",
4082 .cra_blocksize = AES_BLOCK_SIZE,
4083 .cra_priority = CHCR_AEAD_PRIORITY,
4084 .cra_ctxsize = sizeof(struct chcr_context) +
4085 sizeof(struct chcr_aead_ctx) +
4086 sizeof(struct chcr_authenc_ctx),
4089 .ivsize = AES_BLOCK_SIZE,
4090 .maxauthsize = SHA1_DIGEST_SIZE,
4091 .setkey = chcr_authenc_setkey,
4092 .setauthsize = chcr_authenc_setauthsize,
4096 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4097 .is_registered = 0,
4098 .alg.aead = {
4099 .base = {
4101 .cra_name = "authenc(hmac(sha256),cbc(aes))",
4102 .cra_driver_name =
4103 "authenc-hmac-sha256-cbc-aes-chcr",
4104 .cra_blocksize = AES_BLOCK_SIZE,
4105 .cra_priority = CHCR_AEAD_PRIORITY,
4106 .cra_ctxsize = sizeof(struct chcr_context) +
4107 sizeof(struct chcr_aead_ctx) +
4108 sizeof(struct chcr_authenc_ctx),
4111 .ivsize = AES_BLOCK_SIZE,
4112 .maxauthsize = SHA256_DIGEST_SIZE,
4113 .setkey = chcr_authenc_setkey,
4114 .setauthsize = chcr_authenc_setauthsize,
4118 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4119 .is_registered = 0,
4120 .alg.aead = {
4121 .base = {
4122 .cra_name = "authenc(hmac(sha224),cbc(aes))",
4123 .cra_driver_name =
4124 "authenc-hmac-sha224-cbc-aes-chcr",
4125 .cra_blocksize = AES_BLOCK_SIZE,
4126 .cra_priority = CHCR_AEAD_PRIORITY,
4127 .cra_ctxsize = sizeof(struct chcr_context) +
4128 sizeof(struct chcr_aead_ctx) +
4129 sizeof(struct chcr_authenc_ctx),
4131 .ivsize = AES_BLOCK_SIZE,
4132 .maxauthsize = SHA224_DIGEST_SIZE,
4133 .setkey = chcr_authenc_setkey,
4134 .setauthsize = chcr_authenc_setauthsize,
4138 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4139 .is_registered = 0,
4140 .alg.aead = {
4141 .base = {
4142 .cra_name = "authenc(hmac(sha384),cbc(aes))",
4143 .cra_driver_name =
4144 "authenc-hmac-sha384-cbc-aes-chcr",
4145 .cra_blocksize = AES_BLOCK_SIZE,
4146 .cra_priority = CHCR_AEAD_PRIORITY,
4147 .cra_ctxsize = sizeof(struct chcr_context) +
4148 sizeof(struct chcr_aead_ctx) +
4149 sizeof(struct chcr_authenc_ctx),
4152 .ivsize = AES_BLOCK_SIZE,
4153 .maxauthsize = SHA384_DIGEST_SIZE,
4154 .setkey = chcr_authenc_setkey,
4155 .setauthsize = chcr_authenc_setauthsize,
4159 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4160 .is_registered = 0,
4161 .alg.aead = {
4162 .base = {
4163 .cra_name = "authenc(hmac(sha512),cbc(aes))",
4164 .cra_driver_name =
4165 "authenc-hmac-sha512-cbc-aes-chcr",
4166 .cra_blocksize = AES_BLOCK_SIZE,
4167 .cra_priority = CHCR_AEAD_PRIORITY,
4168 .cra_ctxsize = sizeof(struct chcr_context) +
4169 sizeof(struct chcr_aead_ctx) +
4170 sizeof(struct chcr_authenc_ctx),
4173 .ivsize = AES_BLOCK_SIZE,
4174 .maxauthsize = SHA512_DIGEST_SIZE,
4175 .setkey = chcr_authenc_setkey,
4176 .setauthsize = chcr_authenc_setauthsize,
4180 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_NULL,
4181 .is_registered = 0,
4182 .alg.aead = {
4183 .base = {
4184 .cra_name = "authenc(digest_null,cbc(aes))",
4185 .cra_driver_name =
4186 "authenc-digest_null-cbc-aes-chcr",
4187 .cra_blocksize = AES_BLOCK_SIZE,
4188 .cra_priority = CHCR_AEAD_PRIORITY,
4189 .cra_ctxsize = sizeof(struct chcr_context) +
4190 sizeof(struct chcr_aead_ctx) +
4191 sizeof(struct chcr_authenc_ctx),
4194 .ivsize = AES_BLOCK_SIZE,
4195 .maxauthsize = 0,
4196 .setkey = chcr_aead_digest_null_setkey,
4197 .setauthsize = chcr_authenc_null_setauthsize,
4201 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4202 .is_registered = 0,
4203 .alg.aead = {
4204 .base = {
4205 .cra_name = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4206 .cra_driver_name =
4207 "authenc-hmac-sha1-rfc3686-ctr-aes-chcr",
4208 .cra_blocksize = 1,
4209 .cra_priority = CHCR_AEAD_PRIORITY,
4210 .cra_ctxsize = sizeof(struct chcr_context) +
4211 sizeof(struct chcr_aead_ctx) +
4212 sizeof(struct chcr_authenc_ctx),
4215 .ivsize = CTR_RFC3686_IV_SIZE,
4216 .maxauthsize = SHA1_DIGEST_SIZE,
4217 .setkey = chcr_authenc_setkey,
4218 .setauthsize = chcr_authenc_setauthsize,
4222 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4223 .is_registered = 0,
4224 .alg.aead = {
4225 .base = {
4227 .cra_name = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4228 .cra_driver_name =
4229 "authenc-hmac-sha256-rfc3686-ctr-aes-chcr",
4230 .cra_blocksize = 1,
4231 .cra_priority = CHCR_AEAD_PRIORITY,
4232 .cra_ctxsize = sizeof(struct chcr_context) +
4233 sizeof(struct chcr_aead_ctx) +
4234 sizeof(struct chcr_authenc_ctx),
4237 .ivsize = CTR_RFC3686_IV_SIZE,
4238 .maxauthsize = SHA256_DIGEST_SIZE,
4239 .setkey = chcr_authenc_setkey,
4240 .setauthsize = chcr_authenc_setauthsize,
4244 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4245 .is_registered = 0,
4246 .alg.aead = {
4247 .base = {
4248 .cra_name = "authenc(hmac(sha224),rfc3686(ctr(aes)))",
4249 .cra_driver_name =
4250 "authenc-hmac-sha224-rfc3686-ctr-aes-chcr",
4251 .cra_blocksize = 1,
4252 .cra_priority = CHCR_AEAD_PRIORITY,
4253 .cra_ctxsize = sizeof(struct chcr_context) +
4254 sizeof(struct chcr_aead_ctx) +
4255 sizeof(struct chcr_authenc_ctx),
4257 .ivsize = CTR_RFC3686_IV_SIZE,
4258 .maxauthsize = SHA224_DIGEST_SIZE,
4259 .setkey = chcr_authenc_setkey,
4260 .setauthsize = chcr_authenc_setauthsize,
4264 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4265 .is_registered = 0,
4266 .alg.aead = {
4267 .base = {
4268 .cra_name = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4269 .cra_driver_name =
4270 "authenc-hmac-sha384-rfc3686-ctr-aes-chcr",
4271 .cra_blocksize = 1,
4272 .cra_priority = CHCR_AEAD_PRIORITY,
4273 .cra_ctxsize = sizeof(struct chcr_context) +
4274 sizeof(struct chcr_aead_ctx) +
4275 sizeof(struct chcr_authenc_ctx),
4278 .ivsize = CTR_RFC3686_IV_SIZE,
4279 .maxauthsize = SHA384_DIGEST_SIZE,
4280 .setkey = chcr_authenc_setkey,
4281 .setauthsize = chcr_authenc_setauthsize,
4285 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4286 .is_registered = 0,
4287 .alg.aead = {
4288 .base = {
4289 .cra_name = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4290 .cra_driver_name =
4291 "authenc-hmac-sha512-rfc3686-ctr-aes-chcr",
4292 .cra_blocksize = 1,
4293 .cra_priority = CHCR_AEAD_PRIORITY,
4294 .cra_ctxsize = sizeof(struct chcr_context) +
4295 sizeof(struct chcr_aead_ctx) +
4296 sizeof(struct chcr_authenc_ctx),
4299 .ivsize = CTR_RFC3686_IV_SIZE,
4300 .maxauthsize = SHA512_DIGEST_SIZE,
4301 .setkey = chcr_authenc_setkey,
4302 .setauthsize = chcr_authenc_setauthsize,
4306 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_NULL,
4307 .is_registered = 0,
4308 .alg.aead = {
4309 .base = {
4310 .cra_name = "authenc(digest_null,rfc3686(ctr(aes)))",
4311 .cra_driver_name =
4312 "authenc-digest_null-rfc3686-ctr-aes-chcr",
4313 .cra_blocksize = 1,
4314 .cra_priority = CHCR_AEAD_PRIORITY,
4315 .cra_ctxsize = sizeof(struct chcr_context) +
4316 sizeof(struct chcr_aead_ctx) +
4317 sizeof(struct chcr_authenc_ctx),
4320 .ivsize = CTR_RFC3686_IV_SIZE,
4321 .maxauthsize = 0,
4322 .setkey = chcr_aead_digest_null_setkey,
4323 .setauthsize = chcr_authenc_null_setauthsize,
4329 * chcr_unregister_alg - Deregister crypto algorithms with
4330 * kernel framework.
4332 static int chcr_unregister_alg(void)
4334 int i;
4336 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4337 switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
4338 case CRYPTO_ALG_TYPE_SKCIPHER:
4339 if (driver_algs[i].is_registered)
4340 crypto_unregister_skcipher(
4341 &driver_algs[i].alg.skcipher);
4342 break;
4343 case CRYPTO_ALG_TYPE_AEAD:
4344 if (driver_algs[i].is_registered)
4345 crypto_unregister_aead(
4346 &driver_algs[i].alg.aead);
4347 break;
4348 case CRYPTO_ALG_TYPE_AHASH:
4349 if (driver_algs[i].is_registered)
4350 crypto_unregister_ahash(
4351 &driver_algs[i].alg.hash);
4352 break;
4354 driver_algs[i].is_registered = 0;
4356 return 0;
4359 #define SZ_AHASH_CTX sizeof(struct chcr_context)
4360 #define SZ_AHASH_H_CTX (sizeof(struct chcr_context) + sizeof(struct hmac_ctx))
4361 #define SZ_AHASH_REQ_CTX sizeof(struct chcr_ahash_req_ctx)
4364 * chcr_register_alg - Register crypto algorithms with kernel framework.
4366 static int chcr_register_alg(void)
4368 struct crypto_alg ai;
4369 struct ahash_alg *a_hash;
4370 int err = 0, i;
4371 char *name = NULL;
4373 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4374 if (driver_algs[i].is_registered)
4375 continue;
4376 switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
4377 case CRYPTO_ALG_TYPE_SKCIPHER:
4378 driver_algs[i].alg.skcipher.base.cra_priority =
4379 CHCR_CRA_PRIORITY;
4380 driver_algs[i].alg.skcipher.base.cra_module = THIS_MODULE;
4381 driver_algs[i].alg.skcipher.base.cra_flags =
4382 CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_ASYNC |
4383 CRYPTO_ALG_NEED_FALLBACK;
4384 driver_algs[i].alg.skcipher.base.cra_ctxsize =
4385 sizeof(struct chcr_context) +
4386 sizeof(struct ablk_ctx);
4387 driver_algs[i].alg.skcipher.base.cra_alignmask = 0;
4389 err = crypto_register_skcipher(&driver_algs[i].alg.skcipher);
4390 name = driver_algs[i].alg.skcipher.base.cra_driver_name;
4391 break;
4392 case CRYPTO_ALG_TYPE_AEAD:
4393 driver_algs[i].alg.aead.base.cra_flags =
4394 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK;
4395 driver_algs[i].alg.aead.encrypt = chcr_aead_encrypt;
4396 driver_algs[i].alg.aead.decrypt = chcr_aead_decrypt;
4397 driver_algs[i].alg.aead.init = chcr_aead_cra_init;
4398 driver_algs[i].alg.aead.exit = chcr_aead_cra_exit;
4399 driver_algs[i].alg.aead.base.cra_module = THIS_MODULE;
4400 err = crypto_register_aead(&driver_algs[i].alg.aead);
4401 name = driver_algs[i].alg.aead.base.cra_driver_name;
4402 break;
4403 case CRYPTO_ALG_TYPE_AHASH:
4404 a_hash = &driver_algs[i].alg.hash;
4405 a_hash->update = chcr_ahash_update;
4406 a_hash->final = chcr_ahash_final;
4407 a_hash->finup = chcr_ahash_finup;
4408 a_hash->digest = chcr_ahash_digest;
4409 a_hash->export = chcr_ahash_export;
4410 a_hash->import = chcr_ahash_import;
4411 a_hash->halg.statesize = SZ_AHASH_REQ_CTX;
4412 a_hash->halg.base.cra_priority = CHCR_CRA_PRIORITY;
4413 a_hash->halg.base.cra_module = THIS_MODULE;
4414 a_hash->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
4415 a_hash->halg.base.cra_alignmask = 0;
4416 a_hash->halg.base.cra_exit = NULL;
4418 if (driver_algs[i].type == CRYPTO_ALG_TYPE_HMAC) {
4419 a_hash->halg.base.cra_init = chcr_hmac_cra_init;
4420 a_hash->halg.base.cra_exit = chcr_hmac_cra_exit;
4421 a_hash->init = chcr_hmac_init;
4422 a_hash->setkey = chcr_ahash_setkey;
4423 a_hash->halg.base.cra_ctxsize = SZ_AHASH_H_CTX;
4424 } else {
4425 a_hash->init = chcr_sha_init;
4426 a_hash->halg.base.cra_ctxsize = SZ_AHASH_CTX;
4427 a_hash->halg.base.cra_init = chcr_sha_cra_init;
4429 err = crypto_register_ahash(&driver_algs[i].alg.hash);
4430 ai = driver_algs[i].alg.hash.halg.base;
4431 name = ai.cra_driver_name;
4432 break;
4434 if (err) {
4435 pr_err("chcr : %s : Algorithm registration failed\n",
4436 name);
4437 goto register_err;
4438 } else {
4439 driver_algs[i].is_registered = 1;
4442 return 0;
4444 register_err:
4445 chcr_unregister_alg();
4446 return err;
4450 * start_crypto - Register the crypto algorithms.
4451 * This should called once when the first device comesup. After this
4452 * kernel will start calling driver APIs for crypto operations.
4454 int start_crypto(void)
4456 return chcr_register_alg();
4460 * stop_crypto - Deregister all the crypto algorithms with kernel.
4461 * This should be called once when the last device goes down. After this
4462 * kernel will not call the driver API for crypto operations.
4464 int stop_crypto(void)
4466 chcr_unregister_alg();
4467 return 0;