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 | |
Stephan Müller | 1ce1bac | 2021-11-21 15:31:27 +0100 | [diff] [blame] | 8 | #include <linux/fips.h> |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 10 | #include <linux/mpi.h> |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 11 | #include <crypto/internal/rsa.h> |
| 12 | #include <crypto/internal/akcipher.h> |
| 13 | #include <crypto/akcipher.h> |
Andrzej Zaborowski | 3d5b1ec | 2015-12-05 17:09:34 +0100 | [diff] [blame] | 14 | #include <crypto/algapi.h> |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 15 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 16 | struct rsa_mpi_key { |
| 17 | MPI n; |
| 18 | MPI e; |
| 19 | MPI d; |
| 20 | }; |
| 21 | |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 22 | /* |
| 23 | * RSAEP function [RFC3447 sec 5.1.1] |
| 24 | * c = m^e mod n; |
| 25 | */ |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 26 | 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] | 27 | { |
| 28 | /* (1) Validate 0 <= m < n */ |
| 29 | if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0) |
| 30 | return -EINVAL; |
| 31 | |
| 32 | /* (2) c = m^e mod n */ |
| 33 | return mpi_powm(c, m, key->e, key->n); |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * RSADP function [RFC3447 sec 5.1.2] |
| 38 | * m = c^d mod n; |
| 39 | */ |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 40 | 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] | 41 | { |
| 42 | /* (1) Validate 0 <= c < n */ |
| 43 | if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0) |
| 44 | return -EINVAL; |
| 45 | |
| 46 | /* (2) m = c^d mod n */ |
| 47 | return mpi_powm(m, c, key->d, key->n); |
| 48 | } |
| 49 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 50 | 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] | 51 | { |
| 52 | return akcipher_tfm_ctx(tfm); |
| 53 | } |
| 54 | |
| 55 | static int rsa_enc(struct akcipher_request *req) |
| 56 | { |
| 57 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 58 | const struct rsa_mpi_key *pkey = rsa_get_key(tfm); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 59 | MPI m, c = mpi_alloc(0); |
| 60 | int ret = 0; |
| 61 | int sign; |
| 62 | |
| 63 | if (!c) |
| 64 | return -ENOMEM; |
| 65 | |
| 66 | if (unlikely(!pkey->n || !pkey->e)) { |
| 67 | ret = -EINVAL; |
| 68 | goto err_free_c; |
| 69 | } |
| 70 | |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 71 | ret = -ENOMEM; |
| 72 | m = mpi_read_raw_from_sgl(req->src, req->src_len); |
| 73 | if (!m) |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 74 | goto err_free_c; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 75 | |
| 76 | ret = _rsa_enc(pkey, c, m); |
| 77 | if (ret) |
| 78 | goto err_free_m; |
| 79 | |
Herbert Xu | 9b45b7b | 2016-06-29 19:32:21 +0800 | [diff] [blame] | 80 | ret = mpi_write_to_sgl(c, req->dst, req->dst_len, &sign); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 81 | if (ret) |
| 82 | goto err_free_m; |
| 83 | |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 84 | if (sign < 0) |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 85 | ret = -EBADMSG; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 86 | |
| 87 | err_free_m: |
| 88 | mpi_free(m); |
| 89 | err_free_c: |
| 90 | mpi_free(c); |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | static int rsa_dec(struct akcipher_request *req) |
| 95 | { |
| 96 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 97 | const struct rsa_mpi_key *pkey = rsa_get_key(tfm); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 98 | MPI c, m = mpi_alloc(0); |
| 99 | int ret = 0; |
| 100 | int sign; |
| 101 | |
| 102 | if (!m) |
| 103 | return -ENOMEM; |
| 104 | |
| 105 | if (unlikely(!pkey->n || !pkey->d)) { |
| 106 | ret = -EINVAL; |
| 107 | goto err_free_m; |
| 108 | } |
| 109 | |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 110 | ret = -ENOMEM; |
| 111 | c = mpi_read_raw_from_sgl(req->src, req->src_len); |
| 112 | if (!c) |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 113 | goto err_free_m; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 114 | |
| 115 | ret = _rsa_dec(pkey, m, c); |
| 116 | if (ret) |
| 117 | goto err_free_c; |
| 118 | |
Herbert Xu | 9b45b7b | 2016-06-29 19:32:21 +0800 | [diff] [blame] | 119 | ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 120 | if (ret) |
| 121 | goto err_free_c; |
| 122 | |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 123 | if (sign < 0) |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 124 | ret = -EBADMSG; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 125 | err_free_c: |
| 126 | mpi_free(c); |
| 127 | err_free_m: |
| 128 | mpi_free(m); |
| 129 | return ret; |
| 130 | } |
| 131 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 132 | static void rsa_free_mpi_key(struct rsa_mpi_key *key) |
| 133 | { |
| 134 | mpi_free(key->d); |
| 135 | mpi_free(key->e); |
| 136 | mpi_free(key->n); |
| 137 | key->d = NULL; |
| 138 | key->e = NULL; |
| 139 | key->n = NULL; |
| 140 | } |
| 141 | |
Tadeusz Struk | 6e8ec66 | 2015-07-15 15:28:43 -0700 | [diff] [blame] | 142 | static int rsa_check_key_length(unsigned int len) |
| 143 | { |
| 144 | switch (len) { |
| 145 | case 512: |
| 146 | case 1024: |
| 147 | case 1536: |
Stephan Müller | 1ce1bac | 2021-11-21 15:31:27 +0100 | [diff] [blame] | 148 | if (fips_enabled) |
| 149 | return -EINVAL; |
| 150 | fallthrough; |
Tadeusz Struk | 6e8ec66 | 2015-07-15 15:28:43 -0700 | [diff] [blame] | 151 | case 2048: |
| 152 | case 3072: |
| 153 | case 4096: |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 160 | static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, |
| 161 | unsigned int keylen) |
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 | struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm); |
| 164 | struct rsa_key raw_key = {0}; |
Tadeusz Struk | 6e8ec66 | 2015-07-15 15:28:43 -0700 | [diff] [blame] | 165 | int ret; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 166 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 167 | /* 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 Struk | 6e8ec66 | 2015-07-15 15:28:43 -0700 | [diff] [blame] | 171 | if (ret) |
| 172 | return ret; |
| 173 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 174 | 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 Struk | 6e8ec66 | 2015-07-15 15:28:43 -0700 | [diff] [blame] | 185 | } |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 186 | |
| 187 | return 0; |
| 188 | |
| 189 | err: |
| 190 | rsa_free_mpi_key(mpi_key); |
| 191 | return -ENOMEM; |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 194 | static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, |
| 195 | unsigned int keylen) |
| 196 | { |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 197 | struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm); |
| 198 | struct rsa_key raw_key = {0}; |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 199 | int ret; |
| 200 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 201 | /* 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 Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 205 | if (ret) |
| 206 | return ret; |
| 207 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 208 | 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 Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 223 | } |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 224 | |
| 225 | return 0; |
| 226 | |
| 227 | err: |
| 228 | rsa_free_mpi_key(mpi_key); |
| 229 | return -ENOMEM; |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Tudor-Dan Ambarus | 1c23b46 | 2017-05-25 10:18:13 +0300 | [diff] [blame] | 232 | static unsigned int rsa_max_size(struct crypto_akcipher *tfm) |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 233 | { |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 234 | struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm); |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 235 | |
Tudor-Dan Ambarus | 1c23b46 | 2017-05-25 10:18:13 +0300 | [diff] [blame] | 236 | return mpi_get_size(pkey->n); |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 239 | static void rsa_exit_tfm(struct crypto_akcipher *tfm) |
| 240 | { |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 241 | struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 242 | |
Tudor Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 243 | rsa_free_mpi_key(pkey); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static struct akcipher_alg rsa = { |
| 247 | .encrypt = rsa_enc, |
| 248 | .decrypt = rsa_dec, |
Tadeusz Struk | 22287b0 | 2015-10-08 09:26:55 -0700 | [diff] [blame] | 249 | .set_priv_key = rsa_set_priv_key, |
| 250 | .set_pub_key = rsa_set_pub_key, |
| 251 | .max_size = rsa_max_size, |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 252 | .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 Ambarus | 5a7de97 | 2016-06-14 16:14:58 +0300 | [diff] [blame] | 258 | .cra_ctxsize = sizeof(struct rsa_mpi_key), |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 259 | }, |
| 260 | }; |
| 261 | |
| 262 | static int rsa_init(void) |
| 263 | { |
Andrzej Zaborowski | 3d5b1ec | 2015-12-05 17:09:34 +0100 | [diff] [blame] | 264 | 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 Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static void rsa_exit(void) |
| 280 | { |
Andrzej Zaborowski | 3d5b1ec | 2015-12-05 17:09:34 +0100 | [diff] [blame] | 281 | crypto_unregister_template(&rsa_pkcs1pad_tmpl); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 282 | crypto_unregister_akcipher(&rsa); |
| 283 | } |
| 284 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 285 | subsys_initcall(rsa_init); |
Tadeusz Struk | cfc2bb3 | 2015-06-16 10:31:01 -0700 | [diff] [blame] | 286 | module_exit(rsa_exit); |
| 287 | MODULE_ALIAS_CRYPTO("rsa"); |
| 288 | MODULE_LICENSE("GPL"); |
| 289 | MODULE_DESCRIPTION("RSA generic algorithm"); |