Skip to contentSkip to conversation

crypto: aead - un-inline encrypt and decrypt functions

PATCH In review Eric Biggers 3 participants 3 messages Jun 3, 2019 – Jun 13, 2019 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: aead - un-inline encrypt and decrypt functions

From: Eric Biggers <[email protected]>

crypto_aead_encrypt() and crypto_aead_decrypt() have grown to be more than a single indirect function call. They now also check whether a key has been set, the decryption side checks whether the input is at least as long as the authentication tag length, and with CONFIG_CRYPTO_STATS=y they also update the crypto statistics. That can add up to a lot of bloat at every call site. Moreover, these always involve a function call anyway, which greatly limits the benefits of inlining.

So change them to be non-inline.

---
 crypto/aead.c         | 36 ++++++++++++++++++++++++++++++++++++
 include/crypto/aead.h | 34 ++--------------------------------
 2 files changed, 38 insertions(+), 32 deletions(-)
crypto/aead.c +36 -0
index 4908b5e846f0e..fc1d7ad8a487d 100644
@@ -89,6 +89,42 @@ int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
 }
 EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
 
+int crypto_aead_encrypt(struct aead_request *req)
+{
+	struct crypto_aead *aead = crypto_aead_reqtfm(req);
+	struct crypto_alg *alg = aead->base.__crt_alg;
+	unsigned int cryptlen = req->cryptlen;
+	int ret;
+
+	crypto_stats_get(alg);
+	if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
+		ret = -ENOKEY;
+	else
+		ret = crypto_aead_alg(aead)->encrypt(req);
+	crypto_stats_aead_encrypt(cryptlen, alg, ret);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(crypto_aead_encrypt);
+
+int crypto_aead_decrypt(struct aead_request *req)
+{
+	struct crypto_aead *aead = crypto_aead_reqtfm(req);
+	struct crypto_alg *alg = aead->base.__crt_alg;
+	unsigned int cryptlen = req->cryptlen;
+	int ret;
+
+	crypto_stats_get(alg);
+	if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
+		ret = -ENOKEY;
+	else if (req->cryptlen < crypto_aead_authsize(aead))
+		ret = -EINVAL;
+	else
+		ret = crypto_aead_alg(aead)->decrypt(req);
+	crypto_stats_aead_decrypt(cryptlen, alg, ret);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(crypto_aead_decrypt);
+
 static void crypto_aead_exit_tfm(struct crypto_tfm *tfm)
 {
 	struct crypto_aead *aead = __crypto_aead_cast(tfm);
include/crypto/aead.h +2 -32
index 9ad595f97c65a..020d581373abd 100644
@@ -322,21 +322,7 @@ static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
  *
  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  */
-static inline int crypto_aead_encrypt(struct aead_request *req)
-{
-	struct crypto_aead *aead = crypto_aead_reqtfm(req);
-	struct crypto_alg *alg = aead->base.__crt_alg;
-	unsigned int cryptlen = req->cryptlen;
-	int ret;
-
-	crypto_stats_get(alg);
-	if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
-		ret = -ENOKEY;
-	else
-		ret = crypto_aead_alg(aead)->encrypt(req);
-	crypto_stats_aead_encrypt(cryptlen, alg, ret);
-	return ret;
-}
+int crypto_aead_encrypt(struct aead_request *req);
 
 /**
  * crypto_aead_decrypt() - decrypt ciphertext
@@ -360,23 +346,7 @@ static inline int crypto_aead_encrypt(struct aead_request *req)
  *	   integrity of the ciphertext or the associated data was violated);
  *	   < 0 if an error occurred.
  */
-static inline int crypto_aead_decrypt(struct aead_request *req)
-{
-	struct crypto_aead *aead = crypto_aead_reqtfm(req);
-	struct crypto_alg *alg = aead->base.__crt_alg;
-	unsigned int cryptlen = req->cryptlen;
-	int ret;
-
-	crypto_stats_get(alg);
-	if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
-		ret = -ENOKEY;
-	else if (req->cryptlen < crypto_aead_authsize(aead))
-		ret = -EINVAL;
-	else
-		ret = crypto_aead_alg(aead)->decrypt(req);
-	crypto_stats_aead_decrypt(cryptlen, alg, ret);
-	return ret;
-}
+int crypto_aead_decrypt(struct aead_request *req);
 
 /**
  * DOC: Asynchronous AEAD Request Handle
Signature · 1 line
2.21.0
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Eric Biggers

From: Eric Biggers <[email protected]>

crypto_aead_encrypt() and crypto_aead_decrypt() have grown to be more than a single indirect function call. They now also check whether a key has been set, the decryption side checks whether the input is at least as long as the authentication tag length, and with CONFIG_CRYPTO_STATS=y they also update the crypto statistics. That can add up to a lot of bloat at every call site. Moreover, these always involve a function call anyway, which greatly limits the benefits of inlining.

Quoted 3 lines

So change them to be non-inline.

Quoted 107 lines
---
 crypto/aead.c         | 36 ++++++++++++++++++++++++++++++++++++
 include/crypto/aead.h | 34 ++--------------------------------
 2 files changed, 38 insertions(+), 32 deletions(-)
crypto/aead.c +36 -0
index 4908b5e846f0e..fc1d7ad8a487d 100644
@@ -89,6 +89,42 @@ int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
 }
 EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
 
+int crypto_aead_encrypt(struct aead_request *req)
+{
+       struct crypto_aead *aead = crypto_aead_reqtfm(req);
+       struct crypto_alg *alg = aead->base.__crt_alg;
+       unsigned int cryptlen = req->cryptlen;
+       int ret;
+
+       crypto_stats_get(alg);
+       if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
+               ret = -ENOKEY;
+       else
+               ret = crypto_aead_alg(aead)->encrypt(req);
+       crypto_stats_aead_encrypt(cryptlen, alg, ret);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(crypto_aead_encrypt);
+
+int crypto_aead_decrypt(struct aead_request *req)
+{
+       struct crypto_aead *aead = crypto_aead_reqtfm(req);
+       struct crypto_alg *alg = aead->base.__crt_alg;
+       unsigned int cryptlen = req->cryptlen;
+       int ret;
+
+       crypto_stats_get(alg);
+       if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
+               ret = -ENOKEY;
+       else if (req->cryptlen < crypto_aead_authsize(aead))
+               ret = -EINVAL;
+       else
+               ret = crypto_aead_alg(aead)->decrypt(req);
+       crypto_stats_aead_decrypt(cryptlen, alg, ret);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(crypto_aead_decrypt);
+
 static void crypto_aead_exit_tfm(struct crypto_tfm *tfm)
 {
        struct crypto_aead *aead = __crypto_aead_cast(tfm);
include/crypto/aead.h +2 -33
index 9ad595f97c65a..020d581373abd 100644
@@ -322,21 +322,7 @@ static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
  *
  * Return: 0 if the cipher operation was successful; < 0 if an error occurred
  */
-static inline int crypto_aead_encrypt(struct aead_request *req)
-{
-       struct crypto_aead *aead = crypto_aead_reqtfm(req);
-       struct crypto_alg *alg = aead->base.__crt_alg;
-       unsigned int cryptlen = req->cryptlen;
-       int ret;
-
-       crypto_stats_get(alg);
-       if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
-               ret = -ENOKEY;
-       else
-               ret = crypto_aead_alg(aead)->encrypt(req);
-       crypto_stats_aead_encrypt(cryptlen, alg, ret);
-       return ret;
-}
+int crypto_aead_encrypt(struct aead_request *req);
 
 /**
  * crypto_aead_decrypt() - decrypt ciphertext
@@ -360,23 +346,7 @@ static inline int crypto_aead_encrypt(struct aead_request *req)
  *        integrity of the ciphertext or the associated data was violated);
  *        < 0 if an error occurred.
  */
-static inline int crypto_aead_decrypt(struct aead_request *req)
-{
-       struct crypto_aead *aead = crypto_aead_reqtfm(req);
-       struct crypto_alg *alg = aead->base.__crt_alg;
-       unsigned int cryptlen = req->cryptlen;
-       int ret;
-
-       crypto_stats_get(alg);
-       if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
-               ret = -ENOKEY;
-       else if (req->cryptlen < crypto_aead_authsize(aead))
-               ret = -EINVAL;
-       else
-               ret = crypto_aead_alg(aead)->decrypt(req);
-       crypto_stats_aead_decrypt(cryptlen, alg, ret);
-       return ret;
-}
+int crypto_aead_decrypt(struct aead_request *req);
 
 /**
  * DOC: Asynchronous AEAD Request Handle
--

2.21.0

maintainer
#2 lore raw
In reply to Eric Biggers
Quoted 10 lines · Eric Biggers

From: Eric Biggers <[email protected]>

crypto_aead_encrypt() and crypto_aead_decrypt() have grown to be more than a single indirect function call. They now also check whether a key has been set, the decryption side checks whether the input is at least as long as the authentication tag length, and with CONFIG_CRYPTO_STATS=y they also update the crypto statistics. That can add up to a lot of bloat at every call site. Moreover, these always involve a function call anyway, which greatly limits the benefits of inlining.

Quoted 7 lines

So change them to be non-inline.

---
crypto/aead.c | 36 ++++++++++++++++++++++++++++++++++++
include/crypto/aead.h | 34 ++--------------------------------

2 files changed, 38 insertions(+), 32 deletions(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt