Skip to contentSkip to conversation

crypto: api - remove crypto_tfm::crt_u

PATCH In review Eric Biggers 3 participants 5 messages Dec 2, 2019 – Dec 11, 2019 Crypto API

lore
Share & tools
lore
#0 lore raw

cover crypto: api - remove crypto_tfm::crt_u

This series removes the per-algorithm-type union from struct crypto_tfm now that its only remaining users are the "compress" and "cipher" algorithm types, and it's not really needed for them.

This shrinks every crypto transform for every algorithm by 28 bytes on 64-bit platforms (12 bytes on 32-bit), and also removes some code.

Note that the new-style strongly-typed algorithms (i.e. everything other than "compress" and "cipher") don't need crt_u, since they embed struct crypto_tfm in a per-algorithm-type custom struct instead.

Eric Biggers (2):
crypto: compress - remove crt_u.compress (struct compress_tfm)
crypto: cipher - remove crt_u.cipher (struct cipher_tfm)

 crypto/api.c           | 15 +------
 crypto/cipher.c        | 92 +++++++++++++++++-------------------------
 crypto/compress.c      | 31 ++++++--------
 crypto/internal.h      |  3 --
 include/linux/crypto.h | 91 ++++++-----------------------------------
 5 files changed, 61 insertions(+), 171 deletions(-)
Signature · 1 line
2.24.0.393.g34dc348eaf-goog
#1 lore raw

PATCH 1/2 crypto: compress - remove crt_u.compress (struct compress_tfm)

From: Eric Biggers <[email protected]>

crt_u.compress (struct compress_tfm) is pointless because its two fields, ->cot_compress() and ->cot_decompress(), always point to crypto_compress() and crypto_decompress().

Remove this pointless indirection, and just make crypto_comp_compress() and crypto_comp_decompress() be direct calls to what used to be crypto_compress() and crypto_decompress().

Also remove the unused function crypto_comp_cast().

---
 crypto/api.c           |  2 +-
 crypto/compress.c      | 31 ++++++++++++------------------
 crypto/internal.h      |  1 -
 include/linux/crypto.h | 43 ++++++------------------------------------
 4 files changed, 19 insertions(+), 58 deletions(-)
crypto/api.c +1 -1
index 55bca28df92d86..d1ff266a92ff7d 100644
@@ -301,7 +301,7 @@ static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
 		return crypto_init_cipher_ops(tfm);
 
 	case CRYPTO_ALG_TYPE_COMPRESS:
-		return crypto_init_compress_ops(tfm);
+		return 0;
 
 	default:
 		break;
crypto/compress.c +12 -19
index e9edf852478798..9048fe390c4630 100644
@@ -6,34 +6,27 @@
  *
  * Copyright (c) 2002 James Morris <[email protected]>
  */
-#include <linux/types.h>
 #include <linux/crypto.h>
-#include <linux/errno.h>
-#include <linux/string.h>
 #include "internal.h"
 
-static int crypto_compress(struct crypto_tfm *tfm,
-                            const u8 *src, unsigned int slen,
-                            u8 *dst, unsigned int *dlen)
+int crypto_comp_compress(struct crypto_comp *comp,
+			 const u8 *src, unsigned int slen,
+			 u8 *dst, unsigned int *dlen)
 {
+	struct crypto_tfm *tfm = crypto_comp_tfm(comp);
+
 	return tfm->__crt_alg->cra_compress.coa_compress(tfm, src, slen, dst,
 	                                                 dlen);
 }
+EXPORT_SYMBOL_GPL(crypto_comp_compress);
 
-static int crypto_decompress(struct crypto_tfm *tfm,
-                             const u8 *src, unsigned int slen,
-                             u8 *dst, unsigned int *dlen)
+int crypto_comp_decompress(struct crypto_comp *comp,
+			   const u8 *src, unsigned int slen,
+			   u8 *dst, unsigned int *dlen)
 {
+	struct crypto_tfm *tfm = crypto_comp_tfm(comp);
+
 	return tfm->__crt_alg->cra_compress.coa_decompress(tfm, src, slen, dst,
 	                                                   dlen);
 }
-
-int crypto_init_compress_ops(struct crypto_tfm *tfm)
-{
-	struct compress_tfm *ops = &tfm->crt_compress;
-
-	ops->cot_compress = crypto_compress;
-	ops->cot_decompress = crypto_decompress;
-
-	return 0;
-}
+EXPORT_SYMBOL_GPL(crypto_comp_decompress);
crypto/internal.h +0 -1
index 93df7bec844a7c..a58a2af4b66963 100644
@@ -59,7 +59,6 @@ struct crypto_alg *crypto_mod_get(struct crypto_alg *alg);
 struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
 
 int crypto_init_cipher_ops(struct crypto_tfm *tfm);
-int crypto_init_compress_ops(struct crypto_tfm *tfm);
 
 struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask);
 void crypto_larval_kill(struct crypto_alg *alg);
include/linux/crypto.h +6 -37

69 lines not on this page · raw message

Signature · 1 line
2.24.0.393.g34dc348eaf-goog
#2 lore raw

