1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RSA asymmetric public-key algorithm [RFC3447]
4 * Copyright (c) 2015, Intel Corporation
5 * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
8 #include <linux/fips.h>
9 #include <linux/module.h>
10 #include <linux/mpi.h>
11 #include <crypto/internal/rsa.h>
12 #include <crypto/internal/akcipher.h>
13 #include <crypto/akcipher.h>
14 #include <crypto/algapi.h>
23 * RSAEP function [RFC3447 sec 5.1.1]
26 static int _rsa_enc(const struct rsa_mpi_key *key, MPI c, MPI m)
28 /* (1) Validate 0 <= m < n */
29 if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
32 /* (2) c = m^e mod n */
33 return mpi_powm(c, m, key->e, key->n);
37 * RSADP function [RFC3447 sec 5.1.2]
40 static int _rsa_dec(const struct rsa_mpi_key *key, MPI m, MPI c)
42 /* (1) Validate 0 <= c < n */
43 if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0)
46 /* (2) m = c^d mod n */
47 return mpi_powm(m, c, key->d, key->n);
50 static inline struct rsa_mpi_key *rsa_get_key(struct crypto_akcipher *tfm)
52 return akcipher_tfm_ctx(tfm);
55 static int rsa_enc(struct akcipher_request *req)
57 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
58 const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
59 MPI m, c = mpi_alloc(0);
66 if (unlikely(!pkey->n || !pkey->e)) {
72 m = mpi_read_raw_from_sgl(req->src, req->src_len);
76 ret = _rsa_enc(pkey, c, m);
80 ret = mpi_write_to_sgl(c, req->dst, req->dst_len, &sign);
94 static int rsa_dec(struct akcipher_request *req)
96 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
97 const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
98 MPI c, m = mpi_alloc(0);
105 if (unlikely(!pkey->n || !pkey->d)) {
111 c = mpi_read_raw_from_sgl(req->src, req->src_len);
115 ret = _rsa_dec(pkey, m, c);
119 ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign);
132 static void rsa_free_mpi_key(struct rsa_mpi_key *key)
142 static int rsa_check_key_length(unsigned int len)
160 static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
163 struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
164 struct rsa_key raw_key = {0};
167 /* Free the old MPI key if any */
168 rsa_free_mpi_key(mpi_key);
170 ret = rsa_parse_pub_key(&raw_key, key, keylen);
174 mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
178 mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
182 if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
183 rsa_free_mpi_key(mpi_key);
190 rsa_free_mpi_key(mpi_key);
194 static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
197 struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
198 struct rsa_key raw_key = {0};
201 /* Free the old MPI key if any */
202 rsa_free_mpi_key(mpi_key);
204 ret = rsa_parse_priv_key(&raw_key, key, keylen);
208 mpi_key->d = mpi_read_raw_data(raw_key.d, raw_key.d_sz);
212 mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
216 mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
220 if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
221 rsa_free_mpi_key(mpi_key);
228 rsa_free_mpi_key(mpi_key);
232 static unsigned int rsa_max_size(struct crypto_akcipher *tfm)
234 struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
236 return mpi_get_size(pkey->n);
239 static void rsa_exit_tfm(struct crypto_akcipher *tfm)
241 struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
243 rsa_free_mpi_key(pkey);
246 static struct akcipher_alg rsa = {
249 .set_priv_key = rsa_set_priv_key,
250 .set_pub_key = rsa_set_pub_key,
251 .max_size = rsa_max_size,
252 .exit = rsa_exit_tfm,
255 .cra_driver_name = "rsa-generic",
257 .cra_module = THIS_MODULE,
258 .cra_ctxsize = sizeof(struct rsa_mpi_key),
262 static int rsa_init(void)
266 err = crypto_register_akcipher(&rsa);
270 err = crypto_register_template(&rsa_pkcs1pad_tmpl);
272 crypto_unregister_akcipher(&rsa);
279 static void rsa_exit(void)
281 crypto_unregister_template(&rsa_pkcs1pad_tmpl);
282 crypto_unregister_akcipher(&rsa);
285 subsys_initcall(rsa_init);
286 module_exit(rsa_exit);
287 MODULE_ALIAS_CRYPTO("rsa");
288 MODULE_LICENSE("GPL");
289 MODULE_DESCRIPTION("RSA generic algorithm");