blob: 2464e6b082bab6277b1c839d69685fe6d276e1d2 [file] [log] [blame]
Hanno Beckera565f542017-10-11 11:00:19 +01001/**
2 * \file rsa_internal.h
3 *
4 * \brief Context-independent RSA helper functions
Simon Butcher001427b2018-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/*
Hanno Beckera565f542017-10-11 11:00:19 +010038 * Copyright (C) 2006-2017, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020039 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
40 *
41 * This file is provided under the Apache License 2.0, or the
42 * GNU General Public License v2.0 or later.
43 *
44 * **********
45 * Apache License 2.0:
Hanno Beckera565f542017-10-11 11:00:19 +010046 *
47 * Licensed under the Apache License, Version 2.0 (the "License"); you may
48 * not use this file except in compliance with the License.
49 * You may obtain a copy of the License at
50 *
51 * http://www.apache.org/licenses/LICENSE-2.0
52 *
53 * Unless required by applicable law or agreed to in writing, software
54 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
55 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
56 * See the License for the specific language governing permissions and
57 * limitations under the License.
58 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020059 * **********
60 *
61 * **********
62 * GNU General Public License v2.0 or later:
63 *
64 * This program is free software; you can redistribute it and/or modify
65 * it under the terms of the GNU General Public License as published by
66 * the Free Software Foundation; either version 2 of the License, or
67 * (at your option) any later version.
68 *
69 * This program is distributed in the hope that it will be useful,
70 * but WITHOUT ANY WARRANTY; without even the implied warranty of
71 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72 * GNU General Public License for more details.
73 *
74 * You should have received a copy of the GNU General Public License along
75 * with this program; if not, write to the Free Software Foundation, Inc.,
76 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
77 *
78 * **********
79 *
Hanno Beckera565f542017-10-11 11:00:19 +010080 * This file is part of mbed TLS (https://tls.mbed.org)
81 *
Hanno Beckera565f542017-10-11 11:00:19 +010082 */
83
84#ifndef MBEDTLS_RSA_INTERNAL_H
85#define MBEDTLS_RSA_INTERNAL_H
86
87#if !defined(MBEDTLS_CONFIG_FILE)
88#include "config.h"
89#else
90#include MBEDTLS_CONFIG_FILE
91#endif
92
93#include "bignum.h"
94
Hanno Beckera565f542017-10-11 11:00:19 +010095#ifdef __cplusplus
96extern "C" {
97#endif
98
99
100/**
101 * \brief Compute RSA prime moduli P, Q from public modulus N=PQ
102 * and a pair of private and public key.
103 *
104 * \note This is a 'static' helper function not operating on
105 * an RSA context. Alternative implementations need not
106 * overwrite it.
107 *
108 * \param N RSA modulus N = PQ, with P, Q to be found
Hanno Beckera565f542017-10-11 11:00:19 +0100109 * \param E RSA public exponent
Hanno Beckerc36aab62017-10-17 09:15:06 +0100110 * \param D RSA private exponent
Hanno Beckera565f542017-10-11 11:00:19 +0100111 * \param P Pointer to MPI holding first prime factor of N on success
112 * \param Q Pointer to MPI holding second prime factor of N on success
113 *
114 * \return
115 * - 0 if successful. In this case, P and Q constitute a
116 * factorization of N.
117 * - A non-zero error code otherwise.
118 *
119 * \note It is neither checked that P, Q are prime nor that
120 * D, E are modular inverses wrt. P-1 and Q-1. For that,
121 * use the helper function \c mbedtls_rsa_validate_params.
122 *
123 */
Hanno Beckerc36aab62017-10-17 09:15:06 +0100124int mbedtls_rsa_deduce_primes( mbedtls_mpi const *N, mbedtls_mpi const *E,
125 mbedtls_mpi const *D,
Hanno Beckera565f542017-10-11 11:00:19 +0100126 mbedtls_mpi *P, mbedtls_mpi *Q );
127
128/**
129 * \brief Compute RSA private exponent from
130 * prime moduli and public key.
131 *
132 * \note This is a 'static' helper function not operating on
133 * an RSA context. Alternative implementations need not
134 * overwrite it.
135 *
136 * \param P First prime factor of RSA modulus
137 * \param Q Second prime factor of RSA modulus
138 * \param E RSA public exponent
139 * \param D Pointer to MPI holding the private exponent on success.
140 *
141 * \return
142 * - 0 if successful. In this case, D is set to a simultaneous
143 * modular inverse of E modulo both P-1 and Q-1.
144 * - A non-zero error code otherwise.
145 *
146 * \note This function does not check whether P and Q are primes.
147 *
148 */
149int mbedtls_rsa_deduce_private_exponent( mbedtls_mpi const *P,
150 mbedtls_mpi const *Q,
151 mbedtls_mpi const *E,
152 mbedtls_mpi *D );
153
154
155/**
156 * \brief Generate RSA-CRT parameters
157 *
158 * \note This is a 'static' helper function not operating on
159 * an RSA context. Alternative implementations need not
160 * overwrite it.
161 *
162 * \param P First prime factor of N
163 * \param Q Second prime factor of N
164 * \param D RSA private exponent
165 * \param DP Output variable for D modulo P-1
166 * \param DQ Output variable for D modulo Q-1
167 * \param QP Output variable for the modular inverse of Q modulo P.
168 *
169 * \return 0 on success, non-zero error code otherwise.
170 *
171 * \note This function does not check whether P, Q are
172 * prime and whether D is a valid private exponent.
173 *
174 */
175int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
176 const mbedtls_mpi *D, mbedtls_mpi *DP,
177 mbedtls_mpi *DQ, mbedtls_mpi *QP );
178
179
180/**
181 * \brief Check validity of core RSA parameters
182 *
183 * \note This is a 'static' helper function not operating on
184 * an RSA context. Alternative implementations need not
185 * overwrite it.
186 *
187 * \param N RSA modulus N = PQ
188 * \param P First prime factor of N
189 * \param Q Second prime factor of N
190 * \param D RSA private exponent
191 * \param E RSA public exponent
192 * \param f_rng PRNG to be used for primality check, or NULL
193 * \param p_rng PRNG context for f_rng, or NULL
194 *
195 * \return
196 * - 0 if the following conditions are satisfied
197 * if all relevant parameters are provided:
Hanno Becker554c32d2017-10-17 10:21:53 +0100198 * - P prime if f_rng != NULL (%)
199 * - Q prime if f_rng != NULL (%)
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100200 * - 1 < N = P * Q
Hanno Beckera565f542017-10-11 11:00:19 +0100201 * - 1 < D, E < N
202 * - D and E are modular inverses modulo P-1 and Q-1
Hanno Becker554c32d2017-10-17 10:21:53 +0100203 * (%) This is only done if MBEDTLS_GENPRIME is defined.
Hanno Beckera565f542017-10-11 11:00:19 +0100204 * - A non-zero error code otherwise.
205 *
206 * \note The function can be used with a restricted set of arguments
207 * to perform specific checks only. E.g., calling it with
208 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
209 */
210int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
211 const mbedtls_mpi *Q, const mbedtls_mpi *D,
212 const mbedtls_mpi *E,
213 int (*f_rng)(void *, unsigned char *, size_t),
214 void *p_rng );
215
216/**
217 * \brief Check validity of RSA CRT parameters
218 *
219 * \note This is a 'static' helper function not operating on
220 * an RSA context. Alternative implementations need not
221 * overwrite it.
222 *
223 * \param P First prime factor of RSA modulus
224 * \param Q Second prime factor of RSA modulus
225 * \param D RSA private exponent
226 * \param DP MPI to check for D modulo P-1
227 * \param DQ MPI to check for D modulo P-1
228 * \param QP MPI to check for the modular inverse of Q modulo P.
229 *
230 * \return
231 * - 0 if the following conditions are satisfied:
232 * - D = DP mod P-1 if P, D, DP != NULL
233 * - Q = DQ mod P-1 if P, D, DQ != NULL
234 * - QP = Q^-1 mod P if P, Q, QP != NULL
235 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
236 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
237 * MPI calculations failed.
238 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
239 * data was provided to check DP, DQ or QP.
240 *
241 * \note The function can be used with a restricted set of arguments
242 * to perform specific checks only. E.g., calling it with the
243 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
244 */
245int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
246 const mbedtls_mpi *D, const mbedtls_mpi *DP,
247 const mbedtls_mpi *DQ, const mbedtls_mpi *QP );
248
Andrzej Kurekf21eaa12018-03-13 08:17:28 -0400249#ifdef __cplusplus
250}
251#endif
252
Hanno Beckera565f542017-10-11 11:00:19 +0100253#endif /* rsa_internal.h */