summaryrefslogtreecommitdiffstats
path: root/crypto/rsa.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/rsa.c')
-rw-r--r--crypto/rsa.c111
1 files changed, 1 insertions, 110 deletions
diff --git a/crypto/rsa.c b/crypto/rsa.c
index 4167980c243d..dcbb03431778 100644
--- a/crypto/rsa.c
+++ b/crypto/rsa.c
@@ -50,34 +50,6 @@ static int _rsa_dec(const struct rsa_mpi_key *key, MPI m, MPI c)
50 return mpi_powm(m, c, key->d, key->n); 50 return mpi_powm(m, c, key->d, key->n);
51} 51}
52 52
53/*
54 * RSASP1 function [RFC3447 sec 5.2.1]
55 * s = m^d mod n
56 */
57static 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;
62
63 /* (2) s = m^d mod n */
64 return mpi_powm(s, m, key->d, key->n);
65}
66
67/*
68 * RSAVP1 function [RFC3447 sec 5.2.2]
69 * m = s^e mod n;
70 */
71static int _rsa_verify(const struct rsa_mpi_key *key, MPI m, MPI s)
72{
73 /* (1) Validate 0 <= s < n */
74 if (mpi_cmp_ui(s, 0) < 0 || mpi_cmp(s, key->n) >= 0)
75 return -EINVAL;
76
77 /* (2) m = s^e mod n */
78 return mpi_powm(m, s, key->e, key->n);
79}
80
81static inline struct rsa_mpi_key *rsa_get_key(struct crypto_akcipher *tfm) 53static inline struct rsa_mpi_key *rsa_get_key(struct crypto_akcipher *tfm)
82{ 54{
83 return akcipher_tfm_ctx(tfm); 55 return akcipher_tfm_ctx(tfm);
@@ -160,85 +132,6 @@ err_free_m:
160 return ret; 132 return ret;
161} 133}
162 134
163static 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
195err_free_m:
196 mpi_free(m);
197err_free_s:
198 mpi_free(s);
199 return ret;
200}
201
202static 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 s = mpi_read_raw_from_sgl(req->src, req->src_len);
219 if (!s) {
220 ret = -ENOMEM;
221 goto err_free_m;
222 }
223
224 ret = _rsa_verify(pkey, m, s);
225 if (ret)
226 goto err_free_s;
227
228 ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign);
229 if (ret)
230 goto err_free_s;
231
232 if (sign < 0)
233 ret = -EBADMSG;
234
235err_free_s:
236 mpi_free(s);
237err_free_m:
238 mpi_free(m);
239 return ret;
240}
241
242static void rsa_free_mpi_key(struct rsa_mpi_key *key) 135static void rsa_free_mpi_key(struct rsa_mpi_key *key)
243{ 136{
244 mpi_free(key->d); 137 mpi_free(key->d);
@@ -353,8 +246,6 @@ static void rsa_exit_tfm(struct crypto_akcipher *tfm)
353static struct akcipher_alg rsa = { 246static struct akcipher_alg rsa = {
354 .encrypt = rsa_enc, 247 .encrypt = rsa_enc,
355 .decrypt = rsa_dec, 248 .decrypt = rsa_dec,
356 .sign = rsa_sign,
357 .verify = rsa_verify,
358 .set_priv_key = rsa_set_priv_key, 249 .set_priv_key = rsa_set_priv_key,
359 .set_pub_key = rsa_set_pub_key, 250 .set_pub_key = rsa_set_pub_key,
360 .max_size = rsa_max_size, 251 .max_size = rsa_max_size,
@@ -391,7 +282,7 @@ static void rsa_exit(void)
391 crypto_unregister_akcipher(&rsa); 282 crypto_unregister_akcipher(&rsa);
392} 283}
393 284
394module_init(rsa_init); 285subsys_initcall(rsa_init);
395module_exit(rsa_exit); 286module_exit(rsa_exit);
396MODULE_ALIAS_CRYPTO("rsa"); 287MODULE_ALIAS_CRYPTO("rsa");
397MODULE_LICENSE("GPL"); 288MODULE_LICENSE("GPL");