Thomas Gleixner | b4d0d23 | 2019-05-20 19:08:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 2 | /* RSA asymmetric public-key algorithm [RFC3447] |
| 3 | * |
| 4 | * Copyright (c) 2015, Intel Corporation |
| 5 | * Authors: Tadeusz Struk <tadeusz.struk@intel.com> |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 9 | #include <linux/mpi.h> |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 10 | #include <crypto/internal/rsa.h> |
| 11 | #include <crypto/internal/akcipher.h> |
| 12 | #include <crypto/akcipher.h> |
Andrzej Zaborowski | 3d5b1ec | 2015-12-05 17:09:34 +0100 | [diff] [blame] | 13 | #include <crypto/algapi.h> |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 14 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 15 | struct rsa_mpi_key { |
| 16 | MPI n; |
| 17 | MPI e; |
| 18 | MPI d; |
| 19 | }; |
| 20 | |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 21 | /* |
| 22 | * RSAEP function [RFC3447 sec 5.1.1] |
| 23 | * c = m^e mod n; |
| 24 | */ |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 25 | static int _rsa_enc(const struct rsa_mpi_key *key, MPI c, MPI m) |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 26 | { |
| 27 | /* (1) Validate 0 <= m < n */ |
| 28 | if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0) |
| 29 | return -EINVAL; |
| 30 | |
| 31 | /* (2) c = m^e mod n */ |
| 32 | return mpi_powm(c, m, key->e, key->n); |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * RSADP function [RFC3447 sec 5.1.2] |
| 37 | * m = c^d mod n; |
| 38 | */ |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 39 | static int _rsa_dec(const struct rsa_mpi_key *key, MPI m, MPI c) |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 40 | { |
| 41 | /* (1) Validate 0 <= c < n */ |
| 42 | if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0) |
| 43 | return -EINVAL; |
| 44 | |
| 45 | /* (2) m = c^d mod n */ |
| 46 | return mpi_powm(m, c, key->d, key->n); |
| 47 | } |
| 48 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 49 | static inline struct rsa_mpi_key *rsa_get_key(struct crypto_akcipher *tfm) |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 50 | { |
| 51 | return akcipher_tfm_ctx(tfm); |
| 52 | } |
| 53 | |
| 54 | static int rsa_enc(struct akcipher_request *req) |
| 55 | { |
| 56 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 57 | const struct rsa_mpi_key *pkey = rsa_get_key(tfm); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 58 | MPI m, c = mpi_alloc(0); |
| 59 | int ret = 0; |
| 60 | int sign; |
| 61 | |
| 62 | if (!c) |
| 63 | return -ENOMEM; |
| 64 | |
| 65 | if (unlikely(!pkey->n || !pkey->e)) { |
| 66 | ret = -EINVAL; |
| 67 | goto err_free_c; |
| 68 | } |
| 69 | |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 70 | ret = -ENOMEM; |
| 71 | m = mpi_read_raw_from_sgl(req->src, req->src_len); |
| 72 | if (!m) |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 73 | goto err_free_c; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 74 | |
| 75 | ret = _rsa_enc(pkey, c, m); |
| 76 | if (ret) |
| 77 | goto err_free_m; |
| 78 | |
Herbert Xu | 9b45b7b | 2016-06-29 19:32:21 +0800 | [diff] [blame] | 79 | ret = mpi_write_to_sgl(c, req->dst, req->dst_len, &sign); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 80 | if (ret) |
| 81 | goto err_free_m; |
| 82 | |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 83 | if (sign < 0) |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 84 | ret = -EBADMSG; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 85 | |
| 86 | err_free_m: |
| 87 | mpi_free(m); |
| 88 | err_free_c: |
| 89 | mpi_free(c); |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | static int rsa_dec(struct akcipher_request *req) |
| 94 | { |
| 95 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 96 | const struct rsa_mpi_key *pkey = rsa_get_key(tfm); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 97 | MPI c, m = mpi_alloc(0); |
| 98 | int ret = 0; |
| 99 | int sign; |
| 100 | |
| 101 | if (!m) |
| 102 | return -ENOMEM; |
| 103 | |
| 104 | if (unlikely(!pkey->n || !pkey->d)) { |
| 105 | ret = -EINVAL; |
| 106 | goto err_free_m; |
| 107 | } |
| 108 | |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 109 | ret = -ENOMEM; |
| 110 | c = mpi_read_raw_from_sgl(req->src, req->src_len); |
| 111 | if (!c) |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 112 | goto err_free_m; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 113 | |
| 114 | ret = _rsa_dec(pkey, m, c); |
| 115 | if (ret) |
| 116 | goto err_free_c; |
| 117 | |
Herbert Xu | 9b45b7b | 2016-06-29 19:32:21 +0800 | [diff] [blame] | 118 | ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 119 | if (ret) |
| 120 | goto err_free_c; |
| 121 | |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 122 | if (sign < 0) |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 123 | ret = -EBADMSG; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 124 | err_free_c: |
| 125 | mpi_free(c); |
| 126 | err_free_m: |
| 127 | mpi_free(m); |
| 128 | return ret; |
| 129 | } |
| 130 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 131 | static void rsa_free_mpi_key(struct rsa_mpi_key *key) |
| 132 | { |
| 133 | mpi_free(key->d); |
| 134 | mpi_free(key->e); |
| 135 | mpi_free(key->n); |
| 136 | key->d = NULL; |
| 137 | key->e = NULL; |
| 138 | key->n = NULL; |
| 139 | } |
| 140 | |
Tadeusz Struk | 6e8ec66 | 2015-07-15 15:28:43 -0700 | [diff] [blame] | 141 | static int rsa_check_key_length(unsigned int len) |
| 142 | { |
| 143 | switch (len) { |
| 144 | case 512: |
| 145 | case 1024: |
| 146 | case 1536: |
| 147 | case 2048: |
| 148 | case 3072: |
| 149 | case 4096: |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 156 | static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, |
| 157 | unsigned int keylen) |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 158 | { |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 159 | struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm); |
| 160 | struct rsa_key raw_key = {0}; |
Tadeusz Struk | 6e8ec66 | 2015-07-15 15:28:43 -0700 | [diff] [blame] | 161 | int ret; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 162 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 163 | /* Free the old MPI key if any */ |
| 164 | rsa_free_mpi_key(mpi_key); |
| 165 | |
| 166 | ret = rsa_parse_pub_key(&raw_key, key, keylen); |
Tadeusz Struk | 6e8ec66 | 2015-07-15 15:28:43 -0700 | [diff] [blame] | 167 | if (ret) |
| 168 | return ret; |
| 169 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 170 | mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz); |
| 171 | if (!mpi_key->e) |
| 172 | goto err; |
| 173 | |
| 174 | mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz); |
| 175 | if (!mpi_key->n) |
| 176 | goto err; |
| 177 | |
| 178 | if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) { |
| 179 | rsa_free_mpi_key(mpi_key); |
| 180 | return -EINVAL; |
Tadeusz Struk | 6e8ec66 | 2015-07-15 15:28:43 -0700 | [diff] [blame] | 181 | } |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 182 | |
| 183 | return 0; |
| 184 | |
| 185 | err: |
| 186 | rsa_free_mpi_key(mpi_key); |
| 187 | return -ENOMEM; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 190 | static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, |
| 191 | unsigned int keylen) |
| 192 | { |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 193 | struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm); |
| 194 | struct rsa_key raw_key = {0}; |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 195 | int ret; |
| 196 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 197 | /* Free the old MPI key if any */ |
| 198 | rsa_free_mpi_key(mpi_key); |
| 199 | |
| 200 | ret = rsa_parse_priv_key(&raw_key, key, keylen); |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 201 | if (ret) |
| 202 | return ret; |
| 203 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 204 | mpi_key->d = mpi_read_raw_data(raw_key.d, raw_key.d_sz); |
| 205 | if (!mpi_key->d) |
| 206 | goto err; |
| 207 | |
| 208 | mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz); |
| 209 | if (!mpi_key->e) |
| 210 | goto err; |
| 211 | |
| 212 | mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz); |
| 213 | if (!mpi_key->n) |
| 214 | goto err; |
| 215 | |
| 216 | if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) { |
| 217 | rsa_free_mpi_key(mpi_key); |
| 218 | return -EINVAL; |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 219 | } |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 220 | |
| 221 | return 0; |
| 222 | |
| 223 | err: |
| 224 | rsa_free_mpi_key(mpi_key); |
| 225 | return -ENOMEM; |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Tudor-Dan Ambarus | 1c23b46 | 2017-05-25 10:18:13 +0300 | [diff] [blame] | 228 | static unsigned int rsa_max_size(struct crypto_akcipher *tfm) |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 229 | { |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 230 | struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm); |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 231 | |
Tudor-Dan Ambarus | 1c23b46 | 2017-05-25 10:18:13 +0300 | [diff] [blame] | 232 | return mpi_get_size(pkey->n); |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 235 | static void rsa_exit_tfm(struct crypto_akcipher *tfm) |
| 236 | { |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 237 | struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 238 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 239 | rsa_free_mpi_key(pkey); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static struct akcipher_alg rsa = { |
| 243 | .encrypt = rsa_enc, |
| 244 | .decrypt = rsa_dec, |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 245 | .set_priv_key = rsa_set_priv_key, |
| 246 | .set_pub_key = rsa_set_pub_key, |
| 247 | .max_size = rsa_max_size, |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 248 | .exit = rsa_exit_tfm, |
| 249 | .base = { |
| 250 | .cra_name = "rsa", |
| 251 | .cra_driver_name = "rsa-generic", |
| 252 | .cra_priority = 100, |
| 253 | .cra_module = THIS_MODULE, |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 254 | .cra_ctxsize = sizeof(struct rsa_mpi_key), |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 255 | }, |
| 256 | }; |
| 257 | |
| 258 | static int rsa_init(void) |
| 259 | { |
Andrzej Zaborowski | 3d5b1ec | 2015-12-05 17:09:34 +0100 | [diff] [blame] | 260 | int err; |
| 261 | |
| 262 | err = crypto_register_akcipher(&rsa); |
| 263 | if (err) |
| 264 | return err; |
| 265 | |
| 266 | err = crypto_register_template(&rsa_pkcs1pad_tmpl); |
| 267 | if (err) { |
| 268 | crypto_unregister_akcipher(&rsa); |
| 269 | return err; |
| 270 | } |
| 271 | |
| 272 | return 0; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void rsa_exit(void) |
| 276 | { |
Andrzej Zaborowski | 3d5b1ec | 2015-12-05 17:09:34 +0100 | [diff] [blame] | 277 | crypto_unregister_template(&rsa_pkcs1pad_tmpl); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 278 | crypto_unregister_akcipher(&rsa); |
| 279 | } |
| 280 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 281 | subsys_initcall(rsa_init); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 282 | module_exit(rsa_exit); |
| 283 | MODULE_ALIAS_CRYPTO("rsa"); |
| 284 | MODULE_LICENSE("GPL"); |
| 285 | MODULE_DESCRIPTION("RSA generic algorithm"); |