PATCH 2/2 crypto: cipher - remove crt_u.cipher (struct cipher_tfm)

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Of the three fields in crt_u.cipher (struct cipher_tfm), ->cit_setkey() is pointless because it always points to setkey() in crypto/cipher.c.

->cit_decrypt_one() and ->cit_encrypt_one() are slightly less pointless, since if the algorithm doesn't have an alignmask, they are set directly to ->cia_encrypt() and ->cia_decrypt(). However, this "optimization" isn't worthwhile because:

- The "cipher" algorithm type is the only algorithm still using crt_u, so it's bloating every struct crypto_tfm for every algorithm type.

- If the algorithm has an alignmask, this "optimization" actually makes things slower, as it causes 2 indirect calls per block rather than 1.

- It adds extra code complexity.

- Some templates already call ->cia_encrypt()/->cia_decrypt() directly instead of going through ->cit_encrypt_one()/->cit_decrypt_one().

- The "cipher" algorithm type never gives optimal performance anyway. For that, a higher-level type such as skcipher needs to be used.

Therefore, just remove the extra indirection, and make crypto_cipher_setkey(), crypto_cipher_encrypt_one(), and crypto_cipher_decrypt_one() be direct calls into crypto/cipher.c.

Also remove the unused function crypto_cipher_cast().

---
 crypto/api.c           | 15 +------
 crypto/cipher.c        | 92 +++++++++++++++++-------------------------
 crypto/internal.h      |  2 -
 include/linux/crypto.h | 48 +++-------------------
 4 files changed, 43 insertions(+), 114 deletions(-)
crypto/api.c +1 -14
index d1ff266a92ff7d..72b6527d7e890c 100644
@@ -295,20 +295,7 @@ static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
 
 	if (type_obj)
 		return type_obj->init(tfm, type, mask);
-
-	switch (crypto_tfm_alg_type(tfm)) {
-	case CRYPTO_ALG_TYPE_CIPHER:
-		return crypto_init_cipher_ops(tfm);
-
-	case CRYPTO_ALG_TYPE_COMPRESS:
-		return 0;
-
-	default:
-		break;
-	}
-
-	BUG();
-	return -EINVAL;
+	return 0;
 }
 
 static void crypto_exit_ops(struct crypto_tfm *tfm)
crypto/cipher.c +36 -56

137 lines not on this page · raw message

crypto/internal.h +0 -2
index a58a2af4b66963..ff06a3bd1ca10c 100644
@@ -58,8 +58,6 @@ static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg)
 struct crypto_alg *crypto_mod_get(struct crypto_alg *alg);
 struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
 
-int crypto_init_cipher_ops(struct crypto_tfm *tfm);
-
 struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask);
 void crypto_larval_kill(struct crypto_alg *alg);
 void crypto_alg_tested(const char *name, int err);
include/linux/crypto.h +6 -42

88 lines not on this page · raw message

Signature · 1 line
2.24.0.393.g34dc348eaf-goog
Reviewed-by on the v1 series
#3 lore raw
In reply to Eric Biggers
Quoted 8 lines · Eric Biggers

This series removes the per-algorithm-type union from struct crypto_tfm now that its only remaining users are the "compress" and "cipher" algorithm types, and it's not really needed for them.

This shrinks every crypto transform for every algorithm by 28 bytes on 64-bit platforms (12 bytes on 32-bit), and also removes some code.

Quoted 8 lines

Note that the new-style strongly-typed algorithms (i.e. everything other than "compress" and "cipher") don't need crt_u, since they embed struct crypto_tfm in a per-algorithm-type custom struct instead.

Eric Biggers (2):
crypto: compress - remove crt_u.compress (struct compress_tfm)
crypto: cipher - remove crt_u.cipher (struct cipher_tfm)

Good riddance

Quoted 10 lines
 crypto/api.c           | 15 +------
 crypto/cipher.c        | 92 +++++++++++++++++-------------------------
 crypto/compress.c      | 31 ++++++--------
 crypto/internal.h      |  3 --
 include/linux/crypto.h | 91 ++++++-----------------------------------
 5 files changed, 61 insertions(+), 171 deletions(-)

--
2.24.0.393.g34dc348eaf-goog

maintainer
#4 lore raw
In reply to Eric Biggers
Quoted 15 lines · Eric Biggers

This series removes the per-algorithm-type union from struct crypto_tfm now that its only remaining users are the "compress" and "cipher" algorithm types, and it's not really needed for them.

This shrinks every crypto transform for every algorithm by 28 bytes on 64-bit platforms (12 bytes on 32-bit), and also removes some code.

Note that the new-style strongly-typed algorithms (i.e. everything other than "compress" and "cipher") don't need crt_u, since they embed struct crypto_tfm in a per-algorithm-type custom struct instead.

Eric Biggers (2):
crypto: compress - remove crt_u.compress (struct compress_tfm)
crypto: cipher - remove crt_u.cipher (struct cipher_tfm)

Quoted 6 lines
crypto/api.c           | 15 +------
crypto/cipher.c        | 92 +++++++++++++++++-------------------------
crypto/compress.c      | 31 ++++++--------
crypto/internal.h      |  3 --
include/linux/crypto.h | 91 ++++++-----------------------------------
5 files changed, 61 insertions(+), 171 deletions(-)

All 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