~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/crypto/rsa.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /crypto/rsa.c (Version linux-6.12-rc7) and /crypto/rsa.c (Version linux-4.13.16)


  1 // SPDX-License-Identifier: GPL-2.0-or-later   << 
  2 /* RSA asymmetric public-key algorithm [RFC344      1 /* RSA asymmetric public-key algorithm [RFC3447]
  3  *                                                  2  *
  4  * Copyright (c) 2015, Intel Corporation            3  * Copyright (c) 2015, Intel Corporation
  5  * Authors: Tadeusz Struk <tadeusz.struk@intel      4  * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
                                                   >>   5  *
                                                   >>   6  * This program is free software; you can redistribute it and/or
                                                   >>   7  * modify it under the terms of the GNU General Public Licence
                                                   >>   8  * as published by the Free Software Foundation; either version
                                                   >>   9  * 2 of the Licence, or (at your option) any later version.
  6  */                                                10  */
  7                                                    11 
  8 #include <linux/fips.h>                        << 
  9 #include <linux/module.h>                          12 #include <linux/module.h>
 10 #include <linux/mpi.h>                             13 #include <linux/mpi.h>
 11 #include <crypto/internal/rsa.h>                   14 #include <crypto/internal/rsa.h>
 12 #include <crypto/internal/akcipher.h>              15 #include <crypto/internal/akcipher.h>
 13 #include <crypto/akcipher.h>                       16 #include <crypto/akcipher.h>
 14 #include <crypto/algapi.h>                         17 #include <crypto/algapi.h>
 15                                                    18 
 16 struct rsa_mpi_key {                               19 struct rsa_mpi_key {
 17         MPI n;                                     20         MPI n;
 18         MPI e;                                     21         MPI e;
 19         MPI d;                                     22         MPI d;
 20         MPI p;                                 << 
 21         MPI q;                                 << 
 22         MPI dp;                                << 
 23         MPI dq;                                << 
 24         MPI qinv;                              << 
 25 };                                                 23 };
 26                                                    24 
 27 static int rsa_check_payload(MPI x, MPI n)     << 
 28 {                                              << 
 29         MPI n1;                                << 
 30                                                << 
 31         if (mpi_cmp_ui(x, 1) <= 0)             << 
 32                 return -EINVAL;                << 
 33                                                << 
 34         n1 = mpi_alloc(0);                     << 
 35         if (!n1)                               << 
 36                 return -ENOMEM;                << 
 37                                                << 
 38         if (mpi_sub_ui(n1, n, 1) || mpi_cmp(x, << 
 39                 mpi_free(n1);                  << 
 40                 return -EINVAL;                << 
 41         }                                      << 
 42                                                << 
 43         mpi_free(n1);                          << 
 44         return 0;                              << 
 45 }                                              << 
 46                                                << 
 47 /*                                                 25 /*
 48  * RSAEP function [RFC3447 sec 5.1.1]              26  * RSAEP function [RFC3447 sec 5.1.1]
 49  * c = m^e mod n;                                  27  * c = m^e mod n;
 50  */                                                28  */
 51 static int _rsa_enc(const struct rsa_mpi_key *     29 static int _rsa_enc(const struct rsa_mpi_key *key, MPI c, MPI m)
 52 {                                                  30 {
 53         /*                                     !!  31         /* (1) Validate 0 <= m < n */
 54          * Even though (1) in RFC3447 only req !!  32         if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
 55          * slightly more conservative and requ << 
 56          * with SP 800-56Br2, Section 7.1.1.   << 
 57          */                                    << 
 58         if (rsa_check_payload(m, key->n))      << 
 59                 return -EINVAL;                    33                 return -EINVAL;
 60                                                    34 
 61         /* (2) c = m^e mod n */                    35         /* (2) c = m^e mod n */
 62         return mpi_powm(c, m, key->e, key->n);     36         return mpi_powm(c, m, key->e, key->n);
 63 }                                                  37 }
 64                                                    38 
 65 /*                                                 39 /*
 66  * RSADP function [RFC3447 sec 5.1.2]              40  * RSADP function [RFC3447 sec 5.1.2]
 67  * m_1 = c^dP mod p;                           !!  41  * m = c^d mod n;
 68  * m_2 = c^dQ mod q;                           << 
 69  * h = (m_1 - m_2) * qInv mod p;               << 
 70  * m = m_2 + q * h;                            << 
 71  */                                                42  */
 72 static int _rsa_dec_crt(const struct rsa_mpi_k !!  43 static int _rsa_dec(const struct rsa_mpi_key *key, MPI m, MPI c)
 73 {                                                  44 {
 74         MPI m2, m12_or_qh;                     !!  45         /* (1) Validate 0 <= c < n */
 75         int ret = -ENOMEM;                     !!  46         if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0)
 76                                                << 
 77         /*                                     << 
 78          * Even though (1) in RFC3447 only req << 
 79          * slightly more conservative and requ << 
 80          * with SP 800-56Br2, Section 7.1.2.   << 
 81          */                                    << 
 82         if (rsa_check_payload(c, key->n))      << 
 83                 return -EINVAL;                    47                 return -EINVAL;
 84                                                    48 
 85         m2 = mpi_alloc(0);                     !!  49         /* (2) m = c^d mod n */
 86         m12_or_qh = mpi_alloc(0);              !!  50         return mpi_powm(m, c, key->d, key->n);
 87         if (!m2 || !m12_or_qh)                 !!  51 }
 88                 goto err_free_mpi;             << 
 89                                                    52 
 90         /* (2i) m_1 = c^dP mod p */            !!  53 /*
 91         ret = mpi_powm(m_or_m1_or_h, c, key->d !!  54  * RSASP1 function [RFC3447 sec 5.2.1]
 92         if (ret)                               !!  55  * s = m^d mod n
 93                 goto err_free_mpi;             !!  56  */
                                                   >>  57 static int _rsa_sign(const struct rsa_mpi_key *key, MPI s, MPI m)
                                                   >>  58 {
                                                   >>  59         /* (1) Validate 0 <= m < n */
                                                   >>  60         if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
                                                   >>  61                 return -EINVAL;
 94                                                    62 
 95         /* (2i) m_2 = c^dQ mod q */            !!  63         /* (2) s = m^d mod n */
 96         ret = mpi_powm(m2, c, key->dq, key->q) !!  64         return mpi_powm(s, m, key->d, key->n);
 97         if (ret)                               !!  65 }
 98                 goto err_free_mpi;             << 
 99                                                    66 
100         /* (2iii) h = (m_1 - m_2) * qInv mod p !!  67 /*
101         ret = mpi_sub(m12_or_qh, m_or_m1_or_h, !!  68  * RSAVP1 function [RFC3447 sec 5.2.2]
102               mpi_mulm(m_or_m1_or_h, m12_or_qh !!  69  * m = s^e mod n;
103                                                !!  70  */
104         /* (2iv) m = m_2 + q * h */            !!  71 static int _rsa_verify(const struct rsa_mpi_key *key, MPI m, MPI s)
105         ret = ret ?:                           !!  72 {
106               mpi_mul(m12_or_qh, key->q, m_or_ !!  73         /* (1) Validate 0 <= s < n */
107               mpi_addm(m_or_m1_or_h, m2, m12_o !!  74         if (mpi_cmp_ui(s, 0) < 0 || mpi_cmp(s, key->n) >= 0)
108                                                !!  75                 return -EINVAL;
109 err_free_mpi:                                  !!  76 
110         mpi_free(m12_or_qh);                   !!  77         /* (2) m = s^e mod n */
111         mpi_free(m2);                          !!  78         return mpi_powm(m, s, key->e, key->n);
112         return ret;                            << 
113 }                                                  79 }
114                                                    80 
115 static inline struct rsa_mpi_key *rsa_get_key(     81 static inline struct rsa_mpi_key *rsa_get_key(struct crypto_akcipher *tfm)
116 {                                                  82 {
117         return akcipher_tfm_ctx(tfm);              83         return akcipher_tfm_ctx(tfm);
118 }                                                  84 }
119                                                    85 
120 static int rsa_enc(struct akcipher_request *re     86 static int rsa_enc(struct akcipher_request *req)
121 {                                                  87 {
122         struct crypto_akcipher *tfm = crypto_a     88         struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
123         const struct rsa_mpi_key *pkey = rsa_g     89         const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
124         MPI m, c = mpi_alloc(0);                   90         MPI m, c = mpi_alloc(0);
125         int ret = 0;                               91         int ret = 0;
126         int sign;                                  92         int sign;
127                                                    93 
128         if (!c)                                    94         if (!c)
129                 return -ENOMEM;                    95                 return -ENOMEM;
130                                                    96 
131         if (unlikely(!pkey->n || !pkey->e)) {      97         if (unlikely(!pkey->n || !pkey->e)) {
132                 ret = -EINVAL;                     98                 ret = -EINVAL;
133                 goto err_free_c;                   99                 goto err_free_c;
134         }                                         100         }
135                                                   101 
136         ret = -ENOMEM;                            102         ret = -ENOMEM;
137         m = mpi_read_raw_from_sgl(req->src, re    103         m = mpi_read_raw_from_sgl(req->src, req->src_len);
138         if (!m)                                   104         if (!m)
139                 goto err_free_c;                  105                 goto err_free_c;
140                                                   106 
141         ret = _rsa_enc(pkey, c, m);               107         ret = _rsa_enc(pkey, c, m);
142         if (ret)                                  108         if (ret)
143                 goto err_free_m;                  109                 goto err_free_m;
144                                                   110 
145         ret = mpi_write_to_sgl(c, req->dst, re    111         ret = mpi_write_to_sgl(c, req->dst, req->dst_len, &sign);
146         if (ret)                                  112         if (ret)
147                 goto err_free_m;                  113                 goto err_free_m;
148                                                   114 
149         if (sign < 0)                             115         if (sign < 0)
150                 ret = -EBADMSG;                   116                 ret = -EBADMSG;
151                                                   117 
152 err_free_m:                                       118 err_free_m:
153         mpi_free(m);                              119         mpi_free(m);
154 err_free_c:                                       120 err_free_c:
155         mpi_free(c);                              121         mpi_free(c);
156         return ret;                               122         return ret;
157 }                                                 123 }
158                                                   124 
159 static int rsa_dec(struct akcipher_request *re    125 static int rsa_dec(struct akcipher_request *req)
160 {                                                 126 {
161         struct crypto_akcipher *tfm = crypto_a    127         struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
162         const struct rsa_mpi_key *pkey = rsa_g    128         const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
163         MPI c, m = mpi_alloc(0);                  129         MPI c, m = mpi_alloc(0);
164         int ret = 0;                              130         int ret = 0;
165         int sign;                                 131         int sign;
166                                                   132 
167         if (!m)                                   133         if (!m)
168                 return -ENOMEM;                   134                 return -ENOMEM;
169                                                   135 
170         if (unlikely(!pkey->n || !pkey->d)) {     136         if (unlikely(!pkey->n || !pkey->d)) {
171                 ret = -EINVAL;                    137                 ret = -EINVAL;
172                 goto err_free_m;                  138                 goto err_free_m;
173         }                                         139         }
174                                                   140 
175         ret = -ENOMEM;                            141         ret = -ENOMEM;
176         c = mpi_read_raw_from_sgl(req->src, re    142         c = mpi_read_raw_from_sgl(req->src, req->src_len);
177         if (!c)                                   143         if (!c)
178                 goto err_free_m;                  144                 goto err_free_m;
179                                                   145 
180         ret = _rsa_dec_crt(pkey, m, c);        !! 146         ret = _rsa_dec(pkey, m, c);
181         if (ret)                                  147         if (ret)
182                 goto err_free_c;                  148                 goto err_free_c;
183                                                   149 
184         ret = mpi_write_to_sgl(m, req->dst, re    150         ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign);
185         if (ret)                                  151         if (ret)
186                 goto err_free_c;                  152                 goto err_free_c;
187                                                   153 
188         if (sign < 0)                             154         if (sign < 0)
189                 ret = -EBADMSG;                   155                 ret = -EBADMSG;
190 err_free_c:                                       156 err_free_c:
191         mpi_free(c);                              157         mpi_free(c);
192 err_free_m:                                       158 err_free_m:
193         mpi_free(m);                              159         mpi_free(m);
194         return ret;                               160         return ret;
195 }                                                 161 }
196                                                   162 
                                                   >> 163 static int rsa_sign(struct akcipher_request *req)
                                                   >> 164 {
                                                   >> 165         struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
                                                   >> 166         const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
                                                   >> 167         MPI m, s = mpi_alloc(0);
                                                   >> 168         int ret = 0;
                                                   >> 169         int sign;
                                                   >> 170 
                                                   >> 171         if (!s)
                                                   >> 172                 return -ENOMEM;
                                                   >> 173 
                                                   >> 174         if (unlikely(!pkey->n || !pkey->d)) {
                                                   >> 175                 ret = -EINVAL;
                                                   >> 176                 goto err_free_s;
                                                   >> 177         }
                                                   >> 178 
                                                   >> 179         ret = -ENOMEM;
                                                   >> 180         m = mpi_read_raw_from_sgl(req->src, req->src_len);
                                                   >> 181         if (!m)
                                                   >> 182                 goto err_free_s;
                                                   >> 183 
                                                   >> 184         ret = _rsa_sign(pkey, s, m);
                                                   >> 185         if (ret)
                                                   >> 186                 goto err_free_m;
                                                   >> 187 
                                                   >> 188         ret = mpi_write_to_sgl(s, req->dst, req->dst_len, &sign);
                                                   >> 189         if (ret)
                                                   >> 190                 goto err_free_m;
                                                   >> 191 
                                                   >> 192         if (sign < 0)
                                                   >> 193                 ret = -EBADMSG;
                                                   >> 194 
                                                   >> 195 err_free_m:
                                                   >> 196         mpi_free(m);
                                                   >> 197 err_free_s:
                                                   >> 198         mpi_free(s);
                                                   >> 199         return ret;
                                                   >> 200 }
                                                   >> 201 
                                                   >> 202 static int rsa_verify(struct akcipher_request *req)
                                                   >> 203 {
                                                   >> 204         struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
                                                   >> 205         const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
                                                   >> 206         MPI s, m = mpi_alloc(0);
                                                   >> 207         int ret = 0;
                                                   >> 208         int sign;
                                                   >> 209 
                                                   >> 210         if (!m)
                                                   >> 211                 return -ENOMEM;
                                                   >> 212 
                                                   >> 213         if (unlikely(!pkey->n || !pkey->e)) {
                                                   >> 214                 ret = -EINVAL;
                                                   >> 215                 goto err_free_m;
                                                   >> 216         }
                                                   >> 217 
                                                   >> 218         ret = -ENOMEM;
                                                   >> 219         s = mpi_read_raw_from_sgl(req->src, req->src_len);
                                                   >> 220         if (!s) {
                                                   >> 221                 ret = -ENOMEM;
                                                   >> 222                 goto err_free_m;
                                                   >> 223         }
                                                   >> 224 
                                                   >> 225         ret = _rsa_verify(pkey, m, s);
                                                   >> 226         if (ret)
                                                   >> 227                 goto err_free_s;
                                                   >> 228 
                                                   >> 229         ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign);
                                                   >> 230         if (ret)
                                                   >> 231                 goto err_free_s;
                                                   >> 232 
                                                   >> 233         if (sign < 0)
                                                   >> 234                 ret = -EBADMSG;
                                                   >> 235 
                                                   >> 236 err_free_s:
                                                   >> 237         mpi_free(s);
                                                   >> 238 err_free_m:
                                                   >> 239         mpi_free(m);
                                                   >> 240         return ret;
                                                   >> 241 }
                                                   >> 242 
197 static void rsa_free_mpi_key(struct rsa_mpi_ke    243 static void rsa_free_mpi_key(struct rsa_mpi_key *key)
198 {                                                 244 {
199         mpi_free(key->d);                         245         mpi_free(key->d);
200         mpi_free(key->e);                         246         mpi_free(key->e);
201         mpi_free(key->n);                         247         mpi_free(key->n);
202         mpi_free(key->p);                      << 
203         mpi_free(key->q);                      << 
204         mpi_free(key->dp);                     << 
205         mpi_free(key->dq);                     << 
206         mpi_free(key->qinv);                   << 
207         key->d = NULL;                            248         key->d = NULL;
208         key->e = NULL;                            249         key->e = NULL;
209         key->n = NULL;                            250         key->n = NULL;
210         key->p = NULL;                         << 
211         key->q = NULL;                         << 
212         key->dp = NULL;                        << 
213         key->dq = NULL;                        << 
214         key->qinv = NULL;                      << 
215 }                                                 251 }
216                                                   252 
217 static int rsa_check_key_length(unsigned int l    253 static int rsa_check_key_length(unsigned int len)
218 {                                                 254 {
219         switch (len) {                            255         switch (len) {
220         case 512:                                 256         case 512:
221         case 1024:                                257         case 1024:
222         case 1536:                                258         case 1536:
223                 if (fips_enabled)              << 
224                         return -EINVAL;        << 
225                 fallthrough;                   << 
226         case 2048:                                259         case 2048:
227         case 3072:                                260         case 3072:
228         case 4096:                                261         case 4096:
229                 return 0;                         262                 return 0;
230         }                                         263         }
231                                                   264 
232         return -EINVAL;                           265         return -EINVAL;
233 }                                                 266 }
234                                                   267 
235 static int rsa_check_exponent_fips(MPI e)      << 
236 {                                              << 
237         MPI e_max = NULL;                      << 
238         int err;                               << 
239                                                << 
240         /* check if odd */                     << 
241         if (!mpi_test_bit(e, 0)) {             << 
242                 return -EINVAL;                << 
243         }                                      << 
244                                                << 
245         /* check if 2^16 < e < 2^256. */       << 
246         if (mpi_cmp_ui(e, 65536) <= 0) {       << 
247                 return -EINVAL;                << 
248         }                                      << 
249                                                << 
250         e_max = mpi_alloc(0);                  << 
251         if (!e_max)                            << 
252                 return -ENOMEM;                << 
253                                                << 
254         err = mpi_set_bit(e_max, 256);         << 
255         if (err) {                             << 
256                 mpi_free(e_max);               << 
257                 return err;                    << 
258         }                                      << 
259                                                << 
260         if (mpi_cmp(e, e_max) >= 0) {          << 
261                 mpi_free(e_max);               << 
262                 return -EINVAL;                << 
263         }                                      << 
264                                                << 
265         mpi_free(e_max);                       << 
266         return 0;                              << 
267 }                                              << 
268                                                << 
269 static int rsa_set_pub_key(struct crypto_akcip    268 static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
270                            unsigned int keylen    269                            unsigned int keylen)
271 {                                                 270 {
272         struct rsa_mpi_key *mpi_key = akcipher    271         struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
273         struct rsa_key raw_key = {0};             272         struct rsa_key raw_key = {0};
274         int ret;                                  273         int ret;
275                                                   274 
276         /* Free the old MPI key if any */         275         /* Free the old MPI key if any */
277         rsa_free_mpi_key(mpi_key);                276         rsa_free_mpi_key(mpi_key);
278                                                   277 
279         ret = rsa_parse_pub_key(&raw_key, key,    278         ret = rsa_parse_pub_key(&raw_key, key, keylen);
280         if (ret)                                  279         if (ret)
281                 return ret;                       280                 return ret;
282                                                   281 
283         mpi_key->e = mpi_read_raw_data(raw_key    282         mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
284         if (!mpi_key->e)                          283         if (!mpi_key->e)
285                 goto err;                         284                 goto err;
286                                                   285 
287         mpi_key->n = mpi_read_raw_data(raw_key    286         mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
288         if (!mpi_key->n)                          287         if (!mpi_key->n)
289                 goto err;                         288                 goto err;
290                                                   289 
291         if (rsa_check_key_length(mpi_get_size(    290         if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
292                 rsa_free_mpi_key(mpi_key);        291                 rsa_free_mpi_key(mpi_key);
293                 return -EINVAL;                   292                 return -EINVAL;
294         }                                         293         }
295                                                   294 
296         if (fips_enabled && rsa_check_exponent << 
297                 rsa_free_mpi_key(mpi_key);     << 
298                 return -EINVAL;                << 
299         }                                      << 
300                                                << 
301         return 0;                                 295         return 0;
302                                                   296 
303 err:                                              297 err:
304         rsa_free_mpi_key(mpi_key);                298         rsa_free_mpi_key(mpi_key);
305         return -ENOMEM;                           299         return -ENOMEM;
306 }                                                 300 }
307                                                   301 
308 static int rsa_set_priv_key(struct crypto_akci    302 static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
309                             unsigned int keyle    303                             unsigned int keylen)
310 {                                                 304 {
311         struct rsa_mpi_key *mpi_key = akcipher    305         struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
312         struct rsa_key raw_key = {0};             306         struct rsa_key raw_key = {0};
313         int ret;                                  307         int ret;
314                                                   308 
315         /* Free the old MPI key if any */         309         /* Free the old MPI key if any */
316         rsa_free_mpi_key(mpi_key);                310         rsa_free_mpi_key(mpi_key);
317                                                   311 
318         ret = rsa_parse_priv_key(&raw_key, key    312         ret = rsa_parse_priv_key(&raw_key, key, keylen);
319         if (ret)                                  313         if (ret)
320                 return ret;                       314                 return ret;
321                                                   315 
322         mpi_key->d = mpi_read_raw_data(raw_key    316         mpi_key->d = mpi_read_raw_data(raw_key.d, raw_key.d_sz);
323         if (!mpi_key->d)                          317         if (!mpi_key->d)
324                 goto err;                         318                 goto err;
325                                                   319 
326         mpi_key->e = mpi_read_raw_data(raw_key    320         mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
327         if (!mpi_key->e)                          321         if (!mpi_key->e)
328                 goto err;                         322                 goto err;
329                                                   323 
330         mpi_key->n = mpi_read_raw_data(raw_key    324         mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
331         if (!mpi_key->n)                          325         if (!mpi_key->n)
332                 goto err;                         326                 goto err;
333                                                   327 
334         mpi_key->p = mpi_read_raw_data(raw_key << 
335         if (!mpi_key->p)                       << 
336                 goto err;                      << 
337                                                << 
338         mpi_key->q = mpi_read_raw_data(raw_key << 
339         if (!mpi_key->q)                       << 
340                 goto err;                      << 
341                                                << 
342         mpi_key->dp = mpi_read_raw_data(raw_ke << 
343         if (!mpi_key->dp)                      << 
344                 goto err;                      << 
345                                                << 
346         mpi_key->dq = mpi_read_raw_data(raw_ke << 
347         if (!mpi_key->dq)                      << 
348                 goto err;                      << 
349                                                << 
350         mpi_key->qinv = mpi_read_raw_data(raw_ << 
351         if (!mpi_key->qinv)                    << 
352                 goto err;                      << 
353                                                << 
354         if (rsa_check_key_length(mpi_get_size(    328         if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
355                 rsa_free_mpi_key(mpi_key);        329                 rsa_free_mpi_key(mpi_key);
356                 return -EINVAL;                   330                 return -EINVAL;
357         }                                         331         }
358                                                   332 
359         if (fips_enabled && rsa_check_exponent << 
360                 rsa_free_mpi_key(mpi_key);     << 
361                 return -EINVAL;                << 
362         }                                      << 
363                                                << 
364         return 0;                                 333         return 0;
365                                                   334 
366 err:                                              335 err:
367         rsa_free_mpi_key(mpi_key);                336         rsa_free_mpi_key(mpi_key);
368         return -ENOMEM;                           337         return -ENOMEM;
369 }                                                 338 }
370                                                   339 
371 static unsigned int rsa_max_size(struct crypto    340 static unsigned int rsa_max_size(struct crypto_akcipher *tfm)
372 {                                                 341 {
373         struct rsa_mpi_key *pkey = akcipher_tf    342         struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
374                                                   343 
375         return mpi_get_size(pkey->n);             344         return mpi_get_size(pkey->n);
376 }                                                 345 }
377                                                   346 
378 static void rsa_exit_tfm(struct crypto_akciphe    347 static void rsa_exit_tfm(struct crypto_akcipher *tfm)
379 {                                                 348 {
380         struct rsa_mpi_key *pkey = akcipher_tf    349         struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
381                                                   350 
382         rsa_free_mpi_key(pkey);                   351         rsa_free_mpi_key(pkey);
383 }                                                 352 }
384                                                   353 
385 static struct akcipher_alg rsa = {                354 static struct akcipher_alg rsa = {
386         .encrypt = rsa_enc,                       355         .encrypt = rsa_enc,
387         .decrypt = rsa_dec,                       356         .decrypt = rsa_dec,
                                                   >> 357         .sign = rsa_sign,
                                                   >> 358         .verify = rsa_verify,
388         .set_priv_key = rsa_set_priv_key,         359         .set_priv_key = rsa_set_priv_key,
389         .set_pub_key = rsa_set_pub_key,           360         .set_pub_key = rsa_set_pub_key,
390         .max_size = rsa_max_size,                 361         .max_size = rsa_max_size,
391         .exit = rsa_exit_tfm,                     362         .exit = rsa_exit_tfm,
392         .base = {                                 363         .base = {
393                 .cra_name = "rsa",                364                 .cra_name = "rsa",
394                 .cra_driver_name = "rsa-generi    365                 .cra_driver_name = "rsa-generic",
395                 .cra_priority = 100,              366                 .cra_priority = 100,
396                 .cra_module = THIS_MODULE,        367                 .cra_module = THIS_MODULE,
397                 .cra_ctxsize = sizeof(struct r    368                 .cra_ctxsize = sizeof(struct rsa_mpi_key),
398         },                                        369         },
399 };                                                370 };
400                                                   371 
401 static int __init rsa_init(void)               !! 372 static int rsa_init(void)
402 {                                                 373 {
403         int err;                                  374         int err;
404                                                   375 
405         err = crypto_register_akcipher(&rsa);     376         err = crypto_register_akcipher(&rsa);
406         if (err)                                  377         if (err)
407                 return err;                       378                 return err;
408                                                   379 
409         err = crypto_register_template(&rsa_pk    380         err = crypto_register_template(&rsa_pkcs1pad_tmpl);
410         if (err) {                                381         if (err) {
411                 crypto_unregister_akcipher(&rs    382                 crypto_unregister_akcipher(&rsa);
412                 return err;                       383                 return err;
413         }                                         384         }
414                                                   385 
415         return 0;                                 386         return 0;
416 }                                                 387 }
417                                                   388 
418 static void __exit rsa_exit(void)              !! 389 static void rsa_exit(void)
419 {                                                 390 {
420         crypto_unregister_template(&rsa_pkcs1p    391         crypto_unregister_template(&rsa_pkcs1pad_tmpl);
421         crypto_unregister_akcipher(&rsa);         392         crypto_unregister_akcipher(&rsa);
422 }                                                 393 }
423                                                   394 
424 subsys_initcall(rsa_init);                     !! 395 module_init(rsa_init);
425 module_exit(rsa_exit);                            396 module_exit(rsa_exit);
426 MODULE_ALIAS_CRYPTO("rsa");                       397 MODULE_ALIAS_CRYPTO("rsa");
427 MODULE_LICENSE("GPL");                            398 MODULE_LICENSE("GPL");
428 MODULE_DESCRIPTION("RSA generic algorithm");      399 MODULE_DESCRIPTION("RSA generic algorithm");
429                                                   400 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php