]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
crypto: rsa-pkcs1pad - Deduplicate set_{pub,priv}_key callbacks
authorLukas Wunner <lukas@wunner.de>
Tue, 10 Sep 2024 14:30:15 +0000 (16:30 +0200)
committerHerbert Xu <herbert@gondor.apana.org.au>
Sat, 5 Oct 2024 05:22:04 +0000 (13:22 +0800)
pkcs1pad_set_pub_key() and pkcs1pad_set_priv_key() are almost identical.

The upcoming migration of sign/verify operations from rsa-pkcs1pad.c
into a separate crypto_template will require another copy of the exact
same functions.  When RSASSA-PSS and RSAES-OAEP are introduced, each
will need yet another copy.

Deduplicate the functions into a single one which lives in a common
header file for reuse by RSASSA-PKCS1-v1_5, RSASSA-PSS and RSAES-OAEP.

Signed-off-by: Lukas Wunner <lukas@wunner.de>
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/rsa-pkcs1pad.c
include/crypto/internal/rsa.h

index cd501195f34a1a4643874b8b8f63cdbeabac1344..3c5fe8c93938ebe1e0e55435975f983d1c309aa8 100644 (file)
@@ -131,42 +131,16 @@ static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key,
                unsigned int keylen)
 {
        struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
-       int err;
-
-       ctx->key_size = 0;
 
-       err = crypto_akcipher_set_pub_key(ctx->child, key, keylen);
-       if (err)
-               return err;
-
-       /* Find out new modulus size from rsa implementation */
-       err = crypto_akcipher_maxsize(ctx->child);
-       if (err > PAGE_SIZE)
-               return -ENOTSUPP;
-
-       ctx->key_size = err;
-       return 0;
+       return rsa_set_key(ctx->child, &ctx->key_size, RSA_PUB, key, keylen);
 }
 
 static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
                unsigned int keylen)
 {
        struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
-       int err;
-
-       ctx->key_size = 0;
 
-       err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
-       if (err)
-               return err;
-
-       /* Find out new modulus size from rsa implementation */
-       err = crypto_akcipher_maxsize(ctx->child);
-       if (err > PAGE_SIZE)
-               return -ENOTSUPP;
-
-       ctx->key_size = err;
-       return 0;
+       return rsa_set_key(ctx->child, &ctx->key_size, RSA_PRIV, key, keylen);
 }
 
 static unsigned int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
index e870133f4b7751879d03b67820291c1241546a80..754f687134df74ef642fa44e5d2a816fdfcdbae5 100644 (file)
@@ -8,6 +8,7 @@
 #ifndef _RSA_HELPER_
 #define _RSA_HELPER_
 #include <linux/types.h>
+#include <crypto/akcipher.h>
 
 /**
  * rsa_key - RSA key structure
@@ -53,5 +54,32 @@ int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
 int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
                       unsigned int key_len);
 
+#define RSA_PUB (true)
+#define RSA_PRIV (false)
+
+static inline int rsa_set_key(struct crypto_akcipher *child,
+                             unsigned int *key_size, bool is_pubkey,
+                             const void *key, unsigned int keylen)
+{
+       int err;
+
+       *key_size = 0;
+
+       if (is_pubkey)
+               err = crypto_akcipher_set_pub_key(child, key, keylen);
+       else
+               err = crypto_akcipher_set_priv_key(child, key, keylen);
+       if (err)
+               return err;
+
+       /* Find out new modulus size from rsa implementation */
+       err = crypto_akcipher_maxsize(child);
+       if (err > PAGE_SIZE)
+               return -ENOTSUPP;
+
+       *key_size = err;
+       return 0;
+}
+
 extern struct crypto_template rsa_pkcs1pad_tmpl;
 #endif