Mbed OS Reference
Loading...
Searching...
No Matches
rsa.h
Go to the documentation of this file.
1/**
2 * \file rsa.h
3 *
4 * \brief This file provides an API for the RSA public-key cryptosystem.
5 *
6 * The RSA public-key cryptosystem is defined in <em>Public-Key
7 * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em>
8 * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1:
9 * RSA Cryptography Specifications</em>.
10 *
11 */
12/*
13 * Copyright The Mbed TLS Contributors
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28#ifndef MBEDTLS_RSA_H
29#define MBEDTLS_RSA_H
30
31#if !defined(MBEDTLS_CONFIG_FILE)
32#include "mbedtls/config.h"
33#else
34#include MBEDTLS_CONFIG_FILE
35#endif
36
37#include "mbedtls/bignum.h"
38#include "mbedtls/md.h"
39
40#if defined(MBEDTLS_THREADING_C)
41#include "mbedtls/threading.h"
42#endif
43
44/**
45 * \addtogroup mbedtls
46 * \{
47 * \defgroup mbedtls_rsa_module RSA
48 * \{
49 */
50
51/*
52 * RSA Error codes
53 */
54#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
55#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
56#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
57#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the validity check of the library. */
58#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
59#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
60#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
61#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
62#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
63
64/* MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION is deprecated and should not be used.
65 */
66#define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500 /**< The implementation does not offer the requested operation, for example, because of security violations or lack of functionality. */
67
68/* MBEDTLS_ERR_RSA_HW_ACCEL_FAILED is deprecated and should not be used. */
69#define MBEDTLS_ERR_RSA_HW_ACCEL_FAILED -0x4580 /**< RSA hardware accelerator failed. */
70
71/*
72 * RSA constants
73 */
74#define MBEDTLS_RSA_PUBLIC 0 /**< Request private key operation. */
75#define MBEDTLS_RSA_PRIVATE 1 /**< Request public key operation. */
76
77#define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */
78#define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */
79
80#define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */
81#define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */
82
83#define MBEDTLS_RSA_SALT_LEN_ANY -1 ///< Constant for mbedtls_rsa_rsassa_pss_verify_ext to accept any salt length
84
85/*
86 * The above constants may be used even if the RSA module is compile out,
87 * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
88 */
89
90#ifdef __cplusplus
91extern "C" {
92#endif
93
94#if !defined(MBEDTLS_RSA_ALT)
95// Regular implementation
96//
97
98/**
99 * \brief The RSA context structure.
100 *
101 * \note Direct manipulation of the members of this structure
102 * is deprecated. All manipulation should instead be done through
103 * the public interface functions.
104 */
106{
107 int ver; /*!< Always 0.*/
108 size_t len; /*!< The size of \p N in Bytes. */
109
110 mbedtls_mpi N; /*!< The public modulus. */
111 mbedtls_mpi E; /*!< The public exponent. */
112
113 mbedtls_mpi D; /*!< The private exponent. */
114 mbedtls_mpi P; /*!< The first prime factor. */
115 mbedtls_mpi Q; /*!< The second prime factor. */
116
117 mbedtls_mpi DP; /*!< <code>D % (P - 1)</code>. */
118 mbedtls_mpi DQ; /*!< <code>D % (Q - 1)</code>. */
119 mbedtls_mpi QP; /*!< <code>1 / (Q % P)</code>. */
120
121 mbedtls_mpi RN; /*!< cached <code>R^2 mod N</code>. */
122
123 mbedtls_mpi RP; /*!< cached <code>R^2 mod P</code>. */
124 mbedtls_mpi RQ; /*!< cached <code>R^2 mod Q</code>. */
125
126 mbedtls_mpi Vi; /*!< The cached blinding value. */
127 mbedtls_mpi Vf; /*!< The cached un-blinding value. */
128
129 int padding; /*!< Selects padding mode:
130 #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and
131 #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */
132 int hash_id; /*!< Hash identifier of mbedtls_md_type_t type,
133 as specified in md.h for use in the MGF
134 mask generating function used in the
135 EME-OAEP and EMSA-PSS encodings. */
136#if defined(MBEDTLS_THREADING_C)
137 mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex. */
138#endif
139}
141
142#else /* MBEDTLS_RSA_ALT */
143#include "rsa_alt.h"
144#endif /* MBEDTLS_RSA_ALT */
145
146/**
147 * \brief This function initializes an RSA context.
148 *
149 * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
150 * encryption scheme and the RSASSA-PSS signature scheme.
151 *
152 * \note The \p hash_id parameter is ignored when using
153 * #MBEDTLS_RSA_PKCS_V15 padding.
154 *
155 * \note The choice of padding mode is strictly enforced for private key
156 * operations, since there might be security concerns in
157 * mixing padding modes. For public key operations it is
158 * a default value, which can be overridden by calling specific
159 * \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
160 *
161 * \note The hash selected in \p hash_id is always used for OEAP
162 * encryption. For PSS signatures, it is always used for
163 * making signatures, but can be overridden for verifying them.
164 * If set to #MBEDTLS_MD_NONE, it is always overridden.
165 *
166 * \param ctx The RSA context to initialize. This must not be \c NULL.
167 * \param padding The padding mode to use. This must be either
168 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
169 * \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
170 * \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
171 * otherwise.
172 */
174 int padding,
175 int hash_id );
176
177/**
178 * \brief This function imports a set of core parameters into an
179 * RSA context.
180 *
181 * \note This function can be called multiple times for successive
182 * imports, if the parameters are not simultaneously present.
183 *
184 * Any sequence of calls to this function should be followed
185 * by a call to mbedtls_rsa_complete(), which checks and
186 * completes the provided information to a ready-for-use
187 * public or private RSA key.
188 *
189 * \note See mbedtls_rsa_complete() for more information on which
190 * parameters are necessary to set up a private or public
191 * RSA key.
192 *
193 * \note The imported parameters are copied and need not be preserved
194 * for the lifetime of the RSA context being set up.
195 *
196 * \param ctx The initialized RSA context to store the parameters in.
197 * \param N The RSA modulus. This may be \c NULL.
198 * \param P The first prime factor of \p N. This may be \c NULL.
199 * \param Q The second prime factor of \p N. This may be \c NULL.
200 * \param D The private exponent. This may be \c NULL.
201 * \param E The public exponent. This may be \c NULL.
202 *
203 * \return \c 0 on success.
204 * \return A non-zero error code on failure.
205 */
207 const mbedtls_mpi *N,
208 const mbedtls_mpi *P, const mbedtls_mpi *Q,
209 const mbedtls_mpi *D, const mbedtls_mpi *E );
210
211/**
212 * \brief This function imports core RSA parameters, in raw big-endian
213 * binary format, into an RSA context.
214 *
215 * \note This function can be called multiple times for successive
216 * imports, if the parameters are not simultaneously present.
217 *
218 * Any sequence of calls to this function should be followed
219 * by a call to mbedtls_rsa_complete(), which checks and
220 * completes the provided information to a ready-for-use
221 * public or private RSA key.
222 *
223 * \note See mbedtls_rsa_complete() for more information on which
224 * parameters are necessary to set up a private or public
225 * RSA key.
226 *
227 * \note The imported parameters are copied and need not be preserved
228 * for the lifetime of the RSA context being set up.
229 *
230 * \param ctx The initialized RSA context to store the parameters in.
231 * \param N The RSA modulus. This may be \c NULL.
232 * \param N_len The Byte length of \p N; it is ignored if \p N == NULL.
233 * \param P The first prime factor of \p N. This may be \c NULL.
234 * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL.
235 * \param Q The second prime factor of \p N. This may be \c NULL.
236 * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL.
237 * \param D The private exponent. This may be \c NULL.
238 * \param D_len The Byte length of \p D; it is ignored if \p D == NULL.
239 * \param E The public exponent. This may be \c NULL.
240 * \param E_len The Byte length of \p E; it is ignored if \p E == NULL.
241 *
242 * \return \c 0 on success.
243 * \return A non-zero error code on failure.
244 */
246 unsigned char const *N, size_t N_len,
247 unsigned char const *P, size_t P_len,
248 unsigned char const *Q, size_t Q_len,
249 unsigned char const *D, size_t D_len,
250 unsigned char const *E, size_t E_len );
251
252/**
253 * \brief This function completes an RSA context from
254 * a set of imported core parameters.
255 *
256 * To setup an RSA public key, precisely \p N and \p E
257 * must have been imported.
258 *
259 * To setup an RSA private key, sufficient information must
260 * be present for the other parameters to be derivable.
261 *
262 * The default implementation supports the following:
263 * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li>
264 * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul>
265 * Alternative implementations need not support these.
266 *
267 * If this function runs successfully, it guarantees that
268 * the RSA context can be used for RSA operations without
269 * the risk of failure or crash.
270 *
271 * \warning This function need not perform consistency checks
272 * for the imported parameters. In particular, parameters that
273 * are not needed by the implementation might be silently
274 * discarded and left unchecked. To check the consistency
275 * of the key material, see mbedtls_rsa_check_privkey().
276 *
277 * \param ctx The initialized RSA context holding imported parameters.
278 *
279 * \return \c 0 on success.
280 * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations
281 * failed.
282 *
283 */
285
286/**
287 * \brief This function exports the core parameters of an RSA key.
288 *
289 * If this function runs successfully, the non-NULL buffers
290 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
291 * written, with additional unused space filled leading by
292 * zero Bytes.
293 *
294 * Possible reasons for returning
295 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
296 * <li>An alternative RSA implementation is in use, which
297 * stores the key externally, and either cannot or should
298 * not export it into RAM.</li>
299 * <li>A SW or HW implementation might not support a certain
300 * deduction. For example, \p P, \p Q from \p N, \p D,
301 * and \p E if the former are not part of the
302 * implementation.</li></ul>
303 *
304 * If the function fails due to an unsupported operation,
305 * the RSA context stays intact and remains usable.
306 *
307 * \param ctx The initialized RSA context.
308 * \param N The MPI to hold the RSA modulus.
309 * This may be \c NULL if this field need not be exported.
310 * \param P The MPI to hold the first prime factor of \p N.
311 * This may be \c NULL if this field need not be exported.
312 * \param Q The MPI to hold the second prime factor of \p N.
313 * This may be \c NULL if this field need not be exported.
314 * \param D The MPI to hold the private exponent.
315 * This may be \c NULL if this field need not be exported.
316 * \param E The MPI to hold the public exponent.
317 * This may be \c NULL if this field need not be exported.
318 *
319 * \return \c 0 on success.
320 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
321 * requested parameters cannot be done due to missing
322 * functionality or because of security policies.
323 * \return A non-zero return code on any other failure.
324 *
325 */
328 mbedtls_mpi *D, mbedtls_mpi *E );
329
330/**
331 * \brief This function exports core parameters of an RSA key
332 * in raw big-endian binary format.
333 *
334 * If this function runs successfully, the non-NULL buffers
335 * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully
336 * written, with additional unused space filled leading by
337 * zero Bytes.
338 *
339 * Possible reasons for returning
340 * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul>
341 * <li>An alternative RSA implementation is in use, which
342 * stores the key externally, and either cannot or should
343 * not export it into RAM.</li>
344 * <li>A SW or HW implementation might not support a certain
345 * deduction. For example, \p P, \p Q from \p N, \p D,
346 * and \p E if the former are not part of the
347 * implementation.</li></ul>
348 * If the function fails due to an unsupported operation,
349 * the RSA context stays intact and remains usable.
350 *
351 * \note The length parameters are ignored if the corresponding
352 * buffer pointers are NULL.
353 *
354 * \param ctx The initialized RSA context.
355 * \param N The Byte array to store the RSA modulus,
356 * or \c NULL if this field need not be exported.
357 * \param N_len The size of the buffer for the modulus.
358 * \param P The Byte array to hold the first prime factor of \p N,
359 * or \c NULL if this field need not be exported.
360 * \param P_len The size of the buffer for the first prime factor.
361 * \param Q The Byte array to hold the second prime factor of \p N,
362 * or \c NULL if this field need not be exported.
363 * \param Q_len The size of the buffer for the second prime factor.
364 * \param D The Byte array to hold the private exponent,
365 * or \c NULL if this field need not be exported.
366 * \param D_len The size of the buffer for the private exponent.
367 * \param E The Byte array to hold the public exponent,
368 * or \c NULL if this field need not be exported.
369 * \param E_len The size of the buffer for the public exponent.
370 *
371 * \return \c 0 on success.
372 * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the
373 * requested parameters cannot be done due to missing
374 * functionality or because of security policies.
375 * \return A non-zero return code on any other failure.
376 */
378 unsigned char *N, size_t N_len,
379 unsigned char *P, size_t P_len,
380 unsigned char *Q, size_t Q_len,
381 unsigned char *D, size_t D_len,
382 unsigned char *E, size_t E_len );
383
384/**
385 * \brief This function exports CRT parameters of a private RSA key.
386 *
387 * \note Alternative RSA implementations not using CRT-parameters
388 * internally can implement this function based on
389 * mbedtls_rsa_deduce_opt().
390 *
391 * \param ctx The initialized RSA context.
392 * \param DP The MPI to hold \c D modulo `P-1`,
393 * or \c NULL if it need not be exported.
394 * \param DQ The MPI to hold \c D modulo `Q-1`,
395 * or \c NULL if it need not be exported.
396 * \param QP The MPI to hold modular inverse of \c Q modulo \c P,
397 * or \c NULL if it need not be exported.
398 *
399 * \return \c 0 on success.
400 * \return A non-zero error code on failure.
401 *
402 */
404 mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
405
406/**
407 * \brief This function sets padding for an already initialized RSA
408 * context. See mbedtls_rsa_init() for details.
409 *
410 * \param ctx The initialized RSA context to be configured.
411 * \param padding The padding mode to use. This must be either
412 * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
413 * \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
414 */
416 int hash_id );
417
418/**
419 * \brief This function retrieves the length of RSA modulus in Bytes.
420 *
421 * \param ctx The initialized RSA context.
422 *
423 * \return The length of the RSA modulus in Bytes.
424 *
425 */
427
428/**
429 * \brief This function generates an RSA keypair.
430 *
431 * \note mbedtls_rsa_init() must be called before this function,
432 * to set up the RSA context.
433 *
434 * \param ctx The initialized RSA context used to hold the key.
435 * \param f_rng The RNG function to be used for key generation.
436 * This must not be \c NULL.
437 * \param p_rng The RNG context to be passed to \p f_rng.
438 * This may be \c NULL if \p f_rng doesn't need a context.
439 * \param nbits The size of the public key in bits.
440 * \param exponent The public exponent to use. For example, \c 65537.
441 * This must be odd and greater than \c 1.
442 *
443 * \return \c 0 on success.
444 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
445 */
447 int (*f_rng)(void *, unsigned char *, size_t),
448 void *p_rng,
449 unsigned int nbits, int exponent );
450
451/**
452 * \brief This function checks if a context contains at least an RSA
453 * public key.
454 *
455 * If the function runs successfully, it is guaranteed that
456 * enough information is present to perform an RSA public key
457 * operation using mbedtls_rsa_public().
458 *
459 * \param ctx The initialized RSA context to check.
460 *
461 * \return \c 0 on success.
462 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
463 *
464 */
466
467/**
468 * \brief This function checks if a context contains an RSA private key
469 * and perform basic consistency checks.
470 *
471 * \note The consistency checks performed by this function not only
472 * ensure that mbedtls_rsa_private() can be called successfully
473 * on the given context, but that the various parameters are
474 * mutually consistent with high probability, in the sense that
475 * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses.
476 *
477 * \warning This function should catch accidental misconfigurations
478 * like swapping of parameters, but it cannot establish full
479 * trust in neither the quality nor the consistency of the key
480 * material that was used to setup the given RSA context:
481 * <ul><li>Consistency: Imported parameters that are irrelevant
482 * for the implementation might be silently dropped. If dropped,
483 * the current function does not have access to them,
484 * and therefore cannot check them. See mbedtls_rsa_complete().
485 * If you want to check the consistency of the entire
486 * content of an PKCS1-encoded RSA private key, for example, you
487 * should use mbedtls_rsa_validate_params() before setting
488 * up the RSA context.
489 * Additionally, if the implementation performs empirical checks,
490 * these checks substantiate but do not guarantee consistency.</li>
491 * <li>Quality: This function is not expected to perform
492 * extended quality assessments like checking that the prime
493 * factors are safe. Additionally, it is the responsibility of the
494 * user to ensure the trustworthiness of the source of his RSA
495 * parameters, which goes beyond what is effectively checkable
496 * by the library.</li></ul>
497 *
498 * \param ctx The initialized RSA context to check.
499 *
500 * \return \c 0 on success.
501 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
502 */
504
505/**
506 * \brief This function checks a public-private RSA key pair.
507 *
508 * It checks each of the contexts, and makes sure they match.
509 *
510 * \param pub The initialized RSA context holding the public key.
511 * \param prv The initialized RSA context holding the private key.
512 *
513 * \return \c 0 on success.
514 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
515 */
517 const mbedtls_rsa_context *prv );
518
519/**
520 * \brief This function performs an RSA public key operation.
521 *
522 * \param ctx The initialized RSA context to use.
523 * \param input The input buffer. This must be a readable buffer
524 * of length \c ctx->len Bytes. For example, \c 256 Bytes
525 * for an 2048-bit RSA modulus.
526 * \param output The output buffer. This must be a writable buffer
527 * of length \c ctx->len Bytes. For example, \c 256 Bytes
528 * for an 2048-bit RSA modulus.
529 *
530 * \note This function does not handle message padding.
531 *
532 * \note Make sure to set \p input[0] = 0 or ensure that
533 * input is smaller than \p N.
534 *
535 * \return \c 0 on success.
536 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
537 */
539 const unsigned char *input,
540 unsigned char *output );
541
542/**
543 * \brief This function performs an RSA private key operation.
544 *
545 * \note Blinding is used if and only if a PRNG is provided.
546 *
547 * \note If blinding is used, both the base of exponentation
548 * and the exponent are blinded, providing protection
549 * against some side-channel attacks.
550 *
551 * \warning It is deprecated and a security risk to not provide
552 * a PRNG here and thereby prevent the use of blinding.
553 * Future versions of the library may enforce the presence
554 * of a PRNG.
555 *
556 * \param ctx The initialized RSA context to use.
557 * \param f_rng The RNG function, used for blinding. It is discouraged
558 * and deprecated to pass \c NULL here, in which case
559 * blinding will be omitted.
560 * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL
561 * if \p f_rng is \c NULL or if \p f_rng doesn't need a context.
562 * \param input The input buffer. This must be a readable buffer
563 * of length \c ctx->len Bytes. For example, \c 256 Bytes
564 * for an 2048-bit RSA modulus.
565 * \param output The output buffer. This must be a writable buffer
566 * of length \c ctx->len Bytes. For example, \c 256 Bytes
567 * for an 2048-bit RSA modulus.
568 *
569 * \return \c 0 on success.
570 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
571 *
572 */
574 int (*f_rng)(void *, unsigned char *, size_t),
575 void *p_rng,
576 const unsigned char *input,
577 unsigned char *output );
578
579/**
580 * \brief This function adds the message padding, then performs an RSA
581 * operation.
582 *
583 * It is the generic wrapper for performing a PKCS#1 encryption
584 * operation using the \p mode from the context.
585 *
586 * \deprecated It is deprecated and discouraged to call this function
587 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
588 * are likely to remove the \p mode argument and have it
589 * implicitly set to #MBEDTLS_RSA_PUBLIC.
590 *
591 * \note Alternative implementations of RSA need not support
592 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
593 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
594 *
595 * \param ctx The initialized RSA context to use.
596 * \param f_rng The RNG to use. It is mandatory for PKCS#1 v2.1 padding
597 * encoding, and for PKCS#1 v1.5 padding encoding when used
598 * with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5
599 * padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE,
600 * it is used for blinding and should be provided in this
601 * case; see mbedtls_rsa_private() for more.
602 * \param p_rng The RNG context to be passed to \p f_rng. May be
603 * \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't
604 * need a context argument.
605 * \param mode The mode of operation. This must be either
606 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
607 * \param ilen The length of the plaintext in Bytes.
608 * \param input The input data to encrypt. This must be a readable
609 * buffer of size \p ilen Bytes. It may be \c NULL if
610 * `ilen == 0`.
611 * \param output The output buffer. This must be a writable buffer
612 * of length \c ctx->len Bytes. For example, \c 256 Bytes
613 * for an 2048-bit RSA modulus.
614 *
615 * \return \c 0 on success.
616 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
617 */
619 int (*f_rng)(void *, unsigned char *, size_t),
620 void *p_rng,
621 int mode, size_t ilen,
622 const unsigned char *input,
623 unsigned char *output );
624
625/**
626 * \brief This function performs a PKCS#1 v1.5 encryption operation
627 * (RSAES-PKCS1-v1_5-ENCRYPT).
628 *
629 * \deprecated It is deprecated and discouraged to call this function
630 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
631 * are likely to remove the \p mode argument and have it
632 * implicitly set to #MBEDTLS_RSA_PUBLIC.
633 *
634 * \note Alternative implementations of RSA need not support
635 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
636 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
637 *
638 * \param ctx The initialized RSA context to use.
639 * \param f_rng The RNG function to use. It is needed for padding generation
640 * if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is
641 * #MBEDTLS_RSA_PRIVATE (discouraged), it is used for
642 * blinding and should be provided; see mbedtls_rsa_private().
643 * \param p_rng The RNG context to be passed to \p f_rng. This may
644 * be \c NULL if \p f_rng is \c NULL or if \p f_rng
645 * doesn't need a context argument.
646 * \param mode The mode of operation. This must be either
647 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
648 * \param ilen The length of the plaintext in Bytes.
649 * \param input The input data to encrypt. This must be a readable
650 * buffer of size \p ilen Bytes. It may be \c NULL if
651 * `ilen == 0`.
652 * \param output The output buffer. This must be a writable buffer
653 * of length \c ctx->len Bytes. For example, \c 256 Bytes
654 * for an 2048-bit RSA modulus.
655 *
656 * \return \c 0 on success.
657 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
658 */
660 int (*f_rng)(void *, unsigned char *, size_t),
661 void *p_rng,
662 int mode, size_t ilen,
663 const unsigned char *input,
664 unsigned char *output );
665
666/**
667 * \brief This function performs a PKCS#1 v2.1 OAEP encryption
668 * operation (RSAES-OAEP-ENCRYPT).
669 *
670 * \note The output buffer must be as large as the size
671 * of ctx->N. For example, 128 Bytes if RSA-1024 is used.
672 *
673 * \deprecated It is deprecated and discouraged to call this function
674 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
675 * are likely to remove the \p mode argument and have it
676 * implicitly set to #MBEDTLS_RSA_PUBLIC.
677 *
678 * \note Alternative implementations of RSA need not support
679 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
680 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
681 *
682 * \param ctx The initnialized RSA context to use.
683 * \param f_rng The RNG function to use. This is needed for padding
684 * generation and must be provided.
685 * \param p_rng The RNG context to be passed to \p f_rng. This may
686 * be \c NULL if \p f_rng doesn't need a context argument.
687 * \param mode The mode of operation. This must be either
688 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
689 * \param label The buffer holding the custom label to use.
690 * This must be a readable buffer of length \p label_len
691 * Bytes. It may be \c NULL if \p label_len is \c 0.
692 * \param label_len The length of the label in Bytes.
693 * \param ilen The length of the plaintext buffer \p input in Bytes.
694 * \param input The input data to encrypt. This must be a readable
695 * buffer of size \p ilen Bytes. It may be \c NULL if
696 * `ilen == 0`.
697 * \param output The output buffer. This must be a writable buffer
698 * of length \c ctx->len Bytes. For example, \c 256 Bytes
699 * for an 2048-bit RSA modulus.
700 *
701 * \return \c 0 on success.
702 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
703 */
705 int (*f_rng)(void *, unsigned char *, size_t),
706 void *p_rng,
707 int mode,
708 const unsigned char *label, size_t label_len,
709 size_t ilen,
710 const unsigned char *input,
711 unsigned char *output );
712
713/**
714 * \brief This function performs an RSA operation, then removes the
715 * message padding.
716 *
717 * It is the generic wrapper for performing a PKCS#1 decryption
718 * operation using the \p mode from the context.
719 *
720 * \note The output buffer length \c output_max_len should be
721 * as large as the size \p ctx->len of \p ctx->N (for example,
722 * 128 Bytes if RSA-1024 is used) to be able to hold an
723 * arbitrary decrypted message. If it is not large enough to
724 * hold the decryption of the particular ciphertext provided,
725 * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
726 *
727 * \deprecated It is deprecated and discouraged to call this function
728 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
729 * are likely to remove the \p mode argument and have it
730 * implicitly set to #MBEDTLS_RSA_PRIVATE.
731 *
732 * \note Alternative implementations of RSA need not support
733 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
734 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
735 *
736 * \param ctx The initialized RSA context to use.
737 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
738 * this is used for blinding and should be provided; see
739 * mbedtls_rsa_private() for more. If \p mode is
740 * #MBEDTLS_RSA_PUBLIC, it is ignored.
741 * \param p_rng The RNG context to be passed to \p f_rng. This may be
742 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
743 * \param mode The mode of operation. This must be either
744 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
745 * \param olen The address at which to store the length of
746 * the plaintext. This must not be \c NULL.
747 * \param input The ciphertext buffer. This must be a readable buffer
748 * of length \c ctx->len Bytes. For example, \c 256 Bytes
749 * for an 2048-bit RSA modulus.
750 * \param output The buffer used to hold the plaintext. This must
751 * be a writable buffer of length \p output_max_len Bytes.
752 * \param output_max_len The length in Bytes of the output buffer \p output.
753 *
754 * \return \c 0 on success.
755 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
756 */
758 int (*f_rng)(void *, unsigned char *, size_t),
759 void *p_rng,
760 int mode, size_t *olen,
761 const unsigned char *input,
762 unsigned char *output,
763 size_t output_max_len );
764
765/**
766 * \brief This function performs a PKCS#1 v1.5 decryption
767 * operation (RSAES-PKCS1-v1_5-DECRYPT).
768 *
769 * \note The output buffer length \c output_max_len should be
770 * as large as the size \p ctx->len of \p ctx->N, for example,
771 * 128 Bytes if RSA-1024 is used, to be able to hold an
772 * arbitrary decrypted message. If it is not large enough to
773 * hold the decryption of the particular ciphertext provided,
774 * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
775 *
776 * \deprecated It is deprecated and discouraged to call this function
777 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
778 * are likely to remove the \p mode argument and have it
779 * implicitly set to #MBEDTLS_RSA_PRIVATE.
780 *
781 * \note Alternative implementations of RSA need not support
782 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
783 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
784 *
785 * \param ctx The initialized RSA context to use.
786 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
787 * this is used for blinding and should be provided; see
788 * mbedtls_rsa_private() for more. If \p mode is
789 * #MBEDTLS_RSA_PUBLIC, it is ignored.
790 * \param p_rng The RNG context to be passed to \p f_rng. This may be
791 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
792 * \param mode The mode of operation. This must be either
793 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
794 * \param olen The address at which to store the length of
795 * the plaintext. This must not be \c NULL.
796 * \param input The ciphertext buffer. This must be a readable buffer
797 * of length \c ctx->len Bytes. For example, \c 256 Bytes
798 * for an 2048-bit RSA modulus.
799 * \param output The buffer used to hold the plaintext. This must
800 * be a writable buffer of length \p output_max_len Bytes.
801 * \param output_max_len The length in Bytes of the output buffer \p output.
802 *
803 * \return \c 0 on success.
804 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
805 *
806 */
808 int (*f_rng)(void *, unsigned char *, size_t),
809 void *p_rng,
810 int mode, size_t *olen,
811 const unsigned char *input,
812 unsigned char *output,
813 size_t output_max_len );
814
815/**
816 * \brief This function performs a PKCS#1 v2.1 OAEP decryption
817 * operation (RSAES-OAEP-DECRYPT).
818 *
819 * \note The output buffer length \c output_max_len should be
820 * as large as the size \p ctx->len of \p ctx->N, for
821 * example, 128 Bytes if RSA-1024 is used, to be able to
822 * hold an arbitrary decrypted message. If it is not
823 * large enough to hold the decryption of the particular
824 * ciphertext provided, the function returns
825 * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE.
826 *
827 * \deprecated It is deprecated and discouraged to call this function
828 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
829 * are likely to remove the \p mode argument and have it
830 * implicitly set to #MBEDTLS_RSA_PRIVATE.
831 *
832 * \note Alternative implementations of RSA need not support
833 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
834 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
835 *
836 * \param ctx The initialized RSA context to use.
837 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
838 * this is used for blinding and should be provided; see
839 * mbedtls_rsa_private() for more. If \p mode is
840 * #MBEDTLS_RSA_PUBLIC, it is ignored.
841 * \param p_rng The RNG context to be passed to \p f_rng. This may be
842 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
843 * \param mode The mode of operation. This must be either
844 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
845 * \param label The buffer holding the custom label to use.
846 * This must be a readable buffer of length \p label_len
847 * Bytes. It may be \c NULL if \p label_len is \c 0.
848 * \param label_len The length of the label in Bytes.
849 * \param olen The address at which to store the length of
850 * the plaintext. This must not be \c NULL.
851 * \param input The ciphertext buffer. This must be a readable buffer
852 * of length \c ctx->len Bytes. For example, \c 256 Bytes
853 * for an 2048-bit RSA modulus.
854 * \param output The buffer used to hold the plaintext. This must
855 * be a writable buffer of length \p output_max_len Bytes.
856 * \param output_max_len The length in Bytes of the output buffer \p output.
857 *
858 * \return \c 0 on success.
859 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
860 */
862 int (*f_rng)(void *, unsigned char *, size_t),
863 void *p_rng,
864 int mode,
865 const unsigned char *label, size_t label_len,
866 size_t *olen,
867 const unsigned char *input,
868 unsigned char *output,
869 size_t output_max_len );
870
871/**
872 * \brief This function performs a private RSA operation to sign
873 * a message digest using PKCS#1.
874 *
875 * It is the generic wrapper for performing a PKCS#1
876 * signature using the \p mode from the context.
877 *
878 * \note The \p sig buffer must be as large as the size
879 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
880 *
881 * \note For PKCS#1 v2.1 encoding, see comments on
882 * mbedtls_rsa_rsassa_pss_sign() for details on
883 * \p md_alg and \p hash_id.
884 *
885 * \deprecated It is deprecated and discouraged to call this function
886 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
887 * are likely to remove the \p mode argument and have it
888 * implicitly set to #MBEDTLS_RSA_PRIVATE.
889 *
890 * \note Alternative implementations of RSA need not support
891 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
892 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
893 *
894 * \param ctx The initialized RSA context to use.
895 * \param f_rng The RNG function to use. If the padding mode is PKCS#1 v2.1,
896 * this must be provided. If the padding mode is PKCS#1 v1.5 and
897 * \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding
898 * and should be provided; see mbedtls_rsa_private() for more
899 * more. It is ignored otherwise.
900 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
901 * if \p f_rng is \c NULL or doesn't need a context argument.
902 * \param mode The mode of operation. This must be either
903 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
904 * \param md_alg The message-digest algorithm used to hash the original data.
905 * Use #MBEDTLS_MD_NONE for signing raw data.
906 * \param hashlen The length of the message digest.
907 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
908 * \param hash The buffer holding the message digest or raw data.
909 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
910 * buffer of length \p hashlen Bytes. If \p md_alg is not
911 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
912 * the size of the hash corresponding to \p md_alg.
913 * \param sig The buffer to hold the signature. This must be a writable
914 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
915 * for an 2048-bit RSA modulus. A buffer length of
916 * #MBEDTLS_MPI_MAX_SIZE is always safe.
917 *
918 * \return \c 0 if the signing operation was successful.
919 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
920 */
922 int (*f_rng)(void *, unsigned char *, size_t),
923 void *p_rng,
924 int mode,
925 mbedtls_md_type_t md_alg,
926 unsigned int hashlen,
927 const unsigned char *hash,
928 unsigned char *sig );
929
930/**
931 * \brief This function performs a PKCS#1 v1.5 signature
932 * operation (RSASSA-PKCS1-v1_5-SIGN).
933 *
934 * \deprecated It is deprecated and discouraged to call this function
935 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
936 * are likely to remove the \p mode argument and have it
937 * implicitly set to #MBEDTLS_RSA_PRIVATE.
938 *
939 * \note Alternative implementations of RSA need not support
940 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
941 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
942 *
943 * \param ctx The initialized RSA context to use.
944 * \param f_rng The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE,
945 * this is used for blinding and should be provided; see
946 * mbedtls_rsa_private() for more. If \p mode is
947 * #MBEDTLS_RSA_PUBLIC, it is ignored.
948 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
949 * if \p f_rng is \c NULL or doesn't need a context argument.
950 * \param mode The mode of operation. This must be either
951 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
952 * \param md_alg The message-digest algorithm used to hash the original data.
953 * Use #MBEDTLS_MD_NONE for signing raw data.
954 * \param hashlen The length of the message digest.
955 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
956 * \param hash The buffer holding the message digest or raw data.
957 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
958 * buffer of length \p hashlen Bytes. If \p md_alg is not
959 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
960 * the size of the hash corresponding to \p md_alg.
961 * \param sig The buffer to hold the signature. This must be a writable
962 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
963 * for an 2048-bit RSA modulus. A buffer length of
964 * #MBEDTLS_MPI_MAX_SIZE is always safe.
965 *
966 * \return \c 0 if the signing operation was successful.
967 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
968 */
970 int (*f_rng)(void *, unsigned char *, size_t),
971 void *p_rng,
972 int mode,
973 mbedtls_md_type_t md_alg,
974 unsigned int hashlen,
975 const unsigned char *hash,
976 unsigned char *sig );
977
978/**
979 * \brief This function performs a PKCS#1 v2.1 PSS signature
980 * operation (RSASSA-PSS-SIGN).
981 *
982 * \note The \p hash_id in the RSA context is the one used for the
983 * encoding. \p md_alg in the function call is the type of hash
984 * that is encoded. According to <em>RFC-3447: Public-Key
985 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
986 * Specifications</em> it is advised to keep both hashes the
987 * same.
988 *
989 * \note This function always uses the maximum possible salt size,
990 * up to the length of the payload hash. This choice of salt
991 * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1
992 * v2.2) §9.1.1 step 3. Furthermore this function enforces a
993 * minimum salt size which is the hash size minus 2 bytes. If
994 * this minimum size is too large given the key size (the salt
995 * size, plus the hash size, plus 2 bytes must be no more than
996 * the key size in bytes), this function returns
997 * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA.
998 *
999 * \deprecated It is deprecated and discouraged to call this function
1000 * in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library
1001 * are likely to remove the \p mode argument and have it
1002 * implicitly set to #MBEDTLS_RSA_PRIVATE.
1003 *
1004 * \note Alternative implementations of RSA need not support
1005 * mode being set to #MBEDTLS_RSA_PUBLIC and might instead
1006 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
1007 *
1008 * \param ctx The initialized RSA context to use.
1009 * \param f_rng The RNG function. It must not be \c NULL.
1010 * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL
1011 * if \p f_rng doesn't need a context argument.
1012 * \param mode The mode of operation. This must be either
1013 * #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated).
1014 * \param md_alg The message-digest algorithm used to hash the original data.
1015 * Use #MBEDTLS_MD_NONE for signing raw data.
1016 * \param hashlen The length of the message digest.
1017 * Ths is only used if \p md_alg is #MBEDTLS_MD_NONE.
1018 * \param hash The buffer holding the message digest or raw data.
1019 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1020 * buffer of length \p hashlen Bytes. If \p md_alg is not
1021 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1022 * the size of the hash corresponding to \p md_alg.
1023 * \param sig The buffer to hold the signature. This must be a writable
1024 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1025 * for an 2048-bit RSA modulus. A buffer length of
1026 * #MBEDTLS_MPI_MAX_SIZE is always safe.
1027 *
1028 * \return \c 0 if the signing operation was successful.
1029 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1030 */
1032 int (*f_rng)(void *, unsigned char *, size_t),
1033 void *p_rng,
1034 int mode,
1035 mbedtls_md_type_t md_alg,
1036 unsigned int hashlen,
1037 const unsigned char *hash,
1038 unsigned char *sig );
1039
1040/**
1041 * \brief This function performs a public RSA operation and checks
1042 * the message digest.
1043 *
1044 * This is the generic wrapper for performing a PKCS#1
1045 * verification using the mode from the context.
1046 *
1047 * \note For PKCS#1 v2.1 encoding, see comments on
1048 * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and
1049 * \p hash_id.
1050 *
1051 * \deprecated It is deprecated and discouraged to call this function
1052 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1053 * are likely to remove the \p mode argument and have it
1054 * set to #MBEDTLS_RSA_PUBLIC.
1055 *
1056 * \note Alternative implementations of RSA need not support
1057 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1058 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
1059 *
1060 * \param ctx The initialized RSA public key context to use.
1061 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1062 * this is used for blinding and should be provided; see
1063 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
1064 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1065 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1066 * \param mode The mode of operation. This must be either
1067 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
1068 * \param md_alg The message-digest algorithm used to hash the original data.
1069 * Use #MBEDTLS_MD_NONE for signing raw data.
1070 * \param hashlen The length of the message digest.
1071 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1072 * \param hash The buffer holding the message digest or raw data.
1073 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1074 * buffer of length \p hashlen Bytes. If \p md_alg is not
1075 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1076 * the size of the hash corresponding to \p md_alg.
1077 * \param sig The buffer holding the signature. This must be a readable
1078 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1079 * for an 2048-bit RSA modulus.
1080 *
1081 * \return \c 0 if the verify operation was successful.
1082 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1083 */
1085 int (*f_rng)(void *, unsigned char *, size_t),
1086 void *p_rng,
1087 int mode,
1088 mbedtls_md_type_t md_alg,
1089 unsigned int hashlen,
1090 const unsigned char *hash,
1091 const unsigned char *sig );
1092
1093/**
1094 * \brief This function performs a PKCS#1 v1.5 verification
1095 * operation (RSASSA-PKCS1-v1_5-VERIFY).
1096 *
1097 * \deprecated It is deprecated and discouraged to call this function
1098 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1099 * are likely to remove the \p mode argument and have it
1100 * set to #MBEDTLS_RSA_PUBLIC.
1101 *
1102 * \note Alternative implementations of RSA need not support
1103 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1104 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
1105 *
1106 * \param ctx The initialized RSA public key context to use.
1107 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1108 * this is used for blinding and should be provided; see
1109 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
1110 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1111 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1112 * \param mode The mode of operation. This must be either
1113 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
1114 * \param md_alg The message-digest algorithm used to hash the original data.
1115 * Use #MBEDTLS_MD_NONE for signing raw data.
1116 * \param hashlen The length of the message digest.
1117 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1118 * \param hash The buffer holding the message digest or raw data.
1119 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1120 * buffer of length \p hashlen Bytes. If \p md_alg is not
1121 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1122 * the size of the hash corresponding to \p md_alg.
1123 * \param sig The buffer holding the signature. This must be a readable
1124 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1125 * for an 2048-bit RSA modulus.
1126 *
1127 * \return \c 0 if the verify operation was successful.
1128 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1129 */
1131 int (*f_rng)(void *, unsigned char *, size_t),
1132 void *p_rng,
1133 int mode,
1134 mbedtls_md_type_t md_alg,
1135 unsigned int hashlen,
1136 const unsigned char *hash,
1137 const unsigned char *sig );
1138
1139/**
1140 * \brief This function performs a PKCS#1 v2.1 PSS verification
1141 * operation (RSASSA-PSS-VERIFY).
1142 *
1143 * The hash function for the MGF mask generating function
1144 * is that specified in the RSA context.
1145 *
1146 * \note The \p hash_id in the RSA context is the one used for the
1147 * verification. \p md_alg in the function call is the type of
1148 * hash that is verified. According to <em>RFC-3447: Public-Key
1149 * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography
1150 * Specifications</em> it is advised to keep both hashes the
1151 * same. If \p hash_id in the RSA context is unset,
1152 * the \p md_alg from the function call is used.
1153 *
1154 * \deprecated It is deprecated and discouraged to call this function
1155 * in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library
1156 * are likely to remove the \p mode argument and have it
1157 * implicitly set to #MBEDTLS_RSA_PUBLIC.
1158 *
1159 * \note Alternative implementations of RSA need not support
1160 * mode being set to #MBEDTLS_RSA_PRIVATE and might instead
1161 * return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED.
1162 *
1163 * \param ctx The initialized RSA public key context to use.
1164 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1165 * this is used for blinding and should be provided; see
1166 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
1167 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1168 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1169 * \param mode The mode of operation. This must be either
1170 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated).
1171 * \param md_alg The message-digest algorithm used to hash the original data.
1172 * Use #MBEDTLS_MD_NONE for signing raw data.
1173 * \param hashlen The length of the message digest.
1174 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1175 * \param hash The buffer holding the message digest or raw data.
1176 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1177 * buffer of length \p hashlen Bytes. If \p md_alg is not
1178 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1179 * the size of the hash corresponding to \p md_alg.
1180 * \param sig The buffer holding the signature. This must be a readable
1181 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1182 * for an 2048-bit RSA modulus.
1183 *
1184 * \return \c 0 if the verify operation was successful.
1185 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1186 */
1188 int (*f_rng)(void *, unsigned char *, size_t),
1189 void *p_rng,
1190 int mode,
1191 mbedtls_md_type_t md_alg,
1192 unsigned int hashlen,
1193 const unsigned char *hash,
1194 const unsigned char *sig );
1195
1196/**
1197 * \brief This function performs a PKCS#1 v2.1 PSS verification
1198 * operation (RSASSA-PSS-VERIFY).
1199 *
1200 * The hash function for the MGF mask generating function
1201 * is that specified in \p mgf1_hash_id.
1202 *
1203 * \note The \p sig buffer must be as large as the size
1204 * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used.
1205 *
1206 * \note The \p hash_id in the RSA context is ignored.
1207 *
1208 * \param ctx The initialized RSA public key context to use.
1209 * \param f_rng The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE,
1210 * this is used for blinding and should be provided; see
1211 * mbedtls_rsa_private() for more. Otherwise, it is ignored.
1212 * \param p_rng The RNG context to be passed to \p f_rng. This may be
1213 * \c NULL if \p f_rng is \c NULL or doesn't need a context.
1214 * \param mode The mode of operation. This must be either
1215 * #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE.
1216 * \param md_alg The message-digest algorithm used to hash the original data.
1217 * Use #MBEDTLS_MD_NONE for signing raw data.
1218 * \param hashlen The length of the message digest.
1219 * This is only used if \p md_alg is #MBEDTLS_MD_NONE.
1220 * \param hash The buffer holding the message digest or raw data.
1221 * If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable
1222 * buffer of length \p hashlen Bytes. If \p md_alg is not
1223 * #MBEDTLS_MD_NONE, it must be a readable buffer of length
1224 * the size of the hash corresponding to \p md_alg.
1225 * \param mgf1_hash_id The message digest used for mask generation.
1226 * \param expected_salt_len The length of the salt used in padding. Use
1227 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length.
1228 * \param sig The buffer holding the signature. This must be a readable
1229 * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes
1230 * for an 2048-bit RSA modulus.
1231 *
1232 * \return \c 0 if the verify operation was successful.
1233 * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure.
1234 */
1236 int (*f_rng)(void *, unsigned char *, size_t),
1237 void *p_rng,
1238 int mode,
1239 mbedtls_md_type_t md_alg,
1240 unsigned int hashlen,
1241 const unsigned char *hash,
1242 mbedtls_md_type_t mgf1_hash_id,
1243 int expected_salt_len,
1244 const unsigned char *sig );
1245
1246/**
1247 * \brief This function copies the components of an RSA context.
1248 *
1249 * \param dst The destination context. This must be initialized.
1250 * \param src The source context. This must be initialized.
1251 *
1252 * \return \c 0 on success.
1253 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure.
1254 */
1256
1257/**
1258 * \brief This function frees the components of an RSA key.
1259 *
1260 * \param ctx The RSA context to free. May be \c NULL, in which case
1261 * this function is a no-op. If it is not \c NULL, it must
1262 * point to an initialized RSA context.
1263 */
1265
1266#if defined(MBEDTLS_SELF_TEST)
1267
1268/**
1269 * \brief The RSA checkup routine.
1270 *
1271 * \return \c 0 on success.
1272 * \return \c 1 on failure.
1273 */
1274int mbedtls_rsa_self_test( int verbose );
1275
1276#endif /* MBEDTLS_SELF_TEST */
1277
1278#ifdef __cplusplus
1279}
1280#endif
1281
1282/// \}
1283/// \}
1284
1285#endif /* rsa.h */
Multi-precision integer library.
Configuration options (set of defines)
mbedtls_md_type_t
Supported message digests.
Definition: md.h:64
int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
This function adds the message padding, then performs an RSA operation.
int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
This function completes an RSA context from a set of imported core parameters.
int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
This function performs a public RSA operation and checks the message digest.
void mbedtls_rsa_init(mbedtls_rsa_context *ctx, int padding, int hash_id)
This function initializes an RSA context.
int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
This function performs a PKCS#1 v1.5 verification operation (RSASSA-PKCS1-v1_5-VERIFY).
int mbedtls_rsa_import(mbedtls_rsa_context *ctx, const mbedtls_mpi *N, const mbedtls_mpi *P, const mbedtls_mpi *Q, const mbedtls_mpi *D, const mbedtls_mpi *E)
This function imports a set of core parameters into an RSA context.
int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv)
This function checks a public-private RSA key pair.
int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx, unsigned char const *N, size_t N_len, unsigned char const *P, size_t P_len, unsigned char const *Q, size_t Q_len, unsigned char const *D, size_t D_len, unsigned char const *E, size_t E_len)
This function imports core RSA parameters, in raw big-endian binary format, into an RSA context.
int mbedtls_rsa_private(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, const unsigned char *input, unsigned char *output)
This function performs an RSA private key operation.
int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, unsigned int nbits, int exponent)
This function generates an RSA keypair.
int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
This function performs a private RSA operation to sign a message digest using PKCS#1.
size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
This function retrieves the length of RSA modulus in Bytes.
int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
This function performs a PKCS#1 v1.5 encryption operation (RSAES-PKCS1-v1_5-ENCRYPT).
int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
This function performs a PKCS#1 v1.5 decryption operation (RSAES-PKCS1-v1_5-DECRYPT).
int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
This function performs an RSA operation, then removes the message padding.
int mbedtls_rsa_export(const mbedtls_rsa_context *ctx, mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q, mbedtls_mpi *D, mbedtls_mpi *E)
This function exports the core parameters of an RSA key.
int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx, unsigned char *N, size_t N_len, unsigned char *P, size_t P_len, unsigned char *Q, size_t Q_len, unsigned char *D, size_t D_len, unsigned char *E, size_t E_len)
This function exports core parameters of an RSA key in raw big-endian binary format.
int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
This function copies the components of an RSA context.
int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
This function performs a PKCS#1 v2.1 PSS verification operation (RSASSA-PSS-VERIFY).
void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
This function frees the components of an RSA key.
int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx, mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
This function exports CRT parameters of a private RSA key.
int mbedtls_rsa_public(mbedtls_rsa_context *ctx, const unsigned char *input, unsigned char *output)
This function performs an RSA public key operation.
int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
This function checks if a context contains an RSA private key and perform basic consistency checks.
int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, mbedtls_md_type_t mgf1_hash_id, int expected_salt_len, const unsigned char *sig)
This function performs a PKCS#1 v2.1 PSS verification operation (RSASSA-PSS-VERIFY).
int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, const unsigned char *label, size_t label_len, size_t ilen, const unsigned char *input, unsigned char *output)
This function performs a PKCS#1 v2.1 OAEP encryption operation (RSAES-OAEP-ENCRYPT).
int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
This function checks if a context contains at least an RSA public key.
void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding, int hash_id)
This function sets padding for an already initialized RSA context.
int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
This function performs a PKCS#1 v2.1 PSS signature operation (RSASSA-PSS-SIGN).
int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
This function performs a PKCS#1 v1.5 signature operation (RSASSA-PKCS1-v1_5-SIGN).
int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, const unsigned char *label, size_t label_len, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
This function performs a PKCS#1 v2.1 OAEP decryption operation (RSAES-OAEP-DECRYPT).
This file contains the generic message-digest wrapper.
This file provides an API for the RSA public-key cryptosystem.
MPI structure.
Definition: bignum.h:185
The RSA context structure.
Definition: rsa.h:106
mbedtls_mpi N
Definition: rsa.h:110
mbedtls_mpi P
Definition: rsa.h:114
mbedtls_mpi Vi
Definition: rsa.h:126
mbedtls_mpi D
Definition: rsa.h:113
mbedtls_mpi RP
Definition: rsa.h:123
mbedtls_mpi Vf
Definition: rsa.h:127
mbedtls_mpi DQ
Definition: rsa.h:118
size_t len
Definition: rsa.h:108
mbedtls_mpi DP
Definition: rsa.h:117
mbedtls_mpi QP
Definition: rsa.h:119
mbedtls_mpi Q
Definition: rsa.h:115
mbedtls_mpi RN
Definition: rsa.h:121
mbedtls_mpi RQ
Definition: rsa.h:124
mbedtls_mpi E
Definition: rsa.h:111
Threading abstraction layer.