blob: dcbb03431778ce74d9c28e10ee45009b70186276 [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>
Tudor Ambarus5a7de972016-06-14 16:14:58 +030013#include <linux/mpi.h>
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070014#include <crypto/internal/rsa.h>
15#include <crypto/internal/akcipher.h>
16#include <crypto/akcipher.h>
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +010017#include <crypto/algapi.h>
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070018
Tudor Ambarus5a7de972016-06-14 16:14:58 +030019struct rsa_mpi_key {
20 MPI n;
21 MPI e;
22 MPI d;
23};
24
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070025/*
26 * RSAEP function [RFC3447 sec 5.1.1]
27 * c = m^e mod n;
28 */
Tudor Ambarus5a7de972016-06-14 16:14:58 +030029static int _rsa_enc(const struct rsa_mpi_key *key, MPI c, MPI m)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070030{
31 /* (1) Validate 0 <= m < n */
32 if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
33 return -EINVAL;
34
35 /* (2) c = m^e mod n */
36 return mpi_powm(c, m, key->e, key->n);
37}
38
39/*
40 * RSADP function [RFC3447 sec 5.1.2]
41 * m = c^d mod n;
42 */
Tudor Ambarus5a7de972016-06-14 16:14:58 +030043static int _rsa_dec(const struct rsa_mpi_key *key, MPI m, MPI c)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070044{
45 /* (1) Validate 0 <= c < n */
46 if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0)
47 return -EINVAL;
48
49 /* (2) m = c^d mod n */
50 return mpi_powm(m, c, key->d, key->n);
51}
52
Tudor Ambarus5a7de972016-06-14 16:14:58 +030053static inline struct rsa_mpi_key *rsa_get_key(struct crypto_akcipher *tfm)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070054{
55 return akcipher_tfm_ctx(tfm);
56}
57
58static int rsa_enc(struct akcipher_request *req)
59{
60 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
Tudor Ambarus5a7de972016-06-14 16:14:58 +030061 const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070062 MPI m, c = mpi_alloc(0);
63 int ret = 0;
64 int sign;
65
66 if (!c)
67 return -ENOMEM;
68
69 if (unlikely(!pkey->n || !pkey->e)) {
70 ret = -EINVAL;
71 goto err_free_c;
72 }
73
Tadeusz Struk22287b02015-10-08 09:26:55 -070074 ret = -ENOMEM;
75 m = mpi_read_raw_from_sgl(req->src, req->src_len);
76 if (!m)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070077 goto err_free_c;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070078
79 ret = _rsa_enc(pkey, c, m);
80 if (ret)
81 goto err_free_m;
82
Herbert Xu9b45b7b2016-06-29 19:32:21 +080083 ret = mpi_write_to_sgl(c, req->dst, req->dst_len, &sign);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070084 if (ret)
85 goto err_free_m;
86
Tadeusz Struk22287b02015-10-08 09:26:55 -070087 if (sign < 0)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070088 ret = -EBADMSG;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -070089
90err_free_m:
91 mpi_free(m);
92err_free_c:
93 mpi_free(c);
94 return ret;
95}
96
97static int rsa_dec(struct akcipher_request *req)
98{
99 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300100 const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700101 MPI c, m = mpi_alloc(0);
102 int ret = 0;
103 int sign;
104
105 if (!m)
106 return -ENOMEM;
107
108 if (unlikely(!pkey->n || !pkey->d)) {
109 ret = -EINVAL;
110 goto err_free_m;
111 }
112
Tadeusz Struk22287b02015-10-08 09:26:55 -0700113 ret = -ENOMEM;
114 c = mpi_read_raw_from_sgl(req->src, req->src_len);
115 if (!c)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700116 goto err_free_m;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700117
118 ret = _rsa_dec(pkey, m, c);
119 if (ret)
120 goto err_free_c;
121
Herbert Xu9b45b7b2016-06-29 19:32:21 +0800122 ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700123 if (ret)
124 goto err_free_c;
125
Tadeusz Struk22287b02015-10-08 09:26:55 -0700126 if (sign < 0)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700127 ret = -EBADMSG;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700128err_free_c:
129 mpi_free(c);
130err_free_m:
131 mpi_free(m);
132 return ret;
133}
134
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300135static void rsa_free_mpi_key(struct rsa_mpi_key *key)
136{
137 mpi_free(key->d);
138 mpi_free(key->e);
139 mpi_free(key->n);
140 key->d = NULL;
141 key->e = NULL;
142 key->n = NULL;
143}
144
Tadeusz Struk6e8ec662015-07-15 15:28:43 -0700145static int rsa_check_key_length(unsigned int len)
146{
147 switch (len) {
148 case 512:
149 case 1024:
150 case 1536:
151 case 2048:
152 case 3072:
153 case 4096:
154 return 0;
155 }
156
157 return -EINVAL;
158}
159
Tadeusz Struk22287b02015-10-08 09:26:55 -0700160static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
161 unsigned int keylen)
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700162{
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300163 struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
164 struct rsa_key raw_key = {0};
Tadeusz Struk6e8ec662015-07-15 15:28:43 -0700165 int ret;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700166
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300167 /* Free the old MPI key if any */
168 rsa_free_mpi_key(mpi_key);
169
170 ret = rsa_parse_pub_key(&raw_key, key, keylen);
Tadeusz Struk6e8ec662015-07-15 15:28:43 -0700171 if (ret)
172 return ret;
173
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300174 mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
175 if (!mpi_key->e)
176 goto err;
177
178 mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
179 if (!mpi_key->n)
180 goto err;
181
182 if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
183 rsa_free_mpi_key(mpi_key);
184 return -EINVAL;
Tadeusz Struk6e8ec662015-07-15 15:28:43 -0700185 }
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300186
187 return 0;
188
189err:
190 rsa_free_mpi_key(mpi_key);
191 return -ENOMEM;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700192}
193
Tadeusz Struk22287b02015-10-08 09:26:55 -0700194static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
195 unsigned int keylen)
196{
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300197 struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
198 struct rsa_key raw_key = {0};
Tadeusz Struk22287b02015-10-08 09:26:55 -0700199 int ret;
200
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300201 /* Free the old MPI key if any */
202 rsa_free_mpi_key(mpi_key);
203
204 ret = rsa_parse_priv_key(&raw_key, key, keylen);
Tadeusz Struk22287b02015-10-08 09:26:55 -0700205 if (ret)
206 return ret;
207
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300208 mpi_key->d = mpi_read_raw_data(raw_key.d, raw_key.d_sz);
209 if (!mpi_key->d)
210 goto err;
211
212 mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
213 if (!mpi_key->e)
214 goto err;
215
216 mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
217 if (!mpi_key->n)
218 goto err;
219
220 if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
221 rsa_free_mpi_key(mpi_key);
222 return -EINVAL;
Tadeusz Struk22287b02015-10-08 09:26:55 -0700223 }
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300224
225 return 0;
226
227err:
228 rsa_free_mpi_key(mpi_key);
229 return -ENOMEM;
Tadeusz Struk22287b02015-10-08 09:26:55 -0700230}
231
Tudor-Dan Ambarus1c23b462017-05-25 10:18:13 +0300232static unsigned int rsa_max_size(struct crypto_akcipher *tfm)
Tadeusz Struk22287b02015-10-08 09:26:55 -0700233{
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300234 struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
Tadeusz Struk22287b02015-10-08 09:26:55 -0700235
Tudor-Dan Ambarus1c23b462017-05-25 10:18:13 +0300236 return mpi_get_size(pkey->n);
Tadeusz Struk22287b02015-10-08 09:26:55 -0700237}
238
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700239static void rsa_exit_tfm(struct crypto_akcipher *tfm)
240{
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300241 struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700242
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300243 rsa_free_mpi_key(pkey);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700244}
245
246static struct akcipher_alg rsa = {
247 .encrypt = rsa_enc,
248 .decrypt = rsa_dec,
Tadeusz Struk22287b02015-10-08 09:26:55 -0700249 .set_priv_key = rsa_set_priv_key,
250 .set_pub_key = rsa_set_pub_key,
251 .max_size = rsa_max_size,
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700252 .exit = rsa_exit_tfm,
253 .base = {
254 .cra_name = "rsa",
255 .cra_driver_name = "rsa-generic",
256 .cra_priority = 100,
257 .cra_module = THIS_MODULE,
Tudor Ambarus5a7de972016-06-14 16:14:58 +0300258 .cra_ctxsize = sizeof(struct rsa_mpi_key),
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700259 },
260};
261
262static int rsa_init(void)
263{
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100264 int err;
265
266 err = crypto_register_akcipher(&rsa);
267 if (err)
268 return err;
269
270 err = crypto_register_template(&rsa_pkcs1pad_tmpl);
271 if (err) {
272 crypto_unregister_akcipher(&rsa);
273 return err;
274 }
275
276 return 0;
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700277}
278
279static void rsa_exit(void)
280{
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100281 crypto_unregister_template(&rsa_pkcs1pad_tmpl);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700282 crypto_unregister_akcipher(&rsa);
283}
284
Eric Biggersc4741b22019-04-11 21:57:42 -0700285subsys_initcall(rsa_init);
Tadeusz Strukcfc2bb32015-06-16 10:31:01 -0700286module_exit(rsa_exit);
287MODULE_ALIAS_CRYPTO("rsa");
288MODULE_LICENSE("GPL");
289MODULE_DESCRIPTION("RSA generic algorithm");