1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2020 Marvell. */
4 #include <crypto/aes.h>
5 #include <crypto/authenc.h>
6 #include <crypto/cryptd.h>
7 #include <crypto/des.h>
8 #include <crypto/internal/aead.h>
9 #include <crypto/sha1.h>
10 #include <crypto/sha2.h>
11 #include <crypto/xts.h>
12 #include <crypto/gcm.h>
13 #include <crypto/scatterwalk.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/sort.h>
16 #include <linux/module.h>
17 #include "otx2_cptvf.h"
18 #include "otx2_cptvf_algs.h"
19 #include "otx2_cpt_reqmgr.h"
21 /* Size of salt in AES GCM mode */
22 #define AES_GCM_SALT_SIZE 4
23 /* Size of IV in AES GCM mode */
24 #define AES_GCM_IV_SIZE 8
25 /* Size of ICV (Integrity Check Value) in AES GCM mode */
26 #define AES_GCM_ICV_SIZE 16
27 /* Offset of IV in AES GCM mode */
28 #define AES_GCM_IV_OFFSET 8
29 #define CONTROL_WORD_LEN 8
30 #define KEY2_OFFSET 48
31 #define DMA_MODE_FLAG(dma_mode) \
32 (((dma_mode) == OTX2_CPT_DMA_MODE_SG) ? (1 << 7) : 0)
34 /* Truncated SHA digest size */
35 #define SHA1_TRUNC_DIGEST_SIZE 12
36 #define SHA256_TRUNC_DIGEST_SIZE 16
37 #define SHA384_TRUNC_DIGEST_SIZE 24
38 #define SHA512_TRUNC_DIGEST_SIZE 32
40 static DEFINE_MUTEX(mutex);
41 static int is_crypto_registered;
43 struct cpt_device_desc {
48 struct cpt_device_table {
50 struct cpt_device_desc desc[OTX2_CPT_MAX_LFS_NUM];
53 static struct cpt_device_table se_devices = {
54 .count = ATOMIC_INIT(0)
57 static inline int get_se_device(struct pci_dev **pdev, int *cpu_num)
61 count = atomic_read(&se_devices.count);
67 * On OcteonTX2 platform CPT instruction queue is bound to each
68 * local function LF, in turn LFs can be attached to PF
69 * or VF therefore we always use first device. We get maximum
70 * performance if one CPT queue is available for each cpu
71 * otherwise CPT queues need to be shared between cpus.
73 if (*cpu_num >= se_devices.desc[0].num_queues)
74 *cpu_num %= se_devices.desc[0].num_queues;
75 *pdev = se_devices.desc[0].dev;
82 static inline int validate_hmac_cipher_null(struct otx2_cpt_req_info *cpt_req)
84 struct otx2_cpt_req_ctx *rctx;
85 struct aead_request *req;
86 struct crypto_aead *tfm;
88 req = container_of(cpt_req->areq, struct aead_request, base);
89 tfm = crypto_aead_reqtfm(req);
90 rctx = aead_request_ctx(req);
91 if (memcmp(rctx->fctx.hmac.s.hmac_calc,
92 rctx->fctx.hmac.s.hmac_recv,
93 crypto_aead_authsize(tfm)) != 0)
99 static void otx2_cpt_aead_callback(int status, void *arg1, void *arg2)
101 struct otx2_cpt_inst_info *inst_info = arg2;
102 struct crypto_async_request *areq = arg1;
103 struct otx2_cpt_req_info *cpt_req;
104 struct pci_dev *pdev;
107 cpt_req = inst_info->req;
110 * When selected cipher is NULL we need to manually
111 * verify whether calculated hmac value matches
112 * received hmac value
114 if (cpt_req->req_type ==
115 OTX2_CPT_AEAD_ENC_DEC_NULL_REQ &&
117 status = validate_hmac_cipher_null(cpt_req);
119 pdev = inst_info->pdev;
120 otx2_cpt_info_destroy(pdev, inst_info);
123 areq->complete(areq, status);
126 static void output_iv_copyback(struct crypto_async_request *areq)
128 struct otx2_cpt_req_info *req_info;
129 struct otx2_cpt_req_ctx *rctx;
130 struct skcipher_request *sreq;
131 struct crypto_skcipher *stfm;
132 struct otx2_cpt_enc_ctx *ctx;
135 sreq = container_of(areq, struct skcipher_request, base);
136 stfm = crypto_skcipher_reqtfm(sreq);
137 ctx = crypto_skcipher_ctx(stfm);
138 if (ctx->cipher_type == OTX2_CPT_AES_CBC ||
139 ctx->cipher_type == OTX2_CPT_DES3_CBC) {
140 rctx = skcipher_request_ctx(sreq);
141 req_info = &rctx->cpt_req;
142 ivsize = crypto_skcipher_ivsize(stfm);
143 start = sreq->cryptlen - ivsize;
145 if (req_info->is_enc) {
146 scatterwalk_map_and_copy(sreq->iv, sreq->dst, start,
149 if (sreq->src != sreq->dst) {
150 scatterwalk_map_and_copy(sreq->iv, sreq->src,
153 memcpy(sreq->iv, req_info->iv_out, ivsize);
154 kfree(req_info->iv_out);
160 static void otx2_cpt_skcipher_callback(int status, void *arg1, void *arg2)
162 struct otx2_cpt_inst_info *inst_info = arg2;
163 struct crypto_async_request *areq = arg1;
164 struct pci_dev *pdev;
168 output_iv_copyback(areq);
170 pdev = inst_info->pdev;
171 otx2_cpt_info_destroy(pdev, inst_info);
173 areq->complete(areq, status);
177 static inline void update_input_data(struct otx2_cpt_req_info *req_info,
178 struct scatterlist *inp_sg,
179 u32 nbytes, u32 *argcnt)
181 req_info->req.dlen += nbytes;
184 u32 len = (nbytes < inp_sg->length) ? nbytes : inp_sg->length;
185 u8 *ptr = sg_virt(inp_sg);
187 req_info->in[*argcnt].vptr = (void *)ptr;
188 req_info->in[*argcnt].size = len;
191 inp_sg = sg_next(inp_sg);
195 static inline void update_output_data(struct otx2_cpt_req_info *req_info,
196 struct scatterlist *outp_sg,
197 u32 offset, u32 nbytes, u32 *argcnt)
202 req_info->rlen += nbytes;
205 sg_len = outp_sg->length - offset;
206 len = (nbytes < sg_len) ? nbytes : sg_len;
207 ptr = sg_virt(outp_sg);
209 req_info->out[*argcnt].vptr = (void *) (ptr + offset);
210 req_info->out[*argcnt].size = len;
214 outp_sg = sg_next(outp_sg);
218 static inline int create_ctx_hdr(struct skcipher_request *req, u32 enc,
221 struct crypto_skcipher *stfm = crypto_skcipher_reqtfm(req);
222 struct otx2_cpt_req_ctx *rctx = skcipher_request_ctx(req);
223 struct otx2_cpt_enc_ctx *ctx = crypto_skcipher_ctx(stfm);
224 struct otx2_cpt_req_info *req_info = &rctx->cpt_req;
225 struct otx2_cpt_fc_ctx *fctx = &rctx->fctx;
226 int ivsize = crypto_skcipher_ivsize(stfm);
227 u32 start = req->cryptlen - ivsize;
230 flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
231 GFP_KERNEL : GFP_ATOMIC;
232 req_info->ctrl.s.dma_mode = OTX2_CPT_DMA_MODE_SG;
233 req_info->ctrl.s.se_req = 1;
235 req_info->req.opcode.s.major = OTX2_CPT_MAJOR_OP_FC |
236 DMA_MODE_FLAG(OTX2_CPT_DMA_MODE_SG);
238 req_info->req.opcode.s.minor = 2;
240 req_info->req.opcode.s.minor = 3;
241 if ((ctx->cipher_type == OTX2_CPT_AES_CBC ||
242 ctx->cipher_type == OTX2_CPT_DES3_CBC) &&
243 req->src == req->dst) {
244 req_info->iv_out = kmalloc(ivsize, flags);
245 if (!req_info->iv_out)
248 scatterwalk_map_and_copy(req_info->iv_out, req->src,
252 /* Encryption data length */
253 req_info->req.param1 = req->cryptlen;
254 /* Authentication data length */
255 req_info->req.param2 = 0;
257 fctx->enc.enc_ctrl.e.enc_cipher = ctx->cipher_type;
258 fctx->enc.enc_ctrl.e.aes_key = ctx->key_type;
259 fctx->enc.enc_ctrl.e.iv_source = OTX2_CPT_FROM_CPTR;
261 if (ctx->cipher_type == OTX2_CPT_AES_XTS)
262 memcpy(fctx->enc.encr_key, ctx->enc_key, ctx->key_len * 2);
264 memcpy(fctx->enc.encr_key, ctx->enc_key, ctx->key_len);
266 memcpy(fctx->enc.encr_iv, req->iv, crypto_skcipher_ivsize(stfm));
268 cpu_to_be64s(&fctx->enc.enc_ctrl.u);
271 * Storing Packet Data Information in offset
272 * Control Word First 8 bytes
274 req_info->in[*argcnt].vptr = (u8 *)&rctx->ctrl_word;
275 req_info->in[*argcnt].size = CONTROL_WORD_LEN;
276 req_info->req.dlen += CONTROL_WORD_LEN;
279 req_info->in[*argcnt].vptr = (u8 *)fctx;
280 req_info->in[*argcnt].size = sizeof(struct otx2_cpt_fc_ctx);
281 req_info->req.dlen += sizeof(struct otx2_cpt_fc_ctx);
288 static inline int create_input_list(struct skcipher_request *req, u32 enc,
291 struct otx2_cpt_req_ctx *rctx = skcipher_request_ctx(req);
292 struct otx2_cpt_req_info *req_info = &rctx->cpt_req;
296 ret = create_ctx_hdr(req, enc, &argcnt);
300 update_input_data(req_info, req->src, req->cryptlen, &argcnt);
301 req_info->in_cnt = argcnt;
306 static inline void create_output_list(struct skcipher_request *req,
309 struct otx2_cpt_req_ctx *rctx = skcipher_request_ctx(req);
310 struct otx2_cpt_req_info *req_info = &rctx->cpt_req;
314 * OUTPUT Buffer Processing
315 * AES encryption/decryption output would be
316 * received in the following format
318 * ------IV--------|------ENCRYPTED/DECRYPTED DATA-----|
319 * [ 16 Bytes/ [ Request Enc/Dec/ DATA Len AES CBC ]
321 update_output_data(req_info, req->dst, 0, req->cryptlen, &argcnt);
322 req_info->out_cnt = argcnt;
325 static int skcipher_do_fallback(struct skcipher_request *req, bool is_enc)
327 struct crypto_skcipher *stfm = crypto_skcipher_reqtfm(req);
328 struct otx2_cpt_req_ctx *rctx = skcipher_request_ctx(req);
329 struct otx2_cpt_enc_ctx *ctx = crypto_skcipher_ctx(stfm);
332 if (ctx->fbk_cipher) {
333 skcipher_request_set_tfm(&rctx->sk_fbk_req, ctx->fbk_cipher);
334 skcipher_request_set_callback(&rctx->sk_fbk_req,
338 skcipher_request_set_crypt(&rctx->sk_fbk_req, req->src,
339 req->dst, req->cryptlen, req->iv);
340 ret = is_enc ? crypto_skcipher_encrypt(&rctx->sk_fbk_req) :
341 crypto_skcipher_decrypt(&rctx->sk_fbk_req);
348 static inline int cpt_enc_dec(struct skcipher_request *req, u32 enc)
350 struct crypto_skcipher *stfm = crypto_skcipher_reqtfm(req);
351 struct otx2_cpt_req_ctx *rctx = skcipher_request_ctx(req);
352 struct otx2_cpt_enc_ctx *ctx = crypto_skcipher_ctx(stfm);
353 struct otx2_cpt_req_info *req_info = &rctx->cpt_req;
354 u32 enc_iv_len = crypto_skcipher_ivsize(stfm);
355 struct pci_dev *pdev;
358 if (req->cryptlen == 0)
361 if (!IS_ALIGNED(req->cryptlen, ctx->enc_align_len))
364 if (req->cryptlen > OTX2_CPT_MAX_REQ_SIZE)
365 return skcipher_do_fallback(req, enc);
367 /* Clear control words */
368 rctx->ctrl_word.flags = 0;
369 rctx->fctx.enc.enc_ctrl.u = 0;
371 status = create_input_list(req, enc, enc_iv_len);
374 create_output_list(req, enc_iv_len);
376 status = get_se_device(&pdev, &cpu_num);
380 req_info->callback = otx2_cpt_skcipher_callback;
381 req_info->areq = &req->base;
382 req_info->req_type = OTX2_CPT_ENC_DEC_REQ;
383 req_info->is_enc = enc;
384 req_info->is_trunc_hmac = false;
385 req_info->ctrl.s.grp = otx2_cpt_get_kcrypto_eng_grp_num(pdev);
388 * We perform an asynchronous send and once
389 * the request is completed the driver would
390 * intimate through registered call back functions
392 status = otx2_cpt_do_request(pdev, req_info, cpu_num);
397 static int otx2_cpt_skcipher_encrypt(struct skcipher_request *req)
399 return cpt_enc_dec(req, true);
402 static int otx2_cpt_skcipher_decrypt(struct skcipher_request *req)
404 return cpt_enc_dec(req, false);
407 static int otx2_cpt_skcipher_xts_setkey(struct crypto_skcipher *tfm,
408 const u8 *key, u32 keylen)
410 struct otx2_cpt_enc_ctx *ctx = crypto_skcipher_ctx(tfm);
411 const u8 *key2 = key + (keylen / 2);
412 const u8 *key1 = key;
415 ret = xts_check_key(crypto_skcipher_tfm(tfm), key, keylen);
418 ctx->key_len = keylen;
419 ctx->enc_align_len = 1;
420 memcpy(ctx->enc_key, key1, keylen / 2);
421 memcpy(ctx->enc_key + KEY2_OFFSET, key2, keylen / 2);
422 ctx->cipher_type = OTX2_CPT_AES_XTS;
423 switch (ctx->key_len) {
424 case 2 * AES_KEYSIZE_128:
425 ctx->key_type = OTX2_CPT_AES_128_BIT;
427 case 2 * AES_KEYSIZE_192:
428 ctx->key_type = OTX2_CPT_AES_192_BIT;
430 case 2 * AES_KEYSIZE_256:
431 ctx->key_type = OTX2_CPT_AES_256_BIT;
436 return crypto_skcipher_setkey(ctx->fbk_cipher, key, keylen);
439 static int cpt_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
440 u32 keylen, u8 cipher_type)
442 struct otx2_cpt_enc_ctx *ctx = crypto_skcipher_ctx(tfm);
444 if (keylen != DES3_EDE_KEY_SIZE)
447 ctx->key_len = keylen;
448 ctx->cipher_type = cipher_type;
449 ctx->enc_align_len = 8;
451 memcpy(ctx->enc_key, key, keylen);
453 return crypto_skcipher_setkey(ctx->fbk_cipher, key, keylen);
456 static int cpt_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
457 u32 keylen, u8 cipher_type)
459 struct otx2_cpt_enc_ctx *ctx = crypto_skcipher_ctx(tfm);
462 case AES_KEYSIZE_128:
463 ctx->key_type = OTX2_CPT_AES_128_BIT;
465 case AES_KEYSIZE_192:
466 ctx->key_type = OTX2_CPT_AES_192_BIT;
468 case AES_KEYSIZE_256:
469 ctx->key_type = OTX2_CPT_AES_256_BIT;
474 if (cipher_type == OTX2_CPT_AES_CBC || cipher_type == OTX2_CPT_AES_ECB)
475 ctx->enc_align_len = 16;
477 ctx->enc_align_len = 1;
479 ctx->key_len = keylen;
480 ctx->cipher_type = cipher_type;
482 memcpy(ctx->enc_key, key, keylen);
484 return crypto_skcipher_setkey(ctx->fbk_cipher, key, keylen);
487 static int otx2_cpt_skcipher_cbc_aes_setkey(struct crypto_skcipher *tfm,
488 const u8 *key, u32 keylen)
490 return cpt_aes_setkey(tfm, key, keylen, OTX2_CPT_AES_CBC);
493 static int otx2_cpt_skcipher_ecb_aes_setkey(struct crypto_skcipher *tfm,
494 const u8 *key, u32 keylen)
496 return cpt_aes_setkey(tfm, key, keylen, OTX2_CPT_AES_ECB);
499 static int otx2_cpt_skcipher_cbc_des3_setkey(struct crypto_skcipher *tfm,
500 const u8 *key, u32 keylen)
502 return cpt_des_setkey(tfm, key, keylen, OTX2_CPT_DES3_CBC);
505 static int otx2_cpt_skcipher_ecb_des3_setkey(struct crypto_skcipher *tfm,
506 const u8 *key, u32 keylen)
508 return cpt_des_setkey(tfm, key, keylen, OTX2_CPT_DES3_ECB);
511 static int cpt_skcipher_fallback_init(struct otx2_cpt_enc_ctx *ctx,
512 struct crypto_alg *alg)
514 if (alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
516 crypto_alloc_skcipher(alg->cra_name, 0,
518 CRYPTO_ALG_NEED_FALLBACK);
519 if (IS_ERR(ctx->fbk_cipher)) {
520 pr_err("%s() failed to allocate fallback for %s\n",
521 __func__, alg->cra_name);
522 return PTR_ERR(ctx->fbk_cipher);
528 static int otx2_cpt_enc_dec_init(struct crypto_skcipher *stfm)
530 struct otx2_cpt_enc_ctx *ctx = crypto_skcipher_ctx(stfm);
531 struct crypto_tfm *tfm = crypto_skcipher_tfm(stfm);
532 struct crypto_alg *alg = tfm->__crt_alg;
534 memset(ctx, 0, sizeof(*ctx));
536 * Additional memory for skcipher_request is
537 * allocated since the cryptd daemon uses
538 * this memory for request_ctx information
540 crypto_skcipher_set_reqsize(stfm, sizeof(struct otx2_cpt_req_ctx) +
541 sizeof(struct skcipher_request));
543 return cpt_skcipher_fallback_init(ctx, alg);
546 static void otx2_cpt_skcipher_exit(struct crypto_skcipher *tfm)
548 struct otx2_cpt_enc_ctx *ctx = crypto_skcipher_ctx(tfm);
550 if (ctx->fbk_cipher) {
551 crypto_free_skcipher(ctx->fbk_cipher);
552 ctx->fbk_cipher = NULL;
556 static int cpt_aead_fallback_init(struct otx2_cpt_aead_ctx *ctx,
557 struct crypto_alg *alg)
559 if (alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
561 crypto_alloc_aead(alg->cra_name, 0,
563 CRYPTO_ALG_NEED_FALLBACK);
564 if (IS_ERR(ctx->fbk_cipher)) {
565 pr_err("%s() failed to allocate fallback for %s\n",
566 __func__, alg->cra_name);
567 return PTR_ERR(ctx->fbk_cipher);
573 static int cpt_aead_init(struct crypto_aead *atfm, u8 cipher_type, u8 mac_type)
575 struct otx2_cpt_aead_ctx *ctx = crypto_aead_ctx(atfm);
576 struct crypto_tfm *tfm = crypto_aead_tfm(atfm);
577 struct crypto_alg *alg = tfm->__crt_alg;
579 ctx->cipher_type = cipher_type;
580 ctx->mac_type = mac_type;
583 * When selected cipher is NULL we use HMAC opcode instead of
584 * FLEXICRYPTO opcode therefore we don't need to use HASH algorithms
585 * for calculating ipad and opad
587 if (ctx->cipher_type != OTX2_CPT_CIPHER_NULL) {
588 switch (ctx->mac_type) {
590 ctx->hashalg = crypto_alloc_shash("sha1", 0,
592 if (IS_ERR(ctx->hashalg))
593 return PTR_ERR(ctx->hashalg);
596 case OTX2_CPT_SHA256:
597 ctx->hashalg = crypto_alloc_shash("sha256", 0,
599 if (IS_ERR(ctx->hashalg))
600 return PTR_ERR(ctx->hashalg);
603 case OTX2_CPT_SHA384:
604 ctx->hashalg = crypto_alloc_shash("sha384", 0,
606 if (IS_ERR(ctx->hashalg))
607 return PTR_ERR(ctx->hashalg);
610 case OTX2_CPT_SHA512:
611 ctx->hashalg = crypto_alloc_shash("sha512", 0,
613 if (IS_ERR(ctx->hashalg))
614 return PTR_ERR(ctx->hashalg);
618 switch (ctx->cipher_type) {
619 case OTX2_CPT_AES_CBC:
620 case OTX2_CPT_AES_ECB:
621 ctx->enc_align_len = 16;
623 case OTX2_CPT_DES3_CBC:
624 case OTX2_CPT_DES3_ECB:
625 ctx->enc_align_len = 8;
627 case OTX2_CPT_AES_GCM:
628 case OTX2_CPT_CIPHER_NULL:
629 ctx->enc_align_len = 1;
632 crypto_aead_set_reqsize(atfm, sizeof(struct otx2_cpt_req_ctx));
634 return cpt_aead_fallback_init(ctx, alg);
637 static int otx2_cpt_aead_cbc_aes_sha1_init(struct crypto_aead *tfm)
639 return cpt_aead_init(tfm, OTX2_CPT_AES_CBC, OTX2_CPT_SHA1);
642 static int otx2_cpt_aead_cbc_aes_sha256_init(struct crypto_aead *tfm)
644 return cpt_aead_init(tfm, OTX2_CPT_AES_CBC, OTX2_CPT_SHA256);
647 static int otx2_cpt_aead_cbc_aes_sha384_init(struct crypto_aead *tfm)
649 return cpt_aead_init(tfm, OTX2_CPT_AES_CBC, OTX2_CPT_SHA384);
652 static int otx2_cpt_aead_cbc_aes_sha512_init(struct crypto_aead *tfm)
654 return cpt_aead_init(tfm, OTX2_CPT_AES_CBC, OTX2_CPT_SHA512);
657 static int otx2_cpt_aead_ecb_null_sha1_init(struct crypto_aead *tfm)
659 return cpt_aead_init(tfm, OTX2_CPT_CIPHER_NULL, OTX2_CPT_SHA1);
662 static int otx2_cpt_aead_ecb_null_sha256_init(struct crypto_aead *tfm)
664 return cpt_aead_init(tfm, OTX2_CPT_CIPHER_NULL, OTX2_CPT_SHA256);
667 static int otx2_cpt_aead_ecb_null_sha384_init(struct crypto_aead *tfm)
669 return cpt_aead_init(tfm, OTX2_CPT_CIPHER_NULL, OTX2_CPT_SHA384);
672 static int otx2_cpt_aead_ecb_null_sha512_init(struct crypto_aead *tfm)
674 return cpt_aead_init(tfm, OTX2_CPT_CIPHER_NULL, OTX2_CPT_SHA512);
677 static int otx2_cpt_aead_gcm_aes_init(struct crypto_aead *tfm)
679 return cpt_aead_init(tfm, OTX2_CPT_AES_GCM, OTX2_CPT_MAC_NULL);
682 static void otx2_cpt_aead_exit(struct crypto_aead *tfm)
684 struct otx2_cpt_aead_ctx *ctx = crypto_aead_ctx(tfm);
689 crypto_free_shash(ctx->hashalg);
692 if (ctx->fbk_cipher) {
693 crypto_free_aead(ctx->fbk_cipher);
694 ctx->fbk_cipher = NULL;
698 static int otx2_cpt_aead_gcm_set_authsize(struct crypto_aead *tfm,
699 unsigned int authsize)
701 struct otx2_cpt_aead_ctx *ctx = crypto_aead_ctx(tfm);
703 if (crypto_rfc4106_check_authsize(authsize))
706 tfm->authsize = authsize;
707 /* Set authsize for fallback case */
709 ctx->fbk_cipher->authsize = authsize;
714 static int otx2_cpt_aead_set_authsize(struct crypto_aead *tfm,
715 unsigned int authsize)
717 tfm->authsize = authsize;
722 static int otx2_cpt_aead_null_set_authsize(struct crypto_aead *tfm,
723 unsigned int authsize)
725 struct otx2_cpt_aead_ctx *ctx = crypto_aead_ctx(tfm);
727 ctx->is_trunc_hmac = true;
728 tfm->authsize = authsize;
733 static struct otx2_cpt_sdesc *alloc_sdesc(struct crypto_shash *alg)
735 struct otx2_cpt_sdesc *sdesc;
738 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
739 sdesc = kmalloc(size, GFP_KERNEL);
743 sdesc->shash.tfm = alg;
748 static inline void swap_data32(void *buf, u32 len)
750 cpu_to_be32_array(buf, buf, len / 4);
753 static inline void swap_data64(void *buf, u32 len)
758 for (i = 0 ; i < len / 8; i++, src++)
762 static int copy_pad(u8 mac_type, u8 *out_pad, u8 *in_pad)
764 struct sha512_state *sha512;
765 struct sha256_state *sha256;
766 struct sha1_state *sha1;
770 sha1 = (struct sha1_state *) in_pad;
771 swap_data32(sha1->state, SHA1_DIGEST_SIZE);
772 memcpy(out_pad, &sha1->state, SHA1_DIGEST_SIZE);
775 case OTX2_CPT_SHA256:
776 sha256 = (struct sha256_state *) in_pad;
777 swap_data32(sha256->state, SHA256_DIGEST_SIZE);
778 memcpy(out_pad, &sha256->state, SHA256_DIGEST_SIZE);
781 case OTX2_CPT_SHA384:
782 case OTX2_CPT_SHA512:
783 sha512 = (struct sha512_state *) in_pad;
784 swap_data64(sha512->state, SHA512_DIGEST_SIZE);
785 memcpy(out_pad, &sha512->state, SHA512_DIGEST_SIZE);
795 static int aead_hmac_init(struct crypto_aead *cipher)
797 struct otx2_cpt_aead_ctx *ctx = crypto_aead_ctx(cipher);
798 int state_size = crypto_shash_statesize(ctx->hashalg);
799 int ds = crypto_shash_digestsize(ctx->hashalg);
800 int bs = crypto_shash_blocksize(ctx->hashalg);
801 int authkeylen = ctx->auth_key_len;
802 u8 *ipad = NULL, *opad = NULL;
803 int ret = 0, icount = 0;
805 ctx->sdesc = alloc_sdesc(ctx->hashalg);
809 ctx->ipad = kzalloc(bs, GFP_KERNEL);
815 ctx->opad = kzalloc(bs, GFP_KERNEL);
821 ipad = kzalloc(state_size, GFP_KERNEL);
827 opad = kzalloc(state_size, GFP_KERNEL);
833 if (authkeylen > bs) {
834 ret = crypto_shash_digest(&ctx->sdesc->shash, ctx->key,
841 memcpy(ipad, ctx->key, authkeylen);
844 memset(ipad + authkeylen, 0, bs - authkeylen);
845 memcpy(opad, ipad, bs);
847 for (icount = 0; icount < bs; icount++) {
848 ipad[icount] ^= 0x36;
849 opad[icount] ^= 0x5c;
853 * Partial Hash calculated from the software
854 * algorithm is retrieved for IPAD & OPAD
857 /* IPAD Calculation */
858 crypto_shash_init(&ctx->sdesc->shash);
859 crypto_shash_update(&ctx->sdesc->shash, ipad, bs);
860 crypto_shash_export(&ctx->sdesc->shash, ipad);
861 ret = copy_pad(ctx->mac_type, ctx->ipad, ipad);
865 /* OPAD Calculation */
866 crypto_shash_init(&ctx->sdesc->shash);
867 crypto_shash_update(&ctx->sdesc->shash, opad, bs);
868 crypto_shash_export(&ctx->sdesc->shash, opad);
869 ret = copy_pad(ctx->mac_type, ctx->opad, opad);
891 static int otx2_cpt_aead_cbc_aes_sha_setkey(struct crypto_aead *cipher,
892 const unsigned char *key,
895 struct otx2_cpt_aead_ctx *ctx = crypto_aead_ctx(cipher);
896 struct crypto_authenc_key_param *param;
897 int enckeylen = 0, authkeylen = 0;
898 struct rtattr *rta = (void *)key;
901 if (!RTA_OK(rta, keylen))
904 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
907 if (RTA_PAYLOAD(rta) < sizeof(*param))
910 param = RTA_DATA(rta);
911 enckeylen = be32_to_cpu(param->enckeylen);
912 key += RTA_ALIGN(rta->rta_len);
913 keylen -= RTA_ALIGN(rta->rta_len);
914 if (keylen < enckeylen)
917 if (keylen > OTX2_CPT_MAX_KEY_SIZE)
920 authkeylen = keylen - enckeylen;
921 memcpy(ctx->key, key, keylen);
924 case AES_KEYSIZE_128:
925 ctx->key_type = OTX2_CPT_AES_128_BIT;
927 case AES_KEYSIZE_192:
928 ctx->key_type = OTX2_CPT_AES_192_BIT;
930 case AES_KEYSIZE_256:
931 ctx->key_type = OTX2_CPT_AES_256_BIT;
934 /* Invalid key length */
938 ctx->enc_key_len = enckeylen;
939 ctx->auth_key_len = authkeylen;
941 status = aead_hmac_init(cipher);
948 static int otx2_cpt_aead_ecb_null_sha_setkey(struct crypto_aead *cipher,
949 const unsigned char *key,
952 struct otx2_cpt_aead_ctx *ctx = crypto_aead_ctx(cipher);
953 struct crypto_authenc_key_param *param;
954 struct rtattr *rta = (void *)key;
957 if (!RTA_OK(rta, keylen))
960 if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
963 if (RTA_PAYLOAD(rta) < sizeof(*param))
966 param = RTA_DATA(rta);
967 enckeylen = be32_to_cpu(param->enckeylen);
968 key += RTA_ALIGN(rta->rta_len);
969 keylen -= RTA_ALIGN(rta->rta_len);
973 if (keylen > OTX2_CPT_MAX_KEY_SIZE)
976 memcpy(ctx->key, key, keylen);
977 ctx->enc_key_len = enckeylen;
978 ctx->auth_key_len = keylen;
983 static int otx2_cpt_aead_gcm_aes_setkey(struct crypto_aead *cipher,
984 const unsigned char *key,
987 struct otx2_cpt_aead_ctx *ctx = crypto_aead_ctx(cipher);
990 * For aes gcm we expect to get encryption key (16, 24, 32 bytes)
994 case AES_KEYSIZE_128 + AES_GCM_SALT_SIZE:
995 ctx->key_type = OTX2_CPT_AES_128_BIT;
996 ctx->enc_key_len = AES_KEYSIZE_128;
998 case AES_KEYSIZE_192 + AES_GCM_SALT_SIZE:
999 ctx->key_type = OTX2_CPT_AES_192_BIT;
1000 ctx->enc_key_len = AES_KEYSIZE_192;
1002 case AES_KEYSIZE_256 + AES_GCM_SALT_SIZE:
1003 ctx->key_type = OTX2_CPT_AES_256_BIT;
1004 ctx->enc_key_len = AES_KEYSIZE_256;
1007 /* Invalid key and salt length */
1011 /* Store encryption key and salt */
1012 memcpy(ctx->key, key, keylen);
1014 return crypto_aead_setkey(ctx->fbk_cipher, key, keylen);
1017 static inline int create_aead_ctx_hdr(struct aead_request *req, u32 enc,
1020 struct otx2_cpt_req_ctx *rctx = aead_request_ctx(req);
1021 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1022 struct otx2_cpt_aead_ctx *ctx = crypto_aead_ctx(tfm);
1023 struct otx2_cpt_req_info *req_info = &rctx->cpt_req;
1024 struct otx2_cpt_fc_ctx *fctx = &rctx->fctx;
1025 int mac_len = crypto_aead_authsize(tfm);
1028 rctx->ctrl_word.e.enc_data_offset = req->assoclen;
1030 switch (ctx->cipher_type) {
1031 case OTX2_CPT_AES_CBC:
1032 if (req->assoclen > 248 || !IS_ALIGNED(req->assoclen, 8))
1035 fctx->enc.enc_ctrl.e.iv_source = OTX2_CPT_FROM_CPTR;
1036 /* Copy encryption key to context */
1037 memcpy(fctx->enc.encr_key, ctx->key + ctx->auth_key_len,
1039 /* Copy IV to context */
1040 memcpy(fctx->enc.encr_iv, req->iv, crypto_aead_ivsize(tfm));
1042 ds = crypto_shash_digestsize(ctx->hashalg);
1043 if (ctx->mac_type == OTX2_CPT_SHA384)
1044 ds = SHA512_DIGEST_SIZE;
1046 memcpy(fctx->hmac.e.ipad, ctx->ipad, ds);
1048 memcpy(fctx->hmac.e.opad, ctx->opad, ds);
1051 case OTX2_CPT_AES_GCM:
1052 if (crypto_ipsec_check_assoclen(req->assoclen))
1055 fctx->enc.enc_ctrl.e.iv_source = OTX2_CPT_FROM_DPTR;
1056 /* Copy encryption key to context */
1057 memcpy(fctx->enc.encr_key, ctx->key, ctx->enc_key_len);
1058 /* Copy salt to context */
1059 memcpy(fctx->enc.encr_iv, ctx->key + ctx->enc_key_len,
1062 rctx->ctrl_word.e.iv_offset = req->assoclen - AES_GCM_IV_OFFSET;
1066 /* Unknown cipher type */
1069 cpu_to_be64s(&rctx->ctrl_word.flags);
1071 req_info->ctrl.s.dma_mode = OTX2_CPT_DMA_MODE_SG;
1072 req_info->ctrl.s.se_req = 1;
1073 req_info->req.opcode.s.major = OTX2_CPT_MAJOR_OP_FC |
1074 DMA_MODE_FLAG(OTX2_CPT_DMA_MODE_SG);
1076 req_info->req.opcode.s.minor = 2;
1077 req_info->req.param1 = req->cryptlen;
1078 req_info->req.param2 = req->cryptlen + req->assoclen;
1080 req_info->req.opcode.s.minor = 3;
1081 req_info->req.param1 = req->cryptlen - mac_len;
1082 req_info->req.param2 = req->cryptlen + req->assoclen - mac_len;
1085 fctx->enc.enc_ctrl.e.enc_cipher = ctx->cipher_type;
1086 fctx->enc.enc_ctrl.e.aes_key = ctx->key_type;
1087 fctx->enc.enc_ctrl.e.mac_type = ctx->mac_type;
1088 fctx->enc.enc_ctrl.e.mac_len = mac_len;
1089 cpu_to_be64s(&fctx->enc.enc_ctrl.u);
1092 * Storing Packet Data Information in offset
1093 * Control Word First 8 bytes
1095 req_info->in[*argcnt].vptr = (u8 *)&rctx->ctrl_word;
1096 req_info->in[*argcnt].size = CONTROL_WORD_LEN;
1097 req_info->req.dlen += CONTROL_WORD_LEN;
1100 req_info->in[*argcnt].vptr = (u8 *)fctx;
1101 req_info->in[*argcnt].size = sizeof(struct otx2_cpt_fc_ctx);
1102 req_info->req.dlen += sizeof(struct otx2_cpt_fc_ctx);
1108 static inline void create_hmac_ctx_hdr(struct aead_request *req, u32 *argcnt,
1111 struct otx2_cpt_req_ctx *rctx = aead_request_ctx(req);
1112 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1113 struct otx2_cpt_aead_ctx *ctx = crypto_aead_ctx(tfm);
1114 struct otx2_cpt_req_info *req_info = &rctx->cpt_req;
1116 req_info->ctrl.s.dma_mode = OTX2_CPT_DMA_MODE_SG;
1117 req_info->ctrl.s.se_req = 1;
1118 req_info->req.opcode.s.major = OTX2_CPT_MAJOR_OP_HMAC |
1119 DMA_MODE_FLAG(OTX2_CPT_DMA_MODE_SG);
1120 req_info->is_trunc_hmac = ctx->is_trunc_hmac;
1122 req_info->req.opcode.s.minor = 0;
1123 req_info->req.param1 = ctx->auth_key_len;
1124 req_info->req.param2 = ctx->mac_type << 8;
1126 /* Add authentication key */
1127 req_info->in[*argcnt].vptr = ctx->key;
1128 req_info->in[*argcnt].size = round_up(ctx->auth_key_len, 8);
1129 req_info->req.dlen += round_up(ctx->auth_key_len, 8);
1133 static inline int create_aead_input_list(struct aead_request *req, u32 enc)
1135 struct otx2_cpt_req_ctx *rctx = aead_request_ctx(req);
1136 struct otx2_cpt_req_info *req_info = &rctx->cpt_req;
1137 u32 inputlen = req->cryptlen + req->assoclen;
1138 u32 status, argcnt = 0;
1140 status = create_aead_ctx_hdr(req, enc, &argcnt);
1143 update_input_data(req_info, req->src, inputlen, &argcnt);
1144 req_info->in_cnt = argcnt;
1149 static inline void create_aead_output_list(struct aead_request *req, u32 enc,
1152 struct otx2_cpt_req_ctx *rctx = aead_request_ctx(req);
1153 struct otx2_cpt_req_info *req_info = &rctx->cpt_req;
1154 u32 argcnt = 0, outputlen = 0;
1157 outputlen = req->cryptlen + req->assoclen + mac_len;
1159 outputlen = req->cryptlen + req->assoclen - mac_len;
1161 update_output_data(req_info, req->dst, 0, outputlen, &argcnt);
1162 req_info->out_cnt = argcnt;
1165 static inline void create_aead_null_input_list(struct aead_request *req,
1166 u32 enc, u32 mac_len)
1168 struct otx2_cpt_req_ctx *rctx = aead_request_ctx(req);
1169 struct otx2_cpt_req_info *req_info = &rctx->cpt_req;
1170 u32 inputlen, argcnt = 0;
1173 inputlen = req->cryptlen + req->assoclen;
1175 inputlen = req->cryptlen + req->assoclen - mac_len;
1177 create_hmac_ctx_hdr(req, &argcnt, enc);
1178 update_input_data(req_info, req->src, inputlen, &argcnt);
1179 req_info->in_cnt = argcnt;
1182 static inline int create_aead_null_output_list(struct aead_request *req,
1183 u32 enc, u32 mac_len)
1185 struct otx2_cpt_req_ctx *rctx = aead_request_ctx(req);
1186 struct otx2_cpt_req_info *req_info = &rctx->cpt_req;
1187 struct scatterlist *dst;
1189 int argcnt = 0, status, offset;
1193 inputlen = req->cryptlen + req->assoclen;
1195 inputlen = req->cryptlen + req->assoclen - mac_len;
1198 * If source and destination are different
1199 * then copy payload to destination
1201 if (req->src != req->dst) {
1203 ptr = kmalloc(inputlen, (req_info->areq->flags &
1204 CRYPTO_TFM_REQ_MAY_SLEEP) ?
1205 GFP_KERNEL : GFP_ATOMIC);
1209 status = sg_copy_to_buffer(req->src, sg_nents(req->src), ptr,
1211 if (status != inputlen) {
1215 status = sg_copy_from_buffer(req->dst, sg_nents(req->dst), ptr,
1217 if (status != inputlen) {
1226 * In an encryption scenario hmac needs
1227 * to be appended after payload
1231 while (offset >= dst->length) {
1232 offset -= dst->length;
1238 update_output_data(req_info, dst, offset, mac_len, &argcnt);
1241 * In a decryption scenario calculated hmac for received
1242 * payload needs to be compare with hmac received
1244 status = sg_copy_buffer(req->src, sg_nents(req->src),
1245 rctx->fctx.hmac.s.hmac_recv, mac_len,
1247 if (status != mac_len)
1250 req_info->out[argcnt].vptr = rctx->fctx.hmac.s.hmac_calc;
1251 req_info->out[argcnt].size = mac_len;
1255 req_info->out_cnt = argcnt;
1263 static int aead_do_fallback(struct aead_request *req, bool is_enc)
1265 struct otx2_cpt_req_ctx *rctx = aead_request_ctx(req);
1266 struct crypto_aead *aead = crypto_aead_reqtfm(req);
1267 struct otx2_cpt_aead_ctx *ctx = crypto_aead_ctx(aead);
1270 if (ctx->fbk_cipher) {
1271 /* Store the cipher tfm and then use the fallback tfm */
1272 aead_request_set_tfm(&rctx->fbk_req, ctx->fbk_cipher);
1273 aead_request_set_callback(&rctx->fbk_req, req->base.flags,
1274 req->base.complete, req->base.data);
1275 aead_request_set_crypt(&rctx->fbk_req, req->src,
1276 req->dst, req->cryptlen, req->iv);
1277 aead_request_set_ad(&rctx->fbk_req, req->assoclen);
1278 ret = is_enc ? crypto_aead_encrypt(&rctx->fbk_req) :
1279 crypto_aead_decrypt(&rctx->fbk_req);
1287 static int cpt_aead_enc_dec(struct aead_request *req, u8 reg_type, u8 enc)
1289 struct otx2_cpt_req_ctx *rctx = aead_request_ctx(req);
1290 struct otx2_cpt_req_info *req_info = &rctx->cpt_req;
1291 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1292 struct otx2_cpt_aead_ctx *ctx = crypto_aead_ctx(tfm);
1293 struct pci_dev *pdev;
1294 int status, cpu_num;
1296 /* Clear control words */
1297 rctx->ctrl_word.flags = 0;
1298 rctx->fctx.enc.enc_ctrl.u = 0;
1300 req_info->callback = otx2_cpt_aead_callback;
1301 req_info->areq = &req->base;
1302 req_info->req_type = reg_type;
1303 req_info->is_enc = enc;
1304 req_info->is_trunc_hmac = false;
1307 case OTX2_CPT_AEAD_ENC_DEC_REQ:
1308 status = create_aead_input_list(req, enc);
1311 create_aead_output_list(req, enc, crypto_aead_authsize(tfm));
1314 case OTX2_CPT_AEAD_ENC_DEC_NULL_REQ:
1315 create_aead_null_input_list(req, enc,
1316 crypto_aead_authsize(tfm));
1317 status = create_aead_null_output_list(req, enc,
1318 crypto_aead_authsize(tfm));
1326 if (!IS_ALIGNED(req_info->req.param1, ctx->enc_align_len))
1329 if (!req_info->req.param2 ||
1330 (req_info->req.param1 > OTX2_CPT_MAX_REQ_SIZE) ||
1331 (req_info->req.param2 > OTX2_CPT_MAX_REQ_SIZE))
1332 return aead_do_fallback(req, enc);
1334 status = get_se_device(&pdev, &cpu_num);
1338 req_info->ctrl.s.grp = otx2_cpt_get_kcrypto_eng_grp_num(pdev);
1341 * We perform an asynchronous send and once
1342 * the request is completed the driver would
1343 * intimate through registered call back functions
1345 return otx2_cpt_do_request(pdev, req_info, cpu_num);
1348 static int otx2_cpt_aead_encrypt(struct aead_request *req)
1350 return cpt_aead_enc_dec(req, OTX2_CPT_AEAD_ENC_DEC_REQ, true);
1353 static int otx2_cpt_aead_decrypt(struct aead_request *req)
1355 return cpt_aead_enc_dec(req, OTX2_CPT_AEAD_ENC_DEC_REQ, false);
1358 static int otx2_cpt_aead_null_encrypt(struct aead_request *req)
1360 return cpt_aead_enc_dec(req, OTX2_CPT_AEAD_ENC_DEC_NULL_REQ, true);
1363 static int otx2_cpt_aead_null_decrypt(struct aead_request *req)
1365 return cpt_aead_enc_dec(req, OTX2_CPT_AEAD_ENC_DEC_NULL_REQ, false);
1368 static struct skcipher_alg otx2_cpt_skciphers[] = { {
1369 .base.cra_name = "xts(aes)",
1370 .base.cra_driver_name = "cpt_xts_aes",
1371 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1372 .base.cra_blocksize = AES_BLOCK_SIZE,
1373 .base.cra_ctxsize = sizeof(struct otx2_cpt_enc_ctx),
1374 .base.cra_alignmask = 7,
1375 .base.cra_priority = 4001,
1376 .base.cra_module = THIS_MODULE,
1378 .init = otx2_cpt_enc_dec_init,
1379 .exit = otx2_cpt_skcipher_exit,
1380 .ivsize = AES_BLOCK_SIZE,
1381 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1382 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1383 .setkey = otx2_cpt_skcipher_xts_setkey,
1384 .encrypt = otx2_cpt_skcipher_encrypt,
1385 .decrypt = otx2_cpt_skcipher_decrypt,
1387 .base.cra_name = "cbc(aes)",
1388 .base.cra_driver_name = "cpt_cbc_aes",
1389 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1390 .base.cra_blocksize = AES_BLOCK_SIZE,
1391 .base.cra_ctxsize = sizeof(struct otx2_cpt_enc_ctx),
1392 .base.cra_alignmask = 7,
1393 .base.cra_priority = 4001,
1394 .base.cra_module = THIS_MODULE,
1396 .init = otx2_cpt_enc_dec_init,
1397 .exit = otx2_cpt_skcipher_exit,
1398 .ivsize = AES_BLOCK_SIZE,
1399 .min_keysize = AES_MIN_KEY_SIZE,
1400 .max_keysize = AES_MAX_KEY_SIZE,
1401 .setkey = otx2_cpt_skcipher_cbc_aes_setkey,
1402 .encrypt = otx2_cpt_skcipher_encrypt,
1403 .decrypt = otx2_cpt_skcipher_decrypt,
1405 .base.cra_name = "ecb(aes)",
1406 .base.cra_driver_name = "cpt_ecb_aes",
1407 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1408 .base.cra_blocksize = AES_BLOCK_SIZE,
1409 .base.cra_ctxsize = sizeof(struct otx2_cpt_enc_ctx),
1410 .base.cra_alignmask = 7,
1411 .base.cra_priority = 4001,
1412 .base.cra_module = THIS_MODULE,
1414 .init = otx2_cpt_enc_dec_init,
1415 .exit = otx2_cpt_skcipher_exit,
1417 .min_keysize = AES_MIN_KEY_SIZE,
1418 .max_keysize = AES_MAX_KEY_SIZE,
1419 .setkey = otx2_cpt_skcipher_ecb_aes_setkey,
1420 .encrypt = otx2_cpt_skcipher_encrypt,
1421 .decrypt = otx2_cpt_skcipher_decrypt,
1423 .base.cra_name = "cbc(des3_ede)",
1424 .base.cra_driver_name = "cpt_cbc_des3_ede",
1425 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1426 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1427 .base.cra_ctxsize = sizeof(struct otx2_cpt_enc_ctx),
1428 .base.cra_alignmask = 7,
1429 .base.cra_priority = 4001,
1430 .base.cra_module = THIS_MODULE,
1432 .init = otx2_cpt_enc_dec_init,
1433 .exit = otx2_cpt_skcipher_exit,
1434 .min_keysize = DES3_EDE_KEY_SIZE,
1435 .max_keysize = DES3_EDE_KEY_SIZE,
1436 .ivsize = DES_BLOCK_SIZE,
1437 .setkey = otx2_cpt_skcipher_cbc_des3_setkey,
1438 .encrypt = otx2_cpt_skcipher_encrypt,
1439 .decrypt = otx2_cpt_skcipher_decrypt,
1441 .base.cra_name = "ecb(des3_ede)",
1442 .base.cra_driver_name = "cpt_ecb_des3_ede",
1443 .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1444 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1445 .base.cra_ctxsize = sizeof(struct otx2_cpt_enc_ctx),
1446 .base.cra_alignmask = 7,
1447 .base.cra_priority = 4001,
1448 .base.cra_module = THIS_MODULE,
1450 .init = otx2_cpt_enc_dec_init,
1451 .exit = otx2_cpt_skcipher_exit,
1452 .min_keysize = DES3_EDE_KEY_SIZE,
1453 .max_keysize = DES3_EDE_KEY_SIZE,
1455 .setkey = otx2_cpt_skcipher_ecb_des3_setkey,
1456 .encrypt = otx2_cpt_skcipher_encrypt,
1457 .decrypt = otx2_cpt_skcipher_decrypt,
1460 static struct aead_alg otx2_cpt_aeads[] = { {
1462 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1463 .cra_driver_name = "cpt_hmac_sha1_cbc_aes",
1464 .cra_blocksize = AES_BLOCK_SIZE,
1465 .cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1466 .cra_ctxsize = sizeof(struct otx2_cpt_aead_ctx),
1467 .cra_priority = 4001,
1469 .cra_module = THIS_MODULE,
1471 .init = otx2_cpt_aead_cbc_aes_sha1_init,
1472 .exit = otx2_cpt_aead_exit,
1473 .setkey = otx2_cpt_aead_cbc_aes_sha_setkey,
1474 .setauthsize = otx2_cpt_aead_set_authsize,
1475 .encrypt = otx2_cpt_aead_encrypt,
1476 .decrypt = otx2_cpt_aead_decrypt,
1477 .ivsize = AES_BLOCK_SIZE,
1478 .maxauthsize = SHA1_DIGEST_SIZE,
1481 .cra_name = "authenc(hmac(sha256),cbc(aes))",
1482 .cra_driver_name = "cpt_hmac_sha256_cbc_aes",
1483 .cra_blocksize = AES_BLOCK_SIZE,
1484 .cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1485 .cra_ctxsize = sizeof(struct otx2_cpt_aead_ctx),
1486 .cra_priority = 4001,
1488 .cra_module = THIS_MODULE,
1490 .init = otx2_cpt_aead_cbc_aes_sha256_init,
1491 .exit = otx2_cpt_aead_exit,
1492 .setkey = otx2_cpt_aead_cbc_aes_sha_setkey,
1493 .setauthsize = otx2_cpt_aead_set_authsize,
1494 .encrypt = otx2_cpt_aead_encrypt,
1495 .decrypt = otx2_cpt_aead_decrypt,
1496 .ivsize = AES_BLOCK_SIZE,
1497 .maxauthsize = SHA256_DIGEST_SIZE,
1500 .cra_name = "authenc(hmac(sha384),cbc(aes))",
1501 .cra_driver_name = "cpt_hmac_sha384_cbc_aes",
1502 .cra_blocksize = AES_BLOCK_SIZE,
1503 .cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1504 .cra_ctxsize = sizeof(struct otx2_cpt_aead_ctx),
1505 .cra_priority = 4001,
1507 .cra_module = THIS_MODULE,
1509 .init = otx2_cpt_aead_cbc_aes_sha384_init,
1510 .exit = otx2_cpt_aead_exit,
1511 .setkey = otx2_cpt_aead_cbc_aes_sha_setkey,
1512 .setauthsize = otx2_cpt_aead_set_authsize,
1513 .encrypt = otx2_cpt_aead_encrypt,
1514 .decrypt = otx2_cpt_aead_decrypt,
1515 .ivsize = AES_BLOCK_SIZE,
1516 .maxauthsize = SHA384_DIGEST_SIZE,
1519 .cra_name = "authenc(hmac(sha512),cbc(aes))",
1520 .cra_driver_name = "cpt_hmac_sha512_cbc_aes",
1521 .cra_blocksize = AES_BLOCK_SIZE,
1522 .cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1523 .cra_ctxsize = sizeof(struct otx2_cpt_aead_ctx),
1524 .cra_priority = 4001,
1526 .cra_module = THIS_MODULE,
1528 .init = otx2_cpt_aead_cbc_aes_sha512_init,
1529 .exit = otx2_cpt_aead_exit,
1530 .setkey = otx2_cpt_aead_cbc_aes_sha_setkey,
1531 .setauthsize = otx2_cpt_aead_set_authsize,
1532 .encrypt = otx2_cpt_aead_encrypt,
1533 .decrypt = otx2_cpt_aead_decrypt,
1534 .ivsize = AES_BLOCK_SIZE,
1535 .maxauthsize = SHA512_DIGEST_SIZE,
1538 .cra_name = "authenc(hmac(sha1),ecb(cipher_null))",
1539 .cra_driver_name = "cpt_hmac_sha1_ecb_null",
1541 .cra_flags = CRYPTO_ALG_ASYNC,
1542 .cra_ctxsize = sizeof(struct otx2_cpt_aead_ctx),
1543 .cra_priority = 4001,
1545 .cra_module = THIS_MODULE,
1547 .init = otx2_cpt_aead_ecb_null_sha1_init,
1548 .exit = otx2_cpt_aead_exit,
1549 .setkey = otx2_cpt_aead_ecb_null_sha_setkey,
1550 .setauthsize = otx2_cpt_aead_null_set_authsize,
1551 .encrypt = otx2_cpt_aead_null_encrypt,
1552 .decrypt = otx2_cpt_aead_null_decrypt,
1554 .maxauthsize = SHA1_DIGEST_SIZE,
1557 .cra_name = "authenc(hmac(sha256),ecb(cipher_null))",
1558 .cra_driver_name = "cpt_hmac_sha256_ecb_null",
1560 .cra_flags = CRYPTO_ALG_ASYNC,
1561 .cra_ctxsize = sizeof(struct otx2_cpt_aead_ctx),
1562 .cra_priority = 4001,
1564 .cra_module = THIS_MODULE,
1566 .init = otx2_cpt_aead_ecb_null_sha256_init,
1567 .exit = otx2_cpt_aead_exit,
1568 .setkey = otx2_cpt_aead_ecb_null_sha_setkey,
1569 .setauthsize = otx2_cpt_aead_null_set_authsize,
1570 .encrypt = otx2_cpt_aead_null_encrypt,
1571 .decrypt = otx2_cpt_aead_null_decrypt,
1573 .maxauthsize = SHA256_DIGEST_SIZE,
1576 .cra_name = "authenc(hmac(sha384),ecb(cipher_null))",
1577 .cra_driver_name = "cpt_hmac_sha384_ecb_null",
1579 .cra_flags = CRYPTO_ALG_ASYNC,
1580 .cra_ctxsize = sizeof(struct otx2_cpt_aead_ctx),
1581 .cra_priority = 4001,
1583 .cra_module = THIS_MODULE,
1585 .init = otx2_cpt_aead_ecb_null_sha384_init,
1586 .exit = otx2_cpt_aead_exit,
1587 .setkey = otx2_cpt_aead_ecb_null_sha_setkey,
1588 .setauthsize = otx2_cpt_aead_null_set_authsize,
1589 .encrypt = otx2_cpt_aead_null_encrypt,
1590 .decrypt = otx2_cpt_aead_null_decrypt,
1592 .maxauthsize = SHA384_DIGEST_SIZE,
1595 .cra_name = "authenc(hmac(sha512),ecb(cipher_null))",
1596 .cra_driver_name = "cpt_hmac_sha512_ecb_null",
1598 .cra_flags = CRYPTO_ALG_ASYNC,
1599 .cra_ctxsize = sizeof(struct otx2_cpt_aead_ctx),
1600 .cra_priority = 4001,
1602 .cra_module = THIS_MODULE,
1604 .init = otx2_cpt_aead_ecb_null_sha512_init,
1605 .exit = otx2_cpt_aead_exit,
1606 .setkey = otx2_cpt_aead_ecb_null_sha_setkey,
1607 .setauthsize = otx2_cpt_aead_null_set_authsize,
1608 .encrypt = otx2_cpt_aead_null_encrypt,
1609 .decrypt = otx2_cpt_aead_null_decrypt,
1611 .maxauthsize = SHA512_DIGEST_SIZE,
1614 .cra_name = "rfc4106(gcm(aes))",
1615 .cra_driver_name = "cpt_rfc4106_gcm_aes",
1617 .cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1618 .cra_ctxsize = sizeof(struct otx2_cpt_aead_ctx),
1619 .cra_priority = 4001,
1621 .cra_module = THIS_MODULE,
1623 .init = otx2_cpt_aead_gcm_aes_init,
1624 .exit = otx2_cpt_aead_exit,
1625 .setkey = otx2_cpt_aead_gcm_aes_setkey,
1626 .setauthsize = otx2_cpt_aead_gcm_set_authsize,
1627 .encrypt = otx2_cpt_aead_encrypt,
1628 .decrypt = otx2_cpt_aead_decrypt,
1629 .ivsize = AES_GCM_IV_SIZE,
1630 .maxauthsize = AES_GCM_ICV_SIZE,
1633 static inline int cpt_register_algs(void)
1637 for (i = 0; i < ARRAY_SIZE(otx2_cpt_skciphers); i++)
1638 otx2_cpt_skciphers[i].base.cra_flags &= ~CRYPTO_ALG_DEAD;
1640 err = crypto_register_skciphers(otx2_cpt_skciphers,
1641 ARRAY_SIZE(otx2_cpt_skciphers));
1645 for (i = 0; i < ARRAY_SIZE(otx2_cpt_aeads); i++)
1646 otx2_cpt_aeads[i].base.cra_flags &= ~CRYPTO_ALG_DEAD;
1648 err = crypto_register_aeads(otx2_cpt_aeads,
1649 ARRAY_SIZE(otx2_cpt_aeads));
1651 crypto_unregister_skciphers(otx2_cpt_skciphers,
1652 ARRAY_SIZE(otx2_cpt_skciphers));
1659 static inline void cpt_unregister_algs(void)
1661 crypto_unregister_skciphers(otx2_cpt_skciphers,
1662 ARRAY_SIZE(otx2_cpt_skciphers));
1663 crypto_unregister_aeads(otx2_cpt_aeads, ARRAY_SIZE(otx2_cpt_aeads));
1666 static int compare_func(const void *lptr, const void *rptr)
1668 const struct cpt_device_desc *ldesc = (struct cpt_device_desc *) lptr;
1669 const struct cpt_device_desc *rdesc = (struct cpt_device_desc *) rptr;
1671 if (ldesc->dev->devfn < rdesc->dev->devfn)
1673 if (ldesc->dev->devfn > rdesc->dev->devfn)
1678 static void swap_func(void *lptr, void *rptr, int size)
1680 struct cpt_device_desc *ldesc = lptr;
1681 struct cpt_device_desc *rdesc = rptr;
1682 struct cpt_device_desc desc;
1689 int otx2_cpt_crypto_init(struct pci_dev *pdev, struct module *mod,
1690 int num_queues, int num_devices)
1696 count = atomic_read(&se_devices.count);
1697 if (count >= OTX2_CPT_MAX_LFS_NUM) {
1698 dev_err(&pdev->dev, "No space to add a new device\n");
1702 se_devices.desc[count].num_queues = num_queues;
1703 se_devices.desc[count++].dev = pdev;
1704 atomic_inc(&se_devices.count);
1706 if (atomic_read(&se_devices.count) == num_devices &&
1707 is_crypto_registered == false) {
1708 if (cpt_register_algs()) {
1710 "Error in registering crypto algorithms\n");
1714 try_module_get(mod);
1715 is_crypto_registered = true;
1717 sort(se_devices.desc, count, sizeof(struct cpt_device_desc),
1718 compare_func, swap_func);
1721 mutex_unlock(&mutex);
1725 void otx2_cpt_crypto_exit(struct pci_dev *pdev, struct module *mod)
1727 struct cpt_device_table *dev_tbl;
1728 bool dev_found = false;
1733 dev_tbl = &se_devices;
1734 count = atomic_read(&dev_tbl->count);
1735 for (i = 0; i < count; i++) {
1736 if (pdev == dev_tbl->desc[i].dev) {
1737 for (j = i; j < count-1; j++)
1738 dev_tbl->desc[j] = dev_tbl->desc[j+1];
1745 dev_err(&pdev->dev, "%s device not found\n", __func__);
1748 if (atomic_dec_and_test(&se_devices.count)) {
1749 cpt_unregister_algs();
1751 is_crypto_registered = false;
1755 mutex_unlock(&mutex);