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