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]
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)
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]
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)
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]
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)
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]
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)
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);
89 if (unlikely(!pkey->n || !pkey->e)) {
94 if (req->dst_len < mpi_get_size(pkey->n)) {
95 req->dst_len = mpi_get_size(pkey->n);
100 m = mpi_read_raw_data(req->src, req->src_len);
106 ret = _rsa_enc(pkey, c, m);
110 ret = mpi_read_buffer(c, req->dst, req->dst_len, &req->dst_len, &sign);
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);
137 if (unlikely(!pkey->n || !pkey->d)) {
142 if (req->dst_len < mpi_get_size(pkey->n)) {
143 req->dst_len = mpi_get_size(pkey->n);
148 c = mpi_read_raw_data(req->src, req->src_len);
154 ret = _rsa_dec(pkey, m, c);
158 ret = mpi_read_buffer(m, req->dst, req->dst_len, &req->dst_len, &sign);
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);
185 if (unlikely(!pkey->n || !pkey->d)) {
190 if (req->dst_len < mpi_get_size(pkey->n)) {
191 req->dst_len = mpi_get_size(pkey->n);
196 m = mpi_read_raw_data(req->src, req->src_len);
202 ret = _rsa_sign(pkey, s, m);
206 ret = mpi_read_buffer(s, req->dst, req->dst_len, &req->dst_len, &sign);
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);
233 if (unlikely(!pkey->n || !pkey->e)) {
238 if (req->dst_len < mpi_get_size(pkey->n)) {
239 req->dst_len = mpi_get_size(pkey->n);
244 s = mpi_read_raw_data(req->src, req->src_len);
250 ret = _rsa_verify(pkey, m, s);
254 ret = mpi_read_buffer(m, req->dst, req->dst_len, &req->dst_len, &sign);
270 static int rsa_check_key_length(unsigned int len)
285 static int rsa_setkey(struct crypto_akcipher *tfm, const void *key,
288 struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
291 ret = rsa_parse_key(pkey, key, keylen);
295 if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) {
302 static void rsa_exit_tfm(struct crypto_akcipher *tfm)
304 struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
309 static struct akcipher_alg rsa = {
313 .verify = rsa_verify,
314 .setkey = rsa_setkey,
315 .exit = rsa_exit_tfm,
318 .cra_driver_name = "rsa-generic",
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");