blob: ca0840b2a969406b3ff1eece43d9f59d3f2c112e [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 *
Hanno Beckera565f542017-10-11 11:00:19 +010041 */
42
43#ifndef MBEDTLS_RSA_INTERNAL_H
44#define MBEDTLS_RSA_INTERNAL_H
45
Bence Szépkútic662b362021-05-27 11:25:03 +020046#include "mbedtls/build_info.h"
Hanno Beckera565f542017-10-11 11:00:19 +010047
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010048#include "mbedtls/bignum.h"
Hanno Beckera565f542017-10-11 11:00:19 +010049
Hanno Beckera565f542017-10-11 11:00:19 +010050#ifdef __cplusplus
51extern "C" {
52#endif
53
54
55/**
56 * \brief Compute RSA prime moduli P, Q from public modulus N=PQ
57 * and a pair of private and public key.
58 *
59 * \note This is a 'static' helper function not operating on
60 * an RSA context. Alternative implementations need not
61 * overwrite it.
62 *
63 * \param N RSA modulus N = PQ, with P, Q to be found
Hanno Beckera565f542017-10-11 11:00:19 +010064 * \param E RSA public exponent
Hanno Beckerc36aab62017-10-17 09:15:06 +010065 * \param D RSA private exponent
Hanno Beckera565f542017-10-11 11:00:19 +010066 * \param P Pointer to MPI holding first prime factor of N on success
67 * \param Q Pointer to MPI holding second prime factor of N on success
68 *
69 * \return
70 * - 0 if successful. In this case, P and Q constitute a
71 * factorization of N.
72 * - A non-zero error code otherwise.
73 *
74 * \note It is neither checked that P, Q are prime nor that
75 * D, E are modular inverses wrt. P-1 and Q-1. For that,
76 * use the helper function \c mbedtls_rsa_validate_params.
77 *
78 */
Gilles Peskine449bd832023-01-11 14:50:10 +010079int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N, mbedtls_mpi const *E,
80 mbedtls_mpi const *D,
81 mbedtls_mpi *P, mbedtls_mpi *Q);
Hanno Beckera565f542017-10-11 11:00:19 +010082
83/**
84 * \brief Compute RSA private exponent from
85 * prime moduli and public key.
86 *
87 * \note This is a 'static' helper function not operating on
88 * an RSA context. Alternative implementations need not
89 * overwrite it.
90 *
91 * \param P First prime factor of RSA modulus
92 * \param Q Second prime factor of RSA modulus
93 * \param E RSA public exponent
94 * \param D Pointer to MPI holding the private exponent on success.
95 *
96 * \return
97 * - 0 if successful. In this case, D is set to a simultaneous
98 * modular inverse of E modulo both P-1 and Q-1.
99 * - A non-zero error code otherwise.
100 *
101 * \note This function does not check whether P and Q are primes.
102 *
103 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
105 mbedtls_mpi const *Q,
106 mbedtls_mpi const *E,
107 mbedtls_mpi *D);
Hanno Beckera565f542017-10-11 11:00:19 +0100108
109
110/**
111 * \brief Generate RSA-CRT parameters
112 *
113 * \note This is a 'static' helper function not operating on
114 * an RSA context. Alternative implementations need not
115 * overwrite it.
116 *
117 * \param P First prime factor of N
118 * \param Q Second prime factor of N
119 * \param D RSA private exponent
120 * \param DP Output variable for D modulo P-1
121 * \param DQ Output variable for D modulo Q-1
122 * \param QP Output variable for the modular inverse of Q modulo P.
123 *
124 * \return 0 on success, non-zero error code otherwise.
125 *
126 * \note This function does not check whether P, Q are
127 * prime and whether D is a valid private exponent.
128 *
129 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
131 const mbedtls_mpi *D, mbedtls_mpi *DP,
132 mbedtls_mpi *DQ, mbedtls_mpi *QP);
Hanno Beckera565f542017-10-11 11:00:19 +0100133
134
135/**
136 * \brief Check validity of core RSA parameters
137 *
138 * \note This is a 'static' helper function not operating on
139 * an RSA context. Alternative implementations need not
140 * overwrite it.
141 *
142 * \param N RSA modulus N = PQ
143 * \param P First prime factor of N
144 * \param Q Second prime factor of N
145 * \param D RSA private exponent
146 * \param E RSA public exponent
147 * \param f_rng PRNG to be used for primality check, or NULL
148 * \param p_rng PRNG context for f_rng, or NULL
149 *
150 * \return
151 * - 0 if the following conditions are satisfied
152 * if all relevant parameters are provided:
Hanno Becker554c32d2017-10-17 10:21:53 +0100153 * - P prime if f_rng != NULL (%)
154 * - Q prime if f_rng != NULL (%)
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100155 * - 1 < N = P * Q
Hanno Beckera565f542017-10-11 11:00:19 +0100156 * - 1 < D, E < N
157 * - D and E are modular inverses modulo P-1 and Q-1
Hanno Becker554c32d2017-10-17 10:21:53 +0100158 * (%) This is only done if MBEDTLS_GENPRIME is defined.
Hanno Beckera565f542017-10-11 11:00:19 +0100159 * - A non-zero error code otherwise.
160 *
161 * \note The function can be used with a restricted set of arguments
162 * to perform specific checks only. E.g., calling it with
163 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
164 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
166 const mbedtls_mpi *Q, const mbedtls_mpi *D,
167 const mbedtls_mpi *E,
168 int (*f_rng)(void *, unsigned char *, size_t),
169 void *p_rng);
Hanno Beckera565f542017-10-11 11:00:19 +0100170
171/**
172 * \brief Check validity of RSA CRT parameters
173 *
174 * \note This is a 'static' helper function not operating on
175 * an RSA context. Alternative implementations need not
176 * overwrite it.
177 *
178 * \param P First prime factor of RSA modulus
179 * \param Q Second prime factor of RSA modulus
180 * \param D RSA private exponent
181 * \param DP MPI to check for D modulo P-1
182 * \param DQ MPI to check for D modulo P-1
183 * \param QP MPI to check for the modular inverse of Q modulo P.
184 *
185 * \return
186 * - 0 if the following conditions are satisfied:
187 * - D = DP mod P-1 if P, D, DP != NULL
188 * - Q = DQ mod P-1 if P, D, DQ != NULL
189 * - QP = Q^-1 mod P if P, Q, QP != NULL
190 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
191 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
192 * MPI calculations failed.
193 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
194 * data was provided to check DP, DQ or QP.
195 *
196 * \note The function can be used with a restricted set of arguments
197 * to perform specific checks only. E.g., calling it with the
198 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
199 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200int mbedtls_rsa_validate_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
201 const mbedtls_mpi *D, const mbedtls_mpi *DP,
202 const mbedtls_mpi *DQ, const mbedtls_mpi *QP);
Hanno Beckera565f542017-10-11 11:00:19 +0100203
Andrzej Kurekccbd8a42018-03-13 07:52:09 -0400204#ifdef __cplusplus
205}
206#endif
207
Chris Jones66a4cd42021-03-09 16:04:12 +0000208#endif /* rsa_alt_helpers.h */