blob: 1093e041db03b98549a0e7abcd6557683068ba11 [file] [log] [blame]
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -07001/* RSA asymmetric public-key algorithm [RFC3447]
2 *
3 * Copyright (c) 2015, Intel Corporation
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.
10 */
11
12#include <linux/module.h>
13#include <crypto/internal/rsa.h>
14#include <crypto/internal/akcipher.h>
15#include <crypto/akcipher.h>
16
17/*
18 * RSAEP function [RFC3447 sec 5.1.1]
19 * c = m^e mod n;
20 */
21static int _rsa_enc(const struct rsa_key *key, MPI c, MPI m)
22{
23 /* (1) Validate 0 <= m < n */
24 if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
25 return -EINVAL;
26
27 /* (2) c = m^e mod n */
28 return mpi_powm(c, m, key->e, key->n);
29}
30
31/*
32 * RSADP function [RFC3447 sec 5.1.2]
33 * m = c^d mod n;
34 */
35static int _rsa_dec(const struct rsa_key *key, MPI m, MPI c)
36{
37 /* (1) Validate 0 <= c < n */
38 if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0)
39 return -EINVAL;
40
41 /* (2) m = c^d mod n */
42 return mpi_powm(m, c, key->d, key->n);
43}
44
45/*
46 * RSASP1 function [RFC3447 sec 5.2.1]
47 * s = m^d mod n
48 */
49static int _rsa_sign(const struct rsa_key *key, MPI s, MPI m)
50{
51 /* (1) Validate 0 <= m < n */
52 if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
53 return -EINVAL;
54
55 /* (2) s = m^d mod n */
56 return mpi_powm(s, m, key->d, key->n);
57}
58
59/*
60 * RSAVP1 function [RFC3447 sec 5.2.2]
61 * m = s^e mod n;
62 */
63static int _rsa_verify(const struct rsa_key *key, MPI m, MPI s)
64{
65 /* (1) Validate 0 <= s < n */
66 if (mpi_cmp_ui(s, 0) < 0 || mpi_cmp(s, key->n) >= 0)
67 return -EINVAL;
68
69 /* (2) m = s^e mod n */
70 return mpi_powm(m, s, key->e, key->n);
71}
72
73static inline struct rsa_key *rsa_get_key(struct crypto_akcipher *tfm)
74{
75 return akcipher_tfm_ctx(tfm);
76}
77
78static int rsa_enc(struct akcipher_request *req)
79{
80 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
81 const struct rsa_key *pkey = rsa_get_key(tfm);
82 MPI m, c = mpi_alloc(0);
83 int ret = 0;
84 int sign;
85
86 if (!c)
87 return -ENOMEM;
88
89 if (unlikely(!pkey->n || !pkey->e)) {
90 ret = -EINVAL;
91 goto err_free_c;
92 }
93
94 if (req->dst_len < mpi_get_size(pkey->n)) {
95 req->dst_len = mpi_get_size(pkey->n);
96 ret = -EOVERFLOW;
97 goto err_free_c;
98 }
99
Tadeusz Struk22287b02015-10-08 09:26:55 -0700100 ret = -ENOMEM;
101 m = mpi_read_raw_from_sgl(req->src, req->src_len);
102 if (!m)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700103 goto err_free_c;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700104
105 ret = _rsa_enc(pkey, c, m);
106 if (ret)
107 goto err_free_m;
108
Tadeusz Struk22287b02015-10-08 09:26:55 -0700109 ret = mpi_write_to_sgl(c, req->dst, &req->dst_len, &sign);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700110 if (ret)
111 goto err_free_m;
112
Tadeusz Struk22287b02015-10-08 09:26:55 -0700113 if (sign < 0)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700114 ret = -EBADMSG;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700115
116err_free_m:
117 mpi_free(m);
118err_free_c:
119 mpi_free(c);
120 return ret;
121}
122
123static int rsa_dec(struct akcipher_request *req)
124{
125 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
126 const struct rsa_key *pkey = rsa_get_key(tfm);
127 MPI c, m = mpi_alloc(0);
128 int ret = 0;
129 int sign;
130
131 if (!m)
132 return -ENOMEM;
133
134 if (unlikely(!pkey->n || !pkey->d)) {
135 ret = -EINVAL;
136 goto err_free_m;
137 }
138
139 if (req->dst_len < mpi_get_size(pkey->n)) {
140 req->dst_len = mpi_get_size(pkey->n);
141 ret = -EOVERFLOW;
142 goto err_free_m;
143 }
144
Tadeusz Struk22287b02015-10-08 09:26:55 -0700145 ret = -ENOMEM;
146 c = mpi_read_raw_from_sgl(req->src, req->src_len);
147 if (!c)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700148 goto err_free_m;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700149
150 ret = _rsa_dec(pkey, m, c);
151 if (ret)
152 goto err_free_c;
153
Tadeusz Struk22287b02015-10-08 09:26:55 -0700154 ret = mpi_write_to_sgl(m, req->dst, &req->dst_len, &sign);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700155 if (ret)
156 goto err_free_c;
157
Tadeusz Struk22287b02015-10-08 09:26:55 -0700158 if (sign < 0)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700159 ret = -EBADMSG;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700160err_free_c:
161 mpi_free(c);
162err_free_m:
163 mpi_free(m);
164 return ret;
165}
166
167static int rsa_sign(struct akcipher_request *req)
168{
169 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
170 const struct rsa_key *pkey = rsa_get_key(tfm);
171 MPI m, s = mpi_alloc(0);
172 int ret = 0;
173 int sign;
174
175 if (!s)
176 return -ENOMEM;
177
178 if (unlikely(!pkey->n || !pkey->d)) {
179 ret = -EINVAL;
180 goto err_free_s;
181 }
182
183 if (req->dst_len < mpi_get_size(pkey->n)) {
184 req->dst_len = mpi_get_size(pkey->n);
185 ret = -EOVERFLOW;
186 goto err_free_s;
187 }
188
Tadeusz Struk22287b02015-10-08 09:26:55 -0700189 ret = -ENOMEM;
190 m = mpi_read_raw_from_sgl(req->src, req->src_len);
191 if (!m)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700192 goto err_free_s;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700193
194 ret = _rsa_sign(pkey, s, m);
195 if (ret)
196 goto err_free_m;
197
Tadeusz Struk22287b02015-10-08 09:26:55 -0700198 ret = mpi_write_to_sgl(s, req->dst, &req->dst_len, &sign);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700199 if (ret)
200 goto err_free_m;
201
Tadeusz Struk22287b02015-10-08 09:26:55 -0700202 if (sign < 0)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700203 ret = -EBADMSG;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700204
205err_free_m:
206 mpi_free(m);
207err_free_s:
208 mpi_free(s);
209 return ret;
210}
211
212static int rsa_verify(struct akcipher_request *req)
213{
214 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
215 const struct rsa_key *pkey = rsa_get_key(tfm);
216 MPI s, m = mpi_alloc(0);
217 int ret = 0;
218 int sign;
219
220 if (!m)
221 return -ENOMEM;
222
223 if (unlikely(!pkey->n || !pkey->e)) {
224 ret = -EINVAL;
225 goto err_free_m;
226 }
227
228 if (req->dst_len < mpi_get_size(pkey->n)) {
229 req->dst_len = mpi_get_size(pkey->n);
230 ret = -EOVERFLOW;
231 goto err_free_m;
232 }
233
Tadeusz Struk22287b02015-10-08 09:26:55 -0700234 ret = -ENOMEM;
235 s = mpi_read_raw_from_sgl(req->src, req->src_len);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700236 if (!s) {
237 ret = -ENOMEM;
238 goto err_free_m;
239 }
240
241 ret = _rsa_verify(pkey, m, s);
242 if (ret)
243 goto err_free_s;
244
Tadeusz Struk22287b02015-10-08 09:26:55 -0700245 ret = mpi_write_to_sgl(m, req->dst, &req->dst_len, &sign);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700246 if (ret)
247 goto err_free_s;
248
Tadeusz Struk22287b02015-10-08 09:26:55 -0700249 if (sign < 0)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700250 ret = -EBADMSG;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700251
252err_free_s:
253 mpi_free(s);
254err_free_m:
255 mpi_free(m);
256 return ret;
257}
258
Tadeusz Struk6e8ec662015-07-15 15:28:43 -0700259static int rsa_check_key_length(unsigned int len)
260{
261 switch (len) {
262 case 512:
263 case 1024:
264 case 1536:
265 case 2048:
266 case 3072:
267 case 4096:
268 return 0;
269 }
270
271 return -EINVAL;
272}
273
Tadeusz Struk22287b02015-10-08 09:26:55 -0700274static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
275 unsigned int keylen)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700276{
277 struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
Tadeusz Struk6e8ec662015-07-15 15:28:43 -0700278 int ret;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700279
Tadeusz Struk22287b02015-10-08 09:26:55 -0700280 ret = rsa_parse_pub_key(pkey, key, keylen);
Tadeusz Struk6e8ec662015-07-15 15:28:43 -0700281 if (ret)
282 return ret;
283
284 if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) {
285 rsa_free_key(pkey);
286 ret = -EINVAL;
287 }
288 return ret;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700289}
290
Tadeusz Struk22287b02015-10-08 09:26:55 -0700291static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
292 unsigned int keylen)
293{
294 struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
295 int ret;
296
297 ret = rsa_parse_priv_key(pkey, key, keylen);
298 if (ret)
299 return ret;
300
301 if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) {
302 rsa_free_key(pkey);
303 ret = -EINVAL;
304 }
305 return ret;
306}
307
308static int rsa_max_size(struct crypto_akcipher *tfm)
309{
310 struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
311
312 return pkey->n ? mpi_get_size(pkey->n) : -EINVAL;
313}
314
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700315static void rsa_exit_tfm(struct crypto_akcipher *tfm)
316{
317 struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
318
319 rsa_free_key(pkey);
320}
321
322static struct akcipher_alg rsa = {
323 .encrypt = rsa_enc,
324 .decrypt = rsa_dec,
325 .sign = rsa_sign,
326 .verify = rsa_verify,
Tadeusz Struk22287b02015-10-08 09:26:55 -0700327 .set_priv_key = rsa_set_priv_key,
328 .set_pub_key = rsa_set_pub_key,
329 .max_size = rsa_max_size,
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700330 .exit = rsa_exit_tfm,
331 .base = {
332 .cra_name = "rsa",
333 .cra_driver_name = "rsa-generic",
334 .cra_priority = 100,
335 .cra_module = THIS_MODULE,
336 .cra_ctxsize = sizeof(struct rsa_key),
337 },
338};
339
340static int rsa_init(void)
341{
342 return crypto_register_akcipher(&rsa);
343}
344
345static void rsa_exit(void)
346{
347 crypto_unregister_akcipher(&rsa);
348}
349
350module_init(rsa_init);
351module_exit(rsa_exit);
352MODULE_ALIAS_CRYPTO("rsa");
353MODULE_LICENSE("GPL");
354MODULE_DESCRIPTION("RSA generic algorithm");