summaryrefslogtreecommitdiffstats
path: root/crypto/rsa.c
diff options
context:
space:
mode:
authorTudor Ambarus <tudor-dan.ambarus@nxp.com>2016-06-14 09:14:58 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2016-06-15 05:07:54 -0400
commit5a7de97309f5af4458b1a25a2a529a1a893c5269 (patch)
tree269fd7cdc654e79e2f3ff1b4b9c1ca149d56415d /crypto/rsa.c
parent103eb3f7bfb4fce0e299afbf50fef8ffa8d9d38c (diff)
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 <tudor-dan.ambarus@nxp.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/rsa.c')
-rw-r--r--crypto/rsa.c105
1 files changed, 80 insertions, 25 deletions
diff --git a/crypto/rsa.c b/crypto/rsa.c
index 77d737f52147..dc692d43b666 100644
--- a/crypto/rsa.c
+++ b/crypto/rsa.c
@@ -10,16 +10,23 @@
10 */ 10 */
11 11
12#include <linux/module.h> 12#include <linux/module.h>
13#include <linux/mpi.h>
13#include <crypto/internal/rsa.h> 14#include <crypto/internal/rsa.h>
14#include <crypto/internal/akcipher.h> 15#include <crypto/internal/akcipher.h>
15#include <crypto/akcipher.h> 16#include <crypto/akcipher.h>
16#include <crypto/algapi.h> 17#include <crypto/algapi.h>
17 18
19struct rsa_mpi_key {
20 MPI n;
21 MPI e;
22 MPI d;
23};
24
18/* 25/*
19 * RSAEP function [RFC3447 sec 5.1.1] 26 * RSAEP function [RFC3447 sec 5.1.1]
20 * c = m^e mod n; 27 * c = m^e mod n;
21 */ 28 */
22static int _rsa_enc(const struct rsa_key *key, MPI c, MPI m) 29static int _rsa_enc(const struct rsa_mpi_key *key, MPI c, MPI m)
23{ 30{
24 /* (1) Validate 0 <= m < n */ 31 /* (1) Validate 0 <= m < n */
25 if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0) 32 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)
33 * RSADP function [RFC3447 sec 5.1.2] 40 * RSADP function [RFC3447 sec 5.1.2]
34 * m = c^d mod n; 41 * m = c^d mod n;
35 */ 42 */
36static int _rsa_dec(const struct rsa_key *key, MPI m, MPI c) 43static int _rsa_dec(const struct rsa_mpi_key *key, MPI m, MPI c)
37{ 44{
38 /* (1) Validate 0 <= c < n */ 45 /* (1) Validate 0 <= c < n */
39 if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0) 46 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)
47 * RSASP1 function [RFC3447 sec 5.2.1] 54 * RSASP1 function [RFC3447 sec 5.2.1]
48 * s = m^d mod n 55 * s = m^d mod n
49 */ 56 */
50static int _rsa_sign(const struct rsa_key *key, MPI s, MPI m) 57static int _rsa_sign(const struct rsa_mpi_key *key, MPI s, MPI m)
51{ 58{
52 /* (1) Validate 0 <= m < n */ 59 /* (1) Validate 0 <= m < n */
53 if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0) 60 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)
61 * RSAVP1 function [RFC3447 sec 5.2.2] 68 * RSAVP1 function [RFC3447 sec 5.2.2]
62 * m = s^e mod n; 69 * m = s^e mod n;
63 */ 70 */
64static int _rsa_verify(const struct rsa_key *key, MPI m, MPI s) 71static int _rsa_verify(const struct rsa_mpi_key *key, MPI m, MPI s)
65{ 72{
66 /* (1) Validate 0 <= s < n */ 73 /* (1) Validate 0 <= s < n */
67 if (mpi_cmp_ui(s, 0) < 0 || mpi_cmp(s, key->n) >= 0) 74 if (mpi_cmp_ui(s, 0) < 0 || mpi_cmp(s, key->n) >= 0)
@@ -71,7 +78,7 @@ static int _rsa_verify(const struct rsa_key *key, MPI m, MPI s)
71 return mpi_powm(m, s, key->e, key->n); 78 return mpi_powm(m, s, key->e, key->n);
72} 79}
73 80
74static inline struct rsa_key *rsa_get_key(struct crypto_akcipher *tfm) 81static inline struct rsa_mpi_key *rsa_get_key(struct crypto_akcipher *tfm)
75{ 82{
76 return akcipher_tfm_ctx(tfm); 83 return akcipher_tfm_ctx(tfm);
77} 84}
@@ -79,7 +86,7 @@ static inline struct rsa_key *rsa_get_key(struct crypto_akcipher *tfm)
79static int rsa_enc(struct akcipher_request *req) 86static int rsa_enc(struct akcipher_request *req)
80{ 87{
81 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 88 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);
83 MPI m, c = mpi_alloc(0); 90 MPI m, c = mpi_alloc(0);
84 int ret = 0; 91 int ret = 0;
85 int sign; 92 int sign;
@@ -118,7 +125,7 @@ err_free_c:
118static int rsa_dec(struct akcipher_request *req) 125static int rsa_dec(struct akcipher_request *req)
119{ 126{
120 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 127 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);
122 MPI c, m = mpi_alloc(0); 129 MPI c, m = mpi_alloc(0);
123 int ret = 0; 130 int ret = 0;
124 int sign; 131 int sign;
@@ -156,7 +163,7 @@ err_free_m:
156static int rsa_sign(struct akcipher_request *req) 163static int rsa_sign(struct akcipher_request *req)
157{ 164{
158 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 165 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);
160 MPI m, s = mpi_alloc(0); 167 MPI m, s = mpi_alloc(0);
161 int ret = 0; 168 int ret = 0;
162 int sign; 169 int sign;
@@ -195,7 +202,7 @@ err_free_s:
195static int rsa_verify(struct akcipher_request *req) 202static int rsa_verify(struct akcipher_request *req)
196{ 203{
197 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 204 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);
199 MPI s, m = mpi_alloc(0); 206 MPI s, m = mpi_alloc(0);
200 int ret = 0; 207 int ret = 0;
201 int sign; 208 int sign;
@@ -233,6 +240,16 @@ err_free_m:
233 return ret; 240 return ret;
234} 241}
235 242
243static 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
236static int rsa_check_key_length(unsigned int len) 253static int rsa_check_key_length(unsigned int len)
237{ 254{
238 switch (len) { 255 switch (len) {
@@ -251,49 +268,87 @@ static int rsa_check_key_length(unsigned int len)
251static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, 268static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
252 unsigned int keylen) 269 unsigned int keylen)
253{ 270{
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};
255 int ret; 273 int ret;
256 274
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);
258 if (ret) 279 if (ret)
259 return ret; 280 return ret;
260 281
261 if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) { 282 mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
262 rsa_free_key(pkey); 283 if (!mpi_key->e)
263 ret = -EINVAL; 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;
264 } 293 }
265 return ret; 294
295 return 0;
296
297err:
298 rsa_free_mpi_key(mpi_key);
299 return -ENOMEM;
266} 300}
267 301
268static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, 302static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
269 unsigned int keylen) 303 unsigned int keylen)
270{ 304{
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};
272 int ret; 307 int ret;
273 308
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);
275 if (ret) 313 if (ret)
276 return ret; 314 return ret;
277 315
278 if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) { 316 mpi_key->d = mpi_read_raw_data(raw_key.d, raw_key.d_sz);
279 rsa_free_key(pkey); 317 if (!mpi_key->d)
280 ret = -EINVAL; 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;
281 } 331 }
282 return ret; 332
333 return 0;
334
335err:
336 rsa_free_mpi_key(mpi_key);
337 return -ENOMEM;
283} 338}
284 339
285static int rsa_max_size(struct crypto_akcipher *tfm) 340static int rsa_max_size(struct crypto_akcipher *tfm)
286{ 341{
287 struct rsa_key *pkey = akcipher_tfm_ctx(tfm); 342 struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
288 343
289 return pkey->n ? mpi_get_size(pkey->n) : -EINVAL; 344 return pkey->n ? mpi_get_size(pkey->n) : -EINVAL;
290} 345}
291 346
292static void rsa_exit_tfm(struct crypto_akcipher *tfm) 347static void rsa_exit_tfm(struct crypto_akcipher *tfm)
293{ 348{
294 struct rsa_key *pkey = akcipher_tfm_ctx(tfm); 349 struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
295 350
296 rsa_free_key(pkey); 351 rsa_free_mpi_key(pkey);
297} 352}
298 353
299static struct akcipher_alg rsa = { 354static struct akcipher_alg rsa = {
@@ -310,7 +365,7 @@ static struct akcipher_alg rsa = {
310 .cra_driver_name = "rsa-generic", 365 .cra_driver_name = "rsa-generic",
311 .cra_priority = 100, 366 .cra_priority = 100,
312 .cra_module = THIS_MODULE, 367 .cra_module = THIS_MODULE,
313 .cra_ctxsize = sizeof(struct rsa_key), 368 .cra_ctxsize = sizeof(struct rsa_mpi_key),
314 }, 369 },
315}; 370};
316 371