blob: 7e6a2ecd97d3d8f435b6f50af32dfa466cd39d3d [file] [log] [blame]
Hanno Beckera565f542017-10-11 11:00:19 +01001/**
2 * \file rsa_internal.h
3 *
4 * \brief Context-independent RSA helper functions
5 *
6 * Copyright (C) 2006-2017, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 *
23 *
24 * This file declares some RSA-related helper functions useful when
25 * implementing the RSA interface. They are public and provided in a
26 * separate compilation unit in order to make it easy for designers of
27 * alternative RSA implementations to use them in their code, as it is
28 * conceived that the functionality they provide will be necessary
29 * for most complete implementations.
30 *
31 * End-users of Mbed TLS not intending to re-implement the RSA functionality
32 * are not expected to get into the need of making use of these functions directly,
Hanno Beckerf8c028a2017-10-17 09:20:57 +010033 * but instead should be able to use the functions declared in rsa.h.
Hanno Beckera565f542017-10-11 11:00:19 +010034 *
35 * There are two classes of helper functions:
36 * (1) Parameter-generating helpers. These are:
37 * - mbedtls_rsa_deduce_primes
38 * - mbedtls_rsa_deduce_private_exponent
39 * - mbedtls_rsa_deduce_crt
40 * Each of these functions takes a set of core RSA parameters
41 * and generates some other, or CRT related parameters.
42 * (2) Parameter-checking helpers. These are:
43 * - mbedtls_rsa_validate_params
44 * - mbedtls_rsa_validate_crt
45 * They take a set of core or CRT related RSA parameters
46 * and check their validity.
47 *
48 */
49
50#ifndef MBEDTLS_RSA_INTERNAL_H
51#define MBEDTLS_RSA_INTERNAL_H
52
53#if !defined(MBEDTLS_CONFIG_FILE)
54#include "config.h"
55#else
56#include MBEDTLS_CONFIG_FILE
57#endif
58
59#include "bignum.h"
60
Hanno Beckera565f542017-10-11 11:00:19 +010061#ifdef __cplusplus
62extern "C" {
63#endif
64
65
66/**
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 */
Hanno Beckerc36aab62017-10-17 09:15:06 +010090int mbedtls_rsa_deduce_primes( mbedtls_mpi const *N, mbedtls_mpi const *E,
91 mbedtls_mpi const *D,
Hanno Beckera565f542017-10-11 11:00:19 +010092 mbedtls_mpi *P, mbedtls_mpi *Q );
93
94/**
95 * \brief Compute RSA private exponent from
96 * prime moduli and public key.
97 *
98 * \note This is a 'static' helper function not operating on
99 * an RSA context. Alternative implementations need not
100 * overwrite it.
101 *
102 * \param P First prime factor of RSA modulus
103 * \param Q Second prime factor of RSA modulus
104 * \param E RSA public exponent
105 * \param D Pointer to MPI holding the private exponent on success.
106 *
107 * \return
108 * - 0 if successful. In this case, D is set to a simultaneous
109 * modular inverse of E modulo both P-1 and Q-1.
110 * - A non-zero error code otherwise.
111 *
112 * \note This function does not check whether P and Q are primes.
113 *
114 */
115int mbedtls_rsa_deduce_private_exponent( mbedtls_mpi const *P,
116 mbedtls_mpi const *Q,
117 mbedtls_mpi const *E,
118 mbedtls_mpi *D );
119
120
121/**
122 * \brief Generate RSA-CRT parameters
123 *
124 * \note This is a 'static' helper function not operating on
125 * an RSA context. Alternative implementations need not
126 * overwrite it.
127 *
128 * \param P First prime factor of N
129 * \param Q Second prime factor of N
130 * \param D RSA private exponent
131 * \param DP Output variable for D modulo P-1
132 * \param DQ Output variable for D modulo Q-1
133 * \param QP Output variable for the modular inverse of Q modulo P.
134 *
135 * \return 0 on success, non-zero error code otherwise.
136 *
137 * \note This function does not check whether P, Q are
138 * prime and whether D is a valid private exponent.
139 *
140 */
141int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
142 const mbedtls_mpi *D, mbedtls_mpi *DP,
143 mbedtls_mpi *DQ, mbedtls_mpi *QP );
144
145
146/**
147 * \brief Check validity of core RSA parameters
148 *
149 * \note This is a 'static' helper function not operating on
150 * an RSA context. Alternative implementations need not
151 * overwrite it.
152 *
153 * \param N RSA modulus N = PQ
154 * \param P First prime factor of N
155 * \param Q Second prime factor of N
156 * \param D RSA private exponent
157 * \param E RSA public exponent
158 * \param f_rng PRNG to be used for primality check, or NULL
159 * \param p_rng PRNG context for f_rng, or NULL
160 *
161 * \return
162 * - 0 if the following conditions are satisfied
163 * if all relevant parameters are provided:
Hanno Becker554c32d2017-10-17 10:21:53 +0100164 * - P prime if f_rng != NULL (%)
165 * - Q prime if f_rng != NULL (%)
Hanno Beckerf8c028a2017-10-17 09:20:57 +0100166 * - 1 < N = P * Q
Hanno Beckera565f542017-10-11 11:00:19 +0100167 * - 1 < D, E < N
168 * - D and E are modular inverses modulo P-1 and Q-1
Hanno Becker554c32d2017-10-17 10:21:53 +0100169 * (%) This is only done if MBEDTLS_GENPRIME is defined.
Hanno Beckera565f542017-10-11 11:00:19 +0100170 * - A non-zero error code otherwise.
171 *
172 * \note The function can be used with a restricted set of arguments
173 * to perform specific checks only. E.g., calling it with
174 * (-,P,-,-,-) and a PRNG amounts to a primality check for P.
175 */
176int mbedtls_rsa_validate_params( const mbedtls_mpi *N, const mbedtls_mpi *P,
177 const mbedtls_mpi *Q, const mbedtls_mpi *D,
178 const mbedtls_mpi *E,
179 int (*f_rng)(void *, unsigned char *, size_t),
180 void *p_rng );
181
182/**
183 * \brief Check validity of RSA CRT parameters
184 *
185 * \note This is a 'static' helper function not operating on
186 * an RSA context. Alternative implementations need not
187 * overwrite it.
188 *
189 * \param P First prime factor of RSA modulus
190 * \param Q Second prime factor of RSA modulus
191 * \param D RSA private exponent
192 * \param DP MPI to check for D modulo P-1
193 * \param DQ MPI to check for D modulo P-1
194 * \param QP MPI to check for the modular inverse of Q modulo P.
195 *
196 * \return
197 * - 0 if the following conditions are satisfied:
198 * - D = DP mod P-1 if P, D, DP != NULL
199 * - Q = DQ mod P-1 if P, D, DQ != NULL
200 * - QP = Q^-1 mod P if P, Q, QP != NULL
201 * - \c MBEDTLS_ERR_RSA_KEY_CHECK_FAILED if check failed,
202 * potentially including \c MBEDTLS_ERR_MPI_XXX if some
203 * MPI calculations failed.
204 * - \c MBEDTLS_ERR_RSA_BAD_INPUT_DATA if insufficient
205 * data was provided to check DP, DQ or QP.
206 *
207 * \note The function can be used with a restricted set of arguments
208 * to perform specific checks only. E.g., calling it with the
209 * parameters (P, -, D, DP, -, -) will check DP = D mod P-1.
210 */
211int mbedtls_rsa_validate_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q,
212 const mbedtls_mpi *D, const mbedtls_mpi *DP,
213 const mbedtls_mpi *DQ, const mbedtls_mpi *QP );
214
Hanno Beckera565f542017-10-11 11:00:19 +0100215#endif /* rsa_internal.h */