blob: 2d30ed5a2674b305fa615287875e34485377b169 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
2 * This file is part of the Chelsio T6 Crypto driver for Linux.
3 *
4 * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
5 *
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:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
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.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 *
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)
40 */
41
42#define pr_fmt(fmt) "chcr:" fmt
43
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>
52
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>
67
68#include "t4fw_api.h"
69#include "t4_msg.h"
70#include "chcr_core.h"
71#include "chcr_algo.h"
72#include "chcr_crypto.h"
73
74#define IV AES_BLOCK_SIZE
75
76static 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
81};
82
83static 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
88};
89
90static u32 round_constant[11] = {
91 0x01000000, 0x02000000, 0x04000000, 0x08000000,
92 0x10000000, 0x20000000, 0x40000000, 0x80000000,
93 0x1B000000, 0x36000000, 0x6C000000
94};
95
96static int chcr_handle_cipher_resp(struct ablkcipher_request *req,
97 unsigned char *input, int err);
98
99static inline struct chcr_aead_ctx *AEAD_CTX(struct chcr_context *ctx)
100{
101 return ctx->crypto_ctx->aeadctx;
102}
103
104static inline struct ablk_ctx *ABLK_CTX(struct chcr_context *ctx)
105{
106 return ctx->crypto_ctx->ablkctx;
107}
108
109static inline struct hmac_ctx *HMAC_CTX(struct chcr_context *ctx)
110{
111 return ctx->crypto_ctx->hmacctx;
112}
113
114static inline struct chcr_gcm_ctx *GCM_CTX(struct chcr_aead_ctx *gctx)
115{
116 return gctx->ctx->gcm;
117}
118
119static inline struct chcr_authenc_ctx *AUTHENC_CTX(struct chcr_aead_ctx *gctx)
120{
121 return gctx->ctx->authenc;
122}
123
124static inline struct uld_ctx *ULD_CTX(struct chcr_context *ctx)
125{
David Brazdil0f672f62019-12-10 10:32:29 +0000126 return container_of(ctx->dev, struct uld_ctx, dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000127}
128
129static inline int is_ofld_imm(const struct sk_buff *skb)
130{
131 return (skb->len <= SGE_MAX_WR_LEN);
132}
133
134static inline void chcr_init_hctx_per_wr(struct chcr_ahash_req_ctx *reqctx)
135{
136 memset(&reqctx->hctx_wr, 0, sizeof(struct chcr_hctx_per_wr));
137}
138
139static int sg_nents_xlen(struct scatterlist *sg, unsigned int reqlen,
140 unsigned int entlen,
141 unsigned int skip)
142{
143 int nents = 0;
144 unsigned int less;
145 unsigned int skip_len = 0;
146
147 while (sg && skip) {
148 if (sg_dma_len(sg) <= skip) {
149 skip -= sg_dma_len(sg);
150 skip_len = 0;
151 sg = sg_next(sg);
152 } else {
153 skip_len = skip;
154 skip = 0;
155 }
156 }
157
158 while (sg && reqlen) {
159 less = min(reqlen, sg_dma_len(sg) - skip_len);
160 nents += DIV_ROUND_UP(less, entlen);
161 reqlen -= less;
162 skip_len = 0;
163 sg = sg_next(sg);
164 }
165 return nents;
166}
167
168static inline int get_aead_subtype(struct crypto_aead *aead)
169{
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;
174}
175
176void chcr_verify_tag(struct aead_request *req, u8 *input, int *err)
177{
178 u8 temp[SHA512_DIGEST_SIZE];
179 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
180 int authsize = crypto_aead_authsize(tfm);
181 struct cpl_fw6_pld *fw6_pld;
182 int cmp = 0;
183
184 fw6_pld = (struct cpl_fw6_pld *)input;
185 if ((get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) ||
186 (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_GCM)) {
187 cmp = crypto_memneq(&fw6_pld->data[2], (fw6_pld + 1), authsize);
188 } else {
189
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);
194 }
195 if (cmp)
196 *err = -EBADMSG;
197 else
198 *err = 0;
199}
200
David Brazdil0f672f62019-12-10 10:32:29 +0000201static int chcr_inc_wrcount(struct chcr_dev *dev)
202{
203 if (dev->state == CHCR_DETACH)
204 return 1;
205 atomic_inc(&dev->inflight);
206 return 0;
207}
208
209static inline void chcr_dec_wrcount(struct chcr_dev *dev)
210{
211 atomic_dec(&dev->inflight);
212}
213
214static inline int chcr_handle_aead_resp(struct aead_request *req,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000215 unsigned char *input,
216 int err)
217{
218 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
David Brazdil0f672f62019-12-10 10:32:29 +0000219 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
220 struct chcr_dev *dev = a_ctx(tfm)->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000221
222 chcr_aead_common_exit(req);
223 if (reqctx->verify == VERIFY_SW) {
224 chcr_verify_tag(req, input, &err);
225 reqctx->verify = VERIFY_HW;
226 }
David Brazdil0f672f62019-12-10 10:32:29 +0000227 chcr_dec_wrcount(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000228 req->base.complete(&req->base, err);
David Brazdil0f672f62019-12-10 10:32:29 +0000229
230 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231}
232
233static void get_aes_decrypt_key(unsigned char *dec_key,
234 const unsigned char *key,
235 unsigned int keylength)
236{
237 u32 temp;
238 u32 w_ring[MAX_NK];
239 int i, j, k;
240 u8 nr, nk;
241
242 switch (keylength) {
243 case AES_KEYLENGTH_128BIT:
244 nk = KEYLENGTH_4BYTES;
245 nr = NUMBER_OF_ROUNDS_10;
246 break;
247 case AES_KEYLENGTH_192BIT:
248 nk = KEYLENGTH_6BYTES;
249 nr = NUMBER_OF_ROUNDS_12;
250 break;
251 case AES_KEYLENGTH_256BIT:
252 nk = KEYLENGTH_8BYTES;
253 nr = NUMBER_OF_ROUNDS_14;
254 break;
255 default:
256 return;
257 }
258 for (i = 0; i < nk; i++)
259 w_ring[i] = be32_to_cpu(*(u32 *)&key[4 * i]);
260
261 i = 0;
262 temp = w_ring[nk - 1];
263 while (i + nk < (nr + 1) * 4) {
264 if (!(i % nk)) {
265 /* RotWord(temp) */
266 temp = (temp << 8) | (temp >> 24);
267 temp = aes_ks_subword(temp);
268 temp ^= round_constant[i / nk];
269 } else if (nk == 8 && (i % 4 == 0)) {
270 temp = aes_ks_subword(temp);
271 }
272 w_ring[i % nk] ^= temp;
273 temp = w_ring[i % nk];
274 i++;
275 }
276 i--;
277 for (k = 0, j = i % nk; k < nk; k++) {
278 *((u32 *)dec_key + k) = htonl(w_ring[j]);
279 j--;
280 if (j < 0)
281 j += nk;
282 }
283}
284
285static struct crypto_shash *chcr_alloc_shash(unsigned int ds)
286{
287 struct crypto_shash *base_hash = ERR_PTR(-EINVAL);
288
289 switch (ds) {
290 case SHA1_DIGEST_SIZE:
291 base_hash = crypto_alloc_shash("sha1", 0, 0);
292 break;
293 case SHA224_DIGEST_SIZE:
294 base_hash = crypto_alloc_shash("sha224", 0, 0);
295 break;
296 case SHA256_DIGEST_SIZE:
297 base_hash = crypto_alloc_shash("sha256", 0, 0);
298 break;
299 case SHA384_DIGEST_SIZE:
300 base_hash = crypto_alloc_shash("sha384", 0, 0);
301 break;
302 case SHA512_DIGEST_SIZE:
303 base_hash = crypto_alloc_shash("sha512", 0, 0);
304 break;
305 }
306
307 return base_hash;
308}
309
310static int chcr_compute_partial_hash(struct shash_desc *desc,
311 char *iopad, char *result_hash,
312 int digest_size)
313{
314 struct sha1_state sha1_st;
315 struct sha256_state sha256_st;
316 struct sha512_state sha512_st;
317 int error;
318
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);
329
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);
335
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);
341
342 } else if (digest_size == SHA512_DIGEST_SIZE) {
343 error = crypto_shash_init(desc) ?:
344 crypto_shash_update(desc, iopad, SHA512_BLOCK_SIZE) ?:
345 crypto_shash_export(desc, (void *)&sha512_st);
346 memcpy(result_hash, sha512_st.state, SHA512_DIGEST_SIZE);
347 } else {
348 error = -EINVAL;
349 pr_err("Unknown digest size %d\n", digest_size);
350 }
351 return error;
352}
353
354static void chcr_change_order(char *buf, int ds)
355{
356 int i;
357
358 if (ds == SHA512_DIGEST_SIZE) {
359 for (i = 0; i < (ds / sizeof(u64)); i++)
360 *((__be64 *)buf + i) =
361 cpu_to_be64(*((u64 *)buf + i));
362 } else {
363 for (i = 0; i < (ds / sizeof(u32)); i++)
364 *((__be32 *)buf + i) =
365 cpu_to_be32(*((u32 *)buf + i));
366 }
367}
368
369static inline int is_hmac(struct crypto_tfm *tfm)
370{
371 struct crypto_alg *alg = tfm->__crt_alg;
372 struct chcr_alg_template *chcr_crypto_alg =
373 container_of(__crypto_ahash_alg(alg), struct chcr_alg_template,
374 alg.hash);
375 if (chcr_crypto_alg->type == CRYPTO_ALG_TYPE_HMAC)
376 return 1;
377 return 0;
378}
379
380static inline void dsgl_walk_init(struct dsgl_walk *walk,
381 struct cpl_rx_phys_dsgl *dsgl)
382{
383 walk->dsgl = dsgl;
384 walk->nents = 0;
385 walk->to = (struct phys_sge_pairs *)(dsgl + 1);
386}
387
388static inline void dsgl_walk_end(struct dsgl_walk *walk, unsigned short qid,
389 int pci_chan_id)
390{
391 struct cpl_rx_phys_dsgl *phys_cpl;
392
393 phys_cpl = walk->dsgl;
394
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;
408}
409
410static inline void dsgl_walk_add_page(struct dsgl_walk *walk,
411 size_t size,
David Brazdil0f672f62019-12-10 10:32:29 +0000412 dma_addr_t addr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000413{
414 int j;
415
416 if (!size)
417 return;
418 j = walk->nents;
419 walk->to->len[j % 8] = htons(size);
David Brazdil0f672f62019-12-10 10:32:29 +0000420 walk->to->addr[j % 8] = cpu_to_be64(addr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000421 j++;
422 if ((j % 8) == 0)
423 walk->to++;
424 walk->nents = j;
425}
426
427static void dsgl_walk_add_sg(struct dsgl_walk *walk,
428 struct scatterlist *sg,
429 unsigned int slen,
430 unsigned int skip)
431{
432 int skip_len = 0;
433 unsigned int left_size = slen, len = 0;
434 unsigned int j = walk->nents;
435 int offset, ent_len;
436
437 if (!slen)
438 return;
439 while (sg && skip) {
440 if (sg_dma_len(sg) <= skip) {
441 skip -= sg_dma_len(sg);
442 skip_len = 0;
443 sg = sg_next(sg);
444 } else {
445 skip_len = skip;
446 skip = 0;
447 }
448 }
449
450 while (left_size && sg) {
451 len = min_t(u32, left_size, sg_dma_len(sg) - skip_len);
452 offset = 0;
453 while (len) {
454 ent_len = min_t(u32, len, CHCR_DST_SG_SIZE);
455 walk->to->len[j % 8] = htons(ent_len);
456 walk->to->addr[j % 8] = cpu_to_be64(sg_dma_address(sg) +
457 offset + skip_len);
458 offset += ent_len;
459 len -= ent_len;
460 j++;
461 if ((j % 8) == 0)
462 walk->to++;
463 }
464 walk->last_sg = sg;
465 walk->last_sg_len = min_t(u32, left_size, sg_dma_len(sg) -
466 skip_len) + skip_len;
467 left_size -= min_t(u32, left_size, sg_dma_len(sg) - skip_len);
468 skip_len = 0;
469 sg = sg_next(sg);
470 }
471 walk->nents = j;
472}
473
474static inline void ulptx_walk_init(struct ulptx_walk *walk,
475 struct ulptx_sgl *ulp)
476{
477 walk->sgl = ulp;
478 walk->nents = 0;
479 walk->pair_idx = 0;
480 walk->pair = ulp->sge;
481 walk->last_sg = NULL;
482 walk->last_sg_len = 0;
483}
484
485static inline void ulptx_walk_end(struct ulptx_walk *walk)
486{
487 walk->sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
488 ULPTX_NSGE_V(walk->nents));
489}
490
491
492static inline void ulptx_walk_add_page(struct ulptx_walk *walk,
493 size_t size,
David Brazdil0f672f62019-12-10 10:32:29 +0000494 dma_addr_t addr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000495{
496 if (!size)
497 return;
498
499 if (walk->nents == 0) {
500 walk->sgl->len0 = cpu_to_be32(size);
David Brazdil0f672f62019-12-10 10:32:29 +0000501 walk->sgl->addr0 = cpu_to_be64(addr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000502 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000503 walk->pair->addr[walk->pair_idx] = cpu_to_be64(addr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000504 walk->pair->len[walk->pair_idx] = cpu_to_be32(size);
505 walk->pair_idx = !walk->pair_idx;
506 if (!walk->pair_idx)
507 walk->pair++;
508 }
509 walk->nents++;
510}
511
512static void ulptx_walk_add_sg(struct ulptx_walk *walk,
513 struct scatterlist *sg,
514 unsigned int len,
515 unsigned int skip)
516{
517 int small;
518 int skip_len = 0;
519 unsigned int sgmin;
520
521 if (!len)
522 return;
523 while (sg && skip) {
524 if (sg_dma_len(sg) <= skip) {
525 skip -= sg_dma_len(sg);
526 skip_len = 0;
527 sg = sg_next(sg);
528 } else {
529 skip_len = skip;
530 skip = 0;
531 }
532 }
533 WARN(!sg, "SG should not be null here\n");
534 if (sg && (walk->nents == 0)) {
535 small = min_t(unsigned int, sg_dma_len(sg) - skip_len, len);
536 sgmin = min_t(unsigned int, small, CHCR_SRC_SG_SIZE);
537 walk->sgl->len0 = cpu_to_be32(sgmin);
538 walk->sgl->addr0 = cpu_to_be64(sg_dma_address(sg) + skip_len);
539 walk->nents++;
540 len -= sgmin;
541 walk->last_sg = sg;
542 walk->last_sg_len = sgmin + skip_len;
543 skip_len += sgmin;
544 if (sg_dma_len(sg) == skip_len) {
545 sg = sg_next(sg);
546 skip_len = 0;
547 }
548 }
549
550 while (sg && len) {
551 small = min(sg_dma_len(sg) - skip_len, len);
552 sgmin = min_t(unsigned int, small, CHCR_SRC_SG_SIZE);
553 walk->pair->len[walk->pair_idx] = cpu_to_be32(sgmin);
554 walk->pair->addr[walk->pair_idx] =
555 cpu_to_be64(sg_dma_address(sg) + skip_len);
556 walk->pair_idx = !walk->pair_idx;
557 walk->nents++;
558 if (!walk->pair_idx)
559 walk->pair++;
560 len -= sgmin;
561 skip_len += sgmin;
562 walk->last_sg = sg;
563 walk->last_sg_len = skip_len;
564 if (sg_dma_len(sg) == skip_len) {
565 sg = sg_next(sg);
566 skip_len = 0;
567 }
568 }
569}
570
571static inline int get_cryptoalg_subtype(struct crypto_tfm *tfm)
572{
573 struct crypto_alg *alg = tfm->__crt_alg;
574 struct chcr_alg_template *chcr_crypto_alg =
575 container_of(alg, struct chcr_alg_template, alg.crypto);
576
577 return chcr_crypto_alg->type & CRYPTO_ALG_SUB_TYPE_MASK;
578}
579
580static int cxgb4_is_crypto_q_full(struct net_device *dev, unsigned int idx)
581{
582 struct adapter *adap = netdev2adap(dev);
583 struct sge_uld_txq_info *txq_info =
584 adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
585 struct sge_uld_txq *txq;
586 int ret = 0;
587
588 local_bh_disable();
589 txq = &txq_info->uldtxq[idx];
590 spin_lock(&txq->sendq.lock);
591 if (txq->full)
592 ret = -1;
593 spin_unlock(&txq->sendq.lock);
594 local_bh_enable();
595 return ret;
596}
597
598static int generate_copy_rrkey(struct ablk_ctx *ablkctx,
599 struct _key_ctx *key_ctx)
600{
601 if (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) {
602 memcpy(key_ctx->key, ablkctx->rrkey, ablkctx->enckey_len);
603 } else {
604 memcpy(key_ctx->key,
605 ablkctx->key + (ablkctx->enckey_len >> 1),
606 ablkctx->enckey_len >> 1);
607 memcpy(key_ctx->key + (ablkctx->enckey_len >> 1),
608 ablkctx->rrkey, ablkctx->enckey_len >> 1);
609 }
610 return 0;
611}
612
613static int chcr_hash_ent_in_wr(struct scatterlist *src,
614 unsigned int minsg,
615 unsigned int space,
616 unsigned int srcskip)
617{
618 int srclen = 0;
619 int srcsg = minsg;
620 int soffset = 0, sless;
621
622 if (sg_dma_len(src) == srcskip) {
623 src = sg_next(src);
624 srcskip = 0;
625 }
626 while (src && space > (sgl_ent_len[srcsg + 1])) {
627 sless = min_t(unsigned int, sg_dma_len(src) - soffset - srcskip,
628 CHCR_SRC_SG_SIZE);
629 srclen += sless;
630 soffset += sless;
631 srcsg++;
632 if (sg_dma_len(src) == (soffset + srcskip)) {
633 src = sg_next(src);
634 soffset = 0;
635 srcskip = 0;
636 }
637 }
638 return srclen;
639}
640
641static int chcr_sg_ent_in_wr(struct scatterlist *src,
642 struct scatterlist *dst,
643 unsigned int minsg,
644 unsigned int space,
645 unsigned int srcskip,
646 unsigned int dstskip)
647{
648 int srclen = 0, dstlen = 0;
649 int srcsg = minsg, dstsg = minsg;
650 int offset = 0, soffset = 0, less, sless = 0;
651
652 if (sg_dma_len(src) == srcskip) {
653 src = sg_next(src);
654 srcskip = 0;
655 }
656 if (sg_dma_len(dst) == dstskip) {
657 dst = sg_next(dst);
658 dstskip = 0;
659 }
660
661 while (src && dst &&
662 space > (sgl_ent_len[srcsg + 1] + dsgl_ent_len[dstsg])) {
663 sless = min_t(unsigned int, sg_dma_len(src) - srcskip - soffset,
664 CHCR_SRC_SG_SIZE);
665 srclen += sless;
666 srcsg++;
667 offset = 0;
668 while (dst && ((dstsg + 1) <= MAX_DSGL_ENT) &&
669 space > (sgl_ent_len[srcsg] + dsgl_ent_len[dstsg + 1])) {
670 if (srclen <= dstlen)
671 break;
672 less = min_t(unsigned int, sg_dma_len(dst) - offset -
673 dstskip, CHCR_DST_SG_SIZE);
674 dstlen += less;
675 offset += less;
676 if ((offset + dstskip) == sg_dma_len(dst)) {
677 dst = sg_next(dst);
678 offset = 0;
679 }
680 dstsg++;
681 dstskip = 0;
682 }
683 soffset += sless;
684 if ((soffset + srcskip) == sg_dma_len(src)) {
685 src = sg_next(src);
686 srcskip = 0;
687 soffset = 0;
688 }
689
690 }
691 return min(srclen, dstlen);
692}
693
David Brazdil0f672f62019-12-10 10:32:29 +0000694static int chcr_cipher_fallback(struct crypto_sync_skcipher *cipher,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000695 u32 flags,
696 struct scatterlist *src,
697 struct scatterlist *dst,
698 unsigned int nbytes,
699 u8 *iv,
700 unsigned short op_type)
701{
702 int err;
703
David Brazdil0f672f62019-12-10 10:32:29 +0000704 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, cipher);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000705
David Brazdil0f672f62019-12-10 10:32:29 +0000706 skcipher_request_set_sync_tfm(subreq, cipher);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000707 skcipher_request_set_callback(subreq, flags, NULL, NULL);
708 skcipher_request_set_crypt(subreq, src, dst,
709 nbytes, iv);
710
711 err = op_type ? crypto_skcipher_decrypt(subreq) :
712 crypto_skcipher_encrypt(subreq);
713 skcipher_request_zero(subreq);
714
715 return err;
716
717}
718static inline void create_wreq(struct chcr_context *ctx,
719 struct chcr_wr *chcr_req,
720 struct crypto_async_request *req,
721 unsigned int imm,
722 int hash_sz,
723 unsigned int len16,
724 unsigned int sc_len,
725 unsigned int lcb)
726{
727 struct uld_ctx *u_ctx = ULD_CTX(ctx);
728 int qid = u_ctx->lldi.rxq_ids[ctx->rx_qidx];
729
730
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 =
David Brazdil0f672f62019-12-10 10:32:29 +0000738 FILL_WR_RX_Q_ID(ctx->tx_chan_id, qid,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000739 !!lcb, ctx->tx_qidx);
740
741 chcr_req->ulptx.cmd_dest = FILL_ULPTX_CMD_DEST(ctx->tx_chan_id,
742 qid);
743 chcr_req->ulptx.len = htonl((DIV_ROUND_UP(len16, 16) -
744 ((sizeof(chcr_req->wreq)) >> 4)));
745
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);
749}
750
751/**
752 * create_cipher_wr - form the WR for cipher operations
753 * @req: cipher req.
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
757 */
758static struct sk_buff *create_cipher_wr(struct cipher_wr_param *wrparam)
759{
760 struct crypto_ablkcipher *tfm = crypto_ablkcipher_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_blkcipher_req_ctx *reqctx =
767 ablkcipher_request_ctx(wrparam->req);
768 unsigned int temp = 0, transhdr_len, dst_size;
769 int error;
770 int nents;
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);
775
776 nents = sg_nents_xlen(reqctx->dstsg, wrparam->bytes, CHCR_DST_SG_SIZE,
777 reqctx->dst_ofst);
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);
788 if (!skb) {
789 error = -ENOMEM;
790 goto err;
791 }
792 chcr_req = __skb_put_zero(skb, transhdr_len);
793 chcr_req->sec_cpl.op_ivinsrtofst =
David Brazdil0f672f62019-12-10 10:32:29 +0000794 FILL_SEC_CPL_OP_IVINSR(c_ctx(tfm)->tx_chan_id, 2, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000795
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);
799
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,
803 ablkctx->ciph_mode,
804 0, 0, IV >> 1);
805 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 0,
806 0, 1, dst_size);
807
808 chcr_req->key_ctx.ctx_hdr = ablkctx->key_ctx_hdr;
809 if ((reqctx->op == CHCR_DECRYPT_OP) &&
810 (!(get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
811 CRYPTO_ALG_SUB_TYPE_CTR)) &&
812 (!(get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
813 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686))) {
814 generate_copy_rrkey(ablkctx, &chcr_req->key_ctx);
815 } else {
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);
820 } else {
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),
826 ablkctx->key,
827 ablkctx->enckey_len >> 1);
828 }
829 }
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);
834
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,
839 transhdr_len, temp,
840 ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC);
841 reqctx->skb = skb;
842
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->info, 16,
847 reqctx->processed + wrparam->bytes - AES_BLOCK_SIZE);
848
849 return skb;
850err:
851 return ERR_PTR(error);
852}
853
854static inline int chcr_keyctx_ck_size(unsigned int keylen)
855{
856 int ck_size = 0;
857
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;
864 else
865 ck_size = 0;
866
867 return ck_size;
868}
869static int chcr_cipher_fallback_setkey(struct crypto_ablkcipher *cipher,
870 const u8 *key,
871 unsigned int keylen)
872{
873 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
874 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
875 int err = 0;
876
David Brazdil0f672f62019-12-10 10:32:29 +0000877 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000882 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
883 tfm->crt_flags |=
David Brazdil0f672f62019-12-10 10:32:29 +0000884 crypto_sync_skcipher_get_flags(ablkctx->sw_cipher) &
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000885 CRYPTO_TFM_RES_MASK;
886 return err;
887}
888
889static int chcr_aes_cbc_setkey(struct crypto_ablkcipher *cipher,
890 const u8 *key,
891 unsigned int keylen)
892{
893 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
894 unsigned int ck_size, context_size;
895 u16 alignment = 0;
896 int err;
897
898 err = chcr_cipher_fallback_setkey(cipher, key, keylen);
899 if (err)
900 goto badkey_err;
901
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;
909
910 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
911 0, 0, context_size);
912 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CBC;
913 return 0;
914badkey_err:
915 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
916 ablkctx->enckey_len = 0;
917
918 return err;
919}
920
921static int chcr_aes_ctr_setkey(struct crypto_ablkcipher *cipher,
922 const u8 *key,
923 unsigned int keylen)
924{
925 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
926 unsigned int ck_size, context_size;
927 u16 alignment = 0;
928 int err;
929
930 err = chcr_cipher_fallback_setkey(cipher, key, keylen);
931 if (err)
932 goto badkey_err;
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;
939
940 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
941 0, 0, context_size);
942 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR;
943
944 return 0;
945badkey_err:
946 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
947 ablkctx->enckey_len = 0;
948
949 return err;
950}
951
952static int chcr_aes_rfc3686_setkey(struct crypto_ablkcipher *cipher,
953 const u8 *key,
954 unsigned int keylen)
955{
956 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
957 unsigned int ck_size, context_size;
958 u16 alignment = 0;
959 int err;
960
961 if (keylen < CTR_RFC3686_NONCE_SIZE)
962 return -EINVAL;
963 memcpy(ablkctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE),
964 CTR_RFC3686_NONCE_SIZE);
965
966 keylen -= CTR_RFC3686_NONCE_SIZE;
967 err = chcr_cipher_fallback_setkey(cipher, key, keylen);
968 if (err)
969 goto badkey_err;
970
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;
977
978 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
979 0, 0, context_size);
980 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR;
981
982 return 0;
983badkey_err:
984 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
985 ablkctx->enckey_len = 0;
986
987 return err;
988}
989static void ctr_add_iv(u8 *dstiv, u8 *srciv, u32 add)
990{
991 unsigned int size = AES_BLOCK_SIZE;
992 __be32 *b = (__be32 *)(dstiv + size);
993 u32 c, prev;
994
995 memcpy(dstiv, srciv, AES_BLOCK_SIZE);
996 for (; size >= 4; size -= 4) {
997 prev = be32_to_cpu(*--b);
998 c = prev + add;
999 *b = cpu_to_be32(c);
1000 if (prev < c)
1001 break;
1002 add = 1;
1003 }
1004
1005}
1006
1007static unsigned int adjust_ctr_overflow(u8 *iv, u32 bytes)
1008{
1009 __be32 *b = (__be32 *)(iv + AES_BLOCK_SIZE);
1010 u64 c;
1011 u32 temp = be32_to_cpu(*--b);
1012
1013 temp = ~temp;
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;
1017 return bytes;
1018}
1019
1020static int chcr_update_tweak(struct ablkcipher_request *req, u8 *iv,
1021 u32 isfinal)
1022{
1023 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
1024 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
1025 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
David Brazdil0f672f62019-12-10 10:32:29 +00001026 struct crypto_aes_ctx aes;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001027 int ret, i;
1028 u8 *key;
1029 unsigned int keylen;
1030 int round = reqctx->last_req_len / AES_BLOCK_SIZE;
1031 int round8 = round / 8;
1032
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001033 memcpy(iv, reqctx->iv, AES_BLOCK_SIZE);
1034
1035 keylen = ablkctx->enckey_len / 2;
1036 key = ablkctx->key + keylen;
David Brazdil0f672f62019-12-10 10:32:29 +00001037 ret = aes_expandkey(&aes, key, keylen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001038 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +00001039 return ret;
1040 aes_encrypt(&aes, iv, iv);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001041 for (i = 0; i < round8; i++)
1042 gf128mul_x8_ble((le128 *)iv, (le128 *)iv);
1043
1044 for (i = 0; i < (round % 8); i++)
1045 gf128mul_x_ble((le128 *)iv, (le128 *)iv);
1046
1047 if (!isfinal)
David Brazdil0f672f62019-12-10 10:32:29 +00001048 aes_decrypt(&aes, iv, iv);
1049
1050 memzero_explicit(&aes, sizeof(aes));
1051 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001052}
1053
1054static int chcr_update_cipher_iv(struct ablkcipher_request *req,
1055 struct cpl_fw6_pld *fw6_pld, u8 *iv)
1056{
1057 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
1058 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
1059 int subtype = get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm));
1060 int ret = 0;
1061
1062 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR)
1063 ctr_add_iv(iv, req->info, (reqctx->processed /
1064 AES_BLOCK_SIZE));
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) {
1072 if (reqctx->op)
1073 /*Updated before sending last WR*/
1074 memcpy(iv, req->info, AES_BLOCK_SIZE);
1075 else
1076 memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE);
1077 }
1078
1079 return ret;
1080
1081}
1082
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
1086 */
1087
1088static int chcr_final_cipher_iv(struct ablkcipher_request *req,
1089 struct cpl_fw6_pld *fw6_pld, u8 *iv)
1090{
1091 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
1092 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
1093 int subtype = get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm));
1094 int ret = 0;
1095
1096 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR)
David Brazdil0f672f62019-12-10 10:32:29 +00001097 ctr_add_iv(iv, req->info, DIV_ROUND_UP(reqctx->processed,
1098 AES_BLOCK_SIZE));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001099 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*/
1103 if (!reqctx->op)
1104 memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE);
1105
1106 }
1107 return ret;
1108
1109}
1110
1111static int chcr_handle_cipher_resp(struct ablkcipher_request *req,
1112 unsigned char *input, int err)
1113{
1114 struct crypto_ablkcipher *tfm = crypto_ablkcipher_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_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
1120 struct cipher_wr_param wrparam;
David Brazdil0f672f62019-12-10 10:32:29 +00001121 struct chcr_dev *dev = c_ctx(tfm)->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001122 int bytes;
1123
1124 if (err)
1125 goto unmap;
1126 if (req->nbytes == reqctx->processed) {
1127 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
1128 req);
1129 err = chcr_final_cipher_iv(req, fw6_pld, req->info);
1130 goto complete;
1131 }
1132
1133 if (!reqctx->imm) {
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->nbytes)
1138 bytes = req->nbytes - reqctx->processed;
1139 else
1140 bytes = rounddown(bytes, 16);
1141 } else {
1142 /*CTR mode counter overfloa*/
1143 bytes = req->nbytes - reqctx->processed;
1144 }
1145 err = chcr_update_cipher_iv(req, fw6_pld, reqctx->iv);
1146 if (err)
1147 goto unmap;
1148
1149 if (unlikely(bytes == 0)) {
1150 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
1151 req);
1152 err = chcr_cipher_fallback(ablkctx->sw_cipher,
1153 req->base.flags,
1154 req->src,
1155 req->dst,
1156 req->nbytes,
1157 req->info,
1158 reqctx->op);
1159 goto complete;
1160 }
1161
1162 if (get_cryptoalg_subtype(crypto_ablkcipher_tfm(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];
1166 wrparam.req = req;
1167 wrparam.bytes = bytes;
1168 skb = create_cipher_wr(&wrparam);
1169 if (IS_ERR(skb)) {
1170 pr_err("chcr : %s : Failed to form WR. No memory\n", __func__);
1171 err = PTR_ERR(skb);
1172 goto unmap;
1173 }
1174 skb->dev = u_ctx->lldi.ports[0];
1175 set_wr_txq(skb, CPL_PRIORITY_DATA, c_ctx(tfm)->tx_qidx);
1176 chcr_send_wr(skb);
1177 reqctx->last_req_len = bytes;
1178 reqctx->processed += bytes;
1179 return 0;
1180unmap:
1181 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
1182complete:
David Brazdil0f672f62019-12-10 10:32:29 +00001183 chcr_dec_wrcount(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001184 req->base.complete(&req->base, err);
1185 return err;
1186}
1187
1188static int process_cipher(struct ablkcipher_request *req,
1189 unsigned short qid,
1190 struct sk_buff **skb,
1191 unsigned short op_type)
1192{
1193 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
1194 unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
1195 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
1196 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
1197 struct cipher_wr_param wrparam;
1198 int bytes, err = -EINVAL;
1199
1200 reqctx->processed = 0;
1201 if (!req->info)
1202 goto error;
1203 if ((ablkctx->enckey_len == 0) || (ivsize > AES_BLOCK_SIZE) ||
1204 (req->nbytes == 0) ||
1205 (req->nbytes % crypto_ablkcipher_blocksize(tfm))) {
1206 pr_err("AES: Invalid value of Key Len %d nbytes %d IV Len %d\n",
1207 ablkctx->enckey_len, req->nbytes, ivsize);
1208 goto error;
1209 }
David Brazdil0f672f62019-12-10 10:32:29 +00001210
1211 err = chcr_cipher_dma_map(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
1212 if (err)
1213 goto error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001214 if (req->nbytes < (SGE_MAX_WR_LEN - (sizeof(struct chcr_wr) +
1215 AES_MIN_KEY_SIZE +
1216 sizeof(struct cpl_rx_phys_dsgl) +
1217 /*Min dsgl size*/
1218 32))) {
1219 /* Can be sent as Imm*/
1220 unsigned int dnents = 0, transhdr_len, phys_dsgl, kctx_len;
1221
1222 dnents = sg_nents_xlen(req->dst, req->nbytes,
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->nbytes) <=
1228 SGE_MAX_WR_LEN;
1229 bytes = IV + req->nbytes;
1230
1231 } else {
1232 reqctx->imm = 0;
1233 }
1234
1235 if (!reqctx->imm) {
1236 bytes = chcr_sg_ent_in_wr(req->src, req->dst, 0,
1237 CIP_SPACE_LEFT(ablkctx->enckey_len),
1238 0, 0);
1239 if ((bytes + reqctx->processed) >= req->nbytes)
1240 bytes = req->nbytes - reqctx->processed;
1241 else
1242 bytes = rounddown(bytes, 16);
1243 } else {
1244 bytes = req->nbytes;
1245 }
1246 if (get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
1247 CRYPTO_ALG_SUB_TYPE_CTR) {
1248 bytes = adjust_ctr_overflow(req->info, bytes);
1249 }
1250 if (get_cryptoalg_subtype(crypto_ablkcipher_tfm(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->info,
1254 CTR_RFC3686_IV_SIZE);
1255
1256 /* initialize counter portion of counter block */
1257 *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE +
1258 CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
1259
1260 } else {
1261
1262 memcpy(reqctx->iv, req->info, IV);
1263 }
1264 if (unlikely(bytes == 0)) {
1265 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
1266 req);
1267 err = chcr_cipher_fallback(ablkctx->sw_cipher,
1268 req->base.flags,
1269 req->src,
1270 req->dst,
1271 req->nbytes,
1272 reqctx->iv,
1273 op_type);
1274 goto error;
1275 }
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;
1281 wrparam.qid = qid;
1282 wrparam.req = req;
1283 wrparam.bytes = bytes;
1284 *skb = create_cipher_wr(&wrparam);
1285 if (IS_ERR(*skb)) {
1286 err = PTR_ERR(*skb);
1287 goto unmap;
1288 }
1289 reqctx->processed = bytes;
1290 reqctx->last_req_len = bytes;
1291
1292 return 0;
1293unmap:
1294 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
1295error:
1296 return err;
1297}
1298
1299static int chcr_aes_encrypt(struct ablkcipher_request *req)
1300{
1301 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
David Brazdil0f672f62019-12-10 10:32:29 +00001302 struct chcr_dev *dev = c_ctx(tfm)->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001303 struct sk_buff *skb = NULL;
1304 int err, isfull = 0;
1305 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
1306
David Brazdil0f672f62019-12-10 10:32:29 +00001307 err = chcr_inc_wrcount(dev);
1308 if (err)
1309 return -ENXIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001310 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1311 c_ctx(tfm)->tx_qidx))) {
1312 isfull = 1;
David Brazdil0f672f62019-12-10 10:32:29 +00001313 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1314 err = -ENOSPC;
1315 goto error;
1316 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001317 }
1318
1319 err = process_cipher(req, u_ctx->lldi.rxq_ids[c_ctx(tfm)->rx_qidx],
1320 &skb, CHCR_ENCRYPT_OP);
1321 if (err || !skb)
1322 return err;
1323 skb->dev = u_ctx->lldi.ports[0];
1324 set_wr_txq(skb, CPL_PRIORITY_DATA, c_ctx(tfm)->tx_qidx);
1325 chcr_send_wr(skb);
1326 return isfull ? -EBUSY : -EINPROGRESS;
David Brazdil0f672f62019-12-10 10:32:29 +00001327error:
1328 chcr_dec_wrcount(dev);
1329 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001330}
1331
1332static int chcr_aes_decrypt(struct ablkcipher_request *req)
1333{
1334 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
1335 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
David Brazdil0f672f62019-12-10 10:32:29 +00001336 struct chcr_dev *dev = c_ctx(tfm)->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001337 struct sk_buff *skb = NULL;
1338 int err, isfull = 0;
1339
David Brazdil0f672f62019-12-10 10:32:29 +00001340 err = chcr_inc_wrcount(dev);
1341 if (err)
1342 return -ENXIO;
1343
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001344 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1345 c_ctx(tfm)->tx_qidx))) {
1346 isfull = 1;
1347 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
1348 return -ENOSPC;
1349 }
1350
David Brazdil0f672f62019-12-10 10:32:29 +00001351 err = process_cipher(req, u_ctx->lldi.rxq_ids[c_ctx(tfm)->rx_qidx],
1352 &skb, CHCR_DECRYPT_OP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001353 if (err || !skb)
1354 return err;
1355 skb->dev = u_ctx->lldi.ports[0];
1356 set_wr_txq(skb, CPL_PRIORITY_DATA, c_ctx(tfm)->tx_qidx);
1357 chcr_send_wr(skb);
1358 return isfull ? -EBUSY : -EINPROGRESS;
1359}
1360
1361static int chcr_device_init(struct chcr_context *ctx)
1362{
1363 struct uld_ctx *u_ctx = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001364 unsigned int id;
1365 int txq_perchan, txq_idx, ntxq;
1366 int err = 0, rxq_perchan, rxq_idx;
1367
1368 id = smp_processor_id();
1369 if (!ctx->dev) {
1370 u_ctx = assign_chcr_device();
1371 if (!u_ctx) {
David Brazdil0f672f62019-12-10 10:32:29 +00001372 err = -ENXIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001373 pr_err("chcr device assignment fails\n");
1374 goto out;
1375 }
David Brazdil0f672f62019-12-10 10:32:29 +00001376 ctx->dev = &u_ctx->dev;
1377 ntxq = u_ctx->lldi.ntxq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001378 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001383 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"
1394 */
1395 ctx->pci_chan_id = txq_idx / txq_perchan;
1396 }
1397out:
1398 return err;
1399}
1400
1401static int chcr_cra_init(struct crypto_tfm *tfm)
1402{
1403 struct crypto_alg *alg = tfm->__crt_alg;
1404 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
1405 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
1406
David Brazdil0f672f62019-12-10 10:32:29 +00001407 ablkctx->sw_cipher = crypto_alloc_sync_skcipher(alg->cra_name, 0,
1408 CRYPTO_ALG_NEED_FALLBACK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001409 if (IS_ERR(ablkctx->sw_cipher)) {
1410 pr_err("failed to allocate fallback for %s\n", alg->cra_name);
1411 return PTR_ERR(ablkctx->sw_cipher);
1412 }
1413
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001414 tfm->crt_ablkcipher.reqsize = sizeof(struct chcr_blkcipher_req_ctx);
1415 return chcr_device_init(crypto_tfm_ctx(tfm));
1416}
1417
1418static int chcr_rfc3686_init(struct crypto_tfm *tfm)
1419{
1420 struct crypto_alg *alg = tfm->__crt_alg;
1421 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
1422 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
1423
1424 /*RFC3686 initialises IV counter value to 1, rfc3686(ctr(aes))
1425 * cannot be used as fallback in chcr_handle_cipher_response
1426 */
David Brazdil0f672f62019-12-10 10:32:29 +00001427 ablkctx->sw_cipher = crypto_alloc_sync_skcipher("ctr(aes)", 0,
1428 CRYPTO_ALG_NEED_FALLBACK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001429 if (IS_ERR(ablkctx->sw_cipher)) {
1430 pr_err("failed to allocate fallback for %s\n", alg->cra_name);
1431 return PTR_ERR(ablkctx->sw_cipher);
1432 }
1433 tfm->crt_ablkcipher.reqsize = sizeof(struct chcr_blkcipher_req_ctx);
1434 return chcr_device_init(crypto_tfm_ctx(tfm));
1435}
1436
1437
1438static void chcr_cra_exit(struct crypto_tfm *tfm)
1439{
1440 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
1441 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
1442
David Brazdil0f672f62019-12-10 10:32:29 +00001443 crypto_free_sync_skcipher(ablkctx->sw_cipher);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001444}
1445
1446static int get_alg_config(struct algo_param *params,
1447 unsigned int auth_size)
1448{
1449 switch (auth_size) {
1450 case SHA1_DIGEST_SIZE:
1451 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_160;
1452 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA1;
1453 params->result_size = SHA1_DIGEST_SIZE;
1454 break;
1455 case SHA224_DIGEST_SIZE:
1456 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
1457 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA224;
1458 params->result_size = SHA256_DIGEST_SIZE;
1459 break;
1460 case SHA256_DIGEST_SIZE:
1461 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
1462 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA256;
1463 params->result_size = SHA256_DIGEST_SIZE;
1464 break;
1465 case SHA384_DIGEST_SIZE:
1466 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
1467 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_384;
1468 params->result_size = SHA512_DIGEST_SIZE;
1469 break;
1470 case SHA512_DIGEST_SIZE:
1471 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
1472 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_512;
1473 params->result_size = SHA512_DIGEST_SIZE;
1474 break;
1475 default:
1476 pr_err("chcr : ERROR, unsupported digest size\n");
1477 return -EINVAL;
1478 }
1479 return 0;
1480}
1481
1482static inline void chcr_free_shash(struct crypto_shash *base_hash)
1483{
1484 crypto_free_shash(base_hash);
1485}
1486
1487/**
1488 * create_hash_wr - Create hash work request
1489 * @req - Cipher req base
1490 */
1491static struct sk_buff *create_hash_wr(struct ahash_request *req,
1492 struct hash_wr_param *param)
1493{
1494 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1495 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1496 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(tfm));
1497 struct sk_buff *skb = NULL;
1498 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(tfm));
1499 struct chcr_wr *chcr_req;
1500 struct ulptx_sgl *ulptx;
1501 unsigned int nents = 0, transhdr_len;
1502 unsigned int temp = 0;
1503 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1504 GFP_ATOMIC;
1505 struct adapter *adap = padap(h_ctx(tfm)->dev);
1506 int error = 0;
1507
1508 transhdr_len = HASH_TRANSHDR_SIZE(param->kctx_len);
1509 req_ctx->hctx_wr.imm = (transhdr_len + param->bfr_len +
1510 param->sg_len) <= SGE_MAX_WR_LEN;
1511 nents = sg_nents_xlen(req_ctx->hctx_wr.srcsg, param->sg_len,
1512 CHCR_SRC_SG_SIZE, req_ctx->hctx_wr.src_ofst);
1513 nents += param->bfr_len ? 1 : 0;
1514 transhdr_len += req_ctx->hctx_wr.imm ? roundup(param->bfr_len +
1515 param->sg_len, 16) : (sgl_len(nents) * 8);
1516 transhdr_len = roundup(transhdr_len, 16);
1517
1518 skb = alloc_skb(transhdr_len, flags);
1519 if (!skb)
1520 return ERR_PTR(-ENOMEM);
1521 chcr_req = __skb_put_zero(skb, transhdr_len);
1522
1523 chcr_req->sec_cpl.op_ivinsrtofst =
David Brazdil0f672f62019-12-10 10:32:29 +00001524 FILL_SEC_CPL_OP_IVINSR(h_ctx(tfm)->tx_chan_id, 2, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001525 chcr_req->sec_cpl.pldlen = htonl(param->bfr_len + param->sg_len);
1526
1527 chcr_req->sec_cpl.aadstart_cipherstop_hi =
1528 FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, 0, 0);
1529 chcr_req->sec_cpl.cipherstop_lo_authinsert =
1530 FILL_SEC_CPL_AUTHINSERT(0, 1, 0, 0);
1531 chcr_req->sec_cpl.seqno_numivs =
1532 FILL_SEC_CPL_SCMD0_SEQNO(0, 0, 0, param->alg_prm.auth_mode,
1533 param->opad_needed, 0);
1534
1535 chcr_req->sec_cpl.ivgen_hdrlen =
1536 FILL_SEC_CPL_IVGEN_HDRLEN(param->last, param->more, 0, 1, 0, 0);
1537
1538 memcpy(chcr_req->key_ctx.key, req_ctx->partial_hash,
1539 param->alg_prm.result_size);
1540
1541 if (param->opad_needed)
1542 memcpy(chcr_req->key_ctx.key +
1543 ((param->alg_prm.result_size <= 32) ? 32 :
1544 CHCR_HASH_MAX_DIGEST_SIZE),
1545 hmacctx->opad, param->alg_prm.result_size);
1546
1547 chcr_req->key_ctx.ctx_hdr = FILL_KEY_CTX_HDR(CHCR_KEYCTX_NO_KEY,
1548 param->alg_prm.mk_size, 0,
1549 param->opad_needed,
1550 ((param->kctx_len +
1551 sizeof(chcr_req->key_ctx)) >> 4));
1552 chcr_req->sec_cpl.scmd1 = cpu_to_be64((u64)param->scmd1);
1553 ulptx = (struct ulptx_sgl *)((u8 *)(chcr_req + 1) + param->kctx_len +
1554 DUMMY_BYTES);
1555 if (param->bfr_len != 0) {
1556 req_ctx->hctx_wr.dma_addr =
1557 dma_map_single(&u_ctx->lldi.pdev->dev, req_ctx->reqbfr,
1558 param->bfr_len, DMA_TO_DEVICE);
1559 if (dma_mapping_error(&u_ctx->lldi.pdev->dev,
1560 req_ctx->hctx_wr. dma_addr)) {
1561 error = -ENOMEM;
1562 goto err;
1563 }
1564 req_ctx->hctx_wr.dma_len = param->bfr_len;
1565 } else {
1566 req_ctx->hctx_wr.dma_addr = 0;
1567 }
1568 chcr_add_hash_src_ent(req, ulptx, param);
1569 /* Request upto max wr size */
1570 temp = param->kctx_len + DUMMY_BYTES + (req_ctx->hctx_wr.imm ?
1571 (param->sg_len + param->bfr_len) : 0);
1572 atomic_inc(&adap->chcr_stats.digest_rqst);
1573 create_wreq(h_ctx(tfm), chcr_req, &req->base, req_ctx->hctx_wr.imm,
1574 param->hash_size, transhdr_len,
1575 temp, 0);
1576 req_ctx->hctx_wr.skb = skb;
1577 return skb;
1578err:
1579 kfree_skb(skb);
1580 return ERR_PTR(error);
1581}
1582
1583static int chcr_ahash_update(struct ahash_request *req)
1584{
1585 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1586 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1587 struct uld_ctx *u_ctx = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00001588 struct chcr_dev *dev = h_ctx(rtfm)->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001589 struct sk_buff *skb;
1590 u8 remainder = 0, bs;
1591 unsigned int nbytes = req->nbytes;
1592 struct hash_wr_param params;
1593 int error, isfull = 0;
1594
1595 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1596 u_ctx = ULD_CTX(h_ctx(rtfm));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001597
1598 if (nbytes + req_ctx->reqlen >= bs) {
1599 remainder = (nbytes + req_ctx->reqlen) % bs;
1600 nbytes = nbytes + req_ctx->reqlen - remainder;
1601 } else {
1602 sg_pcopy_to_buffer(req->src, sg_nents(req->src), req_ctx->reqbfr
1603 + req_ctx->reqlen, nbytes, 0);
1604 req_ctx->reqlen += nbytes;
1605 return 0;
1606 }
David Brazdil0f672f62019-12-10 10:32:29 +00001607 error = chcr_inc_wrcount(dev);
1608 if (error)
1609 return -ENXIO;
1610 /* Detach state for CHCR means lldi or padap is freed. Increasing
1611 * inflight count for dev guarantees that lldi and padap is valid
1612 */
1613 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1614 h_ctx(rtfm)->tx_qidx))) {
1615 isfull = 1;
1616 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1617 error = -ENOSPC;
1618 goto err;
1619 }
1620 }
1621
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001622 chcr_init_hctx_per_wr(req_ctx);
1623 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
David Brazdil0f672f62019-12-10 10:32:29 +00001624 if (error) {
1625 error = -ENOMEM;
1626 goto err;
1627 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001628 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1629 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1630 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
1631 HASH_SPACE_LEFT(params.kctx_len), 0);
1632 if (params.sg_len > req->nbytes)
1633 params.sg_len = req->nbytes;
1634 params.sg_len = rounddown(params.sg_len + req_ctx->reqlen, bs) -
1635 req_ctx->reqlen;
1636 params.opad_needed = 0;
1637 params.more = 1;
1638 params.last = 0;
1639 params.bfr_len = req_ctx->reqlen;
1640 params.scmd1 = 0;
1641 req_ctx->hctx_wr.srcsg = req->src;
1642
1643 params.hash_size = params.alg_prm.result_size;
1644 req_ctx->data_len += params.sg_len + params.bfr_len;
1645 skb = create_hash_wr(req, &params);
1646 if (IS_ERR(skb)) {
1647 error = PTR_ERR(skb);
1648 goto unmap;
1649 }
1650
1651 req_ctx->hctx_wr.processed += params.sg_len;
1652 if (remainder) {
1653 /* Swap buffers */
1654 swap(req_ctx->reqbfr, req_ctx->skbfr);
1655 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
1656 req_ctx->reqbfr, remainder, req->nbytes -
1657 remainder);
1658 }
1659 req_ctx->reqlen = remainder;
1660 skb->dev = u_ctx->lldi.ports[0];
1661 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
1662 chcr_send_wr(skb);
1663
1664 return isfull ? -EBUSY : -EINPROGRESS;
1665unmap:
1666 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
David Brazdil0f672f62019-12-10 10:32:29 +00001667err:
1668 chcr_dec_wrcount(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001669 return error;
1670}
1671
1672static void create_last_hash_block(char *bfr_ptr, unsigned int bs, u64 scmd1)
1673{
1674 memset(bfr_ptr, 0, bs);
1675 *bfr_ptr = 0x80;
1676 if (bs == 64)
1677 *(__be64 *)(bfr_ptr + 56) = cpu_to_be64(scmd1 << 3);
1678 else
1679 *(__be64 *)(bfr_ptr + 120) = cpu_to_be64(scmd1 << 3);
1680}
1681
1682static int chcr_ahash_final(struct ahash_request *req)
1683{
1684 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1685 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
David Brazdil0f672f62019-12-10 10:32:29 +00001686 struct chcr_dev *dev = h_ctx(rtfm)->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001687 struct hash_wr_param params;
1688 struct sk_buff *skb;
1689 struct uld_ctx *u_ctx = NULL;
1690 u8 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
David Brazdil0f672f62019-12-10 10:32:29 +00001691 int error = -EINVAL;
1692
1693 error = chcr_inc_wrcount(dev);
1694 if (error)
1695 return -ENXIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001696
1697 chcr_init_hctx_per_wr(req_ctx);
1698 u_ctx = ULD_CTX(h_ctx(rtfm));
1699 if (is_hmac(crypto_ahash_tfm(rtfm)))
1700 params.opad_needed = 1;
1701 else
1702 params.opad_needed = 0;
1703 params.sg_len = 0;
1704 req_ctx->hctx_wr.isfinal = 1;
1705 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1706 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1707 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1708 params.opad_needed = 1;
1709 params.kctx_len *= 2;
1710 } else {
1711 params.opad_needed = 0;
1712 }
1713
1714 req_ctx->hctx_wr.result = 1;
1715 params.bfr_len = req_ctx->reqlen;
1716 req_ctx->data_len += params.bfr_len + params.sg_len;
1717 req_ctx->hctx_wr.srcsg = req->src;
1718 if (req_ctx->reqlen == 0) {
1719 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
1720 params.last = 0;
1721 params.more = 1;
1722 params.scmd1 = 0;
1723 params.bfr_len = bs;
1724
1725 } else {
1726 params.scmd1 = req_ctx->data_len;
1727 params.last = 1;
1728 params.more = 0;
1729 }
1730 params.hash_size = crypto_ahash_digestsize(rtfm);
1731 skb = create_hash_wr(req, &params);
David Brazdil0f672f62019-12-10 10:32:29 +00001732 if (IS_ERR(skb)) {
1733 error = PTR_ERR(skb);
1734 goto err;
1735 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001736 req_ctx->reqlen = 0;
1737 skb->dev = u_ctx->lldi.ports[0];
1738 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
1739 chcr_send_wr(skb);
1740 return -EINPROGRESS;
David Brazdil0f672f62019-12-10 10:32:29 +00001741err:
1742 chcr_dec_wrcount(dev);
1743 return error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001744}
1745
1746static int chcr_ahash_finup(struct ahash_request *req)
1747{
1748 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1749 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
David Brazdil0f672f62019-12-10 10:32:29 +00001750 struct chcr_dev *dev = h_ctx(rtfm)->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001751 struct uld_ctx *u_ctx = NULL;
1752 struct sk_buff *skb;
1753 struct hash_wr_param params;
1754 u8 bs;
1755 int error, isfull = 0;
1756
1757 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1758 u_ctx = ULD_CTX(h_ctx(rtfm));
David Brazdil0f672f62019-12-10 10:32:29 +00001759 error = chcr_inc_wrcount(dev);
1760 if (error)
1761 return -ENXIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001762
1763 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1764 h_ctx(rtfm)->tx_qidx))) {
1765 isfull = 1;
David Brazdil0f672f62019-12-10 10:32:29 +00001766 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1767 error = -ENOSPC;
1768 goto err;
1769 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001770 }
1771 chcr_init_hctx_per_wr(req_ctx);
1772 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
David Brazdil0f672f62019-12-10 10:32:29 +00001773 if (error) {
1774 error = -ENOMEM;
1775 goto err;
1776 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001777
1778 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1779 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1780 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1781 params.kctx_len *= 2;
1782 params.opad_needed = 1;
1783 } else {
1784 params.opad_needed = 0;
1785 }
1786
1787 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
1788 HASH_SPACE_LEFT(params.kctx_len), 0);
1789 if (params.sg_len < req->nbytes) {
1790 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1791 params.kctx_len /= 2;
1792 params.opad_needed = 0;
1793 }
1794 params.last = 0;
1795 params.more = 1;
1796 params.sg_len = rounddown(params.sg_len + req_ctx->reqlen, bs)
1797 - req_ctx->reqlen;
1798 params.hash_size = params.alg_prm.result_size;
1799 params.scmd1 = 0;
1800 } else {
1801 params.last = 1;
1802 params.more = 0;
1803 params.sg_len = req->nbytes;
1804 params.hash_size = crypto_ahash_digestsize(rtfm);
1805 params.scmd1 = req_ctx->data_len + req_ctx->reqlen +
1806 params.sg_len;
1807 }
1808 params.bfr_len = req_ctx->reqlen;
1809 req_ctx->data_len += params.bfr_len + params.sg_len;
1810 req_ctx->hctx_wr.result = 1;
1811 req_ctx->hctx_wr.srcsg = req->src;
1812 if ((req_ctx->reqlen + req->nbytes) == 0) {
1813 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
1814 params.last = 0;
1815 params.more = 1;
1816 params.scmd1 = 0;
1817 params.bfr_len = bs;
1818 }
1819 skb = create_hash_wr(req, &params);
1820 if (IS_ERR(skb)) {
1821 error = PTR_ERR(skb);
1822 goto unmap;
1823 }
1824 req_ctx->reqlen = 0;
1825 req_ctx->hctx_wr.processed += params.sg_len;
1826 skb->dev = u_ctx->lldi.ports[0];
1827 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
1828 chcr_send_wr(skb);
1829
1830 return isfull ? -EBUSY : -EINPROGRESS;
1831unmap:
1832 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
David Brazdil0f672f62019-12-10 10:32:29 +00001833err:
1834 chcr_dec_wrcount(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001835 return error;
1836}
1837
1838static int chcr_ahash_digest(struct ahash_request *req)
1839{
1840 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1841 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
David Brazdil0f672f62019-12-10 10:32:29 +00001842 struct chcr_dev *dev = h_ctx(rtfm)->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001843 struct uld_ctx *u_ctx = NULL;
1844 struct sk_buff *skb;
1845 struct hash_wr_param params;
1846 u8 bs;
1847 int error, isfull = 0;
1848
1849 rtfm->init(req);
1850 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
David Brazdil0f672f62019-12-10 10:32:29 +00001851 error = chcr_inc_wrcount(dev);
1852 if (error)
1853 return -ENXIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001854
1855 u_ctx = ULD_CTX(h_ctx(rtfm));
1856 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1857 h_ctx(rtfm)->tx_qidx))) {
1858 isfull = 1;
David Brazdil0f672f62019-12-10 10:32:29 +00001859 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1860 error = -ENOSPC;
1861 goto err;
1862 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001863 }
1864
1865 chcr_init_hctx_per_wr(req_ctx);
1866 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
David Brazdil0f672f62019-12-10 10:32:29 +00001867 if (error) {
1868 error = -ENOMEM;
1869 goto err;
1870 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001871
1872 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1873 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1874 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1875 params.kctx_len *= 2;
1876 params.opad_needed = 1;
1877 } else {
1878 params.opad_needed = 0;
1879 }
1880 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
1881 HASH_SPACE_LEFT(params.kctx_len), 0);
1882 if (params.sg_len < req->nbytes) {
1883 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1884 params.kctx_len /= 2;
1885 params.opad_needed = 0;
1886 }
1887 params.last = 0;
1888 params.more = 1;
1889 params.scmd1 = 0;
1890 params.sg_len = rounddown(params.sg_len, bs);
1891 params.hash_size = params.alg_prm.result_size;
1892 } else {
1893 params.sg_len = req->nbytes;
1894 params.hash_size = crypto_ahash_digestsize(rtfm);
1895 params.last = 1;
1896 params.more = 0;
1897 params.scmd1 = req->nbytes + req_ctx->data_len;
1898
1899 }
1900 params.bfr_len = 0;
1901 req_ctx->hctx_wr.result = 1;
1902 req_ctx->hctx_wr.srcsg = req->src;
1903 req_ctx->data_len += params.bfr_len + params.sg_len;
1904
1905 if (req->nbytes == 0) {
1906 create_last_hash_block(req_ctx->reqbfr, bs, 0);
1907 params.more = 1;
1908 params.bfr_len = bs;
1909 }
1910
1911 skb = create_hash_wr(req, &params);
1912 if (IS_ERR(skb)) {
1913 error = PTR_ERR(skb);
1914 goto unmap;
1915 }
1916 req_ctx->hctx_wr.processed += params.sg_len;
1917 skb->dev = u_ctx->lldi.ports[0];
1918 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
1919 chcr_send_wr(skb);
1920 return isfull ? -EBUSY : -EINPROGRESS;
1921unmap:
1922 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
David Brazdil0f672f62019-12-10 10:32:29 +00001923err:
1924 chcr_dec_wrcount(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001925 return error;
1926}
1927
1928static int chcr_ahash_continue(struct ahash_request *req)
1929{
1930 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
1931 struct chcr_hctx_per_wr *hctx_wr = &reqctx->hctx_wr;
1932 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1933 struct uld_ctx *u_ctx = NULL;
1934 struct sk_buff *skb;
1935 struct hash_wr_param params;
1936 u8 bs;
1937 int error;
1938
1939 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1940 u_ctx = ULD_CTX(h_ctx(rtfm));
1941 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1942 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1943 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1944 params.kctx_len *= 2;
1945 params.opad_needed = 1;
1946 } else {
1947 params.opad_needed = 0;
1948 }
1949 params.sg_len = chcr_hash_ent_in_wr(hctx_wr->srcsg, 0,
1950 HASH_SPACE_LEFT(params.kctx_len),
1951 hctx_wr->src_ofst);
1952 if ((params.sg_len + hctx_wr->processed) > req->nbytes)
1953 params.sg_len = req->nbytes - hctx_wr->processed;
1954 if (!hctx_wr->result ||
1955 ((params.sg_len + hctx_wr->processed) < req->nbytes)) {
1956 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1957 params.kctx_len /= 2;
1958 params.opad_needed = 0;
1959 }
1960 params.last = 0;
1961 params.more = 1;
1962 params.sg_len = rounddown(params.sg_len, bs);
1963 params.hash_size = params.alg_prm.result_size;
1964 params.scmd1 = 0;
1965 } else {
1966 params.last = 1;
1967 params.more = 0;
1968 params.hash_size = crypto_ahash_digestsize(rtfm);
1969 params.scmd1 = reqctx->data_len + params.sg_len;
1970 }
1971 params.bfr_len = 0;
1972 reqctx->data_len += params.sg_len;
1973 skb = create_hash_wr(req, &params);
1974 if (IS_ERR(skb)) {
1975 error = PTR_ERR(skb);
1976 goto err;
1977 }
1978 hctx_wr->processed += params.sg_len;
1979 skb->dev = u_ctx->lldi.ports[0];
1980 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
1981 chcr_send_wr(skb);
1982 return 0;
1983err:
1984 return error;
1985}
1986
1987static inline void chcr_handle_ahash_resp(struct ahash_request *req,
1988 unsigned char *input,
1989 int err)
1990{
1991 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
1992 struct chcr_hctx_per_wr *hctx_wr = &reqctx->hctx_wr;
1993 int digestsize, updated_digestsize;
1994 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1995 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(tfm));
David Brazdil0f672f62019-12-10 10:32:29 +00001996 struct chcr_dev *dev = h_ctx(tfm)->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001997
1998 if (input == NULL)
1999 goto out;
2000 digestsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
2001 updated_digestsize = digestsize;
2002 if (digestsize == SHA224_DIGEST_SIZE)
2003 updated_digestsize = SHA256_DIGEST_SIZE;
2004 else if (digestsize == SHA384_DIGEST_SIZE)
2005 updated_digestsize = SHA512_DIGEST_SIZE;
2006
2007 if (hctx_wr->dma_addr) {
2008 dma_unmap_single(&u_ctx->lldi.pdev->dev, hctx_wr->dma_addr,
2009 hctx_wr->dma_len, DMA_TO_DEVICE);
2010 hctx_wr->dma_addr = 0;
2011 }
2012 if (hctx_wr->isfinal || ((hctx_wr->processed + reqctx->reqlen) ==
2013 req->nbytes)) {
2014 if (hctx_wr->result == 1) {
2015 hctx_wr->result = 0;
2016 memcpy(req->result, input + sizeof(struct cpl_fw6_pld),
2017 digestsize);
2018 } else {
2019 memcpy(reqctx->partial_hash,
2020 input + sizeof(struct cpl_fw6_pld),
2021 updated_digestsize);
2022
2023 }
2024 goto unmap;
2025 }
2026 memcpy(reqctx->partial_hash, input + sizeof(struct cpl_fw6_pld),
2027 updated_digestsize);
2028
2029 err = chcr_ahash_continue(req);
2030 if (err)
2031 goto unmap;
2032 return;
2033unmap:
2034 if (hctx_wr->is_sg_map)
2035 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
2036
2037
2038out:
David Brazdil0f672f62019-12-10 10:32:29 +00002039 chcr_dec_wrcount(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002040 req->base.complete(&req->base, err);
2041}
2042
2043/*
2044 * chcr_handle_resp - Unmap the DMA buffers associated with the request
2045 * @req: crypto request
2046 */
2047int chcr_handle_resp(struct crypto_async_request *req, unsigned char *input,
2048 int err)
2049{
2050 struct crypto_tfm *tfm = req->tfm;
2051 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2052 struct adapter *adap = padap(ctx->dev);
2053
2054 switch (tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
2055 case CRYPTO_ALG_TYPE_AEAD:
David Brazdil0f672f62019-12-10 10:32:29 +00002056 err = chcr_handle_aead_resp(aead_request_cast(req), input, err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002057 break;
2058
2059 case CRYPTO_ALG_TYPE_ABLKCIPHER:
David Brazdil0f672f62019-12-10 10:32:29 +00002060 chcr_handle_cipher_resp(ablkcipher_request_cast(req),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002061 input, err);
2062 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002063 case CRYPTO_ALG_TYPE_AHASH:
2064 chcr_handle_ahash_resp(ahash_request_cast(req), input, err);
2065 }
2066 atomic_inc(&adap->chcr_stats.complete);
2067 return err;
2068}
2069static int chcr_ahash_export(struct ahash_request *areq, void *out)
2070{
2071 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2072 struct chcr_ahash_req_ctx *state = out;
2073
2074 state->reqlen = req_ctx->reqlen;
2075 state->data_len = req_ctx->data_len;
2076 memcpy(state->bfr1, req_ctx->reqbfr, req_ctx->reqlen);
2077 memcpy(state->partial_hash, req_ctx->partial_hash,
2078 CHCR_HASH_MAX_DIGEST_SIZE);
2079 chcr_init_hctx_per_wr(state);
David Brazdil0f672f62019-12-10 10:32:29 +00002080 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002081}
2082
2083static int chcr_ahash_import(struct ahash_request *areq, const void *in)
2084{
2085 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2086 struct chcr_ahash_req_ctx *state = (struct chcr_ahash_req_ctx *)in;
2087
2088 req_ctx->reqlen = state->reqlen;
2089 req_ctx->data_len = state->data_len;
2090 req_ctx->reqbfr = req_ctx->bfr1;
2091 req_ctx->skbfr = req_ctx->bfr2;
2092 memcpy(req_ctx->bfr1, state->bfr1, CHCR_HASH_MAX_BLOCK_SIZE_128);
2093 memcpy(req_ctx->partial_hash, state->partial_hash,
2094 CHCR_HASH_MAX_DIGEST_SIZE);
2095 chcr_init_hctx_per_wr(req_ctx);
2096 return 0;
2097}
2098
2099static int chcr_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
2100 unsigned int keylen)
2101{
2102 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(tfm));
2103 unsigned int digestsize = crypto_ahash_digestsize(tfm);
2104 unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2105 unsigned int i, err = 0, updated_digestsize;
2106
2107 SHASH_DESC_ON_STACK(shash, hmacctx->base_hash);
2108
2109 /* use the key to calculate the ipad and opad. ipad will sent with the
2110 * first request's data. opad will be sent with the final hash result
2111 * ipad in hmacctx->ipad and opad in hmacctx->opad location
2112 */
2113 shash->tfm = hmacctx->base_hash;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002114 if (keylen > bs) {
2115 err = crypto_shash_digest(shash, key, keylen,
2116 hmacctx->ipad);
2117 if (err)
2118 goto out;
2119 keylen = digestsize;
2120 } else {
2121 memcpy(hmacctx->ipad, key, keylen);
2122 }
2123 memset(hmacctx->ipad + keylen, 0, bs - keylen);
2124 memcpy(hmacctx->opad, hmacctx->ipad, bs);
2125
2126 for (i = 0; i < bs / sizeof(int); i++) {
2127 *((unsigned int *)(&hmacctx->ipad) + i) ^= IPAD_DATA;
2128 *((unsigned int *)(&hmacctx->opad) + i) ^= OPAD_DATA;
2129 }
2130
2131 updated_digestsize = digestsize;
2132 if (digestsize == SHA224_DIGEST_SIZE)
2133 updated_digestsize = SHA256_DIGEST_SIZE;
2134 else if (digestsize == SHA384_DIGEST_SIZE)
2135 updated_digestsize = SHA512_DIGEST_SIZE;
2136 err = chcr_compute_partial_hash(shash, hmacctx->ipad,
2137 hmacctx->ipad, digestsize);
2138 if (err)
2139 goto out;
2140 chcr_change_order(hmacctx->ipad, updated_digestsize);
2141
2142 err = chcr_compute_partial_hash(shash, hmacctx->opad,
2143 hmacctx->opad, digestsize);
2144 if (err)
2145 goto out;
2146 chcr_change_order(hmacctx->opad, updated_digestsize);
2147out:
2148 return err;
2149}
2150
2151static int chcr_aes_xts_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
2152 unsigned int key_len)
2153{
2154 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
2155 unsigned short context_size = 0;
2156 int err;
2157
2158 err = chcr_cipher_fallback_setkey(cipher, key, key_len);
2159 if (err)
2160 goto badkey_err;
2161
2162 memcpy(ablkctx->key, key, key_len);
2163 ablkctx->enckey_len = key_len;
2164 get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, key_len << 2);
2165 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len) >> 4;
2166 ablkctx->key_ctx_hdr =
2167 FILL_KEY_CTX_HDR((key_len == AES_KEYSIZE_256) ?
2168 CHCR_KEYCTX_CIPHER_KEY_SIZE_128 :
2169 CHCR_KEYCTX_CIPHER_KEY_SIZE_256,
2170 CHCR_KEYCTX_NO_KEY, 1,
2171 0, context_size);
2172 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_XTS;
2173 return 0;
2174badkey_err:
2175 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
2176 ablkctx->enckey_len = 0;
2177
2178 return err;
2179}
2180
2181static int chcr_sha_init(struct ahash_request *areq)
2182{
2183 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2184 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
2185 int digestsize = crypto_ahash_digestsize(tfm);
2186
2187 req_ctx->data_len = 0;
2188 req_ctx->reqlen = 0;
2189 req_ctx->reqbfr = req_ctx->bfr1;
2190 req_ctx->skbfr = req_ctx->bfr2;
2191 copy_hash_init_values(req_ctx->partial_hash, digestsize);
2192
2193 return 0;
2194}
2195
2196static int chcr_sha_cra_init(struct crypto_tfm *tfm)
2197{
2198 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2199 sizeof(struct chcr_ahash_req_ctx));
2200 return chcr_device_init(crypto_tfm_ctx(tfm));
2201}
2202
2203static int chcr_hmac_init(struct ahash_request *areq)
2204{
2205 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2206 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(areq);
2207 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(rtfm));
2208 unsigned int digestsize = crypto_ahash_digestsize(rtfm);
2209 unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
2210
2211 chcr_sha_init(areq);
2212 req_ctx->data_len = bs;
2213 if (is_hmac(crypto_ahash_tfm(rtfm))) {
2214 if (digestsize == SHA224_DIGEST_SIZE)
2215 memcpy(req_ctx->partial_hash, hmacctx->ipad,
2216 SHA256_DIGEST_SIZE);
2217 else if (digestsize == SHA384_DIGEST_SIZE)
2218 memcpy(req_ctx->partial_hash, hmacctx->ipad,
2219 SHA512_DIGEST_SIZE);
2220 else
2221 memcpy(req_ctx->partial_hash, hmacctx->ipad,
2222 digestsize);
2223 }
2224 return 0;
2225}
2226
2227static int chcr_hmac_cra_init(struct crypto_tfm *tfm)
2228{
2229 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2230 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
2231 unsigned int digestsize =
2232 crypto_ahash_digestsize(__crypto_ahash_cast(tfm));
2233
2234 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2235 sizeof(struct chcr_ahash_req_ctx));
2236 hmacctx->base_hash = chcr_alloc_shash(digestsize);
2237 if (IS_ERR(hmacctx->base_hash))
2238 return PTR_ERR(hmacctx->base_hash);
2239 return chcr_device_init(crypto_tfm_ctx(tfm));
2240}
2241
2242static void chcr_hmac_cra_exit(struct crypto_tfm *tfm)
2243{
2244 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2245 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
2246
2247 if (hmacctx->base_hash) {
2248 chcr_free_shash(hmacctx->base_hash);
2249 hmacctx->base_hash = NULL;
2250 }
2251}
2252
2253inline void chcr_aead_common_exit(struct aead_request *req)
2254{
2255 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2256 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2257 struct uld_ctx *u_ctx = ULD_CTX(a_ctx(tfm));
2258
2259 chcr_aead_dma_unmap(&u_ctx->lldi.pdev->dev, req, reqctx->op);
2260}
2261
2262static int chcr_aead_common_init(struct aead_request *req)
2263{
2264 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2265 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2266 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2267 unsigned int authsize = crypto_aead_authsize(tfm);
2268 int error = -EINVAL;
2269
2270 /* validate key size */
2271 if (aeadctx->enckey_len == 0)
2272 goto err;
2273 if (reqctx->op && req->cryptlen < authsize)
2274 goto err;
2275 if (reqctx->b0_len)
2276 reqctx->scratch_pad = reqctx->iv + IV;
2277 else
2278 reqctx->scratch_pad = NULL;
2279
2280 error = chcr_aead_dma_map(&ULD_CTX(a_ctx(tfm))->lldi.pdev->dev, req,
2281 reqctx->op);
2282 if (error) {
2283 error = -ENOMEM;
2284 goto err;
2285 }
David Brazdil0f672f62019-12-10 10:32:29 +00002286
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002287 return 0;
2288err:
2289 return error;
2290}
2291
2292static int chcr_aead_need_fallback(struct aead_request *req, int dst_nents,
2293 int aadmax, int wrlen,
2294 unsigned short op_type)
2295{
2296 unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
2297
2298 if (((req->cryptlen - (op_type ? authsize : 0)) == 0) ||
2299 dst_nents > MAX_DSGL_ENT ||
2300 (req->assoclen > aadmax) ||
2301 (wrlen > SGE_MAX_WR_LEN))
2302 return 1;
2303 return 0;
2304}
2305
2306static int chcr_aead_fallback(struct aead_request *req, unsigned short op_type)
2307{
2308 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2309 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2310 struct aead_request *subreq = aead_request_ctx(req);
2311
2312 aead_request_set_tfm(subreq, aeadctx->sw_cipher);
2313 aead_request_set_callback(subreq, req->base.flags,
2314 req->base.complete, req->base.data);
2315 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
2316 req->iv);
David Brazdil0f672f62019-12-10 10:32:29 +00002317 aead_request_set_ad(subreq, req->assoclen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002318 return op_type ? crypto_aead_decrypt(subreq) :
2319 crypto_aead_encrypt(subreq);
2320}
2321
2322static struct sk_buff *create_authenc_wr(struct aead_request *req,
2323 unsigned short qid,
2324 int size)
2325{
2326 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2327 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2328 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
2329 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2330 struct sk_buff *skb = NULL;
2331 struct chcr_wr *chcr_req;
2332 struct cpl_rx_phys_dsgl *phys_cpl;
2333 struct ulptx_sgl *ulptx;
2334 unsigned int transhdr_len;
2335 unsigned int dst_size = 0, temp, subtype = get_aead_subtype(tfm);
David Brazdil0f672f62019-12-10 10:32:29 +00002336 unsigned int kctx_len = 0, dnents, snents;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002337 unsigned int authsize = crypto_aead_authsize(tfm);
2338 int error = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00002339 u8 *ivptr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002340 int null = 0;
2341 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
2342 GFP_ATOMIC;
2343 struct adapter *adap = padap(a_ctx(tfm)->dev);
2344
2345 if (req->cryptlen == 0)
2346 return NULL;
2347
2348 reqctx->b0_len = 0;
2349 error = chcr_aead_common_init(req);
2350 if (error)
2351 return ERR_PTR(error);
2352
2353 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL ||
2354 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
2355 null = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002356 }
David Brazdil0f672f62019-12-10 10:32:29 +00002357 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen +
2358 (reqctx->op ? -authsize : authsize), CHCR_DST_SG_SIZE, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002359 dnents += MIN_AUTH_SG; // For IV
David Brazdil0f672f62019-12-10 10:32:29 +00002360 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
2361 CHCR_SRC_SG_SIZE, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002362 dst_size = get_space_for_phys_dsgl(dnents);
2363 kctx_len = (ntohl(KEY_CONTEXT_CTX_LEN_V(aeadctx->key_ctx_hdr)) << 4)
2364 - sizeof(chcr_req->key_ctx);
2365 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
David Brazdil0f672f62019-12-10 10:32:29 +00002366 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen) <
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002367 SGE_MAX_WR_LEN;
David Brazdil0f672f62019-12-10 10:32:29 +00002368 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen, 16)
2369 : (sgl_len(snents) * 8);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002370 transhdr_len += temp;
2371 transhdr_len = roundup(transhdr_len, 16);
2372
2373 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE,
2374 transhdr_len, reqctx->op)) {
2375 atomic_inc(&adap->chcr_stats.fallback);
2376 chcr_aead_common_exit(req);
2377 return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
2378 }
David Brazdil0f672f62019-12-10 10:32:29 +00002379 skb = alloc_skb(transhdr_len, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002380 if (!skb) {
2381 error = -ENOMEM;
2382 goto err;
2383 }
2384
2385 chcr_req = __skb_put_zero(skb, transhdr_len);
2386
2387 temp = (reqctx->op == CHCR_ENCRYPT_OP) ? 0 : authsize;
2388
2389 /*
2390 * Input order is AAD,IV and Payload. where IV should be included as
2391 * the part of authdata. All other fields should be filled according
2392 * to the hardware spec
2393 */
2394 chcr_req->sec_cpl.op_ivinsrtofst =
David Brazdil0f672f62019-12-10 10:32:29 +00002395 FILL_SEC_CPL_OP_IVINSR(a_ctx(tfm)->tx_chan_id, 2, 1);
2396 chcr_req->sec_cpl.pldlen = htonl(req->assoclen + IV + req->cryptlen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002397 chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
David Brazdil0f672f62019-12-10 10:32:29 +00002398 null ? 0 : 1 + IV,
2399 null ? 0 : IV + req->assoclen,
2400 req->assoclen + IV + 1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002401 (temp & 0x1F0) >> 4);
2402 chcr_req->sec_cpl.cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(
2403 temp & 0xF,
David Brazdil0f672f62019-12-10 10:32:29 +00002404 null ? 0 : req->assoclen + IV + 1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002405 temp, temp);
2406 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL ||
2407 subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA)
2408 temp = CHCR_SCMD_CIPHER_MODE_AES_CTR;
2409 else
2410 temp = CHCR_SCMD_CIPHER_MODE_AES_CBC;
2411 chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op,
2412 (reqctx->op == CHCR_ENCRYPT_OP) ? 1 : 0,
2413 temp,
2414 actx->auth_mode, aeadctx->hmac_ctrl,
2415 IV >> 1);
2416 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
2417 0, 0, dst_size);
2418
2419 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
2420 if (reqctx->op == CHCR_ENCRYPT_OP ||
2421 subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
2422 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL)
2423 memcpy(chcr_req->key_ctx.key, aeadctx->key,
2424 aeadctx->enckey_len);
2425 else
2426 memcpy(chcr_req->key_ctx.key, actx->dec_rrkey,
2427 aeadctx->enckey_len);
2428
2429 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
2430 actx->h_iopad, kctx_len - roundup(aeadctx->enckey_len, 16));
David Brazdil0f672f62019-12-10 10:32:29 +00002431 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
2432 ivptr = (u8 *)(phys_cpl + 1) + dst_size;
2433 ulptx = (struct ulptx_sgl *)(ivptr + IV);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002434 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
2435 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
David Brazdil0f672f62019-12-10 10:32:29 +00002436 memcpy(ivptr, aeadctx->nonce, CTR_RFC3686_NONCE_SIZE);
2437 memcpy(ivptr + CTR_RFC3686_NONCE_SIZE, req->iv,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002438 CTR_RFC3686_IV_SIZE);
David Brazdil0f672f62019-12-10 10:32:29 +00002439 *(__be32 *)(ivptr + CTR_RFC3686_NONCE_SIZE +
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002440 CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
2441 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00002442 memcpy(ivptr, req->iv, IV);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002443 }
David Brazdil0f672f62019-12-10 10:32:29 +00002444 chcr_add_aead_dst_ent(req, phys_cpl, qid);
2445 chcr_add_aead_src_ent(req, ulptx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002446 atomic_inc(&adap->chcr_stats.cipher_rqst);
David Brazdil0f672f62019-12-10 10:32:29 +00002447 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
2448 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen) : 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002449 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, size,
2450 transhdr_len, temp, 0);
2451 reqctx->skb = skb;
2452
2453 return skb;
2454err:
2455 chcr_aead_common_exit(req);
2456
2457 return ERR_PTR(error);
2458}
2459
2460int chcr_aead_dma_map(struct device *dev,
2461 struct aead_request *req,
2462 unsigned short op_type)
2463{
2464 int error;
2465 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2466 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2467 unsigned int authsize = crypto_aead_authsize(tfm);
2468 int dst_size;
2469
2470 dst_size = req->assoclen + req->cryptlen + (op_type ?
2471 -authsize : authsize);
2472 if (!req->cryptlen || !dst_size)
2473 return 0;
2474 reqctx->iv_dma = dma_map_single(dev, reqctx->iv, (IV + reqctx->b0_len),
2475 DMA_BIDIRECTIONAL);
2476 if (dma_mapping_error(dev, reqctx->iv_dma))
2477 return -ENOMEM;
2478 if (reqctx->b0_len)
2479 reqctx->b0_dma = reqctx->iv_dma + IV;
2480 else
2481 reqctx->b0_dma = 0;
2482 if (req->src == req->dst) {
Olivier Deprez0e641232021-09-23 10:07:05 +02002483 error = dma_map_sg(dev, req->src,
2484 sg_nents_for_len(req->src, dst_size),
2485 DMA_BIDIRECTIONAL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002486 if (!error)
2487 goto err;
2488 } else {
2489 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2490 DMA_TO_DEVICE);
2491 if (!error)
2492 goto err;
2493 error = dma_map_sg(dev, req->dst, sg_nents(req->dst),
2494 DMA_FROM_DEVICE);
2495 if (!error) {
2496 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2497 DMA_TO_DEVICE);
2498 goto err;
2499 }
2500 }
2501
2502 return 0;
2503err:
2504 dma_unmap_single(dev, reqctx->iv_dma, IV, DMA_BIDIRECTIONAL);
2505 return -ENOMEM;
2506}
2507
2508void chcr_aead_dma_unmap(struct device *dev,
2509 struct aead_request *req,
2510 unsigned short op_type)
2511{
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);
2515 int dst_size;
2516
2517 dst_size = req->assoclen + req->cryptlen + (op_type ?
2518 -authsize : authsize);
2519 if (!req->cryptlen || !dst_size)
2520 return;
2521
2522 dma_unmap_single(dev, reqctx->iv_dma, (IV + reqctx->b0_len),
2523 DMA_BIDIRECTIONAL);
2524 if (req->src == req->dst) {
2525 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2526 DMA_BIDIRECTIONAL);
2527 } else {
2528 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2529 DMA_TO_DEVICE);
2530 dma_unmap_sg(dev, req->dst, sg_nents(req->dst),
2531 DMA_FROM_DEVICE);
2532 }
2533}
2534
2535void chcr_add_aead_src_ent(struct aead_request *req,
David Brazdil0f672f62019-12-10 10:32:29 +00002536 struct ulptx_sgl *ulptx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002537{
2538 struct ulptx_walk ulp_walk;
2539 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2540
2541 if (reqctx->imm) {
2542 u8 *buf = (u8 *)ulptx;
2543
2544 if (reqctx->b0_len) {
2545 memcpy(buf, reqctx->scratch_pad, reqctx->b0_len);
2546 buf += reqctx->b0_len;
2547 }
2548 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
David Brazdil0f672f62019-12-10 10:32:29 +00002549 buf, req->cryptlen + req->assoclen, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002550 } else {
2551 ulptx_walk_init(&ulp_walk, ulptx);
2552 if (reqctx->b0_len)
2553 ulptx_walk_add_page(&ulp_walk, reqctx->b0_len,
David Brazdil0f672f62019-12-10 10:32:29 +00002554 reqctx->b0_dma);
2555 ulptx_walk_add_sg(&ulp_walk, req->src, req->cryptlen +
2556 req->assoclen, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002557 ulptx_walk_end(&ulp_walk);
2558 }
2559}
2560
2561void chcr_add_aead_dst_ent(struct aead_request *req,
2562 struct cpl_rx_phys_dsgl *phys_cpl,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002563 unsigned short qid)
2564{
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);
2570 u32 temp;
2571
2572 dsgl_walk_init(&dsgl_walk, phys_cpl);
David Brazdil0f672f62019-12-10 10:32:29 +00002573 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002577 dsgl_walk_end(&dsgl_walk, qid, ctx->pci_chan_id);
2578}
2579
2580void chcr_add_cipher_src_ent(struct ablkcipher_request *req,
2581 void *ulptx,
2582 struct cipher_wr_param *wrparam)
2583{
2584 struct ulptx_walk ulp_walk;
2585 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
2586 u8 *buf = ulptx;
2587
2588 memcpy(buf, reqctx->iv, IV);
2589 buf += IV;
2590 if (reqctx->imm) {
2591 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
2592 buf, wrparam->bytes, reqctx->processed);
2593 } else {
2594 ulptx_walk_init(&ulp_walk, (struct ulptx_sgl *)buf);
2595 ulptx_walk_add_sg(&ulp_walk, reqctx->srcsg, wrparam->bytes,
2596 reqctx->src_ofst);
2597 reqctx->srcsg = ulp_walk.last_sg;
2598 reqctx->src_ofst = ulp_walk.last_sg_len;
2599 ulptx_walk_end(&ulp_walk);
2600 }
2601}
2602
2603void chcr_add_cipher_dst_ent(struct ablkcipher_request *req,
2604 struct cpl_rx_phys_dsgl *phys_cpl,
2605 struct cipher_wr_param *wrparam,
2606 unsigned short qid)
2607{
2608 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
2609 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(wrparam->req);
2610 struct chcr_context *ctx = c_ctx(tfm);
2611 struct dsgl_walk dsgl_walk;
2612
2613 dsgl_walk_init(&dsgl_walk, phys_cpl);
2614 dsgl_walk_add_sg(&dsgl_walk, reqctx->dstsg, wrparam->bytes,
2615 reqctx->dst_ofst);
2616 reqctx->dstsg = dsgl_walk.last_sg;
2617 reqctx->dst_ofst = dsgl_walk.last_sg_len;
2618
2619 dsgl_walk_end(&dsgl_walk, qid, ctx->pci_chan_id);
2620}
2621
2622void chcr_add_hash_src_ent(struct ahash_request *req,
2623 struct ulptx_sgl *ulptx,
2624 struct hash_wr_param *param)
2625{
2626 struct ulptx_walk ulp_walk;
2627 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
2628
2629 if (reqctx->hctx_wr.imm) {
2630 u8 *buf = (u8 *)ulptx;
2631
2632 if (param->bfr_len) {
2633 memcpy(buf, reqctx->reqbfr, param->bfr_len);
2634 buf += param->bfr_len;
2635 }
2636
2637 sg_pcopy_to_buffer(reqctx->hctx_wr.srcsg,
2638 sg_nents(reqctx->hctx_wr.srcsg), buf,
2639 param->sg_len, 0);
2640 } else {
2641 ulptx_walk_init(&ulp_walk, ulptx);
2642 if (param->bfr_len)
2643 ulptx_walk_add_page(&ulp_walk, param->bfr_len,
David Brazdil0f672f62019-12-10 10:32:29 +00002644 reqctx->hctx_wr.dma_addr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002645 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);
2650 }
2651}
2652
2653int chcr_hash_dma_map(struct device *dev,
2654 struct ahash_request *req)
2655{
2656 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
2657 int error = 0;
2658
2659 if (!req->nbytes)
2660 return 0;
2661 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2662 DMA_TO_DEVICE);
2663 if (!error)
2664 return -ENOMEM;
2665 req_ctx->hctx_wr.is_sg_map = 1;
2666 return 0;
2667}
2668
2669void chcr_hash_dma_unmap(struct device *dev,
2670 struct ahash_request *req)
2671{
2672 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
2673
2674 if (!req->nbytes)
2675 return;
2676
2677 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2678 DMA_TO_DEVICE);
2679 req_ctx->hctx_wr.is_sg_map = 0;
2680
2681}
2682
2683int chcr_cipher_dma_map(struct device *dev,
2684 struct ablkcipher_request *req)
2685{
2686 int error;
2687
2688 if (req->src == req->dst) {
2689 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2690 DMA_BIDIRECTIONAL);
2691 if (!error)
2692 goto err;
2693 } else {
2694 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2695 DMA_TO_DEVICE);
2696 if (!error)
2697 goto err;
2698 error = dma_map_sg(dev, req->dst, sg_nents(req->dst),
2699 DMA_FROM_DEVICE);
2700 if (!error) {
2701 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2702 DMA_TO_DEVICE);
2703 goto err;
2704 }
2705 }
2706
2707 return 0;
2708err:
2709 return -ENOMEM;
2710}
2711
2712void chcr_cipher_dma_unmap(struct device *dev,
2713 struct ablkcipher_request *req)
2714{
2715 if (req->src == req->dst) {
2716 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2717 DMA_BIDIRECTIONAL);
2718 } else {
2719 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2720 DMA_TO_DEVICE);
2721 dma_unmap_sg(dev, req->dst, sg_nents(req->dst),
2722 DMA_FROM_DEVICE);
2723 }
2724}
2725
2726static int set_msg_len(u8 *block, unsigned int msglen, int csize)
2727{
2728 __be32 data;
2729
2730 memset(block, 0, csize);
2731 block += csize;
2732
2733 if (csize >= 4)
2734 csize = 4;
2735 else if (msglen > (unsigned int)(1 << (8 * csize)))
2736 return -EOVERFLOW;
2737
2738 data = cpu_to_be32(msglen);
2739 memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
2740
2741 return 0;
2742}
2743
David Brazdil0f672f62019-12-10 10:32:29 +00002744static int generate_b0(struct aead_request *req, u8 *ivptr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002745 unsigned short op_type)
2746{
2747 unsigned int l, lp, m;
2748 int rc;
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;
2752
2753 m = crypto_aead_authsize(aead);
2754
David Brazdil0f672f62019-12-10 10:32:29 +00002755 memcpy(b0, ivptr, 16);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002756
2757 lp = b0[0];
2758 l = lp + 1;
2759
2760 /* set m, bits 3-5 */
2761 *b0 |= (8 * ((m - 2) / 2));
2762
2763 /* set adata, bit 6, if associated data is used */
2764 if (req->assoclen)
2765 *b0 |= 64;
2766 rc = set_msg_len(b0 + 16 - l,
2767 (op_type == CHCR_DECRYPT_OP) ?
2768 req->cryptlen - m : req->cryptlen, l);
David Brazdil0f672f62019-12-10 10:32:29 +00002769
2770 return rc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002771}
2772
2773static inline int crypto_ccm_check_iv(const u8 *iv)
2774{
2775 /* 2 <= L <= 8, so 1 <= L' <= 7. */
2776 if (iv[0] < 1 || iv[0] > 7)
2777 return -EINVAL;
2778
2779 return 0;
2780}
2781
2782static int ccm_format_packet(struct aead_request *req,
David Brazdil0f672f62019-12-10 10:32:29 +00002783 u8 *ivptr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002784 unsigned int sub_type,
2785 unsigned short op_type,
2786 unsigned int assoclen)
2787{
2788 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
David Brazdil0f672f62019-12-10 10:32:29 +00002789 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2790 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002791 int rc = 0;
2792
2793 if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
David Brazdil0f672f62019-12-10 10:32:29 +00002794 ivptr[0] = 3;
2795 memcpy(ivptr + 1, &aeadctx->salt[0], 3);
2796 memcpy(ivptr + 4, req->iv, 8);
2797 memset(ivptr + 12, 0, 4);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002798 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00002799 memcpy(ivptr, req->iv, 16);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002800 }
2801 if (assoclen)
2802 *((unsigned short *)(reqctx->scratch_pad + 16)) =
2803 htons(assoclen);
2804
David Brazdil0f672f62019-12-10 10:32:29 +00002805 rc = generate_b0(req, ivptr, op_type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002806 /* zero the ctr value */
David Brazdil0f672f62019-12-10 10:32:29 +00002807 memset(ivptr + 15 - ivptr[0], 0, ivptr[0] + 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002808 return rc;
2809}
2810
2811static 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)
2815{
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;
David Brazdil0f672f62019-12-10 10:32:29 +00002820 unsigned int c_id = a_ctx(tfm)->tx_chan_id;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002821 unsigned int ccm_xtra;
Olivier Deprez0e641232021-09-23 10:07:05 +02002822 unsigned int tag_offset = 0, auth_offset = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002823 unsigned int assoclen;
2824
2825 if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
2826 assoclen = req->assoclen - 8;
2827 else
2828 assoclen = req->assoclen;
2829 ccm_xtra = CCM_B0_SIZE +
2830 ((assoclen) ? CCM_AAD_FIELD_SIZE : 0);
2831
2832 auth_offset = req->cryptlen ?
David Brazdil0f672f62019-12-10 10:32:29 +00002833 (req->assoclen + IV + 1 + ccm_xtra) : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002834 if (op_type == CHCR_DECRYPT_OP) {
2835 if (crypto_aead_authsize(tfm) != req->cryptlen)
2836 tag_offset = crypto_aead_authsize(tfm);
2837 else
2838 auth_offset = 0;
2839 }
2840
2841
2842 sec_cpl->op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(c_id,
David Brazdil0f672f62019-12-10 10:32:29 +00002843 2, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002844 sec_cpl->pldlen =
David Brazdil0f672f62019-12-10 10:32:29 +00002845 htonl(req->assoclen + IV + req->cryptlen + ccm_xtra);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002846 /* 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(
David Brazdil0f672f62019-12-10 10:32:29 +00002848 1 + IV, IV + assoclen + ccm_xtra,
2849 req->assoclen + IV + 1 + ccm_xtra, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002850
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);
2859
2860 sec_cpl->ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1, 0,
2861 0, dst_size);
2862}
2863
2864static 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)
2868{
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");
2872 return -EINVAL;
2873 }
2874 } else {
2875 if (req->assoclen != 16 && req->assoclen != 20) {
2876 pr_err("RFC4309: Invalid AAD length %d\n",
2877 req->assoclen);
2878 return -EINVAL;
2879 }
2880 }
2881 return 0;
2882}
2883
2884static struct sk_buff *create_aead_ccm_wr(struct aead_request *req,
2885 unsigned short qid,
2886 int size)
2887{
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;
David Brazdil0f672f62019-12-10 10:32:29 +00002896 unsigned int dst_size = 0, kctx_len, dnents, temp, snents;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002897 unsigned int sub_type, assoclen = req->assoclen;
2898 unsigned int authsize = crypto_aead_authsize(tfm);
2899 int error = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00002900 u8 *ivptr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002901 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
2902 GFP_ATOMIC;
2903 struct adapter *adap = padap(a_ctx(tfm)->dev);
2904
2905 sub_type = get_aead_subtype(tfm);
2906 if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
2907 assoclen -= 8;
2908 reqctx->b0_len = CCM_B0_SIZE + (assoclen ? CCM_AAD_FIELD_SIZE : 0);
2909 error = chcr_aead_common_init(req);
2910 if (error)
2911 return ERR_PTR(error);
2912
2913 error = aead_ccm_validate_input(reqctx->op, req, aeadctx, sub_type);
2914 if (error)
2915 goto err;
David Brazdil0f672f62019-12-10 10:32:29 +00002916 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002917 + (reqctx->op ? -authsize : authsize),
David Brazdil0f672f62019-12-10 10:32:29 +00002918 CHCR_DST_SG_SIZE, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002919 dnents += MIN_CCM_SG; // For IV and B0
2920 dst_size = get_space_for_phys_dsgl(dnents);
David Brazdil0f672f62019-12-10 10:32:29 +00002921 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
2922 CHCR_SRC_SG_SIZE, 0);
2923 snents += MIN_CCM_SG; //For B0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002924 kctx_len = roundup(aeadctx->enckey_len, 16) * 2;
2925 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
David Brazdil0f672f62019-12-10 10:32:29 +00002926 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen +
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002927 reqctx->b0_len) <= SGE_MAX_WR_LEN;
David Brazdil0f672f62019-12-10 10:32:29 +00002928 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen +
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002929 reqctx->b0_len, 16) :
David Brazdil0f672f62019-12-10 10:32:29 +00002930 (sgl_len(snents) * 8);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002931 transhdr_len += temp;
2932 transhdr_len = roundup(transhdr_len, 16);
2933
2934 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE -
David Brazdil0f672f62019-12-10 10:32:29 +00002935 reqctx->b0_len, transhdr_len, reqctx->op)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002936 atomic_inc(&adap->chcr_stats.fallback);
2937 chcr_aead_common_exit(req);
2938 return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
2939 }
David Brazdil0f672f62019-12-10 10:32:29 +00002940 skb = alloc_skb(transhdr_len, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002941
2942 if (!skb) {
2943 error = -ENOMEM;
2944 goto err;
2945 }
2946
David Brazdil0f672f62019-12-10 10:32:29 +00002947 chcr_req = __skb_put_zero(skb, transhdr_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002948
2949 fill_sec_cpl_for_aead(&chcr_req->sec_cpl, dst_size, req, reqctx->op);
2950
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);
2955
2956 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
David Brazdil0f672f62019-12-10 10:32:29 +00002957 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002960 if (error)
2961 goto dstmap_fail;
David Brazdil0f672f62019-12-10 10:32:29 +00002962 chcr_add_aead_dst_ent(req, phys_cpl, qid);
2963 chcr_add_aead_src_ent(req, ulptx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002964
2965 atomic_inc(&adap->chcr_stats.aead_rqst);
David Brazdil0f672f62019-12-10 10:32:29 +00002966 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
2967 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen +
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002968 reqctx->b0_len) : 0);
2969 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, 0,
2970 transhdr_len, temp, 0);
2971 reqctx->skb = skb;
2972
2973 return skb;
2974dstmap_fail:
2975 kfree_skb(skb);
2976err:
2977 chcr_aead_common_exit(req);
2978 return ERR_PTR(error);
2979}
2980
2981static struct sk_buff *create_gcm_wr(struct aead_request *req,
2982 unsigned short qid,
2983 int size)
2984{
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;
David Brazdil0f672f62019-12-10 10:32:29 +00002992 unsigned int transhdr_len, dnents = 0, snents;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002993 unsigned int dst_size = 0, temp = 0, kctx_len, assoclen = req->assoclen;
2994 unsigned int authsize = crypto_aead_authsize(tfm);
2995 int error = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00002996 u8 *ivptr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002997 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
2998 GFP_ATOMIC;
2999 struct adapter *adap = padap(a_ctx(tfm)->dev);
3000
3001 if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106)
3002 assoclen = req->assoclen - 8;
3003
3004 reqctx->b0_len = 0;
3005 error = chcr_aead_common_init(req);
3006 if (error)
3007 return ERR_PTR(error);
David Brazdil0f672f62019-12-10 10:32:29 +00003008 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen +
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003009 (reqctx->op ? -authsize : authsize),
David Brazdil0f672f62019-12-10 10:32:29 +00003010 CHCR_DST_SG_SIZE, 0);
3011 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
3012 CHCR_SRC_SG_SIZE, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003013 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);
David Brazdil0f672f62019-12-10 10:32:29 +00003017 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen) <=
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003018 SGE_MAX_WR_LEN;
David Brazdil0f672f62019-12-10 10:32:29 +00003019 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen, 16) :
3020 (sgl_len(snents) * 8);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003021 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)) {
3025
3026 atomic_inc(&adap->chcr_stats.fallback);
3027 chcr_aead_common_exit(req);
3028 return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
3029 }
David Brazdil0f672f62019-12-10 10:32:29 +00003030 skb = alloc_skb(transhdr_len, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003031 if (!skb) {
3032 error = -ENOMEM;
3033 goto err;
3034 }
3035
3036 chcr_req = __skb_put_zero(skb, transhdr_len);
3037
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(
David Brazdil0f672f62019-12-10 10:32:29 +00003041 a_ctx(tfm)->tx_chan_id, 2, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003042 chcr_req->sec_cpl.pldlen =
David Brazdil0f672f62019-12-10 10:32:29 +00003043 htonl(req->assoclen + IV + req->cryptlen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003044 chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
David Brazdil0f672f62019-12-10 10:32:29 +00003045 assoclen ? 1 + IV : 0,
3046 assoclen ? IV + assoclen : 0,
3047 req->assoclen + IV + 1, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003048 chcr_req->sec_cpl.cipherstop_lo_authinsert =
David Brazdil0f672f62019-12-10 10:32:29 +00003049 FILL_SEC_CPL_AUTHINSERT(0, req->assoclen + IV + 1,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003050 temp, temp);
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,
3058 0, 0, dst_size);
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);
3063
David Brazdil0f672f62019-12-10 10:32:29 +00003064 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
3065 ivptr = (u8 *)(phys_cpl + 1) + dst_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003066 /* 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) {
David Brazdil0f672f62019-12-10 10:32:29 +00003070 memcpy(ivptr, aeadctx->salt, 4);
3071 memcpy(ivptr + 4, req->iv, GCM_RFC4106_IV_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003072 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00003073 memcpy(ivptr, req->iv, GCM_AES_IV_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003074 }
David Brazdil0f672f62019-12-10 10:32:29 +00003075 *((unsigned int *)(ivptr + 12)) = htonl(0x01);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003076
David Brazdil0f672f62019-12-10 10:32:29 +00003077 ulptx = (struct ulptx_sgl *)(ivptr + 16);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003078
David Brazdil0f672f62019-12-10 10:32:29 +00003079 chcr_add_aead_dst_ent(req, phys_cpl, qid);
3080 chcr_add_aead_src_ent(req, ulptx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003081 atomic_inc(&adap->chcr_stats.aead_rqst);
David Brazdil0f672f62019-12-10 10:32:29 +00003082 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
3083 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen) : 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003084 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, size,
3085 transhdr_len, temp, reqctx->verify);
3086 reqctx->skb = skb;
3087 return skb;
3088
3089err:
3090 chcr_aead_common_exit(req);
3091 return ERR_PTR(error);
3092}
3093
3094
3095
3096static int chcr_aead_cra_init(struct crypto_aead *tfm)
3097{
3098 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3099 struct aead_alg *alg = crypto_aead_alg(tfm);
3100
3101 aeadctx->sw_cipher = crypto_alloc_aead(alg->base.cra_name, 0,
3102 CRYPTO_ALG_NEED_FALLBACK |
3103 CRYPTO_ALG_ASYNC);
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));
3110}
3111
3112static void chcr_aead_cra_exit(struct crypto_aead *tfm)
3113{
3114 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3115
3116 crypto_free_aead(aeadctx->sw_cipher);
3117}
3118
3119static int chcr_authenc_null_setauthsize(struct crypto_aead *tfm,
3120 unsigned int authsize)
3121{
3122 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3123
3124 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NOP;
3125 aeadctx->mayverify = VERIFY_HW;
3126 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3127}
3128static int chcr_authenc_setauthsize(struct crypto_aead *tfm,
3129 unsigned int authsize)
3130{
3131 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3132 u32 maxauth = crypto_aead_maxauthsize(tfm);
3133
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)
3137 */
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;
3159 } else {
3160 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3161 aeadctx->mayverify = VERIFY_SW;
3162 }
3163 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3164}
3165
3166
3167static int chcr_gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
3168{
3169 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3170
3171 switch (authsize) {
3172 case ICV_4:
3173 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
3174 aeadctx->mayverify = VERIFY_HW;
3175 break;
3176 case ICV_8:
3177 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3178 aeadctx->mayverify = VERIFY_HW;
3179 break;
3180 case ICV_12:
David Brazdil0f672f62019-12-10 10:32:29 +00003181 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3182 aeadctx->mayverify = VERIFY_HW;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003183 break;
3184 case ICV_14:
David Brazdil0f672f62019-12-10 10:32:29 +00003185 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
3186 aeadctx->mayverify = VERIFY_HW;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003187 break;
3188 case ICV_16:
3189 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3190 aeadctx->mayverify = VERIFY_HW;
3191 break;
3192 case ICV_13:
3193 case ICV_15:
3194 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3195 aeadctx->mayverify = VERIFY_SW;
3196 break;
3197 default:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003198 return -EINVAL;
3199 }
3200 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3201}
3202
3203static int chcr_4106_4309_setauthsize(struct crypto_aead *tfm,
3204 unsigned int authsize)
3205{
3206 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3207
3208 switch (authsize) {
3209 case ICV_8:
3210 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3211 aeadctx->mayverify = VERIFY_HW;
3212 break;
3213 case ICV_12:
3214 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3215 aeadctx->mayverify = VERIFY_HW;
3216 break;
3217 case ICV_16:
3218 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3219 aeadctx->mayverify = VERIFY_HW;
3220 break;
3221 default:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003222 return -EINVAL;
3223 }
3224 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3225}
3226
3227static int chcr_ccm_setauthsize(struct crypto_aead *tfm,
3228 unsigned int authsize)
3229{
3230 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3231
3232 switch (authsize) {
3233 case ICV_4:
3234 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
3235 aeadctx->mayverify = VERIFY_HW;
3236 break;
3237 case ICV_6:
3238 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
3239 aeadctx->mayverify = VERIFY_HW;
3240 break;
3241 case ICV_8:
3242 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3243 aeadctx->mayverify = VERIFY_HW;
3244 break;
3245 case ICV_10:
3246 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
3247 aeadctx->mayverify = VERIFY_HW;
3248 break;
3249 case ICV_12:
3250 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3251 aeadctx->mayverify = VERIFY_HW;
3252 break;
3253 case ICV_14:
3254 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
3255 aeadctx->mayverify = VERIFY_HW;
3256 break;
3257 case ICV_16:
3258 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3259 aeadctx->mayverify = VERIFY_HW;
3260 break;
3261 default:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003262 return -EINVAL;
3263 }
3264 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
3265}
3266
3267static int chcr_ccm_common_setkey(struct crypto_aead *aead,
3268 const u8 *key,
3269 unsigned int keylen)
3270{
3271 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
3272 unsigned char ck_size, mk_size;
3273 int key_ctx_size = 0;
3274
3275 key_ctx_size = sizeof(struct _key_ctx) + roundup(keylen, 16) * 2;
3276 if (keylen == AES_KEYSIZE_128) {
3277 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3278 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_128;
3279 } else if (keylen == AES_KEYSIZE_192) {
3280 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3281 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_192;
3282 } else if (keylen == AES_KEYSIZE_256) {
3283 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3284 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
3285 } else {
Olivier Deprez0e641232021-09-23 10:07:05 +02003286 crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003287 aeadctx->enckey_len = 0;
3288 return -EINVAL;
3289 }
3290 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, mk_size, 0, 0,
3291 key_ctx_size >> 4);
3292 memcpy(aeadctx->key, key, keylen);
3293 aeadctx->enckey_len = keylen;
3294
3295 return 0;
3296}
3297
3298static int chcr_aead_ccm_setkey(struct crypto_aead *aead,
3299 const u8 *key,
3300 unsigned int keylen)
3301{
3302 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
3303 int error;
3304
3305 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3306 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) &
3307 CRYPTO_TFM_REQ_MASK);
3308 error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3309 crypto_aead_clear_flags(aead, CRYPTO_TFM_RES_MASK);
3310 crypto_aead_set_flags(aead, crypto_aead_get_flags(aeadctx->sw_cipher) &
3311 CRYPTO_TFM_RES_MASK);
3312 if (error)
3313 return error;
3314 return chcr_ccm_common_setkey(aead, key, keylen);
3315}
3316
3317static int chcr_aead_rfc4309_setkey(struct crypto_aead *aead, const u8 *key,
3318 unsigned int keylen)
3319{
3320 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
3321 int error;
3322
3323 if (keylen < 3) {
Olivier Deprez0e641232021-09-23 10:07:05 +02003324 crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003325 aeadctx->enckey_len = 0;
3326 return -EINVAL;
3327 }
3328 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3329 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) &
3330 CRYPTO_TFM_REQ_MASK);
3331 error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3332 crypto_aead_clear_flags(aead, CRYPTO_TFM_RES_MASK);
3333 crypto_aead_set_flags(aead, crypto_aead_get_flags(aeadctx->sw_cipher) &
3334 CRYPTO_TFM_RES_MASK);
3335 if (error)
3336 return error;
3337 keylen -= 3;
3338 memcpy(aeadctx->salt, key + keylen, 3);
3339 return chcr_ccm_common_setkey(aead, key, keylen);
3340}
3341
3342static int chcr_gcm_setkey(struct crypto_aead *aead, const u8 *key,
3343 unsigned int keylen)
3344{
3345 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
3346 struct chcr_gcm_ctx *gctx = GCM_CTX(aeadctx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003347 unsigned int ck_size;
3348 int ret = 0, key_ctx_size = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00003349 struct crypto_aes_ctx aes;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003350
3351 aeadctx->enckey_len = 0;
3352 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3353 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead)
3354 & CRYPTO_TFM_REQ_MASK);
3355 ret = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3356 crypto_aead_clear_flags(aead, CRYPTO_TFM_RES_MASK);
3357 crypto_aead_set_flags(aead, crypto_aead_get_flags(aeadctx->sw_cipher) &
3358 CRYPTO_TFM_RES_MASK);
3359 if (ret)
3360 goto out;
3361
3362 if (get_aead_subtype(aead) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 &&
3363 keylen > 3) {
3364 keylen -= 4; /* nonce/salt is present in the last 4 bytes */
3365 memcpy(aeadctx->salt, key + keylen, 4);
3366 }
3367 if (keylen == AES_KEYSIZE_128) {
3368 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3369 } else if (keylen == AES_KEYSIZE_192) {
3370 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3371 } else if (keylen == AES_KEYSIZE_256) {
3372 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3373 } else {
Olivier Deprez0e641232021-09-23 10:07:05 +02003374 crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003375 pr_err("GCM: Invalid key length %d\n", keylen);
3376 ret = -EINVAL;
3377 goto out;
3378 }
3379
3380 memcpy(aeadctx->key, key, keylen);
3381 aeadctx->enckey_len = keylen;
3382 key_ctx_size = sizeof(struct _key_ctx) + roundup(keylen, 16) +
3383 AEAD_H_SIZE;
3384 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size,
3385 CHCR_KEYCTX_MAC_KEY_SIZE_128,
3386 0, 0,
3387 key_ctx_size >> 4);
3388 /* Calculate the H = CIPH(K, 0 repeated 16 times).
3389 * It will go in key context
3390 */
David Brazdil0f672f62019-12-10 10:32:29 +00003391 ret = aes_expandkey(&aes, key, keylen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003392 if (ret) {
3393 aeadctx->enckey_len = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00003394 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003395 }
3396 memset(gctx->ghash_h, 0, AEAD_H_SIZE);
David Brazdil0f672f62019-12-10 10:32:29 +00003397 aes_encrypt(&aes, gctx->ghash_h, gctx->ghash_h);
3398 memzero_explicit(&aes, sizeof(aes));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003399
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003400out:
3401 return ret;
3402}
3403
3404static int chcr_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
3405 unsigned int keylen)
3406{
3407 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(authenc));
3408 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
3409 /* it contains auth and cipher key both*/
3410 struct crypto_authenc_keys keys;
3411 unsigned int bs, subtype;
3412 unsigned int max_authsize = crypto_aead_alg(authenc)->maxauthsize;
3413 int err = 0, i, key_ctx_len = 0;
3414 unsigned char ck_size = 0;
3415 unsigned char pad[CHCR_HASH_MAX_BLOCK_SIZE_128] = { 0 };
3416 struct crypto_shash *base_hash = ERR_PTR(-EINVAL);
3417 struct algo_param param;
3418 int align;
3419 u8 *o_ptr = NULL;
3420
3421 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3422 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
3423 & CRYPTO_TFM_REQ_MASK);
3424 err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3425 crypto_aead_clear_flags(authenc, CRYPTO_TFM_RES_MASK);
3426 crypto_aead_set_flags(authenc, crypto_aead_get_flags(aeadctx->sw_cipher)
3427 & CRYPTO_TFM_RES_MASK);
3428 if (err)
3429 goto out;
3430
3431 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
3432 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
3433 goto out;
3434 }
3435
3436 if (get_alg_config(&param, max_authsize)) {
3437 pr_err("chcr : Unsupported digest size\n");
3438 goto out;
3439 }
3440 subtype = get_aead_subtype(authenc);
3441 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
3442 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
3443 if (keys.enckeylen < CTR_RFC3686_NONCE_SIZE)
3444 goto out;
3445 memcpy(aeadctx->nonce, keys.enckey + (keys.enckeylen
3446 - CTR_RFC3686_NONCE_SIZE), CTR_RFC3686_NONCE_SIZE);
3447 keys.enckeylen -= CTR_RFC3686_NONCE_SIZE;
3448 }
3449 if (keys.enckeylen == AES_KEYSIZE_128) {
3450 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3451 } else if (keys.enckeylen == AES_KEYSIZE_192) {
3452 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3453 } else if (keys.enckeylen == AES_KEYSIZE_256) {
3454 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3455 } else {
3456 pr_err("chcr : Unsupported cipher key\n");
3457 goto out;
3458 }
3459
3460 /* Copy only encryption key. We use authkey to generate h(ipad) and
3461 * h(opad) so authkey is not needed again. authkeylen size have the
3462 * size of the hash digest size.
3463 */
3464 memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
3465 aeadctx->enckey_len = keys.enckeylen;
3466 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_SHA ||
3467 subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL) {
3468
3469 get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
3470 aeadctx->enckey_len << 3);
3471 }
3472 base_hash = chcr_alloc_shash(max_authsize);
3473 if (IS_ERR(base_hash)) {
3474 pr_err("chcr : Base driver cannot be loaded\n");
3475 aeadctx->enckey_len = 0;
3476 memzero_explicit(&keys, sizeof(keys));
3477 return -EINVAL;
3478 }
3479 {
3480 SHASH_DESC_ON_STACK(shash, base_hash);
3481
3482 shash->tfm = base_hash;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003483 bs = crypto_shash_blocksize(base_hash);
3484 align = KEYCTX_ALIGN_PAD(max_authsize);
3485 o_ptr = actx->h_iopad + param.result_size + align;
3486
3487 if (keys.authkeylen > bs) {
3488 err = crypto_shash_digest(shash, keys.authkey,
3489 keys.authkeylen,
3490 o_ptr);
3491 if (err) {
3492 pr_err("chcr : Base driver cannot be loaded\n");
3493 goto out;
3494 }
3495 keys.authkeylen = max_authsize;
3496 } else
3497 memcpy(o_ptr, keys.authkey, keys.authkeylen);
3498
3499 /* Compute the ipad-digest*/
3500 memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
3501 memcpy(pad, o_ptr, keys.authkeylen);
3502 for (i = 0; i < bs >> 2; i++)
3503 *((unsigned int *)pad + i) ^= IPAD_DATA;
3504
3505 if (chcr_compute_partial_hash(shash, pad, actx->h_iopad,
3506 max_authsize))
3507 goto out;
3508 /* Compute the opad-digest */
3509 memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
3510 memcpy(pad, o_ptr, keys.authkeylen);
3511 for (i = 0; i < bs >> 2; i++)
3512 *((unsigned int *)pad + i) ^= OPAD_DATA;
3513
3514 if (chcr_compute_partial_hash(shash, pad, o_ptr, max_authsize))
3515 goto out;
3516
3517 /* convert the ipad and opad digest to network order */
3518 chcr_change_order(actx->h_iopad, param.result_size);
3519 chcr_change_order(o_ptr, param.result_size);
3520 key_ctx_len = sizeof(struct _key_ctx) +
3521 roundup(keys.enckeylen, 16) +
3522 (param.result_size + align) * 2;
3523 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, param.mk_size,
3524 0, 1, key_ctx_len >> 4);
3525 actx->auth_mode = param.auth_mode;
3526 chcr_free_shash(base_hash);
3527
3528 memzero_explicit(&keys, sizeof(keys));
3529 return 0;
3530 }
3531out:
3532 aeadctx->enckey_len = 0;
3533 memzero_explicit(&keys, sizeof(keys));
3534 if (!IS_ERR(base_hash))
3535 chcr_free_shash(base_hash);
3536 return -EINVAL;
3537}
3538
3539static int chcr_aead_digest_null_setkey(struct crypto_aead *authenc,
3540 const u8 *key, unsigned int keylen)
3541{
3542 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(authenc));
3543 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
3544 struct crypto_authenc_keys keys;
3545 int err;
3546 /* it contains auth and cipher key both*/
3547 unsigned int subtype;
3548 int key_ctx_len = 0;
3549 unsigned char ck_size = 0;
3550
3551 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3552 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
3553 & CRYPTO_TFM_REQ_MASK);
3554 err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3555 crypto_aead_clear_flags(authenc, CRYPTO_TFM_RES_MASK);
3556 crypto_aead_set_flags(authenc, crypto_aead_get_flags(aeadctx->sw_cipher)
3557 & CRYPTO_TFM_RES_MASK);
3558 if (err)
3559 goto out;
3560
3561 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
3562 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
3563 goto out;
3564 }
3565 subtype = get_aead_subtype(authenc);
3566 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
3567 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
3568 if (keys.enckeylen < CTR_RFC3686_NONCE_SIZE)
3569 goto out;
3570 memcpy(aeadctx->nonce, keys.enckey + (keys.enckeylen
3571 - CTR_RFC3686_NONCE_SIZE), CTR_RFC3686_NONCE_SIZE);
3572 keys.enckeylen -= CTR_RFC3686_NONCE_SIZE;
3573 }
3574 if (keys.enckeylen == AES_KEYSIZE_128) {
3575 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3576 } else if (keys.enckeylen == AES_KEYSIZE_192) {
3577 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3578 } else if (keys.enckeylen == AES_KEYSIZE_256) {
3579 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3580 } else {
3581 pr_err("chcr : Unsupported cipher key %d\n", keys.enckeylen);
3582 goto out;
3583 }
3584 memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
3585 aeadctx->enckey_len = keys.enckeylen;
3586 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_SHA ||
3587 subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL) {
3588 get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
3589 aeadctx->enckey_len << 3);
3590 }
3591 key_ctx_len = sizeof(struct _key_ctx) + roundup(keys.enckeylen, 16);
3592
3593 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY, 0,
3594 0, key_ctx_len >> 4);
3595 actx->auth_mode = CHCR_SCMD_AUTH_MODE_NOP;
3596 memzero_explicit(&keys, sizeof(keys));
3597 return 0;
3598out:
3599 aeadctx->enckey_len = 0;
3600 memzero_explicit(&keys, sizeof(keys));
3601 return -EINVAL;
3602}
3603
3604static int chcr_aead_op(struct aead_request *req,
3605 int size,
3606 create_wr_t create_wr_fn)
3607{
3608 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
David Brazdil0f672f62019-12-10 10:32:29 +00003609 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003610 struct uld_ctx *u_ctx;
3611 struct sk_buff *skb;
3612 int isfull = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00003613 struct chcr_dev *cdev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003614
David Brazdil0f672f62019-12-10 10:32:29 +00003615 cdev = a_ctx(tfm)->dev;
3616 if (!cdev) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003617 pr_err("chcr : %s : No crypto device.\n", __func__);
3618 return -ENXIO;
3619 }
David Brazdil0f672f62019-12-10 10:32:29 +00003620
3621 if (chcr_inc_wrcount(cdev)) {
3622 /* Detach state for CHCR means lldi or padap is freed.
3623 * We cannot increment fallback here.
3624 */
3625 return chcr_aead_fallback(req, reqctx->op);
3626 }
3627
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003628 u_ctx = ULD_CTX(a_ctx(tfm));
3629 if (cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
3630 a_ctx(tfm)->tx_qidx)) {
3631 isfull = 1;
David Brazdil0f672f62019-12-10 10:32:29 +00003632 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
3633 chcr_dec_wrcount(cdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003634 return -ENOSPC;
David Brazdil0f672f62019-12-10 10:32:29 +00003635 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003636 }
3637
3638 /* Form a WR from req */
3639 skb = create_wr_fn(req, u_ctx->lldi.rxq_ids[a_ctx(tfm)->rx_qidx], size);
3640
David Brazdil0f672f62019-12-10 10:32:29 +00003641 if (IS_ERR_OR_NULL(skb)) {
3642 chcr_dec_wrcount(cdev);
3643 return PTR_ERR_OR_ZERO(skb);
3644 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003645
3646 skb->dev = u_ctx->lldi.ports[0];
3647 set_wr_txq(skb, CPL_PRIORITY_DATA, a_ctx(tfm)->tx_qidx);
3648 chcr_send_wr(skb);
3649 return isfull ? -EBUSY : -EINPROGRESS;
3650}
3651
3652static int chcr_aead_encrypt(struct aead_request *req)
3653{
3654 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3655 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
3656
3657 reqctx->verify = VERIFY_HW;
3658 reqctx->op = CHCR_ENCRYPT_OP;
3659
3660 switch (get_aead_subtype(tfm)) {
3661 case CRYPTO_ALG_SUB_TYPE_CTR_SHA:
3662 case CRYPTO_ALG_SUB_TYPE_CBC_SHA:
3663 case CRYPTO_ALG_SUB_TYPE_CBC_NULL:
3664 case CRYPTO_ALG_SUB_TYPE_CTR_NULL:
3665 return chcr_aead_op(req, 0, create_authenc_wr);
3666 case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
3667 case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
3668 return chcr_aead_op(req, 0, create_aead_ccm_wr);
3669 default:
3670 return chcr_aead_op(req, 0, create_gcm_wr);
3671 }
3672}
3673
3674static int chcr_aead_decrypt(struct aead_request *req)
3675{
3676 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3677 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
3678 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
3679 int size;
3680
3681 if (aeadctx->mayverify == VERIFY_SW) {
3682 size = crypto_aead_maxauthsize(tfm);
3683 reqctx->verify = VERIFY_SW;
3684 } else {
3685 size = 0;
3686 reqctx->verify = VERIFY_HW;
3687 }
3688 reqctx->op = CHCR_DECRYPT_OP;
3689 switch (get_aead_subtype(tfm)) {
3690 case CRYPTO_ALG_SUB_TYPE_CBC_SHA:
3691 case CRYPTO_ALG_SUB_TYPE_CTR_SHA:
3692 case CRYPTO_ALG_SUB_TYPE_CBC_NULL:
3693 case CRYPTO_ALG_SUB_TYPE_CTR_NULL:
3694 return chcr_aead_op(req, size, create_authenc_wr);
3695 case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
3696 case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
3697 return chcr_aead_op(req, size, create_aead_ccm_wr);
3698 default:
3699 return chcr_aead_op(req, size, create_gcm_wr);
3700 }
3701}
3702
3703static struct chcr_alg_template driver_algs[] = {
3704 /* AES-CBC */
3705 {
3706 .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_SUB_TYPE_CBC,
3707 .is_registered = 0,
3708 .alg.crypto = {
3709 .cra_name = "cbc(aes)",
3710 .cra_driver_name = "cbc-aes-chcr",
3711 .cra_blocksize = AES_BLOCK_SIZE,
3712 .cra_init = chcr_cra_init,
3713 .cra_exit = chcr_cra_exit,
3714 .cra_u.ablkcipher = {
3715 .min_keysize = AES_MIN_KEY_SIZE,
3716 .max_keysize = AES_MAX_KEY_SIZE,
3717 .ivsize = AES_BLOCK_SIZE,
3718 .setkey = chcr_aes_cbc_setkey,
3719 .encrypt = chcr_aes_encrypt,
3720 .decrypt = chcr_aes_decrypt,
3721 }
3722 }
3723 },
3724 {
3725 .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_SUB_TYPE_XTS,
3726 .is_registered = 0,
3727 .alg.crypto = {
3728 .cra_name = "xts(aes)",
3729 .cra_driver_name = "xts-aes-chcr",
3730 .cra_blocksize = AES_BLOCK_SIZE,
3731 .cra_init = chcr_cra_init,
3732 .cra_exit = NULL,
3733 .cra_u .ablkcipher = {
3734 .min_keysize = 2 * AES_MIN_KEY_SIZE,
3735 .max_keysize = 2 * AES_MAX_KEY_SIZE,
3736 .ivsize = AES_BLOCK_SIZE,
3737 .setkey = chcr_aes_xts_setkey,
3738 .encrypt = chcr_aes_encrypt,
3739 .decrypt = chcr_aes_decrypt,
3740 }
3741 }
3742 },
3743 {
3744 .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_SUB_TYPE_CTR,
3745 .is_registered = 0,
3746 .alg.crypto = {
3747 .cra_name = "ctr(aes)",
3748 .cra_driver_name = "ctr-aes-chcr",
3749 .cra_blocksize = 1,
3750 .cra_init = chcr_cra_init,
3751 .cra_exit = chcr_cra_exit,
3752 .cra_u.ablkcipher = {
3753 .min_keysize = AES_MIN_KEY_SIZE,
3754 .max_keysize = AES_MAX_KEY_SIZE,
3755 .ivsize = AES_BLOCK_SIZE,
3756 .setkey = chcr_aes_ctr_setkey,
3757 .encrypt = chcr_aes_encrypt,
3758 .decrypt = chcr_aes_decrypt,
3759 }
3760 }
3761 },
3762 {
3763 .type = CRYPTO_ALG_TYPE_ABLKCIPHER |
3764 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686,
3765 .is_registered = 0,
3766 .alg.crypto = {
3767 .cra_name = "rfc3686(ctr(aes))",
3768 .cra_driver_name = "rfc3686-ctr-aes-chcr",
3769 .cra_blocksize = 1,
3770 .cra_init = chcr_rfc3686_init,
3771 .cra_exit = chcr_cra_exit,
3772 .cra_u.ablkcipher = {
3773 .min_keysize = AES_MIN_KEY_SIZE +
3774 CTR_RFC3686_NONCE_SIZE,
3775 .max_keysize = AES_MAX_KEY_SIZE +
3776 CTR_RFC3686_NONCE_SIZE,
3777 .ivsize = CTR_RFC3686_IV_SIZE,
3778 .setkey = chcr_aes_rfc3686_setkey,
3779 .encrypt = chcr_aes_encrypt,
3780 .decrypt = chcr_aes_decrypt,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003781 }
3782 }
3783 },
3784 /* SHA */
3785 {
3786 .type = CRYPTO_ALG_TYPE_AHASH,
3787 .is_registered = 0,
3788 .alg.hash = {
3789 .halg.digestsize = SHA1_DIGEST_SIZE,
3790 .halg.base = {
3791 .cra_name = "sha1",
3792 .cra_driver_name = "sha1-chcr",
3793 .cra_blocksize = SHA1_BLOCK_SIZE,
3794 }
3795 }
3796 },
3797 {
3798 .type = CRYPTO_ALG_TYPE_AHASH,
3799 .is_registered = 0,
3800 .alg.hash = {
3801 .halg.digestsize = SHA256_DIGEST_SIZE,
3802 .halg.base = {
3803 .cra_name = "sha256",
3804 .cra_driver_name = "sha256-chcr",
3805 .cra_blocksize = SHA256_BLOCK_SIZE,
3806 }
3807 }
3808 },
3809 {
3810 .type = CRYPTO_ALG_TYPE_AHASH,
3811 .is_registered = 0,
3812 .alg.hash = {
3813 .halg.digestsize = SHA224_DIGEST_SIZE,
3814 .halg.base = {
3815 .cra_name = "sha224",
3816 .cra_driver_name = "sha224-chcr",
3817 .cra_blocksize = SHA224_BLOCK_SIZE,
3818 }
3819 }
3820 },
3821 {
3822 .type = CRYPTO_ALG_TYPE_AHASH,
3823 .is_registered = 0,
3824 .alg.hash = {
3825 .halg.digestsize = SHA384_DIGEST_SIZE,
3826 .halg.base = {
3827 .cra_name = "sha384",
3828 .cra_driver_name = "sha384-chcr",
3829 .cra_blocksize = SHA384_BLOCK_SIZE,
3830 }
3831 }
3832 },
3833 {
3834 .type = CRYPTO_ALG_TYPE_AHASH,
3835 .is_registered = 0,
3836 .alg.hash = {
3837 .halg.digestsize = SHA512_DIGEST_SIZE,
3838 .halg.base = {
3839 .cra_name = "sha512",
3840 .cra_driver_name = "sha512-chcr",
3841 .cra_blocksize = SHA512_BLOCK_SIZE,
3842 }
3843 }
3844 },
3845 /* HMAC */
3846 {
3847 .type = CRYPTO_ALG_TYPE_HMAC,
3848 .is_registered = 0,
3849 .alg.hash = {
3850 .halg.digestsize = SHA1_DIGEST_SIZE,
3851 .halg.base = {
3852 .cra_name = "hmac(sha1)",
3853 .cra_driver_name = "hmac-sha1-chcr",
3854 .cra_blocksize = SHA1_BLOCK_SIZE,
3855 }
3856 }
3857 },
3858 {
3859 .type = CRYPTO_ALG_TYPE_HMAC,
3860 .is_registered = 0,
3861 .alg.hash = {
3862 .halg.digestsize = SHA224_DIGEST_SIZE,
3863 .halg.base = {
3864 .cra_name = "hmac(sha224)",
3865 .cra_driver_name = "hmac-sha224-chcr",
3866 .cra_blocksize = SHA224_BLOCK_SIZE,
3867 }
3868 }
3869 },
3870 {
3871 .type = CRYPTO_ALG_TYPE_HMAC,
3872 .is_registered = 0,
3873 .alg.hash = {
3874 .halg.digestsize = SHA256_DIGEST_SIZE,
3875 .halg.base = {
3876 .cra_name = "hmac(sha256)",
3877 .cra_driver_name = "hmac-sha256-chcr",
3878 .cra_blocksize = SHA256_BLOCK_SIZE,
3879 }
3880 }
3881 },
3882 {
3883 .type = CRYPTO_ALG_TYPE_HMAC,
3884 .is_registered = 0,
3885 .alg.hash = {
3886 .halg.digestsize = SHA384_DIGEST_SIZE,
3887 .halg.base = {
3888 .cra_name = "hmac(sha384)",
3889 .cra_driver_name = "hmac-sha384-chcr",
3890 .cra_blocksize = SHA384_BLOCK_SIZE,
3891 }
3892 }
3893 },
3894 {
3895 .type = CRYPTO_ALG_TYPE_HMAC,
3896 .is_registered = 0,
3897 .alg.hash = {
3898 .halg.digestsize = SHA512_DIGEST_SIZE,
3899 .halg.base = {
3900 .cra_name = "hmac(sha512)",
3901 .cra_driver_name = "hmac-sha512-chcr",
3902 .cra_blocksize = SHA512_BLOCK_SIZE,
3903 }
3904 }
3905 },
3906 /* Add AEAD Algorithms */
3907 {
3908 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_GCM,
3909 .is_registered = 0,
3910 .alg.aead = {
3911 .base = {
3912 .cra_name = "gcm(aes)",
3913 .cra_driver_name = "gcm-aes-chcr",
3914 .cra_blocksize = 1,
3915 .cra_priority = CHCR_AEAD_PRIORITY,
3916 .cra_ctxsize = sizeof(struct chcr_context) +
3917 sizeof(struct chcr_aead_ctx) +
3918 sizeof(struct chcr_gcm_ctx),
3919 },
3920 .ivsize = GCM_AES_IV_SIZE,
3921 .maxauthsize = GHASH_DIGEST_SIZE,
3922 .setkey = chcr_gcm_setkey,
3923 .setauthsize = chcr_gcm_setauthsize,
3924 }
3925 },
3926 {
3927 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106,
3928 .is_registered = 0,
3929 .alg.aead = {
3930 .base = {
3931 .cra_name = "rfc4106(gcm(aes))",
3932 .cra_driver_name = "rfc4106-gcm-aes-chcr",
3933 .cra_blocksize = 1,
3934 .cra_priority = CHCR_AEAD_PRIORITY + 1,
3935 .cra_ctxsize = sizeof(struct chcr_context) +
3936 sizeof(struct chcr_aead_ctx) +
3937 sizeof(struct chcr_gcm_ctx),
3938
3939 },
3940 .ivsize = GCM_RFC4106_IV_SIZE,
3941 .maxauthsize = GHASH_DIGEST_SIZE,
3942 .setkey = chcr_gcm_setkey,
3943 .setauthsize = chcr_4106_4309_setauthsize,
3944 }
3945 },
3946 {
3947 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_CCM,
3948 .is_registered = 0,
3949 .alg.aead = {
3950 .base = {
3951 .cra_name = "ccm(aes)",
3952 .cra_driver_name = "ccm-aes-chcr",
3953 .cra_blocksize = 1,
3954 .cra_priority = CHCR_AEAD_PRIORITY,
3955 .cra_ctxsize = sizeof(struct chcr_context) +
3956 sizeof(struct chcr_aead_ctx),
3957
3958 },
3959 .ivsize = AES_BLOCK_SIZE,
3960 .maxauthsize = GHASH_DIGEST_SIZE,
3961 .setkey = chcr_aead_ccm_setkey,
3962 .setauthsize = chcr_ccm_setauthsize,
3963 }
3964 },
3965 {
3966 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309,
3967 .is_registered = 0,
3968 .alg.aead = {
3969 .base = {
3970 .cra_name = "rfc4309(ccm(aes))",
3971 .cra_driver_name = "rfc4309-ccm-aes-chcr",
3972 .cra_blocksize = 1,
3973 .cra_priority = CHCR_AEAD_PRIORITY + 1,
3974 .cra_ctxsize = sizeof(struct chcr_context) +
3975 sizeof(struct chcr_aead_ctx),
3976
3977 },
3978 .ivsize = 8,
3979 .maxauthsize = GHASH_DIGEST_SIZE,
3980 .setkey = chcr_aead_rfc4309_setkey,
3981 .setauthsize = chcr_4106_4309_setauthsize,
3982 }
3983 },
3984 {
3985 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
3986 .is_registered = 0,
3987 .alg.aead = {
3988 .base = {
3989 .cra_name = "authenc(hmac(sha1),cbc(aes))",
3990 .cra_driver_name =
3991 "authenc-hmac-sha1-cbc-aes-chcr",
3992 .cra_blocksize = AES_BLOCK_SIZE,
3993 .cra_priority = CHCR_AEAD_PRIORITY,
3994 .cra_ctxsize = sizeof(struct chcr_context) +
3995 sizeof(struct chcr_aead_ctx) +
3996 sizeof(struct chcr_authenc_ctx),
3997
3998 },
3999 .ivsize = AES_BLOCK_SIZE,
4000 .maxauthsize = SHA1_DIGEST_SIZE,
4001 .setkey = chcr_authenc_setkey,
4002 .setauthsize = chcr_authenc_setauthsize,
4003 }
4004 },
4005 {
4006 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4007 .is_registered = 0,
4008 .alg.aead = {
4009 .base = {
4010
4011 .cra_name = "authenc(hmac(sha256),cbc(aes))",
4012 .cra_driver_name =
4013 "authenc-hmac-sha256-cbc-aes-chcr",
4014 .cra_blocksize = AES_BLOCK_SIZE,
4015 .cra_priority = CHCR_AEAD_PRIORITY,
4016 .cra_ctxsize = sizeof(struct chcr_context) +
4017 sizeof(struct chcr_aead_ctx) +
4018 sizeof(struct chcr_authenc_ctx),
4019
4020 },
4021 .ivsize = AES_BLOCK_SIZE,
4022 .maxauthsize = SHA256_DIGEST_SIZE,
4023 .setkey = chcr_authenc_setkey,
4024 .setauthsize = chcr_authenc_setauthsize,
4025 }
4026 },
4027 {
4028 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4029 .is_registered = 0,
4030 .alg.aead = {
4031 .base = {
4032 .cra_name = "authenc(hmac(sha224),cbc(aes))",
4033 .cra_driver_name =
4034 "authenc-hmac-sha224-cbc-aes-chcr",
4035 .cra_blocksize = AES_BLOCK_SIZE,
4036 .cra_priority = CHCR_AEAD_PRIORITY,
4037 .cra_ctxsize = sizeof(struct chcr_context) +
4038 sizeof(struct chcr_aead_ctx) +
4039 sizeof(struct chcr_authenc_ctx),
4040 },
4041 .ivsize = AES_BLOCK_SIZE,
4042 .maxauthsize = SHA224_DIGEST_SIZE,
4043 .setkey = chcr_authenc_setkey,
4044 .setauthsize = chcr_authenc_setauthsize,
4045 }
4046 },
4047 {
4048 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4049 .is_registered = 0,
4050 .alg.aead = {
4051 .base = {
4052 .cra_name = "authenc(hmac(sha384),cbc(aes))",
4053 .cra_driver_name =
4054 "authenc-hmac-sha384-cbc-aes-chcr",
4055 .cra_blocksize = AES_BLOCK_SIZE,
4056 .cra_priority = CHCR_AEAD_PRIORITY,
4057 .cra_ctxsize = sizeof(struct chcr_context) +
4058 sizeof(struct chcr_aead_ctx) +
4059 sizeof(struct chcr_authenc_ctx),
4060
4061 },
4062 .ivsize = AES_BLOCK_SIZE,
4063 .maxauthsize = SHA384_DIGEST_SIZE,
4064 .setkey = chcr_authenc_setkey,
4065 .setauthsize = chcr_authenc_setauthsize,
4066 }
4067 },
4068 {
4069 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
4070 .is_registered = 0,
4071 .alg.aead = {
4072 .base = {
4073 .cra_name = "authenc(hmac(sha512),cbc(aes))",
4074 .cra_driver_name =
4075 "authenc-hmac-sha512-cbc-aes-chcr",
4076 .cra_blocksize = AES_BLOCK_SIZE,
4077 .cra_priority = CHCR_AEAD_PRIORITY,
4078 .cra_ctxsize = sizeof(struct chcr_context) +
4079 sizeof(struct chcr_aead_ctx) +
4080 sizeof(struct chcr_authenc_ctx),
4081
4082 },
4083 .ivsize = AES_BLOCK_SIZE,
4084 .maxauthsize = SHA512_DIGEST_SIZE,
4085 .setkey = chcr_authenc_setkey,
4086 .setauthsize = chcr_authenc_setauthsize,
4087 }
4088 },
4089 {
4090 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_NULL,
4091 .is_registered = 0,
4092 .alg.aead = {
4093 .base = {
4094 .cra_name = "authenc(digest_null,cbc(aes))",
4095 .cra_driver_name =
4096 "authenc-digest_null-cbc-aes-chcr",
4097 .cra_blocksize = AES_BLOCK_SIZE,
4098 .cra_priority = CHCR_AEAD_PRIORITY,
4099 .cra_ctxsize = sizeof(struct chcr_context) +
4100 sizeof(struct chcr_aead_ctx) +
4101 sizeof(struct chcr_authenc_ctx),
4102
4103 },
4104 .ivsize = AES_BLOCK_SIZE,
4105 .maxauthsize = 0,
4106 .setkey = chcr_aead_digest_null_setkey,
4107 .setauthsize = chcr_authenc_null_setauthsize,
4108 }
4109 },
4110 {
4111 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4112 .is_registered = 0,
4113 .alg.aead = {
4114 .base = {
4115 .cra_name = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4116 .cra_driver_name =
4117 "authenc-hmac-sha1-rfc3686-ctr-aes-chcr",
4118 .cra_blocksize = 1,
4119 .cra_priority = CHCR_AEAD_PRIORITY,
4120 .cra_ctxsize = sizeof(struct chcr_context) +
4121 sizeof(struct chcr_aead_ctx) +
4122 sizeof(struct chcr_authenc_ctx),
4123
4124 },
4125 .ivsize = CTR_RFC3686_IV_SIZE,
4126 .maxauthsize = SHA1_DIGEST_SIZE,
4127 .setkey = chcr_authenc_setkey,
4128 .setauthsize = chcr_authenc_setauthsize,
4129 }
4130 },
4131 {
4132 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4133 .is_registered = 0,
4134 .alg.aead = {
4135 .base = {
4136
4137 .cra_name = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4138 .cra_driver_name =
4139 "authenc-hmac-sha256-rfc3686-ctr-aes-chcr",
4140 .cra_blocksize = 1,
4141 .cra_priority = CHCR_AEAD_PRIORITY,
4142 .cra_ctxsize = sizeof(struct chcr_context) +
4143 sizeof(struct chcr_aead_ctx) +
4144 sizeof(struct chcr_authenc_ctx),
4145
4146 },
4147 .ivsize = CTR_RFC3686_IV_SIZE,
4148 .maxauthsize = SHA256_DIGEST_SIZE,
4149 .setkey = chcr_authenc_setkey,
4150 .setauthsize = chcr_authenc_setauthsize,
4151 }
4152 },
4153 {
4154 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4155 .is_registered = 0,
4156 .alg.aead = {
4157 .base = {
4158 .cra_name = "authenc(hmac(sha224),rfc3686(ctr(aes)))",
4159 .cra_driver_name =
4160 "authenc-hmac-sha224-rfc3686-ctr-aes-chcr",
4161 .cra_blocksize = 1,
4162 .cra_priority = CHCR_AEAD_PRIORITY,
4163 .cra_ctxsize = sizeof(struct chcr_context) +
4164 sizeof(struct chcr_aead_ctx) +
4165 sizeof(struct chcr_authenc_ctx),
4166 },
4167 .ivsize = CTR_RFC3686_IV_SIZE,
4168 .maxauthsize = SHA224_DIGEST_SIZE,
4169 .setkey = chcr_authenc_setkey,
4170 .setauthsize = chcr_authenc_setauthsize,
4171 }
4172 },
4173 {
4174 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4175 .is_registered = 0,
4176 .alg.aead = {
4177 .base = {
4178 .cra_name = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4179 .cra_driver_name =
4180 "authenc-hmac-sha384-rfc3686-ctr-aes-chcr",
4181 .cra_blocksize = 1,
4182 .cra_priority = CHCR_AEAD_PRIORITY,
4183 .cra_ctxsize = sizeof(struct chcr_context) +
4184 sizeof(struct chcr_aead_ctx) +
4185 sizeof(struct chcr_authenc_ctx),
4186
4187 },
4188 .ivsize = CTR_RFC3686_IV_SIZE,
4189 .maxauthsize = SHA384_DIGEST_SIZE,
4190 .setkey = chcr_authenc_setkey,
4191 .setauthsize = chcr_authenc_setauthsize,
4192 }
4193 },
4194 {
4195 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4196 .is_registered = 0,
4197 .alg.aead = {
4198 .base = {
4199 .cra_name = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4200 .cra_driver_name =
4201 "authenc-hmac-sha512-rfc3686-ctr-aes-chcr",
4202 .cra_blocksize = 1,
4203 .cra_priority = CHCR_AEAD_PRIORITY,
4204 .cra_ctxsize = sizeof(struct chcr_context) +
4205 sizeof(struct chcr_aead_ctx) +
4206 sizeof(struct chcr_authenc_ctx),
4207
4208 },
4209 .ivsize = CTR_RFC3686_IV_SIZE,
4210 .maxauthsize = SHA512_DIGEST_SIZE,
4211 .setkey = chcr_authenc_setkey,
4212 .setauthsize = chcr_authenc_setauthsize,
4213 }
4214 },
4215 {
4216 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_NULL,
4217 .is_registered = 0,
4218 .alg.aead = {
4219 .base = {
4220 .cra_name = "authenc(digest_null,rfc3686(ctr(aes)))",
4221 .cra_driver_name =
4222 "authenc-digest_null-rfc3686-ctr-aes-chcr",
4223 .cra_blocksize = 1,
4224 .cra_priority = CHCR_AEAD_PRIORITY,
4225 .cra_ctxsize = sizeof(struct chcr_context) +
4226 sizeof(struct chcr_aead_ctx) +
4227 sizeof(struct chcr_authenc_ctx),
4228
4229 },
4230 .ivsize = CTR_RFC3686_IV_SIZE,
4231 .maxauthsize = 0,
4232 .setkey = chcr_aead_digest_null_setkey,
4233 .setauthsize = chcr_authenc_null_setauthsize,
4234 }
4235 },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004236};
4237
4238/*
4239 * chcr_unregister_alg - Deregister crypto algorithms with
4240 * kernel framework.
4241 */
4242static int chcr_unregister_alg(void)
4243{
4244 int i;
4245
4246 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4247 switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
4248 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4249 if (driver_algs[i].is_registered)
4250 crypto_unregister_alg(
4251 &driver_algs[i].alg.crypto);
4252 break;
4253 case CRYPTO_ALG_TYPE_AEAD:
4254 if (driver_algs[i].is_registered)
4255 crypto_unregister_aead(
4256 &driver_algs[i].alg.aead);
4257 break;
4258 case CRYPTO_ALG_TYPE_AHASH:
4259 if (driver_algs[i].is_registered)
4260 crypto_unregister_ahash(
4261 &driver_algs[i].alg.hash);
4262 break;
4263 }
4264 driver_algs[i].is_registered = 0;
4265 }
4266 return 0;
4267}
4268
4269#define SZ_AHASH_CTX sizeof(struct chcr_context)
4270#define SZ_AHASH_H_CTX (sizeof(struct chcr_context) + sizeof(struct hmac_ctx))
4271#define SZ_AHASH_REQ_CTX sizeof(struct chcr_ahash_req_ctx)
4272
4273/*
4274 * chcr_register_alg - Register crypto algorithms with kernel framework.
4275 */
4276static int chcr_register_alg(void)
4277{
4278 struct crypto_alg ai;
4279 struct ahash_alg *a_hash;
4280 int err = 0, i;
4281 char *name = NULL;
4282
4283 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4284 if (driver_algs[i].is_registered)
4285 continue;
4286 switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
4287 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4288 driver_algs[i].alg.crypto.cra_priority =
4289 CHCR_CRA_PRIORITY;
4290 driver_algs[i].alg.crypto.cra_module = THIS_MODULE;
4291 driver_algs[i].alg.crypto.cra_flags =
4292 CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
4293 CRYPTO_ALG_NEED_FALLBACK;
4294 driver_algs[i].alg.crypto.cra_ctxsize =
4295 sizeof(struct chcr_context) +
4296 sizeof(struct ablk_ctx);
4297 driver_algs[i].alg.crypto.cra_alignmask = 0;
4298 driver_algs[i].alg.crypto.cra_type =
4299 &crypto_ablkcipher_type;
4300 err = crypto_register_alg(&driver_algs[i].alg.crypto);
4301 name = driver_algs[i].alg.crypto.cra_driver_name;
4302 break;
4303 case CRYPTO_ALG_TYPE_AEAD:
4304 driver_algs[i].alg.aead.base.cra_flags =
4305 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK;
4306 driver_algs[i].alg.aead.encrypt = chcr_aead_encrypt;
4307 driver_algs[i].alg.aead.decrypt = chcr_aead_decrypt;
4308 driver_algs[i].alg.aead.init = chcr_aead_cra_init;
4309 driver_algs[i].alg.aead.exit = chcr_aead_cra_exit;
4310 driver_algs[i].alg.aead.base.cra_module = THIS_MODULE;
4311 err = crypto_register_aead(&driver_algs[i].alg.aead);
4312 name = driver_algs[i].alg.aead.base.cra_driver_name;
4313 break;
4314 case CRYPTO_ALG_TYPE_AHASH:
4315 a_hash = &driver_algs[i].alg.hash;
4316 a_hash->update = chcr_ahash_update;
4317 a_hash->final = chcr_ahash_final;
4318 a_hash->finup = chcr_ahash_finup;
4319 a_hash->digest = chcr_ahash_digest;
4320 a_hash->export = chcr_ahash_export;
4321 a_hash->import = chcr_ahash_import;
4322 a_hash->halg.statesize = SZ_AHASH_REQ_CTX;
4323 a_hash->halg.base.cra_priority = CHCR_CRA_PRIORITY;
4324 a_hash->halg.base.cra_module = THIS_MODULE;
4325 a_hash->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
4326 a_hash->halg.base.cra_alignmask = 0;
4327 a_hash->halg.base.cra_exit = NULL;
4328
4329 if (driver_algs[i].type == CRYPTO_ALG_TYPE_HMAC) {
4330 a_hash->halg.base.cra_init = chcr_hmac_cra_init;
4331 a_hash->halg.base.cra_exit = chcr_hmac_cra_exit;
4332 a_hash->init = chcr_hmac_init;
4333 a_hash->setkey = chcr_ahash_setkey;
4334 a_hash->halg.base.cra_ctxsize = SZ_AHASH_H_CTX;
4335 } else {
4336 a_hash->init = chcr_sha_init;
4337 a_hash->halg.base.cra_ctxsize = SZ_AHASH_CTX;
4338 a_hash->halg.base.cra_init = chcr_sha_cra_init;
4339 }
4340 err = crypto_register_ahash(&driver_algs[i].alg.hash);
4341 ai = driver_algs[i].alg.hash.halg.base;
4342 name = ai.cra_driver_name;
4343 break;
4344 }
4345 if (err) {
4346 pr_err("chcr : %s : Algorithm registration failed\n",
4347 name);
4348 goto register_err;
4349 } else {
4350 driver_algs[i].is_registered = 1;
4351 }
4352 }
4353 return 0;
4354
4355register_err:
4356 chcr_unregister_alg();
4357 return err;
4358}
4359
4360/*
4361 * start_crypto - Register the crypto algorithms.
4362 * This should called once when the first device comesup. After this
4363 * kernel will start calling driver APIs for crypto operations.
4364 */
4365int start_crypto(void)
4366{
4367 return chcr_register_alg();
4368}
4369
4370/*
4371 * stop_crypto - Deregister all the crypto algorithms with kernel.
4372 * This should be called once when the last device goes down. After this
4373 * kernel will not call the driver API for crypto operations.
4374 */
4375int stop_crypto(void)
4376{
4377 chcr_unregister_alg();
4378 return 0;
4379}