blob: 286cff2582809db5be9856f1a2af54e616207657 [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 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
Dave Rodgman7ff79652023-11-03 12:04:52 +000039 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Hanno Beckera565f542017-10-11 11:00:19 +010040 *
Hanno Beckera565f542017-10-11 11:00:19 +010041 */
42
43#ifndef MBEDTLS_RSA_INTERNAL_H
44#define MBEDTLS_RSA_INTERNAL_H
45
46#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010047#include "mbedtls/config.h"
Hanno Beckera565f542017-10-11 11:00:19 +010048#else
49#include MBEDTLS_CONFIG_FILE
50#endif
51
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010052#include "mbedtls/bignum.h"
Hanno Beckera565f542017-10-11 11:00:19 +010053
Hanno Beckera565f542017-10-11 11:00:19 +010054#ifdef __cplusplus
55extern "C" {
56#endif
57
58
59/**
60 * \brief Compute RSA prime moduli P, Q from public modulus N=PQ
61 * and a pair of private and public key.
62 *
63 * \note This is a 'static' helper function not operating on
64 * an RSA context. Alternative implementations need not
65 * overwrite it.
66 *
67 * \param N RSA modulus N = PQ, with P, Q to be found
Hanno Beckera565f542017-10-11 11:00:19 +010068 * \param E RSA public exponent
Hanno Beckerc36aab62017-10-17 09:15:06 +010069 * \param D RSA private exponent
Hanno Beckera565f542017-10-11 11:00:19 +010070 * \param P Pointer to MPI holding first prime factor of N on success
71 * \param Q Pointer to MPI holding second prime factor of N on success
72 *
73 * \return
74 * - 0 if successful. In this case, P and Q constitute a
75 * factorization of N.
76 * - A non-zero error code otherwise.
77 *
78 * \note It is neither checked that P, Q are prime nor that
79 * D, E are modular inverses wrt. P-1 and Q-1. For that,
80 * use the helper function \c mbedtls_rsa_validate_params.
81 *
82 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N, mbedtls_mpi const *E,
84 mbedtls_mpi const *D,
85 mbedtls_mpi *P, mbedtls_mpi *Q);
Hanno Beckera565f542017-10-11 11:00:19 +010086
87/**
88 * \brief Compute RSA private exponent from
89 * prime moduli and public key.
90 *
91 * \note This is a 'static' helper function not operating on
92 * an RSA context. Alternative implementations need not
93 * overwrite it.
94 *
95 * \param P First prime factor of RSA modulus
96 * \param Q Second prime factor of RSA modulus
97 * \param E RSA public exponent
98 * \param D Pointer to MPI holding the private exponent on success.
99 *
100 * \return
101 * - 0 if successful. In this case, D is set to a simultaneous
102 * modular inverse of E modulo both P-1 and Q-1.
103 * - A non-zero error code otherwise.
104 *
105 * \note This function does not check whether P and Q are primes.
106 *
107 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
109 mbedtls_mpi const *Q,
110 mbedtls_mpi const *E,
111 mbedtls_mpi *D);
Hanno Beckera565f542017-10-11 11:00:19 +0100112
113
114/**
115 * \brief Generate RSA-CRT parameters
116 *
117 * \note This is a 'static' helper function not operating on
118 * an RSA context. Alternative implementations need not
119 * overwrite it.
120 *
121 * \param P First prime factor of N
122 * \param Q Second prime factor of N
123 * \param D RSA private exponent
124 * \param DP Output variable for D modulo P-1
125 * \param DQ Output variable for D modulo Q-1
126 * \param QP Output variable for the modular inverse of Q modulo P.
127 *
128 * \return 0 on success, non-zero error code otherwise.
129 *
130 * \note This function does not check whether P, Q are
131 * prime and whether D is a valid private exponent.
132 *
133 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
135 const mbedtls_mpi *D, mbedtls_mpi *DP,
136 mbedtls_mpi *DQ, mbedtls_mpi *QP);
Hanno Beckera565f542017-10-11 11:00:19 +0100137
138
139/**
140 * \brief Check validity of core RSA parameters
141 *
142 * \note This is a 'static' helper function not operating on
143 * an RSA context. Alternative implementations need not
144 * overwrite it.
145 *
146 * \param N RSA modulus N = PQ
147 * \param P First prime factor of N
148 * \param Q Second prime factor of N
149 * \param D RSA private exponent
150 * \param E RSA public exponent
151 * \param f_rng PRNG to be used for primality check, or NULL
152 * \param p_rng PRNG context for f_rng, or NULL
153 *
154 * \return
155 * - 0 if the following conditions are satisfied
156 * if all relevant parameters are provided:
Hanno Becker554c32d2017-10-17 10:21:53 +0100157 * - P prime if f_rng != NULL (%)
158 * - Q prime if f_rng != NULL (%)
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100159 * - 1 < N = P * Q
Hanno Beckera565f542017-10-11 11:00:19 +0100160 * - 1 < D, E < N
161 * - D and E are modular inverses modulo P-1 and Q-1
Hanno Becker554c32d2017-10-17 10:21:53 +0100162 * (%) This is only done if MBEDTLS_GENPRIME is defined.
Hanno Beckera565f542017-10-11 11:00:19 +0100163 * - A non-zero error code otherwise.
164 *
165 * \note The function can be used with a restricted set of arguments
166 * to perform specific checks only. E.g., calling it with
167 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
168 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
170 const mbedtls_mpi *Q, const mbedtls_mpi *D,
171 const mbedtls_mpi *E,
172 int (*f_rng)(void *, unsigned char *, size_t),
173 void *p_rng);
Hanno Beckera565f542017-10-11 11:00:19 +0100174
175/**
176 * \brief Check validity of RSA CRT parameters
177 *
178 * \note This is a 'static' helper function not operating on
179 * an RSA context. Alternative implementations need not
180 * overwrite it.
181 *
182 * \param P First prime factor of RSA modulus
183 * \param Q Second prime factor of RSA modulus
184 * \param D RSA private exponent
185 * \param DP MPI to check for D modulo P-1
186 * \param DQ MPI to check for D modulo P-1
187 * \param QP MPI to check for the modular inverse of Q modulo P.
188 *
189 * \return
190 * - 0 if the following conditions are satisfied:
191 * - D = DP mod P-1 if P, D, DP != NULL
192 * - Q = DQ mod P-1 if P, D, DQ != NULL
193 * - QP = Q^-1 mod P if P, Q, QP != NULL
194 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
195 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
196 * MPI calculations failed.
197 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
198 * data was provided to check DP, DQ or QP.
199 *
200 * \note The function can be used with a restricted set of arguments
201 * to perform specific checks only. E.g., calling it with the
202 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
203 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204int mbedtls_rsa_validate_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
205 const mbedtls_mpi *D, const mbedtls_mpi *DP,
206 const mbedtls_mpi *DQ, const mbedtls_mpi *QP);
Hanno Beckera565f542017-10-11 11:00:19 +0100207
Andrzej Kurekccbd8a42018-03-13 07:52:09 -0400208#ifdef __cplusplus
209}
210#endif
211
Hanno Beckera565f542017-10-11 11:00:19 +0100212#endif /* rsa_internal.h */