1 /* 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 76 static unsigned int sgl_ent_len[] = { 77 0, 0, 16, 24, 40, 48, 64, 72, 88, 78 96, 112, 120, 136, 144, 160, 168, 184, 79 192, 208, 216, 232, 240, 256, 264, 280, 80 288, 304, 312, 328, 336, 352, 360, 376 81 }; 82 83 static unsigned int dsgl_ent_len[] = { 84 0, 32, 32, 48, 48, 64, 64, 80, 80, 85 112, 112, 128, 128, 144, 144, 160, 160, 86 192, 192, 208, 208, 224, 224, 240, 240, 87 272, 272, 288, 288, 304, 304, 320, 320 88 }; 89 90 static u32 round_constant[11] = { 91 0x01000000, 0x02000000, 0x04000000, 0x08000000, 92 0x10000000, 0x20000000, 0x40000000, 0x80000000, 93 0x1B000000, 0x36000000, 0x6C000000 94 }; 95 96 static int chcr_handle_cipher_resp(struct skcipher_request *req, 97 unsigned char *input, int err); 98 99 static inline struct chcr_aead_ctx *AEAD_CTX(struct chcr_context *ctx) 100 { 101 return ctx->crypto_ctx->aeadctx; 102 } 103 104 static inline struct ablk_ctx *ABLK_CTX(struct chcr_context *ctx) 105 { 106 return ctx->crypto_ctx->ablkctx; 107 } 108 109 static inline struct hmac_ctx *HMAC_CTX(struct chcr_context *ctx) 110 { 111 return ctx->crypto_ctx->hmacctx; 112 } 113 114 static inline struct chcr_gcm_ctx *GCM_CTX(struct chcr_aead_ctx *gctx) 115 { 116 return gctx->ctx->gcm; 117 } 118 119 static inline struct chcr_authenc_ctx *AUTHENC_CTX(struct chcr_aead_ctx *gctx) 120 { 121 return gctx->ctx->authenc; 122 } 123 124 static inline struct uld_ctx *ULD_CTX(struct chcr_context *ctx) 125 { 126 return container_of(ctx->dev, struct uld_ctx, dev); 127 } 128 129 static inline int is_ofld_imm(const struct sk_buff *skb) 130 { 131 return (skb->len <= SGE_MAX_WR_LEN); 132 } 133 134 static 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 139 static 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 168 static 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 176 void 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 201 static 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 209 static inline void chcr_dec_wrcount(struct chcr_dev *dev) 210 { 211 atomic_dec(&dev->inflight); 212 } 213 214 static inline int chcr_handle_aead_resp(struct aead_request *req, 215 unsigned char *input, 216 int err) 217 { 218 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 219 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 220 struct chcr_dev *dev = a_ctx(tfm)->dev; 221 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 } 227 chcr_dec_wrcount(dev); 228 req->base.complete(&req->base, err); 229 230 return err; 231 } 232 233 static 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 285 static 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 310 static 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 354 static 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 369 static 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 380 static 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 388 static 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 410 static inline void dsgl_walk_add_page(struct dsgl_walk *walk, 411 size_t size, 412 dma_addr_t addr) 413 { 414 int j; 415 416 if (!size) 417 return; 418 j = walk->nents; 419 walk->to->len[j % 8] = htons(size); 420 walk->to->addr[j % 8] = cpu_to_be64(addr); 421 j++; 422 if ((j % 8) == 0) 423 walk->to++; 424 walk->nents = j; 425 } 426 427 static 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 474 static 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 485 static 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 492 static inline void ulptx_walk_add_page(struct ulptx_walk *walk, 493 size_t size, 494 dma_addr_t addr) 495 { 496 if (!size) 497 return; 498 499 if (walk->nents == 0) { 500 walk->sgl->len0 = cpu_to_be32(size); 501 walk->sgl->addr0 = cpu_to_be64(addr); 502 } else { 503 walk->pair->addr[walk->pair_idx] = cpu_to_be64(addr); 504 walk->pair->len[walk->pair_idx] = cpu_to_be32(size); 505 walk->pair_idx = !walk->pair_idx; 506 if (!walk->pair_idx) 507 walk->pair++; 508 } 509 walk->nents++; 510 } 511 512 static 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 571 static inline int get_cryptoalg_subtype(struct crypto_skcipher *tfm) 572 { 573 struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 574 struct chcr_alg_template *chcr_crypto_alg = 575 container_of(alg, struct chcr_alg_template, alg.skcipher); 576 577 return chcr_crypto_alg->type & CRYPTO_ALG_SUB_TYPE_MASK; 578 } 579 580 static 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 598 static 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 613 static 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 641 static int chcr_sg_ent_in_wr(struct scatterlist *src, 642 struct scatterlist *dst, 643 unsigned int minsg, 644 unsigned int space, 645 unsigned int srcskip, 646 unsigned int dstskip) 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 694 static int chcr_cipher_fallback(struct crypto_sync_skcipher *cipher, 695 u32 flags, 696 struct scatterlist *src, 697 struct scatterlist *dst, 698 unsigned int nbytes, 699 u8 *iv, 700 unsigned short op_type) 701 { 702 int err; 703 704 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, cipher); 705 706 skcipher_request_set_sync_tfm(subreq, cipher); 707 skcipher_request_set_callback(subreq, flags, NULL, NULL); 708 skcipher_request_set_crypt(subreq, src, dst, 709 nbytes, iv); 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 } 718 719 static inline int get_qidxs(struct crypto_async_request *req, 720 unsigned int *txqidx, unsigned int *rxqidx) 721 { 722 struct crypto_tfm *tfm = req->tfm; 723 int ret = 0; 724 725 switch (tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK) { 726 case CRYPTO_ALG_TYPE_AEAD: 727 { 728 struct aead_request *aead_req = 729 container_of(req, struct aead_request, base); 730 struct chcr_aead_reqctx *reqctx = aead_request_ctx(aead_req); 731 *txqidx = reqctx->txqidx; 732 *rxqidx = reqctx->rxqidx; 733 break; 734 } 735 case CRYPTO_ALG_TYPE_SKCIPHER: 736 { 737 struct skcipher_request *sk_req = 738 container_of(req, struct skcipher_request, base); 739 struct chcr_skcipher_req_ctx *reqctx = 740 skcipher_request_ctx(sk_req); 741 *txqidx = reqctx->txqidx; 742 *rxqidx = reqctx->rxqidx; 743 break; 744 } 745 case CRYPTO_ALG_TYPE_AHASH: 746 { 747 struct ahash_request *ahash_req = 748 container_of(req, struct ahash_request, base); 749 struct chcr_ahash_req_ctx *reqctx = 750 ahash_request_ctx(ahash_req); 751 *txqidx = reqctx->txqidx; 752 *rxqidx = reqctx->rxqidx; 753 break; 754 } 755 default: 756 ret = -EINVAL; 757 /* should never get here */ 758 BUG(); 759 break; 760 } 761 return ret; 762 } 763 764 static inline void create_wreq(struct chcr_context *ctx, 765 struct chcr_wr *chcr_req, 766 struct crypto_async_request *req, 767 unsigned int imm, 768 int hash_sz, 769 unsigned int len16, 770 unsigned int sc_len, 771 unsigned int lcb) 772 { 773 struct uld_ctx *u_ctx = ULD_CTX(ctx); 774 unsigned int tx_channel_id, rx_channel_id; 775 unsigned int txqidx = 0, rxqidx = 0; 776 unsigned int qid, fid; 777 778 get_qidxs(req, &txqidx, &rxqidx); 779 qid = u_ctx->lldi.rxq_ids[rxqidx]; 780 fid = u_ctx->lldi.rxq_ids[0]; 781 tx_channel_id = txqidx / ctx->txq_perchan; 782 rx_channel_id = rxqidx / ctx->rxq_perchan; 783 784 785 chcr_req->wreq.op_to_cctx_size = FILL_WR_OP_CCTX_SIZE; 786 chcr_req->wreq.pld_size_hash_size = 787 htonl(FW_CRYPTO_LOOKASIDE_WR_HASH_SIZE_V(hash_sz)); 788 chcr_req->wreq.len16_pkd = 789 htonl(FW_CRYPTO_LOOKASIDE_WR_LEN16_V(DIV_ROUND_UP(len16, 16))); 790 chcr_req->wreq.cookie = cpu_to_be64((uintptr_t)req); 791 chcr_req->wreq.rx_chid_to_rx_q_id = FILL_WR_RX_Q_ID(rx_channel_id, qid, 792 !!lcb, txqidx); 793 794 chcr_req->ulptx.cmd_dest = FILL_ULPTX_CMD_DEST(tx_channel_id, fid); 795 chcr_req->ulptx.len = htonl((DIV_ROUND_UP(len16, 16) - 796 ((sizeof(chcr_req->wreq)) >> 4))); 797 chcr_req->sc_imm.cmd_more = FILL_CMD_MORE(!imm); 798 chcr_req->sc_imm.len = cpu_to_be32(sizeof(struct cpl_tx_sec_pdu) + 799 sizeof(chcr_req->key_ctx) + sc_len); 800 } 801 802 /** 803 * create_cipher_wr - form the WR for cipher operations 804 * @req: cipher req. 805 * @ctx: crypto driver context of the request. 806 * @qid: ingress qid where response of this WR should be received. 807 * @op_type: encryption or decryption 808 */ 809 static struct sk_buff *create_cipher_wr(struct cipher_wr_param *wrparam) 810 { 811 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(wrparam->req); 812 struct chcr_context *ctx = c_ctx(tfm); 813 struct ablk_ctx *ablkctx = ABLK_CTX(ctx); 814 struct sk_buff *skb = NULL; 815 struct chcr_wr *chcr_req; 816 struct cpl_rx_phys_dsgl *phys_cpl; 817 struct ulptx_sgl *ulptx; 818 struct chcr_skcipher_req_ctx *reqctx = 819 skcipher_request_ctx(wrparam->req); 820 unsigned int temp = 0, transhdr_len, dst_size; 821 int error; 822 int nents; 823 unsigned int kctx_len; 824 gfp_t flags = wrparam->req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? 825 GFP_KERNEL : GFP_ATOMIC; 826 struct adapter *adap = padap(ctx->dev); 827 unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan; 828 829 nents = sg_nents_xlen(reqctx->dstsg, wrparam->bytes, CHCR_DST_SG_SIZE, 830 reqctx->dst_ofst); 831 dst_size = get_space_for_phys_dsgl(nents); 832 kctx_len = roundup(ablkctx->enckey_len, 16); 833 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size); 834 nents = sg_nents_xlen(reqctx->srcsg, wrparam->bytes, 835 CHCR_SRC_SG_SIZE, reqctx->src_ofst); 836 temp = reqctx->imm ? roundup(wrparam->bytes, 16) : 837 (sgl_len(nents) * 8); 838 transhdr_len += temp; 839 transhdr_len = roundup(transhdr_len, 16); 840 skb = alloc_skb(SGE_MAX_WR_LEN, flags); 841 if (!skb) { 842 error = -ENOMEM; 843 goto err; 844 } 845 chcr_req = __skb_put_zero(skb, transhdr_len); 846 chcr_req->sec_cpl.op_ivinsrtofst = 847 FILL_SEC_CPL_OP_IVINSR(rx_channel_id, 2, 1); 848 849 chcr_req->sec_cpl.pldlen = htonl(IV + wrparam->bytes); 850 chcr_req->sec_cpl.aadstart_cipherstop_hi = 851 FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, IV + 1, 0); 852 853 chcr_req->sec_cpl.cipherstop_lo_authinsert = 854 FILL_SEC_CPL_AUTHINSERT(0, 0, 0, 0); 855 chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op, 0, 856 ablkctx->ciph_mode, 857 0, 0, IV >> 1); 858 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 0, 859 0, 1, dst_size); 860 861 chcr_req->key_ctx.ctx_hdr = ablkctx->key_ctx_hdr; 862 if ((reqctx->op == CHCR_DECRYPT_OP) && 863 (!(get_cryptoalg_subtype(tfm) == 864 CRYPTO_ALG_SUB_TYPE_CTR)) && 865 (!(get_cryptoalg_subtype(tfm) == 866 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686))) { 867 generate_copy_rrkey(ablkctx, &chcr_req->key_ctx); 868 } else { 869 if ((ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) || 870 (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CTR)) { 871 memcpy(chcr_req->key_ctx.key, ablkctx->key, 872 ablkctx->enckey_len); 873 } else { 874 memcpy(chcr_req->key_ctx.key, ablkctx->key + 875 (ablkctx->enckey_len >> 1), 876 ablkctx->enckey_len >> 1); 877 memcpy(chcr_req->key_ctx.key + 878 (ablkctx->enckey_len >> 1), 879 ablkctx->key, 880 ablkctx->enckey_len >> 1); 881 } 882 } 883 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len); 884 ulptx = (struct ulptx_sgl *)((u8 *)(phys_cpl + 1) + dst_size); 885 chcr_add_cipher_src_ent(wrparam->req, ulptx, wrparam); 886 chcr_add_cipher_dst_ent(wrparam->req, phys_cpl, wrparam, wrparam->qid); 887 888 atomic_inc(&adap->chcr_stats.cipher_rqst); 889 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + kctx_len + IV 890 + (reqctx->imm ? (wrparam->bytes) : 0); 891 create_wreq(c_ctx(tfm), chcr_req, &(wrparam->req->base), reqctx->imm, 0, 892 transhdr_len, temp, 893 ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC); 894 reqctx->skb = skb; 895 896 if (reqctx->op && (ablkctx->ciph_mode == 897 CHCR_SCMD_CIPHER_MODE_AES_CBC)) 898 sg_pcopy_to_buffer(wrparam->req->src, 899 sg_nents(wrparam->req->src), wrparam->req->iv, 16, 900 reqctx->processed + wrparam->bytes - AES_BLOCK_SIZE); 901 902 return skb; 903 err: 904 return ERR_PTR(error); 905 } 906 907 static inline int chcr_keyctx_ck_size(unsigned int keylen) 908 { 909 int ck_size = 0; 910 911 if (keylen == AES_KEYSIZE_128) 912 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128; 913 else if (keylen == AES_KEYSIZE_192) 914 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192; 915 else if (keylen == AES_KEYSIZE_256) 916 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256; 917 else 918 ck_size = 0; 919 920 return ck_size; 921 } 922 static int chcr_cipher_fallback_setkey(struct crypto_skcipher *cipher, 923 const u8 *key, 924 unsigned int keylen) 925 { 926 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher)); 927 928 crypto_sync_skcipher_clear_flags(ablkctx->sw_cipher, 929 CRYPTO_TFM_REQ_MASK); 930 crypto_sync_skcipher_set_flags(ablkctx->sw_cipher, 931 cipher->base.crt_flags & CRYPTO_TFM_REQ_MASK); 932 return crypto_sync_skcipher_setkey(ablkctx->sw_cipher, key, keylen); 933 } 934 935 static int chcr_aes_cbc_setkey(struct crypto_skcipher *cipher, 936 const u8 *key, 937 unsigned int keylen) 938 { 939 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher)); 940 unsigned int ck_size, context_size; 941 u16 alignment = 0; 942 int err; 943 944 err = chcr_cipher_fallback_setkey(cipher, key, keylen); 945 if (err) 946 goto badkey_err; 947 948 ck_size = chcr_keyctx_ck_size(keylen); 949 alignment = ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192 ? 8 : 0; 950 memcpy(ablkctx->key, key, keylen); 951 ablkctx->enckey_len = keylen; 952 get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, keylen << 3); 953 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + 954 keylen + alignment) >> 4; 955 956 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY, 957 0, 0, context_size); 958 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CBC; 959 return 0; 960 badkey_err: 961 ablkctx->enckey_len = 0; 962 963 return err; 964 } 965 966 static int chcr_aes_ctr_setkey(struct crypto_skcipher *cipher, 967 const u8 *key, 968 unsigned int keylen) 969 { 970 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher)); 971 unsigned int ck_size, context_size; 972 u16 alignment = 0; 973 int err; 974 975 err = chcr_cipher_fallback_setkey(cipher, key, keylen); 976 if (err) 977 goto badkey_err; 978 ck_size = chcr_keyctx_ck_size(keylen); 979 alignment = (ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) ? 8 : 0; 980 memcpy(ablkctx->key, key, keylen); 981 ablkctx->enckey_len = keylen; 982 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + 983 keylen + alignment) >> 4; 984 985 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY, 986 0, 0, context_size); 987 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR; 988 989 return 0; 990 badkey_err: 991 ablkctx->enckey_len = 0; 992 993 return err; 994 } 995 996 static int chcr_aes_rfc3686_setkey(struct crypto_skcipher *cipher, 997 const u8 *key, 998 unsigned int keylen) 999 { 1000 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher)); 1001 unsigned int ck_size, context_size; 1002 u16 alignment = 0; 1003 int err; 1004 1005 if (keylen < CTR_RFC3686_NONCE_SIZE) 1006 return -EINVAL; 1007 memcpy(ablkctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE), 1008 CTR_RFC3686_NONCE_SIZE); 1009 1010 keylen -= CTR_RFC3686_NONCE_SIZE; 1011 err = chcr_cipher_fallback_setkey(cipher, key, keylen); 1012 if (err) 1013 goto badkey_err; 1014 1015 ck_size = chcr_keyctx_ck_size(keylen); 1016 alignment = (ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) ? 8 : 0; 1017 memcpy(ablkctx->key, key, keylen); 1018 ablkctx->enckey_len = keylen; 1019 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + 1020 keylen + alignment) >> 4; 1021 1022 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY, 1023 0, 0, context_size); 1024 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR; 1025 1026 return 0; 1027 badkey_err: 1028 ablkctx->enckey_len = 0; 1029 1030 return err; 1031 } 1032 static void ctr_add_iv(u8 *dstiv, u8 *srciv, u32 add) 1033 { 1034 unsigned int size = AES_BLOCK_SIZE; 1035 __be32 *b = (__be32 *)(dstiv + size); 1036 u32 c, prev; 1037 1038 memcpy(dstiv, srciv, AES_BLOCK_SIZE); 1039 for (; size >= 4; size -= 4) { 1040 prev = be32_to_cpu(*--b); 1041 c = prev + add; 1042 *b = cpu_to_be32(c); 1043 if (prev < c) 1044 break; 1045 add = 1; 1046 } 1047 1048 } 1049 1050 static unsigned int adjust_ctr_overflow(u8 *iv, u32 bytes) 1051 { 1052 __be32 *b = (__be32 *)(iv + AES_BLOCK_SIZE); 1053 u64 c; 1054 u32 temp = be32_to_cpu(*--b); 1055 1056 temp = ~temp; 1057 c = (u64)temp + 1; // No of block can processed without overflow 1058 if ((bytes / AES_BLOCK_SIZE) >= c) 1059 bytes = c * AES_BLOCK_SIZE; 1060 return bytes; 1061 } 1062 1063 static int chcr_update_tweak(struct skcipher_request *req, u8 *iv, 1064 u32 isfinal) 1065 { 1066 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 1067 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm)); 1068 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req); 1069 struct crypto_aes_ctx aes; 1070 int ret, i; 1071 u8 *key; 1072 unsigned int keylen; 1073 int round = reqctx->last_req_len / AES_BLOCK_SIZE; 1074 int round8 = round / 8; 1075 1076 memcpy(iv, reqctx->iv, AES_BLOCK_SIZE); 1077 1078 keylen = ablkctx->enckey_len / 2; 1079 key = ablkctx->key + keylen; 1080 /* For a 192 bit key remove the padded zeroes which was 1081 * added in chcr_xts_setkey 1082 */ 1083 if (KEY_CONTEXT_CK_SIZE_G(ntohl(ablkctx->key_ctx_hdr)) 1084 == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) 1085 ret = aes_expandkey(&aes, key, keylen - 8); 1086 else 1087 ret = aes_expandkey(&aes, key, keylen); 1088 if (ret) 1089 return ret; 1090 aes_encrypt(&aes, iv, iv); 1091 for (i = 0; i < round8; i++) 1092 gf128mul_x8_ble((le128 *)iv, (le128 *)iv); 1093 1094 for (i = 0; i < (round % 8); i++) 1095 gf128mul_x_ble((le128 *)iv, (le128 *)iv); 1096 1097 if (!isfinal) 1098 aes_decrypt(&aes, iv, iv); 1099 1100 memzero_explicit(&aes, sizeof(aes)); 1101 return 0; 1102 } 1103 1104 static int chcr_update_cipher_iv(struct skcipher_request *req, 1105 struct cpl_fw6_pld *fw6_pld, u8 *iv) 1106 { 1107 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 1108 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req); 1109 int subtype = get_cryptoalg_subtype(tfm); 1110 int ret = 0; 1111 1112 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR) 1113 ctr_add_iv(iv, req->iv, (reqctx->processed / 1114 AES_BLOCK_SIZE)); 1115 else if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_RFC3686) 1116 *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE + 1117 CTR_RFC3686_IV_SIZE) = cpu_to_be32((reqctx->processed / 1118 AES_BLOCK_SIZE) + 1); 1119 else if (subtype == CRYPTO_ALG_SUB_TYPE_XTS) 1120 ret = chcr_update_tweak(req, iv, 0); 1121 else if (subtype == CRYPTO_ALG_SUB_TYPE_CBC) { 1122 if (reqctx->op) 1123 /*Updated before sending last WR*/ 1124 memcpy(iv, req->iv, AES_BLOCK_SIZE); 1125 else 1126 memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE); 1127 } 1128 1129 return ret; 1130 1131 } 1132 1133 /* We need separate function for final iv because in rfc3686 Initial counter 1134 * starts from 1 and buffer size of iv is 8 byte only which remains constant 1135 * for subsequent update requests 1136 */ 1137 1138 static int chcr_final_cipher_iv(struct skcipher_request *req, 1139 struct cpl_fw6_pld *fw6_pld, u8 *iv) 1140 { 1141 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 1142 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req); 1143 int subtype = get_cryptoalg_subtype(tfm); 1144 int ret = 0; 1145 1146 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR) 1147 ctr_add_iv(iv, req->iv, DIV_ROUND_UP(reqctx->processed, 1148 AES_BLOCK_SIZE)); 1149 else if (subtype == CRYPTO_ALG_SUB_TYPE_XTS) { 1150 if (!reqctx->partial_req) 1151 memcpy(iv, reqctx->iv, AES_BLOCK_SIZE); 1152 else 1153 ret = chcr_update_tweak(req, iv, 1); 1154 } 1155 else if (subtype == CRYPTO_ALG_SUB_TYPE_CBC) { 1156 /*Already updated for Decrypt*/ 1157 if (!reqctx->op) 1158 memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE); 1159 1160 } 1161 return ret; 1162 1163 } 1164 1165 static int chcr_handle_cipher_resp(struct skcipher_request *req, 1166 unsigned char *input, int err) 1167 { 1168 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req); 1169 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 1170 struct cpl_fw6_pld *fw6_pld = (struct cpl_fw6_pld *)input; 1171 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm)); 1172 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm)); 1173 struct chcr_dev *dev = c_ctx(tfm)->dev; 1174 struct chcr_context *ctx = c_ctx(tfm); 1175 struct adapter *adap = padap(ctx->dev); 1176 struct cipher_wr_param wrparam; 1177 struct sk_buff *skb; 1178 int bytes; 1179 1180 if (err) 1181 goto unmap; 1182 if (req->cryptlen == reqctx->processed) { 1183 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, 1184 req); 1185 err = chcr_final_cipher_iv(req, fw6_pld, req->iv); 1186 goto complete; 1187 } 1188 1189 if (!reqctx->imm) { 1190 bytes = chcr_sg_ent_in_wr(reqctx->srcsg, reqctx->dstsg, 0, 1191 CIP_SPACE_LEFT(ablkctx->enckey_len), 1192 reqctx->src_ofst, reqctx->dst_ofst); 1193 if ((bytes + reqctx->processed) >= req->cryptlen) 1194 bytes = req->cryptlen - reqctx->processed; 1195 else 1196 bytes = rounddown(bytes, 16); 1197 } else { 1198 /*CTR mode counter overfloa*/ 1199 bytes = req->cryptlen - reqctx->processed; 1200 } 1201 err = chcr_update_cipher_iv(req, fw6_pld, reqctx->iv); 1202 if (err) 1203 goto unmap; 1204 1205 if (unlikely(bytes == 0)) { 1206 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, 1207 req); 1208 memcpy(req->iv, reqctx->init_iv, IV); 1209 atomic_inc(&adap->chcr_stats.fallback); 1210 err = chcr_cipher_fallback(ablkctx->sw_cipher, 1211 req->base.flags, 1212 req->src, 1213 req->dst, 1214 req->cryptlen, 1215 req->iv, 1216 reqctx->op); 1217 goto complete; 1218 } 1219 1220 if (get_cryptoalg_subtype(tfm) == 1221 CRYPTO_ALG_SUB_TYPE_CTR) 1222 bytes = adjust_ctr_overflow(reqctx->iv, bytes); 1223 wrparam.qid = u_ctx->lldi.rxq_ids[reqctx->rxqidx]; 1224 wrparam.req = req; 1225 wrparam.bytes = bytes; 1226 skb = create_cipher_wr(&wrparam); 1227 if (IS_ERR(skb)) { 1228 pr_err("chcr : %s : Failed to form WR. No memory\n", __func__); 1229 err = PTR_ERR(skb); 1230 goto unmap; 1231 } 1232 skb->dev = u_ctx->lldi.ports[0]; 1233 set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx); 1234 chcr_send_wr(skb); 1235 reqctx->last_req_len = bytes; 1236 reqctx->processed += bytes; 1237 if (get_cryptoalg_subtype(tfm) == 1238 CRYPTO_ALG_SUB_TYPE_CBC && req->base.flags == 1239 CRYPTO_TFM_REQ_MAY_SLEEP ) { 1240 complete(&ctx->cbc_aes_aio_done); 1241 } 1242 return 0; 1243 unmap: 1244 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req); 1245 complete: 1246 if (get_cryptoalg_subtype(tfm) == 1247 CRYPTO_ALG_SUB_TYPE_CBC && req->base.flags == 1248 CRYPTO_TFM_REQ_MAY_SLEEP ) { 1249 complete(&ctx->cbc_aes_aio_done); 1250 } 1251 chcr_dec_wrcount(dev); 1252 req->base.complete(&req->base, err); 1253 return err; 1254 } 1255 1256 static int process_cipher(struct skcipher_request *req, 1257 unsigned short qid, 1258 struct sk_buff **skb, 1259 unsigned short op_type) 1260 { 1261 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req); 1262 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 1263 unsigned int ivsize = crypto_skcipher_ivsize(tfm); 1264 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm)); 1265 struct adapter *adap = padap(c_ctx(tfm)->dev); 1266 struct cipher_wr_param wrparam; 1267 int bytes, err = -EINVAL; 1268 int subtype; 1269 1270 reqctx->processed = 0; 1271 reqctx->partial_req = 0; 1272 if (!req->iv) 1273 goto error; 1274 subtype = get_cryptoalg_subtype(tfm); 1275 if ((ablkctx->enckey_len == 0) || (ivsize > AES_BLOCK_SIZE) || 1276 (req->cryptlen == 0) || 1277 (req->cryptlen % crypto_skcipher_blocksize(tfm))) { 1278 if (req->cryptlen == 0 && subtype != CRYPTO_ALG_SUB_TYPE_XTS) 1279 goto fallback; 1280 else if (req->cryptlen % crypto_skcipher_blocksize(tfm) && 1281 subtype == CRYPTO_ALG_SUB_TYPE_XTS) 1282 goto fallback; 1283 pr_err("AES: Invalid value of Key Len %d nbytes %d IV Len %d\n", 1284 ablkctx->enckey_len, req->cryptlen, ivsize); 1285 goto error; 1286 } 1287 1288 err = chcr_cipher_dma_map(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req); 1289 if (err) 1290 goto error; 1291 if (req->cryptlen < (SGE_MAX_WR_LEN - (sizeof(struct chcr_wr) + 1292 AES_MIN_KEY_SIZE + 1293 sizeof(struct cpl_rx_phys_dsgl) + 1294 /*Min dsgl size*/ 1295 32))) { 1296 /* Can be sent as Imm*/ 1297 unsigned int dnents = 0, transhdr_len, phys_dsgl, kctx_len; 1298 1299 dnents = sg_nents_xlen(req->dst, req->cryptlen, 1300 CHCR_DST_SG_SIZE, 0); 1301 phys_dsgl = get_space_for_phys_dsgl(dnents); 1302 kctx_len = roundup(ablkctx->enckey_len, 16); 1303 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, phys_dsgl); 1304 reqctx->imm = (transhdr_len + IV + req->cryptlen) <= 1305 SGE_MAX_WR_LEN; 1306 bytes = IV + req->cryptlen; 1307 1308 } else { 1309 reqctx->imm = 0; 1310 } 1311 1312 if (!reqctx->imm) { 1313 bytes = chcr_sg_ent_in_wr(req->src, req->dst, 0, 1314 CIP_SPACE_LEFT(ablkctx->enckey_len), 1315 0, 0); 1316 if ((bytes + reqctx->processed) >= req->cryptlen) 1317 bytes = req->cryptlen - reqctx->processed; 1318 else 1319 bytes = rounddown(bytes, 16); 1320 } else { 1321 bytes = req->cryptlen; 1322 } 1323 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR) { 1324 bytes = adjust_ctr_overflow(req->iv, bytes); 1325 } 1326 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_RFC3686) { 1327 memcpy(reqctx->iv, ablkctx->nonce, CTR_RFC3686_NONCE_SIZE); 1328 memcpy(reqctx->iv + CTR_RFC3686_NONCE_SIZE, req->iv, 1329 CTR_RFC3686_IV_SIZE); 1330 1331 /* initialize counter portion of counter block */ 1332 *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE + 1333 CTR_RFC3686_IV_SIZE) = cpu_to_be32(1); 1334 memcpy(reqctx->init_iv, reqctx->iv, IV); 1335 1336 } else { 1337 1338 memcpy(reqctx->iv, req->iv, IV); 1339 memcpy(reqctx->init_iv, req->iv, IV); 1340 } 1341 if (unlikely(bytes == 0)) { 1342 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, 1343 req); 1344 fallback: atomic_inc(&adap->chcr_stats.fallback); 1345 err = chcr_cipher_fallback(ablkctx->sw_cipher, 1346 req->base.flags, 1347 req->src, 1348 req->dst, 1349 req->cryptlen, 1350 subtype == 1351 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686 ? 1352 reqctx->iv : req->iv, 1353 op_type); 1354 goto error; 1355 } 1356 reqctx->op = op_type; 1357 reqctx->srcsg = req->src; 1358 reqctx->dstsg = req->dst; 1359 reqctx->src_ofst = 0; 1360 reqctx->dst_ofst = 0; 1361 wrparam.qid = qid; 1362 wrparam.req = req; 1363 wrparam.bytes = bytes; 1364 *skb = create_cipher_wr(&wrparam); 1365 if (IS_ERR(*skb)) { 1366 err = PTR_ERR(*skb); 1367 goto unmap; 1368 } 1369 reqctx->processed = bytes; 1370 reqctx->last_req_len = bytes; 1371 reqctx->partial_req = !!(req->cryptlen - reqctx->processed); 1372 1373 return 0; 1374 unmap: 1375 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req); 1376 error: 1377 return err; 1378 } 1379 1380 static int chcr_aes_encrypt(struct skcipher_request *req) 1381 { 1382 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 1383 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req); 1384 struct chcr_dev *dev = c_ctx(tfm)->dev; 1385 struct sk_buff *skb = NULL; 1386 int err; 1387 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm)); 1388 struct chcr_context *ctx = c_ctx(tfm); 1389 unsigned int cpu; 1390 1391 cpu = get_cpu(); 1392 reqctx->txqidx = cpu % ctx->ntxq; 1393 reqctx->rxqidx = cpu % ctx->nrxq; 1394 put_cpu(); 1395 1396 err = chcr_inc_wrcount(dev); 1397 if (err) 1398 return -ENXIO; 1399 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0], 1400 reqctx->txqidx) && 1401 (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) { 1402 err = -ENOSPC; 1403 goto error; 1404 } 1405 1406 err = process_cipher(req, u_ctx->lldi.rxq_ids[reqctx->rxqidx], 1407 &skb, CHCR_ENCRYPT_OP); 1408 if (err || !skb) 1409 return err; 1410 skb->dev = u_ctx->lldi.ports[0]; 1411 set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx); 1412 chcr_send_wr(skb); 1413 if (get_cryptoalg_subtype(tfm) == 1414 CRYPTO_ALG_SUB_TYPE_CBC && req->base.flags == 1415 CRYPTO_TFM_REQ_MAY_SLEEP ) { 1416 reqctx->partial_req = 1; 1417 wait_for_completion(&ctx->cbc_aes_aio_done); 1418 } 1419 return -EINPROGRESS; 1420 error: 1421 chcr_dec_wrcount(dev); 1422 return err; 1423 } 1424 1425 static int chcr_aes_decrypt(struct skcipher_request *req) 1426 { 1427 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 1428 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req); 1429 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm)); 1430 struct chcr_dev *dev = c_ctx(tfm)->dev; 1431 struct sk_buff *skb = NULL; 1432 int err; 1433 struct chcr_context *ctx = c_ctx(tfm); 1434 unsigned int cpu; 1435 1436 cpu = get_cpu(); 1437 reqctx->txqidx = cpu % ctx->ntxq; 1438 reqctx->rxqidx = cpu % ctx->nrxq; 1439 put_cpu(); 1440 1441 err = chcr_inc_wrcount(dev); 1442 if (err) 1443 return -ENXIO; 1444 1445 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0], 1446 reqctx->txqidx) && 1447 (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) 1448 return -ENOSPC; 1449 err = process_cipher(req, u_ctx->lldi.rxq_ids[reqctx->rxqidx], 1450 &skb, CHCR_DECRYPT_OP); 1451 if (err || !skb) 1452 return err; 1453 skb->dev = u_ctx->lldi.ports[0]; 1454 set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx); 1455 chcr_send_wr(skb); 1456 return -EINPROGRESS; 1457 } 1458 static int chcr_device_init(struct chcr_context *ctx) 1459 { 1460 struct uld_ctx *u_ctx = NULL; 1461 int txq_perchan, ntxq; 1462 int err = 0, rxq_perchan; 1463 1464 if (!ctx->dev) { 1465 u_ctx = assign_chcr_device(); 1466 if (!u_ctx) { 1467 pr_err("chcr device assignment fails\n"); 1468 goto out; 1469 } 1470 ctx->dev = &u_ctx->dev; 1471 ntxq = u_ctx->lldi.ntxq; 1472 rxq_perchan = u_ctx->lldi.nrxq / u_ctx->lldi.nchan; 1473 txq_perchan = ntxq / u_ctx->lldi.nchan; 1474 ctx->ntxq = ntxq; 1475 ctx->nrxq = u_ctx->lldi.nrxq; 1476 ctx->rxq_perchan = rxq_perchan; 1477 ctx->txq_perchan = txq_perchan; 1478 } 1479 out: 1480 return err; 1481 } 1482 1483 static int chcr_init_tfm(struct crypto_skcipher *tfm) 1484 { 1485 struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 1486 struct chcr_context *ctx = crypto_skcipher_ctx(tfm); 1487 struct ablk_ctx *ablkctx = ABLK_CTX(ctx); 1488 1489 ablkctx->sw_cipher = crypto_alloc_sync_skcipher(alg->base.cra_name, 0, 1490 CRYPTO_ALG_NEED_FALLBACK); 1491 if (IS_ERR(ablkctx->sw_cipher)) { 1492 pr_err("failed to allocate fallback for %s\n", alg->base.cra_name); 1493 return PTR_ERR(ablkctx->sw_cipher); 1494 } 1495 init_completion(&ctx->cbc_aes_aio_done); 1496 crypto_skcipher_set_reqsize(tfm, sizeof(struct chcr_skcipher_req_ctx)); 1497 1498 return chcr_device_init(ctx); 1499 } 1500 1501 static int chcr_rfc3686_init(struct crypto_skcipher *tfm) 1502 { 1503 struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 1504 struct chcr_context *ctx = crypto_skcipher_ctx(tfm); 1505 struct ablk_ctx *ablkctx = ABLK_CTX(ctx); 1506 1507 /*RFC3686 initialises IV counter value to 1, rfc3686(ctr(aes)) 1508 * cannot be used as fallback in chcr_handle_cipher_response 1509 */ 1510 ablkctx->sw_cipher = crypto_alloc_sync_skcipher("ctr(aes)", 0, 1511 CRYPTO_ALG_NEED_FALLBACK); 1512 if (IS_ERR(ablkctx->sw_cipher)) { 1513 pr_err("failed to allocate fallback for %s\n", alg->base.cra_name); 1514 return PTR_ERR(ablkctx->sw_cipher); 1515 } 1516 crypto_skcipher_set_reqsize(tfm, sizeof(struct chcr_skcipher_req_ctx)); 1517 return chcr_device_init(ctx); 1518 } 1519 1520 1521 static void chcr_exit_tfm(struct crypto_skcipher *tfm) 1522 { 1523 struct chcr_context *ctx = crypto_skcipher_ctx(tfm); 1524 struct ablk_ctx *ablkctx = ABLK_CTX(ctx); 1525 1526 crypto_free_sync_skcipher(ablkctx->sw_cipher); 1527 } 1528 1529 static int get_alg_config(struct algo_param *params, 1530 unsigned int auth_size) 1531 { 1532 switch (auth_size) { 1533 case SHA1_DIGEST_SIZE: 1534 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_160; 1535 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA1; 1536 params->result_size = SHA1_DIGEST_SIZE; 1537 break; 1538 case SHA224_DIGEST_SIZE: 1539 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256; 1540 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA224; 1541 params->result_size = SHA256_DIGEST_SIZE; 1542 break; 1543 case SHA256_DIGEST_SIZE: 1544 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256; 1545 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA256; 1546 params->result_size = SHA256_DIGEST_SIZE; 1547 break; 1548 case SHA384_DIGEST_SIZE: 1549 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512; 1550 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_384; 1551 params->result_size = SHA512_DIGEST_SIZE; 1552 break; 1553 case SHA512_DIGEST_SIZE: 1554 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512; 1555 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_512; 1556 params->result_size = SHA512_DIGEST_SIZE; 1557 break; 1558 default: 1559 pr_err("chcr : ERROR, unsupported digest size\n"); 1560 return -EINVAL; 1561 } 1562 return 0; 1563 } 1564 1565 static inline void chcr_free_shash(struct crypto_shash *base_hash) 1566 { 1567 crypto_free_shash(base_hash); 1568 } 1569 1570 /** 1571 * create_hash_wr - Create hash work request 1572 * @req - Cipher req base 1573 */ 1574 static struct sk_buff *create_hash_wr(struct ahash_request *req, 1575 struct hash_wr_param *param) 1576 { 1577 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req); 1578 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1579 struct chcr_context *ctx = h_ctx(tfm); 1580 struct hmac_ctx *hmacctx = HMAC_CTX(ctx); 1581 struct sk_buff *skb = NULL; 1582 struct uld_ctx *u_ctx = ULD_CTX(ctx); 1583 struct chcr_wr *chcr_req; 1584 struct ulptx_sgl *ulptx; 1585 unsigned int nents = 0, transhdr_len; 1586 unsigned int temp = 0; 1587 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : 1588 GFP_ATOMIC; 1589 struct adapter *adap = padap(h_ctx(tfm)->dev); 1590 int error = 0; 1591 unsigned int rx_channel_id = req_ctx->rxqidx / ctx->rxq_perchan; 1592 1593 transhdr_len = HASH_TRANSHDR_SIZE(param->kctx_len); 1594 req_ctx->hctx_wr.imm = (transhdr_len + param->bfr_len + 1595 param->sg_len) <= SGE_MAX_WR_LEN; 1596 nents = sg_nents_xlen(req_ctx->hctx_wr.srcsg, param->sg_len, 1597 CHCR_SRC_SG_SIZE, req_ctx->hctx_wr.src_ofst); 1598 nents += param->bfr_len ? 1 : 0; 1599 transhdr_len += req_ctx->hctx_wr.imm ? roundup(param->bfr_len + 1600 param->sg_len, 16) : (sgl_len(nents) * 8); 1601 transhdr_len = roundup(transhdr_len, 16); 1602 1603 skb = alloc_skb(transhdr_len, flags); 1604 if (!skb) 1605 return ERR_PTR(-ENOMEM); 1606 chcr_req = __skb_put_zero(skb, transhdr_len); 1607 1608 chcr_req->sec_cpl.op_ivinsrtofst = 1609 FILL_SEC_CPL_OP_IVINSR(rx_channel_id, 2, 0); 1610 1611 chcr_req->sec_cpl.pldlen = htonl(param->bfr_len + param->sg_len); 1612 1613 chcr_req->sec_cpl.aadstart_cipherstop_hi = 1614 FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, 0, 0); 1615 chcr_req->sec_cpl.cipherstop_lo_authinsert = 1616 FILL_SEC_CPL_AUTHINSERT(0, 1, 0, 0); 1617 chcr_req->sec_cpl.seqno_numivs = 1618 FILL_SEC_CPL_SCMD0_SEQNO(0, 0, 0, param->alg_prm.auth_mode, 1619 param->opad_needed, 0); 1620 1621 chcr_req->sec_cpl.ivgen_hdrlen = 1622 FILL_SEC_CPL_IVGEN_HDRLEN(param->last, param->more, 0, 1, 0, 0); 1623 1624 memcpy(chcr_req->key_ctx.key, req_ctx->partial_hash, 1625 param->alg_prm.result_size); 1626 1627 if (param->opad_needed) 1628 memcpy(chcr_req->key_ctx.key + 1629 ((param->alg_prm.result_size <= 32) ? 32 : 1630 CHCR_HASH_MAX_DIGEST_SIZE), 1631 hmacctx->opad, param->alg_prm.result_size); 1632 1633 chcr_req->key_ctx.ctx_hdr = FILL_KEY_CTX_HDR(CHCR_KEYCTX_NO_KEY, 1634 param->alg_prm.mk_size, 0, 1635 param->opad_needed, 1636 ((param->kctx_len + 1637 sizeof(chcr_req->key_ctx)) >> 4)); 1638 chcr_req->sec_cpl.scmd1 = cpu_to_be64((u64)param->scmd1); 1639 ulptx = (struct ulptx_sgl *)((u8 *)(chcr_req + 1) + param->kctx_len + 1640 DUMMY_BYTES); 1641 if (param->bfr_len != 0) { 1642 req_ctx->hctx_wr.dma_addr = 1643 dma_map_single(&u_ctx->lldi.pdev->dev, req_ctx->reqbfr, 1644 param->bfr_len, DMA_TO_DEVICE); 1645 if (dma_mapping_error(&u_ctx->lldi.pdev->dev, 1646 req_ctx->hctx_wr. dma_addr)) { 1647 error = -ENOMEM; 1648 goto err; 1649 } 1650 req_ctx->hctx_wr.dma_len = param->bfr_len; 1651 } else { 1652 req_ctx->hctx_wr.dma_addr = 0; 1653 } 1654 chcr_add_hash_src_ent(req, ulptx, param); 1655 /* Request upto max wr size */ 1656 temp = param->kctx_len + DUMMY_BYTES + (req_ctx->hctx_wr.imm ? 1657 (param->sg_len + param->bfr_len) : 0); 1658 atomic_inc(&adap->chcr_stats.digest_rqst); 1659 create_wreq(h_ctx(tfm), chcr_req, &req->base, req_ctx->hctx_wr.imm, 1660 param->hash_size, transhdr_len, 1661 temp, 0); 1662 req_ctx->hctx_wr.skb = skb; 1663 return skb; 1664 err: 1665 kfree_skb(skb); 1666 return ERR_PTR(error); 1667 } 1668 1669 static int chcr_ahash_update(struct ahash_request *req) 1670 { 1671 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req); 1672 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req); 1673 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(rtfm)); 1674 struct chcr_context *ctx = h_ctx(rtfm); 1675 struct chcr_dev *dev = h_ctx(rtfm)->dev; 1676 struct sk_buff *skb; 1677 u8 remainder = 0, bs; 1678 unsigned int nbytes = req->nbytes; 1679 struct hash_wr_param params; 1680 int error; 1681 unsigned int cpu; 1682 1683 cpu = get_cpu(); 1684 req_ctx->txqidx = cpu % ctx->ntxq; 1685 req_ctx->rxqidx = cpu % ctx->nrxq; 1686 put_cpu(); 1687 1688 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm)); 1689 1690 if (nbytes + req_ctx->reqlen >= bs) { 1691 remainder = (nbytes + req_ctx->reqlen) % bs; 1692 nbytes = nbytes + req_ctx->reqlen - remainder; 1693 } else { 1694 sg_pcopy_to_buffer(req->src, sg_nents(req->src), req_ctx->reqbfr 1695 + req_ctx->reqlen, nbytes, 0); 1696 req_ctx->reqlen += nbytes; 1697 return 0; 1698 } 1699 error = chcr_inc_wrcount(dev); 1700 if (error) 1701 return -ENXIO; 1702 /* Detach state for CHCR means lldi or padap is freed. Increasing 1703 * inflight count for dev guarantees that lldi and padap is valid 1704 */ 1705 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0], 1706 req_ctx->txqidx) && 1707 (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) { 1708 error = -ENOSPC; 1709 goto err; 1710 } 1711 1712 chcr_init_hctx_per_wr(req_ctx); 1713 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req); 1714 if (error) { 1715 error = -ENOMEM; 1716 goto err; 1717 } 1718 get_alg_config(¶ms.alg_prm, crypto_ahash_digestsize(rtfm)); 1719 params.kctx_len = roundup(params.alg_prm.result_size, 16); 1720 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen, 1721 HASH_SPACE_LEFT(params.kctx_len), 0); 1722 if (params.sg_len > req->nbytes) 1723 params.sg_len = req->nbytes; 1724 params.sg_len = rounddown(params.sg_len + req_ctx->reqlen, bs) - 1725 req_ctx->reqlen; 1726 params.opad_needed = 0; 1727 params.more = 1; 1728 params.last = 0; 1729 params.bfr_len = req_ctx->reqlen; 1730 params.scmd1 = 0; 1731 req_ctx->hctx_wr.srcsg = req->src; 1732 1733 params.hash_size = params.alg_prm.result_size; 1734 req_ctx->data_len += params.sg_len + params.bfr_len; 1735 skb = create_hash_wr(req, ¶ms); 1736 if (IS_ERR(skb)) { 1737 error = PTR_ERR(skb); 1738 goto unmap; 1739 } 1740 1741 req_ctx->hctx_wr.processed += params.sg_len; 1742 if (remainder) { 1743 /* Swap buffers */ 1744 swap(req_ctx->reqbfr, req_ctx->skbfr); 1745 sg_pcopy_to_buffer(req->src, sg_nents(req->src), 1746 req_ctx->reqbfr, remainder, req->nbytes - 1747 remainder); 1748 } 1749 req_ctx->reqlen = remainder; 1750 skb->dev = u_ctx->lldi.ports[0]; 1751 set_wr_txq(skb, CPL_PRIORITY_DATA, req_ctx->txqidx); 1752 chcr_send_wr(skb); 1753 return -EINPROGRESS; 1754 unmap: 1755 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req); 1756 err: 1757 chcr_dec_wrcount(dev); 1758 return error; 1759 } 1760 1761 static void create_last_hash_block(char *bfr_ptr, unsigned int bs, u64 scmd1) 1762 { 1763 memset(bfr_ptr, 0, bs); 1764 *bfr_ptr = 0x80; 1765 if (bs == 64) 1766 *(__be64 *)(bfr_ptr + 56) = cpu_to_be64(scmd1 << 3); 1767 else 1768 *(__be64 *)(bfr_ptr + 120) = cpu_to_be64(scmd1 << 3); 1769 } 1770 1771 static int chcr_ahash_final(struct ahash_request *req) 1772 { 1773 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req); 1774 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req); 1775 struct chcr_dev *dev = h_ctx(rtfm)->dev; 1776 struct hash_wr_param params; 1777 struct sk_buff *skb; 1778 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(rtfm)); 1779 struct chcr_context *ctx = h_ctx(rtfm); 1780 u8 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm)); 1781 int error = -EINVAL; 1782 unsigned int cpu; 1783 1784 cpu = get_cpu(); 1785 req_ctx->txqidx = cpu % ctx->ntxq; 1786 req_ctx->rxqidx = cpu % ctx->nrxq; 1787 put_cpu(); 1788 1789 error = chcr_inc_wrcount(dev); 1790 if (error) 1791 return -ENXIO; 1792 1793 chcr_init_hctx_per_wr(req_ctx); 1794 if (is_hmac(crypto_ahash_tfm(rtfm))) 1795 params.opad_needed = 1; 1796 else 1797 params.opad_needed = 0; 1798 params.sg_len = 0; 1799 req_ctx->hctx_wr.isfinal = 1; 1800 get_alg_config(¶ms.alg_prm, crypto_ahash_digestsize(rtfm)); 1801 params.kctx_len = roundup(params.alg_prm.result_size, 16); 1802 if (is_hmac(crypto_ahash_tfm(rtfm))) { 1803 params.opad_needed = 1; 1804 params.kctx_len *= 2; 1805 } else { 1806 params.opad_needed = 0; 1807 } 1808 1809 req_ctx->hctx_wr.result = 1; 1810 params.bfr_len = req_ctx->reqlen; 1811 req_ctx->data_len += params.bfr_len + params.sg_len; 1812 req_ctx->hctx_wr.srcsg = req->src; 1813 if (req_ctx->reqlen == 0) { 1814 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len); 1815 params.last = 0; 1816 params.more = 1; 1817 params.scmd1 = 0; 1818 params.bfr_len = bs; 1819 1820 } else { 1821 params.scmd1 = req_ctx->data_len; 1822 params.last = 1; 1823 params.more = 0; 1824 } 1825 params.hash_size = crypto_ahash_digestsize(rtfm); 1826 skb = create_hash_wr(req, ¶ms); 1827 if (IS_ERR(skb)) { 1828 error = PTR_ERR(skb); 1829 goto err; 1830 } 1831 req_ctx->reqlen = 0; 1832 skb->dev = u_ctx->lldi.ports[0]; 1833 set_wr_txq(skb, CPL_PRIORITY_DATA, req_ctx->txqidx); 1834 chcr_send_wr(skb); 1835 return -EINPROGRESS; 1836 err: 1837 chcr_dec_wrcount(dev); 1838 return error; 1839 } 1840 1841 static int chcr_ahash_finup(struct ahash_request *req) 1842 { 1843 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req); 1844 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req); 1845 struct chcr_dev *dev = h_ctx(rtfm)->dev; 1846 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(rtfm)); 1847 struct chcr_context *ctx = h_ctx(rtfm); 1848 struct sk_buff *skb; 1849 struct hash_wr_param params; 1850 u8 bs; 1851 int error; 1852 unsigned int cpu; 1853 1854 cpu = get_cpu(); 1855 req_ctx->txqidx = cpu % ctx->ntxq; 1856 req_ctx->rxqidx = cpu % ctx->nrxq; 1857 put_cpu(); 1858 1859 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm)); 1860 error = chcr_inc_wrcount(dev); 1861 if (error) 1862 return -ENXIO; 1863 1864 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0], 1865 req_ctx->txqidx) && 1866 (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) { 1867 error = -ENOSPC; 1868 goto err; 1869 } 1870 chcr_init_hctx_per_wr(req_ctx); 1871 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req); 1872 if (error) { 1873 error = -ENOMEM; 1874 goto err; 1875 } 1876 1877 get_alg_config(¶ms.alg_prm, crypto_ahash_digestsize(rtfm)); 1878 params.kctx_len = roundup(params.alg_prm.result_size, 16); 1879 if (is_hmac(crypto_ahash_tfm(rtfm))) { 1880 params.kctx_len *= 2; 1881 params.opad_needed = 1; 1882 } else { 1883 params.opad_needed = 0; 1884 } 1885 1886 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen, 1887 HASH_SPACE_LEFT(params.kctx_len), 0); 1888 if (params.sg_len < req->nbytes) { 1889 if (is_hmac(crypto_ahash_tfm(rtfm))) { 1890 params.kctx_len /= 2; 1891 params.opad_needed = 0; 1892 } 1893 params.last = 0; 1894 params.more = 1; 1895 params.sg_len = rounddown(params.sg_len + req_ctx->reqlen, bs) 1896 - req_ctx->reqlen; 1897 params.hash_size = params.alg_prm.result_size; 1898 params.scmd1 = 0; 1899 } else { 1900 params.last = 1; 1901 params.more = 0; 1902 params.sg_len = req->nbytes; 1903 params.hash_size = crypto_ahash_digestsize(rtfm); 1904 params.scmd1 = req_ctx->data_len + req_ctx->reqlen + 1905 params.sg_len; 1906 } 1907 params.bfr_len = req_ctx->reqlen; 1908 req_ctx->data_len += params.bfr_len + params.sg_len; 1909 req_ctx->hctx_wr.result = 1; 1910 req_ctx->hctx_wr.srcsg = req->src; 1911 if ((req_ctx->reqlen + req->nbytes) == 0) { 1912 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len); 1913 params.last = 0; 1914 params.more = 1; 1915 params.scmd1 = 0; 1916 params.bfr_len = bs; 1917 } 1918 skb = create_hash_wr(req, ¶ms); 1919 if (IS_ERR(skb)) { 1920 error = PTR_ERR(skb); 1921 goto unmap; 1922 } 1923 req_ctx->reqlen = 0; 1924 req_ctx->hctx_wr.processed += params.sg_len; 1925 skb->dev = u_ctx->lldi.ports[0]; 1926 set_wr_txq(skb, CPL_PRIORITY_DATA, req_ctx->txqidx); 1927 chcr_send_wr(skb); 1928 return -EINPROGRESS; 1929 unmap: 1930 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req); 1931 err: 1932 chcr_dec_wrcount(dev); 1933 return error; 1934 } 1935 1936 static int chcr_ahash_digest(struct ahash_request *req) 1937 { 1938 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req); 1939 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req); 1940 struct chcr_dev *dev = h_ctx(rtfm)->dev; 1941 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(rtfm)); 1942 struct chcr_context *ctx = h_ctx(rtfm); 1943 struct sk_buff *skb; 1944 struct hash_wr_param params; 1945 u8 bs; 1946 int error; 1947 unsigned int cpu; 1948 1949 cpu = get_cpu(); 1950 req_ctx->txqidx = cpu % ctx->ntxq; 1951 req_ctx->rxqidx = cpu % ctx->nrxq; 1952 put_cpu(); 1953 1954 rtfm->init(req); 1955 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm)); 1956 error = chcr_inc_wrcount(dev); 1957 if (error) 1958 return -ENXIO; 1959 1960 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0], 1961 req_ctx->txqidx) && 1962 (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))) { 1963 error = -ENOSPC; 1964 goto err; 1965 } 1966 1967 chcr_init_hctx_per_wr(req_ctx); 1968 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req); 1969 if (error) { 1970 error = -ENOMEM; 1971 goto err; 1972 } 1973 1974 get_alg_config(¶ms.alg_prm, crypto_ahash_digestsize(rtfm)); 1975 params.kctx_len = roundup(params.alg_prm.result_size, 16); 1976 if (is_hmac(crypto_ahash_tfm(rtfm))) { 1977 params.kctx_len *= 2; 1978 params.opad_needed = 1; 1979 } else { 1980 params.opad_needed = 0; 1981 } 1982 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen, 1983 HASH_SPACE_LEFT(params.kctx_len), 0); 1984 if (params.sg_len < req->nbytes) { 1985 if (is_hmac(crypto_ahash_tfm(rtfm))) { 1986 params.kctx_len /= 2; 1987 params.opad_needed = 0; 1988 } 1989 params.last = 0; 1990 params.more = 1; 1991 params.scmd1 = 0; 1992 params.sg_len = rounddown(params.sg_len, bs); 1993 params.hash_size = params.alg_prm.result_size; 1994 } else { 1995 params.sg_len = req->nbytes; 1996 params.hash_size = crypto_ahash_digestsize(rtfm); 1997 params.last = 1; 1998 params.more = 0; 1999 params.scmd1 = req->nbytes + req_ctx->data_len; 2000 2001 } 2002 params.bfr_len = 0; 2003 req_ctx->hctx_wr.result = 1; 2004 req_ctx->hctx_wr.srcsg = req->src; 2005 req_ctx->data_len += params.bfr_len + params.sg_len; 2006 2007 if (req->nbytes == 0) { 2008 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len); 2009 params.more = 1; 2010 params.bfr_len = bs; 2011 } 2012 2013 skb = create_hash_wr(req, ¶ms); 2014 if (IS_ERR(skb)) { 2015 error = PTR_ERR(skb); 2016 goto unmap; 2017 } 2018 req_ctx->hctx_wr.processed += params.sg_len; 2019 skb->dev = u_ctx->lldi.ports[0]; 2020 set_wr_txq(skb, CPL_PRIORITY_DATA, req_ctx->txqidx); 2021 chcr_send_wr(skb); 2022 return -EINPROGRESS; 2023 unmap: 2024 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req); 2025 err: 2026 chcr_dec_wrcount(dev); 2027 return error; 2028 } 2029 2030 static int chcr_ahash_continue(struct ahash_request *req) 2031 { 2032 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req); 2033 struct chcr_hctx_per_wr *hctx_wr = &reqctx->hctx_wr; 2034 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req); 2035 struct chcr_context *ctx = h_ctx(rtfm); 2036 struct uld_ctx *u_ctx = ULD_CTX(ctx); 2037 struct sk_buff *skb; 2038 struct hash_wr_param params; 2039 u8 bs; 2040 int error; 2041 unsigned int cpu; 2042 2043 cpu = get_cpu(); 2044 reqctx->txqidx = cpu % ctx->ntxq; 2045 reqctx->rxqidx = cpu % ctx->nrxq; 2046 put_cpu(); 2047 2048 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm)); 2049 get_alg_config(¶ms.alg_prm, crypto_ahash_digestsize(rtfm)); 2050 params.kctx_len = roundup(params.alg_prm.result_size, 16); 2051 if (is_hmac(crypto_ahash_tfm(rtfm))) { 2052 params.kctx_len *= 2; 2053 params.opad_needed = 1; 2054 } else { 2055 params.opad_needed = 0; 2056 } 2057 params.sg_len = chcr_hash_ent_in_wr(hctx_wr->srcsg, 0, 2058 HASH_SPACE_LEFT(params.kctx_len), 2059 hctx_wr->src_ofst); 2060 if ((params.sg_len + hctx_wr->processed) > req->nbytes) 2061 params.sg_len = req->nbytes - hctx_wr->processed; 2062 if (!hctx_wr->result || 2063 ((params.sg_len + hctx_wr->processed) < req->nbytes)) { 2064 if (is_hmac(crypto_ahash_tfm(rtfm))) { 2065 params.kctx_len /= 2; 2066 params.opad_needed = 0; 2067 } 2068 params.last = 0; 2069 params.more = 1; 2070 params.sg_len = rounddown(params.sg_len, bs); 2071 params.hash_size = params.alg_prm.result_size; 2072 params.scmd1 = 0; 2073 } else { 2074 params.last = 1; 2075 params.more = 0; 2076 params.hash_size = crypto_ahash_digestsize(rtfm); 2077 params.scmd1 = reqctx->data_len + params.sg_len; 2078 } 2079 params.bfr_len = 0; 2080 reqctx->data_len += params.sg_len; 2081 skb = create_hash_wr(req, ¶ms); 2082 if (IS_ERR(skb)) { 2083 error = PTR_ERR(skb); 2084 goto err; 2085 } 2086 hctx_wr->processed += params.sg_len; 2087 skb->dev = u_ctx->lldi.ports[0]; 2088 set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx); 2089 chcr_send_wr(skb); 2090 return 0; 2091 err: 2092 return error; 2093 } 2094 2095 static inline void chcr_handle_ahash_resp(struct ahash_request *req, 2096 unsigned char *input, 2097 int err) 2098 { 2099 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req); 2100 struct chcr_hctx_per_wr *hctx_wr = &reqctx->hctx_wr; 2101 int digestsize, updated_digestsize; 2102 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 2103 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(tfm)); 2104 struct chcr_dev *dev = h_ctx(tfm)->dev; 2105 2106 if (input == NULL) 2107 goto out; 2108 digestsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req)); 2109 updated_digestsize = digestsize; 2110 if (digestsize == SHA224_DIGEST_SIZE) 2111 updated_digestsize = SHA256_DIGEST_SIZE; 2112 else if (digestsize == SHA384_DIGEST_SIZE) 2113 updated_digestsize = SHA512_DIGEST_SIZE; 2114 2115 if (hctx_wr->dma_addr) { 2116 dma_unmap_single(&u_ctx->lldi.pdev->dev, hctx_wr->dma_addr, 2117 hctx_wr->dma_len, DMA_TO_DEVICE); 2118 hctx_wr->dma_addr = 0; 2119 } 2120 if (hctx_wr->isfinal || ((hctx_wr->processed + reqctx->reqlen) == 2121 req->nbytes)) { 2122 if (hctx_wr->result == 1) { 2123 hctx_wr->result = 0; 2124 memcpy(req->result, input + sizeof(struct cpl_fw6_pld), 2125 digestsize); 2126 } else { 2127 memcpy(reqctx->partial_hash, 2128 input + sizeof(struct cpl_fw6_pld), 2129 updated_digestsize); 2130 2131 } 2132 goto unmap; 2133 } 2134 memcpy(reqctx->partial_hash, input + sizeof(struct cpl_fw6_pld), 2135 updated_digestsize); 2136 2137 err = chcr_ahash_continue(req); 2138 if (err) 2139 goto unmap; 2140 return; 2141 unmap: 2142 if (hctx_wr->is_sg_map) 2143 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req); 2144 2145 2146 out: 2147 chcr_dec_wrcount(dev); 2148 req->base.complete(&req->base, err); 2149 } 2150 2151 /* 2152 * chcr_handle_resp - Unmap the DMA buffers associated with the request 2153 * @req: crypto request 2154 */ 2155 int chcr_handle_resp(struct crypto_async_request *req, unsigned char *input, 2156 int err) 2157 { 2158 struct crypto_tfm *tfm = req->tfm; 2159 struct chcr_context *ctx = crypto_tfm_ctx(tfm); 2160 struct adapter *adap = padap(ctx->dev); 2161 2162 switch (tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK) { 2163 case CRYPTO_ALG_TYPE_AEAD: 2164 err = chcr_handle_aead_resp(aead_request_cast(req), input, err); 2165 break; 2166 2167 case CRYPTO_ALG_TYPE_SKCIPHER: 2168 chcr_handle_cipher_resp(skcipher_request_cast(req), 2169 input, err); 2170 break; 2171 case CRYPTO_ALG_TYPE_AHASH: 2172 chcr_handle_ahash_resp(ahash_request_cast(req), input, err); 2173 } 2174 atomic_inc(&adap->chcr_stats.complete); 2175 return err; 2176 } 2177 static int chcr_ahash_export(struct ahash_request *areq, void *out) 2178 { 2179 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 2180 struct chcr_ahash_req_ctx *state = out; 2181 2182 state->reqlen = req_ctx->reqlen; 2183 state->data_len = req_ctx->data_len; 2184 memcpy(state->bfr1, req_ctx->reqbfr, req_ctx->reqlen); 2185 memcpy(state->partial_hash, req_ctx->partial_hash, 2186 CHCR_HASH_MAX_DIGEST_SIZE); 2187 chcr_init_hctx_per_wr(state); 2188 return 0; 2189 } 2190 2191 static int chcr_ahash_import(struct ahash_request *areq, const void *in) 2192 { 2193 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 2194 struct chcr_ahash_req_ctx *state = (struct chcr_ahash_req_ctx *)in; 2195 2196 req_ctx->reqlen = state->reqlen; 2197 req_ctx->data_len = state->data_len; 2198 req_ctx->reqbfr = req_ctx->bfr1; 2199 req_ctx->skbfr = req_ctx->bfr2; 2200 memcpy(req_ctx->bfr1, state->bfr1, CHCR_HASH_MAX_BLOCK_SIZE_128); 2201 memcpy(req_ctx->partial_hash, state->partial_hash, 2202 CHCR_HASH_MAX_DIGEST_SIZE); 2203 chcr_init_hctx_per_wr(req_ctx); 2204 return 0; 2205 } 2206 2207 static int chcr_ahash_setkey(struct crypto_ahash *tfm, const u8 *key, 2208 unsigned int keylen) 2209 { 2210 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(tfm)); 2211 unsigned int digestsize = crypto_ahash_digestsize(tfm); 2212 unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm)); 2213 unsigned int i, err = 0, updated_digestsize; 2214 2215 SHASH_DESC_ON_STACK(shash, hmacctx->base_hash); 2216 2217 /* use the key to calculate the ipad and opad. ipad will sent with the 2218 * first request's data. opad will be sent with the final hash result 2219 * ipad in hmacctx->ipad and opad in hmacctx->opad location 2220 */ 2221 shash->tfm = hmacctx->base_hash; 2222 if (keylen > bs) { 2223 err = crypto_shash_digest(shash, key, keylen, 2224 hmacctx->ipad); 2225 if (err) 2226 goto out; 2227 keylen = digestsize; 2228 } else { 2229 memcpy(hmacctx->ipad, key, keylen); 2230 } 2231 memset(hmacctx->ipad + keylen, 0, bs - keylen); 2232 memcpy(hmacctx->opad, hmacctx->ipad, bs); 2233 2234 for (i = 0; i < bs / sizeof(int); i++) { 2235 *((unsigned int *)(&hmacctx->ipad) + i) ^= IPAD_DATA; 2236 *((unsigned int *)(&hmacctx->opad) + i) ^= OPAD_DATA; 2237 } 2238 2239 updated_digestsize = digestsize; 2240 if (digestsize == SHA224_DIGEST_SIZE) 2241 updated_digestsize = SHA256_DIGEST_SIZE; 2242 else if (digestsize == SHA384_DIGEST_SIZE) 2243 updated_digestsize = SHA512_DIGEST_SIZE; 2244 err = chcr_compute_partial_hash(shash, hmacctx->ipad, 2245 hmacctx->ipad, digestsize); 2246 if (err) 2247 goto out; 2248 chcr_change_order(hmacctx->ipad, updated_digestsize); 2249 2250 err = chcr_compute_partial_hash(shash, hmacctx->opad, 2251 hmacctx->opad, digestsize); 2252 if (err) 2253 goto out; 2254 chcr_change_order(hmacctx->opad, updated_digestsize); 2255 out: 2256 return err; 2257 } 2258 2259 static int chcr_aes_xts_setkey(struct crypto_skcipher *cipher, const u8 *key, 2260 unsigned int key_len) 2261 { 2262 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher)); 2263 unsigned short context_size = 0; 2264 int err; 2265 2266 err = chcr_cipher_fallback_setkey(cipher, key, key_len); 2267 if (err) 2268 goto badkey_err; 2269 2270 memcpy(ablkctx->key, key, key_len); 2271 ablkctx->enckey_len = key_len; 2272 get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, key_len << 2); 2273 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len) >> 4; 2274 /* Both keys for xts must be aligned to 16 byte boundary 2275 * by padding with zeros. So for 24 byte keys padding 8 zeroes. 2276 */ 2277 if (key_len == 48) { 2278 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len 2279 + 16) >> 4; 2280 memmove(ablkctx->key + 32, ablkctx->key + 24, 24); 2281 memset(ablkctx->key + 24, 0, 8); 2282 memset(ablkctx->key + 56, 0, 8); 2283 ablkctx->enckey_len = 64; 2284 ablkctx->key_ctx_hdr = 2285 FILL_KEY_CTX_HDR(CHCR_KEYCTX_CIPHER_KEY_SIZE_192, 2286 CHCR_KEYCTX_NO_KEY, 1, 2287 0, context_size); 2288 } else { 2289 ablkctx->key_ctx_hdr = 2290 FILL_KEY_CTX_HDR((key_len == AES_KEYSIZE_256) ? 2291 CHCR_KEYCTX_CIPHER_KEY_SIZE_128 : 2292 CHCR_KEYCTX_CIPHER_KEY_SIZE_256, 2293 CHCR_KEYCTX_NO_KEY, 1, 2294 0, context_size); 2295 } 2296 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_XTS; 2297 return 0; 2298 badkey_err: 2299 ablkctx->enckey_len = 0; 2300 2301 return err; 2302 } 2303 2304 static int chcr_sha_init(struct ahash_request *areq) 2305 { 2306 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 2307 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq); 2308 int digestsize = crypto_ahash_digestsize(tfm); 2309 2310 req_ctx->data_len = 0; 2311 req_ctx->reqlen = 0; 2312 req_ctx->reqbfr = req_ctx->bfr1; 2313 req_ctx->skbfr = req_ctx->bfr2; 2314 copy_hash_init_values(req_ctx->partial_hash, digestsize); 2315 2316 return 0; 2317 } 2318 2319 static int chcr_sha_cra_init(struct crypto_tfm *tfm) 2320 { 2321 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), 2322 sizeof(struct chcr_ahash_req_ctx)); 2323 return chcr_device_init(crypto_tfm_ctx(tfm)); 2324 } 2325 2326 static int chcr_hmac_init(struct ahash_request *areq) 2327 { 2328 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 2329 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(areq); 2330 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(rtfm)); 2331 unsigned int digestsize = crypto_ahash_digestsize(rtfm); 2332 unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm)); 2333 2334 chcr_sha_init(areq); 2335 req_ctx->data_len = bs; 2336 if (is_hmac(crypto_ahash_tfm(rtfm))) { 2337 if (digestsize == SHA224_DIGEST_SIZE) 2338 memcpy(req_ctx->partial_hash, hmacctx->ipad, 2339 SHA256_DIGEST_SIZE); 2340 else if (digestsize == SHA384_DIGEST_SIZE) 2341 memcpy(req_ctx->partial_hash, hmacctx->ipad, 2342 SHA512_DIGEST_SIZE); 2343 else 2344 memcpy(req_ctx->partial_hash, hmacctx->ipad, 2345 digestsize); 2346 } 2347 return 0; 2348 } 2349 2350 static int chcr_hmac_cra_init(struct crypto_tfm *tfm) 2351 { 2352 struct chcr_context *ctx = crypto_tfm_ctx(tfm); 2353 struct hmac_ctx *hmacctx = HMAC_CTX(ctx); 2354 unsigned int digestsize = 2355 crypto_ahash_digestsize(__crypto_ahash_cast(tfm)); 2356 2357 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), 2358 sizeof(struct chcr_ahash_req_ctx)); 2359 hmacctx->base_hash = chcr_alloc_shash(digestsize); 2360 if (IS_ERR(hmacctx->base_hash)) 2361 return PTR_ERR(hmacctx->base_hash); 2362 return chcr_device_init(crypto_tfm_ctx(tfm)); 2363 } 2364 2365 static void chcr_hmac_cra_exit(struct crypto_tfm *tfm) 2366 { 2367 struct chcr_context *ctx = crypto_tfm_ctx(tfm); 2368 struct hmac_ctx *hmacctx = HMAC_CTX(ctx); 2369 2370 if (hmacctx->base_hash) { 2371 chcr_free_shash(hmacctx->base_hash); 2372 hmacctx->base_hash = NULL; 2373 } 2374 } 2375 2376 inline void chcr_aead_common_exit(struct aead_request *req) 2377 { 2378 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 2379 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 2380 struct uld_ctx *u_ctx = ULD_CTX(a_ctx(tfm)); 2381 2382 chcr_aead_dma_unmap(&u_ctx->lldi.pdev->dev, req, reqctx->op); 2383 } 2384 2385 static int chcr_aead_common_init(struct aead_request *req) 2386 { 2387 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 2388 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm)); 2389 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 2390 unsigned int authsize = crypto_aead_authsize(tfm); 2391 int error = -EINVAL; 2392 2393 /* validate key size */ 2394 if (aeadctx->enckey_len == 0) 2395 goto err; 2396 if (reqctx->op && req->cryptlen < authsize) 2397 goto err; 2398 if (reqctx->b0_len) 2399 reqctx->scratch_pad = reqctx->iv + IV; 2400 else 2401 reqctx->scratch_pad = NULL; 2402 2403 error = chcr_aead_dma_map(&ULD_CTX(a_ctx(tfm))->lldi.pdev->dev, req, 2404 reqctx->op); 2405 if (error) { 2406 error = -ENOMEM; 2407 goto err; 2408 } 2409 2410 return 0; 2411 err: 2412 return error; 2413 } 2414 2415 static int chcr_aead_need_fallback(struct aead_request *req, int dst_nents, 2416 int aadmax, int wrlen, 2417 unsigned short op_type) 2418 { 2419 unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req)); 2420 2421 if (((req->cryptlen - (op_type ? authsize : 0)) == 0) || 2422 dst_nents > MAX_DSGL_ENT || 2423 (req->assoclen > aadmax) || 2424 (wrlen > SGE_MAX_WR_LEN)) 2425 return 1; 2426 return 0; 2427 } 2428 2429 static int chcr_aead_fallback(struct aead_request *req, unsigned short op_type) 2430 { 2431 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 2432 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm)); 2433 struct aead_request *subreq = aead_request_ctx(req); 2434 2435 aead_request_set_tfm(subreq, aeadctx->sw_cipher); 2436 aead_request_set_callback(subreq, req->base.flags, 2437 req->base.complete, req->base.data); 2438 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, 2439 req->iv); 2440 aead_request_set_ad(subreq, req->assoclen); 2441 return op_type ? crypto_aead_decrypt(subreq) : 2442 crypto_aead_encrypt(subreq); 2443 } 2444 2445 static struct sk_buff *create_authenc_wr(struct aead_request *req, 2446 unsigned short qid, 2447 int size) 2448 { 2449 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 2450 struct chcr_context *ctx = a_ctx(tfm); 2451 struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx); 2452 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx); 2453 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 2454 struct sk_buff *skb = NULL; 2455 struct chcr_wr *chcr_req; 2456 struct cpl_rx_phys_dsgl *phys_cpl; 2457 struct ulptx_sgl *ulptx; 2458 unsigned int transhdr_len; 2459 unsigned int dst_size = 0, temp, subtype = get_aead_subtype(tfm); 2460 unsigned int kctx_len = 0, dnents, snents; 2461 unsigned int authsize = crypto_aead_authsize(tfm); 2462 int error = -EINVAL; 2463 u8 *ivptr; 2464 int null = 0; 2465 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : 2466 GFP_ATOMIC; 2467 struct adapter *adap = padap(ctx->dev); 2468 unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan; 2469 2470 if (req->cryptlen == 0) 2471 return NULL; 2472 2473 reqctx->b0_len = 0; 2474 error = chcr_aead_common_init(req); 2475 if (error) 2476 return ERR_PTR(error); 2477 2478 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL || 2479 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) { 2480 null = 1; 2481 } 2482 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen + 2483 (reqctx->op ? -authsize : authsize), CHCR_DST_SG_SIZE, 0); 2484 dnents += MIN_AUTH_SG; // For IV 2485 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen, 2486 CHCR_SRC_SG_SIZE, 0); 2487 dst_size = get_space_for_phys_dsgl(dnents); 2488 kctx_len = (KEY_CONTEXT_CTX_LEN_G(ntohl(aeadctx->key_ctx_hdr)) << 4) 2489 - sizeof(chcr_req->key_ctx); 2490 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size); 2491 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen) < 2492 SGE_MAX_WR_LEN; 2493 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen, 16) 2494 : (sgl_len(snents) * 8); 2495 transhdr_len += temp; 2496 transhdr_len = roundup(transhdr_len, 16); 2497 2498 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE, 2499 transhdr_len, reqctx->op)) { 2500 atomic_inc(&adap->chcr_stats.fallback); 2501 chcr_aead_common_exit(req); 2502 return ERR_PTR(chcr_aead_fallback(req, reqctx->op)); 2503 } 2504 skb = alloc_skb(transhdr_len, flags); 2505 if (!skb) { 2506 error = -ENOMEM; 2507 goto err; 2508 } 2509 2510 chcr_req = __skb_put_zero(skb, transhdr_len); 2511 2512 temp = (reqctx->op == CHCR_ENCRYPT_OP) ? 0 : authsize; 2513 2514 /* 2515 * Input order is AAD,IV and Payload. where IV should be included as 2516 * the part of authdata. All other fields should be filled according 2517 * to the hardware spec 2518 */ 2519 chcr_req->sec_cpl.op_ivinsrtofst = 2520 FILL_SEC_CPL_OP_IVINSR(rx_channel_id, 2, 1); 2521 chcr_req->sec_cpl.pldlen = htonl(req->assoclen + IV + req->cryptlen); 2522 chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI( 2523 null ? 0 : 1 + IV, 2524 null ? 0 : IV + req->assoclen, 2525 req->assoclen + IV + 1, 2526 (temp & 0x1F0) >> 4); 2527 chcr_req->sec_cpl.cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT( 2528 temp & 0xF, 2529 null ? 0 : req->assoclen + IV + 1, 2530 temp, temp); 2531 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL || 2532 subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA) 2533 temp = CHCR_SCMD_CIPHER_MODE_AES_CTR; 2534 else 2535 temp = CHCR_SCMD_CIPHER_MODE_AES_CBC; 2536 chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op, 2537 (reqctx->op == CHCR_ENCRYPT_OP) ? 1 : 0, 2538 temp, 2539 actx->auth_mode, aeadctx->hmac_ctrl, 2540 IV >> 1); 2541 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1, 2542 0, 0, dst_size); 2543 2544 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr; 2545 if (reqctx->op == CHCR_ENCRYPT_OP || 2546 subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA || 2547 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) 2548 memcpy(chcr_req->key_ctx.key, aeadctx->key, 2549 aeadctx->enckey_len); 2550 else 2551 memcpy(chcr_req->key_ctx.key, actx->dec_rrkey, 2552 aeadctx->enckey_len); 2553 2554 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16), 2555 actx->h_iopad, kctx_len - roundup(aeadctx->enckey_len, 16)); 2556 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len); 2557 ivptr = (u8 *)(phys_cpl + 1) + dst_size; 2558 ulptx = (struct ulptx_sgl *)(ivptr + IV); 2559 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA || 2560 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) { 2561 memcpy(ivptr, aeadctx->nonce, CTR_RFC3686_NONCE_SIZE); 2562 memcpy(ivptr + CTR_RFC3686_NONCE_SIZE, req->iv, 2563 CTR_RFC3686_IV_SIZE); 2564 *(__be32 *)(ivptr + CTR_RFC3686_NONCE_SIZE + 2565 CTR_RFC3686_IV_SIZE) = cpu_to_be32(1); 2566 } else { 2567 memcpy(ivptr, req->iv, IV); 2568 } 2569 chcr_add_aead_dst_ent(req, phys_cpl, qid); 2570 chcr_add_aead_src_ent(req, ulptx); 2571 atomic_inc(&adap->chcr_stats.cipher_rqst); 2572 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV + 2573 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen) : 0); 2574 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, size, 2575 transhdr_len, temp, 0); 2576 reqctx->skb = skb; 2577 2578 return skb; 2579 err: 2580 chcr_aead_common_exit(req); 2581 2582 return ERR_PTR(error); 2583 } 2584 2585 int chcr_aead_dma_map(struct device *dev, 2586 struct aead_request *req, 2587 unsigned short op_type) 2588 { 2589 int error; 2590 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 2591 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 2592 unsigned int authsize = crypto_aead_authsize(tfm); 2593 int dst_size; 2594 2595 dst_size = req->assoclen + req->cryptlen + (op_type ? 2596 0 : authsize); 2597 if (!req->cryptlen || !dst_size) 2598 return 0; 2599 reqctx->iv_dma = dma_map_single(dev, reqctx->iv, (IV + reqctx->b0_len), 2600 DMA_BIDIRECTIONAL); 2601 if (dma_mapping_error(dev, reqctx->iv_dma)) 2602 return -ENOMEM; 2603 if (reqctx->b0_len) 2604 reqctx->b0_dma = reqctx->iv_dma + IV; 2605 else 2606 reqctx->b0_dma = 0; 2607 if (req->src == req->dst) { 2608 error = dma_map_sg(dev, req->src, 2609 sg_nents_for_len(req->src, dst_size), 2610 DMA_BIDIRECTIONAL); 2611 if (!error) 2612 goto err; 2613 } else { 2614 error = dma_map_sg(dev, req->src, sg_nents(req->src), 2615 DMA_TO_DEVICE); 2616 if (!error) 2617 goto err; 2618 error = dma_map_sg(dev, req->dst, sg_nents(req->dst), 2619 DMA_FROM_DEVICE); 2620 if (!error) { 2621 dma_unmap_sg(dev, req->src, sg_nents(req->src), 2622 DMA_TO_DEVICE); 2623 goto err; 2624 } 2625 } 2626 2627 return 0; 2628 err: 2629 dma_unmap_single(dev, reqctx->iv_dma, IV, DMA_BIDIRECTIONAL); 2630 return -ENOMEM; 2631 } 2632 2633 void chcr_aead_dma_unmap(struct device *dev, 2634 struct aead_request *req, 2635 unsigned short op_type) 2636 { 2637 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 2638 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 2639 unsigned int authsize = crypto_aead_authsize(tfm); 2640 int dst_size; 2641 2642 dst_size = req->assoclen + req->cryptlen + (op_type ? 2643 0 : authsize); 2644 if (!req->cryptlen || !dst_size) 2645 return; 2646 2647 dma_unmap_single(dev, reqctx->iv_dma, (IV + reqctx->b0_len), 2648 DMA_BIDIRECTIONAL); 2649 if (req->src == req->dst) { 2650 dma_unmap_sg(dev, req->src, 2651 sg_nents_for_len(req->src, dst_size), 2652 DMA_BIDIRECTIONAL); 2653 } else { 2654 dma_unmap_sg(dev, req->src, sg_nents(req->src), 2655 DMA_TO_DEVICE); 2656 dma_unmap_sg(dev, req->dst, sg_nents(req->dst), 2657 DMA_FROM_DEVICE); 2658 } 2659 } 2660 2661 void chcr_add_aead_src_ent(struct aead_request *req, 2662 struct ulptx_sgl *ulptx) 2663 { 2664 struct ulptx_walk ulp_walk; 2665 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 2666 2667 if (reqctx->imm) { 2668 u8 *buf = (u8 *)ulptx; 2669 2670 if (reqctx->b0_len) { 2671 memcpy(buf, reqctx->scratch_pad, reqctx->b0_len); 2672 buf += reqctx->b0_len; 2673 } 2674 sg_pcopy_to_buffer(req->src, sg_nents(req->src), 2675 buf, req->cryptlen + req->assoclen, 0); 2676 } else { 2677 ulptx_walk_init(&ulp_walk, ulptx); 2678 if (reqctx->b0_len) 2679 ulptx_walk_add_page(&ulp_walk, reqctx->b0_len, 2680 reqctx->b0_dma); 2681 ulptx_walk_add_sg(&ulp_walk, req->src, req->cryptlen + 2682 req->assoclen, 0); 2683 ulptx_walk_end(&ulp_walk); 2684 } 2685 } 2686 2687 void chcr_add_aead_dst_ent(struct aead_request *req, 2688 struct cpl_rx_phys_dsgl *phys_cpl, 2689 unsigned short qid) 2690 { 2691 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 2692 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 2693 struct dsgl_walk dsgl_walk; 2694 unsigned int authsize = crypto_aead_authsize(tfm); 2695 struct chcr_context *ctx = a_ctx(tfm); 2696 u32 temp; 2697 unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan; 2698 2699 dsgl_walk_init(&dsgl_walk, phys_cpl); 2700 dsgl_walk_add_page(&dsgl_walk, IV + reqctx->b0_len, reqctx->iv_dma); 2701 temp = req->assoclen + req->cryptlen + 2702 (reqctx->op ? -authsize : authsize); 2703 dsgl_walk_add_sg(&dsgl_walk, req->dst, temp, 0); 2704 dsgl_walk_end(&dsgl_walk, qid, rx_channel_id); 2705 } 2706 2707 void chcr_add_cipher_src_ent(struct skcipher_request *req, 2708 void *ulptx, 2709 struct cipher_wr_param *wrparam) 2710 { 2711 struct ulptx_walk ulp_walk; 2712 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req); 2713 u8 *buf = ulptx; 2714 2715 memcpy(buf, reqctx->iv, IV); 2716 buf += IV; 2717 if (reqctx->imm) { 2718 sg_pcopy_to_buffer(req->src, sg_nents(req->src), 2719 buf, wrparam->bytes, reqctx->processed); 2720 } else { 2721 ulptx_walk_init(&ulp_walk, (struct ulptx_sgl *)buf); 2722 ulptx_walk_add_sg(&ulp_walk, reqctx->srcsg, wrparam->bytes, 2723 reqctx->src_ofst); 2724 reqctx->srcsg = ulp_walk.last_sg; 2725 reqctx->src_ofst = ulp_walk.last_sg_len; 2726 ulptx_walk_end(&ulp_walk); 2727 } 2728 } 2729 2730 void chcr_add_cipher_dst_ent(struct skcipher_request *req, 2731 struct cpl_rx_phys_dsgl *phys_cpl, 2732 struct cipher_wr_param *wrparam, 2733 unsigned short qid) 2734 { 2735 struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req); 2736 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(wrparam->req); 2737 struct chcr_context *ctx = c_ctx(tfm); 2738 struct dsgl_walk dsgl_walk; 2739 unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan; 2740 2741 dsgl_walk_init(&dsgl_walk, phys_cpl); 2742 dsgl_walk_add_sg(&dsgl_walk, reqctx->dstsg, wrparam->bytes, 2743 reqctx->dst_ofst); 2744 reqctx->dstsg = dsgl_walk.last_sg; 2745 reqctx->dst_ofst = dsgl_walk.last_sg_len; 2746 dsgl_walk_end(&dsgl_walk, qid, rx_channel_id); 2747 } 2748 2749 void chcr_add_hash_src_ent(struct ahash_request *req, 2750 struct ulptx_sgl *ulptx, 2751 struct hash_wr_param *param) 2752 { 2753 struct ulptx_walk ulp_walk; 2754 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req); 2755 2756 if (reqctx->hctx_wr.imm) { 2757 u8 *buf = (u8 *)ulptx; 2758 2759 if (param->bfr_len) { 2760 memcpy(buf, reqctx->reqbfr, param->bfr_len); 2761 buf += param->bfr_len; 2762 } 2763 2764 sg_pcopy_to_buffer(reqctx->hctx_wr.srcsg, 2765 sg_nents(reqctx->hctx_wr.srcsg), buf, 2766 param->sg_len, 0); 2767 } else { 2768 ulptx_walk_init(&ulp_walk, ulptx); 2769 if (param->bfr_len) 2770 ulptx_walk_add_page(&ulp_walk, param->bfr_len, 2771 reqctx->hctx_wr.dma_addr); 2772 ulptx_walk_add_sg(&ulp_walk, reqctx->hctx_wr.srcsg, 2773 param->sg_len, reqctx->hctx_wr.src_ofst); 2774 reqctx->hctx_wr.srcsg = ulp_walk.last_sg; 2775 reqctx->hctx_wr.src_ofst = ulp_walk.last_sg_len; 2776 ulptx_walk_end(&ulp_walk); 2777 } 2778 } 2779 2780 int chcr_hash_dma_map(struct device *dev, 2781 struct ahash_request *req) 2782 { 2783 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req); 2784 int error = 0; 2785 2786 if (!req->nbytes) 2787 return 0; 2788 error = dma_map_sg(dev, req->src, sg_nents(req->src), 2789 DMA_TO_DEVICE); 2790 if (!error) 2791 return -ENOMEM; 2792 req_ctx->hctx_wr.is_sg_map = 1; 2793 return 0; 2794 } 2795 2796 void chcr_hash_dma_unmap(struct device *dev, 2797 struct ahash_request *req) 2798 { 2799 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req); 2800 2801 if (!req->nbytes) 2802 return; 2803 2804 dma_unmap_sg(dev, req->src, sg_nents(req->src), 2805 DMA_TO_DEVICE); 2806 req_ctx->hctx_wr.is_sg_map = 0; 2807 2808 } 2809 2810 int chcr_cipher_dma_map(struct device *dev, 2811 struct skcipher_request *req) 2812 { 2813 int error; 2814 2815 if (req->src == req->dst) { 2816 error = dma_map_sg(dev, req->src, sg_nents(req->src), 2817 DMA_BIDIRECTIONAL); 2818 if (!error) 2819 goto err; 2820 } else { 2821 error = dma_map_sg(dev, req->src, sg_nents(req->src), 2822 DMA_TO_DEVICE); 2823 if (!error) 2824 goto err; 2825 error = dma_map_sg(dev, req->dst, sg_nents(req->dst), 2826 DMA_FROM_DEVICE); 2827 if (!error) { 2828 dma_unmap_sg(dev, req->src, sg_nents(req->src), 2829 DMA_TO_DEVICE); 2830 goto err; 2831 } 2832 } 2833 2834 return 0; 2835 err: 2836 return -ENOMEM; 2837 } 2838 2839 void chcr_cipher_dma_unmap(struct device *dev, 2840 struct skcipher_request *req) 2841 { 2842 if (req->src == req->dst) { 2843 dma_unmap_sg(dev, req->src, sg_nents(req->src), 2844 DMA_BIDIRECTIONAL); 2845 } else { 2846 dma_unmap_sg(dev, req->src, sg_nents(req->src), 2847 DMA_TO_DEVICE); 2848 dma_unmap_sg(dev, req->dst, sg_nents(req->dst), 2849 DMA_FROM_DEVICE); 2850 } 2851 } 2852 2853 static int set_msg_len(u8 *block, unsigned int msglen, int csize) 2854 { 2855 __be32 data; 2856 2857 memset(block, 0, csize); 2858 block += csize; 2859 2860 if (csize >= 4) 2861 csize = 4; 2862 else if (msglen > (unsigned int)(1 << (8 * csize))) 2863 return -EOVERFLOW; 2864 2865 data = cpu_to_be32(msglen); 2866 memcpy(block - csize, (u8 *)&data + 4 - csize, csize); 2867 2868 return 0; 2869 } 2870 2871 static int generate_b0(struct aead_request *req, u8 *ivptr, 2872 unsigned short op_type) 2873 { 2874 unsigned int l, lp, m; 2875 int rc; 2876 struct crypto_aead *aead = crypto_aead_reqtfm(req); 2877 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 2878 u8 *b0 = reqctx->scratch_pad; 2879 2880 m = crypto_aead_authsize(aead); 2881 2882 memcpy(b0, ivptr, 16); 2883 2884 lp = b0[0]; 2885 l = lp + 1; 2886 2887 /* set m, bits 3-5 */ 2888 *b0 |= (8 * ((m - 2) / 2)); 2889 2890 /* set adata, bit 6, if associated data is used */ 2891 if (req->assoclen) 2892 *b0 |= 64; 2893 rc = set_msg_len(b0 + 16 - l, 2894 (op_type == CHCR_DECRYPT_OP) ? 2895 req->cryptlen - m : req->cryptlen, l); 2896 2897 return rc; 2898 } 2899 2900 static inline int crypto_ccm_check_iv(const u8 *iv) 2901 { 2902 /* 2 <= L <= 8, so 1 <= L' <= 7. */ 2903 if (iv[0] < 1 || iv[0] > 7) 2904 return -EINVAL; 2905 2906 return 0; 2907 } 2908 2909 static int ccm_format_packet(struct aead_request *req, 2910 u8 *ivptr, 2911 unsigned int sub_type, 2912 unsigned short op_type, 2913 unsigned int assoclen) 2914 { 2915 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 2916 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 2917 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm)); 2918 int rc = 0; 2919 2920 if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) { 2921 ivptr[0] = 3; 2922 memcpy(ivptr + 1, &aeadctx->salt[0], 3); 2923 memcpy(ivptr + 4, req->iv, 8); 2924 memset(ivptr + 12, 0, 4); 2925 } else { 2926 memcpy(ivptr, req->iv, 16); 2927 } 2928 if (assoclen) 2929 *((unsigned short *)(reqctx->scratch_pad + 16)) = 2930 htons(assoclen); 2931 2932 rc = generate_b0(req, ivptr, op_type); 2933 /* zero the ctr value */ 2934 memset(ivptr + 15 - ivptr[0], 0, ivptr[0] + 1); 2935 return rc; 2936 } 2937 2938 static void fill_sec_cpl_for_aead(struct cpl_tx_sec_pdu *sec_cpl, 2939 unsigned int dst_size, 2940 struct aead_request *req, 2941 unsigned short op_type) 2942 { 2943 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 2944 struct chcr_context *ctx = a_ctx(tfm); 2945 struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx); 2946 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 2947 unsigned int cipher_mode = CHCR_SCMD_CIPHER_MODE_AES_CCM; 2948 unsigned int mac_mode = CHCR_SCMD_AUTH_MODE_CBCMAC; 2949 unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan; 2950 unsigned int ccm_xtra; 2951 unsigned int tag_offset = 0, auth_offset = 0; 2952 unsigned int assoclen; 2953 2954 if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) 2955 assoclen = req->assoclen - 8; 2956 else 2957 assoclen = req->assoclen; 2958 ccm_xtra = CCM_B0_SIZE + 2959 ((assoclen) ? CCM_AAD_FIELD_SIZE : 0); 2960 2961 auth_offset = req->cryptlen ? 2962 (req->assoclen + IV + 1 + ccm_xtra) : 0; 2963 if (op_type == CHCR_DECRYPT_OP) { 2964 if (crypto_aead_authsize(tfm) != req->cryptlen) 2965 tag_offset = crypto_aead_authsize(tfm); 2966 else 2967 auth_offset = 0; 2968 } 2969 2970 sec_cpl->op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(rx_channel_id, 2, 1); 2971 sec_cpl->pldlen = 2972 htonl(req->assoclen + IV + req->cryptlen + ccm_xtra); 2973 /* For CCM there wil be b0 always. So AAD start will be 1 always */ 2974 sec_cpl->aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI( 2975 1 + IV, IV + assoclen + ccm_xtra, 2976 req->assoclen + IV + 1 + ccm_xtra, 0); 2977 2978 sec_cpl->cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(0, 2979 auth_offset, tag_offset, 2980 (op_type == CHCR_ENCRYPT_OP) ? 0 : 2981 crypto_aead_authsize(tfm)); 2982 sec_cpl->seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(op_type, 2983 (op_type == CHCR_ENCRYPT_OP) ? 0 : 1, 2984 cipher_mode, mac_mode, 2985 aeadctx->hmac_ctrl, IV >> 1); 2986 2987 sec_cpl->ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1, 0, 2988 0, dst_size); 2989 } 2990 2991 static int aead_ccm_validate_input(unsigned short op_type, 2992 struct aead_request *req, 2993 struct chcr_aead_ctx *aeadctx, 2994 unsigned int sub_type) 2995 { 2996 if (sub_type != CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) { 2997 if (crypto_ccm_check_iv(req->iv)) { 2998 pr_err("CCM: IV check fails\n"); 2999 return -EINVAL; 3000 } 3001 } else { 3002 if (req->assoclen != 16 && req->assoclen != 20) { 3003 pr_err("RFC4309: Invalid AAD length %d\n", 3004 req->assoclen); 3005 return -EINVAL; 3006 } 3007 } 3008 return 0; 3009 } 3010 3011 static struct sk_buff *create_aead_ccm_wr(struct aead_request *req, 3012 unsigned short qid, 3013 int size) 3014 { 3015 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 3016 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm)); 3017 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 3018 struct sk_buff *skb = NULL; 3019 struct chcr_wr *chcr_req; 3020 struct cpl_rx_phys_dsgl *phys_cpl; 3021 struct ulptx_sgl *ulptx; 3022 unsigned int transhdr_len; 3023 unsigned int dst_size = 0, kctx_len, dnents, temp, snents; 3024 unsigned int sub_type, assoclen = req->assoclen; 3025 unsigned int authsize = crypto_aead_authsize(tfm); 3026 int error = -EINVAL; 3027 u8 *ivptr; 3028 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : 3029 GFP_ATOMIC; 3030 struct adapter *adap = padap(a_ctx(tfm)->dev); 3031 3032 sub_type = get_aead_subtype(tfm); 3033 if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) 3034 assoclen -= 8; 3035 reqctx->b0_len = CCM_B0_SIZE + (assoclen ? CCM_AAD_FIELD_SIZE : 0); 3036 error = chcr_aead_common_init(req); 3037 if (error) 3038 return ERR_PTR(error); 3039 3040 error = aead_ccm_validate_input(reqctx->op, req, aeadctx, sub_type); 3041 if (error) 3042 goto err; 3043 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen 3044 + (reqctx->op ? -authsize : authsize), 3045 CHCR_DST_SG_SIZE, 0); 3046 dnents += MIN_CCM_SG; // For IV and B0 3047 dst_size = get_space_for_phys_dsgl(dnents); 3048 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen, 3049 CHCR_SRC_SG_SIZE, 0); 3050 snents += MIN_CCM_SG; //For B0 3051 kctx_len = roundup(aeadctx->enckey_len, 16) * 2; 3052 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size); 3053 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen + 3054 reqctx->b0_len) <= SGE_MAX_WR_LEN; 3055 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen + 3056 reqctx->b0_len, 16) : 3057 (sgl_len(snents) * 8); 3058 transhdr_len += temp; 3059 transhdr_len = roundup(transhdr_len, 16); 3060 3061 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE - 3062 reqctx->b0_len, transhdr_len, reqctx->op)) { 3063 atomic_inc(&adap->chcr_stats.fallback); 3064 chcr_aead_common_exit(req); 3065 return ERR_PTR(chcr_aead_fallback(req, reqctx->op)); 3066 } 3067 skb = alloc_skb(transhdr_len, flags); 3068 3069 if (!skb) { 3070 error = -ENOMEM; 3071 goto err; 3072 } 3073 3074 chcr_req = __skb_put_zero(skb, transhdr_len); 3075 3076 fill_sec_cpl_for_aead(&chcr_req->sec_cpl, dst_size, req, reqctx->op); 3077 3078 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr; 3079 memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len); 3080 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16), 3081 aeadctx->key, aeadctx->enckey_len); 3082 3083 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len); 3084 ivptr = (u8 *)(phys_cpl + 1) + dst_size; 3085 ulptx = (struct ulptx_sgl *)(ivptr + IV); 3086 error = ccm_format_packet(req, ivptr, sub_type, reqctx->op, assoclen); 3087 if (error) 3088 goto dstmap_fail; 3089 chcr_add_aead_dst_ent(req, phys_cpl, qid); 3090 chcr_add_aead_src_ent(req, ulptx); 3091 3092 atomic_inc(&adap->chcr_stats.aead_rqst); 3093 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV + 3094 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen + 3095 reqctx->b0_len) : 0); 3096 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, 0, 3097 transhdr_len, temp, 0); 3098 reqctx->skb = skb; 3099 3100 return skb; 3101 dstmap_fail: 3102 kfree_skb(skb); 3103 err: 3104 chcr_aead_common_exit(req); 3105 return ERR_PTR(error); 3106 } 3107 3108 static struct sk_buff *create_gcm_wr(struct aead_request *req, 3109 unsigned short qid, 3110 int size) 3111 { 3112 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 3113 struct chcr_context *ctx = a_ctx(tfm); 3114 struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx); 3115 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 3116 struct sk_buff *skb = NULL; 3117 struct chcr_wr *chcr_req; 3118 struct cpl_rx_phys_dsgl *phys_cpl; 3119 struct ulptx_sgl *ulptx; 3120 unsigned int transhdr_len, dnents = 0, snents; 3121 unsigned int dst_size = 0, temp = 0, kctx_len, assoclen = req->assoclen; 3122 unsigned int authsize = crypto_aead_authsize(tfm); 3123 int error = -EINVAL; 3124 u8 *ivptr; 3125 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : 3126 GFP_ATOMIC; 3127 struct adapter *adap = padap(ctx->dev); 3128 unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan; 3129 3130 if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) 3131 assoclen = req->assoclen - 8; 3132 3133 reqctx->b0_len = 0; 3134 error = chcr_aead_common_init(req); 3135 if (error) 3136 return ERR_PTR(error); 3137 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen + 3138 (reqctx->op ? -authsize : authsize), 3139 CHCR_DST_SG_SIZE, 0); 3140 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen, 3141 CHCR_SRC_SG_SIZE, 0); 3142 dnents += MIN_GCM_SG; // For IV 3143 dst_size = get_space_for_phys_dsgl(dnents); 3144 kctx_len = roundup(aeadctx->enckey_len, 16) + AEAD_H_SIZE; 3145 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size); 3146 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen) <= 3147 SGE_MAX_WR_LEN; 3148 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen, 16) : 3149 (sgl_len(snents) * 8); 3150 transhdr_len += temp; 3151 transhdr_len = roundup(transhdr_len, 16); 3152 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE, 3153 transhdr_len, reqctx->op)) { 3154 3155 atomic_inc(&adap->chcr_stats.fallback); 3156 chcr_aead_common_exit(req); 3157 return ERR_PTR(chcr_aead_fallback(req, reqctx->op)); 3158 } 3159 skb = alloc_skb(transhdr_len, flags); 3160 if (!skb) { 3161 error = -ENOMEM; 3162 goto err; 3163 } 3164 3165 chcr_req = __skb_put_zero(skb, transhdr_len); 3166 3167 //Offset of tag from end 3168 temp = (reqctx->op == CHCR_ENCRYPT_OP) ? 0 : authsize; 3169 chcr_req->sec_cpl.op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR( 3170 rx_channel_id, 2, 1); 3171 chcr_req->sec_cpl.pldlen = 3172 htonl(req->assoclen + IV + req->cryptlen); 3173 chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI( 3174 assoclen ? 1 + IV : 0, 3175 assoclen ? IV + assoclen : 0, 3176 req->assoclen + IV + 1, 0); 3177 chcr_req->sec_cpl.cipherstop_lo_authinsert = 3178 FILL_SEC_CPL_AUTHINSERT(0, req->assoclen + IV + 1, 3179 temp, temp); 3180 chcr_req->sec_cpl.seqno_numivs = 3181 FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op, (reqctx->op == 3182 CHCR_ENCRYPT_OP) ? 1 : 0, 3183 CHCR_SCMD_CIPHER_MODE_AES_GCM, 3184 CHCR_SCMD_AUTH_MODE_GHASH, 3185 aeadctx->hmac_ctrl, IV >> 1); 3186 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1, 3187 0, 0, dst_size); 3188 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr; 3189 memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len); 3190 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16), 3191 GCM_CTX(aeadctx)->ghash_h, AEAD_H_SIZE); 3192 3193 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len); 3194 ivptr = (u8 *)(phys_cpl + 1) + dst_size; 3195 /* prepare a 16 byte iv */ 3196 /* S A L T | IV | 0x00000001 */ 3197 if (get_aead_subtype(tfm) == 3198 CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) { 3199 memcpy(ivptr, aeadctx->salt, 4); 3200 memcpy(ivptr + 4, req->iv, GCM_RFC4106_IV_SIZE); 3201 } else { 3202 memcpy(ivptr, req->iv, GCM_AES_IV_SIZE); 3203 } 3204 *((unsigned int *)(ivptr + 12)) = htonl(0x01); 3205 3206 ulptx = (struct ulptx_sgl *)(ivptr + 16); 3207 3208 chcr_add_aead_dst_ent(req, phys_cpl, qid); 3209 chcr_add_aead_src_ent(req, ulptx); 3210 atomic_inc(&adap->chcr_stats.aead_rqst); 3211 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV + 3212 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen) : 0); 3213 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, size, 3214 transhdr_len, temp, reqctx->verify); 3215 reqctx->skb = skb; 3216 return skb; 3217 3218 err: 3219 chcr_aead_common_exit(req); 3220 return ERR_PTR(error); 3221 } 3222 3223 3224 3225 static int chcr_aead_cra_init(struct crypto_aead *tfm) 3226 { 3227 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm)); 3228 struct aead_alg *alg = crypto_aead_alg(tfm); 3229 3230 aeadctx->sw_cipher = crypto_alloc_aead(alg->base.cra_name, 0, 3231 CRYPTO_ALG_NEED_FALLBACK | 3232 CRYPTO_ALG_ASYNC); 3233 if (IS_ERR(aeadctx->sw_cipher)) 3234 return PTR_ERR(aeadctx->sw_cipher); 3235 crypto_aead_set_reqsize(tfm, max(sizeof(struct chcr_aead_reqctx), 3236 sizeof(struct aead_request) + 3237 crypto_aead_reqsize(aeadctx->sw_cipher))); 3238 return chcr_device_init(a_ctx(tfm)); 3239 } 3240 3241 static void chcr_aead_cra_exit(struct crypto_aead *tfm) 3242 { 3243 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm)); 3244 3245 crypto_free_aead(aeadctx->sw_cipher); 3246 } 3247 3248 static int chcr_authenc_null_setauthsize(struct crypto_aead *tfm, 3249 unsigned int authsize) 3250 { 3251 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm)); 3252 3253 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NOP; 3254 aeadctx->mayverify = VERIFY_HW; 3255 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize); 3256 } 3257 static int chcr_authenc_setauthsize(struct crypto_aead *tfm, 3258 unsigned int authsize) 3259 { 3260 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm)); 3261 u32 maxauth = crypto_aead_maxauthsize(tfm); 3262 3263 /*SHA1 authsize in ipsec is 12 instead of 10 i.e maxauthsize / 2 is not 3264 * true for sha1. authsize == 12 condition should be before 3265 * authsize == (maxauth >> 1) 3266 */ 3267 if (authsize == ICV_4) { 3268 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1; 3269 aeadctx->mayverify = VERIFY_HW; 3270 } else if (authsize == ICV_6) { 3271 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2; 3272 aeadctx->mayverify = VERIFY_HW; 3273 } else if (authsize == ICV_10) { 3274 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366; 3275 aeadctx->mayverify = VERIFY_HW; 3276 } else if (authsize == ICV_12) { 3277 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT; 3278 aeadctx->mayverify = VERIFY_HW; 3279 } else if (authsize == ICV_14) { 3280 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3; 3281 aeadctx->mayverify = VERIFY_HW; 3282 } else if (authsize == (maxauth >> 1)) { 3283 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2; 3284 aeadctx->mayverify = VERIFY_HW; 3285 } else if (authsize == maxauth) { 3286 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC; 3287 aeadctx->mayverify = VERIFY_HW; 3288 } else { 3289 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC; 3290 aeadctx->mayverify = VERIFY_SW; 3291 } 3292 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize); 3293 } 3294 3295 3296 static int chcr_gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize) 3297 { 3298 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm)); 3299 3300 switch (authsize) { 3301 case ICV_4: 3302 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1; 3303 aeadctx->mayverify = VERIFY_HW; 3304 break; 3305 case ICV_8: 3306 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2; 3307 aeadctx->mayverify = VERIFY_HW; 3308 break; 3309 case ICV_12: 3310 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT; 3311 aeadctx->mayverify = VERIFY_HW; 3312 break; 3313 case ICV_14: 3314 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3; 3315 aeadctx->mayverify = VERIFY_HW; 3316 break; 3317 case ICV_16: 3318 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC; 3319 aeadctx->mayverify = VERIFY_HW; 3320 break; 3321 case ICV_13: 3322 case ICV_15: 3323 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC; 3324 aeadctx->mayverify = VERIFY_SW; 3325 break; 3326 default: 3327 return -EINVAL; 3328 } 3329 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize); 3330 } 3331 3332 static int chcr_4106_4309_setauthsize(struct crypto_aead *tfm, 3333 unsigned int authsize) 3334 { 3335 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm)); 3336 3337 switch (authsize) { 3338 case ICV_8: 3339 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2; 3340 aeadctx->mayverify = VERIFY_HW; 3341 break; 3342 case ICV_12: 3343 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT; 3344 aeadctx->mayverify = VERIFY_HW; 3345 break; 3346 case ICV_16: 3347 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC; 3348 aeadctx->mayverify = VERIFY_HW; 3349 break; 3350 default: 3351 return -EINVAL; 3352 } 3353 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize); 3354 } 3355 3356 static int chcr_ccm_setauthsize(struct crypto_aead *tfm, 3357 unsigned int authsize) 3358 { 3359 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm)); 3360 3361 switch (authsize) { 3362 case ICV_4: 3363 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1; 3364 aeadctx->mayverify = VERIFY_HW; 3365 break; 3366 case ICV_6: 3367 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2; 3368 aeadctx->mayverify = VERIFY_HW; 3369 break; 3370 case ICV_8: 3371 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2; 3372 aeadctx->mayverify = VERIFY_HW; 3373 break; 3374 case ICV_10: 3375 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366; 3376 aeadctx->mayverify = VERIFY_HW; 3377 break; 3378 case ICV_12: 3379 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT; 3380 aeadctx->mayverify = VERIFY_HW; 3381 break; 3382 case ICV_14: 3383 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3; 3384 aeadctx->mayverify = VERIFY_HW; 3385 break; 3386 case ICV_16: 3387 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC; 3388 aeadctx->mayverify = VERIFY_HW; 3389 break; 3390 default: 3391 return -EINVAL; 3392 } 3393 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize); 3394 } 3395 3396 static int chcr_ccm_common_setkey(struct crypto_aead *aead, 3397 const u8 *key, 3398 unsigned int keylen) 3399 { 3400 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead)); 3401 unsigned char ck_size, mk_size; 3402 int key_ctx_size = 0; 3403 3404 key_ctx_size = sizeof(struct _key_ctx) + roundup(keylen, 16) * 2; 3405 if (keylen == AES_KEYSIZE_128) { 3406 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128; 3407 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_128; 3408 } else if (keylen == AES_KEYSIZE_192) { 3409 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192; 3410 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_192; 3411 } else if (keylen == AES_KEYSIZE_256) { 3412 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256; 3413 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256; 3414 } else { 3415 aeadctx->enckey_len = 0; 3416 return -EINVAL; 3417 } 3418 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, mk_size, 0, 0, 3419 key_ctx_size >> 4); 3420 memcpy(aeadctx->key, key, keylen); 3421 aeadctx->enckey_len = keylen; 3422 3423 return 0; 3424 } 3425 3426 static int chcr_aead_ccm_setkey(struct crypto_aead *aead, 3427 const u8 *key, 3428 unsigned int keylen) 3429 { 3430 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead)); 3431 int error; 3432 3433 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK); 3434 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) & 3435 CRYPTO_TFM_REQ_MASK); 3436 error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen); 3437 if (error) 3438 return error; 3439 return chcr_ccm_common_setkey(aead, key, keylen); 3440 } 3441 3442 static int chcr_aead_rfc4309_setkey(struct crypto_aead *aead, const u8 *key, 3443 unsigned int keylen) 3444 { 3445 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead)); 3446 int error; 3447 3448 if (keylen < 3) { 3449 aeadctx->enckey_len = 0; 3450 return -EINVAL; 3451 } 3452 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK); 3453 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) & 3454 CRYPTO_TFM_REQ_MASK); 3455 error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen); 3456 if (error) 3457 return error; 3458 keylen -= 3; 3459 memcpy(aeadctx->salt, key + keylen, 3); 3460 return chcr_ccm_common_setkey(aead, key, keylen); 3461 } 3462 3463 static int chcr_gcm_setkey(struct crypto_aead *aead, const u8 *key, 3464 unsigned int keylen) 3465 { 3466 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead)); 3467 struct chcr_gcm_ctx *gctx = GCM_CTX(aeadctx); 3468 unsigned int ck_size; 3469 int ret = 0, key_ctx_size = 0; 3470 struct crypto_aes_ctx aes; 3471 3472 aeadctx->enckey_len = 0; 3473 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK); 3474 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) 3475 & CRYPTO_TFM_REQ_MASK); 3476 ret = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen); 3477 if (ret) 3478 goto out; 3479 3480 if (get_aead_subtype(aead) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 && 3481 keylen > 3) { 3482 keylen -= 4; /* nonce/salt is present in the last 4 bytes */ 3483 memcpy(aeadctx->salt, key + keylen, 4); 3484 } 3485 if (keylen == AES_KEYSIZE_128) { 3486 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128; 3487 } else if (keylen == AES_KEYSIZE_192) { 3488 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192; 3489 } else if (keylen == AES_KEYSIZE_256) { 3490 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256; 3491 } else { 3492 pr_err("GCM: Invalid key length %d\n", keylen); 3493 ret = -EINVAL; 3494 goto out; 3495 } 3496 3497 memcpy(aeadctx->key, key, keylen); 3498 aeadctx->enckey_len = keylen; 3499 key_ctx_size = sizeof(struct _key_ctx) + roundup(keylen, 16) + 3500 AEAD_H_SIZE; 3501 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, 3502 CHCR_KEYCTX_MAC_KEY_SIZE_128, 3503 0, 0, 3504 key_ctx_size >> 4); 3505 /* Calculate the H = CIPH(K, 0 repeated 16 times). 3506 * It will go in key context 3507 */ 3508 ret = aes_expandkey(&aes, key, keylen); 3509 if (ret) { 3510 aeadctx->enckey_len = 0; 3511 goto out; 3512 } 3513 memset(gctx->ghash_h, 0, AEAD_H_SIZE); 3514 aes_encrypt(&aes, gctx->ghash_h, gctx->ghash_h); 3515 memzero_explicit(&aes, sizeof(aes)); 3516 3517 out: 3518 return ret; 3519 } 3520 3521 static int chcr_authenc_setkey(struct crypto_aead *authenc, const u8 *key, 3522 unsigned int keylen) 3523 { 3524 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(authenc)); 3525 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx); 3526 /* it contains auth and cipher key both*/ 3527 struct crypto_authenc_keys keys; 3528 unsigned int bs, subtype; 3529 unsigned int max_authsize = crypto_aead_alg(authenc)->maxauthsize; 3530 int err = 0, i, key_ctx_len = 0; 3531 unsigned char ck_size = 0; 3532 unsigned char pad[CHCR_HASH_MAX_BLOCK_SIZE_128] = { 0 }; 3533 struct crypto_shash *base_hash = ERR_PTR(-EINVAL); 3534 struct algo_param param; 3535 int align; 3536 u8 *o_ptr = NULL; 3537 3538 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK); 3539 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc) 3540 & CRYPTO_TFM_REQ_MASK); 3541 err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen); 3542 if (err) 3543 goto out; 3544 3545 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) 3546 goto out; 3547 3548 if (get_alg_config(¶m, max_authsize)) { 3549 pr_err("chcr : Unsupported digest size\n"); 3550 goto out; 3551 } 3552 subtype = get_aead_subtype(authenc); 3553 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA || 3554 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) { 3555 if (keys.enckeylen < CTR_RFC3686_NONCE_SIZE) 3556 goto out; 3557 memcpy(aeadctx->nonce, keys.enckey + (keys.enckeylen 3558 - CTR_RFC3686_NONCE_SIZE), CTR_RFC3686_NONCE_SIZE); 3559 keys.enckeylen -= CTR_RFC3686_NONCE_SIZE; 3560 } 3561 if (keys.enckeylen == AES_KEYSIZE_128) { 3562 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128; 3563 } else if (keys.enckeylen == AES_KEYSIZE_192) { 3564 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192; 3565 } else if (keys.enckeylen == AES_KEYSIZE_256) { 3566 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256; 3567 } else { 3568 pr_err("chcr : Unsupported cipher key\n"); 3569 goto out; 3570 } 3571 3572 /* Copy only encryption key. We use authkey to generate h(ipad) and 3573 * h(opad) so authkey is not needed again. authkeylen size have the 3574 * size of the hash digest size. 3575 */ 3576 memcpy(aeadctx->key, keys.enckey, keys.enckeylen); 3577 aeadctx->enckey_len = keys.enckeylen; 3578 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_SHA || 3579 subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL) { 3580 3581 get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key, 3582 aeadctx->enckey_len << 3); 3583 } 3584 base_hash = chcr_alloc_shash(max_authsize); 3585 if (IS_ERR(base_hash)) { 3586 pr_err("chcr : Base driver cannot be loaded\n"); 3587 aeadctx->enckey_len = 0; 3588 memzero_explicit(&keys, sizeof(keys)); 3589 return -EINVAL; 3590 } 3591 { 3592 SHASH_DESC_ON_STACK(shash, base_hash); 3593 3594 shash->tfm = base_hash; 3595 bs = crypto_shash_blocksize(base_hash); 3596 align = KEYCTX_ALIGN_PAD(max_authsize); 3597 o_ptr = actx->h_iopad + param.result_size + align; 3598 3599 if (keys.authkeylen > bs) { 3600 err = crypto_shash_digest(shash, keys.authkey, 3601 keys.authkeylen, 3602 o_ptr); 3603 if (err) { 3604 pr_err("chcr : Base driver cannot be loaded\n"); 3605 goto out; 3606 } 3607 keys.authkeylen = max_authsize; 3608 } else 3609 memcpy(o_ptr, keys.authkey, keys.authkeylen); 3610 3611 /* Compute the ipad-digest*/ 3612 memset(pad + keys.authkeylen, 0, bs - keys.authkeylen); 3613 memcpy(pad, o_ptr, keys.authkeylen); 3614 for (i = 0; i < bs >> 2; i++) 3615 *((unsigned int *)pad + i) ^= IPAD_DATA; 3616 3617 if (chcr_compute_partial_hash(shash, pad, actx->h_iopad, 3618 max_authsize)) 3619 goto out; 3620 /* Compute the opad-digest */ 3621 memset(pad + keys.authkeylen, 0, bs - keys.authkeylen); 3622 memcpy(pad, o_ptr, keys.authkeylen); 3623 for (i = 0; i < bs >> 2; i++) 3624 *((unsigned int *)pad + i) ^= OPAD_DATA; 3625 3626 if (chcr_compute_partial_hash(shash, pad, o_ptr, max_authsize)) 3627 goto out; 3628 3629 /* convert the ipad and opad digest to network order */ 3630 chcr_change_order(actx->h_iopad, param.result_size); 3631 chcr_change_order(o_ptr, param.result_size); 3632 key_ctx_len = sizeof(struct _key_ctx) + 3633 roundup(keys.enckeylen, 16) + 3634 (param.result_size + align) * 2; 3635 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, param.mk_size, 3636 0, 1, key_ctx_len >> 4); 3637 actx->auth_mode = param.auth_mode; 3638 chcr_free_shash(base_hash); 3639 3640 memzero_explicit(&keys, sizeof(keys)); 3641 return 0; 3642 } 3643 out: 3644 aeadctx->enckey_len = 0; 3645 memzero_explicit(&keys, sizeof(keys)); 3646 if (!IS_ERR(base_hash)) 3647 chcr_free_shash(base_hash); 3648 return -EINVAL; 3649 } 3650 3651 static int chcr_aead_digest_null_setkey(struct crypto_aead *authenc, 3652 const u8 *key, unsigned int keylen) 3653 { 3654 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(authenc)); 3655 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx); 3656 struct crypto_authenc_keys keys; 3657 int err; 3658 /* it contains auth and cipher key both*/ 3659 unsigned int subtype; 3660 int key_ctx_len = 0; 3661 unsigned char ck_size = 0; 3662 3663 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK); 3664 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc) 3665 & CRYPTO_TFM_REQ_MASK); 3666 err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen); 3667 if (err) 3668 goto out; 3669 3670 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) 3671 goto out; 3672 3673 subtype = get_aead_subtype(authenc); 3674 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA || 3675 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) { 3676 if (keys.enckeylen < CTR_RFC3686_NONCE_SIZE) 3677 goto out; 3678 memcpy(aeadctx->nonce, keys.enckey + (keys.enckeylen 3679 - CTR_RFC3686_NONCE_SIZE), CTR_RFC3686_NONCE_SIZE); 3680 keys.enckeylen -= CTR_RFC3686_NONCE_SIZE; 3681 } 3682 if (keys.enckeylen == AES_KEYSIZE_128) { 3683 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128; 3684 } else if (keys.enckeylen == AES_KEYSIZE_192) { 3685 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192; 3686 } else if (keys.enckeylen == AES_KEYSIZE_256) { 3687 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256; 3688 } else { 3689 pr_err("chcr : Unsupported cipher key %d\n", keys.enckeylen); 3690 goto out; 3691 } 3692 memcpy(aeadctx->key, keys.enckey, keys.enckeylen); 3693 aeadctx->enckey_len = keys.enckeylen; 3694 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_SHA || 3695 subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL) { 3696 get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key, 3697 aeadctx->enckey_len << 3); 3698 } 3699 key_ctx_len = sizeof(struct _key_ctx) + roundup(keys.enckeylen, 16); 3700 3701 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY, 0, 3702 0, key_ctx_len >> 4); 3703 actx->auth_mode = CHCR_SCMD_AUTH_MODE_NOP; 3704 memzero_explicit(&keys, sizeof(keys)); 3705 return 0; 3706 out: 3707 aeadctx->enckey_len = 0; 3708 memzero_explicit(&keys, sizeof(keys)); 3709 return -EINVAL; 3710 } 3711 3712 static int chcr_aead_op(struct aead_request *req, 3713 int size, 3714 create_wr_t create_wr_fn) 3715 { 3716 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 3717 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 3718 struct chcr_context *ctx = a_ctx(tfm); 3719 struct uld_ctx *u_ctx = ULD_CTX(ctx); 3720 struct sk_buff *skb; 3721 struct chcr_dev *cdev; 3722 3723 cdev = a_ctx(tfm)->dev; 3724 if (!cdev) { 3725 pr_err("chcr : %s : No crypto device.\n", __func__); 3726 return -ENXIO; 3727 } 3728 3729 if (chcr_inc_wrcount(cdev)) { 3730 /* Detach state for CHCR means lldi or padap is freed. 3731 * We cannot increment fallback here. 3732 */ 3733 return chcr_aead_fallback(req, reqctx->op); 3734 } 3735 3736 if (cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0], 3737 reqctx->txqidx) && 3738 (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))) { 3739 chcr_dec_wrcount(cdev); 3740 return -ENOSPC; 3741 } 3742 3743 if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 && 3744 crypto_ipsec_check_assoclen(req->assoclen) != 0) { 3745 pr_err("RFC4106: Invalid value of assoclen %d\n", 3746 req->assoclen); 3747 return -EINVAL; 3748 } 3749 3750 /* Form a WR from req */ 3751 skb = create_wr_fn(req, u_ctx->lldi.rxq_ids[reqctx->rxqidx], size); 3752 3753 if (IS_ERR_OR_NULL(skb)) { 3754 chcr_dec_wrcount(cdev); 3755 return PTR_ERR_OR_ZERO(skb); 3756 } 3757 3758 skb->dev = u_ctx->lldi.ports[0]; 3759 set_wr_txq(skb, CPL_PRIORITY_DATA, reqctx->txqidx); 3760 chcr_send_wr(skb); 3761 return -EINPROGRESS; 3762 } 3763 3764 static int chcr_aead_encrypt(struct aead_request *req) 3765 { 3766 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 3767 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 3768 struct chcr_context *ctx = a_ctx(tfm); 3769 unsigned int cpu; 3770 3771 cpu = get_cpu(); 3772 reqctx->txqidx = cpu % ctx->ntxq; 3773 reqctx->rxqidx = cpu % ctx->nrxq; 3774 put_cpu(); 3775 3776 reqctx->verify = VERIFY_HW; 3777 reqctx->op = CHCR_ENCRYPT_OP; 3778 3779 switch (get_aead_subtype(tfm)) { 3780 case CRYPTO_ALG_SUB_TYPE_CTR_SHA: 3781 case CRYPTO_ALG_SUB_TYPE_CBC_SHA: 3782 case CRYPTO_ALG_SUB_TYPE_CBC_NULL: 3783 case CRYPTO_ALG_SUB_TYPE_CTR_NULL: 3784 return chcr_aead_op(req, 0, create_authenc_wr); 3785 case CRYPTO_ALG_SUB_TYPE_AEAD_CCM: 3786 case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309: 3787 return chcr_aead_op(req, 0, create_aead_ccm_wr); 3788 default: 3789 return chcr_aead_op(req, 0, create_gcm_wr); 3790 } 3791 } 3792 3793 static int chcr_aead_decrypt(struct aead_request *req) 3794 { 3795 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 3796 struct chcr_context *ctx = a_ctx(tfm); 3797 struct chcr_aead_ctx *aeadctx = AEAD_CTX(ctx); 3798 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req); 3799 int size; 3800 unsigned int cpu; 3801 3802 cpu = get_cpu(); 3803 reqctx->txqidx = cpu % ctx->ntxq; 3804 reqctx->rxqidx = cpu % ctx->nrxq; 3805 put_cpu(); 3806 3807 if (aeadctx->mayverify == VERIFY_SW) { 3808 size = crypto_aead_maxauthsize(tfm); 3809 reqctx->verify = VERIFY_SW; 3810 } else { 3811 size = 0; 3812 reqctx->verify = VERIFY_HW; 3813 } 3814 reqctx->op = CHCR_DECRYPT_OP; 3815 switch (get_aead_subtype(tfm)) { 3816 case CRYPTO_ALG_SUB_TYPE_CBC_SHA: 3817 case CRYPTO_ALG_SUB_TYPE_CTR_SHA: 3818 case CRYPTO_ALG_SUB_TYPE_CBC_NULL: 3819 case CRYPTO_ALG_SUB_TYPE_CTR_NULL: 3820 return chcr_aead_op(req, size, create_authenc_wr); 3821 case CRYPTO_ALG_SUB_TYPE_AEAD_CCM: 3822 case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309: 3823 return chcr_aead_op(req, size, create_aead_ccm_wr); 3824 default: 3825 return chcr_aead_op(req, size, create_gcm_wr); 3826 } 3827 } 3828 3829 static struct chcr_alg_template driver_algs[] = { 3830 /* AES-CBC */ 3831 { 3832 .type = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_SUB_TYPE_CBC, 3833 .is_registered = 0, 3834 .alg.skcipher = { 3835 .base.cra_name = "cbc(aes)", 3836 .base.cra_driver_name = "cbc-aes-chcr", 3837 .base.cra_blocksize = AES_BLOCK_SIZE, 3838 3839 .init = chcr_init_tfm, 3840 .exit = chcr_exit_tfm, 3841 .min_keysize = AES_MIN_KEY_SIZE, 3842 .max_keysize = AES_MAX_KEY_SIZE, 3843 .ivsize = AES_BLOCK_SIZE, 3844 .setkey = chcr_aes_cbc_setkey, 3845 .encrypt = chcr_aes_encrypt, 3846 .decrypt = chcr_aes_decrypt, 3847 } 3848 }, 3849 { 3850 .type = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_SUB_TYPE_XTS, 3851 .is_registered = 0, 3852 .alg.skcipher = { 3853 .base.cra_name = "xts(aes)", 3854 .base.cra_driver_name = "xts-aes-chcr", 3855 .base.cra_blocksize = AES_BLOCK_SIZE, 3856 3857 .init = chcr_init_tfm, 3858 .exit = chcr_exit_tfm, 3859 .min_keysize = 2 * AES_MIN_KEY_SIZE, 3860 .max_keysize = 2 * AES_MAX_KEY_SIZE, 3861 .ivsize = AES_BLOCK_SIZE, 3862 .setkey = chcr_aes_xts_setkey, 3863 .encrypt = chcr_aes_encrypt, 3864 .decrypt = chcr_aes_decrypt, 3865 } 3866 }, 3867 { 3868 .type = CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_SUB_TYPE_CTR, 3869 .is_registered = 0, 3870 .alg.skcipher = { 3871 .base.cra_name = "ctr(aes)", 3872 .base.cra_driver_name = "ctr-aes-chcr", 3873 .base.cra_blocksize = 1, 3874 3875 .init = chcr_init_tfm, 3876 .exit = chcr_exit_tfm, 3877 .min_keysize = AES_MIN_KEY_SIZE, 3878 .max_keysize = AES_MAX_KEY_SIZE, 3879 .ivsize = AES_BLOCK_SIZE, 3880 .setkey = chcr_aes_ctr_setkey, 3881 .encrypt = chcr_aes_encrypt, 3882 .decrypt = chcr_aes_decrypt, 3883 } 3884 }, 3885 { 3886 .type = CRYPTO_ALG_TYPE_SKCIPHER | 3887 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686, 3888 .is_registered = 0, 3889 .alg.skcipher = { 3890 .base.cra_name = "rfc3686(ctr(aes))", 3891 .base.cra_driver_name = "rfc3686-ctr-aes-chcr", 3892 .base.cra_blocksize = 1, 3893 3894 .init = chcr_rfc3686_init, 3895 .exit = chcr_exit_tfm, 3896 .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE, 3897 .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE, 3898 .ivsize = CTR_RFC3686_IV_SIZE, 3899 .setkey = chcr_aes_rfc3686_setkey, 3900 .encrypt = chcr_aes_encrypt, 3901 .decrypt = chcr_aes_decrypt, 3902 } 3903 }, 3904 /* SHA */ 3905 { 3906 .type = CRYPTO_ALG_TYPE_AHASH, 3907 .is_registered = 0, 3908 .alg.hash = { 3909 .halg.digestsize = SHA1_DIGEST_SIZE, 3910 .halg.base = { 3911 .cra_name = "sha1", 3912 .cra_driver_name = "sha1-chcr", 3913 .cra_blocksize = SHA1_BLOCK_SIZE, 3914 } 3915 } 3916 }, 3917 { 3918 .type = CRYPTO_ALG_TYPE_AHASH, 3919 .is_registered = 0, 3920 .alg.hash = { 3921 .halg.digestsize = SHA256_DIGEST_SIZE, 3922 .halg.base = { 3923 .cra_name = "sha256", 3924 .cra_driver_name = "sha256-chcr", 3925 .cra_blocksize = SHA256_BLOCK_SIZE, 3926 } 3927 } 3928 }, 3929 { 3930 .type = CRYPTO_ALG_TYPE_AHASH, 3931 .is_registered = 0, 3932 .alg.hash = { 3933 .halg.digestsize = SHA224_DIGEST_SIZE, 3934 .halg.base = { 3935 .cra_name = "sha224", 3936 .cra_driver_name = "sha224-chcr", 3937 .cra_blocksize = SHA224_BLOCK_SIZE, 3938 } 3939 } 3940 }, 3941 { 3942 .type = CRYPTO_ALG_TYPE_AHASH, 3943 .is_registered = 0, 3944 .alg.hash = { 3945 .halg.digestsize = SHA384_DIGEST_SIZE, 3946 .halg.base = { 3947 .cra_name = "sha384", 3948 .cra_driver_name = "sha384-chcr", 3949 .cra_blocksize = SHA384_BLOCK_SIZE, 3950 } 3951 } 3952 }, 3953 { 3954 .type = CRYPTO_ALG_TYPE_AHASH, 3955 .is_registered = 0, 3956 .alg.hash = { 3957 .halg.digestsize = SHA512_DIGEST_SIZE, 3958 .halg.base = { 3959 .cra_name = "sha512", 3960 .cra_driver_name = "sha512-chcr", 3961 .cra_blocksize = SHA512_BLOCK_SIZE, 3962 } 3963 } 3964 }, 3965 /* HMAC */ 3966 { 3967 .type = CRYPTO_ALG_TYPE_HMAC, 3968 .is_registered = 0, 3969 .alg.hash = { 3970 .halg.digestsize = SHA1_DIGEST_SIZE, 3971 .halg.base = { 3972 .cra_name = "hmac(sha1)", 3973 .cra_driver_name = "hmac-sha1-chcr", 3974 .cra_blocksize = SHA1_BLOCK_SIZE, 3975 } 3976 } 3977 }, 3978 { 3979 .type = CRYPTO_ALG_TYPE_HMAC, 3980 .is_registered = 0, 3981 .alg.hash = { 3982 .halg.digestsize = SHA224_DIGEST_SIZE, 3983 .halg.base = { 3984 .cra_name = "hmac(sha224)", 3985 .cra_driver_name = "hmac-sha224-chcr", 3986 .cra_blocksize = SHA224_BLOCK_SIZE, 3987 } 3988 } 3989 }, 3990 { 3991 .type = CRYPTO_ALG_TYPE_HMAC, 3992 .is_registered = 0, 3993 .alg.hash = { 3994 .halg.digestsize = SHA256_DIGEST_SIZE, 3995 .halg.base = { 3996 .cra_name = "hmac(sha256)", 3997 .cra_driver_name = "hmac-sha256-chcr", 3998 .cra_blocksize = SHA256_BLOCK_SIZE, 3999 } 4000 } 4001 }, 4002 { 4003 .type = CRYPTO_ALG_TYPE_HMAC, 4004 .is_registered = 0, 4005 .alg.hash = { 4006 .halg.digestsize = SHA384_DIGEST_SIZE, 4007 .halg.base = { 4008 .cra_name = "hmac(sha384)", 4009 .cra_driver_name = "hmac-sha384-chcr", 4010 .cra_blocksize = SHA384_BLOCK_SIZE, 4011 } 4012 } 4013 }, 4014 { 4015 .type = CRYPTO_ALG_TYPE_HMAC, 4016 .is_registered = 0, 4017 .alg.hash = { 4018 .halg.digestsize = SHA512_DIGEST_SIZE, 4019 .halg.base = { 4020 .cra_name = "hmac(sha512)", 4021 .cra_driver_name = "hmac-sha512-chcr", 4022 .cra_blocksize = SHA512_BLOCK_SIZE, 4023 } 4024 } 4025 }, 4026 /* Add AEAD Algorithms */ 4027 { 4028 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_GCM, 4029 .is_registered = 0, 4030 .alg.aead = { 4031 .base = { 4032 .cra_name = "gcm(aes)", 4033 .cra_driver_name = "gcm-aes-chcr", 4034 .cra_blocksize = 1, 4035 .cra_priority = CHCR_AEAD_PRIORITY, 4036 .cra_ctxsize = sizeof(struct chcr_context) + 4037 sizeof(struct chcr_aead_ctx) + 4038 sizeof(struct chcr_gcm_ctx), 4039 }, 4040 .ivsize = GCM_AES_IV_SIZE, 4041 .maxauthsize = GHASH_DIGEST_SIZE, 4042 .setkey = chcr_gcm_setkey, 4043 .setauthsize = chcr_gcm_setauthsize, 4044 } 4045 }, 4046 { 4047 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106, 4048 .is_registered = 0, 4049 .alg.aead = { 4050 .base = { 4051 .cra_name = "rfc4106(gcm(aes))", 4052 .cra_driver_name = "rfc4106-gcm-aes-chcr", 4053 .cra_blocksize = 1, 4054 .cra_priority = CHCR_AEAD_PRIORITY + 1, 4055 .cra_ctxsize = sizeof(struct chcr_context) + 4056 sizeof(struct chcr_aead_ctx) + 4057 sizeof(struct chcr_gcm_ctx), 4058 4059 }, 4060 .ivsize = GCM_RFC4106_IV_SIZE, 4061 .maxauthsize = GHASH_DIGEST_SIZE, 4062 .setkey = chcr_gcm_setkey, 4063 .setauthsize = chcr_4106_4309_setauthsize, 4064 } 4065 }, 4066 { 4067 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_CCM, 4068 .is_registered = 0, 4069 .alg.aead = { 4070 .base = { 4071 .cra_name = "ccm(aes)", 4072 .cra_driver_name = "ccm-aes-chcr", 4073 .cra_blocksize = 1, 4074 .cra_priority = CHCR_AEAD_PRIORITY, 4075 .cra_ctxsize = sizeof(struct chcr_context) + 4076 sizeof(struct chcr_aead_ctx), 4077 4078 }, 4079 .ivsize = AES_BLOCK_SIZE, 4080 .maxauthsize = GHASH_DIGEST_SIZE, 4081 .setkey = chcr_aead_ccm_setkey, 4082 .setauthsize = chcr_ccm_setauthsize, 4083 } 4084 }, 4085 { 4086 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309, 4087 .is_registered = 0, 4088 .alg.aead = { 4089 .base = { 4090 .cra_name = "rfc4309(ccm(aes))", 4091 .cra_driver_name = "rfc4309-ccm-aes-chcr", 4092 .cra_blocksize = 1, 4093 .cra_priority = CHCR_AEAD_PRIORITY + 1, 4094 .cra_ctxsize = sizeof(struct chcr_context) + 4095 sizeof(struct chcr_aead_ctx), 4096 4097 }, 4098 .ivsize = 8, 4099 .maxauthsize = GHASH_DIGEST_SIZE, 4100 .setkey = chcr_aead_rfc4309_setkey, 4101 .setauthsize = chcr_4106_4309_setauthsize, 4102 } 4103 }, 4104 { 4105 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA, 4106 .is_registered = 0, 4107 .alg.aead = { 4108 .base = { 4109 .cra_name = "authenc(hmac(sha1),cbc(aes))", 4110 .cra_driver_name = 4111 "authenc-hmac-sha1-cbc-aes-chcr", 4112 .cra_blocksize = AES_BLOCK_SIZE, 4113 .cra_priority = CHCR_AEAD_PRIORITY, 4114 .cra_ctxsize = sizeof(struct chcr_context) + 4115 sizeof(struct chcr_aead_ctx) + 4116 sizeof(struct chcr_authenc_ctx), 4117 4118 }, 4119 .ivsize = AES_BLOCK_SIZE, 4120 .maxauthsize = SHA1_DIGEST_SIZE, 4121 .setkey = chcr_authenc_setkey, 4122 .setauthsize = chcr_authenc_setauthsize, 4123 } 4124 }, 4125 { 4126 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA, 4127 .is_registered = 0, 4128 .alg.aead = { 4129 .base = { 4130 4131 .cra_name = "authenc(hmac(sha256),cbc(aes))", 4132 .cra_driver_name = 4133 "authenc-hmac-sha256-cbc-aes-chcr", 4134 .cra_blocksize = AES_BLOCK_SIZE, 4135 .cra_priority = CHCR_AEAD_PRIORITY, 4136 .cra_ctxsize = sizeof(struct chcr_context) + 4137 sizeof(struct chcr_aead_ctx) + 4138 sizeof(struct chcr_authenc_ctx), 4139 4140 }, 4141 .ivsize = AES_BLOCK_SIZE, 4142 .maxauthsize = SHA256_DIGEST_SIZE, 4143 .setkey = chcr_authenc_setkey, 4144 .setauthsize = chcr_authenc_setauthsize, 4145 } 4146 }, 4147 { 4148 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA, 4149 .is_registered = 0, 4150 .alg.aead = { 4151 .base = { 4152 .cra_name = "authenc(hmac(sha224),cbc(aes))", 4153 .cra_driver_name = 4154 "authenc-hmac-sha224-cbc-aes-chcr", 4155 .cra_blocksize = AES_BLOCK_SIZE, 4156 .cra_priority = CHCR_AEAD_PRIORITY, 4157 .cra_ctxsize = sizeof(struct chcr_context) + 4158 sizeof(struct chcr_aead_ctx) + 4159 sizeof(struct chcr_authenc_ctx), 4160 }, 4161 .ivsize = AES_BLOCK_SIZE, 4162 .maxauthsize = SHA224_DIGEST_SIZE, 4163 .setkey = chcr_authenc_setkey, 4164 .setauthsize = chcr_authenc_setauthsize, 4165 } 4166 }, 4167 { 4168 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA, 4169 .is_registered = 0, 4170 .alg.aead = { 4171 .base = { 4172 .cra_name = "authenc(hmac(sha384),cbc(aes))", 4173 .cra_driver_name = 4174 "authenc-hmac-sha384-cbc-aes-chcr", 4175 .cra_blocksize = AES_BLOCK_SIZE, 4176 .cra_priority = CHCR_AEAD_PRIORITY, 4177 .cra_ctxsize = sizeof(struct chcr_context) + 4178 sizeof(struct chcr_aead_ctx) + 4179 sizeof(struct chcr_authenc_ctx), 4180 4181 }, 4182 .ivsize = AES_BLOCK_SIZE, 4183 .maxauthsize = SHA384_DIGEST_SIZE, 4184 .setkey = chcr_authenc_setkey, 4185 .setauthsize = chcr_authenc_setauthsize, 4186 } 4187 }, 4188 { 4189 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA, 4190 .is_registered = 0, 4191 .alg.aead = { 4192 .base = { 4193 .cra_name = "authenc(hmac(sha512),cbc(aes))", 4194 .cra_driver_name = 4195 "authenc-hmac-sha512-cbc-aes-chcr", 4196 .cra_blocksize = AES_BLOCK_SIZE, 4197 .cra_priority = CHCR_AEAD_PRIORITY, 4198 .cra_ctxsize = sizeof(struct chcr_context) + 4199 sizeof(struct chcr_aead_ctx) + 4200 sizeof(struct chcr_authenc_ctx), 4201 4202 }, 4203 .ivsize = AES_BLOCK_SIZE, 4204 .maxauthsize = SHA512_DIGEST_SIZE, 4205 .setkey = chcr_authenc_setkey, 4206 .setauthsize = chcr_authenc_setauthsize, 4207 } 4208 }, 4209 { 4210 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_NULL, 4211 .is_registered = 0, 4212 .alg.aead = { 4213 .base = { 4214 .cra_name = "authenc(digest_null,cbc(aes))", 4215 .cra_driver_name = 4216 "authenc-digest_null-cbc-aes-chcr", 4217 .cra_blocksize = AES_BLOCK_SIZE, 4218 .cra_priority = CHCR_AEAD_PRIORITY, 4219 .cra_ctxsize = sizeof(struct chcr_context) + 4220 sizeof(struct chcr_aead_ctx) + 4221 sizeof(struct chcr_authenc_ctx), 4222 4223 }, 4224 .ivsize = AES_BLOCK_SIZE, 4225 .maxauthsize = 0, 4226 .setkey = chcr_aead_digest_null_setkey, 4227 .setauthsize = chcr_authenc_null_setauthsize, 4228 } 4229 }, 4230 { 4231 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA, 4232 .is_registered = 0, 4233 .alg.aead = { 4234 .base = { 4235 .cra_name = "authenc(hmac(sha1),rfc3686(ctr(aes)))", 4236 .cra_driver_name = 4237 "authenc-hmac-sha1-rfc3686-ctr-aes-chcr", 4238 .cra_blocksize = 1, 4239 .cra_priority = CHCR_AEAD_PRIORITY, 4240 .cra_ctxsize = sizeof(struct chcr_context) + 4241 sizeof(struct chcr_aead_ctx) + 4242 sizeof(struct chcr_authenc_ctx), 4243 4244 }, 4245 .ivsize = CTR_RFC3686_IV_SIZE, 4246 .maxauthsize = SHA1_DIGEST_SIZE, 4247 .setkey = chcr_authenc_setkey, 4248 .setauthsize = chcr_authenc_setauthsize, 4249 } 4250 }, 4251 { 4252 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA, 4253 .is_registered = 0, 4254 .alg.aead = { 4255 .base = { 4256 4257 .cra_name = "authenc(hmac(sha256),rfc3686(ctr(aes)))", 4258 .cra_driver_name = 4259 "authenc-hmac-sha256-rfc3686-ctr-aes-chcr", 4260 .cra_blocksize = 1, 4261 .cra_priority = CHCR_AEAD_PRIORITY, 4262 .cra_ctxsize = sizeof(struct chcr_context) + 4263 sizeof(struct chcr_aead_ctx) + 4264 sizeof(struct chcr_authenc_ctx), 4265 4266 }, 4267 .ivsize = CTR_RFC3686_IV_SIZE, 4268 .maxauthsize = SHA256_DIGEST_SIZE, 4269 .setkey = chcr_authenc_setkey, 4270 .setauthsize = chcr_authenc_setauthsize, 4271 } 4272 }, 4273 { 4274 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA, 4275 .is_registered = 0, 4276 .alg.aead = { 4277 .base = { 4278 .cra_name = "authenc(hmac(sha224),rfc3686(ctr(aes)))", 4279 .cra_driver_name = 4280 "authenc-hmac-sha224-rfc3686-ctr-aes-chcr", 4281 .cra_blocksize = 1, 4282 .cra_priority = CHCR_AEAD_PRIORITY, 4283 .cra_ctxsize = sizeof(struct chcr_context) + 4284 sizeof(struct chcr_aead_ctx) + 4285 sizeof(struct chcr_authenc_ctx), 4286 }, 4287 .ivsize = CTR_RFC3686_IV_SIZE, 4288 .maxauthsize = SHA224_DIGEST_SIZE, 4289 .setkey = chcr_authenc_setkey, 4290 .setauthsize = chcr_authenc_setauthsize, 4291 } 4292 }, 4293 { 4294 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA, 4295 .is_registered = 0, 4296 .alg.aead = { 4297 .base = { 4298 .cra_name = "authenc(hmac(sha384),rfc3686(ctr(aes)))", 4299 .cra_driver_name = 4300 "authenc-hmac-sha384-rfc3686-ctr-aes-chcr", 4301 .cra_blocksize = 1, 4302 .cra_priority = CHCR_AEAD_PRIORITY, 4303 .cra_ctxsize = sizeof(struct chcr_context) + 4304 sizeof(struct chcr_aead_ctx) + 4305 sizeof(struct chcr_authenc_ctx), 4306 4307 }, 4308 .ivsize = CTR_RFC3686_IV_SIZE, 4309 .maxauthsize = SHA384_DIGEST_SIZE, 4310 .setkey = chcr_authenc_setkey, 4311 .setauthsize = chcr_authenc_setauthsize, 4312 } 4313 }, 4314 { 4315 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA, 4316 .is_registered = 0, 4317 .alg.aead = { 4318 .base = { 4319 .cra_name = "authenc(hmac(sha512),rfc3686(ctr(aes)))", 4320 .cra_driver_name = 4321 "authenc-hmac-sha512-rfc3686-ctr-aes-chcr", 4322 .cra_blocksize = 1, 4323 .cra_priority = CHCR_AEAD_PRIORITY, 4324 .cra_ctxsize = sizeof(struct chcr_context) + 4325 sizeof(struct chcr_aead_ctx) + 4326 sizeof(struct chcr_authenc_ctx), 4327 4328 }, 4329 .ivsize = CTR_RFC3686_IV_SIZE, 4330 .maxauthsize = SHA512_DIGEST_SIZE, 4331 .setkey = chcr_authenc_setkey, 4332 .setauthsize = chcr_authenc_setauthsize, 4333 } 4334 }, 4335 { 4336 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_NULL, 4337 .is_registered = 0, 4338 .alg.aead = { 4339 .base = { 4340 .cra_name = "authenc(digest_null,rfc3686(ctr(aes)))", 4341 .cra_driver_name = 4342 "authenc-digest_null-rfc3686-ctr-aes-chcr", 4343 .cra_blocksize = 1, 4344 .cra_priority = CHCR_AEAD_PRIORITY, 4345 .cra_ctxsize = sizeof(struct chcr_context) + 4346 sizeof(struct chcr_aead_ctx) + 4347 sizeof(struct chcr_authenc_ctx), 4348 4349 }, 4350 .ivsize = CTR_RFC3686_IV_SIZE, 4351 .maxauthsize = 0, 4352 .setkey = chcr_aead_digest_null_setkey, 4353 .setauthsize = chcr_authenc_null_setauthsize, 4354 } 4355 }, 4356 }; 4357 4358 /* 4359 * chcr_unregister_alg - Deregister crypto algorithms with 4360 * kernel framework. 4361 */ 4362 static int chcr_unregister_alg(void) 4363 { 4364 int i; 4365 4366 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) { 4367 switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) { 4368 case CRYPTO_ALG_TYPE_SKCIPHER: 4369 if (driver_algs[i].is_registered) 4370 crypto_unregister_skcipher( 4371 &driver_algs[i].alg.skcipher); 4372 break; 4373 case CRYPTO_ALG_TYPE_AEAD: 4374 if (driver_algs[i].is_registered) 4375 crypto_unregister_aead( 4376 &driver_algs[i].alg.aead); 4377 break; 4378 case CRYPTO_ALG_TYPE_AHASH: 4379 if (driver_algs[i].is_registered) 4380 crypto_unregister_ahash( 4381 &driver_algs[i].alg.hash); 4382 break; 4383 } 4384 driver_algs[i].is_registered = 0; 4385 } 4386 return 0; 4387 } 4388 4389 #define SZ_AHASH_CTX sizeof(struct chcr_context) 4390 #define SZ_AHASH_H_CTX (sizeof(struct chcr_context) + sizeof(struct hmac_ctx)) 4391 #define SZ_AHASH_REQ_CTX sizeof(struct chcr_ahash_req_ctx) 4392 4393 /* 4394 * chcr_register_alg - Register crypto algorithms with kernel framework. 4395 */ 4396 static int chcr_register_alg(void) 4397 { 4398 struct crypto_alg ai; 4399 struct ahash_alg *a_hash; 4400 int err = 0, i; 4401 char *name = NULL; 4402 4403 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) { 4404 if (driver_algs[i].is_registered) 4405 continue; 4406 switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) { 4407 case CRYPTO_ALG_TYPE_SKCIPHER: 4408 driver_algs[i].alg.skcipher.base.cra_priority = 4409 CHCR_CRA_PRIORITY; 4410 driver_algs[i].alg.skcipher.base.cra_module = THIS_MODULE; 4411 driver_algs[i].alg.skcipher.base.cra_flags = 4412 CRYPTO_ALG_TYPE_SKCIPHER | CRYPTO_ALG_ASYNC | 4413 CRYPTO_ALG_NEED_FALLBACK; 4414 driver_algs[i].alg.skcipher.base.cra_ctxsize = 4415 sizeof(struct chcr_context) + 4416 sizeof(struct ablk_ctx); 4417 driver_algs[i].alg.skcipher.base.cra_alignmask = 0; 4418 4419 err = crypto_register_skcipher(&driver_algs[i].alg.skcipher); 4420 name = driver_algs[i].alg.skcipher.base.cra_driver_name; 4421 break; 4422 case CRYPTO_ALG_TYPE_AEAD: 4423 driver_algs[i].alg.aead.base.cra_flags = 4424 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK; 4425 driver_algs[i].alg.aead.encrypt = chcr_aead_encrypt; 4426 driver_algs[i].alg.aead.decrypt = chcr_aead_decrypt; 4427 driver_algs[i].alg.aead.init = chcr_aead_cra_init; 4428 driver_algs[i].alg.aead.exit = chcr_aead_cra_exit; 4429 driver_algs[i].alg.aead.base.cra_module = THIS_MODULE; 4430 err = crypto_register_aead(&driver_algs[i].alg.aead); 4431 name = driver_algs[i].alg.aead.base.cra_driver_name; 4432 break; 4433 case CRYPTO_ALG_TYPE_AHASH: 4434 a_hash = &driver_algs[i].alg.hash; 4435 a_hash->update = chcr_ahash_update; 4436 a_hash->final = chcr_ahash_final; 4437 a_hash->finup = chcr_ahash_finup; 4438 a_hash->digest = chcr_ahash_digest; 4439 a_hash->export = chcr_ahash_export; 4440 a_hash->import = chcr_ahash_import; 4441 a_hash->halg.statesize = SZ_AHASH_REQ_CTX; 4442 a_hash->halg.base.cra_priority = CHCR_CRA_PRIORITY; 4443 a_hash->halg.base.cra_module = THIS_MODULE; 4444 a_hash->halg.base.cra_flags = CRYPTO_ALG_ASYNC; 4445 a_hash->halg.base.cra_alignmask = 0; 4446 a_hash->halg.base.cra_exit = NULL; 4447 4448 if (driver_algs[i].type == CRYPTO_ALG_TYPE_HMAC) { 4449 a_hash->halg.base.cra_init = chcr_hmac_cra_init; 4450 a_hash->halg.base.cra_exit = chcr_hmac_cra_exit; 4451 a_hash->init = chcr_hmac_init; 4452 a_hash->setkey = chcr_ahash_setkey; 4453 a_hash->halg.base.cra_ctxsize = SZ_AHASH_H_CTX; 4454 } else { 4455 a_hash->init = chcr_sha_init; 4456 a_hash->halg.base.cra_ctxsize = SZ_AHASH_CTX; 4457 a_hash->halg.base.cra_init = chcr_sha_cra_init; 4458 } 4459 err = crypto_register_ahash(&driver_algs[i].alg.hash); 4460 ai = driver_algs[i].alg.hash.halg.base; 4461 name = ai.cra_driver_name; 4462 break; 4463 } 4464 if (err) { 4465 pr_err("chcr : %s : Algorithm registration failed\n", 4466 name); 4467 goto register_err; 4468 } else { 4469 driver_algs[i].is_registered = 1; 4470 } 4471 } 4472 return 0; 4473 4474 register_err: 4475 chcr_unregister_alg(); 4476 return err; 4477 } 4478 4479 /* 4480 * start_crypto - Register the crypto algorithms. 4481 * This should called once when the first device comesup. After this 4482 * kernel will start calling driver APIs for crypto operations. 4483 */ 4484 int start_crypto(void) 4485 { 4486 return chcr_register_alg(); 4487 } 4488 4489 /* 4490 * stop_crypto - Deregister all the crypto algorithms with kernel. 4491 * This should be called once when the last device goes down. After this 4492 * kernel will not call the driver API for crypto operations. 4493 */ 4494 int stop_crypto(void) 4495 { 4496 chcr_unregister_alg(); 4497 return 0; 4498 } 4499