blob: c329425761f1a48884867acf4e0acfe2b3b347d5 [file] [log] [blame]
Hanno Beckera565f542017-10-11 11:00:19 +01001/**
Chris Jones66a4cd42021-03-09 16:04:12 +00002 * \file rsa_alt_helpers.h
Hanno Beckera565f542017-10-11 11:00:19 +01003 *
4 * \brief Context-independent RSA helper functions
Simon Butchera4cbfa32018-03-16 15:42:54 +00005 *
6 * This module declares some RSA-related helper functions useful when
7 * implementing the RSA interface. These functions are provided in a separate
8 * compilation unit in order to make it easy for designers of alternative RSA
9 * implementations to use them in their own code, as it is conceived that the
10 * functionality they provide will be necessary for most complete
11 * implementations.
12 *
13 * End-users of Mbed TLS who are not providing their own alternative RSA
14 * implementations should not use these functions directly, and should instead
15 * use only the functions declared in rsa.h.
16 *
17 * The interface provided by this module will be maintained through LTS (Long
18 * Term Support) branches of Mbed TLS, but may otherwise be subject to change,
19 * and must be considered an internal interface of the library.
20 *
21 * There are two classes of helper functions:
22 *
23 * (1) Parameter-generating helpers. These are:
24 * - mbedtls_rsa_deduce_primes
25 * - mbedtls_rsa_deduce_private_exponent
26 * - mbedtls_rsa_deduce_crt
27 * Each of these functions takes a set of core RSA parameters and
28 * generates some other, or CRT related parameters.
29 *
30 * (2) Parameter-checking helpers. These are:
31 * - mbedtls_rsa_validate_params
32 * - mbedtls_rsa_validate_crt
33 * They take a set of core or CRT related RSA parameters and check their
34 * validity.
35 *
Darryl Greena40a1012018-01-05 15:33:17 +000036 */
37/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020038 * Copyright The Mbed TLS Contributors
Hanno Beckera565f542017-10-11 11:00:19 +010039 * SPDX-License-Identifier: Apache-2.0
40 *
41 * Licensed under the Apache License, Version 2.0 (the "License"); you may
42 * not use this file except in compliance with the License.
43 * You may obtain a copy of the License at
44 *
45 * http://www.apache.org/licenses/LICENSE-2.0
46 *
47 * Unless required by applicable law or agreed to in writing, software
48 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
49 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50 * See the License for the specific language governing permissions and
51 * limitations under the License.
52 *
Hanno Beckera565f542017-10-11 11:00:19 +010053 */
54
55#ifndef MBEDTLS_RSA_INTERNAL_H
56#define MBEDTLS_RSA_INTERNAL_H
57
Bence Szépkútic662b362021-05-27 11:25:03 +020058#include "mbedtls/build_info.h"
Hanno Beckera565f542017-10-11 11:00:19 +010059
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010060#include "mbedtls/bignum.h"
Hanno Beckera565f542017-10-11 11:00:19 +010061
Hanno Beckera565f542017-10-11 11:00:19 +010062#ifdef __cplusplus
63extern "C" {
64#endif
65
Hanno Beckera565f542017-10-11 11:00:19 +010066/**
67 * \brief Compute RSA prime moduli P, Q from public modulus N=PQ
68 * and a pair of private and public key.
69 *
70 * \note This is a 'static' helper function not operating on
71 * an RSA context. Alternative implementations need not
72 * overwrite it.
73 *
74 * \param N RSA modulus N = PQ, with P, Q to be found
Hanno Beckera565f542017-10-11 11:00:19 +010075 * \param E RSA public exponent
Hanno Beckerc36aab62017-10-17 09:15:06 +010076 * \param D RSA private exponent
Hanno Beckera565f542017-10-11 11:00:19 +010077 * \param P Pointer to MPI holding first prime factor of N on success
78 * \param Q Pointer to MPI holding second prime factor of N on success
79 *
80 * \return
81 * - 0 if successful. In this case, P and Q constitute a
82 * factorization of N.
83 * - A non-zero error code otherwise.
84 *
85 * \note It is neither checked that P, Q are prime nor that
86 * D, E are modular inverses wrt. P-1 and Q-1. For that,
87 * use the helper function \c mbedtls_rsa_validate_params.
88 *
89 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020090int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N,
91 mbedtls_mpi const *E,
92 mbedtls_mpi const *D,
93 mbedtls_mpi *P,
94 mbedtls_mpi *Q);
Hanno Beckera565f542017-10-11 11:00:19 +010095
96/**
97 * \brief Compute RSA private exponent from
98 * prime moduli and public key.
99 *
100 * \note This is a 'static' helper function not operating on
101 * an RSA context. Alternative implementations need not
102 * overwrite it.
103 *
104 * \param P First prime factor of RSA modulus
105 * \param Q Second prime factor of RSA modulus
106 * \param E RSA public exponent
107 * \param D Pointer to MPI holding the private exponent on success.
108 *
109 * \return
110 * - 0 if successful. In this case, D is set to a simultaneous
111 * modular inverse of E modulo both P-1 and Q-1.
112 * - A non-zero error code otherwise.
113 *
114 * \note This function does not check whether P and Q are primes.
115 *
116 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200117int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
118 mbedtls_mpi const *Q,
119 mbedtls_mpi const *E,
120 mbedtls_mpi *D);
Hanno Beckera565f542017-10-11 11:00:19 +0100121
122/**
123 * \brief Generate RSA-CRT parameters
124 *
125 * \note This is a 'static' helper function not operating on
126 * an RSA context. Alternative implementations need not
127 * overwrite it.
128 *
129 * \param P First prime factor of N
130 * \param Q Second prime factor of N
131 * \param D RSA private exponent
132 * \param DP Output variable for D modulo P-1
133 * \param DQ Output variable for D modulo Q-1
134 * \param QP Output variable for the modular inverse of Q modulo P.
135 *
136 * \return 0 on success, non-zero error code otherwise.
137 *
138 * \note This function does not check whether P, Q are
139 * prime and whether D is a valid private exponent.
140 *
141 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200142int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P,
143 const mbedtls_mpi *Q,
144 const mbedtls_mpi *D,
145 mbedtls_mpi *DP,
146 mbedtls_mpi *DQ,
147 mbedtls_mpi *QP);
Hanno Beckera565f542017-10-11 11:00:19 +0100148
149/**
150 * \brief Check validity of core RSA parameters
151 *
152 * \note This is a 'static' helper function not operating on
153 * an RSA context. Alternative implementations need not
154 * overwrite it.
155 *
156 * \param N RSA modulus N = PQ
157 * \param P First prime factor of N
158 * \param Q Second prime factor of N
159 * \param D RSA private exponent
160 * \param E RSA public exponent
161 * \param f_rng PRNG to be used for primality check, or NULL
162 * \param p_rng PRNG context for f_rng, or NULL
163 *
164 * \return
165 * - 0 if the following conditions are satisfied
166 * if all relevant parameters are provided:
Hanno Becker554c32d2017-10-17 10:21:53 +0100167 * - P prime if f_rng != NULL (%)
168 * - Q prime if f_rng != NULL (%)
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100169 * - 1 < N = P * Q
Hanno Beckera565f542017-10-11 11:00:19 +0100170 * - 1 < D, E < N
171 * - D and E are modular inverses modulo P-1 and Q-1
Hanno Becker554c32d2017-10-17 10:21:53 +0100172 * (%) This is only done if MBEDTLS_GENPRIME is defined.
Hanno Beckera565f542017-10-11 11:00:19 +0100173 * - A non-zero error code otherwise.
174 *
175 * \note The function can be used with a restricted set of arguments
176 * to perform specific checks only. E.g., calling it with
177 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
178 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200179int mbedtls_rsa_validate_params(const mbedtls_mpi *N,
180 const mbedtls_mpi *P,
181 const mbedtls_mpi *Q,
182 const mbedtls_mpi *D,
183 const mbedtls_mpi *E,
184 int (*f_rng)(void *, unsigned char *, size_t),
185 void *p_rng);
Hanno Beckera565f542017-10-11 11:00:19 +0100186
187/**
188 * \brief Check validity of RSA CRT parameters
189 *
190 * \note This is a 'static' helper function not operating on
191 * an RSA context. Alternative implementations need not
192 * overwrite it.
193 *
194 * \param P First prime factor of RSA modulus
195 * \param Q Second prime factor of RSA modulus
196 * \param D RSA private exponent
197 * \param DP MPI to check for D modulo P-1
198 * \param DQ MPI to check for D modulo P-1
199 * \param QP MPI to check for the modular inverse of Q modulo P.
200 *
201 * \return
202 * - 0 if the following conditions are satisfied:
203 * - D = DP mod P-1 if P, D, DP != NULL
204 * - Q = DQ mod P-1 if P, D, DQ != NULL
205 * - QP = Q^-1 mod P if P, Q, QP != NULL
206 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
207 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
208 * MPI calculations failed.
209 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
210 * data was provided to check DP, DQ or QP.
211 *
212 * \note The function can be used with a restricted set of arguments
213 * to perform specific checks only. E.g., calling it with the
214 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
215 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200216int mbedtls_rsa_validate_crt(const mbedtls_mpi *P,
217 const mbedtls_mpi *Q,
218 const mbedtls_mpi *D,
219 const mbedtls_mpi *DP,
220 const mbedtls_mpi *DQ,
221 const mbedtls_mpi *QP);
Hanno Beckera565f542017-10-11 11:00:19 +0100222
Andrzej Kurekccbd8a42018-03-13 07:52:09 -0400223#ifdef __cplusplus
224}
225#endif
226
Chris Jones66a4cd42021-03-09 16:04:12 +0000227#endif /* rsa_alt_helpers.h */