Skip to content

Commit 5a7de97

Browse files
Tudor Ambarusherbertx
Tudor Ambarus
authored andcommitted
crypto: rsa - return raw integers for the ASN.1 parser
Return the raw key with no other processing so that the caller can copy it or MPI parse it, etc. The scope is to have only one ANS.1 parser for all RSA implementations. Update the RSA software implementation so that it does the MPI conversion on top. Signed-off-by: Tudor Ambarus <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 103eb3f commit 5a7de97

File tree

3 files changed

+135
-103
lines changed

3 files changed

+135
-103
lines changed

crypto/rsa.c

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,23 @@
1010
*/
1111

1212
#include <linux/module.h>
13+
#include <linux/mpi.h>
1314
#include <crypto/internal/rsa.h>
1415
#include <crypto/internal/akcipher.h>
1516
#include <crypto/akcipher.h>
1617
#include <crypto/algapi.h>
1718

19+
struct rsa_mpi_key {
20+
MPI n;
21+
MPI e;
22+
MPI d;
23+
};
24+
1825
/*
1926
* RSAEP function [RFC3447 sec 5.1.1]
2027
* c = m^e mod n;
2128
*/
22-
static int _rsa_enc(const struct rsa_key *key, MPI c, MPI m)
29+
static int _rsa_enc(const struct rsa_mpi_key *key, MPI c, MPI m)
2330
{
2431
/* (1) Validate 0 <= m < n */
2532
if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
@@ -33,7 +40,7 @@ static int _rsa_enc(const struct rsa_key *key, MPI c, MPI m)
3340
* RSADP function [RFC3447 sec 5.1.2]
3441
* m = c^d mod n;
3542
*/
36-
static int _rsa_dec(const struct rsa_key *key, MPI m, MPI c)
43+
static int _rsa_dec(const struct rsa_mpi_key *key, MPI m, MPI c)
3744
{
3845
/* (1) Validate 0 <= c < n */
3946
if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0)
@@ -47,7 +54,7 @@ static int _rsa_dec(const struct rsa_key *key, MPI m, MPI c)
4754
* RSASP1 function [RFC3447 sec 5.2.1]
4855
* s = m^d mod n
4956
*/
50-
static int _rsa_sign(const struct rsa_key *key, MPI s, MPI m)
57+
static int _rsa_sign(const struct rsa_mpi_key *key, MPI s, MPI m)
5158
{
5259
/* (1) Validate 0 <= m < n */
5360
if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
@@ -61,7 +68,7 @@ static int _rsa_sign(const struct rsa_key *key, MPI s, MPI m)
6168
* RSAVP1 function [RFC3447 sec 5.2.2]
6269
* m = s^e mod n;
6370
*/
64-
static int _rsa_verify(const struct rsa_key *key, MPI m, MPI s)
71+
static int _rsa_verify(const struct rsa_mpi_key *key, MPI m, MPI s)
6572
{
6673
/* (1) Validate 0 <= s < n */
6774
if (mpi_cmp_ui(s, 0) < 0 || mpi_cmp(s, key->n) >= 0)
@@ -71,15 +78,15 @@ static int _rsa_verify(const struct rsa_key *key, MPI m, MPI s)
7178
return mpi_powm(m, s, key->e, key->n);
7279
}
7380

74-
static inline struct rsa_key *rsa_get_key(struct crypto_akcipher *tfm)
81+
static inline struct rsa_mpi_key *rsa_get_key(struct crypto_akcipher *tfm)
7582
{
7683
return akcipher_tfm_ctx(tfm);
7784
}
7885

7986
static int rsa_enc(struct akcipher_request *req)
8087
{
8188
struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
82-
const struct rsa_key *pkey = rsa_get_key(tfm);
89+
const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
8390
MPI m, c = mpi_alloc(0);
8491
int ret = 0;
8592
int sign;
@@ -118,7 +125,7 @@ static int rsa_enc(struct akcipher_request *req)
118125
static int rsa_dec(struct akcipher_request *req)
119126
{
120127
struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
121-
const struct rsa_key *pkey = rsa_get_key(tfm);
128+
const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
122129
MPI c, m = mpi_alloc(0);
123130
int ret = 0;
124131
int sign;
@@ -156,7 +163,7 @@ static int rsa_dec(struct akcipher_request *req)
156163
static int rsa_sign(struct akcipher_request *req)
157164
{
158165
struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
159-
const struct rsa_key *pkey = rsa_get_key(tfm);
166+
const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
160167
MPI m, s = mpi_alloc(0);
161168
int ret = 0;
162169
int sign;
@@ -195,7 +202,7 @@ static int rsa_sign(struct akcipher_request *req)
195202
static int rsa_verify(struct akcipher_request *req)
196203
{
197204
struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
198-
const struct rsa_key *pkey = rsa_get_key(tfm);
205+
const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
199206
MPI s, m = mpi_alloc(0);
200207
int ret = 0;
201208
int sign;
@@ -233,6 +240,16 @@ static int rsa_verify(struct akcipher_request *req)
233240
return ret;
234241
}
235242

243+
static void rsa_free_mpi_key(struct rsa_mpi_key *key)
244+
{
245+
mpi_free(key->d);
246+
mpi_free(key->e);
247+
mpi_free(key->n);
248+
key->d = NULL;
249+
key->e = NULL;
250+
key->n = NULL;
251+
}
252+
236253
static int rsa_check_key_length(unsigned int len)
237254
{
238255
switch (len) {
@@ -251,49 +268,87 @@ static int rsa_check_key_length(unsigned int len)
251268
static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
252269
unsigned int keylen)
253270
{
254-
struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
271+
struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
272+
struct rsa_key raw_key = {0};
255273
int ret;
256274

257-
ret = rsa_parse_pub_key(pkey, key, keylen);
275+
/* Free the old MPI key if any */
276+
rsa_free_mpi_key(mpi_key);
277+
278+
ret = rsa_parse_pub_key(&raw_key, key, keylen);
258279
if (ret)
259280
return ret;
260281

261-
if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) {
262-
rsa_free_key(pkey);
263-
ret = -EINVAL;
282+
mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
283+
if (!mpi_key->e)
284+
goto err;
285+
286+
mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
287+
if (!mpi_key->n)
288+
goto err;
289+
290+
if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
291+
rsa_free_mpi_key(mpi_key);
292+
return -EINVAL;
264293
}
265-
return ret;
294+
295+
return 0;
296+
297+
err:
298+
rsa_free_mpi_key(mpi_key);
299+
return -ENOMEM;
266300
}
267301

268302
static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
269303
unsigned int keylen)
270304
{
271-
struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
305+
struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
306+
struct rsa_key raw_key = {0};
272307
int ret;
273308

274-
ret = rsa_parse_priv_key(pkey, key, keylen);
309+
/* Free the old MPI key if any */
310+
rsa_free_mpi_key(mpi_key);
311+
312+
ret = rsa_parse_priv_key(&raw_key, key, keylen);
275313
if (ret)
276314
return ret;
277315

278-
if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) {
279-
rsa_free_key(pkey);
280-
ret = -EINVAL;
316+
mpi_key->d = mpi_read_raw_data(raw_key.d, raw_key.d_sz);
317+
if (!mpi_key->d)
318+
goto err;
319+
320+
mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
321+
if (!mpi_key->e)
322+
goto err;
323+
324+
mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
325+
if (!mpi_key->n)
326+
goto err;
327+
328+
if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
329+
rsa_free_mpi_key(mpi_key);
330+
return -EINVAL;
281331
}
282-
return ret;
332+
333+
return 0;
334+
335+
err:
336+
rsa_free_mpi_key(mpi_key);
337+
return -ENOMEM;
283338
}
284339

285340
static int rsa_max_size(struct crypto_akcipher *tfm)
286341
{
287-
struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
342+
struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
288343

289344
return pkey->n ? mpi_get_size(pkey->n) : -EINVAL;
290345
}
291346

292347
static void rsa_exit_tfm(struct crypto_akcipher *tfm)
293348
{
294-
struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
349+
struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
295350

296-
rsa_free_key(pkey);
351+
rsa_free_mpi_key(pkey);
297352
}
298353

299354
static struct akcipher_alg rsa = {
@@ -310,7 +365,7 @@ static struct akcipher_alg rsa = {
310365
.cra_driver_name = "rsa-generic",
311366
.cra_priority = 100,
312367
.cra_module = THIS_MODULE,
313-
.cra_ctxsize = sizeof(struct rsa_key),
368+
.cra_ctxsize = sizeof(struct rsa_mpi_key),
314369
},
315370
};
316371

crypto/rsa_helper.c

Lines changed: 39 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,29 @@ int rsa_get_n(void *context, size_t hdrlen, unsigned char tag,
2222
const void *value, size_t vlen)
2323
{
2424
struct rsa_key *key = context;
25+
const u8 *ptr = value;
26+
size_t n_sz = vlen;
2527

26-
key->n = mpi_read_raw_data(value, vlen);
27-
28-
if (!key->n)
29-
return -ENOMEM;
30-
31-
/* In FIPS mode only allow key size 2K & 3K */
32-
if (fips_enabled && (mpi_get_size(key->n) != 256 &&
33-
mpi_get_size(key->n) != 384)) {
34-
pr_err("RSA: key size not allowed in FIPS mode\n");
35-
mpi_free(key->n);
36-
key->n = NULL;
28+
/* invalid key provided */
29+
if (!value || !vlen)
3730
return -EINVAL;
31+
32+
if (fips_enabled) {
33+
while (!*ptr && n_sz) {
34+
ptr++;
35+
n_sz--;
36+
}
37+
38+
/* In FIPS mode only allow key size 2K & 3K */
39+
if (n_sz != 256 && n_sz != 384) {
40+
pr_err("RSA: key size not allowed in FIPS mode\n");
41+
return -EINVAL;
42+
}
3843
}
44+
45+
key->n = value;
46+
key->n_sz = vlen;
47+
3948
return 0;
4049
}
4150

@@ -44,10 +53,12 @@ int rsa_get_e(void *context, size_t hdrlen, unsigned char tag,
4453
{
4554
struct rsa_key *key = context;
4655

47-
key->e = mpi_read_raw_data(value, vlen);
56+
/* invalid key provided */
57+
if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
58+
return -EINVAL;
4859

49-
if (!key->e)
50-
return -ENOMEM;
60+
key->e = value;
61+
key->e_sz = vlen;
5162

5263
return 0;
5364
}
@@ -57,46 +68,20 @@ int rsa_get_d(void *context, size_t hdrlen, unsigned char tag,
5768
{
5869
struct rsa_key *key = context;
5970

60-
key->d = mpi_read_raw_data(value, vlen);
61-
62-
if (!key->d)
63-
return -ENOMEM;
64-
65-
/* In FIPS mode only allow key size 2K & 3K */
66-
if (fips_enabled && (mpi_get_size(key->d) != 256 &&
67-
mpi_get_size(key->d) != 384)) {
68-
pr_err("RSA: key size not allowed in FIPS mode\n");
69-
mpi_free(key->d);
70-
key->d = NULL;
71+
/* invalid key provided */
72+
if (!value || !key->n_sz || !vlen || vlen > key->n_sz)
7173
return -EINVAL;
72-
}
73-
return 0;
74-
}
7574

76-
static void free_mpis(struct rsa_key *key)
77-
{
78-
mpi_free(key->n);
79-
mpi_free(key->e);
80-
mpi_free(key->d);
81-
key->n = NULL;
82-
key->e = NULL;
83-
key->d = NULL;
84-
}
75+
key->d = value;
76+
key->d_sz = vlen;
8577

86-
/**
87-
* rsa_free_key() - frees rsa key allocated by rsa_parse_key()
88-
*
89-
* @rsa_key: struct rsa_key key representation
90-
*/
91-
void rsa_free_key(struct rsa_key *key)
92-
{
93-
free_mpis(key);
78+
return 0;
9479
}
95-
EXPORT_SYMBOL_GPL(rsa_free_key);
9680

9781
/**
98-
* rsa_parse_pub_key() - extracts an rsa public key from BER encoded buffer
99-
* and stores it in the provided struct rsa_key
82+
* rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the
83+
* provided struct rsa_key, pointers to the raw key as is,
84+
* so that the caller can copy it or MPI parse it, etc.
10085
*
10186
* @rsa_key: struct rsa_key key representation
10287
* @key: key in BER format
@@ -107,23 +92,15 @@ EXPORT_SYMBOL_GPL(rsa_free_key);
10792
int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
10893
unsigned int key_len)
10994
{
110-
int ret;
111-
112-
free_mpis(rsa_key);
113-
ret = asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len);
114-
if (ret < 0)
115-
goto error;
116-
117-
return 0;
118-
error:
119-
free_mpis(rsa_key);
120-
return ret;
95+
return asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len);
12196
}
12297
EXPORT_SYMBOL_GPL(rsa_parse_pub_key);
12398

12499
/**
125-
* rsa_parse_pub_key() - extracts an rsa private key from BER encoded buffer
126-
* and stores it in the provided struct rsa_key
100+
* rsa_parse_priv_key() - decodes the BER encoded buffer and stores in the
101+
* provided struct rsa_key, pointers to the raw key
102+
* as is, so that the caller can copy it or MPI parse it,
103+
* etc.
127104
*
128105
* @rsa_key: struct rsa_key key representation
129106
* @key: key in BER format
@@ -134,16 +111,6 @@ EXPORT_SYMBOL_GPL(rsa_parse_pub_key);
134111
int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key,
135112
unsigned int key_len)
136113
{
137-
int ret;
138-
139-
free_mpis(rsa_key);
140-
ret = asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len);
141-
if (ret < 0)
142-
goto error;
143-
144-
return 0;
145-
error:
146-
free_mpis(rsa_key);
147-
return ret;
114+
return asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len);
148115
}
149116
EXPORT_SYMBOL_GPL(rsa_parse_priv_key);

0 commit comments

Comments
 (0)