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
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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
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>
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,
145 unsigned int skip_len = 0;
148 if (sg_dma_len(sg) <= skip) {
149 skip -= sg_dma_len(sg);
158 while (sg && reqlen) {
159 less = min(reqlen, sg_dma_len(sg) - skip_len);
160 nents += DIV_ROUND_UP(less, entlen);
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;
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);
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);
201 static int chcr_inc_wrcount(struct chcr_dev *dev)
203 if (dev->state == CHCR_DETACH)
205 atomic_inc(&dev->inflight);
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,
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);
233 static void get_aes_decrypt_key(unsigned char *dec_key,
234 const unsigned char *key,
235 unsigned int keylength)
243 case AES_KEYLENGTH_128BIT:
244 nk = KEYLENGTH_4BYTES;
245 nr = NUMBER_OF_ROUNDS_10;
247 case AES_KEYLENGTH_192BIT:
248 nk = KEYLENGTH_6BYTES;
249 nr = NUMBER_OF_ROUNDS_12;
251 case AES_KEYLENGTH_256BIT:
252 nk = KEYLENGTH_8BYTES;
253 nr = NUMBER_OF_ROUNDS_14;
258 for (i = 0; i < nk; i++)
259 w_ring[i] = be32_to_cpu(*(u32 *)&key[4 * i]);
262 temp = w_ring[nk - 1];
263 while (i + nk < (nr + 1) * 4) {
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];
277 for (k = 0, j = i % nk; k < nk; k++) {
278 *((u32 *)dec_key + k) = htonl(w_ring[j]);
285 static struct crypto_shash *chcr_alloc_shash(unsigned int ds)
287 struct crypto_shash *base_hash = ERR_PTR(-EINVAL);
290 case SHA1_DIGEST_SIZE:
291 base_hash = crypto_alloc_shash("sha1", 0, 0);
293 case SHA224_DIGEST_SIZE:
294 base_hash = crypto_alloc_shash("sha224", 0, 0);
296 case SHA256_DIGEST_SIZE:
297 base_hash = crypto_alloc_shash("sha256", 0, 0);
299 case SHA384_DIGEST_SIZE:
300 base_hash = crypto_alloc_shash("sha384", 0, 0);
302 case SHA512_DIGEST_SIZE:
303 base_hash = crypto_alloc_shash("sha512", 0, 0);
310 static int chcr_compute_partial_hash(struct shash_desc *desc,
311 char *iopad, char *result_hash,
314 struct sha1_state sha1_st;
315 struct sha256_state sha256_st;
316 struct sha512_state sha512_st;
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);
349 pr_err("Unknown digest size %d\n", digest_size);
354 static void chcr_change_order(char *buf, int ds)
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));
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,
375 if (chcr_crypto_alg->type == CRYPTO_ALG_TYPE_HMAC)
380 static inline void dsgl_walk_init(struct dsgl_walk *walk,
381 struct cpl_rx_phys_dsgl *dsgl)
385 walk->to = (struct phys_sge_pairs *)(dsgl + 1);
388 static inline void dsgl_walk_end(struct dsgl_walk *walk, unsigned short qid,
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,
419 walk->to->len[j % 8] = htons(size);
420 walk->to->addr[j % 8] = cpu_to_be64(addr);
427 static void dsgl_walk_add_sg(struct dsgl_walk *walk,
428 struct scatterlist *sg,
433 unsigned int left_size = slen, len = 0;
434 unsigned int j = walk->nents;
440 if (sg_dma_len(sg) <= skip) {
441 skip -= sg_dma_len(sg);
450 while (left_size && sg) {
451 len = min_t(u32, left_size, sg_dma_len(sg) - skip_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) +
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);
474 static inline void ulptx_walk_init(struct ulptx_walk *walk,
475 struct ulptx_sgl *ulp)
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,
499 if (walk->nents == 0) {
500 walk->sgl->len0 = cpu_to_be32(size);
501 walk->sgl->addr0 = cpu_to_be64(addr);
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;
512 static void ulptx_walk_add_sg(struct ulptx_walk *walk,
513 struct scatterlist *sg,
524 if (sg_dma_len(sg) <= skip) {
525 skip -= sg_dma_len(sg);
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);
542 walk->last_sg_len = sgmin + skip_len;
544 if (sg_dma_len(sg) == skip_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;
563 walk->last_sg_len = skip_len;
564 if (sg_dma_len(sg) == skip_len) {
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;
589 txq = &txq_info->uldtxq[idx];
590 spin_lock(&txq->sendq.lock);
593 spin_unlock(&txq->sendq.lock);
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);
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);
613 static int chcr_hash_ent_in_wr(struct scatterlist *src,
616 unsigned int srcskip)
620 int soffset = 0, sless;
622 if (sg_dma_len(src) == srcskip) {
626 while (src && space > (sgl_ent_len[srcsg + 1])) {
627 sless = min_t(unsigned int, sg_dma_len(src) - soffset - srcskip,
632 if (sg_dma_len(src) == (soffset + srcskip)) {
641 static int chcr_sg_ent_in_wr(struct scatterlist *src,
642 struct scatterlist *dst,
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) {
656 if (sg_dma_len(dst) == dstskip) {
662 space > (sgl_ent_len[srcsg + 1] + dsgl_ent_len[dstsg])) {
663 sless = min_t(unsigned int, sg_dma_len(src) - srcskip - soffset,
668 while (dst && ((dstsg + 1) <= MAX_DSGL_ENT) &&
669 space > (sgl_ent_len[srcsg] + dsgl_ent_len[dstsg + 1])) {
670 if (srclen <= dstlen)
672 less = min_t(unsigned int, sg_dma_len(dst) - offset -
673 dstskip, CHCR_DST_SG_SIZE);
676 if ((offset + dstskip) == sg_dma_len(dst)) {
684 if ((soffset + srcskip) == sg_dma_len(src)) {
691 return min(srclen, dstlen);
694 static int chcr_cipher_fallback(struct crypto_sync_skcipher *cipher,
696 struct scatterlist *src,
697 struct scatterlist *dst,
700 unsigned short op_type)
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,
711 err = op_type ? crypto_skcipher_decrypt(subreq) :
712 crypto_skcipher_encrypt(subreq);
713 skcipher_request_zero(subreq);
718 static inline void create_wreq(struct chcr_context *ctx,
719 struct chcr_wr *chcr_req,
720 struct crypto_async_request *req,
727 struct uld_ctx *u_ctx = ULD_CTX(ctx);
728 int qid = u_ctx->lldi.rxq_ids[ctx->rx_qidx];
731 chcr_req->wreq.op_to_cctx_size = FILL_WR_OP_CCTX_SIZE;
732 chcr_req->wreq.pld_size_hash_size =
733 htonl(FW_CRYPTO_LOOKASIDE_WR_HASH_SIZE_V(hash_sz));
734 chcr_req->wreq.len16_pkd =
735 htonl(FW_CRYPTO_LOOKASIDE_WR_LEN16_V(DIV_ROUND_UP(len16, 16)));
736 chcr_req->wreq.cookie = cpu_to_be64((uintptr_t)req);
737 chcr_req->wreq.rx_chid_to_rx_q_id =
738 FILL_WR_RX_Q_ID(ctx->tx_chan_id, qid,
739 !!lcb, ctx->tx_qidx);
741 chcr_req->ulptx.cmd_dest = FILL_ULPTX_CMD_DEST(ctx->tx_chan_id,
743 chcr_req->ulptx.len = htonl((DIV_ROUND_UP(len16, 16) -
744 ((sizeof(chcr_req->wreq)) >> 4)));
746 chcr_req->sc_imm.cmd_more = FILL_CMD_MORE(!imm);
747 chcr_req->sc_imm.len = cpu_to_be32(sizeof(struct cpl_tx_sec_pdu) +
748 sizeof(chcr_req->key_ctx) + sc_len);
752 * create_cipher_wr - form the WR for cipher operations
754 * @ctx: crypto driver context of the request.
755 * @qid: ingress qid where response of this WR should be received.
756 * @op_type: encryption or decryption
758 static struct sk_buff *create_cipher_wr(struct cipher_wr_param *wrparam)
760 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(wrparam->req);
761 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
762 struct sk_buff *skb = NULL;
763 struct chcr_wr *chcr_req;
764 struct cpl_rx_phys_dsgl *phys_cpl;
765 struct ulptx_sgl *ulptx;
766 struct chcr_skcipher_req_ctx *reqctx =
767 skcipher_request_ctx(wrparam->req);
768 unsigned int temp = 0, transhdr_len, dst_size;
771 unsigned int kctx_len;
772 gfp_t flags = wrparam->req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
773 GFP_KERNEL : GFP_ATOMIC;
774 struct adapter *adap = padap(c_ctx(tfm)->dev);
776 nents = sg_nents_xlen(reqctx->dstsg, wrparam->bytes, CHCR_DST_SG_SIZE,
778 dst_size = get_space_for_phys_dsgl(nents);
779 kctx_len = roundup(ablkctx->enckey_len, 16);
780 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
781 nents = sg_nents_xlen(reqctx->srcsg, wrparam->bytes,
782 CHCR_SRC_SG_SIZE, reqctx->src_ofst);
783 temp = reqctx->imm ? roundup(wrparam->bytes, 16) :
784 (sgl_len(nents) * 8);
785 transhdr_len += temp;
786 transhdr_len = roundup(transhdr_len, 16);
787 skb = alloc_skb(SGE_MAX_WR_LEN, flags);
792 chcr_req = __skb_put_zero(skb, transhdr_len);
793 chcr_req->sec_cpl.op_ivinsrtofst =
794 FILL_SEC_CPL_OP_IVINSR(c_ctx(tfm)->tx_chan_id, 2, 1);
796 chcr_req->sec_cpl.pldlen = htonl(IV + wrparam->bytes);
797 chcr_req->sec_cpl.aadstart_cipherstop_hi =
798 FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, IV + 1, 0);
800 chcr_req->sec_cpl.cipherstop_lo_authinsert =
801 FILL_SEC_CPL_AUTHINSERT(0, 0, 0, 0);
802 chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op, 0,
805 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 0,
808 chcr_req->key_ctx.ctx_hdr = ablkctx->key_ctx_hdr;
809 if ((reqctx->op == CHCR_DECRYPT_OP) &&
810 (!(get_cryptoalg_subtype(tfm) ==
811 CRYPTO_ALG_SUB_TYPE_CTR)) &&
812 (!(get_cryptoalg_subtype(tfm) ==
813 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686))) {
814 generate_copy_rrkey(ablkctx, &chcr_req->key_ctx);
816 if ((ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) ||
817 (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CTR)) {
818 memcpy(chcr_req->key_ctx.key, ablkctx->key,
819 ablkctx->enckey_len);
821 memcpy(chcr_req->key_ctx.key, ablkctx->key +
822 (ablkctx->enckey_len >> 1),
823 ablkctx->enckey_len >> 1);
824 memcpy(chcr_req->key_ctx.key +
825 (ablkctx->enckey_len >> 1),
827 ablkctx->enckey_len >> 1);
830 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
831 ulptx = (struct ulptx_sgl *)((u8 *)(phys_cpl + 1) + dst_size);
832 chcr_add_cipher_src_ent(wrparam->req, ulptx, wrparam);
833 chcr_add_cipher_dst_ent(wrparam->req, phys_cpl, wrparam, wrparam->qid);
835 atomic_inc(&adap->chcr_stats.cipher_rqst);
836 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + kctx_len + IV
837 + (reqctx->imm ? (wrparam->bytes) : 0);
838 create_wreq(c_ctx(tfm), chcr_req, &(wrparam->req->base), reqctx->imm, 0,
840 ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC);
843 if (reqctx->op && (ablkctx->ciph_mode ==
844 CHCR_SCMD_CIPHER_MODE_AES_CBC))
845 sg_pcopy_to_buffer(wrparam->req->src,
846 sg_nents(wrparam->req->src), wrparam->req->iv, 16,
847 reqctx->processed + wrparam->bytes - AES_BLOCK_SIZE);
851 return ERR_PTR(error);
854 static inline int chcr_keyctx_ck_size(unsigned int keylen)
858 if (keylen == AES_KEYSIZE_128)
859 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
860 else if (keylen == AES_KEYSIZE_192)
861 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
862 else if (keylen == AES_KEYSIZE_256)
863 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
869 static int chcr_cipher_fallback_setkey(struct crypto_skcipher *cipher,
873 struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
874 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
877 crypto_sync_skcipher_clear_flags(ablkctx->sw_cipher,
878 CRYPTO_TFM_REQ_MASK);
879 crypto_sync_skcipher_set_flags(ablkctx->sw_cipher,
880 cipher->base.crt_flags & CRYPTO_TFM_REQ_MASK);
881 err = crypto_sync_skcipher_setkey(ablkctx->sw_cipher, key, keylen);
882 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
884 crypto_sync_skcipher_get_flags(ablkctx->sw_cipher) &
889 static int chcr_aes_cbc_setkey(struct crypto_skcipher *cipher,
893 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
894 unsigned int ck_size, context_size;
898 err = chcr_cipher_fallback_setkey(cipher, key, keylen);
902 ck_size = chcr_keyctx_ck_size(keylen);
903 alignment = ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192 ? 8 : 0;
904 memcpy(ablkctx->key, key, keylen);
905 ablkctx->enckey_len = keylen;
906 get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, keylen << 3);
907 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
908 keylen + alignment) >> 4;
910 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
912 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CBC;
915 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
916 ablkctx->enckey_len = 0;
921 static int chcr_aes_ctr_setkey(struct crypto_skcipher *cipher,
925 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
926 unsigned int ck_size, context_size;
930 err = chcr_cipher_fallback_setkey(cipher, key, keylen);
933 ck_size = chcr_keyctx_ck_size(keylen);
934 alignment = (ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) ? 8 : 0;
935 memcpy(ablkctx->key, key, keylen);
936 ablkctx->enckey_len = keylen;
937 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
938 keylen + alignment) >> 4;
940 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
942 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR;
946 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
947 ablkctx->enckey_len = 0;
952 static int chcr_aes_rfc3686_setkey(struct crypto_skcipher *cipher,
956 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
957 unsigned int ck_size, context_size;
961 if (keylen < CTR_RFC3686_NONCE_SIZE)
963 memcpy(ablkctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE),
964 CTR_RFC3686_NONCE_SIZE);
966 keylen -= CTR_RFC3686_NONCE_SIZE;
967 err = chcr_cipher_fallback_setkey(cipher, key, keylen);
971 ck_size = chcr_keyctx_ck_size(keylen);
972 alignment = (ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) ? 8 : 0;
973 memcpy(ablkctx->key, key, keylen);
974 ablkctx->enckey_len = keylen;
975 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
976 keylen + alignment) >> 4;
978 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
980 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR;
984 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
985 ablkctx->enckey_len = 0;
989 static void ctr_add_iv(u8 *dstiv, u8 *srciv, u32 add)
991 unsigned int size = AES_BLOCK_SIZE;
992 __be32 *b = (__be32 *)(dstiv + size);
995 memcpy(dstiv, srciv, AES_BLOCK_SIZE);
996 for (; size >= 4; size -= 4) {
997 prev = be32_to_cpu(*--b);
1007 static unsigned int adjust_ctr_overflow(u8 *iv, u32 bytes)
1009 __be32 *b = (__be32 *)(iv + AES_BLOCK_SIZE);
1011 u32 temp = be32_to_cpu(*--b);
1014 c = (u64)temp + 1; // No of block can processed withou overflow
1015 if ((bytes / AES_BLOCK_SIZE) > c)
1016 bytes = c * AES_BLOCK_SIZE;
1020 static int chcr_update_tweak(struct skcipher_request *req, u8 *iv,
1023 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1024 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
1025 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1026 struct crypto_aes_ctx aes;
1029 unsigned int keylen;
1030 int round = reqctx->last_req_len / AES_BLOCK_SIZE;
1031 int round8 = round / 8;
1033 memcpy(iv, reqctx->iv, AES_BLOCK_SIZE);
1035 keylen = ablkctx->enckey_len / 2;
1036 key = ablkctx->key + keylen;
1037 ret = aes_expandkey(&aes, key, keylen);
1040 aes_encrypt(&aes, iv, iv);
1041 for (i = 0; i < round8; i++)
1042 gf128mul_x8_ble((le128 *)iv, (le128 *)iv);
1044 for (i = 0; i < (round % 8); i++)
1045 gf128mul_x_ble((le128 *)iv, (le128 *)iv);
1048 aes_decrypt(&aes, iv, iv);
1050 memzero_explicit(&aes, sizeof(aes));
1054 static int chcr_update_cipher_iv(struct skcipher_request *req,
1055 struct cpl_fw6_pld *fw6_pld, u8 *iv)
1057 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1058 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1059 int subtype = get_cryptoalg_subtype(tfm);
1062 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR)
1063 ctr_add_iv(iv, req->iv, (reqctx->processed /
1065 else if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_RFC3686)
1066 *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE +
1067 CTR_RFC3686_IV_SIZE) = cpu_to_be32((reqctx->processed /
1068 AES_BLOCK_SIZE) + 1);
1069 else if (subtype == CRYPTO_ALG_SUB_TYPE_XTS)
1070 ret = chcr_update_tweak(req, iv, 0);
1071 else if (subtype == CRYPTO_ALG_SUB_TYPE_CBC) {
1073 /*Updated before sending last WR*/
1074 memcpy(iv, req->iv, AES_BLOCK_SIZE);
1076 memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE);
1083 /* We need separate function for final iv because in rfc3686 Initial counter
1084 * starts from 1 and buffer size of iv is 8 byte only which remains constant
1085 * for subsequent update requests
1088 static int chcr_final_cipher_iv(struct skcipher_request *req,
1089 struct cpl_fw6_pld *fw6_pld, u8 *iv)
1091 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1092 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1093 int subtype = get_cryptoalg_subtype(tfm);
1096 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR)
1097 ctr_add_iv(iv, req->iv, DIV_ROUND_UP(reqctx->processed,
1099 else if (subtype == CRYPTO_ALG_SUB_TYPE_XTS)
1100 ret = chcr_update_tweak(req, iv, 1);
1101 else if (subtype == CRYPTO_ALG_SUB_TYPE_CBC) {
1102 /*Already updated for Decrypt*/
1104 memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE);
1111 static int chcr_handle_cipher_resp(struct skcipher_request *req,
1112 unsigned char *input, int err)
1114 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1115 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
1116 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
1117 struct sk_buff *skb;
1118 struct cpl_fw6_pld *fw6_pld = (struct cpl_fw6_pld *)input;
1119 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1120 struct cipher_wr_param wrparam;
1121 struct chcr_dev *dev = c_ctx(tfm)->dev;
1126 if (req->cryptlen == reqctx->processed) {
1127 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
1129 err = chcr_final_cipher_iv(req, fw6_pld, req->iv);
1134 bytes = chcr_sg_ent_in_wr(reqctx->srcsg, reqctx->dstsg, 0,
1135 CIP_SPACE_LEFT(ablkctx->enckey_len),
1136 reqctx->src_ofst, reqctx->dst_ofst);
1137 if ((bytes + reqctx->processed) >= req->cryptlen)
1138 bytes = req->cryptlen - reqctx->processed;
1140 bytes = rounddown(bytes, 16);
1142 /*CTR mode counter overfloa*/
1143 bytes = req->cryptlen - reqctx->processed;
1145 err = chcr_update_cipher_iv(req, fw6_pld, reqctx->iv);
1149 if (unlikely(bytes == 0)) {
1150 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
1152 err = chcr_cipher_fallback(ablkctx->sw_cipher,
1162 if (get_cryptoalg_subtype(tfm) ==
1163 CRYPTO_ALG_SUB_TYPE_CTR)
1164 bytes = adjust_ctr_overflow(reqctx->iv, bytes);
1165 wrparam.qid = u_ctx->lldi.rxq_ids[c_ctx(tfm)->rx_qidx];
1167 wrparam.bytes = bytes;
1168 skb = create_cipher_wr(&wrparam);
1170 pr_err("chcr : %s : Failed to form WR. No memory\n", __func__);
1174 skb->dev = u_ctx->lldi.ports[0];
1175 set_wr_txq(skb, CPL_PRIORITY_DATA, c_ctx(tfm)->tx_qidx);
1177 reqctx->last_req_len = bytes;
1178 reqctx->processed += bytes;
1181 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
1183 chcr_dec_wrcount(dev);
1184 req->base.complete(&req->base, err);
1188 static int process_cipher(struct skcipher_request *req,
1190 struct sk_buff **skb,
1191 unsigned short op_type)
1193 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1194 unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1195 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1196 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
1197 struct cipher_wr_param wrparam;
1198 int bytes, err = -EINVAL;
1200 reqctx->processed = 0;
1203 if ((ablkctx->enckey_len == 0) || (ivsize > AES_BLOCK_SIZE) ||
1204 (req->cryptlen == 0) ||
1205 (req->cryptlen % crypto_skcipher_blocksize(tfm))) {
1206 pr_err("AES: Invalid value of Key Len %d nbytes %d IV Len %d\n",
1207 ablkctx->enckey_len, req->cryptlen, ivsize);
1211 err = chcr_cipher_dma_map(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
1214 if (req->cryptlen < (SGE_MAX_WR_LEN - (sizeof(struct chcr_wr) +
1216 sizeof(struct cpl_rx_phys_dsgl) +
1219 /* Can be sent as Imm*/
1220 unsigned int dnents = 0, transhdr_len, phys_dsgl, kctx_len;
1222 dnents = sg_nents_xlen(req->dst, req->cryptlen,
1223 CHCR_DST_SG_SIZE, 0);
1224 phys_dsgl = get_space_for_phys_dsgl(dnents);
1225 kctx_len = roundup(ablkctx->enckey_len, 16);
1226 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, phys_dsgl);
1227 reqctx->imm = (transhdr_len + IV + req->cryptlen) <=
1229 bytes = IV + req->cryptlen;
1236 bytes = chcr_sg_ent_in_wr(req->src, req->dst, 0,
1237 CIP_SPACE_LEFT(ablkctx->enckey_len),
1239 if ((bytes + reqctx->processed) >= req->cryptlen)
1240 bytes = req->cryptlen - reqctx->processed;
1242 bytes = rounddown(bytes, 16);
1244 bytes = req->cryptlen;
1246 if (get_cryptoalg_subtype(tfm) ==
1247 CRYPTO_ALG_SUB_TYPE_CTR) {
1248 bytes = adjust_ctr_overflow(req->iv, bytes);
1250 if (get_cryptoalg_subtype(tfm) ==
1251 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686) {
1252 memcpy(reqctx->iv, ablkctx->nonce, CTR_RFC3686_NONCE_SIZE);
1253 memcpy(reqctx->iv + CTR_RFC3686_NONCE_SIZE, req->iv,
1254 CTR_RFC3686_IV_SIZE);
1256 /* initialize counter portion of counter block */
1257 *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE +
1258 CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
1262 memcpy(reqctx->iv, req->iv, IV);
1264 if (unlikely(bytes == 0)) {
1265 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
1267 err = chcr_cipher_fallback(ablkctx->sw_cipher,
1276 reqctx->op = op_type;
1277 reqctx->srcsg = req->src;
1278 reqctx->dstsg = req->dst;
1279 reqctx->src_ofst = 0;
1280 reqctx->dst_ofst = 0;
1283 wrparam.bytes = bytes;
1284 *skb = create_cipher_wr(&wrparam);
1286 err = PTR_ERR(*skb);
1289 reqctx->processed = bytes;
1290 reqctx->last_req_len = bytes;
1294 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
1299 static int chcr_aes_encrypt(struct skcipher_request *req)
1301 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1302 struct chcr_dev *dev = c_ctx(tfm)->dev;
1303 struct sk_buff *skb = NULL;
1304 int err, isfull = 0;
1305 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
1307 err = chcr_inc_wrcount(dev);
1310 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1311 c_ctx(tfm)->tx_qidx))) {
1313 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1319 err = process_cipher(req, u_ctx->lldi.rxq_ids[c_ctx(tfm)->rx_qidx],
1320 &skb, CHCR_ENCRYPT_OP);
1323 skb->dev = u_ctx->lldi.ports[0];
1324 set_wr_txq(skb, CPL_PRIORITY_DATA, c_ctx(tfm)->tx_qidx);
1326 return isfull ? -EBUSY : -EINPROGRESS;
1328 chcr_dec_wrcount(dev);
1332 static int chcr_aes_decrypt(struct skcipher_request *req)
1334 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1335 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
1336 struct chcr_dev *dev = c_ctx(tfm)->dev;
1337 struct sk_buff *skb = NULL;
1338 int err, isfull = 0;
1340 err = chcr_inc_wrcount(dev);
1344 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1345 c_ctx(tfm)->tx_qidx))) {
1347 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
1351 err = process_cipher(req, u_ctx->lldi.rxq_ids[c_ctx(tfm)->rx_qidx],
1352 &skb, CHCR_DECRYPT_OP);
1355 skb->dev = u_ctx->lldi.ports[0];
1356 set_wr_txq(skb, CPL_PRIORITY_DATA, c_ctx(tfm)->tx_qidx);
1358 return isfull ? -EBUSY : -EINPROGRESS;
1361 static int chcr_device_init(struct chcr_context *ctx)
1363 struct uld_ctx *u_ctx = NULL;
1365 int txq_perchan, txq_idx, ntxq;
1366 int err = 0, rxq_perchan, rxq_idx;
1368 id = smp_processor_id();
1370 u_ctx = assign_chcr_device();
1373 pr_err("chcr device assignment fails\n");
1376 ctx->dev = &u_ctx->dev;
1377 ntxq = u_ctx->lldi.ntxq;
1378 rxq_perchan = u_ctx->lldi.nrxq / u_ctx->lldi.nchan;
1379 txq_perchan = ntxq / u_ctx->lldi.nchan;
1380 spin_lock(&ctx->dev->lock_chcr_dev);
1381 ctx->tx_chan_id = ctx->dev->tx_channel_id;
1382 ctx->dev->tx_channel_id = !ctx->dev->tx_channel_id;
1383 spin_unlock(&ctx->dev->lock_chcr_dev);
1384 rxq_idx = ctx->tx_chan_id * rxq_perchan;
1385 rxq_idx += id % rxq_perchan;
1386 txq_idx = ctx->tx_chan_id * txq_perchan;
1387 txq_idx += id % txq_perchan;
1388 ctx->rx_qidx = rxq_idx;
1389 ctx->tx_qidx = txq_idx;
1390 /* Channel Id used by SGE to forward packet to Host.
1391 * Same value should be used in cpl_fw6_pld RSS_CH field
1392 * by FW. Driver programs PCI channel ID to be used in fw
1393 * at the time of queue allocation with value "pi->tx_chan"
1395 ctx->pci_chan_id = txq_idx / txq_perchan;
1401 static int chcr_init_tfm(struct crypto_skcipher *tfm)
1403 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
1404 struct chcr_context *ctx = crypto_skcipher_ctx(tfm);
1405 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
1407 ablkctx->sw_cipher = crypto_alloc_sync_skcipher(alg->base.cra_name, 0,
1408 CRYPTO_ALG_NEED_FALLBACK);
1409 if (IS_ERR(ablkctx->sw_cipher)) {
1410 pr_err("failed to allocate fallback for %s\n", alg->base.cra_name);
1411 return PTR_ERR(ablkctx->sw_cipher);
1414 crypto_skcipher_set_reqsize(tfm, sizeof(struct chcr_skcipher_req_ctx));
1416 return chcr_device_init(ctx);
1419 static int chcr_rfc3686_init(struct crypto_skcipher *tfm)
1421 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
1422 struct chcr_context *ctx = crypto_skcipher_ctx(tfm);
1423 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
1425 /*RFC3686 initialises IV counter value to 1, rfc3686(ctr(aes))
1426 * cannot be used as fallback in chcr_handle_cipher_response
1428 ablkctx->sw_cipher = crypto_alloc_sync_skcipher("ctr(aes)", 0,
1429 CRYPTO_ALG_NEED_FALLBACK);
1430 if (IS_ERR(ablkctx->sw_cipher)) {
1431 pr_err("failed to allocate fallback for %s\n", alg->base.cra_name);
1432 return PTR_ERR(ablkctx->sw_cipher);
1434 crypto_skcipher_set_reqsize(tfm, sizeof(struct chcr_skcipher_req_ctx));
1435 return chcr_device_init(ctx);
1439 static void chcr_exit_tfm(struct crypto_skcipher *tfm)
1441 struct chcr_context *ctx = crypto_skcipher_ctx(tfm);
1442 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
1444 crypto_free_sync_skcipher(ablkctx->sw_cipher);
1447 static int get_alg_config(struct algo_param *params,
1448 unsigned int auth_size)
1450 switch (auth_size) {
1451 case SHA1_DIGEST_SIZE:
1452 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_160;
1453 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA1;
1454 params->result_size = SHA1_DIGEST_SIZE;
1456 case SHA224_DIGEST_SIZE:
1457 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
1458 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA224;
1459 params->result_size = SHA256_DIGEST_SIZE;
1461 case SHA256_DIGEST_SIZE:
1462 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
1463 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA256;
1464 params->result_size = SHA256_DIGEST_SIZE;
1466 case SHA384_DIGEST_SIZE:
1467 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
1468 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_384;
1469 params->result_size = SHA512_DIGEST_SIZE;
1471 case SHA512_DIGEST_SIZE:
1472 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
1473 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_512;
1474 params->result_size = SHA512_DIGEST_SIZE;
1477 pr_err("chcr : ERROR, unsupported digest size\n");
1483 static inline void chcr_free_shash(struct crypto_shash *base_hash)
1485 crypto_free_shash(base_hash);
1489 * create_hash_wr - Create hash work request
1490 * @req - Cipher req base
1492 static struct sk_buff *create_hash_wr(struct ahash_request *req,
1493 struct hash_wr_param *param)
1495 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1496 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1497 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(tfm));
1498 struct sk_buff *skb = NULL;
1499 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(tfm));
1500 struct chcr_wr *chcr_req;
1501 struct ulptx_sgl *ulptx;
1502 unsigned int nents = 0, transhdr_len;
1503 unsigned int temp = 0;
1504 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1506 struct adapter *adap = padap(h_ctx(tfm)->dev);
1509 transhdr_len = HASH_TRANSHDR_SIZE(param->kctx_len);
1510 req_ctx->hctx_wr.imm = (transhdr_len + param->bfr_len +
1511 param->sg_len) <= SGE_MAX_WR_LEN;
1512 nents = sg_nents_xlen(req_ctx->hctx_wr.srcsg, param->sg_len,
1513 CHCR_SRC_SG_SIZE, req_ctx->hctx_wr.src_ofst);
1514 nents += param->bfr_len ? 1 : 0;
1515 transhdr_len += req_ctx->hctx_wr.imm ? roundup(param->bfr_len +
1516 param->sg_len, 16) : (sgl_len(nents) * 8);
1517 transhdr_len = roundup(transhdr_len, 16);
1519 skb = alloc_skb(transhdr_len, flags);
1521 return ERR_PTR(-ENOMEM);
1522 chcr_req = __skb_put_zero(skb, transhdr_len);
1524 chcr_req->sec_cpl.op_ivinsrtofst =
1525 FILL_SEC_CPL_OP_IVINSR(h_ctx(tfm)->tx_chan_id, 2, 0);
1526 chcr_req->sec_cpl.pldlen = htonl(param->bfr_len + param->sg_len);
1528 chcr_req->sec_cpl.aadstart_cipherstop_hi =
1529 FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, 0, 0);
1530 chcr_req->sec_cpl.cipherstop_lo_authinsert =
1531 FILL_SEC_CPL_AUTHINSERT(0, 1, 0, 0);
1532 chcr_req->sec_cpl.seqno_numivs =
1533 FILL_SEC_CPL_SCMD0_SEQNO(0, 0, 0, param->alg_prm.auth_mode,
1534 param->opad_needed, 0);
1536 chcr_req->sec_cpl.ivgen_hdrlen =
1537 FILL_SEC_CPL_IVGEN_HDRLEN(param->last, param->more, 0, 1, 0, 0);
1539 memcpy(chcr_req->key_ctx.key, req_ctx->partial_hash,
1540 param->alg_prm.result_size);
1542 if (param->opad_needed)
1543 memcpy(chcr_req->key_ctx.key +
1544 ((param->alg_prm.result_size <= 32) ? 32 :
1545 CHCR_HASH_MAX_DIGEST_SIZE),
1546 hmacctx->opad, param->alg_prm.result_size);
1548 chcr_req->key_ctx.ctx_hdr = FILL_KEY_CTX_HDR(CHCR_KEYCTX_NO_KEY,
1549 param->alg_prm.mk_size, 0,
1552 sizeof(chcr_req->key_ctx)) >> 4));
1553 chcr_req->sec_cpl.scmd1 = cpu_to_be64((u64)param->scmd1);
1554 ulptx = (struct ulptx_sgl *)((u8 *)(chcr_req + 1) + param->kctx_len +
1556 if (param->bfr_len != 0) {
1557 req_ctx->hctx_wr.dma_addr =
1558 dma_map_single(&u_ctx->lldi.pdev->dev, req_ctx->reqbfr,
1559 param->bfr_len, DMA_TO_DEVICE);
1560 if (dma_mapping_error(&u_ctx->lldi.pdev->dev,
1561 req_ctx->hctx_wr. dma_addr)) {
1565 req_ctx->hctx_wr.dma_len = param->bfr_len;
1567 req_ctx->hctx_wr.dma_addr = 0;
1569 chcr_add_hash_src_ent(req, ulptx, param);
1570 /* Request upto max wr size */
1571 temp = param->kctx_len + DUMMY_BYTES + (req_ctx->hctx_wr.imm ?
1572 (param->sg_len + param->bfr_len) : 0);
1573 atomic_inc(&adap->chcr_stats.digest_rqst);
1574 create_wreq(h_ctx(tfm), chcr_req, &req->base, req_ctx->hctx_wr.imm,
1575 param->hash_size, transhdr_len,
1577 req_ctx->hctx_wr.skb = skb;
1581 return ERR_PTR(error);
1584 static int chcr_ahash_update(struct ahash_request *req)
1586 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1587 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1588 struct uld_ctx *u_ctx = NULL;
1589 struct chcr_dev *dev = h_ctx(rtfm)->dev;
1590 struct sk_buff *skb;
1591 u8 remainder = 0, bs;
1592 unsigned int nbytes = req->nbytes;
1593 struct hash_wr_param params;
1594 int error, isfull = 0;
1596 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1597 u_ctx = ULD_CTX(h_ctx(rtfm));
1599 if (nbytes + req_ctx->reqlen >= bs) {
1600 remainder = (nbytes + req_ctx->reqlen) % bs;
1601 nbytes = nbytes + req_ctx->reqlen - remainder;
1603 sg_pcopy_to_buffer(req->src, sg_nents(req->src), req_ctx->reqbfr
1604 + req_ctx->reqlen, nbytes, 0);
1605 req_ctx->reqlen += nbytes;
1608 error = chcr_inc_wrcount(dev);
1611 /* Detach state for CHCR means lldi or padap is freed. Increasing
1612 * inflight count for dev guarantees that lldi and padap is valid
1614 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1615 h_ctx(rtfm)->tx_qidx))) {
1617 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1623 chcr_init_hctx_per_wr(req_ctx);
1624 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
1629 get_alg_config(¶ms.alg_prm, crypto_ahash_digestsize(rtfm));
1630 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1631 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
1632 HASH_SPACE_LEFT(params.kctx_len), 0);
1633 if (params.sg_len > req->nbytes)
1634 params.sg_len = req->nbytes;
1635 params.sg_len = rounddown(params.sg_len + req_ctx->reqlen, bs) -
1637 params.opad_needed = 0;
1640 params.bfr_len = req_ctx->reqlen;
1642 req_ctx->hctx_wr.srcsg = req->src;
1644 params.hash_size = params.alg_prm.result_size;
1645 req_ctx->data_len += params.sg_len + params.bfr_len;
1646 skb = create_hash_wr(req, ¶ms);
1648 error = PTR_ERR(skb);
1652 req_ctx->hctx_wr.processed += params.sg_len;
1655 swap(req_ctx->reqbfr, req_ctx->skbfr);
1656 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
1657 req_ctx->reqbfr, remainder, req->nbytes -
1660 req_ctx->reqlen = remainder;
1661 skb->dev = u_ctx->lldi.ports[0];
1662 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
1665 return isfull ? -EBUSY : -EINPROGRESS;
1667 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
1669 chcr_dec_wrcount(dev);
1673 static void create_last_hash_block(char *bfr_ptr, unsigned int bs, u64 scmd1)
1675 memset(bfr_ptr, 0, bs);
1678 *(__be64 *)(bfr_ptr + 56) = cpu_to_be64(scmd1 << 3);
1680 *(__be64 *)(bfr_ptr + 120) = cpu_to_be64(scmd1 << 3);
1683 static int chcr_ahash_final(struct ahash_request *req)
1685 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1686 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1687 struct chcr_dev *dev = h_ctx(rtfm)->dev;
1688 struct hash_wr_param params;
1689 struct sk_buff *skb;
1690 struct uld_ctx *u_ctx = NULL;
1691 u8 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1692 int error = -EINVAL;
1694 error = chcr_inc_wrcount(dev);
1698 chcr_init_hctx_per_wr(req_ctx);
1699 u_ctx = ULD_CTX(h_ctx(rtfm));
1700 if (is_hmac(crypto_ahash_tfm(rtfm)))
1701 params.opad_needed = 1;
1703 params.opad_needed = 0;
1705 req_ctx->hctx_wr.isfinal = 1;
1706 get_alg_config(¶ms.alg_prm, crypto_ahash_digestsize(rtfm));
1707 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1708 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1709 params.opad_needed = 1;
1710 params.kctx_len *= 2;
1712 params.opad_needed = 0;
1715 req_ctx->hctx_wr.result = 1;
1716 params.bfr_len = req_ctx->reqlen;
1717 req_ctx->data_len += params.bfr_len + params.sg_len;
1718 req_ctx->hctx_wr.srcsg = req->src;
1719 if (req_ctx->reqlen == 0) {
1720 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
1724 params.bfr_len = bs;
1727 params.scmd1 = req_ctx->data_len;
1731 params.hash_size = crypto_ahash_digestsize(rtfm);
1732 skb = create_hash_wr(req, ¶ms);
1734 error = PTR_ERR(skb);
1737 req_ctx->reqlen = 0;
1738 skb->dev = u_ctx->lldi.ports[0];
1739 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
1741 return -EINPROGRESS;
1743 chcr_dec_wrcount(dev);
1747 static int chcr_ahash_finup(struct ahash_request *req)
1749 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1750 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1751 struct chcr_dev *dev = h_ctx(rtfm)->dev;
1752 struct uld_ctx *u_ctx = NULL;
1753 struct sk_buff *skb;
1754 struct hash_wr_param params;
1756 int error, isfull = 0;
1758 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1759 u_ctx = ULD_CTX(h_ctx(rtfm));
1760 error = chcr_inc_wrcount(dev);
1764 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1765 h_ctx(rtfm)->tx_qidx))) {
1767 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1772 chcr_init_hctx_per_wr(req_ctx);
1773 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
1779 get_alg_config(¶ms.alg_prm, crypto_ahash_digestsize(rtfm));
1780 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1781 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1782 params.kctx_len *= 2;
1783 params.opad_needed = 1;
1785 params.opad_needed = 0;
1788 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
1789 HASH_SPACE_LEFT(params.kctx_len), 0);
1790 if (params.sg_len < req->nbytes) {
1791 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1792 params.kctx_len /= 2;
1793 params.opad_needed = 0;
1797 params.sg_len = rounddown(params.sg_len + req_ctx->reqlen, bs)
1799 params.hash_size = params.alg_prm.result_size;
1804 params.sg_len = req->nbytes;
1805 params.hash_size = crypto_ahash_digestsize(rtfm);
1806 params.scmd1 = req_ctx->data_len + req_ctx->reqlen +
1809 params.bfr_len = req_ctx->reqlen;
1810 req_ctx->data_len += params.bfr_len + params.sg_len;
1811 req_ctx->hctx_wr.result = 1;
1812 req_ctx->hctx_wr.srcsg = req->src;
1813 if ((req_ctx->reqlen + req->nbytes) == 0) {
1814 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
1818 params.bfr_len = bs;
1820 skb = create_hash_wr(req, ¶ms);
1822 error = PTR_ERR(skb);
1825 req_ctx->reqlen = 0;
1826 req_ctx->hctx_wr.processed += params.sg_len;
1827 skb->dev = u_ctx->lldi.ports[0];
1828 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
1831 return isfull ? -EBUSY : -EINPROGRESS;
1833 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
1835 chcr_dec_wrcount(dev);
1839 static int chcr_ahash_digest(struct ahash_request *req)
1841 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1842 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1843 struct chcr_dev *dev = h_ctx(rtfm)->dev;
1844 struct uld_ctx *u_ctx = NULL;
1845 struct sk_buff *skb;
1846 struct hash_wr_param params;
1848 int error, isfull = 0;
1851 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1852 error = chcr_inc_wrcount(dev);
1856 u_ctx = ULD_CTX(h_ctx(rtfm));
1857 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1858 h_ctx(rtfm)->tx_qidx))) {
1860 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1866 chcr_init_hctx_per_wr(req_ctx);
1867 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
1873 get_alg_config(¶ms.alg_prm, crypto_ahash_digestsize(rtfm));
1874 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1875 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1876 params.kctx_len *= 2;
1877 params.opad_needed = 1;
1879 params.opad_needed = 0;
1881 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
1882 HASH_SPACE_LEFT(params.kctx_len), 0);
1883 if (params.sg_len < req->nbytes) {
1884 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1885 params.kctx_len /= 2;
1886 params.opad_needed = 0;
1891 params.sg_len = rounddown(params.sg_len, bs);
1892 params.hash_size = params.alg_prm.result_size;
1894 params.sg_len = req->nbytes;
1895 params.hash_size = crypto_ahash_digestsize(rtfm);
1898 params.scmd1 = req->nbytes + req_ctx->data_len;
1902 req_ctx->hctx_wr.result = 1;
1903 req_ctx->hctx_wr.srcsg = req->src;
1904 req_ctx->data_len += params.bfr_len + params.sg_len;
1906 if (req->nbytes == 0) {
1907 create_last_hash_block(req_ctx->reqbfr, bs, 0);
1909 params.bfr_len = bs;
1912 skb = create_hash_wr(req, ¶ms);
1914 error = PTR_ERR(skb);
1917 req_ctx->hctx_wr.processed += params.sg_len;
1918 skb->dev = u_ctx->lldi.ports[0];
1919 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
1921 return isfull ? -EBUSY : -EINPROGRESS;
1923 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
1925 chcr_dec_wrcount(dev);
1929 static int chcr_ahash_continue(struct ahash_request *req)
1931 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
1932 struct chcr_hctx_per_wr *hctx_wr = &reqctx->hctx_wr;
1933 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1934 struct uld_ctx *u_ctx = NULL;
1935 struct sk_buff *skb;
1936 struct hash_wr_param params;
1940 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1941 u_ctx = ULD_CTX(h_ctx(rtfm));
1942 get_alg_config(¶ms.alg_prm, crypto_ahash_digestsize(rtfm));
1943 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1944 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1945 params.kctx_len *= 2;
1946 params.opad_needed = 1;
1948 params.opad_needed = 0;
1950 params.sg_len = chcr_hash_ent_in_wr(hctx_wr->srcsg, 0,
1951 HASH_SPACE_LEFT(params.kctx_len),
1953 if ((params.sg_len + hctx_wr->processed) > req->nbytes)
1954 params.sg_len = req->nbytes - hctx_wr->processed;
1955 if (!hctx_wr->result ||
1956 ((params.sg_len + hctx_wr->processed) < req->nbytes)) {
1957 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1958 params.kctx_len /= 2;
1959 params.opad_needed = 0;
1963 params.sg_len = rounddown(params.sg_len, bs);
1964 params.hash_size = params.alg_prm.result_size;
1969 params.hash_size = crypto_ahash_digestsize(rtfm);
1970 params.scmd1 = reqctx->data_len + params.sg_len;
1973 reqctx->data_len += params.sg_len;
1974 skb = create_hash_wr(req, ¶ms);
1976 error = PTR_ERR(skb);
1979 hctx_wr->processed += params.sg_len;
1980 skb->dev = u_ctx->lldi.ports[0];
1981 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
1988 static inline void chcr_handle_ahash_resp(struct ahash_request *req,
1989 unsigned char *input,
1992 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
1993 struct chcr_hctx_per_wr *hctx_wr = &reqctx->hctx_wr;
1994 int digestsize, updated_digestsize;
1995 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1996 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(tfm));
1997 struct chcr_dev *dev = h_ctx(tfm)->dev;
2001 digestsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
2002 updated_digestsize = digestsize;
2003 if (digestsize == SHA224_DIGEST_SIZE)
2004 updated_digestsize = SHA256_DIGEST_SIZE;
2005 else if (digestsize == SHA384_DIGEST_SIZE)
2006 updated_digestsize = SHA512_DIGEST_SIZE;
2008 if (hctx_wr->dma_addr) {
2009 dma_unmap_single(&u_ctx->lldi.pdev->dev, hctx_wr->dma_addr,
2010 hctx_wr->dma_len, DMA_TO_DEVICE);
2011 hctx_wr->dma_addr = 0;
2013 if (hctx_wr->isfinal || ((hctx_wr->processed + reqctx->reqlen) ==
2015 if (hctx_wr->result == 1) {
2016 hctx_wr->result = 0;
2017 memcpy(req->result, input + sizeof(struct cpl_fw6_pld),
2020 memcpy(reqctx->partial_hash,
2021 input + sizeof(struct cpl_fw6_pld),
2022 updated_digestsize);
2027 memcpy(reqctx->partial_hash, input + sizeof(struct cpl_fw6_pld),
2028 updated_digestsize);
2030 err = chcr_ahash_continue(req);
2035 if (hctx_wr->is_sg_map)
2036 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
2040 chcr_dec_wrcount(dev);
2041 req->base.complete(&req->base, err);
2045 * chcr_handle_resp - Unmap the DMA buffers associated with the request
2046 * @req: crypto request
2048 int chcr_handle_resp(struct crypto_async_request *req, unsigned char *input,
2051 struct crypto_tfm *tfm = req->tfm;
2052 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2053 struct adapter *adap = padap(ctx->dev);
2055 switch (tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
2056 case CRYPTO_ALG_TYPE_AEAD:
2057 err = chcr_handle_aead_resp(aead_request_cast(req), input, err);
2060 case CRYPTO_ALG_TYPE_SKCIPHER:
2061 chcr_handle_cipher_resp(skcipher_request_cast(req),
2064 case CRYPTO_ALG_TYPE_AHASH:
2065 chcr_handle_ahash_resp(ahash_request_cast(req), input, err);
2067 atomic_inc(&adap->chcr_stats.complete);
2070 static int chcr_ahash_export(struct ahash_request *areq, void *out)
2072 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2073 struct chcr_ahash_req_ctx *state = out;
2075 state->reqlen = req_ctx->reqlen;
2076 state->data_len = req_ctx->data_len;
2077 memcpy(state->bfr1, req_ctx->reqbfr, req_ctx->reqlen);
2078 memcpy(state->partial_hash, req_ctx->partial_hash,
2079 CHCR_HASH_MAX_DIGEST_SIZE);
2080 chcr_init_hctx_per_wr(state);
2084 static int chcr_ahash_import(struct ahash_request *areq, const void *in)
2086 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2087 struct chcr_ahash_req_ctx *state = (struct chcr_ahash_req_ctx *)in;
2089 req_ctx->reqlen = state->reqlen;
2090 req_ctx->data_len = state->data_len;
2091 req_ctx->reqbfr = req_ctx->bfr1;
2092 req_ctx->skbfr = req_ctx->bfr2;
2093 memcpy(req_ctx->bfr1, state->bfr1, CHCR_HASH_MAX_BLOCK_SIZE_128);
2094 memcpy(req_ctx->partial_hash, state->partial_hash,
2095 CHCR_HASH_MAX_DIGEST_SIZE);
2096 chcr_init_hctx_per_wr(req_ctx);
2100 static int chcr_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
2101 unsigned int keylen)
2103 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(tfm));
2104 unsigned int digestsize = crypto_ahash_digestsize(tfm);
2105 unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2106 unsigned int i, err = 0, updated_digestsize;
2108 SHASH_DESC_ON_STACK(shash, hmacctx->base_hash);
2110 /* use the key to calculate the ipad and opad. ipad will sent with the
2111 * first request's data. opad will be sent with the final hash result
2112 * ipad in hmacctx->ipad and opad in hmacctx->opad location
2114 shash->tfm = hmacctx->base_hash;
2116 err = crypto_shash_digest(shash, key, keylen,
2120 keylen = digestsize;
2122 memcpy(hmacctx->ipad, key, keylen);
2124 memset(hmacctx->ipad + keylen, 0, bs - keylen);
2125 memcpy(hmacctx->opad, hmacctx->ipad, bs);
2127 for (i = 0; i < bs / sizeof(int); i++) {
2128 *((unsigned int *)(&hmacctx->ipad) + i) ^= IPAD_DATA;
2129 *((unsigned int *)(&hmacctx->opad) + i) ^= OPAD_DATA;
2132 updated_digestsize = digestsize;
2133 if (digestsize == SHA224_DIGEST_SIZE)
2134 updated_digestsize = SHA256_DIGEST_SIZE;
2135 else if (digestsize == SHA384_DIGEST_SIZE)
2136 updated_digestsize = SHA512_DIGEST_SIZE;
2137 err = chcr_compute_partial_hash(shash, hmacctx->ipad,
2138 hmacctx->ipad, digestsize);
2141 chcr_change_order(hmacctx->ipad, updated_digestsize);
2143 err = chcr_compute_partial_hash(shash, hmacctx->opad,
2144 hmacctx->opad, digestsize);
2147 chcr_change_order(hmacctx->opad, updated_digestsize);
2152 static int chcr_aes_xts_setkey(struct crypto_skcipher *cipher, const u8 *key,
2153 unsigned int key_len)
2155 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
2156 unsigned short context_size = 0;
2159 err = chcr_cipher_fallback_setkey(cipher, key, key_len);
2163 memcpy(ablkctx->key, key, key_len);
2164 ablkctx->enckey_len = key_len;
2165 get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, key_len << 2);
2166 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len) >> 4;
2167 ablkctx->key_ctx_hdr =
2168 FILL_KEY_CTX_HDR((key_len == AES_KEYSIZE_256) ?
2169 CHCR_KEYCTX_CIPHER_KEY_SIZE_128 :
2170 CHCR_KEYCTX_CIPHER_KEY_SIZE_256,
2171 CHCR_KEYCTX_NO_KEY, 1,
2173 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_XTS;
2176 crypto_skcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
2177 ablkctx->enckey_len = 0;
2182 static int chcr_sha_init(struct ahash_request *areq)
2184 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2185 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
2186 int digestsize = crypto_ahash_digestsize(tfm);
2188 req_ctx->data_len = 0;
2189 req_ctx->reqlen = 0;
2190 req_ctx->reqbfr = req_ctx->bfr1;
2191 req_ctx->skbfr = req_ctx->bfr2;
2192 copy_hash_init_values(req_ctx->partial_hash, digestsize);
2197 static int chcr_sha_cra_init(struct crypto_tfm *tfm)
2199 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2200 sizeof(struct chcr_ahash_req_ctx));
2201 return chcr_device_init(crypto_tfm_ctx(tfm));
2204 static int chcr_hmac_init(struct ahash_request *areq)
2206 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2207 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(areq);
2208 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(rtfm));
2209 unsigned int digestsize = crypto_ahash_digestsize(rtfm);
2210 unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
2212 chcr_sha_init(areq);
2213 req_ctx->data_len = bs;
2214 if (is_hmac(crypto_ahash_tfm(rtfm))) {
2215 if (digestsize == SHA224_DIGEST_SIZE)
2216 memcpy(req_ctx->partial_hash, hmacctx->ipad,
2217 SHA256_DIGEST_SIZE);
2218 else if (digestsize == SHA384_DIGEST_SIZE)
2219 memcpy(req_ctx->partial_hash, hmacctx->ipad,
2220 SHA512_DIGEST_SIZE);
2222 memcpy(req_ctx->partial_hash, hmacctx->ipad,
2228 static int chcr_hmac_cra_init(struct crypto_tfm *tfm)
2230 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2231 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
2232 unsigned int digestsize =
2233 crypto_ahash_digestsize(__crypto_ahash_cast(tfm));
2235 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2236 sizeof(struct chcr_ahash_req_ctx));
2237 hmacctx->base_hash = chcr_alloc_shash(digestsize);
2238 if (IS_ERR(hmacctx->base_hash))
2239 return PTR_ERR(hmacctx->base_hash);
2240 return chcr_device_init(crypto_tfm_ctx(tfm));
2243 static void chcr_hmac_cra_exit(struct crypto_tfm *tfm)
2245 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2246 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
2248 if (hmacctx->base_hash) {
2249 chcr_free_shash(hmacctx->base_hash);
2250 hmacctx->base_hash = NULL;
2254 inline void chcr_aead_common_exit(struct aead_request *req)
2256 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2257 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2258 struct uld_ctx *u_ctx = ULD_CTX(a_ctx(tfm));
2260 chcr_aead_dma_unmap(&u_ctx->lldi.pdev->dev, req, reqctx->op);
2263 static int chcr_aead_common_init(struct aead_request *req)
2265 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2266 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2267 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2268 unsigned int authsize = crypto_aead_authsize(tfm);
2269 int error = -EINVAL;
2271 /* validate key size */
2272 if (aeadctx->enckey_len == 0)
2274 if (reqctx->op && req->cryptlen < authsize)
2277 reqctx->scratch_pad = reqctx->iv + IV;
2279 reqctx->scratch_pad = NULL;
2281 error = chcr_aead_dma_map(&ULD_CTX(a_ctx(tfm))->lldi.pdev->dev, req,
2293 static int chcr_aead_need_fallback(struct aead_request *req, int dst_nents,
2294 int aadmax, int wrlen,
2295 unsigned short op_type)
2297 unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
2299 if (((req->cryptlen - (op_type ? authsize : 0)) == 0) ||
2300 dst_nents > MAX_DSGL_ENT ||
2301 (req->assoclen > aadmax) ||
2302 (wrlen > SGE_MAX_WR_LEN))
2307 static int chcr_aead_fallback(struct aead_request *req, unsigned short op_type)
2309 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2310 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2311 struct aead_request *subreq = aead_request_ctx(req);
2313 aead_request_set_tfm(subreq, aeadctx->sw_cipher);
2314 aead_request_set_callback(subreq, req->base.flags,
2315 req->base.complete, req->base.data);
2316 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
2318 aead_request_set_ad(subreq, req->assoclen);
2319 return op_type ? crypto_aead_decrypt(subreq) :
2320 crypto_aead_encrypt(subreq);
2323 static struct sk_buff *create_authenc_wr(struct aead_request *req,
2327 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2328 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2329 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
2330 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2331 struct sk_buff *skb = NULL;
2332 struct chcr_wr *chcr_req;
2333 struct cpl_rx_phys_dsgl *phys_cpl;
2334 struct ulptx_sgl *ulptx;
2335 unsigned int transhdr_len;
2336 unsigned int dst_size = 0, temp, subtype = get_aead_subtype(tfm);
2337 unsigned int kctx_len = 0, dnents, snents;
2338 unsigned int authsize = crypto_aead_authsize(tfm);
2339 int error = -EINVAL;
2342 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
2344 struct adapter *adap = padap(a_ctx(tfm)->dev);
2346 if (req->cryptlen == 0)
2350 error = chcr_aead_common_init(req);
2352 return ERR_PTR(error);
2354 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL ||
2355 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
2358 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen +
2359 (reqctx->op ? -authsize : authsize), CHCR_DST_SG_SIZE, 0);
2360 dnents += MIN_AUTH_SG; // For IV
2361 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
2362 CHCR_SRC_SG_SIZE, 0);
2363 dst_size = get_space_for_phys_dsgl(dnents);
2364 kctx_len = (ntohl(KEY_CONTEXT_CTX_LEN_V(aeadctx->key_ctx_hdr)) << 4)
2365 - sizeof(chcr_req->key_ctx);
2366 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
2367 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen) <
2369 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen, 16)
2370 : (sgl_len(snents) * 8);
2371 transhdr_len += temp;
2372 transhdr_len = roundup(transhdr_len, 16);
2374 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE,
2375 transhdr_len, reqctx->op)) {
2376 atomic_inc(&adap->chcr_stats.fallback);
2377 chcr_aead_common_exit(req);
2378 return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
2380 skb = alloc_skb(transhdr_len, flags);
2386 chcr_req = __skb_put_zero(skb, transhdr_len);
2388 temp = (reqctx->op == CHCR_ENCRYPT_OP) ? 0 : authsize;
2391 * Input order is AAD,IV and Payload. where IV should be included as
2392 * the part of authdata. All other fields should be filled according
2393 * to the hardware spec
2395 chcr_req->sec_cpl.op_ivinsrtofst =
2396 FILL_SEC_CPL_OP_IVINSR(a_ctx(tfm)->tx_chan_id, 2, 1);
2397 chcr_req->sec_cpl.pldlen = htonl(req->assoclen + IV + req->cryptlen);
2398 chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
2400 null ? 0 : IV + req->assoclen,
2401 req->assoclen + IV + 1,
2402 (temp & 0x1F0) >> 4);
2403 chcr_req->sec_cpl.cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(
2405 null ? 0 : req->assoclen + IV + 1,
2407 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL ||
2408 subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA)
2409 temp = CHCR_SCMD_CIPHER_MODE_AES_CTR;
2411 temp = CHCR_SCMD_CIPHER_MODE_AES_CBC;
2412 chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op,
2413 (reqctx->op == CHCR_ENCRYPT_OP) ? 1 : 0,
2415 actx->auth_mode, aeadctx->hmac_ctrl,
2417 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
2420 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
2421 if (reqctx->op == CHCR_ENCRYPT_OP ||
2422 subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
2423 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL)
2424 memcpy(chcr_req->key_ctx.key, aeadctx->key,
2425 aeadctx->enckey_len);
2427 memcpy(chcr_req->key_ctx.key, actx->dec_rrkey,
2428 aeadctx->enckey_len);
2430 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
2431 actx->h_iopad, kctx_len - roundup(aeadctx->enckey_len, 16));
2432 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
2433 ivptr = (u8 *)(phys_cpl + 1) + dst_size;
2434 ulptx = (struct ulptx_sgl *)(ivptr + IV);
2435 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
2436 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
2437 memcpy(ivptr, aeadctx->nonce, CTR_RFC3686_NONCE_SIZE);
2438 memcpy(ivptr + CTR_RFC3686_NONCE_SIZE, req->iv,
2439 CTR_RFC3686_IV_SIZE);
2440 *(__be32 *)(ivptr + CTR_RFC3686_NONCE_SIZE +
2441 CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
2443 memcpy(ivptr, req->iv, IV);
2445 chcr_add_aead_dst_ent(req, phys_cpl, qid);
2446 chcr_add_aead_src_ent(req, ulptx);
2447 atomic_inc(&adap->chcr_stats.cipher_rqst);
2448 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
2449 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen) : 0);
2450 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, size,
2451 transhdr_len, temp, 0);
2456 chcr_aead_common_exit(req);
2458 return ERR_PTR(error);
2461 int chcr_aead_dma_map(struct device *dev,
2462 struct aead_request *req,
2463 unsigned short op_type)
2466 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2467 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2468 unsigned int authsize = crypto_aead_authsize(tfm);
2471 dst_size = req->assoclen + req->cryptlen + (op_type ?
2472 -authsize : authsize);
2473 if (!req->cryptlen || !dst_size)
2475 reqctx->iv_dma = dma_map_single(dev, reqctx->iv, (IV + reqctx->b0_len),
2477 if (dma_mapping_error(dev, reqctx->iv_dma))
2480 reqctx->b0_dma = reqctx->iv_dma + IV;
2483 if (req->src == req->dst) {
2484 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2489 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2493 error = dma_map_sg(dev, req->dst, sg_nents(req->dst),
2496 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2504 dma_unmap_single(dev, reqctx->iv_dma, IV, DMA_BIDIRECTIONAL);
2508 void chcr_aead_dma_unmap(struct device *dev,
2509 struct aead_request *req,
2510 unsigned short op_type)
2512 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2513 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2514 unsigned int authsize = crypto_aead_authsize(tfm);
2517 dst_size = req->assoclen + req->cryptlen + (op_type ?
2518 -authsize : authsize);
2519 if (!req->cryptlen || !dst_size)
2522 dma_unmap_single(dev, reqctx->iv_dma, (IV + reqctx->b0_len),
2524 if (req->src == req->dst) {
2525 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2528 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2530 dma_unmap_sg(dev, req->dst, sg_nents(req->dst),
2535 void chcr_add_aead_src_ent(struct aead_request *req,
2536 struct ulptx_sgl *ulptx)
2538 struct ulptx_walk ulp_walk;
2539 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2542 u8 *buf = (u8 *)ulptx;
2544 if (reqctx->b0_len) {
2545 memcpy(buf, reqctx->scratch_pad, reqctx->b0_len);
2546 buf += reqctx->b0_len;
2548 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
2549 buf, req->cryptlen + req->assoclen, 0);
2551 ulptx_walk_init(&ulp_walk, ulptx);
2553 ulptx_walk_add_page(&ulp_walk, reqctx->b0_len,
2555 ulptx_walk_add_sg(&ulp_walk, req->src, req->cryptlen +
2557 ulptx_walk_end(&ulp_walk);
2561 void chcr_add_aead_dst_ent(struct aead_request *req,
2562 struct cpl_rx_phys_dsgl *phys_cpl,
2565 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2566 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2567 struct dsgl_walk dsgl_walk;
2568 unsigned int authsize = crypto_aead_authsize(tfm);
2569 struct chcr_context *ctx = a_ctx(tfm);
2572 dsgl_walk_init(&dsgl_walk, phys_cpl);
2573 dsgl_walk_add_page(&dsgl_walk, IV + reqctx->b0_len, reqctx->iv_dma);
2574 temp = req->assoclen + req->cryptlen +
2575 (reqctx->op ? -authsize : authsize);
2576 dsgl_walk_add_sg(&dsgl_walk, req->dst, temp, 0);
2577 dsgl_walk_end(&dsgl_walk, qid, ctx->pci_chan_id);
2580 void chcr_add_cipher_src_ent(struct skcipher_request *req,
2582 struct cipher_wr_param *wrparam)
2584 struct ulptx_walk ulp_walk;
2585 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
2588 memcpy(buf, reqctx->iv, IV);
2591 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
2592 buf, wrparam->bytes, reqctx->processed);
2594 ulptx_walk_init(&ulp_walk, (struct ulptx_sgl *)buf);
2595 ulptx_walk_add_sg(&ulp_walk, reqctx->srcsg, wrparam->bytes,
2597 reqctx->srcsg = ulp_walk.last_sg;
2598 reqctx->src_ofst = ulp_walk.last_sg_len;
2599 ulptx_walk_end(&ulp_walk);
2603 void chcr_add_cipher_dst_ent(struct skcipher_request *req,
2604 struct cpl_rx_phys_dsgl *phys_cpl,
2605 struct cipher_wr_param *wrparam,
2608 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
2609 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(wrparam->req);
2610 struct chcr_context *ctx = c_ctx(tfm);
2611 struct dsgl_walk dsgl_walk;
2613 dsgl_walk_init(&dsgl_walk, phys_cpl);
2614 dsgl_walk_add_sg(&dsgl_walk, reqctx->dstsg, wrparam->bytes,
2616 reqctx->dstsg = dsgl_walk.last_sg;
2617 reqctx->dst_ofst = dsgl_walk.last_sg_len;
2619 dsgl_walk_end(&dsgl_walk, qid, ctx->pci_chan_id);
2622 void chcr_add_hash_src_ent(struct ahash_request *req,
2623 struct ulptx_sgl *ulptx,
2624 struct hash_wr_param *param)
2626 struct ulptx_walk ulp_walk;
2627 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
2629 if (reqctx->hctx_wr.imm) {
2630 u8 *buf = (u8 *)ulptx;
2632 if (param->bfr_len) {
2633 memcpy(buf, reqctx->reqbfr, param->bfr_len);
2634 buf += param->bfr_len;
2637 sg_pcopy_to_buffer(reqctx->hctx_wr.srcsg,
2638 sg_nents(reqctx->hctx_wr.srcsg), buf,
2641 ulptx_walk_init(&ulp_walk, ulptx);
2643 ulptx_walk_add_page(&ulp_walk, param->bfr_len,
2644 reqctx->hctx_wr.dma_addr);
2645 ulptx_walk_add_sg(&ulp_walk, reqctx->hctx_wr.srcsg,
2646 param->sg_len, reqctx->hctx_wr.src_ofst);
2647 reqctx->hctx_wr.srcsg = ulp_walk.last_sg;
2648 reqctx->hctx_wr.src_ofst = ulp_walk.last_sg_len;
2649 ulptx_walk_end(&ulp_walk);
2653 int chcr_hash_dma_map(struct device *dev,
2654 struct ahash_request *req)
2656 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
2661 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2665 req_ctx->hctx_wr.is_sg_map = 1;
2669 void chcr_hash_dma_unmap(struct device *dev,
2670 struct ahash_request *req)
2672 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
2677 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2679 req_ctx->hctx_wr.is_sg_map = 0;
2683 int chcr_cipher_dma_map(struct device *dev,
2684 struct skcipher_request *req)
2688 if (req->src == req->dst) {
2689 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2694 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2698 error = dma_map_sg(dev, req->dst, sg_nents(req->dst),
2701 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2712 void chcr_cipher_dma_unmap(struct device *dev,
2713 struct skcipher_request *req)
2715 if (req->src == req->dst) {
2716 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2719 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2721 dma_unmap_sg(dev, req->dst, sg_nents(req->dst),
2726 static int set_msg_len(u8 *block, unsigned int msglen, int csize)
2730 memset(block, 0, csize);
2735 else if (msglen > (unsigned int)(1 << (8 * csize)))
2738 data = cpu_to_be32(msglen);
2739 memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
2744 static int generate_b0(struct aead_request *req, u8 *ivptr,
2745 unsigned short op_type)
2747 unsigned int l, lp, m;
2749 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2750 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2751 u8 *b0 = reqctx->scratch_pad;
2753 m = crypto_aead_authsize(aead);
2755 memcpy(b0, ivptr, 16);
2760 /* set m, bits 3-5 */
2761 *b0 |= (8 * ((m - 2) / 2));
2763 /* set adata, bit 6, if associated data is used */
2766 rc = set_msg_len(b0 + 16 - l,
2767 (op_type == CHCR_DECRYPT_OP) ?
2768 req->cryptlen - m : req->cryptlen, l);
2773 static inline int crypto_ccm_check_iv(const u8 *iv)
2775 /* 2 <= L <= 8, so 1 <= L' <= 7. */
2776 if (iv[0] < 1 || iv[0] > 7)
2782 static int ccm_format_packet(struct aead_request *req,
2784 unsigned int sub_type,
2785 unsigned short op_type,
2786 unsigned int assoclen)
2788 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2789 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2790 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2793 if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
2795 memcpy(ivptr + 1, &aeadctx->salt[0], 3);
2796 memcpy(ivptr + 4, req->iv, 8);
2797 memset(ivptr + 12, 0, 4);
2799 memcpy(ivptr, req->iv, 16);
2802 *((unsigned short *)(reqctx->scratch_pad + 16)) =
2805 rc = generate_b0(req, ivptr, op_type);
2806 /* zero the ctr value */
2807 memset(ivptr + 15 - ivptr[0], 0, ivptr[0] + 1);
2811 static void fill_sec_cpl_for_aead(struct cpl_tx_sec_pdu *sec_cpl,
2812 unsigned int dst_size,
2813 struct aead_request *req,
2814 unsigned short op_type)
2816 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2817 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2818 unsigned int cipher_mode = CHCR_SCMD_CIPHER_MODE_AES_CCM;
2819 unsigned int mac_mode = CHCR_SCMD_AUTH_MODE_CBCMAC;
2820 unsigned int c_id = a_ctx(tfm)->tx_chan_id;
2821 unsigned int ccm_xtra;
2822 unsigned char tag_offset = 0, auth_offset = 0;
2823 unsigned int assoclen;
2825 if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
2826 assoclen = req->assoclen - 8;
2828 assoclen = req->assoclen;
2829 ccm_xtra = CCM_B0_SIZE +
2830 ((assoclen) ? CCM_AAD_FIELD_SIZE : 0);
2832 auth_offset = req->cryptlen ?
2833 (req->assoclen + IV + 1 + ccm_xtra) : 0;
2834 if (op_type == CHCR_DECRYPT_OP) {
2835 if (crypto_aead_authsize(tfm) != req->cryptlen)
2836 tag_offset = crypto_aead_authsize(tfm);
2842 sec_cpl->op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(c_id,
2845 htonl(req->assoclen + IV + req->cryptlen + ccm_xtra);
2846 /* For CCM there wil be b0 always. So AAD start will be 1 always */
2847 sec_cpl->aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
2848 1 + IV, IV + assoclen + ccm_xtra,
2849 req->assoclen + IV + 1 + ccm_xtra, 0);
2851 sec_cpl->cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(0,
2852 auth_offset, tag_offset,
2853 (op_type == CHCR_ENCRYPT_OP) ? 0 :
2854 crypto_aead_authsize(tfm));
2855 sec_cpl->seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(op_type,
2856 (op_type == CHCR_ENCRYPT_OP) ? 0 : 1,
2857 cipher_mode, mac_mode,
2858 aeadctx->hmac_ctrl, IV >> 1);
2860 sec_cpl->ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1, 0,
2864 static int aead_ccm_validate_input(unsigned short op_type,
2865 struct aead_request *req,
2866 struct chcr_aead_ctx *aeadctx,
2867 unsigned int sub_type)
2869 if (sub_type != CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
2870 if (crypto_ccm_check_iv(req->iv)) {
2871 pr_err("CCM: IV check fails\n");
2875 if (req->assoclen != 16 && req->assoclen != 20) {
2876 pr_err("RFC4309: Invalid AAD length %d\n",
2884 static struct sk_buff *create_aead_ccm_wr(struct aead_request *req,
2888 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2889 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2890 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2891 struct sk_buff *skb = NULL;
2892 struct chcr_wr *chcr_req;
2893 struct cpl_rx_phys_dsgl *phys_cpl;
2894 struct ulptx_sgl *ulptx;
2895 unsigned int transhdr_len;
2896 unsigned int dst_size = 0, kctx_len, dnents, temp, snents;
2897 unsigned int sub_type, assoclen = req->assoclen;
2898 unsigned int authsize = crypto_aead_authsize(tfm);
2899 int error = -EINVAL;
2901 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
2903 struct adapter *adap = padap(a_ctx(tfm)->dev);
2905 sub_type = get_aead_subtype(tfm);
2906 if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
2908 reqctx->b0_len = CCM_B0_SIZE + (assoclen ? CCM_AAD_FIELD_SIZE : 0);
2909 error = chcr_aead_common_init(req);
2911 return ERR_PTR(error);
2913 error = aead_ccm_validate_input(reqctx->op, req, aeadctx, sub_type);
2916 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen
2917 + (reqctx->op ? -authsize : authsize),
2918 CHCR_DST_SG_SIZE, 0);
2919 dnents += MIN_CCM_SG; // For IV and B0
2920 dst_size = get_space_for_phys_dsgl(dnents);
2921 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
2922 CHCR_SRC_SG_SIZE, 0);
2923 snents += MIN_CCM_SG; //For B0
2924 kctx_len = roundup(aeadctx->enckey_len, 16) * 2;
2925 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
2926 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen +
2927 reqctx->b0_len) <= SGE_MAX_WR_LEN;
2928 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen +
2929 reqctx->b0_len, 16) :
2930 (sgl_len(snents) * 8);
2931 transhdr_len += temp;
2932 transhdr_len = roundup(transhdr_len, 16);
2934 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE -
2935 reqctx->b0_len, transhdr_len, reqctx->op)) {
2936 atomic_inc(&adap->chcr_stats.fallback);
2937 chcr_aead_common_exit(req);
2938 return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
2940 skb = alloc_skb(transhdr_len, flags);
2947 chcr_req = __skb_put_zero(skb, transhdr_len);
2949 fill_sec_cpl_for_aead(&chcr_req->sec_cpl, dst_size, req, reqctx->op);
2951 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
2952 memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len);
2953 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
2954 aeadctx->key, aeadctx->enckey_len);
2956 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
2957 ivptr = (u8 *)(phys_cpl + 1) + dst_size;
2958 ulptx = (struct ulptx_sgl *)(ivptr + IV);
2959 error = ccm_format_packet(req, ivptr, sub_type, reqctx->op, assoclen);
2962 chcr_add_aead_dst_ent(req, phys_cpl, qid);
2963 chcr_add_aead_src_ent(req, ulptx);
2965 atomic_inc(&adap->chcr_stats.aead_rqst);
2966 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
2967 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen +
2968 reqctx->b0_len) : 0);
2969 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, 0,
2970 transhdr_len, temp, 0);
2977 chcr_aead_common_exit(req);
2978 return ERR_PTR(error);
2981 static struct sk_buff *create_gcm_wr(struct aead_request *req,
2985 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2986 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2987 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2988 struct sk_buff *skb = NULL;
2989 struct chcr_wr *chcr_req;
2990 struct cpl_rx_phys_dsgl *phys_cpl;
2991 struct ulptx_sgl *ulptx;
2992 unsigned int transhdr_len, dnents = 0, snents;
2993 unsigned int dst_size = 0, temp = 0, kctx_len, assoclen = req->assoclen;
2994 unsigned int authsize = crypto_aead_authsize(tfm);
2995 int error = -EINVAL;
2997 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
2999 struct adapter *adap = padap(a_ctx(tfm)->dev);
3001 if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106)
3002 assoclen = req->assoclen - 8;
3005 error = chcr_aead_common_init(req);
3007 return ERR_PTR(error);
3008 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen +
3009 (reqctx->op ? -authsize : authsize),
3010 CHCR_DST_SG_SIZE, 0);
3011 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
3012 CHCR_SRC_SG_SIZE, 0);
3013 dnents += MIN_GCM_SG; // For IV
3014 dst_size = get_space_for_phys_dsgl(dnents);
3015 kctx_len = roundup(aeadctx->enckey_len, 16) + AEAD_H_SIZE;
3016 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
3017 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen) <=
3019 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen, 16) :
3020 (sgl_len(snents) * 8);
3021 transhdr_len += temp;
3022 transhdr_len = roundup(transhdr_len, 16);
3023 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE,
3024 transhdr_len, reqctx->op)) {
3026 atomic_inc(&adap->chcr_stats.fallback);
3027 chcr_aead_common_exit(req);
3028 return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
3030 skb = alloc_skb(transhdr_len, flags);
3036 chcr_req = __skb_put_zero(skb, transhdr_len);
3038 //Offset of tag from end
3039 temp = (reqctx->op == CHCR_ENCRYPT_OP) ? 0 : authsize;
3040 chcr_req->sec_cpl.op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(
3041 a_ctx(tfm)->tx_chan_id, 2, 1);
3042 chcr_req->sec_cpl.pldlen =
3043 htonl(req->assoclen + IV + req->cryptlen);
3044 chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
3045 assoclen ? 1 + IV : 0,
3046 assoclen ? IV + assoclen : 0,
3047 req->assoclen + IV + 1, 0);
3048 chcr_req->sec_cpl.cipherstop_lo_authinsert =
3049 FILL_SEC_CPL_AUTHINSERT(0, req->assoclen + IV + 1,
3051 chcr_req->sec_cpl.seqno_numivs =
3052 FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op, (reqctx->op ==
3053 CHCR_ENCRYPT_OP) ? 1 : 0,
3054 CHCR_SCMD_CIPHER_MODE_AES_GCM,
3055 CHCR_SCMD_AUTH_MODE_GHASH,
3056 aeadctx->hmac_ctrl, IV >> 1);
3057 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
3059 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
3060 memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len);
3061 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
3062 GCM_CTX(aeadctx)->ghash_h, AEAD_H_SIZE);
3064 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
3065 ivptr = (u8 *)(phys_cpl + 1) + dst_size;
3066 /* prepare a 16 byte iv */
3067 /* S A L T | IV | 0x00000001 */
3068 if (get_aead_subtype(tfm) ==
3069 CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) {
3070 memcpy(ivptr, aeadctx->salt, 4);
3071 memcpy(ivptr + 4, req->iv, GCM_RFC4106_IV_SIZE);
3073 memcpy(ivptr, req->iv, GCM_AES_IV_SIZE);
3075 *((unsigned int *)(ivptr + 12)) = htonl(0x01);
3077 ulptx = (struct ulptx_sgl *)(ivptr + 16);
3079 chcr_add_aead_dst_ent(req, phys_cpl, qid);
3080 chcr_add_aead_src_ent(req, ulptx);
3081 atomic_inc(&adap->chcr_stats.aead_rqst);
3082 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
3083 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen) : 0);
3084 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, size,
3085 transhdr_len, temp, reqctx->verify);
3090 chcr_aead_common_exit(req);
3091 return ERR_PTR(error);
3096 static int chcr_aead_cra_init(struct crypto_aead *tfm)
3098 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3099 struct aead_alg *alg = crypto_aead_alg(tfm);
3101 aeadctx->sw_cipher = crypto_alloc_aead(alg->base.cra_name, 0,
3102 CRYPTO_ALG_NEED_FALLBACK |
3104 if (IS_ERR(aeadctx->sw_cipher))
3105 return PTR_ERR(aeadctx->sw_cipher);
3106 crypto_aead_set_reqsize(tfm, max(sizeof(struct chcr_aead_reqctx),
3107 sizeof(struct aead_request) +
3108 crypto_aead_reqsize(aeadctx->sw_cipher)));
3109 return chcr_device_init(a_ctx(tfm));
3112 static void chcr_aead_cra_exit(struct crypto_aead *tfm)
3114 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3116 crypto_free_aead(aeadctx->sw_cipher);
3119 static int chcr_authenc_null_setauthsize(struct crypto_aead *tfm,
3120 unsigned int authsize)
3122 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3124 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NOP;
3125 aeadctx->mayverify = VERIFY_HW;
3126 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3128 static int chcr_authenc_setauthsize(struct crypto_aead *tfm,
3129 unsigned int authsize)
3131 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3132 u32 maxauth = crypto_aead_maxauthsize(tfm);
3134 /*SHA1 authsize in ipsec is 12 instead of 10 i.e maxauthsize / 2 is not
3135 * true for sha1. authsize == 12 condition should be before
3136 * authsize == (maxauth >> 1)
3138 if (authsize == ICV_4) {
3139 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
3140 aeadctx->mayverify = VERIFY_HW;
3141 } else if (authsize == ICV_6) {
3142 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
3143 aeadctx->mayverify = VERIFY_HW;
3144 } else if (authsize == ICV_10) {
3145 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
3146 aeadctx->mayverify = VERIFY_HW;
3147 } else if (authsize == ICV_12) {
3148 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3149 aeadctx->mayverify = VERIFY_HW;
3150 } else if (authsize == ICV_14) {
3151 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
3152 aeadctx->mayverify = VERIFY_HW;
3153 } else if (authsize == (maxauth >> 1)) {
3154 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3155 aeadctx->mayverify = VERIFY_HW;
3156 } else if (authsize == maxauth) {
3157 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3158 aeadctx->mayverify = VERIFY_HW;
3160 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3161 aeadctx->mayverify = VERIFY_SW;
3163 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3167 static int chcr_gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
3169 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3173 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
3174 aeadctx->mayverify = VERIFY_HW;
3177 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3178 aeadctx->mayverify = VERIFY_HW;
3181 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3182 aeadctx->mayverify = VERIFY_HW;
3185 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
3186 aeadctx->mayverify = VERIFY_HW;
3189 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3190 aeadctx->mayverify = VERIFY_HW;
3194 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3195 aeadctx->mayverify = VERIFY_SW;
3199 crypto_tfm_set_flags((struct crypto_tfm *) tfm,
3200 CRYPTO_TFM_RES_BAD_KEY_LEN);
3203 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3206 static int chcr_4106_4309_setauthsize(struct crypto_aead *tfm,
3207 unsigned int authsize)
3209 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3213 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3214 aeadctx->mayverify = VERIFY_HW;
3217 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3218 aeadctx->mayverify = VERIFY_HW;
3221 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3222 aeadctx->mayverify = VERIFY_HW;
3225 crypto_tfm_set_flags((struct crypto_tfm *)tfm,
3226 CRYPTO_TFM_RES_BAD_KEY_LEN);
3229 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3232 static int chcr_ccm_setauthsize(struct crypto_aead *tfm,
3233 unsigned int authsize)
3235 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3239 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
3240 aeadctx->mayverify = VERIFY_HW;
3243 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
3244 aeadctx->mayverify = VERIFY_HW;
3247 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3248 aeadctx->mayverify = VERIFY_HW;
3251 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
3252 aeadctx->mayverify = VERIFY_HW;
3255 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3256 aeadctx->mayverify = VERIFY_HW;
3259 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
3260 aeadctx->mayverify = VERIFY_HW;
3263 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3264 aeadctx->mayverify = VERIFY_HW;
3267 crypto_tfm_set_flags((struct crypto_tfm *)tfm,
3268 CRYPTO_TFM_RES_BAD_KEY_LEN);
3271 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3274 static int chcr_ccm_common_setkey(struct crypto_aead *aead,
3276 unsigned int keylen)
3278 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
3279 unsigned char ck_size, mk_size;
3280 int key_ctx_size = 0;
3282 key_ctx_size = sizeof(struct _key_ctx) + roundup(keylen, 16) * 2;
3283 if (keylen == AES_KEYSIZE_128) {
3284 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3285 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_128;
3286 } else if (keylen == AES_KEYSIZE_192) {
3287 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3288 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_192;
3289 } else if (keylen == AES_KEYSIZE_256) {
3290 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3291 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
3293 crypto_tfm_set_flags((struct crypto_tfm *)aead,
3294 CRYPTO_TFM_RES_BAD_KEY_LEN);
3295 aeadctx->enckey_len = 0;
3298 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, mk_size, 0, 0,
3300 memcpy(aeadctx->key, key, keylen);
3301 aeadctx->enckey_len = keylen;
3306 static int chcr_aead_ccm_setkey(struct crypto_aead *aead,
3308 unsigned int keylen)
3310 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
3313 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3314 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) &
3315 CRYPTO_TFM_REQ_MASK);
3316 error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3317 crypto_aead_clear_flags(aead, CRYPTO_TFM_RES_MASK);
3318 crypto_aead_set_flags(aead, crypto_aead_get_flags(aeadctx->sw_cipher) &
3319 CRYPTO_TFM_RES_MASK);
3322 return chcr_ccm_common_setkey(aead, key, keylen);
3325 static int chcr_aead_rfc4309_setkey(struct crypto_aead *aead, const u8 *key,
3326 unsigned int keylen)
3328 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
3332 crypto_tfm_set_flags((struct crypto_tfm *)aead,
3333 CRYPTO_TFM_RES_BAD_KEY_LEN);
3334 aeadctx->enckey_len = 0;
3337 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3338 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) &
3339 CRYPTO_TFM_REQ_MASK);
3340 error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3341 crypto_aead_clear_flags(aead, CRYPTO_TFM_RES_MASK);
3342 crypto_aead_set_flags(aead, crypto_aead_get_flags(aeadctx->sw_cipher) &
3343 CRYPTO_TFM_RES_MASK);
3347 memcpy(aeadctx->salt, key + keylen, 3);
3348 return chcr_ccm_common_setkey(aead, key, keylen);
3351 static int chcr_gcm_setkey(struct crypto_aead *aead, const u8 *key,
3352 unsigned int keylen)
3354 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
3355 struct chcr_gcm_ctx *gctx = GCM_CTX(aeadctx);
3356 unsigned int ck_size;
3357 int ret = 0, key_ctx_size = 0;
3358 struct crypto_aes_ctx aes;
3360 aeadctx->enckey_len = 0;
3361 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3362 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead)
3363 & CRYPTO_TFM_REQ_MASK);
3364 ret = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3365 crypto_aead_clear_flags(aead, CRYPTO_TFM_RES_MASK);
3366 crypto_aead_set_flags(aead, crypto_aead_get_flags(aeadctx->sw_cipher) &
3367 CRYPTO_TFM_RES_MASK);
3371 if (get_aead_subtype(aead) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 &&
3373 keylen -= 4; /* nonce/salt is present in the last 4 bytes */
3374 memcpy(aeadctx->salt, key + keylen, 4);
3376 if (keylen == AES_KEYSIZE_128) {
3377 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3378 } else if (keylen == AES_KEYSIZE_192) {
3379 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3380 } else if (keylen == AES_KEYSIZE_256) {
3381 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3383 crypto_tfm_set_flags((struct crypto_tfm *)aead,
3384 CRYPTO_TFM_RES_BAD_KEY_LEN);
3385 pr_err("GCM: Invalid key length %d\n", keylen);
3390 memcpy(aeadctx->key, key, keylen);
3391 aeadctx->enckey_len = keylen;
3392 key_ctx_size = sizeof(struct _key_ctx) + roundup(keylen, 16) +
3394 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size,
3395 CHCR_KEYCTX_MAC_KEY_SIZE_128,
3398 /* Calculate the H = CIPH(K, 0 repeated 16 times).
3399 * It will go in key context
3401 ret = aes_expandkey(&aes, key, keylen);
3403 aeadctx->enckey_len = 0;
3406 memset(gctx->ghash_h, 0, AEAD_H_SIZE);
3407 aes_encrypt(&aes, gctx->ghash_h, gctx->ghash_h);
3408 memzero_explicit(&aes, sizeof(aes));
3414 static int chcr_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
3415 unsigned int keylen)
3417 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(authenc));
3418 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
3419 /* it contains auth and cipher key both*/
3420 struct crypto_authenc_keys keys;
3421 unsigned int bs, subtype;
3422 unsigned int max_authsize = crypto_aead_alg(authenc)->maxauthsize;
3423 int err = 0, i, key_ctx_len = 0;
3424 unsigned char ck_size = 0;
3425 unsigned char pad[CHCR_HASH_MAX_BLOCK_SIZE_128] = { 0 };
3426 struct crypto_shash *base_hash = ERR_PTR(-EINVAL);
3427 struct algo_param param;
3431 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3432 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
3433 & CRYPTO_TFM_REQ_MASK);
3434 err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3435 crypto_aead_clear_flags(authenc, CRYPTO_TFM_RES_MASK);
3436 crypto_aead_set_flags(authenc, crypto_aead_get_flags(aeadctx->sw_cipher)
3437 & CRYPTO_TFM_RES_MASK);
3441 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
3442 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
3446 if (get_alg_config(¶m, max_authsize)) {
3447 pr_err("chcr : Unsupported digest size\n");
3450 subtype = get_aead_subtype(authenc);
3451 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
3452 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
3453 if (keys.enckeylen < CTR_RFC3686_NONCE_SIZE)
3455 memcpy(aeadctx->nonce, keys.enckey + (keys.enckeylen
3456 - CTR_RFC3686_NONCE_SIZE), CTR_RFC3686_NONCE_SIZE);
3457 keys.enckeylen -= CTR_RFC3686_NONCE_SIZE;
3459 if (keys.enckeylen == AES_KEYSIZE_128) {
3460 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3461 } else if (keys.enckeylen == AES_KEYSIZE_192) {
3462 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3463 } else if (keys.enckeylen == AES_KEYSIZE_256) {
3464 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3466 pr_err("chcr : Unsupported cipher key\n");
3470 /* Copy only encryption key. We use authkey to generate h(ipad) and
3471 * h(opad) so authkey is not needed again. authkeylen size have the
3472 * size of the hash digest size.
3474 memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
3475 aeadctx->enckey_len = keys.enckeylen;
3476 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_SHA ||
3477 subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL) {
3479 get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
3480 aeadctx->enckey_len << 3);
3482 base_hash = chcr_alloc_shash(max_authsize);
3483 if (IS_ERR(base_hash)) {
3484 pr_err("chcr : Base driver cannot be loaded\n");
3485 aeadctx->enckey_len = 0;
3486 memzero_explicit(&keys, sizeof(keys));
3490 SHASH_DESC_ON_STACK(shash, base_hash);
3492 shash->tfm = base_hash;
3493 bs = crypto_shash_blocksize(base_hash);
3494 align = KEYCTX_ALIGN_PAD(max_authsize);
3495 o_ptr = actx->h_iopad + param.result_size + align;
3497 if (keys.authkeylen > bs) {
3498 err = crypto_shash_digest(shash, keys.authkey,
3502 pr_err("chcr : Base driver cannot be loaded\n");
3505 keys.authkeylen = max_authsize;
3507 memcpy(o_ptr, keys.authkey, keys.authkeylen);
3509 /* Compute the ipad-digest*/
3510 memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
3511 memcpy(pad, o_ptr, keys.authkeylen);
3512 for (i = 0; i < bs >> 2; i++)
3513 *((unsigned int *)pad + i) ^= IPAD_DATA;
3515 if (chcr_compute_partial_hash(shash, pad, actx->h_iopad,
3518 /* Compute the opad-digest */
3519 memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
3520 memcpy(pad, o_ptr, keys.authkeylen);
3521 for (i = 0; i < bs >> 2; i++)
3522 *((unsigned int *)pad + i) ^= OPAD_DATA;
3524 if (chcr_compute_partial_hash(shash, pad, o_ptr, max_authsize))
3527 /* convert the ipad and opad digest to network order */
3528 chcr_change_order(actx->h_iopad, param.result_size);
3529 chcr_change_order(o_ptr, param.result_size);
3530 key_ctx_len = sizeof(struct _key_ctx) +
3531 roundup(keys.enckeylen, 16) +
3532 (param.result_size + align) * 2;
3533 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, param.mk_size,
3534 0, 1, key_ctx_len >> 4);
3535 actx->auth_mode = param.auth_mode;
3536 chcr_free_shash(base_hash);
3538 memzero_explicit(&keys, sizeof(keys));
3542 aeadctx->enckey_len = 0;
3543 memzero_explicit(&keys, sizeof(keys));
3544 if (!IS_ERR(base_hash))
3545 chcr_free_shash(base_hash);
3549 static int chcr_aead_digest_null_setkey(struct crypto_aead *authenc,
3550 const u8 *key, unsigned int keylen)
3552 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(authenc));
3553 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
3554 struct crypto_authenc_keys keys;
3556 /* it contains auth and cipher key both*/
3557 unsigned int subtype;
3558 int key_ctx_len = 0;
3559 unsigned char ck_size = 0;
3561 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3562 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
3563 & CRYPTO_TFM_REQ_MASK);
3564 err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3565 crypto_aead_clear_flags(authenc, CRYPTO_TFM_RES_MASK);
3566 crypto_aead_set_flags(authenc, crypto_aead_get_flags(aeadctx->sw_cipher)
3567 & CRYPTO_TFM_RES_MASK);
3571 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
3572 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
3575 subtype = get_aead_subtype(authenc);
3576 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
3577 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
3578 if (keys.enckeylen < CTR_RFC3686_NONCE_SIZE)
3580 memcpy(aeadctx->nonce, keys.enckey + (keys.enckeylen
3581 - CTR_RFC3686_NONCE_SIZE), CTR_RFC3686_NONCE_SIZE);
3582 keys.enckeylen -= CTR_RFC3686_NONCE_SIZE;
3584 if (keys.enckeylen == AES_KEYSIZE_128) {
3585 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3586 } else if (keys.enckeylen == AES_KEYSIZE_192) {
3587 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3588 } else if (keys.enckeylen == AES_KEYSIZE_256) {
3589 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3591 pr_err("chcr : Unsupported cipher key %d\n", keys.enckeylen);
3594 memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
3595 aeadctx->enckey_len = keys.enckeylen;
3596 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_SHA ||
3597 subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL) {
3598 get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
3599 aeadctx->enckey_len << 3);
3601 key_ctx_len = sizeof(struct _key_ctx) + roundup(keys.enckeylen, 16);
3603 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY, 0,
3604 0, key_ctx_len >> 4);
3605 actx->auth_mode = CHCR_SCMD_AUTH_MODE_NOP;
3606 memzero_explicit(&keys, sizeof(keys));
3609 aeadctx->enckey_len = 0;
3610 memzero_explicit(&keys, sizeof(keys));
3614 static int chcr_aead_op(struct aead_request *req,
3616 create_wr_t create_wr_fn)
3618 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3619 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
3620 struct uld_ctx *u_ctx;
3621 struct sk_buff *skb;
3623 struct chcr_dev *cdev;
3625 cdev = a_ctx(tfm)->dev;
3627 pr_err("chcr : %s : No crypto device.\n", __func__);
3631 if (chcr_inc_wrcount(cdev)) {
3632 /* Detach state for CHCR means lldi or padap is freed.
3633 * We cannot increment fallback here.
3635 return chcr_aead_fallback(req, reqctx->op);
3638 u_ctx = ULD_CTX(a_ctx(tfm));
3639 if (cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
3640 a_ctx(tfm)->tx_qidx)) {
3642 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
3643 chcr_dec_wrcount(cdev);
3648 /* Form a WR from req */
3649 skb = create_wr_fn(req, u_ctx->lldi.rxq_ids[a_ctx(tfm)->rx_qidx], size);
3651 if (IS_ERR_OR_NULL(skb)) {
3652 chcr_dec_wrcount(cdev);
3653 return PTR_ERR_OR_ZERO(skb);
3656 skb->dev = u_ctx->lldi.ports[0];
3657 set_wr_txq(skb, CPL_PRIORITY_DATA, a_ctx(tfm)->tx_qidx);
3659 return isfull ? -EBUSY : -EINPROGRESS;
3662 static int chcr_aead_encrypt(struct aead_request *req)
3664 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3665 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
3667 reqctx->verify = VERIFY_HW;
3668 reqctx->op = CHCR_ENCRYPT_OP;
3670 switch (get_aead_subtype(tfm)) {
3671 case CRYPTO_ALG_SUB_TYPE_CTR_SHA:
3672 case CRYPTO_ALG_SUB_TYPE_CBC_SHA:
3673 case CRYPTO_ALG_SUB_TYPE_CBC_NULL:
3674 case CRYPTO_ALG_SUB_TYPE_CTR_NULL:
3675 return chcr_aead_op(req, 0, create_authenc_wr);
3676 case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
3677 case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
3678 return chcr_aead_op(req, 0, create_aead_ccm_wr);
3680 return chcr_aead_op(req, 0, create_gcm_wr);
3684 static int chcr_aead_decrypt(struct aead_request *req)
3686 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3687 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3688 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
3691 if (aeadctx->mayverify == VERIFY_SW) {
3692 size = crypto_aead_maxauthsize(tfm);
3693 reqctx->verify = VERIFY_SW;
3696 reqctx->verify = VERIFY_HW;
3698 reqctx->op = CHCR_DECRYPT_OP;
3699 switch (get_aead_subtype(tfm)) {
3700 case CRYPTO_ALG_SUB_TYPE_CBC_SHA:
3701 case CRYPTO_ALG_SUB_TYPE_CTR_SHA:
3702 case CRYPTO_ALG_SUB_TYPE_CBC_NULL:
3703 case CRYPTO_ALG_SUB_TYPE_CTR_NULL:
3704 return chcr_aead_op(req, size, create_authenc_wr);
3705 case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
3706 case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
3707 return chcr_aead_op(req, size, create_aead_ccm_wr);
3709 return chcr_aead_op(req, size, create_gcm_wr);
3713 static struct chcr_alg_template driver_algs[] = {
3716 .type = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_SUB_TYPE_CBC,
3719 .base.cra_name = "cbc(aes)",
3720 .base.cra_driver_name = "cbc-aes-chcr",
3721 .base.cra_blocksize = AES_BLOCK_SIZE,
3723 .init = chcr_init_tfm,
3724 .exit = chcr_exit_tfm,
3725 .min_keysize = AES_MIN_KEY_SIZE,
3726 .max_keysize = AES_MAX_KEY_SIZE,
3727 .ivsize = AES_BLOCK_SIZE,
3728 .setkey = chcr_aes_cbc_setkey,
3729 .encrypt = chcr_aes_encrypt,
3730 .decrypt = chcr_aes_decrypt,
3734 .type = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_SUB_TYPE_XTS,
3737 .base.cra_name = "xts(aes)",
3738 .base.cra_driver_name = "xts-aes-chcr",
3739 .base.cra_blocksize = AES_BLOCK_SIZE,
3741 .init = chcr_init_tfm,
3742 .exit = chcr_exit_tfm,
3743 .min_keysize = 2 * AES_MIN_KEY_SIZE,
3744 .max_keysize = 2 * AES_MAX_KEY_SIZE,
3745 .ivsize = AES_BLOCK_SIZE,
3746 .setkey = chcr_aes_xts_setkey,
3747 .encrypt = chcr_aes_encrypt,
3748 .decrypt = chcr_aes_decrypt,
3752 .type = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_SUB_TYPE_CTR,
3755 .base.cra_name = "ctr(aes)",
3756 .base.cra_driver_name = "ctr-aes-chcr",
3757 .base.cra_blocksize = 1,
3759 .init = chcr_init_tfm,
3760 .exit = chcr_exit_tfm,
3761 .min_keysize = AES_MIN_KEY_SIZE,
3762 .max_keysize = AES_MAX_KEY_SIZE,
3763 .ivsize = AES_BLOCK_SIZE,
3764 .setkey = chcr_aes_ctr_setkey,
3765 .encrypt = chcr_aes_encrypt,
3766 .decrypt = chcr_aes_decrypt,
3770 .type = CRYPTO_ALG_TYPE_SKCIPHER |
3771 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686,
3774 .base.cra_name = "rfc3686(ctr(aes))",
3775 .base.cra_driver_name = "rfc3686-ctr-aes-chcr",
3776 .base.cra_blocksize = 1,
3778 .init = chcr_rfc3686_init,
3779 .exit = chcr_exit_tfm,
3780 .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
3781 .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
3782 .ivsize = CTR_RFC3686_IV_SIZE,
3783 .setkey = chcr_aes_rfc3686_setkey,
3784 .encrypt = chcr_aes_encrypt,
3785 .decrypt = chcr_aes_decrypt,
3790 .type = CRYPTO_ALG_TYPE_AHASH,
3793 .halg.digestsize = SHA1_DIGEST_SIZE,
3796 .cra_driver_name = "sha1-chcr",
3797 .cra_blocksize = SHA1_BLOCK_SIZE,
3802 .type = CRYPTO_ALG_TYPE_AHASH,
3805 .halg.digestsize = SHA256_DIGEST_SIZE,
3807 .cra_name = "sha256",
3808 .cra_driver_name = "sha256-chcr",
3809 .cra_blocksize = SHA256_BLOCK_SIZE,
3814 .type = CRYPTO_ALG_TYPE_AHASH,
3817 .halg.digestsize = SHA224_DIGEST_SIZE,
3819 .cra_name = "sha224",
3820 .cra_driver_name = "sha224-chcr",
3821 .cra_blocksize = SHA224_BLOCK_SIZE,
3826 .type = CRYPTO_ALG_TYPE_AHASH,
3829 .halg.digestsize = SHA384_DIGEST_SIZE,
3831 .cra_name = "sha384",
3832 .cra_driver_name = "sha384-chcr",
3833 .cra_blocksize = SHA384_BLOCK_SIZE,
3838 .type = CRYPTO_ALG_TYPE_AHASH,
3841 .halg.digestsize = SHA512_DIGEST_SIZE,
3843 .cra_name = "sha512",
3844 .cra_driver_name = "sha512-chcr",
3845 .cra_blocksize = SHA512_BLOCK_SIZE,
3851 .type = CRYPTO_ALG_TYPE_HMAC,
3854 .halg.digestsize = SHA1_DIGEST_SIZE,
3856 .cra_name = "hmac(sha1)",
3857 .cra_driver_name = "hmac-sha1-chcr",
3858 .cra_blocksize = SHA1_BLOCK_SIZE,
3863 .type = CRYPTO_ALG_TYPE_HMAC,
3866 .halg.digestsize = SHA224_DIGEST_SIZE,
3868 .cra_name = "hmac(sha224)",
3869 .cra_driver_name = "hmac-sha224-chcr",
3870 .cra_blocksize = SHA224_BLOCK_SIZE,
3875 .type = CRYPTO_ALG_TYPE_HMAC,
3878 .halg.digestsize = SHA256_DIGEST_SIZE,
3880 .cra_name = "hmac(sha256)",
3881 .cra_driver_name = "hmac-sha256-chcr",
3882 .cra_blocksize = SHA256_BLOCK_SIZE,
3887 .type = CRYPTO_ALG_TYPE_HMAC,
3890 .halg.digestsize = SHA384_DIGEST_SIZE,
3892 .cra_name = "hmac(sha384)",
3893 .cra_driver_name = "hmac-sha384-chcr",
3894 .cra_blocksize = SHA384_BLOCK_SIZE,
3899 .type = CRYPTO_ALG_TYPE_HMAC,
3902 .halg.digestsize = SHA512_DIGEST_SIZE,
3904 .cra_name = "hmac(sha512)",
3905 .cra_driver_name = "hmac-sha512-chcr",
3906 .cra_blocksize = SHA512_BLOCK_SIZE,
3910 /* Add AEAD Algorithms */
3912 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_GCM,
3916 .cra_name = "gcm(aes)",
3917 .cra_driver_name = "gcm-aes-chcr",
3919 .cra_priority = CHCR_AEAD_PRIORITY,
3920 .cra_ctxsize = sizeof(struct chcr_context) +
3921 sizeof(struct chcr_aead_ctx) +
3922 sizeof(struct chcr_gcm_ctx),
3924 .ivsize = GCM_AES_IV_SIZE,
3925 .maxauthsize = GHASH_DIGEST_SIZE,
3926 .setkey = chcr_gcm_setkey,
3927 .setauthsize = chcr_gcm_setauthsize,
3931 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106,
3935 .cra_name = "rfc4106(gcm(aes))",
3936 .cra_driver_name = "rfc4106-gcm-aes-chcr",
3938 .cra_priority = CHCR_AEAD_PRIORITY + 1,
3939 .cra_ctxsize = sizeof(struct chcr_context) +
3940 sizeof(struct chcr_aead_ctx) +
3941 sizeof(struct chcr_gcm_ctx),
3944 .ivsize = GCM_RFC4106_IV_SIZE,
3945 .maxauthsize = GHASH_DIGEST_SIZE,
3946 .setkey = chcr_gcm_setkey,
3947 .setauthsize = chcr_4106_4309_setauthsize,
3951 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_CCM,
3955 .cra_name = "ccm(aes)",
3956 .cra_driver_name = "ccm-aes-chcr",
3958 .cra_priority = CHCR_AEAD_PRIORITY,
3959 .cra_ctxsize = sizeof(struct chcr_context) +
3960 sizeof(struct chcr_aead_ctx),
3963 .ivsize = AES_BLOCK_SIZE,
3964 .maxauthsize = GHASH_DIGEST_SIZE,
3965 .setkey = chcr_aead_ccm_setkey,
3966 .setauthsize = chcr_ccm_setauthsize,
3970 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309,
3974 .cra_name = "rfc4309(ccm(aes))",
3975 .cra_driver_name = "rfc4309-ccm-aes-chcr",
3977 .cra_priority = CHCR_AEAD_PRIORITY + 1,
3978 .cra_ctxsize = sizeof(struct chcr_context) +
3979 sizeof(struct chcr_aead_ctx),
3983 .maxauthsize = GHASH_DIGEST_SIZE,
3984 .setkey = chcr_aead_rfc4309_setkey,
3985 .setauthsize = chcr_4106_4309_setauthsize,
3989 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
3993 .cra_name = "authenc(hmac(sha1),cbc(aes))",
3995 "authenc-hmac-sha1-cbc-aes-chcr",
3996 .cra_blocksize = AES_BLOCK_SIZE,
3997 .cra_priority = CHCR_AEAD_PRIORITY,
3998 .cra_ctxsize = sizeof(struct chcr_context) +
3999 sizeof(struct chcr_aead_ctx) +
4000 sizeof(struct chcr_authenc_ctx),
4003 .ivsize = AES_BLOCK_SIZE,
4004 .maxauthsize = SHA1_DIGEST_SIZE,
4005 .setkey = chcr_authenc_setkey,
4006 .setauthsize = chcr_authenc_setauthsize,
4010 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4015 .cra_name = "authenc(hmac(sha256),cbc(aes))",
4017 "authenc-hmac-sha256-cbc-aes-chcr",
4018 .cra_blocksize = AES_BLOCK_SIZE,
4019 .cra_priority = CHCR_AEAD_PRIORITY,
4020 .cra_ctxsize = sizeof(struct chcr_context) +
4021 sizeof(struct chcr_aead_ctx) +
4022 sizeof(struct chcr_authenc_ctx),
4025 .ivsize = AES_BLOCK_SIZE,
4026 .maxauthsize = SHA256_DIGEST_SIZE,
4027 .setkey = chcr_authenc_setkey,
4028 .setauthsize = chcr_authenc_setauthsize,
4032 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4036 .cra_name = "authenc(hmac(sha224),cbc(aes))",
4038 "authenc-hmac-sha224-cbc-aes-chcr",
4039 .cra_blocksize = AES_BLOCK_SIZE,
4040 .cra_priority = CHCR_AEAD_PRIORITY,
4041 .cra_ctxsize = sizeof(struct chcr_context) +
4042 sizeof(struct chcr_aead_ctx) +
4043 sizeof(struct chcr_authenc_ctx),
4045 .ivsize = AES_BLOCK_SIZE,
4046 .maxauthsize = SHA224_DIGEST_SIZE,
4047 .setkey = chcr_authenc_setkey,
4048 .setauthsize = chcr_authenc_setauthsize,
4052 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4056 .cra_name = "authenc(hmac(sha384),cbc(aes))",
4058 "authenc-hmac-sha384-cbc-aes-chcr",
4059 .cra_blocksize = AES_BLOCK_SIZE,
4060 .cra_priority = CHCR_AEAD_PRIORITY,
4061 .cra_ctxsize = sizeof(struct chcr_context) +
4062 sizeof(struct chcr_aead_ctx) +
4063 sizeof(struct chcr_authenc_ctx),
4066 .ivsize = AES_BLOCK_SIZE,
4067 .maxauthsize = SHA384_DIGEST_SIZE,
4068 .setkey = chcr_authenc_setkey,
4069 .setauthsize = chcr_authenc_setauthsize,
4073 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4077 .cra_name = "authenc(hmac(sha512),cbc(aes))",
4079 "authenc-hmac-sha512-cbc-aes-chcr",
4080 .cra_blocksize = AES_BLOCK_SIZE,
4081 .cra_priority = CHCR_AEAD_PRIORITY,
4082 .cra_ctxsize = sizeof(struct chcr_context) +
4083 sizeof(struct chcr_aead_ctx) +
4084 sizeof(struct chcr_authenc_ctx),
4087 .ivsize = AES_BLOCK_SIZE,
4088 .maxauthsize = SHA512_DIGEST_SIZE,
4089 .setkey = chcr_authenc_setkey,
4090 .setauthsize = chcr_authenc_setauthsize,
4094 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_NULL,
4098 .cra_name = "authenc(digest_null,cbc(aes))",
4100 "authenc-digest_null-cbc-aes-chcr",
4101 .cra_blocksize = AES_BLOCK_SIZE,
4102 .cra_priority = CHCR_AEAD_PRIORITY,
4103 .cra_ctxsize = sizeof(struct chcr_context) +
4104 sizeof(struct chcr_aead_ctx) +
4105 sizeof(struct chcr_authenc_ctx),
4108 .ivsize = AES_BLOCK_SIZE,
4110 .setkey = chcr_aead_digest_null_setkey,
4111 .setauthsize = chcr_authenc_null_setauthsize,
4115 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4119 .cra_name = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4121 "authenc-hmac-sha1-rfc3686-ctr-aes-chcr",
4123 .cra_priority = CHCR_AEAD_PRIORITY,
4124 .cra_ctxsize = sizeof(struct chcr_context) +
4125 sizeof(struct chcr_aead_ctx) +
4126 sizeof(struct chcr_authenc_ctx),
4129 .ivsize = CTR_RFC3686_IV_SIZE,
4130 .maxauthsize = SHA1_DIGEST_SIZE,
4131 .setkey = chcr_authenc_setkey,
4132 .setauthsize = chcr_authenc_setauthsize,
4136 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4141 .cra_name = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4143 "authenc-hmac-sha256-rfc3686-ctr-aes-chcr",
4145 .cra_priority = CHCR_AEAD_PRIORITY,
4146 .cra_ctxsize = sizeof(struct chcr_context) +
4147 sizeof(struct chcr_aead_ctx) +
4148 sizeof(struct chcr_authenc_ctx),
4151 .ivsize = CTR_RFC3686_IV_SIZE,
4152 .maxauthsize = SHA256_DIGEST_SIZE,
4153 .setkey = chcr_authenc_setkey,
4154 .setauthsize = chcr_authenc_setauthsize,
4158 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4162 .cra_name = "authenc(hmac(sha224),rfc3686(ctr(aes)))",
4164 "authenc-hmac-sha224-rfc3686-ctr-aes-chcr",
4166 .cra_priority = CHCR_AEAD_PRIORITY,
4167 .cra_ctxsize = sizeof(struct chcr_context) +
4168 sizeof(struct chcr_aead_ctx) +
4169 sizeof(struct chcr_authenc_ctx),
4171 .ivsize = CTR_RFC3686_IV_SIZE,
4172 .maxauthsize = SHA224_DIGEST_SIZE,
4173 .setkey = chcr_authenc_setkey,
4174 .setauthsize = chcr_authenc_setauthsize,
4178 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4182 .cra_name = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4184 "authenc-hmac-sha384-rfc3686-ctr-aes-chcr",
4186 .cra_priority = CHCR_AEAD_PRIORITY,
4187 .cra_ctxsize = sizeof(struct chcr_context) +
4188 sizeof(struct chcr_aead_ctx) +
4189 sizeof(struct chcr_authenc_ctx),
4192 .ivsize = CTR_RFC3686_IV_SIZE,
4193 .maxauthsize = SHA384_DIGEST_SIZE,
4194 .setkey = chcr_authenc_setkey,
4195 .setauthsize = chcr_authenc_setauthsize,
4199 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4203 .cra_name = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4205 "authenc-hmac-sha512-rfc3686-ctr-aes-chcr",
4207 .cra_priority = CHCR_AEAD_PRIORITY,
4208 .cra_ctxsize = sizeof(struct chcr_context) +
4209 sizeof(struct chcr_aead_ctx) +
4210 sizeof(struct chcr_authenc_ctx),
4213 .ivsize = CTR_RFC3686_IV_SIZE,
4214 .maxauthsize = SHA512_DIGEST_SIZE,
4215 .setkey = chcr_authenc_setkey,
4216 .setauthsize = chcr_authenc_setauthsize,
4220 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_NULL,
4224 .cra_name = "authenc(digest_null,rfc3686(ctr(aes)))",
4226 "authenc-digest_null-rfc3686-ctr-aes-chcr",
4228 .cra_priority = CHCR_AEAD_PRIORITY,
4229 .cra_ctxsize = sizeof(struct chcr_context) +
4230 sizeof(struct chcr_aead_ctx) +
4231 sizeof(struct chcr_authenc_ctx),
4234 .ivsize = CTR_RFC3686_IV_SIZE,
4236 .setkey = chcr_aead_digest_null_setkey,
4237 .setauthsize = chcr_authenc_null_setauthsize,
4243 * chcr_unregister_alg - Deregister crypto algorithms with
4246 static int chcr_unregister_alg(void)
4250 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4251 switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
4252 case CRYPTO_ALG_TYPE_SKCIPHER:
4253 if (driver_algs[i].is_registered)
4254 crypto_unregister_skcipher(
4255 &driver_algs[i].alg.skcipher);
4257 case CRYPTO_ALG_TYPE_AEAD:
4258 if (driver_algs[i].is_registered)
4259 crypto_unregister_aead(
4260 &driver_algs[i].alg.aead);
4262 case CRYPTO_ALG_TYPE_AHASH:
4263 if (driver_algs[i].is_registered)
4264 crypto_unregister_ahash(
4265 &driver_algs[i].alg.hash);
4268 driver_algs[i].is_registered = 0;
4273 #define SZ_AHASH_CTX sizeof(struct chcr_context)
4274 #define SZ_AHASH_H_CTX (sizeof(struct chcr_context) + sizeof(struct hmac_ctx))
4275 #define SZ_AHASH_REQ_CTX sizeof(struct chcr_ahash_req_ctx)
4278 * chcr_register_alg - Register crypto algorithms with kernel framework.
4280 static int chcr_register_alg(void)
4282 struct crypto_alg ai;
4283 struct ahash_alg *a_hash;
4287 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4288 if (driver_algs[i].is_registered)
4290 switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
4291 case CRYPTO_ALG_TYPE_SKCIPHER:
4292 driver_algs[i].alg.skcipher.base.cra_priority =
4294 driver_algs[i].alg.skcipher.base.cra_module = THIS_MODULE;
4295 driver_algs[i].alg.skcipher.base.cra_flags =
4296 CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_ASYNC |
4297 CRYPTO_ALG_NEED_FALLBACK;
4298 driver_algs[i].alg.skcipher.base.cra_ctxsize =
4299 sizeof(struct chcr_context) +
4300 sizeof(struct ablk_ctx);
4301 driver_algs[i].alg.skcipher.base.cra_alignmask = 0;
4303 err = crypto_register_skcipher(&driver_algs[i].alg.skcipher);
4304 name = driver_algs[i].alg.skcipher.base.cra_driver_name;
4306 case CRYPTO_ALG_TYPE_AEAD:
4307 driver_algs[i].alg.aead.base.cra_flags =
4308 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK;
4309 driver_algs[i].alg.aead.encrypt = chcr_aead_encrypt;
4310 driver_algs[i].alg.aead.decrypt = chcr_aead_decrypt;
4311 driver_algs[i].alg.aead.init = chcr_aead_cra_init;
4312 driver_algs[i].alg.aead.exit = chcr_aead_cra_exit;
4313 driver_algs[i].alg.aead.base.cra_module = THIS_MODULE;
4314 err = crypto_register_aead(&driver_algs[i].alg.aead);
4315 name = driver_algs[i].alg.aead.base.cra_driver_name;
4317 case CRYPTO_ALG_TYPE_AHASH:
4318 a_hash = &driver_algs[i].alg.hash;
4319 a_hash->update = chcr_ahash_update;
4320 a_hash->final = chcr_ahash_final;
4321 a_hash->finup = chcr_ahash_finup;
4322 a_hash->digest = chcr_ahash_digest;
4323 a_hash->export = chcr_ahash_export;
4324 a_hash->import = chcr_ahash_import;
4325 a_hash->halg.statesize = SZ_AHASH_REQ_CTX;
4326 a_hash->halg.base.cra_priority = CHCR_CRA_PRIORITY;
4327 a_hash->halg.base.cra_module = THIS_MODULE;
4328 a_hash->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
4329 a_hash->halg.base.cra_alignmask = 0;
4330 a_hash->halg.base.cra_exit = NULL;
4332 if (driver_algs[i].type == CRYPTO_ALG_TYPE_HMAC) {
4333 a_hash->halg.base.cra_init = chcr_hmac_cra_init;
4334 a_hash->halg.base.cra_exit = chcr_hmac_cra_exit;
4335 a_hash->init = chcr_hmac_init;
4336 a_hash->setkey = chcr_ahash_setkey;
4337 a_hash->halg.base.cra_ctxsize = SZ_AHASH_H_CTX;
4339 a_hash->init = chcr_sha_init;
4340 a_hash->halg.base.cra_ctxsize = SZ_AHASH_CTX;
4341 a_hash->halg.base.cra_init = chcr_sha_cra_init;
4343 err = crypto_register_ahash(&driver_algs[i].alg.hash);
4344 ai = driver_algs[i].alg.hash.halg.base;
4345 name = ai.cra_driver_name;
4349 pr_err("chcr : %s : Algorithm registration failed\n",
4353 driver_algs[i].is_registered = 1;
4359 chcr_unregister_alg();
4364 * start_crypto - Register the crypto algorithms.
4365 * This should called once when the first device comesup. After this
4366 * kernel will start calling driver APIs for crypto operations.
4368 int start_crypto(void)
4370 return chcr_register_alg();
4374 * stop_crypto - Deregister all the crypto algorithms with kernel.
4375 * This should be called once when the last device goes down. After this
4376 * kernel will not call the driver API for crypto operations.
4378 int stop_crypto(void)
4380 chcr_unregister_alg();