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/module.h>
10 #include <crypto/internal/rsa.h>
11 #include <crypto/internal/akcipher.h>
12 #include <crypto/akcipher.h>
13 #include <crypto/algapi.h>
22 * RSAEP function [RFC3447 sec 5.1.1]
25 static int _rsa_enc(const struct rsa_mpi_key
*key
, MPI c
, MPI m
)
27 /* (1) Validate 0 <= m < n */
28 if (mpi_cmp_ui(m
, 0) < 0 || mpi_cmp(m
, key
->n
) >= 0)
31 /* (2) c = m^e mod n */
32 return mpi_powm(c
, m
, key
->e
, key
->n
);
36 * RSADP function [RFC3447 sec 5.1.2]
39 static int _rsa_dec(const struct rsa_mpi_key
*key
, MPI m
, MPI c
)
41 /* (1) Validate 0 <= c < n */
42 if (mpi_cmp_ui(c
, 0) < 0 || mpi_cmp(c
, key
->n
) >= 0)
45 /* (2) m = c^d mod n */
46 return mpi_powm(m
, c
, key
->d
, key
->n
);
49 static inline struct rsa_mpi_key
*rsa_get_key(struct crypto_akcipher
*tfm
)
51 return akcipher_tfm_ctx(tfm
);
54 static int rsa_enc(struct akcipher_request
*req
)
56 struct crypto_akcipher
*tfm
= crypto_akcipher_reqtfm(req
);
57 const struct rsa_mpi_key
*pkey
= rsa_get_key(tfm
);
58 MPI m
, c
= mpi_alloc(0);
65 if (unlikely(!pkey
->n
|| !pkey
->e
)) {
71 m
= mpi_read_raw_from_sgl(req
->src
, req
->src_len
);
75 ret
= _rsa_enc(pkey
, c
, m
);
79 ret
= mpi_write_to_sgl(c
, req
->dst
, req
->dst_len
, &sign
);
93 static int rsa_dec(struct akcipher_request
*req
)
95 struct crypto_akcipher
*tfm
= crypto_akcipher_reqtfm(req
);
96 const struct rsa_mpi_key
*pkey
= rsa_get_key(tfm
);
97 MPI c
, m
= mpi_alloc(0);
104 if (unlikely(!pkey
->n
|| !pkey
->d
)) {
110 c
= mpi_read_raw_from_sgl(req
->src
, req
->src_len
);
114 ret
= _rsa_dec(pkey
, m
, c
);
118 ret
= mpi_write_to_sgl(m
, req
->dst
, req
->dst_len
, &sign
);
131 static void rsa_free_mpi_key(struct rsa_mpi_key
*key
)
141 static int rsa_check_key_length(unsigned int len
)
156 static int rsa_set_pub_key(struct crypto_akcipher
*tfm
, const void *key
,
159 struct rsa_mpi_key
*mpi_key
= akcipher_tfm_ctx(tfm
);
160 struct rsa_key raw_key
= {0};
163 /* Free the old MPI key if any */
164 rsa_free_mpi_key(mpi_key
);
166 ret
= rsa_parse_pub_key(&raw_key
, key
, keylen
);
170 mpi_key
->e
= mpi_read_raw_data(raw_key
.e
, raw_key
.e_sz
);
174 mpi_key
->n
= mpi_read_raw_data(raw_key
.n
, raw_key
.n_sz
);
178 if (rsa_check_key_length(mpi_get_size(mpi_key
->n
) << 3)) {
179 rsa_free_mpi_key(mpi_key
);
186 rsa_free_mpi_key(mpi_key
);
190 static int rsa_set_priv_key(struct crypto_akcipher
*tfm
, const void *key
,
193 struct rsa_mpi_key
*mpi_key
= akcipher_tfm_ctx(tfm
);
194 struct rsa_key raw_key
= {0};
197 /* Free the old MPI key if any */
198 rsa_free_mpi_key(mpi_key
);
200 ret
= rsa_parse_priv_key(&raw_key
, key
, keylen
);
204 mpi_key
->d
= mpi_read_raw_data(raw_key
.d
, raw_key
.d_sz
);
208 mpi_key
->e
= mpi_read_raw_data(raw_key
.e
, raw_key
.e_sz
);
212 mpi_key
->n
= mpi_read_raw_data(raw_key
.n
, raw_key
.n_sz
);
216 if (rsa_check_key_length(mpi_get_size(mpi_key
->n
) << 3)) {
217 rsa_free_mpi_key(mpi_key
);
224 rsa_free_mpi_key(mpi_key
);
228 static unsigned int rsa_max_size(struct crypto_akcipher
*tfm
)
230 struct rsa_mpi_key
*pkey
= akcipher_tfm_ctx(tfm
);
232 return mpi_get_size(pkey
->n
);
235 static void rsa_exit_tfm(struct crypto_akcipher
*tfm
)
237 struct rsa_mpi_key
*pkey
= akcipher_tfm_ctx(tfm
);
239 rsa_free_mpi_key(pkey
);
242 static struct akcipher_alg rsa
= {
245 .set_priv_key
= rsa_set_priv_key
,
246 .set_pub_key
= rsa_set_pub_key
,
247 .max_size
= rsa_max_size
,
248 .exit
= rsa_exit_tfm
,
251 .cra_driver_name
= "rsa-generic",
253 .cra_module
= THIS_MODULE
,
254 .cra_ctxsize
= sizeof(struct rsa_mpi_key
),
258 static int rsa_init(void)
262 err
= crypto_register_akcipher(&rsa
);
266 err
= crypto_register_template(&rsa_pkcs1pad_tmpl
);
268 crypto_unregister_akcipher(&rsa
);
275 static void rsa_exit(void)
277 crypto_unregister_template(&rsa_pkcs1pad_tmpl
);
278 crypto_unregister_akcipher(&rsa
);
281 subsys_initcall(rsa_init
);
282 module_exit(rsa_exit
);
283 MODULE_ALIAS_CRYPTO("rsa");
284 MODULE_LICENSE("GPL");
285 MODULE_DESCRIPTION("RSA generic algorithm");