]>
Commit | Line | Data |
---|---|---|
cfc2bb32 TS |
1 | /* 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 | */ | |
21 | static 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 | */ | |
35 | static 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 | */ | |
49 | static 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 | */ | |
63 | static 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 | ||
73 | static inline struct rsa_key *rsa_get_key(struct crypto_akcipher *tfm) | |
74 | { | |
75 | return akcipher_tfm_ctx(tfm); | |
76 | } | |
77 | ||
78 | static 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 | ||
22287b0b TS |
94 | ret = -ENOMEM; |
95 | m = mpi_read_raw_from_sgl(req->src, req->src_len); | |
96 | if (!m) | |
cfc2bb32 | 97 | goto err_free_c; |
cfc2bb32 TS |
98 | |
99 | ret = _rsa_enc(pkey, c, m); | |
100 | if (ret) | |
101 | goto err_free_m; | |
102 | ||
22287b0b | 103 | ret = mpi_write_to_sgl(c, req->dst, &req->dst_len, &sign); |
cfc2bb32 TS |
104 | if (ret) |
105 | goto err_free_m; | |
106 | ||
22287b0b | 107 | if (sign < 0) |
cfc2bb32 | 108 | ret = -EBADMSG; |
cfc2bb32 TS |
109 | |
110 | err_free_m: | |
111 | mpi_free(m); | |
112 | err_free_c: | |
113 | mpi_free(c); | |
114 | return ret; | |
115 | } | |
116 | ||
117 | static int rsa_dec(struct akcipher_request *req) | |
118 | { | |
119 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | |
120 | const struct rsa_key *pkey = rsa_get_key(tfm); | |
121 | MPI c, m = mpi_alloc(0); | |
122 | int ret = 0; | |
123 | int sign; | |
124 | ||
125 | if (!m) | |
126 | return -ENOMEM; | |
127 | ||
128 | if (unlikely(!pkey->n || !pkey->d)) { | |
129 | ret = -EINVAL; | |
130 | goto err_free_m; | |
131 | } | |
132 | ||
22287b0b TS |
133 | ret = -ENOMEM; |
134 | c = mpi_read_raw_from_sgl(req->src, req->src_len); | |
135 | if (!c) | |
cfc2bb32 | 136 | goto err_free_m; |
cfc2bb32 TS |
137 | |
138 | ret = _rsa_dec(pkey, m, c); | |
139 | if (ret) | |
140 | goto err_free_c; | |
141 | ||
22287b0b | 142 | ret = mpi_write_to_sgl(m, req->dst, &req->dst_len, &sign); |
cfc2bb32 TS |
143 | if (ret) |
144 | goto err_free_c; | |
145 | ||
22287b0b | 146 | if (sign < 0) |
cfc2bb32 | 147 | ret = -EBADMSG; |
cfc2bb32 TS |
148 | err_free_c: |
149 | mpi_free(c); | |
150 | err_free_m: | |
151 | mpi_free(m); | |
152 | return ret; | |
153 | } | |
154 | ||
155 | static int rsa_sign(struct akcipher_request *req) | |
156 | { | |
157 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | |
158 | const struct rsa_key *pkey = rsa_get_key(tfm); | |
159 | MPI m, s = mpi_alloc(0); | |
160 | int ret = 0; | |
161 | int sign; | |
162 | ||
163 | if (!s) | |
164 | return -ENOMEM; | |
165 | ||
166 | if (unlikely(!pkey->n || !pkey->d)) { | |
167 | ret = -EINVAL; | |
168 | goto err_free_s; | |
169 | } | |
170 | ||
22287b0b TS |
171 | ret = -ENOMEM; |
172 | m = mpi_read_raw_from_sgl(req->src, req->src_len); | |
173 | if (!m) | |
cfc2bb32 | 174 | goto err_free_s; |
cfc2bb32 TS |
175 | |
176 | ret = _rsa_sign(pkey, s, m); | |
177 | if (ret) | |
178 | goto err_free_m; | |
179 | ||
22287b0b | 180 | ret = mpi_write_to_sgl(s, req->dst, &req->dst_len, &sign); |
cfc2bb32 TS |
181 | if (ret) |
182 | goto err_free_m; | |
183 | ||
22287b0b | 184 | if (sign < 0) |
cfc2bb32 | 185 | ret = -EBADMSG; |
cfc2bb32 TS |
186 | |
187 | err_free_m: | |
188 | mpi_free(m); | |
189 | err_free_s: | |
190 | mpi_free(s); | |
191 | return ret; | |
192 | } | |
193 | ||
194 | static int rsa_verify(struct akcipher_request *req) | |
195 | { | |
196 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | |
197 | const struct rsa_key *pkey = rsa_get_key(tfm); | |
198 | MPI s, m = mpi_alloc(0); | |
199 | int ret = 0; | |
200 | int sign; | |
201 | ||
202 | if (!m) | |
203 | return -ENOMEM; | |
204 | ||
205 | if (unlikely(!pkey->n || !pkey->e)) { | |
206 | ret = -EINVAL; | |
207 | goto err_free_m; | |
208 | } | |
209 | ||
22287b0b TS |
210 | ret = -ENOMEM; |
211 | s = mpi_read_raw_from_sgl(req->src, req->src_len); | |
cfc2bb32 TS |
212 | if (!s) { |
213 | ret = -ENOMEM; | |
214 | goto err_free_m; | |
215 | } | |
216 | ||
217 | ret = _rsa_verify(pkey, m, s); | |
218 | if (ret) | |
219 | goto err_free_s; | |
220 | ||
22287b0b | 221 | ret = mpi_write_to_sgl(m, req->dst, &req->dst_len, &sign); |
cfc2bb32 TS |
222 | if (ret) |
223 | goto err_free_s; | |
224 | ||
22287b0b | 225 | if (sign < 0) |
cfc2bb32 | 226 | ret = -EBADMSG; |
cfc2bb32 TS |
227 | |
228 | err_free_s: | |
229 | mpi_free(s); | |
230 | err_free_m: | |
231 | mpi_free(m); | |
232 | return ret; | |
233 | } | |
234 | ||
6e8ec66c TS |
235 | static int rsa_check_key_length(unsigned int len) |
236 | { | |
237 | switch (len) { | |
238 | case 512: | |
239 | case 1024: | |
240 | case 1536: | |
241 | case 2048: | |
242 | case 3072: | |
243 | case 4096: | |
244 | return 0; | |
245 | } | |
246 | ||
247 | return -EINVAL; | |
248 | } | |
249 | ||
22287b0b TS |
250 | static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, |
251 | unsigned int keylen) | |
cfc2bb32 TS |
252 | { |
253 | struct rsa_key *pkey = akcipher_tfm_ctx(tfm); | |
6e8ec66c | 254 | int ret; |
cfc2bb32 | 255 | |
22287b0b | 256 | ret = rsa_parse_pub_key(pkey, key, keylen); |
6e8ec66c TS |
257 | if (ret) |
258 | return ret; | |
259 | ||
260 | if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) { | |
261 | rsa_free_key(pkey); | |
262 | ret = -EINVAL; | |
263 | } | |
264 | return ret; | |
cfc2bb32 TS |
265 | } |
266 | ||
22287b0b TS |
267 | static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, |
268 | unsigned int keylen) | |
269 | { | |
270 | struct rsa_key *pkey = akcipher_tfm_ctx(tfm); | |
271 | int ret; | |
272 | ||
273 | ret = rsa_parse_priv_key(pkey, key, keylen); | |
274 | if (ret) | |
275 | return ret; | |
276 | ||
277 | if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) { | |
278 | rsa_free_key(pkey); | |
279 | ret = -EINVAL; | |
280 | } | |
281 | return ret; | |
282 | } | |
283 | ||
284 | static int rsa_max_size(struct crypto_akcipher *tfm) | |
285 | { | |
286 | struct rsa_key *pkey = akcipher_tfm_ctx(tfm); | |
287 | ||
288 | return pkey->n ? mpi_get_size(pkey->n) : -EINVAL; | |
289 | } | |
290 | ||
cfc2bb32 TS |
291 | static void rsa_exit_tfm(struct crypto_akcipher *tfm) |
292 | { | |
293 | struct rsa_key *pkey = akcipher_tfm_ctx(tfm); | |
294 | ||
295 | rsa_free_key(pkey); | |
296 | } | |
297 | ||
298 | static struct akcipher_alg rsa = { | |
299 | .encrypt = rsa_enc, | |
300 | .decrypt = rsa_dec, | |
301 | .sign = rsa_sign, | |
302 | .verify = rsa_verify, | |
22287b0b TS |
303 | .set_priv_key = rsa_set_priv_key, |
304 | .set_pub_key = rsa_set_pub_key, | |
305 | .max_size = rsa_max_size, | |
cfc2bb32 TS |
306 | .exit = rsa_exit_tfm, |
307 | .base = { | |
308 | .cra_name = "rsa", | |
309 | .cra_driver_name = "rsa-generic", | |
310 | .cra_priority = 100, | |
311 | .cra_module = THIS_MODULE, | |
312 | .cra_ctxsize = sizeof(struct rsa_key), | |
313 | }, | |
314 | }; | |
315 | ||
316 | static int rsa_init(void) | |
317 | { | |
318 | return crypto_register_akcipher(&rsa); | |
319 | } | |
320 | ||
321 | static void rsa_exit(void) | |
322 | { | |
323 | crypto_unregister_akcipher(&rsa); | |
324 | } | |
325 | ||
326 | module_init(rsa_init); | |
327 | module_exit(rsa_exit); | |
328 | MODULE_ALIAS_CRYPTO("rsa"); | |
329 | MODULE_LICENSE("GPL"); | |
330 | MODULE_DESCRIPTION("RSA generic algorithm"); |