Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: rsa-pkcs1pad - Deduplicate set_{pub,priv}_key callbacks

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>

authored by

Lukas Wunner and committed by
Herbert Xu
7964b0d4 ae117924

+30 -28
+2 -28
crypto/rsa-pkcs1pad.c
··· 131 131 unsigned int keylen) 132 132 { 133 133 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); 134 - int err; 135 134 136 - ctx->key_size = 0; 137 - 138 - err = crypto_akcipher_set_pub_key(ctx->child, key, keylen); 139 - if (err) 140 - return err; 141 - 142 - /* Find out new modulus size from rsa implementation */ 143 - err = crypto_akcipher_maxsize(ctx->child); 144 - if (err > PAGE_SIZE) 145 - return -ENOTSUPP; 146 - 147 - ctx->key_size = err; 148 - return 0; 135 + return rsa_set_key(ctx->child, &ctx->key_size, RSA_PUB, key, keylen); 149 136 } 150 137 151 138 static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key, 152 139 unsigned int keylen) 153 140 { 154 141 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); 155 - int err; 156 142 157 - ctx->key_size = 0; 158 - 159 - err = crypto_akcipher_set_priv_key(ctx->child, key, keylen); 160 - if (err) 161 - return err; 162 - 163 - /* Find out new modulus size from rsa implementation */ 164 - err = crypto_akcipher_maxsize(ctx->child); 165 - if (err > PAGE_SIZE) 166 - return -ENOTSUPP; 167 - 168 - ctx->key_size = err; 169 - return 0; 143 + return rsa_set_key(ctx->child, &ctx->key_size, RSA_PRIV, key, keylen); 170 144 } 171 145 172 146 static unsigned int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
+28
include/crypto/internal/rsa.h
··· 8 8 #ifndef _RSA_HELPER_ 9 9 #define _RSA_HELPER_ 10 10 #include <linux/types.h> 11 + #include <crypto/akcipher.h> 11 12 12 13 /** 13 14 * rsa_key - RSA key structure ··· 53 52 54 53 int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key, 55 54 unsigned int key_len); 55 + 56 + #define RSA_PUB (true) 57 + #define RSA_PRIV (false) 58 + 59 + static inline int rsa_set_key(struct crypto_akcipher *child, 60 + unsigned int *key_size, bool is_pubkey, 61 + const void *key, unsigned int keylen) 62 + { 63 + int err; 64 + 65 + *key_size = 0; 66 + 67 + if (is_pubkey) 68 + err = crypto_akcipher_set_pub_key(child, key, keylen); 69 + else 70 + err = crypto_akcipher_set_priv_key(child, key, keylen); 71 + if (err) 72 + return err; 73 + 74 + /* Find out new modulus size from rsa implementation */ 75 + err = crypto_akcipher_maxsize(child); 76 + if (err > PAGE_SIZE) 77 + return -ENOTSUPP; 78 + 79 + *key_size = err; 80 + return 0; 81 + } 56 82 57 83 extern struct crypto_template rsa_pkcs1pad_tmpl; 58 84 #endif