blob: 8ff74a02bf9c80fb75d13f0680ea311c379c3f55 [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
Dave Rodgman16799db2023-11-02 19:47:20 +000039 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Hanno Beckera565f542017-10-11 11:00:19 +010040 */
Tomi Fontanilles573dc232023-12-10 14:57:51 +020041#ifndef MBEDTLS_RSA_ALT_HELPERS_H
42#define MBEDTLS_RSA_ALT_HELPERS_H
Hanno Beckera565f542017-10-11 11:00:19 +010043
Bence Szépkútic662b362021-05-27 11:25:03 +020044#include "mbedtls/build_info.h"
Hanno Beckera565f542017-10-11 11:00:19 +010045
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010046#include "mbedtls/bignum.h"
Hanno Beckera565f542017-10-11 11:00:19 +010047
Hanno Beckera565f542017-10-11 11:00:19 +010048#ifdef __cplusplus
49extern "C" {
50#endif
51
52
53/**
54 * \brief Compute RSA prime moduli P, Q from public modulus N=PQ
55 * and a pair of private and public key.
56 *
57 * \note This is a 'static' helper function not operating on
58 * an RSA context. Alternative implementations need not
59 * overwrite it.
60 *
61 * \param N RSA modulus N = PQ, with P, Q to be found
Hanno Beckera565f542017-10-11 11:00:19 +010062 * \param E RSA public exponent
Hanno Beckerc36aab62017-10-17 09:15:06 +010063 * \param D RSA private exponent
Hanno Beckera565f542017-10-11 11:00:19 +010064 * \param P Pointer to MPI holding first prime factor of N on success
65 * \param Q Pointer to MPI holding second prime factor of N on success
66 *
67 * \return
68 * - 0 if successful. In this case, P and Q constitute a
69 * factorization of N.
70 * - A non-zero error code otherwise.
71 *
72 * \note It is neither checked that P, Q are prime nor that
73 * D, E are modular inverses wrt. P-1 and Q-1. For that,
74 * use the helper function \c mbedtls_rsa_validate_params.
75 *
76 */
Gilles Peskine449bd832023-01-11 14:50:10 +010077int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N, mbedtls_mpi const *E,
78 mbedtls_mpi const *D,
79 mbedtls_mpi *P, mbedtls_mpi *Q);
Hanno Beckera565f542017-10-11 11:00:19 +010080
81/**
82 * \brief Compute RSA private exponent from
83 * prime moduli and public key.
84 *
85 * \note This is a 'static' helper function not operating on
86 * an RSA context. Alternative implementations need not
87 * overwrite it.
88 *
89 * \param P First prime factor of RSA modulus
90 * \param Q Second prime factor of RSA modulus
91 * \param E RSA public exponent
Manuel Pégourié-Gonnarda4bf6802025-07-10 10:48:23 +020092 * \param D Pointer to MPI holding the private exponent on success,
Manuel Pégourié-Gonnardc18eea62025-08-26 11:34:45 +020093 * i.e. the modular inverse of E modulo LCM(P-1,Q-1).
Hanno Beckera565f542017-10-11 11:00:19 +010094 *
Manuel Pégourié-Gonnarda4bf6802025-07-10 10:48:23 +020095 * \return \c 0 if successful.
96 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
97 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if E is not coprime to P-1
98 * and Q-1, that is, if GCD( E, (P-1)*(Q-1) ) != 1.
99 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if inputs are otherwise
100 * invalid.
Hanno Beckera565f542017-10-11 11:00:19 +0100101 *
102 * \note This function does not check whether P and Q are primes.
103 *
104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
106 mbedtls_mpi const *Q,
107 mbedtls_mpi const *E,
108 mbedtls_mpi *D);
Hanno Beckera565f542017-10-11 11:00:19 +0100109
110
111/**
112 * \brief Generate RSA-CRT parameters
113 *
114 * \note This is a 'static' helper function not operating on
115 * an RSA context. Alternative implementations need not
116 * overwrite it.
117 *
118 * \param P First prime factor of N
119 * \param Q Second prime factor of N
120 * \param D RSA private exponent
121 * \param DP Output variable for D modulo P-1
122 * \param DQ Output variable for D modulo Q-1
123 * \param QP Output variable for the modular inverse of Q modulo P.
124 *
125 * \return 0 on success, non-zero error code otherwise.
126 *
127 * \note This function does not check whether P, Q are
128 * prime and whether D is a valid private exponent.
129 *
130 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100131int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
132 const mbedtls_mpi *D, mbedtls_mpi *DP,
133 mbedtls_mpi *DQ, mbedtls_mpi *QP);
Hanno Beckera565f542017-10-11 11:00:19 +0100134
135
136/**
137 * \brief Check validity of core RSA parameters
138 *
139 * \note This is a 'static' helper function not operating on
140 * an RSA context. Alternative implementations need not
141 * overwrite it.
142 *
143 * \param N RSA modulus N = PQ
144 * \param P First prime factor of N
145 * \param Q Second prime factor of N
146 * \param D RSA private exponent
147 * \param E RSA public exponent
148 * \param f_rng PRNG to be used for primality check, or NULL
149 * \param p_rng PRNG context for f_rng, or NULL
150 *
151 * \return
152 * - 0 if the following conditions are satisfied
153 * if all relevant parameters are provided:
Hanno Becker554c32d2017-10-17 10:21:53 +0100154 * - P prime if f_rng != NULL (%)
155 * - Q prime if f_rng != NULL (%)
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100156 * - 1 < N = P * Q
Hanno Beckera565f542017-10-11 11:00:19 +0100157 * - 1 < D, E < N
158 * - D and E are modular inverses modulo P-1 and Q-1
Hanno Becker554c32d2017-10-17 10:21:53 +0100159 * (%) This is only done if MBEDTLS_GENPRIME is defined.
Hanno Beckera565f542017-10-11 11:00:19 +0100160 * - A non-zero error code otherwise.
161 *
162 * \note The function can be used with a restricted set of arguments
163 * to perform specific checks only. E.g., calling it with
164 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
165 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100166int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
167 const mbedtls_mpi *Q, const mbedtls_mpi *D,
168 const mbedtls_mpi *E,
169 int (*f_rng)(void *, unsigned char *, size_t),
170 void *p_rng);
Hanno Beckera565f542017-10-11 11:00:19 +0100171
172/**
173 * \brief Check validity of RSA CRT parameters
174 *
175 * \note This is a 'static' helper function not operating on
176 * an RSA context. Alternative implementations need not
177 * overwrite it.
178 *
179 * \param P First prime factor of RSA modulus
180 * \param Q Second prime factor of RSA modulus
181 * \param D RSA private exponent
182 * \param DP MPI to check for D modulo P-1
183 * \param DQ MPI to check for D modulo P-1
184 * \param QP MPI to check for the modular inverse of Q modulo P.
185 *
186 * \return
187 * - 0 if the following conditions are satisfied:
188 * - D = DP mod P-1 if P, D, DP != NULL
189 * - Q = DQ mod P-1 if P, D, DQ != NULL
190 * - QP = Q^-1 mod P if P, Q, QP != NULL
191 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
192 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
193 * MPI calculations failed.
194 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
195 * data was provided to check DP, DQ or QP.
196 *
197 * \note The function can be used with a restricted set of arguments
198 * to perform specific checks only. E.g., calling it with the
199 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
200 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201int mbedtls_rsa_validate_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
202 const mbedtls_mpi *D, const mbedtls_mpi *DP,
203 const mbedtls_mpi *DQ, const mbedtls_mpi *QP);
Hanno Beckera565f542017-10-11 11:00:19 +0100204
Andrzej Kurekccbd8a42018-03-13 07:52:09 -0400205#ifdef __cplusplus
206}
207#endif
208
Chris Jones66a4cd42021-03-09 16:04:12 +0000209#endif /* rsa_alt_helpers.h */