926-crypto-eip93-use-AES-fallback-for-small-requests.patch 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. From 46d673033b7f6974d0bf5696ff8365fd412cd646 Mon Sep 17 00:00:00 2001
  2. From: Aviana Cruz <[email protected]>
  3. Date: Mon, 20 Jun 2022 21:55:45 +0800
  4. Subject: [PATCH] crypto: eip93 - use AES fallback for small requests
  5. For data blocks smaller than 1024, software crypto is faster.
  6. Signed-off-by: Aviana Cruz <[email protected]>
  7. ---
  8. --- a/drivers/crypto/inside-secure/eip93/Kconfig
  9. +++ b/drivers/crypto/inside-secure/eip93/Kconfig
  10. @@ -18,3 +18,19 @@ config CRYPTO_DEV_EIP93
  11. CTR crypto. Also provide DES and 3DES ECB and CBC.
  12. Also provide AEAD authenc(hmac(x), cipher(y)) for supported algo.
  13. +
  14. +config CRYPTO_DEV_EIP93_GENERIC_SW_MAX_LEN
  15. + int "Max skcipher software fallback length"
  16. + depends on CRYPTO_DEV_EIP93
  17. + default 256
  18. + help
  19. + Max length of crypt request which
  20. + will fallback to software crypt of skcipher *except* AES-128.
  21. +
  22. +config CRYPTO_DEV_EIP93_AES_128_SW_MAX_LEN
  23. + int "Max AES-128 skcipher software fallback length"
  24. + depends on CRYPTO_DEV_EIP93
  25. + default 512
  26. + help
  27. + Max length of crypt request which
  28. + will fallback to software crypt of AES-128 skcipher.
  29. --- a/drivers/crypto/inside-secure/eip93/eip93-cipher.c
  30. +++ b/drivers/crypto/inside-secure/eip93/eip93-cipher.c
  31. @@ -30,6 +30,13 @@ void eip93_skcipher_handle_result(struct
  32. skcipher_request_complete(req, err);
  33. }
  34. +static inline bool eip93_skcipher_is_fallback(const struct crypto_tfm *tfm,
  35. + u32 flags)
  36. +{
  37. + return (tfm->__crt_alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) &&
  38. + !IS_RFC3686(flags);
  39. +}
  40. +
  41. static int eip93_skcipher_send_req(struct crypto_async_request *async)
  42. {
  43. struct skcipher_request *req = skcipher_request_cast(async);
  44. @@ -52,12 +59,21 @@ static int eip93_skcipher_cra_init(struc
  45. struct eip93_crypto_ctx *ctx = crypto_tfm_ctx(tfm);
  46. struct eip93_alg_template *tmpl = container_of(tfm->__crt_alg,
  47. struct eip93_alg_template, alg.skcipher.base);
  48. -
  49. - crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
  50. - sizeof(struct eip93_cipher_reqctx));
  51. + bool fallback = eip93_skcipher_is_fallback(tfm, tmpl->flags);
  52. memset(ctx, 0, sizeof(*ctx));
  53. + if (fallback) {
  54. + ctx->fallback = crypto_alloc_skcipher(
  55. + crypto_tfm_alg_name(tfm), 0, CRYPTO_ALG_NEED_FALLBACK);
  56. + if (IS_ERR(ctx->fallback))
  57. + return PTR_ERR(ctx->fallback);
  58. + }
  59. +
  60. + crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
  61. + sizeof(struct eip93_cipher_reqctx) + (fallback ?
  62. + crypto_skcipher_reqsize(ctx->fallback) : 0));
  63. +
  64. ctx->eip93 = tmpl->eip93;
  65. ctx->type = tmpl->type;
  66. @@ -75,6 +91,8 @@ static void eip93_skcipher_cra_exit(stru
  67. dma_unmap_single(ctx->eip93->dev, ctx->sa_record_base,
  68. sizeof(*ctx->sa_record), DMA_TO_DEVICE);
  69. kfree(ctx->sa_record);
  70. +
  71. + crypto_free_skcipher(ctx->fallback);
  72. }
  73. static int eip93_skcipher_setkey(struct crypto_skcipher *ctfm, const u8 *key,
  74. @@ -117,6 +135,14 @@ static int eip93_skcipher_setkey(struct
  75. if (flags & EIP93_ALG_AES) {
  76. struct crypto_aes_ctx aes;
  77. + bool fallback = eip93_skcipher_is_fallback(tfm, flags);
  78. +
  79. + if (fallback && !IS_RFC3686(flags)) {
  80. + ret = crypto_skcipher_setkey(ctx->fallback, key,
  81. + keylen);
  82. + if (ret)
  83. + return ret;
  84. + }
  85. ctx->blksize = AES_BLOCK_SIZE;
  86. ret = aes_expandkey(&aes, key, keylen);
  87. @@ -133,12 +159,13 @@ static int eip93_skcipher_setkey(struct
  88. return 0;
  89. }
  90. -static int eip93_skcipher_crypt(struct skcipher_request *req)
  91. +static int eip93_skcipher_crypt(struct skcipher_request *req, bool encrypt)
  92. {
  93. struct eip93_cipher_reqctx *rctx = skcipher_request_ctx(req);
  94. struct crypto_async_request *async = &req->base;
  95. struct eip93_crypto_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  96. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  97. + bool fallback = eip93_skcipher_is_fallback(req->base.tfm, rctx->flags);
  98. int ret;
  99. if (!req->cryptlen)
  100. @@ -153,6 +180,21 @@ static int eip93_skcipher_crypt(struct s
  101. crypto_skcipher_blocksize(skcipher)))
  102. return -EINVAL;
  103. + if (fallback &&
  104. + req->cryptlen <= (AES_KEYSIZE_128 ?
  105. + CONFIG_CRYPTO_DEV_EIP93_AES_128_SW_MAX_LEN :
  106. + CONFIG_CRYPTO_DEV_EIP93_GENERIC_SW_MAX_LEN)) {
  107. + skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback);
  108. + skcipher_request_set_callback(&rctx->fallback_req,
  109. + req->base.flags,
  110. + req->base.complete,
  111. + req->base.data);
  112. + skcipher_request_set_crypt(&rctx->fallback_req, req->src,
  113. + req->dst, req->cryptlen, req->iv);
  114. + return encrypt ? crypto_skcipher_encrypt(&rctx->fallback_req) :
  115. + crypto_skcipher_decrypt(&rctx->fallback_req);
  116. + }
  117. +
  118. ctx->sa_record_base = dma_map_single(ctx->eip93->dev, ctx->sa_record,
  119. sizeof(*ctx->sa_record), DMA_TO_DEVICE);
  120. ret = dma_mapping_error(ctx->eip93->dev, ctx->sa_record_base);
  121. @@ -181,7 +223,7 @@ static int eip93_skcipher_encrypt(struct
  122. rctx->flags = tmpl->flags;
  123. rctx->flags |= EIP93_ENCRYPT;
  124. - return eip93_skcipher_crypt(req);
  125. + return eip93_skcipher_crypt(req, true);
  126. }
  127. static int eip93_skcipher_decrypt(struct skcipher_request *req)
  128. @@ -196,7 +238,7 @@ static int eip93_skcipher_decrypt(struct
  129. rctx->flags = tmpl->flags;
  130. rctx->flags |= EIP93_DECRYPT;
  131. - return eip93_skcipher_crypt(req);
  132. + return eip93_skcipher_crypt(req, false);
  133. }
  134. /* Available algorithms in this module */
  135. --- a/drivers/crypto/inside-secure/eip93/eip93-cipher.h
  136. +++ b/drivers/crypto/inside-secure/eip93/eip93-cipher.h
  137. @@ -22,6 +22,7 @@ struct eip93_crypto_ctx {
  138. unsigned int assoclen;
  139. bool set_assoc;
  140. enum eip93_alg_type type;
  141. + struct crypto_skcipher *fallback;
  142. };
  143. struct eip93_cipher_reqctx {
  144. @@ -42,6 +43,7 @@ struct eip93_cipher_reqctx {
  145. int dst_nents;
  146. struct sa_state *sa_state_ctr;
  147. dma_addr_t sa_state_ctr_base;
  148. + struct skcipher_request fallback_req;
  149. };
  150. int check_valid_request(struct eip93_cipher_reqctx *rctx);