1 // SPDX-License-Identifier: GPL-2.0-or-later << 2 /* RSA asymmetric public-key algorithm [RFC344 1 /* RSA asymmetric public-key algorithm [RFC3447] 3 * 2 * 4 * Copyright (c) 2015, Intel Corporation 3 * Copyright (c) 2015, Intel Corporation 5 * Authors: Tadeusz Struk <tadeusz.struk@intel 4 * Authors: Tadeusz Struk <tadeusz.struk@intel.com> >> 5 * >> 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. 6 */ 10 */ 7 11 8 #include <linux/fips.h> << 9 #include <linux/module.h> 12 #include <linux/module.h> 10 #include <linux/mpi.h> << 11 #include <crypto/internal/rsa.h> 13 #include <crypto/internal/rsa.h> 12 #include <crypto/internal/akcipher.h> 14 #include <crypto/internal/akcipher.h> 13 #include <crypto/akcipher.h> 15 #include <crypto/akcipher.h> 14 #include <crypto/algapi.h> << 15 << 16 struct rsa_mpi_key { << 17 MPI n; << 18 MPI e; << 19 MPI d; << 20 MPI p; << 21 MPI q; << 22 MPI dp; << 23 MPI dq; << 24 MPI qinv; << 25 }; << 26 << 27 static int rsa_check_payload(MPI x, MPI n) << 28 { << 29 MPI n1; << 30 << 31 if (mpi_cmp_ui(x, 1) <= 0) << 32 return -EINVAL; << 33 << 34 n1 = mpi_alloc(0); << 35 if (!n1) << 36 return -ENOMEM; << 37 << 38 if (mpi_sub_ui(n1, n, 1) || mpi_cmp(x, << 39 mpi_free(n1); << 40 return -EINVAL; << 41 } << 42 << 43 mpi_free(n1); << 44 return 0; << 45 } << 46 16 47 /* 17 /* 48 * RSAEP function [RFC3447 sec 5.1.1] 18 * RSAEP function [RFC3447 sec 5.1.1] 49 * c = m^e mod n; 19 * c = m^e mod n; 50 */ 20 */ 51 static int _rsa_enc(const struct rsa_mpi_key * !! 21 static int _rsa_enc(const struct rsa_key *key, MPI c, MPI m) 52 { 22 { 53 /* !! 23 /* (1) Validate 0 <= m < n */ 54 * Even though (1) in RFC3447 only req !! 24 if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0) 55 * slightly more conservative and requ << 56 * with SP 800-56Br2, Section 7.1.1. << 57 */ << 58 if (rsa_check_payload(m, key->n)) << 59 return -EINVAL; 25 return -EINVAL; 60 26 61 /* (2) c = m^e mod n */ 27 /* (2) c = m^e mod n */ 62 return mpi_powm(c, m, key->e, key->n); 28 return mpi_powm(c, m, key->e, key->n); 63 } 29 } 64 30 65 /* 31 /* 66 * RSADP function [RFC3447 sec 5.1.2] 32 * RSADP function [RFC3447 sec 5.1.2] 67 * m_1 = c^dP mod p; !! 33 * m = c^d mod n; 68 * m_2 = c^dQ mod q; << 69 * h = (m_1 - m_2) * qInv mod p; << 70 * m = m_2 + q * h; << 71 */ 34 */ 72 static int _rsa_dec_crt(const struct rsa_mpi_k !! 35 static int _rsa_dec(const struct rsa_key *key, MPI m, MPI c) 73 { 36 { 74 MPI m2, m12_or_qh; !! 37 /* (1) Validate 0 <= c < n */ 75 int ret = -ENOMEM; !! 38 if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0) 76 << 77 /* << 78 * Even though (1) in RFC3447 only req << 79 * slightly more conservative and requ << 80 * with SP 800-56Br2, Section 7.1.2. << 81 */ << 82 if (rsa_check_payload(c, key->n)) << 83 return -EINVAL; 39 return -EINVAL; 84 40 85 m2 = mpi_alloc(0); !! 41 /* (2) m = c^d mod n */ 86 m12_or_qh = mpi_alloc(0); !! 42 return mpi_powm(m, c, key->d, key->n); 87 if (!m2 || !m12_or_qh) !! 43 } 88 goto err_free_mpi; << 89 44 90 /* (2i) m_1 = c^dP mod p */ !! 45 /* 91 ret = mpi_powm(m_or_m1_or_h, c, key->d !! 46 * RSASP1 function [RFC3447 sec 5.2.1] 92 if (ret) !! 47 * s = m^d mod n 93 goto err_free_mpi; !! 48 */ >> 49 static int _rsa_sign(const struct rsa_key *key, MPI s, MPI m) >> 50 { >> 51 /* (1) Validate 0 <= m < n */ >> 52 if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0) >> 53 return -EINVAL; 94 54 95 /* (2i) m_2 = c^dQ mod q */ !! 55 /* (2) s = m^d mod n */ 96 ret = mpi_powm(m2, c, key->dq, key->q) !! 56 return mpi_powm(s, m, key->d, key->n); 97 if (ret) !! 57 } 98 goto err_free_mpi; << 99 58 100 /* (2iii) h = (m_1 - m_2) * qInv mod p !! 59 /* 101 ret = mpi_sub(m12_or_qh, m_or_m1_or_h, !! 60 * RSAVP1 function [RFC3447 sec 5.2.2] 102 mpi_mulm(m_or_m1_or_h, m12_or_qh !! 61 * m = s^e mod n; 103 !! 62 */ 104 /* (2iv) m = m_2 + q * h */ !! 63 static int _rsa_verify(const struct rsa_key *key, MPI m, MPI s) 105 ret = ret ?: !! 64 { 106 mpi_mul(m12_or_qh, key->q, m_or_ !! 65 /* (1) Validate 0 <= s < n */ 107 mpi_addm(m_or_m1_or_h, m2, m12_o !! 66 if (mpi_cmp_ui(s, 0) < 0 || mpi_cmp(s, key->n) >= 0) 108 !! 67 return -EINVAL; 109 err_free_mpi: !! 68 110 mpi_free(m12_or_qh); !! 69 /* (2) m = s^e mod n */ 111 mpi_free(m2); !! 70 return mpi_powm(m, s, key->e, key->n); 112 return ret; << 113 } 71 } 114 72 115 static inline struct rsa_mpi_key *rsa_get_key( !! 73 static inline struct rsa_key *rsa_get_key(struct crypto_akcipher *tfm) 116 { 74 { 117 return akcipher_tfm_ctx(tfm); 75 return akcipher_tfm_ctx(tfm); 118 } 76 } 119 77 120 static int rsa_enc(struct akcipher_request *re 78 static int rsa_enc(struct akcipher_request *req) 121 { 79 { 122 struct crypto_akcipher *tfm = crypto_a 80 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 123 const struct rsa_mpi_key *pkey = rsa_g !! 81 const struct rsa_key *pkey = rsa_get_key(tfm); 124 MPI m, c = mpi_alloc(0); 82 MPI m, c = mpi_alloc(0); 125 int ret = 0; 83 int ret = 0; 126 int sign; 84 int sign; 127 85 128 if (!c) 86 if (!c) 129 return -ENOMEM; 87 return -ENOMEM; 130 88 131 if (unlikely(!pkey->n || !pkey->e)) { 89 if (unlikely(!pkey->n || !pkey->e)) { 132 ret = -EINVAL; 90 ret = -EINVAL; 133 goto err_free_c; 91 goto err_free_c; 134 } 92 } 135 93 >> 94 if (req->dst_len < mpi_get_size(pkey->n)) { >> 95 req->dst_len = mpi_get_size(pkey->n); >> 96 ret = -EOVERFLOW; >> 97 goto err_free_c; >> 98 } >> 99 136 ret = -ENOMEM; 100 ret = -ENOMEM; 137 m = mpi_read_raw_from_sgl(req->src, re 101 m = mpi_read_raw_from_sgl(req->src, req->src_len); 138 if (!m) 102 if (!m) 139 goto err_free_c; 103 goto err_free_c; 140 104 141 ret = _rsa_enc(pkey, c, m); 105 ret = _rsa_enc(pkey, c, m); 142 if (ret) 106 if (ret) 143 goto err_free_m; 107 goto err_free_m; 144 108 145 ret = mpi_write_to_sgl(c, req->dst, re !! 109 ret = mpi_write_to_sgl(c, req->dst, &req->dst_len, &sign); 146 if (ret) 110 if (ret) 147 goto err_free_m; 111 goto err_free_m; 148 112 149 if (sign < 0) 113 if (sign < 0) 150 ret = -EBADMSG; 114 ret = -EBADMSG; 151 115 152 err_free_m: 116 err_free_m: 153 mpi_free(m); 117 mpi_free(m); 154 err_free_c: 118 err_free_c: 155 mpi_free(c); 119 mpi_free(c); 156 return ret; 120 return ret; 157 } 121 } 158 122 159 static int rsa_dec(struct akcipher_request *re 123 static int rsa_dec(struct akcipher_request *req) 160 { 124 { 161 struct crypto_akcipher *tfm = crypto_a 125 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 162 const struct rsa_mpi_key *pkey = rsa_g !! 126 const struct rsa_key *pkey = rsa_get_key(tfm); 163 MPI c, m = mpi_alloc(0); 127 MPI c, m = mpi_alloc(0); 164 int ret = 0; 128 int ret = 0; 165 int sign; 129 int sign; 166 130 167 if (!m) 131 if (!m) 168 return -ENOMEM; 132 return -ENOMEM; 169 133 170 if (unlikely(!pkey->n || !pkey->d)) { 134 if (unlikely(!pkey->n || !pkey->d)) { 171 ret = -EINVAL; 135 ret = -EINVAL; 172 goto err_free_m; 136 goto err_free_m; 173 } 137 } 174 138 >> 139 if (req->dst_len < mpi_get_size(pkey->n)) { >> 140 req->dst_len = mpi_get_size(pkey->n); >> 141 ret = -EOVERFLOW; >> 142 goto err_free_m; >> 143 } >> 144 175 ret = -ENOMEM; 145 ret = -ENOMEM; 176 c = mpi_read_raw_from_sgl(req->src, re 146 c = mpi_read_raw_from_sgl(req->src, req->src_len); 177 if (!c) 147 if (!c) 178 goto err_free_m; 148 goto err_free_m; 179 149 180 ret = _rsa_dec_crt(pkey, m, c); !! 150 ret = _rsa_dec(pkey, m, c); 181 if (ret) 151 if (ret) 182 goto err_free_c; 152 goto err_free_c; 183 153 184 ret = mpi_write_to_sgl(m, req->dst, re !! 154 ret = mpi_write_to_sgl(m, req->dst, &req->dst_len, &sign); 185 if (ret) 155 if (ret) 186 goto err_free_c; 156 goto err_free_c; 187 157 188 if (sign < 0) 158 if (sign < 0) 189 ret = -EBADMSG; 159 ret = -EBADMSG; 190 err_free_c: 160 err_free_c: 191 mpi_free(c); 161 mpi_free(c); 192 err_free_m: 162 err_free_m: 193 mpi_free(m); 163 mpi_free(m); 194 return ret; 164 return ret; 195 } 165 } 196 166 197 static void rsa_free_mpi_key(struct rsa_mpi_ke !! 167 static int rsa_sign(struct akcipher_request *req) 198 { 168 { 199 mpi_free(key->d); !! 169 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 200 mpi_free(key->e); !! 170 const struct rsa_key *pkey = rsa_get_key(tfm); 201 mpi_free(key->n); !! 171 MPI m, s = mpi_alloc(0); 202 mpi_free(key->p); !! 172 int ret = 0; 203 mpi_free(key->q); !! 173 int sign; 204 mpi_free(key->dp); << 205 mpi_free(key->dq); << 206 mpi_free(key->qinv); << 207 key->d = NULL; << 208 key->e = NULL; << 209 key->n = NULL; << 210 key->p = NULL; << 211 key->q = NULL; << 212 key->dp = NULL; << 213 key->dq = NULL; << 214 key->qinv = NULL; << 215 } << 216 174 217 static int rsa_check_key_length(unsigned int l !! 175 if (!s) 218 { !! 176 return -ENOMEM; 219 switch (len) { !! 177 220 case 512: !! 178 if (unlikely(!pkey->n || !pkey->d)) { 221 case 1024: !! 179 ret = -EINVAL; 222 case 1536: !! 180 goto err_free_s; 223 if (fips_enabled) << 224 return -EINVAL; << 225 fallthrough; << 226 case 2048: << 227 case 3072: << 228 case 4096: << 229 return 0; << 230 } 181 } 231 182 232 return -EINVAL; !! 183 if (req->dst_len < mpi_get_size(pkey->n)) { >> 184 req->dst_len = mpi_get_size(pkey->n); >> 185 ret = -EOVERFLOW; >> 186 goto err_free_s; >> 187 } >> 188 >> 189 ret = -ENOMEM; >> 190 m = mpi_read_raw_from_sgl(req->src, req->src_len); >> 191 if (!m) >> 192 goto err_free_s; >> 193 >> 194 ret = _rsa_sign(pkey, s, m); >> 195 if (ret) >> 196 goto err_free_m; >> 197 >> 198 ret = mpi_write_to_sgl(s, req->dst, &req->dst_len, &sign); >> 199 if (ret) >> 200 goto err_free_m; >> 201 >> 202 if (sign < 0) >> 203 ret = -EBADMSG; >> 204 >> 205 err_free_m: >> 206 mpi_free(m); >> 207 err_free_s: >> 208 mpi_free(s); >> 209 return ret; 233 } 210 } 234 211 235 static int rsa_check_exponent_fips(MPI e) !! 212 static int rsa_verify(struct akcipher_request *req) 236 { 213 { 237 MPI e_max = NULL; !! 214 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 238 int err; !! 215 const struct rsa_key *pkey = rsa_get_key(tfm); >> 216 MPI s, m = mpi_alloc(0); >> 217 int ret = 0; >> 218 int sign; 239 219 240 /* check if odd */ !! 220 if (!m) 241 if (!mpi_test_bit(e, 0)) { !! 221 return -ENOMEM; 242 return -EINVAL; << 243 } << 244 222 245 /* check if 2^16 < e < 2^256. */ !! 223 if (unlikely(!pkey->n || !pkey->e)) { 246 if (mpi_cmp_ui(e, 65536) <= 0) { !! 224 ret = -EINVAL; 247 return -EINVAL; !! 225 goto err_free_m; 248 } 226 } 249 227 250 e_max = mpi_alloc(0); !! 228 if (req->dst_len < mpi_get_size(pkey->n)) { 251 if (!e_max) !! 229 req->dst_len = mpi_get_size(pkey->n); 252 return -ENOMEM; !! 230 ret = -EOVERFLOW; >> 231 goto err_free_m; >> 232 } 253 233 254 err = mpi_set_bit(e_max, 256); !! 234 ret = -ENOMEM; 255 if (err) { !! 235 s = mpi_read_raw_from_sgl(req->src, req->src_len); 256 mpi_free(e_max); !! 236 if (!s) { 257 return err; !! 237 ret = -ENOMEM; >> 238 goto err_free_m; 258 } 239 } 259 240 260 if (mpi_cmp(e, e_max) >= 0) { !! 241 ret = _rsa_verify(pkey, m, s); 261 mpi_free(e_max); !! 242 if (ret) 262 return -EINVAL; !! 243 goto err_free_s; >> 244 >> 245 ret = mpi_write_to_sgl(m, req->dst, &req->dst_len, &sign); >> 246 if (ret) >> 247 goto err_free_s; >> 248 >> 249 if (sign < 0) >> 250 ret = -EBADMSG; >> 251 >> 252 err_free_s: >> 253 mpi_free(s); >> 254 err_free_m: >> 255 mpi_free(m); >> 256 return ret; >> 257 } >> 258 >> 259 static int rsa_check_key_length(unsigned int len) >> 260 { >> 261 switch (len) { >> 262 case 512: >> 263 case 1024: >> 264 case 1536: >> 265 case 2048: >> 266 case 3072: >> 267 case 4096: >> 268 return 0; 263 } 269 } 264 270 265 mpi_free(e_max); !! 271 return -EINVAL; 266 return 0; << 267 } 272 } 268 273 269 static int rsa_set_pub_key(struct crypto_akcip 274 static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, 270 unsigned int keylen 275 unsigned int keylen) 271 { 276 { 272 struct rsa_mpi_key *mpi_key = akcipher !! 277 struct rsa_key *pkey = akcipher_tfm_ctx(tfm); 273 struct rsa_key raw_key = {0}; << 274 int ret; 278 int ret; 275 279 276 /* Free the old MPI key if any */ !! 280 ret = rsa_parse_pub_key(pkey, key, keylen); 277 rsa_free_mpi_key(mpi_key); << 278 << 279 ret = rsa_parse_pub_key(&raw_key, key, << 280 if (ret) 281 if (ret) 281 return ret; 282 return ret; 282 283 283 mpi_key->e = mpi_read_raw_data(raw_key !! 284 if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) { 284 if (!mpi_key->e) !! 285 rsa_free_key(pkey); 285 goto err; !! 286 ret = -EINVAL; 286 << 287 mpi_key->n = mpi_read_raw_data(raw_key << 288 if (!mpi_key->n) << 289 goto err; << 290 << 291 if (rsa_check_key_length(mpi_get_size( << 292 rsa_free_mpi_key(mpi_key); << 293 return -EINVAL; << 294 } << 295 << 296 if (fips_enabled && rsa_check_exponent << 297 rsa_free_mpi_key(mpi_key); << 298 return -EINVAL; << 299 } 287 } 300 !! 288 return ret; 301 return 0; << 302 << 303 err: << 304 rsa_free_mpi_key(mpi_key); << 305 return -ENOMEM; << 306 } 289 } 307 290 308 static int rsa_set_priv_key(struct crypto_akci 291 static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, 309 unsigned int keyle 292 unsigned int keylen) 310 { 293 { 311 struct rsa_mpi_key *mpi_key = akcipher !! 294 struct rsa_key *pkey = akcipher_tfm_ctx(tfm); 312 struct rsa_key raw_key = {0}; << 313 int ret; 295 int ret; 314 296 315 /* Free the old MPI key if any */ !! 297 ret = rsa_parse_priv_key(pkey, key, keylen); 316 rsa_free_mpi_key(mpi_key); << 317 << 318 ret = rsa_parse_priv_key(&raw_key, key << 319 if (ret) 298 if (ret) 320 return ret; 299 return ret; 321 300 322 mpi_key->d = mpi_read_raw_data(raw_key !! 301 if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) { 323 if (!mpi_key->d) !! 302 rsa_free_key(pkey); 324 goto err; !! 303 ret = -EINVAL; 325 << 326 mpi_key->e = mpi_read_raw_data(raw_key << 327 if (!mpi_key->e) << 328 goto err; << 329 << 330 mpi_key->n = mpi_read_raw_data(raw_key << 331 if (!mpi_key->n) << 332 goto err; << 333 << 334 mpi_key->p = mpi_read_raw_data(raw_key << 335 if (!mpi_key->p) << 336 goto err; << 337 << 338 mpi_key->q = mpi_read_raw_data(raw_key << 339 if (!mpi_key->q) << 340 goto err; << 341 << 342 mpi_key->dp = mpi_read_raw_data(raw_ke << 343 if (!mpi_key->dp) << 344 goto err; << 345 << 346 mpi_key->dq = mpi_read_raw_data(raw_ke << 347 if (!mpi_key->dq) << 348 goto err; << 349 << 350 mpi_key->qinv = mpi_read_raw_data(raw_ << 351 if (!mpi_key->qinv) << 352 goto err; << 353 << 354 if (rsa_check_key_length(mpi_get_size( << 355 rsa_free_mpi_key(mpi_key); << 356 return -EINVAL; << 357 } << 358 << 359 if (fips_enabled && rsa_check_exponent << 360 rsa_free_mpi_key(mpi_key); << 361 return -EINVAL; << 362 } 304 } 363 !! 305 return ret; 364 return 0; << 365 << 366 err: << 367 rsa_free_mpi_key(mpi_key); << 368 return -ENOMEM; << 369 } 306 } 370 307 371 static unsigned int rsa_max_size(struct crypto !! 308 static int rsa_max_size(struct crypto_akcipher *tfm) 372 { 309 { 373 struct rsa_mpi_key *pkey = akcipher_tf !! 310 struct rsa_key *pkey = akcipher_tfm_ctx(tfm); 374 311 375 return mpi_get_size(pkey->n); !! 312 return pkey->n ? mpi_get_size(pkey->n) : -EINVAL; 376 } 313 } 377 314 378 static void rsa_exit_tfm(struct crypto_akciphe 315 static void rsa_exit_tfm(struct crypto_akcipher *tfm) 379 { 316 { 380 struct rsa_mpi_key *pkey = akcipher_tf !! 317 struct rsa_key *pkey = akcipher_tfm_ctx(tfm); 381 318 382 rsa_free_mpi_key(pkey); !! 319 rsa_free_key(pkey); 383 } 320 } 384 321 385 static struct akcipher_alg rsa = { 322 static struct akcipher_alg rsa = { 386 .encrypt = rsa_enc, 323 .encrypt = rsa_enc, 387 .decrypt = rsa_dec, 324 .decrypt = rsa_dec, >> 325 .sign = rsa_sign, >> 326 .verify = rsa_verify, 388 .set_priv_key = rsa_set_priv_key, 327 .set_priv_key = rsa_set_priv_key, 389 .set_pub_key = rsa_set_pub_key, 328 .set_pub_key = rsa_set_pub_key, 390 .max_size = rsa_max_size, 329 .max_size = rsa_max_size, 391 .exit = rsa_exit_tfm, 330 .exit = rsa_exit_tfm, 392 .base = { 331 .base = { 393 .cra_name = "rsa", 332 .cra_name = "rsa", 394 .cra_driver_name = "rsa-generi 333 .cra_driver_name = "rsa-generic", 395 .cra_priority = 100, 334 .cra_priority = 100, 396 .cra_module = THIS_MODULE, 335 .cra_module = THIS_MODULE, 397 .cra_ctxsize = sizeof(struct r !! 336 .cra_ctxsize = sizeof(struct rsa_key), 398 }, 337 }, 399 }; 338 }; 400 339 401 static int __init rsa_init(void) !! 340 static int rsa_init(void) 402 { 341 { 403 int err; !! 342 return crypto_register_akcipher(&rsa); 404 << 405 err = crypto_register_akcipher(&rsa); << 406 if (err) << 407 return err; << 408 << 409 err = crypto_register_template(&rsa_pk << 410 if (err) { << 411 crypto_unregister_akcipher(&rs << 412 return err; << 413 } << 414 << 415 return 0; << 416 } 343 } 417 344 418 static void __exit rsa_exit(void) !! 345 static void rsa_exit(void) 419 { 346 { 420 crypto_unregister_template(&rsa_pkcs1p << 421 crypto_unregister_akcipher(&rsa); 347 crypto_unregister_akcipher(&rsa); 422 } 348 } 423 349 424 subsys_initcall(rsa_init); !! 350 module_init(rsa_init); 425 module_exit(rsa_exit); 351 module_exit(rsa_exit); 426 MODULE_ALIAS_CRYPTO("rsa"); 352 MODULE_ALIAS_CRYPTO("rsa"); 427 MODULE_LICENSE("GPL"); 353 MODULE_LICENSE("GPL"); 428 MODULE_DESCRIPTION("RSA generic algorithm"); 354 MODULE_DESCRIPTION("RSA generic algorithm"); 429 355
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.