.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Cryptographic API. |
---|
3 | 4 | * |
---|
.. | .. |
---|
5 | 6 | * |
---|
6 | 7 | * Copyright (c) 2016 Ryder Lee <ryder.lee@mediatek.com> |
---|
7 | 8 | * |
---|
8 | | - * This program is free software; you can redistribute it and/or modify |
---|
9 | | - * it under the terms of the GNU General Public License version 2 as |
---|
10 | | - * published by the Free Software Foundation. |
---|
11 | | - * |
---|
12 | 9 | * Some ideas are from atmel-aes.c drivers. |
---|
13 | 10 | */ |
---|
14 | 11 | |
---|
15 | 12 | #include <crypto/aes.h> |
---|
16 | 13 | #include <crypto/gcm.h> |
---|
| 14 | +#include <crypto/internal/skcipher.h> |
---|
17 | 15 | #include "mtk-platform.h" |
---|
18 | 16 | |
---|
19 | 17 | #define AES_QUEUE_SIZE 512 |
---|
.. | .. |
---|
26 | 24 | |
---|
27 | 25 | #define AES_CT_CTRL_HDR cpu_to_le32(0x00220000) |
---|
28 | 26 | |
---|
29 | | -/* AES-CBC/ECB/CTR command token */ |
---|
| 27 | +/* AES-CBC/ECB/CTR/OFB/CFB command token */ |
---|
30 | 28 | #define AES_CMD0 cpu_to_le32(0x05000000) |
---|
31 | 29 | #define AES_CMD1 cpu_to_le32(0x2d060000) |
---|
32 | 30 | #define AES_CMD2 cpu_to_le32(0xe4a63806) |
---|
.. | .. |
---|
53 | 51 | /* AES transform information word 1 fields */ |
---|
54 | 52 | #define AES_TFM_ECB cpu_to_le32(0x0 << 0) |
---|
55 | 53 | #define AES_TFM_CBC cpu_to_le32(0x1 << 0) |
---|
| 54 | +#define AES_TFM_OFB cpu_to_le32(0x4 << 0) |
---|
| 55 | +#define AES_TFM_CFB128 cpu_to_le32(0x5 << 0) |
---|
56 | 56 | #define AES_TFM_CTR_INIT cpu_to_le32(0x2 << 0) /* init counter to 1 */ |
---|
57 | 57 | #define AES_TFM_CTR_LOAD cpu_to_le32(0x6 << 0) /* load/reuse counter */ |
---|
58 | 58 | #define AES_TFM_3IV cpu_to_le32(0x7 << 5) /* using IV 0-2 */ |
---|
.. | .. |
---|
61 | 61 | #define AES_TFM_ENC_HASH cpu_to_le32(0x1 << 17) |
---|
62 | 62 | |
---|
63 | 63 | /* AES flags */ |
---|
64 | | -#define AES_FLAGS_CIPHER_MSK GENMASK(2, 0) |
---|
| 64 | +#define AES_FLAGS_CIPHER_MSK GENMASK(4, 0) |
---|
65 | 65 | #define AES_FLAGS_ECB BIT(0) |
---|
66 | 66 | #define AES_FLAGS_CBC BIT(1) |
---|
67 | 67 | #define AES_FLAGS_CTR BIT(2) |
---|
68 | | -#define AES_FLAGS_GCM BIT(3) |
---|
69 | | -#define AES_FLAGS_ENCRYPT BIT(4) |
---|
70 | | -#define AES_FLAGS_BUSY BIT(5) |
---|
| 68 | +#define AES_FLAGS_OFB BIT(3) |
---|
| 69 | +#define AES_FLAGS_CFB128 BIT(4) |
---|
| 70 | +#define AES_FLAGS_GCM BIT(5) |
---|
| 71 | +#define AES_FLAGS_ENCRYPT BIT(6) |
---|
| 72 | +#define AES_FLAGS_BUSY BIT(7) |
---|
71 | 73 | |
---|
72 | 74 | #define AES_AUTH_TAG_ERR cpu_to_le32(BIT(26)) |
---|
73 | 75 | |
---|
.. | .. |
---|
104 | 106 | struct mtk_aes_base_ctx { |
---|
105 | 107 | struct mtk_cryp *cryp; |
---|
106 | 108 | u32 keylen; |
---|
| 109 | + __le32 key[12]; |
---|
107 | 110 | __le32 keymode; |
---|
108 | 111 | |
---|
109 | 112 | mtk_aes_fn start; |
---|
.. | .. |
---|
123 | 126 | struct mtk_aes_ctr_ctx { |
---|
124 | 127 | struct mtk_aes_base_ctx base; |
---|
125 | 128 | |
---|
126 | | - u32 iv[AES_BLOCK_SIZE / sizeof(u32)]; |
---|
| 129 | + __be32 iv[AES_BLOCK_SIZE / sizeof(u32)]; |
---|
127 | 130 | size_t offset; |
---|
128 | 131 | struct scatterlist src[2]; |
---|
129 | 132 | struct scatterlist dst[2]; |
---|
.. | .. |
---|
134 | 137 | |
---|
135 | 138 | u32 authsize; |
---|
136 | 139 | size_t textlen; |
---|
137 | | - |
---|
138 | | - struct crypto_skcipher *ctr; |
---|
139 | 140 | }; |
---|
140 | 141 | |
---|
141 | 142 | struct mtk_aes_drv { |
---|
.. | .. |
---|
241 | 242 | sg->length += dma->remainder; |
---|
242 | 243 | } |
---|
243 | 244 | |
---|
244 | | -static inline void mtk_aes_write_state_le(__le32 *dst, const u32 *src, u32 size) |
---|
245 | | -{ |
---|
246 | | - int i; |
---|
247 | | - |
---|
248 | | - for (i = 0; i < SIZE_IN_WORDS(size); i++) |
---|
249 | | - dst[i] = cpu_to_le32(src[i]); |
---|
250 | | -} |
---|
251 | | - |
---|
252 | | -static inline void mtk_aes_write_state_be(__be32 *dst, const u32 *src, u32 size) |
---|
253 | | -{ |
---|
254 | | - int i; |
---|
255 | | - |
---|
256 | | - for (i = 0; i < SIZE_IN_WORDS(size); i++) |
---|
257 | | - dst[i] = cpu_to_be32(src[i]); |
---|
258 | | -} |
---|
259 | | - |
---|
260 | 245 | static inline int mtk_aes_complete(struct mtk_cryp *cryp, |
---|
261 | 246 | struct mtk_aes_rec *aes, |
---|
262 | 247 | int err) |
---|
.. | .. |
---|
320 | 305 | |
---|
321 | 306 | /* Prepare enough space for authenticated tag */ |
---|
322 | 307 | if (aes->flags & AES_FLAGS_GCM) |
---|
323 | | - res->hdr += AES_BLOCK_SIZE; |
---|
| 308 | + le32_add_cpu(&res->hdr, AES_BLOCK_SIZE); |
---|
324 | 309 | |
---|
325 | 310 | /* |
---|
326 | 311 | * Make sure that all changes to the DMA ring are done before we |
---|
.. | .. |
---|
408 | 393 | return mtk_aes_complete(cryp, aes, -EINVAL); |
---|
409 | 394 | } |
---|
410 | 395 | |
---|
411 | | -/* Initialize transform information of CBC/ECB/CTR mode */ |
---|
| 396 | +/* Initialize transform information of CBC/ECB/CTR/OFB/CFB mode */ |
---|
412 | 397 | static void mtk_aes_info_init(struct mtk_cryp *cryp, struct mtk_aes_rec *aes, |
---|
413 | 398 | size_t len) |
---|
414 | 399 | { |
---|
415 | | - struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq); |
---|
| 400 | + struct skcipher_request *req = skcipher_request_cast(aes->areq); |
---|
416 | 401 | struct mtk_aes_base_ctx *ctx = aes->ctx; |
---|
417 | 402 | struct mtk_aes_info *info = &ctx->info; |
---|
418 | 403 | u32 cnt = 0; |
---|
.. | .. |
---|
437 | 422 | case AES_FLAGS_CTR: |
---|
438 | 423 | info->tfm[1] = AES_TFM_CTR_LOAD; |
---|
439 | 424 | goto ctr; |
---|
440 | | - |
---|
| 425 | + case AES_FLAGS_OFB: |
---|
| 426 | + info->tfm[1] = AES_TFM_OFB; |
---|
| 427 | + break; |
---|
| 428 | + case AES_FLAGS_CFB128: |
---|
| 429 | + info->tfm[1] = AES_TFM_CFB128; |
---|
| 430 | + break; |
---|
441 | 431 | default: |
---|
442 | 432 | /* Should not happen... */ |
---|
443 | 433 | return; |
---|
444 | 434 | } |
---|
445 | 435 | |
---|
446 | | - mtk_aes_write_state_le(info->state + ctx->keylen, req->info, |
---|
447 | | - AES_BLOCK_SIZE); |
---|
| 436 | + memcpy(info->state + ctx->keylen, req->iv, AES_BLOCK_SIZE); |
---|
448 | 437 | ctr: |
---|
449 | | - info->tfm[0] += AES_TFM_SIZE(SIZE_IN_WORDS(AES_BLOCK_SIZE)); |
---|
| 438 | + le32_add_cpu(&info->tfm[0], |
---|
| 439 | + le32_to_cpu(AES_TFM_SIZE(SIZE_IN_WORDS(AES_BLOCK_SIZE)))); |
---|
450 | 440 | info->tfm[1] |= AES_TFM_FULL_IV; |
---|
451 | 441 | info->cmd[cnt++] = AES_CMD2; |
---|
452 | 442 | ecb: |
---|
.. | .. |
---|
528 | 518 | backlog->complete(backlog, -EINPROGRESS); |
---|
529 | 519 | |
---|
530 | 520 | ctx = crypto_tfm_ctx(areq->tfm); |
---|
| 521 | + /* Write key into state buffer */ |
---|
| 522 | + memcpy(ctx->info.state, ctx->key, sizeof(ctx->key)); |
---|
531 | 523 | |
---|
532 | 524 | aes->areq = areq; |
---|
533 | 525 | aes->ctx = ctx; |
---|
.. | .. |
---|
543 | 535 | |
---|
544 | 536 | static int mtk_aes_start(struct mtk_cryp *cryp, struct mtk_aes_rec *aes) |
---|
545 | 537 | { |
---|
546 | | - struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq); |
---|
547 | | - struct mtk_aes_reqctx *rctx = ablkcipher_request_ctx(req); |
---|
| 538 | + struct skcipher_request *req = skcipher_request_cast(aes->areq); |
---|
| 539 | + struct mtk_aes_reqctx *rctx = skcipher_request_ctx(req); |
---|
548 | 540 | |
---|
549 | 541 | mtk_aes_set_mode(aes, rctx); |
---|
550 | 542 | aes->resume = mtk_aes_transfer_complete; |
---|
551 | 543 | |
---|
552 | | - return mtk_aes_dma(cryp, aes, req->src, req->dst, req->nbytes); |
---|
| 544 | + return mtk_aes_dma(cryp, aes, req->src, req->dst, req->cryptlen); |
---|
553 | 545 | } |
---|
554 | 546 | |
---|
555 | 547 | static inline struct mtk_aes_ctr_ctx * |
---|
.. | .. |
---|
562 | 554 | { |
---|
563 | 555 | struct mtk_aes_base_ctx *ctx = aes->ctx; |
---|
564 | 556 | struct mtk_aes_ctr_ctx *cctx = mtk_aes_ctr_ctx_cast(ctx); |
---|
565 | | - struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq); |
---|
| 557 | + struct skcipher_request *req = skcipher_request_cast(aes->areq); |
---|
566 | 558 | struct scatterlist *src, *dst; |
---|
567 | 559 | u32 start, end, ctr, blocks; |
---|
568 | 560 | size_t datalen; |
---|
.. | .. |
---|
570 | 562 | |
---|
571 | 563 | /* Check for transfer completion. */ |
---|
572 | 564 | cctx->offset += aes->total; |
---|
573 | | - if (cctx->offset >= req->nbytes) |
---|
| 565 | + if (cctx->offset >= req->cryptlen) |
---|
574 | 566 | return mtk_aes_transfer_complete(cryp, aes); |
---|
575 | 567 | |
---|
576 | 568 | /* Compute data length. */ |
---|
577 | | - datalen = req->nbytes - cctx->offset; |
---|
| 569 | + datalen = req->cryptlen - cctx->offset; |
---|
578 | 570 | blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE); |
---|
579 | 571 | ctr = be32_to_cpu(cctx->iv[3]); |
---|
580 | 572 | |
---|
.. | .. |
---|
582 | 574 | start = ctr; |
---|
583 | 575 | end = start + blocks - 1; |
---|
584 | 576 | if (end < start) { |
---|
585 | | - ctr |= 0xffffffff; |
---|
| 577 | + ctr = 0xffffffff; |
---|
586 | 578 | datalen = AES_BLOCK_SIZE * -start; |
---|
587 | 579 | fragmented = true; |
---|
588 | 580 | } |
---|
.. | .. |
---|
593 | 585 | scatterwalk_ffwd(cctx->dst, req->dst, cctx->offset)); |
---|
594 | 586 | |
---|
595 | 587 | /* Write IVs into transform state buffer. */ |
---|
596 | | - mtk_aes_write_state_le(ctx->info.state + ctx->keylen, cctx->iv, |
---|
597 | | - AES_BLOCK_SIZE); |
---|
| 588 | + memcpy(ctx->info.state + ctx->keylen, cctx->iv, AES_BLOCK_SIZE); |
---|
598 | 589 | |
---|
599 | 590 | if (unlikely(fragmented)) { |
---|
600 | 591 | /* |
---|
.. | .. |
---|
611 | 602 | static int mtk_aes_ctr_start(struct mtk_cryp *cryp, struct mtk_aes_rec *aes) |
---|
612 | 603 | { |
---|
613 | 604 | struct mtk_aes_ctr_ctx *cctx = mtk_aes_ctr_ctx_cast(aes->ctx); |
---|
614 | | - struct ablkcipher_request *req = ablkcipher_request_cast(aes->areq); |
---|
615 | | - struct mtk_aes_reqctx *rctx = ablkcipher_request_ctx(req); |
---|
| 605 | + struct skcipher_request *req = skcipher_request_cast(aes->areq); |
---|
| 606 | + struct mtk_aes_reqctx *rctx = skcipher_request_ctx(req); |
---|
616 | 607 | |
---|
617 | 608 | mtk_aes_set_mode(aes, rctx); |
---|
618 | 609 | |
---|
619 | | - memcpy(cctx->iv, req->info, AES_BLOCK_SIZE); |
---|
| 610 | + memcpy(cctx->iv, req->iv, AES_BLOCK_SIZE); |
---|
620 | 611 | cctx->offset = 0; |
---|
621 | 612 | aes->total = 0; |
---|
622 | 613 | aes->resume = mtk_aes_ctr_transfer; |
---|
.. | .. |
---|
625 | 616 | } |
---|
626 | 617 | |
---|
627 | 618 | /* Check and set the AES key to transform state buffer */ |
---|
628 | | -static int mtk_aes_setkey(struct crypto_ablkcipher *tfm, |
---|
| 619 | +static int mtk_aes_setkey(struct crypto_skcipher *tfm, |
---|
629 | 620 | const u8 *key, u32 keylen) |
---|
630 | 621 | { |
---|
631 | | - struct mtk_aes_base_ctx *ctx = crypto_ablkcipher_ctx(tfm); |
---|
| 622 | + struct mtk_aes_base_ctx *ctx = crypto_skcipher_ctx(tfm); |
---|
632 | 623 | |
---|
633 | 624 | switch (keylen) { |
---|
634 | 625 | case AES_KEYSIZE_128: |
---|
.. | .. |
---|
642 | 633 | break; |
---|
643 | 634 | |
---|
644 | 635 | default: |
---|
645 | | - crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
---|
646 | 636 | return -EINVAL; |
---|
647 | 637 | } |
---|
648 | 638 | |
---|
649 | 639 | ctx->keylen = SIZE_IN_WORDS(keylen); |
---|
650 | | - mtk_aes_write_state_le(ctx->info.state, (const u32 *)key, keylen); |
---|
| 640 | + memcpy(ctx->key, key, keylen); |
---|
651 | 641 | |
---|
652 | 642 | return 0; |
---|
653 | 643 | } |
---|
654 | 644 | |
---|
655 | | -static int mtk_aes_crypt(struct ablkcipher_request *req, u64 mode) |
---|
| 645 | +static int mtk_aes_crypt(struct skcipher_request *req, u64 mode) |
---|
656 | 646 | { |
---|
657 | | - struct mtk_aes_base_ctx *ctx; |
---|
| 647 | + struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); |
---|
| 648 | + struct mtk_aes_base_ctx *ctx = crypto_skcipher_ctx(skcipher); |
---|
658 | 649 | struct mtk_aes_reqctx *rctx; |
---|
| 650 | + struct mtk_cryp *cryp; |
---|
659 | 651 | |
---|
660 | | - ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req)); |
---|
661 | | - rctx = ablkcipher_request_ctx(req); |
---|
| 652 | + cryp = mtk_aes_find_dev(ctx); |
---|
| 653 | + if (!cryp) |
---|
| 654 | + return -ENODEV; |
---|
| 655 | + |
---|
| 656 | + rctx = skcipher_request_ctx(req); |
---|
662 | 657 | rctx->mode = mode; |
---|
663 | 658 | |
---|
664 | | - return mtk_aes_handle_queue(ctx->cryp, !(mode & AES_FLAGS_ENCRYPT), |
---|
| 659 | + return mtk_aes_handle_queue(cryp, !(mode & AES_FLAGS_ENCRYPT), |
---|
665 | 660 | &req->base); |
---|
666 | 661 | } |
---|
667 | 662 | |
---|
668 | | -static int mtk_aes_ecb_encrypt(struct ablkcipher_request *req) |
---|
| 663 | +static int mtk_aes_ecb_encrypt(struct skcipher_request *req) |
---|
669 | 664 | { |
---|
670 | 665 | return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_ECB); |
---|
671 | 666 | } |
---|
672 | 667 | |
---|
673 | | -static int mtk_aes_ecb_decrypt(struct ablkcipher_request *req) |
---|
| 668 | +static int mtk_aes_ecb_decrypt(struct skcipher_request *req) |
---|
674 | 669 | { |
---|
675 | 670 | return mtk_aes_crypt(req, AES_FLAGS_ECB); |
---|
676 | 671 | } |
---|
677 | 672 | |
---|
678 | | -static int mtk_aes_cbc_encrypt(struct ablkcipher_request *req) |
---|
| 673 | +static int mtk_aes_cbc_encrypt(struct skcipher_request *req) |
---|
679 | 674 | { |
---|
680 | 675 | return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_CBC); |
---|
681 | 676 | } |
---|
682 | 677 | |
---|
683 | | -static int mtk_aes_cbc_decrypt(struct ablkcipher_request *req) |
---|
| 678 | +static int mtk_aes_cbc_decrypt(struct skcipher_request *req) |
---|
684 | 679 | { |
---|
685 | 680 | return mtk_aes_crypt(req, AES_FLAGS_CBC); |
---|
686 | 681 | } |
---|
687 | 682 | |
---|
688 | | -static int mtk_aes_ctr_encrypt(struct ablkcipher_request *req) |
---|
| 683 | +static int mtk_aes_ctr_encrypt(struct skcipher_request *req) |
---|
689 | 684 | { |
---|
690 | 685 | return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_CTR); |
---|
691 | 686 | } |
---|
692 | 687 | |
---|
693 | | -static int mtk_aes_ctr_decrypt(struct ablkcipher_request *req) |
---|
| 688 | +static int mtk_aes_ctr_decrypt(struct skcipher_request *req) |
---|
694 | 689 | { |
---|
695 | 690 | return mtk_aes_crypt(req, AES_FLAGS_CTR); |
---|
696 | 691 | } |
---|
697 | 692 | |
---|
698 | | -static int mtk_aes_cra_init(struct crypto_tfm *tfm) |
---|
| 693 | +static int mtk_aes_ofb_encrypt(struct skcipher_request *req) |
---|
699 | 694 | { |
---|
700 | | - struct mtk_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
---|
701 | | - struct mtk_cryp *cryp = NULL; |
---|
| 695 | + return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_OFB); |
---|
| 696 | +} |
---|
702 | 697 | |
---|
703 | | - cryp = mtk_aes_find_dev(&ctx->base); |
---|
704 | | - if (!cryp) { |
---|
705 | | - pr_err("can't find crypto device\n"); |
---|
706 | | - return -ENODEV; |
---|
707 | | - } |
---|
| 698 | +static int mtk_aes_ofb_decrypt(struct skcipher_request *req) |
---|
| 699 | +{ |
---|
| 700 | + return mtk_aes_crypt(req, AES_FLAGS_OFB); |
---|
| 701 | +} |
---|
708 | 702 | |
---|
709 | | - tfm->crt_ablkcipher.reqsize = sizeof(struct mtk_aes_reqctx); |
---|
| 703 | +static int mtk_aes_cfb_encrypt(struct skcipher_request *req) |
---|
| 704 | +{ |
---|
| 705 | + return mtk_aes_crypt(req, AES_FLAGS_ENCRYPT | AES_FLAGS_CFB128); |
---|
| 706 | +} |
---|
| 707 | + |
---|
| 708 | +static int mtk_aes_cfb_decrypt(struct skcipher_request *req) |
---|
| 709 | +{ |
---|
| 710 | + return mtk_aes_crypt(req, AES_FLAGS_CFB128); |
---|
| 711 | +} |
---|
| 712 | + |
---|
| 713 | +static int mtk_aes_init_tfm(struct crypto_skcipher *tfm) |
---|
| 714 | +{ |
---|
| 715 | + struct mtk_aes_ctx *ctx = crypto_skcipher_ctx(tfm); |
---|
| 716 | + |
---|
| 717 | + crypto_skcipher_set_reqsize(tfm, sizeof(struct mtk_aes_reqctx)); |
---|
710 | 718 | ctx->base.start = mtk_aes_start; |
---|
711 | 719 | return 0; |
---|
712 | 720 | } |
---|
713 | 721 | |
---|
714 | | -static int mtk_aes_ctr_cra_init(struct crypto_tfm *tfm) |
---|
| 722 | +static int mtk_aes_ctr_init_tfm(struct crypto_skcipher *tfm) |
---|
715 | 723 | { |
---|
716 | | - struct mtk_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
---|
717 | | - struct mtk_cryp *cryp = NULL; |
---|
| 724 | + struct mtk_aes_ctx *ctx = crypto_skcipher_ctx(tfm); |
---|
718 | 725 | |
---|
719 | | - cryp = mtk_aes_find_dev(&ctx->base); |
---|
720 | | - if (!cryp) { |
---|
721 | | - pr_err("can't find crypto device\n"); |
---|
722 | | - return -ENODEV; |
---|
723 | | - } |
---|
724 | | - |
---|
725 | | - tfm->crt_ablkcipher.reqsize = sizeof(struct mtk_aes_reqctx); |
---|
| 726 | + crypto_skcipher_set_reqsize(tfm, sizeof(struct mtk_aes_reqctx)); |
---|
726 | 727 | ctx->base.start = mtk_aes_ctr_start; |
---|
727 | 728 | return 0; |
---|
728 | 729 | } |
---|
729 | 730 | |
---|
730 | | -static struct crypto_alg aes_algs[] = { |
---|
| 731 | +static struct skcipher_alg aes_algs[] = { |
---|
731 | 732 | { |
---|
732 | | - .cra_name = "cbc(aes)", |
---|
733 | | - .cra_driver_name = "cbc-aes-mtk", |
---|
734 | | - .cra_priority = 400, |
---|
735 | | - .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
---|
736 | | - CRYPTO_ALG_ASYNC, |
---|
737 | | - .cra_init = mtk_aes_cra_init, |
---|
738 | | - .cra_blocksize = AES_BLOCK_SIZE, |
---|
739 | | - .cra_ctxsize = sizeof(struct mtk_aes_ctx), |
---|
740 | | - .cra_alignmask = 0xf, |
---|
741 | | - .cra_type = &crypto_ablkcipher_type, |
---|
742 | | - .cra_module = THIS_MODULE, |
---|
743 | | - .cra_u.ablkcipher = { |
---|
744 | | - .min_keysize = AES_MIN_KEY_SIZE, |
---|
745 | | - .max_keysize = AES_MAX_KEY_SIZE, |
---|
746 | | - .setkey = mtk_aes_setkey, |
---|
747 | | - .encrypt = mtk_aes_cbc_encrypt, |
---|
748 | | - .decrypt = mtk_aes_cbc_decrypt, |
---|
749 | | - .ivsize = AES_BLOCK_SIZE, |
---|
750 | | - } |
---|
| 733 | + .base.cra_name = "cbc(aes)", |
---|
| 734 | + .base.cra_driver_name = "cbc-aes-mtk", |
---|
| 735 | + .base.cra_priority = 400, |
---|
| 736 | + .base.cra_flags = CRYPTO_ALG_ASYNC, |
---|
| 737 | + .base.cra_blocksize = AES_BLOCK_SIZE, |
---|
| 738 | + .base.cra_ctxsize = sizeof(struct mtk_aes_ctx), |
---|
| 739 | + .base.cra_alignmask = 0xf, |
---|
| 740 | + .base.cra_module = THIS_MODULE, |
---|
| 741 | + |
---|
| 742 | + .min_keysize = AES_MIN_KEY_SIZE, |
---|
| 743 | + .max_keysize = AES_MAX_KEY_SIZE, |
---|
| 744 | + .setkey = mtk_aes_setkey, |
---|
| 745 | + .encrypt = mtk_aes_cbc_encrypt, |
---|
| 746 | + .decrypt = mtk_aes_cbc_decrypt, |
---|
| 747 | + .ivsize = AES_BLOCK_SIZE, |
---|
| 748 | + .init = mtk_aes_init_tfm, |
---|
751 | 749 | }, |
---|
752 | 750 | { |
---|
753 | | - .cra_name = "ecb(aes)", |
---|
754 | | - .cra_driver_name = "ecb-aes-mtk", |
---|
755 | | - .cra_priority = 400, |
---|
756 | | - .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
---|
757 | | - CRYPTO_ALG_ASYNC, |
---|
758 | | - .cra_init = mtk_aes_cra_init, |
---|
759 | | - .cra_blocksize = AES_BLOCK_SIZE, |
---|
760 | | - .cra_ctxsize = sizeof(struct mtk_aes_ctx), |
---|
761 | | - .cra_alignmask = 0xf, |
---|
762 | | - .cra_type = &crypto_ablkcipher_type, |
---|
763 | | - .cra_module = THIS_MODULE, |
---|
764 | | - .cra_u.ablkcipher = { |
---|
765 | | - .min_keysize = AES_MIN_KEY_SIZE, |
---|
766 | | - .max_keysize = AES_MAX_KEY_SIZE, |
---|
767 | | - .setkey = mtk_aes_setkey, |
---|
768 | | - .encrypt = mtk_aes_ecb_encrypt, |
---|
769 | | - .decrypt = mtk_aes_ecb_decrypt, |
---|
770 | | - } |
---|
| 751 | + .base.cra_name = "ecb(aes)", |
---|
| 752 | + .base.cra_driver_name = "ecb-aes-mtk", |
---|
| 753 | + .base.cra_priority = 400, |
---|
| 754 | + .base.cra_flags = CRYPTO_ALG_ASYNC, |
---|
| 755 | + .base.cra_blocksize = AES_BLOCK_SIZE, |
---|
| 756 | + .base.cra_ctxsize = sizeof(struct mtk_aes_ctx), |
---|
| 757 | + .base.cra_alignmask = 0xf, |
---|
| 758 | + .base.cra_module = THIS_MODULE, |
---|
| 759 | + |
---|
| 760 | + .min_keysize = AES_MIN_KEY_SIZE, |
---|
| 761 | + .max_keysize = AES_MAX_KEY_SIZE, |
---|
| 762 | + .setkey = mtk_aes_setkey, |
---|
| 763 | + .encrypt = mtk_aes_ecb_encrypt, |
---|
| 764 | + .decrypt = mtk_aes_ecb_decrypt, |
---|
| 765 | + .init = mtk_aes_init_tfm, |
---|
771 | 766 | }, |
---|
772 | 767 | { |
---|
773 | | - .cra_name = "ctr(aes)", |
---|
774 | | - .cra_driver_name = "ctr-aes-mtk", |
---|
775 | | - .cra_priority = 400, |
---|
776 | | - .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
---|
777 | | - CRYPTO_ALG_ASYNC, |
---|
778 | | - .cra_init = mtk_aes_ctr_cra_init, |
---|
779 | | - .cra_blocksize = 1, |
---|
780 | | - .cra_ctxsize = sizeof(struct mtk_aes_ctr_ctx), |
---|
781 | | - .cra_alignmask = 0xf, |
---|
782 | | - .cra_type = &crypto_ablkcipher_type, |
---|
783 | | - .cra_module = THIS_MODULE, |
---|
784 | | - .cra_u.ablkcipher = { |
---|
785 | | - .min_keysize = AES_MIN_KEY_SIZE, |
---|
786 | | - .max_keysize = AES_MAX_KEY_SIZE, |
---|
787 | | - .ivsize = AES_BLOCK_SIZE, |
---|
788 | | - .setkey = mtk_aes_setkey, |
---|
789 | | - .encrypt = mtk_aes_ctr_encrypt, |
---|
790 | | - .decrypt = mtk_aes_ctr_decrypt, |
---|
791 | | - } |
---|
| 768 | + .base.cra_name = "ctr(aes)", |
---|
| 769 | + .base.cra_driver_name = "ctr-aes-mtk", |
---|
| 770 | + .base.cra_priority = 400, |
---|
| 771 | + .base.cra_flags = CRYPTO_ALG_ASYNC, |
---|
| 772 | + .base.cra_blocksize = 1, |
---|
| 773 | + .base.cra_ctxsize = sizeof(struct mtk_aes_ctx), |
---|
| 774 | + .base.cra_alignmask = 0xf, |
---|
| 775 | + .base.cra_module = THIS_MODULE, |
---|
| 776 | + |
---|
| 777 | + .min_keysize = AES_MIN_KEY_SIZE, |
---|
| 778 | + .max_keysize = AES_MAX_KEY_SIZE, |
---|
| 779 | + .ivsize = AES_BLOCK_SIZE, |
---|
| 780 | + .setkey = mtk_aes_setkey, |
---|
| 781 | + .encrypt = mtk_aes_ctr_encrypt, |
---|
| 782 | + .decrypt = mtk_aes_ctr_decrypt, |
---|
| 783 | + .init = mtk_aes_ctr_init_tfm, |
---|
| 784 | +}, |
---|
| 785 | +{ |
---|
| 786 | + .base.cra_name = "ofb(aes)", |
---|
| 787 | + .base.cra_driver_name = "ofb-aes-mtk", |
---|
| 788 | + .base.cra_priority = 400, |
---|
| 789 | + .base.cra_flags = CRYPTO_ALG_ASYNC, |
---|
| 790 | + .base.cra_blocksize = AES_BLOCK_SIZE, |
---|
| 791 | + .base.cra_ctxsize = sizeof(struct mtk_aes_ctx), |
---|
| 792 | + .base.cra_alignmask = 0xf, |
---|
| 793 | + .base.cra_module = THIS_MODULE, |
---|
| 794 | + |
---|
| 795 | + .min_keysize = AES_MIN_KEY_SIZE, |
---|
| 796 | + .max_keysize = AES_MAX_KEY_SIZE, |
---|
| 797 | + .ivsize = AES_BLOCK_SIZE, |
---|
| 798 | + .setkey = mtk_aes_setkey, |
---|
| 799 | + .encrypt = mtk_aes_ofb_encrypt, |
---|
| 800 | + .decrypt = mtk_aes_ofb_decrypt, |
---|
| 801 | +}, |
---|
| 802 | +{ |
---|
| 803 | + .base.cra_name = "cfb(aes)", |
---|
| 804 | + .base.cra_driver_name = "cfb-aes-mtk", |
---|
| 805 | + .base.cra_priority = 400, |
---|
| 806 | + .base.cra_flags = CRYPTO_ALG_ASYNC, |
---|
| 807 | + .base.cra_blocksize = 1, |
---|
| 808 | + .base.cra_ctxsize = sizeof(struct mtk_aes_ctx), |
---|
| 809 | + .base.cra_alignmask = 0xf, |
---|
| 810 | + .base.cra_module = THIS_MODULE, |
---|
| 811 | + |
---|
| 812 | + .min_keysize = AES_MIN_KEY_SIZE, |
---|
| 813 | + .max_keysize = AES_MAX_KEY_SIZE, |
---|
| 814 | + .ivsize = AES_BLOCK_SIZE, |
---|
| 815 | + .setkey = mtk_aes_setkey, |
---|
| 816 | + .encrypt = mtk_aes_cfb_encrypt, |
---|
| 817 | + .decrypt = mtk_aes_cfb_decrypt, |
---|
792 | 818 | }, |
---|
793 | 819 | }; |
---|
794 | 820 | |
---|
.. | .. |
---|
805 | 831 | static int mtk_aes_gcm_tag_verify(struct mtk_cryp *cryp, |
---|
806 | 832 | struct mtk_aes_rec *aes) |
---|
807 | 833 | { |
---|
808 | | - u32 status = cryp->ring[aes->id]->res_prev->ct; |
---|
| 834 | + __le32 status = cryp->ring[aes->id]->res_prev->ct; |
---|
809 | 835 | |
---|
810 | 836 | return mtk_aes_complete(cryp, aes, (status & AES_AUTH_TAG_ERR) ? |
---|
811 | 837 | -EBADMSG : 0); |
---|
.. | .. |
---|
823 | 849 | u32 ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req)); |
---|
824 | 850 | u32 cnt = 0; |
---|
825 | 851 | |
---|
826 | | - ctx->ct_hdr = AES_CT_CTRL_HDR | len; |
---|
| 852 | + ctx->ct_hdr = AES_CT_CTRL_HDR | cpu_to_le32(len); |
---|
827 | 853 | |
---|
828 | 854 | info->cmd[cnt++] = AES_GCM_CMD0 | cpu_to_le32(req->assoclen); |
---|
829 | 855 | info->cmd[cnt++] = AES_GCM_CMD1 | cpu_to_le32(req->assoclen); |
---|
.. | .. |
---|
846 | 872 | info->tfm[1] = AES_TFM_CTR_INIT | AES_TFM_IV_CTR_MODE | AES_TFM_3IV | |
---|
847 | 873 | AES_TFM_ENC_HASH; |
---|
848 | 874 | |
---|
849 | | - mtk_aes_write_state_le(info->state + ctx->keylen + SIZE_IN_WORDS( |
---|
850 | | - AES_BLOCK_SIZE), (const u32 *)req->iv, ivsize); |
---|
| 875 | + memcpy(info->state + ctx->keylen + SIZE_IN_WORDS(AES_BLOCK_SIZE), |
---|
| 876 | + req->iv, ivsize); |
---|
851 | 877 | } |
---|
852 | 878 | |
---|
853 | 879 | static int mtk_aes_gcm_dma(struct mtk_cryp *cryp, struct mtk_aes_rec *aes, |
---|
.. | .. |
---|
908 | 934 | aes->resume = mtk_aes_transfer_complete; |
---|
909 | 935 | /* Compute total process length. */ |
---|
910 | 936 | aes->total = len + gctx->authsize; |
---|
911 | | - /* Compute text length. */ |
---|
912 | | - gctx->textlen = req->cryptlen; |
---|
913 | 937 | /* Hardware will append authenticated tag to output buffer */ |
---|
914 | 938 | scatterwalk_map_and_copy(tag, req->dst, len, gctx->authsize, 1); |
---|
915 | 939 | } else { |
---|
916 | 940 | aes->resume = mtk_aes_gcm_tag_verify; |
---|
917 | 941 | aes->total = len; |
---|
918 | | - gctx->textlen = req->cryptlen - gctx->authsize; |
---|
919 | 942 | } |
---|
920 | 943 | |
---|
921 | 944 | return mtk_aes_gcm_dma(cryp, aes, req->src, req->dst, len); |
---|
.. | .. |
---|
926 | 949 | struct mtk_aes_base_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
---|
927 | 950 | struct mtk_aes_gcm_ctx *gctx = mtk_aes_gcm_ctx_cast(ctx); |
---|
928 | 951 | struct mtk_aes_reqctx *rctx = aead_request_ctx(req); |
---|
| 952 | + struct mtk_cryp *cryp; |
---|
| 953 | + bool enc = !!(mode & AES_FLAGS_ENCRYPT); |
---|
| 954 | + |
---|
| 955 | + cryp = mtk_aes_find_dev(ctx); |
---|
| 956 | + if (!cryp) |
---|
| 957 | + return -ENODEV; |
---|
| 958 | + |
---|
| 959 | + /* Compute text length. */ |
---|
| 960 | + gctx->textlen = req->cryptlen - (enc ? 0 : gctx->authsize); |
---|
929 | 961 | |
---|
930 | 962 | /* Empty messages are not supported yet */ |
---|
931 | 963 | if (!gctx->textlen && !req->assoclen) |
---|
.. | .. |
---|
933 | 965 | |
---|
934 | 966 | rctx->mode = AES_FLAGS_GCM | mode; |
---|
935 | 967 | |
---|
936 | | - return mtk_aes_handle_queue(ctx->cryp, !!(mode & AES_FLAGS_ENCRYPT), |
---|
937 | | - &req->base); |
---|
| 968 | + return mtk_aes_handle_queue(cryp, enc, &req->base); |
---|
938 | 969 | } |
---|
939 | 970 | |
---|
940 | 971 | /* |
---|
.. | .. |
---|
946 | 977 | u32 keylen) |
---|
947 | 978 | { |
---|
948 | 979 | struct mtk_aes_base_ctx *ctx = crypto_aead_ctx(aead); |
---|
949 | | - struct mtk_aes_gcm_ctx *gctx = mtk_aes_gcm_ctx_cast(ctx); |
---|
950 | | - struct crypto_skcipher *ctr = gctx->ctr; |
---|
951 | | - struct { |
---|
952 | | - u32 hash[4]; |
---|
953 | | - u8 iv[8]; |
---|
954 | | - |
---|
955 | | - struct crypto_wait wait; |
---|
956 | | - |
---|
957 | | - struct scatterlist sg[1]; |
---|
958 | | - struct skcipher_request req; |
---|
959 | | - } *data; |
---|
| 980 | + union { |
---|
| 981 | + u32 x32[SIZE_IN_WORDS(AES_BLOCK_SIZE)]; |
---|
| 982 | + u8 x8[AES_BLOCK_SIZE]; |
---|
| 983 | + } hash = {}; |
---|
| 984 | + struct crypto_aes_ctx aes_ctx; |
---|
960 | 985 | int err; |
---|
| 986 | + int i; |
---|
961 | 987 | |
---|
962 | 988 | switch (keylen) { |
---|
963 | 989 | case AES_KEYSIZE_128: |
---|
.. | .. |
---|
971 | 997 | break; |
---|
972 | 998 | |
---|
973 | 999 | default: |
---|
974 | | - crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN); |
---|
975 | 1000 | return -EINVAL; |
---|
976 | 1001 | } |
---|
977 | 1002 | |
---|
978 | 1003 | ctx->keylen = SIZE_IN_WORDS(keylen); |
---|
979 | 1004 | |
---|
980 | | - /* Same as crypto_gcm_setkey() from crypto/gcm.c */ |
---|
981 | | - crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK); |
---|
982 | | - crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) & |
---|
983 | | - CRYPTO_TFM_REQ_MASK); |
---|
984 | | - err = crypto_skcipher_setkey(ctr, key, keylen); |
---|
985 | | - crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) & |
---|
986 | | - CRYPTO_TFM_RES_MASK); |
---|
| 1005 | + err = aes_expandkey(&aes_ctx, key, keylen); |
---|
987 | 1006 | if (err) |
---|
988 | 1007 | return err; |
---|
989 | 1008 | |
---|
990 | | - data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr), |
---|
991 | | - GFP_KERNEL); |
---|
992 | | - if (!data) |
---|
993 | | - return -ENOMEM; |
---|
| 1009 | + aes_encrypt(&aes_ctx, hash.x8, hash.x8); |
---|
| 1010 | + memzero_explicit(&aes_ctx, sizeof(aes_ctx)); |
---|
994 | 1011 | |
---|
995 | | - crypto_init_wait(&data->wait); |
---|
996 | | - sg_init_one(data->sg, &data->hash, AES_BLOCK_SIZE); |
---|
997 | | - skcipher_request_set_tfm(&data->req, ctr); |
---|
998 | | - skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP | |
---|
999 | | - CRYPTO_TFM_REQ_MAY_BACKLOG, |
---|
1000 | | - crypto_req_done, &data->wait); |
---|
1001 | | - skcipher_request_set_crypt(&data->req, data->sg, data->sg, |
---|
1002 | | - AES_BLOCK_SIZE, data->iv); |
---|
| 1012 | + memcpy(ctx->key, key, keylen); |
---|
1003 | 1013 | |
---|
1004 | | - err = crypto_wait_req(crypto_skcipher_encrypt(&data->req), |
---|
1005 | | - &data->wait); |
---|
1006 | | - if (err) |
---|
1007 | | - goto out; |
---|
| 1014 | + /* Why do we need to do this? */ |
---|
| 1015 | + for (i = 0; i < SIZE_IN_WORDS(AES_BLOCK_SIZE); i++) |
---|
| 1016 | + hash.x32[i] = swab32(hash.x32[i]); |
---|
1008 | 1017 | |
---|
1009 | | - /* Write key into state buffer */ |
---|
1010 | | - mtk_aes_write_state_le(ctx->info.state, (const u32 *)key, keylen); |
---|
1011 | | - /* Write key(H) into state buffer */ |
---|
1012 | | - mtk_aes_write_state_be(ctx->info.state + ctx->keylen, data->hash, |
---|
1013 | | - AES_BLOCK_SIZE); |
---|
1014 | | -out: |
---|
1015 | | - kzfree(data); |
---|
1016 | | - return err; |
---|
| 1018 | + memcpy(ctx->key + ctx->keylen, &hash, AES_BLOCK_SIZE); |
---|
| 1019 | + |
---|
| 1020 | + return 0; |
---|
1017 | 1021 | } |
---|
1018 | 1022 | |
---|
1019 | 1023 | static int mtk_aes_gcm_setauthsize(struct crypto_aead *aead, |
---|
.. | .. |
---|
1049 | 1053 | static int mtk_aes_gcm_init(struct crypto_aead *aead) |
---|
1050 | 1054 | { |
---|
1051 | 1055 | struct mtk_aes_gcm_ctx *ctx = crypto_aead_ctx(aead); |
---|
1052 | | - struct mtk_cryp *cryp = NULL; |
---|
1053 | | - |
---|
1054 | | - cryp = mtk_aes_find_dev(&ctx->base); |
---|
1055 | | - if (!cryp) { |
---|
1056 | | - pr_err("can't find crypto device\n"); |
---|
1057 | | - return -ENODEV; |
---|
1058 | | - } |
---|
1059 | | - |
---|
1060 | | - ctx->ctr = crypto_alloc_skcipher("ctr(aes)", 0, |
---|
1061 | | - CRYPTO_ALG_ASYNC); |
---|
1062 | | - if (IS_ERR(ctx->ctr)) { |
---|
1063 | | - pr_err("Error allocating ctr(aes)\n"); |
---|
1064 | | - return PTR_ERR(ctx->ctr); |
---|
1065 | | - } |
---|
1066 | 1056 | |
---|
1067 | 1057 | crypto_aead_set_reqsize(aead, sizeof(struct mtk_aes_reqctx)); |
---|
1068 | 1058 | ctx->base.start = mtk_aes_gcm_start; |
---|
1069 | 1059 | return 0; |
---|
1070 | | -} |
---|
1071 | | - |
---|
1072 | | -static void mtk_aes_gcm_exit(struct crypto_aead *aead) |
---|
1073 | | -{ |
---|
1074 | | - struct mtk_aes_gcm_ctx *ctx = crypto_aead_ctx(aead); |
---|
1075 | | - |
---|
1076 | | - crypto_free_skcipher(ctx->ctr); |
---|
1077 | 1060 | } |
---|
1078 | 1061 | |
---|
1079 | 1062 | static struct aead_alg aes_gcm_alg = { |
---|
.. | .. |
---|
1082 | 1065 | .encrypt = mtk_aes_gcm_encrypt, |
---|
1083 | 1066 | .decrypt = mtk_aes_gcm_decrypt, |
---|
1084 | 1067 | .init = mtk_aes_gcm_init, |
---|
1085 | | - .exit = mtk_aes_gcm_exit, |
---|
1086 | 1068 | .ivsize = GCM_AES_IV_SIZE, |
---|
1087 | 1069 | .maxauthsize = AES_BLOCK_SIZE, |
---|
1088 | 1070 | |
---|
.. | .. |
---|
1201 | 1183 | crypto_unregister_aead(&aes_gcm_alg); |
---|
1202 | 1184 | |
---|
1203 | 1185 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) |
---|
1204 | | - crypto_unregister_alg(&aes_algs[i]); |
---|
| 1186 | + crypto_unregister_skcipher(&aes_algs[i]); |
---|
1205 | 1187 | } |
---|
1206 | 1188 | |
---|
1207 | 1189 | static int mtk_aes_register_algs(void) |
---|
.. | .. |
---|
1209 | 1191 | int err, i; |
---|
1210 | 1192 | |
---|
1211 | 1193 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) { |
---|
1212 | | - err = crypto_register_alg(&aes_algs[i]); |
---|
| 1194 | + err = crypto_register_skcipher(&aes_algs[i]); |
---|
1213 | 1195 | if (err) |
---|
1214 | 1196 | goto err_aes_algs; |
---|
1215 | 1197 | } |
---|
.. | .. |
---|
1222 | 1204 | |
---|
1223 | 1205 | err_aes_algs: |
---|
1224 | 1206 | for (; i--; ) |
---|
1225 | | - crypto_unregister_alg(&aes_algs[i]); |
---|
| 1207 | + crypto_unregister_skcipher(&aes_algs[i]); |
---|
1226 | 1208 | |
---|
1227 | 1209 | return err; |
---|
1228 | 1210 | } |
---|