iommu/vt-d: Clean up pasid_enabled() and ecs_enabled() dependencies
[linux/fpc-iii.git] / crypto / rsa.c
blob466003e1a8cf20b501425ef8049cdc71b3f262bb
1 /* RSA asymmetric public-key algorithm [RFC3447]
3 * Copyright (c) 2015, Intel Corporation
4 * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
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.
12 #include <linux/module.h>
13 #include <crypto/internal/rsa.h>
14 #include <crypto/internal/akcipher.h>
15 #include <crypto/akcipher.h>
18 * RSAEP function [RFC3447 sec 5.1.1]
19 * c = m^e mod n;
21 static int _rsa_enc(const struct rsa_key *key, MPI c, MPI m)
23 /* (1) Validate 0 <= m < n */
24 if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
25 return -EINVAL;
27 /* (2) c = m^e mod n */
28 return mpi_powm(c, m, key->e, key->n);
32 * RSADP function [RFC3447 sec 5.1.2]
33 * m = c^d mod n;
35 static int _rsa_dec(const struct rsa_key *key, MPI m, MPI c)
37 /* (1) Validate 0 <= c < n */
38 if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0)
39 return -EINVAL;
41 /* (2) m = c^d mod n */
42 return mpi_powm(m, c, key->d, key->n);
46 * RSASP1 function [RFC3447 sec 5.2.1]
47 * s = m^d mod n
49 static int _rsa_sign(const struct rsa_key *key, MPI s, MPI m)
51 /* (1) Validate 0 <= m < n */
52 if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
53 return -EINVAL;
55 /* (2) s = m^d mod n */
56 return mpi_powm(s, m, key->d, key->n);
60 * RSAVP1 function [RFC3447 sec 5.2.2]
61 * m = s^e mod n;
63 static int _rsa_verify(const struct rsa_key *key, MPI m, MPI s)
65 /* (1) Validate 0 <= s < n */
66 if (mpi_cmp_ui(s, 0) < 0 || mpi_cmp(s, key->n) >= 0)
67 return -EINVAL;
69 /* (2) m = s^e mod n */
70 return mpi_powm(m, s, key->e, key->n);
73 static inline struct rsa_key *rsa_get_key(struct crypto_akcipher *tfm)
75 return akcipher_tfm_ctx(tfm);
78 static int rsa_enc(struct akcipher_request *req)
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;
86 if (!c)
87 return -ENOMEM;
89 if (unlikely(!pkey->n || !pkey->e)) {
90 ret = -EINVAL;
91 goto err_free_c;
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;
100 m = mpi_read_raw_data(req->src, req->src_len);
101 if (!m) {
102 ret = -ENOMEM;
103 goto err_free_c;
106 ret = _rsa_enc(pkey, c, m);
107 if (ret)
108 goto err_free_m;
110 ret = mpi_read_buffer(c, req->dst, req->dst_len, &req->dst_len, &sign);
111 if (ret)
112 goto err_free_m;
114 if (sign < 0) {
115 ret = -EBADMSG;
116 goto err_free_m;
119 err_free_m:
120 mpi_free(m);
121 err_free_c:
122 mpi_free(c);
123 return ret;
126 static int rsa_dec(struct akcipher_request *req)
128 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
129 const struct rsa_key *pkey = rsa_get_key(tfm);
130 MPI c, m = mpi_alloc(0);
131 int ret = 0;
132 int sign;
134 if (!m)
135 return -ENOMEM;
137 if (unlikely(!pkey->n || !pkey->d)) {
138 ret = -EINVAL;
139 goto err_free_m;
142 if (req->dst_len < mpi_get_size(pkey->n)) {
143 req->dst_len = mpi_get_size(pkey->n);
144 ret = -EOVERFLOW;
145 goto err_free_m;
148 c = mpi_read_raw_data(req->src, req->src_len);
149 if (!c) {
150 ret = -ENOMEM;
151 goto err_free_m;
154 ret = _rsa_dec(pkey, m, c);
155 if (ret)
156 goto err_free_c;
158 ret = mpi_read_buffer(m, req->dst, req->dst_len, &req->dst_len, &sign);
159 if (ret)
160 goto err_free_c;
162 if (sign < 0) {
163 ret = -EBADMSG;
164 goto err_free_c;
167 err_free_c:
168 mpi_free(c);
169 err_free_m:
170 mpi_free(m);
171 return ret;
174 static int rsa_sign(struct akcipher_request *req)
176 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
177 const struct rsa_key *pkey = rsa_get_key(tfm);
178 MPI m, s = mpi_alloc(0);
179 int ret = 0;
180 int sign;
182 if (!s)
183 return -ENOMEM;
185 if (unlikely(!pkey->n || !pkey->d)) {
186 ret = -EINVAL;
187 goto err_free_s;
190 if (req->dst_len < mpi_get_size(pkey->n)) {
191 req->dst_len = mpi_get_size(pkey->n);
192 ret = -EOVERFLOW;
193 goto err_free_s;
196 m = mpi_read_raw_data(req->src, req->src_len);
197 if (!m) {
198 ret = -ENOMEM;
199 goto err_free_s;
202 ret = _rsa_sign(pkey, s, m);
203 if (ret)
204 goto err_free_m;
206 ret = mpi_read_buffer(s, req->dst, req->dst_len, &req->dst_len, &sign);
207 if (ret)
208 goto err_free_m;
210 if (sign < 0) {
211 ret = -EBADMSG;
212 goto err_free_m;
215 err_free_m:
216 mpi_free(m);
217 err_free_s:
218 mpi_free(s);
219 return ret;
222 static int rsa_verify(struct akcipher_request *req)
224 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
225 const struct rsa_key *pkey = rsa_get_key(tfm);
226 MPI s, m = mpi_alloc(0);
227 int ret = 0;
228 int sign;
230 if (!m)
231 return -ENOMEM;
233 if (unlikely(!pkey->n || !pkey->e)) {
234 ret = -EINVAL;
235 goto err_free_m;
238 if (req->dst_len < mpi_get_size(pkey->n)) {
239 req->dst_len = mpi_get_size(pkey->n);
240 ret = -EOVERFLOW;
241 goto err_free_m;
244 s = mpi_read_raw_data(req->src, req->src_len);
245 if (!s) {
246 ret = -ENOMEM;
247 goto err_free_m;
250 ret = _rsa_verify(pkey, m, s);
251 if (ret)
252 goto err_free_s;
254 ret = mpi_read_buffer(m, req->dst, req->dst_len, &req->dst_len, &sign);
255 if (ret)
256 goto err_free_s;
258 if (sign < 0) {
259 ret = -EBADMSG;
260 goto err_free_s;
263 err_free_s:
264 mpi_free(s);
265 err_free_m:
266 mpi_free(m);
267 return ret;
270 static int rsa_check_key_length(unsigned int len)
272 switch (len) {
273 case 512:
274 case 1024:
275 case 1536:
276 case 2048:
277 case 3072:
278 case 4096:
279 return 0;
282 return -EINVAL;
285 static int rsa_setkey(struct crypto_akcipher *tfm, const void *key,
286 unsigned int keylen)
288 struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
289 int ret;
291 ret = rsa_parse_key(pkey, key, keylen);
292 if (ret)
293 return ret;
295 if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) {
296 rsa_free_key(pkey);
297 ret = -EINVAL;
299 return ret;
302 static void rsa_exit_tfm(struct crypto_akcipher *tfm)
304 struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
306 rsa_free_key(pkey);
309 static struct akcipher_alg rsa = {
310 .encrypt = rsa_enc,
311 .decrypt = rsa_dec,
312 .sign = rsa_sign,
313 .verify = rsa_verify,
314 .setkey = rsa_setkey,
315 .exit = rsa_exit_tfm,
316 .base = {
317 .cra_name = "rsa",
318 .cra_driver_name = "rsa-generic",
319 .cra_priority = 100,
320 .cra_module = THIS_MODULE,
321 .cra_ctxsize = sizeof(struct rsa_key),
325 static int rsa_init(void)
327 return crypto_register_akcipher(&rsa);
330 static void rsa_exit(void)
332 crypto_unregister_akcipher(&rsa);
335 module_init(rsa_init);
336 module_exit(rsa_exit);
337 MODULE_ALIAS_CRYPTO("rsa");
338 MODULE_LICENSE("GPL");
339 MODULE_DESCRIPTION("RSA generic algorithm");