Lines Matching +full:key +full:- +full:2
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RSA asymmetric public-key algorithm [RFC3447]
32 return -EINVAL; in rsa_check_payload()
36 return -ENOMEM; in rsa_check_payload()
40 return -EINVAL; in rsa_check_payload()
51 static int _rsa_enc(const struct rsa_mpi_key *key, MPI c, MPI m) in _rsa_enc() argument
54 * Even though (1) in RFC3447 only requires 0 <= m <= n - 1, we are in _rsa_enc()
55 * slightly more conservative and require 1 < m < n - 1. This is in line in _rsa_enc()
56 * with SP 800-56Br2, Section 7.1.1. in _rsa_enc()
58 if (rsa_check_payload(m, key->n)) in _rsa_enc()
59 return -EINVAL; in _rsa_enc()
61 /* (2) c = m^e mod n */ in _rsa_enc()
62 return mpi_powm(c, m, key->e, key->n); in _rsa_enc()
66 * RSADP function [RFC3447 sec 5.1.2]
69 * h = (m_1 - m_2) * qInv mod p;
72 static int _rsa_dec_crt(const struct rsa_mpi_key *key, MPI m_or_m1_or_h, MPI c) in _rsa_dec_crt() argument
75 int ret = -ENOMEM; in _rsa_dec_crt()
78 * Even though (1) in RFC3447 only requires 0 <= c <= n - 1, we are in _rsa_dec_crt()
79 * slightly more conservative and require 1 < c < n - 1. This is in line in _rsa_dec_crt()
80 * with SP 800-56Br2, Section 7.1.2. in _rsa_dec_crt()
82 if (rsa_check_payload(c, key->n)) in _rsa_dec_crt()
83 return -EINVAL; in _rsa_dec_crt()
90 /* (2i) m_1 = c^dP mod p */ in _rsa_dec_crt()
91 ret = mpi_powm(m_or_m1_or_h, c, key->dp, key->p); in _rsa_dec_crt()
95 /* (2i) m_2 = c^dQ mod q */ in _rsa_dec_crt()
96 ret = mpi_powm(m2, c, key->dq, key->q); in _rsa_dec_crt()
100 /* (2iii) h = (m_1 - m_2) * qInv mod p */ in _rsa_dec_crt()
102 mpi_mulm(m_or_m1_or_h, m12_or_qh, key->qinv, key->p); in _rsa_dec_crt()
104 /* (2iv) m = m_2 + q * h */ in _rsa_dec_crt()
106 mpi_mul(m12_or_qh, key->q, m_or_m1_or_h) ?: in _rsa_dec_crt()
107 mpi_addm(m_or_m1_or_h, m2, m12_or_qh, key->n); in _rsa_dec_crt()
129 return -ENOMEM; in rsa_enc()
131 if (unlikely(!pkey->n || !pkey->e)) { in rsa_enc()
132 ret = -EINVAL; in rsa_enc()
136 ret = -ENOMEM; in rsa_enc()
137 m = mpi_read_raw_from_sgl(req->src, req->src_len); in rsa_enc()
145 ret = mpi_write_to_sgl(c, req->dst, req->dst_len, &sign); in rsa_enc()
150 ret = -EBADMSG; in rsa_enc()
168 return -ENOMEM; in rsa_dec()
170 if (unlikely(!pkey->n || !pkey->d)) { in rsa_dec()
171 ret = -EINVAL; in rsa_dec()
175 ret = -ENOMEM; in rsa_dec()
176 c = mpi_read_raw_from_sgl(req->src, req->src_len); in rsa_dec()
184 ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign); in rsa_dec()
189 ret = -EBADMSG; in rsa_dec()
197 static void rsa_free_mpi_key(struct rsa_mpi_key *key) in rsa_free_mpi_key() argument
199 mpi_free(key->d); in rsa_free_mpi_key()
200 mpi_free(key->e); in rsa_free_mpi_key()
201 mpi_free(key->n); in rsa_free_mpi_key()
202 mpi_free(key->p); in rsa_free_mpi_key()
203 mpi_free(key->q); in rsa_free_mpi_key()
204 mpi_free(key->dp); in rsa_free_mpi_key()
205 mpi_free(key->dq); in rsa_free_mpi_key()
206 mpi_free(key->qinv); in rsa_free_mpi_key()
207 key->d = NULL; in rsa_free_mpi_key()
208 key->e = NULL; in rsa_free_mpi_key()
209 key->n = NULL; in rsa_free_mpi_key()
210 key->p = NULL; in rsa_free_mpi_key()
211 key->q = NULL; in rsa_free_mpi_key()
212 key->dp = NULL; in rsa_free_mpi_key()
213 key->dq = NULL; in rsa_free_mpi_key()
214 key->qinv = NULL; in rsa_free_mpi_key()
224 return -EINVAL; in rsa_check_key_length()
232 return -EINVAL; in rsa_check_key_length()
242 return -EINVAL; in rsa_check_exponent_fips()
245 /* check if 2^16 < e < 2^256. */ in rsa_check_exponent_fips()
247 return -EINVAL; in rsa_check_exponent_fips()
252 return -ENOMEM; in rsa_check_exponent_fips()
262 return -EINVAL; in rsa_check_exponent_fips()
269 static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, in rsa_set_pub_key() argument
276 /* Free the old MPI key if any */ in rsa_set_pub_key()
279 ret = rsa_parse_pub_key(&raw_key, key, keylen); in rsa_set_pub_key()
283 mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz); in rsa_set_pub_key()
284 if (!mpi_key->e) in rsa_set_pub_key()
287 mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz); in rsa_set_pub_key()
288 if (!mpi_key->n) in rsa_set_pub_key()
291 if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) { in rsa_set_pub_key()
293 return -EINVAL; in rsa_set_pub_key()
296 if (fips_enabled && rsa_check_exponent_fips(mpi_key->e)) { in rsa_set_pub_key()
298 return -EINVAL; in rsa_set_pub_key()
305 return -ENOMEM; in rsa_set_pub_key()
308 static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, in rsa_set_priv_key() argument
315 /* Free the old MPI key if any */ in rsa_set_priv_key()
318 ret = rsa_parse_priv_key(&raw_key, key, keylen); in rsa_set_priv_key()
322 mpi_key->d = mpi_read_raw_data(raw_key.d, raw_key.d_sz); in rsa_set_priv_key()
323 if (!mpi_key->d) in rsa_set_priv_key()
326 mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz); in rsa_set_priv_key()
327 if (!mpi_key->e) in rsa_set_priv_key()
330 mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz); in rsa_set_priv_key()
331 if (!mpi_key->n) in rsa_set_priv_key()
334 mpi_key->p = mpi_read_raw_data(raw_key.p, raw_key.p_sz); in rsa_set_priv_key()
335 if (!mpi_key->p) in rsa_set_priv_key()
338 mpi_key->q = mpi_read_raw_data(raw_key.q, raw_key.q_sz); in rsa_set_priv_key()
339 if (!mpi_key->q) in rsa_set_priv_key()
342 mpi_key->dp = mpi_read_raw_data(raw_key.dp, raw_key.dp_sz); in rsa_set_priv_key()
343 if (!mpi_key->dp) in rsa_set_priv_key()
346 mpi_key->dq = mpi_read_raw_data(raw_key.dq, raw_key.dq_sz); in rsa_set_priv_key()
347 if (!mpi_key->dq) in rsa_set_priv_key()
350 mpi_key->qinv = mpi_read_raw_data(raw_key.qinv, raw_key.qinv_sz); in rsa_set_priv_key()
351 if (!mpi_key->qinv) in rsa_set_priv_key()
354 if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) { in rsa_set_priv_key()
356 return -EINVAL; in rsa_set_priv_key()
359 if (fips_enabled && rsa_check_exponent_fips(mpi_key->e)) { in rsa_set_priv_key()
361 return -EINVAL; in rsa_set_priv_key()
368 return -ENOMEM; in rsa_set_priv_key()
375 return mpi_get_size(pkey->n); in rsa_max_size()
394 .cra_driver_name = "rsa-generic",